(cc-imenu-java-generic-expression): A new, more accurate version of the
[emacs.git] / lisp / pcvs-util.el
blobe40caae6b14b57afec9b174032351e72d1e4d0a4
1 ;;; pcvs-util.el --- utility functions for PCL-CVS -*- byte-compile-dynamic: t -*-
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 ;; 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
7 ;; Keywords: pcl-cvs
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
27 ;;; Code:
29 (eval-when-compile (require 'cl))
31 ;;;;
32 ;;;; list processing
33 ;;;;
35 (defsubst cvs-car (x) (if (consp x) (car x) x))
36 (defalias 'cvs-cdr 'cdr-safe)
37 (defsubst cvs-append (&rest xs)
38 (apply 'append (mapcar (lambda (x) (if (listp x) x (list x))) xs)))
40 (defsubst cvs-every (-cvs-every-f -cvs-every-l)
41 (while (consp -cvs-every-l)
42 (unless (funcall -cvs-every-f (pop -cvs-every-l))
43 (setq -cvs-every-l t)))
44 (not -cvs-every-l))
46 (defun cvs-union (xs ys)
47 (let ((zs ys))
48 (dolist (x xs zs)
49 (unless (member x ys) (push x zs)))))
51 (defun cvs-map (-cvs-map-f &rest -cvs-map-ls)
52 (let ((accum ()))
53 (while (not (cvs-every 'null -cvs-map-ls))
54 (push (apply -cvs-map-f (mapcar 'car -cvs-map-ls)) accum)
55 (setq -cvs-map-ls (mapcar 'cdr -cvs-map-ls)))
56 (nreverse accum)))
58 (defun cvs-first (l &optional n)
59 (if (null n) (car l)
60 (when l
61 (let* ((nl (list (pop l)))
62 (ret nl))
63 (while (and l (> n 1))
64 (setcdr nl (list (pop l)))
65 (setq nl (cdr nl))
66 (decf n))
67 ret))))
69 (defun cvs-partition (p l)
70 "Partition a list L into two lists based on predicate P.
71 The function returns a `cons' cell where the `car' contains
72 elements of L for which P is true while the `cdr' contains
73 the other elements. The ordering among elements is maintained."
74 (let (car cdr)
75 (dolist (x l)
76 (if (funcall p x) (push x car) (push x cdr)))
77 (cons (nreverse car) (nreverse cdr))))
79 ;;;
80 ;;; frame, window, buffer handling
81 ;;;
83 (defun cvs-pop-to-buffer-same-frame (buf)
84 "Pop to BUF like `pop-to-buffer' but staying on the same frame.
85 If `pop-to-buffer' would have opened a new frame, this function would
86 try to split a new window instead."
87 (let ((pop-up-windows (or pop-up-windows pop-up-frames))
88 (pop-up-frames nil))
89 (or (let ((buf (get-buffer-window buf))) (and buf (select-window buf)))
90 (and pop-up-windows
91 (ignore-errors (select-window (split-window-vertically)))
92 (switch-to-buffer buf))
93 (pop-to-buffer (current-buffer)))))
95 (defun cvs-bury-buffer (buf &optional mainbuf)
96 "Hide the buffer BUF that was temporarily popped up.
97 BUF is assumed to be a temporary buffer used from the buffer MAINBUF."
98 (interactive (list (current-buffer)))
99 (save-current-buffer
100 (let ((win (if (eq buf (window-buffer (selected-window))) (selected-window)
101 (get-buffer-window buf t))))
102 (when win
103 (if (window-dedicated-p win)
104 (condition-case ()
105 (delete-window win)
106 (error (iconify-frame (window-frame win))))
107 ;;; (if (and mainbuf (get-buffer-window mainbuf))
108 ;;; ;; FIXME: if the buffer popped into a pre-existing window,
109 ;;; ;; we don't want to delete that window.
110 ;;; t ;;(delete-window win)
111 ;;; )
113 (with-current-buffer buf
114 (bury-buffer (unless (and (eq buf (window-buffer (selected-window)))
115 (not (window-dedicated-p (selected-window))))
116 buf)))
117 (when mainbuf
118 (let ((mainwin (or (get-buffer-window mainbuf)
119 (get-buffer-window mainbuf 'visible))))
120 (when mainwin (select-window mainwin))))))
122 (defun cvs-get-buffer-create (name &optional noreuse)
123 "Create a buffer NAME unless such a buffer already exists.
124 If the NAME looks like an absolute file name, the buffer will be created
125 with `create-file-buffer' and will probably get another name than NAME.
126 In such a case, the search for another buffer with the same name doesn't
127 use the buffer name but the buffer's `list-buffers-directory' variable.
128 If NOREUSE is non-nil, always return a new buffer."
129 (or (and (not (file-name-absolute-p name))
130 (if noreuse (generate-new-buffer name)
131 (get-buffer-create name)))
132 (unless noreuse
133 (dolist (buf (buffer-list))
134 (with-current-buffer buf
135 (when (equal name list-buffers-directory)
136 (return buf)))))
137 (with-current-buffer (create-file-buffer name)
138 (setq list-buffers-directory name)
139 (current-buffer))))
141 ;;;;
142 ;;;; string processing
143 ;;;;
145 (defun cvs-insert-strings (strings)
146 "Insert a list of STRINGS into the current buffer.
147 Uses columns to keep the listing readable but compact."
148 (when (consp strings)
149 (let* ((length (apply 'max (mapcar 'length strings)))
150 (wwidth (1- (window-width)))
151 (columns (min
152 ;; At least 2 columns; at least 2 spaces between columns.
153 (max 2 (/ wwidth (+ 2 length)))
154 ;; Don't allocate more columns than we can fill.
155 ;; Windows can't show less than 3 lines anyway.
156 (max 1 (/ (length strings) 2))))
157 (colwidth (/ wwidth columns)))
158 ;; Use tab-width rather than indent-to.
159 (setq tab-width colwidth)
160 ;; The insertion should be "sensible" no matter what choices were made.
161 (dolist (str strings)
162 (unless (bolp)
163 (insert " \t")
164 (when (< wwidth (+ (max colwidth (length str)) (current-column)))
165 (delete-char -2) (insert "\n")))
166 (insert str)))))
169 (defun cvs-file-to-string (file &optional oneline args)
170 "Read the content of FILE and return it as a string.
171 If ONELINE is t, only the first line (no \\n) will be returned.
172 If ARGS is non-nil, the file will be executed with ARGS as its
173 arguments. If ARGS is not a list, no argument will be passed."
174 (condition-case nil
175 (with-temp-buffer
176 (if args
177 (apply 'call-process
178 file nil t nil (when (listp args) args))
179 (insert-file-contents file))
180 (goto-char (point-min))
181 (buffer-substring (point)
182 (if oneline (line-end-position) (point-max))))
183 (file-error nil)))
185 (defun cvs-string-prefix-p (str1 str2)
186 "Tell whether STR1 is a prefix of STR2."
187 (eq t (compare-strings str2 nil (length str1) str1 nil nil)))
189 ;;;;
190 ;;;; file names
191 ;;;;
193 (defsubst cvs-expand-dir-name (d)
194 (file-name-as-directory (expand-file-name d)))
196 ;;;;
197 ;;;; (interactive <foo>) support function
198 ;;;;
200 (defstruct (cvs-qtypedesc
201 (:constructor nil) (:copier nil)
202 (:constructor cvs-qtypedesc-create
203 (str2obj obj2str &optional complete hist-sym require)))
204 str2obj
205 obj2str
206 hist-sym
207 complete
208 require)
211 (defconst cvs-qtypedesc-string1 (cvs-qtypedesc-create 'identity 'identity t))
212 (defconst cvs-qtypedesc-string (cvs-qtypedesc-create 'identity 'identity))
213 (defconst cvs-qtypedesc-strings
214 (cvs-qtypedesc-create 'split-string-and-unquote
215 'combine-and-quote-strings nil))
217 (defun cvs-query-read (default prompt qtypedesc &optional hist-sym)
218 (let* ((qtypedesc (or qtypedesc cvs-qtypedesc-strings))
219 (hist-sym (or hist-sym (cvs-qtypedesc-hist-sym qtypedesc)))
220 (complete (cvs-qtypedesc-complete qtypedesc))
221 (completions (and (functionp complete) (funcall complete)))
222 (initval (funcall (cvs-qtypedesc-obj2str qtypedesc) default)))
223 (funcall (cvs-qtypedesc-str2obj qtypedesc)
224 (cond
225 ((null complete) (read-string prompt initval hist-sym))
226 ((functionp complete)
227 (completing-read prompt completions
228 nil (cvs-qtypedesc-require qtypedesc)
229 initval hist-sym))
230 (t initval)))))
232 ;;;;
233 ;;;; Flags handling
234 ;;;;
236 (defstruct (cvs-flags
237 (:constructor nil)
238 (:constructor -cvs-flags-make
239 (desc defaults &optional qtypedesc hist-sym)))
240 defaults persist desc qtypedesc hist-sym)
242 (defmacro cvs-flags-define (sym defaults
243 &optional desc qtypedesc hist-sym docstring)
244 `(defconst ,sym
245 (let ((bound (boundp ',sym)))
246 (if (and bound (cvs-flags-p ,sym)) ,sym
247 (let ((defaults ,defaults))
248 (-cvs-flags-make ,desc
249 (if bound (cons ,sym (cdr defaults)) defaults)
250 ,qtypedesc ,hist-sym))))
251 ,docstring))
253 (defun cvs-flags-query (sym &optional desc arg)
254 "Query flags based on SYM.
255 Optional argument DESC will be used for the prompt.
256 If ARG (or a prefix argument) is nil, just use the 0th default.
257 If it is a non-negative integer, use the corresponding default.
258 If it is a negative integer query for a new value of the corresponding
259 default and return that new value.
260 If it is \\[universal-argument], just query and return a value without
261 altering the defaults.
262 If it is \\[universal-argument] \\[universal-argument], behave just
263 as if a negative zero was provided."
264 (let* ((flags (symbol-value sym))
265 (desc (or desc (cvs-flags-desc flags)))
266 (qtypedesc (cvs-flags-qtypedesc flags))
267 (hist-sym (cvs-flags-hist-sym flags))
268 (arg (if (eq arg 'noquery) 0 (or arg current-prefix-arg 0)))
269 (numarg (prefix-numeric-value arg))
270 (defaults (cvs-flags-defaults flags))
271 (permstr (if (< numarg 0) (format " (%sth default)" (- numarg)))))
272 ;; special case for universal-argument
273 (when (consp arg)
274 (setq permstr (if (> numarg 4) " (permanent)" ""))
275 (setq numarg 0))
277 ;; sanity check
278 (unless (< (abs numarg) (length defaults))
279 (error "There is no %sth default" (abs numarg)))
281 (if permstr
282 (let* ((prompt (format "%s%s: " desc permstr))
283 (fs (cvs-query-read (nth (- numarg) (cvs-flags-defaults flags))
284 prompt qtypedesc hist-sym)))
285 (when (not (equal permstr ""))
286 (setf (nth (- numarg) (cvs-flags-defaults flags)) fs))
288 (nth numarg defaults))))
290 (defsubst cvs-flags-set (sym index value)
291 "Set SYM's INDEX'th setting to VALUE."
292 (setf (nth index (cvs-flags-defaults (symbol-value sym))) value))
294 ;;;;
295 ;;;; Prefix keys
296 ;;;;
298 (defconst cvs-prefix-number 10)
300 (defsubst cvs-prefix-sym (sym) (intern (concat (symbol-name sym) "-cps")))
302 (defmacro cvs-prefix-define (sym docstring desc defaults
303 &optional qtypedesc hist-sym)
304 (let ((cps (cvs-prefix-sym sym)))
305 `(progn
306 (defvar ,sym nil ,(concat (or docstring "") "
307 See `cvs-prefix-set' for further description of the behavior."))
308 (defvar ,cps
309 (let ((defaults ,defaults))
310 ;; sanity ensurance
311 (unless (>= (length defaults) cvs-prefix-number)
312 (setq defaults (append defaults
313 (make-list (1- cvs-prefix-number)
314 (nth 0 defaults)))))
315 (-cvs-flags-make ,desc defaults ,qtypedesc ,hist-sym))))))
317 (defun cvs-prefix-make-local (sym)
318 (let ((cps (cvs-prefix-sym sym)))
319 (make-local-variable sym)
320 (set (make-local-variable cps) (copy-cvs-flags (symbol-value cps)))))
322 (defun cvs-prefix-set (sym arg)
323 ;; we could distinguish between numeric and non-numeric prefix args instead of
324 ;; relying on that magic `4'.
325 "Set the cvs-prefix contained in SYM.
326 If ARG is between 0 and 9, it selects the corresponding default.
327 If ARG is negative (or \\[universal-argument] which corresponds to negative 0),
328 it queries the user and sets the -ARG'th default.
329 If ARG is greater than 9 (or \\[universal-argument] \\[universal-argument]),
330 the (ARG mod 10)'th prefix is made persistent.
331 If ARG is nil toggle the PREFIX's value between its 0th default and nil
332 and reset the persistence."
333 (let* ((prefix (symbol-value (cvs-prefix-sym sym)))
334 (numarg (if (integerp arg) arg 0))
335 ;; (defs (cvs-flags-defaults prefix))
338 ;; set persistence if requested
339 (when (> (prefix-numeric-value arg) 9)
340 (setf (cvs-flags-persist prefix) t)
341 (setq numarg (mod numarg 10)))
343 ;; set the value
344 (set sym
345 (cond
346 ((null arg)
347 (setf (cvs-flags-persist prefix) nil)
348 (unless (symbol-value sym) (nth 0 (cvs-flags-defaults prefix))))
350 ((or (consp arg) (< numarg 0))
351 (setf (nth (- numarg) (cvs-flags-defaults prefix))
352 (cvs-query-read (nth (- numarg) (cvs-flags-defaults prefix))
353 (format "%s: " (cvs-flags-desc prefix))
354 (cvs-flags-qtypedesc prefix)
355 (cvs-flags-hist-sym prefix))))
356 (t (nth numarg (cvs-flags-defaults prefix)))))
357 (force-mode-line-update)))
359 (defun cvs-prefix-get (sym &optional read-only)
360 "Return the current value of the prefix SYM.
361 And reset it unless READ-ONLY is non-nil."
362 (prog1 (symbol-value sym)
363 (unless (or read-only
364 (cvs-flags-persist (symbol-value (cvs-prefix-sym sym))))
365 (set sym nil)
366 (force-mode-line-update))))
368 (provide 'pcvs-util)
370 ;; arch-tag: 3b2588bb-2ae3-4f1f-bf5b-dea91b1f8a59
371 ;;; pcvs-util.el ends here