1 ;;; org-velocity.el --- something like Notational Velocity for Org.
3 ;; Copyright (C) 2010-2012 Paul M. Rodriguez
5 ;; Author: Paul M. Rodriguez <paulmrodriguez@gmail.com>
9 ;; This file is not part of GNU Emacs.
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation version 2.
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 ;; For a copy of the GNU General Public License, search the Internet,
21 ;; or write to the Free Software Foundation, Inc., 59 Temple Place,
22 ;; Suite 330, Boston, MA 02111-1307 USA
25 ;; Org-Velocity.el is an interface for Org inspired by the minimalist
26 ;; notetaking program Notational Velocity. The idea is to let you
27 ;; amass and access brief notes on many subjects with minimal fuss.
28 ;; Each note is an entry in an ordinary Org file.
30 ;; Org-Velocity can be used in two ways: when called outside Org, to
31 ;; store and access notes in a designated bucket file; or, when called
32 ;; inside Org, as a method for navigating any Org file. (Setting the
33 ;; option `org-velocity-always-use-bucket' disables navigation inside
34 ;; Org files by default, although you can still force this behavior by
35 ;; calling `org-velocity-read' with an argument.)
37 ;; Org-Velocity prompts for search terms in the minibuffer. A list of
38 ;; headings of entries whose text matches your search is updated as
39 ;; you type; you can end the search and visit an entry at any time by
40 ;; clicking on its heading.
42 ;; RET displays the results. If there are no matches, Org-Velocity
43 ;; offers to create a new entry with your search string as its
44 ;; heading. If there are matches, it displays a list of results where
45 ;; the heading of each matching entry is hinted with a number or
46 ;; letter; clicking a result, or typing the matching hint, opens the
47 ;; entry for editing in an indirect buffer. 0 forces a new entry; RET
48 ;; reopens the search for editing.
50 ;; You can customize every step in this process, including the search
51 ;; method, completion for search terms, and templates for creating new
52 ;; entries; M-x customize-group RET org-velocity RET to see all the
55 ;; Thanks to Richard Riley, Carsten Dominik, Bastien Guerry, and Jeff
56 ;; Horn for their suggestions.
59 ;; (require 'org-velocity)
60 ;; (setq org-velocity-bucket (expand-file-name "bucket.org" org-directory))
61 ;; (global-set-key (kbd "C-c v") 'org-velocity)
68 (eval-when-compile (require 'cl
))
70 (defgroup org-velocity nil
71 "Notational Velocity-style interface for Org."
77 (defcustom org-velocity-bucket
""
78 "Where is the bucket file?"
82 (defcustom org-velocity-search-is-incremental t
83 "Show results incrementally when possible?"
88 (defcustom org-velocity-show-previews t
89 "Show previews of the text of each heading?"
94 (defcustom org-velocity-exit-on-match nil
95 "When searching incrementally, exit on a single match?"
100 (defcustom org-velocity-force-new nil
101 "Should exiting the minibuffer with C-j force a new entry?"
106 (defcustom org-velocity-use-search-ring t
107 "Push search to `search-ring' when visiting an entry?
109 This means that C-s C-s will take you directly to the first
110 instance of the search string."
115 (defcustom org-velocity-always-use-bucket nil
116 "Use bucket file even when called from an Org buffer?"
121 (defcustom org-velocity-use-completion nil
124 Notwithstanding the value of this option, calling
125 `dabbrev-expand' always completes against the text of the bucket
129 (const :tag
"Do not use completion" nil
)
130 (const :tag
"Use completion" t
))
133 (defcustom org-velocity-search-method
'phrase
134 "Match on whole phrase, any word, or all words?"
137 (const :tag
"Match whole phrase" phrase
)
138 (const :tag
"Match any word" any
)
139 (const :tag
"Match all words" all
)
140 (const :tag
"Match a regular expression" regexp
))
141 :safe
(lambda (v) (memq v
'(phrase any all regexp
))))
143 (defcustom org-velocity-capture-templates
148 "* %:search\n\n%i%?"))
149 "Use these template with `org-capture'.
150 Meanwhile `org-default-notes-file' is bound to `org-velocity-bucket-file'.
151 The keyword :search inserts the current search.
152 See the documentation for `org-capture-templates'."
154 :type
(or (get 'org-capture-templates
'custom-type
) 'list
))
156 (defsubst org-velocity-grab-preview
()
157 "Grab preview of a subtree.
158 The length of the preview is determined by `window-width'.
160 Replace all contiguous whitespace with single spaces."
163 (if (looking-at org-property-start-re
)
164 (re-search-forward org-property-end-re
)
169 (buffer-substring-no-properties
172 (+ start
(window-width))
176 (defstruct org-velocity-heading buffer position name level preview
)
178 (defsubst org-velocity-nearest-heading
(position)
179 "Return last heading at POSITION.
180 If there is no last heading, return nil."
183 (re-search-backward org-velocity-heading-regexp
)
184 (let ((components (org-heading-components)))
185 (make-org-velocity-heading
186 :buffer
(current-buffer)
188 :name
(nth 4 components
)
189 :level
(nth 0 components
)
190 :preview
(if org-velocity-show-previews
191 (org-velocity-grab-preview))))))
193 (defconst org-velocity-index
195 (nconc (number-sequence 49 57) ;numbers
196 (number-sequence 97 122) ;lowercase letters
197 (number-sequence 65 90))) ;uppercase letters
198 "List of chars for indexing results.")
200 (defconst org-velocity-match-buffer-name
"*Velocity matches*")
202 (defconst org-velocity-heading-regexp
"^\\* "
203 "Regexp to match only top-level headings.")
205 (defvar org-velocity-search nil
206 "Variable to bind to current search.")
208 (defun org-velocity-buffer-file-name (&optional buffer
)
209 "Return the name of the file BUFFER saves to.
210 Same as function `buffer-file-name' unless BUFFER is an indirect
211 buffer or a minibuffer. In the former case, return the file name
212 of the base buffer; in the latter, return the file name of
213 `minibuffer-selected-window' (or its base buffer)."
214 (let ((buffer (if (minibufferp buffer
)
215 (window-buffer (minibuffer-selected-window))
218 (or (buffer-base-buffer buffer
)
221 (defun org-velocity-minibuffer-contents ()
222 "Return the contents of the minibuffer when it is active."
223 (if (active-minibuffer-window)
224 (with-current-buffer (window-buffer (active-minibuffer-window))
225 (minibuffer-contents))))
227 (defsubst org-velocity-singlep
(object)
228 "Return t when OBJECT is a list or sequence of one element."
231 (= (length object
) 1)))
233 (defun org-velocity-bucket-file ()
234 "Return the proper file for Org-Velocity to search.
235 If `org-velocity-always-use-bucket' is t, use bucket file;
236 complain if missing. Otherwise, if an Org file is current, then
238 (let ((org-velocity-bucket
239 (when org-velocity-bucket
(expand-file-name org-velocity-bucket
)))
241 (let ((buffer-file (org-velocity-buffer-file-name)))
243 ;; Use the target in capture buffers.
244 (org-find-base-buffer-visiting buffer-file
)))))
245 (if org-velocity-always-use-bucket
246 (or org-velocity-bucket
(error "Bucket required but not defined"))
247 (if (and (eq (buffer-local-value 'major-mode
(or buffer
(current-buffer)))
249 (org-velocity-buffer-file-name))
250 (org-velocity-buffer-file-name)
251 (or org-velocity-bucket
252 (error "No bucket and not an Org file"))))))
254 (defvar org-velocity-bucket-buffer nil
)
256 (defsubst org-velocity-bucket-buffer
()
257 (or org-velocity-bucket-buffer
258 (find-file-noselect (org-velocity-bucket-file))))
260 (defsubst org-velocity-match-buffer
()
261 "Return the proper buffer for Org-Velocity to display in."
262 (get-buffer-create org-velocity-match-buffer-name
))
264 (defun org-velocity-beginning-of-headings ()
265 "Goto the start of the first heading."
266 (goto-char (point-min))
267 ;; If we are before the first heading we could still be at the
269 (or (looking-at org-velocity-heading-regexp
)
270 (re-search-forward org-velocity-heading-regexp
)))
272 (defun org-velocity-make-indirect-buffer (heading)
273 "Make or switch to an indirect buffer visiting HEADING."
275 (let* ((bucket (org-velocity-heading-buffer heading
))
276 (name (org-velocity-heading-name heading
))
277 (existing (get-buffer name
)))
278 (if (and existing
(buffer-base-buffer existing
)
279 (equal (buffer-base-buffer existing
) bucket
))
281 (make-indirect-buffer
283 (generate-new-buffer-name (org-velocity-heading-name heading
))))))
285 (defun org-velocity-capture ()
286 "Record a note with `org-capture'."
287 (let ((org-capture-templates
288 org-velocity-capture-templates
))
290 ;; This is no longer automatically selected.
291 (when (org-velocity-singlep org-capture-templates
)
292 (caar org-capture-templates
)))
293 (if org-capture-mode
(rename-buffer org-velocity-search t
))))
295 (defvar org-velocity-saved-winconf nil
)
296 (make-variable-buffer-local 'org-velocity-saved-winconf
)
298 (defun org-velocity-edit-entry (heading)
299 "Edit entry at HEADING in an indirect buffer."
300 (let ((winconf (current-window-configuration)))
301 (let ((buffer (org-velocity-make-indirect-buffer heading
)))
302 (with-current-buffer buffer
303 (let ((org-inhibit-startup t
))
305 (setq org-velocity-saved-winconf winconf
)
306 (goto-char (org-velocity-heading-position heading
))
307 (narrow-to-region (point)
309 (org-end-of-subtree t
)
311 (goto-char (point-min))
312 (add-hook 'org-ctrl-c-ctrl-c-hook
'org-velocity-dismiss nil t
))
313 (pop-to-buffer buffer
)
314 (set (make-local-variable 'header-line-format
)
315 (format "%s Use C-c C-c to finish."
316 (abbreviate-file-name
318 (org-velocity-heading-buffer heading
))))))))
320 (defun org-velocity-dismiss ()
321 "Save current entry and close indirect buffer."
322 (let ((winconf org-velocity-saved-winconf
))
323 (prog1 t
;Tell hook we're done.
326 (when (window-configuration-p winconf
)
327 (set-window-configuration winconf
)))))
329 (defun org-velocity-visit-button (button)
330 (run-hooks 'mouse-leave-buffer-hook
)
331 (if org-velocity-use-search-ring
332 (add-to-history 'search-ring
333 (button-get button
'search
)
335 (org-velocity-edit-entry (button-get button
'match
)))
337 (define-button-type 'org-velocity-button
338 'action
#'org-velocity-visit-button
)
340 (defsubst org-velocity-buttonize
(heading)
341 "Insert HEADING as a text button with no hints."
343 (propertize (org-velocity-heading-name heading
) 'face
'link
)
344 :type
'org-velocity-button
346 'search org-velocity-search
))
348 (defsubst org-velocity-insert-preview
(heading)
349 (when org-velocity-show-previews
353 (org-velocity-heading-preview heading
)
356 (defsubst* org-velocity-present-match
(&key hint match
)
357 (with-current-buffer (org-velocity-match-buffer)
358 (when hint
(insert "#" hint
" "))
359 (org-velocity-buttonize match
)
360 (org-velocity-insert-preview match
)
363 (defun org-velocity-generic-search (search &optional hide-hints
)
364 "Display any entry containing SEARCH."
365 (let ((hints org-velocity-index
) matches
)
367 (while (and hints
(re-search-forward search nil t
))
368 (let ((match (org-velocity-nearest-heading (point))))
369 (org-velocity-present-match
370 :hint
(unless hide-hints
(car hints
))
372 (push match matches
))
373 (setq hints
(cdr hints
))
374 (unless (re-search-forward org-velocity-heading-regexp nil t
)
378 (defun* org-velocity-all-search
(search &optional hide-hints max
)
379 "Display only entries containing every word in SEARCH."
380 (let ((keywords (mapcar 'regexp-quote
(split-string search
)))
381 (hints org-velocity-index
)
385 ;; Return if we've run out of hints.
387 (return-from org-velocity-all-search
(nreverse matches
)))
388 ;; Only search the subtree once.
389 (setq org-map-continue-from
391 (goto-char (line-end-position))
392 (if (re-search-forward org-velocity-heading-regexp nil t
)
395 (when (loop for word in keywords
396 always
(save-excursion
398 (concat "\\<" word
"\\>")
399 org-map-continue-from t
)))
400 (let ((match (org-velocity-nearest-heading (match-end 0))))
401 (org-velocity-present-match
402 :hint
(unless hide-hints
(car hints
))
405 (setq hints
(cdr hints
))))))
408 (defun* org-velocity-present
(search &key hide-hints
)
409 "Buttonize matches for SEARCH in `org-velocity-match-buffer'.
410 If HIDE-HINTS is non-nil, display entries without indices. SEARCH
411 binds `org-velocity-search'.
414 (if (and (stringp search
) (not (string= "" search
)))
415 ;; Fold case when the search string is all lowercase.
416 (let ((case-fold-search (equal search
(downcase search
)))
417 (truncate-partial-width-windows t
))
418 (with-current-buffer (org-velocity-match-buffer)
421 (setq cursor-type nil
424 (with-current-buffer (org-velocity-bucket-buffer)
425 (let ((inhibit-point-motion-hooks t
)
426 (inhibit-field-text-motion t
))
428 (org-velocity-beginning-of-headings)
429 (case org-velocity-search-method
430 (all (org-velocity-all-search search hide-hints
))
431 (phrase (org-velocity-generic-search
432 (concat "\\<" (regexp-quote search
))
434 (any (org-velocity-generic-search
436 (regexp-opt (split-string search
)))
438 (regexp (condition-case lossage
439 (org-velocity-generic-search
442 (minibuffer-message "%s" lossage
))))))))
443 (with-current-buffer (org-velocity-match-buffer)
444 (goto-char (point-min)))))
445 (with-current-buffer (org-velocity-match-buffer)
448 (defun org-velocity-store-link ()
449 "Function for `org-store-link-functions'."
450 (if org-velocity-search
451 (org-store-link-props
452 :search org-velocity-search
)))
454 (add-hook 'org-store-link-functions
'org-velocity-store-link
)
456 (defun* org-velocity-create
(search &key ask
)
457 "Create new heading named SEARCH.
458 If ASK is non-nil, ask first."
459 (when (or (null ask
) (y-or-n-p "No match found, create? "))
460 (let ((org-velocity-search search
)
461 (org-default-notes-file (org-velocity-bucket-file))
462 ;; save a stored link
463 org-store-link-plist
)
464 (org-velocity-capture))
467 (defun org-velocity-engine (search)
468 "Display a list of headings where SEARCH occurs."
469 (let ((org-velocity-search search
))
471 (not (stringp search
))
472 (string= "" search
)) ;exit on empty string
474 (if (and org-velocity-force-new
(eq last-command-event ?\C-j
))
476 (let ((matches (org-velocity-present search
)))
477 (cond ((null matches
) :new
)
478 ((org-velocity-singlep matches
) :follow
)
481 (pop-to-buffer (org-velocity-match-buffer))
482 (let ((hint (org-velocity-electric-read-hint)))
483 (when hint
(case hint
484 (:edit
(org-velocity-read nil search
))
485 (:force
(org-velocity-create search
))
486 (otherwise (org-velocity-activate-button hint
)))))))
487 (:new
(unless (org-velocity-create search
:ask t
)
488 (org-velocity-read nil search
)))
489 (:force
(org-velocity-create search
))
490 (:follow
(if (y-or-n-p "One match, follow? ")
492 (set-buffer (org-velocity-match-buffer))
493 (goto-char (point-min))
494 (button-activate (next-button (point))))
495 (org-velocity-read nil search
)))))))
497 (defun org-velocity-position (item list
)
498 "Return first position of ITEM in LIST."
499 (loop for elt in list
501 when
(equal elt item
)
504 (defun org-velocity-activate-button (char)
505 "Go to button on line number associated with CHAR in `org-velocity-index'."
506 (goto-char (point-min))
507 (forward-line (org-velocity-position char org-velocity-index
))
510 (next-button (point))))
511 (message "%s" (button-label (button-at (point))))
512 (button-activate (button-at (point))))
514 (defun org-velocity-electric-undefined ()
515 "Complain about an undefined key."
518 (substitute-command-keys
519 "\\[org-velocity-electric-new] for new entry,
520 \\[org-velocity-electric-edit] to edit search,
521 \\[scroll-up] to scroll up,
522 \\[scroll-down] to scroll down,
523 \\[keyboard-quit] to quit."))
526 (defun org-velocity-electric-follow (ev)
527 "Follow a hint indexed by keyboard event EV."
528 (interactive (list last-command-event
))
529 (if (not (> (org-velocity-position ev org-velocity-index
)
530 (1- (count-lines (point-min) (point-max)))))
531 (throw 'org-velocity-select ev
)
532 (call-interactively 'org-velocity-electric-undefined
)))
534 (defun org-velocity-electric-click (ev)
535 "Follow hint indexed by a mouse event EV."
537 (throw 'org-velocity-select
538 (nth (1- (count-lines
540 (posn-point (event-start ev
))))
541 org-velocity-index
)))
543 (defun org-velocity-electric-edit ()
544 "Edit the search string."
546 (throw 'org-velocity-select
:edit
))
548 (defun org-velocity-electric-new ()
551 (throw 'org-velocity-select
:force
))
553 (defvar org-velocity-electric-map
554 (let ((map (make-sparse-keymap)))
555 (define-key map
[t] 'org-velocity-electric-undefined)
556 (loop for c in org-velocity-index
557 do (define-key map (char-to-string c) 'org-velocity-electric-follow))
558 (define-key map "0" 'org-velocity-electric-new)
559 (define-key map "\C-v" 'scroll-up)
560 (define-key map "\M-v" 'scroll-down)
561 (define-key map (kbd "RET") 'org-velocity-electric-edit)
562 (define-key map [mouse-1] 'org-velocity-electric-click)
563 (define-key map [mouse-2] 'org-velocity-electric-click)
564 (define-key map [escape] 'keyboard-quit)
565 (define-key map "\C-h" 'help-command)
568 (defun org-velocity-electric-read-hint ()
569 "Read index of button electrically."
570 (with-current-buffer (org-velocity-match-buffer)
571 (use-local-map org-velocity-electric-map)
572 (catch 'org-velocity-select
573 (Electric-command-loop 'org-velocity-select "Follow: "))))
575 (defvar org-velocity-incremental-keymap
576 (let ((map (make-sparse-keymap)))
577 (define-key map [mouse-1] 'org-velocity-click-for-incremental)
578 (define-key map [mouse-2] 'org-velocity-click-for-incremental)
579 (define-key map "\C-v" 'scroll-up)
580 (define-key map "\M-v" 'scroll-down)
583 (defun org-velocity-click-for-incremental ()
584 "Jump out of search and select hint clicked on."
586 (let ((ev last-command-event))
587 (org-velocity-activate-button
590 (posn-point (event-start ev))) 2)
591 org-velocity-index)))
592 (throw 'click (current-buffer)))
594 (defun org-velocity-displaying-completions-p ()
595 "Is there a *Completions* buffer showing?"
596 (get-window-with-predicate
598 (eq (buffer-local-value 'major-mode (window-buffer w))
599 'completion-list-mode))))
601 (defun org-velocity-update ()
602 "Display results of search without hinting.
603 Stop searching once there are more matches than can be displayed."
604 (unless (org-velocity-displaying-completions-p)
605 (let* ((search (org-velocity-minibuffer-contents))
606 (matches (org-velocity-present search :hide-hints t)))
607 (cond ((null matches)
608 (select-window (active-minibuffer-window))
609 (unless (or (null search) (string= "" search))
610 (minibuffer-message "No match; RET to create")))
611 ((and (org-velocity-singlep matches)
612 org-velocity-exit-on-match)
613 (throw 'click search))
615 (with-current-buffer (org-velocity-match-buffer)
616 (use-local-map org-velocity-incremental-keymap)))))))
618 (defvar dabbrev--last-abbrev)
620 (defun org-velocity-dabbrev-completion-list (abbrev)
621 "Return all dabbrev completions for ABBREV."
622 ;; This is based on `dabbrev-completion'.
623 (dabbrev--reset-global-variables)
624 (setq dabbrev--last-abbrev abbrev)
625 (dabbrev--find-all-expansions abbrev case-fold-search))
627 (defvar org-velocity-local-completion-map
628 (let ((map (make-sparse-keymap)))
629 (set-keymap-parent map minibuffer-local-completion-map)
630 (define-key map " " 'self-insert-command)
631 (define-key map [remap minibuffer-complete] 'minibuffer-complete-word)
633 "Keymap for completion with `completing-read'.")
635 (defun org-velocity-read-with-completion (prompt)
636 "Completing read with PROMPT."
637 (let ((minibuffer-local-completion-map
638 org-velocity-local-completion-map)
639 (completion-no-auto-exit t)
642 (case org-velocity-search-method
643 (phrase #'completing-read)
644 (any #'completing-read-multiple)
645 (all #'completing-read-multiple))
647 (completion-table-dynamic
648 'org-velocity-dabbrev-completion-list))))
650 (defun org-velocity-read-string (prompt &optional initial-input)
651 "Read string with PROMPT followed by INITIAL-INPUT."
652 ;; The use of initial inputs to the minibuffer is deprecated (see
653 ;; `read-from-minibuffer'), but in this case it is the user-friendly
655 (minibuffer-with-setup-hook
656 (lexical-let ((initial-input initial-input))
658 (and initial-input (insert initial-input))
659 (goto-char (point-max))))
660 (if (eq org-velocity-search-method 'regexp)
662 (if org-velocity-use-completion
663 (org-velocity-read-with-completion prompt)
664 (read-string prompt)))))
666 (defun org-velocity-incremental-read (prompt)
667 "Read string with PROMPT and display results incrementally."
670 (let* ((match-window (display-buffer (org-velocity-match-buffer)))
672 ;; Truncate the index to the size of the buffer to be
674 (with-selected-window match-window
675 (if (> (window-height) (length org-velocity-index))
676 ;; (subseq org-velocity-index 0 (window-height))
677 (let ((hints (copy-sequence org-velocity-index)))
678 (setcdr (nthcdr (window-height) hints) nil)
680 org-velocity-index))))
682 (add-hook 'post-command-hook 'org-velocity-update)
683 (if (eq org-velocity-search-method 'regexp)
685 (if org-velocity-use-completion
686 (org-velocity-read-with-completion prompt)
687 (read-string prompt)))))
688 (remove-hook 'post-command-hook 'org-velocity-update))))
689 (if (bufferp res) (org-pop-to-buffer-same-window res) res)))
691 (defun org-velocity (arg &optional search)
692 "Read a search string SEARCH for Org-Velocity interface.
693 This means that a buffer will display all headings where SEARCH
694 occurs, where one can be selected by a mouse click or by typing
695 its index. If SEARCH does not occur, then a new heading may be
696 created named SEARCH.
698 If `org-velocity-bucket' is defined and
699 `org-velocity-always-use-bucket' is non-nil, then the bucket file
700 will be used; otherwise, this will work when called in any Org
701 file. Calling with ARG forces current file."
703 (let ((org-velocity-always-use-bucket
704 (if arg nil org-velocity-always-use-bucket)))
705 ;; complain if inappropriate
706 (assert (org-velocity-bucket-file))
707 (let ((org-velocity-bucket-buffer
708 (find-file-noselect (org-velocity-bucket-file))))
710 (let ((dabbrev-search-these-buffers-only
711 (list (org-velocity-bucket-buffer))))
713 (if org-velocity-search-is-incremental
714 (org-velocity-incremental-read "Velocity search: ")
715 (org-velocity-read-string "Velocity search: " search))))
717 (kill-buffer (org-velocity-match-buffer))
718 (delete-other-windows))))))
720 (defalias 'org-velocity-read 'org-velocity)
722 (provide 'org-velocity)
724 ;;; org-velocity.el ends here