1 ;;; notes-mode.el --- Indexing system for on-line note-taking
3 ;;; Copyright (C) 1994-2007,2012 Free Software Foundation, Inc.
5 ;; Author: <johnh@isi.edu>.
7 ;; This file is part of GNU Emacs.
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.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 (require 'notes-variables)
31 (defvar notes-mode-hooks nil
32 "Hooks to run when entering notes-mode.")
33 (defvar notes-load-mode-hooks nil
34 "Hooks to run when entering notes-mode is loaded.")
36 (defconst notes-beginning-of-defun-regexp "^\\* .*\n\\-"
37 "Regexp matching the beginning of notes section.")
39 (defvar notes-default-tab-binding (global-key-binding "\t")
40 "Saved tab binding for notes-complete-subject.")
41 (defvar notes-default-return-binding (global-key-binding "\r")
42 "Saved return binding for notes-electric-return.")
45 (defun notes-beginning-of-defun ()
46 "Go to the beginning of a notes ``section''."
51 ;; handle starting on a title
52 (if (and (looking-at notes-beginning-of-defun-regexp)
53 (/= (point) old-point))
56 (if (looking-at "^-") ;; handle starting on the underline under a title
58 (re-search-backward notes-beginning-of-defun-regexp nil 'to-limit))))
60 (defun notes-end-of-defun ()
61 "Go to the end of a notes ``section''."
63 (let ((regexp notes-beginning-of-defun-regexp))
64 (if (looking-at regexp)
65 (goto-char (match-end 0)))
66 ;; Find next section and leave cursor at section beginning
67 (if (re-search-forward regexp nil 'to-limit)
68 (re-search-backward regexp 0 t)
69 ;;(goto-char restore-point)
72 (defun notes-follow-link (which)
73 "Go to the WHICH link for this topic.
74 WHICH is either \"next\" or \"prev\".
75 If there are no links for the current note,
76 we go to the last note based upon the index file."
80 (start-buffer (current-buffer))
81 ;; We have to handle links in the same buffer,
82 ;; so the following code figure out where we go
83 ;; and returns it out of the save-excursion.
84 ;; If we end up in another buffer, we let the save-excursion
85 ;; leave the original buffer unchanged. If we end up in
86 ;; the same buffer, we need to go wherever we end up.
87 ;; Can anyone suggest a better way?
91 (setq end-of-note (point))
92 (notes-beginning-of-defun)
93 (setq beginning-of-note (point))
94 (if (and (= beginning-of-note 1) (not (looking-at notes-beginning-of-defun-regexp)))
96 ;; When "above" the first note, search to end of first
97 ;; real note (otherwise end-of-note is just the start
98 ;; of the first real note and there are no links).
101 (setq end-of-note (point))
102 (goto-char beginning-of-note)))
103 (if (re-search-forward (concat "^"
104 (if (eq which 'next) "next" "prev")
105 ":[ ]+<") end-of-note t)
106 (progn ; link exists, just take it
108 (notes-w3-follow-link (point))
109 (cons (current-buffer) (point)))
110 ;; No link; go through the index file.
111 (if (notes-goto-index-entry which)
112 (let ((index-buffer (current-buffer)))
113 (notes-index-follow-link (point))
114 (bury-buffer index-buffer))
115 (error "No known notes in that direction.")
116 (bury-buffer (current-buffer)))
117 (cons (current-buffer) (point))))))
118 ;; Check for going to the same buffer (and the save-excursion
119 ;; undoing our work).
120 (if (eq start-buffer (car end-buffer-and-point))
121 (goto-char (cdr end-buffer-and-point)))))
124 (defun notes-follow-next-link ()
125 "Go to the next link for this topic."
127 (notes-follow-link 'next))
129 (defun notes-follow-prev-link ()
130 "Go to the previous link for this topic."
132 (notes-follow-link 'prev))
134 (defvar notes-complete-subject-abbrevs-alist
135 '(("SP2010" "USC/Classes/CS551/SP2010")
136 ("FA2011" "USC/Classes/CS551/FA2011"))
137 "Alist of simple substitution of subjects.
138 If subject completion is requested, then subject that matches
139 the left-side of an alist value is replaced by the right-side value.")
141 (defun notes-complete-subject-abbrevs (key)
142 "Handle abbreviations on notes SUBJECTS.
143 Currently this is just a hack."
144 (let ((value (assoc key notes-complete-subject-abbrevs-alist)))
150 (defun notes-complete-subject ()
151 "Complete the notes subject under point."
154 ((subject (save-excursion
156 (notes-extract-subject t)))
157 old-completion-ignore-case
159 (if (not (and notes-mode-complete-subjects subject))
160 (call-interactively notes-default-tab-binding)
161 ;; Complete the title.
162 (if (null notes-subject-table)
163 (save-excursion ;; FIXME: Why??
164 (find-file-noselect (expand-file-name "index" notes-dir))))
166 ;; Run completer if it's loaded,
167 ;; otherwise do our own thing.
168 (setq completion-ignore-case t)
170 ((fboundp 'completer-complete-goto)
171 (completer-complete-goto "^ \t\n\"" " " notes-subject-table nil))
172 ;; NEEDSWORK: should try other completers, too.
173 (t ;; Do our own completion.
174 (setq full-subject (try-completion subject notes-subject-table)
175 subject (completing-read "Subject: "
176 notes-subject-table nil nil
177 (if (stringp full-subject)
180 (delete-region (line-beginning-position) (line-end-position))
181 (insert "* " (notes-complete-subject-abbrevs subject))))
182 (setq completion-ignore-case old-completion-ignore-case))))
184 (defun notes-fix-prevnext-this-entry ()
185 "Fix up the prev link for the current entry, if necessary.
186 Currently this code only handles brand new entries."
187 ;; Contributed from Takashi Nishimoto <g96p0935@mse.waseda.ac.jp>.
190 (let ((subject (notes-extract-subject nil t))
191 (this-url (notes-current-url))
193 (with-current-buffer (find-file-noselect
194 (expand-file-name "index" notes-dir))
195 (goto-char (point-min))
196 (if (re-search-forward
197 (concat "^" (regexp-quote subject) ":.* \\([0-9]+\\)$")
199 (save-window-excursion
200 (cond ((and (notes-w3-url
201 (notes-file-to-url (match-string 1) subject))
202 (re-search-forward "^next: " nil t)
203 (looking-at "<none>"))
205 (pre-modified (buffer-modified-p))
208 (setq last-url (notes-current-url))
209 (if (and (null pre-modified)
210 (>= notes-electric-prevnext 2))
214 (notes-beginning-of-defun)
216 (if (not (looking-at "prev: "))
217 (insert "prev: " last-url "\n" "next: <none>\n\n")
218 (forward-line 3))))))
220 (defun notes-electric-return (arg)
221 "* Return, underlining if we're on a subject."
223 (if (let ((cur-point (point)))
226 (and (not (eq cur-point (point))) ;; normal return if at b-o-ln
227 (notes-extract-subject t))))
228 (progn (notes-underline-line)
229 (if notes-electric-prevnext
230 (notes-fix-prevnext-this-entry)))
231 (call-interactively notes-default-return-binding)))
233 (defun notes-current-url ()
234 "Return the notes-URL of the current entry around the current point."
235 (let ((subject (notes-extract-subject nil t))
236 (date (file-name-nondirectory buffer-file-name)))
238 (abbreviate-file-name buffer-file-name)
239 (if subject (concat "#* " subject) "")
242 (defun notes-current-url-as-kill ()
243 "* Put the notes-URL of the current entry into the kill ring."
245 (kill-new (notes-current-url)))
247 (defun notes-goto-index-entry (&optional direction)
248 "* Jump to the index entry corresponding to our current note entry.
249 If we're not in an entry, we leave you in the index file.
250 If the current date doesn't exist, error in DIRECTION.
251 Returns nil if on errors (no index; no date in DIRECTION),
252 otherwise the point of the hit."
254 (let ((start-buffer (current-buffer))
255 (subject (notes-extract-subject)) ; get subject if on it
256 (date (if (null (buffer-file-name)) nil
257 (file-name-nondirectory (buffer-file-name)))))
258 ;; Try and get the subject, either forward...
261 (notes-beginning-of-defun)
262 (setq subject (notes-extract-subject))))
267 (setq subject (notes-extract-subject))))
268 ;; Form and jump to the url for the index-entry.
269 (if (and (notes-w3-url (concat notes-url-prefix
271 (if subject (concat "#" subject) ""))
273 ;; Go to the current date, if any.
274 (notes-index-goto-date date direction))
278 (defun notes-extract-subject (&optional relaxed search)
279 "Extract the subject under the point in the current buffer.
280 If RELAXED, then accept non-underlined subjects.
281 If SEARCH we'll search back in the buffer for the nearest
284 Returns nil if we're not on as subject."
287 ;; directly on a note
288 ((or (looking-at notes-beginning-of-defun-regexp)
290 (looking-at "^\\* ")))
293 ((start (+ (point) 2))
294 (end (progn (end-of-line) (point))))
295 (buffer-substring start end))))
298 (notes-beginning-of-defun)
299 (notes-extract-subject relaxed nil)))
305 (defun notes-underline-line ()
306 "* Create a row of dashes as long as this line, or adjust the current underline."
308 ;; check to see if it's already underlined
311 (looking-at "^[ \t]*--*$"))
312 (notes-old-underline-line)
314 (notes-new-underline-line)
317 (defun notes-new-underline-line ()
318 "Underline a line with a row of dashes. Move point after the dashes.
319 \\[notes-new-underline-line] reproduces leading spaces."
322 ((bol (progn (beginning-of-line)
324 (bospaces (progn (skip-chars-forward " \t")
326 (nospaces (- bospaces bol))
327 (eol (progn (end-of-line)
328 (untabify bol (point))
330 (insert "\n" (buffer-substring bol bospaces))
331 (insert-char ?- (- eol bospaces))))
333 (defun notes-old-underline-line ()
334 "Replace the following line with a row of dashes. Leave the point unchanged."
338 (delete-region (line-beginning-position) (1+ (line-end-position))))
339 (notes-new-underline-line)))
341 (defun notes-mode-initialize-note-from-cache ()
342 "Build a new note from the cache. Return valid cache contents or nil."
345 ((new-buffer (current-buffer))
346 (cache-file (expand-file-name "mknew.cache" notes-dir))
347 (buf (find-file cache-file))
355 (>= (count-lines (point-min) (point-max)) 3))
357 ;; If you know a more elegant way to extact the first
358 ;; three lines of a file, please let me know.
359 (goto-char (point-min))
362 (setq magic-line (buffer-substring m (- (point) 1)))
365 (setq prev-file (buffer-substring m (- (point) 1)))
368 (setq this-file (buffer-substring m (- (point) 1)))
369 (setq cache-contents (buffer-substring (point) (point-max)))
374 (string-equal magic-line "mknew.cache 830494922")
375 (file-newer-than-file-p cache-file prev-file)
376 (string-equal (file-name-nondirectory this-file)
377 (file-name-nondirectory (buffer-file-name
382 ;; Kill the buffer to avoid "buf changed, reload?" warnings.
387 (defun notes-mode-initialize-note ()
388 "Fill in an empty new note.
389 Create any directories as necessary.
390 Use the mknew cache if possible."
393 ((dir (directory-file-name (file-name-directory (buffer-file-name)))))
394 (if (file-exists-p dir)
396 (make-directory dir t)
397 (message "New intermediate directory created.")))
398 (if notes-mode-initialization-program
400 ((cache-contents (notes-mode-initialize-note-from-cache)))
402 (insert cache-contents)
403 (shell-command-on-region
406 (concat (expand-file-name notes-mode-initialization-program
409 ;; FIXME: Use shell-quote-argument.
410 (buffer-file-name) "'") 't)))))
415 ;; requires "PEM - PGP Enhanced Messaging for GNU Emacs"
416 ;; from Roy Frederick Busdiecker, III (Rick)
417 ;; or mailcrypt 3.4.x or >=3.5.x
420 (defvar notes-encryption-library
423 ; ((fboundp 'mc-encrypt-region) 'mailcrypt)
424 ; ((fboundp 'npgp:encrypt-region) 'npgp)
426 "PGP library to use.")
428 (defvar notes-encryption-sub-library
430 "Variant of mailcrypt to use (`pgp', `pgp50', or `gpg').")
432 (defvar notes-encryption-npgp-userid nil
433 "PGP key for the current user.")
435 (defvar notes-encryption-npgp-key-id nil
436 "Keyid of PGP key for the current user.
437 Useful if your \\[user-full-name] doesn't match a unique key.
438 Should have a leading 0x.")
440 (defun notes-encryption-npgp-userid ()
441 "Return notes-encryption-userid, initializing it if necessary."
443 (if (and notes-encryption-userid
445 notes-encryption-userid
446 (setq notes-encryption-userid
448 (if notes-encryption-key-id
449 (npgp:get-key-by-key-id notes-encryption-key-id)
450 (pam:read-name-key (user-full-name)))))))
452 (defun notes-encryption-mailcrypt-keyid ()
453 "Do the right thing."
456 ((eq notes-encryption-sub-library 'pgp)
457 (cdr (mc-pgp-lookup-key mc-pgp-user-id)))
458 ((eq notes-encryption-sub-library 'pgp50)
459 (cdr (mc-pgp50-lookup-key mc-pgp50-user-id)))
460 ((eq notes-encryption-sub-library 'gpg)
461 (cdr (mc-gpg-lookup-key mc-gpg-user-id)))
462 (t (error "notes-encryption-decrypt-region: no pgp sub-library."))))
464 (defun notes-encryption-load-mailcrypt ()
466 ;; ick ick ick this code needs to be cleaned up
468 ((null (eq notes-encryption-library 'mailcrypt))
470 ((eq notes-encryption-sub-library 'pgp)
471 (load-library "mc-pgp"))
472 ((eq notes-encryption-sub-library 'pgp50)
473 (load-library "mc-pgp5"))
474 ((eq notes-encryption-sub-library 'gpg)
475 (load-library "mc-gpg"))
476 (t (error "notes-encryption-load-mailcrypt: no pgp sub-library."))))
478 (defun notes-encryption-decrypt-region (start end)
480 ((eq notes-encryption-library 'npgp)
483 (npgp:decrypt-region start end))
484 ((eq notes-encryption-library 'mailcrypt)
485 (notes-encryption-load-mailcrypt)
487 ((eq notes-encryption-sub-library 'pgp)
488 (mc-pgp-decrypt-region start end))
489 ((eq notes-encryption-sub-library 'pgp50)
490 (mc-pgp50-decrypt-region start end))
491 ((eq notes-encryption-sub-library 'gpg)
492 (mc-gpg-decrypt-region start end))
493 (t (error "notes-encryption-decrypt-region: no pgp sub-library."))))
494 (t (error "notes-encryption-decrypt-region: no pgp library."))))
496 (defun notes-encryption-encrypt-region (start end)
498 ((eq notes-encryption-library 'npgp)
499 (npgp:encrypt-region (notes-encryption-npgp-userid) start end))
500 ((eq notes-encryption-library 'mailcrypt)
501 (notes-encryption-load-mailcrypt)
502 (let ((old-sign mc-pgp-always-sign)
503 old-comment recipients)
504 (setq mc-pgp-always-sign 'never
505 recipients (list (notes-encryption-mailcrypt-keyid)))
507 ((eq notes-encryption-sub-library 'pgp)
508 (setq old-comment mc-pgp-comment
510 (mc-pgp-encrypt-region recipients start end
511 (notes-encryption-mailcrypt-keyid) nil)
512 (setq mc-pgp-comment old-comment))
513 ((eq notes-encryption-sub-library 'pgp50)
514 (setq old-comment mc-pgp50-comment
516 (mc-pgp50-encrypt-region recipients start end
517 (notes-encryption-mailcrypt-keyid) nil)
518 (setq mc-pgp50-comment old-comment))
519 ((eq notes-encryption-sub-library 'gpg)
520 (setq old-comment mc-gpg-comment
522 (mc-gpg-encrypt-region recipients start end
523 (notes-encryption-mailcrypt-keyid) nil)
524 (setq mc-gpg-comment old-comment))
525 (t (error "notes-encryption-decrypt-region: no gpg sub-library.")))
526 (setq mc-pgp-always-sign old-sign)))
527 (t (error "notes-encryption-decrypt-region: no pgp library."))))
529 (defun notes-encrypt-note (prefix)
530 "Encrypt the current note for the current user. With PREFIX, start from point."
534 ;; Unless a prefix arg, start at beginning-of-note.
537 (if (not (looking-at notes-beginning-of-defun-regexp))
538 (notes-beginning-of-defun))
539 ;; skip over the header
540 (while (and (or (looking-at notes-beginning-of-defun-regexp)
542 (looking-at "^\\(prev\\|next\\): ")
543 (looking-at "^[ \t]*$"))
544 (< (point) (point-max)))
548 (if (re-search-forward "^-----BEGIN PGP MESSAGE"
553 (error "Note is already encrypted."))
556 (while (or (looking-at notes-beginning-of-defun-regexp)
557 (looking-at "^[ \t]*$"))
561 (notes-encryption-encrypt-region start end))))
563 (defun notes-decrypt-note ()
564 "Decrypt the current note for the current user."
567 (if (not (looking-at notes-beginning-of-defun-regexp))
568 (notes-beginning-of-defun))
569 (if (null (re-search-forward "^-----BEGIN PGP"
574 (error "Note is not encrypted."))
576 (let ((start (point)))
577 (if (null (re-search-forward "^-----END PGP"
582 (error "Could not find end of encrypted note."))
585 (notes-encryption-decrypt-region start (point)))))
589 ;;; notes or notes-index?
591 (defun notes-summarize-subject (regexp-subject &optional subject)
592 "* Collect all of a subject."
594 (require 'notes-index-mode)
597 ((eq major-mode 'notes-mode)
598 (setq subject (notes-extract-subject nil t)))
599 ((eq major-mode 'notes-index-mode)
600 (setq subject (notes-index-extract-subject)))))
602 (error "notes-summarize-subject: no subject specified or inferable."))
604 ((buf (get-buffer-create (concat "*notes on " subject "*"))))
607 (apply 'call-process (expand-file-name "catsubject" notes-bin-dir)
616 ;;; notes-rename-subject
618 (defun notes-rename-subject ()
619 "* Rename the current subject.
620 Assumes working next/prev linkage between the entries."
622 (let ((subject (notes-extract-subject)))
627 (if (not (looking-at "* "))
630 (error "not yet done")
639 (defvar notes-mode-map
640 (let ((map (make-sparse-keymap)))
641 ;; Random key-bindings.
642 (define-key map "\M-\C-a" 'notes-beginning-of-defun)
643 (define-key map "\M-\C-e" 'notes-end-of-defun)
644 (define-key map "\C-c\C-d" 'notes-decrypt-note)
645 (define-key map "\C-c\C-e" 'notes-encrypt-note)
646 (define-key map "\C-c\r" 'notes-w3-follow-link)
647 (define-key map "\C-c\C-p" 'notes-follow-prev-link)
648 (define-key map "\C-c\C-n" 'notes-follow-next-link)
649 (define-key map "\C-c\C-i" 'notes-goto-index-entry)
650 (define-key map "\C-c\C-k" 'notes-current-url-as-kill)
651 (define-key map "\C-c\C-s" 'notes-summarize-subject)
652 (define-key map "\C-c-" 'notes-underline-line)
653 ;; FIXME: Use completion-at-point-functions instead.
654 (define-key map "\t" 'notes-complete-subject)
655 ;; FIXME: Use post-self-insert-hook instead.
656 (define-key map "\r" 'notes-electric-return)
657 (define-key map "\n" 'notes-electric-return) ; a more common synonym
658 (notes-platform-bind-mouse map 'S-mouse-2 'notes-w3-follow-link-mouse)
662 (define-derived-mode notes-mode indented-text-mode "Notes"
663 "Enable notes-mode for a buffer.
665 Inside a notes buffer one can click on URLs and follow them to
668 See the file notes-variables.el for all customization options.
669 To change options, (require 'notes-variables) in your .emacs
670 and then change things.
672 Subjects in notes mode are lines beginning with an asterisk
673 and underlined with dashes. Subjects can be completed
674 with \\[notes-complete-subject] and are automatically underlined.
676 You may wish to add this code to your .emacs file:
677 (add-to-list 'auto-mode-alist
678 (cons \"/9[0-9][0-9][0-9][0-9][0-9].?\\\\'\" 'notes-mode))
679 (define-key global-map [?\\C-c ?n] 'notes-index-todays-link)
680 to automatically enter notes mode.
682 I have two suggestions for how to organize your notes files.
683 First, I collect my notes into a separate file per day. (If you have
684 fewer notes, you may find once-per-week or month more suitable.)
685 Second, at the beginning of each file I have a subject \"* Today\".
686 Since every file has this subject, I can use its prev and next links
687 to easily move around the collection of files.
689 The key-bindings of this mode are:
691 (notes-platform-init)
693 ;; Now set up the mode.
697 (set (make-local-variable 'imenu-prev-index-position-function)
698 'notes-beginning-of-defun)
699 (set (make-local-variable 'imenu-extract-index-name-function)
700 'notes-extract-subject)
702 (set (make-local-variable 'font-lock-defaults)
703 `(notes-font-lock-keywords
704 t nil nil beginning-of-line))
706 ;; Finally, try to fill in an empty note.
707 (if (zerop (buffer-size))
708 (notes-mode-initialize-note))
710 ;; Enable outline-minor-mode (forcebly, in case someone already
711 ;; has it in their text-mode hook). Bug found by
712 ;; Nils Ackermann <nils@nieback.de>.
713 (if notes-use-outline-mode
714 (outline-minor-mode 1)))
721 (run-hooks 'notes-mode-load-hooks)
722 (provide 'notes-mode)
723 ;;; notes-mode.el ends here