Emacs config utilizing prelude as a base
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4427 lines
175 KiB

  1. ;;; anything.el --- open anything / QuickSilver-like candidate-selection framework
  2. ;; Copyright (C) 2007 Tamas Patrovics
  3. ;; 2008 ~ 2012 rubikitch <rubikitch@ruby-lang.org>
  4. ;; 2011 ~ 2012 Thierry Volpiatto <thierry.volpiatto@gmail.com>
  5. ;; Author: Tamas Patrovics
  6. ;; Maintainers: rubikitch <rubikitch@ruby-lang.org>
  7. ;; Thierry Volpiatto <thierry.volpiatto@gmail.com>
  8. ;; Keywords: files, frames, help, matching, outlines,
  9. ;; processes, tools, convenience, anything
  10. ;; X-URL: <http://repo.or.cz/w/anything-config.git>
  11. ;; Site: <http://www.emacswiki.org/cgi-bin/emacs/Anything>
  12. ;; MailingList: <https://groups.google.com/group/emacs-anything?hl=en>
  13. ;;; This file is NOT part of GNU Emacs
  14. ;;; License
  15. ;;
  16. ;; This file is free software; you can redistribute it and/or modify
  17. ;; it under the terms of the GNU General Public License as published by
  18. ;; the Free Software Foundation; either version 2, or (at your option)
  19. ;; any later version.
  20. ;; This file is distributed in the hope that it will be useful,
  21. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. ;; GNU General Public License for more details.
  24. ;; You should have received a copy of the GNU General Public License
  25. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  26. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  27. ;; Boston, MA 02110-1301, USA.
  28. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  29. ;;
  30. ;;; Autodoc documentation:
  31. ;; ---------------------
  32. ;; * Commands defined here are:
  33. ;; [EVAL] (autodoc-document-lisp-buffer :type 'command :prefix "anything" :docstring t)
  34. ;; `anything-open-last-log'
  35. ;; Open anything log file of last anything session.
  36. ;; `anything'
  37. ;; Main function to execute anything sources.
  38. ;; `anything-resume'
  39. ;; Resurrect previously invoked `anything'.
  40. ;; `anything-resume-window-only'
  41. ;;
  42. ;; `anything-at-point'
  43. ;; Call anything with symbol at point as initial input.
  44. ;; `anything-force-update'
  45. ;; Force recalculation and update of candidates.
  46. ;; `anything-select-action'
  47. ;; Select an action for the currently selected candidate.
  48. ;; `anything-previous-line'
  49. ;; Move selection to the previous line.
  50. ;; `anything-next-line'
  51. ;; Move selection to the next line.
  52. ;; `anything-previous-page'
  53. ;; Move selection back with a pageful.
  54. ;; `anything-next-page'
  55. ;; Move selection forward with a pageful.
  56. ;; `anything-beginning-of-buffer'
  57. ;; Move selection at the top.
  58. ;; `anything-end-of-buffer'
  59. ;; Move selection at the bottom.
  60. ;; `anything-previous-source'
  61. ;; Move selection to the previous source.
  62. ;; `anything-next-source'
  63. ;; Move selection to the next source.
  64. ;; `anything-select-with-prefix-shortcut'
  65. ;; Invoke default action with prefix shortcut.
  66. ;; `anything-select-with-digit-shortcut'
  67. ;; Invoke default action with digit/alphabet shortcut.
  68. ;; `anything-confirm-and-exit-minibuffer'
  69. ;; Maybe ask for confirmation when exiting anything.
  70. ;; `anything-exit-minibuffer'
  71. ;; Select the current candidate by exiting the minibuffer.
  72. ;; `anything-keyboard-quit'
  73. ;; Quit minibuffer in anything.
  74. ;; `anything-help'
  75. ;; Help of `anything'.
  76. ;; `anything-debug-output'
  77. ;; Show all anything-related variables at this time.
  78. ;; `anything-delete-current-selection'
  79. ;; Delete the currently selected item.
  80. ;; `anything-delete-minibuffer-contents'
  81. ;; Same as `delete-minibuffer-contents' but this is a command.
  82. ;; `anything-toggle-resplit-window'
  83. ;; Toggle resplit anything window, vertically or horizontally.
  84. ;; `anything-narrow-window'
  85. ;; Narrow anything window.
  86. ;; `anything-enlarge-window'
  87. ;; Enlarge anything window.
  88. ;; `anything-select-2nd-action'
  89. ;; Select the 2nd action for the currently selected candidate.
  90. ;; `anything-select-3rd-action'
  91. ;; Select the 3rd action for the currently selected candidate.
  92. ;; `anything-select-4th-action'
  93. ;; Select the 4th action for the currently selected candidate.
  94. ;; `anything-select-2nd-action-or-end-of-line'
  95. ;; Select the 2nd action for the currently selected candidate.
  96. ;; `anything-execute-persistent-action'
  97. ;; Perform the associated action ATTR without quitting anything.
  98. ;; `anything-scroll-other-window'
  99. ;; Scroll other window (not *Anything* window) upward.
  100. ;; `anything-scroll-other-window-down'
  101. ;; Scroll other window (not *Anything* window) downward.
  102. ;; `anything-toggle-visible-mark'
  103. ;; Toggle anything visible mark at point.
  104. ;; `anything-display-all-visible-marks'
  105. ;; Show all `anything' visible marks strings.
  106. ;; `anything-next-visible-mark'
  107. ;; Move next anything visible mark.
  108. ;; `anything-prev-visible-mark'
  109. ;; Move previous anything visible mark.
  110. ;; `anything-yank-selection'
  111. ;; Set minibuffer contents to current selection.
  112. ;; `anything-kill-selection-and-quit'
  113. ;; Store current selection to kill ring.
  114. ;; `anything-follow-mode'
  115. ;; If this mode is on, persistent action is executed everytime the cursor is moved.
  116. ;; `anything-migrate-sources'
  117. ;; Help to migrate to new `anything' way.
  118. ;; `anything-describe-anything-attribute'
  119. ;; Display the full documentation of ANYTHING-ATTRIBUTE.
  120. ;; `anything-send-bug-report'
  121. ;; Send a bug report of anything.el.
  122. ;; `anything-send-bug-report-from-anything'
  123. ;; Send a bug report of anything.el in anything session.
  124. ;; * Variables defined here are:
  125. ;; [EVAL] (autodoc-document-lisp-buffer :type 'internal-variable :prefix "anything-" :docstring t)
  126. ;; `anything-version'
  127. ;; Not documented.
  128. ;; `anything-sources'
  129. ;; A list of sources to use with `anything'.
  130. ;; `anything-type-attributes'
  131. ;; It's a list of (TYPE ATTRIBUTES ...).
  132. ;; `anything-enable-shortcuts'
  133. ;; *Whether to use digit/alphabet shortcut to select the first nine matches.
  134. ;; `anything-shortcut-keys-alist'
  135. ;; Not documented.
  136. ;; `anything-display-source-at-screen-top'
  137. ;; *Display candidates at the top of screen.
  138. ;; `anything-candidate-number-limit'
  139. ;; Apply candidate-number-limit attribute value.
  140. ;; `anything-idle-delay'
  141. ;; *Be idle for this many seconds, before updating in delayed sources.
  142. ;; `anything-input-idle-delay'
  143. ;; Be idle for this many seconds, before updating.
  144. ;; `anything-samewindow'
  145. ;; Use current window to show the candidates.
  146. ;; `anything-source-filter'
  147. ;; A list of source names to be displayed.
  148. ;; `anything-map'
  149. ;; Keymap for anything.
  150. ;; `anything-header-face'
  151. ;; *Face for header lines in the anything buffer.
  152. ;; `anything-selection-face'
  153. ;; *Face for currently selected item.
  154. ;; `anything-buffer'
  155. ;; Buffer showing completions.
  156. ;; `anything-action-buffer'
  157. ;; Buffer showing actions.
  158. ;; `anything-selection-overlay'
  159. ;; Overlay used to highlight the currently selected item.
  160. ;; `anything-digit-overlays'
  161. ;; Overlays for digit shortcuts. See `anything-enable-shortcuts'.
  162. ;; `anything-candidate-cache'
  163. ;; Holds the available candidate withing a single anything invocation.
  164. ;; `anything-pattern'
  165. ;; Not documented.
  166. ;; `anything-input'
  167. ;; Not documented.
  168. ;; `anything-async-processes'
  169. ;; List of information about asynchronous processes managed by anything.
  170. ;; `anything-digit-shortcut-count'
  171. ;; Number of digit shortcuts shown in the anything buffer.
  172. ;; `anything-before-initialize-hook'
  173. ;; Run before anything initialization.
  174. ;; `anything-after-initialize-hook'
  175. ;; Run after anything initialization.
  176. ;; `anything-update-hook'
  177. ;; Run after the anything buffer was updated according the new input pattern.
  178. ;; `anything-after-update-hook'
  179. ;; Run after the anything buffer was updated according the new input pattern.
  180. ;; `anything-cleanup-hook'
  181. ;; Run after anything minibuffer is closed.
  182. ;; `anything-select-action-hook'
  183. ;; Run when opening the action buffer.
  184. ;; `anything-before-action-hook'
  185. ;; Run before executing action.
  186. ;; `anything-after-action-hook'
  187. ;; Run after executing action.
  188. ;; `anything-after-persistent-action-hook'
  189. ;; Run after executing persistent action.
  190. ;; `anything-move-selection-before-hook'
  191. ;; Run before moving selection in `anything-buffer'.
  192. ;; `anything-move-selection-after-hook'
  193. ;; Run after moving selection in `anything-buffer'.
  194. ;; `anything-restored-variables'
  195. ;; Variables which are restored after `anything' invocation.
  196. ;; `anything-saved-selection'
  197. ;; Value of the currently selected object when the action list is shown.
  198. ;; `anything-current-prefix-arg'
  199. ;; Record `current-prefix-arg' when exiting minibuffer.
  200. ;; `anything-candidate-separator'
  201. ;; Candidates separator of `multiline' source.
  202. ;; `anything-current-buffer'
  203. ;; Current buffer when `anything' is invoked.
  204. ;; `anything-buffer-file-name'
  205. ;; Variable `buffer-file-name' when `anything' is invoked.
  206. ;; `anything-saved-action'
  207. ;; Saved value of the currently selected action by key.
  208. ;; `anything-last-sources'
  209. ;; OBSOLETE!! Sources of previously invoked `anything'.
  210. ;; `anything-saved-current-source'
  211. ;; Value of the current source when the action list is shown.
  212. ;; `anything-compiled-sources'
  213. ;; Compiled version of `anything-sources'.
  214. ;; `anything-in-persistent-action'
  215. ;; Flag whether in persistent-action or not.
  216. ;; `anything-quick-update'
  217. ;; If non-nil, suppress displaying sources which are out of screen at first.
  218. ;; `anything-last-sources-local'
  219. ;; Buffer local value of `anything-sources'.
  220. ;; `anything-last-buffer'
  221. ;; `anything-buffer' of previously `anything' session.
  222. ;; `anything-save-configuration-functions'
  223. ;; The functions used to restore/save window or frame configurations.
  224. ;; `anything-persistent-action-use-special-display'
  225. ;; If non-nil, use `special-display-function' in persistent action.
  226. ;; `anything-execute-action-at-once-if-one'
  227. ;; Execute default action and exit when only one candidate is remaining.
  228. ;; `anything-quit-if-no-candidate'
  229. ;; Quit when there is no candidates when non--nil.
  230. ;; `anything-scroll-amount'
  231. ;; Scroll amount when scrolling other window in an anything session.
  232. ;; `anything-display-function'
  233. ;; Function to display *anything* buffer.
  234. ;; `anything-delayed-init-executed'
  235. ;; Not documented.
  236. ;; `anything-mode-line-string'
  237. ;; Help string displayed in mode-line in `anything'.
  238. ;; `anything-help-message'
  239. ;; Detailed help message string for `anything'.
  240. ;; `anything-source-in-each-line-flag'
  241. ;; Non-nil means add anything-source text-property in each candidate.
  242. ;; `anything-debug-forms'
  243. ;; Forms to show in `anything-debug-output'.
  244. ;; `anything-debug'
  245. ;; If non-nil, write log message into *Anything Log* buffer.
  246. ;; `anything-test-candidate-list'
  247. ;; Not documented.
  248. ;; `anything-test-mode'
  249. ;; Not documented.
  250. ;; `anything-source-name'
  251. ;; Not documented.
  252. ;; `anything-candidate-buffer-alist'
  253. ;; Not documented.
  254. ;; `anything-check-minibuffer-input-timer'
  255. ;; Not documented.
  256. ;; `anything-match-hash'
  257. ;; Not documented.
  258. ;; `anything-cib-hash'
  259. ;; Not documented.
  260. ;; `anything-tick-hash'
  261. ;; Not documented.
  262. ;; `anything-issued-errors'
  263. ;; Not documented.
  264. ;; `anything-shortcut-keys'
  265. ;; Not documented.
  266. ;; `anything-once-called-functions'
  267. ;; Not documented.
  268. ;; `anything-follow-mode'
  269. ;; If this mode is on, persistent action is executed everytime the cursor is moved.
  270. ;; `anything-let-variables'
  271. ;; Not documented.
  272. ;; `anything-split-window-state'
  273. ;; Not documented.
  274. ;; `anything-selection-point'
  275. ;; Not documented.
  276. ;; `anything-last-log-file'
  277. ;; Not documented.
  278. ;; `anything-compile-source-functions'
  279. ;; Functions to compile elements of `anything-sources' (plug-in).
  280. ;; `anything-quit'
  281. ;; Not documented.
  282. ;; `anything-additional-attributes'
  283. ;; List of all `anything' attributes.
  284. ;; `anything-buffers'
  285. ;; All of `anything-buffer' in most recently used order.
  286. ;; `anything-current-position'
  287. ;; Restore or save current position in `anything-current-buffer'.
  288. ;; `anything-last-frame-or-window-configuration'
  289. ;; Used to store window or frame configuration when anything start.
  290. ;; `anything-reading-pattern'
  291. ;; Whether in `read-string' in anything or not.
  292. ;; `anything-compile-source-functions-default'
  293. ;; Plug-ins this file provides.
  294. ;; `anything-input-local'
  295. ;; Not documented.
  296. ;; `anything-process-delayed-sources-timer'
  297. ;; Not documented.
  298. ;; `anything-mode-line-string-real'
  299. ;; Not documented.
  300. ;; `anything-exit-status'
  301. ;; Flag to inform whether anything have exited or quitted.
  302. ;; `anything-minibuffer-confirm-state'
  303. ;; Not documented.
  304. ;; `anything-types'
  305. ;; Not documented.
  306. ;; `anything-orig-enable-shortcuts'
  307. ;; Not documented.
  308. ;; `anything-persistent-action-display-window'
  309. ;; Return the window that will be used for presistent action.
  310. ;; `anything-visible-mark-face'
  311. ;; Not documented.
  312. ;; `anything-visible-mark-overlays'
  313. ;; Not documented.
  314. ;; `anything-marked-candidates'
  315. ;; Return marked candidates of current source if any.
  316. ;; `anything-maintainer-mail-address'
  317. ;; Not documented.
  318. ;; `anything-bug-report-salutation'
  319. ;; Not documented.
  320. ;; `anything-no-dump-variables'
  321. ;; Variables not to dump in bug report.
  322. ;; *** END auto-documentation
  323. ;; [EVAL] (autodoc-update-all)
  324. ;;; Commentary:
  325. ;;
  326. ;; Start with M-x anything, narrow the list by typing some pattern,
  327. ;; select with up/down/pgup/pgdown/C-p/C-n/C-v/M-v, choose with enter,
  328. ;; left/right moves between sources. With TAB actions can be selected
  329. ;; if the selected candidate has more than one possible action.
  330. ;;
  331. ;; Note that anything.el provides only the framework and some example
  332. ;; configurations for demonstration purposes. See anything-config.el
  333. ;; for practical, polished, easy to use configurations which can be
  334. ;; used to assemble a custom personalized configuration.
  335. ;;
  336. ;; NOTE: What you find on Emacswiki is mostly deprecated and not maintained,
  337. ;; don't complain if you use such code or configuration and something
  338. ;; doesn't work.
  339. ;;
  340. ;; Here is Japanese translation of `anything-sources' attributes. Thanks.
  341. ;; http://d.hatena.ne.jp/sirocco634/20091012/1255336649
  342. ;;; Bug Report:
  343. ;;
  344. ;; If you have problems, send a bug report via C-c C-x C-b in anything session (best)
  345. ;; or M-x anything-send-bug-report outside anything session.
  346. ;; I implemented bug report feature because I want to know your current state.
  347. ;; It helps me to solve problems easily.
  348. ;; The step is:
  349. ;; 0) Setup mail in Emacs, the easiest way is:
  350. ;; (setq user-mail-address "your@mail.address")
  351. ;; (setq user-full-name "Your Full Name")
  352. ;; (setq smtpmail-smtp-server "your.smtp.server.jp")
  353. ;; (setq mail-user-agent 'message-user-agent)
  354. ;; (setq message-send-mail-function 'message-smtpmail-send-it)
  355. ;; 1) Be sure to use the LATEST version of anything.el.
  356. ;; 2) Enable debugger. M-x toggle-debug-on-error or (setq debug-on-error t)
  357. ;; 3) Use Lisp version instead of compiled one: (load "anything.el")
  358. ;; 4) Do it!
  359. ;; 5) If you got an error, please do not close *Backtrace* buffer.
  360. ;; 6) Type C-c C-x C-b (anything session, best!)
  361. ;; or M-x anything-send-bug-report (outside)
  362. ;; then M-x insert-buffer *Backtrace* (if you got error)
  363. ;; 7) Describe the bug using a precise recipe.
  364. ;; 8) Type C-c C-c to send.
  365. ;;
  366. ;; You can also just report bug to:
  367. ;; https://groups.google.com/group/emacs-anything?hl=en
  368. ;; You can extend `anything' by writing plug-ins. As soon as
  369. ;; `anything' is invoked, `anything-sources' is compiled into basic
  370. ;; attributes, then compiled one is used during invocation.
  371. ;;
  372. ;; The oldest built-in plug-in is `type' attribute: appends
  373. ;; appropriate element of `anything-type-attributes'. Second built-in
  374. ;; plug-in is `candidates-in-buffer': selecting a line from candidates
  375. ;; buffer.
  376. ;;
  377. ;; To write a plug-in:
  378. ;; 1. Define a compiler: anything-compile-source--*
  379. ;; 2. Add compier function to `anything-compile-source-functions'.
  380. ;; 3. (optional) Write helper functions.
  381. ;
  382. ;; Tested on Emacs 22/23/24.
  383. ;;
  384. ;;
  385. ;; Thanks to Vagn Johansen for ideas.
  386. ;; Thanks to Stefan Kamphausen for fixes and XEmacs support.
  387. ;; Thanks to Tassilo Horn for fixes.
  388. ;; Thanks to Drew Adams for various fixes
  389. ;; Thanks to IMAKADO for candidates-in-buffer idea.
  390. ;; Thanks to Tomohiro MATSUYAMA for multiline patch.
  391. ;;
  392. ;;; (@* "Index")
  393. ;; If you have library `linkd.el', load
  394. ;; `linkd.el' and turn on `linkd-mode' now. It lets you easily
  395. ;; navigate around the sections Linkd mode will
  396. ;; highlight this Index. You can get `linkd.el' here:
  397. ;; http://www.emacswiki.org/cgi-bin/wiki/download/linkd.el
  398. ;;
  399. ;;; (@* "Tips")
  400. ;;
  401. ;; `anything' accepts keyword arguments. See docstring.
  402. ;; [EVAL IT] (describe-function 'anything)
  403. ;;
  404. ;; `anything-enable-shortcuts' enables us to select candidate easily.
  405. ;; If 'prefix then they can be selected using <prefix-key> <alnum>.
  406. ;; The prefix key is `anything-select-with-prefix-shortcut'.
  407. ;; If the <prefix-key> is a letter, pressing twice inputs the letter itself.
  408. ;; e.g.
  409. ;; (setq anything-enable-shortcuts 'prefix)
  410. ;; (define-key anything-map \"@\" 'anything-select-with-prefix-shortcut)
  411. ;;
  412. ;; You can edit current selection using `anything-edit-current-selection'.
  413. ;; It is useful after persistent-action.
  414. ;;
  415. ;; For `anything' users, setting `anything-sources' directly and
  416. ;; invoke M-x anything is obsolete way for now. Try M-x
  417. ;; `anything-migrate-sources'!
  418. ;;
  419. ;; If you want to create anything sources, yasnippet would help you.
  420. ;; http://yasnippet.googlecode.com/
  421. ;;
  422. ;; Then get the snippet from
  423. ;; http://www.emacswiki.org/cgi-bin/wiki/download/anything-source.yasnippet
  424. ;;
  425. ;; Put it in ~/.emacs.d/plugins/yasnippet/snippets/text-mode/emacs-lisp-mode/
  426. ;;
  427. ;; `anything-interpret-value' is useful function to interpret value
  428. ;; like `candidates' attribute.
  429. ;;
  430. ;; (anything-interpret-value "literal") ; => "literal"
  431. ;; (anything-interpret-value (lambda () "lambda")) ; => "lambda"
  432. ;; (let ((source '((name . "lambda with source name"))))
  433. ;; (anything-interpret-value
  434. ;; (lambda () anything-source-name)
  435. ;; source)) ; => "lambda with source name"
  436. ;; (flet ((f () "function symbol"))
  437. ;; (anything-interpret-value 'f)) ; => "function symbol"
  438. ;; (let ((v "variable symbol"))
  439. ;; (anything-interpret-value 'v)) ; => "variable symbol"
  440. ;; (anything-interpret-value 'unbounded-1) ; error
  441. ;;
  442. ;; Now symbols are acceptable as candidates. So you do not have to use
  443. ;; `symbol-name' function. The source is much simpler. For example,
  444. ;; `apropos-internal' returns a list of symbols.
  445. ;;
  446. ;; (anything
  447. ;; '(((name . "Commands")
  448. ;; (candidates . (lambda () (apropos-internal anything-pattern 'commandp)))
  449. ;; (volatile)
  450. ;; (action . describe-function))))
  451. ;;
  452. ;; To mark a candidate, press C-SPC as normal Emacs marking. To go to
  453. ;; marked candidate, press M-[ or M-].
  454. ;;
  455. ;; `anything-map' is now Emacs-standard key bindings by default.
  456. ;;
  457. ;; There are many `anything' applications, using `anything' for
  458. ;; selecting candidate. In this case, if there is one candidate or no
  459. ;; candidate, popping up *anything* buffer is irritating. If one
  460. ;; candidate, you want to select it at once. If no candidate, you want
  461. ;; to quit `anything'. Set `anything-execute-action-at-once-if-one'
  462. ;; and `anything-quit-if-no-candidate' to non-nil to remedy it. Note
  463. ;; that setting these variables GLOBALLY is bad idea because of
  464. ;; delayed sources. These are meant to be let-binded.
  465. ;;
  466. ;; ex.
  467. ;; (let ((anything-execute-action-at-once-if-one t)
  468. ;; (anything-quit-if-no-candidate (lambda () (message "No candidate"))))
  469. ;; (anything temporary-sources input))
  470. ;;
  471. ;; `set-frame-configuration' arises flickering. If you hate
  472. ;; flickering, eval:
  473. ;; (setq anything-save-configuration-functions
  474. ;; '(set-window-configuration . current-window-configuration))
  475. ;; at the cost of restoring frame configuration (only window configuration).
  476. ;;
  477. ;; `anything-delete-current-selection' deletes the current line.
  478. ;; It is useful when deleting a candidate in persistent action.
  479. ;; eg. `kill-buffer'.
  480. ;;
  481. ;; [EVAL IT] (describe-function 'anything-delete-current-selection)
  482. ;;
  483. ;; `anything-attr' gets the attribute. `anything-attrset' sets the
  484. ;; attribute. `anything-attr-defined' tests whether the attribute is
  485. ;; defined. They handles source-local variables.
  486. ;;
  487. ;; [EVAL IT] (describe-function 'anything-attr)
  488. ;; [EVAL IT] (describe-function 'anything-attrset)
  489. ;; [EVAL IT] (describe-function 'anything-attr-defined)
  490. ;;
  491. ;; `anything-sources' accepts many attributes to make your life easier.
  492. ;; Now `anything-sources' accepts a list of symbols.
  493. ;;
  494. ;; [EVAL IT] (describe-variable 'anything-sources)
  495. ;;
  496. ;; `anything' has optional arguments. Now you do not have to let-bind
  497. ;; `anything-sources'.
  498. ;;
  499. ;; [EVAL IT] (describe-function 'anything)
  500. ;;
  501. ;; `anything-resume' resumes last `anything' session. Now you do not
  502. ;; have to retype pattern.
  503. ;;
  504. ;; [EVAL IT] (describe-function 'anything-resume)
  505. ;;
  506. ;; `anything-execute-persistent-action' executes action without
  507. ;; quitting `anything'. When popping up a buffer in other window by
  508. ;; persistent action, you can scroll with `anything-scroll-other-window' and
  509. ;; `anything-scroll-other-window-down'. See also `anything-sources' docstring.
  510. ;;
  511. ;; [EVAL IT] (describe-function 'anything-execute-persistent-action)
  512. ;; [EVAL IT] (describe-variable 'anything-sources)
  513. ;;
  514. ;; `anything-select-2nd-action', `anything-select-3rd-action' and
  515. ;; `anything-select-4th-action' select other than default action
  516. ;; without pressing Tab.
  517. ;;
  518. ;; Using `anything-candidate-buffer' and the candidates-in-buffer
  519. ;; attribute is much faster than traditional "candidates and match"
  520. ;; way. And `anything-current-buffer-is-modified' avoids to
  521. ;; recalculate candidates for unmodified buffer. See docstring of
  522. ;; them.
  523. ;;
  524. ;; [EVAL IT] (describe-function 'anything-candidate-buffer)
  525. ;; [EVAL IT] (describe-function 'anything-candidates-in-buffer)
  526. ;; [EVAL IT] (describe-function 'anything-current-buffer-is-modified)
  527. ;;
  528. ;; `anything-current-buffer' and `anything-buffer-file-name' stores
  529. ;; `(current-buffer)' and `buffer-file-name' in the buffer `anything'
  530. ;; is invoked. Use them freely.
  531. ;;
  532. ;; [EVAL IT] (describe-variable 'anything-current-buffer)
  533. ;; [EVAL IT] (describe-variable 'anything-buffer-file-name)
  534. ;;
  535. ;; `anything-completing-read' and `anything-read-file-name' are
  536. ;; experimental implementation. If you are curious, type M-x
  537. ;; anything-read-string-mode. It is a minor mode and toggles on/off.
  538. ;;
  539. ;; Use `anything-test-candidates' to test your handmade anything
  540. ;; sources. It simulates contents of *anything* buffer with pseudo
  541. ;; `anything-sources' and `anything-pattern', without side-effect. So
  542. ;; you can unit-test your anything sources! Let's TDD!
  543. ;;
  544. ;; [EVAL IT] (describe-function 'anything-test-candidates)
  545. ;;
  546. ;; For anything developpers:
  547. ;;
  548. ;; There are many unit-testing framework in Emacs Lisp. See the EmacsWiki.
  549. ;; http://www.emacswiki.org/cgi-bin/emacs/UnitTesting
  550. ;; There is an unit-test by Emacs Lisp Expectations in developper-tools directory.
  551. ;; http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el
  552. ;; http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el
  553. ;;
  554. ;; If you want to create anything sources, see anything-config.el.
  555. ;; It is huge collection of sources. You can learn from examples.
  556. ;; (@* "TODO")
  557. ;;
  558. ;; - process status indication
  559. ;;
  560. ;; - async sources doesn't honor digit-shortcut-count
  561. ;;
  562. ;; - anything-candidate-number-limit can't be nil everywhere
  563. ;; (@* "HISTORY")
  564. ;;
  565. ;; Change log of this file is found at
  566. ;; http://repo.or.cz/w/anything-config.git/history/master:/anything.el
  567. ;;
  568. ;; Change log of this project is found at
  569. ;; http://repo.or.cz/w/anything-config.git?a=shortlog
  570. ;;
  571. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  572. ;;; Code:
  573. (require 'cl)
  574. (defvar anything-version "1.3.9")
  575. ;; (@* "User Configuration")
  576. ;;; Variables
  577. ;;
  578. ;;
  579. ;; [DEPRECATED]
  580. ;; A default value is provided in anything-config.el
  581. (defvar anything-sources nil
  582. "A list of sources to use with `anything'.
  583. It is deprecated, you should not use this.
  584. Use instead individual sources or list of sources of your choice.")
  585. ;; Default values are provided in anything-config.el.
  586. (defvar anything-type-attributes nil
  587. "It's a list of \(TYPE ATTRIBUTES ...\).
  588. ATTRIBUTES are the same as attributes for `anything-sources'.
  589. TYPE connects the value to the appropriate sources.
  590. Don't set this directly, use instead `define-anything-type-attribute'.
  591. This allows specifying common attributes for several sources.
  592. For example, sources which provide files can specify
  593. common attributes with a `file' type.")
  594. (defvaralias 'anything-enable-digit-shortcuts 'anything-enable-shortcuts
  595. "Same as `anything-enable-shortcuts'.
  596. Alphabet shortcuts are available now in `anything-enable-shortcuts'.
  597. `anything-enable-digit-shortcuts' is retained for compatibility.")
  598. (defvar anything-enable-shortcuts nil
  599. "*Whether to use digit/alphabet shortcut to select the first nine matches.
  600. If t then they can be selected using Ctrl+<number>.
  601. If 'prefix then they can be selected using <prefix-key> <alnum>.
  602. The prefix key is `anything-select-with-prefix-shortcut'.
  603. If the <prefix-key> is a letter, pressing twice inputs the letter itself.
  604. e.g.
  605. (setq anything-enable-shortcuts 'prefix)
  606. (define-key anything-map \"@\" 'anything-select-with-prefix-shortcut)
  607. If 'alphabet then they can be selected using Shift+<alphabet> (deprecated).
  608. It is not recommended because you cannot input capital letters in pattern.
  609. Keys (digit/alphabet) are listed in `anything-shortcut-keys-alist'.")
  610. (defvar anything-shortcut-keys-alist
  611. '((alphabet . "asdfghjklzxcvbnmqwertyuiop")
  612. (prefix . "asdfghjklzxcvbnmqwertyuiop1234567890")
  613. (t . "123456789")))
  614. (defvar anything-display-source-at-screen-top t
  615. "*Display candidates at the top of screen.
  616. This happen when using `anything-next-source' and `anything-previous-source'.")
  617. (defvar anything-candidate-number-limit 50
  618. "*Limit candidate number globally.
  619. Do not show more candidates than this limit from individual sources.
  620. It is usually pointless to show hundreds of matches
  621. when the pattern is empty, because it is much simpler to type a
  622. few characters to narrow down the list of potential candidates.
  623. Set it to nil if you don't want this limit.")
  624. (defvar anything-idle-delay 0.3
  625. "*Be idle for this many seconds, before updating in delayed sources.
  626. This is useful for sources involving heavy operations
  627. \(like launching external programs\), so that candidates
  628. from the source are not retrieved unnecessarily if the user keeps typing.
  629. It also can be used to declutter the results anything displays,
  630. so that results from certain sources are not shown with every
  631. character typed, only if the user hesitates a bit.")
  632. (defvar anything-input-idle-delay 0.3
  633. "Be idle for this many seconds, before updating.
  634. Unlike `anything-idle-delay', it is also effective for non-delayed sources.
  635. If nil, candidates are collected immediately.
  636. Note: If this value is too low compared to `anything-idle-delay',
  637. you may have duplicated sources when using multiples sources.
  638. Safe value is always >= `anything-idle-delay'.
  639. Default settings are equal value for both.")
  640. (defvar anything-samewindow nil
  641. "Use current window to show the candidates.
  642. If t then Anything doesn't pop up a new window.")
  643. (defvar anything-source-filter nil
  644. "A list of source names to be displayed.
  645. Other sources won't appear in the search results.
  646. If nil then there is no filtering.
  647. See also `anything-set-source-filter'.")
  648. (defvar anything-map
  649. (let ((map (copy-keymap minibuffer-local-map)))
  650. (define-key map (kbd "<down>") 'anything-next-line)
  651. (define-key map (kbd "<up>") 'anything-previous-line)
  652. (define-key map (kbd "C-n") 'anything-next-line)
  653. (define-key map (kbd "C-p") 'anything-previous-line)
  654. (define-key map (kbd "<prior>") 'anything-previous-page)
  655. (define-key map (kbd "<next>") 'anything-next-page)
  656. (define-key map (kbd "M-v") 'anything-previous-page)
  657. (define-key map (kbd "C-v") 'anything-next-page)
  658. (define-key map (kbd "M-<") 'anything-beginning-of-buffer)
  659. (define-key map (kbd "M->") 'anything-end-of-buffer)
  660. (define-key map (kbd "C-g") 'anything-keyboard-quit)
  661. (define-key map (kbd "<right>") 'anything-next-source)
  662. (define-key map (kbd "<left>") 'anything-previous-source)
  663. (define-key map (kbd "<RET>") 'anything-exit-minibuffer)
  664. (define-key map (kbd "C-1") 'anything-select-with-digit-shortcut)
  665. (define-key map (kbd "C-2") 'anything-select-with-digit-shortcut)
  666. (define-key map (kbd "C-3") 'anything-select-with-digit-shortcut)
  667. (define-key map (kbd "C-4") 'anything-select-with-digit-shortcut)
  668. (define-key map (kbd "C-5") 'anything-select-with-digit-shortcut)
  669. (define-key map (kbd "C-6") 'anything-select-with-digit-shortcut)
  670. (define-key map (kbd "C-7") 'anything-select-with-digit-shortcut)
  671. (define-key map (kbd "C-8") 'anything-select-with-digit-shortcut)
  672. (define-key map (kbd "C-9") 'anything-select-with-digit-shortcut)
  673. (loop for c from ?A to ?Z do
  674. (define-key map (make-string 1 c) 'anything-select-with-digit-shortcut))
  675. (define-key map (kbd "C-i") 'anything-select-action)
  676. (define-key map (kbd "C-z") 'anything-execute-persistent-action)
  677. (define-key map (kbd "C-e") 'anything-select-2nd-action-or-end-of-line)
  678. (define-key map (kbd "C-j") 'anything-select-3rd-action)
  679. (define-key map (kbd "C-o") 'anything-next-source)
  680. (define-key map (kbd "C-M-v") 'anything-scroll-other-window)
  681. (define-key map (kbd "M-<next>") 'anything-scroll-other-window)
  682. (define-key map (kbd "C-M-y") 'anything-scroll-other-window-down)
  683. (define-key map (kbd "C-M-S-v") 'anything-scroll-other-window-down)
  684. (define-key map (kbd "M-<prior>") 'anything-scroll-other-window-down)
  685. (define-key map (kbd "<C-M-down>") 'anything-scroll-other-window)
  686. (define-key map (kbd "<C-M-up>") 'anything-scroll-other-window-down)
  687. (define-key map (kbd "C-SPC") 'anything-toggle-visible-mark)
  688. (define-key map (kbd "M-SPC") 'anything-toggle-visible-mark)
  689. (define-key map (kbd "M-[") 'anything-prev-visible-mark)
  690. (define-key map (kbd "M-]") 'anything-next-visible-mark)
  691. (define-key map (kbd "C-k") 'anything-delete-minibuffer-contents)
  692. (define-key map (kbd "C-r") 'undefined)
  693. (define-key map (kbd "C-t") 'anything-toggle-resplit-window)
  694. (define-key map (kbd "C-}") 'anything-narrow-window)
  695. (define-key map (kbd "C-{") 'anything-enlarge-window)
  696. (define-key map (kbd "C-c C-d") 'anything-delete-current-selection)
  697. (define-key map (kbd "C-c C-y") 'anything-yank-selection)
  698. (define-key map (kbd "C-c C-k") 'anything-kill-selection-and-quit)
  699. (define-key map (kbd "C-c C-f") 'anything-follow-mode)
  700. (define-key map (kbd "C-c C-u") 'anything-force-update)
  701. (define-key map (kbd "M-p") 'previous-history-element)
  702. (define-key map (kbd "M-n") 'next-history-element)
  703. ;; Debugging command
  704. (define-key map "\C-c\C-x\C-d" 'anything-debug-output)
  705. (define-key map "\C-c\C-x\C-m" 'anything-display-all-visible-marks)
  706. (define-key map "\C-c\C-x\C-b" 'anything-send-bug-report-from-anything)
  707. ;; Use `describe-mode' key in `global-map'.
  708. (define-key map [f1] nil) ; Allow to eval keymap whithout errors.
  709. (dolist (k (where-is-internal 'describe-mode global-map))
  710. (define-key map k 'anything-help))
  711. map)
  712. "Keymap for anything.")
  713. (defgroup anything nil
  714. "Open anything."
  715. :prefix "anything-" :group 'convenience)
  716. (defface anything-header
  717. '((t (:inherit header-line)))
  718. "Face for header lines in the anything buffer."
  719. :group 'anything)
  720. (defvar anything-header-face 'anything-header
  721. "*Face for header lines in the anything buffer.")
  722. (defface anything-candidate-number
  723. '((t (:background "Yellow" :foreground "black")))
  724. "Face for candidate number in mode-line." :group 'anything)
  725. (defvar anything-selection-face 'highlight
  726. "*Face for currently selected item.")
  727. (defvar anything-buffer "*anything*"
  728. "Buffer showing completions.")
  729. (defvar anything-action-buffer "*anything action*"
  730. "Buffer showing actions.")
  731. (defvar anything-selection-overlay nil
  732. "Overlay used to highlight the currently selected item.")
  733. (defvar anything-digit-overlays nil
  734. "Overlays for digit shortcuts. See `anything-enable-shortcuts'.")
  735. (defvar anything-candidate-cache nil
  736. "Holds the available candidate withing a single anything invocation.")
  737. (defvar anything-pattern
  738. "The input pattern used to update the anything buffer.")
  739. (defvar anything-input
  740. "The input typed in the candidates panel.")
  741. (defvar anything-async-processes nil
  742. "List of information about asynchronous processes managed by anything.")
  743. (defvar anything-digit-shortcut-count 0
  744. "Number of digit shortcuts shown in the anything buffer.")
  745. (defvar anything-before-initialize-hook nil
  746. "Run before anything initialization.
  747. This hook is run before init functions in `anything-sources'.")
  748. (defvar anything-after-initialize-hook nil
  749. "Run after anything initialization.
  750. Global variables are initialized and the anything buffer is created.
  751. But the anything buffer has no contents.")
  752. (defvar anything-update-hook nil
  753. "Run after the anything buffer was updated according the new input pattern.
  754. This hook is run at the beginning of buffer.
  755. The first candidate is selected after running this hook.
  756. See also `anything-after-update-hook'.")
  757. (defvar anything-after-update-hook nil
  758. "Run after the anything buffer was updated according the new input pattern.
  759. This is very similar to `anything-update-hook' but selection is not moved.
  760. It is useful to select a particular object instead of the first one.")
  761. (defvar anything-cleanup-hook nil
  762. "Run after anything minibuffer is closed.
  763. IOW this hook is executed BEFORE performing action.")
  764. (defvar anything-select-action-hook nil
  765. "Run when opening the action buffer.")
  766. (defvar anything-before-action-hook nil
  767. "Run before executing action.
  768. Contrarily to `anything-cleanup-hook',
  769. this hook run before anything minibuffer is closed
  770. and before performing action.")
  771. (defvar anything-after-action-hook nil
  772. "Run after executing action.")
  773. (defvar anything-after-persistent-action-hook nil
  774. "Run after executing persistent action.")
  775. (defvar anything-move-selection-before-hook nil
  776. "Run before moving selection in `anything-buffer'.")
  777. (defvar anything-move-selection-after-hook nil
  778. "Run after moving selection in `anything-buffer'.")
  779. (defvar anything-restored-variables
  780. '(anything-candidate-number-limit
  781. anything-source-filter
  782. anything-source-in-each-line-flag
  783. anything-map
  784. anything-sources)
  785. "Variables which are restored after `anything' invocation.")
  786. (defvar anything-saved-selection nil
  787. "Value of the currently selected object when the action list is shown.")
  788. (defvar anything-current-prefix-arg nil
  789. "Record `current-prefix-arg' when exiting minibuffer.")
  790. (defvar anything-candidate-separator
  791. "--------------------"
  792. "Candidates separator of `multiline' source.")
  793. (defvar anything-current-buffer nil
  794. "Current buffer when `anything' is invoked.")
  795. (defvar anything-buffer-file-name nil
  796. "Variable `buffer-file-name' when `anything' is invoked.")
  797. (defvar anything-saved-action nil
  798. "Saved value of the currently selected action by key.")
  799. (defvar anything-last-sources nil
  800. "OBSOLETE!! Sources of previously invoked `anything'.")
  801. (defvar anything-saved-current-source nil
  802. "Value of the current source when the action list is shown.")
  803. (defvar anything-compiled-sources nil
  804. "Compiled version of `anything-sources'.")
  805. (defvar anything-in-persistent-action nil
  806. "Flag whether in persistent-action or not.")
  807. (defvar anything-quick-update nil
  808. "If non-nil, suppress displaying sources which are out of screen at first.
  809. They are treated as delayed sources at this input.
  810. This flag makes `anything' a bit faster with many sources.")
  811. (defvar anything-last-sources-local nil
  812. "Buffer local value of `anything-sources'.")
  813. (defvar anything-last-buffer nil
  814. "`anything-buffer' of previously `anything' session.")
  815. (defvar anything-save-configuration-functions
  816. '(set-window-configuration . current-window-configuration)
  817. "The functions used to restore/save window or frame configurations.
  818. It is a pair where the car is the function to restore window or frame config,
  819. and the cdr is the function to save the window or frame config.
  820. If you want to save and restore frame configuration, set this variable to
  821. '\(set-frame-configuration . current-frame-configuration\)
  822. Older version saves/restores frame configuration, but the default is changed now
  823. because flickering can occur in some environment. ")
  824. (defvar anything-persistent-action-use-special-display nil
  825. "If non-nil, use `special-display-function' in persistent action.")
  826. (defvar anything-execute-action-at-once-if-one nil
  827. "Execute default action and exit when only one candidate is remaining.
  828. It is useful for `anything' applications.")
  829. (defvar anything-quit-if-no-candidate nil
  830. "Quit when there is no candidates when non--nil.
  831. This variable accepts a function, which is executed if no candidate.
  832. It is useful for `anything' applications.")
  833. (defvar anything-scroll-amount nil
  834. "Scroll amount when scrolling other window in an anything session.
  835. It is used by `anything-scroll-other-window'
  836. and `anything-scroll-other-window-down'.
  837. If you prefer scrolling line by line, set this value to 1.")
  838. (defvar anything-display-function 'anything-default-display-buffer
  839. "Function to display *anything* buffer.
  840. It is `anything-default-display-buffer' by default,
  841. which affects `anything-samewindow'.")
  842. (defvar anything-delayed-init-executed nil)
  843. (defvar anything-mode-line-string "\\<anything-map>\\[anything-help]:help \
  844. \\[anything-select-action]:Acts \
  845. \\[anything-exit-minibuffer]/\\[anything-select-2nd-action-or-end-of-line]/\
  846. \\[anything-select-3rd-action]:NthAct \
  847. \\[anything-send-bug-report-from-anything]:BugReport"
  848. "Help string displayed in mode-line in `anything'.
  849. It can be a string or a list of two args, in this case,
  850. first arg is a string that will be used as name for candidates number,
  851. second arg any string to display in mode line.
  852. If nil, use default `mode-line-format'.")
  853. (defvar anything-help-message
  854. "\\<anything-map>The keys that are defined for `anything' are:
  855. \\{anything-map}"
  856. "Detailed help message string for `anything'.
  857. It also accepts function or variable symbol.")
  858. (defvar anything-source-in-each-line-flag nil
  859. "Non-nil means add anything-source text-property in each candidate.
  860. experimental feature.")
  861. (defvaralias 'anything-debug-variables 'anything-debug-forms)
  862. (defvar anything-debug-forms nil
  863. "Forms to show in `anything-debug-output'.
  864. Otherwise all variables started with `anything-' are shown.
  865. It is useful for debug.")
  866. (defvar anything-debug nil
  867. "If non-nil, write log message into *Anything Log* buffer.
  868. If `debug-on-error' is non-nil, write log message regardless of this variable.
  869. It is disabled by default because *Anything Log* grows quickly.")
  870. (defcustom anything-local-map-override-anything-map t
  871. "Override `anything-map' keys with the corresponding ones in source local map.
  872. When non--nil keys in source local map will override same keys in `anything-map'
  873. otherwise same keys in `anything-map' will take precedence."
  874. :group 'anything
  875. :type 'boolean)
  876. ;; (@* "Internal Variables")
  877. (defvar anything-test-candidate-list nil)
  878. (defvar anything-test-mode nil)
  879. (defvar anything-source-name nil)
  880. (defvar anything-candidate-buffer-alist nil)
  881. (defvar anything-check-minibuffer-input-timer nil)
  882. (defvar anything-match-hash (make-hash-table :test 'equal))
  883. (defvar anything-cib-hash (make-hash-table :test 'equal))
  884. (defvar anything-tick-hash (make-hash-table :test 'equal))
  885. (defvar anything-issued-errors nil)
  886. (defvar anything-shortcut-keys nil)
  887. (defvar anything-once-called-functions nil)
  888. (defvar anything-follow-mode nil)
  889. (defvar anything-let-variables nil)
  890. (defvar anything-split-window-state nil)
  891. (defvar anything-selection-point nil)
  892. (defvar anything-alive-p nil)
  893. ;; (@* "Utility: logging")
  894. (defun anything-log (format-string &rest args)
  895. "Log message if `debug-on-error' or `anything-debug' is non-nil.
  896. Messages are written to the *Anything Log* buffer.
  897. Argument FORMAT-STRING is a string to use with `format'.
  898. Use optional arguments ARGS like in `format'."
  899. (when (or debug-on-error anything-debug)
  900. (with-current-buffer (get-buffer-create "*Anything Log*")
  901. (buffer-disable-undo)
  902. (set (make-local-variable 'inhibit-read-only) t)
  903. (goto-char (point-max))
  904. (insert (let ((tm (current-time)))
  905. (format "%s.%06d (%s) %s\n"
  906. (format-time-string "%H:%M:%S" tm)
  907. (nth 2 tm)
  908. (anything-log-get-current-function)
  909. (apply #'format (cons format-string args))))))))
  910. (defmacro anything-log-eval (&rest exprs)
  911. "Write each EXPRS evaluation result to the *Anything Log* buffer."
  912. `(anything-log-eval-internal ',exprs))
  913. (defun anything-log-run-hook (hook)
  914. "Run HOOK like `run-hooks' but write these actions to anything log buffer."
  915. (anything-log "executing %s" hook)
  916. (when (boundp hook)
  917. (anything-log-eval (symbol-value hook))
  918. (anything-log-eval (default-value hook)))
  919. (run-hooks hook)
  920. (anything-log "executed %s" hook))
  921. (defun anything-log-eval-internal (exprs)
  922. "Eval EXPRS and write results to anything log buffer."
  923. (dolist (expr exprs)
  924. (condition-case err
  925. (anything-log "%S = %S" expr (eval expr))
  926. (error (anything-log "%S = ERROR!" expr)))))
  927. (defun anything-log-get-current-function ()
  928. "Get function name calling `anything-log'.
  929. The original idea is from `tramp-debug-message'."
  930. (loop with exclude-func-re = "^anything-\\(?:interpret\\|log\\|.*funcall\\)"
  931. for btn from 1 to 40 ;avoid inf-loop
  932. for btf = (second (backtrace-frame btn))
  933. for fn = (if (symbolp btf) (symbol-name btf) "")
  934. if (and (string-match "^anything" fn)
  935. (not (string-match exclude-func-re fn)))
  936. return fn))
  937. (defun anything-log-error (&rest args)
  938. "Accumulate error messages into `anything-issued-errors'.
  939. ARGS are args given to `format'."
  940. (apply 'anything-log (concat "ERROR: " (car args)) (cdr args))
  941. (let ((msg (apply 'format args)))
  942. (unless (member msg anything-issued-errors)
  943. (add-to-list 'anything-issued-errors msg))))
  944. (defvar anything-last-log-file nil)
  945. (defun anything-log-save-maybe ()
  946. "May be save log buffer to `anything-last-log-file'."
  947. (when (stringp anything-debug)
  948. (let ((logdir (expand-file-name (format-time-string "%Y%m%d")
  949. anything-debug)))
  950. (make-directory logdir t)
  951. (with-current-buffer (get-buffer-create "*Anything Log*")
  952. (write-region (point-min) (point-max)
  953. (setq anything-last-log-file
  954. (expand-file-name (format-time-string "%Y%m%d-%H%M%S")
  955. logdir))
  956. nil 'silent)
  957. (erase-buffer)))))
  958. (defun anything-open-last-log ()
  959. "Open anything log file of last anything session."
  960. (interactive)
  961. (if anything-last-log-file
  962. (view-file anything-last-log-file)
  963. (switch-to-buffer "*Anything Log*")))
  964. (defun anything-print-error-messages ()
  965. "Print error messages in `anything-issued-errors'."
  966. (message "%s" (mapconcat 'identity (reverse anything-issued-errors) "\n")))
  967. ;; (anything-log "test")
  968. ;; (switch-to-buffer-other-window "*Anything Log*")
  969. ;; (@* "Programming Tools")
  970. (defmacro anything-aif (test-form then-form &rest else-forms)
  971. "Like `if' but set the result of TEST-FORM in a temprary variable called `it'.
  972. THEN-FORM and ELSE-FORMS are then excuted just like in `if'."
  973. (declare (indent 2) (debug t))
  974. `(let ((it ,test-form))
  975. (if it ,then-form ,@else-forms)))
  976. (defun anything-mklist (obj)
  977. "If OBJ is a list \(but not lambda\), return itself.
  978. Otherwise make a list with one element."
  979. (if (and (listp obj) (not (functionp obj)))
  980. obj
  981. (list obj)))
  982. ;; (@* "Anything API")
  983. (defun anything-buffer-get ()
  984. "Return `anything-action-buffer' if shown otherwise `anything-buffer'."
  985. (if (anything-action-window)
  986. anything-action-buffer
  987. anything-buffer))
  988. (defun anything-window ()
  989. "Window of `anything-buffer'."
  990. (get-buffer-window (anything-buffer-get) 'visible))
  991. (defun anything-action-window ()
  992. "Window of `anything-action-buffer'."
  993. (get-buffer-window anything-action-buffer 'visible))
  994. (defmacro with-anything-window (&rest body)
  995. "Be sure BODY is excuted in the anything window."
  996. (declare (indent 0) (debug t))
  997. `(if anything-test-mode
  998. (with-current-buffer (anything-buffer-get)
  999. ,@body)
  1000. (with-selected-window (anything-window)
  1001. ,@body)))
  1002. (defmacro with-anything-current-buffer (&rest body)
  1003. "Eval BODY inside `anything-current-buffer'."
  1004. (declare (indent 0) (debug t))
  1005. `(with-current-buffer anything-current-buffer
  1006. ,@body))
  1007. (defmacro with-anything-restore-variables(&rest body)
  1008. "Restore `anything-restored-variables' after executing BODY.
  1009. `post-command-hook' is handled specially."
  1010. (declare (indent 0) (debug t))
  1011. `(let ((--orig-vars (mapcar (lambda (v)
  1012. (cons v (symbol-value v)))
  1013. anything-restored-variables))
  1014. (--post-command-hook-pair (cons post-command-hook
  1015. (default-value 'post-command-hook))))
  1016. (setq post-command-hook '(t))
  1017. (setq-default post-command-hook nil)
  1018. (unwind-protect (progn ,@body)
  1019. (loop for (var . value) in --orig-vars
  1020. do (set var value))
  1021. (setq post-command-hook (car --post-command-hook-pair))
  1022. (setq-default post-command-hook (cdr --post-command-hook-pair))
  1023. (anything-log "restore variables"))))
  1024. (defun* anything-attr (attribute-name &optional
  1025. (src (anything-get-current-source)))
  1026. "Get the value of ATTRIBUTE-NAME of SRC.
  1027. if SRC is omitted, use current source.
  1028. It is useful to write your sources."
  1029. (anything-aif (assq attribute-name src)
  1030. (cdr it)))
  1031. (defun* anything-attr* (attribute-name
  1032. &optional (src (anything-get-current-source)))
  1033. "Pass the value of ATTRIBUTE-NAME of SRC to `anything-interpret-value'.
  1034. if SRC is omitted, use current source.
  1035. It is useful to write your sources."
  1036. (anything-interpret-value (anything-attr attribute-name src)))
  1037. (defun* anything-attr-defined (attribute-name
  1038. &optional (src (anything-get-current-source)))
  1039. "Return non-nil if ATTRIBUTE-NAME of SRC is defined.
  1040. if SRC is omitted, use current source.
  1041. It is useful to write your sources."
  1042. (and (assq attribute-name src) t))
  1043. (defun* anything-attrset (attribute-name value
  1044. &optional
  1045. (src (anything-get-current-source)))
  1046. "Set the value of ATTRIBUTE-NAME of SRC to VALUE.
  1047. if SRC is omitted, use current source.
  1048. It is useful to write your sources."
  1049. (anything-aif (assq attribute-name src)
  1050. (setcdr it value)
  1051. (setcdr src (cons (cons attribute-name value) (cdr src))))
  1052. value)
  1053. (defun anything-set-source-filter (sources)
  1054. "Set the value of `anything-source-filter' to SOURCES and update.
  1055. This function sets a filter for anything sources and it may be
  1056. called while anything is running. It can be used to toggle
  1057. displaying of sources dinamically. For example, additional keys
  1058. can be bound into `anything-map' to display only the file-related
  1059. results if there are too many matches from other sources and
  1060. you're after files only:
  1061. Shift+F shows only file results from some sources:
  1062. \(define-key anything-map \"F\" 'anything-my-show-files-only)
  1063. \(defun anything-my-show-files-only ()
  1064. (interactive)
  1065. (anything-set-source-filter '(\"File Name History\"
  1066. \"Files from Current Directory\")))
  1067. Shift+A shows all results:
  1068. \(define-key anything-map \"A\" 'anything-my-show-all)
  1069. \(defun anything-my-show-all ()
  1070. (interactive)
  1071. (anything-set-source-filter nil))
  1072. Note that you have to prefix the functions with anything- prefix,
  1073. otherwise they won't be bound when Anything is used under
  1074. Iswitchb. The -my- part is added to avoid collisions with
  1075. existing Anything function names."
  1076. (unless (and (listp sources)
  1077. (loop for name in sources always (stringp name)))
  1078. (error "Invalid data in `anything-set-source-filter': %S" sources))
  1079. (setq anything-source-filter sources)
  1080. (anything-log-eval anything-source-filter)
  1081. (anything-update))
  1082. (defun anything-set-sources (sources &optional no-init no-update)
  1083. "Set SOURCES during `anything' invocation.
  1084. If NO-INIT is non-nil, skip executing init functions of SOURCES.
  1085. If NO-UPDATE is non-nil, skip executing `anything-update'."
  1086. (with-current-buffer anything-buffer
  1087. (setq anything-compiled-sources nil
  1088. anything-sources sources
  1089. anything-last-sources-local sources)
  1090. (anything-log-eval anything-compiled-sources anything-sources))
  1091. (unless no-init (anything-funcall-foreach 'init))
  1092. (unless no-update (anything-update)))
  1093. (defvar anything-compile-source-functions
  1094. '(anything-compile-source--type
  1095. anything-compile-source--dummy
  1096. anything-compile-source--disable-shortcuts
  1097. anything-compile-source--candidates-in-buffer)
  1098. "Functions to compile elements of `anything-sources' (plug-in).")
  1099. (defun anything-get-sources ()
  1100. "Return compiled `anything-sources', which is memoized.
  1101. Attributes:
  1102. - type
  1103. `anything-type-attributes' are merged in.
  1104. - candidates-buffer
  1105. candidates, volatile and match attribute are created."
  1106. (cond
  1107. ;; action
  1108. ((anything-action-window)
  1109. anything-sources)
  1110. ;; memoized
  1111. (anything-compiled-sources)
  1112. ;; first time
  1113. (t
  1114. (prog1
  1115. (setq anything-compiled-sources
  1116. (anything-compile-sources
  1117. anything-sources anything-compile-source-functions))
  1118. (anything-log-eval anything-compiled-sources)))))
  1119. (defun* anything-get-selection (&optional (buffer nil buffer-s) (force-display-part))
  1120. "Return the currently selected item or nil.
  1121. if BUFFER is nil or unspecified, use anything-buffer as default value.
  1122. If FORCE-DISPLAY-PART is non-nil, return the display string."
  1123. (setq buffer (if (and buffer buffer-s) buffer anything-buffer))
  1124. (unless (anything-empty-buffer-p buffer)
  1125. (with-current-buffer buffer
  1126. (let ((selection
  1127. (or (and (not force-display-part)
  1128. (get-text-property (overlay-start
  1129. anything-selection-overlay)
  1130. 'anything-realvalue))
  1131. (let ((disp (buffer-substring-no-properties
  1132. (overlay-start anything-selection-overlay)
  1133. (1- (overlay-end anything-selection-overlay))))
  1134. (source (anything-get-current-source)))
  1135. (anything-aif (and (not force-display-part)
  1136. (assoc-default 'display-to-real source))
  1137. (anything-funcall-with-source source it disp)
  1138. disp)))))
  1139. (unless (equal selection "")
  1140. (anything-log-eval selection)
  1141. selection)))))
  1142. (defun anything-get-action ()
  1143. "Return the associated action for the selected candidate.
  1144. It is a function symbol \(sole action\) or list
  1145. of \(action-display . function\)."
  1146. (unless (anything-empty-buffer-p (anything-buffer-get))
  1147. (anything-aif (anything-attr 'action-transformer)
  1148. (anything-composed-funcall-with-source
  1149. (anything-get-current-source) it
  1150. (anything-attr 'action) (anything-get-selection))
  1151. (anything-attr 'action))))
  1152. (defun anything-get-current-source ()
  1153. "Return the source for the current selection.
  1154. Use it in init, candidates, action, candidate-transformer,
  1155. filtered-candidate-transformer functions."
  1156. (declare (special source))
  1157. ;; The name `anything-get-current-source' should be used in init function etc.
  1158. (if (and (boundp 'anything-source-name) (stringp anything-source-name))
  1159. source
  1160. (with-current-buffer (anything-buffer-get)
  1161. (or
  1162. ;; This happen only when `anything-source-in-each-line-flag'
  1163. ;; is non--nil and there is candidates in buffer.
  1164. (get-text-property (point) 'anything-source)
  1165. ;; Return nil when no--candidates.
  1166. (block exit
  1167. ;; This goto-char shouldn't be necessary, but point is moved to
  1168. ;; point-min somewhere else which shouldn't happen.
  1169. (goto-char (overlay-start anything-selection-overlay))
  1170. (let* ((header-pos (or (anything-get-previous-header-pos)
  1171. (anything-get-next-header-pos)))
  1172. (source-name
  1173. (save-excursion
  1174. (unless header-pos
  1175. (return-from exit nil))
  1176. (goto-char header-pos)
  1177. (anything-current-line-contents))))
  1178. (loop for source in (anything-get-sources) thereis
  1179. (and (equal (assoc-default 'name source) source-name)
  1180. source))))))))
  1181. (defun anything-buffer-is-modified (buffer)
  1182. "Return non-nil when BUFFER is modified since `anything' was invoked."
  1183. (let* ((b (get-buffer buffer))
  1184. (key (concat (buffer-name b) "/" (anything-attr 'name)))
  1185. (source-tick (or (gethash key anything-tick-hash) 0))
  1186. (buffer-tick (buffer-chars-modified-tick b))
  1187. (modifiedp (/= source-tick buffer-tick)))
  1188. (puthash key buffer-tick anything-tick-hash)
  1189. (anything-log-eval buffer modifiedp)
  1190. modifiedp))
  1191. (defun anything-current-buffer-is-modified ()
  1192. "Check if `anything-current-buffer' is modified since `anything' was invoked."
  1193. (anything-buffer-is-modified anything-current-buffer))
  1194. (defvar anything-quit nil)
  1195. (defun anything-run-after-quit (function &rest args)
  1196. "Perform an action after quitting `anything'.
  1197. The action is to call FUNCTION with arguments ARGS."
  1198. (setq anything-quit t)
  1199. (anything-kill-async-processes)
  1200. (anything-log-eval function args)
  1201. (apply 'run-with-idle-timer 0 nil function args)
  1202. (anything-exit-minibuffer))
  1203. (defun define-anything-type-attribute (type definition &optional doc)
  1204. "Register type attribute of TYPE as DEFINITION with DOC.
  1205. DOC is displayed in `anything-type-attributes' docstring.
  1206. Use this function is better than setting `anything-type-attributes' directly."
  1207. (loop for i in definition do
  1208. ;; without `ignore-errors', error at emacs22
  1209. (ignore-errors (setf i (delete nil i))))
  1210. (anything-add-type-attribute type definition)
  1211. (and doc (anything-document-type-attribute type doc))
  1212. nil)
  1213. (defvaralias 'anything-attributes 'anything-additional-attributes)
  1214. (defvar anything-additional-attributes nil
  1215. "List of all `anything' attributes.")
  1216. (defun anything-document-attribute (attribute short-doc &optional long-doc)
  1217. "Register ATTRIBUTE documentation introduced by plug-in.
  1218. SHORT-DOC is displayed beside attribute name.
  1219. LONG-DOC is displayed below attribute name and short documentation."
  1220. (if long-doc
  1221. (setq short-doc (concat "(" short-doc ")"))
  1222. (setq long-doc short-doc
  1223. short-doc ""))
  1224. (add-to-list 'anything-additional-attributes attribute t)
  1225. (put attribute 'anything-attrdoc
  1226. (concat "- " (symbol-name attribute)
  1227. " " short-doc "\n\n" long-doc "\n")))
  1228. (put 'anything-document-attribute 'lisp-indent-function 2)
  1229. (defun anything-interpret-value (value &optional source)
  1230. "Interpret VALUE as variable, function or literal.
  1231. If VALUE is a function, call it with no arguments and return the value.
  1232. If SOURCE is `anything' source, `anything-source-name' is source name.
  1233. If VALUE is a variable, return the value.
  1234. If VALUE is a symbol, but it is not a function or a variable, cause an error.
  1235. Otherwise, return VALUE itself."
  1236. (cond ((and source (functionp value))
  1237. (anything-funcall-with-source source value))
  1238. ((functionp value)
  1239. (funcall value))
  1240. ((and (symbolp value) (boundp value))
  1241. (symbol-value value))
  1242. ((symbolp value)
  1243. (error "anything-interpret-value: Symbol must be a function or a variable"))
  1244. (t
  1245. value)))
  1246. (defun anything-once (function &rest args)
  1247. "Ensure FUNCTION with ARGS to be called once in `anything' session."
  1248. (let ((spec (cons function args)))
  1249. (unless (member spec anything-once-called-functions)
  1250. (apply function args)
  1251. (push spec anything-once-called-functions))))
  1252. ;; (@* "Core: API helper")
  1253. (defun* anything-empty-buffer-p (&optional (buffer anything-buffer))
  1254. "Check if BUFFER have candidates.
  1255. Default value for BUFFER is `anything-buffer'."
  1256. (zerop (buffer-size (and buffer (get-buffer buffer)))))
  1257. (defun anything-let-eval-varlist (varlist)
  1258. "Return the list of pairs VARLIST with each cdr of pair evaluated.
  1259. If VARLIST contain single elements, those are returned
  1260. as a list of one element."
  1261. (mapcar (lambda (pair)
  1262. (if (listp pair)
  1263. (cons (car pair) (eval (cadr pair)))
  1264. (cons pair nil)))
  1265. varlist))
  1266. ;; [NOT USED]
  1267. ;; (defun anything-let*-eval-varlist (varlist)
  1268. ;; (let ((vars (mapcar (lambda (pair)
  1269. ;; (or (car-safe pair) pair))
  1270. ;; varlist)))
  1271. ;; (eval `(let ,vars
  1272. ;; ,@(mapcar (lambda (pair)
  1273. ;; (if (listp pair)
  1274. ;; `(setq ,(car pair) ,(cadr pair))
  1275. ;; `(setq ,pair nil)))
  1276. ;; varlist)
  1277. ;; (mapcar (lambda (v)
  1278. ;; (cons v (symbol-value v)))
  1279. ;; ',vars)))))
  1280. (defun anything-let-internal (binding bodyfunc)
  1281. "Set BINDING to anything buffer-local variables and Evaluate BODYFUNC.
  1282. BINDING is a list of \(VARNAME . VALUE\) pair."
  1283. (setq anything-let-variables binding)
  1284. (unwind-protect
  1285. (funcall bodyfunc)
  1286. (setq anything-let-variables nil)))
  1287. ;; (@* "Core: tools")
  1288. (defun anything-current-line-contents ()
  1289. "Current line string without properties."
  1290. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
  1291. (defun anything-funcall-with-source (source func &rest args)
  1292. "Call from SOURCE FUNC list or single function FUNC with ARGS.
  1293. FUNC can be a symbol or a list of functions.
  1294. Return the result of last function call."
  1295. (let ((anything-source-name (assoc-default 'name source))
  1296. result)
  1297. (anything-log-eval anything-source-name func args)
  1298. (dolist (func (if (functionp func) (list func) func) result)
  1299. (setq result (apply func args)))))
  1300. (defun anything-funcall-foreach (sym)
  1301. "Call the function SYM for each source if any."
  1302. (dolist (source (anything-get-sources))
  1303. (anything-aif (assoc-default sym source)
  1304. (anything-funcall-with-source source it))))
  1305. (defun anything-normalize-sources (sources)
  1306. "If SOURCES is only one source, make a list of one element."
  1307. (cond ((or (and sources
  1308. (symbolp sources))
  1309. (and (listp sources) (assq 'name sources)))
  1310. (list sources))
  1311. (sources)
  1312. (t anything-sources)))
  1313. (defun anything-approximate-candidate-number (&optional in-current-source)
  1314. "Return approximate number of candidates in `anything-buffer'.
  1315. If IN-CURRENT-SOURCE is provided return number of candidates
  1316. in the source where point is.
  1317. It is used to check if candidate number is 0, 1, or 2+."
  1318. (with-current-buffer anything-buffer
  1319. (save-excursion
  1320. (if in-current-source
  1321. (goto-char (anything-get-previous-header-pos))
  1322. (goto-char (point-min)))
  1323. (forward-line 1)
  1324. (let ((count-multi 1))
  1325. (if (anything-pos-multiline-p)
  1326. (save-excursion
  1327. (loop while (and (not (if in-current-source
  1328. (save-excursion
  1329. (forward-line 2)
  1330. (or (anything-pos-header-line-p) (eobp)))
  1331. (eobp)))
  1332. (search-forward anything-candidate-separator nil t))
  1333. do (incf count-multi)
  1334. finally return count-multi))
  1335. (save-excursion
  1336. (loop with ln = 0
  1337. while (not (if in-current-source
  1338. (or (anything-pos-header-line-p) (eobp))
  1339. (eobp)))
  1340. unless (anything-pos-header-line-p)
  1341. do (incf ln)
  1342. do (forward-line 1) finally return ln)))))))
  1343. (defmacro with-anything-quittable (&rest body)
  1344. "If an error occur in execution of BODY, quit anything safely."
  1345. (declare (indent 0) (debug t))
  1346. `(let (inhibit-quit)
  1347. (condition-case v
  1348. (progn ,@body)
  1349. (quit (setq anything-quit t)
  1350. (exit-minibuffer)
  1351. (keyboard-quit)))))
  1352. (defun anything-compose (arg-lst func-lst)
  1353. "Apply arguments specified in ARG-LST with each function of FUNC-LST.
  1354. The result of each function will be the new `car' of ARG-LST.
  1355. This function allows easy sequencing of transformer functions."
  1356. (dolist (func func-lst)
  1357. (setcar arg-lst (apply func arg-lst)))
  1358. (car arg-lst))
  1359. (defun anything-composed-funcall-with-source (source funcs &rest args)
  1360. "With SOURCE apply `anything-funcall-with-source' with each FUNCS and ARGS.
  1361. This is used in transformers to modify candidates list."
  1362. (if (functionp funcs)
  1363. (apply 'anything-funcall-with-source source funcs args)
  1364. (apply 'anything-funcall-with-source source
  1365. (lambda (&rest args)
  1366. (anything-compose args funcs))
  1367. args)))
  1368. (defun anything-new-timer (variable timer)
  1369. "Give VARIABLE value to TIMER and cancel old timer."
  1370. (anything-aif (symbol-value variable)
  1371. (cancel-timer it))
  1372. (set variable timer))
  1373. ;; (@* "Core: entry point")
  1374. (defconst anything-argument-keys
  1375. '(:sources :input :prompt :resume :preselect :buffer :keymap :default :history))
  1376. ;;;###autoload
  1377. (defun anything (&rest plist)
  1378. "Main function to execute anything sources.
  1379. Keywords supported:
  1380. :sources :input :prompt :resume :preselect :buffer :keymap :default :history
  1381. Extra keywords are supported and can be added, see below.
  1382. When call interactively with no arguments deprecated `anything-sources'
  1383. will be used if non--nil.
  1384. PLIST is a list like \(:key1 val1 :key2 val2 ...\) or
  1385. \(&optional sources input prompt resume preselect buffer keymap default history\).
  1386. Basic keywords are the following:
  1387. \:sources
  1388. Temporary value of `anything-sources'. It also accepts a
  1389. symbol, interpreted as a variable of an anything source. It
  1390. also accepts an alist representing an anything source, which is
  1391. detected by \(assq 'name ANY-SOURCES\)
  1392. \:input
  1393. Temporary value of `anything-pattern', ie. initial input of minibuffer.
  1394. \:prompt
  1395. Prompt other than \"pattern: \".
  1396. \:resume
  1397. If t, Resurrect previously instance of `anything'. Skip the initialization.
  1398. If 'noresume, this instance of `anything' cannot be resumed.
  1399. \:preselect
  1400. Initially selected candidate. Specified by exact candidate or a regexp.
  1401. \:buffer
  1402. `anything-buffer' instead of *anything*.
  1403. \:keymap
  1404. `anything-map' for current `anything' session.
  1405. \:default
  1406. A default argument that will be inserted in minibuffer \
  1407. with \\<minibuffer-local-map>\\[next-history-element].
  1408. When nil of not present `thing-at-point' will be used instead.
  1409. \:history
  1410. By default all minibuffer input is pushed to `minibuffer-history',
  1411. if an argument HISTORY is provided, input will be pushed to HISTORY.
  1412. History element should be a symbol.
  1413. Of course, conventional arguments are supported, the two are same.
  1414. \(anything :sources sources :input input :prompt prompt :resume resume
  1415. :preselect preselect :buffer buffer :keymap keymap :default default
  1416. :history history\)
  1417. \(anything sources input prompt resume preselect buffer keymap default history\)
  1418. Other keywords are interpreted as local variables of this anything session.
  1419. The `anything-' prefix can be omitted. For example,
  1420. \(anything :sources 'anything-c-source-buffers
  1421. :buffer \"*buffers*\" :candidate-number-limit 10\)
  1422. means starting anything session with `anything-c-source-buffers'
  1423. source in *buffers* buffer and set variable `anything-candidate-number-limit'
  1424. to 10 as session local variable."
  1425. (interactive)
  1426. (if (keywordp (car plist))
  1427. (anything-let-internal
  1428. (anything-parse-keys plist)
  1429. (lambda ()
  1430. (apply 'anything
  1431. (mapcar (lambda (key) (plist-get plist key))
  1432. anything-argument-keys))))
  1433. (apply 'anything-internal plist)))
  1434. (defun anything-parse-keys (keys)
  1435. "Parse the KEYS arguments of `anything'.
  1436. Return only the keys that are not in `anything-argument-keys'.
  1437. It is used to set local variables via `anything-let-internal'.
  1438. This allow to add arguments that are not part of `anything-argument-keys',
  1439. but are valid anything attributes.
  1440. i.e :candidate-number-limit will be bound to `anything-candidate-number-limit'
  1441. in source."
  1442. ;; (anything-parse-keys '(:sources ((name . "test")
  1443. ;; (candidates . (a b c)))
  1444. ;; :buffer "toto"
  1445. ;; :candidate-number-limit 4))
  1446. ;; ==> ((anything-candidate-number-limit . 4))
  1447. (loop for (key value) on keys by #'cddr
  1448. for symname = (substring (symbol-name key) 1)
  1449. for sym = (intern (if (string-match "^anything-" symname)
  1450. symname
  1451. (concat "anything-" symname)))
  1452. unless (memq key anything-argument-keys)
  1453. collect (cons sym value)))
  1454. ;;; (@* "Core: entry point helper")
  1455. (defun anything-internal (&optional
  1456. any-sources any-input
  1457. any-prompt any-resume
  1458. any-preselect any-buffer
  1459. any-keymap any-default any-history)
  1460. "The internal anything function called by `anything'.
  1461. For ANY-SOURCES ANY-INPUT ANY-PROMPT ANY-RESUME ANY-PRESELECT ANY-BUFFER and
  1462. ANY-KEYMAP ANY-DEFAULT ANY-HISTORY See `anything'."
  1463. (anything-log (concat "[Start session] " (make-string 41 ?+)))
  1464. (anything-log-eval any-prompt any-preselect
  1465. any-buffer any-keymap any-default)
  1466. (let ((old-overridding-local-map overriding-local-map))
  1467. (unwind-protect
  1468. (condition-case v
  1469. (let ( ;; It is needed because `anything-source-name' is non-nil
  1470. ;; when `anything' is invoked by action. Awful global scope.
  1471. anything-source-name
  1472. anything-in-persistent-action
  1473. anything-quit
  1474. (case-fold-search t)
  1475. (anything-buffer (or any-buffer anything-buffer))
  1476. ;; cua-mode ; avoid error when region is selected
  1477. )
  1478. (with-anything-restore-variables
  1479. (anything-initialize any-resume any-input any-sources)
  1480. (anything-display-buffer anything-buffer)
  1481. (anything-log "show prompt")
  1482. (unwind-protect
  1483. (anything-read-pattern-maybe
  1484. any-prompt any-input any-preselect
  1485. any-resume any-keymap any-default
  1486. (when (and any-history (symbolp any-history)) any-history))
  1487. (anything-cleanup)))
  1488. (prog1 (unless anything-quit
  1489. (anything-execute-selection-action-1))
  1490. (anything-log (concat "[End session] " (make-string 41 ?-)))))
  1491. (quit
  1492. (anything-restore-position-on-quit)
  1493. (anything-log (concat "[End session (quit)] " (make-string 34 ?-)))
  1494. nil))
  1495. (setq overriding-local-map old-overridding-local-map)
  1496. (anything-log-save-maybe))))
  1497. ;;; Anything resume
  1498. ;;
  1499. ;;
  1500. (defun* anything-resume (&optional
  1501. (any-buffer anything-last-buffer)
  1502. buffer-pattern (any-resume t))
  1503. "Resurrect previously invoked `anything'.
  1504. Called with a prefix arg, allow choosing among all existing
  1505. anything buffers. i.e choose among various anything sessions."
  1506. (interactive)
  1507. (when (or current-prefix-arg buffer-pattern)
  1508. (setq any-buffer (anything-resume-select-buffer buffer-pattern)))
  1509. (setq anything-compiled-sources nil)
  1510. (anything
  1511. :sources (or (buffer-local-value 'anything-last-sources-local (get-buffer any-buffer))
  1512. anything-last-sources anything-sources)
  1513. :input (buffer-local-value 'anything-input-local (get-buffer any-buffer))
  1514. :resume any-resume
  1515. :buffer any-buffer))
  1516. ;;; rubikitch: experimental
  1517. ;;; I use this and check it whether I am convenient.
  1518. ;;; I may introduce an option to control the behavior.
  1519. (defun* anything-resume-window-only (&optional
  1520. (any-buffer anything-last-buffer)
  1521. buffer-pattern)
  1522. (interactive)
  1523. (anything-resume any-buffer buffer-pattern 'window-only))
  1524. (defun anything-resume-p (any-resume)
  1525. "Whether current anything session is resumed or not.
  1526. Just check if ANY-RESUME value is t or window-only."
  1527. (memq any-resume '(t window-only)))
  1528. (defun anything-resume-select-buffer (input)
  1529. "Resume precedent anything session with initial input INPUT."
  1530. (or (anything :sources '(((name . "Resume anything buffer")
  1531. (candidates . anything-buffers)
  1532. (action . identity)))
  1533. :input input
  1534. :resume 'noresume
  1535. :buffer "*anything resume*")
  1536. (keyboard-quit)))
  1537. ;;;###autoload
  1538. (defun anything-at-point (&optional
  1539. any-sources any-input
  1540. any-prompt any-resume
  1541. any-preselect any-buffer)
  1542. "Call anything with symbol at point as initial input.
  1543. ANY-SOURCES ANY-INPUT ANY-PROMPT ANY-RESUME ANY-PRESELECT and ANY-BUFFER
  1544. are same args as in `anything'."
  1545. (interactive)
  1546. (anything :sources any-sources
  1547. :input (if current-prefix-arg
  1548. (concat "\\b" (thing-at-point 'symbol) "\\b"
  1549. (if (featurep 'anything-match-plugin) " " ""))
  1550. any-input)
  1551. :prompt any-prompt
  1552. :resume any-resume
  1553. :preselect any-preselect
  1554. :buffer any-buffer))
  1555. ;;;###autoload
  1556. (defun anything-other-buffer (any-sources any-buffer)
  1557. "Simplified interface of `anything' with other `anything-buffer'.
  1558. Call `anything' with only ANY-SOURCES and ANY-BUFFER as args."
  1559. (anything :sources any-sources :buffer any-buffer))
  1560. (defun anything-nest (&rest same-as-anything)
  1561. "Allow calling `anything' whithin a running anything session."
  1562. (with-anything-window
  1563. (let (anything-current-position
  1564. anything-current-buffer
  1565. (orig-anything-current-buffer anything-current-buffer)
  1566. (orig-anything-buffer anything-buffer)
  1567. (orig-anything-last-frame-or-window-configuration
  1568. anything-last-frame-or-window-configuration)
  1569. anything-pattern
  1570. (anything-buffer (or (getf same-as-anything :buffer)
  1571. (nth 5 same-as-anything)
  1572. "*Anything*"))
  1573. anything-sources
  1574. anything-compiled-sources
  1575. (anything-samewindow t)
  1576. (enable-recursive-minibuffers t))
  1577. (unwind-protect
  1578. (apply #'anything same-as-anything)
  1579. (with-current-buffer orig-anything-buffer
  1580. (anything-initialize-overlays orig-anything-buffer)
  1581. (setq anything-buffer (current-buffer))
  1582. (anything-mark-current-line)
  1583. (setq anything-last-frame-or-window-configuration
  1584. orig-anything-last-frame-or-window-configuration)
  1585. (setq cursor-type t)
  1586. (setq anything-current-buffer orig-anything-current-buffer))))))
  1587. ;;; Initialize
  1588. ;;
  1589. ;;
  1590. (defvar anything-buffers nil
  1591. "All of `anything-buffer' in most recently used order.")
  1592. (defun anything-initialize (any-resume any-input any-sources)
  1593. "Start initialization of `anything' session.
  1594. For ANY-RESUME ANY-INPUT and ANY-SOURCES See `anything'."
  1595. (anything-log "start initialization: any-resume=%S any-input=%S" any-resume any-input)
  1596. (anything-frame-or-window-configuration 'save)
  1597. (setq anything-sources (anything-normalize-sources any-sources))
  1598. (anything-log "sources = %S" anything-sources)
  1599. (anything-hooks 'setup)
  1600. (anything-current-position 'save)
  1601. (if (anything-resume-p any-resume)
  1602. (anything-initialize-overlays (anything-buffer-get))
  1603. (anything-initial-setup))
  1604. (unless (eq any-resume 'noresume)
  1605. (anything-recent-push anything-buffer 'anything-buffers)
  1606. (setq anything-last-buffer anything-buffer))
  1607. (when any-input (setq anything-input any-input anything-pattern any-input))
  1608. (and (anything-resume-p any-resume) (anything-funcall-foreach 'resume))
  1609. (anything-log "end initialization"))
  1610. (defun anything-execute-selection-action-1 ()
  1611. "Execute current action."
  1612. (anything-log-run-hook 'anything-before-action-hook)
  1613. (unwind-protect
  1614. (anything-execute-selection-action)
  1615. (anything-aif (get-buffer anything-action-buffer)
  1616. (kill-buffer it))
  1617. (anything-log-run-hook 'anything-after-action-hook)))
  1618. (defun anything-restore-position-on-quit ()
  1619. "Restore position in `anything-current-buffer' when quitting."
  1620. (anything-current-position 'restore))
  1621. (defun anything-recent-push (elt list-var)
  1622. "Add ELT to the value of LIST-VAR as most recently used value."
  1623. (let ((m (member elt (symbol-value list-var))))
  1624. (and m (set list-var (delq (car m) (symbol-value list-var))))
  1625. (push elt (symbol-value list-var))))
  1626. ;;; (@* "Core: Accessors")
  1627. ;;; rubikitch: I love to create functions to control variables.
  1628. (defvar anything-current-position nil
  1629. "Cons of \(point . window-start\) when `anything' is invoked.
  1630. It is needed to restore position in `anything-current-buffer'
  1631. when `anything' is keyboard-quitted.")
  1632. (defun anything-current-position (save-or-restore)
  1633. "Restore or save current position in `anything-current-buffer'.
  1634. Argument SAVE-OR-RESTORE is one of save or restore."
  1635. (case save-or-restore
  1636. (save
  1637. (anything-log "Save position at %S" (cons (point) (window-start)))
  1638. (setq anything-current-position (cons (point) (window-start))))
  1639. (restore
  1640. (anything-log "Restore position at %S in buffer %s"
  1641. anything-current-position
  1642. (buffer-name (current-buffer)))
  1643. (goto-char (car anything-current-position))
  1644. ;; Fix this position with the NOFORCE arg of `set-window-start'
  1645. ;; otherwise, if there is some other buffer than `anything-current-buffer'
  1646. ;; one, position will be lost.
  1647. (set-window-start (selected-window) (cdr anything-current-position) t))))
  1648. ;; Internal.
  1649. (defvar anything-last-frame-or-window-configuration nil
  1650. "Used to store window or frame configuration when anything start.")
  1651. (defun anything-frame-or-window-configuration (save-or-restore)
  1652. "Save or restore last frame or window configuration.
  1653. Possible value of SAVE-OR-RESTORE are 'save and 'restore.
  1654. window or frame configuration is saved/restored according to values of
  1655. `anything-save-configuration-functions'."
  1656. (anything-log-eval anything-save-configuration-functions)
  1657. (case save-or-restore
  1658. (save (setq anything-last-frame-or-window-configuration
  1659. (funcall (cdr anything-save-configuration-functions))))
  1660. (restore (funcall (car anything-save-configuration-functions)
  1661. anything-last-frame-or-window-configuration)
  1662. ;; Restore frame focus.
  1663. (let ((frame (and (listp anything-last-frame-or-window-configuration)
  1664. (caadr anything-last-frame-or-window-configuration))))
  1665. ;; If `anything-save-configuration-functions' are window functions
  1666. ;; frame should be nil, use current frame.
  1667. (unless (framep frame)
  1668. (setq frame (selected-frame)))
  1669. (select-frame-set-input-focus frame)))))
  1670. ;; (@* "Core: Display *anything* buffer")
  1671. (defun anything-display-buffer (buf)
  1672. "Display *anything* buffer BUF."
  1673. (let (pop-up-frames)
  1674. (funcall (with-current-buffer buf anything-display-function) buf)))
  1675. (defun anything-default-display-buffer (buf)
  1676. "Default function to display BUF.
  1677. Where BUF is generally `anything-buffer'.
  1678. It use `switch-to-buffer' or `pop-to-buffer' depending of value of
  1679. `anything-samewindow'."
  1680. (funcall (if anything-samewindow 'switch-to-buffer 'pop-to-buffer) buf))
  1681. ;; (@* "Core: initialize")
  1682. (defun anything-initial-setup ()
  1683. "Initialize anything settings and set up the anything buffer."
  1684. (anything-log-run-hook 'anything-before-initialize-hook)
  1685. (setq anything-current-prefix-arg nil)
  1686. (setq anything-once-called-functions nil)
  1687. (setq anything-delayed-init-executed nil)
  1688. (setq anything-alive-p t)
  1689. (setq anything-current-buffer
  1690. (if (minibuffer-window-active-p (minibuffer-window))
  1691. ;; If minibuffer is active be sure to use it's buffer
  1692. ;; as `anything-current-buffer'.
  1693. (window-buffer (active-minibuffer-window))
  1694. (current-buffer)))
  1695. (setq anything-buffer-file-name buffer-file-name)
  1696. (setq anything-issued-errors nil)
  1697. (setq anything-compiled-sources nil)
  1698. (setq anything-saved-current-source nil)
  1699. (if (or (not split-width-threshold)
  1700. (and (integerp split-width-threshold)
  1701. (>= split-width-threshold (+ (frame-width) 4))))
  1702. (setq anything-split-window-state 'vertical)
  1703. (setq anything-split-window-state 'horizontal))
  1704. ;; Call the init function for sources where appropriate
  1705. (anything-funcall-foreach 'init)
  1706. (setq anything-pattern "")
  1707. (setq anything-input "")
  1708. (setq anything-candidate-cache nil)
  1709. (setq anything-last-sources anything-sources)
  1710. (anything-create-anything-buffer)
  1711. (anything-log-run-hook 'anything-after-initialize-hook))
  1712. (defvar anything-reading-pattern nil
  1713. "Whether in `read-string' in anything or not.")
  1714. (defun anything-read-pattern-maybe (any-prompt any-input
  1715. any-preselect any-resume any-keymap
  1716. any-default any-history)
  1717. "Read pattern with prompt ANY-PROMPT and initial input ANY-INPUT.
  1718. For ANY-PRESELECT ANY-RESUME ANY-KEYMAP, See `anything'."
  1719. (if (anything-resume-p any-resume)
  1720. (anything-mark-current-line t)
  1721. (anything-update any-preselect))
  1722. (with-current-buffer (anything-buffer-get)
  1723. (let ((src-keymap (assoc-default 'keymap (anything-get-current-source))))
  1724. ;; Startup with the first keymap found either in current source
  1725. ;; or anything arg, otherwise use global value of `anything-map'.
  1726. ;; This map will be used as a `minibuffer-local-map'.
  1727. ;; Maybe it will be overriden when changing source
  1728. ;; by `anything-maybe-update-keymap'.
  1729. (unless anything-local-map-override-anything-map
  1730. (anything-aif (or src-keymap any-keymap)
  1731. (ignore-errors
  1732. (set-keymap-parent it anything-map))))
  1733. (set (make-local-variable 'anything-map)
  1734. (or src-keymap any-keymap anything-map))
  1735. (anything-log-eval (anything-approximate-candidate-number)
  1736. anything-execute-action-at-once-if-one
  1737. anything-quit-if-no-candidate)
  1738. (cond ((and anything-execute-action-at-once-if-one
  1739. (= (anything-approximate-candidate-number) 1))
  1740. (ignore))
  1741. ((and anything-quit-if-no-candidate
  1742. (= (anything-approximate-candidate-number) 0))
  1743. (setq anything-quit t)
  1744. (and (functionp anything-quit-if-no-candidate)
  1745. (funcall anything-quit-if-no-candidate)))
  1746. (t
  1747. (let ((anything-reading-pattern t)
  1748. (tap (or any-default
  1749. (with-anything-current-buffer
  1750. (thing-at-point 'symbol)))))
  1751. (read-from-minibuffer (or any-prompt "pattern: ")
  1752. any-input anything-map
  1753. nil any-history tap)))))))
  1754. (defun anything-maybe-update-keymap ()
  1755. "Handle differents keymaps in multiples sources.
  1756. This function is meant to be run in `anything-move-selection-after-hook'.
  1757. It will override `anything-map' with the keymap attribute of current source
  1758. if some when multiples sources are present."
  1759. (with-anything-window
  1760. (let ((kmap (assoc-default 'keymap (anything-get-current-source))))
  1761. (when kmap
  1762. (and (not anything-local-map-override-anything-map)
  1763. (ignore-errors (set-keymap-parent kmap (default-value 'anything-map))))
  1764. (setq overriding-local-map kmap)))))
  1765. (add-hook 'anything-move-selection-after-hook 'anything-maybe-update-keymap)
  1766. (defun anything-create-anything-buffer (&optional test-mode)
  1767. "Create newly created `anything-buffer'.
  1768. If TEST-MODE is non-nil, clear `anything-candidate-cache'."
  1769. (when test-mode
  1770. (setq anything-candidate-cache nil))
  1771. (with-current-buffer (get-buffer-create anything-buffer)
  1772. (anything-log "kill local variables: %S" (buffer-local-variables))
  1773. (kill-all-local-variables)
  1774. (set (make-local-variable 'inhibit-read-only) t)
  1775. (buffer-disable-undo)
  1776. (erase-buffer)
  1777. (set (make-local-variable 'inhibit-read-only) t)
  1778. (set (make-local-variable 'anything-last-sources-local) anything-sources)
  1779. (set (make-local-variable 'anything-follow-mode) nil)
  1780. (set (make-local-variable 'anything-display-function) anything-display-function)
  1781. (set (make-local-variable 'anything-selection-point) nil)
  1782. (anything-initialize-persistent-action)
  1783. (anything-log-eval anything-display-function anything-let-variables)
  1784. (loop for (var . val) in anything-let-variables
  1785. do (set (make-local-variable var) val))
  1786. (setq cursor-type nil)
  1787. (setq mode-name "Anything"))
  1788. (anything-initialize-overlays anything-buffer)
  1789. (get-buffer anything-buffer))
  1790. (defun anything-initialize-overlays (buffer)
  1791. "Initialize anything overlays in BUFFER."
  1792. (anything-log "overlay setup")
  1793. (if anything-selection-overlay
  1794. ;; make sure the overlay belongs to the anything buffer if
  1795. ;; it's newly created
  1796. (move-overlay anything-selection-overlay (point-min) (point-min)
  1797. (get-buffer buffer))
  1798. (setq anything-selection-overlay
  1799. (make-overlay (point-min) (point-min) (get-buffer buffer)))
  1800. (overlay-put anything-selection-overlay 'face anything-selection-face))
  1801. (cond (anything-enable-shortcuts
  1802. (setq anything-shortcut-keys
  1803. (assoc-default anything-enable-shortcuts anything-shortcut-keys-alist))
  1804. (unless anything-digit-overlays
  1805. (setq anything-digit-overlays
  1806. (loop for key across anything-shortcut-keys
  1807. for overlay = (make-overlay (point-min) (point-min)
  1808. (get-buffer buffer))
  1809. do (overlay-put overlay 'before-string
  1810. (format "%s - " (upcase (make-string 1 key))))
  1811. collect overlay))))
  1812. (anything-digit-overlays
  1813. (mapc 'delete-overlay anything-digit-overlays)
  1814. (setq anything-digit-overlays nil))))
  1815. (defun anything-hooks (setup-or-cleanup)
  1816. "Add or remove hooks according to SETUP-OR-CLEANUP value.
  1817. if SETUP-OR-CLEANUP value is setup add hooks, any other value
  1818. will remove hooks.
  1819. hooks concerned are `post-command-hook' and `minibuffer-setup-hook'."
  1820. (let ((hooks '((post-command-hook anything-check-minibuffer-input)
  1821. (minibuffer-setup-hook anything-print-error-messages))))
  1822. (if (eq setup-or-cleanup 'setup)
  1823. (dolist (args hooks) (apply 'add-hook args))
  1824. (dolist (args (reverse hooks)) (apply 'remove-hook args)))))
  1825. ;; (@* "Core: clean up")
  1826. ;;; TODO move
  1827. (defun anything-cleanup ()
  1828. "Clean up the mess when anything exit or quit."
  1829. (anything-log "start cleanup")
  1830. (with-current-buffer anything-buffer
  1831. ;; rubikitch: I think it is not needed.
  1832. ;; thierry: If you end up for any reasons (error etc...)
  1833. ;; with an anything-buffer staying around (visible),
  1834. ;; You will have no cursor in this buffer when switching to it,
  1835. ;; so I think this is needed.
  1836. (setq cursor-type t)
  1837. ;; Call burry-buffer whithout arg
  1838. ;; to be sure anything-buffer is removed from window.
  1839. (bury-buffer)
  1840. ;; Be sure we call this from anything-buffer.
  1841. (anything-funcall-foreach 'cleanup))
  1842. (anything-new-timer 'anything-check-minibuffer-input-timer nil)
  1843. (anything-kill-async-processes)
  1844. (anything-log-run-hook 'anything-cleanup-hook)
  1845. (anything-hooks 'cleanup)
  1846. (anything-frame-or-window-configuration 'restore)
  1847. (setq anything-alive-p nil)
  1848. ;; This is needed in some cases where last input
  1849. ;; is yielded infinitely in minibuffer after anything session.
  1850. (anything-clean-up-minibuffer))
  1851. (defun anything-clean-up-minibuffer ()
  1852. "Remove contents of minibuffer."
  1853. (let ((miniwin (minibuffer-window)))
  1854. ;; Clean only current minibuffer used by anything.
  1855. ;; i.e The precedent one is active.
  1856. (unless (minibuffer-window-active-p miniwin)
  1857. (with-current-buffer (window-buffer miniwin)
  1858. (delete-minibuffer-contents)))))
  1859. ;; (@* "Core: input handling")
  1860. (defun anything-check-minibuffer-input ()
  1861. "Extract input string from the minibuffer and check if it needs to be handled."
  1862. (let ((delay (with-current-buffer anything-buffer
  1863. (and anything-input-idle-delay
  1864. (max anything-input-idle-delay 0.1)))))
  1865. (if (or (not delay) (anything-action-window))
  1866. (anything-check-minibuffer-input-1)
  1867. (anything-new-timer
  1868. 'anything-check-minibuffer-input-timer
  1869. (run-with-idle-timer delay nil 'anything-check-minibuffer-input-1)))))
  1870. (defun anything-check-minibuffer-input-1 ()
  1871. "Check minibuffer content."
  1872. (with-anything-quittable
  1873. (with-selected-window (or (active-minibuffer-window) (minibuffer-window))
  1874. (anything-check-new-input (minibuffer-contents)))))
  1875. (defun anything-check-new-input (input)
  1876. "Check INPUT string and update the anything buffer if necessary."
  1877. (unless (equal input anything-pattern)
  1878. (setq anything-pattern input)
  1879. (unless (anything-action-window)
  1880. (setq anything-input anything-pattern))
  1881. (anything-log-eval anything-pattern anything-input)
  1882. (anything-update)))
  1883. ;; (@* "Core: source compiler")
  1884. (defvar anything-compile-source-functions-default anything-compile-source-functions
  1885. "Plug-ins this file provides.")
  1886. (defun anything-compile-sources (sources funcs)
  1887. "Compile SOURCES with FUNCS.
  1888. See `anything-compile-source-functions'.
  1889. Anything plug-ins are realized by this function."
  1890. (mapcar
  1891. (lambda (source)
  1892. (loop with source = (if (listp source) source (symbol-value source))
  1893. for f in funcs
  1894. do (setq source (funcall f source))
  1895. finally (return source)))
  1896. sources))
  1897. ;; (@* "Core: plug-in attribute documentation hack")
  1898. ;; `anything-document-attribute' is public API.
  1899. (defadvice documentation-property (after anything-document-attribute activate)
  1900. "Display plug-in attributes' documentation as `anything-sources' docstring."
  1901. (when (eq (ad-get-arg 0) 'anything-sources)
  1902. (setq ad-return-value
  1903. (concat ad-return-value "\n"
  1904. (mapconcat (lambda (sym) (get sym 'anything-attrdoc))
  1905. anything-additional-attributes
  1906. "\n")))))
  1907. ;; (describe-variable 'anything-sources)
  1908. ;; (documentation-property 'anything-sources 'variable-documentation)
  1909. ;; (progn (ad-disable-advice 'documentation-property 'after 'anything-document-attribute) (ad-update 'documentation-property))
  1910. ;; (@* "Core: all candidates")
  1911. (defun anything-process-delayed-init (source)
  1912. "Initialize delayed SOURCE."
  1913. (let ((name (assoc-default 'name source)))
  1914. (unless (member name anything-delayed-init-executed)
  1915. (anything-aif (assoc-default 'delayed-init source)
  1916. (with-current-buffer anything-current-buffer
  1917. (anything-funcall-with-source source it)
  1918. (dolist (f (if (functionp it) (list it) it))
  1919. (add-to-list 'anything-delayed-init-executed name)))))))
  1920. (defun anything-get-candidates (source)
  1921. "Retrieve and return the list of candidates from SOURCE."
  1922. (anything-process-delayed-init source)
  1923. (let* ((candidate-source (assoc-default 'candidates source))
  1924. (type-error (lambda ()
  1925. (error (concat "Candidates must either be a function, "
  1926. " a variable or a list: %s")
  1927. candidate-source)))
  1928. (candidates (condition-case err
  1929. (anything-interpret-value candidate-source source)
  1930. (error (funcall type-error)))))
  1931. (cond ((processp candidates) candidates)
  1932. ((listp candidates) (anything-transform-candidates candidates source))
  1933. (t (funcall type-error)))))
  1934. (defun anything-get-cached-candidates (source)
  1935. "Return the cached value of candidates for SOURCE.
  1936. Cache the candidates if there is not yet a cached value."
  1937. (let* ((name (assoc-default 'name source))
  1938. (candidate-cache (assoc name anything-candidate-cache)))
  1939. (cond (candidate-cache
  1940. (anything-log "use cached candidates")
  1941. (cdr candidate-cache))
  1942. (t
  1943. (anything-log "calculate candidates")
  1944. (let ((candidates (anything-get-candidates source)))
  1945. (cond ((processp candidates)
  1946. (push (cons candidates
  1947. (append source
  1948. (list (cons 'item-count 0)
  1949. (cons 'incomplete-line ""))))
  1950. anything-async-processes)
  1951. (set-process-filter candidates 'anything-output-filter)
  1952. (setq candidates nil))
  1953. ((not (assoc 'volatile source))
  1954. (setq candidate-cache (cons name candidates))
  1955. (push candidate-cache anything-candidate-cache)))
  1956. candidates)))))
  1957. ;;; (@* "Core: candidate transformers")
  1958. (defun anything-transform-mapcar (function args)
  1959. "`mapcar' for candidate-transformer.
  1960. ARGS is (cand1 cand2 ...) or ((disp1 . real1) (disp2 . real2) ...)
  1961. \(anything-transform-mapcar 'upcase '(\"foo\" \"bar\"))
  1962. => (\"FOO\" \"BAR\")
  1963. \(anything-transform-mapcar 'upcase '((\"1st\" . \"foo\") (\"2nd\" . \"bar\")))
  1964. => ((\"1st\" . \"FOO\") (\"2nd\" . \"BAR\"))
  1965. "
  1966. (loop for arg in args
  1967. if (consp arg)
  1968. collect (cons (car arg) (funcall function (cdr arg)))
  1969. else
  1970. collect (funcall function arg)))
  1971. (defun anything-process-candidate-transformer (candidates source)
  1972. "Execute candidate-transformer function on all CANDIDATES of SOURCE."
  1973. (anything-aif (assoc-default 'candidate-transformer source)
  1974. (anything-composed-funcall-with-source source it candidates)
  1975. candidates))
  1976. (defun anything-process-filtered-candidate-transformer (candidates source)
  1977. "Execute filtered-candidate-transformer function on all CANDIDATES of SOURCE."
  1978. (anything-aif (assoc-default 'filtered-candidate-transformer source)
  1979. (anything-composed-funcall-with-source source it candidates source)
  1980. candidates))
  1981. (defun anything-process-filtered-candidate-transformer-maybe (candidates source process-p)
  1982. "Execute filtered-candidate-transformer function on all CANDIDATES of SOURCE.
  1983. This happen if PROCESS-P is non-nil."
  1984. (if process-p
  1985. (anything-process-filtered-candidate-transformer candidates source)
  1986. candidates))
  1987. (defun anything-process-real-to-display (candidates source)
  1988. "Execute real-to-display function on all CANDIDATES of SOURCE."
  1989. (anything-aif (assoc-default 'real-to-display source)
  1990. (setq candidates (anything-funcall-with-source
  1991. source 'mapcar
  1992. (lambda (cand_)
  1993. (if (consp cand_)
  1994. ;; override DISPLAY from candidate-transformer
  1995. (cons (funcall it (cdr cand_)) (cdr cand_))
  1996. (cons (funcall it cand_) cand_)))
  1997. candidates))
  1998. candidates))
  1999. (defun anything-transform-candidates (candidates source &optional process-p)
  2000. "Transform CANDIDATES of SOURCE according to candidate transformers.
  2001. This happen if PROCESS-P is non-nil."
  2002. (anything-process-real-to-display
  2003. (anything-process-filtered-candidate-transformer-maybe
  2004. (anything-process-candidate-transformer candidates source) source process-p)
  2005. source))
  2006. ;; (@* "Core: narrowing candidates")
  2007. (defun anything-candidate-number-limit (source)
  2008. "Apply candidate-number-limit attribute value.
  2009. This overhide variable `anything-candidate-number-limit'.
  2010. e.g:
  2011. If \(candidate-number-limit\) is in SOURCE, show all candidates in SOURCE.
  2012. If \(candidate-number-limit . 123\) is in SOURCE limit candidate to 123."
  2013. (anything-aif (assq 'candidate-number-limit source)
  2014. (or (cdr it) 99999999)
  2015. (or anything-candidate-number-limit 99999999)))
  2016. ;; FIXME: Why a defconst here
  2017. (defconst anything-default-match-functions
  2018. (list (lambda (candidate)
  2019. (string-match anything-pattern candidate)))
  2020. "Default functions to match candidates according to `anything-pattern'.")
  2021. (defun anything-compute-matches (source)
  2022. "Compute matched results from SOURCE according to its settings."
  2023. (if debug-on-error
  2024. (anything-compute-matches-internal source)
  2025. (condition-case v
  2026. (anything-compute-matches-internal source)
  2027. (error (anything-log-error
  2028. "anything-compute-matches: error when processing source: %s"
  2029. (assoc-default 'name source))
  2030. nil))))
  2031. (defun anything-candidate-get-display (candidate)
  2032. "Get searched display part from CANDIDATE.
  2033. CANDIDATE is a string, a symbol, or \(DISPLAY . REAL\) cons cell."
  2034. (format "%s" (or (car-safe candidate) candidate)))
  2035. (defun anything-process-pattern-transformer (pattern source)
  2036. "Execute pattern-transformer attribute PATTERN function in SOURCE."
  2037. (anything-aif (assoc-default 'pattern-transformer source)
  2038. (anything-composed-funcall-with-source source it pattern)
  2039. pattern))
  2040. (defun anything-match-functions (source)
  2041. (or (assoc-default 'match source)
  2042. anything-default-match-functions))
  2043. (defmacro anything-accumulate-candidates-internal (cand newmatches
  2044. hash item-count limit)
  2045. "Internal, add CAND into NEWMATCHES.
  2046. Use HASH to uniq NEWMATCHES.
  2047. Argument ITEM-COUNT count the matches.
  2048. if ITEM-COUNT reaches LIMIT, exit from inner loop."
  2049. `(unless (gethash ,cand ,hash)
  2050. (puthash ,cand t ,hash)
  2051. (push ,cand ,newmatches)
  2052. (incf ,item-count)
  2053. (when (= ,item-count ,limit)
  2054. (setq exit t)
  2055. (return))))
  2056. (defun anything-take-first-elements (seq n)
  2057. (if (> (length seq) n)
  2058. (setq seq (subseq seq 0 n))
  2059. seq))
  2060. (defun anything-match-from-candidates (cands matchfns limit)
  2061. (let (matches)
  2062. (condition-case nil
  2063. (let ((item-count 0) exit)
  2064. (clrhash anything-match-hash)
  2065. (dolist (match matchfns)
  2066. (let (newmatches)
  2067. (dolist (candidate cands)
  2068. (when (funcall match (anything-candidate-get-display candidate))
  2069. (anything-accumulate-candidates-internal
  2070. candidate newmatches anything-match-hash item-count limit)))
  2071. (setq matches (append matches (reverse newmatches)))
  2072. (if exit (return)))))
  2073. (invalid-regexp (setq matches nil)))
  2074. matches))
  2075. (defun anything-compute-matches-internal (source)
  2076. (save-current-buffer
  2077. (let ((matchfns (anything-match-functions source))
  2078. (anything-source-name (assoc-default 'name source))
  2079. (limit (anything-candidate-number-limit source))
  2080. (anything-pattern (anything-process-pattern-transformer
  2081. anything-pattern source)))
  2082. (anything-process-filtered-candidate-transformer
  2083. (if (or (equal anything-pattern "") (equal matchfns '(identity)))
  2084. (anything-take-first-elements
  2085. (anything-get-cached-candidates source) limit)
  2086. (anything-match-from-candidates
  2087. (anything-get-cached-candidates source) matchfns limit))
  2088. source))))
  2089. ;; (anything '(((name . "error")(candidates . (lambda () (hage))) (action . identity))))
  2090. (defun anything-process-source (source)
  2091. "Display matched results from SOURCE according to its settings."
  2092. (anything-log-eval (assoc-default 'name source))
  2093. (if (assq 'direct-insert-match source) ;experimental
  2094. (anything-process-source--direct-insert-match source)
  2095. (let ((matches (anything-compute-matches source)))
  2096. (when matches
  2097. (when anything-test-mode
  2098. (setq anything-test-candidate-list
  2099. `(,@anything-test-candidate-list
  2100. (,(assoc-default 'name source)
  2101. ,matches))))
  2102. (anything-insert-header-from-source source)
  2103. (if (not (assq 'multiline source))
  2104. (mapc 'anything-insert-match-with-digit-overlay matches)
  2105. (let ((start (point)) separate)
  2106. (dolist (match matches)
  2107. (if separate
  2108. (anything-insert-candidate-separator)
  2109. (setq separate t))
  2110. (anything-insert-match-with-digit-overlay match))
  2111. (put-text-property start (point) 'anything-multiline t)))))))
  2112. (defun anything-insert-match-with-digit-overlay (match)
  2113. (declare (special source))
  2114. (anything-put-digit-overlay-maybe)
  2115. (anything-insert-match match 'insert source))
  2116. (defun anything-put-digit-overlay-maybe ()
  2117. (when (and anything-enable-shortcuts
  2118. (not (eq anything-digit-shortcut-count
  2119. (length anything-digit-overlays))))
  2120. (move-overlay (nth anything-digit-shortcut-count
  2121. anything-digit-overlays)
  2122. (point-at-bol)
  2123. (point-at-bol))
  2124. (incf anything-digit-shortcut-count)))
  2125. (defun anything-process-source--direct-insert-match (source)
  2126. "[EXPERIMENTAL] Insert candidates from `anything-candidate-buffer' in SOURCE."
  2127. (anything-log-eval (assoc-default 'name source))
  2128. (let ((anything-source-name (assoc-default 'name source))
  2129. content-buf)
  2130. (funcall (assoc-default 'candidates source))
  2131. (setq content-buf (anything-candidate-buffer))
  2132. (unless (anything-empty-buffer-p content-buf)
  2133. (anything-insert-header-from-source source)
  2134. (insert-buffer-substring content-buf)
  2135. ;; TODO call anything-put-digit-overlay-maybe with loop
  2136. )))
  2137. (defun anything-process-delayed-sources (delayed-sources &optional preselect)
  2138. "Process anything DELAYED-SOURCES.
  2139. Move selection to string or regexp PRESELECT if non--nil.
  2140. This function is called in `anything-process-delayed-sources-timer'
  2141. when emacs is idle for `anything-idle-delay'."
  2142. (with-anything-quittable
  2143. (anything-log-eval (mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
  2144. (with-current-buffer anything-buffer
  2145. (save-excursion
  2146. (goto-char (point-max))
  2147. (mapc 'anything-process-source delayed-sources)
  2148. (when (and (not (anything-empty-buffer-p))
  2149. ;; No selection yet.
  2150. (= (overlay-start anything-selection-overlay)
  2151. (overlay-end anything-selection-overlay)))
  2152. (anything-update-move-first-line 'without-hook)))
  2153. (when preselect (anything-preselect preselect))
  2154. (save-excursion
  2155. (goto-char (point-min))
  2156. (anything-log-run-hook 'anything-update-hook))
  2157. (anything-log-run-hook 'anything-after-update-hook))))
  2158. ;; (@* "Core: *anything* buffer contents")
  2159. (defvar anything-input-local nil)
  2160. (defvar anything-process-delayed-sources-timer nil)
  2161. (defun anything-update (&optional preselect)
  2162. "Update candidates list in `anything-buffer' according to `anything-pattern'.
  2163. Argument PRESELECT is a string or regexp used to move selection to a particular
  2164. place once updating is done. It should be used on single source because search
  2165. is done on whole `anything-buffer' and not on current source."
  2166. (anything-log "start update")
  2167. (setq anything-digit-shortcut-count 0)
  2168. (anything-kill-async-processes)
  2169. (with-current-buffer (anything-buffer-get)
  2170. (set (make-local-variable 'anything-input-local) anything-pattern)
  2171. (erase-buffer)
  2172. (when anything-enable-shortcuts
  2173. (mapc 'delete-overlay anything-digit-overlays))
  2174. (let (delayed-sources
  2175. normal-sources)
  2176. (unwind-protect ; Process normal sources and store delayed one's.
  2177. (setq delayed-sources
  2178. (loop for source in (remove-if-not 'anything-update-source-p
  2179. (anything-get-sources))
  2180. if (anything-delayed-source-p source)
  2181. collect source
  2182. else do (progn (push source normal-sources)
  2183. (anything-process-source source))))
  2184. (anything-log-eval
  2185. (mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
  2186. (if anything-test-mode ; Need only to process sources.
  2187. (mapc 'anything-process-source delayed-sources)
  2188. (cond ((and preselect delayed-sources normal-sources)
  2189. ;; Preselection run here when there is
  2190. ;; normal AND delayed sources.
  2191. (anything-log "Update preselect candidate %s" preselect)
  2192. (anything-preselect preselect))
  2193. (delayed-sources ; Preselection and hooks will run later.
  2194. (anything-update-move-first-line 'without-hook))
  2195. (t ; No delayed sources, run the hooks now.
  2196. (anything-update-move-first-line)
  2197. (anything-log-run-hook 'anything-after-update-hook)
  2198. (when preselect
  2199. (anything-log "Update preselect candidate %s" preselect)
  2200. (anything-preselect preselect))))
  2201. (when delayed-sources
  2202. (anything-new-timer
  2203. 'anything-process-delayed-sources-timer
  2204. (run-with-idle-timer
  2205. ;; Be sure anything-idle-delay is >
  2206. ;; to anything-input-idle-delay
  2207. ;; otherwise use value of anything-input-idle-delay
  2208. ;; or 0.1 if == to 0.
  2209. (max anything-idle-delay anything-input-idle-delay 0.1) nil
  2210. 'anything-process-delayed-sources delayed-sources preselect))))
  2211. (anything-log "end update")))))
  2212. (defun anything-update-source-p (source)
  2213. "Wheter SOURCE need updating or not."
  2214. (and (or (not anything-source-filter)
  2215. (member (assoc-default 'name source) anything-source-filter))
  2216. (>= (length anything-pattern)
  2217. (anything-aif (assoc 'requires-pattern source)
  2218. (or (cdr it) 1)
  2219. 0))))
  2220. (defun anything-delayed-source-p (source)
  2221. "Wheter SOURCE is a delayed source or not."
  2222. (or (assoc 'delayed source)
  2223. (and anything-quick-update
  2224. (< (window-height (get-buffer-window (current-buffer)))
  2225. (line-number-at-pos (point-max))))))
  2226. (defun anything-update-move-first-line (&optional without-hook)
  2227. "Goto first line of `anything-buffer'."
  2228. (goto-char (point-min))
  2229. (unless without-hook
  2230. (save-excursion (anything-log-run-hook 'anything-update-hook)))
  2231. (anything-next-line))
  2232. (defun anything-force-update (&optional preselect)
  2233. "Force recalculation and update of candidates.
  2234. If current source has `update' attribute, a function without argument,
  2235. call it before update."
  2236. (interactive)
  2237. (let ((source (anything-get-current-source))
  2238. (selection (anything-get-selection nil t)))
  2239. (when source
  2240. (mapc 'anything-force-update--reinit
  2241. (anything-get-sources)))
  2242. (anything-update preselect)
  2243. ;; If preselect arg exists, `anything-update' should
  2244. ;; have moved to selection, otherwise do it now.
  2245. (unless preselect
  2246. (anything-keep-selection (assoc-default 'name source) selection))
  2247. (with-anything-window (recenter))))
  2248. (defun anything-force-update--reinit (source)
  2249. "Reinit SOURCE by calling his update and/or init functions."
  2250. (anything-aif (anything-funcall-with-source
  2251. source 'anything-candidate-buffer)
  2252. (kill-buffer it))
  2253. (dolist (attr '(update init))
  2254. (anything-aif (assoc-default attr source)
  2255. (anything-funcall-with-source source it)))
  2256. (anything-remove-candidate-cache source))
  2257. (defun anything-keep-selection (source selection)
  2258. "Switch to SOURCE and goto SELECTION."
  2259. (when (and source selection)
  2260. (with-anything-window
  2261. (anything-goto-source source)
  2262. (forward-char -1)
  2263. (if (search-forward selection nil t)
  2264. (forward-line 0)
  2265. (goto-char (point-min))
  2266. (forward-line 1))
  2267. (anything-mark-current-line))))
  2268. (defun anything-remove-candidate-cache (source)
  2269. "Remove SOURCE from `anything-candidate-cache'."
  2270. (setq anything-candidate-cache
  2271. (delete (assoc (assoc-default 'name source)
  2272. anything-candidate-cache)
  2273. anything-candidate-cache)))
  2274. (defun anything-insert-match (match insert-function source)
  2275. "Insert MATCH into `anything-buffer' with INSERT-FUNCTION for SOURCE.
  2276. If MATCH is a list then insert the string intended to appear on the display
  2277. and store the real value in a text property."
  2278. (let ((start (point-at-bol (point)))
  2279. (dispvalue (or (car-safe match) match))
  2280. (realvalue (cdr-safe match)))
  2281. (setq dispvalue
  2282. (cond ((symbolp dispvalue) (symbol-name dispvalue))
  2283. ((numberp dispvalue) (number-to-string dispvalue))
  2284. (t dispvalue)))
  2285. (when (stringp dispvalue)
  2286. (funcall insert-function dispvalue)
  2287. ;; Some sources with candidates-in-buffer have already added
  2288. ;; 'anything-realvalue property when creating candidate buffer.
  2289. (unless (get-text-property start 'anything-realvalue)
  2290. (and realvalue
  2291. (put-text-property start (point-at-eol)
  2292. 'anything-realvalue realvalue)))
  2293. (when anything-source-in-each-line-flag
  2294. (put-text-property start (point-at-eol) 'anything-source source))
  2295. (funcall insert-function "\n"))))
  2296. (defun anything-insert-header-from-source (source)
  2297. "Insert SOURCE name in `anything-buffer' header.
  2298. Maybe insert by overlay additional info after source name if SOURCE have
  2299. header-name attribute."
  2300. (let ((name (assoc-default 'name source)))
  2301. (anything-insert-header
  2302. name
  2303. (anything-aif (assoc-default 'header-name source)
  2304. (anything-funcall-with-source source it name)))))
  2305. (defun anything-insert-header (name &optional display-string)
  2306. "Insert header of source NAME into the anything buffer.
  2307. If DISPLAY-STRING is non--nil and a string, display this additional info
  2308. after the source name by overlay."
  2309. (unless (bobp)
  2310. (let ((start (point)))
  2311. (insert "\n")
  2312. (put-text-property start (point) 'anything-header-separator t)))
  2313. (let ((start (point)))
  2314. (insert name)
  2315. (put-text-property (point-at-bol)
  2316. (point-at-eol) 'anything-header t)
  2317. (when display-string
  2318. (overlay-put (make-overlay (point-at-bol) (point-at-eol))
  2319. 'display display-string))
  2320. (insert "\n")
  2321. (put-text-property start (point) 'face anything-header-face)))
  2322. (defun anything-insert-candidate-separator ()
  2323. "Insert separator of candidates into the anything buffer."
  2324. (insert anything-candidate-separator)
  2325. (put-text-property (point-at-bol)
  2326. (point-at-eol) 'anything-candidate-separator t)
  2327. (insert "\n"))
  2328. ;; (@* "Core: async process")
  2329. (defun anything-output-filter (process string)
  2330. "From PROCESS process output STRING."
  2331. (anything-output-filter-1 (assoc process anything-async-processes) string))
  2332. (defun anything-output-filter-1 (process-assoc string)
  2333. (anything-log-eval string)
  2334. (with-current-buffer anything-buffer
  2335. (let ((source (cdr process-assoc)))
  2336. (save-excursion
  2337. (anything-aif (assoc-default 'insertion-marker source)
  2338. (goto-char it)
  2339. (goto-char (point-max))
  2340. (anything-insert-header-from-source source)
  2341. (setcdr process-assoc
  2342. (append source `((insertion-marker . ,(point-marker))))))
  2343. (anything-output-filter--process-source
  2344. (car process-assoc) string source
  2345. (anything-candidate-number-limit source))))
  2346. (anything-output-filter--post-process)))
  2347. (defun anything-output-filter--process-source (process string source limit)
  2348. (dolist (candidate (anything-transform-candidates
  2349. (anything-output-filter--collect-candidates
  2350. (split-string string "\n")
  2351. (assoc 'incomplete-line source))
  2352. source t))
  2353. (if (not (assq 'multiline source))
  2354. (anything-insert-match candidate 'insert-before-markers source)
  2355. (let ((start (point)))
  2356. (anything-insert-candidate-separator)
  2357. (anything-insert-match candidate 'insert-before-markers source)
  2358. (put-text-property start (point) 'anything-multiline t)))
  2359. (incf (cdr (assoc 'item-count source)))
  2360. (when (>= (assoc-default 'item-count source) limit)
  2361. (anything-kill-async-process process)
  2362. (return))))
  2363. (defun anything-output-filter--collect-candidates (lines incomplete-line-info)
  2364. (anything-log-eval (cdr incomplete-line-info))
  2365. (butlast
  2366. (loop for line in lines collect
  2367. (if (cdr incomplete-line-info)
  2368. (prog1
  2369. (concat (cdr incomplete-line-info) line)
  2370. (setcdr incomplete-line-info nil))
  2371. line)
  2372. finally (setcdr incomplete-line-info line))))
  2373. (defun anything-output-filter--post-process ()
  2374. (anything-log-run-hook 'anything-update-hook)
  2375. (anything-aif (get-buffer-window anything-buffer 'visible)
  2376. (save-selected-window
  2377. (select-window it)
  2378. (anything-skip-noncandidate-line 'next)
  2379. (anything-mark-current-line))))
  2380. (defun anything-kill-async-processes ()
  2381. "Kill all known asynchronous processes of `anything-async-processes'."
  2382. (mapc 'anything-kill-async-process (mapcar 'car anything-async-processes))
  2383. (setq anything-async-processes nil))
  2384. (defun anything-kill-async-process (process)
  2385. "Kill PROCESS and detach the associated functions."
  2386. (set-process-filter process nil)
  2387. (delete-process process))
  2388. ;; (@* "Core: action")
  2389. (defun anything-execute-selection-action (&optional
  2390. selection action
  2391. preserve-saved-action)
  2392. "If a candidate SELECTION is present then perform the associated ACTION on it.
  2393. If PRESERVE-SAVED-ACTION is non-nil don't save action."
  2394. (anything-log "executing action")
  2395. (setq action (anything-get-default-action
  2396. (or action
  2397. anything-saved-action
  2398. (if (get-buffer anything-action-buffer)
  2399. (anything-get-selection anything-action-buffer)
  2400. (anything-get-action)))))
  2401. (let ((source (or anything-saved-current-source
  2402. (anything-get-current-source))))
  2403. (setq selection (or selection
  2404. (anything-get-selection)
  2405. (and (assoc 'accept-empty source) "")))
  2406. (unless preserve-saved-action (setq anything-saved-action nil))
  2407. (if (and selection action)
  2408. (anything-funcall-with-source
  2409. source action
  2410. (anything-coerce-selection selection source)))))
  2411. (defun anything-coerce-selection (selection source)
  2412. "Apply coerce attribute function to SELECTION in SOURCE.
  2413. Coerce source with coerce function."
  2414. (anything-aif (assoc-default 'coerce source)
  2415. (anything-funcall-with-source source it selection)
  2416. selection))
  2417. (defun anything-get-default-action (action)
  2418. "Get the first ACTION value of action list in source."
  2419. (if (and (listp action) (not (functionp action)))
  2420. (cdar action)
  2421. action))
  2422. (defun anything-select-action ()
  2423. "Select an action for the currently selected candidate.
  2424. If action buffer is selected, back to the anything buffer."
  2425. (interactive)
  2426. (anything-log-run-hook 'anything-select-action-hook)
  2427. (cond ((get-buffer-window anything-action-buffer 'visible)
  2428. (set-window-buffer (get-buffer-window anything-action-buffer)
  2429. anything-buffer)
  2430. (kill-buffer anything-action-buffer)
  2431. (anything-set-pattern anything-input 'noupdate))
  2432. (t
  2433. (setq anything-saved-selection (anything-get-selection))
  2434. (unless anything-saved-selection
  2435. (error "Nothing is selected"))
  2436. (setq anything-saved-current-source (anything-get-current-source))
  2437. (let ((actions (anything-get-action)))
  2438. (if (functionp actions)
  2439. (message "Sole action: %s" actions)
  2440. (anything-show-action-buffer actions)
  2441. (anything-delete-minibuffer-contents)
  2442. (setq anything-pattern 'dummy) ; so that it differs from the previous one
  2443. (anything-check-minibuffer-input))))))
  2444. (defun anything-show-action-buffer (actions)
  2445. (with-current-buffer (get-buffer-create anything-action-buffer)
  2446. (erase-buffer)
  2447. (buffer-disable-undo)
  2448. (set-window-buffer (get-buffer-window anything-buffer) anything-action-buffer)
  2449. (set (make-local-variable 'anything-sources)
  2450. `(((name . "Actions")
  2451. (volatile)
  2452. (candidates . ,actions)
  2453. (candidate-number-limit))))
  2454. (set (make-local-variable 'anything-source-filter) nil)
  2455. (set (make-local-variable 'anything-selection-overlay) nil)
  2456. (set (make-local-variable 'anything-digit-overlays) nil)
  2457. (anything-initialize-overlays anything-action-buffer)))
  2458. ;; (@* "Core: selection")
  2459. (defun anything-move-selection-common (move-func unit direction)
  2460. "Move the selection marker to a new position wit function MOVE-FUNC.
  2461. It is determined by UNIT and DIRECTION."
  2462. (unless (or (anything-empty-buffer-p (anything-buffer-get))
  2463. (not (anything-window)))
  2464. (with-anything-window
  2465. (anything-log-run-hook 'anything-move-selection-before-hook)
  2466. (funcall move-func)
  2467. (anything-skip-noncandidate-line direction)
  2468. (anything-display-source-at-screen-top-maybe unit)
  2469. (when (anything-get-previous-header-pos)
  2470. (anything-mark-current-line))
  2471. (anything-display-mode-line (anything-get-current-source))
  2472. (anything-log-run-hook 'anything-move-selection-after-hook))))
  2473. (defun anything-display-source-at-screen-top-maybe (unit)
  2474. (when (and anything-display-source-at-screen-top (eq unit 'source))
  2475. (set-window-start (selected-window)
  2476. (save-excursion (forward-line -1) (point)))))
  2477. (defun anything-skip-noncandidate-line (direction)
  2478. (anything-skip-header-and-separator-line direction)
  2479. (and (bobp) (forward-line 1)) ;skip first header
  2480. (and (eobp) (forward-line -1))) ;avoid last empty line
  2481. (defun anything-skip-header-and-separator-line (direction)
  2482. (while (and (not (bobp))
  2483. (or (anything-pos-header-line-p)
  2484. (anything-pos-candidate-separator-p)))
  2485. (forward-line (if (and (eq direction 'previous)
  2486. (not (eq (point-at-bol) (point-min))))
  2487. -1 1))))
  2488. (defvar anything-mode-line-string-real nil)
  2489. (defun anything-display-mode-line (source)
  2490. (set (make-local-variable 'anything-mode-line-string)
  2491. (anything-interpret-value (or (assoc-default 'mode-line source)
  2492. (default-value 'anything-mode-line-string))
  2493. source))
  2494. (if anything-mode-line-string
  2495. (setq mode-line-format
  2496. '(" " mode-line-buffer-identification " "
  2497. (line-number-mode "L%l") " " (anything-follow-mode "(F) ")
  2498. (:eval (anything-show-candidate-number
  2499. (when (listp anything-mode-line-string)
  2500. (car anything-mode-line-string))))
  2501. " " anything-mode-line-string-real "-%-")
  2502. anything-mode-line-string-real
  2503. (substitute-command-keys (if (listp anything-mode-line-string)
  2504. (cadr anything-mode-line-string)
  2505. anything-mode-line-string)))
  2506. (setq mode-line-format
  2507. (default-value 'mode-line-format)))
  2508. (setq header-line-format
  2509. (anything-interpret-value (assoc-default 'header-line source) source)))
  2510. (defun anything-show-candidate-number (&optional name)
  2511. "Used to display candidate number in mode-line.
  2512. You can specify NAME of candidates e.g \"Buffers\" otherwise
  2513. it is \"Candidate\(s\)\" by default."
  2514. (propertize
  2515. (format "[%s %s]"
  2516. (anything-approximate-candidate-number 'in-current-source)
  2517. (or name "Candidate(s)"))
  2518. 'face 'anything-candidate-number))
  2519. (defun anything-previous-line ()
  2520. "Move selection to the previous line."
  2521. (interactive)
  2522. (anything-move-selection-common
  2523. (lambda ()
  2524. (if (not (anything-pos-multiline-p))
  2525. (forward-line -1) ;double forward-line is meaningful
  2526. (forward-line -1) ;because evaluation order is important
  2527. (anything-skip-header-and-separator-line 'previous)
  2528. (let ((header-pos (anything-get-previous-header-pos))
  2529. (separator-pos (anything-get-previous-candidate-separator-pos)))
  2530. (when header-pos
  2531. (goto-char (if (or (null separator-pos) (< separator-pos header-pos))
  2532. header-pos ; first candidate
  2533. separator-pos))
  2534. (forward-line 1)))))
  2535. 'line 'previous))
  2536. (defun anything-next-line ()
  2537. "Move selection to the next line."
  2538. (interactive)
  2539. (anything-move-selection-common
  2540. (lambda ()
  2541. (if (not (anything-pos-multiline-p))
  2542. (forward-line 1)
  2543. (let ((header-pos (anything-get-next-header-pos))
  2544. (separator-pos (anything-get-next-candidate-separator-pos)))
  2545. (cond ((and separator-pos
  2546. (or (null header-pos) (< separator-pos header-pos)))
  2547. (goto-char separator-pos))
  2548. (header-pos
  2549. (goto-char header-pos))))))
  2550. 'line 'next))
  2551. (defun anything-previous-page ()
  2552. "Move selection back with a pageful."
  2553. (interactive)
  2554. (anything-move-selection-common
  2555. (lambda ()
  2556. (condition-case nil
  2557. (scroll-down)
  2558. (beginning-of-buffer (goto-char (point-min)))))
  2559. 'page 'previous))
  2560. (defun anything-next-page ()
  2561. "Move selection forward with a pageful."
  2562. (interactive)
  2563. (anything-move-selection-common
  2564. (lambda ()
  2565. (condition-case nil
  2566. (scroll-up)
  2567. (end-of-buffer (goto-char (point-max)))))
  2568. 'page 'next))
  2569. (defun anything-beginning-of-buffer ()
  2570. "Move selection at the top."
  2571. (interactive)
  2572. (anything-move-selection-common
  2573. (lambda () (goto-char (point-min)))
  2574. 'edge 'previous))
  2575. (defun anything-end-of-buffer ()
  2576. "Move selection at the bottom."
  2577. (interactive)
  2578. (anything-move-selection-common
  2579. (lambda () (goto-char (point-max)))
  2580. 'edge 'next))
  2581. (defun anything-previous-source ()
  2582. "Move selection to the previous source."
  2583. (interactive)
  2584. (anything-move-selection-common
  2585. (lambda ()
  2586. (forward-line -1)
  2587. (if (bobp)
  2588. (goto-char (point-max))
  2589. (anything-skip-header-and-separator-line 'previous))
  2590. (goto-char (anything-get-previous-header-pos))
  2591. (forward-line 1))
  2592. 'source 'previous))
  2593. (defun anything-next-source ()
  2594. "Move selection to the next source."
  2595. (interactive)
  2596. (anything-move-selection-common
  2597. (lambda ()
  2598. (goto-char (or (anything-get-next-header-pos) (point-min))))
  2599. 'source 'next))
  2600. (defun anything-goto-source (source-or-name)
  2601. "Move the selection to the source SOURCE-OR-NAME."
  2602. (anything-move-selection-common
  2603. (lambda ()
  2604. (goto-char (point-min))
  2605. (let ((name (if (stringp source-or-name) source-or-name
  2606. (assoc-default 'name source-or-name))))
  2607. (condition-case err
  2608. (while (not (string= name (anything-current-line-contents)))
  2609. (goto-char (anything-get-next-header-pos)))
  2610. (error (message "")))))
  2611. 'source 'next))
  2612. (defun anything-mark-current-line (&optional resumep)
  2613. "Move `anything-selection-overlay' to current line.
  2614. Note that this is not related with visibles marks, which are used
  2615. to mark candidates."
  2616. (with-anything-window
  2617. (when resumep
  2618. (goto-char anything-selection-point))
  2619. (move-overlay
  2620. anything-selection-overlay (point-at-bol)
  2621. (if (anything-pos-multiline-p)
  2622. (let ((header-pos (anything-get-next-header-pos))
  2623. (separator-pos (anything-get-next-candidate-separator-pos)))
  2624. (or (and (null header-pos) separator-pos)
  2625. (and header-pos separator-pos (< separator-pos header-pos)
  2626. separator-pos)
  2627. header-pos
  2628. (point-max)))
  2629. (1+ (point-at-eol))))
  2630. (setq anything-selection-point (overlay-start anything-selection-overlay)))
  2631. (anything-follow-execute-persistent-action-maybe))
  2632. (defun anything-this-command-key ()
  2633. (event-basic-type (elt (this-command-keys-vector) 0)))
  2634. ;; (progn (read-key-sequence "Key: ") (p (anything-this-command-key)))
  2635. (defun anything-select-with-shortcut-internal (types get-key-func)
  2636. (if (memq anything-enable-shortcuts types)
  2637. (save-selected-window
  2638. (select-window (anything-window))
  2639. (let* ((key (funcall get-key-func))
  2640. (overlay (ignore-errors (nth (position key anything-shortcut-keys)
  2641. anything-digit-overlays))))
  2642. (if (not (and overlay (overlay-buffer overlay)))
  2643. (when (numberp key)
  2644. (select-window (minibuffer-window))
  2645. (self-insert-command 1))
  2646. (goto-char (overlay-start overlay))
  2647. (anything-mark-current-line)
  2648. (anything-exit-minibuffer))))
  2649. (self-insert-command 1)))
  2650. (defun anything-select-with-prefix-shortcut ()
  2651. "Invoke default action with prefix shortcut."
  2652. (interactive)
  2653. (anything-select-with-shortcut-internal
  2654. '(prefix)
  2655. (lambda () (read-event "Select shortcut key: "))))
  2656. (defun anything-select-with-digit-shortcut ()
  2657. "Invoke default action with digit/alphabet shortcut."
  2658. (interactive)
  2659. (anything-select-with-shortcut-internal
  2660. '(alphabet t) 'anything-this-command-key))
  2661. ;; (setq anything-enable-shortcuts 'prefix)
  2662. ;; (define-key anything-map "@" 'anything-select-with-prefix-shortcut)
  2663. ;; (define-key anything-map (kbd "<f18>") 'anything-select-with-prefix-shortcut)
  2664. (defvar anything-exit-status 0
  2665. "Flag to inform whether anything have exited or quitted.
  2666. Exit with 0 mean anything have exited executing an action.
  2667. Exit with 1 mean anything have quitted with \\[keyboard-quit]
  2668. It is useful for example to restore a window config if anything abort
  2669. in special cases.
  2670. See `anything-exit-minibuffer' and `anything-keyboard-quit'.")
  2671. (defvar anything-minibuffer-confirm-state nil)
  2672. (defun anything-confirm-and-exit-minibuffer ()
  2673. "Maybe ask for confirmation when exiting anything.
  2674. It is similar to `minibuffer-complete-and-exit' adapted to anything.
  2675. If `minibuffer-completion-confirm' value is 'confirm,
  2676. send in minibuffer confirm message and exit on next hit.
  2677. If `minibuffer-completion-confirm' value is t,
  2678. don't exit and send message 'no match'."
  2679. (interactive)
  2680. (let ((empty-buffer-p (with-current-buffer anything-buffer
  2681. (eq (point-min) (point-max)))))
  2682. (cond ((and empty-buffer-p
  2683. (eq minibuffer-completion-confirm 'confirm))
  2684. (setq anything-minibuffer-confirm-state
  2685. 'confirm)
  2686. (setq minibuffer-completion-confirm nil)
  2687. (minibuffer-message " [confirm]"))
  2688. ((and empty-buffer-p
  2689. (eq minibuffer-completion-confirm t))
  2690. (minibuffer-message " [No match]"))
  2691. (t
  2692. (setq anything-minibuffer-confirm-state nil)
  2693. (anything-exit-minibuffer)))))
  2694. (add-hook 'anything-after-update-hook 'anything-confirm-and-exit-hook)
  2695. (defun anything-confirm-and-exit-hook ()
  2696. "Restore `minibuffer-completion-confirm' when anything update."
  2697. (unless (or (eq minibuffer-completion-confirm t)
  2698. (not anything-minibuffer-confirm-state))
  2699. (setq minibuffer-completion-confirm
  2700. anything-minibuffer-confirm-state)))
  2701. (defun anything-exit-minibuffer ()
  2702. "Select the current candidate by exiting the minibuffer."
  2703. (interactive)
  2704. (unless anything-current-prefix-arg
  2705. (setq anything-current-prefix-arg current-prefix-arg))
  2706. (setq anything-exit-status 0)
  2707. (exit-minibuffer))
  2708. (defun anything-keyboard-quit ()
  2709. "Quit minibuffer in anything.
  2710. If action buffer is displayed, kill it."
  2711. (interactive)
  2712. (when (get-buffer-window anything-action-buffer 'visible)
  2713. (kill-buffer anything-action-buffer))
  2714. (setq anything-exit-status 1)
  2715. (abort-recursive-edit))
  2716. (defun anything-get-next-header-pos ()
  2717. "Return the position of the next header from point."
  2718. (next-single-property-change (point) 'anything-header))
  2719. (defun anything-get-previous-header-pos ()
  2720. "Return the position of the previous header from point."
  2721. (previous-single-property-change (point) 'anything-header))
  2722. (defun anything-pos-multiline-p ()
  2723. "Return non-nil if the current position is in the multiline source region."
  2724. (get-text-property (point) 'anything-multiline))
  2725. (defun anything-get-next-candidate-separator-pos ()
  2726. "Return the position of the next candidate separator from point."
  2727. (next-single-property-change (point) 'anything-candidate-separator))
  2728. (defun anything-get-previous-candidate-separator-pos ()
  2729. "Return the position of the previous candidate separator from point."
  2730. (previous-single-property-change (point) 'anything-candidate-separator))
  2731. (defun anything-pos-header-line-p ()
  2732. "Return t if the current line is a header line."
  2733. (or (get-text-property (point-at-bol) 'anything-header)
  2734. (get-text-property (point-at-bol) 'anything-header-separator)))
  2735. (defun anything-pos-candidate-separator-p ()
  2736. "Return t if the current line is a candidate separator."
  2737. (get-text-property (point-at-bol) 'anything-candidate-separator))
  2738. ;; (@* "Core: help")
  2739. (defun anything-help-internal (bufname insert-content-fn)
  2740. "Show long message during `anything' session in BUFNAME.
  2741. INSERT-CONTENT-FN is the text to be displayed in BUFNAME."
  2742. (save-window-excursion
  2743. (select-window (anything-window))
  2744. (delete-other-windows)
  2745. (switch-to-buffer (get-buffer-create bufname))
  2746. (erase-buffer)
  2747. (funcall insert-content-fn)
  2748. (setq mode-line-format "%b (SPC,C-v:NextPage b,M-v:PrevPage other:Exit)")
  2749. (setq cursor-type nil)
  2750. (goto-char 1)
  2751. (anything-help-event-loop)))
  2752. (defun anything-help-event-loop ()
  2753. (ignore-errors
  2754. (loop for event = (read-event) do
  2755. (case event
  2756. ((?\C-v ? ) (scroll-up))
  2757. ((?\M-v ?b) (scroll-down))
  2758. (t (return))))))
  2759. (defun anything-help ()
  2760. "Help of `anything'."
  2761. (interactive)
  2762. (anything-help-internal
  2763. " *Anything Help*"
  2764. (lambda ()
  2765. (insert (substitute-command-keys
  2766. (anything-interpret-value (or (assoc-default
  2767. 'help-message
  2768. (anything-get-current-source))
  2769. anything-help-message))))
  2770. (org-mode))))
  2771. (defun anything-debug-output ()
  2772. "Show all anything-related variables at this time."
  2773. (interactive)
  2774. (anything-help-internal " *Anything Debug*" 'anything-debug-output-function))
  2775. (defun anything-debug-output-function (&optional vars)
  2776. (message "Calculating all anything-related values...")
  2777. (insert "If you debug some variables or forms, set `anything-debug-forms'
  2778. to a list of forms.\n\n")
  2779. (dolist (v (or vars
  2780. anything-debug-forms
  2781. (apropos-internal "^anything-" 'boundp)))
  2782. (insert "** "
  2783. (pp-to-string v) "\n"
  2784. (pp-to-string (with-current-buffer anything-buffer (eval v))) "\n"))
  2785. (message "Calculating all anything-related values...Done"))
  2786. ;; (@* "Core: misc")
  2787. (defun anything-kill-buffer-hook ()
  2788. "Remove tick entry from `anything-tick-hash' when killing a buffer."
  2789. (loop for key being the hash-keys in anything-tick-hash
  2790. if (string-match (format "^%s/" (regexp-quote (buffer-name))) key)
  2791. do (remhash key anything-tick-hash)))
  2792. (add-hook 'kill-buffer-hook 'anything-kill-buffer-hook)
  2793. (defun anything-preselect (candidate-or-regexp)
  2794. "Move `anything-selection-overlay' to CANDIDATE-OR-REGEXP on startup."
  2795. (with-anything-window
  2796. (when candidate-or-regexp
  2797. (goto-char (point-min))
  2798. ;; go to first candidate of first source
  2799. (forward-line 1)
  2800. (let ((start (point)))
  2801. (or (re-search-forward
  2802. (concat "^" (regexp-quote candidate-or-regexp) "$") nil t)
  2803. (re-search-forward candidate-or-regexp nil t)
  2804. (search-forward candidate-or-regexp nil t)
  2805. (goto-char start))))
  2806. (anything-mark-current-line)))
  2807. (defun anything-delete-current-selection ()
  2808. "Delete the currently selected item."
  2809. (interactive)
  2810. (with-anything-window
  2811. (cond ((anything-pos-multiline-p)
  2812. (anything-aif (anything-get-next-candidate-separator-pos)
  2813. (delete-region (point-at-bol)
  2814. (1+ (progn (goto-char it) (point-at-eol))))
  2815. ;; last candidate
  2816. (goto-char (anything-get-previous-candidate-separator-pos))
  2817. (delete-region (point-at-bol) (point-max)))
  2818. (when (anything-end-of-source-p)
  2819. (goto-char (or (anything-get-previous-candidate-separator-pos)
  2820. (point-min)))
  2821. (forward-line 1)))
  2822. (t
  2823. (delete-region (point-at-bol) (1+ (point-at-eol)))
  2824. (when (anything-end-of-source-p) (forward-line -1))))
  2825. (anything-mark-current-line)))
  2826. (defun anything-end-of-source-p ()
  2827. "Return non--nil if we are at eob or end of source."
  2828. (save-excursion
  2829. (forward-line 1)
  2830. (or (eq (point-at-bol) (point-at-eol))
  2831. (anything-pos-header-line-p)
  2832. (eobp))))
  2833. (defun anything-edit-current-selection-internal (func)
  2834. (with-anything-window
  2835. (beginning-of-line)
  2836. (let ((realvalue (get-text-property (point) 'anything-realvalue)))
  2837. (funcall func)
  2838. (beginning-of-line)
  2839. (and realvalue
  2840. (put-text-property (point) (point-at-eol)
  2841. 'anything-realvalue realvalue))
  2842. (anything-mark-current-line))))
  2843. (defmacro anything-edit-current-selection (&rest forms)
  2844. "Evaluate FORMS at current selection in the anything buffer.
  2845. You can edit the line."
  2846. (declare (indent 0) (debug t))
  2847. `(anything-edit-current-selection-internal
  2848. (lambda () ,@forms)))
  2849. (defun anything-set-pattern (pattern &optional noupdate)
  2850. "Set minibuffer contents to PATTERN.
  2851. if optional NOUPDATE is non-nil, anything buffer is not changed."
  2852. (with-selected-window (or (active-minibuffer-window) (minibuffer-window))
  2853. (delete-minibuffer-contents)
  2854. (insert pattern))
  2855. (when noupdate
  2856. (setq anything-pattern pattern)
  2857. (anything-hooks 'cleanup)
  2858. (run-with-idle-timer 0 nil 'anything-hooks 'setup)))
  2859. (defun anything-delete-minibuffer-contents ()
  2860. "Same as `delete-minibuffer-contents' but this is a command."
  2861. (interactive)
  2862. (anything-set-pattern ""))
  2863. (defalias 'anything-delete-minibuffer-content 'anything-delete-minibuffer-contents)
  2864. ;;; Plugins
  2865. ;;
  2866. ;; (@* "Built-in plug-in: type")
  2867. (defun anything-compile-source--type (source)
  2868. (anything-aif (assoc-default 'type source)
  2869. (append source (assoc-default it anything-type-attributes) nil)
  2870. source))
  2871. ;; `define-anything-type-attribute' is public API.
  2872. (defun anything-add-type-attribute (type definition)
  2873. (anything-aif (assq type anything-type-attributes)
  2874. (setq anything-type-attributes (delete it anything-type-attributes)))
  2875. (push (cons type definition) anything-type-attributes))
  2876. (defvar anything-types nil)
  2877. (defun anything-document-type-attribute (type doc)
  2878. (add-to-list 'anything-types type t)
  2879. (put type 'anything-typeattrdoc
  2880. (concat "- " (symbol-name type) "\n\n" doc "\n")))
  2881. (defadvice documentation-property (after anything-document-type-attribute activate)
  2882. "Display type attributes' documentation as `anything-type-attributes' docstring."
  2883. (when (eq (ad-get-arg 0) 'anything-type-attributes)
  2884. (setq ad-return-value
  2885. (concat ad-return-value "\n\n++++ Types currently defined ++++\n"
  2886. (mapconcat (lambda (sym) (get sym 'anything-typeattrdoc))
  2887. anything-types "\n")))))
  2888. ;; (@* "Built-in plug-in: dummy")
  2889. (defun anything-dummy-candidate (candidate source)
  2890. "Use `anything-pattern' as CANDIDATE in SOURCE."
  2891. ;; `source' is defined in filtered-candidate-transformer
  2892. (list anything-pattern))
  2893. (defun anything-compile-source--dummy (source)
  2894. (if (assoc 'dummy source)
  2895. (append source
  2896. '((candidates "dummy")
  2897. (accept-empty)
  2898. (match identity)
  2899. (filtered-candidate-transformer . anything-dummy-candidate)
  2900. (disable-shortcuts)
  2901. (volatile)))
  2902. source))
  2903. ;; (@* "Built-in plug-in: disable-shortcuts")
  2904. (defvar anything-orig-enable-shortcuts nil)
  2905. (defun anything-save-enable-shortcuts ()
  2906. (anything-once
  2907. (lambda ()
  2908. (setq anything-orig-enable-shortcuts anything-enable-shortcuts
  2909. anything-enable-shortcuts nil))))
  2910. (defun anything-compile-source--disable-shortcuts (source)
  2911. (if (assoc 'disable-shortcuts source)
  2912. (append `((init ,@(anything-mklist (assoc-default 'init source))
  2913. anything-save-enable-shortcuts)
  2914. (resume ,@(anything-mklist (assoc-default 'resume source))
  2915. anything-save-enable-shortcuts)
  2916. (cleanup ,@(anything-mklist (assoc-default 'cleanup source))
  2917. (lambda () (setq anything-enable-shortcuts
  2918. anything-orig-enable-shortcuts))))
  2919. source)
  2920. source))
  2921. ;; (@* "Built-in plug-in: candidates-in-buffer")
  2922. (defun anything-candidates-in-buffer ()
  2923. "Get candidates from the candidates buffer according to `anything-pattern'.
  2924. BUFFER is `anything-candidate-buffer' by default. Each
  2925. candidate must be placed in one line. This function is meant to
  2926. be used in candidates-in-buffer or candidates attribute of an
  2927. anything source. Especially fast for many (1000+) candidates.
  2928. eg.
  2929. '((name . \"many files\")
  2930. (init . (lambda () (with-current-buffer (anything-candidate-buffer 'local)
  2931. (insert-many-filenames))))
  2932. (search re-search-forward) ; optional
  2933. (candidates-in-buffer)
  2934. (type . file))
  2935. +===============================================================+
  2936. | The new way of making and narrowing candidates: Using buffers |
  2937. +===============================================================+
  2938. By default, `anything' makes candidates by evaluating the
  2939. candidates function, then narrows them by `string-match' for each
  2940. candidate.
  2941. But this way is very slow for many candidates. The new way is
  2942. storing all candidates in a buffer and narrowing them by
  2943. `re-search-forward'. Search function is customizable by search
  2944. attribute. The important point is that buffer processing is MUCH
  2945. FASTER than string list processing and is the Emacs way.
  2946. The init function writes all candidates to a newly-created
  2947. candidate buffer. The candidates buffer is created or specified
  2948. by `anything-candidate-buffer'. Candidates are stored in a line.
  2949. The candidates function narrows all candidates, IOW creates a
  2950. subset of candidates dynamically. It is the task of
  2951. `anything-candidates-in-buffer'. As long as
  2952. `anything-candidate-buffer' is used,`(candidates-in-buffer)' is
  2953. sufficient in most cases.
  2954. Note that `(candidates-in-buffer)' is shortcut of three attributes:
  2955. (candidates . anything-candidates-in-buffer)
  2956. (volatile)
  2957. (match identity)
  2958. And `(candidates-in-buffer . func)' is shortcut of three attributes:
  2959. (candidates . func)
  2960. (volatile)
  2961. (match identity)
  2962. The expansion is performed in `anything-get-sources'.
  2963. The candidates-in-buffer attribute implies the volatile attribute.
  2964. The volatile attribute is needed because `anything-candidates-in-buffer'
  2965. creates candidates dynamically and need to be called everytime
  2966. `anything-pattern' changes.
  2967. Because `anything-candidates-in-buffer' plays the role of `match' attribute
  2968. function, specifying `(match identity)' makes the source slightly faster.
  2969. To customize `anything-candidates-in-buffer' behavior, use search,
  2970. get-line and search-from-end attributes. See also `anything-sources' docstring."
  2971. (declare (special source))
  2972. (anything-candidates-in-buffer-1
  2973. (anything-candidate-buffer)
  2974. anything-pattern
  2975. (or (assoc-default 'get-line source)
  2976. #'buffer-substring-no-properties)
  2977. ;; use external variable `source'.
  2978. (or (assoc-default 'search source)
  2979. (if (assoc 'search-from-end source)
  2980. '(anything-candidates-in-buffer-search-from-end)
  2981. '(anything-candidates-in-buffer-search-from-start)))
  2982. (anything-candidate-number-limit source)
  2983. (assoc 'search-from-end source)))
  2984. (defun anything-candidates-in-buffer-search-from-start (pattern)
  2985. "Search PATTERN with `re-search-forward' with bound and noerror args."
  2986. (re-search-forward pattern nil t))
  2987. (defun anything-candidates-in-buffer-search-from-end (pattern)
  2988. "Search PATTERN with `re-search-backward' with bound and noerror args."
  2989. (re-search-backward pattern nil t))
  2990. (defun anything-candidates-in-buffer-1 (buffer pattern get-line-fn
  2991. search-fns limit search-from-end)
  2992. ;; buffer == nil when candidates buffer does not exist.
  2993. (when buffer
  2994. (with-current-buffer buffer
  2995. (let ((start-point (if search-from-end (point-max) (point-min)))
  2996. (endp (if search-from-end #'bobp #'eobp)))
  2997. (goto-char (1- start-point))
  2998. (if (string= pattern "")
  2999. (anything-initial-candidates-from-candidate-buffer
  3000. endp get-line-fn limit search-from-end)
  3001. (anything-search-from-candidate-buffer
  3002. pattern get-line-fn search-fns limit search-from-end
  3003. start-point endp))))))
  3004. (defun anything-point-is-moved (proc)
  3005. "If point is moved after executing PROC, return t, otherwise nil."
  3006. (/= (point) (progn (funcall proc) (point))))
  3007. (defun anything-search-from-candidate-buffer (pattern get-line-fn search-fns
  3008. limit search-from-end
  3009. start-point endp)
  3010. (let (buffer-read-only
  3011. matches exit newmatches)
  3012. (anything-search-from-candidate-buffer-internal
  3013. (lambda ()
  3014. (clrhash anything-cib-hash)
  3015. (dolist (searcher search-fns)
  3016. (goto-char start-point)
  3017. (setq newmatches nil)
  3018. (loop with item-count = 0
  3019. while (funcall searcher pattern)
  3020. for cand = (funcall get-line-fn (point-at-bol) (point-at-eol))
  3021. do (anything-accumulate-candidates-internal
  3022. cand newmatches anything-cib-hash item-count limit)
  3023. unless (anything-point-is-moved
  3024. (lambda ()
  3025. (if search-from-end
  3026. (goto-char (1- (point-at-bol)))
  3027. (forward-line 1))))
  3028. return nil)
  3029. (setq matches (append matches (nreverse newmatches)))
  3030. (if exit (return)))
  3031. (delq nil matches)))))
  3032. (defun anything-initial-candidates-from-candidate-buffer (endp get-line-fn limit search-from-end)
  3033. (delq nil (loop with next-line-fn =
  3034. (if search-from-end
  3035. (lambda (x) (goto-char (max (1- (point-at-bol)) 1)))
  3036. #'forward-line)
  3037. until (funcall endp)
  3038. for i from 1 to limit
  3039. collect (funcall get-line-fn (point-at-bol) (point-at-eol))
  3040. do (funcall next-line-fn 1))))
  3041. (defun anything-search-from-candidate-buffer-internal (search-fn)
  3042. (goto-char (point-min))
  3043. (insert "\n")
  3044. (goto-char (point-max))
  3045. (insert "\n")
  3046. (unwind-protect
  3047. (funcall search-fn)
  3048. (goto-char (point-min))
  3049. (delete-char 1)
  3050. (goto-char (1- (point-max)))
  3051. (delete-char 1)
  3052. (set-buffer-modified-p nil)))
  3053. (defun anything-candidate-buffer (&optional create-or-buffer)
  3054. "Register and return a buffer containing candidates of current source.
  3055. `anything-candidate-buffer' searches buffer-local candidates buffer first,
  3056. then global candidates buffer.
  3057. Acceptable values of CREATE-OR-BUFFER:
  3058. - nil (omit)
  3059. Only return the candidates buffer.
  3060. - a buffer
  3061. Register a buffer as a candidates buffer.
  3062. - 'global
  3063. Create a new global candidates buffer,
  3064. named \" *anything candidates:SOURCE*\".
  3065. - other non-nil value
  3066. Create a new local candidates buffer,
  3067. named \" *anything candidates:SOURCE*ANYTHING-CURRENT-BUFFER\"."
  3068. (let* ((global-bname (format " *anything candidates:%s*"
  3069. anything-source-name))
  3070. (local-bname (format " *anything candidates:%s*%s"
  3071. anything-source-name
  3072. (buffer-name anything-current-buffer))))
  3073. (flet ((register-func ()
  3074. (setq anything-candidate-buffer-alist
  3075. (cons (cons anything-source-name create-or-buffer)
  3076. (delete (assoc anything-source-name
  3077. anything-candidate-buffer-alist)
  3078. anything-candidate-buffer-alist))))
  3079. (kill-buffers-func ()
  3080. (loop for b in (buffer-list)
  3081. if (string-match (format "^%s" (regexp-quote global-bname))
  3082. (buffer-name b))
  3083. do (kill-buffer b)))
  3084. (create-func ()
  3085. (with-current-buffer
  3086. (get-buffer-create (if (eq create-or-buffer 'global)
  3087. global-bname
  3088. local-bname))
  3089. (buffer-disable-undo)
  3090. (erase-buffer)
  3091. (font-lock-mode -1)))
  3092. (return-func ()
  3093. (or (get-buffer local-bname)
  3094. (get-buffer global-bname)
  3095. (anything-aif (assoc-default anything-source-name
  3096. anything-candidate-buffer-alist)
  3097. (and (buffer-live-p it) it)))))
  3098. (when create-or-buffer
  3099. (register-func)
  3100. (unless (bufferp create-or-buffer)
  3101. (and (eq create-or-buffer 'global) (kill-buffers-func))
  3102. (create-func)))
  3103. (return-func))))
  3104. (defun anything-compile-source--candidates-in-buffer (source)
  3105. (anything-aif (assoc 'candidates-in-buffer source)
  3106. (append source
  3107. `((candidates . ,(or (cdr it) 'anything-candidates-in-buffer))
  3108. (volatile) (match identity)))
  3109. source))
  3110. ;; (@* "Utility: resplit anything window")
  3111. (defun anything-toggle-resplit-window ()
  3112. "Toggle resplit anything window, vertically or horizontally."
  3113. (interactive)
  3114. (with-anything-window
  3115. (let ((before-height (window-height)))
  3116. (delete-window)
  3117. (set-window-buffer
  3118. (select-window (if (= (window-height) before-height)
  3119. (prog1
  3120. (split-window-vertically)
  3121. (setq anything-split-window-state 'vertical))
  3122. (setq anything-split-window-state 'horizontal)
  3123. (split-window-horizontally)))
  3124. anything-buffer))))
  3125. ;; (@* "Utility: Resize anything window.")
  3126. (defun anything-enlarge-window-1 (n)
  3127. "Enlarge or narrow anything window.
  3128. If N is positive enlarge, if negative narrow."
  3129. (unless anything-samewindow
  3130. (let ((horizontal-p (eq anything-split-window-state 'horizontal)))
  3131. (with-anything-window
  3132. (enlarge-window n horizontal-p)))))
  3133. (defun anything-narrow-window ()
  3134. "Narrow anything window."
  3135. (interactive)
  3136. (anything-enlarge-window-1 -1))
  3137. (defun anything-enlarge-window ()
  3138. "Enlarge anything window."
  3139. (interactive)
  3140. (anything-enlarge-window-1 1))
  3141. ;; (@* "Utility: select another action by key")
  3142. (defun anything-select-nth-action (n)
  3143. "Select the N nth action for the currently selected candidate."
  3144. (setq anything-saved-selection (anything-get-selection))
  3145. (unless anything-saved-selection
  3146. (error "Nothing is selected"))
  3147. (setq anything-saved-action (anything-get-nth-action n (anything-get-action)))
  3148. (anything-exit-minibuffer))
  3149. (defun anything-get-nth-action (n action)
  3150. (cond ((and (zerop n) (functionp action))
  3151. action)
  3152. ((listp action)
  3153. (or (cdr (elt action n))
  3154. (error "No such action")))
  3155. ((and (functionp action) (< 0 n))
  3156. (error "Sole action"))
  3157. (t
  3158. (error "Error in `anything-select-nth-action'"))))
  3159. (defun anything-select-2nd-action ()
  3160. "Select the 2nd action for the currently selected candidate."
  3161. (interactive)
  3162. (anything-select-nth-action 1))
  3163. (defun anything-select-3rd-action ()
  3164. "Select the 3rd action for the currently selected candidate."
  3165. (interactive)
  3166. (anything-select-nth-action 2))
  3167. (defun anything-select-4th-action ()
  3168. "Select the 4th action for the currently selected candidate."
  3169. (interactive)
  3170. (anything-select-nth-action 3))
  3171. (defun anything-select-2nd-action-or-end-of-line ()
  3172. "Select the 2nd action for the currently selected candidate.
  3173. This happen when point is at the end of minibuffer.
  3174. Otherwise goto the end of minibuffer."
  3175. (interactive)
  3176. (if (eolp)
  3177. (anything-select-nth-action 1)
  3178. (end-of-line)))
  3179. ;; (@* "Utility: Persistent Action")
  3180. (defmacro with-anything-display-same-window (&rest body)
  3181. "Execute BODY in the window used for persistent action.
  3182. Make `pop-to-buffer' and `display-buffer' display in the same window."
  3183. (declare (indent 0) (debug t))
  3184. `(let ((display-buffer-function 'anything-persistent-action-display-buffer))
  3185. ,@body))
  3186. (defvar anything-persistent-action-display-window nil)
  3187. (defun anything-initialize-persistent-action ()
  3188. (set (make-local-variable 'anything-persistent-action-display-window) nil))
  3189. (defun* anything-execute-persistent-action (&optional (attr 'persistent-action) onewindow)
  3190. "Perform the associated action ATTR without quitting anything.
  3191. ATTR default is 'persistent-action', but it can be anything else.
  3192. In this case you have to add this new attribute to your source.
  3193. When `anything-samewindow' and ONEWINDOW are non--nil,
  3194. the anything window is never split in persistent action."
  3195. (interactive)
  3196. (anything-log "executing persistent-action")
  3197. (with-anything-window
  3198. (save-selected-window
  3199. (anything-select-persistent-action-window onewindow)
  3200. (anything-log-eval (current-buffer))
  3201. (let ((anything-in-persistent-action t))
  3202. (with-anything-display-same-window
  3203. (anything-execute-selection-action
  3204. nil
  3205. (or (assoc-default attr (anything-get-current-source))
  3206. (anything-get-action))
  3207. t)
  3208. (anything-log-run-hook 'anything-after-persistent-action-hook))))))
  3209. (defun anything-persistent-action-display-window (&optional onewindow)
  3210. "Return the window that will be used for presistent action.
  3211. If ONEWINDOW is non--nil window will not be splitted in persistent action
  3212. if `anything-samewindow' is non--nil also."
  3213. (with-anything-window
  3214. (setq anything-persistent-action-display-window
  3215. (cond ((window-live-p anything-persistent-action-display-window)
  3216. anything-persistent-action-display-window)
  3217. ((and anything-samewindow (one-window-p t) (not onewindow))
  3218. (split-window))
  3219. ((get-buffer-window anything-current-buffer))
  3220. (t
  3221. (next-window (selected-window) 1))))))
  3222. (defun anything-select-persistent-action-window (&optional onewindow)
  3223. "Select the window that will be used for persistent action.
  3224. See `anything-persistent-action-display-window' for how to use ONEWINDOW."
  3225. (select-window (get-buffer-window (anything-buffer-get)))
  3226. (select-window
  3227. (setq minibuffer-scroll-window
  3228. (anything-persistent-action-display-window onewindow))))
  3229. (defun anything-persistent-action-display-buffer (buf &optional not-this-window)
  3230. "Make `pop-to-buffer' and `display-buffer' display in the same window.
  3231. If `anything-persistent-action-use-special-display' is non-nil and
  3232. BUF is to be displayed by `special-display-function', use it.
  3233. Otherwise ignores `special-display-buffer-names' and `special-display-regexps'.
  3234. Argument NOT-THIS-WINDOW if present will be used as
  3235. second argument of `display-buffer'."
  3236. (let* ((name (buffer-name buf))
  3237. display-buffer-function pop-up-windows pop-up-frames
  3238. (same-window-regexps
  3239. (unless (and anything-persistent-action-use-special-display
  3240. (or (member name
  3241. (mapcar (lambda (x) (or (car-safe x) x))
  3242. special-display-buffer-names))
  3243. (remove-if-not
  3244. (lambda (x) (string-match (or (car-safe x) x) name))
  3245. special-display-regexps)))
  3246. '("."))))
  3247. (display-buffer buf not-this-window)))
  3248. ;; scroll-other-window(-down)? for persistent-action
  3249. (defun anything-scroll-other-window-base (command)
  3250. (with-selected-window (anything-persistent-action-display-window)
  3251. (funcall command anything-scroll-amount)))
  3252. (defun anything-scroll-other-window ()
  3253. "Scroll other window (not *Anything* window) upward."
  3254. (interactive)
  3255. (anything-scroll-other-window-base 'scroll-up))
  3256. (defun anything-scroll-other-window-down ()
  3257. "Scroll other window (not *Anything* window) downward."
  3258. (interactive)
  3259. (anything-scroll-other-window-base 'scroll-down))
  3260. ;; (@* "Utility: Visible Mark")
  3261. (defface anything-visible-mark
  3262. '((((min-colors 88) (background dark))
  3263. (:background "green1" :foreground "black"))
  3264. (((background dark)) (:background "green" :foreground "black"))
  3265. (((min-colors 88)) (:background "green1"))
  3266. (t (:background "green")))
  3267. "Face for visible mark."
  3268. :group 'anything)
  3269. (defvar anything-visible-mark-face 'anything-visible-mark)
  3270. (defvar anything-visible-mark-overlays nil)
  3271. (defun anything-clear-visible-mark ()
  3272. (with-current-buffer (anything-buffer-get)
  3273. (mapc 'delete-overlay anything-visible-mark-overlays)
  3274. (set (make-local-variable 'anything-visible-mark-overlays) nil)))
  3275. (add-hook 'anything-after-initialize-hook 'anything-clear-visible-mark)
  3276. (defvar anything-marked-candidates nil
  3277. "Marked candadates. List of \(source . real\) pair.")
  3278. (defun anything-this-visible-mark ()
  3279. (loop for o in anything-visible-mark-overlays
  3280. when (equal (point-at-bol) (overlay-start o))
  3281. return o))
  3282. (defun anything-delete-visible-mark (overlay)
  3283. (setq anything-marked-candidates
  3284. (remove
  3285. (cons (anything-get-current-source) (anything-get-selection))
  3286. anything-marked-candidates))
  3287. (delete-overlay overlay)
  3288. (setq anything-visible-mark-overlays
  3289. (delq overlay anything-visible-mark-overlays)))
  3290. (defun anything-make-visible-mark ()
  3291. (let ((o (make-overlay (point-at-bol) (1+ (point-at-eol)))))
  3292. (overlay-put o 'face anything-visible-mark-face)
  3293. (overlay-put o 'source (assoc-default 'name (anything-get-current-source)))
  3294. (overlay-put o 'string (buffer-substring (overlay-start o) (overlay-end o)))
  3295. (overlay-put o 'real (anything-get-selection))
  3296. (add-to-list 'anything-visible-mark-overlays o))
  3297. (push (cons (anything-get-current-source) (anything-get-selection))
  3298. anything-marked-candidates))
  3299. (defun anything-toggle-visible-mark ()
  3300. "Toggle anything visible mark at point."
  3301. (interactive)
  3302. (with-anything-window
  3303. (anything-aif (anything-this-visible-mark)
  3304. (anything-delete-visible-mark it)
  3305. (anything-make-visible-mark))
  3306. (anything-next-line)))
  3307. (defun anything-display-all-visible-marks ()
  3308. "Show all `anything' visible marks strings."
  3309. (interactive)
  3310. (with-anything-window
  3311. (lexical-let ((overlays (reverse anything-visible-mark-overlays)))
  3312. (anything-run-after-quit
  3313. (lambda ()
  3314. (with-output-to-temp-buffer "*anything visible marks*"
  3315. (dolist (o overlays) (princ (overlay-get o 'string)))))))))
  3316. (defun anything-marked-candidates ()
  3317. "Return marked candidates of current source if any.
  3318. Otherwise one element list of current selection.
  3319. It is analogous to `dired-get-marked-files'."
  3320. (with-current-buffer (anything-buffer-get)
  3321. (let ((cands
  3322. (if anything-marked-candidates
  3323. (loop with current-src = (anything-get-current-source)
  3324. for (source . real) in (reverse anything-marked-candidates)
  3325. when (equal current-src source)
  3326. collect (anything-coerce-selection real source))
  3327. (list (anything-get-selection)))))
  3328. (anything-log-eval cands)
  3329. cands)))
  3330. (defun anything-reset-marked-candidates ()
  3331. (with-current-buffer (anything-buffer-get)
  3332. (set (make-local-variable 'anything-marked-candidates) nil)))
  3333. (add-hook 'anything-after-initialize-hook 'anything-reset-marked-candidates)
  3334. ;; (add-hook 'anything-after-action-hook 'anything-reset-marked-candidates)
  3335. (defun anything-current-source-name= (name)
  3336. (save-excursion
  3337. (goto-char (anything-get-previous-header-pos))
  3338. (equal name (anything-current-line-contents))))
  3339. (defun anything-revive-visible-mark ()
  3340. "Restore marked candidates when anything update display."
  3341. (with-current-buffer anything-buffer
  3342. (dolist (o anything-visible-mark-overlays)
  3343. (goto-char (point-min))
  3344. (while (and (search-forward (overlay-get o 'string) nil t)
  3345. (anything-current-source-name= (overlay-get o 'source)))
  3346. ;; Calculate real value of candidate.
  3347. ;; It can be nil if candidate have only a display value.
  3348. (let ((real (get-text-property (point-at-bol 0) 'anything-realvalue)))
  3349. (if real
  3350. ;; Check if real value of current candidate is the same
  3351. ;; that the one stored in overlay.
  3352. (and (string= (overlay-get o 'real) real)
  3353. (move-overlay o (point-at-bol 0) (1+ (point-at-eol 0))))
  3354. (move-overlay o (point-at-bol 0) (1+ (point-at-eol 0)))))))))
  3355. (add-hook 'anything-update-hook 'anything-revive-visible-mark)
  3356. (defun anything-next-point-in-list (curpos points &optional prev)
  3357. (cond
  3358. ;; rule out special cases
  3359. ((null points) curpos)
  3360. ((and prev (< curpos (car points))) curpos)
  3361. ((< (car (last points)) curpos)
  3362. (if prev (car (last points)) curpos))
  3363. (t
  3364. (nth (if prev
  3365. (loop for pt in points
  3366. for i from 0
  3367. if (<= curpos pt)
  3368. return (1- i))
  3369. (loop for pt in points
  3370. for i from 0
  3371. if (< curpos pt)
  3372. return i))
  3373. points))))
  3374. (defun anything-next-visible-mark (&optional prev)
  3375. "Move next anything visible mark.
  3376. If PREV is non-nil move to precedent."
  3377. (interactive)
  3378. (with-anything-window
  3379. (ignore-errors
  3380. (goto-char (anything-next-point-in-list
  3381. (point)
  3382. (sort (mapcar 'overlay-start anything-visible-mark-overlays) '<)
  3383. prev)))
  3384. (anything-mark-current-line)))
  3385. (defun anything-prev-visible-mark ()
  3386. "Move previous anything visible mark."
  3387. (interactive)
  3388. (anything-next-visible-mark t))
  3389. ;; (@* "Utility: Selection Paste")
  3390. (defun anything-yank-selection ()
  3391. "Set minibuffer contents to current selection."
  3392. (interactive)
  3393. (anything-set-pattern (anything-get-selection nil t)))
  3394. (defun anything-kill-selection-and-quit ()
  3395. "Store current selection to kill ring.
  3396. You can paste it by typing \\[yank]."
  3397. (interactive)
  3398. (anything-run-after-quit
  3399. (lambda (sel)
  3400. (kill-new sel)
  3401. (message "Killed: %s" sel))
  3402. (anything-get-selection nil t)))
  3403. ;; (@* "Utility: Automatical execution of persistent-action")
  3404. (add-to-list 'minor-mode-alist '(anything-follow-mode " AFollow"))
  3405. (defun anything-follow-mode ()
  3406. "If this mode is on, persistent action is executed everytime the cursor is moved."
  3407. (interactive)
  3408. (with-current-buffer anything-buffer
  3409. (setq anything-follow-mode (not anything-follow-mode))
  3410. (message "anything-follow-mode is %s"
  3411. (if anything-follow-mode "enabled" "disabled"))))
  3412. (defun anything-follow-execute-persistent-action-maybe ()
  3413. "Execute persistent action in mode `anything-follow-mode'.
  3414. This happen after `anything-input-idle-delay' secs."
  3415. (and (not (get-buffer-window anything-action-buffer 'visible))
  3416. (buffer-local-value 'anything-follow-mode
  3417. (get-buffer-create anything-buffer))
  3418. (sit-for (and anything-input-idle-delay
  3419. (max anything-input-idle-delay 0.1)))
  3420. (anything-window)
  3421. (anything-get-selection)
  3422. (save-excursion
  3423. (anything-execute-persistent-action))))
  3424. ;; (@* "Utility: Migrate `anything-sources' to my-anything command")
  3425. (defun anything-migrate-sources ()
  3426. "Help to migrate to new `anything' way."
  3427. (interactive)
  3428. (with-current-buffer (get-buffer-create "*anything migrate*")
  3429. (erase-buffer)
  3430. (insert (format "\
  3431. Setting `anything-sources' directly is not good because
  3432. `anything' is not for one command. For now, interactive use of
  3433. `anything' (M-x anything) is only for demonstration purpose.
  3434. So you should define commands calling `anything'.
  3435. I help you to migrate to the new way.
  3436. The code below is automatically generated from current
  3437. `anything-sources' value. You can use the `my-anything' command
  3438. now!
  3439. Copy and paste it to your .emacs. Then substitute `my-anything'
  3440. for `anything' bindings in all `define-key', `local-set-key' and
  3441. `global-set-key' calls.
  3442. \(defun my-anything ()
  3443. \"Anything command for you.
  3444. It is automatically generated by `anything-migrate-sources'.\"
  3445. (interactive)
  3446. (anything-other-buffer
  3447. '%S
  3448. \"*my-anything*\"))
  3449. " anything-sources))
  3450. (eval-last-sexp nil)
  3451. (substitute-key-definition 'anything 'my-anything global-map)
  3452. (pop-to-buffer (current-buffer))))
  3453. ;; (@* "Compatibility")
  3454. ;; Copied assoc-default from XEmacs version 21.5.12
  3455. (unless (fboundp 'assoc-default)
  3456. (defun assoc-default (key alist &optional test default)
  3457. "Find object KEY in a pseudo-alist ALIST.
  3458. ALIST is a list of conses or objects. Each element (or the element's car,
  3459. if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
  3460. If that is non-nil, the element matches;
  3461. then `assoc-default' returns the element's cdr, if it is a cons,
  3462. or DEFAULT if the element is not a cons.
  3463. If no element matches, the value is nil.
  3464. If TEST is omitted or nil, `equal' is used."
  3465. (let (found (tail alist) value)
  3466. (while (and tail (not found))
  3467. (let ((elt (car tail)))
  3468. (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
  3469. (setq found t value (if (consp elt) (cdr elt) default))))
  3470. (setq tail (cdr tail)))
  3471. value)))
  3472. ;; Function not available in XEmacs,
  3473. (unless (fboundp 'minibuffer-contents)
  3474. (defun minibuffer-contents ()
  3475. "Return the user input in a minbuffer as a string.
  3476. The current buffer must be a minibuffer."
  3477. (field-string (point-max)))
  3478. (defun delete-minibuffer-contents ()
  3479. "Delete all user input in a minibuffer.
  3480. The current buffer must be a minibuffer."
  3481. (delete-field (point-max))))
  3482. ;; Function not available in older Emacs (<= 22.1).
  3483. (unless (fboundp 'buffer-chars-modified-tick)
  3484. (defun buffer-chars-modified-tick (&optional buffer)
  3485. "Return BUFFER's character-change tick counter.
  3486. Each buffer has a character-change tick counter, which is set to the
  3487. value of the buffer's tick counter (see `buffer-modified-tick'), each
  3488. time text in that buffer is inserted or deleted. By comparing the
  3489. values returned by two individual calls of `buffer-chars-modified-tick',
  3490. you can tell whether a character change occurred in that buffer in
  3491. between these calls. No argument or nil as argument means use current
  3492. buffer as BUFFER."
  3493. (with-current-buffer (or buffer (current-buffer))
  3494. (if (listp buffer-undo-list)
  3495. (length buffer-undo-list)
  3496. (buffer-modified-tick)))))
  3497. ;; (@* "CUA workaround")
  3498. (defadvice cua-delete-region (around anything-avoid-cua activate)
  3499. (ignore-errors ad-do-it))
  3500. (defadvice copy-region-as-kill (around anything-avoid-cua activate)
  3501. (if cua-mode
  3502. (ignore-errors ad-do-it)
  3503. ad-do-it))
  3504. ;;(@* "Attribute Documentation")
  3505. (defun anything-describe-anything-attribute (anything-attribute)
  3506. "Display the full documentation of ANYTHING-ATTRIBUTE.
  3507. ANYTHING-ATTRIBUTE should be a symbol."
  3508. (interactive (list (intern
  3509. (completing-read
  3510. "Describe anything attribute: "
  3511. (mapcar 'symbol-name anything-additional-attributes)
  3512. nil t))))
  3513. (with-output-to-temp-buffer "*Help*"
  3514. (princ (get anything-attribute 'anything-attrdoc))))
  3515. (anything-document-attribute 'name "mandatory"
  3516. " The name of the source. It is also the heading which appears
  3517. above the list of matches from the source. Must be unique.")
  3518. (anything-document-attribute 'header-name "optional"
  3519. " A function returning the display string of the header. Its
  3520. argument is the name of the source. This attribute is useful to
  3521. add an additional information with the source name.")
  3522. (anything-document-attribute 'candidates "mandatory if candidates-in-buffer attribute is not provided"
  3523. " Specifies how to retrieve candidates from the source. It can
  3524. either be a variable name, a function called with no parameters
  3525. or the actual list of candidates.
  3526. The list must be a list whose members are strings, symbols
  3527. or (DISPLAY . REAL) pairs.
  3528. In case of (DISPLAY . REAL) pairs, the DISPLAY string is shown
  3529. in the Anything buffer, but the REAL one is used as action
  3530. argument when the candidate is selected. This allows a more
  3531. readable presentation for candidates which would otherwise be,
  3532. for example, too long or have a common part shared with other
  3533. candidates which can be safely replaced with an abbreviated
  3534. string for display purposes.
  3535. Note that if the (DISPLAY . REAL) form is used then pattern
  3536. matching is done on the displayed string, not on the real
  3537. value.
  3538. If the candidates have to be retrieved asynchronously (for
  3539. example, by an external command which takes a while to run)
  3540. then the function should start the external command
  3541. asynchronously and return the associated process object.
  3542. Anything will take care of managing the process (receiving the
  3543. output from it, killing it if necessary, etc.). The process
  3544. should return candidates matching the current pattern (see
  3545. variable `anything-pattern'.)
  3546. Note that currently results from asynchronous sources appear
  3547. last in the anything buffer regardless of their position in
  3548. `anything-sources'.")
  3549. (anything-document-attribute 'action "mandatory if type attribute is not provided"
  3550. " It is a list of (DISPLAY . FUNCTION) pairs or FUNCTION.
  3551. FUNCTION is called with one parameter: the selected candidate.
  3552. An action other than the default can be chosen from this list
  3553. of actions for the currently selected candidate (by default
  3554. with TAB). The DISPLAY string is shown in the completions
  3555. buffer and the FUNCTION is invoked when an action is
  3556. selected. The first action of the list is the default.")
  3557. (anything-document-attribute 'coerce "optional"
  3558. " It's a function called with one argument: the selected candidate.
  3559. This function is intended for type convertion.
  3560. In normal case, the selected candidate (string) is passed to action function.
  3561. If coerce function is specified, it is called just before action function.
  3562. Example: converting string to symbol
  3563. (coerce . intern)")
  3564. (anything-document-attribute 'type "optional if action attribute is provided"
  3565. " Indicates the type of the items the source returns.
  3566. Merge attributes not specified in the source itself from
  3567. `anything-type-attributes'.
  3568. This attribute is implemented by plug-in.")
  3569. (anything-document-attribute 'init "optional"
  3570. " Function called with no parameters when anything is started. It
  3571. is useful for collecting current state information which can be
  3572. used to create the list of candidates later.
  3573. For example, if a source needs to work with the current
  3574. directory then it can store its value here, because later
  3575. anything does its job in the minibuffer and in the
  3576. `anything-buffer' and the current directory can be different
  3577. there.")
  3578. (anything-document-attribute 'delayed-init "optional"
  3579. " Function called with no parameters before candidate function is
  3580. called. It is similar with `init' attribute, but its
  3581. evaluation is deferred. It is useful to combine with ")
  3582. (anything-document-attribute 'match "optional"
  3583. " List of functions called with one parameter: a candidate. The
  3584. function should return non-nil if the candidate matches the
  3585. current pattern (see variable `anything-pattern').
  3586. This attribute allows the source to override the default
  3587. pattern matching based on `string-match'. It can be used, for
  3588. example, to implement a source for file names and do the
  3589. pattern matching on the basename of files, since it's more
  3590. likely one is typing part of the basename when searching for a
  3591. file, instead of some string anywhere else in its path.
  3592. If the list contains more than one function then the list of
  3593. matching candidates from the source is constructed by appending
  3594. the results after invoking the first function on all the
  3595. potential candidates, then the next function, and so on. The
  3596. matching candidates supplied by the first function appear first
  3597. in the list of results and then results from the other
  3598. functions, respectively.
  3599. This attribute has no effect for asynchronous sources (see
  3600. attribute `candidates'), since they perform pattern matching
  3601. themselves.")
  3602. (anything-document-attribute 'candidate-transformer "optional"
  3603. " It's a function or a list of functions called with one argument
  3604. when the completion list from the source is built. The argument
  3605. is the list of candidates retrieved from the source. The
  3606. function should return a transformed list of candidates which
  3607. will be used for the actual completion. If it is a list of
  3608. functions, it calls each function sequentially.
  3609. This can be used to transform or remove items from the list of
  3610. candidates.
  3611. Note that `candidates' is run already, so the given transformer
  3612. function should also be able to handle candidates with (DISPLAY
  3613. . REAL) format.")
  3614. (anything-document-attribute 'filtered-candidate-transformer "optional"
  3615. " It has the same format as `candidate-transformer', except the
  3616. function is called with two parameters: the candidate list and
  3617. the source.
  3618. This transformer is run on the candidate list which is already
  3619. filtered by the current pattern. While `candidate-transformer'
  3620. is run only once, it is run every time the input pattern is
  3621. changed.
  3622. It can be used to transform the candidate list dynamically, for
  3623. example, based on the current pattern.
  3624. In some cases it may also be more efficent to perform candidate
  3625. transformation here, instead of with `candidate-transformer'
  3626. even if this transformation is done every time the pattern is
  3627. changed. For example, if a candidate set is very large then
  3628. `candidate-transformer' transforms every candidate while only
  3629. some of them will actually be dislpayed due to the limit
  3630. imposed by `anything-candidate-number-limit'.
  3631. Note that `candidates' and `candidate-transformer' is run
  3632. already, so the given transformer function should also be able
  3633. to handle candidates with (DISPLAY . REAL) format.
  3634. This option has no effect for asynchronous sources. (Not yet,
  3635. at least.")
  3636. (anything-document-attribute 'action-transformer "optional"
  3637. " It's a function or a list of functions called with two
  3638. arguments when the action list from the source is
  3639. assembled. The first argument is the list of actions, the
  3640. second is the current selection. If it is a list of functions,
  3641. it calls each function sequentially.
  3642. The function should return a transformed action list.
  3643. This can be used to customize the list of actions based on the
  3644. currently selected candidate.")
  3645. (anything-document-attribute 'pattern-transformer "optional"
  3646. " It's a function or a list of functions called with one argument
  3647. before computing matches. Its argument is `anything-pattern'.
  3648. Functions should return transformed `anything-pattern'.
  3649. It is useful to change interpretation of `anything-pattern'.")
  3650. (anything-document-attribute 'delayed "optional"
  3651. " Candidates from the source are shown only if the user stops
  3652. typing and is idle for `anything-idle-delay' seconds.")
  3653. (anything-document-attribute 'volatile "optional"
  3654. " Indicates the source assembles the candidate list dynamically,
  3655. so it shouldn't be cached within a single Anything
  3656. invocation. It is only applicable to synchronous sources,
  3657. because asynchronous sources are not cached.")
  3658. (anything-document-attribute 'requires-pattern "optional"
  3659. " If present matches from the source are shown only if the
  3660. pattern is not empty. Optionally, it can have an integer
  3661. parameter specifying the required length of input which is
  3662. useful in case of sources with lots of candidates.")
  3663. (anything-document-attribute 'persistent-action "optional"
  3664. " Function called with one parameter; the selected candidate.
  3665. An action performed by `anything-execute-persistent-action'.
  3666. If none, use the default action.")
  3667. (anything-document-attribute 'candidates-in-buffer "optional"
  3668. " Shortcut attribute for making and narrowing candidates using
  3669. buffers. This newly-introduced attribute prevents us from
  3670. forgetting to add volatile and match attributes.
  3671. See docstring of `anything-candidates-in-buffer'.
  3672. (candidates-in-buffer) is equivalent of three attributes:
  3673. (candidates . anything-candidates-in-buffer)
  3674. (volatile)
  3675. (match identity)
  3676. (candidates-in-buffer . candidates-function) is equivalent of:
  3677. (candidates . candidates-function)
  3678. (volatile)
  3679. (match identity)
  3680. This attribute is implemented by plug-in.")
  3681. (anything-document-attribute 'search "optional"
  3682. " List of functions like `re-search-forward' or `search-forward'.
  3683. Buffer search function used by `anything-candidates-in-buffer'.
  3684. By default, `anything-candidates-in-buffer' uses `re-search-forward'.
  3685. This attribute is meant to be used with
  3686. (candidates . anything-candidates-in-buffer) or
  3687. (candidates-in-buffer) in short.")
  3688. (anything-document-attribute 'search-from-end "optional"
  3689. " Make `anything-candidates-in-buffer' search from the end of buffer.
  3690. If this attribute is specified, `anything-candidates-in-buffer' uses
  3691. `re-search-backward' instead.")
  3692. (anything-document-attribute 'get-line "optional"
  3693. " A function like `buffer-substring-no-properties' or `buffer-substring'.
  3694. This function converts point of line-beginning and point of line-end,
  3695. which represents a candidate computed by `anything-candidates-in-buffer'.
  3696. By default, `anything-candidates-in-buffer' uses
  3697. `buffer-substring-no-properties'.")
  3698. (anything-document-attribute 'display-to-real "optional"
  3699. " Function called with one parameter; the selected candidate.
  3700. The function transforms the selected candidate, and the result
  3701. is passed to the action function. The display-to-real
  3702. attribute provides another way to pass other string than one
  3703. shown in Anything buffer.
  3704. Traditionally, it is possible to make candidates,
  3705. candidate-transformer or filtered-candidate-transformer
  3706. function return a list with (DISPLAY . REAL) pairs. But if REAL
  3707. can be generated from DISPLAY, display-to-real is more
  3708. convenient and faster.")
  3709. (anything-document-attribute 'real-to-display "optional"
  3710. " Function called with one parameter; the selected candidate.
  3711. The inverse of display-to-real attribute.
  3712. The function transforms the selected candidate, which is passed
  3713. to the action function, for display. The real-to-display
  3714. attribute provides the other way to pass other string than one
  3715. shown in Anything buffer.
  3716. Traditionally, it is possible to make candidates,
  3717. candidate-transformer or filtered-candidate-transformer
  3718. function return a list with (DISPLAY . REAL) pairs. But if
  3719. DISPLAY can be generated from REAL, real-to-display is more
  3720. convenient.
  3721. Note that DISPLAY parts returned from candidates /
  3722. candidate-transformer are IGNORED as the name `display-to-real'
  3723. says.")
  3724. (anything-document-attribute 'cleanup "optional"
  3725. " Function called with no parameters when *anything* buffer is closed. It
  3726. is useful for killing unneeded candidates buffer.
  3727. Note that the function is executed BEFORE performing action.")
  3728. (anything-document-attribute 'candidate-number-limit "optional"
  3729. " Override `anything-candidate-number-limit' only for this source.")
  3730. (anything-document-attribute 'accept-empty "optional"
  3731. " Pass empty string \"\" to action function.")
  3732. (anything-document-attribute 'disable-shortcuts "optional"
  3733. " Disable `anything-enable-shortcuts' in current `anything' session.
  3734. This attribute is implemented by plug-in.")
  3735. (anything-document-attribute 'dummy "optional"
  3736. " Set `anything-pattern' to candidate. If this attribute is
  3737. specified, The candidates attribute is ignored.
  3738. This attribute is implemented by plug-in.
  3739. This plug-in implies disable-shortcuts plug-in.")
  3740. (anything-document-attribute 'multiline "optional"
  3741. " Enable to selection multiline candidates.")
  3742. (anything-document-attribute 'update "optional"
  3743. (substitute-command-keys
  3744. " Function called with no parameters when \
  3745. \\<anything-map>\\[anything-force-update] is pressed."))
  3746. (anything-document-attribute 'mode-line "optional"
  3747. " source local `anything-mode-line-string'. (included in `mode-line-format')
  3748. It accepts also variable/function name.")
  3749. (anything-document-attribute 'header-line "optional"
  3750. " source local `header-line-format'.
  3751. It accepts also variable/function name. ")
  3752. (anything-document-attribute
  3753. 'resume "optional"
  3754. " Function called with no parameters when `anything-resume' is started.")
  3755. (anything-document-attribute 'keymap "optional"
  3756. " Specific keymap for this source.
  3757. It is useful to have a keymap per source when using more than one source.
  3758. Otherwise, a keymap can be set per command with `anything' argument KEYMAP.
  3759. NOTE: when a source have `anything-map' as keymap attr,
  3760. the global value of `anything-map' will override the actual local one.")
  3761. (anything-document-attribute 'help-message "optional"
  3762. " Help message for this source.
  3763. If not present, `anything-help-message' value will be used.")
  3764. ;; (@* "Bug Report")
  3765. (defvar anything-maintainer-mail-address "emacs-anything@googlegroups.com")
  3766. (defvar anything-bug-report-salutation
  3767. "Describe bug below, using a precise recipe.
  3768. When I executed M-x ...
  3769. How to send a bug report:
  3770. 1) Be sure to use the LATEST version of anything.el.
  3771. 2) Enable debugger. M-x toggle-debug-on-error or (setq debug-on-error t)
  3772. 3) Use Lisp version instead of compiled one: (load \"anything.el\")
  3773. 4) If you got an error, please paste *Backtrace* buffer.
  3774. 5) Type C-c C-c to send.")
  3775. (defvar anything-no-dump-variables
  3776. '(anything-candidate-buffer-alist
  3777. anything-digit-overlays
  3778. anything-help-message
  3779. anything-candidate-cache
  3780. )
  3781. "Variables not to dump in bug report.")
  3782. (defun anything-dumped-variables-in-bug-report ()
  3783. (let ((hash (make-hash-table)))
  3784. (loop for var in (apropos-internal "anything-" 'boundp)
  3785. for vname = (symbol-name var)
  3786. unless (or (string-match "-map$" vname)
  3787. (string-match "^anything-c-source-" vname)
  3788. (string-match "-hash$" vname)
  3789. (string-match "-face$" vname)
  3790. (memq var anything-no-dump-variables))
  3791. collect var)))
  3792. (defun anything-send-bug-report ()
  3793. "Send a bug report of anything.el."
  3794. (interactive)
  3795. (with-current-buffer (or anything-last-buffer
  3796. (current-buffer))
  3797. (reporter-submit-bug-report
  3798. anything-maintainer-mail-address
  3799. "anything.el"
  3800. (anything-dumped-variables-in-bug-report)
  3801. nil nil
  3802. anything-bug-report-salutation)))
  3803. (defun anything-send-bug-report-from-anything ()
  3804. "Send a bug report of anything.el in anything session."
  3805. (interactive)
  3806. (anything-run-after-quit 'anything-send-bug-report))
  3807. ;; Debugging function.
  3808. (defun* anything-test-candidates
  3809. (sources &optional (input "")
  3810. (compile-source-functions
  3811. anything-compile-source-functions-default))
  3812. "Test helper function for anything.
  3813. Given pseudo `anything-sources' and `anything-pattern', returns list like
  3814. ((\"source name1\" (\"candidate1\" \"candidate2\"))
  3815. (\"source name2\" (\"candidate3\" \"candidate4\")))"
  3816. (let ((anything-test-mode t)
  3817. anything-enable-shortcuts
  3818. anything-candidate-cache
  3819. (anything-compile-source-functions compile-source-functions)
  3820. anything-before-initialize-hook
  3821. anything-after-initialize-hook
  3822. anything-update-hook
  3823. anything-test-candidate-list)
  3824. (get-buffer-create anything-buffer)
  3825. (anything-initialize nil input sources)
  3826. (anything-update)
  3827. ;; test-mode spec: select 1st candidate!
  3828. (with-current-buffer anything-buffer
  3829. (forward-line 1)
  3830. (anything-mark-current-line))
  3831. (prog1
  3832. anything-test-candidate-list
  3833. (anything-cleanup))))
  3834. ;; (@* "Unit Tests")
  3835. ;; See developer-tools/unit-test-anything.el
  3836. (provide 'anything)
  3837. ;; Local Variables:
  3838. ;; coding: utf-8
  3839. ;; End:
  3840. ;;; anything.el ends here