pcvs-util.el 12 KB

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