ol-info.el 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. ;;; ol-info.el --- Links to Info Nodes -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2004-2021 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: 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 'ol)
  27. ;; Declare external functions and variables
  28. (declare-function Info-find-node "info"
  29. (filename nodename &optional no-going-back strict-case))
  30. (defvar Info-current-file)
  31. (defvar Info-current-node)
  32. ;; Install the link type
  33. (org-link-set-parameters "info"
  34. :follow #'org-info-open
  35. :export #'org-info-export
  36. :store #'org-info-store-link)
  37. ;; Implementation
  38. (defun org-info-store-link ()
  39. "Store a link to an Info file and node."
  40. (when (eq major-mode 'Info-mode)
  41. (let ((link (concat "info:"
  42. (file-name-nondirectory Info-current-file)
  43. "#" Info-current-node))
  44. (desc (concat (file-name-nondirectory Info-current-file)
  45. "#" Info-current-node)))
  46. (org-link-store-props :type "info" :file Info-current-file
  47. :node Info-current-node
  48. :link link :desc desc)
  49. link)))
  50. (defun org-info-open (path _)
  51. "Follow an Info file and node link specified by PATH."
  52. (org-info-follow-link path))
  53. (defun org-info-follow-link (name)
  54. "Follow an Info file and node link specified by NAME."
  55. (if (or (string-match "\\(.*\\)\\(?:#\\|::\\)\\(.*\\)" name)
  56. (string-match "\\(.*\\)" name))
  57. (let ((filename (match-string 1 name))
  58. (nodename-or-index (or (match-string 2 name) "Top")))
  59. (require 'info)
  60. ;; If nodename-or-index is invalid node name, then look it up
  61. ;; in the index.
  62. (condition-case nil
  63. (Info-find-node filename nodename-or-index)
  64. (user-error (Info-find-node filename "Top")
  65. (condition-case nil
  66. (Info-index nodename-or-index)
  67. (user-error "Could not find '%s' node or index entry"
  68. nodename-or-index)))))
  69. (user-error "Could not open: %s" name)))
  70. (defconst org-info-emacs-documents
  71. '("ada-mode" "auth" "autotype" "bovine" "calc" "ccmode" "cl" "dbus" "dired-x"
  72. "ebrowse" "ede" "ediff" "edt" "efaq-w32" "efaq" "eieio" "eintr" "elisp"
  73. "emacs-gnutls" "emacs-mime" "emacs" "epa" "erc" "ert" "eshell" "eudc" "eww"
  74. "flymake" "forms" "gnus" "htmlfontify" "idlwave" "ido" "info" "mairix-el"
  75. "message" "mh-e" "newsticker" "nxml-mode" "octave-mode" "org" "pcl-cvs"
  76. "pgg" "rcirc" "reftex" "remember" "sasl" "sc" "semantic" "ses" "sieve"
  77. "smtpmail" "speedbar" "srecode" "todo-mode" "tramp" "url" "vip" "viper"
  78. "widget" "wisent" "woman")
  79. "List of emacs documents available.
  80. Taken from <https://www.gnu.org/software/emacs/manual/html_mono/.>")
  81. (defconst org-info-other-documents
  82. '(("libc" . "https://www.gnu.org/software/libc/manual/html_mono/libc.html")
  83. ("make" . "https://www.gnu.org/software/make/manual/make.html"))
  84. "Alist of documents generated from Texinfo source.
  85. When converting info links to HTML, links to any one of these manuals are
  86. converted to use these URL.")
  87. (defun org-info-map-html-url (filename)
  88. "Return URL or HTML file associated to Info FILENAME.
  89. If FILENAME refers to an official GNU document, return a URL pointing to
  90. the official page for that document, e.g., use \"gnu.org\" for all Emacs
  91. related documents. Otherwise, append \".html\" extension to FILENAME.
  92. See `org-info-emacs-documents' and `org-info-other-documents' for details."
  93. (cond ((member filename org-info-emacs-documents)
  94. (format "https://www.gnu.org/software/emacs/manual/html_mono/%s.html"
  95. filename))
  96. ((cdr (assoc filename org-info-other-documents)))
  97. (t (concat filename ".html"))))
  98. (defun org-info--expand-node-name (node)
  99. "Expand Info NODE to HTML cross reference."
  100. ;; See (info "(texinfo) HTML Xref Node Name Expansion") for the
  101. ;; expansion rule.
  102. (let ((node (replace-regexp-in-string
  103. "\\([ \t\n\r]+\\)\\|\\([^a-zA-Z0-9]\\)"
  104. (lambda (m)
  105. (if (match-end 1) "-" (format "_%04x" (string-to-char m))))
  106. (org-trim node))))
  107. (cond ((string= node "") "")
  108. ((string-match-p "\\`[0-9]" node) (concat "g_t" node))
  109. (t node))))
  110. (defun org-info-export (path desc format)
  111. "Export an info link.
  112. See `org-link-parameters' for details about PATH, DESC and FORMAT."
  113. (let* ((parts (split-string path "#\\|::"))
  114. (manual (car parts))
  115. (node (or (nth 1 parts) "Top")))
  116. (pcase format
  117. (`html
  118. (format "<a href=\"%s#%s\">%s</a>"
  119. (org-info-map-html-url manual)
  120. (org-info--expand-node-name node)
  121. (or desc path)))
  122. (`texinfo
  123. (let ((title (or desc "")))
  124. (format "@ref{%s,%s,,%s,}" node title manual)))
  125. (_ nil))))
  126. (provide 'ol-info)
  127. ;;; ol-info.el ends here