org-velocity.el 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. ;;; org-velocity.el --- something like Notational Velocity for Org.
  2. ;; Copyright (C) 2010 Paul M. Rodriguez
  3. ;; Author: Paul M. Rodriguez <paulmrodriguez@gmail.com>
  4. ;; Created: 2010-05-05
  5. ;; Version: 2.3
  6. ;; This file is not part of GNU Emacs.
  7. ;; This program is free software; you can redistribute it and/or
  8. ;; modify it under the terms of the GNU General Public License as
  9. ;; published by the Free Software Foundation version 2.
  10. ;; This program is distributed in the hope that it will be useful, but
  11. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;; General Public License for more details.
  14. ;; For a copy of the GNU General Public License, search the Internet,
  15. ;; or write to the Free Software Foundation, Inc., 59 Temple Place,
  16. ;; Suite 330, Boston, MA 02111-1307 USA
  17. ;;; Commentary:
  18. ;; Org-Velocity.el implements an interface for Org inspired by the
  19. ;; minimalist notetaking program Notational Velocity. The idea is to
  20. ;; allow you to maintain, amass and access brief notes on many
  21. ;; subjects with minimal fuss.
  22. ;; It can be used in two ways: to store and access notes from any
  23. ;; buffer a universal bucket file; or as a method for navigating any
  24. ;; Org file.
  25. ;; The name of the bucket-file (`org-velocity-bucket') and whether to
  26. ;; always use it (`org-velocity-always-use-bucket-file') are set
  27. ;; through Customize. If the bucket file is set but not always to be
  28. ;; used, then calling Org-Velocity outside of Org-mode uses the bucket
  29. ;; file; calling it in Org mode uses the current buffer. If no bucket
  30. ;; file is set then Org-Velocity only works when called from Org.
  31. ;; Even if the bucket file is always to be used, calling
  32. ;; `org-velocity-read' with an argument will use the current file.
  33. ;; The interface, unlike its inspiration, is not incremental.
  34. ;; Org-Velocity prompts for search terms in the usual way; if the user
  35. ;; has customized `org-velocity-use-completion', completion is offered
  36. ;; on the headings in the target file. If the search multiple times
  37. ;; in the target file, a buffer containing a buttonized list of the
  38. ;; headings where it occurs is displayed. Results beyond what can be
  39. ;; indexed are discarded. After clicking on a heading, or typing a
  40. ;; character associated with it, the user is taken to the heading.
  41. ;; (Typing 0 forces a new heading to be created.) If
  42. ;; `org-velocity-edit-indirectly' is so set, the heading and its
  43. ;; subtree are displayed in an indirect buffer. Otherwise the user is
  44. ;; simply taken to the proper buffer and position.
  45. ;; If the user simply hits RET at the prompt, without making a choice,
  46. ;; then the search is restored for editing. A blank search quits.
  47. ;; This method of selection is obviously not as slick as the original,
  48. ;; but probably more useful for a keyboard-driven interface.
  49. ;; If the search does not occur in the file the user is offered a
  50. ;; choice to create a new heading named with the search. Org-Velocity
  51. ;; will use `org-capture' or `org-remember' if they are loaded,
  52. ;; preferring `org-capture'. Otherwise the user is simply taken to a
  53. ;; new heading at the end of the file.
  54. ;; Thanks to Richard Riley, Carsten Dominik, Bastien Guerry, and Jeff
  55. ;; Horn for their suggestions.
  56. ;;; Usage:
  57. ;; (require 'org-velocity)
  58. ;; (setq org-velocity-bucket (concat org-directory "/bucket.org"))
  59. ;; (global-set-key (kbd "C-c v") 'org-velocity-read)
  60. ;;; Code:
  61. (require 'org)
  62. (require 'button)
  63. (require 'electric)
  64. (eval-when-compile (require 'cl))
  65. (defgroup org-velocity nil
  66. "Notational Velocity-style interface for Org."
  67. :tag "Org-Velocity"
  68. :group 'outlines
  69. :group 'hypermedia)
  70. (defcustom org-velocity-bucket ""
  71. "Where is the bucket file?"
  72. :group 'org-velocity
  73. :type 'file)
  74. (defcustom org-velocity-always-use-bucket nil
  75. "Use bucket file even when called from an Org buffer?"
  76. :group 'org-velocity
  77. :type 'boolean)
  78. (defcustom org-velocity-use-completion nil
  79. "Complete on heading names?"
  80. :group 'org-velocity
  81. :type 'boolean)
  82. (defcustom org-velocity-edit-indirectly t
  83. "Edit entries in an indirect buffer or just visit the file?"
  84. :group 'org-velocity
  85. :type 'boolean)
  86. (defcustom org-velocity-search-method 'phrase
  87. "Match on whole phrase, any word, or all words?"
  88. :group 'org-velocity
  89. :type '(choice
  90. (const :tag "Match whole phrase" phrase)
  91. (const :tag "Match any word" any)
  92. (const :tag "Match all words" all)))
  93. (defcustom org-velocity-create-method 'capture
  94. "Prefer `org-capture', `org-remember', or neither?"
  95. :group 'org-velocity
  96. :type '(choice
  97. (const :tag "Prefer capture > remember > default." capture)
  98. (const :tag "Prefer remember > default." remember)
  99. (const :tag "Edit in buffer." buffer)))
  100. (defcustom org-velocity-allow-regexps nil
  101. "Allow searches to use regular expressions?"
  102. :group 'org-velocity
  103. :type 'boolean)
  104. (defcustom org-velocity-remember-templates
  105. '(("Velocity entry"
  106. ?v
  107. "* %:search\n\n%i%?"
  108. nil
  109. bottom))
  110. "Use these templates with `org-remember'.
  111. Meanwhile `org-default-notes-file' is bound to `org-velocity-use-file'.
  112. The keyword :search inserts the current search.
  113. See the documentation for `org-remember-templates'."
  114. :group 'org-velocity
  115. :type (or (get 'org-remember-templates 'custom-type) 'list))
  116. (defcustom org-velocity-capture-templates
  117. '(("v"
  118. "Velocity entry"
  119. entry
  120. (file "")
  121. "* %:search\n\n%i%?"))
  122. "Use these template with `org-capture'.
  123. Meanwhile `org-default-notes-file' is bound to `org-velocity-use-file'.
  124. The keyword :search inserts the current search.
  125. See the documentation for `org-capture-templates'."
  126. :group 'org-velocity
  127. :type (or (get 'org-capture-templates 'custom-type) 'list))
  128. (defstruct (org-velocity-heading
  129. (:constructor org-velocity-make-heading)
  130. (:type list))
  131. (marker (point-marker))
  132. (name (substring-no-properties
  133. (org-get-heading))))
  134. (defconst org-velocity-index
  135. (eval-when-compile
  136. (nconc (number-sequence 49 57) ;numbers
  137. (number-sequence 97 122) ;lowercase letters
  138. (number-sequence 65 90))) ;uppercase letters
  139. "List of chars for indexing results.")
  140. (defconst org-velocity-display-buffer-name "*Velocity headings*")
  141. (defvar org-velocity-search nil
  142. "Variable to bind to current search.")
  143. (defsubst org-velocity-buffer-file-name (&optional buffer)
  144. "Return the name of the file BUFFER saves to.
  145. Same as function `buffer-file-name' unless BUFFER is an
  146. indirect buffer."
  147. (buffer-file-name
  148. (or (buffer-base-buffer buffer)
  149. buffer)))
  150. (defun org-velocity-use-file ()
  151. "Return the proper file for Org-Velocity to search.
  152. If `org-velocity-always-use-bucket' is t, use bucket file; complain
  153. if missing. Otherwise if this is an Org file, use it."
  154. (or
  155. ;; In remember and capture buffers the target should be used.
  156. (and org-remember-mode org-default-notes-file)
  157. (let ((org-velocity-bucket
  158. (and org-velocity-bucket (expand-file-name org-velocity-bucket))))
  159. (if org-velocity-always-use-bucket
  160. (or org-velocity-bucket (error "Bucket required but not defined"))
  161. (if (and (eq major-mode 'org-mode)
  162. (org-velocity-buffer-file-name))
  163. (org-velocity-buffer-file-name)
  164. (or org-velocity-bucket
  165. (error "No bucket and not an Org file")))))))
  166. (defsubst org-velocity-display-buffer ()
  167. "Return the proper buffer for Org-Velocity to display in."
  168. (get-buffer-create org-velocity-display-buffer-name))
  169. (defsubst org-velocity-bucket-buffer ()
  170. "Return proper buffer for bucket operations."
  171. (find-file-noselect (org-velocity-use-file)))
  172. (defun org-velocity-quote (search)
  173. "Quote SEARCH as a regexp if `org-velocity-allow-regexps' is non-nil.
  174. Acts like `regexp-quote' on a string, `regexp-opt' on a list."
  175. (if org-velocity-allow-regexps
  176. search
  177. (if (listp search)
  178. (regexp-opt search)
  179. (regexp-quote search))))
  180. (defun org-velocity-nearest-heading (position)
  181. "Return last heading at POSITION.
  182. If there is no last heading, return nil."
  183. (save-excursion
  184. (goto-char position)
  185. (unless (org-before-first-heading-p)
  186. (org-back-to-heading)
  187. (org-velocity-make-heading))))
  188. (defun org-velocity-make-button-action (heading)
  189. "Return a form to visit HEADING."
  190. `(lambda (button)
  191. (run-hooks 'mouse-leave-buffer-hook) ;turn off temporary modes
  192. (if org-velocity-edit-indirectly
  193. (org-velocity-edit-entry ',heading)
  194. (progn
  195. (message "%s" ,(org-velocity-heading-name heading))
  196. (switch-to-buffer (marker-buffer
  197. ,(org-velocity-heading-marker heading)))
  198. (goto-char (marker-position
  199. ,(org-velocity-heading-marker heading)))))))
  200. (defun org-velocity-edit-entry (heading)
  201. "Edit entry at HEADING in an indirect buffer."
  202. (let ((buffer (make-indirect-buffer
  203. (marker-buffer (org-velocity-heading-marker heading))
  204. (generate-new-buffer-name
  205. (org-velocity-heading-name heading)))))
  206. (with-current-buffer buffer
  207. (let ((org-inhibit-startup t))
  208. (org-mode))
  209. (goto-char (marker-position (org-velocity-heading-marker heading)))
  210. (narrow-to-region (point)
  211. (save-excursion
  212. (org-end-of-subtree)
  213. (point)))
  214. (goto-char (point-min))
  215. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-velocity-dismiss nil t))
  216. (pop-to-buffer buffer)
  217. (set (make-local-variable 'header-line-format)
  218. (format "%s Use C-c C-c to finish."
  219. (abbreviate-file-name
  220. (buffer-file-name
  221. (marker-buffer
  222. (org-velocity-heading-marker heading))))))))
  223. (defun org-velocity-dismiss ()
  224. "Save current entry and close indirect buffer."
  225. (progn
  226. (save-buffer)
  227. (kill-buffer)))
  228. (defun org-velocity-buttonize (heading)
  229. "Insert HEADING as a text button."
  230. (insert (format "#%c " (nth (1- (line-number-at-pos))
  231. org-velocity-index)))
  232. (let ((action (org-velocity-make-button-action heading)))
  233. (insert-text-button
  234. (org-velocity-heading-name heading)
  235. 'action action))
  236. (newline))
  237. (defun org-velocity-remember ()
  238. "Use `org-remember' to record a note."
  239. (let ((org-remember-templates
  240. org-velocity-remember-templates))
  241. (call-interactively 'org-remember)))
  242. (defun org-velocity-capture ()
  243. "Use `org-capture' to record a note."
  244. (let ((org-capture-templates
  245. org-velocity-capture-templates))
  246. (if (fboundp 'org-capture) ;; quiet compiler
  247. (call-interactively 'org-capture))))
  248. (defun org-velocity-insert-heading (heading)
  249. "Add a new heading named HEADING."
  250. (with-current-buffer (org-velocity-bucket-buffer)
  251. (goto-char (point-max))
  252. (newline)
  253. (org-insert-heading) (insert heading)
  254. (newline)
  255. (goto-char (point-max))))
  256. (defun org-velocity-create-heading ()
  257. "Add and visit a new heading."
  258. (org-store-link nil)
  259. (destructuring-bind (&key search initial
  260. &allow-other-keys)
  261. org-store-link-plist
  262. (org-velocity-insert-heading search)
  263. (switch-to-buffer (org-velocity-bucket-buffer))
  264. (insert (or initial ""))))
  265. (defun org-velocity-all-search (search)
  266. "Return entries containing all words in SEARCH."
  267. (when (file-exists-p (org-velocity-use-file))
  268. (save-excursion
  269. (delq nil
  270. (let ((keywords
  271. (mapcar 'org-velocity-quote
  272. (split-string search)))
  273. (case-fold-search t))
  274. (org-map-entries
  275. (lambda ()
  276. (if (loop with limit = (save-excursion
  277. (org-end-of-subtree)
  278. (point))
  279. for word in keywords
  280. always (save-excursion
  281. (re-search-forward word limit t)))
  282. (org-velocity-nearest-heading
  283. (match-beginning 0))))))))))
  284. (defun org-velocity-generic-search (search)
  285. "Return entries containing SEARCH."
  286. (save-excursion
  287. (delq nil
  288. (nreverse
  289. (let (matches (case-fold-search t))
  290. (goto-char (point-min))
  291. (while (re-search-forward search
  292. (point-max) t)
  293. (push (org-velocity-nearest-heading (match-beginning 0))
  294. matches)
  295. (outline-next-heading))
  296. matches)))))
  297. (defsubst org-velocity-phrase-search (search)
  298. "Return entries containing SEARCH as a phrase."
  299. (org-velocity-generic-search (org-velocity-quote search)))
  300. (defsubst org-velocity-any-search (search)
  301. "Return entries containing any word in SEARCH."
  302. (org-velocity-generic-search (org-velocity-quote (split-string search))))
  303. (defun org-velocity-present (headings)
  304. "Buttonize HEADINGS in `org-velocity-display-buffer'."
  305. (and (listp headings) (delete-dups headings))
  306. (let ((cdr (nthcdr
  307. (1- (length org-velocity-index))
  308. headings)))
  309. (and (consp cdr) (setcdr cdr nil)))
  310. (with-current-buffer (org-velocity-display-buffer)
  311. (mapc
  312. 'org-velocity-buttonize
  313. headings)
  314. (goto-char (point-min))))
  315. (defun org-velocity-create-1 ()
  316. "Create a new heading.
  317. The possible methods are `org-velocity-capture',
  318. `org-velocity-remember', or `org-velocity-create-heading', in
  319. that order. Which is preferred is determined by
  320. `org-velocity-create-method'."
  321. (funcall
  322. (ecase org-velocity-create-method
  323. (capture (or (and (featurep 'org-capture) 'org-velocity-capture)
  324. (and (featurep 'org-remember) 'org-velocity-remember)
  325. 'org-velocity-create-heading))
  326. (remember (or (and (featurep 'org-remember) 'org-velocity-remember)
  327. 'org-velocity-create-heading))
  328. (buffer 'org-velocity-create-heading))))
  329. (defun org-velocity-store-link ()
  330. "Function for `org-store-link-functions'."
  331. (if org-velocity-search
  332. (org-store-link-props
  333. :search org-velocity-search)))
  334. (add-hook 'org-store-link-functions 'org-velocity-store-link)
  335. (defun org-velocity-create (search &optional ask)
  336. "Create new heading named SEARCH.
  337. If ASK is non-nil, ask first."
  338. (when (or (null ask)
  339. (y-or-n-p "No match found, create? "))
  340. (let ((org-velocity-search search)
  341. (org-default-notes-file (org-velocity-use-file))
  342. ;; save a stored link
  343. (org-store-link-plist))
  344. (org-velocity-create-1))
  345. search))
  346. (defun org-velocity-get-matches (search)
  347. "Return matches for SEARCH in current bucket.
  348. Use method specified by `org-velocity-search-method'."
  349. (with-current-buffer (org-velocity-bucket-buffer)
  350. (case org-velocity-search-method
  351. ('phrase (org-velocity-phrase-search search))
  352. ('any (org-velocity-any-search search))
  353. ('all (org-velocity-all-search search)))))
  354. (defun org-velocity-engine (search)
  355. "Display a list of headings where SEARCH occurs."
  356. (with-current-buffer (org-velocity-display-buffer)
  357. (erase-buffer)
  358. (setq cursor-type nil))
  359. (unless (or
  360. (not (stringp search))
  361. (string-equal "" search)) ;exit on empty string
  362. (case
  363. (with-current-buffer (org-velocity-bucket-buffer)
  364. (save-excursion
  365. (let ((matches (org-velocity-get-matches search)))
  366. (org-velocity-present matches)
  367. (cond ((zerop (length matches)) 'new)
  368. ((= (length matches) 1) 'follow)
  369. ((> (length matches) 1) 'prompt)))))
  370. ('prompt (progn
  371. (Electric-pop-up-window (org-velocity-display-buffer))
  372. (let ((hint (org-velocity-electric-follow-hint)))
  373. (if hint
  374. (case hint
  375. (edit (org-velocity-read nil search))
  376. (new (org-velocity-create search))
  377. (otherwise (org-velocity-activate-button hint)))))))
  378. ('new (unless (org-velocity-create search t)
  379. (org-velocity-read nil search)))
  380. ('follow (if (y-or-n-p "One match, follow? ")
  381. (progn
  382. (set-buffer (org-velocity-display-buffer))
  383. (goto-char (point-min))
  384. (button-activate (next-button (point))))
  385. (org-velocity-read nil search))))))
  386. (defun org-velocity-position (item list)
  387. "Return first position of ITEM in LIST."
  388. (loop for elt in list
  389. for i from 0
  390. if (equal elt item)
  391. return i))
  392. (defun org-velocity-activate-button (char)
  393. "Go to button on line number associated with CHAR in `org-velocity-index'."
  394. (goto-char (point-min))
  395. (forward-line (org-velocity-position char org-velocity-index))
  396. (goto-char
  397. (button-start
  398. (next-button (point))))
  399. (message "%s" (button-label (button-at (point))))
  400. (button-activate (button-at (point))))
  401. (defun org-velocity-electric-undefined ()
  402. "Complain about an undefined key."
  403. (interactive)
  404. (message "%s"
  405. (substitute-command-keys
  406. "\\[org-velocity-electric-new] for new entry, \\[org-velocity-electric-edit] to edit search, \\[scroll-up] to scroll."))
  407. (sit-for 4))
  408. (defun org-velocity-electric-follow (ev)
  409. "Follow a hint indexed by keyboard event EV."
  410. (interactive (list last-command-event))
  411. (if (not (> (org-velocity-position ev org-velocity-index)
  412. (1- (count-lines (point-min) (point-max)))))
  413. (throw 'org-velocity-select ev)
  414. (call-interactively 'org-velocity-electric-undefined)))
  415. (defun org-velocity-electric-click (ev)
  416. "Follow hint indexed by a mouse event EV."
  417. (interactive "e")
  418. (throw 'org-velocity-select
  419. (nth (1- (count-lines
  420. (point-min)
  421. (posn-point (event-start ev))))
  422. org-velocity-index)))
  423. (defun org-velocity-electric-edit ()
  424. "Edit the search string."
  425. (interactive)
  426. (throw 'org-velocity-select 'edit))
  427. (defun org-velocity-electric-new ()
  428. "Force a new entry."
  429. (interactive)
  430. (throw 'org-velocity-select 'new))
  431. (defvar org-velocity-electric-map
  432. (let ((map (make-sparse-keymap)))
  433. (define-key map [t] 'org-velocity-electric-undefined) (loop for c in org-velocity-index
  434. do (define-key map (char-to-string c) 'org-velocity-electric-follow))
  435. (define-key map "0" 'org-velocity-electric-new)
  436. (define-key map [tab] 'scroll-up)
  437. (define-key map [return] 'org-velocity-electric-edit)
  438. (define-key map [mouse-1] 'org-velocity-electric-click)
  439. (define-key map [mouse-2] 'org-velocity-electric-click)
  440. (define-key map [escape escape escape] 'keyboard-quit)
  441. (define-key map "\C-h" 'help-command)
  442. map))
  443. (defun org-velocity-electric-follow-hint ()
  444. "Read index of button electrically."
  445. (with-current-buffer (org-velocity-display-buffer)
  446. (use-local-map org-velocity-electric-map)
  447. (catch 'org-velocity-select
  448. (Electric-command-loop 'org-velocity-select
  449. "Follow: "))))
  450. (defun org-velocity-read-with-completion (prompt)
  451. "Like `completing-read' on entries with PROMPT.
  452. Use `minibuffer-local-filename-completion-map'."
  453. (let ((minibuffer-local-completion-map
  454. minibuffer-local-filename-completion-map))
  455. (completing-read
  456. prompt
  457. (mapcar 'substring-no-properties
  458. (org-map-entries 'org-get-heading)))))
  459. (defun org-velocity-read-string (prompt &optional initial-input)
  460. "Read string with PROMPT followed by INITIAL-INPUT."
  461. ;; The use of initial inputs to the minibuffer is deprecated (see
  462. ;; `read-from-minibuffer'), but in this case it is the user-friendly
  463. ;; thing to do.
  464. (minibuffer-with-setup-hook
  465. (lexical-let ((initial-input initial-input))
  466. (lambda ()
  467. (and initial-input (insert initial-input))
  468. (goto-char (point-max))))
  469. (if (and org-velocity-use-completion
  470. ;; map-entries complains for nonexistent files
  471. (file-exists-p (org-velocity-use-file)))
  472. (org-velocity-read-with-completion prompt)
  473. (read-string prompt))))
  474. (defun org-velocity-read (arg &optional search)
  475. "Read a search string SEARCH for Org-Velocity interface.
  476. This means that a buffer will display all headings where SEARCH
  477. occurs, where one can be selected by a mouse click or by typing
  478. its index. If SEARCH does not occur, then a new heading may be
  479. created named SEARCH.
  480. If `org-velocity-bucket' is defined and
  481. `org-velocity-always-use-bucket' is non-nil, then the bucket file
  482. will be used; otherwise, this will work when called in any Org
  483. file. Calling with ARG forces current file."
  484. (interactive "P")
  485. (let ((org-velocity-always-use-bucket
  486. (if arg nil org-velocity-always-use-bucket)))
  487. ;; complain if inappropriate
  488. (assert (org-velocity-use-file))
  489. (unwind-protect
  490. (org-velocity-engine
  491. (org-velocity-read-string "Velocity search: " search))
  492. (progn
  493. (kill-buffer (org-velocity-display-buffer))
  494. (delete-other-windows)))))
  495. (provide 'org-velocity)
  496. ;;; org-velocity.el ends here