Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
Add nov-search-forward and nov-search-backwards
Browse files Browse the repository at this point in the history
  • Loading branch information
freesteph committed Dec 28, 2018
1 parent 3bb7a40 commit 3e9fc52
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Features:
- Basic navigation (jump to TOC, previous/next chapter)
- Remembering and restoring the last read position
- Jump to next chapter when scrolling beyond end
- Search backward and forward
- Renders EPUB2 (.ncx) and EPUB3 (<nav>) TOCs
- Hyperlinks to internal and external targets
- Supports textual and image documents
Expand Down
36 changes: 36 additions & 0 deletions nov.el
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,8 @@ Each alist item consists of the identifier and full path."
(define-key map (kbd "]") 'nov-next-document)
(define-key map (kbd "p") 'nov-previous-document)
(define-key map (kbd "[") 'nov-previous-document)
(define-key map (kbd "s") 'nov-search-forward)
(define-key map (kbd "b") 'nov-search-backward)
(define-key map (kbd "t") 'nov-goto-toc)
(define-key map (kbd "RET") 'nov-browse-url)
(define-key map (kbd "<follow-link>") 'mouse-face)
Expand Down Expand Up @@ -663,6 +665,40 @@ Saving is only done if `nov-save-place-file' is set."
(>= index 0)
(< index (length documents))))

(defun nov--search-direction (query direction)
"Search document for QUERY in the specified DIRECTION. DIRECTION should be either :forward or :backward."
(cl-multiple-value-setq (search-fn next-page-fn buffer-edge-fn)
(or (and (eq direction :forward)
'(search-forward
nov-next-document
beginning-of-buffer))
'(search-backward
nov-previous-document
end-of-buffer)))
(lexical-let* ((original-index nov-documents-index)
(original-point (point))
(found (funcall search-fn query nil t)))
(while (and
(not found)
(funcall next-page-fn))
(funcall buffer-edge-fn)
(setf found (funcall search-fn query nil t)))
(unless found
(setf nov-documents-index original-index)
(nov-render-document)
(setf (point) original-point)
(message "No match found for %s." query))))

(defun nov-search-backward (query)
"Search the document backward for QUERY."
(interactive "sSearch query backward: ")
(nov--search-direction query :backward))

(defun nov-search-forward (query)
"Search the document forward for QUERY."
(interactive "sSearch query: ")
(nov--search-direction query :forward))

;;;###autoload
(define-derived-mode nov-mode special-mode "EPUB"
"Major mode for reading EPUB documents"
Expand Down

0 comments on commit 3e9fc52

Please sign in to comment.