Improve `anything-c-source-emms-streams' (Edit action added).
[anything-config.git] / anything-config.el
blob67e5e442d415846b7405240f2485c5f837c4380a
1 ;;; anything-config.el --- Predefined configurations for `anything.el'
3 ;; Filename: anything-config.el
5 ;; Description: Predefined configurations for `anything.el'
6 ;; Author: Tassilo Horn <tassilo@member.fsf.org>
7 ;; Maintainer: Tassilo Horn <tassilo@member.fsf.org>
8 ;; Andy Stewart <lazycat.manatee@gmail.com>
9 ;; rubikitch <rubikitch@ruby-lang.org>
10 ;; Thierry Volpiatto <thierry.volpiatto@gmail.com>
11 ;; Copyright (C) 2007 ~ 2009, Tassilo Horn, all rights reserved.
12 ;; Copyright (C) 2009, Andy Stewart, all rights reserved.
13 ;; Copyright (C) 2009, rubikitch, all rights reserved.
14 ;; Copyright (C) 2009, Thierry Volpiatto, all rights reserved.
15 ;; Created: 2009-02-16 21:38:23
16 ;; Version: 0.4.0
17 ;; URL: http://www.emacswiki.org/emacs/download/anything-config.el
18 ;; Keywords: anything, anything-config
19 ;; Compatibility: GNU Emacs 22 ~ 23
21 ;; Features that might be required by this library:
23 ;; `anything'
26 ;;; This file is NOT part of GNU Emacs
28 ;;; License
30 ;; This program is free software; you can redistribute it and/or modify
31 ;; it under the terms of the GNU General Public License as published by
32 ;; the Free Software Foundation; either version 3, or (at your option)
33 ;; any later version.
35 ;; This program is distributed in the hope that it will be useful,
36 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
37 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 ;; GNU General Public License for more details.
40 ;; You should have received a copy of the GNU General Public License
41 ;; along with this program; see the file COPYING. If not, write to
42 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
43 ;; Floor, Boston, MA 02110-1301, USA.
45 ;;; !NOTICE!
47 ;; If this file does not work, upgrade anything.el!
48 ;; http://www.emacswiki.org/cgi-bin/wiki/download/anything.el
50 ;;; Commentary:
52 ;; Predefined configurations for `anything.el'
54 ;; For quick start, try `anything-for-files' to open files.
55 ;;
56 ;; To configure anything you should setup `anything-sources'
57 ;; with specify source, like below:
59 ;; (setq anything-sources
60 ;; '(anything-c-source-buffers
61 ;; anything-c-source-buffer-not-found
62 ;; anything-c-source-file-name-history
63 ;; anything-c-source-info-pages
64 ;; anything-c-source-info-elisp
65 ;; anything-c-source-man-pages
66 ;; anything-c-source-locate
67 ;; anything-c-source-emacs-commands
68 ;; ))
70 ;; Below are complete source list you can setup in `anything-sources':
72 ;; Buffer:
73 ;; `anything-c-source-buffers' (Buffers)
74 ;; `anything-c-source-buffer-not-found' (Create buffer)
75 ;; `anything-c-source-buffers+' (Buffers)
76 ;; File:
77 ;; `anything-c-source-file-name-history' (File Name History)
78 ;; `anything-c-source-files-in-current-dir' (Files from Current Directory)
79 ;; `anything-c-source-files-in-current-dir+' (Files from Current Directory)
80 ;; `anything-c-source-file-cache' (File Cache)
81 ;; `anything-c-source-locate' (Locate)
82 ;; `anything-c-source-recentf' (Recentf)
83 ;; `anything-c-source-ffap-guesser' (File at point)
84 ;; `anything-c-source-ffap-line' (File/Lineno at point)
85 ;; Help:
86 ;; `anything-c-source-man-pages' (Manual Pages)
87 ;; `anything-c-source-info-pages' (Info Pages)
88 ;; `anything-c-source-info-elisp' (Info Elisp)
89 ;; `anything-c-source-info-cl' (Info Common-Lisp)
90 ;; Command:
91 ;; `anything-c-source-complex-command-history' (Complex Command History)
92 ;; `anything-c-source-extended-command-history' (Emacs Commands History)
93 ;; `anything-c-source-emacs-commands' (Emacs Commands)
94 ;; `anything-c-source-lacarte' (Lacarte)
95 ;; Function:
96 ;; `anything-c-source-emacs-functions' (Emacs Functions)
97 ;; `anything-c-source-emacs-functions-with-abbrevs' (Emacs Functions)
98 ;; Variable:
99 ;; `anything-c-source-emacs-variables' (Emacs Variables)
100 ;; Bookmark:
101 ;; `anything-c-source-bookmarks' (Bookmarks)
102 ;; `anything-c-source-bookmark-set' (Set Bookmark)
103 ;; `anything-c-source-bookmarks-ssh' (Bookmarks-ssh)
104 ;; `anything-c-source-bookmarks-su' (Bookmarks-root)
105 ;; `anything-c-source-bookmarks-local' (Bookmarks-Local)
106 ;; `anything-c-source-w3m-bookmarks' (W3m Bookmarks)
107 ;; Library:
108 ;; `anything-c-source-elisp-library-scan' (Elisp libraries (Scan))
109 ;; Programming:
110 ;; `anything-c-source-imenu' (Imenu)
111 ;; `anything-c-source-ctags' (Exuberant ctags)
112 ;; `anything-c-source-semantic' (Semantic Tags)
113 ;; `anything-c-source-simple-call-tree-functions-callers' (Function is called by)
114 ;; `anything-c-source-simple-call-tree-callers-functions' (Function calls)
115 ;; `anything-c-source-commands-and-options-in-file' (Commands/Options in file)
116 ;; Color and Face:
117 ;; `anything-c-source-customize-face' (Customize Face)
118 ;; `anything-c-source-colors' (Colors)
119 ;; Search Engine:
120 ;; `anything-c-source-tracker-search' (Tracker Search)
121 ;; `anything-c-source-mac-spotlight' (mdfind)
122 ;; Kill ring:
123 ;; `anything-c-source-kill-ring' (Kill Ring)
124 ;; Register:
125 ;; `anything-c-source-register' (Registers)
126 ;; Headline Extraction:
127 ;; `anything-c-source-fixme' (TODO/FIXME/DRY comments)
128 ;; `anything-c-source-rd-headline' (RD HeadLine)
129 ;; `anything-c-source-oddmuse-headline' (Oddmuse HeadLine)
130 ;; `anything-c-source-emacs-source-defun' (Emacs Source DEFUN)
131 ;; `anything-c-source-emacs-lisp-expectations' (Emacs Lisp Expectations)
132 ;; `anything-c-source-emacs-lisp-toplevels' (Emacs Lisp Toplevel / Level 4 Comment / Linkd Star)
133 ;; `anything-c-source-org-headline' (Org HeadLine)
134 ;; Misc:
135 ;; `anything-c-source-picklist' (Picklist)
136 ;; `anything-c-source-bbdb' (BBDB)
137 ;; `anything-c-source-evaluation-result' (Evaluation Result)
138 ;; `anything-c-source-calculation-result' (Calculation Result)
139 ;; `anything-c-source-google-suggest' (Google Suggest)
140 ;; `anything-c-source-surfraw' (Surfraw)
141 ;; `anything-c-source-emms-streams' (Emms Streams)
142 ;; `anything-c-source-emms-dired' (Music Directory)
143 ;; `anything-c-source-jabber-contacts' (Jabber Contacts)
144 ;; `anything-c-source-call-source' (Call anything source)
145 ;; `anything-c-source-occur' (Occur)
146 ;; `anything-c-source-create' (Create)
147 ;; `anything-c-source-minibuffer-history' (Minibuffer History)
148 ;; System:
149 ;; `anything-c-source-gentoo' (Portage sources)
150 ;; `anything-c-source-use-flags' (Use Flags)
151 ;; `anything-c-source-emacs-process' (Emacs Process)
153 ;;; Commands:
155 ;; Below are complete command list:
157 ;; `anything-for-files'
158 ;; Preconfigured `anything' for opening files.
159 ;; `anything-info-at-point'
160 ;; Preconfigured `anything' for searching info at point.
161 ;; `anything-show-kill-ring'
162 ;; Show `kill-ring'. It is drop-in replacement of `yank-pop'.
163 ;; `anything-minibuffer-history'
164 ;; Show `minibuffer-history'.
165 ;; `anything-gentoo'
166 ;; Start anything with only gentoo sources.
167 ;; `anything-kill-buffers'
168 ;; You can continuously kill buffer you selected.
169 ;; `anything-insert-buffer-name'
170 ;; Insert buffer name.
171 ;; `anything-insert-symbol'
172 ;; Insert current symbol.
173 ;; `anything-insert-selection'
174 ;; Insert current selection.
175 ;; `anything-show-buffer-only'
176 ;; Only show sources about buffer.
177 ;; `anything-show-bbdb-only'
178 ;; Only show sources about BBDB.
179 ;; `anything-show-locate-only'
180 ;; Only show sources about Locate.
181 ;; `anything-show-info-only'
182 ;; Only show sources about Info.
183 ;; `anything-show-imenu-only'
184 ;; Only show sources about Imenu.
185 ;; `anything-show-files-only'
186 ;; Only show sources about File.
187 ;; `anything-show-w3m-bookmarks-only'
188 ;; Only show source about w3m bookmark.
189 ;; `anything-show-colors-only'
190 ;; Only show source about color.
191 ;; `anything-show-kill-ring-only'
192 ;; Only show source about kill ring.
193 ;; `anything-show-this-source-only'
194 ;; Only show this source.
195 ;; `anything-test-sources'
196 ;; List all anything sources for test.
197 ;; `anything-select-source'
198 ;; Select source.
199 ;; `anything-call-source'
200 ;; Call anything source.
201 ;; `anything-call-source-from-anything'
202 ;; Call anything source within `anything' session.
203 ;; `anything-create-from-anything'
204 ;; Run `anything-create' from `anything' as a fallback.
205 ;; `anything-create'
206 ;; Do many create actions from STRING.
207 ;; `anything-c-set-variable'
208 ;; Set value to VAR interactively.
209 ;; `anything-c-adaptive-save-history'
210 ;; Save history information to file given by `anything-c-adaptive-history-file'.
212 ;;; Customizable Options:
214 ;; Below are customizable option list:
216 ;; `anything-c-use-standard-keys'
217 ;; Whether use standard keybindings. (no effect)
218 ;; default = nil
219 ;; `anything-c-adaptive-history-file'
220 ;; Path of file where history information is stored.
221 ;; default = "~/.emacs.d/anything-c-adaptive-history"
222 ;; `anything-c-adaptive-history-length'
223 ;; Maximum number of candidates stored for a source.
224 ;; default = 50
225 ;; `anything-c-google-suggest-url'
226 ;; URL used for looking up suggestions.
227 ;; default = "http://www.google.com/complete/search?hl=en&js=true&qu="
228 ;; `anything-c-google-suggest-search-url'
229 ;; URL used for searching.
230 ;; default = "http://www.google.com/search?ie=utf-8&oe=utf-8&q="
231 ;; `anything-c-boring-buffer-regexp'
232 ;; The regexp that match boring buffers.
233 ;; default = (rx (or (group bos " ") "*anything" " *Echo Area" " *Minibuf"))
234 ;; `anything-c-boring-file-regexp'
235 ;; The regexp that match boring files.
236 ;; default = (rx (or (and "/" ... ...) (and line-start ".#") (and ... eol)))
237 ;; `anything-kill-ring-threshold'
238 ;; *Minimum length to be listed by `anything-c-source-kill-ring'.
239 ;; default = 10
240 ;; `anything-su-or-sudo'
241 ;; What command to use for root access.
242 ;; default = "su"
243 ;; `anything-create--actions-private'
244 ;; User defined actions for `anything-create' / `anything-c-source-create'.
245 ;; default = nil
247 ;;; Change log:
249 ;; Change log of this file is found at
250 ;; http://repo.or.cz/w/anything-config.git?a=shortlog;h=b30091a6bb64828eb3d70007db5b68d51b868bcc
252 ;;; Contributors:
254 ;; Tamas Patrovics
255 ;; Tassilo Horn <tassilo@member.fsf.org>
256 ;; Vagn Johansen <gonz808@hotmail.com>
257 ;; Mathias Dahl <mathias.dahl@gmail.com>
258 ;; Bill Clementson <billclem@gmail.com>
259 ;; Stefan Kamphausen (see http://www.skamphausen.de for more informations)
260 ;; Drew Adams <drew.adams@oracle.com>
261 ;; Jason McBrayer <jmcbray@carcosa.net>
262 ;; Andy Stewart <lazycat.manatee@gmail.com>
263 ;; Thierry Volpiatto <thierry.volpiatto@gmail.com>
264 ;; rubikitch <rubikitch@ruby-lang.org>
265 ;; Scott Vokes <vokes.s@gmail.com>
268 ;;; For Maintainers:
270 ;; Evaluate (anything-c-insert-summary) before commit. This function
271 ;; generates anything-c-source-* list.
273 ;; Install also http://www.emacswiki.org/emacs/auto-document.el
274 ;; And eval it or run interactively.
276 ;; [EVAL IT] (anything-c-insert-summary)
277 ;; [EVAL IT] (auto-document)
279 ;; Please write details documentation about function, then others will
280 ;; read code more easier. -- Andy Stewart
284 ;;; TODO
286 ;; - anything-c-adaptive stores infos for sources/types that don't have
287 ;; set it as `filtered-candidate-transformer'.
289 ;; - Fix documentation, now many functions haven't documentations.
292 ;;; Require
293 (require 'anything)
294 (require 'thingatpt)
296 ;;; Code:
298 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Customize ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
299 (defgroup anything-config nil
300 "Predefined configurations for `anything.el'."
301 :group 'anything)
303 (defcustom anything-c-use-standard-keys nil
304 "Whether use standard keybindings. (no effect)
306 Key definitions in anything-config.el are removed because
307 anything.el uses Emacs-standard keys by default. e.g. M-p/M-n for
308 minibuffer history, C-s for isearch, etc.
310 If you use `iswitchb' with `anything',
311 evaluate (anything-iswitchb-setup) . Then some bindings that
312 conflict with `iswitchb', e.g. C-p/C-n for the minibuffer
313 history, are removed from `anything-map'. "
314 :type 'boolean
315 :group 'anything-config)
317 (defcustom anything-c-adaptive-history-file "~/.emacs.d/anything-c-adaptive-history"
318 "Path of file where history information is stored."
319 :type 'string
320 :group 'anything-config)
322 (defcustom anything-c-adaptive-history-length 50
323 "Maximum number of candidates stored for a source."
324 :type 'number
325 :group 'anything-config)
327 (defcustom anything-c-google-suggest-url
328 "http://www.google.com/complete/search?hl=en&js=true&qu="
329 "URL used for looking up suggestions."
330 :type 'string
331 :group 'anything-config)
333 (defcustom anything-c-google-suggest-search-url
334 "http://www.google.com/search?ie=utf-8&oe=utf-8&q="
335 "URL used for searching."
336 :type 'string
337 :group 'anything-config)
339 (defcustom anything-c-boring-buffer-regexp
340 (rx (or
341 (group bos " ")
342 ;; anything-buffer
343 "*anything"
344 ;; echo area
345 " *Echo Area" " *Minibuf"))
346 "The regexp that match boring buffers.
347 Buffer candidates matching this regular expression will be
348 filtered from the list of candidates if the
349 `anything-c-skip-boring-buffers' candidate transformer is used, or
350 they will be displayed with face `file-name-shadow' if
351 `anything-c-shadow-boring-buffers' is used."
352 :type 'string
353 :group 'anything-config)
354 ;; (string-match anything-c-boring-buffer-regexp "buf")
355 ;; (string-match anything-c-boring-buffer-regexp " hidden")
356 ;; (string-match anything-c-boring-buffer-regexp " *Minibuf-1*")
358 (defcustom anything-c-boring-file-regexp
359 (rx (or
360 ;; Boring directories
361 (and "/" (or ".svn" "CVS" "_darcs" ".git" ".hg") (or "/" eol))
362 ;; Boring files
363 (and line-start ".#")
364 (and (or ".class" ".la" ".o" "~") eol)))
365 "The regexp that match boring files.
366 File candidates matching this regular expression will be
367 filtered from the list of candidates if the
368 `anything-c-skip-boring-files' candidate transformer is used, or
369 they will be displayed with face `file-name-shadow' if
370 `anything-c-shadow-boring-files' is used."
371 :type 'string
372 :group 'anything-config)
374 (defcustom anything-kill-ring-threshold 10
375 "*Minimum length to be listed by `anything-c-source-kill-ring'."
376 :type 'integer
377 :group 'anything-config)
379 (defcustom anything-su-or-sudo "su"
380 "What command to use for root access."
381 :type 'string
382 :group 'anything-config)
385 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Preconfigured Anything ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
386 (defun anything-for-files ()
387 "Preconfigured `anything' for opening files.
388 ffap -> recentf -> buffer -> bookmark -> file-cache -> files-in-current-dir -> locate"
389 (interactive)
390 (anything '(anything-c-source-ffap-line
391 anything-c-source-ffap-guesser
392 anything-c-source-recentf
393 anything-c-source-buffers+
394 anything-c-source-bookmarks
395 anything-c-source-file-cache
396 anything-c-source-files-in-current-dir+
397 anything-c-source-locate)))
399 (defun anything-info-at-point ()
400 "Preconfigured `anything' for searching info at point."
401 (interactive)
402 (anything '(anything-c-source-info-elisp
403 anything-c-source-info-cl
404 anything-c-source-info-pages)
405 (thing-at-point 'symbol)))
407 (defun anything-show-kill-ring ()
408 "Show `kill-ring'. It is drop-in replacement of `yank-pop'.
409 You may bind this command to M-y."
410 (interactive)
411 (anything 'anything-c-source-kill-ring nil nil nil nil "*anything kill-ring*"))
413 (defun anything-minibuffer-history ()
414 "Show `minibuffer-history'.
415 You may bind this command to C-r in minibuffer-local-map / minibuffer-local-completion-map."
416 (interactive)
417 (anything 'anything-c-source-minibuffer-history nil nil nil nil
418 "*anything minibuffer-history*"))
419 ;; (define-key minibuffer-local-map "\C-r" 'anything-minibuffer-history)
420 ;; (define-key minibuffer-local-completion-map "\C-r" 'anything-minibuffer-history)
422 (defun anything-gentoo ()
423 "Start anything with only gentoo sources."
424 (interactive)
425 (anything '(anything-c-source-gentoo
426 anything-c-source-use-flags)))
428 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Anything Applications ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
429 (defun anything-kill-buffers ()
430 "You can continuously kill buffer you selected."
431 (interactive)
432 (anything
433 '(((name . "Kill Buffers")
434 (candidates . anything-c-buffer-list)
435 (action
436 ("Kill Buffer" . (lambda (candidate)
437 (kill-buffer candidate)
438 (anything-kill-buffers)
439 )))))
440 nil nil))
442 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Interactive Functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
444 (defun anything-insert-buffer-name ()
445 "Insert buffer name."
446 (interactive)
447 (anything-insert-string
448 (with-current-buffer anything-current-buffer
449 (if buffer-file-name (file-name-nondirectory buffer-file-name)
450 (buffer-name)))))
452 (defun anything-insert-symbol ()
453 "Insert current symbol."
454 (interactive)
455 (anything-insert-string
456 (with-current-buffer anything-current-buffer
457 (save-excursion
458 (buffer-substring (beginning-of-thing 'symbol)
459 (end-of-thing 'symbol))))))
461 (defun anything-insert-selection ()
462 "Insert current selection."
463 (interactive)
464 (anything-insert-string
465 (with-current-buffer anything-current-buffer
466 (anything-get-selection))))
468 (defun anything-show-buffer-only ()
469 "Only show sources about buffer."
470 (interactive)
471 (anything-set-source-filter '("Buffers")))
473 (defun anything-show-bbdb-only ()
474 "Only show sources about BBDB."
475 (interactive)
476 (anything-set-source-filter '("BBDB")))
478 (defun anything-show-locate-only ()
479 "Only show sources about Locate."
480 (interactive)
481 (anything-set-source-filter '("Locate")))
483 (defun anything-show-info-only ()
484 "Only show sources about Info."
485 (interactive)
486 (anything-set-source-filter '("Info Pages"
487 "Info Elisp"
488 "Info Common-Lisp")))
490 (defun anything-show-imenu-only ()
491 "Only show sources about Imenu."
492 (interactive)
493 (anything-set-source-filter '("Imenu")))
495 (defun anything-show-files-only ()
496 "Only show sources about File."
497 (interactive)
498 (anything-set-source-filter '("File Name History"
499 "Files from Current Directory"
500 "Recentf")))
502 (defun anything-show-w3m-bookmarks-only ()
503 "Only show source about w3m bookmark."
504 (interactive)
505 (anything-set-source-filter '("W3m Bookmarks")))
507 (defun anything-show-colors-only ()
508 "Only show source about color."
509 (interactive)
510 (anything-set-source-filter '("Colors"
511 "Customize Faces")))
513 (defun anything-show-kill-ring-only ()
514 "Only show source about kill ring."
515 (interactive)
516 (anything-set-source-filter '("Kill Ring")))
518 (defun anything-show-this-source-only ()
519 "Only show this source."
520 (interactive)
521 (setq anything-candidate-number-limit 9999)
522 (anything-set-source-filter
523 (list (assoc-default 'name (anything-get-current-source)))))
525 (defun anything-test-sources ()
526 "List all anything sources for test.
527 The output is sexps which are evaluated by \\[eval-last-sexp]."
528 (interactive)
529 (with-output-to-temp-buffer "*Anything Test Sources*"
530 (mapc (lambda (s) (princ (format ";; (anything '%s)\n" s)))
531 (apropos-internal "^anything-c-source" #'boundp))
532 (pop-to-buffer standard-output)))
534 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Utilities Functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
535 (defun anything-nest (&rest same-as-anything)
536 "Nested `anything'. If you use `anything' within `anything', use it."
537 (with-selected-window (anything-window)
538 (let (anything-current-position
539 anything-current-buffer
540 (orig-anything-buffer anything-buffer)
541 anything-pattern
542 anything-buffer
543 anything-sources
544 anything-compiled-sources
545 anything-buffer-chars-modified-tick
546 (anything-samewindow t)
547 (enable-recursive-minibuffers t))
548 (unwind-protect
549 (apply #'anything same-as-anything)
550 (anything-initialize-overlays orig-anything-buffer)
551 (add-hook 'post-command-hook 'anything-check-minibuffer-input)))))
553 (defun anything-displaying-source-names ()
554 "Display sources name."
555 (with-current-buffer anything-buffer
556 (goto-char (point-min))
557 (loop with pos
558 while (setq pos (next-single-property-change (point) 'anything-header))
559 do (goto-char pos)
560 collect (buffer-substring-no-properties (point-at-bol)(point-at-eol))
561 do (forward-line 1))))
563 (defun anything-select-source ()
564 "Select source."
565 (interactive)
566 (let ((default (assoc-default 'name (anything-get-current-source)))
567 (source-names (anything-displaying-source-names))
568 (all-source-names (mapcar (lambda (s) (assoc-default 'name s))
569 (anything-get-sources))))
570 (setq anything-candidate-number-limit 9999)
571 (anything-aif
572 (let (anything-source-filter)
573 (anything-nest '(((name . "Anything Source")
574 (candidates . source-names)
575 (action . identity))
576 ((name . "Anything Source (ALL)")
577 (candidates . all-source-names)
578 (action . identity)))
579 nil "Source: " nil
580 default "*anything select source*"))
581 (anything-set-source-filter (list it))
582 (anything-set-source-filter nil))))
584 (defun anything-insert-string (str)
585 "Insert STR."
586 (delete-minibuffer-contents)
587 (insert str))
589 (defun anything-c-match-on-file-name (candidate)
590 "Return non-nil if `anything-pattern' match the filename (without directory part) of CANDIDATE."
591 (string-match anything-pattern (file-name-nondirectory candidate)))
593 (defun anything-c-match-on-directory-name (candidate)
594 "Return non-nil if `anything-pattern' match the directory part of CANDIDATE (a file)."
595 (anything-aif (file-name-directory candidate)
596 (string-match anything-pattern it)))
598 (defun anything-c-string-match (candidate)
599 "Return non-nil if `anything-pattern' match CANDIDATE.
600 The match is done with `string-match'."
601 (string-match anything-pattern candidate))
603 ;; `anything-c-compose' is no more needed, it is for compatibility.
604 (defalias 'anything-c-compose 'anything-compose)
606 (defun anything-c-skip-entries (list regexp)
607 "Remove entries which matches REGEXP from LIST."
608 (remove-if (lambda (x) (and (stringp x) (string-match regexp x)))
609 list))
611 (defun anything-c-shadow-entries (list regexp)
612 "Elements of LIST matching REGEXP will be displayed with the `file-name-shadow' face if available."
613 (mapcar (lambda (file)
614 ;; Add shadow face property to boring files.
615 (let ((face (if (facep 'file-name-shadow)
616 'file-name-shadow
617 ;; fall back to default on XEmacs
618 'default)))
619 (if (string-match regexp file)
620 (setq file (propertize file 'face face))))
621 file)
622 list))
624 (defsubst anything-c-stringify (str-or-sym)
625 "Get string of STR-OR-SYM."
626 (if (stringp str-or-sym)
627 str-or-sym
628 (symbol-name str-or-sym)))
630 (defsubst anything-c-symbolify (str-or-sym)
631 "Get symbol of STR-OR-SYM."
632 (if (symbolp str-or-sym)
633 str-or-sym
634 (intern str-or-sym)))
636 (defun anything-c-describe-function (func)
637 "FUNC is symbol or string."
638 (describe-function (anything-c-symbolify func)))
640 (defun anything-c-describe-variable (var)
641 "VAR is symbol or string."
642 (describe-variable (anything-c-symbolify var)))
644 (defun anything-c-find-function (func)
645 "FUNC is symbol or string."
646 (find-function (anything-c-symbolify func)))
648 (defun anything-c-find-variable (var)
649 "VAR is symbol or string."
650 (find-variable (anything-c-symbolify var)))
652 (defun anything-c-kill-new (string &optional replace yank-handler)
653 "STRING is symbol or string."
654 (kill-new (anything-c-stringify string) replace yank-handler))
656 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Prefix argument in action ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
657 ;; TODO
658 (defvar anything-current-prefix-arg nil
659 "`current-prefix-arg' when selecting action.
660 It is cleared after executing action.")
662 (defadvice anything-exit-minibuffer (before anything-current-prefix-arg activate)
663 (unless anything-current-prefix-arg
664 (setq anything-current-prefix-arg current-prefix-arg)))
666 (add-hook 'anything-after-action-hook
667 (lambda () (setq anything-current-prefix-arg nil)))
670 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Document Generator ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
671 (defun anything-c-create-summary ()
672 "Create `anything' summary."
673 (save-excursion
674 (goto-char (point-min))
675 (loop while (re-search-forward "^;;;; <\\(.+?\\)>$\\|^;; (anything '\\(.+?\\))$" nil t)
676 collect (if (match-beginning 1)
677 (cons 'section (match-string-no-properties 1))
678 (cons 'source
679 (cons (match-string-no-properties 2)
680 (assoc-default 'name (symbol-value (intern (match-string-no-properties 2))))))))))
681 ;; (find-epp (anything-c-create-summary))
683 (defun anything-c-insert-summary ()
684 "Insert `anything' summary."
685 (save-excursion
686 (goto-char (point-min))
687 (search-forward ";; Below are complete source list you can setup in")
688 (forward-line 1)
689 (delete-region (point)
690 (progn (search-forward ";;; Change log:" nil t)
691 (forward-line -1) (point)))
692 (insert ";;\n")
693 (loop with beg
694 for (kind . value) in (anything-c-create-summary)
695 for i from 0
696 do (cond ((eq kind 'section)
697 (unless (zerop i)
698 (align-regexp beg (point) "\\(\\s-*\\)(" 1 1 nil))
699 (insert ";; " value ":\n")
700 (setq beg (point)))
702 (insert ";; `" (car value) "' (" (cdr value) ")\n")))
703 finally (align-regexp beg (point) "\\(\\s-*\\)(" 1 1 nil))))
704 ;; (anything-c-insert-summary)
706 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Anything Sources ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
707 ;;;; <Buffer>
708 (defun anything-c-buffer-list ()
709 "Return the list of names of buffers with boring buffers filtered out.
710 Boring buffers is specified by `anything-c-boring-buffer-regexp'.
711 The first buffer in the list will be the last recently used
712 buffer that is not the current buffer."
713 (let ((buffers (mapcar 'buffer-name (buffer-list))))
714 (append (cdr buffers) (list (car buffers)))))
716 (defvar anything-c-source-buffers
717 '((name . "Buffers")
718 (candidates . anything-c-buffer-list)
719 (volatile)
720 (type . buffer)))
721 ;; (anything 'anything-c-source-buffers)
723 (defvar anything-c-source-buffer-not-found
724 '((name . "Create buffer")
725 (dummy)
726 (type . buffer)))
727 ;; (anything 'anything-c-source-buffer-not-found)
729 ;;; Buffers+
730 (defface anything-dir-heading '((t (:foreground "Blue" :background "Pink")))
731 "*Face used for directory headings in dired buffers."
732 :group 'anything)
734 (defface anything-file-name
735 '((t (:foreground "Blue")))
736 "*Face used for file names (without suffixes) in dired buffers."
737 :group 'anything)
739 (defface anything-dir-priv
740 '((t (:foreground "DarkRed" :background "LightGray")))
741 "*Face used for directory privilege indicator (d) in dired buffers."
742 :group 'anything)
744 (defvar anything-c-buffers-face1 'anything-dir-priv)
745 (defvar anything-c-buffers-face2 'font-lock-type-face)
746 (defvar anything-c-buffers-face3 'italic)
747 (eval-when-compile (require 'dired))
748 (defun anything-c-highlight-buffers (buffers)
749 (require 'dired)
750 (loop for i in buffers
751 if (rassoc (get-buffer i) dired-buffers)
752 collect (propertize i
753 'face anything-c-buffers-face1
754 'help-echo (car (rassoc (get-buffer i) dired-buffers)))
755 if (buffer-file-name (get-buffer i))
756 collect (propertize i
757 'face anything-c-buffers-face2
758 'help-echo (buffer-file-name (get-buffer i)))
759 if (and (not (rassoc (get-buffer i) dired-buffers))
760 (not (buffer-file-name (get-buffer i))))
761 collect (propertize i
762 'face anything-c-buffers-face3)))
764 (defvar anything-c-source-buffers+
765 '((name . "Buffers")
766 (candidates . anything-c-buffer-list)
767 (volatile)
768 (type . buffer)
769 (candidate-transformer anything-c-highlight-buffers
770 anything-c-skip-boring-buffers)
771 (persistent-action . anything-c-buffers+-persistent-action)))
773 (defun anything-c-buffers+-persistent-action (name)
774 (flet ((kill (item)
775 (with-current-buffer item
776 (if (and (buffer-modified-p)
777 (buffer-file-name (current-buffer)))
778 (progn
779 (save-buffer)
780 (kill-buffer item))
781 (kill-buffer item))))
782 (goto (item)
783 (switch-to-buffer item)))
784 (if current-prefix-arg
785 (progn
786 (kill name)
787 (anything-delete-current-selection))
788 (goto name))))
790 ;; (anything 'anything-c-source-buffers+)
793 ;;;; <File>
794 ;;; File name history
795 (defvar anything-c-source-file-name-history
796 '((name . "File Name History")
797 (candidates . file-name-history)
798 (match anything-c-match-on-file-name
799 anything-c-match-on-directory-name)
800 (type . file)))
801 ;; (anything 'anything-c-source-file-name-history)
803 ;;; Files in current dir
804 (defvar anything-c-source-files-in-current-dir
805 '((name . "Files from Current Directory")
806 (init . (lambda () (setq anything-c-default-directory default-directory)))
807 (candidates . (lambda () (directory-files anything-c-default-directory)))
808 (volatile)
809 (type . file)))
810 ;; (anything 'anything-c-source-files-in-current-dir)
812 (defvar anything-c-files-face1 'anything-dir-priv)
813 (defvar anything-c-files-face2 'anything-file-name)
814 (defun anything-c-highlight-files (files)
815 (loop for i in files
816 if (file-directory-p i)
817 collect (propertize (file-name-nondirectory i)
818 'face anything-c-files-face1
819 'help-echo (expand-file-name i))
820 else
821 collect (propertize (file-name-nondirectory i)
822 'face anything-c-files-face2
823 'help-echo (expand-file-name i))))
826 (defvar anything-c-source-files-in-current-dir+
827 '((name . "Files from Current Directory")
828 (init . (lambda ()
829 (setq anything-c-default-directory
830 (expand-file-name default-directory))))
831 (candidates . (lambda ()
832 (directory-files
833 anything-c-default-directory t)))
834 (candidate-transformer anything-c-highlight-files)
835 (volatile)
836 (type . file)))
838 ;; (anything 'anything-c-source-files-in-current-dir+)
840 ;;; File Cache
841 (defvar anything-c-source-file-cache-initialized nil)
843 (defvar anything-c-file-cache-files nil)
845 (defvar anything-c-source-file-cache
846 '((name . "File Cache")
847 (init . (lambda ()
848 (require 'filecache nil t)
849 (unless anything-c-source-file-cache-initialized
850 (setq anything-c-file-cache-files
851 (loop for item in file-cache-alist append
852 (destructuring-bind (base &rest dirs) item
853 (loop for dir in dirs collect
854 (concat dir base)))))
855 (defadvice file-cache-add-file (after file-cache-list activate)
856 (add-to-list 'anything-c-file-cache-files (expand-file-name file)))
857 (setq anything-c-source-file-cache-initialized t))))
858 (candidates . anything-c-file-cache-files)
859 (match anything-c-match-on-file-name
860 anything-c-match-on-directory-name)
861 (type . file)))
862 ;; (anything 'anything-c-source-file-cache)
864 ;;; Locate
865 (defvar anything-c-locate-options
866 (cond
867 ((eq system-type 'darwin) '("locate"))
868 ((eq system-type 'berkeley-unix) '("locate" "-i"))
869 (t '("locate" "-i" "-r")))
870 "A list where the `car' is the name of the locat program followed by options.
871 The search pattern will be appended, so the
872 \"-r\" option should be the last option.")
874 (defvar anything-c-source-locate
875 '((name . "Locate")
876 (candidates . (lambda ()
877 (apply 'start-process "locate-process" nil
878 (append anything-c-locate-options
879 (list anything-pattern)))))
880 (type . file)
881 (requires-pattern . 3)
882 (delayed))
883 "Source for retrieving files matching the current input pattern with locate.")
884 ;; (anything 'anything-c-source-locate)
886 ;;; Recentf files
887 (defvar anything-c-source-recentf
888 '((name . "Recentf")
889 (init . (lambda ()
890 (require 'recentf)
891 (or recentf-mode (recentf-mode 1))
892 ;; Big value empowers anything/recentf
893 (when (and (numberp recentf-max-saved-items)
894 (<= recentf-max-saved-items 20))
895 (setq recentf-max-saved-items 500))))
896 (candidates . recentf-list)
897 (match anything-c-match-on-file-name
898 anything-c-match-on-directory-name)
899 (type . file))
900 "See (info \"(emacs)File Conveniences\").
901 if `recentf-max-saved-items' is too small, set it to 500.")
902 ;; (anything 'anything-c-source-recentf)
904 ;;; ffap
905 (eval-when-compile (require 'ffap))
906 (defvar anything-c-source-ffap-guesser
907 '((name . "File at point")
908 (init . (lambda () (require 'ffap)))
909 (candidates . (lambda ()
910 (anything-aif
911 (with-current-buffer anything-current-buffer
912 (ffap-guesser))
913 (list it))))
914 (type . file)))
915 ;; (anything 'anything-c-source-ffap-guesser)
917 ;;; ffap with line number
918 (defun anything-c-ffap-file-line-at-point ()
919 "Get (FILENAME . LINENO) at point."
920 (anything-aif (let (ffap-alist) (ffap-file-at-point))
921 (save-excursion
922 (beginning-of-line)
923 (when (and (search-forward it nil t)
924 (looking-at ":\\([0-9]+\\)"))
925 (cons it (string-to-number (match-string 1)))))))
927 (defvar anything-c-ffap-line-location nil
928 "(FILENAME . LINENO) used by `anything-c-source-ffap-line'.
929 It is cleared after jumping line.")
931 (defun anything-c-ffap-line-candidates ()
932 (with-current-buffer anything-current-buffer
933 (setq anything-c-ffap-line-location (anything-c-ffap-file-line-at-point)))
934 (when anything-c-ffap-line-location
935 (destructuring-bind (file . line) anything-c-ffap-line-location
936 (list (cons (format "%s (line %d)" file line) file)))))
938 ;;; Goto line after opening file by `anything-c-source-ffap-line'.
939 (defun anything-c-ffap-line-goto-line ()
940 (when (car anything-c-ffap-line-location)
941 (unwind-protect
942 (ignore-errors
943 (with-selected-window (get-buffer-window
944 (get-file-buffer (car anything-c-ffap-line-location)))
945 (goto-line (cdr anything-c-ffap-line-location))))
946 (setq anything-c-ffap-line-location nil))))
947 (add-hook 'anything-after-action-hook 'anything-c-ffap-line-goto-line)
949 (defvar anything-c-source-ffap-line
950 '((name . "File/Lineno at point")
951 (init . (lambda () (require 'ffap)))
952 (candidates . anything-c-ffap-line-candidates)
953 (type . file)))
954 ;; (anything 'anything-c-source-ffap-line)
957 ;;;; <Help>
958 ;;; Man Pages
959 (defvar anything-c-man-pages nil
960 "All man pages on system.
961 Will be calculated the first time you invoke anything with this
962 source.")
964 (defvar anything-c-source-man-pages
965 `((name . "Manual Pages")
966 (candidates . (lambda ()
967 (if anything-c-man-pages
968 anything-c-man-pages
969 ;; XEmacs doesn't have a woman :)
970 (setq anything-c-man-pages
971 (ignore-errors
972 (require 'woman)
973 (woman-file-name "")
974 (sort (mapcar 'car woman-topic-all-completions)
975 'string-lessp))))))
976 (action ("Show with Woman" . woman))
977 (requires-pattern . 2)))
978 ;; (anything 'anything-c-source-man-pages)
980 ;;; Info pages
981 (defvar anything-c-info-pages nil
982 "All info pages on system.
983 Will be calculated the first time you invoke anything with this
984 source.")
986 (defvar anything-c-source-info-pages
987 `((name . "Info Pages")
988 (candidates . (lambda ()
989 (if anything-c-info-pages
990 anything-c-info-pages
991 (setq anything-c-info-pages
992 (save-window-excursion
993 (save-excursion
994 (require 'info)
995 (Info-find-node "dir" "top")
996 (goto-char (point-min))
997 (let ((info-topic-regexp "\\* +\\([^:]+: ([^)]+)[^.]*\\)\\.")
998 topics)
999 (while (re-search-forward info-topic-regexp nil t)
1000 (add-to-list 'topics (match-string-no-properties 1)))
1001 (goto-char (point-min))
1002 (Info-exit)
1003 topics)))))))
1004 (action . (("Show with Info" .(lambda (node-str)
1005 (info (replace-regexp-in-string "^[^:]+: "
1007 node-str))))))
1008 (requires-pattern . 2)))
1009 ;; (anything 'anything-c-source-info-pages)
1011 ;; Info Elisp
1012 (defvar anything-c-info-elisp nil)
1013 (defvar anything-c-source-info-elisp
1014 `((name . "Info Elisp")
1015 (init . (lambda ()
1016 (save-window-excursion
1017 (unless anything-c-info-elisp
1018 (with-temp-buffer
1019 (Info-find-node "elisp" "Index")
1020 (setq anything-c-info-elisp (split-string (buffer-string) "\n"))
1021 (Info-exit))))))
1022 (candidates . (lambda ()
1023 (loop for i in anything-c-info-elisp
1024 if (string-match "^* [^ \n]+[^: ]" i)
1025 collect (match-string 0 i))))
1026 (action . (lambda (candidate)
1027 (Info-find-node "elisp" "Index")
1028 (Info-index (replace-regexp-in-string "* " "" candidate))))
1029 (volatile)
1030 (requires-pattern . 2)))
1031 ;; (anything 'anything-c-source-info-elisp)
1033 ;; Info-Common-Lisp
1034 (defvar anything-c-info-cl-fn nil)
1035 (defvar anything-c-source-info-cl
1036 `((name . "Info Common-Lisp")
1037 (init . (lambda ()
1038 (save-window-excursion
1039 (unless anything-c-info-cl-fn
1040 (with-temp-buffer
1041 (Info-find-node "cl" "Function Index")
1042 (setq anything-c-info-cl-fn (split-string (buffer-string) "\n"))
1043 (Info-exit))))))
1044 (candidates . (lambda ()
1045 (loop for i in anything-c-info-cl-fn
1046 if (string-match "^* [^ \n]+[^: ]" i)
1047 collect (match-string 0 i))))
1048 (action . (lambda (candidate)
1049 (Info-find-node "cl" "Function Index")
1050 (Info-index (replace-regexp-in-string "* " "" candidate))))
1051 (volatile)
1052 (requires-pattern . 2)))
1053 ;; (anything 'anything-c-source-info-cl)
1055 ;;;; <Command>
1056 ;;; Complex command history
1057 (defvar anything-c-source-complex-command-history
1058 '((name . "Complex Command History")
1059 (candidates . (lambda () (mapcar 'prin1-to-string command-history)))
1060 (type . sexp)))
1061 ;; (anything 'anything-c-source-complex-command-history)
1063 ;;; M-x history
1064 (defvar anything-c-source-extended-command-history
1065 '((name . "Emacs Commands History")
1066 (candidates . extended-command-history)
1067 (type . command)))
1068 ;; (anything 'anything-c-source-extended-command-history)
1070 ;;; Emacs commands
1071 (defvar anything-c-source-emacs-commands
1072 '((name . "Emacs Commands")
1073 (candidates . (lambda ()
1074 (let (commands)
1075 (mapatoms (lambda (a)
1076 (if (commandp a)
1077 (push (symbol-name a)
1078 commands))))
1079 (sort commands 'string-lessp))))
1080 (volatile)
1081 (type . command)
1082 (requires-pattern . 2))
1083 "Source for completing and invoking Emacs commands.
1084 A command is a function with interactive spec that can
1085 be invoked with `M-x'.
1087 To get non-interactive functions listed, use
1088 `anything-c-source-emacs-functions'.")
1089 ;; (anything 'anything-c-source-emacs-commands)
1091 ;;; LaCarte
1092 (defvar anything-c-source-lacarte
1093 '((name . "Lacarte")
1094 (init . (lambda () (require 'lacarte )))
1095 (candidates . (lambda () (delete '(nil) (lacarte-get-overall-menu-item-alist))))
1096 (candidate-number-limit . 9999)
1097 (action . anything-c-call-interactively))
1098 "Needs lacarte.el.
1100 http://www.emacswiki.org/cgi-bin/wiki/download/lacarte.el")
1101 ;; (anything 'anything-c-source-lacarte)
1103 ;;;; <Function>
1104 ;;; Emacs functions
1105 (defvar anything-c-source-emacs-functions
1106 '((name . "Emacs Functions")
1107 (candidates . (lambda ()
1108 (let (commands)
1109 (mapatoms (lambda (a) (if (functionp a)
1110 (push (symbol-name a) commands))))
1111 (sort commands 'string-lessp))))
1112 (volatile)
1113 (type . function)
1114 (requires-pattern . 2))
1115 "Source for completing Emacs functions.")
1116 ;; (anything 'anything-c-source-emacs-functions)
1118 ;;; With abbrev expansion
1119 ;;; Similar to my exec-abbrev-cmd.el
1120 ;;; See http://www.tsdh.de/cgi-bin/wiki.pl/exec-abbrev-cmd.el
1121 (defvar anything-c-function-abbrev-regexp nil
1122 "The regexp for `anything-c-source-emacs-functions-with-abbrevs'.
1123 Regexp built from the current `anything-pattern' interpreting it
1124 as abbreviation.
1125 Only for internal use.")
1127 (defun anything-c-match-function-by-abbrev (candidate)
1128 "Return non-nil if `anything-pattern' is an abbreviation of the function CANDIDATE.
1130 Abbreviations are made by taking the first character from each
1131 word in the function's name, e.g. \"bb\" is an abbrev for
1132 `bury-buffer', \"stb\" is an abbrev for `switch-to-buffer'."
1133 (string-match anything-c-function-abbrev-regexp candidate))
1135 (defvar anything-c-source-emacs-functions-with-abbrevs
1136 (append anything-c-source-emacs-functions
1137 '((match anything-c-match-function-by-abbrev
1138 anything-c-string-match))
1139 '((init . (lambda ()
1140 (defadvice anything-update
1141 (before anything-c-update-function-abbrev-regexp activate)
1142 (let ((char-list (append anything-pattern nil))
1143 (str "^"))
1144 (dolist (c char-list)
1145 (setq str (concat str (list c) "[^-]*-")))
1146 (setq str (concat (substring str 0 (1- (length str))) "$"))
1147 (setq anything-c-function-abbrev-regexp str))))))))
1148 ;; (anything 'anything-c-source-emacs-functions-with-abbrevs)
1150 ;;;; <Variable>
1151 ;;; Emacs variables
1152 (defvar anything-c-source-emacs-variables
1153 '((name . "Emacs Variables")
1154 (candidates . (lambda ()
1155 (sort (all-completions "" obarray 'boundp) 'string-lessp)))
1156 (type . variable)
1157 (requires-pattern . 2))
1158 "Source for completing Emacs variables.")
1159 ;; (anything 'anything-c-source-emacs-variables)
1161 ;;;; <Bookmark>
1162 ;;; Bookmarks
1163 (eval-when-compile (require 'bookmark))
1164 (defvar anything-c-source-bookmarks
1165 '((name . "Bookmarks")
1166 (init . (lambda ()
1167 (require 'bookmark)))
1168 (candidates . bookmark-all-names)
1169 (type . bookmark))
1170 "See (info \"(emacs)Bookmarks\").")
1171 ;; (anything 'anything-c-source-bookmarks)
1173 ;;; bookmark-set
1174 (defvar anything-c-source-bookmark-set
1175 '((name . "Set Bookmark")
1176 (dummy)
1177 (action . bookmark-set))
1178 "See (info \"(emacs)Bookmarks\").")
1179 ;; (anything 'anything-c-source-bookmark-set)
1181 ;;; Visible Bookmarks
1182 ;; (install-elisp "http://cvs.savannah.gnu.org/viewvc/*checkout*/bm/bm/bm.el")
1185 ;; http://d.hatena.ne.jp/grandVin/20080911/1221114327
1186 (defvar anything-c-source-bm
1187 '((name . "Visible Bookmarks")
1188 (init . anything-c-bm-init)
1189 (candidates-in-buffer)
1190 (type . line))
1191 "Needs bm.el.
1193 http://www.nongnu.org/bm/")
1195 (defun anything-c-bm-init ()
1196 "Init function for `anything-c-source-bm'."
1197 (when (require 'bm nil t)
1198 (with-no-warnings
1199 (let ((bookmarks (bm-lists))
1200 (buf (anything-candidate-buffer 'global)))
1201 (dolist (bm (sort* (append (car bookmarks) (cdr bookmarks))
1202 '< :key 'overlay-start))
1203 (let ((start (overlay-start bm))
1204 (end (overlay-end bm))
1205 (annotation (or (overlay-get bm 'annotation) "")))
1206 (unless (< (- end start) 1) ; org => (if (< (- end start) 2)
1207 (let ((str (format "%7d: [%s]: %s\n"
1208 (line-number-at-pos start)
1209 annotation
1210 (buffer-substring start (1- end)))))
1211 (with-current-buffer buf (insert str))))))))))
1213 ;;; Special bookmarks
1214 (defvar anything-c-source-bookmarks-ssh
1215 '((name . "Bookmarks-ssh")
1216 (init . (lambda ()
1217 (require 'bookmark)))
1218 ;; DRY
1219 (candidates . (lambda ()
1220 (let (lis-all lis-ssh)
1221 (setq lis-all (bookmark-all-names))
1222 (setq lis-ssh (loop for i in lis-all
1223 if (string-match "^(ssh)" i)
1224 collect i))
1225 (sort lis-ssh 'string-lessp))))
1226 (type . bookmark))
1227 "See (info \"(emacs)Bookmarks\").")
1228 ;; (anything 'anything-c-source-bookmarks-ssh)
1230 (defvar anything-c-source-bookmarks-su
1231 '((name . "Bookmarks-root")
1232 (init . (lambda ()
1233 (require 'bookmark)))
1234 ;; DRY
1235 (candidates . (lambda ()
1236 (let (lis-all lis-su)
1237 (setq lis-all (bookmark-all-names))
1238 (setq lis-su (loop for i in lis-all
1239 if (string-match (format "^(%s)" anything-su-or-sudo) i)
1240 collect i))
1241 (sort lis-su 'string-lessp))))
1242 (candidate-transformer anything-c-highlight-bookmark-su)
1244 (type . bookmark))
1245 "See (info \"(emacs)Bookmarks\").")
1246 ;; (anything 'anything-c-source-bookmarks-su)
1248 (defface anything-bookmarks-su-face '((t (:foreground "red")))
1249 "TRAVERSEDIR face."
1250 :group 'traverse-faces)
1252 (defvar anything-c-bookmarks-face1 'anything-dir-heading)
1253 (defvar anything-c-bookmarks-face2 'anything-file-name)
1254 (defvar anything-c-bookmarks-face3 'anything-bookmarks-su-face)
1256 (defun tv-root-logged-p ()
1257 (catch 'break
1258 (dolist (i (mapcar #'buffer-name (buffer-list)))
1259 (when (string-match (format "*tramp/%s ." anything-su-or-sudo) i)
1260 (throw 'break t)))))
1263 (defun anything-c-highlight-bookmark-su (files)
1264 (if (tv-root-logged-p)
1265 (anything-c-highlight-bookmark files)
1266 (anything-c-highlight-not-logged files)))
1268 (defun anything-c-highlight-not-logged (files)
1269 (loop for i in files
1270 collect (propertize i 'face anything-c-bookmarks-face3)))
1272 (defun anything-c-highlight-bookmark (files)
1273 (loop for i in files
1274 if (file-directory-p (bookmark-get-filename i))
1275 collect (propertize i 'face anything-c-bookmarks-face1)
1276 else
1277 collect (propertize i 'face anything-c-bookmarks-face2)))
1279 (defvar anything-c-source-bookmarks-local
1280 '((name . "Bookmarks-Local")
1281 (init . (lambda ()
1282 (require 'bookmark)))
1283 ;; DRY
1284 (candidates . (lambda ()
1285 (let (lis-all lis-loc)
1286 (setq lis-all (bookmark-all-names))
1287 (setq lis-loc (loop for i in lis-all
1288 if (and (not (string-match "^(ssh)" i))
1289 (not (string-match "^(su)" i)))
1290 collect i))
1291 (sort lis-loc 'string-lessp))))
1292 (candidate-transformer anything-c-highlight-bookmark)
1293 (type . bookmark))
1294 "See (info \"(emacs)Bookmarks\").")
1295 ;; (anything 'anything-c-source-bookmarks-local)
1298 ;; W3m bookmark
1299 (eval-when-compile (require 'w3m-bookmark nil t))
1300 (unless (and (require 'w3m nil t)
1301 (require 'w3m-bookmark nil t))
1302 (defvar w3m-bookmark-file "~/.w3m/bookmark.html"))
1303 ;; (defvar anything-w3m-bookmarks-regexp ">[^><]+[^</a>]+[a-z)0-9]+")
1305 (defface anything-w3m-bookmarks-face '((t (:foreground "cyan1" :underline t)))
1306 "Face for w3m bookmarks" :group 'anything)
1308 (defvar anything-w3m-bookmarks-regexp ">[^><]+.[^</a>]")
1309 (defun anything-w3m-bookmarks-to-alist ()
1310 (let (bookmarks-alist url title)
1311 (with-temp-buffer
1312 (insert-file-contents w3m-bookmark-file) ;; or w3m-bookmark-file
1313 (goto-char (point-min))
1314 (while (not (eobp))
1315 (forward-line)
1316 (when (re-search-forward "href=" nil t)
1317 (beginning-of-line)
1318 (when (re-search-forward "http://[^>]*" nil t)
1319 (setq url (concat "\"" (match-string 0))))
1320 (beginning-of-line)
1321 (when (re-search-forward anything-w3m-bookmarks-regexp nil t)
1322 (setq title (match-string 0)))
1323 (push (cons title url) bookmarks-alist))))
1324 (reverse bookmarks-alist)))
1326 (defvar anything-c-w3m-bookmarks-alist nil)
1327 (defvar anything-c-source-w3m-bookmarks
1328 '((name . "W3m Bookmarks")
1329 (init . (lambda ()
1330 (setq anything-c-w3m-bookmarks-alist
1331 (anything-w3m-bookmarks-to-alist))))
1332 (candidates . (lambda ()
1333 (mapcar #'car
1334 anything-c-w3m-bookmarks-alist)))
1335 (candidate-transformer anything-c-highlight-w3m-bookmarks)
1336 (action . (("Browse Url" . (lambda (candidate)
1337 (anything-c-w3m-browse-bookmark candidate)))
1338 ("Copy Url" . (lambda (elm)
1339 (kill-new (anything-c-w3m-bookmarks-get-value elm))))
1340 ("Browse Url Firefox" . (lambda (candidate)
1341 (anything-c-w3m-browse-bookmark candidate t)))
1342 ("Delete Bookmark" . (lambda (candidate)
1343 (anything-c-w3m-delete-bookmark candidate)))
1344 ("Rename Bookmark" . (lambda (candidate)
1345 (anything-c-w3m-rename-bookmark candidate)))))
1346 (persistent-action . (lambda (candidate)
1347 (if current-prefix-arg
1348 (anything-c-w3m-browse-bookmark candidate t)
1349 (anything-c-w3m-browse-bookmark candidate nil t))))
1350 (delayed)))
1352 ;; (anything 'anything-c-source-w3m-bookmarks)
1354 (defun anything-c-w3m-bookmarks-get-value (elm)
1355 (replace-regexp-in-string "\"" ""
1356 (cdr (assoc elm
1357 anything-c-w3m-bookmarks-alist))))
1360 (defun anything-c-w3m-browse-bookmark (elm &optional use-firefox new-tab)
1361 (let* ((fn (if use-firefox
1362 'browse-url-firefox
1363 'w3m-browse-url))
1364 (arg (and (eq fn 'w3m-browse-url)
1365 new-tab)))
1366 (funcall fn (anything-c-w3m-bookmarks-get-value elm) arg)))
1369 (defun anything-c-highlight-w3m-bookmarks (books)
1370 (loop for i in books
1371 collect (propertize i
1372 'face 'anything-w3m-bookmarks-face
1373 'help-echo (anything-c-w3m-bookmarks-get-value i))))
1376 (defun anything-c-w3m-delete-bookmark (elm)
1377 (save-excursion
1378 (find-file-literally w3m-bookmark-file)
1379 (goto-char (point-min))
1380 (when (re-search-forward elm nil t)
1381 (beginning-of-line)
1382 (delete-region (point)
1383 (line-end-position))
1384 (delete-blank-lines))
1385 (save-buffer (current-buffer))
1386 (kill-buffer (current-buffer))))
1388 (defun anything-c-w3m-rename-bookmark (elm)
1389 (let* ((old-title (replace-regexp-in-string ">" "" elm))
1390 (new-title (read-string "NewTitle: " old-title)))
1391 (save-excursion
1392 (find-file-literally w3m-bookmark-file)
1393 (goto-char (point-min))
1394 (when (re-search-forward (concat elm "<") nil t)
1395 (goto-char (1- (point)))
1396 (delete-backward-char (length old-title))
1397 (insert new-title))
1398 (save-buffer (current-buffer))
1399 (kill-buffer (current-buffer)))))
1401 ;;;; <Library>
1402 ;;; Elisp library scan
1403 (defvar anything-c-source-elisp-library-scan
1404 '((name . "Elisp libraries (Scan)")
1405 (init . (anything-c-elisp-library-scan-init))
1406 (candidates-in-buffer)
1407 (action ("Find library" . (lambda (candidate)
1408 (find-file (find-library-name candidate))))
1409 ("Find library other window" . (lambda (candidate)
1410 (find-file-other-window (find-library-name candidate))))
1411 ("Load library" . (lambda (candidate)
1412 (load-library candidate))))))
1413 ;; (anything 'anything-c-source-elisp-library-scan)
1415 (defun anything-c-elisp-library-scan-init ()
1416 "Init anything buffer status."
1417 (let ((anything-buffer (anything-candidate-buffer 'global))
1418 (library-list (anything-c-elisp-library-scan-list)))
1419 (with-current-buffer anything-buffer
1420 (dolist (library library-list)
1421 (insert (format "%s\n" library))))))
1423 (defun anything-c-elisp-library-scan-list (&optional dirs string)
1424 "Do completion for file names passed to `locate-file'.
1425 DIRS is directory to search path.
1426 STRING is string to match."
1427 ;; Use `load-path' as path when ignore `dirs'.
1428 (or dirs (setq dirs load-path))
1429 ;; Init with blank when ignore `string'.
1430 (or string (setq string ""))
1431 ;; Get library list.
1432 (let ((string-dir (file-name-directory string))
1433 ;; File regexp that suffix match `load-file-rep-suffixes'.
1434 (match-regexp (format "^.*\\.el%s$" (regexp-opt load-file-rep-suffixes)))
1435 name
1436 names)
1437 (dolist (dir dirs)
1438 (unless dir
1439 (setq dir default-directory))
1440 (if string-dir
1441 (setq dir (expand-file-name string-dir dir)))
1442 (when (file-directory-p dir)
1443 (dolist (file (file-name-all-completions
1444 (file-name-nondirectory string) dir))
1445 ;; Suffixes match `load-file-rep-suffixes'.
1446 (setq name (if string-dir (concat string-dir file) file))
1447 (if (string-match match-regexp name)
1448 (add-to-list 'names name)))))
1449 names))
1451 ;;;; <Programming>
1452 ;;; Imenu
1453 (defvar anything-c-imenu-delimiter " / ")
1455 (defvar anything-c-imenu-index-filter nil)
1456 (make-variable-buffer-local 'anything-c-imenu-index-filter)
1458 (defvar anything-c-cached-imenu-alist nil)
1459 (make-variable-buffer-local 'anything-c-cached-imenu-alist)
1461 (defvar anything-c-cached-imenu-candidates nil)
1462 (make-variable-buffer-local 'anything-c-cached-imenu-candidates)
1464 (defvar anything-c-cached-imenu-tick nil)
1465 (make-variable-buffer-local 'anything-c-cached-imenu-tick)
1467 (eval-when-compile (require 'imenu))
1468 (setq imenu-auto-rescan t)
1470 (defun anything-imenu-create-candidates (entry)
1471 "Create candidates with ENTRY."
1472 (if (listp (cdr entry))
1473 (mapcan (lambda (sub)
1474 (if (consp (cdr sub))
1475 (mapcar
1476 (lambda (subentry)
1477 (concat (car entry) anything-c-imenu-delimiter subentry))
1478 (anything-imenu-create-candidates sub))
1479 (list (concat (car entry) anything-c-imenu-delimiter (car sub)))))
1480 (cdr entry))
1481 (list entry)))
1483 (defvar anything-c-source-imenu
1484 '((name . "Imenu")
1485 (candidates . anything-c-imenu-candidates)
1486 (volatile)
1487 (persistent-action . (lambda (elm)
1488 (anything-c-imenu-default-action elm)
1489 (unless (fboundp 'semantic-imenu-tag-overlay)
1490 (anything-match-line-color-current-line))))
1491 (action . anything-c-imenu-default-action))
1492 "See (info \"(emacs)Imenu\")")
1494 ;; (anything 'anything-c-source-imenu)
1496 (defun anything-c-imenu-candidates ()
1497 (with-current-buffer anything-current-buffer
1498 (let ((tick (buffer-modified-tick)))
1499 (if (eq anything-c-cached-imenu-tick tick)
1500 anything-c-cached-imenu-candidates
1501 (setq imenu--index-alist nil)
1502 (setq anything-c-cached-imenu-tick tick
1503 anything-c-cached-imenu-candidates
1504 (condition-case nil
1505 (mapcan
1506 'anything-imenu-create-candidates
1507 (setq anything-c-cached-imenu-alist
1508 (let ((index (imenu--make-index-alist)))
1509 (if anything-c-imenu-index-filter
1510 (funcall anything-c-imenu-index-filter index)
1511 index))))
1512 (error nil)))
1513 (setq anything-c-cached-imenu-candidates
1514 (mapcar #'(lambda (x)
1515 (if (stringp x)
1517 (car x)))
1518 anything-c-cached-imenu-candidates))))))
1520 (setq imenu-default-goto-function 'imenu-default-goto-function)
1521 (defun anything-c-imenu-default-action (elm)
1522 "The default action for `anything-c-source-imenu'."
1523 (let ((path (split-string elm anything-c-imenu-delimiter))
1524 (alist anything-c-cached-imenu-alist))
1525 (if (> (length path) 1)
1526 (progn
1527 (setq alist (assoc (car path) alist))
1528 (setq elm (cadr path))
1529 (imenu (assoc elm alist)))
1530 (imenu (assoc elm alist)))))
1532 ;;; Ctags
1533 (defvar anything-c-ctags-modes
1534 '( c-mode c++-mode awk-mode csharp-mode java-mode javascript-mode lua-mode
1535 makefile-mode pascal-mode perl-mode cperl-mode php-mode python-mode
1536 scheme-mode sh-mode slang-mode sql-mode tcl-mode ))
1538 (defun anything-c-source-ctags-init ()
1539 (when (and buffer-file-name
1540 (memq major-mode anything-c-ctags-modes)
1541 (anything-current-buffer-is-modified))
1542 (with-current-buffer (anything-candidate-buffer 'local)
1543 (call-process-shell-command
1544 (if (string-match "\\.el\\.gz$" anything-buffer-file-name)
1545 (format "ctags -e -u -f- --language-force=lisp --fields=n =(zcat %s) " anything-buffer-file-name)
1546 (format "ctags -e -u -f- --fields=n %s " anything-buffer-file-name))
1547 nil (current-buffer))
1548 (goto-char (point-min))
1549 (forward-line 2)
1550 (delete-region (point-min) (point))
1551 (loop while (and (not (eobp)) (search-forward "\001" (point-at-eol) t))
1552 for lineno-start = (point)
1553 for lineno = (buffer-substring lineno-start (1- (search-forward "," (point-at-eol) t)))
1555 (beginning-of-line)
1556 (insert (format "%5s:" lineno))
1557 (search-forward "\177" (point-at-eol) t)
1558 (delete-region (1- (point)) (point-at-eol))
1559 (forward-line 1)))))
1561 (defvar anything-c-source-ctags
1562 '((name . "Exuberant ctags")
1563 (init . anything-c-source-ctags-init)
1564 (candidates-in-buffer)
1565 (adjust)
1566 (type . line))
1567 "Needs Exuberant Ctags.
1569 http://ctags.sourceforge.net/")
1570 ;; (anything 'anything-c-source-ctags)
1572 ;; Semantic
1573 (eval-when-compile (require 'semantic nil t))
1574 (defun anything-semantic-construct-candidates (tags depth)
1575 (when (require 'semantic nil t)
1576 (apply 'append
1577 (mapcar (lambda (tag)
1578 (if (listp tag)
1579 (let ((type (semantic-tag-type tag))
1580 (class (semantic-tag-class tag)))
1581 (if (or (and (stringp type)
1582 (string= type "class"))
1583 (eq class 'function)
1584 (eq class 'variable))
1585 (cons (cons (concat (make-string (* depth 2) ?\s)
1586 (semantic-format-tag-summarize tag nil t)) tag)
1587 (anything-semantic-construct-candidates (semantic-tag-components tag)
1588 (1+ depth)))))))
1589 tags))))
1591 (defun anything-semantic-default-action (candidate)
1592 (let ((tag (cdr (assoc candidate anything-semantic-candidates))))
1593 (semantic-go-to-tag tag)))
1595 (defvar anything-c-source-semantic
1596 '((name . "Semantic Tags")
1597 (init . (lambda ()
1598 (setq anything-semantic-candidates
1599 (condition-case nil
1600 (anything-semantic-construct-candidates (semantic-fetch-tags) 0)
1601 (error nil)))))
1602 (candidates . (lambda ()
1603 (if anything-semantic-candidates
1604 (mapcar 'car anything-semantic-candidates))))
1605 (persistent-action . (lambda (elm)
1606 (anything-semantic-default-action elm)
1607 (anything-match-line-color-current-line)))
1608 (action ("Goto tag" . (lambda (candidate)
1609 (let ((tag (cdr (assoc candidate anything-semantic-candidates))))
1610 (semantic-go-to-tag tag))))))
1611 "Needs semantic in CEDET.
1613 http://cedet.sourceforge.net/semantic.shtml
1614 http://cedet.sourceforge.net/")
1616 ;; (anything 'anything-c-source-semantic)
1618 ;;; Function is called by
1619 (defvar anything-c-source-simple-call-tree-functions-callers
1620 '((name . "Function is called by")
1621 (init . anything-c-simple-call-tree-functions-callers-init)
1622 (delayed)
1623 (candidates-in-buffer))
1624 "Needs simple-call-tree.el.
1625 http://www.emacswiki.org/cgi-bin/wiki/download/simple-call-tree.el")
1627 (defun anything-c-simple-call-tree-functions-callers-init ()
1628 (require 'simple-call-tree)
1629 (with-no-warnings
1630 (when (anything-current-buffer-is-modified)
1631 (simple-call-tree-analyze)
1632 (let ((list (simple-call-tree-invert simple-call-tree-alist)))
1633 (with-current-buffer (anything-candidate-buffer 'local)
1634 (dolist (entry list)
1635 (let ((callers (mapconcat #'identity (cdr entry) ", ")))
1636 (insert (car entry) " is called by "
1637 (if (string= callers "")
1638 "no functions."
1639 callers)
1640 ".\n"))))))))
1641 ;; (anything 'anything-c-source-simple-call-tree-functions-callers)
1643 ;;; Function calls
1644 (defvar anything-c-source-simple-call-tree-callers-functions
1645 '((name . "Function calls")
1646 (init . anything-c-simple-call-tree-callers-functions-init)
1647 (delayed)
1648 (candidates-in-buffer))
1649 "Needs simple-call-tree.el.
1650 http://www.emacswiki.org/cgi-bin/wiki/download/simple-call-tree.el")
1652 (defun anything-c-simple-call-tree-callers-functions-init ()
1653 (require 'simple-call-tree)
1654 (with-no-warnings
1655 (when (anything-current-buffer-is-modified)
1656 (simple-call-tree-analyze)
1657 (let ((list simple-call-tree-alist))
1658 (with-current-buffer (anything-candidate-buffer 'local)
1659 (dolist (entry list)
1660 (let ((functions (mapconcat #'identity (cdr entry) ", ")))
1661 (insert (car entry) " calls "
1662 (if (string= functions "")
1663 "no functions"
1664 functions)
1665 ".\n"))))))))
1667 ;; (anything 'anything-c-source-simple-call-tree-callers-functions)
1669 ;;; Commands/Options with doc
1670 (defvar anything-c-auto-document-data nil)
1671 (make-variable-buffer-local 'anything-c-auto-document-data)
1672 (defvar anything-c-source-commands-and-options-in-file
1673 '((name . "Commands/Options in file")
1674 (header-name
1675 . (lambda (x) (format "Commands/Options in %s"
1676 (buffer-local-value 'buffer-file-name anything-current-buffer))))
1677 (candidates . anything-command-and-options-candidates)
1678 (multiline)
1679 (action . imenu))
1680 "List Commands and Options with doc. It needs auto-document.el .
1682 http://www.emacswiki.org/cgi-bin/wiki/download/auto-document.el")
1684 (eval-when-compile (require 'auto-document nil t))
1685 (defun anything-command-and-options-candidates ()
1686 (with-current-buffer anything-current-buffer
1687 (when (and (require 'auto-document nil t)
1688 (eq major-mode 'emacs-lisp-mode)
1689 (or (anything-current-buffer-is-modified)
1690 (not anything-c-auto-document-data)))
1691 (or imenu--index-alist (imenu--make-index-alist t))
1692 (setq anything-c-auto-document-data
1693 (destructuring-bind (commands options)
1694 (adoc-construct anything-current-buffer)
1695 (append
1696 (loop for (command . doc) in commands
1697 for cmdname = (symbol-name command)
1698 collect
1699 (cons (format "Command: %s\n %s"
1700 (propertize cmdname 'face font-lock-function-name-face)
1701 (adoc-first-line doc))
1702 (assoc cmdname imenu--index-alist)))
1703 (loop with var-alist = (cdr (assoc "Variables" imenu--index-alist))
1704 for (option doc default) in options
1705 for optname = (symbol-name option)
1706 collect
1707 (cons (format "Option: %s\n %s\n default = %s"
1708 (propertize optname 'face font-lock-variable-name-face)
1709 (adoc-first-line doc)
1710 (adoc-prin1-to-string default))
1711 (assoc optname
1712 var-alist)))))))
1713 anything-c-auto-document-data))
1715 ;; (anything 'anything-c-source-commands-and-options-in-file)
1717 ;;;; <Color and Face>
1718 ;;; Customize Face
1719 (defvar anything-c-source-customize-face
1720 '((name . "Customize Face")
1721 (init . (lambda ()
1722 (unless (anything-candidate-buffer)
1723 (save-window-excursion (list-faces-display))
1724 (anything-candidate-buffer (get-buffer "*Faces*")))))
1725 (candidates-in-buffer)
1726 (get-line . buffer-substring)
1727 (action . (lambda (line)
1728 (customize-face (intern (car (split-string line))))))
1729 (requires-pattern . 3))
1730 "See (info \"(emacs)Faces\")")
1731 ;; (anything 'anything-c-source-customize-face)
1733 ;; Color
1734 (defvar anything-c-source-colors
1735 '((name . "Colors")
1736 (init . (lambda () (unless (anything-candidate-buffer)
1737 (save-window-excursion (list-colors-display))
1738 (anything-candidate-buffer (get-buffer "*Colors*")))))
1739 (candidates-in-buffer)
1740 (get-line . buffer-substring)
1741 (action ("Copy Name" . (lambda (candidate)
1742 (kill-new (anything-c-colors-get-name candidate))))
1743 ("Copy RGB" . (lambda (candidate)
1744 (kill-new (anything-c-colors-get-rgb candidate))))
1745 ("Insert Name" . (lambda (candidate)
1746 (with-current-buffer anything-current-buffer
1747 (insert (anything-c-colors-get-name candidate)))))
1748 ("Insert RGB" . (lambda (candidate)
1749 (with-current-buffer anything-current-buffer
1750 (insert (anything-c-colors-get-rgb candidate))))))
1751 (requires-pattern . 3)))
1752 ;; (anything 'anything-c-source-colors)
1754 (defun anything-c-colors-get-name (candidate)
1755 "Get color name."
1756 (replace-regexp-in-string
1757 " " ""
1758 (with-temp-buffer
1759 (insert (capitalize candidate))
1760 (goto-char (point-min))
1761 (search-forward-regexp "\\s-\\{2,\\}")
1762 (kill-line)
1763 (buffer-string))))
1765 (defun anything-c-colors-get-rgb (candidate)
1766 "Get color RGB."
1767 (replace-regexp-in-string
1768 " " ""
1769 (with-temp-buffer
1770 (insert (capitalize candidate))
1771 (goto-char (point-max))
1772 (search-backward-regexp "\\s-\\{2,\\}")
1773 (kill-region (point) (point-min))
1774 (buffer-string))))
1776 ;;;; <Search Engine>
1777 ;;; Tracker desktop search
1778 (defvar anything-c-source-tracker-search
1779 '((name . "Tracker Search")
1780 (candidates . (lambda ()
1781 (start-process "tracker-search-process" nil
1782 "tracker-search"
1783 anything-pattern)))
1784 (type . file)
1785 (requires-pattern . 3)
1786 (delayed))
1787 "Source for retrieving files matching the current input pattern
1788 with the tracker desktop search.")
1789 ;; (anything 'anything-c-source-tracker-search)
1791 ;;; Spotlight (MacOS X desktop search)
1792 (defvar anything-c-source-mac-spotlight
1793 '((name . "mdfind")
1794 (candidates . (lambda ()
1795 (start-process "mdfind-process" nil "mdfind" anything-pattern)))
1796 (type . file)
1797 (requires-pattern . 3)
1798 (delayed))
1799 "Source for retrieving files via Spotlight's command line
1800 utility mdfind.")
1801 ;; (anything 'anything-c-source-mac-spotlight)
1803 ;;;; <Kill ring>
1804 ;;; Kill ring
1805 (defvar anything-c-source-kill-ring
1806 '((name . "Kill Ring")
1807 (init . (lambda () (anything-attrset 'last-command last-command)))
1808 (candidates . (lambda ()
1809 (loop for kill in kill-ring
1810 unless (or (< (length kill) anything-kill-ring-threshold)
1811 (string-match "^[\\s\\t]+$" kill))
1812 collect kill)))
1813 (action . anything-c-kill-ring-action)
1814 (last-command)
1815 (migemo)
1816 (multiline))
1817 "Source for browse and insert contents of kill-ring.")
1819 (defun anything-c-kill-ring-action (str)
1820 "Insert STR in `kill-ring' and set STR to the head.
1821 If this action is executed just after `yank', replace with STR as yanked string."
1822 (setq kill-ring (delete str kill-ring))
1823 (if (not (eq (anything-attr 'last-command) 'yank))
1824 (insert-for-yank str)
1825 ;; from `yank-pop'
1826 (let ((inhibit-read-only t)
1827 (before (< (point) (mark t))))
1828 (if before
1829 (funcall (or yank-undo-function 'delete-region) (point) (mark t))
1830 (funcall (or yank-undo-function 'delete-region) (mark t) (point)))
1831 (setq yank-undo-function nil)
1832 (set-marker (mark-marker) (point) (current-buffer))
1833 (insert-for-yank str)
1834 ;; Set the window start back where it was in the yank command,
1835 ;; if possible.
1836 (set-window-start (selected-window) yank-window-start t)
1837 (if before
1838 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1839 ;; It is cleaner to avoid activation, even though the command
1840 ;; loop would deactivate the mark because we inserted text.
1841 (goto-char (prog1 (mark t)
1842 (set-marker (mark-marker) (point) (current-buffer)))))))
1843 (kill-new str))
1845 ;; (anything 'anything-c-source-kill-ring)
1847 ;;;; <Register>
1848 ;;; Insert from register
1849 (defvar anything-c-source-register
1850 '((name . "Registers")
1851 (candidates . anything-c-register-candidates)
1852 (action-transformer . anything-c-register-action-transformer)
1853 (multiline)
1854 (action))
1855 "See (info \"(emacs)Registers\")")
1857 (defun anything-c-register-candidates ()
1858 "Collecting register contents and appropriate commands."
1859 (loop for (char . val) in register-alist
1860 for key = (single-key-description char)
1861 for string-actions = (cond
1862 ((numberp val)
1863 (list (int-to-string val)
1864 'insert-register
1865 'increment-register))
1866 ((markerp val)
1867 (let ((buf (marker-buffer val)))
1868 (if (null buf)
1869 (list "a marker in no buffer")
1870 (list (concat
1871 "a buffer position:"
1872 (buffer-name buf)
1873 ", position "
1874 (int-to-string (marker-position val)))
1875 'jump-to-register
1876 'insert-register))))
1877 ((and (consp val) (window-configuration-p (car val)))
1878 (list "window configuration."
1879 'jump-to-register))
1880 ((and (consp val) (frame-configuration-p (car val)))
1881 (list "frame configuration."
1882 'jump-to-register))
1883 ((and (consp val) (eq (car val) 'file))
1884 (list (concat "file:"
1885 (prin1-to-string (cdr val))
1886 ".")
1887 'jump-to-register))
1888 ((and (consp val) (eq (car val) 'file-query))
1889 (list (concat "file:a file-query reference: file "
1890 (car (cdr val))
1891 ", position "
1892 (int-to-string (car (cdr (cdr val))))
1893 ".")
1894 'jump-to-register))
1895 ((consp val)
1896 (let ((lines (format "%4d" (length val))))
1897 (list (format "%s: %s\n" lines
1898 (truncate-string-to-width
1899 (mapconcat 'identity (list (car val))
1900 ;; (mapconcat (lambda (y) y) val
1901 "^J") (- (window-width) 15)))
1902 'insert-register)))
1903 ((stringp val)
1904 (list ;; without properties
1905 (substring-no-properties val)
1906 'insert-register
1907 'append-to-register
1908 'prepend-to-register))
1910 "GARBAGE!"))
1911 collect (cons (format "register %3s: %s" key (car string-actions))
1912 (cons char (cdr string-actions)))))
1914 (defun anything-c-register-action-transformer (actions register-and-functions)
1915 "Decide actions by the contents of register."
1916 (loop with func-actions =
1917 '((insert-register
1918 "Insert Register" .
1919 (lambda (c) (insert-register (car c))))
1920 (jump-to-register
1921 "Jump to Register" .
1922 (lambda (c) (jump-to-register (car c))))
1923 (append-to-register
1924 "Append Region to Register" .
1925 (lambda (c) (append-to-register (car c) (region-beginning) (region-end))))
1926 (prepend-to-register
1927 "Prepend Region to Register" .
1928 (lambda (c) (prepend-to-register (car c) (region-beginning) (region-end))))
1929 (increment-register
1930 "Increment Prefix Arg to Register" .
1931 (lambda (c) (increment-register anything-current-prefix-arg (car c)))))
1932 for func in (cdr register-and-functions)
1933 for cell = (assq func func-actions)
1934 when cell
1935 collect (cdr cell)))
1937 ;; (anything 'anything-c-source-register)
1939 ;;;; <Headline Extraction>
1940 (defvar anything-c-source-fixme
1941 '((name . "TODO/FIXME/DRY comments")
1942 (headline . "^.*\\<\\(TODO\\|FIXME\\|DRY\\)\\>.*$")
1943 (adjust)
1944 (recenter))
1945 "Show TODO/FIXME/DRY comments in current file.")
1946 ;; (anything 'anything-c-source-fixme)
1948 (defvar anything-c-source-rd-headline
1949 '((name . "RD HeadLine")
1950 (headline "^= \\(.+\\)$" "^== \\(.+\\)$" "^=== \\(.+\\)$" "^==== \\(.+\\)$")
1951 (condition . (memq major-mode '(rdgrep-mode rd-mode)))
1952 (migemo)
1953 (subexp . 1))
1954 "Show RD headlines.
1956 RD is Ruby's POD.
1957 http://en.wikipedia.org/wiki/Ruby_Document_format")
1958 ;; (anything 'anything-c-source-rd-headline)
1960 (defvar anything-c-source-oddmuse-headline
1961 '((name . "Oddmuse HeadLine")
1962 (headline "^= \\(.+\\) =$" "^== \\(.+\\) ==$"
1963 "^=== \\(.+\\) ===$" "^==== \\(.+\\) ====$")
1964 (condition . (memq major-mode '(oddmuse-mode yaoddmuse-mode)))
1965 (migemo)
1966 (subexp . 1))
1967 "Show Oddmuse headlines, such as EmacsWiki.")
1968 ;; (anything 'anything-c-source-oddmuse-headline)
1970 (defvar anything-c-source-emacs-source-defun
1971 '((name . "Emacs Source DEFUN")
1972 (headline . "DEFUN\\|DEFVAR")
1973 (condition . (string-match "/emacs2[0-9].+/src/.+c$" (or buffer-file-name ""))))
1974 "Show DEFUN/DEFVAR in Emacs C source file.")
1975 ;; (anything 'anything-c-source-emacs-source-defun)
1977 (defvar anything-c-source-emacs-lisp-expectations
1978 '((name . "Emacs Lisp Expectations")
1979 (headline . "(desc[ ]\\|(expectations")
1980 (condition . (eq major-mode 'emacs-lisp-mode)))
1981 "Show descriptions (desc) in Emacs Lisp Expectations.
1983 http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el")
1984 ;; (anything 'anything-c-source-emacs-lisp-expectations)
1986 (defvar anything-c-source-emacs-lisp-toplevels
1987 '((name . "Emacs Lisp Toplevel / Level 4 Comment / Linkd Star")
1988 (headline . "^(\\|(@\\*\\|^;;;;")
1989 (get-line . buffer-substring)
1990 (condition . (eq major-mode 'emacs-lisp-mode))
1991 (adjust))
1992 "Show top-level forms, level 4 comments and linkd stars (optional) in Emacs Lisp.
1993 linkd.el is optional because linkd stars are extracted by regexp.
1994 http://www.emacswiki.org/cgi-bin/wiki/download/linkd.el")
1995 ;; (anything 'anything-c-source-emacs-lisp-toplevels)
1997 (defvar anything-c-source-org-headline
1998 '((name . "Org HeadLine")
1999 (headline
2000 "^\\* \\(.+?\\)\\([ \t]*:[a-zA-Z0-9_@:]+:\\)?[ \t]*$"
2001 "^\\*\\* \\(.+?\\)\\([ \t]*:[a-zA-Z0-9_@:]+:\\)?[ \t]*$"
2002 "^\\*\\*\\* \\(.+?\\)\\([ \t]*:[a-zA-Z0-9_@:]+:\\)?[ \t]*$"
2003 "^\\*\\*\\*\\* \\(.+?\\)\\([ \t]*:[a-zA-Z0-9_@:]+:\\)?[ \t]*$"
2004 "^\\*\\*\\*\\*\\* \\(.+?\\)\\([ \t]*:[a-zA-Z0-9_@:]+:\\)?[ \t]*$"
2005 "^\\*\\*\\*\\*\\*\\* \\(.+?\\)\\([ \t]*:[a-zA-Z0-9_@:]+:\\)?[ \t]*$"
2006 "^\\*\\*\\*\\*\\*\\*\\* \\(.+?\\)\\([ \t]*:[a-zA-Z0-9_@:]+:\\)?[ \t]*$"
2007 "^\\*\\*\\*\\*\\*\\*\\*\\* \\(.+?\\)\\([ \t]*:[a-zA-Z0-9_@:]+:\\)?[ \t]*$")
2008 (condition . (eq major-mode 'org-mode))
2009 (migemo)
2010 (subexp . 1)
2011 (persistent-action . (lambda (elm)
2012 (anything-c-action-line-goto elm)
2013 (org-cycle)))
2014 (action-transformer
2015 . (lambda (actions candidate)
2016 '(("Go to Line" . anything-c-action-line-goto)
2017 ("Insert Link to This Headline" . anything-c-org-headline-insert-link-to-headline)))))
2018 "Show Org headlines.
2019 org-mode is very very much extended text-mode/outline-mode.
2021 See (find-library \"org.el\")
2022 See http://orgmode.org for the latest version.")
2024 (defun anything-c-org-headline-insert-link-to-headline (lineno-and-content)
2025 (insert
2026 (save-excursion
2027 (goto-line (car lineno-and-content))
2028 (and (looking-at "^\\*+ \\(.+?\\)\\([ \t]*:[a-zA-Z0-9_@:]+:\\)?[ \t]*$")
2029 (org-make-link-string (concat "*" (match-string 1)))))))
2031 ;; (anything 'anything-c-source-org-headline)
2033 ;;;; <Misc>
2034 ;;; Picklist
2035 (defvar anything-c-source-picklist
2036 '((name . "Picklist")
2037 (candidates . (lambda () (mapcar 'car picklist-list)))
2038 (volatile)
2039 (type . file)))
2040 ;; (anything 'anything-c-source-picklist)
2042 ;;; BBDB
2043 (defun anything-c-bbdb-candidates ()
2044 "Return a list of all names in the bbdb database. The format
2045 is \"Firstname Lastname\"."
2046 (mapcar (lambda (bbdb-record)
2047 (replace-regexp-in-string
2048 "\\s-+$" ""
2049 (concat (aref bbdb-record 0) " " (aref bbdb-record 1))))
2050 (bbdb-records)))
2052 (defun anything-c-bbdb-create-contact (actions candidate)
2053 "Action transformer that returns only an entry to add the
2054 current `anything-pattern' as new contact. All other actions are
2055 removed."
2056 (if (string= candidate "*Add to contacts*")
2057 '(("Add to contacts" . (lambda (actions)
2058 (bbdb-create-internal
2059 (read-from-minibuffer "Name: " anything-c-bbdb-name)
2060 (read-from-minibuffer "Company: ")
2061 (read-from-minibuffer "Email: ")
2064 (read-from-minibuffer "Note: ")))))
2065 actions))
2067 (defun anything-c-bbdb-get-record (candidate)
2068 "Return record that match CANDIDATE."
2069 (bbdb candidate nil)
2070 (set-buffer "*BBDB*")
2071 (bbdb-current-record))
2073 (defvar anything-c-bbdb-name nil
2074 "Only for internal use.")
2076 (defvar anything-c-source-bbdb
2077 '((name . "BBDB")
2078 (candidates . anything-c-bbdb-candidates)
2079 (volatile)
2080 (action ("Send a mail" . (lambda (candidate)
2081 (bbdb-send-mail (anything-c-bbdb-get-record candidate))))
2082 ("View person's data" . (lambda (candidate)
2083 (bbdb-redisplay-one-record (anything-c-bbdb-get-record candidate)))))
2084 (filtered-candidate-transformer . (lambda (candidates source)
2085 (setq anything-c-bbdb-name anything-pattern)
2086 (if (not candidates)
2087 (list "*Add to contacts*")
2088 candidates)))
2089 (action-transformer . (lambda (actions candidate)
2090 (anything-c-bbdb-create-contact actions candidate)))))
2091 ;; (anything 'anything-c-source-bbdb)
2093 ;;; Evaluation Result
2094 (defvar anything-c-source-evaluation-result
2095 '((name . "Evaluation Result")
2096 (requires-pattern)
2097 (match (lambda (candidate) t))
2098 (candidates "dummy")
2099 (filtered-candidate-transformer . (lambda (candidates source)
2100 (list
2101 (condition-case nil
2102 (prin1-to-string
2103 (eval (read anything-pattern)))
2104 (error "Error")))))
2105 (volatile)
2106 (action ("Do Nothing" . ignore))))
2107 ;; (anything 'anything-c-source-evaluation-result)
2109 ;;; Calculation Result
2110 (defvar anything-c-source-calculation-result
2111 '((name . "Calculation Result")
2112 (requires-pattern)
2113 (match (lambda (candidate) t))
2114 (candidates "dummy")
2115 (filtered-candidate-transformer . (lambda (candidates source)
2116 (list
2117 (condition-case nil
2118 (calc-eval anything-pattern)
2119 (error "error")))))
2120 (volatile)
2121 (action ("Copy result to kill-ring" . kill-new))))
2122 ;; (anything 'anything-c-source-calculation-result)
2124 ;;; Google Suggestions
2125 (defvar anything-c-source-google-suggest
2126 '((name . "Google Suggest")
2127 (candidates . (lambda ()
2128 (let ((suggestions (anything-c-google-suggest-fetch anything-input)))
2129 (if (some (lambda (suggestion)
2130 (equal (cdr suggestion) anything-input))
2131 suggestions)
2132 suggestions
2133 ;; if there is no suggestion exactly matching the input then
2134 ;; prepend a Search on Google item to the list
2135 (append (list (cons (concat "Search for "
2136 "'" anything-input "'"
2137 " on Google")
2138 anything-input))
2139 suggestions)))))
2140 (action . (("Google Search" .
2141 (lambda (candidate)
2142 (browse-url (concat anything-c-google-suggest-search-url
2143 (url-hexify-string candidate)))))))
2144 (volatile)
2145 (requires-pattern . 3)
2146 (delayed)))
2147 ;; (anything 'anything-c-source-google-suggest)
2149 (defun anything-c-google-suggest-fetch (input)
2150 "Fetch suggestions for INPUT."
2151 (let* ((result (with-current-buffer
2152 (url-retrieve-synchronously
2153 (concat anything-c-google-suggest-url
2154 (url-hexify-string input)))
2155 (buffer-substring (point-min) (point-max))))
2156 (split (split-string result "new Array("))
2157 (suggestions (anything-c-google-suggest-get-items (second split)))
2158 (numbers (anything-c-google-suggest-get-items (third split)))
2159 (longest (+ (apply 'max 0 (let (lengths)
2160 (dotimes (i (length suggestions))
2161 (push (+ (length (nth i suggestions))
2162 (length (nth i numbers)))
2163 lengths))
2164 lengths))
2165 10))
2166 items)
2167 (dotimes (i (length suggestions))
2168 (let ((suggestion (nth i suggestions))
2169 (number (nth i numbers)))
2170 (push (cons (concat suggestion
2171 (make-string (- longest
2172 (length suggestion)
2173 (length number))
2175 number)
2176 suggestion)
2177 items)))
2178 items))
2180 (defun anything-c-google-suggest-get-items (str)
2181 "Extract items from STR returned by Google Suggest."
2182 (let ((start nil) items)
2183 (while (string-match "\"\\([^\"]+?\\)\"" str start)
2184 (push (match-string 1 str) items)
2185 (setq start (1+ (match-end 1))))
2186 items))
2188 ;;; Surfraw
2189 ;;; Need external program surfraw.
2190 ;;; http://surfraw.alioth.debian.org/
2191 ;; user variables
2192 (defvar anything-c-surfraw-favorites '("google" "wikipedia"
2193 "yahoo" "translate"
2194 "codesearch" "genpkg"
2195 "genportage" "fast"
2196 "currency")
2197 "All elements of this list will appear first in results.")
2198 (defvar anything-c-surfraw-use-only-favorites nil
2199 "If non-nil use only `anything-c-surfraw-favorites'.")
2202 (defun anything-c-build-elvi-alist ()
2203 "Build elvi alist.
2204 A list of search engines."
2205 (let* ((elvi-list
2206 (with-temp-buffer
2207 (call-process "surfraw" nil t nil
2208 "-elvi")
2209 (split-string (buffer-string) "\n")))
2210 (elvi-alist
2211 (let (line)
2212 (loop for i in elvi-list
2214 (setq line (split-string i))
2215 collect (cons (first line) (mapconcat #'(lambda (x) x) (cdr line) " "))))))
2216 elvi-alist))
2218 (defun anything-c-surfraw-sort-elvi (&optional only-fav)
2219 "Sort elvi alist according to `anything-c-surfraw-favorites'."
2220 (let* ((elvi-alist (anything-c-build-elvi-alist))
2221 (fav-alist (loop for j in anything-c-surfraw-favorites
2222 collect (assoc j elvi-alist)))
2223 (rest-elvi (loop for i in elvi-alist
2224 if (not (member i fav-alist))
2225 collect i)))
2226 (if only-fav
2227 fav-alist
2228 (append fav-alist rest-elvi))))
2230 (defun anything-c-surfraw-get-url (engine pattern)
2231 "Get search url from `engine' for `anything-pattern'."
2232 (with-temp-buffer
2233 (apply #'call-process "surfraw" nil t nil
2234 `(,engine
2235 "-p"
2236 ,anything-pattern))
2237 (buffer-string)))
2240 (defvar anything-c-surfraw-elvi nil)
2241 (defvar anything-c-surfraw-cache nil)
2242 (defvar anything-c-source-surfraw
2243 '((name . "Surfraw")
2244 (init . (lambda ()
2245 (unless anything-c-surfraw-cache
2246 (setq anything-c-surfraw-elvi (anything-c-surfraw-sort-elvi
2247 anything-c-surfraw-use-only-favorites))
2248 (setq anything-c-surfraw-cache
2249 (loop for i in anything-c-surfraw-elvi
2250 if (car i)
2251 collect (car i))))))
2252 (candidates . (lambda ()
2253 (loop for i in anything-c-surfraw-cache
2254 for s = (anything-c-surfraw-get-url i anything-pattern)
2255 collect (concat (propertize i
2256 'face '((:foreground "green"))
2257 'help-echo (cdr (assoc i anything-c-surfraw-elvi)))
2258 ">>>" (replace-regexp-in-string "\n" "" s)))))
2259 (action . (("Browse" . (lambda (candidate)
2260 (let ((url (second (split-string candidate ">>>"))))
2261 (browse-url url))))
2262 ("Browse firefox" . (lambda (candidate)
2263 (let ((url (second (split-string candidate ">>>"))))
2264 (browse-url-firefox url t))))))
2265 (volatile)
2266 (requires-pattern . 3)
2267 (multiline)
2268 (delayed)))
2270 ;; (anything 'anything-c-source-surfraw)
2272 ;;; Emms
2274 (defun anything-emms-stream-edit-bookmark (elm)
2275 "Change the information of current bookmark."
2276 (interactive)
2277 (let* ((cur-buf anything-current-buffer)
2278 (bookmark (assoc elm emms-stream-list))
2279 (name (read-from-minibuffer "Description: "
2280 (nth 0 bookmark)))
2281 (url (read-from-minibuffer "URL: "
2282 (nth 1 bookmark)))
2283 (fd (read-from-minibuffer "Feed Descriptor: "
2284 (int-to-string (nth 2 bookmark))))
2285 (type (read-from-minibuffer "Type (url, streamlist, or lastfm): "
2286 (format "%s" (car (last bookmark))))))
2287 (save-excursion
2288 (emms-streams)
2289 (when (re-search-forward (concat "^" name) nil t)
2290 (beginning-of-line)
2291 (emms-stream-delete-bookmark)
2292 (emms-stream-add-bookmark name url (string-to-number fd) type)
2293 (emms-stream-save-bookmarks-file)
2294 (emms-stream-quit)
2295 (switch-to-buffer cur-buf)))))
2297 (defvar anything-c-source-emms-streams
2298 '((name . "Emms Streams")
2299 (init . (lambda ()
2300 (emms-stream-init)))
2301 (candidates . (lambda ()
2302 (mapcar 'car emms-stream-list)))
2303 (action . (("Play" . (lambda (elm)
2304 (let* ((stream (assoc elm emms-stream-list))
2305 (fn (intern (concat "emms-play-" (symbol-name (car (last stream))))))
2306 (url (second stream)))
2307 (funcall fn url))))
2308 ("Edit" . anything-emms-stream-edit-bookmark)))
2309 (volatile)))
2310 ;; (anything 'anything-c-source-emms-streams)
2312 ;; Don't forget to set `emms-source-file-default-directory'
2313 (defvar anything-c-source-emms-dired
2314 '((name . "Music Directory")
2315 (candidates . (lambda ()
2316 (cddr (directory-files emms-source-file-default-directory))))
2317 (action . (("Play Directory" . (lambda (item)
2318 (emms-play-directory
2319 (expand-file-name item
2320 emms-source-file-default-directory))))
2321 ("Open dired in file's directory" . (lambda (item)
2322 (anything-c-open-dired
2323 (expand-file-name item
2324 emms-source-file-default-directory))))))
2325 (volatile)))
2326 ;; (anything 'anything-c-source-emms-dired)
2328 ;;; Jabber Contacts (jabber.el)
2329 (defun anything-c-jabber-online-contacts ()
2330 "List online Jabber contacts."
2331 (with-no-warnings
2332 (let (jids)
2333 (dolist (item (jabber-concat-rosters) jids)
2334 (when (get item 'connected)
2335 (push (if (get item 'name)
2336 (cons (get item 'name) item)
2337 (cons (symbol-name item) item)) jids))))))
2339 (defvar anything-c-source-jabber-contacts
2340 '((name . "Jabber Contacts")
2341 (init . (lambda () (require 'jabber)))
2342 (candidates . (lambda () (mapcar 'car (anything-c-jabber-online-contacts))))
2343 (action . (lambda (x)
2344 (jabber-chat-with
2345 (jabber-read-account)
2346 (symbol-name
2347 (cdr (assoc x (anything-c-jabber-online-contacts)))))))))
2348 ;; (anything 'anything-c-source-jabber-contacts)
2351 ;;; Call source.
2352 (defvar anything-source-select-buffer "*anything source select*")
2353 (defvar anything-c-source-call-source
2354 `((name . "Call anything source")
2355 (candidate-number-limit . 9999)
2356 (candidates . (lambda ()
2357 (loop for vname in (all-completions "anything-c-source-" obarray)
2358 for var = (intern vname)
2359 for name = (ignore-errors (assoc-default 'name (symbol-value var)))
2360 if name collect (cons (format "%s (%s)" name vname) var))))
2361 (action . (("Invoke anything with selected source" .
2362 (lambda (candidate)
2363 (setq anything-candidate-number-limit 9999)
2364 (anything candidate nil nil nil nil
2365 anything-source-select-buffer)))
2366 ("Describe variable" . describe-variable)))
2367 (persistent-action . describe-variable)))
2368 ;; (anything 'anything-c-source-call-source)
2370 (defun anything-call-source ()
2371 "Call anything source."
2372 (interactive)
2373 (anything 'anything-c-source-call-source nil nil nil nil
2374 anything-source-select-buffer))
2376 (defun anything-call-source-from-anything ()
2377 "Call anything source within `anything' session."
2378 (interactive)
2379 (setq anything-input-idle-delay 0)
2380 (anything-set-sources '(anything-c-source-call-source)))
2382 ;; Occur
2383 (defvar anything-c-source-occur
2384 '((name . "Occur")
2385 (init . (lambda ()
2386 (setq anything-c-source-occur-current-buffer
2387 (current-buffer))))
2388 (candidates . (lambda ()
2389 (setq anything-occur-buf (get-buffer-create "*Anything Occur*"))
2390 (with-current-buffer anything-occur-buf
2391 (erase-buffer)
2392 (let ((count (occur-engine anything-pattern
2393 (list anything-c-source-occur-current-buffer) anything-occur-buf
2394 list-matching-lines-default-context-lines nil
2395 list-matching-lines-buffer-name-face
2396 nil list-matching-lines-face
2397 (not (eq occur-excluded-properties t)))))
2398 (when (> count 0)
2399 (let ((lines (split-string (buffer-string) "\n" t)))
2400 (cdr lines)))))))
2401 (action . (("Goto line" . (lambda (candidate)
2402 (goto-line (string-to-number candidate) anything-c-source-occur-current-buffer)))))
2403 (requires-pattern . 1)
2404 (volatile)))
2405 ;; (anything 'anything-c-source-occur)
2407 ;; Do many actions for input
2408 (defvar anything-c-source-create
2409 '((name . "Create")
2410 (dummy)
2411 (action)
2412 (candidate-number-limit . 9999)
2413 (action-transformer . anything-create--actions))
2414 "Do many create actions from `anything-pattern'.
2415 See also `anything-create--actions'.")
2416 ;; (anything 'anything-c-source-create)
2417 (defcustom anything-create--actions-private nil
2418 "User defined actions for `anything-create' / `anything-c-source-create'.
2419 It is a list of (DISPLAY . FUNCTION) pairs like `action'
2420 attribute of `anything-sources'.
2422 It is prepended to predefined pairs."
2423 :type 'list
2424 :group 'anything-config)
2426 (defun anything-create-from-anything ()
2427 "Run `anything-create' from `anything' as a fallback."
2428 (interactive)
2429 (anything-run-after-quit 'anything-create nil anything-pattern))
2431 (defun anything-create (&optional string initial-input)
2432 "Do many create actions from STRING.
2433 See also `anything-create--actions'."
2434 (interactive)
2435 (setq string (or string (read-string "Create Anything: " initial-input)))
2436 (anything '(((name . "Anything Create")
2437 (header-name . (lambda (_) (format "Action for \"%s\"" string)))
2438 (candidates . anything-create--actions)
2439 (candidate-number-limit . 9999)
2440 (action . (lambda (func) (funcall func string)))))))
2442 (defun anything-create--actions (&rest ignored)
2443 "Default actions for `anything-create' / `anything-c-source-create'."
2444 (remove-if-not
2445 (lambda (pair) (and (consp pair) (functionp (cdr pair))))
2446 (append anything-create--actions-private
2447 '(("find-file" . find-file)
2448 ("find-file other window" . find-file-other-window)
2449 ("New buffer" . switch-to-buffer)
2450 ("New buffer other window" . switch-to-buffer-other-window)
2451 ("Bookmark Set" . bookmark-set)
2452 ("Set Register" .
2453 (lambda (x) (set-register (read-char "Register: ") x)))
2454 ("Insert Linkd star" . linkd-insert-star)
2455 ("Insert Linkd Tag" . linkd-insert-tag)
2456 ("Insert Linkd Link" . linkd-insert-link)
2457 ("Insert Linkd Lisp" . linkd-insert-lisp)
2458 ("Insert Linkd Wiki" . linkd-insert-wiki)
2459 ("Google Search" . google)))))
2461 ;; Minibuffer History
2462 (defvar anything-c-source-minibuffer-history
2463 '((name . "Minibuffer History")
2464 (candidates . minibuffer-history)
2465 (migemo)
2466 (action . insert)))
2468 ;; (anything 'anything-c-source-minibuffer-history)
2469 ;;;; <System>
2471 ;; Sources for gentoo users
2473 (defvar anything-c-gentoo-use-flags nil)
2474 (defvar anything-c-gentoo-buffer "*anything-gentoo-output*")
2475 (defvar anything-c-cache-gentoo nil)
2476 (defvar anything-c-cache-world nil)
2477 (defvar anything-c-source-gentoo
2478 '((name . "Portage sources")
2479 (init . (lambda ()
2480 (get-buffer-create anything-c-gentoo-buffer)
2481 (unless anything-c-cache-gentoo
2482 (anything-c-gentoo-setup-cache))
2483 (unless anything-c-cache-world
2484 (setq anything-c-cache-world (anything-c-gentoo-get-world)))
2485 (anything-c-gentoo-init-list)))
2486 (candidates-in-buffer)
2487 (match . identity)
2488 (candidate-transformer anything-c-highlight-world)
2489 (action . (("Show package" . (lambda (elm)
2490 (anything-c-gentoo-eshell-action elm "eix")))
2491 ("Show history" . (lambda (elm)
2492 (if (member elm anything-c-cache-world)
2493 (anything-c-gentoo-eshell-action elm "genlop -qe")
2494 (message "No infos on packages not yet installed"))))
2495 ("Copy in kill-ring" . kill-new)
2496 ("insert at point" . insert)
2497 ("Browse HomePage" . (lambda (elm)
2498 (browse-url (car (anything-c-gentoo-get-url elm)))))
2499 ("Show extra infos" . (lambda (elm)
2500 (if (member elm anything-c-cache-world)
2501 (anything-c-gentoo-eshell-action elm "genlop -qi")
2502 (message "No infos on packages not yet installed"))))
2503 ("Show use flags" . (lambda (elm)
2504 (anything-c-gentoo-default-action elm "equery" "-C" "u")
2505 (font-lock-add-keywords nil '(("^\+.*" . font-lock-variable-name-face)))
2506 (font-lock-mode 1)))
2507 ("Run emerge pretend" . (lambda (elm)
2508 (anything-c-gentoo-eshell-action elm "emerge -p")))
2509 ("Show dependencies" . (lambda (elm)
2510 (anything-c-gentoo-default-action elm "equery" "-C" "d")))
2511 ("Show related files" . (lambda (elm)
2512 (anything-c-gentoo-default-action elm "equery" "files")))
2513 ("Update" . (lambda (elm)
2514 (anything-c-gentoo-setup-cache)
2515 (setq anything-c-cache-world (anything-c-gentoo-get-world))))))))
2517 ;; (anything 'anything-c-source-gentoo)
2519 (defun anything-c-gentoo-default-action (elm command &rest args)
2520 "Gentoo default action that use `anything-c-gentoo-buffer'."
2521 (if (member elm anything-c-cache-world)
2522 (progn
2523 (switch-to-buffer anything-c-gentoo-buffer)
2524 (erase-buffer)
2525 (let ((com-list (append args (list elm))))
2526 (apply #'call-process command nil t nil
2527 com-list)))
2528 (message "No infos on packages not yet installed")))
2530 (defvar anything-c-source-use-flags
2531 '((name . "Use Flags")
2532 (init . (lambda ()
2533 (unless anything-c-gentoo-use-flags
2534 (anything-c-gentoo-setup-use-flags-cache))
2535 (anything-c-gentoo-get-use)))
2536 (candidates-in-buffer)
2537 (match . identity)
2538 (candidate-transformer anything-c-highlight-local-use)
2539 (action . (("Show which dep use this flag"
2540 . (lambda (elm)
2541 (switch-to-buffer anything-c-gentoo-buffer)
2542 (erase-buffer)
2543 (apply #'call-process "equery" nil t nil
2544 `("-C"
2546 ,elm))))
2547 ("Description"
2548 . (lambda (elm)
2549 (switch-to-buffer anything-c-gentoo-buffer)
2550 (erase-buffer)
2551 (apply #'call-process "euse" nil t nil
2552 `("-i"
2553 ,elm))
2554 (font-lock-add-keywords nil `((,elm . font-lock-variable-name-face)))
2555 (font-lock-mode 1)))))))
2558 ;; (anything 'anything-c-source-use-flags)
2560 (defun anything-c-gentoo-init-list ()
2561 "Initialize buffer with all packages in Portage."
2562 (let* ((portage-buf (get-buffer-create "*anything-gentoo*"))
2563 (buf (anything-candidate-buffer 'portage-buf)))
2564 (with-current-buffer buf
2565 (dolist (i anything-c-cache-gentoo)
2566 (insert (concat i "\n"))))))
2568 (defun anything-c-gentoo-setup-cache ()
2569 "Set up `anything-c-cache-gentoo'"
2570 (setq anything-c-cache-gentoo
2571 (split-string (with-temp-buffer
2572 (call-process "eix" nil t nil
2573 "--only-names")
2574 (buffer-string)))))
2576 (defun anything-c-gentoo-eshell-action (elm command)
2577 (when (get-buffer "*EShell Command Output*")
2578 (kill-buffer "*EShell Command Output*"))
2579 (message "Wait searching...")
2580 (eshell-command (format "%s %s" command elm)))
2582 (defun anything-c-gentoo-get-use ()
2583 "Initialize buffer with all use flags."
2584 (let* ((use-buf (get-buffer-create "*anything-gentoo-use*"))
2585 (buf (anything-candidate-buffer 'use-buf)))
2586 (with-current-buffer buf
2587 (dolist (i anything-c-gentoo-use-flags)
2588 (insert (concat i "\n"))))))
2591 (defun anything-c-gentoo-setup-use-flags-cache ()
2592 "Setup `anything-c-gentoo-use-flags'"
2593 (setq anything-c-gentoo-use-flags
2594 (split-string (with-temp-buffer
2595 (call-process "eix" nil t nil
2596 "--print-all-useflags")
2597 (buffer-string)))))
2599 (defun anything-c-gentoo-get-url (elm)
2600 "Return a list of urls from eix output."
2601 (split-string (eshell-command-result
2602 (format "eix %s | grep Homepage | awk '{print $2}'" elm))))
2604 (defun anything-c-gentoo-get-world ()
2605 "Return list of all installed package on your system."
2606 (split-string (with-temp-buffer
2607 (call-process "qlist" nil t nil
2608 "-I")
2609 (buffer-string))))
2611 (defun anything-c-gentoo-get-local-use ()
2612 (split-string (with-temp-buffer
2613 (call-process "portageq" nil t nil
2614 "envvar"
2615 "USE")
2616 (buffer-string))))
2618 (defface anything-gentoo-match-face '((t (:foreground "red")))
2619 "Face for anything-gentoo installed packages."
2620 :group 'traverse-faces)
2622 (defun anything-c-highlight-world (eix)
2623 "Highlight all installed package."
2624 (loop for i in eix
2625 if (member i anything-c-cache-world)
2626 collect (propertize i 'face 'anything-gentoo-match-face)
2627 else
2628 collect i))
2630 (defun anything-c-highlight-local-use (use-flags)
2631 (let ((local-uses (anything-c-gentoo-get-local-use)))
2632 (loop for i in use-flags
2633 if (member i local-uses)
2634 collect (propertize i 'face 'anything-gentoo-match-face)
2635 else
2636 collect i)))
2638 (defvar anything-c-source-emacs-process
2639 '((name . "Emacs Process")
2640 (candidates . (lambda ()
2641 (mapcar #'process-name
2642 (process-list))))
2643 (action . (("Kill Process" . (lambda (elm)
2644 (delete-process (get-process elm))))))))
2646 ;; (anything 'anything-c-source-emacs-process)
2648 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Action Helpers ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2649 ;;; Files
2650 (defvar anything-c-external-commands-list nil
2651 "A list of all external commands the user can execute. If this
2652 variable is not set by the user, it will be calculated
2653 automatically.")
2655 (defun anything-c-external-commands-list-1 ()
2656 "Returns a list of all external commands the user can execute.
2658 If `anything-c-external-commands-list' is non-nil it will
2659 return its contents. Else it calculates all external commands
2660 and sets `anything-c-external-commands-list'.
2662 The code is ripped out of `eshell-complete-commands-list'."
2663 (if anything-c-external-commands-list
2664 anything-c-external-commands-list
2665 (setq anything-c-external-commands-list
2666 (let* ((paths (split-string (getenv "PATH") path-separator))
2667 (cwd (file-name-as-directory
2668 (expand-file-name default-directory)))
2669 (path "") (comps-in-path ())
2670 (file "") (filepath "") (completions ()))
2671 ;; Go thru each path in the search path, finding completions.
2672 (while paths
2673 (setq path (file-name-as-directory
2674 (expand-file-name (or (car paths) ".")))
2675 comps-in-path
2676 (and (file-accessible-directory-p path)
2677 (file-name-all-completions "" path)))
2678 ;; Go thru each completion found, to see whether it should be
2679 ;; used, e.g. see if it's executable.
2680 (while comps-in-path
2681 (setq file (car comps-in-path)
2682 filepath (concat path file))
2683 (if (and (not (member file completions))
2684 (or (string-equal path cwd)
2685 (not (file-directory-p filepath)))
2686 (file-executable-p filepath))
2687 (setq completions (cons file completions)))
2688 (setq comps-in-path (cdr comps-in-path)))
2689 (setq paths (cdr paths)))
2690 completions))))
2692 (defun anything-c-file-buffers (filename)
2693 "Returns a list of those buffer names which correspond to the
2694 file given by FILENAME."
2695 (let (name ret)
2696 (dolist (buf (buffer-list) ret)
2697 (let ((bfn (buffer-file-name buf)))
2698 (when (and bfn
2699 (string= filename bfn))
2700 (push (buffer-name buf) ret)))
2701 ret)))
2703 (defun anything-c-delete-file (file)
2704 "Delete the given file after querying the user. Ask to kill
2705 buffers associated with that file, too."
2706 (if (y-or-n-p (format "Really delete file %s? " file))
2707 (progn
2708 (let ((buffers (anything-c-file-buffers file)))
2709 (delete-file file)
2710 (dolist (buf buffers)
2711 (when (y-or-n-p (format "Kill buffer %s, too? " buf))
2712 (kill-buffer buf)))))
2713 (message "Nothing deleted.")))
2715 (defun anything-c-open-file-externally (file)
2716 "Open FILE with an external tool. Query the user which tool to
2717 use."
2718 (start-process "anything-c-open-file-externally"
2720 (completing-read "Program: "
2721 (anything-c-external-commands-list-1))
2722 file))
2724 (defun w32-shell-execute-open-file (file)
2725 (interactive "fOpen file:")
2726 (with-no-warnings
2727 (w32-shell-execute "open" (replace-regexp-in-string ;for UNC paths
2728 "/" "\\"
2729 (replace-regexp-in-string ; strip cygdrive paths
2730 "/cygdrive/\\(.\\)" "\\1:" file nil nil) nil t))))
2731 (defun anything-c-open-file-with-default-tool (file)
2732 "Open FILE with the default tool on this platform."
2733 (if (eq system-type 'windows-nt)
2734 (w32-shell-execute-open-file file)
2735 (start-process "anything-c-open-file-with-default-tool"
2737 (cond ((eq system-type 'gnu/linux)
2738 "xdg-open")
2739 ((or (eq system-type 'darwin) ;; Mac OS X
2740 (eq system-type 'macos)) ;; Mac OS 9
2741 "open"))
2742 file)))
2744 (defun anything-c-open-dired (file)
2745 "Opens a dired buffer in FILE's directory. If FILE is a
2746 directory, open this directory."
2747 (if (file-directory-p file)
2748 (dired file)
2749 (dired (file-name-directory file))
2750 (dired-goto-file file)))
2752 (defun anything-c-display-to-real-line (candidate)
2753 (if (string-match "^ *\\([0-9]+\\):\\(.+\\)$" candidate)
2754 (list (string-to-number (match-string 1 candidate)) (match-string 2 candidate))
2755 (error "Line number not found")))
2757 (defun anything-c-action-line-goto (lineno-and-content)
2758 (apply #'anything-goto-file-line (anything-attr 'target-file)
2759 (append lineno-and-content
2760 (list (if (and (anything-attr-defined 'target-file)
2761 (not anything-in-persistent-action))
2762 'find-file-other-window
2763 'find-file)))))
2765 (defun* anything-c-action-file-line-goto (file-line-content &optional (find-file-function #'find-file))
2766 (apply #'anything-goto-file-line file-line-content))
2768 (require 'compile)
2769 (defun anything-c-filtered-candidate-transformer-file-line (candidates source)
2770 (mapcar
2771 (lambda (candidate)
2772 (if (not (string-match "^\\(.+?\\):\\([0-9]+\\):\\(.+\\)$" candidate))
2773 (error "Filename and line number not found")
2774 (let ((filename (match-string 1 candidate))
2775 (lineno (match-string 2 candidate))
2776 (content (match-string 3 candidate)))
2777 (cons (format "%s:%s\n %s"
2778 (propertize filename 'face compilation-info-face)
2779 (propertize lineno 'face compilation-line-face)
2780 content)
2781 (list (expand-file-name
2782 filename
2783 (anything-aif (anything-attr 'default-directory)
2784 (if (functionp it) (funcall it) it)
2785 (and (anything-candidate-buffer)
2786 (buffer-local-value
2787 'default-directory
2788 (anything-candidate-buffer)))))
2789 (string-to-number lineno) content)))))
2790 candidates))
2792 (defun* anything-goto-file-line (file lineno content &optional (find-file-function #'find-file))
2793 (anything-aif (anything-attr 'before-jump-hook)
2794 (funcall it))
2795 (when file (funcall find-file-function file))
2796 (if (anything-attr-defined 'adjust)
2797 (anything-c-goto-line-with-adjustment lineno content)
2798 (goto-line lineno))
2799 (unless (anything-attr-defined 'recenter)
2800 (set-window-start (get-buffer-window anything-current-buffer) (point)))
2801 (anything-aif (anything-attr 'after-jump-hook)
2802 (funcall it))
2803 (when anything-in-persistent-action
2804 (anything-match-line-color-current-line)))
2806 (defun anything-find-file-as-root (candidate)
2807 (find-file (concat "/" anything-su-or-sudo "::" (expand-file-name candidate))))
2809 ;; borrowed from etags.el
2810 ;; (anything-c-goto-line-with-adjustment (line-number-at-pos) ";; borrowed from etags.el")
2811 (defun anything-c-goto-line-with-adjustment (line line-content)
2812 (let ((startpos)
2813 offset found pat)
2814 ;; This constant is 1/2 the initial search window.
2815 ;; There is no sense in making it too small,
2816 ;; since just going around the loop once probably
2817 ;; costs about as much as searching 2000 chars.
2818 (setq offset 1000
2819 found nil
2820 pat (concat (if (eq selective-display t)
2821 "\\(^\\|\^m\\) *" "^ *") ;allow indent
2822 (regexp-quote line-content)))
2823 ;; If no char pos was given, try the given line number.
2824 (setq startpos (progn (goto-line line) (point)))
2825 (or startpos (setq startpos (point-min)))
2826 ;; First see if the tag is right at the specified location.
2827 (goto-char startpos)
2828 (setq found (looking-at pat))
2829 (while (and (not found)
2830 (progn
2831 (goto-char (- startpos offset))
2832 (not (bobp))))
2833 (setq found
2834 (re-search-forward pat (+ startpos offset) t)
2835 offset (* 3 offset))) ; expand search window
2836 (or found
2837 (re-search-forward pat nil t)
2838 (error "not found")))
2839 ;; Position point at the right place
2840 ;; if the search string matched an extra Ctrl-m at the beginning.
2841 (and (eq selective-display t)
2842 (looking-at "\^m")
2843 (forward-char 1))
2844 (beginning-of-line))
2846 (anything-document-attribute 'default-directory "type . file-line"
2847 "`default-directory' to interpret file.")
2848 (anything-document-attribute 'before-jump-hook "type . file-line / line"
2849 "Function to call before jumping to the target location.")
2850 (anything-document-attribute 'after-jump-hook "type . file-line / line"
2851 "Function to call after jumping to the target location.")
2852 (anything-document-attribute 'adjust "type . file-line"
2853 "Search around line matching line contents.")
2854 (anything-document-attribute 'recenter "type . file-line / line"
2855 "`recenter' after jumping.")
2856 (anything-document-attribute 'target-file "type . line"
2857 "Goto line of target-file.")
2859 (defun anything-c-call-interactively (cmd-or-name)
2860 "Execute CMD-OR-NAME as Emacs command.
2861 It is added to `extended-command-history'.
2862 `anything-current-prefix-arg' is used as the command's prefix argument."
2863 (setq extended-command-history
2864 (cons (anything-c-stringify cmd-or-name)
2865 (delete (anything-c-stringify cmd-or-name) extended-command-history)))
2866 (let ((current-prefix-arg anything-current-prefix-arg))
2867 (call-interactively (anything-c-symbolify cmd-or-name))))
2869 (defun anything-c-set-variable (var)
2870 "Set value to VAR interactively."
2871 (interactive)
2872 (let ((sym (anything-c-symbolify var)))
2873 (set sym (eval-minibuffer (format "Set %s: " var)
2874 (prin1-to-string (symbol-value sym))))))
2875 ;; (setq hh 12)
2876 ;; (anything-c-set-variable 'hh)
2878 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Persistent Action Helpers ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2879 (defvar anything-match-line-overlay-face nil)
2880 (defvar anything-match-line-overlay nil)
2882 (defun anything-match-line-color-current-line (&optional start end buf face rec)
2883 "Highlight and underline current position"
2884 (let ((args (list (or start (line-beginning-position))
2885 (or end (1+ (line-end-position)))
2886 buf)))
2887 (if (not anything-match-line-overlay)
2888 (setq anything-match-line-overlay (apply 'make-overlay args))
2889 (apply 'move-overlay anything-match-line-overlay args)))
2890 (overlay-put anything-match-line-overlay
2891 'face (or face anything-match-line-overlay-face))
2892 (when rec
2893 (goto-char start)
2894 (recenter)))
2896 (defalias 'anything-persistent-highlight-point 'anything-match-line-color-current-line)
2898 (defface anything-overlay-line-face '((t (:background "IndianRed4" :underline t)))
2899 "Face for source header in the anything buffer." :group 'anything)
2901 (setq anything-match-line-overlay-face 'anything-overlay-line-face)
2903 (add-hook 'anything-cleanup-hook #'(lambda ()
2904 (when anything-match-line-overlay
2905 (delete-overlay anything-match-line-overlay)
2906 (setq anything-match-line-overlay nil))))
2908 (add-hook 'anything-after-persistent-action-hook #'(lambda ()
2909 (when anything-match-line-overlay
2910 (delete-overlay anything-match-line-overlay)
2911 (anything-match-line-color-current-line))))
2913 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Actions Transformers ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2914 ;;; Files
2915 (defun anything-c-transform-file-load-el (actions candidate)
2916 "Add action to load the file CANDIDATE if it is an emacs lisp
2917 file. Else return ACTIONS unmodified."
2918 (if (or (string= (file-name-extension candidate) "el")
2919 (string= (file-name-extension candidate) "elc"))
2920 (append actions '(("Load Emacs Lisp File" . load-file)))
2921 actions))
2923 (defun anything-c-transform-file-browse-url (actions candidate)
2924 "Add an action to browse the file CANDIDATE if it in a html
2925 file. Else return ACTIONS unmodified."
2926 (if (or (string= (file-name-extension candidate) "htm")
2927 (string= (file-name-extension candidate) "html"))
2928 (append actions '(("Browse with Browser" . browse-url)))
2929 actions))
2931 ;;;; Function
2932 (defun anything-c-transform-function-call-interactively (actions candidate)
2933 "Add an action to call the function CANDIDATE interactively if
2934 it is a command. Else return ACTIONS unmodified."
2935 (if (commandp (intern-soft candidate))
2936 (append actions '(("Call Interactively"
2938 anything-c-call-interactively)))
2939 actions))
2941 ;;;; S-Expressions
2942 (defun anything-c-transform-sexp-eval-command-sexp (actions candidate)
2943 "If CANDIDATE's `car' is a command, then add an action to
2944 evaluate it and put it onto the `command-history'."
2945 (if (commandp (car (read candidate)))
2946 ;; Make it first entry
2947 (cons '("Eval and put onto command-history" .
2948 (lambda (sexp)
2949 (let ((sym (read sexp)))
2950 (eval sym)
2951 (setq command-history
2952 (cons sym command-history)))))
2953 actions)
2954 actions))
2956 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Candidate Transformers ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2957 ;;; Buffers
2958 (defun anything-c-skip-boring-buffers (buffers)
2959 (anything-c-skip-entries buffers anything-c-boring-buffer-regexp))
2961 (defun anything-c-skip-current-buffer (buffers)
2962 (remove (buffer-name anything-current-buffer) buffers))
2964 (defun anything-c-shadow-boring-buffers (buffers)
2965 "Buffers matching `anything-c-boring-buffer-regexp' will be
2966 displayed with the `file-name-shadow' face if available."
2967 (anything-c-shadow-entries buffers anything-c-boring-buffer-regexp))
2969 ;;; Files
2970 (defun anything-c-shadow-boring-files (files)
2971 "Files matching `anything-c-boring-file-regexp' will be
2972 displayed with the `file-name-shadow' face if available."
2973 (anything-c-shadow-entries files anything-c-boring-file-regexp))
2975 (defun anything-c-skip-boring-files (files)
2976 "Files matching `anything-c-boring-file-regexp' will be skipped."
2977 (anything-c-skip-entries files anything-c-boring-file-regexp))
2978 ;; (anything-c-skip-boring-files '("README" "/src/.svn/hoge"))
2980 (defun anything-c-skip-current-file (files)
2981 "Current file will be skipped."
2982 (remove (buffer-file-name anything-current-buffer) files))
2984 (defun anything-c-w32-pathname-transformer (args)
2985 "Change undesirable features of windows pathnames to ones more acceptable to
2986 other candidate transformers."
2987 (if (eq system-type 'windows-nt)
2988 (mapcar (lambda (x)
2989 (replace-regexp-in-string "/cygdrive/\\(.\\)" "\\1:" x))
2990 (mapcar (lambda (y)
2991 (replace-regexp-in-string "\\\\" "/" y)) args))
2992 args))
2994 (defun anything-c-shorten-home-path (files)
2995 "Replaces /home/user with ~."
2996 (mapcar (lambda (file)
2997 (let ((home (replace-regexp-in-string "\\\\" "/" ; stupid Windows...
2998 (getenv "HOME"))))
2999 (if (and (stringp file) (string-match home file))
3000 (cons (replace-match "~" nil nil file) file)
3001 file)))
3002 files))
3004 ;;; Functions
3005 (defun anything-c-mark-interactive-functions (functions)
3006 "Mark interactive functions (commands) with (i) after the function name."
3007 (let (list)
3008 (loop for function in functions
3009 do (push (cons (concat function
3010 (when (commandp (intern-soft function)) " (i)"))
3011 function)
3012 list)
3013 finally (return (nreverse list)))))
3015 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Marked candidates ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3017 (defun anything-c-list-marked-candidate ()
3018 (interactive)
3019 (let (marked-candidates)
3020 (with-anything-window
3021 (goto-char (point-min))
3022 (beginning-of-line)
3023 (while (anything-next-visible-mark)
3024 (push (buffer-substring-no-properties (point-at-bol) (point-at-eol)) marked-candidates)))
3025 marked-candidates))
3027 (defvar anything-c-marked-candidate-list nil)
3028 (defadvice anything-select-action (before save-marked-candidates () activate)
3029 (setq anything-c-marked-candidate-list (anything-c-list-marked-candidate)))
3031 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Adaptive Sorting of Candidates ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3032 (defvar anything-c-adaptive-done nil
3033 "nil if history information is not yet stored for the current
3034 selection.")
3036 (defvar anything-c-adaptive-history nil
3037 "Contains the stored history information.
3038 Format: ((SOURCE-NAME (SELECTED-CANDIDATE (PATTERN . NUMBER-OF-USE) ...) ...) ...)")
3040 (defadvice anything-initialize (before anything-c-adaptive-initialize activate)
3041 "Advise `anything-initialize' to reset `anything-c-adaptive-done'
3042 when anything is started."
3043 (setq anything-c-adaptive-done nil))
3045 (defadvice anything-exit-minibuffer (before anything-c-adaptive-exit-minibuffer activate)
3046 "Advise `anything-exit-minibuffer' to store history information
3047 when a candidate is selected with RET."
3048 (anything-c-adaptive-store-selection))
3050 (defadvice anything-select-action (before anything-c-adaptive-select-action activate)
3051 "Advise `anything-select-action' to store history information
3052 when the user goes to the action list with TAB."
3053 (anything-c-adaptive-store-selection))
3055 (defun anything-c-adaptive-store-selection ()
3056 "Store history information for the selected candidate."
3057 (unless anything-c-adaptive-done
3058 (setq anything-c-adaptive-done t)
3059 (let* ((source (anything-get-current-source))
3060 (source-name (or (assoc-default 'type source)
3061 (assoc-default 'name source)))
3062 (source-info (or (assoc source-name anything-c-adaptive-history)
3063 (progn
3064 (push (list source-name) anything-c-adaptive-history)
3065 (car anything-c-adaptive-history))))
3066 (selection (anything-get-selection))
3067 (selection-info (progn
3068 (setcdr source-info
3069 (cons
3070 (let ((found (assoc selection (cdr source-info))))
3071 (if (not found)
3072 ;; new entry
3073 (list selection)
3075 ;; move entry to the beginning of the
3076 ;; list, so that it doesn't get
3077 ;; trimmed when the history is
3078 ;; truncated
3079 (setcdr source-info
3080 (delete found (cdr source-info)))
3081 found))
3082 (cdr source-info)))
3083 (cadr source-info)))
3084 (pattern-info (progn
3085 (setcdr selection-info
3086 (cons
3087 (let ((found (assoc anything-pattern (cdr selection-info))))
3088 (if (not found)
3089 ;; new entry
3090 (cons anything-pattern 0)
3092 ;; move entry to the beginning of the
3093 ;; list, so if two patterns used the
3094 ;; same number of times then the one
3095 ;; used last appears first in the list
3096 (setcdr selection-info
3097 (delete found (cdr selection-info)))
3098 found))
3099 (cdr selection-info)))
3100 (cadr selection-info))))
3102 ;; increase usage count
3103 (setcdr pattern-info (1+ (cdr pattern-info)))
3105 ;; truncate history if needed
3106 (if (> (length (cdr selection-info)) anything-c-adaptive-history-length)
3107 (setcdr selection-info
3108 (subseq (cdr selection-info) 0 anything-c-adaptive-history-length))))))
3110 (if (file-readable-p anything-c-adaptive-history-file)
3111 (load-file anything-c-adaptive-history-file))
3112 (add-hook 'kill-emacs-hook 'anything-c-adaptive-save-history)
3114 (defun anything-c-adaptive-save-history ()
3115 "Save history information to file given by `anything-c-adaptive-history-file'."
3116 (interactive)
3117 (with-temp-buffer
3118 (insert
3119 ";; -*- mode: emacs-lisp -*-\n"
3120 ";; History entries used for anything adaptive display.\n")
3121 (prin1 `(setq anything-c-adaptive-history ',anything-c-adaptive-history)
3122 (current-buffer))
3123 (insert ?\n)
3124 (write-region (point-min) (point-max) anything-c-adaptive-history-file nil
3125 (unless (interactive-p) 'quiet))))
3127 (defun anything-c-adaptive-sort (candidates source)
3128 "Sort the CANDIDATES for SOURCE by usage frequency.
3129 This is a filtered candidate transformer you can use for the
3130 attribute `filtered-candidate-transformer' of a source in
3131 `anything-sources' or a type in `anything-type-attributes'."
3132 (let* ((source-name (or (assoc-default 'type source)
3133 (assoc-default 'name source)))
3134 (source-info (assoc source-name anything-c-adaptive-history)))
3135 (if (not source-info)
3136 ;; if there is no information stored for this source then do nothing
3137 candidates
3138 ;; else...
3139 (let ((usage
3140 ;; ... assemble a list containing the (CANIDATE . USAGE-COUNT)
3141 ;; pairs
3142 (mapcar (lambda (candidate-info)
3143 (let ((count 0))
3144 (dolist (pattern-info (cdr candidate-info))
3145 (if (not (equal (car pattern-info)
3146 anything-pattern))
3147 (incf count (cdr pattern-info))
3149 ;; if current pattern is equal to the previously
3150 ;; used one then this candidate has priority
3151 ;; (that's why its count is boosted by 10000) and
3152 ;; it only has to compete with other candidates
3153 ;; which were also selected with the same pattern
3154 (setq count (+ 10000 (cdr pattern-info)))
3155 (return)))
3156 (cons (car candidate-info) count)))
3157 (cdr source-info)))
3158 sorted)
3160 ;; sort the list in descending order, so candidates with highest
3161 ;; priorty come first
3162 (setq usage (sort usage (lambda (first second)
3163 (> (cdr first) (cdr second)))))
3165 ;; put those candidates first which have the highest usage count
3166 (dolist (info usage)
3167 (when (member* (car info) candidates
3168 :test 'anything-c-adaptive-compare)
3169 (push (car info) sorted)
3170 (setq candidates (remove* (car info) candidates
3171 :test 'anything-c-adaptive-compare))))
3173 ;; and append the rest
3174 (append (reverse sorted) candidates nil)))))
3176 (defun anything-c-adaptive-compare (x y)
3177 "Compare candidates X and Y taking into account that the
3178 candidate can be in (DISPLAY . REAL) format."
3179 (equal (if (listp x)
3180 (cdr x)
3182 (if (listp y)
3183 (cdr y)
3184 y)))
3186 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Plug-in ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3187 ;; Plug-in: candidates-file
3188 (defun anything-compile-source--candidates-file (source)
3189 (if (assoc-default 'candidates-file source)
3190 `((init anything-p-candidats-file-init
3191 ,@(let ((orig-init (assoc-default 'init source)))
3192 (cond ((null orig-init) nil)
3193 ((functionp orig-init) (list orig-init))
3194 (t orig-init))))
3195 (candidates-in-buffer)
3196 ,@source)
3197 source))
3198 (add-to-list 'anything-compile-source-functions 'anything-compile-source--candidates-file)
3200 (defun anything-p-candidats-file-init ()
3201 (destructuring-bind (file &optional updating)
3202 (anything-mklist (anything-attr 'candidates-file))
3203 (with-current-buffer (anything-candidate-buffer (find-file-noselect file))
3204 (when updating
3205 (buffer-disable-undo)
3206 (font-lock-mode -1)
3207 (auto-revert-mode 1)))))
3209 (anything-document-attribute 'candidates-file "candidates-file plugin"
3210 "Use a file as the candidates buffer.
3212 If optional 2nd argument is non-nil, the file opened with `auto-revert-mode'.")
3214 ;; Plug-in: headline
3215 (defun anything-compile-source--anything-headline (source)
3216 (if (assoc-default 'headline source)
3217 (append '((init . anything-headline-init)
3218 (get-line-fn . buffer-substring)
3219 (type . line))
3220 source
3221 '((candidates-in-buffer)))
3222 source))
3223 (add-to-list 'anything-compile-source-functions 'anything-compile-source--anything-headline)
3225 (defun anything-headline-init ()
3226 (when (and (anything-current-buffer-is-modified)
3227 (with-current-buffer anything-current-buffer
3228 (eval (or (anything-attr 'condition) t))))
3229 (anything-headline-make-candidate-buffer
3230 (anything-attr 'headline)
3231 (anything-attr 'subexp))))
3233 (anything-document-attribute 'headline "Headline plug-in"
3234 "Regexp string for anything-headline to scan.")
3235 (anything-document-attribute 'condition "Headline plug-in"
3236 "A sexp representing the condition to use anything-headline.")
3237 (anything-document-attribute 'subexp "Headline plug-in"
3238 "Display (match-string-no-properties subexp).")
3240 (defun anything-headline-get-candidates (regexp subexp)
3241 (save-excursion
3242 (set-buffer anything-current-buffer)
3243 (save-excursion
3244 (goto-char (point-min))
3245 (if (functionp regexp) (setq regexp (funcall regexp)))
3246 (let (hierarchy curhead)
3247 (flet ((matched ()
3248 (if (numberp subexp)
3249 (cons (match-string-no-properties subexp) (match-beginning subexp))
3250 (cons (buffer-substring (point-at-bol) (point-at-eol))
3251 (point-at-bol))))
3252 (hierarchies (headlines)
3253 (1+ (loop for (_ . hierarchy) in headlines
3254 maximize hierarchy)))
3255 (vector-0-n (v n)
3256 (loop for i from 0 to hierarchy
3257 collecting (aref curhead i)))
3258 (arrange (headlines)
3259 (loop with curhead = (make-vector (hierarchies headlines) "")
3260 for ((str . pt) . hierarchy) in headlines
3261 do (aset curhead hierarchy str)
3262 collecting
3263 (cons
3264 (mapconcat 'identity (vector-0-n curhead hierarchy) " / ")
3265 pt))))
3266 (if (listp regexp)
3267 (arrange
3268 (sort
3269 (loop for re in regexp
3270 for hierarchy from 0
3271 do (goto-char (point-min))
3272 appending
3273 (loop
3274 while (re-search-forward re nil t)
3275 collect (cons (matched) hierarchy)))
3276 (lambda (a b) (> (cdar b) (cdar a)))))
3277 (loop while (re-search-forward regexp nil t)
3278 collect (matched))))))))
3280 (defun anything-headline-make-candidate-buffer (regexp subexp)
3281 (with-current-buffer (anything-candidate-buffer 'local)
3282 (loop for (content . pos) in (anything-headline-get-candidates regexp subexp)
3283 do (insert
3284 (format "%5d:%s\n"
3285 (with-current-buffer anything-current-buffer
3286 (line-number-at-pos pos))
3287 content)))))
3289 (defun anything-headline-goto-position (pos recenter)
3290 (goto-char pos)
3291 (unless recenter
3292 (set-window-start (get-buffer-window anything-current-buffer) (point))))
3294 (defun anything-revert-buffer (candidate)
3295 (with-current-buffer candidate
3296 (when (buffer-modified-p)
3297 (revert-buffer t t))))
3299 (defun anything-revert-marked-buffers (candidate)
3300 (dolist (i anything-c-marked-candidate-list)
3301 (anything-revert-buffer i)))
3303 (defun anything-kill-marked-buffers (candidate)
3304 (dolist (i anything-c-marked-candidate-list)
3305 (kill-buffer i)))
3307 (defun anything-delete-marked-files (candidate)
3308 (dolist (i anything-c-marked-candidate-list)
3309 (anything-c-delete-file i)))
3311 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Setup ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3313 ;;; Type Attributes
3314 (define-anything-type-attribute 'buffer
3315 `((action
3316 ,@(if pop-up-frames
3317 '(("Switch to buffer other window" . switch-to-buffer-other-window)
3318 ("Switch to buffer" . switch-to-buffer))
3319 '(("Switch to buffer" . switch-to-buffer)
3320 ("Switch to buffer other window" . switch-to-buffer-other-window)
3321 ("Switch to buffer other frame" . switch-to-buffer-other-frame)))
3322 ("Display buffer" . display-buffer)
3323 ("Revert buffer" . anything-revert-buffer)
3324 ("Revert Marked buffers" . anything-revert-marked-buffers)
3325 ("Kill buffer" . kill-buffer)
3326 ("Kill Marked buffers" . anything-kill-marked-buffers))
3327 (candidate-transformer . anything-c-skip-boring-buffers))
3328 "Buffer or buffer name.")
3330 (define-anything-type-attribute 'file
3331 `((action
3332 ,@(if pop-up-frames
3333 '(("Find file other window" . find-file-other-window)
3334 ("Find file" . find-file)
3335 ("Find file as root" . anything-find-file-as-root))
3336 '(("Find file" . find-file)
3337 ("Find file as root" . anything-find-file-as-root)
3338 ("Find file other window" . find-file-other-window)
3339 ("Find file other frame" . find-file-other-frame)))
3340 ("Open dired in file's directory" . anything-c-open-dired)
3341 ("Delete file" . anything-c-delete-file)
3342 ("Delete Marked files" . anything-delete-marked-files)
3343 ("Open file externally" . anything-c-open-file-externally)
3344 ("Open file with default tool" . anything-c-open-file-with-default-tool))
3345 (action-transformer anything-c-transform-file-load-el
3346 anything-c-transform-file-browse-url)
3347 (candidate-transformer anything-c-w32-pathname-transformer
3348 anything-c-skip-current-file
3349 anything-c-skip-boring-files
3350 anything-c-shorten-home-path))
3351 "File name.")
3353 (define-anything-type-attribute 'command
3354 `((action ("Call interactively" . anything-c-call-interactively)
3355 ("Describe command" . anything-c-describe-function)
3356 ("Add command to kill ring" . anything-c-kill-new)
3357 ("Go to command's definition" . anything-c-find-function))
3358 ;; Sort commands according to their usage count.
3359 (filtered-candidate-transformer . anything-c-adaptive-sort))
3360 "Command. (string or symbol)")
3362 (define-anything-type-attribute 'function
3363 '((action ("Describe function" . anything-c-describe-function)
3364 ("Add function to kill ring" . anything-c-kill-new)
3365 ("Go to function's definition" . anything-c-find-function))
3366 (action-transformer anything-c-transform-function-call-interactively)
3367 (candidate-transformer anything-c-mark-interactive-functions))
3368 "Function. (string or symbol)")
3370 (define-anything-type-attribute 'variable
3371 '((action ("Describe variable" . anything-c-describe-variable)
3372 ("Add variable to kill ring" . anything-c-kill-new)
3373 ("Go to variable's definition" . anything-c-find-variable)
3374 ("Set variable" . anything-c-set-variable)))
3375 "Variable.")
3377 (define-anything-type-attribute 'sexp
3378 '((action ("Eval s-expression" . (lambda (c) (eval (read c))))
3379 ("Add s-expression to kill ring" . kill-new))
3380 (action-transformer anything-c-transform-sexp-eval-command-sexp))
3381 "String representing S-Expressions.")
3383 (define-anything-type-attribute 'bookmark
3384 '((action ("Jump to bookmark" . (lambda (candidate)
3385 (bookmark-jump candidate)
3386 (anything-update)))
3387 ("Delete bookmark" . bookmark-delete)
3388 ("Rename bookmark" . bookmark-rename)
3389 ("Relocate bookmark" . bookmark-relocate)))
3390 "Bookmark name.")
3392 (define-anything-type-attribute 'line
3393 '((display-to-real . anything-c-display-to-real-line)
3394 (action ("Go to Line" . anything-c-action-line-goto)))
3395 "LINENO:CONTENT string, eg. \" 16:foo\".")
3397 ;;;; unit test
3398 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el")
3399 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el")
3400 (dont-compile
3401 (when (fboundp 'expectations)
3402 (expectations
3403 (desc "candidates-file plug-in")
3404 (expect '(anything-p-candidats-file-init)
3405 (assoc-default 'init
3406 (car (anything-compile-sources
3407 '(((name . "test")
3408 (candidates-file . "test.txt")))
3409 '(anything-compile-source--candidates-file)))))
3410 (expect '(anything-p-candidats-file-init
3411 (lambda () 1))
3412 (assoc-default 'init
3413 (car (anything-compile-sources
3414 '(((name . "test")
3415 (candidates-file . "test.txt")
3416 (init . (lambda () 1))))
3417 '(anything-compile-source--candidates-file)))))
3418 (expect '(anything-p-candidats-file-init
3419 (lambda () 1))
3420 (assoc-default 'init
3421 (car (anything-compile-sources
3422 '(((name . "test")
3423 (candidates-file . "test.txt")
3424 (init (lambda () 1))))
3425 '(anything-compile-source--candidates-file)))))
3426 (desc "anything-c-source-buffers")
3427 (expect '(("Buffers" ("foo" "curbuf")))
3428 (stub buffer-list => '("curbuf" " hidden" "foo" "*anything*"))
3429 (let ((anything-c-boring-buffer-regexp
3430 (rx (or
3431 (group bos " ")
3432 "*anything"
3433 ;; echo area
3434 " *Echo Area" " *Minibuf"))))
3435 (flet ((buffer-name (x) x))
3436 (anything-test-candidates 'anything-c-source-buffers))))
3437 (desc "anything-c-stringify")
3438 (expect "str1"
3439 (anything-c-stringify "str1"))
3440 (expect "str2"
3441 (anything-c-stringify 'str2))
3442 (desc "anything-c-symbolify")
3443 (expect 'sym1
3444 (anything-c-symbolify "sym1"))
3445 (expect 'sym2
3446 (anything-c-symbolify 'sym2)))))
3449 (provide 'anything-config)
3451 ;;; Local Variables:
3452 ;;; time-stamp-format: "%:y-%02m-%02d %02H:%02M:%02S (%Z) %u"
3453 ;;; End:
3455 ;; How to save (DO NOT REMOVE!!)
3456 ;; (emacswiki-post "anything-config.el")
3457 ;;; anything-config.el ends here
3459 ;;; LocalWords: Tassilo Patrovics Vagn Johansen Dahl Clementson infos
3460 ;;; LocalWords: Kamphausen informations McBrayer Volpiatto bbdb bb
3461 ;;; LocalWords: iswitchb imenu Recentf sym samewindow pos bol eol
3462 ;;; LocalWords: aif str lst func attrib recentf lessp prin mapatoms commandp
3463 ;;; LocalWords: cmd stb Picklist picklist mapcan subentry destructuring dirs
3464 ;;; LocalWords: darwin locat MacOS mdfind Firstname Lastname calc prepend jids
3465 ;;; LocalWords: dotimes Thierry online vname
3466 ;;; LocalWords: csharp javascript lua makefile cperl zcat lineno buf
3467 ;;; LocalWords: multiline href fn cand NewTitle cwd filepath thru ret
3468 ;;; LocalWords: bfn fOpen UNC cygdrive nt xdg macos FILE's elc rx svn hg
3469 ;;; LocalWords: CANDIDATE's darcs facep pathname args pathnames subseq priorty
3470 ;;; LocalWords: Vokes rfind berkeley JST ffap lacarte bos
3471 ;;; LocalWords: Lacarte Minibuf epp LaCarte bm attrset migemo attr conf mklist
3472 ;;; LocalWords: startpos noselect dont desc