Commit | Line | Data |
---|---|---|
3afbc435 | 1 | ;;; pcvs-util.el --- utility functions for PCL-CVS -*- byte-compile-dynamic: t -*- |
5b467bf4 | 2 | |
ac62b9e4 | 3 | ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
d7a0267c | 4 | ;; 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc. |
5b467bf4 | 5 | |
cc1eecfd | 6 | ;; Author: Stefan Monnier <monnier@iro.umontreal.ca> |
5b467bf4 | 7 | ;; Keywords: pcl-cvs |
5b467bf4 SM |
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 | |
b4aa6026 | 13 | ;; the Free Software Foundation; either version 3, or (at your option) |
5b467bf4 SM |
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 | |
086add15 LK |
23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 | ;; Boston, MA 02110-1301, USA. | |
5b467bf4 SM |
25 | |
26 | ;;; Commentary: | |
27 | ||
28 | ||
29 | ;;; Code: | |
30 | ||
31 | (eval-when-compile (require 'cl)) | |
32 | ||
33 | ;;;; | |
34 | ;;;; list processing | |
80786c0a | 35 | ;;;; |
5b467bf4 SM |
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))))) | |
f1180544 | 52 | |
5b467bf4 SM |
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 | ||
178917f7 SM |
79 | ;;; |
80 | ;;; frame, window, buffer handling | |
81 | ;;; | |
5b467bf4 SM |
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 | |
178917f7 | 86 | try to split a new window instead." |
5b467bf4 SM |
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)))) | |
931a3331 RS |
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 | ))) | |
5b467bf4 SM |
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)))))) | |
f1180544 | 121 | |
5b467bf4 SM |
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." | |
c6dbae47 SM |
129 | (or (and (not (file-name-absolute-p name)) |
130 | (if noreuse (generate-new-buffer name) | |
131 | (get-buffer-create name))) | |
5b467bf4 SM |
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 | ||
80786c0a SM |
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. | |
0c8f5edd | 155 | ;; Windows can't show less than 3 lines anyway. |
80786c0a SM |
156 | (max 1 (/ (length strings) 2)))) |
157 | (colwidth (/ wwidth columns))) | |
0c8f5edd | 158 | ;; Use tab-width rather than indent-to. |
80786c0a SM |
159 | (setq tab-width colwidth) |
160 | ;; The insertion should be "sensible" no matter what choices were made. | |
161 | (dolist (str strings) | |
1c50e1e5 SM |
162 | (unless (bolp) |
163 | (insert " \t") | |
164 | (when (< wwidth (+ (max colwidth (length str)) (current-column))) | |
165 | (delete-char -2) (insert "\n"))) | |
166 | (insert str))))) | |
80786c0a SM |
167 | |
168 | ||
5b467bf4 SM |
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." | |
6d57b1a3 SM |
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))) | |
5b467bf4 SM |
184 | |
185 | (defun cvs-string-prefix-p (str1 str2) | |
186 | "Tell whether STR1 is a prefix of STR2." | |
ac62b9e4 | 187 | (eq t (compare-strings str2 nil (length str1) str1 nil nil))) |
5b467bf4 | 188 | |
f1180544 | 189 | ;;;; |
5b467bf4 | 190 | ;;;; file names |
f1180544 | 191 | ;;;; |
5b467bf4 SM |
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 | |
517e7169 RS |
214 | (cvs-qtypedesc-create 'split-string-and-unquote |
215 | 'combine-and-quote-strings nil)) | |
5b467bf4 SM |
216 | |
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))))) | |
231 | ||
f1180544 | 232 | ;;;; |
5b467bf4 | 233 | ;;;; Flags handling |
f1180544 | 234 | ;;;; |
5b467bf4 SM |
235 | |
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) | |
241 | ||
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)) | |
252 | ||
253 | (defun cvs-flags-query (sym &optional desc arg) | |
254 | "Query flags based on SYM. | |
e05a39ba | 255 | Optional argument DESC will be used for the prompt. |
5b467bf4 SM |
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)) | |
276 | ||
277 | ;; sanity check | |
278 | (unless (< (abs numarg) (length defaults)) | |
3afbc435 | 279 | (error "There is no %sth default" (abs numarg))) |
5b467bf4 SM |
280 | |
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)) | |
287 | fs) | |
288 | (nth numarg defaults)))) | |
289 | ||
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)) | |
293 | ||
f1180544 | 294 | ;;;; |
5b467bf4 | 295 | ;;;; Prefix keys |
f1180544 | 296 | ;;;; |
5b467bf4 SM |
297 | |
298 | (defconst cvs-prefix-number 10) | |
299 | ||
300 | (defsubst cvs-prefix-sym (sym) (intern (concat (symbol-name sym) "-cps"))) | |
301 | ||
302 | (defmacro cvs-prefix-define (sym docstring desc defaults | |
303 | &optional qtypedesc hist-sym) | |
304 | (let ((cps (cvs-prefix-sym sym))) | |
305 | `(progn | |
630784a2 | 306 | (defvar ,sym nil ,(concat (or docstring "") " |
5b467bf4 | 307 | See `cvs-prefix-set' for further description of the behavior.")) |
08171162 | 308 | (defvar ,cps |
5b467bf4 SM |
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) | |
dedffa6a | 314 | (nth 0 defaults))))) |
5b467bf4 SM |
315 | (-cvs-flags-make ,desc defaults ,qtypedesc ,hist-sym)))))) |
316 | ||
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))))) | |
321 | ||
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. | |
0ff9b955 | 331 | If ARG is nil toggle the PREFIX's value between its 0th default and nil |
5b467bf4 SM |
332 | and reset the persistence." |
333 | (let* ((prefix (symbol-value (cvs-prefix-sym sym))) | |
334 | (numarg (if (integerp arg) arg 0)) | |
1c50e1e5 SM |
335 | ;; (defs (cvs-flags-defaults prefix)) |
336 | ) | |
5b467bf4 SM |
337 | |
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))) | |
342 | ||
343 | ;; set the value | |
344 | (set sym | |
345 | (cond | |
346 | ((null arg) | |
347 | (setf (cvs-flags-persist prefix) nil) | |
dedffa6a | 348 | (unless (symbol-value sym) (nth 0 (cvs-flags-defaults prefix)))) |
5b467bf4 SM |
349 | |
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))) | |
358 | ||
359 | (defun cvs-prefix-get (sym &optional read-only) | |
360 | "Return the current value of the prefix SYM. | |
e05a39ba | 361 | And reset it unless READ-ONLY is non-nil." |
5b467bf4 SM |
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)))) | |
367 | ||
368 | (provide 'pcvs-util) | |
369 | ||
ac62b9e4 | 370 | ;; arch-tag: 3b2588bb-2ae3-4f1f-bf5b-dea91b1f8a59 |
3afbc435 | 371 | ;;; pcvs-util.el ends here |