org-velocity.el 27 KB

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