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