org-info.el 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. ;;; org-info.el --- Support for links to Info nodes from within Org-Mode
  2. ;; Copyright (C) 2004-2015 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://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 <http://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)
  27. ;; Declare external functions and variables
  28. (declare-function Info-find-node "info" (filename nodename
  29. &optional no-going-back))
  30. (defvar Info-current-file)
  31. (defvar Info-current-node)
  32. ;; Install the link type
  33. (org-add-link-type "info" 'org-info-open 'org-info-export)
  34. (add-hook 'org-store-link-functions 'org-info-store-link)
  35. ;; Implementation
  36. (defun org-info-store-link ()
  37. "Store a link to an Info file and node."
  38. (when (eq major-mode 'Info-mode)
  39. (let (link desc)
  40. (setq link (concat "info:"
  41. (file-name-nondirectory Info-current-file)
  42. "#" Info-current-node))
  43. (setq desc (concat (file-name-nondirectory Info-current-file)
  44. "#" Info-current-node))
  45. (org-store-link-props :type "info" :file Info-current-file
  46. :node Info-current-node
  47. :link link :desc desc)
  48. link)))
  49. (defun org-info-open (path)
  50. "Follow an Info file and node link specified by PATH."
  51. (org-info-follow-link path))
  52. (defun org-info-follow-link (name)
  53. "Follow an Info file and node link specified by NAME."
  54. (if (or (string-match "\\(.*\\)[#:]:?\\(.*\\)" name)
  55. (string-match "\\(.*\\)" name))
  56. (let ((filename (match-string 1 name))
  57. (nodename-or-index (or (match-string 2 name) "Top")))
  58. (require 'info)
  59. ;; If nodename-or-index is invalid node name, then look it up
  60. ;; in the index.
  61. (condition-case nil
  62. (Info-find-node filename nodename-or-index)
  63. (user-error (Info-find-node filename "Top")
  64. (condition-case nil
  65. (Info-index nodename-or-index)
  66. (user-error "Could not find '%s' node or index entry"
  67. nodename-or-index)))))
  68. (user-error "Could not open: %s" name)))
  69. (defun org-info-export (path desc format)
  70. "Export an info link.
  71. See `org-add-link-type' for details about PATH, DESC and FORMAT."
  72. (when (eq format 'html)
  73. (or (string-match "\\(.*\\)[#:]:?\\(.*\\)" path)
  74. (string-match "\\(.*\\)" path))
  75. (let ((filename (match-string 1 path))
  76. (node (or (match-string 2 path) "Top")))
  77. (format "<a href=\"%s.html#%s\">%s</a>"
  78. filename
  79. (replace-regexp-in-string " " "-" node)
  80. (or desc path)))))
  81. (provide 'org-info)
  82. ;;; org-info.el ends here