-
Notifications
You must be signed in to change notification settings - Fork 45
/
ol.el
2381 lines (2126 loc) · 90 KB
/
ol.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; ol.el --- Org links library -*- lexical-binding: t; -*-
;; Copyright (C) 2018-2024 Free Software Foundation, Inc.
;; Author: Carsten Dominik <[email protected]>
;; Keywords: outlines, hypermedia, calendar, text
;; This file is part of GNU Emacs.
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This library provides tooling to handle both external and internal
;; links.
;;; Code:
(require 'org-macs)
(org-assert-version)
(require 'org-compat)
(require 'org-macs)
(require 'org-fold)
(defvar clean-buffer-list-kill-buffer-names)
(defvar org-agenda-buffer-name)
(defvar org-comment-string)
(defvar org-highlight-links)
(defvar org-id-link-to-org-use-id)
(defvar org-inhibit-startup)
(defvar org-outline-regexp-bol)
(defvar org-src-source-file-name)
(defvar org-ts-regexp)
(declare-function calendar-cursor-to-date "calendar" (&optional error event))
(declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
(declare-function org-at-heading-p "org" (&optional _))
(declare-function org-back-to-heading "org" (&optional invisible-ok))
(declare-function org-before-first-heading-p "org" ())
(declare-function org-do-occur "org" (regexp &optional cleanup))
(declare-function org-element-at-point "org-element" (&optional pom cached-only))
(declare-function org-element-cache-refresh "org-element" (pos))
(declare-function org-element-cache-reset "org-element" (&optional all no-persistence))
(declare-function org-element-context "org-element" (&optional element))
(declare-function org-element-lineage "org-element-ast" (datum &optional types with-self))
(declare-function org-element-link-parser "org-element" ())
(declare-function org-element-property "org-element-ast" (property node))
(declare-function org-element-begin "org-element" (node))
(declare-function org-element-end "org-element" (node))
(declare-function org-element-type-p "org-element-ast" (node types))
(declare-function org-element-update-syntax "org-element" ())
(declare-function org-entry-get "org" (pom property &optional inherit literal-nil))
(declare-function org-find-property "org" (property &optional value))
(declare-function org-get-heading "org" (&optional no-tags no-todo no-priority no-comment))
(declare-function org-id-find-id-file "org-id" (id))
(declare-function org-insert-heading "org" (&optional arg invisible-ok top))
(declare-function org-load-modules-maybe "org" (&optional force))
(declare-function org-mark-ring-push "org" (&optional pos buffer))
(declare-function org-mode "org" ())
(declare-function org-occur "org" (regexp &optional keep-previous callback))
(declare-function org-open-file "org" (path &optional in-emacs line search))
(declare-function org-cycle-overview "org-cycle" ())
(declare-function org-restart-font-lock "org" ())
(declare-function org-run-like-in-org-mode "org" (cmd))
(declare-function org-fold-show-context "org-fold" (&optional key))
(declare-function org-src-coderef-format "org-src" (&optional element))
(declare-function org-src-coderef-regexp "org-src" (fmt &optional label))
(declare-function org-src-edit-buffer-p "org-src" (&optional buffer))
(declare-function org-src-source-buffer "org-src" ())
(declare-function org-src-source-type "org-src" ())
(declare-function org-time-stamp-format "org" (&optional long inactive))
(declare-function outline-next-heading "outline" ())
;;; Customization
(defgroup org-link nil
"Options concerning links in Org mode."
:tag "Org Link"
:group 'org)
(defcustom org-link-parameters nil
"Alist of properties that defines all the links in Org mode.
The key in each association is a string of the link type.
Subsequent optional elements make up a property list for that
type.
All properties are optional. However, the most important ones
are, in this order, `:follow', `:export', and `:store', described
below.
`:follow'
Function used to follow the link, when the `org-open-at-point'
command runs on it. It is called with two arguments: the path,
as a string, and a universal prefix argument.
Here, you may use `org-link-open-as-file' helper function for
types similar to \"file\".
`:export'
Function that accepts four arguments:
- the path, as a string,
- the description as a string, or nil,
- the export backend,
- the export communication channel, as a plist.
When nil, export for that type of link is delegated to the
backend.
`:store'
Function responsible for storing the link. See the function
`org-store-link-functions' for a description of the expected
arguments.
Additional properties provide more specific control over the
link.
`:activate-func'
Function to run at the end of Font Lock activation. It must
accept four arguments:
- the buffer position at the start of the link,
- the buffer position at its end,
- the path, as a string,
- a boolean, non-nil when the link has brackets.
`:complete'
Function that inserts a link with completion. The function
takes one optional prefix argument.
`:insert-description'
String or function used as a default when prompting users for a
link's description. A string is used as-is, a function is
called with two arguments: the link location (a string such as
\"~/foobar\", \"id:some-org-id\" or \"https://www.foo.com\")
and the description generated by `org-insert-link'. It should
return the description to use (this reflects the behavior of
`org-link-make-description-function'). If it returns nil, no
default description is used, but no error is thrown (from the
user's perspective, this is equivalent to a default description
of \"\").
`:display'
Value for `invisible' text property on the hidden parts of the
link. The most useful value is `full', which will not fold the
link in descriptive display. Default is `org-link'.
`:face'
Face for the link, or a function returning a face. The
function takes one argument, which is the path.
The default face is `org-link'.
`:help-echo'
String or function used as a value for the `help-echo' text
property. The function is called with one argument, the help
string to display, and should return a string.
`:htmlize-link'
Function or plist for the `htmlize-link' text property. The
function takes no argument.
Default is (:uri \"type:path\")
`:keymap'
Active keymap when point is on the link. Default is
`org-mouse-map'.
`:mouse-face'
Face used when hovering over the link. Default is
`highlight'."
:group 'org-link
:package-version '(Org . "9.1")
:type '(alist :tag "Link display parameters"
:value-type plist))
(defun org-link--set-link-display (symbol value)
"Set `org-link-descriptive' (SYMBOL) to VALUE.
Also, ensure that links are updated in current buffer.
This function is intended to be used as a :set function."
(set symbol value)
(dolist (buf (org-buffer-list))
(with-current-buffer buf
(org-restart-font-lock))))
(defcustom org-link-descriptive t
"Non-nil means Org displays descriptive links.
E.g. [[https://orgmode.org][Org website]] is displayed as
\"Org Website\", hiding the link itself and just displaying its
description. When set to nil, Org displays the full links
literally.
You can interactively set the value of this variable by calling
`org-toggle-link-display' or from the \"Org > Hyperlinks\" menu."
:group 'org-link
:set #'org-link--set-link-display
:type 'boolean
:safe #'booleanp)
(defcustom org-link-make-description-function nil
"Function to use for generating link descriptions from links.
This function must take two parameters: the first one is the
link, the second one is the description generated by
`org-insert-link'. The function should return the description to
use. If it returns nil, no default description is used, but no
error is thrown (from the user’s perspective, this is equivalent
to a default description of \"\")."
:group 'org-link
:type '(choice (const nil) (function))
:safe #'null)
(defcustom org-link-file-path-type 'adaptive
"How the path name in file links should be stored.
Valid values are:
relative Relative to the current directory, i.e. the directory of the file
into which the link is being inserted.
absolute Absolute path, if possible with ~ for home directory.
noabbrev Absolute path, no abbreviation of home directory.
adaptive Use relative path for files in the current directory and sub-
directories of it. For other files, use an absolute path.
Alternatively, users may supply a custom function that takes the
filename in the link as an argument and returns the path."
:group 'org-link
:type '(choice
(const relative)
(const absolute)
(const noabbrev)
(const adaptive)
(function))
:package-version '(Org . "9.5")
:safe #'symbolp)
(defcustom org-link-abbrev-alist nil
"Alist of link abbreviations.
The car of each element is a string, to be replaced at the start of a link.
The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
links in Org buffers can have an optional tag after a double colon, e.g.,
[[linkkey:tag][description]]
The `linkkey' must be a single word, starting with a letter, followed
by letters, numbers, `-' or `_'.
If REPLACE is a string, the tag will simply be appended to create the link.
If the string contains \"%s\", the tag will be inserted there. If the string
contains \"%h\", it will cause a url-encoded version of the tag to be inserted
at that point (see the function `url-hexify-string'). If the string contains
the specifier \"%(my-function)\", then the custom function `my-function' will
be invoked: this function takes the tag as its only argument and must return
a string.
REPLACE may also be a function that will be called with the tag as the
only argument to create the link, which should be returned as a string.
See the manual for examples."
:group 'org-link
:type '(repeat
(cons (string :tag "Protocol")
(choice
(string :tag "Format")
(function))))
:safe (lambda (alist)
(when (listp alist)
(catch :unsafe
(dolist (val alist)
(pcase val
(`(,(pred stringp) . ,(pred stringp)) t)
(_ (throw :unsafe nil))))
t))))
(defgroup org-link-follow nil
"Options concerning following links in Org mode."
:tag "Org Follow Link"
:group 'org-link)
(defcustom org-link-translation-function nil
"Function to translate links with different syntax to Org syntax.
This can be used to translate links created for example by the Planner
or emacs-wiki packages to Org syntax.
The function must accept two parameters, a TYPE containing the link
protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
which is everything after the link protocol. It should return a cons
with possibly modified values of type and path."
:group 'org-link-follow
:type '(choice (const nil) (function))
:safe #'null)
(defcustom org-link-frame-setup
'((vm . vm-visit-folder-other-frame)
(vm-imap . vm-visit-imap-folder-other-frame)
(gnus . org-gnus-no-new-news)
(file . find-file-other-window)
(wl . wl-other-frame))
"Setup the frame configuration for following links.
When following a link with Emacs, it may often be useful to display
this link in another window or frame. This variable can be used to
set this up for the different types of links.
For VM, use any of
`vm-visit-folder'
`vm-visit-folder-other-window'
`vm-visit-folder-other-frame'
For Gnus, use any of
`gnus'
`gnus-other-frame'
`org-gnus-no-new-news'
For FILE, use any of
`find-file'
`find-file-other-window'
`find-file-other-frame'
For Wanderlust use any of
`wl'
`wl-other-frame'
For the calendar, use the variable `calendar-setup'.
For BBDB, it is currently only possible to display the matches in
another window."
:group 'org-link-follow
:type '(list
(cons (const vm)
(choice
(const vm-visit-folder)
(const vm-visit-folder-other-window)
(const vm-visit-folder-other-frame)))
(cons (const vm-imap)
(choice
(const vm-visit-imap-folder)
(const vm-visit-imap-folder-other-window)
(const vm-visit-imap-folder-other-frame)))
(cons (const gnus)
(choice
(const gnus)
(const gnus-other-frame)
(const org-gnus-no-new-news)))
(cons (const file)
(choice
(const find-file)
(const find-file-other-window)
(const find-file-other-frame)))
(cons (const wl)
(choice
(const wl)
(const wl-other-frame)))))
(defcustom org-link-search-must-match-exact-headline 'query-to-create
"Control fuzzy link behavior when specific matches not found.
When nil, if a fuzzy link does not match a more specific
target (such as a heading, named block, target, or code ref),
attempt a regular text search. When set to the special value
`query-to-create', offer to create a new heading matching the
link instead. Otherwise, signal an error rather than attempting
a regular text search.
This option only affects behavior in Org buffers. Spaces and
statistics cookies are ignored during heading searches."
:group 'org-link-follow
:version "24.1"
:type '(choice
(const :tag "Use fuzzy text search" nil)
(const :tag "Match only exact headline" t)
(const :tag "Match exact headline or query to create it"
query-to-create))
:safe #'symbolp)
(defcustom org-link-use-indirect-buffer-for-internals nil
"Non-nil means use indirect buffer to display infile links.
Activating internal links (from one location in a file to another location
in the same file) normally just jumps to the location. When the link is
activated with a `\\[universal-argument]' prefix (or with mouse-3), the link \
is displayed in
another window. When this option is set, the other window actually displays
an indirect buffer clone of the current buffer, to avoid any visibility
changes to the current buffer."
:group 'org-link-follow
:type 'boolean
:safe #'booleanp)
(defcustom org-link-shell-confirm-function 'yes-or-no-p
"Non-nil means ask for confirmation before executing shell links.
Shell links can be dangerous: just think about a link
[[shell:rm -rf ~/*][Web Search]]
This link would show up in your Org document as \"Web Search\",
but really it would remove your entire home directory.
Therefore we advise against setting this variable to nil.
Just change it to `y-or-n-p' if you want to confirm with a
single keystroke rather than having to type \"yes\"."
:group 'org-link-follow
:type '(choice
(const :tag "with yes-or-no (safer)" yes-or-no-p)
(const :tag "with y-or-n (faster)" y-or-n-p)
(const :tag "no confirmation (dangerous)" nil)))
(defcustom org-link-shell-skip-confirm-regexp ""
"Regexp to skip confirmation for shell links."
:group 'org-link-follow
:version "24.1"
:type 'regexp)
(defcustom org-link-elisp-confirm-function 'yes-or-no-p
"Non-nil means ask for confirmation before executing Emacs Lisp links.
Elisp links can be dangerous: just think about a link
[[elisp:(shell-command \"rm -rf ~/*\")][Web Search]]
This link would show up in your Org document as \"Web Search\",
but really it would remove your entire home directory.
Therefore we advise against setting this variable to nil.
Just change it to `y-or-n-p' if you want to confirm with a
single keystroke rather than having to type \"yes\"."
:group 'org-link-follow
:type '(choice
(const :tag "with yes-or-no (safer)" yes-or-no-p)
(const :tag "with y-or-n (faster)" y-or-n-p)
(const :tag "no confirmation (dangerous)" nil)))
(defcustom org-link-elisp-skip-confirm-regexp ""
"A regexp to skip confirmation for Elisp links."
:group 'org-link-follow
:version "24.1"
:type 'regexp)
(defgroup org-link-store nil
"Options concerning storing links in Org mode."
:tag "Org Store Link"
:group 'org-link)
(defcustom org-link-context-for-files t
"Non-nil means file links from `org-store-link' contain context.
\\<org-mode-map>
A search string is added to the file name with \"::\" as separator
and used to find the context when the link is activated by the command
`org-open-at-point'. When this option is t, the entire active region
is be placed in the search string of the file link. If set to a
positive integer N, only the first N lines of context are stored.
Using a prefix argument to the command `org-store-link' \
\(`\\[universal-argument] \\[org-store-link]')
negates this setting for the duration of the command."
:group 'org-link-store
:type '(choice boolean integer)
:safe (lambda (val) (or (booleanp val) (integerp val))))
(defcustom org-link-email-description-format "Email %c: %s"
"Format of the description part of a link to an email or Usenet message.
The following %-escapes will be replaced by corresponding information:
%F full \"From\" field
%f name, taken from \"From\" field, address if no name
%T full \"To\" field
%t first name in \"To\" field, address if no name
%c correspondent. Usually \"from NAME\", but if you sent it yourself, it
will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
%s subject
%d date
%m message-id.
You may use normal field width specification between the % and the letter.
This is for example useful to limit the length of the subject.
Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
:group 'org-link-store
:package-version '(Org . "9.3")
:type 'string
:safe #'stringp)
(defcustom org-link-from-user-regexp
(let ((mail (and (org-string-nw-p user-mail-address)
(format "\\<%s\\>" (regexp-quote user-mail-address))))
(name (and (org-string-nw-p user-full-name)
(format "\\<%s\\>" (regexp-quote user-full-name)))))
(if (and mail name) (concat mail "\\|" name) (or mail name)))
"Regexp matched against the \"From:\" header of an email or Usenet message.
It should match if the message is from the user him/herself."
:group 'org-link-store
:type 'regexp
:safe #'stringp)
(defcustom org-link-keep-stored-after-insertion nil
"Non-nil means keep link in list for entire session.
\\<org-mode-map>
The command `org-store-link' adds a link pointing to the current
location to an internal list. These links accumulate during a session.
The command `org-insert-link' can be used to insert links into any
Org file (offering completion for all stored links).
When this option is nil, every link which has been inserted once using
`\\[org-insert-link]' will be removed from the list, to make completing the \
unused
links more efficient."
:group 'org-link-store
:type 'boolean
:safe #'booleanp)
;;; Public variables
(defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
(format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
border border border))
"Regular expression matching a link target.")
(defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
"Regular expression matching a radio target.")
(defvar-local org-target-link-regexp nil
"Regular expression matching radio targets in plain text.")
(defconst org-target-link-regexp-limit (ash 2 12)
"Maximum allowed length of regexp.
The number should generally be ~order of magnitude smaller than
MAX_BUF_SIZE in src/regex-emacs.c. The number of regexp-emacs.c is
for processed regexp, which appears to be larger compared to the
original string length.")
(defvar-local org-target-link-regexps nil
"List of regular expressions matching radio targets in plain text.
This list is non-nil, when a single regexp would be too long to match
all the possible targets, exceeding Emacs's regexp length limit.")
(defvar org-link-types-re nil
"Matches a link that has a url-like prefix like \"http:\".")
(defvar org-link-angle-re nil
"Matches link with angular brackets, spaces are allowed.")
(defvar org-link-plain-re nil
"Matches plain link, without spaces.
Group 1 must contain the link type (i.e. https).
Group 2 must contain the link path (i.e. //example.com).
Used by `org-element-link-parser'.")
(defvar org-link-bracket-re nil
"Matches a link in double brackets.")
(defvar org-link-any-re nil
"Regular expression matching any link.")
(defvar-local org-link-abbrev-alist-local nil
"Buffer-local version of `org-link-abbrev-alist', which see.
The value of this is taken from the LINK keywords.")
(defvar org-stored-links nil
"Contains the links stored with `org-store-link'.")
(defvar org-store-link-plist nil
"Plist with info about the most recently link created with `org-store-link'.")
(defvar org-create-file-search-functions nil
"List of functions to construct the right search string for a file link.
These functions are called in turn with point at the location to
which the link should point.
A function in the hook should first test if it would like to
handle this file type, for example by checking the `major-mode'
or the file extension. If it decides not to handle this file, it
should just return nil to give other functions a chance. If it
does handle the file, it must return the search string to be used
when following the link. The search string will be part of the
file link, given after a double colon, and `org-open-at-point'
will automatically search for it. If special measures must be
taken to make the search successful, another function should be
added to the companion hook `org-execute-file-search-functions',
which see.
A function in this hook may also use `org-link-store-props' and set
`:description' property to provide a suggestion for the descriptive
text to be used for this link when it gets inserted into an Org buffer
with \\[org-insert-link].")
(defvar org-execute-file-search-functions nil
"List of functions to execute a file search triggered by a link.
Functions added to this hook must accept a single argument, the
search string that was part of the file link, the part after the
double colon. The function must first check if it would like to
handle this search, for example by checking the `major-mode' or
the file extension. If it decides not to handle this search, it
should just return nil to give other functions a chance. If it
does handle the search, it must return a non-nil value to keep
other functions from trying.
Each function can access the current prefix argument through the
variable `current-prefix-arg'. Note that a single prefix is used
to force opening a link in Emacs, so it may be good to only use a
numeric or double prefix to guide the search function.
In case this is needed, a function in this hook can also restore
the window configuration before `org-open-at-point' was called using:
(set-window-configuration org-window-config-before-follow-link)")
(defvar org-open-link-functions nil
"Hook for functions finding a plain text link.
These functions must take a single argument, the link content.
They will be called for links that look like [[link text][description]]
when LINK TEXT does not have a protocol like \"http:\" and does not look
like a filename (e.g. \"./blue.png\").
These functions will be called *before* Org attempts to resolve the
link by doing text searches in the current buffer - so if you want a
link \"[[target]]\" to still find \"<<target>>\", your function should
handle this as a special case.
When the function does handle the link, it must return a non-nil value.
If it decides that it is not responsible for this link, it must return
nil to indicate that Org can continue with other options like
exact and fuzzy text search.")
;;; Internal Variables
(defconst org-link--forbidden-chars "]\t\n\r<>"
"Characters forbidden within a link, as a string.")
(defvar org-link--history nil
"History for inserted links.")
(defvar org-link--insert-history nil
"Minibuffer history for links inserted with `org-insert-link'.")
(defvar org-link--search-failed nil
"Non-nil when last link search failed.")
;;; Internal Functions
(defun org-link--try-special-completion (type)
"If there is completion support for link type TYPE, offer it."
(let ((fun (org-link-get-parameter type :complete)))
(if (functionp fun)
(funcall fun)
(read-string "Link (no completion support): " (concat type ":")))))
(defun org-link--prettify (link)
"Return a human-readable representation of LINK.
The car of LINK must be a raw link. The cdr of LINK must be
either a link description or nil."
(let ((desc (or (cadr link) "<no description>")))
(concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
"<" (car link) ">")))
(defun org-link--decode-compound (hex)
"Unhexify Unicode hex-chars HEX.
E.g. \"%C3%B6\" is the German o-Umlaut. Note: this function also
decodes single byte encodings like \"%E1\" (a-acute) if not
followed by another \"%[A-F0-9]{2}\" group."
(save-match-data
(let* ((bytes (cdr (split-string hex "%")))
(ret "")
(eat 0)
(sum 0))
(while bytes
(let* ((val (string-to-number (pop bytes) 16))
(shift-xor
(if (= 0 eat)
(cond
((>= val 252) (cons 6 252))
((>= val 248) (cons 5 248))
((>= val 240) (cons 4 240))
((>= val 224) (cons 3 224))
((>= val 192) (cons 2 192))
(t (cons 0 0)))
(cons 6 128))))
(when (>= val 192) (setq eat (car shift-xor)))
(setq val (logxor val (cdr shift-xor)))
(setq sum (+ (ash sum (car shift-xor)) val))
(when (> eat 0) (setq eat (- eat 1)))
(cond
((= 0 eat) ;multi byte
(setq ret (concat ret (char-to-string sum)))
(setq sum 0))
((not bytes) ; single byte(s)
(setq ret (org-link--decode-single-byte-sequence hex))))))
ret)))
(defun org-link--decode-single-byte-sequence (hex)
"Unhexify hex-encoded single byte character sequence HEX."
(mapconcat (lambda (byte)
(char-to-string (string-to-number byte 16)))
(cdr (split-string hex "%"))
""))
(defun org-link--fontify-links-to-this-file ()
"Fontify links to the current file in `org-stored-links'."
(let ((f (buffer-file-name)) a b)
(setq a (mapcar (lambda(l)
(let ((ll (car l)))
(when (and (string-match "^file:\\(.+\\)::" ll)
(equal f (expand-file-name (match-string 1 ll))))
ll)))
org-stored-links))
(when (featurep 'org-id)
(setq b (mapcar (lambda(l)
(let ((ll (car l)))
(when (and (string-match "^id:\\(.+\\)$" ll)
(equal f (expand-file-name
(or (org-id-find-id-file
(match-string 1 ll)) ""))))
ll)))
org-stored-links)))
(mapcar (lambda(l)
(put-text-property 0 (length l) 'face 'font-lock-comment-face l))
(delq nil (append a b)))))
(defun org-link--buffer-for-internals ()
"Return buffer used for displaying the target of internal links."
(cond
((not org-link-use-indirect-buffer-for-internals) (current-buffer))
((string-suffix-p "(Clone)" (buffer-name))
(message "Buffer is already a clone, not making another one")
;; We also do not modify visibility in this case.
(current-buffer))
(t ;make a new indirect buffer for displaying the link
(let* ((indirect-buffer-name (concat (buffer-name) "(Clone)"))
(indirect-buffer
(or (get-buffer indirect-buffer-name)
(make-indirect-buffer (current-buffer)
indirect-buffer-name
'clone))))
(with-current-buffer indirect-buffer (org-cycle-overview))
indirect-buffer))))
(defun org-link--search-radio-target (target)
"Search a radio target matching TARGET in current buffer.
White spaces are not significant."
(let ((re (format "<<<%s>>>"
(mapconcat #'regexp-quote
(split-string target)
"[ \t]+\\(?:\n[ \t]*\\)?")))
(origin (point)))
(goto-char (point-min))
(catch :radio-match
(while (re-search-forward re nil t)
(forward-char -1)
(let ((object (org-element-context)))
(when (org-element-type-p object 'radio-target)
(goto-char (org-element-begin object))
(org-fold-show-context 'link-search)
(throw :radio-match nil))))
(goto-char origin)
(user-error "No match for radio target: %s" target))))
(defun org-link--context-from-region ()
"Return context string from active region, or nil."
(when (org-region-active-p)
(let ((context (buffer-substring (region-beginning) (region-end))))
(when (and (wholenump org-link-context-for-files)
(> org-link-context-for-files 0))
(let ((lines (org-split-string context "\n")))
(setq context
(mapconcat #'identity
(cl-subseq lines 0 org-link-context-for-files)
"\n"))))
context)))
(defun org-link--normalize-string (string &optional context)
"Remove ignored contents from STRING string and return it.
This function removes contiguous white spaces and statistics
cookies. When optional argument CONTEXT is non-nil, it assumes
STRING is a context string, and also removes special search
syntax around the string."
(let ((string
(org-trim
(replace-regexp-in-string
(rx (one-or-more (any " \t")))
" "
(replace-regexp-in-string
;; Statistics cookie regexp.
(rx (seq "[" (0+ digit) (or "%" (seq "/" (0+ digit))) "]"))
" "
string)))))
(when context
(while (cond ((and (string-prefix-p "(" string)
(string-suffix-p ")" string))
(setq string (org-trim (substring string 1 -1))))
((string-match "\\`[#*]+[ \t]*" string)
(setq string (substring string (match-end 0))))
(t nil))))
string))
(defun org-link--reveal-maybe (region _)
"Reveal folded link in REGION when needed.
This function is intended to be used as :fragile property of a folding
spec."
(org-with-point-at (car region)
(not (org-in-regexp org-link-any-re))))
(defun org-link--try-link-store-functions (interactive?)
"Try storing external links, prompting if more than one is possible.
Each function returned by `org-store-link-functions' is called in
turn. If multiple functions return non-nil, prompt for which
link should be stored.
Argument INTERACTIVE? indicates whether `org-store-link' was
called interactively and is passed to the link store functions.
Return t when a link has been stored in `org-link-store-props'."
(let ((results-alist nil))
(dolist (f (org-store-link-functions))
(when (condition-case nil
(funcall f interactive?)
;; FIXME: The store function used (< Org 9.7) to accept
;; no arguments; provide backward compatibility support
;; for them.
(wrong-number-of-arguments
(funcall f)))
;; FIXME: return value is not link's plist, so we store the
;; new value before it is modified. It would be cleaner to
;; ask store link functions to return the plist instead.
(push (cons f (copy-sequence org-store-link-plist))
results-alist)))
(pcase results-alist
(`nil nil)
(`((,_ . ,_)) t) ;single choice: nothing to do
(`((,name . ,_) . ,_)
;; Reinstate link plist associated to the chosen
;; function.
(apply #'org-link-store-props
(cdr (assoc-string
(completing-read
(format "Store link with (default %s): " name)
(mapcar #'car results-alist)
nil t nil nil (symbol-name name))
results-alist)))
t))))
(defun org-link--add-to-stored-links (link desc)
"Add LINK to `org-stored-links' with description DESC."
(cond
((not (member (list link desc) org-stored-links))
(push (list link desc) org-stored-links)
(message "Stored: %s" (or desc link)))
((equal (list link desc) (car org-stored-links))
(message "This link has already been stored"))
(t
(setq org-stored-links
(delete (list link desc) org-stored-links))
(push (list link desc) org-stored-links)
(message "Link moved to front: %s" (or desc link)))))
(defun org-link--file-link-to-here ()
"Return as (LINK . DESC) a file link with search string to here."
(let ((link (concat "file:"
(abbreviate-file-name
(buffer-file-name (buffer-base-buffer)))))
desc)
(when org-link-context-for-files
(pcase (org-link-precise-link-target)
(`nil nil)
(`(,search-string ,search-desc ,_position)
(setq link (format "%s::%s" link search-string))
(setq desc search-desc))))
(cons link desc)))
;;; Public API
(defun org-link-types ()
"Return a list of known link types."
(mapcar #'car org-link-parameters))
(defun org-link-get-parameter (type key)
"Get TYPE link property for KEY.
TYPE is a string and KEY is a plist keyword. See
`org-link-parameters' for supported keywords."
(plist-get (cdr (assoc type org-link-parameters))
key))
(defun org-link-set-parameters (type &rest parameters)
"Set link TYPE properties to PARAMETERS.
PARAMETERS should be keyword value pairs. See
`org-link-parameters' for supported keys."
(when (member type '("coderef" "custom-id" "fuzzy" "radio"))
(error "Cannot override reserved link type: %S" type))
(let ((data (assoc type org-link-parameters)))
(if data (setcdr data (org-combine-plists (cdr data) parameters))
(push (cons type parameters) org-link-parameters)
(org-link-make-regexps)
(when (featurep 'org-element) (org-element-update-syntax)))))
;; This way, one can add multiple functions as, say, :follow parameter.
;; For example,
;; (add-function :before-until (org-link-get-parameter "id" :follow) #'my-function)
;; See https://orgmode.org/list/[email protected]
(gv-define-setter org-link-get-parameter (value type key)
`(org-link-set-parameters ,type ,key ,value))
(defun org-link-make-regexps ()
"Update the link regular expressions.
This should be called after the variable `org-link-parameters' has changed."
(let ((types-re (regexp-opt (org-link-types) t)))
(setq org-link-types-re
(concat "\\`" types-re ":")
org-link-angle-re
(format "<%s:\\([^>\n]*\\(?:\n[ \t]*[^> \t\n][^>\n]*\\)*\\)>"
types-re)
org-link-plain-re
(let* ((non-space-bracket "[^][ \t\n()<>]")
(parenthesis
`(seq (any "<([")
(0+ (or (regex ,non-space-bracket)
(seq (any "<([")
(0+ (regex ,non-space-bracket))
(any "])>"))))
(any "])>"))))
;; Heuristics for an URL link inspired by
;; https://daringfireball.net/2010/07/improved_regex_for_matching_urls
(rx-to-string
`(seq word-start
;; Link type: match group 1.
(regexp ,types-re)
":"
;; Link path: match group 2.
(group
(1+ (or (regex ,non-space-bracket)
,parenthesis))
(or (regexp "[^[:punct:][:space:]\n]")
;; Allow "-" punctuation, as an exception
;; See https://list.orgmode.org/orgmode/[email protected]/
;; This is also in line with the heuristics
;; above - it also does not include "-"
;; punctuation.
?-
?/
,parenthesis)))))
org-link-bracket-re
(rx (seq "[["
;; URI part: match group 1.
(group
(one-or-more
(or (not (any "[]\\"))
(and "\\" (zero-or-more "\\\\") (any "[]"))
(and (one-or-more "\\") (not (any "[]"))))))
"]"
;; Description (optional): match group 2.
(opt "[" (group (+? anything)) "]")
"]"))
org-link-any-re
(concat "\\(" org-link-bracket-re "\\)\\|\\("
org-link-angle-re "\\)\\|\\("
org-link-plain-re "\\)"))))
(defun org-link-complete-file (&optional arg)
"Create a file link using completion.
With optional ARG \\='(16), abbreviate the file name in the link."
(let ((file (read-file-name "File: "))
(pwd (file-name-as-directory (expand-file-name ".")))
(pwd1 (file-name-as-directory (abbreviate-file-name
(expand-file-name ".")))))
(cond ((equal arg '(16))
(concat "file:"
(abbreviate-file-name (expand-file-name file))))
((string-match
(concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
(concat "file:" (match-string 1 file)))
((string-match
(concat "^" (regexp-quote pwd) "\\(.+\\)")
(expand-file-name file))
(concat "file:"
(match-string 1 (expand-file-name file))))
(t (concat "file:" file)))))
(defun org-link-email-description (&optional fmt)
"Return the description part of an email link.
This takes information from `org-store-link-plist' and formats it
according to FMT (default from `org-link-email-description-format')."
(setq fmt (or fmt org-link-email-description-format))
(let* ((p org-store-link-plist)
(to (plist-get p :toaddress))
(from (plist-get p :fromaddress))
(table