1

I need to check if an org sub(heading) is a link to a file with a certain file type or not (for example only images of type png or jpg. I have no idea how to check if a string links to a file.

An example of a header

* [[/path/to/image.png]["this image"]]

What I have now in pseudocode.

(defun mp/org-expand-img-header ()
  (interactive)
  (let ((current-item) (cdr (assoc "ITEM" (org-entry-properties))))
    (when (eq (current-item) ??is-link??)
      (eq (?get-file-extension? (current-item)) ??is-in-extenstion-list??))))

Am I on the correct way to check if it's a link?

1
  • emacs.stackexchange.com/tags/elisp/info - elisp is almost never a good tag to use: it has a specific meaning as described in the link above and it is not meant for questions that just include some snippets of elisp (after all, many, if not most, questions on the Emacs SE are bound to include such a snippet: using the tag for all such questions would make it useless for searching).
    – NickD
    Mar 10, 2023 at 13:51

1 Answer 1

1

Best is to use M-x org-insert-link (C-c C-l) and then choose file: when creating the header (if you are visiting the buffer of some file, then you can quickly store a file: link using M-x org-store-link, and subsequently insert it using org-insert-link, see section Handling Links of the Org manual).

Anyway, also without using the above, you can use the following function to check if something is a file link

(defun org-file-link-p ()
  (let ((context (org-element-context)))
    (and (eq (car context) 'link)
         (string= (org-element-property :type context) "file")
         (member (file-name-extension (org-element-property :path context))
                 '("jpg" "png")))))

the cursor should be placed inside the link to work correctly.

If you'd like to determine it while the cursor is on a headline, but not on over the link, then you should jump to the headline title first, e.g. using:

(defun org-headline-jump-to-title ()
  (let (title-end)
    (save-excursion
      (beginning-of-line)
      (let ((context (org-element-context)))
    (when (eq (car context) 'headline)
        (let ((raw-value (org-element-property :raw-value context)))
          (setq title-end (search-forward raw-value))))))
    (if title-end
    (goto-char title-end)
      (message "Not on a headline"))))
4
  • 1
    Thanks. I already use C-c C-l to place the links. org-element-context was the one I need. I'm iterating over all the headers with org-map-entries so your first snippet should be enough Mar 10, 2023 at 12:44
  • Could you just add a check if the filetype is part of some list of filetypes? I'm only interested in the headings if it links to an jpg or png. Mar 10, 2023 at 12:45
  • Sure, I have updated the answer. However, it would be great if you could (still) update your question, instead of 'extending' it via a comment. B.t.w., for the check, you could also use image-supported-file-p if you'd like to include all 'supported' image types. Finally, you can accept an answer by clicking the check mark on the left (if you think it's appropriate). Mar 10, 2023 at 13:00
  • 1
    It was already in the question? I just updated the title as it was not clear. Thanks for the update. Smart as well to maybe put it in a seperate image-supported-file-p function. -- Yes! I was just waiting for you to add the file extension check. Mar 10, 2023 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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