org.el: use `user-error' instead of `error'.
[org-mode.git] / contrib / lisp / org-annotate-file.el
blob8fbb590217258a4c525333d89eb5bf35b29707da
1 ;;; org-annotate-file.el --- Annotate a file with org syntax
3 ;; Copyright (C) 2008-2014 Philip Jackson
5 ;; Author: Philip Jackson <phil@shellarchive.co.uk>
6 ;; Version: 0.2
8 ;; This file is not currently part of GNU Emacs.
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program ; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; This is yet another implementation to allow the annotation of a
28 ;; file without modification of the file itself. The annotation is in
29 ;; org syntax so you can use all of the org features you are used to.
31 ;; To use you might put the following in your .emacs:
33 ;; (require 'org-annotate-file)
34 ;; (global-set-key (kbd "C-c C-l") 'org-annotate-file) ; for example
36 ;; To change the location of the annotation file:
38 ;; (setq org-annotate-file-storage-file "~/annotated.org")
40 ;; Then when you visit any file and hit C-c C-l you will find yourself
41 ;; in an org buffer on a headline which links to the file you were
42 ;; visiting, e.g:
44 ;; * ~/org-annotate-file.el
46 ;; Under here you can put anything you like, save the file
47 ;; and next time you hit C-c C-l you will hit those notes again.
49 ;; To put a subheading with a text search for the current line set
50 ;; `org-annotate-file-add-search` to non-nil value. Then when you hit
51 ;; C-c C-l (on the above line for example) you will get:
53 ;; * ~/org-annotate-file.el
54 ;; ** `org-annotate-file-add-search` to non-nil value. Then whe...
56 ;; Note that both of the above will be links.
58 ;;; Code:
60 (require 'org)
62 (defvar org-annotate-file-storage-file "~/.org-annotate-file.org"
63 "File in which to keep annotations.")
65 (defvar org-annotate-file-add-search nil
66 "If non-nil, add a link as a second level to the actual file location.")
68 (defvar org-annotate-file-always-open t
69 "If non-nil, always expand the full tree when visiting the annotation file.")
71 (defun org-annotate-file-ellipsify-desc (string &optional after)
72 "Return shortened STRING with appended ellipsis.
73 Trim whitespace at beginning and end of STRING and replace any
74 characters that appear after the occurrence of AFTER with '...'"
75 (let* ((after (number-to-string (or after 30)))
76 (replace-map (list (cons "^[ \t]*" "")
77 (cons "[ \t]*$" "")
78 (cons (concat "^\\(.\\{" after
79 "\\}\\).*") "\\1..."))))
80 (mapc (lambda (x)
81 (when (string-match (car x) string)
82 (setq string (replace-match (cdr x) nil nil string))))
83 replace-map)
84 string))
86 ;;;###autoload
87 (defun org-annotate-file ()
88 "Visit `org-annotate-file-storage-file` and add a new annotation section.
89 The annotation is opened at the new section which will be referencing
90 the point in the current file."
91 (interactive)
92 (unless (buffer-file-name)
93 (error "This buffer has no associated file!"))
94 (switch-to-buffer
95 (org-annotate-file-show-section org-annotate-file-storage-file)))
97 ;;;###autoload
98 (defun org-annotate-file-show-section (storage-file &optional annotated-buffer)
99 "Add or show annotation entry in STORAGE-FILE and return the buffer.
100 The annotation will link to ANNOTATED-BUFFER if specified,
101 otherwise the current buffer is used."
102 (let ((filename (abbreviate-file-name (or annotated-buffer
103 (buffer-file-name))))
104 (line (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
105 (annotation-buffer (find-file-noselect storage-file)))
106 (with-current-buffer annotation-buffer
107 (org-annotate-file-annotate filename line))
108 annotation-buffer))
110 (defun org-annotate-file-annotate (filename line)
111 "Add annotation for FILENAME at LINE using current buffer."
112 (let* ((link (org-make-link-string (concat "file:" filename) filename))
113 (search-link (org-make-link-string
114 (concat "file:" filename "::" line)
115 (org-annotate-file-ellipsify-desc line))))
116 (unless (eq major-mode 'org-mode)
117 (org-mode))
118 (goto-char (point-min))
119 (widen)
120 (when org-annotate-file-always-open
121 (show-all))
122 (unless (search-forward-regexp
123 (concat "^* " (regexp-quote link)) nil t)
124 (org-annotate-file-add-upper-level link))
125 (beginning-of-line)
126 (org-narrow-to-subtree)
127 ;; deal with a '::' search if need be
128 (when org-annotate-file-add-search
129 (unless (search-forward-regexp
130 (concat "^** " (regexp-quote search-link)) nil t)
131 (org-annotate-file-add-second-level search-link)))))
133 (defun org-annotate-file-add-upper-level (link)
134 "Add and link heading to LINK."
135 (goto-char (point-min))
136 (call-interactively 'org-insert-heading)
137 (insert link))
139 (defun org-annotate-file-add-second-level (link)
140 "Add and link subheading to LINK."
141 (goto-char (point-at-eol))
142 (call-interactively 'org-insert-subheading)
143 (insert link))
145 (provide 'org-annotate-file)
147 ;;; org-annotate-file.el ends here