emacs-orgmode
[Top][All Lists]
Advanced

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

Re: [O] How to change a link?


From: Nicolas Goaziou
Subject: Re: [O] How to change a link?
Date: Wed, 15 Oct 2014 23:52:23 +0200

Marcin Borkowski <address@hidden> 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.

You may also directly work on the buffer, with something like

  (org-with-wide-buffer
   (goto-char (point-min))
   (while (re-search-forward org-bracket-link-regexp nil t)
     (forward-char -1)
     (let ((context (org-element-context)))
       (when (and (eq (org-element-type context) 'link)
                  (member (org-element-property :type context)
                          '("custom-id" "fuzzy")))
         (goto-char (+ (org-element-property :begin context) 2))
         (insert "file:path/to-file::")))))


Regards,

-- 
Nicolas Goaziou



reply via email to

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