emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] FR: refile-and-link


From: Kyle Meyer
Subject: Re: [O] FR: refile-and-link
Date: Thu, 04 Dec 2014 19:21:30 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)

Adam Spiers <address@hidden> wrote:
> Forgive me if this has already been implemented, but I couldn't see
> it...

I don't know of a command that does this.

> I'm looking for something similar to the "extract method" operation
> which refactoring IDEs can perform on code.  You would select a
> headline (or maybe even region), hit `refile-and-link', and then after
> the normal refiling, a link to the refiled section would be inserted
> in the place where the refiled section previously lived.
>
> Thoughts?

The last refile location is stored in org-bookmark-names-plist. The
(lightly tested) function below uses that information to create a link
to the refiled heading.

#+begin_src emacs-lisp
  (defun org-refile-and-link ()
    "Refile heading, adding a link to the new location.
  Prefix arguments are interpreted by `org-refile'."
    (interactive)
    (when (member current-prefix-arg '(3 (4) (16)))
      (user-error "Linking is incompatible with that prefix argument"))
    (let ((heading  (org-get-heading t t))
          (orig-file (buffer-file-name)))
      (call-interactively #'org-refile)
      (let* ((refile-file
              (bookmark-get-filename
               (assoc (plist-get org-bookmark-names-plist :last-refile)
                      bookmark-alist)))
             (same-file (string= orig-file refile-file))
             (link (if same-file
                       (concat "*" heading)
                     (concat refile-file "::*" heading)))
             (desc heading))
        (open-line 1)
        (insert (org-make-link-string link desc)))))
#+end_src

--
Kyle



reply via email to

[Prev in Thread] Current Thread [Next in Thread]