org-velocity.el 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. ;;; org-velocity.el --- something like Notational Velocity for Org. -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2010-2014 Paul M. Rodriguez
  3. ;; Author: Paul M. Rodriguez <paulmrodriguez@gmail.com>
  4. ;; Created: 2010-05-05
  5. ;; Version: 4.0
  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. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Org-Velocity.el is an interface for Org inspired by the minimalist
  18. ;; notetaking program Notational Velocity. The idea is to let you
  19. ;; amass and access brief notes on many subjects with minimal fuss.
  20. ;; Each note is an entry in an ordinary Org file.
  21. ;; Org-Velocity can be used in two ways: when called outside Org, to
  22. ;; store and access notes in a designated bucket file; or, when called
  23. ;; inside Org, as a method for navigating any Org file. (Setting the
  24. ;; option `org-velocity-always-use-bucket' disables navigation inside
  25. ;; Org files by default, although you can still force this behavior by
  26. ;; calling `org-velocity-read' with an argument.)
  27. ;; Org-Velocity prompts for search terms in the minibuffer. A list of
  28. ;; headings of entries whose text matches your search is updated as
  29. ;; you type; you can end the search and visit an entry at any time by
  30. ;; clicking on its heading.
  31. ;; RET displays the results. If there are no matches, Org-Velocity
  32. ;; offers to create a new entry with your search string as its
  33. ;; heading. If there are matches, it displays a list of results where
  34. ;; the heading of each matching entry is hinted with a number or
  35. ;; letter; clicking a result, or typing the matching hint, opens the
  36. ;; entry for editing in an indirect buffer. 0 forces a new entry; RET
  37. ;; reopens the search for editing.
  38. ;; You can customize every step in this process, including the search
  39. ;; method, completion for search terms, and templates for creating new
  40. ;; entries; M-x customize-group RET org-velocity RET to see all the
  41. ;; options.
  42. ;; Thanks to Richard Riley, Carsten Dominik, Bastien Guerry, and Jeff
  43. ;; Horn for their suggestions.
  44. ;;; Usage:
  45. ;; (require 'org-velocity)
  46. ;; (setq org-velocity-bucket (expand-file-name "bucket.org" org-directory))
  47. ;; (global-set-key (kbd "C-c v") 'org-velocity)
  48. ;;; Code:
  49. (require 'org)
  50. (require 'button)
  51. (require 'electric)
  52. (require 'dabbrev)
  53. (require 'cl-lib)
  54. (defgroup org-velocity nil
  55. "Notational Velocity-style interface for Org."
  56. :tag "Org-Velocity"
  57. :group 'outlines
  58. :group 'hypermedia
  59. :group 'org)
  60. (defcustom org-velocity-bucket ""
  61. "Where is the bucket file?"
  62. :group 'org-velocity
  63. :type 'file)
  64. (defcustom org-velocity-search-is-incremental t
  65. "Show results incrementally when possible?"
  66. :group 'org-velocity
  67. :type 'boolean
  68. :safe 'booleanp)
  69. (defcustom org-velocity-show-previews t
  70. "Show previews of the text of each heading?"
  71. :group 'velocity
  72. :type 'boolean
  73. :safe 'booleanp)
  74. (defcustom org-velocity-exit-on-match nil
  75. "When searching incrementally, exit on a single match?"
  76. :group 'org-velocity
  77. :type 'boolean
  78. :safe 'booleanp)
  79. (defcustom org-velocity-force-new nil
  80. "Should exiting the minibuffer with C-j force a new entry?"
  81. :group 'org-velocity
  82. :type 'boolean
  83. :safe 'booleanp)
  84. (defcustom org-velocity-use-search-ring t
  85. "Push search to `search-ring' when visiting an entry?
  86. This means that C-s C-s will take you directly to the first
  87. instance of the search string."
  88. :group 'org-velocity
  89. :type 'boolean
  90. :safe 'booleanp)
  91. (defcustom org-velocity-always-use-bucket nil
  92. "Use bucket file even when called from an Org buffer?"
  93. :group 'org-velocity
  94. :type 'boolean
  95. :safe 'booleanp)
  96. (defcustom org-velocity-use-completion nil
  97. "Use completion?
  98. Notwithstanding the value of this option, calling
  99. `dabbrev-expand' always completes against the text of the bucket
  100. file."
  101. :group 'org-velocity
  102. :type '(choice
  103. (const :tag "Do not use completion" nil)
  104. (const :tag "Use completion" t))
  105. :safe 'booleanp)
  106. (defcustom org-velocity-search-method 'phrase
  107. "Match on whole phrase, any word, or all words?"
  108. :group 'org-velocity
  109. :type '(choice
  110. (const :tag "Match whole phrase" phrase)
  111. (const :tag "Match any word" any)
  112. (const :tag "Match all words" all)
  113. (const :tag "Match a regular expression" regexp))
  114. :safe (lambda (v) (memq v '(phrase any all regexp))))
  115. (defcustom org-velocity-capture-templates
  116. '(("v"
  117. "Velocity entry"
  118. entry
  119. (file "")
  120. "* %:search\n\n%i%?"))
  121. "Use these template with `org-capture'.
  122. Meanwhile `org-default-notes-file' is bound to `org-velocity-bucket-file'.
  123. The keyword :search inserts the current search.
  124. See the documentation for `org-capture-templates'."
  125. :group 'org-velocity
  126. :type (or (get 'org-capture-templates 'custom-type) 'list))
  127. (defcustom org-velocity-heading-level 1
  128. "Only match headings at this level or higher.
  129. 0 means to match headings at any level."
  130. :group 'org-velocity
  131. :type 'integer
  132. :safe (lambda (x)
  133. (and (integerp x)
  134. (>= x 0))))
  135. (defvar crm-separator) ;Ensure dynamic binding.
  136. (defsubst org-velocity-grab-preview ()
  137. "Grab preview of a subtree.
  138. The length of the preview is determined by `window-width'.
  139. Replace all contiguous whitespace with single spaces."
  140. (let ((start (progn
  141. (forward-line 1)
  142. (if (looking-at org-property-start-re)
  143. (re-search-forward org-property-end-re)
  144. (1- (point))))))
  145. (mapconcat
  146. #'identity
  147. (split-string
  148. (buffer-substring-no-properties
  149. start
  150. (min
  151. (+ start (window-width))
  152. (point-max))))
  153. " ")))
  154. (cl-defstruct org-velocity-heading buffer position name level preview)
  155. (defsubst org-velocity-nearest-heading (position)
  156. "Return last heading at POSITION.
  157. If there is no last heading, return nil."
  158. (save-excursion
  159. (goto-char position)
  160. (re-search-backward (org-velocity-heading-regexp))
  161. (let ((components (org-heading-components)))
  162. (make-org-velocity-heading
  163. :buffer (current-buffer)
  164. :position (point)
  165. :name (nth 4 components)
  166. :level (nth 0 components)
  167. :preview (if org-velocity-show-previews
  168. (org-velocity-grab-preview))))))
  169. (defconst org-velocity-index
  170. (eval-when-compile
  171. (nconc (number-sequence 49 57) ;numbers
  172. (number-sequence 97 122) ;lowercase letters
  173. (number-sequence 65 90))) ;uppercase letters
  174. "List of chars for indexing results.")
  175. (defconst org-velocity-match-buffer-name "*Velocity matches*")
  176. (cl-defun org-velocity-heading-regexp (&optional (level org-velocity-heading-level))
  177. "Regexp to match headings at LEVEL or deeper."
  178. (if (zerop level)
  179. "^\\*+ "
  180. (format "^\\*\\{1,%d\\} " level)))
  181. (defvar org-velocity-search nil
  182. "Variable to bind to current search.")
  183. (defun org-velocity-buffer-file-name (&optional buffer)
  184. "Return the name of the file BUFFER saves to.
  185. Same as function `buffer-file-name' unless BUFFER is an indirect
  186. buffer or a minibuffer. In the former case, return the file name
  187. of the base buffer; in the latter, return the file name of
  188. `minibuffer-selected-window' (or its base buffer)."
  189. (let ((buffer (if (minibufferp buffer)
  190. (window-buffer (minibuffer-selected-window))
  191. buffer)))
  192. (buffer-file-name
  193. (or (buffer-base-buffer buffer)
  194. buffer))))
  195. (defun org-velocity-minibuffer-contents ()
  196. "Return the contents of the minibuffer when it is active."
  197. (if (active-minibuffer-window)
  198. (with-current-buffer (window-buffer (active-minibuffer-window))
  199. (minibuffer-contents))))
  200. (defun org-velocity-bucket-file ()
  201. "Return the proper file for Org-Velocity to search.
  202. If `org-velocity-always-use-bucket' is t, use bucket file;
  203. complain if missing. Otherwise, if an Org file is current, then
  204. use it."
  205. (let ((org-velocity-bucket
  206. (when org-velocity-bucket (expand-file-name org-velocity-bucket)))
  207. (buffer
  208. (let ((buffer-file (org-velocity-buffer-file-name)))
  209. (when buffer-file
  210. ;; Use the target in capture buffers.
  211. (org-find-base-buffer-visiting buffer-file)))))
  212. (if org-velocity-always-use-bucket
  213. (or org-velocity-bucket (error "Bucket required but not defined"))
  214. (if (and (eq (buffer-local-value 'major-mode (or buffer (current-buffer)))
  215. 'org-mode)
  216. (org-velocity-buffer-file-name))
  217. (org-velocity-buffer-file-name)
  218. (or org-velocity-bucket
  219. (error "No bucket and not an Org file"))))))
  220. (defvar org-velocity-bucket-buffer nil)
  221. (defsubst org-velocity-bucket-buffer ()
  222. (or org-velocity-bucket-buffer
  223. (find-file-noselect (org-velocity-bucket-file))))
  224. (defsubst org-velocity-match-buffer ()
  225. "Return the proper buffer for Org-Velocity to display in."
  226. (get-buffer-create org-velocity-match-buffer-name))
  227. (defsubst org-velocity-match-window ()
  228. (get-buffer-window (org-velocity-match-buffer)))
  229. (defsubst org-velocity-match-staging-buffer ()
  230. (get-buffer-create " Velocity matches"))
  231. (defun org-velocity-beginning-of-headings ()
  232. "Goto the start of the first heading."
  233. (goto-char (point-min))
  234. ;; If we are before the first heading we could still be at the
  235. ;; first heading.
  236. (or (looking-at (org-velocity-heading-regexp))
  237. (re-search-forward (org-velocity-heading-regexp))))
  238. (defun org-velocity-make-indirect-buffer (heading)
  239. "Make or switch to an indirect buffer visiting HEADING."
  240. (let* ((bucket (org-velocity-heading-buffer heading))
  241. (name (org-velocity-heading-name heading))
  242. (existing (get-buffer name)))
  243. (if (and existing (buffer-base-buffer existing)
  244. (equal (buffer-base-buffer existing) bucket))
  245. existing
  246. (make-indirect-buffer
  247. bucket
  248. (generate-new-buffer-name (org-velocity-heading-name heading))
  249. t))))
  250. (defun org-velocity-capture ()
  251. "Record a note with `org-capture'."
  252. (let ((org-capture-templates
  253. org-velocity-capture-templates))
  254. (org-capture nil
  255. ;; This is no longer automatically selected.
  256. (when (null (cdr org-capture-templates))
  257. (caar org-capture-templates)))
  258. (when org-capture-mode
  259. (rename-buffer org-velocity-search t))))
  260. (defvar org-velocity-saved-winconf nil)
  261. (make-variable-buffer-local 'org-velocity-saved-winconf)
  262. (defun org-velocity-edit-entry (heading)
  263. "Edit entry at HEADING in an indirect buffer."
  264. (let ((winconf (current-window-configuration))
  265. (buffer (org-velocity-make-indirect-buffer heading))
  266. (inhibit-point-motion-hooks t)
  267. (inhibit-field-text-motion t))
  268. (with-current-buffer buffer
  269. (setq org-velocity-saved-winconf winconf)
  270. (goto-char (org-velocity-heading-position heading))
  271. (let ((start (point))
  272. (end (save-excursion
  273. (org-end-of-subtree t)
  274. (point))))
  275. ;; Outline view and narrow-to-region interact poorly.
  276. (outline-flag-region start end nil)
  277. (narrow-to-region start end))
  278. (goto-char (point-max))
  279. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-velocity-dismiss nil t))
  280. (pop-to-buffer buffer)
  281. (set (make-local-variable 'header-line-format)
  282. (format "%s Use C-c C-c to finish."
  283. (abbreviate-file-name
  284. (buffer-file-name
  285. (org-velocity-heading-buffer heading)))))))
  286. (defun org-velocity-dismiss ()
  287. "Save current entry and close indirect buffer."
  288. (let ((winconf org-velocity-saved-winconf))
  289. (prog1 t ;Tell hook we're done.
  290. (save-buffer)
  291. (kill-buffer)
  292. (when (window-configuration-p winconf)
  293. (set-window-configuration winconf)))))
  294. (defun org-velocity-visit-button (button)
  295. (run-hooks 'mouse-leave-buffer-hook)
  296. (when org-velocity-use-search-ring
  297. (add-to-history 'search-ring
  298. (button-get button 'search)
  299. search-ring-max))
  300. (let ((match (button-get button 'match)))
  301. (throw 'org-velocity-done
  302. (lambda ()
  303. (org-velocity-edit-entry match)))))
  304. (define-button-type 'org-velocity-button
  305. 'action #'org-velocity-visit-button
  306. 'follow-link 'mouse-face)
  307. (defsubst org-velocity-buttonize (heading)
  308. "Insert HEADING as a text button with no hints."
  309. (insert-text-button
  310. (propertize (org-velocity-heading-name heading) 'face 'link)
  311. :type 'org-velocity-button
  312. 'match heading
  313. 'search org-velocity-search))
  314. (defsubst org-velocity-insert-preview (heading)
  315. (when org-velocity-show-previews
  316. (insert-char ?\ 1)
  317. (insert
  318. (propertize
  319. (org-velocity-heading-preview heading)
  320. 'face 'shadow))))
  321. (defsubst org-velocity-present-match (hint match)
  322. (with-current-buffer (org-velocity-match-staging-buffer)
  323. (when hint (insert "#" hint " "))
  324. (org-velocity-buttonize match)
  325. (org-velocity-insert-preview match)
  326. (newline)))
  327. (defun org-velocity-generic-search (search &optional hide-hints)
  328. "Display any entry containing SEARCH."
  329. (let ((hints org-velocity-index) matches)
  330. (cl-block nil
  331. (while (and hints (re-search-forward search nil t))
  332. (let ((match (org-velocity-nearest-heading (point))))
  333. (org-velocity-present-match
  334. (unless hide-hints (car hints))
  335. match)
  336. (push match matches))
  337. (setq hints (cdr hints))
  338. (unless (re-search-forward (org-velocity-heading-regexp) nil t)
  339. (return))))
  340. (nreverse matches)))
  341. (cl-defun org-velocity-all-search (search &optional hide-hints)
  342. "Display only entries containing every word in SEARCH."
  343. (let ((keywords (mapcar 'regexp-quote (split-string search)))
  344. (hints org-velocity-index)
  345. matches)
  346. (org-map-entries
  347. (lambda ()
  348. ;; Return if we've run out of hints.
  349. (when (null hints)
  350. (return-from org-velocity-all-search (nreverse matches)))
  351. ;; Only search the subtree once.
  352. (setq org-map-continue-from
  353. (save-excursion
  354. (goto-char (line-end-position))
  355. (if (re-search-forward (org-velocity-heading-regexp) nil t)
  356. (line-end-position)
  357. (point-max))))
  358. (when (cl-loop for word in keywords
  359. always (save-excursion
  360. (re-search-forward
  361. (concat "\\<" word "\\>")
  362. org-map-continue-from t)))
  363. (let ((match (org-velocity-nearest-heading (match-end 0))))
  364. (org-velocity-present-match
  365. (unless hide-hints (car hints))
  366. match)
  367. (push match matches)
  368. (setq hints (cdr hints))))))
  369. (nreverse matches)))
  370. (cl-defun org-velocity-present (search &key hide-hints)
  371. "Buttonize matches for SEARCH in `org-velocity-match-buffer'.
  372. If HIDE-HINTS is non-nil, display entries without indices. SEARCH
  373. binds `org-velocity-search'.
  374. Return matches."
  375. (if (and (stringp search) (not (string= "" search)))
  376. ;; Fold case when the search string is all lowercase.
  377. (let ((case-fold-search (equal search (downcase search)))
  378. (truncate-partial-width-windows t))
  379. (with-current-buffer (org-velocity-match-buffer)
  380. (erase-buffer)
  381. ;; Permanent locals.
  382. (setq cursor-type nil
  383. truncate-lines t))
  384. (prog1
  385. (with-current-buffer (org-velocity-bucket-buffer)
  386. (let ((inhibit-point-motion-hooks t)
  387. (inhibit-field-text-motion t))
  388. (save-excursion
  389. (org-velocity-beginning-of-headings)
  390. (cl-case org-velocity-search-method
  391. (all (org-velocity-all-search search hide-hints))
  392. (phrase (org-velocity-generic-search
  393. (concat "\\<" (regexp-quote search))
  394. hide-hints))
  395. (any (org-velocity-generic-search
  396. (concat "\\<"
  397. (regexp-opt (split-string search)))
  398. hide-hints))
  399. (regexp (condition-case lossage
  400. (org-velocity-generic-search
  401. search hide-hints)
  402. (invalid-regexp
  403. (minibuffer-message "%s" lossage))))))))
  404. (with-current-buffer (org-velocity-match-buffer)
  405. (buffer-swap-text (org-velocity-match-staging-buffer))
  406. (goto-char (point-min)))))
  407. (with-current-buffer (org-velocity-match-buffer)
  408. (erase-buffer))))
  409. (defun org-velocity-store-link ()
  410. "Function for `org-store-link-functions'."
  411. (if org-velocity-search
  412. (org-store-link-props
  413. :search org-velocity-search)))
  414. (add-hook 'org-store-link-functions 'org-velocity-store-link)
  415. (cl-defun org-velocity-create (search &key ask)
  416. "Create new heading named SEARCH.
  417. If ASK is non-nil, ask first."
  418. (when (or (null ask) (y-or-n-p "No match found, create? "))
  419. (let ((org-velocity-search search)
  420. (org-default-notes-file (org-velocity-bucket-file))
  421. ;; save a stored link
  422. org-store-link-plist)
  423. (org-velocity-capture))
  424. search))
  425. (defun org-velocity-engine (search)
  426. "Display a list of headings where SEARCH occurs."
  427. (let ((org-velocity-search search))
  428. (unless (or
  429. (not (stringp search))
  430. (string= "" search)) ;exit on empty string
  431. (cl-case
  432. (if (and org-velocity-force-new (eq last-command-event ?\C-j))
  433. :force
  434. (let* ((org-velocity-index (org-velocity-adjust-index))
  435. (matches (org-velocity-present search)))
  436. (cond ((null matches) :new)
  437. ((null (cdr matches)) :follow)
  438. (t :prompt))))
  439. (:prompt (progn
  440. (pop-to-buffer (org-velocity-match-buffer))
  441. (let ((hint (org-velocity-electric-read-hint)))
  442. (when hint (cl-case hint
  443. (:edit (org-velocity-read nil search))
  444. (:force (org-velocity-create search))
  445. (otherwise (org-velocity-activate-button hint)))))))
  446. (:new (unless (org-velocity-create search :ask t)
  447. (org-velocity-read nil search)))
  448. (:force (org-velocity-create search))
  449. (:follow (if (y-or-n-p "One match, follow? ")
  450. (progn
  451. (set-buffer (org-velocity-match-buffer))
  452. (goto-char (point-min))
  453. (button-activate (next-button (point))))
  454. (org-velocity-read nil search)))))))
  455. (defun org-velocity-activate-button (char)
  456. "Go to button on line number associated with CHAR in `org-velocity-index'."
  457. (goto-char (point-min))
  458. (forward-line (cl-position char org-velocity-index))
  459. (goto-char
  460. (button-start
  461. (next-button (point))))
  462. (message "%s" (button-label (button-at (point))))
  463. (button-activate (button-at (point))))
  464. (defun org-velocity-electric-undefined ()
  465. "Complain about an undefined key."
  466. (interactive)
  467. (message "%s"
  468. (substitute-command-keys
  469. "\\[org-velocity-electric-new] for new entry,
  470. \\[org-velocity-electric-edit] to edit search,
  471. \\[scroll-up] to scroll up,
  472. \\[scroll-down] to scroll down,
  473. \\[keyboard-quit] to quit."))
  474. (sit-for 4))
  475. (defun org-velocity-electric-follow (ev)
  476. "Follow a hint indexed by keyboard event EV."
  477. (interactive (list last-command-event))
  478. (if (not (> (cl-position ev org-velocity-index)
  479. (1- (count-lines (point-min) (point-max)))))
  480. (throw 'org-velocity-select ev)
  481. (call-interactively 'org-velocity-electric-undefined)))
  482. (defun org-velocity-electric-edit ()
  483. "Edit the search string."
  484. (interactive)
  485. (throw 'org-velocity-select :edit))
  486. (defun org-velocity-electric-new ()
  487. "Force a new entry."
  488. (interactive)
  489. (throw 'org-velocity-select :force))
  490. (defvar org-velocity-electric-map
  491. (let ((map (make-sparse-keymap)))
  492. (define-key map [t] 'org-velocity-electric-undefined)
  493. (dolist (c org-velocity-index)
  494. (define-key map (char-to-string c)
  495. 'org-velocity-electric-follow))
  496. (define-key map "0" 'org-velocity-electric-new)
  497. (define-key map "\C-v" 'scroll-up)
  498. (define-key map "\M-v" 'scroll-down)
  499. (define-key map (kbd "RET") 'org-velocity-electric-edit)
  500. (define-key map [mouse-1] nil)
  501. (define-key map [mouse-2] nil)
  502. (define-key map [escape] 'keyboard-quit)
  503. (define-key map "\C-h" 'help-command)
  504. map))
  505. (defun org-velocity-electric-read-hint ()
  506. "Read index of button electrically."
  507. (with-current-buffer (org-velocity-match-buffer)
  508. (when (featurep 'evil)
  509. ;; NB Idempotent.
  510. (evil-make-overriding-map org-velocity-electric-map))
  511. (use-local-map org-velocity-electric-map)
  512. (catch 'org-velocity-select
  513. (Electric-command-loop 'org-velocity-select "Follow: "))))
  514. (defvar org-velocity-incremental-keymap
  515. (let ((map (make-sparse-keymap)))
  516. (define-key map "\C-v" 'scroll-up)
  517. (define-key map "\M-v" 'scroll-down)
  518. map))
  519. (defun org-velocity-displaying-completions-p ()
  520. "Is there a *Completions* buffer showing?"
  521. (get-window-with-predicate
  522. (lambda (w)
  523. (eq (buffer-local-value 'major-mode (window-buffer w))
  524. 'completion-list-mode))))
  525. (defun org-velocity-update ()
  526. "Display results of search without hinting."
  527. (unless (org-velocity-displaying-completions-p)
  528. (let* ((search (org-velocity-minibuffer-contents))
  529. (matches (org-velocity-present search :hide-hints t)))
  530. (cond ((null matches)
  531. (select-window (active-minibuffer-window))
  532. (unless (or (null search) (string= "" search))
  533. (minibuffer-message "No match; RET to create")))
  534. ((and (null (cdr matches))
  535. org-velocity-exit-on-match)
  536. (throw 'click search))
  537. (t
  538. (with-current-buffer (org-velocity-match-buffer)
  539. (use-local-map org-velocity-incremental-keymap)))))))
  540. (defvar dabbrev--last-abbreviation)
  541. (defun org-velocity-dabbrev-completion-list (abbrev)
  542. "Return all dabbrev completions for ABBREV."
  543. ;; This is based on `dabbrev-completion'.
  544. (dabbrev--reset-global-variables)
  545. (setq dabbrev--last-abbreviation abbrev)
  546. (dabbrev--find-all-expansions abbrev case-fold-search))
  547. (defvar org-velocity-local-completion-map
  548. (let ((map (make-sparse-keymap)))
  549. (set-keymap-parent map minibuffer-local-completion-map)
  550. (define-key map " " 'self-insert-command)
  551. (define-key map [remap minibuffer-complete] 'minibuffer-complete-word)
  552. map)
  553. "Keymap for completion with `completing-read'.")
  554. (defun org-velocity-read-with-completion (prompt)
  555. "Completing read with PROMPT."
  556. (let ((minibuffer-local-completion-map
  557. org-velocity-local-completion-map)
  558. (completion-no-auto-exit t)
  559. (crm-separator " "))
  560. (funcall
  561. (cl-case org-velocity-search-method
  562. (phrase #'completing-read)
  563. (any #'completing-read-multiple)
  564. (all #'completing-read-multiple))
  565. prompt
  566. (completion-table-dynamic
  567. 'org-velocity-dabbrev-completion-list))))
  568. (defun org-velocity-read-string (prompt &optional initial-input)
  569. "Read string with PROMPT followed by INITIAL-INPUT."
  570. ;; The use of initial inputs to the minibuffer is deprecated (see
  571. ;; `read-from-minibuffer'), but in this case it is the user-friendly
  572. ;; thing to do.
  573. (minibuffer-with-setup-hook
  574. (let ((initial-input initial-input))
  575. (lambda ()
  576. (and initial-input (insert initial-input))
  577. (goto-char (point-max))))
  578. (if (eq org-velocity-search-method 'regexp)
  579. (read-regexp prompt)
  580. (if org-velocity-use-completion
  581. (org-velocity-read-with-completion prompt)
  582. (read-string prompt)))))
  583. (cl-defun org-velocity-adjust-index
  584. (&optional (match-window (org-velocity-match-window)))
  585. "Truncate or extend `org-velocity-index' to the lines in
  586. MATCH-WINDOW."
  587. (with-selected-window match-window
  588. (let ((lines (window-height))
  589. (hints (length org-velocity-index)))
  590. (cond ((= lines hints)
  591. org-velocity-index)
  592. ;; Truncate the index to the size of
  593. ;; the buffer to be displayed.
  594. ((< lines hints)
  595. (cl-subseq org-velocity-index 0 lines))
  596. ;; If the window is so tall we run out of indices, at
  597. ;; least make the additional results clickable.
  598. ((> lines hints)
  599. (append org-velocity-index
  600. (make-list (- lines hints) nil)))))))
  601. (defun org-velocity-incremental-read (prompt)
  602. "Read string with PROMPT and display results incrementally.
  603. Stop searching once there are more matches than can be
  604. displayed."
  605. (let ((res
  606. (unwind-protect
  607. (let* ((match-window (display-buffer (org-velocity-match-buffer)))
  608. (org-velocity-index (org-velocity-adjust-index match-window)))
  609. (catch 'click
  610. (add-hook 'post-command-hook 'org-velocity-update)
  611. (cond ((eq org-velocity-search-method 'regexp)
  612. (read-regexp prompt))
  613. (org-velocity-use-completion
  614. (org-velocity-read-with-completion prompt))
  615. (t (read-string prompt)))))
  616. (remove-hook 'post-command-hook 'org-velocity-update))))
  617. (if (bufferp res) (org-pop-to-buffer-same-window res) res)))
  618. (defun org-velocity (arg &optional search)
  619. "Read a search string SEARCH for Org-Velocity interface.
  620. This means that a buffer will display all headings where SEARCH
  621. occurs, where one can be selected by a mouse click or by typing
  622. its index. If SEARCH does not occur, then a new heading may be
  623. created named SEARCH.
  624. If `org-velocity-bucket' is defined and
  625. `org-velocity-always-use-bucket' is non-nil, then the bucket file
  626. will be used; otherwise, this will work when called in any Org
  627. file.
  628. Calling with ARG reverses which file – the current file or the
  629. bucket file – to use. If the bucket file would have been used,
  630. then the current file is used instead, and vice versa."
  631. (interactive "P")
  632. (let ((org-velocity-always-use-bucket
  633. (if org-velocity-always-use-bucket
  634. (not arg)
  635. arg)))
  636. ;; complain if inappropriate
  637. (cl-assert (org-velocity-bucket-file))
  638. (let ((org-velocity-bucket-buffer
  639. (find-file-noselect (org-velocity-bucket-file))))
  640. (unwind-protect
  641. (let ((dabbrev-search-these-buffers-only
  642. (list (org-velocity-bucket-buffer))))
  643. (funcall
  644. (catch 'org-velocity-done
  645. (org-velocity-engine
  646. (if org-velocity-search-is-incremental
  647. (org-velocity-incremental-read "Velocity search: ")
  648. (org-velocity-read-string "Velocity search: " search)))
  649. #'ignore)))
  650. (kill-buffer (org-velocity-match-buffer))))))
  651. (defalias 'org-velocity-read 'org-velocity)
  652. (provide 'org-velocity)
  653. ;;; org-velocity.el ends here