ol-info.el 7.3 KB

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