From 7e0b165e5baa50ef80cd0c4cfd0ea4d0325465b9 Mon Sep 17 00:00:00 2001 From: Kaushal Modi Date: Thu, 2 Apr 2015 12:36:05 -0400 Subject: [PATCH 1/4] Code optimization; new functions - Use of `funcall` to call the right command based on if the user uses `pdf-tools` package or not - Refine `interleave--doc-view-kill-proc-and-buffer` fn so that the `doc-view-kill-proc` is called only if the major mode is `doc-view-mode`. - Added `*interleave--pdf-buffer*`; renamed *interleave--org-buf* to *interleave--org-buffer* - Use of `switch-to-buffer` and `switch-to-buffer-other-window` instead of `(other-window 1)` for more accuracy (in the event the user has opened a buffer other than the pdf or org notes file in the "other" window). - Added commands to synchronize the pdf page with the page displayed in current org notes buffer, to sync the pdf page to the next/previous notes page: - interleave--sync-pdf-page-current - interleave--sync-pdf-page-previous - interleave--sync-pdf-page-next - Creates variable handles `interleave-map` and `interleave-pdf-mode-map` so a user can customize the key bindings in them without having to modify the source. --- interleave.el | 283 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 173 insertions(+), 110 deletions(-) diff --git a/interleave.el b/interleave.el index 9ed2d7b..996ce61 100644 --- a/interleave.el +++ b/interleave.el @@ -2,7 +2,8 @@ ;; Author: Sebastian Christ ;; URL: https://github.com/rudolfochrist/interleave -;; Version: 0.2.0 +;; Package-Version: 20150402.206 +;; Version: 0.2.2 ;; This file is not part of GNU Emacs @@ -38,28 +39,48 @@ ;; M-x interleave ;; ;; To insert a note for a page, type i. -;; Navigation is the same as in `doc-view-mode'. +;; Navigation is the same as in `doc-view-mode'/`pdf-view-mode'. ;;; Code: (require 'org) (require 'doc-view) -;; Redefining `doc-view-kill-proc-and-buffer' as `interleave--doc-view-kill-proc-and-buffer' +;; Redefining `doc-view-kill-proc-and-buffer' as `interleave--pdf-kill-proc-and-buffer' ;; because this function is obsolete in emacs 25.1 onwards. -(defun interleave--doc-view-kill-proc-and-buffer () +(defun interleave--pdf-kill-proc-and-buffer () "Kill the current converter process and buffer." (interactive) - (doc-view-kill-proc) - (when (eq major-mode 'doc-view-mode) + (when (derived-mode-p 'doc-view-mode) + (doc-view-kill-proc)) + (when (or (derived-mode-p 'doc-view-mode) + (derived-mode-p 'pdf-view-mode)) (kill-buffer (current-buffer)))) -(defvar *interleave--org-buf* nil "The Org Buffer") +(defvar *interleave--org-buffer* nil "Org notes buffer") +(defvar *interleave--pdf-buffer* nil "PDF buffer associated with the notes buffer") (defvar interleave--window-configuration nil "Variable to store the window configuration before interleave mode was enabled.") +(if (featurep 'pdf-view) ; if `pdf-tools' is installed + (progn + ;; Function wrapper for the macro `pdf-view-current-page' + (defconst interleave--pdf-current-page-fn (lambda () (pdf-view-current-page))) + (defconst interleave--pdf-next-page-fn #'pdf-view-next-page) + (defconst interleave--pdf-previous-page-fn #'pdf-view-previous-page) + (defconst interleave--pdf-goto-page-fn #'pdf-view-goto-page) + (defconst interleave--pdf-scroll-up-or-next-page-fn #'pdf-view-scroll-up-or-next-page) + (defconst interleave--pdf-scroll-down-or-previous-page-fn #'pdf-view-scroll-down-or-previous-page)) + (progn + (defconst interleave--pdf-current-page-fn #'doc-view-current-page) + (defconst interleave--pdf-next-page-fn #'doc-view-next-page) + (defconst interleave--pdf-previous-page-fn #'doc-view-previous-page) + (defconst interleave--pdf-goto-page-fn #'doc-view-goto-page) + (defconst interleave--pdf-scroll-up-or-next-page-fn #'doc-view-scroll-up-or-next-page) + (defconst interleave--pdf-scroll-down-or-previous-page-fn #'doc-view-scroll-down-or-previous-page))) + (make-variable-buffer-local (defvar *interleave--page-marker* 0 "Caches the current page while scrolling")) @@ -74,7 +95,8 @@ enabled.") (match-string 1))))) (defun interleave--open-file (split-window) - "Opens the interleave pdf file in doc-view besides the notes buffer. + "Opens the interleave pdf file in `doc-view-mode'/`pdf-view-mode' besides the +notes buffer. SPLIT-WINDOW is a function that actually splits the window, so it must be either `split-window-right' or `split-window-below'." @@ -91,7 +113,7 @@ SPLIT-WINDOW is a function that actually splits the window, so it must be either (defun interleave--go-to-page-note (page) "Searches the notes buffer for an headline with the 'interleave_page_note' property set to PAGE. It narrows the subtree when found." - (with-current-buffer *interleave--org-buf* + (with-current-buffer *interleave--org-buffer* (save-excursion (widen) (goto-char (point-min)) @@ -100,17 +122,54 @@ to PAGE. It narrows the subtree when found." (org-show-entry) t)))) -(defun interleave--switch-to-other-window () - (other-window 1) - (goto-char (point-max)) - (redisplay) - ;; Insert a new line if not already on a new line - (when (not (looking-back "^ *")) - (org-return))) +(defun interleave--go-to-next-page () + "Go to the next page in PDF. Look up for available notes." + (interactive) + (funcall interleave--pdf-next-page-fn) + (interleave--go-to-page-note (funcall interleave--pdf-current-page-fn))) + +(defun interleave--go-to-previous-page () + "Go to the previous page in PDF. Look up for available notes." + (interactive) + (funcall interleave--pdf-previous-page-fn) + (interleave--go-to-page-note (funcall interleave--pdf-current-page-fn))) + +(defun interleave--scroll-up () + "Scroll up the PDF. Look up for available notes." + (interactive) + (setq *interleave--page-marker* (funcall interleave--pdf-current-page-fn)) + (funcall interleave--pdf-scroll-up-or-next-page-fn) + (unless (= *interleave--page-marker* (funcall interleave--pdf-current-page-fn)) + (interleave--go-to-page-note (funcall interleave--pdf-current-page-fn)))) + +(defun interleave--scroll-down () + "Scroll down the PDF. Look up for available notes." + (interactive) + (setq *interleave--page-marker* (funcall interleave--pdf-current-page-fn)) + (funcall interleave--pdf-scroll-down-or-previous-page-fn) + (unless (= *interleave--page-marker* (funcall interleave--pdf-current-page-fn)) + (interleave--go-to-page-note (funcall interleave--pdf-current-page-fn)))) + +(defun interleave--switch-to-org-buffer (&optional insert-newline-maybe) + (if (or (derived-mode-p 'doc-view-mode) + (derived-mode-p 'pdf-view-mode)) + (switch-to-buffer-other-window *interleave--org-buffer*) + (switch-to-buffer *interleave--org-buffer*)) + (when insert-newline-maybe + (goto-char (point-max)) + (redisplay) + ;; Insert a new line if not already on a new line + (when (not (looking-back "^ *")) + (org-return)))) + +(defun interleave--switch-to-pdf-buffer () + (if (derived-mode-p 'org-mode) + (switch-to-buffer-other-window *interleave--pdf-buffer*) + (switch-to-buffer *interleave--pdf-buffer*))) (defun interleave--create-new-note (page) "Creates a new headline for the page PAGE." - (with-current-buffer *interleave--org-buf* + (with-current-buffer *interleave--org-buffer* (save-excursion (widen) (goto-char (point-max)) @@ -118,91 +177,76 @@ to PAGE. It narrows the subtree when found." (insert (format "Notes for page %d" page)) (org-set-property "interleave_page_note" (number-to-string page)) (org-narrow-to-subtree))) - (interleave--switch-to-other-window)) + (interleave--switch-to-org-buffer t)) -(if (featurep 'pdf-view) ; if `pdf-tools' is installed - (progn - (defun interleave--go-to-next-page () - "Go to the next page in PDF. Look up for available notes." - (interactive) - (pdf-view-next-page-command 1) - (interleave--go-to-page-note (pdf-view-current-page))) - - (defun interleave--go-to-previous-page () - "Go to the previous page in PDF. Look up for available notes." - (interactive) - (pdf-view-previous-page-command 1) - (interleave--go-to-page-note (pdf-view-current-page))) - - (defun interleave--scroll-up () - "Scroll up the PDF. Look up for available notes." - (interactive) - (setq *interleave--page-marker* (pdf-view-current-page)) - (pdf-view-scroll-up-or-next-page) - (unless (= *interleave--page-marker* (pdf-view-current-page)) - (interleave--go-to-page-note (pdf-view-current-page)))) - - (defun interleave--scroll-down () - "Scroll down the PDF. Look up for available notes." - (interactive) - (setq *interleave--page-marker* (pdf-view-current-page)) - (pdf-view-scroll-down-or-previous-page) - (unless (= *interleave--page-marker* (pdf-view-current-page)) - (interleave--go-to-page-note (pdf-view-current-page)))) - - (defun interleave--add-note () - "Add note for the current page. If there are already notes for this page, +(defun interleave--add-note () + "Add note for the current page. If there are already notes for this page, jump to the notes buffer." - (interactive) - (let ((page (pdf-view-current-page))) - (if (interleave--go-to-page-note page) - (interleave--switch-to-other-window) - (interleave--create-new-note page))))) - (progn ; if `pdf-tools' is NOT installed - (defun interleave--go-to-next-page () - "Go to the next page in PDF. Look up for available notes." - (interactive) - (doc-view-next-page) - (interleave--go-to-page-note (doc-view-current-page))) - - (defun interleave--go-to-previous-page () - "Go to the previous page in PDF. Look up for available notes." - (interactive) - (doc-view-previous-page) - (interleave--go-to-page-note (doc-view-current-page))) - - (defun interleave--scroll-up () - "Scroll up the PDF. Look up for available notes." - (interactive) - (setq *interleave--page-marker* (doc-view-current-page)) - (doc-view-scroll-up-or-next-page) - (unless (= *interleave--page-marker* (doc-view-current-page)) - (interleave--go-to-page-note (doc-view-current-page)))) - - (defun interleave--scroll-down () - "Scroll down the PDF. Look up for available notes." - (interactive) - (setq *interleave--page-marker* (doc-view-current-page)) - (doc-view-scroll-down-or-previous-page) - (unless (= *interleave--page-marker* (doc-view-current-page)) - (interleave--go-to-page-note (doc-view-current-page)))) - - (defun interleave--add-note () - "Add note for the current page. If there are already notes for this page, -jump to the notes buffer." - (interactive) - (let ((page (doc-view-current-page))) - (if (interleave--go-to-page-note page) - (interleave--switch-to-other-window) - (interleave--create-new-note page)))))) + (interactive) + (let ((page (funcall interleave--pdf-current-page-fn))) + (if (interleave--go-to-page-note page) + (interleave--switch-to-org-buffer t) + (interleave--create-new-note page)))) + +(defun interleave--sync-pdf-page-current () + "Synchronize the page in the pdf buffer to be the same as the page in +the current narrowed down notes view." + (interactive) + (interleave--switch-to-org-buffer) + (let ((pdf-page)) + (save-excursion + (org-narrow-to-subtree) + (goto-char (point-min)) + (search-forward-regexp "Notes for page \\(.*\\)") + (setq pdf-page (string-to-number (match-string 1)))) + (interleave--switch-to-pdf-buffer) + (funcall interleave--pdf-goto-page-fn pdf-page))) + +(defun interleave--sync-pdf-page-previous () + "Synchronize the page in the pdf buffer to be the same as the page in the +previous set of notes." + (interactive) + (interleave--switch-to-org-buffer) + (let ((pdf-page)) + (save-excursion + (widen) + (org-backward-heading-same-level 1) + (org-narrow-to-subtree) + (goto-char (point-min)) + (search-forward-regexp "Notes for page \\(.*\\)") + (setq pdf-page (string-to-number (match-string 1)))) + (interleave--switch-to-pdf-buffer) + (funcall interleave--pdf-goto-page-fn pdf-page))) + +(defun interleave--sync-pdf-page-next () + "Synchronize the page in the pdf buffer to be the same as the page in the +next set of notes." + (interactive) + (interleave--switch-to-org-buffer) + (let ((pdf-page)) + (save-excursion + (widen) + (org-forward-heading-same-level 1) + (org-narrow-to-subtree) + (goto-char (point-min)) + (search-forward-regexp "Notes for page \\(.*\\)") + (setq pdf-page (string-to-number (match-string 1)))) + (interleave--switch-to-pdf-buffer) + (funcall interleave--pdf-goto-page-fn pdf-page))) (defun interleave--quit () "Quit interleave mode." (interactive) - (with-current-buffer *interleave--org-buf* + (with-current-buffer *interleave--org-buffer* (widen) (interleave 0)) - (interleave--doc-view-kill-proc-and-buffer)) + (interleave--pdf-kill-proc-and-buffer)) + +;;; Interleave +;; Minor mode for the org file buffer containing notes + +(defvar interleave-map (make-sparse-keymap) + "Keymap while `interleave' is active in the org file buffer.") ;;;###autoload (define-minor-mode interleave @@ -225,32 +269,51 @@ Then start 'interleave' with M-x interleave To insert a note for a page, type 'i'. -Navigation is the same as in `doc-view-mode'." - :lighter " Interleave" +Navigation is the same as in `doc-view-mode'/`pdf-view-mode'." + :lighter " ≡" + :keymap interleave-map (if interleave - ;; Stuff to do when enabling `interleave' (progn + (message "Interleave enabled") (setq interleave--window-configuration (current-window-configuration)) - (setq *interleave--org-buf* (current-buffer)) - (interleave--open-file (or (and current-prefix-arg 'split-window-below) - 'split-window-right))) - ;; Stuff to do when disabling `interleave' + (setq *interleave--org-buffer* (current-buffer)) + (interleave--open-file (or (and current-prefix-arg #'split-window-below) + #'split-window-right))) (progn + (message "Interleave disabled") (set-window-configuration interleave--window-configuration)))) +;;; Interleave PDF Mode +;; Minor mode for the pdf file buffer associated with the notes + +(defvar interleave-pdf-mode-map (make-sparse-keymap) + "Keymap while `interleave-pdf-mode' is active in the pdf file buffer.") + ;;;###autoload (define-minor-mode interleave-pdf-mode "Interleave view for the pdf." :lighter " ≡" - :keymap (let ((map (make-sparse-keymap))) - (define-key map (kbd "n") #'interleave--go-to-next-page) - (define-key map (kbd "p") #'interleave--go-to-previous-page) - (define-key map (kbd "SPC") #'interleave--scroll-up) - (define-key map (kbd "S-SPC") #'interleave--scroll-down) - (define-key map (kbd "DEL") #'interleave--scroll-down) - (define-key map (kbd "i") #'interleave--add-note) - (define-key map (kbd "q") #'interleave--quit) - map)) + :keymap interleave-pdf-mode-map + (when interleave-pdf-mode + (progn + (setq *interleave--pdf-buffer* (current-buffer))))) + +;;; Key-bindings + +(define-key interleave-map (kbd "M-.") #'interleave--sync-pdf-page-current) +(define-key interleave-map (kbd "M-p") #'interleave--sync-pdf-page-previous) +(define-key interleave-map (kbd "M-n") #'interleave--sync-pdf-page-next) + +(define-key interleave-pdf-mode-map (kbd "n") #'interleave--go-to-next-page) +(define-key interleave-pdf-mode-map (kbd "p") #'interleave--go-to-previous-page) +(define-key interleave-pdf-mode-map (kbd "SPC") #'interleave--scroll-up) +(define-key interleave-pdf-mode-map (kbd "S-SPC") #'interleave--scroll-down) +(define-key interleave-pdf-mode-map (kbd "DEL") #'interleave--scroll-down) +(define-key interleave-pdf-mode-map (kbd "i") #'interleave--add-note) +(define-key interleave-pdf-mode-map (kbd "q") #'interleave--quit) +(define-key interleave-pdf-mode-map (kbd "M-.") #'interleave--sync-pdf-page-current) +(define-key interleave-pdf-mode-map (kbd "M-p") #'interleave--sync-pdf-page-previous) +(define-key interleave-pdf-mode-map (kbd "M-n") #'interleave--sync-pdf-page-next) (provide 'interleave) From 018bec4150e19b1e0e7f1d67327a514f37e2f04c Mon Sep 17 00:00:00 2001 From: Kaushal Modi Date: Thu, 2 Apr 2015 13:23:28 -0400 Subject: [PATCH 2/4] Fix: Wrap `doc-view-current-page' in `lambda' too - Other minor changes - Documentation fill column reduced - Slight rewording of Commentary and Usage sections --- interleave.el | 83 ++++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 38 deletions(-) diff --git a/interleave.el b/interleave.el index 996ce61..776f452 100644 --- a/interleave.el +++ b/interleave.el @@ -2,7 +2,6 @@ ;; Author: Sebastian Christ ;; URL: https://github.com/rudolfochrist/interleave -;; Package-Version: 20150402.206 ;; Version: 0.2.2 ;; This file is not part of GNU Emacs @@ -21,25 +20,28 @@ ;; see . ;;; Commentary: -;; -;; In the past, textbooks were sometimes published as /interleaved/ editions. That meant, each page -;; was followed by a blank page and the ambitious student/scholar had the ability to take his notes directly -;; in her copy of the textbook. Newton and Kant were prominent representatives of this technique. -;; Nowadays textbooks (or lecture material) come in PDF format. Although almost every PDF Reader has the ability to add some notes to the PDF itself, it is not as powerful as it could be. -;; This is what this minor mode tries to accomplish. It presents your PDF side by side to an [[http://orgmode.org][Org Mode]] buffer with you notes. -;; Narrowing down to just those passages that are relevant to this particular page in the document viewer. +;; In the past, textbooks were sometimes published as 'interleaved' editions. +;; That meant, each page was followed by a blank page and the ambitious student/ +;; scholar had the ability to take their notes directly in their copy of the +;; textbook. Newton and Kant were prominent representatives of this technique. + +;; Nowadays textbooks (or lecture material) come in PDF format. Although almost +;; every PDF Reader has the ability to add some notes to the PDF itself, it is +;; not as powerful as it could be. + +;; This is what this minor mode tries to accomplish. It presents your PDF side by +;; side to an [[http://orgmode.org][Org Mode]] buffer with your notes, narrowing +;; down to just those passages that are relevant to the particular page in the +;; document viewer. ;;; Usage: -;; -;; Create a Org file thath will keep your notes. In the Org headers section add + +;; - Create a Org file that will keep your notes. In the Org headers section, add ;; #+INTERLEAVE_PDF: /the/path/to/your/pdf.pdf -;; -;; Then start 'interleave' with -;; M-x interleave -;; -;; To insert a note for a page, type i. -;; Navigation is the same as in `doc-view-mode'/`pdf-view-mode'. +;; - Start `interleave' with `M-x interleave'. +;; - To insert a note for a page, type `i'. +;; - Navigation is the same as in `doc-view-mode'/`pdf-view-mode'." ;;; Code: @@ -61,8 +63,7 @@ (defvar *interleave--pdf-buffer* nil "PDF buffer associated with the notes buffer") (defvar interleave--window-configuration nil - "Variable to store the window configuration before interleave mode was -enabled.") + "Variable to store the window configuration before interleave mode was enabled.") (if (featurep 'pdf-view) ; if `pdf-tools' is installed (progn @@ -74,7 +75,7 @@ enabled.") (defconst interleave--pdf-scroll-up-or-next-page-fn #'pdf-view-scroll-up-or-next-page) (defconst interleave--pdf-scroll-down-or-previous-page-fn #'pdf-view-scroll-down-or-previous-page)) (progn - (defconst interleave--pdf-current-page-fn #'doc-view-current-page) + (defconst interleave--pdf-current-page-fn (lambda () (doc-view-current-page))) (defconst interleave--pdf-next-page-fn #'doc-view-next-page) (defconst interleave--pdf-previous-page-fn #'doc-view-previous-page) (defconst interleave--pdf-goto-page-fn #'doc-view-goto-page) @@ -86,7 +87,7 @@ enabled.") "Caches the current page while scrolling")) (defun interleave--find-pdf-path (buffer) - "Searches for the 'interleave_pdf' property in BUFFER and extracts it when found" + "Searches for the `interleave_pdf' property in BUFFER and extracts it when found." (with-current-buffer buffer (save-excursion (goto-char (point-min)) @@ -107,17 +108,20 @@ SPLIT-WINDOW is a function that actually splits the window, so it must be either (funcall split-window) (find-file (expand-file-name (interleave--find-pdf-path buf))) (interleave-pdf-mode 1)) - ('error (message "Please specify PDF file with #+INTERLEAVE_PDF document property.") - (interleave--quit))))) + ('error + (message "Please specify PDF file with #+INTERLEAVE_PDF document property.") + (interleave--quit))))) (defun interleave--go-to-page-note (page) - "Searches the notes buffer for an headline with the 'interleave_page_note' property set -to PAGE. It narrows the subtree when found." + "Searches the notes buffer for an headline with the `interleave_page_note' +property set to PAGE. It narrows the subtree when found." (with-current-buffer *interleave--org-buffer* (save-excursion (widen) (goto-char (point-min)) - (when (re-search-forward (format "^\[ \t\r\]*\:interleave_page_note\: %s$" page) nil t) + (when (re-search-forward (format "^\[ \t\r\]*\:interleave_page_note\: %s$" + page) + nil t) (org-narrow-to-subtree) (org-show-entry) t)))) @@ -252,24 +256,27 @@ next set of notes." (define-minor-mode interleave "Interleaving your text books since 2015. -In the past, textbooks were sometimes published as /interleaved/ editions. That meant, each page -was followed by a blank page and the ambitious student/scholar had the ability to take his notes directly -in her copy of the textbook. Newton and Kant were prominent representatives of this technique. +In the past, textbooks were sometimes published as 'interleaved' editions. +That meant, each page was followed by a blank page and the ambitious student/ +scholar had the ability to take their notes directly in their copy of the +textbook. Newton and Kant were prominent representatives of this technique. -Nowadays textbooks (or lecture material) come in PDF format. Although almost every PDF Reader has the ability to add some notes to the PDF itself, it is not as powerful as it could be. -This is what this minor mode tries to accomplish. It presents your PDF side by side to an [[http://orgmode.org][Org Mode]] buffer with you notes. -Narrowing down to just those passages that are relevant to this particular page in the document viewer. +Nowadays textbooks (or lecture material) come in PDF format. Although almost +every PDF Reader has the ability to add some notes to the PDF itself, it is +not as powerful as it could be. + +This is what this minor mode tries to accomplish. It presents your PDF side by +side to an [[http://orgmode.org][Org Mode]] buffer with your notes, narrowing +down to just those passages that are relevant to the particular page in the +document viewer. Usage: -Create a Org file thath will keep your notes. In the Org headers section add +- Create a Org file that will keep your notes. In the Org headers section, add #+INTERLEAVE_PDF: /the/path/to/your/pdf.pdf - -Then start 'interleave' with -M-x interleave - -To insert a note for a page, type 'i'. -Navigation is the same as in `doc-view-mode'/`pdf-view-mode'." +- Start `interleave' with `M-x interleave'. +- To insert a note for a page, type `i'. +- Navigation is the same as in `doc-view-mode'/`pdf-view-mode'." :lighter " ≡" :keymap interleave-map (if interleave From 3fff9d9001989a5ae9bf2666bf6c32ceef930ff3 Mon Sep 17 00:00:00 2001 From: Kaushal Modi Date: Fri, 3 Apr 2015 15:14:09 -0400 Subject: [PATCH 3/4] Improve: Search for org heading property --- interleave.el | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/interleave.el b/interleave.el index 776f452..a9d6c13 100644 --- a/interleave.el +++ b/interleave.el @@ -201,7 +201,7 @@ the current narrowed down notes view." (save-excursion (org-narrow-to-subtree) (goto-char (point-min)) - (search-forward-regexp "Notes for page \\(.*\\)") + (re-search-forward "^ *:interleave_page_note: *\\(.*\\)") (setq pdf-page (string-to-number (match-string 1)))) (interleave--switch-to-pdf-buffer) (funcall interleave--pdf-goto-page-fn pdf-page))) @@ -217,7 +217,7 @@ previous set of notes." (org-backward-heading-same-level 1) (org-narrow-to-subtree) (goto-char (point-min)) - (search-forward-regexp "Notes for page \\(.*\\)") + (re-search-forward "^ *:interleave_page_note: *\\(.*\\)") (setq pdf-page (string-to-number (match-string 1)))) (interleave--switch-to-pdf-buffer) (funcall interleave--pdf-goto-page-fn pdf-page))) @@ -233,7 +233,7 @@ next set of notes." (org-forward-heading-same-level 1) (org-narrow-to-subtree) (goto-char (point-min)) - (search-forward-regexp "Notes for page \\(.*\\)") + (re-search-forward "^ *:interleave_page_note: *\\(.*\\)") (setq pdf-page (string-to-number (match-string 1)))) (interleave--switch-to-pdf-buffer) (funcall interleave--pdf-goto-page-fn pdf-page))) From e5a8304c8a33d79f56d6ac7afbbbbafb57db828b Mon Sep 17 00:00:00 2001 From: Kaushal Modi Date: Fri, 3 Apr 2015 15:49:05 -0400 Subject: [PATCH 4/4] Improve: Robustness of `interleave--sync*` fns - Make the narrowing and widening of org subtrees consistent - Don't throw error when trying to find the next or previous page notes --- interleave.el | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/interleave.el b/interleave.el index a9d6c13..5c6716c 100644 --- a/interleave.el +++ b/interleave.el @@ -213,14 +213,17 @@ previous set of notes." (interleave--switch-to-org-buffer) (let ((pdf-page)) (save-excursion - (widen) - (org-backward-heading-same-level 1) (org-narrow-to-subtree) (goto-char (point-min)) - (re-search-forward "^ *:interleave_page_note: *\\(.*\\)") - (setq pdf-page (string-to-number (match-string 1)))) - (interleave--switch-to-pdf-buffer) - (funcall interleave--pdf-goto-page-fn pdf-page))) + (widen) + (when (re-search-backward "^ *:interleave_page_note: *\\(.*\\)" nil :noerror) + (setq pdf-page (string-to-number (match-string 1))))) + (if pdf-page + (progn + (interleave--go-to-page-note pdf-page) + (interleave--switch-to-pdf-buffer) + (funcall interleave--pdf-goto-page-fn pdf-page)) + (org-narrow-to-subtree)))) (defun interleave--sync-pdf-page-next () "Synchronize the page in the pdf buffer to be the same as the page in the @@ -229,22 +232,26 @@ next set of notes." (interleave--switch-to-org-buffer) (let ((pdf-page)) (save-excursion - (widen) - (org-forward-heading-same-level 1) (org-narrow-to-subtree) (goto-char (point-min)) - (re-search-forward "^ *:interleave_page_note: *\\(.*\\)") - (setq pdf-page (string-to-number (match-string 1)))) - (interleave--switch-to-pdf-buffer) - (funcall interleave--pdf-goto-page-fn pdf-page))) - -(defun interleave--quit () - "Quit interleave mode." - (interactive) - (with-current-buffer *interleave--org-buffer* - (widen) - (interleave 0)) - (interleave--pdf-kill-proc-and-buffer)) + (re-search-forward "^ *:interleave_page_note:") ; current page + (widen) + (when (re-search-forward "^ *:interleave_page_note: *\\(.*\\)" nil :noerror) ; next page + (setq pdf-page (string-to-number (match-string 1))))) + (if pdf-page + (progn + (interleave--go-to-page-note pdf-page) + (interleave--switch-to-pdf-buffer) + (funcall interleave--pdf-goto-page-fn pdf-page)) + (org-narrow-to-subtree)))) + + (defun interleave--quit () + "Quit interleave mode." + (interactive) + (with-current-buffer *interleave--org-buffer* + (widen) + (interleave 0)) + (interleave--pdf-kill-proc-and-buffer)) ;;; Interleave ;; Minor mode for the org file buffer containing notes