Hi Nicolas,

On Sun, Oct 25, 2015 at 2:08 PM, Nicolas Goaziou <m...@nicolasgoaziou.fr> wrote:
> Hello,
>
> Puneeth Chaganti <puncha...@gmail.com> writes:
>
>> Here is a patch that works for the case you describe.
>
> Thank you. Some comments follow.

Thanks for your careful review and detailed comments.  I've attached
an updated patch.

-- Puneeth
From 5f0746566cc63c37634b20070a50601c5bb990ab Mon Sep 17 00:00:00 2001
From: Puneeth Chaganti <puncha...@muse-amuse.in>
Date: Sun, 25 Oct 2015 14:36:16 +0530
Subject: [PATCH] Widen if target id location is not in the narrow.

If the target location for `org-id-goto' or `org-id-open' is in a
narrowed buffer but not in the narrowed region, the buffer is widened.
---
 lisp/org-id.el | 54 ++++++++++++++++++++++++++++--------------------------
 1 file changed, 28 insertions(+), 26 deletions(-)

diff --git a/lisp/org-id.el b/lisp/org-id.el
index f86ef22..0e8ba75 100644
--- a/lisp/org-id.el
+++ b/lisp/org-id.el
@@ -292,13 +292,7 @@ It returns the ID of the entry.  If necessary, the ID is created."
   "Switch to the buffer containing the entry with id ID.
 Move the cursor to that entry in that buffer."
   (interactive "sID: ")
-  (let ((m (org-id-find id 'marker)))
-    (unless m
-      (error "Cannot find entry with ID \"%s\"" id))
-    (org-pop-to-buffer-same-window (marker-buffer m))
-    (goto-char m)
-    (move-marker m nil)
-    (org-show-context)))
+  (org-id-show id #'org-pop-to-buffer-same-window))
 
 ;;;###autoload
 (defun org-id-find (id &optional markerp)
@@ -634,6 +628,24 @@ optional argument MARKERP, return the position as a new marker."
 		(move-marker (make-marker) pos buf)
 	      (cons file pos))))))))
 
+(defun org-id-show (id cmd)
+  "Show an entry with id ID by buffer-switching using CMD.
+CMD is a function that takes a buffer or a string (buffer name)
+as an argument, which will be used to switch to the buffer
+containing the entry with id ID."
+  (let ((m (org-id-find id 'marker)))
+    (unless m
+      (error "Cannot find entry with ID \"%s\"" id))
+    (unless (equal (current-buffer) (marker-buffer m))
+      (funcall cmd (marker-buffer m)))
+    (when (let ((pos (marker-position m)))
+	    (or (< pos (point-min))
+		(> pos (point-max))))
+      (widen))
+    (goto-char m)
+    (move-marker m nil)
+    (org-show-context 'link-search)))
+
 ;; id link type
 
 ;; Calling the following function is hard-coded into `org-store-link',
@@ -659,25 +671,15 @@ optional argument MARKERP, return the position as a new marker."
 (defun org-id-open (id)
   "Go to the entry with id ID."
   (org-mark-ring-push)
-  (let ((m (org-id-find id 'marker))
-	cmd)
-    (unless m
-      (error "Cannot find entry with ID \"%s\"" id))
-    ;; Use a buffer-switching command in analogy to finding files
-    (setq cmd
-	  (or
-	   (cdr
-	    (assq
-	     (cdr (assq 'file org-link-frame-setup))
-	     '((find-file . switch-to-buffer)
-	       (find-file-other-window . switch-to-buffer-other-window)
-	       (find-file-other-frame . switch-to-buffer-other-frame))))
-	   'switch-to-buffer-other-window))
-    (if (not (equal (current-buffer) (marker-buffer m)))
-	(funcall cmd (marker-buffer m)))
-    (goto-char m)
-    (move-marker m nil)
-    (org-show-context)))
+  (let ((cmd (or
+	      (cdr
+	       (assq
+		(cdr (assq 'file org-link-frame-setup))
+		'((find-file . switch-to-buffer)
+		  (find-file-other-window . switch-to-buffer-other-window)
+		  (find-file-other-frame . switch-to-buffer-other-frame))))
+	      #'switch-to-buffer-other-window)))
+    (org-id-show id cmd)))
 
 (org-add-link-type "id" 'org-id-open)
 
-- 
2.5.0

Reply via email to