From mboxrd@z Thu Jan 1 00:00:00 1970 From: Thorsten Jolitz Subject: Re: How to change a link? Date: Fri, 17 Oct 2014 00:19:15 +0200 Message-ID: <87d29ru07w.fsf@gmail.com> References: <87a94yjia9.fsf@wmi.amu.edu.pl> <87bnpd4ov7.fsf@nicolasgoaziou.fr> <87tx35hvh8.fsf@wmi.amu.edu.pl> <87y4sh2e7c.fsf@nicolasgoaziou.fr> <87mw8vj3vs.fsf@wmi.amu.edu.pl> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:48963) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XetON-00053d-M3 for emacs-orgmode@gnu.org; Thu, 16 Oct 2014 18:19:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XetOJ-0002O3-7w for emacs-orgmode@gnu.org; Thu, 16 Oct 2014 18:19:31 -0400 Received: from plane.gmane.org ([80.91.229.3]:50242) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XetOJ-0002Nx-0n for emacs-orgmode@gnu.org; Thu, 16 Oct 2014 18:19:27 -0400 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1XetOH-0004BZ-K8 for emacs-orgmode@gnu.org; Fri, 17 Oct 2014 00:19:25 +0200 Received: from e178059085.adsl.alicedsl.de ([85.178.59.85]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 17 Oct 2014 00:19:25 +0200 Received: from tjolitz by e178059085.adsl.alicedsl.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 17 Oct 2014 00:19:25 +0200 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: emacs-orgmode@gnu.org Marcin Borkowski writes: > On 2014-10-15, at 23:52, Nicolas Goaziou wrote: > >> Marcin Borkowski writes: >> >>> I have one more question. What I'm about to do is (basically) put >>> "file:some-file-name::" in front of the link, without changing the >>> description. I could use `org-element-put-property' and (AFAIU) >>> org-element-link-interpreter to put it into the buffer (and probably >>> delete the old one). It would be much easier (and maybe faster) just to >>> go to the point in the buffer where the link starts, go `(forward-char >>> 2)' (past the brackets) and `(insert (concat "file" name "::"))'. >>> >>> But, is it safe? Wouldn't it break something? And is it considered a >>> good practice? >> >> There are caveats. >> >> For example, as soon as you alter the buffer, your AST becomes invalid >> (buffer positions are all wrong after the insertion). If you want to >> process all the links from the same AST, you can, for example, maintain >> a counter for characters inserted so far that will fix buffer positions, >> or first get all internal links with `org-element-map', then process >> them in reverse order so buffer modifications do not invalidate them. > > OK, so what is the canonical way of doing this? I don't want to use > org-dp, since it is another dependency. It is a problem to add dependencies to libraries the user must install himself, and at the same time its a pity that there is so much duplication instead of reuse ... However, here is a org-dp solution, use 't' instead of 'prepend to replace the links, and whatever you want instead of "file+emacs" as replacement. Of course one could easily re-search and replace "[[file:" in this simple case, but this uses the parser and allows doing more complex stuff in a clean way too: ,---- | * ORG SCRATCH | | ** Level 2 | | [[file+emacs:~/junk/org/minimal.org][min.org]] | | [[file:~/junk/org/minimal.org][min.org]] | | *** Level 3 | | [[file+emacs:~/junk/org/trash-me.org][trash.org]] | | [[file:~/junk/org/trash-me.org][trash.org]] | | | #+BEGIN_SRC emacs-lisp :results none | (require 'org-dp) | (org-dp-map | '(org-dp-rewire | 'paragraph | (lambda (cont elem) | (let* ((link (car cont)) | (raw-val (org-element-property :raw-link link)) | (new-val (mapconcat 'identity | (cons "file+emacs" | (cdr | (split-string | raw-val ":" t))) | ":"))) | (org-element-put-property link :raw-link new-val))) | 'prepend) | org-link-re-with-space t) | #+END_SRC `---- -- cheers, Thorsten