branch: externals/org
commit a92951d0c65256695eab40dfd0138c0aceefad71
Author: Ihor Radchenko <yanta...@posteo.net>
Commit: Ihor Radchenko <yanta...@posteo.net>

    org-link-expand-abbrev: Auto-disable custom abbrevs that fail to expand
    
    * lisp/ol.el (org-link-expand-abbrev): When a custom link abbrev
    function errs or returns non-string, show a warning and disable that
    abbrev.
---
 lisp/ol.el | 40 +++++++++++++++++++++++++++++-----------
 1 file changed, 29 insertions(+), 11 deletions(-)

diff --git a/lisp/ol.el b/lisp/ol.el
index a8a59ddaae..93ba9db6d9 100644
--- a/lisp/ol.el
+++ b/lisp/ol.el
@@ -1158,17 +1158,35 @@ Abbreviations are defined in `org-link-abbrev-alist'."
       (if (not as)
          link
        (setq rpl (cdr as))
-       (cond
-        ((symbolp rpl) (funcall rpl tag))
-        ((string-match "%(\\([^)]+\\))" rpl)
-         (replace-match
-          (save-match-data
-            (funcall (intern-soft (match-string 1 rpl)) tag))
-          t t rpl))
-        ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
-        ((string-match "%h" rpl)
-         (replace-match (url-hexify-string (or tag "")) t t rpl))
-        (t (concat rpl tag)))))))
+        (cl-macrolet
+            ((eval-or-disable (&rest body)
+               "Run BODY and disable AS abbrev if it errs."
+               `(condition-case err
+                   (progn ,@body)
+                  (error
+                   (org-display-warning
+                    (format "Disabling link abbrev %s <- %s after expansion 
failure: %S"
+                            rpl link (error-message-string err)))
+                   (setq org-link-abbrev-alist-local (delete as 
org-link-abbrev-alist-local)
+                        org-link-abbrev-alist (delete as 
org-link-abbrev-alist))
+                   link))))
+         (cond
+          ((symbolp rpl)
+            (eval-or-disable
+             (let ((expanded (funcall rpl tag)))
+               (unless (stringp expanded)
+                 (error "%s did not return a string: %S" rpl expanded))
+               expanded)))
+          ((string-match "%(\\([^)]+\\))" rpl)
+            (eval-or-disable
+             (replace-match
+              (save-match-data
+                (funcall (intern-soft (match-string 1 rpl)) tag))
+              t t rpl)))
+          ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
+          ((string-match "%h" rpl)
+           (replace-match (url-hexify-string (or tag "")) t t rpl))
+          (t (concat rpl tag))))))))
 
 (defun org-link-open (link &optional arg)
   "Open a link object LINK.

Reply via email to