Browse Source

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.
Gustav Wikström 4 years ago
parent
commit
be2966abb5
2 changed files with 22 additions and 9 deletions
  1. 7 0
      etc/ORG-NEWS
  2. 15 9
      lisp/org-id.el

+ 7 - 0
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~ (=<C-c l>=) 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

+ 15 - 9
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)))