ol-info.el (7464B)
1 ;;; ol-info.el --- Links to Info Nodes -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2004-2023 Free Software Foundation, Inc. 4 5 ;; Author: Carsten Dominik <carsten.dominik@gmail.com> 6 ;; Keywords: outlines, hypermedia, calendar, wp 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 () 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" "eieio" "eintr" "elisp" 133 "emacs-gnutls" "emacs-mime" "emacs" "epa" "erc" "ert" "eshell" "eudc" "eww" 134 "flymake" "forms" "gnus" "htmlfontify" "idlwave" "ido" "info" "mairix-el" 135 "message" "mh-e" "newsticker" "nxml-mode" "octave-mode" "org" "pcl-cvs" 136 "pgg" "rcirc" "reftex" "remember" "sasl" "sc" "semantic" "ses" "sieve" 137 "smtpmail" "speedbar" "srecode" "todo-mode" "tramp" "url" "vip" "viper" 138 "widget" "wisent" "woman") 139 "List of Emacs documents available. 140 Taken from <https://www.gnu.org/software/emacs/manual/html_mono/.>") 141 142 (defconst 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 150 (defun org-info-map-html-url (filename) 151 "Return URL or HTML file associated to Info FILENAME. 152 If FILENAME refers to an official GNU document, return a URL pointing to 153 the official page for that document, e.g., use \"gnu.org\" for all Emacs 154 related documents. Otherwise, append \".html\" extension to FILENAME. 155 See `org-info-emacs-documents' and `org-info-other-documents' for details." 156 (cond ((member filename org-info-emacs-documents) 157 (format "https://www.gnu.org/software/emacs/manual/html_mono/%s.html" 158 filename)) 159 ((cdr (assoc filename org-info-other-documents))) 160 (t (concat filename ".html")))) 161 162 (defun org-info--expand-node-name (node) 163 "Expand Info NODE to HTML cross reference." 164 ;; See (info "(texinfo) HTML Xref Node Name Expansion") for the 165 ;; expansion rule. 166 (let ((node (replace-regexp-in-string 167 "\\([ \t\n\r]+\\)\\|\\([^a-zA-Z0-9]\\)" 168 (lambda (m) 169 (if (match-end 1) "-" (format "_%04x" (string-to-char m)))) 170 (org-trim node)))) 171 (cond ((string= node "") "") 172 ((string-match-p "\\`[0-9]" node) (concat "g_t" node)) 173 (t node)))) 174 175 (defun org-info-export (path desc format) 176 "Export an info link. 177 See `org-link-parameters' for details about PATH, DESC and FORMAT." 178 (pcase-let ((`(,manual . ,node) (org-info--link-file-node path))) 179 (pcase format 180 (`html 181 (format "<a href=\"%s#%s\">%s</a>" 182 (org-info-map-html-url manual) 183 (org-info--expand-node-name node) 184 (or desc path))) 185 (`texinfo 186 (let ((title (or desc ""))) 187 (format "@ref{%s,%s,,%s,}" node title manual))) 188 (_ nil)))) 189 190 (provide 'ol-info) 191 192 ;;; ol-info.el ends here