0

I have a function that executes save-excursion from within an Org Agenda buffer, but it doesn't return the point to the agenda buffer once the body of the excursion finishes executing. Here is the function I call:

(defun link-headline-from-agenda ()
  "From an agenda window, link the corresponding headline to the selected file."
  (interactive)
  (save-excursion
    (org-agenda-goto)
    (link-headline)))

Is there a way to make save-excursion work as expected or get similar behavior inside an org agenda buffer?

  • 1
    If you are leaving the *Org Agenda* buffer to go to a source buffer and wish to keep the new window configuration, then you may wish to record the initial point/window/buffer in let-bound variables and then return to that point/window/buffer when you are done. If you wish to restore the window layout, then have a look at save-window-excursion: gnu.org/software/emacs/manual/html_node/elisp/… The *Org Agenda* buffer is a summary containing excerpts from one or more source files -- when acting on a source file, a user leaves the agenda buffer. – lawlist Feb 4 at 17:16
0

It turns out the solution was in the source files. Agenda commands that operate on the corresponding buffer save the org buffer attributes, then operate on them using with-current-buffer. Here is my working version of the above function:

(defun link-headline-from-agenda ()
  "From an agenda window, link the corresponding headline to the selected file."
  (interactive)
  (let* ((marker (org-get-at-bol 'org-marker))
     (buffer (marker-buffer marker))
     (pos (marker-position marker)))
    (org-with-remote-undo buffer
      (with-current-buffer buffer
    (goto-char pos)
    (link-headline)))))

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.