Skip to content

Commit 1e34c5d

Browse files
author
Bastien Guerry
committed
org.el: Fontify links to current buffer when inserting a link.
* org.el (org-link-to-org-use-id): use `org-capture' instead of `org-remember' in the docstring. (org-link-fontify-links-to-this-file): New function to fontify links to the current buffer in `org-stored-links'. (org-store-link): Small code simplification. (org-link-prettify): Enclose literal links into <...> instead of [[...]]. (org-insert-link): Use `org-link-fontify-links-to-this-file'. Also allow completion over links' descriptions, as well as links destinations. When the user uses the description for completion, don't prompt again for a description. Thanks to Yagnesh Raghava Yakkala who suggested this.
1 parent 7fdd49d commit 1e34c5d

File tree

1 file changed

+52
-24
lines changed

1 file changed

+52
-24
lines changed

lisp/org.el

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,10 +1454,10 @@ create-if-interactive
14541454
job for remember, only use the ID if it already exists. The
14551455
purpose of this setting is to avoid proliferation of unwanted
14561456
IDs, just because you happen to be in an Org file when you
1457-
call `org-remember' that automatically and preemptively
1458-
creates a link. If you do want to get an ID link in a remember
1459-
template to an entry not having an ID, create it first by
1460-
explicitly creating a link to it, using `C-c C-l' first.
1457+
call `org-capture' that automatically and preemptively creates a
1458+
link. If you do want to get an ID link in a remember template to
1459+
an entry not having an ID, create it first by explicitly creating
1460+
a link to it, using `C-c C-l' first.
14611461

14621462
create-if-interactive-and-no-custom-id
14631463
Like create-if-interactive, but do not create an ID if there is
@@ -8876,19 +8876,16 @@ For file links, arg negates `org-context-in-file-links'."
88768876
link (org-make-link cpltxt)))
88778877
((and (featurep 'org-id)
88788878
(or (eq org-link-to-org-use-id t)
8879-
(and (eq org-link-to-org-use-id 'create-if-interactive)
8880-
(org-called-interactively-p 'any))
8881-
(and (eq org-link-to-org-use-id
8882-
'create-if-interactive-and-no-custom-id)
8883-
(org-called-interactively-p 'any)
8884-
(not custom-id))
8885-
(and org-link-to-org-use-id
8886-
(org-entry-get nil "ID"))))
8879+
(and (org-called-interactively-p 'any)
8880+
(or (eq org-link-to-org-use-id 'create-if-interactive)
8881+
(and (eq org-link-to-org-use-id
8882+
'create-if-interactive-and-no-custom-id)
8883+
(not custom-id))))
8884+
(and org-link-to-org-use-id (org-entry-get nil "ID"))))
88878885
;; We can make a link using the ID.
88888886
(setq link (condition-case nil
88898887
(prog1 (org-id-store-link)
8890-
(setq desc (plist-get org-store-link-plist
8891-
:description)))
8888+
(setq desc (plist-get org-store-link-plist :description)))
88928889
(error
88938890
;; probably before first headline, link to file only
88948891
(concat "file:"
@@ -9190,7 +9187,7 @@ The car of LINK must be a raw link the cdr of LINK must be either
91909187
a link description or nil."
91919188
(let ((desc (or (cadr link) "<no description>")))
91929189
(concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
9193-
"[[" (car link) "]]")))
9190+
"<" (car link) ">")))
91949191

91959192
;;;###autoload
91969193
(defun org-insert-link-global ()
@@ -9209,6 +9206,29 @@ This command can be called in any mode to insert a link in Org-mode syntax."
92099206
(org-insert-link nil (car l) (cadr l))
92109207
(insert "\n"))))
92119208

9209+
(defun org-link-fontify-links-to-this-file ()
9210+
"Fontify links to the current file in `org-stored-links'."
9211+
(let ((f (buffer-file-name)) a b)
9212+
(setq a (mapcar (lambda(l)
9213+
(let ((ll (car l)))
9214+
(when (and (string-match "^file:\\(.+\\)::" ll)
9215+
(equal f (expand-file-name (match-string 1 ll))))
9216+
ll)))
9217+
org-stored-links))
9218+
(when (featurep 'org-id)
9219+
(setq b (mapcar (lambda(l)
9220+
(let ((ll (car l)))
9221+
(when (and (string-match "^id:\\(.+\\)$" ll)
9222+
(equal f (expand-file-name
9223+
(or (org-id-find-id-file
9224+
(match-string 1 ll)) ""))))
9225+
ll)))
9226+
org-stored-links)))
9227+
(mapcar (lambda(l)
9228+
(put-text-property 0 (length l) 'face 'font-lock-comment-face l))
9229+
(delq nil (append a b)))))
9230+
9231+
(defvar org-link-links-in-this-file nil)
92129232
(defun org-insert-link (&optional complete-file link-location default-description)
92139233
"Insert a link. At the prompt, enter the link.
92149234

@@ -9257,7 +9277,7 @@ be used as the default description."
92579277
(desc region)
92589278
tmphist ; byte-compile incorrectly complains about this
92599279
(link link-location)
9260-
entry file all-prefixes)
9280+
entry file all-prefixes auto-desc)
92619281
(cond
92629282
(link-location) ; specified by arg, just use it.
92639283
((org-in-regexp org-bracket-link-regexp 1)
@@ -9278,13 +9298,16 @@ be used as the default description."
92789298
(setq link (org-file-complete-link complete-file)))
92799299
(t
92809300
;; Read link, with completion for stored links.
9281-
(with-output-to-temp-buffer "*Org Links*"
9282-
(princ "Insert a link.
9301+
(org-link-fontify-links-to-this-file)
9302+
(org-switch-to-buffer-other-window "*Org Links*")
9303+
(with-current-buffer "*Org Links*"
9304+
(erase-buffer)
9305+
(insert "Insert a link.
92839306
Use TAB to complete link prefixes, then RET for type-specific completion support\n")
92849307
(when org-stored-links
9285-
(princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
9286-
(princ (mapconcat 'org-link-prettify
9287-
(reverse org-stored-links) "\n"))))
9308+
(insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
9309+
(insert (mapconcat 'org-link-prettify
9310+
(reverse org-stored-links) "\n"))))
92889311
(let ((cw (selected-window)))
92899312
(select-window (get-buffer-window "*Org Links*" 'visible))
92909313
(with-current-buffer "*Org Links*" (setq truncate-lines t))
@@ -9307,12 +9330,16 @@ Use TAB to complete link prefixes, then RET for type-specific completion support
93079330
(append
93089331
(mapcar (lambda (x) (list (concat x ":")))
93099332
all-prefixes)
9310-
(mapcar 'car org-stored-links))
9333+
(mapcar 'car org-stored-links)
9334+
(mapcar 'cadr org-stored-links))
93119335
nil nil nil
93129336
'tmphist
9313-
(car (car org-stored-links)))))
9337+
(caar org-stored-links))))
93149338
(if (not (string-match "\\S-" link))
93159339
(error "No link selected"))
9340+
(mapc (lambda(l)
9341+
(when (equal link (cadr l)) (setq link (car l) auto-desc t)))
9342+
org-stored-links)
93169343
(if (or (member link all-prefixes)
93179344
(and (equal ":" (substring link -1))
93189345
(member (substring link 0 -1) all-prefixes)
@@ -9377,7 +9404,8 @@ Use TAB to complete link prefixes, then RET for type-specific completion support
93779404
(if org-make-link-description-function
93789405
(setq desc (funcall org-make-link-description-function link desc))
93799406
(if default-description (setq desc default-description)
9380-
(setq desc (read-string "Description: " desc))))
9407+
(setq desc (or (and auto-desc desc)
9408+
(read-string "Description: " desc)))))
93819409

93829410
(unless (string-match "\\S-" desc) (setq desc nil))
93839411
(if remove (apply 'delete-region remove))

0 commit comments

Comments
 (0)