0

I've stored several links to various shell commands and I would like to update each link description with the output of the correspoding shell command. For example:

View in text-mode: [[shell:echo "hello"][xxx]]
View in org-mode:  xxx

after running some post-processing function, I would get:

View in text-mode: [[shell:echo "hello"][hello​]]
View in org-mode:  hello

I know how to parse the different links and get the underlying shell command:

(org-element-map (org-element-parse-buffer) 'link
  (lambda (link)
    (when (string= (org-element-property :type link) "shell")
        (org-element-property :path link))))

but then I don't know how to modify the description using (org-element-context link)

1

The link description is the region in the buffer between the values of the properties contents-begin and contents-end:

#+begin_src emacs-lisp
(defun link-desc-region (link)
   (list (org-element-property :contents-begin link)
         (org-element-property :contents-end link)))

(defun replace-desc-in-link (link newdesc)
   (let ((region (link-desc-region link)))
       (goto-char (nth 0 region))
       (delete-region (nth 0 region) (nth 1 region))
       (insert newdesc))))
#+end_src

Evaluating (replace-desc-in-link (org-element-context) "hello") with point in the link should do what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.