ol-info.el 7.4 KB

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