Skip to content

Commit 372788a

Browse files
maxnikulinyantar92
authored andcommitted
ol-info: Define :insert-description function
* lisp/ol-info.el (org-info--link-file-node): New helper to parse info link info file (manual) name and node. (org-info-follow-link, org-info-export): Use `org-info--link-file-node'. (org-info-description-as-command): New function to create description for info links that may executed to view the manual. (org-link-parameters): Specify `org-info-description-as-command' as `:insert-description' for info links. (org-info-other-documents): Add URL of directory index. * testing/lisp/test-org-info.el (test-org-info/export): Add cases for texinfo export with link description. (test-org-info/link-file-node, test-org-info/description-as-command): New tests for new functions `org-info--link-file-node' and `org-info-description-as-command'. Use recently added :insert-description feature of `org-link'. Alternative separators between file name and node ":", "::", "#:" are preserved. Added interpretation of empty path or omitted file name as info dir index.
1 parent b7f4afe commit 372788a

File tree

2 files changed

+146
-21
lines changed

2 files changed

+146
-21
lines changed

lisp/ol-info.el

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
;;; Code:
3232

33+
(require 'subr-x) ; `string-trim', `string-remove-prefix'
3334
(require 'ol)
3435

3536
;; Declare external functions and variables
@@ -43,7 +44,8 @@
4344
(org-link-set-parameters "info"
4445
:follow #'org-info-open
4546
:export #'org-info-export
46-
:store #'org-info-store-link)
47+
:store #'org-info-store-link
48+
:insert-description #'org-info-description-as-command)
4749

4850
;; Implementation
4951
(defun org-info-store-link ()
@@ -63,24 +65,65 @@
6365
"Follow an Info file and node link specified by PATH."
6466
(org-info-follow-link path))
6567

68+
(defun org-info--link-file-node (path)
69+
"Extract file name and node from info link PATH.
70+
71+
Return cons consisting of file name and node name or \"Top\" if node
72+
part is not specified. Components may be separated by \":\" or by \"#\".
73+
File may be a virtual one, see `Info-virtual-files'."
74+
(if (not path)
75+
'("dir" . "Top")
76+
(string-match "\\`\\([^#:]*\\)\\(?:[#:]:?\\(.*\\)\\)?\\'" path)
77+
(let* ((node (match-string 2 path))
78+
;; Do not reorder, `string-trim' modifies match.
79+
(file (string-trim (match-string 1 path))))
80+
(cons
81+
(if (org-string-nw-p file) file "dir")
82+
(if (org-string-nw-p node) (string-trim node) "Top")))))
83+
84+
(defun org-info-description-as-command (link desc)
85+
"Info link description that can be pasted as command.
86+
87+
For the following LINK
88+
89+
\"info:elisp#Non-ASCII in Strings\"
90+
91+
the result is
92+
93+
info \"(elisp) Non-ASCII in Strings\"
94+
95+
that may be executed as shell command or evaluated by
96+
\\[eval-expression] (wrapped with parenthesis) to read the manual
97+
in Emacs.
98+
99+
Calling convention is similar to `org-link-make-description-function'.
100+
DESC has higher priority and returned when it is not nil or empty string.
101+
If LINK is not an info link then DESC is returned."
102+
(let* ((prefix "info:")
103+
(need-file-node (and (not (org-string-nw-p desc))
104+
(string-prefix-p prefix link))))
105+
(pcase (and need-file-node
106+
(org-info--link-file-node (string-remove-prefix prefix link)))
107+
;; Unlike (info "dir"), "info dir" shell command opens "(coreutils)dir invocation".
108+
(`("dir" . "Top") "info \"(dir)\"")
109+
(`(,file . "Top") (format "info %s" file))
110+
(`(,file . ,node) (format "info \"(%s) %s\"" file node))
111+
(_ desc))))
66112

67113
(defun org-info-follow-link (name)
68114
"Follow an Info file and node link specified by NAME."
69-
(if (or (string-match "\\(.*\\)\\(?:#\\|::\\)\\(.*\\)" name)
70-
(string-match "\\(.*\\)" name))
71-
(let ((filename (match-string 1 name))
72-
(nodename-or-index (or (match-string 2 name) "Top")))
73-
(require 'info)
74-
;; If nodename-or-index is invalid node name, then look it up
75-
;; in the index.
76-
(condition-case nil
77-
(Info-find-node filename nodename-or-index)
78-
(user-error (Info-find-node filename "Top")
79-
(condition-case nil
80-
(Info-index nodename-or-index)
81-
(user-error "Could not find '%s' node or index entry"
82-
nodename-or-index)))))
83-
(user-error "Could not open: %s" name)))
115+
(pcase-let ((`(,filename . ,nodename-or-index)
116+
(org-info--link-file-node name)))
117+
(require 'info)
118+
;; If nodename-or-index is invalid node name, then look it up
119+
;; in the index.
120+
(condition-case nil
121+
(Info-find-node filename nodename-or-index)
122+
(user-error (Info-find-node filename "Top")
123+
(condition-case nil
124+
(Info-index nodename-or-index)
125+
(user-error "Could not find '%s' node or index entry"
126+
nodename-or-index))))))
84127

85128
(defconst org-info-emacs-documents
86129
'("ada-mode" "auth" "autotype" "bovine" "calc" "ccmode" "cl" "dbus" "dired-x"
@@ -95,7 +138,8 @@
95138
Taken from <https://www.gnu.org/software/emacs/manual/html_mono/.>")
96139

97140
(defconst org-info-other-documents
98-
'(("libc" . "https://www.gnu.org/software/libc/manual/html_mono/libc.html")
141+
'(("dir" . "https://www.gnu.org/manual/manual.html") ; index
142+
("libc" . "https://www.gnu.org/software/libc/manual/html_mono/libc.html")
99143
("make" . "https://www.gnu.org/software/make/manual/make.html"))
100144
"Alist of documents generated from Texinfo source.
101145
When converting info links to HTML, links to any one of these manuals are
@@ -129,9 +173,7 @@ See `org-info-emacs-documents' and `org-info-other-documents' for details."
129173
(defun org-info-export (path desc format)
130174
"Export an info link.
131175
See `org-link-parameters' for details about PATH, DESC and FORMAT."
132-
(let* ((parts (split-string path "#\\|::"))
133-
(manual (car parts))
134-
(node (or (nth 1 parts) "Top")))
176+
(pcase-let ((`(,manual . ,node) (org-info--link-file-node path)))
135177
(pcase format
136178
(`html
137179
(format "<a href=\"%s#%s\">%s</a>"

testing/lisp/test-org-info.el

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
(should
2929
(equal (org-info-export "filename" nil 'html)
3030
"<a href=\"filename.html#Top\">filename</a>"))
31+
;; Directory index. Top anchor actually should not be added,
32+
;; but it should be rather rare case to add special code path.
33+
(should
34+
(equal (org-info-export "dir" nil 'html)
35+
"<a href=\"https://www.gnu.org/manual/manual.html#Top\">dir</a>"))
3136
;; When exporting to HTML, ensure node names are expanded according
3237
;; to (info "(texinfo) HTML Xref Node Name Expansion").
3338
(should
@@ -56,9 +61,87 @@
5661
"@ref{Top,,,filename,}"))
5762
(should
5863
(equal (org-info-export "filename#node" nil 'texinfo)
59-
"@ref{node,,,filename,}")))
64+
"@ref{node,,,filename,}"))
65+
;; "Top" is preserved, "::" as node separator.
66+
(should
67+
(equal "@ref{Top,,,emacs,}"
68+
(org-info-export "emacs::Top" nil 'texinfo)))
69+
70+
;; Description.
71+
(should
72+
(equal "@ref{Top,Emacs,,emacs,}"
73+
(org-info-export "emacs" "Emacs" 'texinfo)))
74+
(should
75+
(equal "@ref{Destructuring with pcase Patterns,pcase-let,,emacs,}"
76+
(org-info-export "emacs#Destructuring with pcase Patterns"
77+
"pcase-let" 'texinfo))))
6078

79+
(ert-deftest test-org-info/link-file-node ()
80+
"Test parse info links by `org-info--link-file-node'."
81+
(should (equal '("success" . "Hash Separator")
82+
(org-info--link-file-node "success#Hash Separator")))
83+
;; Other separators.
84+
(should (equal '("success" . "Single Colon Separator")
85+
(org-info--link-file-node "success:Single Colon Separator")))
86+
(should (equal '("success" . "Double Colon Separator")
87+
(org-info--link-file-node "success::Double Colon Separator")))
88+
(should (equal '("success" . "Hash Colon Separator")
89+
(org-info--link-file-node "success#:Hash Colon Separator")))
90+
;; Partial specification.
91+
(should (equal '("nodeless" . "Top")
92+
(org-info--link-file-node "nodeless")))
93+
(should (equal '("dir" . "Top")
94+
(org-info--link-file-node "")))
95+
(should (equal '("dir" . "Top")
96+
(org-info--link-file-node nil)))
97+
;; Feel free to change behavior of underspecified links,
98+
;; the case is added to check that it does not signal some error.
99+
(should (equal '("dir" . "broken")
100+
(org-info--link-file-node "#broken")))
101+
;; Trailing separator.
102+
(should (equal '("trailing-hash" . "Top")
103+
(org-info--link-file-node "trailing-hash#")))
104+
(should (equal '("trailing-single-colon" . "Top")
105+
(org-info--link-file-node "trailing-single-colon:")))
106+
(should (equal '("trailing-double-colon" . "Top")
107+
(org-info--link-file-node "trailing-double-colon::")))
108+
(should (equal '("trailing-hash-colon" . "Top")
109+
(org-info--link-file-node "trailing-hash-colon#:")))
110+
;; Trim spaces.
111+
(should (equal '("trim" . "Spaces")
112+
(org-info--link-file-node " trim # Spaces \t"))))
61113

114+
(ert-deftest test-org-info/description-as-command ()
115+
"Test `org-info-description-as-command'."
116+
(let ((cases
117+
'(("info file" "info:file")
118+
("info strip-top-hash" "info:strip-top-hash#Top")
119+
("info strip-top-single-colon" "info:strip-top-single-colon:Top")
120+
("info strip-top-double-colon" "info:strip-top-double-colon::Top")
121+
("info \"(pass) Hash\"" "info:pass#Hash")
122+
("info \"(pass) Double Colon\"" "info:pass:: Double Colon")
123+
("info \"(info) Advanced\"" "info:info:Advanced")
124+
("info \"(dir)\"" "info:")
125+
;; It actually works as "(dir) Top", test that no errors is signalled.
126+
("info \"(dir) Invalid\"" "info::Invalid")
127+
(nil "http://orgmode.org/index.html#Not-info-link"))))
128+
(dolist (expectation-input cases)
129+
(let ((expectation (car expectation-input))
130+
(input (cadr expectation-input)))
131+
(should (equal
132+
expectation
133+
(org-info-description-as-command input nil))))))
134+
(let ((cases
135+
'(("Override link" "info:ignored#Link" "Override link")
136+
("Fallback description" "http://not.info/link" "Fallback description")
137+
("Link is nil" nil "Link is nil"))))
138+
(dolist (expectation-input-desc cases)
139+
(let ((expectation (car expectation-input-desc))
140+
(input (cadr expectation-input-desc))
141+
(desc (nth 2 expectation-input-desc)))
142+
(should (equal
143+
expectation
144+
(org-info-description-as-command input desc)))))))
62145

63146
(provide 'test-org-info)
64147
;;; test-org-info.el ends here

0 commit comments

Comments
 (0)