From mboxrd@z Thu Jan 1 00:00:00 1970 From: Kyle Meyer Subject: Re: FR: refile-and-link Date: Thu, 04 Dec 2014 19:21:30 -0500 Message-ID: <871tofx7xx.fsf@kyleam.com> References: <20141203124652.GE28615@pacific.linksys.moosehall> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:59518) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XwgeU-00016S-7m for emacs-orgmode@gnu.org; Thu, 04 Dec 2014 19:21:48 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XwgeO-0006zH-45 for emacs-orgmode@gnu.org; Thu, 04 Dec 2014 19:21:42 -0500 Received: from mail-qc0-f181.google.com ([209.85.216.181]:61170) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XwgeN-0006z4-Vu for emacs-orgmode@gnu.org; Thu, 04 Dec 2014 19:21:36 -0500 Received: by mail-qc0-f181.google.com with SMTP id m20so13504153qcx.40 for ; Thu, 04 Dec 2014 16:21:34 -0800 (PST) In-Reply-To: <20141203124652.GE28615@pacific.linksys.moosehall> (Adam Spiers's message of "Wed, 3 Dec 2014 12:46:52 +0000") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Adam Spiers Cc: org-mode mailing list Adam Spiers 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