ol-info.el (7704B)
1 ;;; ol-info.el --- Links to Info Nodes -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2004-2024 Free Software Foundation, Inc. 4 5 ;; Author: Carsten Dominik <carsten.dominik@gmail.com> 6 ;; Keywords: outlines, hypermedia, calendar, text 7 ;; URL: https://orgmode.org 8 ;; 9 ;; This file is part of GNU Emacs. 10 ;; 11 ;; GNU Emacs is free software: you can redistribute it and/or modify 12 ;; it under the terms of the GNU General Public License as published by 13 ;; the Free Software Foundation, either version 3 of the License, or 14 ;; (at your option) any later version. 15 16 ;; GNU Emacs is distributed in the hope that it will be useful, 17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 ;; GNU General Public License for more details. 20 21 ;; You should have received a copy of the GNU General Public License 22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. 23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 24 ;; 25 ;;; Commentary: 26 27 ;; This file implements links to Info nodes from within Org mode. 28 ;; Org mode loads this module by default - if this is not what you want, 29 ;; configure the variable `org-modules'. 30 31 ;;; Code: 32 33 (require 'org-macs) 34 (org-assert-version) 35 36 (require 'ol) 37 38 ;; Declare external functions and variables 39 40 (declare-function Info-find-node "info" 41 (filename nodename &optional no-going-back strict-case)) 42 (defvar Info-current-file) 43 (defvar Info-current-node) 44 45 ;; Install the link type 46 (org-link-set-parameters "info" 47 :follow #'org-info-open 48 :export #'org-info-export 49 :store #'org-info-store-link 50 :insert-description #'org-info-description-as-command) 51 52 ;; Implementation 53 (defun org-info-store-link (&optional _interactive?) 54 "Store a link to an Info file and node." 55 (when (eq major-mode 'Info-mode) 56 (let ((link (concat "info:" 57 (file-name-nondirectory Info-current-file) 58 "#" Info-current-node)) 59 (desc (concat (file-name-nondirectory Info-current-file) 60 "#" Info-current-node))) 61 (org-link-store-props :type "info" :file Info-current-file 62 :node Info-current-node 63 :link link :description desc) 64 link))) 65 66 (defun org-info-open (path _) 67 "Follow an Info file and node link specified by PATH." 68 (org-info-follow-link path)) 69 70 (defun org-info--link-file-node (path) 71 "Extract file name and node from info link PATH. 72 73 Return cons consisting of file name and node name or \"Top\" if node 74 part is not specified. Components may be separated by \":\" or by \"#\". 75 File may be a virtual one, see `Info-virtual-files'." 76 (if (not path) 77 '("dir" . "Top") 78 (string-match "\\`\\([^#:]*\\)\\(?:[#:]:?\\(.*\\)\\)?\\'" path) 79 (let* ((node (match-string 2 path)) 80 ;; Do not reorder, `org-trim' modifies match. 81 (file (org-trim (match-string 1 path)))) 82 (cons 83 (if (org-string-nw-p file) file "dir") 84 (if (org-string-nw-p node) (org-trim node) "Top"))))) 85 86 (defun org-info-description-as-command (link desc) 87 "Info link description that can be pasted as command. 88 89 For the following LINK 90 91 \"info:elisp#Non-ASCII in Strings\" 92 93 the result is 94 95 info \"(elisp) Non-ASCII in Strings\" 96 97 that may be executed as shell command or evaluated by 98 \\[eval-expression] (wrapped with parenthesis) to read the manual 99 in Emacs. 100 101 Calling convention is similar to `org-link-make-description-function'. 102 DESC has higher priority and returned when it is not nil or empty string. 103 If LINK is not an info link then DESC is returned." 104 (let* ((prefix "info:") 105 (need-file-node (and (not (org-string-nw-p desc)) 106 (string-prefix-p prefix link)))) 107 (pcase (and need-file-node 108 (org-info--link-file-node (org-unbracket-string prefix "" link))) 109 ;; Unlike (info "dir"), "info dir" shell command opens "(coreutils)dir invocation". 110 (`("dir" . "Top") "info \"(dir)\"") 111 (`(,file . "Top") (format "info %s" file)) 112 (`(,file . ,node) (format "info \"(%s) %s\"" file node)) 113 (_ desc)))) 114 115 (defun org-info-follow-link (name) 116 "Follow an Info file and node link specified by NAME." 117 (pcase-let ((`(,filename . ,nodename-or-index) 118 (org-info--link-file-node name))) 119 (require 'info) 120 ;; If nodename-or-index is invalid node name, then look it up 121 ;; in the index. 122 (condition-case nil 123 (Info-find-node filename nodename-or-index) 124 (user-error (Info-find-node filename "Top") 125 (condition-case nil 126 (Info-index nodename-or-index) 127 (user-error "Could not find '%s' node or index entry" 128 nodename-or-index)))))) 129 130 (defconst org-info-emacs-documents 131 '("ada-mode" "auth" "autotype" "bovine" "calc" "ccmode" "cl" "dbus" "dired-x" 132 "ebrowse" "ede" "ediff" "edt" "efaq-w32" "efaq" "eglot" "eieio" "eintr" 133 "elisp" "emacs-gnutls" "emacs-mime" "emacs" "epa" "erc" "ert" "eshell" 134 "eudc" "eww" "flymake" "forms" "gnus" "htmlfontify" "idlwave" "ido" "info" 135 "mairix-el" "message" "mh-e" "modus-themes" "newsticker" "nxml-mode" "octave-mode" 136 "org" "pcl-cvs" "pgg" "rcirc" "reftex" "remember" "sasl" "sc" "semantic" 137 "ses" "sieve" "smtpmail" "speedbar" "srecode" "todo-mode" "tramp" "transient" 138 "url" "use-package" "vhdl-mode" "vip" "viper" "vtable" "widget" "wisent" "woman") 139 "List of Emacs documents available. 140 Taken from <https://www.gnu.org/software/emacs/manual/html_mono/.>") 141 142 (defcustom org-info-other-documents 143 '(("dir" . "https://www.gnu.org/manual/manual.html") ; index 144 ("libc" . "https://www.gnu.org/software/libc/manual/html_mono/libc.html") 145 ("make" . "https://www.gnu.org/software/make/manual/make.html")) 146 "Alist of documents generated from Texinfo source. 147 When converting info links to HTML, links to any one of these manuals are 148 converted to use these URL." 149 :group 'org-link 150 :type '(alist :key-type string :value-type string) 151 :package-version '(Org . "9.7") 152 :safe t) 153 154 (defun org-info-map-html-url (filename) 155 "Return URL or HTML file associated to Info FILENAME. 156 If FILENAME refers to an official GNU document, return a URL pointing to 157 the official page for that document, e.g., use \"gnu.org\" for all Emacs 158 related documents. Otherwise, append \".html\" extension to FILENAME. 159 See `org-info-emacs-documents' and `org-info-other-documents' for details." 160 (cond ((cdr (assoc filename org-info-other-documents))) 161 ((member filename org-info-emacs-documents) 162 (format "https://www.gnu.org/software/emacs/manual/html_mono/%s.html" 163 filename)) 164 (t (concat filename ".html")))) 165 166 (defun org-info--expand-node-name (node) 167 "Expand Info NODE to HTML cross reference." 168 ;; See (info "(texinfo) HTML Xref Node Name Expansion") for the 169 ;; expansion rule. 170 (let ((node (replace-regexp-in-string 171 "\\([ \t\n\r]+\\)\\|\\([^a-zA-Z0-9]\\)" 172 (lambda (m) 173 (if (match-end 1) "-" (format "_%04x" (string-to-char m)))) 174 (org-trim node)))) 175 (cond ((string= node "") "") 176 ((string-match-p "\\`[0-9]" node) (concat "g_t" node)) 177 (t node)))) 178 179 (defun org-info-export (path desc format) 180 "Export an info link. 181 See `org-link-parameters' for details about PATH, DESC and FORMAT." 182 (pcase-let ((`(,manual . ,node) (org-info--link-file-node path))) 183 (pcase format 184 (`html 185 (format "<a href=\"%s#%s\">%s</a>" 186 (org-info-map-html-url manual) 187 (org-info--expand-node-name node) 188 (or desc path))) 189 (`texinfo 190 (let ((title (or desc ""))) 191 (format "@ref{%s,%s,,%s,}" node title manual))) 192 (_ nil)))) 193 194 (provide 'ol-info) 195 196 ;;; ol-info.el ends here