Skip to content

Commit

Permalink
Fix revert buffer going to start of the buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenceWarne committed Aug 17, 2024
1 parent 29f6739 commit 94c3f59
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
52 changes: 31 additions & 21 deletions finito.el
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
;; Version: 0.2.0
;; Keywords: outlines
;; URL: https://github.com/LaurenceWarne/finito.el
;; Package-Requires: ((emacs "27.1") (dash "2.17.0") (request "0.3.2") (f "0.2.0") (s "1.12.0") (transient "0.3.0") (graphql "0.1.1") (async "1.9.3"))
;; Package-Requires: ((emacs "27.1") (dash "2.19.1") (request "0.3.2") (f "0.2.0") (s "1.12.0") (transient "0.3.0") (graphql "0.1.1") (async "1.9.3"))

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -201,7 +201,7 @@ as a symbol."
:sync sync))

(defun finito--process-books-data (data init-obj)
"Insert the books data DATA into a buffer.
"Insert the books data DATA into a buffer, and return the buffer.
Use INIT-OBJ, an instance of `finito-buffer-info' to initialize the buffer."
(make-directory finito-img-cache-directory t)
Expand All @@ -225,7 +225,7 @@ Use INIT-OBJ, an instance of `finito-buffer-info' to initialize the buffer."
(proc-books))))))

(defun finito--prepare-buffer (init-obj callback)
"Prepare a finito buffer.
"Prepare a finito buffer and return it.
Prepare a finito buffer using INIT-OBJ which should be a `finito-buffer-info'
instance, then call CALLBACK with an ewoc, which should use it to insert text
Expand All @@ -247,7 +247,8 @@ in some way, and then apply some final configuration to the buffer."
(toggle-truncate-lines -1))
(goto-char (point-min))
(org-display-inline-images)))
(display-buffer buf '(display-buffer-same-window . nil))))
(display-buffer buf '(display-buffer-same-window . nil))
buf))

(defun finito--download-images (books)
"Download the images for BOOKS."
Expand Down Expand Up @@ -338,24 +339,26 @@ BOOK-ALIST should be of the form returned by `finito--create-book-alist'."
(browse-url
(concat "https://openlibrary.org/isbn/" (alist-get 'isbn book-alist))))

(defun finito--open-specified-collection (collection &optional sync offset)
"Open the collection COLLECTION in a view buffer.
(defun finito--open-specified-collection (collection &optional sync offset callback)
"Open the collection COLLECTION in a view buffer and call CALLBACK.
If SYNC it non-nil, perform all actions synchronously.
OFFSET is the offset of the books in collection."
(finito--make-request
(finito--collection-request-plist
collection (and (finito--use-pagination) finito-collection-books-limit) (or offset 0))
(lambda (response)
(finito--process-books-data
(cdar response)
(finito-collection-buffer-info
:title collection
:mode #'finito-collection-view-mode
:buf-name (concat "Collection: " collection)
:buf-name-unique t
:books-offset (or offset 0)
:total-books (or (ignore-errors (cdadar (last response))) 0))))
(with-current-buffer
(finito--process-books-data
(cdar response)
(finito-collection-buffer-info
:title collection
:mode #'finito-collection-view-mode
:buf-name (concat "Collection: " collection)
:buf-name-unique t
:books-offset (or offset 0)
:total-books (or (ignore-errors (cdadar (last response))) 0)))
(when callback (funcall callback))))
:sync sync))

(defun finito--remove-book-region ()
Expand Down Expand Up @@ -787,11 +790,15 @@ maximum of MAX-RESULTS results."
0))
(request-backend 'url-retrieve))
(kill-current-buffer)
(finito--open-specified-collection collection t)
(unless (ignore-errors (ewoc-goto-node
finito--ewoc (ewoc-nth finito--ewoc node-idx)))
(goto-char (min old-point (point-max))))
(message "Refreshed collection '%s'" collection))))
(finito--open-specified-collection
collection
t
nil
(lambda ()
(unless (ignore-errors (ewoc-goto-node
finito--ewoc (ewoc-nth finito--ewoc node-idx)))
(goto-char (min old-point (point-max))))
(message "Refreshed collection '%s'" collection))))))

