org-elisp-symbol.el 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. ;;; org-elisp-symbol.el --- Org links to emacs-lisp symbols
  2. ;;
  3. ;; Copyright 2007, 2008, 2009 Bastien Guerry
  4. ;;
  5. ;; Author: bzg AT altern DOT org
  6. ;; Version: 0.2
  7. ;; Keywords: org, remember, lisp
  8. ;; URL: http://www.cognition.ens.fr/~guerry/u/org-elisp-symbol.el
  9. ;;
  10. ;; This program is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 3, or (at your option)
  13. ;; any later version.
  14. ;;
  15. ;; This program is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;;
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with this program; if not, write to the Free Software
  22. ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode already lets you store/insert links to emacs-lisp files,
  27. ;; just like any other file. This package lets you precisely link to
  28. ;; any emacs-lisp symbol and access useful information about the symbol.
  29. ;;
  30. ;; Here is the list of available properties when linking from a elisp-symbol:
  31. ;;
  32. ;; :name The symbol's name.
  33. ;; :stype The symbol's type (commandp, function, etc.)
  34. ;; :def The function used to set the symbol's value (defun, etc.)
  35. ;; :keys The keys associated with the command.
  36. ;; :args The arguments of the function.
  37. ;; :docstring The docstring of the symbol.
  38. ;; :doc The first line of the dostring.
  39. ;; :comment A comment line just above the sexp, if any.
  40. ;; :fixme A FIXME comment line just above the sexp, if any.
  41. ;;
  42. ;; Let's say we have a defun like this one:
  43. ;;
  44. ;; ;; FIXME update docstring
  45. ;; (defun org-export-latex-lists ()
  46. ;; "Convert lists to LaTeX."
  47. ;; (goto-char (point-min))
  48. ;; (while (re-search-forward org-export-latex-list-beginning-re nil t)
  49. ;; (beginning-of-line)
  50. ;; (insert (org-list-to-latex (org-list-parse-list t)) "\n")))
  51. ;;
  52. ;; And a remember template like:
  53. ;;
  54. ;; (setq org-remember-templates
  55. ;; '((?s "* DEBUG `%:name' (%:args)\n\n%?\n\nFixme: %:fixme\n \
  56. ;; Doc: \"%:doc\"\n\n%a")))
  57. ;;
  58. ;; Then M-x `org-remember' on this sexp will produce this buffer:
  59. ;;
  60. ;; =====================================================================
  61. ;; * DEBUG `org-export-latex-lists' ()
  62. ;;
  63. ;; <== point
  64. ;;
  65. ;; Fixme: update the docstring
  66. ;; Doc: "Convert lists to LaTeX."
  67. ;;
  68. ;; [[file:~/path/file.el::defun%20my-func][Function: my-func]]
  69. ;; =====================================================================
  70. ;;
  71. ;; Put this file into your load-path and the following into your ~/.emacs:
  72. ;; (require 'org-elisp-symbol)
  73. ;;; Code:
  74. (provide 'org-elisp-symbol)
  75. (require 'org)
  76. (org-add-link-type "elisp-symbol" 'org-elisp-symbol-open)
  77. (add-hook 'org-store-link-functions 'org-elisp-symbol-store-link)
  78. (defun org-elisp-symbol-open (path)
  79. "Visit the emacs-lisp elisp-symbol at PATH."
  80. (let* ((search (when (string-match "::\\(.+\\)\\'" path)
  81. (match-string 1 path)))
  82. (path (substring path 0 (match-beginning 0))))
  83. (org-open-file path t nil search)))
  84. (defun org-elisp-symbol-store-link ()
  85. "Store a link to an emacs-lisp elisp-symbol."
  86. (when (eq major-mode 'emacs-lisp-mode)
  87. (save-excursion
  88. (or (looking-at "^(") (beginning-of-defun))
  89. (looking-at "^(\\([a-z]+\\) \\([^)\n ]+\\) ?\n?[ \t]*\\(?:(\\(.*\\))\\)?")
  90. (let* ((end (save-excursion
  91. (save-match-data
  92. (end-of-defun) (point))))
  93. (def (match-string 1))
  94. (name (match-string 2))
  95. (sym-name (intern-soft name))
  96. (stype (cond ((commandp sym-name) "Command")
  97. ((functionp sym-name) "Function")
  98. ((user-variable-p sym-name) "User variable")
  99. ((eq def "defvar") "Variable")
  100. ((eq def "defmacro") "Macro")
  101. (t "Symbol")))
  102. (args (if (match-string 3)
  103. (mapconcat (lambda (a) (unless (string-match "^&" a) a))
  104. (split-string (match-string 3)) " ")
  105. "no arg"))
  106. (docstring (cond ((functionp sym-name)
  107. (or (documentation sym-name)
  108. "[no documentation]"))
  109. ((string-match "[Vv]ariable" stype)
  110. (documentation-property sym-name
  111. 'variable-documentation))
  112. (t "no documentation")))
  113. (doc (and (string-match "^\\([^\n]+\\)$" docstring)
  114. (match-string 1 docstring)))
  115. (fixme (save-excursion
  116. (beginning-of-defun) (end-of-defun)
  117. (if (re-search-forward "^;+ ?FIXME[ :]*\\(.*\\)$" end t)
  118. (match-string 1) "nothing to fix")))
  119. (comment (save-excursion
  120. (beginning-of-defun) (end-of-defun)
  121. (if (re-search-forward "^;;+ ?\\(.*\\)$" end t)
  122. (match-string 1) "no comment")))
  123. keys keys-desc link description)
  124. (if (equal stype "Command")
  125. (setq keys (where-is-internal sym-name)
  126. keys-desc
  127. (if keys (mapconcat 'key-description keys " ") "none")))
  128. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  129. "::" def " " name))
  130. (setq description (concat stype ": " name))
  131. (org-store-link-props
  132. :type "elisp-symbol"
  133. :link link
  134. :description description
  135. :def def
  136. :name name
  137. :stype stype
  138. :args args
  139. :keys keys-desc
  140. :docstring docstring
  141. :doc doc
  142. :fixme fixme
  143. :comment comment)))))
  144. (provide 'org-elisp-symbol)
  145. ;;;;##########################################################################
  146. ;;;; User Options, Variables
  147. ;;;;##########################################################################
  148. ;;; org-elisp-symbol.el ends here