From be2966abb5a97f3503cf7c26a83d9e111fc0b8aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20Wikstr=C3=B6m?= Date: Sat, 6 Feb 2021 20:27:01 +0100 Subject: [PATCH] org-id: Add title to description when storing id links * lisp/org-id.el (org-id-store-link): Store title as link description if it is available. --- etc/ORG-NEWS | 7 +++++++ lisp/org-id.el | 24 +++++++++++++++--------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index ba769224f..ef100edd0 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -197,6 +197,13 @@ changed to :height 0.75))) #+end_src +*** Storing ID-links before first heading uses title as description + +Storing links to files using ~org-store-link~ (==) when +~org-id-link-to-org-use-id~ is not nil will now store the title as +description of the link, if available. If no title exists it falls +back to the filename as before. + * Version 9.4 ** Incompatible changes *** Possibly broken internal file links: please check and fix diff --git a/lisp/org-id.el b/lisp/org-id.el index d1e9aa977..76077b7b2 100644 --- a/lisp/org-id.el +++ b/lisp/org-id.el @@ -693,21 +693,27 @@ optional argument MARKERP, return the position as a new marker." ;;;###autoload (defun org-id-store-link () - "Store a link to the current entry, using its ID." + "Store a link to the current entry, using its ID. + +If before first heading store first title-keyword as description +or filename if no title." (interactive) (when (and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode)) (let* ((link (concat "id:" (org-id-get-create))) (case-fold-search nil) (desc (save-excursion (org-back-to-heading-or-point-min t) - (or (and (org-before-first-heading-p) - (file-name-nondirectory - (buffer-file-name (buffer-base-buffer)))) - (and (looking-at org-complex-heading-regexp) - (if (match-end 4) - (match-string 4) - (match-string 0))) - link)))) + (cond ((org-before-first-heading-p) + (let ((keywords (org-collect-keywords '("TITLE")))) + (if keywords + (car (alist-get "TITLE" keywords nil nil 'equal)) + (file-name-nondirectory + (buffer-file-name (buffer-base-buffer)))))) + ((looking-at org-complex-heading-regexp) + (if (match-end 4) + (match-string 4) + (match-string 0))) + (t link))))) (org-link-store-props :link link :description desc :type "id") link)))