(vc-hg-workfile-unchanged-p): New function.
[bpt/emacs.git] / lisp / pcvs-util.el
1 ;;; pcvs-util.el --- utility functions for PCL-CVS -*- byte-compile-dynamic: t -*-
2
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 ;; 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5
6 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
7 ;; Keywords: pcl-cvs
8
9 ;; This file is part of GNU Emacs.
10
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 2, or (at your option)
14 ;; any later version.
15
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.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32
33 ;;;;
34 ;;;; list processing
35 ;;;;
36
37 (defsubst cvs-car (x) (if (consp x) (car x) x))
38 (defalias 'cvs-cdr 'cdr-safe)
39 (defsubst cvs-append (&rest xs)
40 (apply 'append (mapcar (lambda (x) (if (listp x) x (list x))) xs)))
41
42 (defsubst cvs-every (-cvs-every-f -cvs-every-l)
43 (while (consp -cvs-every-l)
44 (unless (funcall -cvs-every-f (pop -cvs-every-l))
45 (setq -cvs-every-l t)))
46 (not -cvs-every-l))
47
48 (defun cvs-union (xs ys)
49 (let ((zs ys))
50 (dolist (x xs zs)
51 (unless (member x ys) (push x zs)))))
52
53 (defun cvs-map (-cvs-map-f &rest -cvs-map-ls)
54 (unless (cvs-every 'null -cvs-map-ls)
55 (cons (apply -cvs-map-f (mapcar 'car -cvs-map-ls))
56 (apply 'cvs-map -cvs-map-f (mapcar 'cdr -cvs-map-ls)))))
57
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))))
68
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))))
78
79 ;;;
80 ;;; frame, window, buffer handling
81 ;;;
82
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)))))
94
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 ;;; )
112 )))
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))))))
121
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 (set (make-local-variable 'list-buffers-directory) name)
139 (current-buffer))))
140
141 ;;;;
142 ;;;; string processing
143 ;;;;
144
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)))))
167
168
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)))
184
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)))
188
189 ;;;;
190 ;;;; file names
191 ;;;;
192
193 (defsubst cvs-expand-dir-name (d)
194 (file-name-as-directory (expand-file-name d)))
195
196 ;;;;
197 ;;;; (interactive <foo>) support function
198 ;;;;
199
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)
209
210
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 'string->strings 'strings->string nil))
215
216 (defun cvs-query-read (default prompt qtypedesc &optional hist-sym)
217 (let* ((qtypedesc (or qtypedesc cvs-qtypedesc-strings))
218 (hist-sym (or hist-sym (cvs-qtypedesc-hist-sym qtypedesc)))
219 (complete (cvs-qtypedesc-complete qtypedesc))
220 (completions (and (functionp complete) (funcall complete)))
221 (initval (funcall (cvs-qtypedesc-obj2str qtypedesc) default)))
222 (funcall (cvs-qtypedesc-str2obj qtypedesc)
223 (cond
224 ((null complete) (read-string prompt initval hist-sym))
225 ((functionp complete)
226 (completing-read prompt completions
227 nil (cvs-qtypedesc-require qtypedesc)
228 initval hist-sym))
229 (t initval)))))
230
231 ;;;;
232 ;;;; Flags handling
233 ;;;;
234
235 (defstruct (cvs-flags
236 (:constructor nil)
237 (:constructor -cvs-flags-make
238 (desc defaults &optional qtypedesc hist-sym)))
239 defaults persist desc qtypedesc hist-sym)
240
241 (defmacro cvs-flags-define (sym defaults
242 &optional desc qtypedesc hist-sym docstring)
243 `(defconst ,sym
244 (let ((bound (boundp ',sym)))
245 (if (and bound (cvs-flags-p ,sym)) ,sym
246 (let ((defaults ,defaults))
247 (-cvs-flags-make ,desc
248 (if bound (cons ,sym (cdr defaults)) defaults)
249 ,qtypedesc ,hist-sym))))
250 ,docstring))
251
252 (defun cvs-flags-query (sym &optional desc arg)
253 "Query flags based on SYM.
254 Optional argument DESC will be used for the prompt.
255 If ARG (or a prefix argument) is nil, just use the 0th default.
256 If it is a non-negative integer, use the corresponding default.
257 If it is a negative integer query for a new value of the corresponding
258 default and return that new value.
259 If it is \\[universal-argument], just query and return a value without
260 altering the defaults.
261 If it is \\[universal-argument] \\[universal-argument], behave just
262 as if a negative zero was provided."
263 (let* ((flags (symbol-value sym))
264 (desc (or desc (cvs-flags-desc flags)))
265 (qtypedesc (cvs-flags-qtypedesc flags))
266 (hist-sym (cvs-flags-hist-sym flags))
267 (arg (if (eq arg 'noquery) 0 (or arg current-prefix-arg 0)))
268 (numarg (prefix-numeric-value arg))
269 (defaults (cvs-flags-defaults flags))
270 (permstr (if (< numarg 0) (format " (%sth default)" (- numarg)))))
271 ;; special case for universal-argument
272 (when (consp arg)
273 (setq permstr (if (> numarg 4) " (permanent)" ""))
274 (setq numarg 0))
275
276 ;; sanity check
277 (unless (< (abs numarg) (length defaults))
278 (error "There is no %sth default" (abs numarg)))
279
280 (if permstr
281 (let* ((prompt (format "%s%s: " desc permstr))
282 (fs (cvs-query-read (nth (- numarg) (cvs-flags-defaults flags))
283 prompt qtypedesc hist-sym)))
284 (when (not (equal permstr ""))
285 (setf (nth (- numarg) (cvs-flags-defaults flags)) fs))
286 fs)
287 (nth numarg defaults))))
288
289 (defsubst cvs-flags-set (sym index value)
290 "Set SYM's INDEX'th setting to VALUE."
291 (setf (nth index (cvs-flags-defaults (symbol-value sym))) value))
292
293 ;;;;
294 ;;;; Prefix keys
295 ;;;;
296
297 (defconst cvs-prefix-number 10)
298
299 (defsubst cvs-prefix-sym (sym) (intern (concat (symbol-name sym) "-cps")))
300
301 (defmacro cvs-prefix-define (sym docstring desc defaults
302 &optional qtypedesc hist-sym)
303 (let ((cps (cvs-prefix-sym sym)))
304 `(progn
305 (defvar ,sym nil ,(concat (or docstring "") "
306 See `cvs-prefix-set' for further description of the behavior."))
307 (defvar ,cps
308 (let ((defaults ,defaults))
309 ;; sanity ensurance
310 (unless (>= (length defaults) cvs-prefix-number)
311 (setq defaults (append defaults
312 (make-list (1- cvs-prefix-number)
313 (nth 0 defaults)))))
314 (-cvs-flags-make ,desc defaults ,qtypedesc ,hist-sym))))))
315
316 (defun cvs-prefix-make-local (sym)
317 (let ((cps (cvs-prefix-sym sym)))
318 (make-local-variable sym)
319 (set (make-local-variable cps) (copy-cvs-flags (symbol-value cps)))))
320
321 (defun cvs-prefix-set (sym arg)
322 ;; we could distinguish between numeric and non-numeric prefix args instead of
323 ;; relying on that magic `4'.
324 "Set the cvs-prefix contained in SYM.
325 If ARG is between 0 and 9, it selects the corresponding default.
326 If ARG is negative (or \\[universal-argument] which corresponds to negative 0),
327 it queries the user and sets the -ARG'th default.
328 If ARG is greater than 9 (or \\[universal-argument] \\[universal-argument]),
329 the (ARG mod 10)'th prefix is made persistent.
330 If ARG is nil toggle the PREFIX's value between its 0th default and nil
331 and reset the persistence."
332 (let* ((prefix (symbol-value (cvs-prefix-sym sym)))
333 (numarg (if (integerp arg) arg 0))
334 ;; (defs (cvs-flags-defaults prefix))
335 )
336
337 ;; set persistence if requested
338 (when (> (prefix-numeric-value arg) 9)
339 (setf (cvs-flags-persist prefix) t)
340 (setq numarg (mod numarg 10)))
341
342 ;; set the value
343 (set sym
344 (cond
345 ((null arg)
346 (setf (cvs-flags-persist prefix) nil)
347 (unless (symbol-value sym) (nth 0 (cvs-flags-defaults prefix))))
348
349 ((or (consp arg) (< numarg 0))
350 (setf (nth (- numarg) (cvs-flags-defaults prefix))
351 (cvs-query-read (nth (- numarg) (cvs-flags-defaults prefix))
352 (format "%s: " (cvs-flags-desc prefix))
353 (cvs-flags-qtypedesc prefix)
354 (cvs-flags-hist-sym prefix))))
355 (t (nth numarg (cvs-flags-defaults prefix)))))
356 (force-mode-line-update)))
357
358 (defun cvs-prefix-get (sym &optional read-only)
359 "Return the current value of the prefix SYM.
360 And reset it unless READ-ONLY is non-nil."
361 (prog1 (symbol-value sym)
362 (unless (or read-only
363 (cvs-flags-persist (symbol-value (cvs-prefix-sym sym))))
364 (set sym nil)
365 (force-mode-line-update))))
366
367 (provide 'pcvs-util)
368
369 ;; arch-tag: 3b2588bb-2ae3-4f1f-bf5b-dea91b1f8a59
370 ;;; pcvs-util.el ends here