ol-info.el 7.3 KB

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