branch: externals/denote
commit d92fad4cdbd1e756039d3dc573ceaf937b374ae0
Merge: 9a08d6bf25 fb39088ded
Author: Protesilaos Stavrou <i...@protesilaos.com>
Commit: GitHub <nore...@github.com>

    Merge pull request #385 from haji-ali/link-fontification-fix
    
    Added a helper function to conditionally enable `denote-fontify-links-mode`
---
 README.org | 21 +++++++++++----------
 denote.el  | 46 +++++++++++++++++++++++++++++-----------------
 2 files changed, 40 insertions(+), 27 deletions(-)

diff --git a/README.org b/README.org
index 113e4356c3..2f23b72f77 100644
--- a/README.org
+++ b/README.org
@@ -1133,7 +1133,7 @@ ways to go about it, anyway:
    ;;;     (info "(emacs) Directory Variables")
 
    ((nil . ((denote-directory . "/path/to/silo/")
-            (denote-org-front-matter . 
+            (denote-org-front-matter .
              "#+title:      %s
 ,#+date:       %s
 ,#+filetags:   %s
@@ -2756,23 +2756,24 @@ such as ~markdown-mode~ (for =.md= files) or 
~text-mode~ (for =.txt=
 files) do not have this feature built into them. Users can still get
 the same behaviour as with Org by activating the ~denote-fontify-links-mode~.
 
-The ~denote-fontify-links-mode~ is a buffer-local minor mode. Users
-can enable it automatically in all plain text files with something
-like this:
+The ~denote-fontify-links-mode~ is a buffer-local minor mode. Users can enable
+it automatically in plain text files that correspond to denote notes with
+something like this:
 
 #+begin_src emacs-lisp
-(add-hook 'text-mode-hook #'denote-fontify-links-mode)
+(add-hook 'text-mode-hook #'denote-fontify-links-mode-maybe)
 #+end_src
 
-The ~text-mode-hook~ applies to all modes derived from ~text-mode~,
-including ~markdown-mode~. Though a more explicit setup does no harm:
+The ~text-mode-hook~ applies to all modes derived from ~text-mode~, including
+~markdown-mode~. Though a more explicit setup does no harm:
 
 #+begin_src emacs-lisp
-(add-hook 'markdown-mode-hook #'denote-fontify-links-mode)
+(add-hook 'markdown-mode-hook #'denote-fontify-links-mode-maybe)
 #+end_src
 
-Because Org already recognises =denote:= links, the minor mode
-~denote-fontify-links-mode~ will do nothing in Org buffers.
+Because Org already recognises =denote:= links, the function
+~denote-fontify-links-mode-maybe~ will not enable the mode
+~denote-fontify-links-mode~ in Org buffers.
 
 #+findex: denote-link-markdown-follow
 In files whose major mode is ~markdown-mode~, the default key binding
diff --git a/denote.el b/denote.el
index 55aee1a3c0..0874ffd9e9 100644
--- a/denote.el
+++ b/denote.el
@@ -4158,27 +4158,39 @@ To be used as a `thing-at' provider."
 
 (defvar thing-at-point-provider-alist)
 
+(defun denote-fontify-links-mode-maybe ()
+  "Enable `denote-fontify-links-mode' in a denote file unless in `org-mode'."
+  (when (and (buffer-file-name)
+             (denote-file-is-note-p (buffer-file-name))
+             (not (derived-mode-p 'org-mode)))
+    (denote-fontify-links-mode)))
+
 (define-minor-mode denote-fontify-links-mode
-  "A minor mode to fontify and fold Denote links."
+  "A minor mode to fontify and fold Denote links.
+
+It is recommended that this mode is enabled only when the current
+buffer is from a denote note and the current buffer is not an
+`org-mode' one; as `org-mode' implemented its own fontification
+of links. You may use `denote-fontify-links-mode-maybe' for this
+purpose."
   :init-value nil
   :global nil
   :group 'denote
-  (unless (derived-mode-p 'org-mode)
-    (require 'thingatpt)
-    (if denote-fontify-links-mode
-        (progn
-          (add-to-invisibility-spec 'denote-link)
-          (font-lock-add-keywords nil '(denote-fontify-links))
-          (setq-local thing-at-point-provider-alist
-                      (append thing-at-point-provider-alist
-                              '((url . denote--get-link-file-path-at-point)))))
-      (remove-from-invisibility-spec 'denote-link)
-      (font-lock-remove-keywords nil '(denote-fontify-links))
-      (setq-local thing-at-point-provider-alist
-                  (delete
-                   '(url . denote--get-link-file-path-at-point)
-                   thing-at-point-provider-alist)))
-    (font-lock-update)))
+  (require 'thingatpt)
+  (if denote-fontify-links-mode
+      (progn
+        (add-to-invisibility-spec 'denote-link)
+        (font-lock-add-keywords nil '(denote-fontify-links))
+        (setq-local thing-at-point-provider-alist
+                    (append thing-at-point-provider-alist
+                            '((url . denote--get-link-file-path-at-point)))))
+    (remove-from-invisibility-spec 'denote-link)
+    (font-lock-remove-keywords nil '(denote-fontify-links))
+    (setq-local thing-at-point-provider-alist
+                (delete
+                 '(url . denote--get-link-file-path-at-point)
+                 thing-at-point-provider-alist)))
+  (font-lock-update))
 
 ;;;;; Backlinks' buffer
 

Reply via email to