Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 569 lines (508 sloc) 23.382 kb
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
1 ;;; helm-buffers.el --- helm support for buffers.
2
3 ;; Copyright (C) 2012 Thierry Volpiatto <thierry.volpiatto@gmail.com>
4
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation, either version 3 of the License, or
8 ;; (at your option) any later version.
9
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
14
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 ;;; Code:
19
3d768cc @thierryvolpiatto * helm-config.el: Move regexp and occur code to helm-regexp.el.
thierryvolpiatto authored
20 (require 'cl)
21 (require 'helm)
66550b2 @thierryvolpiatto * helm-buffers.el: require cl, helm, helm-vars, helm-utils.
thierryvolpiatto authored
22 (require 'helm-utils)
7974fc5 @thierryvolpiatto Create autoload system in helm-config.
thierryvolpiatto authored
23 (require 'helm-elscreen)
c4ea874 @thierryvolpiatto * helm-files.el (helm-ff-directory-files): Always reread an ftp direc…
thierryvolpiatto authored
24 (require 'helm-grep)
7288a9a @thierryvolpiatto Introduce multi occur.
thierryvolpiatto authored
25 (require 'helm-regexp)
3d768cc @thierryvolpiatto * helm-config.el: Move regexp and occur code to helm-regexp.el.
thierryvolpiatto authored
26
7dac376 @thierryvolpiatto * helm-buffers.el: Use ido to fetch buffers-list as suggested by Dan …
thierryvolpiatto authored
27 (declare-function ido-make-buffer-list "ido" (default))
28
ba17f09 @thierryvolpiatto Move all defcustoms and faces in their respectives groups and files.
thierryvolpiatto authored
29 (defgroup helm-buffers nil
30 "Buffers related Applications and libraries for Helm."
31 :group 'helm)
32
80c34cb @thierryvolpiatto * helm-buffers.el (helm-c-boring-buffer-regexp-list): New, replacemen…
thierryvolpiatto authored
33 (defcustom helm-c-boring-buffer-regexp-list
34 '("\\` " "\\*helm" "\\*helm-mode" "\\*Echo Area" "\\*Minibuf")
35 "The regexp list that match boring buffers.
36 Buffer candidates matching these regular expression will be
37 filtered from the list of candidates if the
38 `helm-c-skip-boring-buffers' candidate transformer is used."
3aeb618 @thierryvolpiatto Fix defcustoms.
thierryvolpiatto authored
39 :type '(repeat (choice regexp))
80c34cb @thierryvolpiatto * helm-buffers.el (helm-c-boring-buffer-regexp-list): New, replacemen…
thierryvolpiatto authored
40 :group 'helm-buffers)
41
504588a @thierryvolpiatto * helm-buffers.el: let user choose a mode for new buffers.
thierryvolpiatto authored
42 (defcustom helm-buffers-favorite-modes '(lisp-interaction-mode
43 emacs-lisp-mode
44 text-mode
45 org-mode)
8d082d5 correct type prefered => preferred
Le Wang authored
46 "List of preferred mode to open new buffers with."
3aeb618 @thierryvolpiatto Fix defcustoms.
thierryvolpiatto authored
47 :type '(repeat (choice function))
504588a @thierryvolpiatto * helm-buffers.el: let user choose a mode for new buffers.
thierryvolpiatto authored
48 :group 'helm-buffers)
49
7577432 @thierryvolpiatto * helm-buffers.el (helm-buffer-max-length): new user var.
thierryvolpiatto authored
50 (defcustom helm-buffer-max-length 20
51 "Max length of buffer names before truncate."
52 :group 'helm-buffers
53 :type 'integer)
ba17f09 @thierryvolpiatto Move all defcustoms and faces in their respectives groups and files.
thierryvolpiatto authored
54
55 ;;; Faces
56 ;;
57 ;;
58 (defface helm-buffer-saved-out
5f04e40 color adjustments for helm-buffer faces
Le Wang authored
59 '((t (:foreground "red" :background "black")))
09bc1b0 @thierryvolpiatto * helm-buffers.el: Show size of buffers.
thierryvolpiatto authored
60 "Face used for buffer files modified outside of emacs."
ba17f09 @thierryvolpiatto Move all defcustoms and faces in their respectives groups and files.
thierryvolpiatto authored
61 :group 'helm-buffers)
62
63 (defface helm-buffer-not-saved
64 '((t (:foreground "Indianred2")))
09bc1b0 @thierryvolpiatto * helm-buffers.el: Show size of buffers.
thierryvolpiatto authored
65 "Face used for buffer files not already saved on disk."
66 :group 'helm-buffers)
67
68 (defface helm-buffer-size
5f04e40 color adjustments for helm-buffer faces
Le Wang authored
69 '((((background dark)) :foreground "RosyBrown")
70 (((background light)) :foreground "SlateGray"))
09bc1b0 @thierryvolpiatto * helm-buffers.el: Show size of buffers.
thierryvolpiatto authored
71 "Face used for buffer size."
ba17f09 @thierryvolpiatto Move all defcustoms and faces in their respectives groups and files.
thierryvolpiatto authored
72 :group 'helm-buffers)
73
74
75 ;;; Buffers keymap
76 ;;
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
77 (defvar helm-c-buffer-map
78 (let ((map (make-sparse-keymap)))
79 (set-keymap-parent map helm-map)
80 (define-key map (kbd "C-c ?") 'helm-c-buffer-help)
81 ;; No need to have separate command for grep and zgrep
82 ;; as we don't use recursivity for buffers.
83 ;; So use zgrep for both as it is capable to handle non--compressed files.
84 (define-key map (kbd "M-g s") 'helm-buffer-run-zgrep)
750af58 @thierryvolpiatto * helm-buffers.el (helm-c-buffer-map): Bind C-s to multi occur.
thierryvolpiatto authored
85 (define-key map (kbd "C-s") 'helm-buffers-run-multi-occur)
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
86 (define-key map (kbd "C-c o") 'helm-buffer-switch-other-window)
87 (define-key map (kbd "C-c C-o") 'helm-buffer-switch-other-frame)
88 (define-key map (kbd "C-c =") 'helm-buffer-run-ediff)
89 (define-key map (kbd "M-=") 'helm-buffer-run-ediff-merge)
90 (define-key map (kbd "C-=") 'helm-buffer-diff-persistent)
91 (define-key map (kbd "M-U") 'helm-buffer-revert-persistent)
92 (define-key map (kbd "M-D") 'helm-buffer-run-kill-buffers)
93 (define-key map (kbd "C-x C-s") 'helm-buffer-save-persistent)
94 (define-key map (kbd "C-M-%") 'helm-buffer-run-query-replace-regexp)
95 (define-key map (kbd "M-%") 'helm-buffer-run-query-replace)
96 (define-key map (kbd "M-m") 'helm-toggle-all-marks)
97 (define-key map (kbd "M-a") 'helm-mark-all)
98 (when (locate-library "elscreen")
99 (define-key map (kbd "<C-tab>") 'helm-buffer-switch-to-elscreen))
100 (delq nil map))
101 "Keymap for buffer sources in helm.")
102
d31c524 @thierryvolpiatto * helm-buffers.el: Add keymap and actions for ido virtual source.
thierryvolpiatto authored
103 (defvar helm-buffers-ido-virtual-map
104 (let ((map (make-sparse-keymap)))
105 (set-keymap-parent map helm-map)
d3e5331 @thierryvolpiatto * helm-buffers.el: Add help command.
thierryvolpiatto authored
106 (define-key map (kbd "C-c ?") 'helm-buffers-ido-virtual-help)
d31c524 @thierryvolpiatto * helm-buffers.el: Add keymap and actions for ido virtual source.
thierryvolpiatto authored
107 (define-key map (kbd "C-c o") 'helm-ff-run-switch-other-window)
108 (define-key map (kbd "C-c C-o") 'helm-ff-run-switch-other-frame)
109 (define-key map (kbd "M-g s") 'helm-ff-run-grep)
110 (define-key map (kbd "M-g z") 'helm-ff-run-zgrep)
111 (define-key map (kbd "M-D") 'helm-ff-run-delete-file)
112 (define-key map (kbd "C-c C-x") 'helm-ff-run-open-file-externally)
113 map))
26a9255 @thierryvolpiatto * helm-buffers.el: reorder and add new source for ido virtual buffers.
thierryvolpiatto authored
114
115 (defvar helm-buffers-list-cache nil)
116 (defvar helm-c-source-buffers-list
117 `((name . "Buffers")
118 (init . (lambda ()
09bc1b0 @thierryvolpiatto * helm-buffers.el: Show size of buffers.
thierryvolpiatto authored
119 ;; Issue #51 Create the list before `helm-buffer' creation.
26a9255 @thierryvolpiatto * helm-buffers.el: reorder and add new source for ido virtual buffers.
thierryvolpiatto authored
120 (setq helm-buffers-list-cache (helm-c-buffer-list))))
121 (candidates . helm-buffers-list-cache)
122 (type . buffer)
123 (match helm-c-buffer-match-major-mode)
124 (persistent-action . helm-c-buffers-list-persistent-action)
125 (keymap . ,helm-c-buffer-map)
126 (volatile)
127 (mode-line . helm-buffer-mode-line-string)
128 (persistent-help
129 . "Show this buffer / C-u \\[helm-execute-persistent-action]: Kill this buffer")))
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
130
131 (defvar helm-c-source-buffer-not-found
132 `((name . "Create buffer")
133 (dummy)
134 (filtered-candidate-transformer (lambda (cands source)
135 (list helm-pattern)))
136 (keymap . ,helm-map)
137 (action . (lambda (candidate)
42f60d2 @thierryvolpiatto * helm-buffers.el (helm-c-source-buffer-not-found): Don't create buff…
thierryvolpiatto authored
138 (let ((mjm (and helm-current-prefix-arg
139 (intern (helm-comp-read
140 "Major-mode: "
141 helm-buffers-favorite-modes))))
142 buffer)
143 (setq buffer (get-buffer-create candidate))
144 (if mjm
145 (with-current-buffer buffer (funcall mjm))
504588a @thierryvolpiatto * helm-buffers.el: let user choose a mode for new buffers.
thierryvolpiatto authored
146 (set-buffer-major-mode buffer))
49db052 @cofi helm-buffers.el: Set major mode on new created buffers.
cofi authored
147 (helm-c-switch-to-buffer buffer))))))
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
148
26a9255 @thierryvolpiatto * helm-buffers.el: reorder and add new source for ido virtual buffers.
thierryvolpiatto authored
149 (defvar helm-c-source-ido-virtual-buffers
d31c524 @thierryvolpiatto * helm-buffers.el: Add keymap and actions for ido virtual source.
thierryvolpiatto authored
150 `((name . "Ido virtual buffers")
26a9255 @thierryvolpiatto * helm-buffers.el: reorder and add new source for ido virtual buffers.
thierryvolpiatto authored
151 (candidates . (lambda ()
152 (let (ido-temp-list
153 ido-ignored-list
154 (ido-process-ignore-lists t))
155 (when ido-use-virtual-buffers
156 (ido-add-virtual-buffers-to-list)
157 ido-virtual-buffers))))
d31c524 @thierryvolpiatto * helm-buffers.el: Add keymap and actions for ido virtual source.
thierryvolpiatto authored
158 (keymap . ,helm-buffers-ido-virtual-map)
d3e5331 @thierryvolpiatto * helm-buffers.el: Add help command.
thierryvolpiatto authored
159 (mode-line . helm-buffers-ido-virtual-mode-line-string)
d31c524 @thierryvolpiatto * helm-buffers.el: Add keymap and actions for ido virtual source.
thierryvolpiatto authored
160 (action . (("Find file" . helm-find-many-files)
161 ("Find file other window" . find-file-other-window)
162 ("Find file other frame" . find-file-other-frame)
163 ("Find file as root" . helm-find-file-as-root)
164 ("Grep File(s) `C-u recurse'" . helm-find-files-grep)
165 ("Zgrep File(s) `C-u Recurse'" . helm-ff-zgrep)
166 ("View file" . view-file)
167 ("Delete file(s)" . helm-delete-marked-files)
168 ("Open file externally (C-u to choose)"
169 . helm-c-open-file-externally)))))
26a9255 @thierryvolpiatto * helm-buffers.el: reorder and add new source for ido virtual buffers.
thierryvolpiatto authored
170
171 (defun helm-c-buffer-list ()
172 "Return the current list of buffers.
173 Currently visible buffers are put at the end of the list.
174 See `ido-make-buffer-list' for more infos."
175 (require 'ido)
176 (let ((ido-process-ignore-lists t)
177 ido-ignored-list
178 ido-use-virtual-buffers)
179 (ido-make-buffer-list nil)))
180
09bc1b0 @thierryvolpiatto * helm-buffers.el: Show size of buffers.
thierryvolpiatto authored
181 (defun helm-buffer-size (buffer)
182 "Return size of BUFFER."
183 (with-current-buffer buffer
184 (save-restriction
185 (widen)
186 (helm-file-human-size
187 (- (position-bytes (point-max))
188 (position-bytes (point-min)))))))
189
190 (defun helm-c-highlight-buffers (buffers sources)
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
191 "Transformer function to highlight BUFFERS list.
192 Should be called after others transformers i.e (boring buffers)."
7577432 @thierryvolpiatto * helm-buffers.el (helm-buffer-max-length): new user var.
thierryvolpiatto authored
193 (loop ;; length of last buffer size string.
194 ;; Start at ten, such a length should never be reach.
195 ;; e.g 9999K, so the max should be 5 + a space = 6.
196 with old-len-size = 10
25102c1 @thierryvolpiatto * helm-buffers.el (helm-c-highlight-buffers): Align correctly, add mo…
thierryvolpiatto authored
197 for i in buffers
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
198 for buf = (get-buffer i)
09bc1b0 @thierryvolpiatto * helm-buffers.el: Show size of buffers.
thierryvolpiatto authored
199 for size = (propertize (helm-buffer-size buf)
200 'face 'helm-buffer-size)
25102c1 @thierryvolpiatto * helm-buffers.el (helm-c-highlight-buffers): Align correctly, add mo…
thierryvolpiatto authored
201 for len-size = (length size)
202 for str-before-size = (helm-aif (and (> old-len-size len-size)
203 (- old-len-size len-size))
204 (make-string it ? ) "")
205 do (setq old-len-size (+ len-size (length str-before-size)))
cef5713 @thierryvolpiatto * helm-buffers.el: Issue #82 Fix hardcoded value of helm-buffer-max-l…
thierryvolpiatto authored
206 for truncbuf = (if (> (length i) helm-buffer-max-length)
7577432 @thierryvolpiatto * helm-buffers.el (helm-buffer-max-length): new user var.
thierryvolpiatto authored
207 (concat (substring i 0 helm-buffer-max-length)
208 "...")
209 (concat i (make-string
210 (- (+ helm-buffer-max-length 3)
211 (length i)) ? )))
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
212 for bfname = (buffer-file-name buf)
25102c1 @thierryvolpiatto * helm-buffers.el (helm-c-highlight-buffers): Align correctly, add mo…
thierryvolpiatto authored
213 for mode = (with-current-buffer i (symbol-name major-mode))
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
214 collect
215 (cond (;; A dired buffer.
216 (rassoc buf dired-buffers)
09bc1b0 @thierryvolpiatto * helm-buffers.el: Show size of buffers.
thierryvolpiatto authored
217 (cons (concat (propertize
25102c1 @thierryvolpiatto * helm-buffers.el (helm-c-highlight-buffers): Align correctly, add mo…
thierryvolpiatto authored
218 truncbuf 'face 'helm-ff-directory
09bc1b0 @thierryvolpiatto * helm-buffers.el: Show size of buffers.
thierryvolpiatto authored
219 'help-echo (car (rassoc buf dired-buffers)))
25102c1 @thierryvolpiatto * helm-buffers.el (helm-c-highlight-buffers): Align correctly, add mo…
thierryvolpiatto authored
220 " " str-before-size size " " mode)
09bc1b0 @thierryvolpiatto * helm-buffers.el: Show size of buffers.
thierryvolpiatto authored
221 i))
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
222 ;; A buffer file modified somewhere outside of emacs.
223 ((and bfname (not (file-remote-p bfname))
224 (file-exists-p bfname)
225 (not (verify-visited-file-modtime buf)))
25102c1 @thierryvolpiatto * helm-buffers.el (helm-c-highlight-buffers): Align correctly, add mo…
thierryvolpiatto authored
226 (cons (concat (propertize truncbuf 'face 'helm-buffer-saved-out
09bc1b0 @thierryvolpiatto * helm-buffers.el: Show size of buffers.
thierryvolpiatto authored
227 'help-echo bfname)
25102c1 @thierryvolpiatto * helm-buffers.el (helm-c-highlight-buffers): Align correctly, add mo…
thierryvolpiatto authored
228 " " str-before-size size " " mode)
09bc1b0 @thierryvolpiatto * helm-buffers.el: Show size of buffers.
thierryvolpiatto authored
229 i))
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
230 ;; A new buffer file not already saved on disk.
231 ((and bfname (not (file-remote-p bfname))
232 (not (verify-visited-file-modtime buf)))
25102c1 @thierryvolpiatto * helm-buffers.el (helm-c-highlight-buffers): Align correctly, add mo…
thierryvolpiatto authored
233 (cons (concat (propertize truncbuf 'face 'helm-buffer-not-saved
09bc1b0 @thierryvolpiatto * helm-buffers.el: Show size of buffers.
thierryvolpiatto authored
234 'help-echo bfname)
25102c1 @thierryvolpiatto * helm-buffers.el (helm-c-highlight-buffers): Align correctly, add mo…
thierryvolpiatto authored
235 " " str-before-size size " " mode)
09bc1b0 @thierryvolpiatto * helm-buffers.el: Show size of buffers.
thierryvolpiatto authored
236 i))
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
237 ;; A Remote buffer file modified and not saved on disk.
238 ((and bfname (file-remote-p bfname) (buffer-modified-p buf))
239 (let ((prefix (propertize
240 " " 'display
241 (propertize "@ " 'face 'helm-ff-prefix))))
25102c1 @thierryvolpiatto * helm-buffers.el (helm-c-highlight-buffers): Align correctly, add mo…
thierryvolpiatto authored
242 (cons (concat prefix (propertize truncbuf 'face 'helm-ff-symlink
09bc1b0 @thierryvolpiatto * helm-buffers.el: Show size of buffers.
thierryvolpiatto authored
243 'help-echo bfname)
25102c1 @thierryvolpiatto * helm-buffers.el (helm-c-highlight-buffers): Align correctly, add mo…
thierryvolpiatto authored
244 " " str-before-size size " " mode)
09bc1b0 @thierryvolpiatto * helm-buffers.el: Show size of buffers.
thierryvolpiatto authored
245 i)))
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
246 ;; A buffer file modified and not saved on disk.
247 ((and bfname (buffer-modified-p buf))
25102c1 @thierryvolpiatto * helm-buffers.el (helm-c-highlight-buffers): Align correctly, add mo…
thierryvolpiatto authored
248 (cons (concat (propertize truncbuf 'face 'helm-ff-symlink
09bc1b0 @thierryvolpiatto * helm-buffers.el: Show size of buffers.
thierryvolpiatto authored
249 'help-echo bfname)
25102c1 @thierryvolpiatto * helm-buffers.el (helm-c-highlight-buffers): Align correctly, add mo…
thierryvolpiatto authored
250 " " str-before-size size " " mode)
09bc1b0 @thierryvolpiatto * helm-buffers.el: Show size of buffers.
thierryvolpiatto authored
251 i))
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
252 ;; A remote buffer file not modified and saved on disk.
253 ((and bfname (file-remote-p bfname))
254 (let ((prefix (propertize
255 " " 'display
256 (propertize "@ " 'face 'helm-ff-prefix))))
25102c1 @thierryvolpiatto * helm-buffers.el (helm-c-highlight-buffers): Align correctly, add mo…
thierryvolpiatto authored
257 (cons (concat prefix (propertize truncbuf 'face 'font-lock-type-face
09bc1b0 @thierryvolpiatto * helm-buffers.el: Show size of buffers.
thierryvolpiatto authored
258 'help-echo bfname)
25102c1 @thierryvolpiatto * helm-buffers.el (helm-c-highlight-buffers): Align correctly, add mo…
thierryvolpiatto authored
259 " " str-before-size size " " mode)
09bc1b0 @thierryvolpiatto * helm-buffers.el: Show size of buffers.
thierryvolpiatto authored
260 i)))
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
261 ;; A buffer file not modified and saved on disk.
262 (bfname
25102c1 @thierryvolpiatto * helm-buffers.el (helm-c-highlight-buffers): Align correctly, add mo…
thierryvolpiatto authored
263 (cons (concat (propertize truncbuf 'face 'font-lock-type-face
09bc1b0 @thierryvolpiatto * helm-buffers.el: Show size of buffers.
thierryvolpiatto authored
264 'help-echo bfname)
25102c1 @thierryvolpiatto * helm-buffers.el (helm-c-highlight-buffers): Align correctly, add mo…
thierryvolpiatto authored
265 " " str-before-size size " " mode)
09bc1b0 @thierryvolpiatto * helm-buffers.el: Show size of buffers.
thierryvolpiatto authored
266 i))
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
267 ;; Any non--file buffer.
25102c1 @thierryvolpiatto * helm-buffers.el (helm-c-highlight-buffers): Align correctly, add mo…
thierryvolpiatto authored
268 (t (cons (concat (propertize truncbuf 'face 'italic
269 'help-echo i)
270 " " str-before-size size " " mode) i)))))
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
271
272 (defun helm-c-buffer-match-major-mode (candidate)
273 "Match maybe buffer by major-mode.
274 If you give a major-mode or partial major-mode,
275 it will list all buffers of this major-mode and/or buffers with name
276 matching this major-mode.
277 If you add a space after major-mode and then a space,
278 it will match all buffers of the major-mode
279 before space matching pattern after space.
280 If you give a pattern which doesn't match a major-mode, it will search buffer
281 with name matching pattern."
282 (let* ((cand (replace-regexp-in-string "^\\s-\\{1\\}" "" candidate))
283 (buf (get-buffer cand)))
284 (when buf
285 (with-current-buffer buf
286 (let ((mjm (symbol-name major-mode))
287 (split (split-string helm-pattern)))
ce88e38 @thierryvolpiatto * helm-buffers.el (helm-c-buffer-match-major-mode): Issue #103 Match …
thierryvolpiatto authored
288 (cond ((string-match "^@" helm-pattern)
289 (or (helm-buffers-match-inside cand split)
290 (string-match helm-pattern cand)))
291 ((string-match "\\s-$" helm-pattern)
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
292 (string-match (car split) mjm))
ce88e38 @thierryvolpiatto * helm-buffers.el (helm-c-buffer-match-major-mode): Issue #103 Match …
thierryvolpiatto authored
293 ((string-match "\\s-[@]" helm-pattern)
294 (and (string-match (car split) mjm)
295 (helm-buffers-match-inside cand (cdr split))))
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
296 ((string-match "\\s-" helm-pattern)
297 (and (string-match (car split) mjm)
4760a01 @thierryvolpiatto * helm-buffers.el (helm-c-buffer-match-major-mode): Fix multiple matc…
thierryvolpiatto authored
298 (loop for i in (cdr split) always (string-match i cand))))
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
299 (t (or (string-match helm-pattern mjm)
300 (string-match helm-pattern cand)))))))))
301
ce88e38 @thierryvolpiatto * helm-buffers.el (helm-c-buffer-match-major-mode): Issue #103 Match …
thierryvolpiatto authored
302 (defun helm-buffers-match-inside (candidate lst)
303 (loop for i in lst
304 always
305 (cond ((string-match "\\`[\\]@" i)
306 (string-match i candidate))
307 ((string-match "\\`@\\(.*\\)" i)
308 (save-excursion
309 (let ((str (match-string 1 i)))
310 (goto-char (point-min))
311 (re-search-forward str nil t))))
312 (t (string-match i candidate)))))
313
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
314 (defun helm-c-buffer-query-replace-1 (&optional regexp-flag)
315 "Query replace in marked buffers.
316 If REGEXP-FLAG is given use `query-replace-regexp'."
317 (let ((fn (if regexp-flag 'query-replace-regexp 'query-replace))
318 (prompt (if regexp-flag "Query replace regexp" "Query replace"))
319 (bufs (helm-marked-candidates)))
320 (loop with replace = (query-replace-read-from prompt regexp-flag)
321 with tostring = (unless (consp replace)
322 (query-replace-read-to
323 replace prompt regexp-flag))
324 for buf in bufs
325 do
326 (save-window-excursion
327 (helm-c-switch-to-buffer buf)
328 (save-excursion
329 (let ((case-fold-search t))
330 (goto-char (point-min))
331 (if (consp replace)
332 (apply fn (list (car replace) (cdr replace)))
333 (apply fn (list replace tostring)))))))))
334
335 (defun helm-c-buffer-query-replace-regexp (candidate)
336 (helm-c-buffer-query-replace-1 'regexp))
337
338 (defun helm-c-buffer-query-replace (candidate)
339 (helm-c-buffer-query-replace-1))
340
341 (defun helm-buffer-toggle-diff (candidate)
342 "Toggle diff buffer CANDIDATE with it's file."
c0af0d6 @thierryvolpiatto * helm-buffers.el (helm-buffer-toggle-diff): restore underlying windo…
thierryvolpiatto authored
343 (helm-aif (get-buffer-window "*Diff*")
344 (progn (kill-buffer "*Diff*")
345 (set-window-buffer it helm-current-buffer))
346 (diff-buffer-with-file (get-buffer candidate))))
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
347
348 ;;;###autoload
349 (defun helm-buffer-diff-persistent ()
350 "Toggle diff buffer without quitting helm."
351 (interactive)
352 (helm-attrset 'diff-action 'helm-buffer-toggle-diff)
353 (helm-execute-persistent-action 'diff-action))
354
355 (defun helm-buffer-revert-and-update (candidate)
356 (let ((marked (helm-marked-candidates)))
357 (loop for buf in marked do (helm-revert-buffer buf))
358 (helm-force-update candidate)))
359
360 ;;;###autoload
361 (defun helm-buffer-revert-persistent ()
362 "Revert buffer without quitting helm."
363 (interactive)
364 (helm-attrset 'revert-action 'helm-buffer-revert-and-update)
a35d8f7 @thierryvolpiatto * helm-buffers.el (helm-buffer-save-persistent,helm-buffer-revert-per…
thierryvolpiatto authored
365 (helm-execute-persistent-action 'revert-action))
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
366
367 (defun helm-buffer-save-and-update (candidate)
368 (let ((marked (helm-marked-candidates))
369 (enable-recursive-minibuffers t))
370 (loop for buf in marked do
371 (with-current-buffer (get-buffer buf)
372 (save-buffer)))
373 (helm-force-update candidate)))
374
375 ;;;###autoload
376 (defun helm-buffer-save-persistent ()
377 "Save buffer without quitting helm."
378 (interactive)
379 (helm-attrset 'save-action 'helm-buffer-save-and-update)
a35d8f7 @thierryvolpiatto * helm-buffers.el (helm-buffer-save-persistent,helm-buffer-revert-per…
thierryvolpiatto authored
380 (helm-execute-persistent-action 'save-action))
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
381
382 ;;;###autoload
383 (defun helm-buffer-run-kill-buffers ()
384 "Run kill buffer action from `helm-c-source-buffers-list'."
385 (interactive)
386 (helm-c-quit-and-execute-action 'helm-kill-marked-buffers))
387
388 ;;;###autoload
389 (defun helm-buffer-run-grep ()
390 "Run Grep action from `helm-c-source-buffers-list'."
391 (interactive)
392 (helm-c-quit-and-execute-action 'helm-c-grep-buffers))
393
394 ;;;###autoload
395 (defun helm-buffer-run-zgrep ()
396 "Run Grep action from `helm-c-source-buffers-list'."
397 (interactive)
398 (helm-c-quit-and-execute-action 'helm-c-zgrep-buffers))
399
400 ;;;###autoload
401 (defun helm-buffer-run-query-replace-regexp ()
402 "Run Query replace regexp action from `helm-c-source-buffers-list'."
403 (interactive)
404 (helm-c-quit-and-execute-action 'helm-c-buffer-query-replace-regexp))
405
406 ;;;###autoload
407 (defun helm-buffer-run-query-replace ()
408 "Run Query replace action from `helm-c-source-buffers-list'."
409 (interactive)
410 (helm-c-quit-and-execute-action 'helm-c-buffer-query-replace))
411
412 ;;;###autoload
413 (defun helm-buffer-switch-other-window ()
414 "Run switch to other window action from `helm-c-source-buffers-list'."
415 (interactive)
416 (helm-c-quit-and-execute-action 'switch-to-buffer-other-window))
417
418 ;;;###autoload
419 (defun helm-buffer-switch-other-frame ()
420 "Run switch to other frame action from `helm-c-source-buffers-list'."
421 (interactive)
422 (helm-c-quit-and-execute-action 'switch-to-buffer-other-frame))
423
424 ;;;###autoload
425 (defun helm-buffer-switch-to-elscreen ()
426 "Run switch to elscreen action from `helm-c-source-buffers-list'."
427 (interactive)
428 (helm-c-quit-and-execute-action 'helm-find-buffer-on-elscreen))
429
430 ;;;###autoload
431 (defun helm-buffer-run-ediff ()
432 "Run ediff action from `helm-c-source-buffers-list'."
433 (interactive)
434 (helm-c-quit-and-execute-action 'helm-ediff-marked-buffers))
435
436 (defun helm-buffer-run-ediff-merge ()
437 "Run ediff action from `helm-c-source-buffers-list'."
438 (interactive)
439 (helm-c-quit-and-execute-action 'helm-ediff-marked-buffers-merge))
440
441 (defun helm-c-buffers-persistent-kill (buffer)
442 "Persistent action to kill buffer."
443 (with-current-buffer (get-buffer buffer)
444 (if (and (buffer-modified-p)
445 (buffer-file-name (current-buffer)))
446 (progn
447 (save-buffer)
448 (kill-buffer buffer))
449 (kill-buffer buffer)))
450 (helm-delete-current-selection))
451
452 (defun helm-c-buffers-list-persistent-action (candidate)
453 (if current-prefix-arg
454 (helm-c-buffers-persistent-kill candidate)
455 (helm-c-switch-to-buffer candidate)))
456
66550b2 @thierryvolpiatto * helm-buffers.el: require cl, helm, helm-vars, helm-utils.
thierryvolpiatto authored
457 (defun helm-ediff-marked-buffers (candidate &optional merge)
458 "Ediff 2 marked buffers or CANDIDATE and `helm-current-buffer'.
459 With optional arg MERGE call `ediff-merge-buffers'."
460 (let ((lg-lst (length (helm-marked-candidates)))
461 buf1 buf2)
462 (case lg-lst
463 (0
464 (error "Error:You have to mark at least 1 buffer"))
465 (1
466 (setq buf1 helm-current-buffer
467 buf2 (first (helm-marked-candidates))))
468 (2
469 (setq buf1 (first (helm-marked-candidates))
470 buf2 (second (helm-marked-candidates))))
471 (t
472 (error "Error:To much buffers marked!")))
473 (if merge
474 (ediff-merge-buffers buf1 buf2)
475 (ediff-buffers buf1 buf2))))
476
477 (defun helm-ediff-marked-buffers-merge (candidate)
478 "Ediff merge `helm-current-buffer' with CANDIDATE.
479 See `helm-ediff-marked-buffers'."
480 (helm-ediff-marked-buffers candidate t))
481
7288a9a @thierryvolpiatto Introduce multi occur.
thierryvolpiatto authored
482 (defun helm-multi-occur-as-action (_candidate)
39c674b @thierryvolpiatto * helm-buffers.el (helm-multi-occur-as-action): Issue #103 Use @ inpu…
thierryvolpiatto authored
483 "Multi occur action for `helm-c-source-buffers-list'.
484 Can be used by any source that list buffers."
3211573 @thierryvolpiatto * helm-buffers.el (helm-multi-occur-as-action): Force searching in cu…
thierryvolpiatto authored
485 (let ((helm-moccur-always-search-in-current
486 (or helm-moccur-always-search-in-current
487 helm-current-prefix-arg))
488 (buffers (helm-marked-candidates))
39c674b @thierryvolpiatto * helm-buffers.el (helm-multi-occur-as-action): Issue #103 Use @ inpu…
thierryvolpiatto authored
489 (input (loop for i in (split-string helm-pattern " " t)
490 thereis (and (string-match "\\`@\\(.*\\)" i)
491 (match-string 1 i)))))
492 (helm-multi-occur-1 buffers input)))
7288a9a @thierryvolpiatto Introduce multi occur.
thierryvolpiatto authored
493
56db4ca @thierryvolpiatto * helm-buffers.el: Forget autoload cookie for moccur action.
thierryvolpiatto authored
494 ;;;###autoload
7288a9a @thierryvolpiatto Introduce multi occur.
thierryvolpiatto authored
495 (defun helm-buffers-run-multi-occur ()
39c674b @thierryvolpiatto * helm-buffers.el (helm-multi-occur-as-action): Issue #103 Use @ inpu…
thierryvolpiatto authored
496 "Run `helm-multi-occur-as-action' by key."
7288a9a @thierryvolpiatto Introduce multi occur.
thierryvolpiatto authored
497 (interactive)
498 (helm-c-quit-and-execute-action 'helm-multi-occur-as-action))
499
66550b2 @thierryvolpiatto * helm-buffers.el: require cl, helm, helm-vars, helm-utils.
thierryvolpiatto authored
500 ;;; Candidate Transformers
501 ;;
502 ;;
09bc1b0 @thierryvolpiatto * helm-buffers.el: Show size of buffers.
thierryvolpiatto authored
503 (defun helm-c-skip-boring-buffers (buffers sources)
de6cb44 @thierryvolpiatto Fix shadow/skip boring files/buffers.
thierryvolpiatto authored
504 (helm-skip-entries buffers helm-c-boring-buffer-regexp-list))
66550b2 @thierryvolpiatto * helm-buffers.el: require cl, helm, helm-vars, helm-utils.
thierryvolpiatto authored
505
506 (defun helm-c-shadow-boring-buffers (buffers)
507 "Buffers matching `helm-c-boring-buffer-regexp' will be
508 displayed with the `file-name-shadow' face if available."
de6cb44 @thierryvolpiatto Fix shadow/skip boring files/buffers.
thierryvolpiatto authored
509 (helm-shadow-entries buffers helm-c-boring-buffer-regexp-list))
66550b2 @thierryvolpiatto * helm-buffers.el: require cl, helm, helm-vars, helm-utils.
thierryvolpiatto authored
510
511 (defun helm-revert-buffer (candidate)
512 (with-current-buffer candidate
513 (when (or (buffer-modified-p)
514 (not (verify-visited-file-modtime
515 (get-buffer candidate))))
516 (revert-buffer t t))))
517
518 (defun helm-revert-marked-buffers (ignore)
519 (mapc 'helm-revert-buffer (helm-marked-candidates)))
520
521 (defun helm-kill-marked-buffers (ignore)
522 (mapc 'kill-buffer (helm-marked-candidates)))
523
7974fc5 @thierryvolpiatto Create autoload system in helm-config.
thierryvolpiatto authored
524 (define-helm-type-attribute 'buffer
525 `((action
526 ("Switch to buffer" . helm-c-switch-to-buffer)
527 ,(and (locate-library "popwin") '("Switch to buffer in popup window" . popwin:popup-buffer))
528 ("Switch to buffer other window" . switch-to-buffer-other-window)
529 ("Switch to buffer other frame" . switch-to-buffer-other-frame)
530 ,(and (locate-library "elscreen") '("Display buffer in Elscreen" . helm-find-buffer-on-elscreen))
531 ("Query replace regexp" . helm-c-buffer-query-replace-regexp)
532 ("Query replace" . helm-c-buffer-query-replace)
533 ("View buffer" . view-buffer)
534 ("Display buffer" . display-buffer)
535 ("Grep buffers (C-u grep all buffers)" . helm-c-zgrep-buffers)
7288a9a @thierryvolpiatto Introduce multi occur.
thierryvolpiatto authored
536 ("Multi occur buffer(s)" . helm-multi-occur-as-action)
7974fc5 @thierryvolpiatto Create autoload system in helm-config.
thierryvolpiatto authored
537 ("Revert buffer(s)" . helm-revert-marked-buffers)
538 ("Insert buffer" . insert-buffer)
539 ("Kill buffer(s)" . helm-kill-marked-buffers)
540 ("Diff with file" . diff-buffer-with-file)
541 ("Ediff Marked buffers" . helm-ediff-marked-buffers)
542 ("Ediff Merge marked buffers" . (lambda (candidate)
543 (helm-ediff-marked-buffers candidate t))))
544 (persistent-help . "Show this buffer")
09bc1b0 @thierryvolpiatto * helm-buffers.el: Show size of buffers.
thierryvolpiatto authored
545 (filtered-candidate-transformer helm-c-skip-boring-buffers
546 helm-c-highlight-buffers))
7974fc5 @thierryvolpiatto Create autoload system in helm-config.
thierryvolpiatto authored
547 "Buffer or buffer name.")
548
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
549 ;;;###autoload
550 (defun helm-buffers-list ()
551 "Preconfigured `helm' to list buffers.
552 It is an enhanced version of `helm-for-buffers'."
553 (interactive)
554 (helm :sources '(helm-c-source-buffers-list
26a9255 @thierryvolpiatto * helm-buffers.el: reorder and add new source for ido virtual buffers.
thierryvolpiatto authored
555 helm-c-source-ido-virtual-buffers
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
556 helm-c-source-buffer-not-found)
557 :buffer "*helm buffers*" :keymap helm-c-buffer-map))
558
559 (provide 'helm-buffers)
560
37b4201 @thierryvolpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored
561 ;; Local Variables:
a7ee65b @thierryvolpiatto Disable compile warnings and add new keyword to helm.
thierryvolpiatto authored
562 ;; byte-compile-warnings: (not cl-functions obsolete)
37b4201 @thierryvolpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored
563 ;; coding: utf-8
564 ;; indent-tabs-mode: nil
565 ;; byte-compile-dynamic: t
566 ;; End:
567
9385bbf @thierryvolpiatto * helm-config.el Move buffers code to helm-buffers.el
thierryvolpiatto authored
568 ;;; helm-buffers.el ends here
Something went wrong with that request. Please try again.