branch: externals/doc-view-follow
commit 55384318cdca17335ce81e03cff105023e36fce3
Author: Paul Nelson <ultr...@gmail.com>
Commit: Paul Nelson <ultr...@gmail.com>

    Add doc-view-follow-hijack option
    
    * doc-view-follow.el (doc-view-follow-hijack): New custom option that,
    when non-nil, causes follow-mode to delegate to doc-view-follow-mode
    in document buffers.
    (doc-view-follow--hijack): New function that implements around advice
    for follow-mode to enable the hijacking behavior.
    
    * README.org (Convenience): Update to recommend using
    doc-view-follow-hijack instead of custom functions.
---
 README.org         | 17 ++---------------
 doc-view-follow.el | 29 +++++++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 15 deletions(-)

diff --git a/README.org b/README.org
index fad7de5065..ee7ff969a2 100644
--- a/README.org
+++ b/README.org
@@ -50,21 +50,8 @@ Both windows will automatically stay synchronized.
 
 ** Convenience
 
-Since =doc-view-follow-mode= works with document files (PDF, PS, etc.) and 
=follow-mode= works with text buffers, you might want a single command that 
applies the appropriate mode based on your current buffer.  Here's one:
-
-#+begin_src elisp
-(defun toggle-some-follow-mode ()
-  "Toggle either `doc-view-follow-mode' or `follow-mode' as appropriate.
-In buffers where `doc-view-follow-mode' is supported, toggle that mode.
-Otherwise, toggle the standard `follow-mode'."
-  (interactive)
-  (if (and (fboundp 'doc-view-follow-supported-p)
-           (doc-view-follow-supported-p major-mode))
-      (call-interactively 'doc-view-follow-mode)
-    (call-interactively 'follow-mode)))
-
-(keymap-global-set "H-f" 'toggle-some-follow-mode)
-#+end_src
+Since =doc-view-follow-mode= works with document files (PDF, PS, etc.) and 
=follow-mode= works with text buffers, you might want =M-x follow-mode= to 
activate the appropriate mode based on your current buffer.  You can do this by 
customizing the user option =doc-view-follow-hijack=, or adding =(setopt 
doc-view-follow-hijack t)= to your config.
+
 ** Prefix Key Commands
 
 When =doc-view-follow-mode= is active, you can use the following commands with 
the prefix key (by default =C-c .=, the same as =follow-mode='s prefix):
diff --git a/doc-view-follow.el b/doc-view-follow.el
index 20efa15935..8150c03e9c 100644
--- a/doc-view-follow.el
+++ b/doc-view-follow.el
@@ -46,6 +46,10 @@
 ;; Navigating with standard commands (like 'n' for next page and 'p'
 ;; for previous page) in one window automatically synchronizes the
 ;; view in the other.
+;;
+;; Setting the user option `doc-view-follow-hijack' to non-nil will
+;; hijack `follow-mode' in document buffers, so that it delegates to
+;; `doc-view-follow-mode' automatically.
 
 ;;; Code:
 
@@ -143,6 +147,31 @@ page of the document in that window."
         (doc-view-follow-set-page last-page major-mode)
         (doc-view-follow-sync-pages)))))
 
+;;;###autoload
+(defcustom doc-view-follow-hijack nil
+  "Non-nil to hijack `follow-mode' in document buffers.
+When non-nil, `follow-mode' will delegate to `doc-view-follow' in
+document buffers."
+  :type 'boolean
+  :group 'doc-view
+  :set (lambda (sym val)
+         (set-default sym val)
+         (if val
+             (advice-add 'follow-mode :around #'doc-view-follow--hijack)
+           (advice-remove 'follow-mode #'doc-view-follow--hijack))))
+
+(defun doc-view-follow--hijack (orig-fun &rest args)
+  "Around advice for `follow-mode' to delegate to `doc-view-follow-mode'.
+Called non-interactively, calls ORIG-FUN with ARGS.  Called
+interactively, toggles `doc-view-follow-mode' when supported, otherwise
+toggles `follow-mode'."
+  (if (called-interactively-p 'any)
+      (let ((target-fun (if (doc-view-follow-supported-p major-mode)
+                            #'doc-view-follow-mode
+                          orig-fun)))
+        (call-interactively target-fun))
+    (apply orig-fun args)))
+
 ;;;###autoload
 (define-minor-mode doc-view-follow-mode
   "Minor mode to sync pages between document windows.

Reply via email to