1 ;;; anything-complete.el --- completion with anything
2 ;; $Id: anything-complete.el,v 1.86 2010-03-31 23:14:13 rubikitch Exp $
4 ;; Copyright (C) 2008, 2009, 2010, 2011 rubikitch
6 ;; Author: rubikitch <rubikitch@ruby-lang.org>
7 ;; Keywords: matching, convenience, anything
8 ;; URL: http://www.emacswiki.org/cgi-bin/wiki/download/anything-complete.el
10 ;; This file is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; This file is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; Completion with Anything interface.
31 ;; Below are complete command list:
33 ;; `alcs-update-restart'
34 ;; Update lisp symbols and restart current `anything' session.
35 ;; `anything-lisp-complete-symbol'
36 ;; `lisp-complete-symbol' replacement using `anything'.
37 ;; `anything-lisp-complete-symbol-partial-match'
38 ;; `lisp-complete-symbol' replacement using `anything' (partial match).
40 ;; `apropos' replacement using `anything'.
41 ;; `anything-read-string-mode'
42 ;; If this minor mode is on, use `anything' version of `completing-read' and `read-file-name'.
43 ;; `anything-complete-shell-history'
44 ;; Select a command from shell history and insert it.
45 ;; `anything-execute-extended-command'
46 ;; Replacement of `execute-extended-command'.
48 ;;; Customizable Options:
50 ;; Below are customizable option list:
52 ;; `anything-complete-sort-candidates'
53 ;; *Whether to sort completion candidates.
55 ;; `anything-execute-extended-command-use-kyr'
56 ;; *Use `anything-kyr' (context-aware commands) in `anything-execute-extended-command'.
59 ;; * `anything-lisp-complete-symbol', `anything-lisp-complete-symbol-partial-match':
60 ;; `lisp-complete-symbol' with `anything'
61 ;; * `anything-apropos': `apropos' with `anything'
62 ;; * `anything-complete-shell-history': complete from .*sh_history
63 ;; * Many read functions:
64 ;; `anything-read-file-name', `anything-read-buffer', `anything-read-variable',
65 ;; `anything-read-command', `anything-completing-read'
66 ;; * `anything-read-string-mode' replaces default read functions with anything ones.
67 ;; * Many anything sources:
68 ;; [EVAL IT] (occur "defvar anything-c-source")
72 ;; Put anything-complete.el to your load-path.
73 ;; The load-path is usually ~/elisp/.
74 ;; It's set in your ~/.emacs like this:
75 ;; (add-to-list 'load-path (expand-file-name "~/elisp"))
77 ;; Then install dependencies.
79 ;; Install anything-match-plugin.el (must).
80 ;; M-x install-elisp http://www.emacswiki.org/cgi-bin/wiki/download/anything-match-plugin.el
82 ;; shell-history.el / shell-command.el would help you (optional).
83 ;; M-x install-elisp http://www.emacswiki.org/cgi-bin/wiki/download/shell-history.el
84 ;; M-x install-elisp http://www.emacswiki.org/cgi-bin/wiki/download/shell-command.el
86 ;; If you want `anything-execute-extended-command' to show
87 ;; context-aware commands, use anything-kyr.el and
88 ;; anything-kyr-config.el (optional).
90 ;; M-x install-elisp http://www.emacswiki.org/cgi-bin/wiki/download/anything-kyr.el
91 ;; M-x install-elisp http://www.emacswiki.org/cgi-bin/wiki/download/anything-kyr-config.el
93 ;; And the following to your ~/.emacs startup file.
95 ;; (require 'anything-complete)
96 ;; ;; Automatically collect symbols by 150 secs
97 ;; (anything-lisp-complete-symbol-set-timer 150)
98 ;; (define-key emacs-lisp-mode-map "\C-\M-i" 'anything-lisp-complete-symbol-partial-match)
99 ;; (define-key lisp-interaction-mode-map "\C-\M-i" 'anything-lisp-complete-symbol-partial-match)
100 ;; ;; replace completion commands with `anything'
101 ;; (anything-read-string-mode 1)
102 ;; ;; Bind C-o to complete shell history
103 ;; (anything-complete-shell-history-setup-key "\C-o")
107 ;; Change log of this file is found at
108 ;; http://repo.or.cz/w/anything-config.git/history/master:/anything-complete.el
110 ;; Change log of this project is found at
111 ;; http://repo.or.cz/w/anything-config.git?a=shortlog
116 (defvar anything-complete-version
"20161203")
117 (require 'anything-match-plugin
)
119 (require 'anything-obsolete
)
123 (when (require 'anything-show-completion nil t
)
124 (dolist (f '(anything-complete
125 anything-lisp-complete-symbol
126 anything-lisp-complete-symbol-partial-match
))
127 (use-anything-show-completion f
'(length anything-complete-target
))))
130 (defvar anything-complete-target
"")
132 (defun ac-insert (candidate)
134 (when (and (search-backward anything-complete-target nil t
)
135 (string= (buffer-substring (point) pt
) anything-complete-target
))
136 (delete-region (point) pt
)))
139 (define-anything-type-attribute 'complete
140 '((candidates-in-buffer)
141 (action . ac-insert
)))
143 ;; Warning: I'll change this function's interface. DON'T USE IN YOUR PROGRAM!
144 (defun anything-noresume (&optional any-sources any-input any-prompt any-resume any-preselect any-buffer
)
145 (let (anything-last-sources anything-compiled-sources anything-last-buffer
)
146 (anything any-sources any-input any-prompt
'noresume any-preselect any-buffer
)))
148 (defun anything-complete (sources target
&optional limit idle-delay input-idle-delay
)
149 "Basic completion interface using `anything'."
150 (let ((anything-candidate-number-limit (or limit anything-candidate-number-limit
))
151 (anything-idle-delay (or idle-delay anything-idle-delay
))
152 (anything-input-idle-delay (or input-idle-delay anything-input-idle-delay
))
153 (anything-complete-target target
)
154 (anything-execute-action-at-once-if-one t
)
155 (enable-recursive-minibuffers t
)
157 (anything-noresume sources target nil nil nil
"*anything complete*")))
159 ;; (@* "`lisp-complete-symbol' and `apropos' replacement")
160 (defvar anything-lisp-complete-symbol-input-idle-delay
0.1
161 "`anything-input-idle-delay' for `anything-lisp-complete-symbol',
162 `anything-lisp-complete-symbol-partial-match' and `anything-apropos'.")
163 (defvar anything-lisp-complete-symbol-add-space-on-startup t
164 "If non-nil, `anything-lisp-complete-symbol' and `anything-lisp-complete-symbol-partial-match' adds space on startup.
165 It utilizes anything-match-plugin's feature.")
167 (defun alcs-create-buffer (name)
168 (let ((b (get-buffer-create name
)))
169 (with-current-buffer b
170 (buffer-disable-undo)
174 (defvar alcs-variables-buffer
" *variable symbols*")
175 (defvar alcs-functions-buffer
" *function symbols*")
176 (defvar alcs-commands-buffer
" *command symbols*")
177 (defvar alcs-faces-buffer
" *face symbols*")
178 (defvar alcs-symbol-buffer
" *other symbols*")
180 (defvar alcs-symbols-time nil
181 "Timestamp of collected symbols")
183 (defun alcs-make-candidates-internal (bufname predicate
)
185 (let ((inhibit-read-only t
))
186 (setq alcs-symbols-time
(current-time))
187 (set-buffer (alcs-create-buffer bufname
))
188 (insert (mapconcat 'identity
(all-completions "" obarray predicate
) "\n")))))
189 (defun alcs-make-candidates--commands ()
190 (alcs-make-candidates-internal alcs-commands-buffer
'commandp
))
191 (defun alcs-make-candidates--functions ()
192 (alcs-make-candidates-internal alcs-functions-buffer
'fboundp
))
193 (defun alcs-make-candidates--variables ()
194 (alcs-make-candidates-internal alcs-variables-buffer
'boundp
))
195 (defun alcs-make-candidates--faces ()
196 (alcs-make-candidates-internal alcs-faces-buffer
'facep
))
197 (defun alcs-make-candidates--symbol ()
198 (alcs-make-candidates-internal alcs-symbol-buffer nil
))
200 (defun alcs-make-candidates ()
201 (message "Collecting symbols...")
202 ;; To ignore read-only property.
203 (alcs-make-candidates--commands)
204 (alcs-make-candidates--functions)
205 (alcs-make-candidates--variables)
206 (alcs-make-candidates--faces)
207 (alcs-make-candidates--symbol)
208 (message "Collecting symbols...done"))
210 (defun alcs-header-name (name)
211 (format "%s at %s (Press `C-c C-u' to update)"
212 name
(format-time-string "%H:%M:%S" alcs-symbols-time
)))
214 (defvar alcs-make-candidates-timer nil
)
215 (defun anything-lisp-complete-symbol-set-timer (update-period)
216 "Update Emacs symbols list when Emacs is idle,
217 used by `anything-lisp-complete-symbol-set-timer' and `anything-apropos'"
218 (when alcs-make-candidates-timer
219 (cancel-timer alcs-make-candidates-timer
))
220 (setq alcs-make-candidates-timer
221 (run-with-idle-timer update-period update-period
'alcs-make-candidates
)))
223 (defvar alcs-physical-column-at-startup nil
)
224 (defun alcs-init (bufname)
225 (declare (special anything-dabbrev-last-target
))
226 (setq alcs-physical-column-at-startup nil
)
227 (setq anything-complete-target
228 (if (loop for src in
(anything-get-sources)
229 thereis
(string-match "^dabbrev" (assoc-default 'name src
)))
230 anything-dabbrev-last-target
231 (or (tap-symbol) "")))
232 (anything-candidate-buffer (get-buffer bufname
)))
234 (defcustom anything-complete-sort-candidates nil
235 "*Whether to sort completion candidates."
237 :group
'anything-complete
)
239 (defcustom anything-execute-extended-command-use-kyr t
240 "*Use `anything-kyr' (context-aware commands) in `anything-execute-extended-command'. "
242 :group
'anything-complete
)
243 (defun alcs-sort-maybe (candidates source
)
244 (if anything-complete-sort-candidates
245 (sort candidates
#'string
<)
247 (defun alcs-fontify-face (candidates source
)
250 (propertize facename
'face
(intern-soft facename
)))
252 ;;; borrowed from pulldown.el
253 (defun alcs-current-physical-column ()
254 "Current physical column. (not logical column)"
255 ;; (- (point) (save-excursion (vertical-motion 0) (point)))
256 (car (posn-col-row (posn-at-point))))
258 (defun alcs-transformer-prepend-spacer (candidates source
)
259 "Prepend spaces according to `current-column' for each CANDIDATES."
260 (setq alcs-physical-column-at-startup
261 (or alcs-physical-column-at-startup
262 (with-current-buffer anything-current-buffer
264 (backward-char (string-width anything-complete-target
))
266 (- (alcs-current-physical-column)
267 (if (buffer-local-value 'anything-enable-shortcuts
(get-buffer anything-buffer
))
268 4 ;length of shortcut overlay
270 (mapcar (lambda (cand) (cons (concat (make-string alcs-physical-column-at-startup ?
) cand
) cand
))
273 (defun alcs-transformer-prepend-spacer-maybe (candidates source
)
274 ;; `anything-show-completion-activate' is defined in anything-show-completion.el
275 (if (and (boundp 'anything-show-completion-activate
)
276 anything-show-completion-activate
)
277 (alcs-transformer-prepend-spacer candidates source
)
280 (defun alcs-describe-function (name)
281 (describe-function (anything-c-symbolify name
)))
282 (defun alcs-describe-variable (name)
283 (with-current-buffer anything-current-buffer
284 (describe-variable (anything-c-symbolify name
))))
285 (defun alcs-describe-face (name)
286 (describe-face (anything-c-symbolify name
)))
287 (defun alcs-customize-face (name)
288 (customize-face (anything-c-symbolify name
)))
289 (defun alcs-find-function (name)
290 (find-function (anything-c-symbolify name
)))
291 (defun alcs-find-variable (name)
292 (find-variable (anything-c-symbolify name
)))
294 (defvar anything-c-source-complete-emacs-functions
295 '((name .
"Functions")
296 (init .
(lambda () (alcs-init alcs-functions-buffer
)))
297 (candidates-in-buffer)
298 (type . complete-function
)))
299 (defvar anything-c-source-complete-emacs-commands
300 '((name .
"Commands")
301 (init .
(lambda () (alcs-init alcs-commands-buffer
)))
302 (candidates-in-buffer)
303 (type . complete-function
)))
304 (defvar anything-c-source-complete-emacs-variables
305 '((name .
"Variables")
306 (init .
(lambda () (alcs-init alcs-variables-buffer
)))
307 (candidates-in-buffer)
308 (type . complete-variable
)))
309 (defvar anything-c-source-complete-emacs-faces
311 (init .
(lambda () (alcs-init alcs-faces-buffer
)))
312 (candidates-in-buffer)
313 (type . complete-face
)))
314 (defvar anything-c-source-complete-emacs-other-symbols
315 '((name .
"Other Symbols")
316 (init .
(lambda () (alcs-init alcs-symbol-buffer
)))
317 (candidates-in-buffer)
318 (filtered-candidate-transformer . alcs-sort-maybe
)
319 (action . ac-insert
)))
320 (defvar anything-c-source-apropos-emacs-functions
321 '((name .
"Apropos Functions")
322 (init .
(lambda () (alcs-init alcs-functions-buffer
)))
323 (candidates-in-buffer)
324 (requires-pattern .
3)
325 (type . apropos-function
)))
326 (defvar anything-c-source-apropos-emacs-commands
327 '((name .
"Apropos Commands")
328 (init .
(lambda () (alcs-init alcs-commands-buffer
)))
329 (candidates-in-buffer)
330 (requires-pattern .
3)
331 (type . apropos-function
)))
332 (defvar anything-c-source-apropos-emacs-variables
333 '((name .
"Apropos Variables")
334 (init .
(lambda () (alcs-init alcs-variables-buffer
)))
335 (candidates-in-buffer)
336 (requires-pattern .
3)
337 (type . apropos-variable
)))
338 (defvar anything-c-source-apropos-emacs-faces
339 '((name .
"Apropos Faces")
340 (init .
(lambda () (alcs-init alcs-faces-buffer
)))
341 (candidates-in-buffer)
342 (requires-pattern .
3)
343 (type . apropos-face
)))
344 (defvar anything-c-source-emacs-function-at-point
345 '((name .
"Function at point")
347 .
(lambda () (with-current-buffer anything-current-buffer
348 (anything-aif (function-called-at-point)
349 (list (symbol-name it
))))))
350 (type . apropos-function
)))
351 (defvar anything-c-source-emacs-variable-at-point
352 '((name .
"Variable at point")
354 .
(lambda () (with-current-buffer anything-current-buffer
355 (anything-aif (variable-at-point)
356 (unless (equal 0 it
) (list (symbol-name it
)))))))
357 (type . apropos-variable
)))
358 (defvar anything-c-source-emacs-face-at-point
359 '((name .
"Face at point")
361 .
(lambda () (with-current-buffer anything-current-buffer
362 (anything-aif (face-at-point)
363 (unless (equal 0 it
) (list (symbol-name it
)))))))
364 (type . apropos-variable
)))
366 (defvar anything-lisp-complete-symbol-sources
367 '(anything-c-source-complete-anything-attributes
368 anything-c-source-complete-emacs-commands
369 anything-c-source-complete-emacs-functions
370 anything-c-source-complete-emacs-variables
371 anything-c-source-complete-emacs-faces
))
373 (defvar anything-apropos-sources
374 '(anything-c-source-emacs-function-at-point
375 anything-c-source-emacs-variable-at-point
376 anything-c-source-apropos-emacs-commands
377 anything-c-source-apropos-emacs-functions
378 anything-c-source-apropos-emacs-variables
379 anything-c-source-apropos-emacs-faces
))
381 (define-anything-type-attribute 'apropos-function
382 '((filtered-candidate-transformer . alcs-sort-maybe
)
383 (header-name . alcs-header-name
)
384 (persistent-action . alcs-describe-function
)
385 (update . alcs-make-candidates--functions
)
387 ("Describe Function" . alcs-describe-function
)
388 ("Find Function" . alcs-find-function
))))
389 (define-anything-type-attribute 'apropos-variable
390 '((filtered-candidate-transformer . alcs-sort-maybe
)
391 (header-name . alcs-header-name
)
392 (persistent-action . alcs-describe-variable
)
393 (update . alcs-make-candidates--variables
)
395 ("Describe Variable" . alcs-describe-variable
)
396 ("Find Variable" . alcs-find-variable
))))
397 (define-anything-type-attribute 'apropos-face
398 '((filtered-candidate-transformer alcs-sort-maybe alcs-fontify-face
)
399 (get-line . buffer-substring
)
400 (header-name . alcs-header-name
)
401 (update . alcs-make-candidates--faces
)
402 (persistent-action . alcs-describe-face
)
404 ("Customize Face" . alcs-customize-face
)
405 ("Describe Face" . alcs-describe-face
))))
406 (define-anything-type-attribute 'complete-function
407 '((filtered-candidate-transformer alcs-sort-maybe alcs-transformer-prepend-spacer-maybe
)
408 (header-name . alcs-header-name
)
410 (update . alcs-make-candidates--functions
)
411 (persistent-action . alcs-describe-function
)))
412 (define-anything-type-attribute 'complete-variable
413 '((filtered-candidate-transformer alcs-sort-maybe alcs-transformer-prepend-spacer-maybe
)
414 (header-name . alcs-header-name
)
416 (update . alcs-make-candidates--variables
)
417 (persistent-action . alcs-describe-variable
)))
418 (define-anything-type-attribute 'complete-face
419 '((filtered-candidate-transformer alcs-sort-maybe alcs-transformer-prepend-spacer-maybe
)
420 (header-name . alcs-header-name
)
422 (update . alcs-make-candidates--faces
)
423 (persistent-action . alcs-describe-face
)))
425 (defvar alcs-this-command nil
)
426 (defun* anything-lisp-complete-symbol-1
(update sources input
&optional
(buffer "*anything complete*"))
427 (setq alcs-this-command this-command
)
428 (when (or update
(null (get-buffer alcs-variables-buffer
)))
429 (alcs-make-candidates))
430 (let (anything-samewindow
431 (anything-input-idle-delay
432 (or anything-lisp-complete-symbol-input-idle-delay
433 anything-input-idle-delay
)))
435 (if (equal buffer
"*anything complete*") 'anything-noresume
'anything
)
436 sources input nil nil nil buffer
)))
438 ;; Test alcs-update-restart (with-current-buffer alcs-commands-buffer (erase-buffer))
439 ;; Test alcs-update-restart (kill-buffer alcs-commands-buffer)
440 (defun alcs-update-restart ()
441 "Update lisp symbols and restart current `anything' session."
443 (alcs-make-candidates)
447 "Get symbol name before point."
450 ;; older regexp "\(\\|\\s-\\|^\\|\\_<\\|\r\\|'\\|#'"
451 (when (re-search-backward "\\_<" (point-at-bol) t
)
452 (buffer-substring-no-properties beg
(match-end 0))))))
454 (defun alcs-initial-input (partial-match)
455 (anything-aif (tap-symbol)
457 (if partial-match
"" "^")
459 (if anything-lisp-complete-symbol-add-space-on-startup
" " ""))
462 (defun anything-lisp-complete-symbol (update)
463 "`lisp-complete-symbol' replacement using `anything'."
465 (anything-lisp-complete-symbol-1 update anything-lisp-complete-symbol-sources
466 (alcs-initial-input nil
)))
467 (defun anything-lisp-complete-symbol-partial-match (&optional update
)
468 "`lisp-complete-symbol' replacement using `anything' (partial match)."
470 (anything-lisp-complete-symbol-1 update anything-lisp-complete-symbol-sources
471 (alcs-initial-input t
)))
472 (defun anything-apropos (update)
473 "`apropos' replacement using `anything'."
475 (anything-lisp-complete-symbol-1 update anything-apropos-sources nil
"*anything apropos*"))
477 ;; (@* "anything attribute completion")
478 (defvar anything-c-source-complete-anything-attributes
479 '((name .
"Anything Attributes")
480 (candidates . acaa-candidates
)
482 (persistent-action . acaa-describe-anything-attribute
)
483 (filtered-candidate-transformer alcs-sort-maybe alcs-transformer-prepend-spacer-maybe
)
484 (header-name . alcs-header-name
)
485 (action . ac-insert
)))
486 ;; (anything 'anything-c-source-complete-anything-attributes)
488 (defun acaa-describe-anything-attribute (str)
489 (anything-describe-anything-attribute (anything-c-symbolify str
)))
491 (defun acaa-candidates ()
492 (with-current-buffer anything-current-buffer
493 (when (and (require 'yasnippet nil t
)
494 (acaa-completing-attribute-p (point)))
495 (mapcar 'symbol-name anything-additional-attributes
))))
497 (defvar acaa-anything-commands-regexp
498 (concat "(" (regexp-opt
499 '("anything" "anything-other-buffer"
500 "define-anything-type-attribute" "anything-c-arrange-type-attribute"))
503 (defun acaa-completing-attribute-p (point)
509 (looking-at (concat "(defvar anything-c-source-"
511 acaa-anything-commands-regexp
)))
514 (looking-at acaa-anything-commands-regexp
))))))
518 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el")
519 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el")
521 (when (fboundp 'expectations
)
523 (desc "acaa-completing-attribute-p")
526 (insert "(anything '(((na")
527 (acaa-completing-attribute-p (point))))
530 (insert "(anything '((na")
531 (acaa-completing-attribute-p (point))))
534 (insert "(anything-hoge '((na")
535 (acaa-completing-attribute-p (point))))
538 (insert "(anything-hoge '(((na")
539 (acaa-completing-attribute-p (point))))
542 (insert "(defvar anything-c-source-hoge '((na")
543 (acaa-completing-attribute-p (point))))
547 ;; (@* "anything-read-string-mode / read-* compatibility functions")
548 ;; moved from anything.el
549 (defun anything-compile-source--default-value (source)
550 (anything-aif (assoc-default 'default-value source
)
553 (filtered-candidate-transformer
554 .
(lambda (cands source
)
555 (if (string= anything-pattern
"") cands nil
)))))
557 (add-to-list 'anything-compile-source-functions
'anything-compile-source--default-value
)
559 ;; (@* "`anything-read-string-mode' initialization")
560 (defvar anything-read-string-mode nil
)
561 (defvar anything-read-string-mode-flags
'(string buffer variable command
)
562 "Saved ARG of `anything-read-string-mode'.")
563 (defun anything-read-string-mode (arg)
564 "If this minor mode is on, use `anything' version of `completing-read' and `read-file-name'.
566 ARG also accepts a symbol list. The elements are:
567 string: replace `completing-read' except `read-file-name'
569 file: replace `read-file-name' (disabled by default)
571 So, (anything-read-string-mode 1) and
572 (anything-read-string-mode '(string buffer variable command) are identical.
574 It is deprecated now, fall back to `anything-completion-mode'
575 because it is better implementation."
577 (when (consp anything-read-string-mode
)
578 (anything-read-string-mode-uninstall))
579 (setq anything-read-string-mode
580 (cond ((consp arg
) (setq anything-read-string-mode-flags arg
)) ; not interactive
581 (arg (> (prefix-numeric-value arg
) 0)) ; C-u M-x
582 (t (not anything-read-string-mode
)))) ; M-x
583 (when (eq anything-read-string-mode t
)
584 (setq anything-read-string-mode anything-read-string-mode-flags
))
585 (if anything-read-string-mode
586 (anything-read-string-mode-install)
587 (anything-read-string-mode-uninstall)))
589 (defun anything-read-string-mode-install ()
590 ;; redefine to anything version
591 (setq anything-completion-types
592 (if (memq 'file anything-read-string-mode
)
593 '(complete file
) '(complete)))
594 (anything-completion-mode 1)
595 (when (memq 'command anything-read-string-mode
)
596 (substitute-key-definition 'execute-extended-command
'anything-execute-extended-command global-map
))
597 (message "Installed anything version of read functions."))
598 (defun anything-read-string-mode-uninstall ()
599 ;; restore to original version
600 (anything-completion-mode -
1)
601 (substitute-key-definition 'anything-execute-extended-command
'execute-extended-command global-map
)
602 (substitute-key-definition 'anything-find-file
'find-file global-map
)
603 (message "Uninstalled anything version of read functions."))
606 ;; (@* " shell history")
607 (defun anything-complete-shell-history ()
608 "Select a command from shell history and insert it."
610 (let ((anything-show-completion-minimum-window-height (/ (frame-height) 2)))
611 (anything-complete 'anything-c-source-complete-shell-history
612 (or (word-at-point) "")
614 (defun anything-complete-shell-history-setup-key (key)
616 (when (and (not (boundp 'minibuffer-local-shell-command-map
))
617 (require 'shell-command nil t
)
618 (boundp 'shell-command-minibuffer-map
))
619 (shell-command-completion-mode)
620 (define-key shell-command-minibuffer-map key
'anything-complete-shell-history
))
622 (when (boundp 'minibuffer-local-shell-command-map
)
623 (define-key minibuffer-local-shell-command-map key
'anything-complete-shell-history
))
625 (when (require 'background nil t
)
626 (define-key background-minibuffer-map key
'anything-complete-shell-history
))
628 (define-key shell-mode-map key
'anything-complete-shell-history
))
631 (defvar anything-c-source-complete-shell-history
632 '((name .
"Shell History")
634 (require 'shell-history
)
635 (with-current-buffer (anything-candidate-buffer (shell-history-buffer))
637 (set (make-local-variable 'zsh-p
)
638 (shell-history-zsh-extended-history-p)))))
639 (get-line . acsh-get-line
)
643 (defun acsh-get-line (s e
)
644 (let ((extended-history (string= (buffer-substring s
(+ s
2)) ": "))
645 (single-line (not (string= (buffer-substring (1- e
) e
) "\\"))))
647 (buffer-substring s e
))
648 ((and extended-history single-line
)
649 (buffer-substring (+ s
15) e
))
650 (extended-history ;zsh multi-line / 1st line
652 (let ((e2 (1- (if (re-search-forward "^: [0-9]+:[0-9];" nil t
)
655 (prog1 (replace-regexp-in-string
656 "\\\\\n" ";" (buffer-substring (+ s
15) e2
))
658 (t ; zsh multi-line history / not 1st line
660 (re-search-backward "^: [0-9]+:[0-9];" nil t
)
661 (let ((s2 (match-end 0)) e2
)
663 (setq e2
(1- (if (re-search-forward "^: [0-9]+:[0-9];" nil t
)
666 (prog1 (replace-regexp-in-string
667 "\\\\\n" ";" (buffer-substring s2 e2
))
671 (defvar anything-execute-extended-command-map
672 (let ((map (make-sparse-keymap)))
673 (set-keymap-parent map anything-map
)
674 (define-key map
(kbd "M-x") 'anything-next-line
)
677 (define-anything-type-attribute 'execute-command
678 `((update . alcs-make-candidates--commands
)
679 (keymap .
,anything-execute-extended-command-map
)
680 (persistent-action . alcs-describe-function
)
681 (action ("Execute" . anything-execute-extended-command-execute
)
682 ("Describe Function" . alcs-describe-function
)
683 ("Find Function" . alcs-find-function
))))
685 ;; I do not want to make anything-c-source-* symbols because they are
686 ;; private in `anything-execute-extended-command'.
687 (defvar anything-execute-extended-command-sources
688 '(((name .
"Emacs Commands History")
689 (candidates . extended-command-history
)
690 (type . execute-command
))
692 (header-name . alcs-header-name
)
693 (init .
(lambda () (anything-candidate-buffer
694 (get-buffer-create alcs-commands-buffer
))))
695 (candidates-in-buffer)
696 (type . execute-command
))
697 ((name .
"New Command")
699 (type . execute-command
))))
701 ;; (with-current-buffer " *command symbols*" (erase-buffer))
702 (defvar anything-execute-extended-command-prefix-arg nil
)
704 (defun anything-execute-extended-command-execute (cmdname)
705 (let ((sym-com (and (stringp cmdname
) (intern-soft cmdname
))))
706 (unless (and sym-com
(commandp sym-com
))
707 (error "No such command: %s" cmdname
))
708 (setq this-command sym-com
709 real-this-command sym-com
)
710 (let ((prefix-arg anything-execute-extended-command-prefix-arg
))
711 (setq extended-command-history
713 (delete cmdname extended-command-history
)))
714 (command-execute sym-com
'record
))))
716 (defun anything-execute-extended-command (arg)
717 "Replacement of `execute-extended-command'."
719 (setq anything-execute-extended-command-prefix-arg arg
)
720 (setq alcs-this-command this-command
)
722 (if (and anything-execute-extended-command-use-kyr
723 (require 'anything-kyr-config nil t
))
724 (cons anything-c-source-kyr
725 anything-execute-extended-command-sources
)
726 anything-execute-extended-command-sources
)))
728 (add-hook 'after-init-hook
'alcs-make-candidates
)
732 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el")
733 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el")
735 (when (fboundp 'expectations
)
737 (desc "acsh-get-line command")
741 (insert ": 1118554690:0;cat ~/.zsh_history\n"
742 ": 1118554690:0;ls\n")
744 (acsh-get-line (point-at-bol) (point-at-eol)))))
748 (insert ": 1118554690:0;cat ~/.zsh_history\n"
749 ": 1118554690:0;cd\\\n"
751 ": 1118554690:0;hoge\n")
753 (acsh-get-line (point-at-bol) (point-at-eol)))))
757 (insert ": 1118554690:0;cat ~/.zsh_history\n"
758 ": 1118554690:0;cd\\\n"
760 ": 1118554690:0;hoge\n")
762 (acsh-get-line (point-at-bol) (point-at-eol)))))
766 (insert ": 1118554690:0;cat ~/.zsh_history\n"
767 ": 1118554690:0;cd\\\n"
770 (acsh-get-line (point-at-bol) (point-at-eol)))))
774 (insert ": 1118554690:0;cat ~/.zsh_history\n"
775 ": 1118554690:0;cd\\\n"
778 (acsh-get-line (point-at-bol) (point-at-eol)))))
785 (acsh-get-line (point-at-bol) (point-at-eol)))))
786 (desc "acsh-get-line lineno")
790 (insert ": 1118554690:0;cat ~/.zsh_history\n"
791 ": 1118554690:0;cd\\\n"
793 ": 1118554690:0;hoge\n")
795 (acsh-get-line (point-at-bol) (point-at-eol))
796 (line-number-at-pos))))
800 (insert ": 1118554690:0;cat ~/.zsh_history\n"
801 ": 1118554690:0;cd\\\n"
803 ": 1118554690:0;hoge\n")
805 (acsh-get-line (point-at-bol) (point-at-eol))
806 (line-number-at-pos))))
810 ;;; for compatibility
811 (defvaralias 'anything-c-source-complete-emacs-variables-partial-match
812 'anything-c-source-complete-emacs-variables
)
813 (defvaralias 'anything-c-source-complete-emacs-commands-partial-match
814 'anything-c-source-complete-emacs-commands
)
815 (defvaralias 'anything-c-source-complete-emacs-functions-partial-match
816 'anything-c-source-complete-emacs-functions
)
820 (provide 'anything-complete
)
822 ;; How to save (DO NOT REMOVE!!)
823 ;; (progn (magit-push) (emacswiki-post "anything-complete.el"))
824 ;;; anything-complete.el ends here