From 9e4578366160cfdc484d3f4a11ba10d4739ffaa3 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Tue, 17 Jan 2017 10:42:13 +0100 Subject: [PATCH] Save position before searching for a search option in case of error * lisp/org.el (org-open-file): Save position with `org-mark-ring-push' before calling `org-link-search'. --- etc/ORG-NEWS | 7 +++++++ lisp/org.el | 35 +++++++++++++++++++++-------------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index e93986ba7..99952a907 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -195,6 +195,13 @@ Call ~org-agenda-set-restriction-lock~ from the agenda. *** Computed dates in tables appear as inactive time stamps +*** Save point before opening a file with an unknown search option + +When following a file link with a search option (e.g., =::#custom-id=) +that doesn't exist in the target file, save positon before raising an +error. As a consequence, it is possible to jump back to the original +document with ~org-mark-ring-goto~ (default binding =C-c &=). + * Version 9.0 ** Incompatible changes diff --git a/lisp/org.el b/lisp/org.el index 1807ce7f3..102f2d1b5 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -11362,12 +11362,19 @@ If the file does not exist, an error is thrown." (search (concat file "::" search)) (t file))) (dlink (downcase link)) - (old-buffer (current-buffer)) - (old-pos (point)) - (old-mode major-mode) (ext (and (string-match "\\`.*?\\.\\([a-zA-Z0-9]+\\(\\.gz\\)?\\)\\'" dfile) (match-string 1 dfile))) + (save-position-maybe + (let ((old-buffer (current-buffer)) + (old-pos (point)) + (old-mode major-mode)) + (lambda () + (and (derived-mode-p 'org-mode) + (eq old-mode 'org-mode) + (or (not (eq old-buffer (current-buffer))) + (not (eq old-pos (point)))) + (org-mark-ring-push old-pos old-buffer))))) cmd link-match-data) (cond ((member in-emacs '((16) system)) @@ -11441,7 +11448,12 @@ If the file does not exist, an error is thrown." (widen) (cond (line (org-goto-line line) (when (derived-mode-p 'org-mode) (org-reveal))) - (search (org-link-search search)))) + (search (condition-case err + (org-link-search search) + ;; Save position before error-ing out so user + ;; can easily move back to the original buffer. + (error (funcall save-position-maybe) + (error (nth 1 err))))))) ((functionp cmd) (save-match-data (set-match-data link-match-data) @@ -11450,23 +11462,18 @@ If the file does not exist, an error is thrown." ;; FIXME: Remove this check when most default installations ;; of Emacs have at least Org 9.0. ((debug wrong-number-of-arguments wrong-type-argument - invalid-function) + invalid-function) (user-error "Please see Org News for version 9.0 about \ `org-file-apps'--Lisp error: %S" cmd))))) ((consp cmd) ;; FIXME: Remove this check when most default installations of - ;; Emacs have at least Org 9.0. - ;; Heads-up instead of silently fall back to - ;; `org-link-frame-setup' for an old usage of `org-file-apps' - ;; with sexp instead of a function for `cmd'. + ;; Emacs have at least Org 9.0. Heads-up instead of silently + ;; fall back to `org-link-frame-setup' for an old usage of + ;; `org-file-apps' with sexp instead of a function for `cmd'. (user-error "Please see Org News for version 9.0 about \ `org-file-apps'--Error: Deprecated usage of %S" cmd)) (t (funcall (cdr (assq 'file org-link-frame-setup)) file))) - (and (derived-mode-p 'org-mode) - (eq old-mode 'org-mode) - (or (not (eq old-buffer (current-buffer))) - (not (eq old-pos (point)))) - (org-mark-ring-push old-pos old-buffer)))) + (funcall save-position-maybe))) (defun org-file-apps-entry-match-against-dlink-p (entry) "This function returns non-nil if `entry' uses a regular