anything-config.el: anything-c-source-call-source: Add action: "Copy variable name"
[anything-config.git] / contrib / anything-multi-sources.el
blob81ff5a1dd23a3aafe91dab657792aed11d634df5
1 ;;;; anything-multi-sources.el --- concatenate candidates in multi souces
3 ;; Copyright (C) 2010 rubikitch
5 ;; Author: rubikitch <rubikitch@ruby-lang.org>
6 ;; Keywords: anything, experimental
7 ;; URL: http://www.emacswiki.org/cgi-bin/wiki/download/anything-multi-sources.el
9 ;; This file is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
14 ;; This file is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
24 ;;; Commentary:
26 ;;
28 ;;; Commands:
30 ;; Below are complete command list:
33 ;;; Customizable Options:
35 ;; Below are customizable option list:
38 ;;; Customize:
41 ;; All of the above can customize by:
42 ;; M-x customize-group RET anything-multi-sources RET
45 ;;; Code:
47 (eval-when-compile (require 'cl))
48 (defgroup anything-multi-sources nil
49 "anything-multi-sources"
50 :group 'emacs)
52 (defvar anything-multi-source-delimiter-candidates
53 '("anything-multi-source-delimiter-candidates--3e3r3x903oj09fnreiojioxe"))
54 (defvar anything-multi-source-delimiter-name
55 "anything-multi-source-delimiter-name--339024u3fz09e90390ir309330923")
56 (defvar anything-multi-source-delimiter
57 `((name . ,anything-multi-source-delimiter-name)
58 (init . anything-multi-source-delimiter-init)
59 (match identity)
60 (disable-shortcuts)
61 (volatile)
62 (candidates . anything-multi-source-delimiter-candidates)))
64 (defun anything-multi-source-delimiter-init ()
65 (setq anything-source-in-each-line-flag t))
67 (defun ams-delete-delimiter-source ()
68 (delete-region (point) (progn (forward-line 3) (point))))
69 (defun anything-multi-sources-update-function ()
70 (setq cursor-type t)
71 (let ((pos 1)
72 (push-line
73 (lambda ()
74 (push (buffer-substring-no-properties (point-at-bol) (point-at-eol))
75 concat-headers)))
76 (first-source
77 (lambda ()
78 (some (lambda (source)
79 (if (equal (assoc-default 'name source)
80 (buffer-substring (point-at-bol) (point-at-eol)))
81 source))
82 (anything-get-sources))))
83 (rewrite-first-source-name
84 (lambda ()
85 (overlay-put (make-overlay (line-beginning-position)
86 (line-end-position))
87 'display (mapconcat 'identity
88 ;;omit separator
89 (reverse (cdr concat-headers))
90 " + "))))
91 in-multi-source delimiter-flag concat-header-pos concat-headers
92 multi-source-start multi-source-end)
93 (while (or (eq pos 1)
94 (progn (forward-line 1)
95 (setq pos (anything-get-next-header-pos))))
96 (goto-char pos)
97 (setq pos 0) ;reset
98 (funcall push-line)
99 (setq delimiter-flag
100 (equal (car concat-headers) anything-multi-source-delimiter-name))
101 (when (and (not delimiter-flag) ;delete header
102 in-multi-source)
103 (delete-region (1- (point)) (progn (forward-line 1) (point))))
104 (when delimiter-flag
105 (cond ((not in-multi-source)
106 (setq concat-header-pos (point))
107 (setq concat-headers nil)
108 (save-excursion
109 (forward-line 1) ; anything-get-next-header-pos workaround
110 (ignore-errors
111 (goto-char (anything-get-next-header-pos))
112 (funcall push-line)))
113 (forward-line 0)
114 (ams-delete-delimiter-source)
115 (if (equal (buffer-substring-no-properties (point-at-bol) (point-at-eol))
116 anything-multi-source-delimiter-name)
117 (ams-delete-delimiter-source)
118 (save-excursion (forward-line 1) (setq multi-source-start (point)))
119 (setq in-multi-source (not in-multi-source))))
121 (save-excursion
122 (forward-line -1) (setq multi-source-end (point))
123 (goto-char (1- multi-source-start))
124 (ams-sort-candidates
125 multi-source-start multi-source-end (funcall first-source)))
126 (ams-delete-delimiter-source)
127 (save-excursion
128 (goto-char concat-header-pos)
129 (funcall rewrite-first-source-name))
130 (setq in-multi-source (not in-multi-source))))))))
132 (defun ams-sort-candidates (s e source)
133 (sort-numeric-fields 1 s e))
135 (add-hook 'anything-update-hook 'anything-multi-sources-update-function)
136 (provide 'anything-multi-sources)
138 ;; How to save (DO NOT REMOVE!!)
139 ;; (emacswiki-post "anything-multi-sources.el")
140 ;;; anything-multi-sources.el ends here