From mboxrd@z Thu Jan 1 00:00:00 1970 From: Thorsten Jolitz Subject: Re: How to change a link? Date: Sat, 18 Oct 2014 15:48:21 +0200 Message-ID: <874mv14hga.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> <87d29ru07w.fsf@gmail.com> <87r3y6noca.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]:33644) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XfUN5-00025p-8T for emacs-orgmode@gnu.org; Sat, 18 Oct 2014 09:48:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XfUN0-0008GS-Cc for emacs-orgmode@gnu.org; Sat, 18 Oct 2014 09:48:39 -0400 Received: from plane.gmane.org ([80.91.229.3]:37282) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XfUN0-0008F9-2q for emacs-orgmode@gnu.org; Sat, 18 Oct 2014 09:48:34 -0400 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1XfUMy-0003EO-Gv for emacs-orgmode@gnu.org; Sat, 18 Oct 2014 15:48:32 +0200 Received: from e178062219.adsl.alicedsl.de ([85.178.62.219]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 18 Oct 2014 15:48:32 +0200 Received: from tjolitz by e178062219.adsl.alicedsl.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 18 Oct 2014 15:48:32 +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-17, at 00:19, Thorsten Jolitz wrote: > >> 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: >> >> ,---- >> | #+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 >> `---- > Hi Marcin, > one thing I don't quite understand yet: why is the first argument to > org-dp-rewire `'paragraph'? My intuition says it should rather be > 'link, though this doesn't seem to work. How come that you say > 'paragraph, but the lambda in the second parameter gets the link data in > `cont'? (This might be a stupid question, but I really want to grok > this.) I was surprised too! But links are objects, and with point at a link org-element-at-point return a paragraph with the link as content. So to get the contents of a link, you take the contents of the contents of a paragraph, but that a special case, because mostly you will work directly with elements. The 'paragraph' is the target element type. In this case I could have written ,---- | '(org-dp-rewire nil ...) `---- instead, since then parsed and rewired element types are the same. See examples below of how to convert a link into a src-block or a keyword (e.g.). For more in depth info, have a look at: ,----[ C-h f org-dp-rewire RET ] | org-dp-rewire is a Lisp function in `org-dp.el'. | | (org-dp-rewire ELEM-TYPE &optional CONTENTS REPLACE AFFILIATED ELEMENT | &rest ARGS) | | Rewire element-at-point or ELEMENT (if given). [...] `---- Example: When you start with the file link below and eval the following src-blocks in order, you get: #+BEGIN_SRC picolisp (println "Link label: min.org") (println "Link type: file") #+END_SRC #+LABEL: min.org [[file:junk/org/minimal.org][min.org]] #+BEGIN_SRC emacs-lisp :results none (require 'org-dp) (org-dp-map '(org-dp-rewire 'src-block nil 'prepend nil nil :language "picolisp" :value (lambda (_old_ elem) (format (concat "(println \"Link label: %s\")\n" "(println \"Link type: %s\")") (org-dp-contents (car (org-dp-contents elem nil t)) t t) (org-element-property :type (car (org-dp-contents elem nil t)))))) org-link-re-with-space t) #+END_SRC #+BEGIN_SRC emacs-lisp :results none (require 'org-dp) (org-dp-map '(org-dp-rewire 'keyword nil 'prepend nil nil :key "LABEL" :value (lambda (_old_ elem) (org-dp-contents (car (org-dp-contents elem nil t)) t t))) org-link-re-with-space t) #+END_SRC This should give you a better idea of what the org-dp-rewire function arguments stand for. In generel, org-dp functions have extensive docstrings, so C-h f 'org-dp-xyz' is you friend. For org-dp, the (few) properties that are interpreted are most relevant, not so much the (many) properties that are parsed. See variable `org-dp-elem-props' for an overview. In general, when programming with org-dp, I often look into org-element.el, especially at the interpreters. > Second question: do I get it correctly that `org-element-put-property' > returns the "new" version of the element (link, in this case), with > everything as it was but the :raw-link property changed? ,----[ C-h f org-element-put-property RET ] | org-element-put-property is a compiled Lisp function in | `org-element.el'. [...] | Return modified element. `---- Yes! -- cheers, Thorsten