org-velocity.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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.2
  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, and Bastien Guerry for
  55. ;; 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. (defstruct (org-velocity-heading
  105. (:constructor org-velocity-make-heading)
  106. (:type list))
  107. (marker (point-marker))
  108. (name (substring-no-properties
  109. (org-get-heading))))
  110. (defconst org-velocity-index
  111. (eval-when-compile
  112. (nconc (number-sequence 49 57) ;numbers
  113. (number-sequence 97 122) ;lowercase letters
  114. (number-sequence 65 90))) ;uppercase letters
  115. "List of chars for indexing results.")
  116. (defun org-velocity-use-file ()
  117. "Return the proper file for Org-Velocity to search.
  118. If `org-velocity-always-use-bucket' is t, use bucket file; complain
  119. if missing. Otherwise if this is an Org file, use it."
  120. (let ((org-velocity-bucket
  121. (and org-velocity-bucket (expand-file-name org-velocity-bucket))))
  122. (if org-velocity-always-use-bucket
  123. (or org-velocity-bucket (error "Bucket required but not defined"))
  124. (if (and (eq major-mode 'org-mode)
  125. (buffer-file-name))
  126. (buffer-file-name)
  127. (or org-velocity-bucket
  128. (error "No bucket and not an Org file"))))))
  129. (defsubst org-velocity-display-buffer ()
  130. "Return the proper buffer for Org-Velocity to display in."
  131. (get-buffer-create "*Velocity headings*"))
  132. (defsubst org-velocity-bucket-buffer ()
  133. "Return proper buffer for bucket operations."
  134. (find-file-noselect (org-velocity-use-file)))
  135. (defun org-velocity-quote (search)
  136. "Quote SEARCH as a regexp if `org-velocity-allow-regexps' is non-nil.
  137. Acts like `regexp-quote' on a string, `regexp-opt' on a list."
  138. (if org-velocity-allow-regexps
  139. search
  140. (if (listp search)
  141. (regexp-opt search)
  142. (regexp-quote search))))
  143. (defun org-velocity-nearest-heading (position)
  144. "Return last heading at POSITION.
  145. If there is no last heading, return nil."
  146. (save-excursion
  147. (goto-char position)
  148. (unless (org-before-first-heading-p)
  149. (org-back-to-heading)
  150. (org-velocity-make-heading))))
  151. (defun org-velocity-make-button-action (heading)
  152. "Return a form to visit HEADING."
  153. `(lambda (button)
  154. (run-hooks 'mouse-leave-buffer-hook) ;turn off temporary modes
  155. (if org-velocity-edit-indirectly
  156. (org-velocity-edit-entry ',heading)
  157. (progn
  158. (message "%s" ,(org-velocity-heading-name heading))
  159. (switch-to-buffer (marker-buffer
  160. ,(org-velocity-heading-marker heading)))
  161. (goto-char (marker-position
  162. ,(org-velocity-heading-marker heading)))))))
  163. (defun org-velocity-edit-entry (heading)
  164. "Edit entry at HEADING in an indirect buffer."
  165. (let ((buffer (make-indirect-buffer
  166. (marker-buffer (org-velocity-heading-marker heading))
  167. (generate-new-buffer-name
  168. (org-velocity-heading-name heading)))))
  169. (with-current-buffer buffer
  170. (let ((org-inhibit-startup t))
  171. (org-mode))
  172. (goto-char (marker-position (org-velocity-heading-marker heading)))
  173. (narrow-to-region (point)
  174. (save-excursion
  175. (org-end-of-subtree)
  176. (point)))
  177. (goto-char (point-min))
  178. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-velocity-dismiss nil t))
  179. (pop-to-buffer buffer)
  180. (set (make-local-variable 'header-line-format)
  181. (format "%s Use C-c C-c to finish."
  182. (abbreviate-file-name
  183. (buffer-file-name
  184. (marker-buffer
  185. (org-velocity-heading-marker heading))))))))
  186. (defun org-velocity-dismiss ()
  187. "Save current entry and close indirect buffer."
  188. (progn
  189. (save-buffer)
  190. (kill-buffer)))
  191. (defun org-velocity-buttonize (heading)
  192. "Insert HEADING as a text button."
  193. (insert (format "#%c " (nth (1- (line-number-at-pos))
  194. org-velocity-index)))
  195. (let ((action (org-velocity-make-button-action heading)))
  196. (insert-text-button
  197. (org-velocity-heading-name heading)
  198. 'action action))
  199. (newline))
  200. (defun org-velocity-remember (heading &optional region)
  201. "Use `org-remember' to record a note to HEADING.
  202. If there is a REGION that will be inserted."
  203. (let ((org-remember-templates
  204. (list (list
  205. "Velocity entry"
  206. ?v
  207. (format "* %s\n\n%%?%s" heading (or region ""))
  208. (org-velocity-use-file)
  209. 'bottom))))
  210. (org-remember nil ?v)))
  211. (defun org-velocity-capture (heading &optional region)
  212. "Use `org-capture' to record a note to HEADING.
  213. If there is a REGION that will be inserted."
  214. (let ((org-capture-templates
  215. (list `("v"
  216. "Velocity entry"
  217. entry
  218. (file ,(org-velocity-use-file))
  219. ,(format "* %s\n\n%%?%s" heading (or region ""))))))
  220. (if (fboundp 'org-capture) ;; quiet compiler
  221. (org-capture nil "v"))))
  222. (defun org-velocity-insert-heading (heading)
  223. "Add a new heading named HEADING."
  224. (with-current-buffer (org-velocity-bucket-buffer)
  225. (goto-char (point-max))
  226. (newline)
  227. (org-insert-heading) (insert heading)
  228. (newline)
  229. (goto-char (point-max))))
  230. (defun org-velocity-create-heading (search region)
  231. "Add and visit a new heading named SEARCH.
  232. If REGION is non-nil insert as the contents of the heading."
  233. (org-velocity-insert-heading search)
  234. (switch-to-buffer (org-velocity-bucket-buffer))
  235. (when region (insert region)))
  236. (defun org-velocity-all-search (search)
  237. "Return entries containing all words in SEARCH."
  238. (when (file-exists-p (org-velocity-use-file))
  239. (save-excursion
  240. (delq nil
  241. (let ((keywords
  242. (mapcar 'org-velocity-quote
  243. (split-string search)))
  244. (case-fold-search t))
  245. (org-map-entries
  246. (lambda ()
  247. (if (loop with limit = (save-excursion
  248. (org-end-of-subtree)
  249. (point))
  250. for word in keywords
  251. always (save-excursion
  252. (re-search-forward word limit t)))
  253. (org-velocity-nearest-heading
  254. (match-beginning 0))))))))))
  255. (defun org-velocity-generic-search (search)
  256. "Return entries containing SEARCH."
  257. (save-excursion
  258. (delq nil
  259. (nreverse
  260. (let (matches (case-fold-search t))
  261. (goto-char (point-min))
  262. (while (re-search-forward search
  263. (point-max) t)
  264. (push (org-velocity-nearest-heading (match-beginning 0))
  265. matches)
  266. (outline-next-heading))
  267. matches)))))
  268. (defsubst org-velocity-phrase-search (search)
  269. "Return entries containing SEARCH as a phrase."
  270. (org-velocity-generic-search (org-velocity-quote search)))
  271. (defsubst org-velocity-any-search (search)
  272. "Return entries containing any word in SEARCH."
  273. (org-velocity-generic-search (org-velocity-quote (split-string search))))
  274. (defun org-velocity-present (headings)
  275. "Buttonize HEADINGS in `org-velocity-display-buffer'."
  276. (and (listp headings) (delete-dups headings))
  277. (let ((cdr (nthcdr
  278. (1- (length org-velocity-index))
  279. headings)))
  280. (and (consp cdr) (setcdr cdr nil)))
  281. (with-current-buffer (org-velocity-display-buffer)
  282. (mapc
  283. 'org-velocity-buttonize
  284. headings)
  285. (goto-char (point-min))))
  286. (defun org-velocity-create-1 (search region)
  287. "Create a new heading named SEARCH.
  288. If REGION is non-nil insert as contents of new heading.
  289. The possible methods are `org-velocity-capture',
  290. `org-velocity-remember', or `org-velocity-create-heading', in
  291. that order. Which is preferred is determined by
  292. `org-velocity-create-method'."
  293. (funcall
  294. (ecase org-velocity-create-method
  295. (capture (or (and (featurep 'org-capture) 'org-velocity-capture)
  296. (and (featurep 'org-remember) 'org-velocity-remember)
  297. 'org-velocity-create-heading))
  298. (remember (or (and (featurep 'org-remember) 'org-velocity-remember)
  299. 'org-velocity-create-heading))
  300. (buffer 'org-velocity-create-heading))
  301. search region))
  302. (defun org-velocity-create (search &optional ask)
  303. "Create new heading named SEARCH.
  304. If ASK is non-nil, ask first."
  305. (if (or (null ask)
  306. (y-or-n-p "No match found, create? "))
  307. ;; if there's a region, we want to insert it
  308. (let ((region (if (use-region-p)
  309. (buffer-substring
  310. (region-beginning)
  311. (region-end)))))
  312. (with-current-buffer (org-velocity-bucket-buffer)
  313. (org-velocity-create-1 search region))
  314. (when region (message "%s" "Inserted region"))
  315. search)))
  316. (defun org-velocity-get-matches (search)
  317. "Return matches for SEARCH in current bucket.
  318. Use method specified by `org-velocity-search-method'."
  319. (with-current-buffer (org-velocity-bucket-buffer)
  320. (case org-velocity-search-method
  321. ('phrase (org-velocity-phrase-search search))
  322. ('any (org-velocity-any-search search))
  323. ('all (org-velocity-all-search search)))))
  324. (defun org-velocity-engine (search)
  325. "Display a list of headings where SEARCH occurs."
  326. (with-current-buffer (org-velocity-display-buffer)
  327. (erase-buffer)
  328. (setq cursor-type nil))
  329. (unless (or
  330. (not (stringp search))
  331. (string-equal "" search)) ;exit on empty string
  332. (case
  333. (with-current-buffer (org-velocity-bucket-buffer)
  334. (save-excursion
  335. (let ((matches (org-velocity-get-matches search)))
  336. (org-velocity-present matches)
  337. (cond ((zerop (length matches)) 'new)
  338. ((= (length matches) 1) 'follow)
  339. ((> (length matches) 1) 'prompt)))))
  340. ('prompt (progn
  341. (Electric-pop-up-window (org-velocity-display-buffer))
  342. (let ((hint (org-velocity-electric-follow-hint)))
  343. (if hint
  344. (case hint
  345. (edit (org-velocity-read nil search))
  346. (new (org-velocity-create search))
  347. (otherwise (org-velocity-activate-button hint)))))))
  348. ('new (unless (org-velocity-create search t)
  349. (org-velocity-read nil search)))
  350. ('follow (if (y-or-n-p "One match, follow? ")
  351. (progn
  352. (set-buffer (org-velocity-display-buffer))
  353. (goto-char (point-min))
  354. (button-activate (next-button (point))))
  355. (org-velocity-read nil search))))))
  356. (defun org-velocity-position (item list)
  357. "Return first position of ITEM in LIST."
  358. (loop for elt in list
  359. for i from 0
  360. if (equal elt item)
  361. return i))
  362. (defun org-velocity-activate-button (char)
  363. "Go to button on line number associated with CHAR in `org-velocity-index'."
  364. (goto-char (point-min))
  365. (forward-line (org-velocity-position char org-velocity-index))
  366. (goto-char
  367. (button-start
  368. (next-button (point))))
  369. (message "%s" (button-label (button-at (point))))
  370. (button-activate (button-at (point))))
  371. (defun org-velocity-electric-undefined ()
  372. "Complain about an undefined key."
  373. (interactive)
  374. (message "%s"
  375. (substitute-command-keys
  376. "\\[org-velocity-electric-new] for new entry, \\[org-velocity-electric-edit] to edit search, \\[scroll-up] to scroll."))
  377. (sit-for 4))
  378. (defun org-velocity-electric-follow (ev)
  379. "Follow a hint indexed by keyboard event EV."
  380. (interactive (list last-command-event))
  381. (if (not (> (org-velocity-position ev org-velocity-index)
  382. (1- (count-lines (point-min) (point-max)))))
  383. (throw 'org-velocity-select ev)
  384. (call-interactively 'org-velocity-electric-undefined)))
  385. (defun org-velocity-electric-click (ev)
  386. "Follow hint indexed by a mouse event EV."
  387. (interactive "e")
  388. (throw 'org-velocity-select
  389. (nth (1- (count-lines
  390. (point-min)
  391. (posn-point (event-start ev))))
  392. org-velocity-index)))
  393. (defun org-velocity-electric-edit ()
  394. "Edit the search string."
  395. (interactive)
  396. (throw 'org-velocity-select 'edit))
  397. (defun org-velocity-electric-new ()
  398. "Force a new entry."
  399. (interactive)
  400. (throw 'org-velocity-select 'new))
  401. (defvar org-velocity-electric-map
  402. (let ((map (make-sparse-keymap)))
  403. (define-key map [t] 'org-velocity-electric-undefined) (loop for c in org-velocity-index
  404. do (define-key map (char-to-string c) 'org-velocity-electric-follow))
  405. (define-key map "0" 'org-velocity-electric-new)
  406. (define-key map [tab] 'scroll-up)
  407. (define-key map [return] 'org-velocity-electric-edit)
  408. (define-key map [mouse-1] 'org-velocity-electric-click)
  409. (define-key map [mouse-2] 'org-velocity-electric-click)
  410. (define-key map [escape escape escape] 'keyboard-quit)
  411. (define-key map "\C-h" 'help-command)
  412. map))
  413. (defun org-velocity-electric-follow-hint ()
  414. "Read index of button electrically."
  415. (with-current-buffer (org-velocity-display-buffer)
  416. (use-local-map org-velocity-electric-map)
  417. (catch 'org-velocity-select
  418. (Electric-command-loop 'org-velocity-select
  419. "Follow: "))))
  420. (defun org-velocity-read-with-completion (prompt)
  421. "Like `completing-read' on entries with PROMPT.
  422. Use `minibuffer-local-filename-completion-map'."
  423. (let ((minibuffer-local-completion-map
  424. minibuffer-local-filename-completion-map))
  425. (completing-read
  426. prompt
  427. (mapcar 'substring-no-properties
  428. (org-map-entries 'org-get-heading)))))
  429. (defun org-velocity-read-string (prompt &optional initial-input)
  430. "Read string with PROMPT followed by INITIAL-INPUT."
  431. ;; The use of initial inputs to the minibuffer is deprecated (see
  432. ;; `read-from-minibuffer'), but in this case it is the user-friendly
  433. ;; thing to do.
  434. (minibuffer-with-setup-hook
  435. (lexical-let ((initial-input initial-input))
  436. (lambda ()
  437. (and initial-input (insert initial-input))
  438. (goto-char (point-max))))
  439. (if (and org-velocity-use-completion
  440. ;; map-entries complains for nonexistent files
  441. (file-exists-p (org-velocity-use-file)))
  442. (org-velocity-read-with-completion prompt)
  443. (read-string prompt))))
  444. (defun org-velocity-read (arg &optional search)
  445. "Read a search string SEARCH for Org-Velocity interface.
  446. This means that a buffer will display all headings where SEARCH
  447. occurs, where one can be selected by a mouse click or by typing
  448. its index. If SEARCH does not occur, then a new heading may be
  449. created named SEARCH.
  450. If `org-velocity-bucket' is defined and
  451. `org-velocity-always-use-bucket' is non-nil, then the bucket file
  452. will be used; otherwise, this will work when called in any Org
  453. file. Calling with ARG forces current file."
  454. (interactive "P")
  455. (let ((org-velocity-always-use-bucket
  456. (if arg nil org-velocity-always-use-bucket)))
  457. ;; complain if inappropriate
  458. (assert (org-velocity-use-file))
  459. (unwind-protect
  460. (org-velocity-engine
  461. (org-velocity-read-string "Velocity search: " search))
  462. (progn
  463. (kill-buffer (org-velocity-display-buffer))
  464. (delete-other-windows)))))
  465. (provide 'org-velocity)
  466. ;;; org-velocity.el ends here