(defun finito-search-revert (&optional _ignore-auto _noconfirm)
"Refresh the current search, _IGNORE-AUTO and _NOCONFIRM are ignored."
Expand Down Expand Up @@ -830,6 +837,7 @@ Note this overwrites any existing review."
(interactive)
(finito--wait-for-server-then
(let* ((book (finito--book-at-point))
(coll-buf (current-buffer))
(buf (generate-new-buffer (format "%s Review" (alist-get 'title book)))))
(with-current-buffer buf
(local-set-key
Expand All @@ -840,7 +848,9 @@ Note this overwrites any existing review."
book
(buffer-substring-no-properties (point-min) (point-max)))
(lambda (&rest _) (format "Added review for '%s'" (alist-get 'title book))))
(kill-buffer-and-window))))
(kill-buffer-and-window)
(with-current-buffer coll-buf
(revert-buffer)))))
(pop-to-buffer buf '(display-buffer-below-selected . nil))
(message "Use C-c C-c to submit the review"))))

Expand Down
16 changes: 11 additions & 5 deletions test/finito-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
(img-uri . "https://random-url")
(image-file-name . "cache/directory/footitleisbn.jpeg")
(rating . nil)
(review . nil)
(started-reading . nil)
(last-read . nil)))

Expand Down Expand Up @@ -190,6 +191,7 @@ Occurrences of `.buffer-text' will be replaced by:
(isbn . "isbn")
(thumbnailUri . "https://random-url")
(rating . 3)
(review . "some review")
(startedReading . "some-date")
(lastRead . "some other date"))))
(expect (finito--create-book-alist response-alist)
Expand All @@ -201,6 +203,7 @@ Occurrences of `.buffer-text' will be replaced by:
(img-uri . "https://random-url")
(image-file-name . "cache/directory/footitleisbn.jpeg")
(rating . 3)
(review . "some review")
(started-reading . "some-date")
(last-read . "some other date")))))

Expand All @@ -212,9 +215,9 @@ Occurrences of `.buffer-text' will be replaced by:
(isbn . "isbn")
(thumbnailUri . "https://random-url")
(rating . nil)
(review . nil)
(startedReading . nil)
(lastRead . nil)
(review . nil))))
(lastRead . nil))))
(expect (finito--create-book-alist response-alist)
:to-equal
finito--stub-book))))
Expand Down Expand Up @@ -420,7 +423,8 @@ GNU Emacs is awesome!
(funcall callback)))

(spy-on 'finito--collection-request-plist :and-call-through)
(spy-on 'finito--process-books-data :and-return-value nil))
(spy-on 'finito--process-books-data :and-call-fake
(lambda (_date _init-obj) (get-buffer-create "finito test"))))

(it "opens collection"
(finito-open-collection)
Expand All @@ -444,7 +448,8 @@ GNU Emacs is awesome!
(funcall callback)))

(spy-on 'finito--collection-request-plist :and-call-through)
(spy-on 'finito--process-books-data :and-return-value nil))
(spy-on 'finito--process-books-data :and-call-fake
(lambda (_date _init-obj) (get-buffer-create "finito test"))))

(it "opens my books collection"
(finito-open-my-books-collection)
Expand All @@ -467,7 +472,8 @@ GNU Emacs is awesome!
(funcall callback)))

(spy-on 'finito--collection-request-plist :and-call-through)
(spy-on 'finito--process-books-data :and-return-value nil))
(spy-on 'finito--process-books-data :and-call-fake
(lambda (_date _init-obj) (get-buffer-create "finito test"))))

(it "opens currently reading collection"
(finito-open-currently-reading-collection)
Expand Down

0 comments on commit 94c3f59

Please sign in to comment.