Intermediate state, I am just trying comiting now.
[org-mode.git] / EXPERIMENTAL / org-elisp-symbol.el
blob05e5afc2a57ff32a6a493686d0fe3c8aff4499b7
1 ;;; org-elisp-symbol.el --- Org links to emacs-lisp symbols
2 ;;
3 ;; Copyright 2007 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.
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.
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.
24 ;;; Commentary:
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 uesful information about the symbol.
30 ;; Here is the list of available properties when linking from a elisp-symbol:
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.
42 ;; Let's say we have a defun like this one:
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")))
52 ;; And a remember template like:
54 ;; (setq org-remember-templates
55 ;; '((?s "* DEBUG `%:name' (%:args)\n\n%?\n\nFixme: %:fixme\n \
56 ;; Doc: \"%:doc\"\n\n%a")))
58 ;; Then M-x `org-remember' on this sexp will produce this buffer:
60 ;; =====================================================================
61 ;; * DEBUG `org-export-latex-lists' ()
63 ;; <== point
65 ;; Fixme: update the docstring
66 ;; Doc: "Convert lists to LaTeX."
68 ;; [[file:~/path/file.el::defun%20org-export-latex-lists][Function: org-export-latex-lists]]
69 ;; =====================================================================
71 ;; Put this file into your load-path and the following into your ~/.emacs:
72 ;; (require 'org-elisp-symbol)
74 ;;; Code:
76 (provide 'org-elisp-symbol)
78 (require 'org)
80 (org-add-link-type "elisp-symbol" 'org-elisp-symbol-open)
81 (add-hook 'org-store-link-functions 'org-elisp-symbol-store-link)
83 (defun org-elisp-symbol-open (path)
84 "Visit the emacs-lisp elisp-symbol at PATH."
85 (let* ((search (when (string-match "::\\(.+\\)\\'" path)
86 (match-string 1 path)))
87 (path (substring path 0 (match-beginning 0))))
88 (org-open-file path t nil search)))
90 (defun org-elisp-symbol-store-link ()
91 "Store a link to an emacs-lisp elisp-symbol."
92 (when (eq major-mode 'emacs-lisp-mode)
93 (save-excursion
94 (or (looking-at "^(") (beginning-of-defun))
95 (looking-at "^(\\([a-z]+\\) \\([^)\n ]+\\) ?\n?[ \t]*\\(?:(\\(.*\\))\\)?")
96 (let* ((end (save-excursion
97 (save-match-data
98 (end-of-defun) (point))))
99 (def (match-string 1))
100 (name (match-string 2))
101 (sym-name (intern-soft name))
102 (stype (cond ((commandp sym-name) "Command")
103 ((functionp sym-name) "Function")
104 ((user-variable-p sym-name) "User variable")
105 ((eq def "defvar") "Variable")
106 ((eq def "defmacro") "Macro")
107 (t "Symbol")))
108 (args (if (match-string 3)
109 (mapconcat (lambda (a) (unless (string-match "^&" a) a))
110 (split-string (match-string 3)) " ")
111 "no arg"))
112 (docstring (cond ((functionp sym-name)
113 (documentation sym-name))
114 ((string-match "[Vv]ariable" stype)
115 (documentation-property sym-name
116 'variable-documentation))
117 (t "no documentation")))
118 (doc (and (string-match "^\\([^\n]+\\)$" docstring)
119 (match-string 1 docstring)))
120 (fixme (save-excursion
121 (beginning-of-defun) (end-of-defun)
122 (if (re-search-forward "^;+ ?FIXME[ :]*\\(.*\\)$" end t)
123 (match-string 1) "nothing to fix")))
124 (comment (save-excursion
125 (beginning-of-defun) (end-of-defun)
126 (if (re-search-forward "^;;+ ?\\(.*\\)$" end t)
127 (match-string 1) "no comment")))
128 keys keys-desc link description)
129 (if (equal stype "Command")
130 (setq keys (where-is-internal sym-name)
131 keys-desc
132 (if keys (mapconcat 'key-description keys " ") "none")))
133 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
134 "::" def " " name))
135 (setq description (concat stype ": " name))
136 (org-store-link-props
137 :type "elisp-symbol"
138 :link link
139 :description description
140 :def def
141 :name name
142 :stype stype
143 :args args
144 :keys keys-desc
145 :docstring docstring
146 :doc doc
147 :fixme fixme
148 :comment comment)))))
150 (provide 'org-elisp-symbol)
153 ;;;;##########################################################################
154 ;;;; User Options, Variables
155 ;;;;##########################################################################
158 ;;; org-elisp-symbol.el ends here