Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 209 lines (185 sloc) 7.526 kb
94ce74e Steve Purcell Tag version 0.1 for submission to marmalade-repo.org
authored
1 ;;; ibuffer-vc.el --- Group ibuffer's list by VC project, or show VC status
2 ;;
02b489b Steve Purcell Update copyright year
authored
3 ;; Copyright (C) 2011-2014 Steve Purcell
94ce74e Steve Purcell Tag version 0.1 for submission to marmalade-repo.org
authored
4 ;;
5 ;; Author: Steve Purcell <steve@sanityinc.com>
d2c9817 Steve Purcell Initial check-in
authored
6 ;; Keywords: themes
3b203e4 Steve Purcell Switch to cl-lib
authored
7 ;; Package-Requires: ((cl-lib "0.2"))
d2c9817 Steve Purcell Initial check-in
authored
8 ;; X-URL: http://github.com/purcell/ibuffer-vc
9 ;; URL: http://github.com/purcell/ibuffer-vc
268c425 Steve Purcell By default, don't access vc status of remote files (fixes #3)
authored
10 ;; Version: DEV
94ce74e Steve Purcell Tag version 0.1 for submission to marmalade-repo.org
authored
11 ;;
d2c9817 Steve Purcell Initial check-in
authored
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
94ce74e Steve Purcell Tag version 0.1 for submission to marmalade-repo.org
authored
16 ;;
d2c9817 Steve Purcell Initial check-in
authored
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
94ce74e Steve Purcell Tag version 0.1 for submission to marmalade-repo.org
authored
21 ;;
d2c9817 Steve Purcell Initial check-in
authored
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
94ce74e Steve Purcell Tag version 0.1 for submission to marmalade-repo.org
authored
24 ;;
d2c9817 Steve Purcell Initial check-in
authored
25 ;;; Commentary:
26 ;;
27 ;; Adds functionality to ibuffer for grouping buffers by their parent
aff2fd5 Steve Purcell Add an ibuffer sorter for vc status
authored
28 ;; vc root directory, and for displaying and/or sorting by the vc
29 ;; status of listed files.
d2c9817 Steve Purcell Initial check-in
authored
30 ;;
31 ;;; Use:
32 ;;
33 ;; To group buffers by vc parent dir:
34 ;;
35 ;; M-x ibuffer-vc-set-filter-groups-by-vc-root
36 ;;
37 ;; or, make this the default:
38 ;;
286f228 Steve Purcell Recommend the use of 'ibuffer-hook rather than 'ibuffer-mode-hook
authored
39 ;; (add-hook 'ibuffer-hook
d2c9817 Steve Purcell Initial check-in
authored
40 ;; (lambda ()
41 ;; (ibuffer-vc-set-filter-groups-by-vc-root)
cf35118 Steve Purcell Improve code snippet in suggested usage section
authored
42 ;; (unless (eq ibuffer-sorting-mode 'alphabetic)
43 ;; (ibuffer-do-sort-by-alphabetic))))
d2c9817 Steve Purcell Initial check-in
authored
44 ;;
296bf3c Steve Purcell Note ability to use ibuffer-vc-generate-filter-groups-by-vc-root directl...
authored
45 ;; Alternatively, use `ibuffer-vc-generate-filter-groups-by-vc-root'
46 ;; to programmatically obtain a list of filter groups that you can
47 ;; combine with your own custom groups.
48 ;;
d2c9817 Steve Purcell Initial check-in
authored
49 ;; To include vc status info in the ibuffer list, add either
50 ;; vc-status-mini or vc-status to `ibuffer-formats':
51 ;;
52 ;; (setq ibuffer-formats
53 ;; '((mark modified read-only vc-status-mini " "
54 ;; (name 18 18 :left :elide)
55 ;; " "
56 ;; (size 9 -1 :right)
57 ;; " "
58 ;; (mode 16 16 :left :elide)
59 ;; " "
60 ;; (vc-status 16 16 :left)
61 ;; " "
62 ;; filename-and-process)))
63 ;;
aff2fd5 Steve Purcell Add an ibuffer sorter for vc status
authored
64 ;; To sort by vc status, use `ibuffer-do-sort-by-vc-status', which can
65 ;; also be selected by repeatedly executing
66 ;; `ibuffer-toggle-sorting-mode' (bound to "," by default).
67 ;;
d2c9817 Steve Purcell Initial check-in
authored
68 ;;; Code:
69
70 ;; requires
71
72 (require 'ibuffer)
7290c3a Steve Purcell Ensure ibuf-ext is loaded first
authored
73 (require 'ibuf-ext)
d2c9817 Steve Purcell Initial check-in
authored
74 (require 'vc-hooks)
3b203e4 Steve Purcell Switch to cl-lib
authored
75 (require 'cl-lib)
e7c71b1 Steve Purcell Don't require cl
authored
76
d2c9817 Steve Purcell Initial check-in
authored
77
268c425 Steve Purcell By default, don't access vc status of remote files (fixes #3)
authored
78 (defgroup ibuffer-vc nil
79 "Group ibuffer entries according to their version control status."
80 :prefix "ibuffer-vc-"
81 :group 'convenience)
82
83 (defcustom ibuffer-vc-skip-if-remote t
84 "If non-nil, don't query the VC status of remote files."
85 :type 'boolean
86 :group 'ibuffer-vc)
87
155c3bb Steve Purcell Add ibuffer-vc-include-function option
authored
88 (defcustom ibuffer-vc-include-function 'identity
89 "A function which tells whether a given file should be grouped.
90
91 The function is passed a filename, and should return non-nil if the file
92 is to be grouped.
93
ee78998 Steve Purcell Fix docstring
authored
94 This option can be used to exclude certain files from the grouping mechanism."
155c3bb Steve Purcell Add ibuffer-vc-include-function option
authored
95 :type 'function
96 :group 'ibuffer-vc)
97
d2c9817 Steve Purcell Initial check-in
authored
98 ;;; Group and filter ibuffer entries by parent vc directory
99
268c425 Steve Purcell By default, don't access vc status of remote files (fixes #3)
authored
100 (defun ibuffer-vc--include-file-p (file)
6e4c575 Steve Purcell Fix checkdoc issues
authored
101 "Return t iff FILE should be included in ibuffer-vc's filtering."
155c3bb Steve Purcell Add ibuffer-vc-include-function option
authored
102 (and file
103 (or (null ibuffer-vc-skip-if-remote)
104 (not (file-remote-p file)))
105 (funcall ibuffer-vc-include-function file)))
268c425 Steve Purcell By default, don't access vc status of remote files (fixes #3)
authored
106
d708bb2 Steve Purcell Properly determine the (most likely) VC root of unregistered files, to a...
authored
107 (defun ibuffer-vc--deduce-backend (file)
6e4c575 Steve Purcell Fix checkdoc issues
authored
108 "Return the vc backend for FILE, or nil if not under VC supervision."
d708bb2 Steve Purcell Properly determine the (most likely) VC root of unregistered files, to a...
authored
109 (or (vc-backend file)
3b203e4 Steve Purcell Switch to cl-lib
authored
110 (cl-loop for backend in vc-handled-backends
111 when (vc-call-backend backend 'responsible-p file)
112 return backend)))
d2c9817 Steve Purcell Initial check-in
authored
113
d708bb2 Steve Purcell Properly determine the (most likely) VC root of unregistered files, to a...
authored
114 (defun ibuffer-vc-root (buf)
6e4c575 Steve Purcell Fix checkdoc issues
authored
115 "Return a cons cell (backend-name . root-dir) for BUF.
116 If the file is not under version control, nil is returned instead."
268c425 Steve Purcell By default, don't access vc status of remote files (fixes #3)
authored
117 (let ((file-name (with-current-buffer buf (or buffer-file-name default-directory))))
118 (when (ibuffer-vc--include-file-p file-name)
119 (let ((backend (ibuffer-vc--deduce-backend file-name)))
120 (when backend
121 (let* ((root-fn-name (intern (format "vc-%s-root" (downcase (symbol-name backend)))))
122 (root-dir
123 (cond
124 ((fboundp root-fn-name) (funcall root-fn-name file-name)) ; git, svn, hg, bzr (at least)
125 ((memq backend '(darcs DARCS)) (vc-darcs-find-root file-name))
126 ((memq backend '(cvs CVS)) (vc-find-root file-name "CVS"))
127 (t (error "ibuffer-vc: don't know how to find root for vc backend '%s' - please submit a bug report or patch" backend)))))
128 (cons backend root-dir)))))))
c45b1d9 Steve Purcell Use vc to find root dirs more directly, and display the vc type in each ...
authored
129
d2c9817 Steve Purcell Initial check-in
authored
130 (define-ibuffer-filter vc-root
131 "Toggle current view to buffers with vc root dir QUALIFIER."
132 (:description "vc root dir"
133 :reader (read-from-minibuffer "Filter by vc root dir (regexp): "))
d708bb2 Steve Purcell Properly determine the (most likely) VC root of unregistered files, to a...
authored
134 (ibuffer-awhen (ibuffer-vc-root buf)
135 (equal qualifier it)))
d2c9817 Steve Purcell Initial check-in
authored
136
296bf3c Steve Purcell Note ability to use ibuffer-vc-generate-filter-groups-by-vc-root directl...
authored
137 ;;;###autoload
d2c9817 Steve Purcell Initial check-in
authored
138 (defun ibuffer-vc-generate-filter-groups-by-vc-root ()
6e4c575 Steve Purcell Fix checkdoc issues
authored
139 "Create a set of ibuffer filter groups based on the vc root dirs of buffers."
8011a85 Steve Purcell Sort sub-repos before parent repos so that buffers are correctly filed i...
authored
140 (let ((roots (ibuffer-remove-duplicates
141 (delq nil (mapcar 'ibuffer-vc-root (buffer-list))))))
142 (mapcar (lambda (vc-root)
143 (cons (format "%s:%s" (car vc-root) (cdr vc-root))
d708bb2 Steve Purcell Properly determine the (most likely) VC root of unregistered files, to a...
authored
144 `((vc-root . ,vc-root))))
8011a85 Steve Purcell Sort sub-repos before parent repos so that buffers are correctly filed i...
authored
145 roots)))
d2c9817 Steve Purcell Initial check-in
authored
146
147 ;;;###autoload
148 (defun ibuffer-vc-set-filter-groups-by-vc-root ()
149 "Set the current filter groups to filter by vc root dir."
150 (interactive)
151 (setq ibuffer-filter-groups (ibuffer-vc-generate-filter-groups-by-vc-root))
3791892 Steve Purcell When setting filter groups, only call ibuffer-update if ibuffer is runni...
authored
152 (message "ibuffer-vc: groups set")
153 (let ((ibuf (get-buffer "*Ibuffer*")))
154 (when ibuf
155 (with-current-buffer ibuf
156 (pop-to-buffer ibuf)
157 (ibuffer-update nil t)))))
d2c9817 Steve Purcell Initial check-in
authored
158
159
160 ;;; Display vc status info in the ibuffer list
161
aff2fd5 Steve Purcell Add an ibuffer sorter for vc status
authored
162 (defun ibuffer-vc--status-string ()
6e4c575 Steve Purcell Fix checkdoc issues
authored
163 "Return a short string to represent the current buffer's status."
268c425 Steve Purcell By default, don't access vc status of remote files (fixes #3)
authored
164 (when (and buffer-file-name (ibuffer-vc--include-file-p buffer-file-name))
aff2fd5 Steve Purcell Add an ibuffer sorter for vc status
authored
165 (let ((state (vc-state buffer-file-name)))
166 (if state
167 (symbol-name state)
168 "-"))))
169
be223fa Steve Purcell Figured out how to autoload column definitions
authored
170 ;;;###autoload (autoload 'ibuffer-make-column-vc-status "ibuffer-vc")
d2c9817 Steve Purcell Initial check-in
authored
171 (define-ibuffer-column vc-status
be223fa Steve Purcell Figured out how to autoload column definitions
authored
172 (:name "VC status")
aff2fd5 Steve Purcell Add an ibuffer sorter for vc status
authored
173 (ibuffer-vc--status-string))
d2c9817 Steve Purcell Initial check-in
authored
174
be223fa Steve Purcell Figured out how to autoload column definitions
authored
175 ;;;###autoload (autoload 'ibuffer-make-column-vc-status-mini "ibuffer-vc")
d2c9817 Steve Purcell Initial check-in
authored
176 (define-ibuffer-column vc-status-mini
be223fa Steve Purcell Figured out how to autoload column definitions
authored
177 (:name "V")
268c425 Steve Purcell By default, don't access vc status of remote files (fixes #3)
authored
178 (if (and buffer-file-name (ibuffer-vc--include-file-p buffer-file-name))
d2c9817 Steve Purcell Initial check-in
authored
179 (let ((state (vc-state buffer-file-name)))
180 (cond
181 ((eq 'added state) "A")
182 ((eq 'removed state) "D")
23cb447 Steve Purcell Use "=" instead of "@" to indicate up-to-date files
authored
183 ((eq 'up-to-date state) "=")
d2c9817 Steve Purcell Initial check-in
authored
184 ((eq 'edited state) "*")
185 ((eq 'needs-update state) "O")
186 ((memq state '(conflict needs-merge unlocked-changes)) "!")
187 ((eq 'ignored state) "I")
188 ((memq state '(() unregistered missing)) "?")))
189 " "))
190
aff2fd5 Steve Purcell Add an ibuffer sorter for vc status
authored
191 ;;;###autoload (autoload 'ibuffer-do-sort-by-vc-status "ibuffer-vc")
192 (define-ibuffer-sorter vc-status
193 "Sort the buffers by their vc status."
194 (:description "vc status")
195 (let ((file1 (with-current-buffer (car a)
196 buffer-file-name))
197 (file2 (with-current-buffer (car b)
198 buffer-file-name)))
199 (if (and file1 file2)
200 (string-lessp (with-current-buffer (car a)
201 (ibuffer-vc--status-string))
202 (with-current-buffer (car b)
203 (ibuffer-vc--status-string)))
204 (not (null file1)))))
205
d2c9817 Steve Purcell Initial check-in
authored
206
207 (provide 'ibuffer-vc)
94ce74e Steve Purcell Tag version 0.1 for submission to marmalade-repo.org
authored
208 ;;; ibuffer-vc.el ends here
Something went wrong with that request. Please try again.