emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Thorsten Jolitz <tjolitz@gmail.com>
To: emacs-orgmode@gnu.org
Subject: Re: How to change a link?
Date: Sat, 18 Oct 2014 15:48:21 +0200	[thread overview]
Message-ID: <874mv14hga.fsf@gmail.com> (raw)
In-Reply-To: 87r3y6noca.fsf@wmi.amu.edu.pl

Marcin Borkowski <mbork@wmi.amu.edu.pl> 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

  reply	other threads:[~2014-10-18 13:48 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-15  0:19 How to change a link? Marcin Borkowski
2014-10-15  7:16 ` Thorsten Jolitz
2014-10-15  9:50   ` Marcin Borkowski
2014-10-15 10:02     ` Thorsten Jolitz
2014-10-15 16:17       ` Marcin Borkowski
2014-10-15 22:28         ` Thorsten Jolitz
2014-10-15 22:36           ` Marcin Borkowski
2014-10-15 22:51             ` Thorsten Jolitz
2014-10-15 23:06               ` Thorsten Jolitz
2014-10-15 10:19 ` Nicolas Goaziou
2014-10-15 21:30   ` Marcin Borkowski
2014-10-15 21:52     ` Nicolas Goaziou
2014-10-16 17:55       ` Marcin Borkowski
2014-10-16 20:10         ` Nicolas Goaziou
2014-10-16 21:46           ` Marcin Borkowski
2014-10-16 22:19         ` Thorsten Jolitz
2014-10-17  8:52           ` Marcin Borkowski
2014-10-17 15:25             ` Nick Dokos
2014-10-18 13:54               ` Thorsten Jolitz
2014-10-18  1:44           ` Marcin Borkowski
2014-10-18 13:48             ` Thorsten Jolitz [this message]
2014-10-20  0:02   ` Marcin Borkowski
2014-10-20 13:02     ` Nicolas Goaziou
2014-10-20 13:11       ` Nicolas Goaziou

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=874mv14hga.fsf@gmail.com \
    --to=tjolitz@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).