10789
|
1 ;;; viper-util.el --- Utilities used by viper.el
|
|
2
|
|
3 ;; This file is part of GNU Emacs.
|
|
4
|
|
5 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
6 ;; it under the terms of the GNU General Public License as published by
|
|
7 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
8 ;; any later version.
|
|
9
|
|
10 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 ;; GNU General Public License for more details.
|
|
14
|
|
15 ;; You should have received a copy of the GNU General Public License
|
|
16 ;; along with GNU Emacs; see the file COPYING. If not, write to
|
|
17 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
18
|
|
19 (require 'ring)
|
|
20
|
|
21 (defconst vip-xemacs-p (string-match "\\(Lucid\\|Xemacs\\)" emacs-version)
|
|
22 "Whether it is XEmacs or not.")
|
|
23 (defconst vip-emacs-p (not vip-xemacs-p)
|
|
24 "Whether it is Emacs or not.")
|
|
25
|
|
26
|
|
27 ;;; Macros
|
|
28
|
|
29 (defmacro vip-deflocalvar (var default-value &optional documentation)
|
|
30 (` (progn
|
|
31 (defvar (, var) (, default-value)
|
|
32 (, (format "%s\n\(buffer local\)" documentation)))
|
|
33 (make-variable-buffer-local '(, var))
|
|
34 )))
|
|
35
|
|
36 (defmacro vip-loop (count body)
|
|
37 "(vip-loop COUNT BODY) Execute BODY COUNT times."
|
|
38 (list 'let (list (list 'count count))
|
|
39 (list 'while '(> count 0)
|
|
40 body
|
|
41 '(setq count (1- count))
|
|
42 )))
|
|
43
|
|
44 (defmacro vip-buffer-live-p (buf)
|
|
45 (` (and (, buf) (get-buffer (, buf)) (buffer-name (get-buffer (, buf))))))
|
|
46
|
|
47 ;; return buffer-specific macro definition, given a full macro definition
|
|
48 (defmacro vip-kbd-buf-alist (macro-elt)
|
|
49 (` (nth 1 (, macro-elt))))
|
|
50 ;; get a pair: (curr-buffer . macro-definition)
|
|
51 (defmacro vip-kbd-buf-pair (macro-elt)
|
|
52 (` (assoc (buffer-name) (vip-kbd-buf-alist (, macro-elt)))))
|
|
53 ;; get macro definition for current buffer
|
|
54 (defmacro vip-kbd-buf-definition (macro-elt)
|
|
55 (` (cdr (vip-kbd-buf-pair (, macro-elt)))))
|
|
56
|
|
57 ;; return mode-specific macro definitions, given a full macro definition
|
|
58 (defmacro vip-kbd-mode-alist (macro-elt)
|
|
59 (` (nth 2 (, macro-elt))))
|
|
60 ;; get a pair: (major-mode . macro-definition)
|
|
61 (defmacro vip-kbd-mode-pair (macro-elt)
|
|
62 (` (assoc major-mode (vip-kbd-mode-alist (, macro-elt)))))
|
|
63 ;; get macro definition for the current major mode
|
|
64 (defmacro vip-kbd-mode-definition (macro-elt)
|
|
65 (` (cdr (vip-kbd-mode-pair (, macro-elt)))))
|
|
66
|
|
67 ;; return global macro definition, given a full macro definition
|
|
68 (defmacro vip-kbd-global-pair (macro-elt)
|
|
69 (` (nth 3 (, macro-elt))))
|
|
70 ;; get global macro definition from an elt of macro-alist
|
|
71 (defmacro vip-kbd-global-definition (macro-elt)
|
|
72 (` (cdr (vip-kbd-global-pair (, macro-elt)))))
|
|
73
|
|
74 ;; last elt of a sequence
|
|
75 (defsubst vip-seq-last-elt (seq)
|
|
76 (elt seq (1- (length seq))))
|
|
77
|
|
78 ;; Check if arg is a valid character for register
|
|
79 ;; TYPE is a list that can contain `letter', `Letter', and `digit'.
|
|
80 ;; Letter means lowercase letters, Letter means uppercase letters, and
|
|
81 ;; digit means digits from 1 to 9.
|
|
82 ;; If TYPE is nil, then down/uppercase letters and digits are allowed.
|
|
83 (defun vip-valid-register (reg &optional type)
|
|
84 (or type (setq type '(letter Letter digit)))
|
|
85 (or (if (memq 'letter type)
|
|
86 (and (<= ?a reg) (<= reg ?z)))
|
|
87 (if (memq 'digit type)
|
|
88 (and (<= ?1 reg) (<= reg ?9)))
|
|
89 (if (memq 'Letter type)
|
|
90 (and (<= ?A reg) (<= reg ?Z)))
|
|
91 ))
|
|
92
|
|
93 (defun vip-valid-marker (marker)
|
|
94 (if (markerp marker)
|
|
95 (let ((buf (marker-buffer marker))
|
|
96 (pos (marker-position marker)))
|
|
97 (save-excursion
|
|
98 (set-buffer buf)
|
|
99 (and (<= pos (point-max)) (<= (point-min) pos))))))
|
|
100
|
|
101
|
|
102 (defvar vip-minibuffer-overlay-priority 300)
|
|
103 (defvar vip-replace-overlay-priority 400)
|
|
104 (defvar vip-search-overlay-priority 500)
|
|
105
|
|
106
|
|
107 ;;; XEmacs support
|
|
108
|
|
109 (if vip-xemacs-p
|
|
110 (progn
|
|
111 (fset 'vip-read-event (symbol-function 'next-command-event))
|
|
112 (fset 'vip-make-overlay (symbol-function 'make-extent))
|
|
113 (fset 'vip-overlay-start (symbol-function 'extent-start-position))
|
|
114 (fset 'vip-overlay-end (symbol-function 'extent-end-position))
|
|
115 (fset 'vip-overlay-put (symbol-function 'set-extent-property))
|
|
116 (fset 'vip-overlay-p (symbol-function 'extentp))
|
|
117 (fset 'vip-overlay-get (symbol-function 'extent-property))
|
|
118 (fset 'vip-move-overlay (symbol-function 'set-extent-endpoints))
|
|
119 (if window-system
|
|
120 (fset 'vip-iconify (symbol-function 'iconify-screen)))
|
|
121 (fset 'vip-raise-frame (symbol-function 'raise-screen))
|
|
122 (fset 'vip-window-frame (symbol-function 'window-screen))
|
|
123 (fset 'vip-select-frame (symbol-function 'select-screen))
|
|
124 (fset 'vip-selected-frame (symbol-function 'selected-screen))
|
|
125 (fset 'vip-frame-selected-window
|
|
126 (symbol-function 'screen-selected-window))
|
|
127 (fset 'vip-frame-parameters (symbol-function 'screen-parameters))
|
|
128 (fset 'vip-modify-frame-parameters
|
|
129 (symbol-function 'modify-screen-parameters))
|
|
130 (cond (window-system
|
|
131 (fset 'vip-get-face (symbol-function 'get-face))
|
|
132 (fset 'vip-color-defined-p
|
|
133 (symbol-function 'x-valid-color-name-p))
|
|
134 (fset 'vip-display-color-p
|
|
135 (symbol-function 'x-color-display-p)))))
|
|
136 (fset 'vip-read-event (symbol-function 'read-event))
|
|
137 (fset 'vip-make-overlay (symbol-function 'make-overlay))
|
|
138 (fset 'vip-overlay-start (symbol-function 'overlay-start))
|
|
139 (fset 'vip-overlay-end (symbol-function 'overlay-end))
|
|
140 (fset 'vip-overlay-put (symbol-function 'overlay-put))
|
|
141 (fset 'vip-overlay-p (symbol-function 'overlayp))
|
|
142 (fset 'vip-overlay-get (symbol-function 'overlay-get))
|
|
143 (fset 'vip-move-overlay (symbol-function 'move-overlay))
|
|
144 (if window-system
|
|
145 (fset 'vip-iconify (symbol-function 'iconify-or-deiconify-frame)))
|
|
146 (fset 'vip-raise-frame (symbol-function 'raise-frame))
|
|
147 (fset 'vip-window-frame (symbol-function 'window-frame))
|
|
148 (fset 'vip-select-frame (symbol-function 'select-frame))
|
|
149 (fset 'vip-selected-frame (symbol-function 'selected-frame))
|
|
150 (fset 'vip-frame-selected-window (symbol-function 'frame-selected-window))
|
|
151 (fset 'vip-frame-parameters (symbol-function 'frame-parameters))
|
|
152 (fset 'vip-modify-frame-parameters
|
|
153 (symbol-function 'modify-frame-parameters))
|
|
154 (cond (window-system
|
|
155 (fset 'vip-get-face (symbol-function 'internal-get-face))
|
|
156 (fset 'vip-color-defined-p (symbol-function 'x-color-defined-p))
|
|
157 (fset 'vip-display-color-p (symbol-function 'x-display-color-p)))))
|
|
158
|
|
159 ;; OS/2
|
|
160 (cond ((eq window-system 'pm)
|
|
161 (fset 'vip-color-defined-p
|
|
162 (function (lambda (color) (assoc color pm-color-alist))))))
|
|
163
|
|
164 ;; needed to smooth out the difference between Emacs and XEmacs
|
|
165 (defsubst vip-italicize-face (face)
|
|
166 (if vip-xemacs-p
|
|
167 (make-face-italic face)
|
|
168 (make-face-italic face nil 'noerror)))
|
|
169
|
|
170 ;; test if display is color and the colors are defined
|
|
171 (defsubst vip-can-use-colors (&rest colors)
|
|
172 (if (vip-display-color-p)
|
|
173 (not (memq nil (mapcar 'vip-color-defined-p colors)))
|
|
174 ))
|
|
175
|
|
176 ;; currently doesn't work for XEmacs
|
|
177 (defun vip-change-cursor-color (new-color)
|
|
178 (if (and window-system (vip-display-color-p)
|
|
179 (stringp new-color) (vip-color-defined-p new-color))
|
|
180 (vip-modify-frame-parameters
|
|
181 (vip-selected-frame) (list (cons 'cursor-color new-color)))))
|
|
182
|
|
183 (defsubst vip-save-cursor-color ()
|
|
184 (if (and window-system (vip-display-color-p))
|
|
185 (let ((color (cdr (assoc 'cursor-color (vip-frame-parameters)))))
|
|
186 (if (and (stringp color) (vip-color-defined-p color)
|
|
187 (not (string= color vip-replace-overlay-cursor-color)))
|
|
188 (vip-overlay-put vip-replace-overlay 'vip-cursor-color color)))))
|
|
189
|
|
190 (defsubst vip-restore-cursor-color ()
|
|
191 (vip-change-cursor-color
|
|
192 (vip-overlay-get vip-replace-overlay 'vip-cursor-color)))
|
|
193
|
|
194
|
|
195 ;; Check the current version against the major and minor version numbers
|
|
196 ;; using op: cur-vers op major.minor If emacs-major-version or
|
|
197 ;; emacs-minor-version are not defined, we assume that the current version
|
|
198 ;; is hopelessly outdated. We assume that emacs-major-version and
|
|
199 ;; emacs-minor-version are defined. Otherwise, for Emacs/XEmacs 19, if the
|
|
200 ;; current minor version is < 10 (xemacs) or < 23 (emacs) the return value
|
|
201 ;; will be nil (when op is =, >, or >=) and t (when op is <, <=), which may be
|
|
202 ;; incorrect. However, this gives correct result in our cases, since we are
|
|
203 ;; testing for sufficiently high Emacs versions.
|
|
204 (defun vip-check-version (op major minor &optional type-of-emacs)
|
|
205 (if (and (boundp 'emacs-major-version) (boundp 'emacs-minor-version))
|
|
206 (and (cond ((eq type-of-emacs 'xemacs) vip-xemacs-p)
|
|
207 ((eq type-of-emacs 'emacs) vip-emacs-p)
|
|
208 (t t))
|
|
209 (cond ((eq op '=) (and (= emacs-minor-version minor)
|
|
210 (= emacs-major-version major)))
|
|
211 ((memq op '(> >= < <=))
|
|
212 (and (or (funcall op emacs-major-version major)
|
|
213 (= emacs-major-version major))
|
|
214 (if (= emacs-major-version major)
|
|
215 (funcall op emacs-minor-version minor)
|
|
216 t)))
|
|
217 (t
|
|
218 (error "%S: Invalid op in vip-check-version" op))))
|
|
219 (cond ((memq op '(= > >=)) nil)
|
|
220 ((memq op '(< <=)) t))))
|
|
221
|
|
222
|
|
223 ;; Early versions of XEmacs didn't have window-live-p (or it didn't work right)
|
|
224 (if (vip-check-version '< 19 11 'xemacs)
|
|
225 (defun window-live-p (win)
|
|
226 (let ((visible nil))
|
|
227 (walk-windows
|
|
228 '(lambda (walk-win)
|
|
229 (if(equal walk-win win)
|
|
230 (setq visible t)))
|
|
231 nil 'all-screens)
|
|
232 visible))
|
|
233 )
|
|
234
|
|
235
|
|
236 (defun vip-get-visible-buffer-window (wind)
|
|
237 (if vip-xemacs-p
|
|
238 (get-buffer-window wind t)
|
|
239 (get-buffer-window wind 'visible)))
|
|
240
|
|
241
|
|
242 (defun vip-line-pos (pos)
|
|
243 "Return line position.
|
|
244 If pos is 'start then returns position of line start.
|
|
245 If pos is 'end, returns line end. If pos is 'mid, returns line center.
|
|
246 Pos = 'indent returns beginning of indentation.
|
|
247 Otherwise, returns point. Current point is not moved in any case."
|
|
248 (let ((cur-pos (point))
|
|
249 (result))
|
|
250 (cond
|
|
251 ((equal pos 'start)
|
|
252 (beginning-of-line))
|
|
253 ((equal pos 'end)
|
|
254 (end-of-line))
|
|
255 ((equal pos 'mid)
|
|
256 (goto-char (+ (vip-line-pos 'start) (vip-line-pos 'end) 2)))
|
|
257 ((equal pos 'indent)
|
|
258 (back-to-indentation))
|
|
259 (t nil))
|
|
260 (setq result (point))
|
|
261 (goto-char cur-pos)
|
|
262 result))
|
|
263
|
|
264
|
|
265 (defun vip-move-marker-locally (var pos &optional buffer)
|
|
266 "Like move-marker but creates a virgin marker if arg isn't already a marker.
|
|
267 The first argument must eval to a variable name.
|
|
268 Arguments: (var-name position &optional buffer).
|
|
269
|
|
270 This is useful for moving markers that are supposed to be local.
|
|
271 For this, VAR-NAME should be made buffer-local with nil as a default.
|
|
272 Then, each time this var is used in `vip-move-marker-locally' in a new
|
|
273 buffer, a new marker will be created."
|
|
274 (if (markerp (eval var))
|
|
275 ()
|
|
276 (set var (make-marker)))
|
|
277 (move-marker (eval var) pos buffer))
|
|
278
|
|
279
|
|
280 (defun vip-message-conditions (conditions)
|
|
281 "Print CONDITIONS as a message."
|
|
282 (let ((case (car conditions)) (msg (cdr conditions)))
|
|
283 (if (null msg)
|
|
284 (message "%s" case)
|
|
285 (message "%s: %s" case (mapconcat 'prin1-to-string msg " ")))
|
|
286 (beep 1)))
|
|
287
|
|
288
|
|
289 ;;; List/alist utilities
|
|
290
|
|
291 (defun vip-list-to-alist (lst)
|
|
292 "Convert LIST to an alist."
|
|
293 (let ((alist))
|
|
294 (while lst
|
|
295 (setq alist (cons (list (car lst)) alist))
|
|
296 (setq lst (cdr lst)))
|
|
297 alist))
|
|
298
|
|
299 (defun vip-alist-to-list (alst)
|
|
300 "Convert ALIST to a list."
|
|
301 (let ((lst))
|
|
302 (while alst
|
|
303 (setq lst (cons (car (car alst)) lst))
|
|
304 (setq alst (cdr alst)))
|
|
305 lst))
|
|
306
|
|
307 (defun vip-filter-alist (regexp alst)
|
|
308 "Filter ALIST using REGEXP. Return alist whose elements match the regexp."
|
|
309 (interactive "s x")
|
|
310 (let ((outalst) (inalst alst))
|
|
311 (while (car inalst)
|
|
312 (if (string-match regexp (car (car inalst)))
|
|
313 (setq outalst (cons (car inalst) outalst)))
|
|
314 (setq inalst (cdr inalst)))
|
|
315 outalst))
|
|
316
|
|
317 (defun vip-filter-list (regexp lst)
|
|
318 "Filter LIST using REGEXP. Return list whose elements match the regexp."
|
|
319 (interactive "s x")
|
|
320 (let ((outlst) (inlst lst))
|
|
321 (while (car inlst)
|
|
322 (if (string-match regexp (car inlst))
|
|
323 (setq outlst (cons (car inlst) outlst)))
|
|
324 (setq inlst (cdr inlst)))
|
|
325 outlst))
|
|
326
|
|
327
|
|
328 ;; Append LIS2 to LIS1, both alists, by side-effect and returns LIS1
|
|
329 ;; LIS2 is modified by filtering it: deleting its members of the form
|
|
330 ;; \(car elt\) such that (car elt') is in LIS1.
|
|
331 (defun vip-append-filter-alist (lis1 lis2)
|
|
332 (let ((temp lis1)
|
|
333 elt)
|
|
334
|
|
335 ;;filter-append the second list
|
|
336 (while temp
|
|
337 ;; delete all occurrences
|
|
338 (while (setq elt (assoc (car (car temp)) lis2))
|
|
339 (setq lis2 (delq elt lis2)))
|
|
340 (setq temp (cdr temp)))
|
|
341
|
|
342 (nconc lis1 lis2)))
|
|
343
|
|
344
|
|
345
|
|
346
|
|
347 ;;; Insertion ring
|
|
348
|
|
349 ;; Rotate RING's index. DIRection can be positive or negative.
|
|
350 (defun vip-ring-rotate1 (ring dir)
|
|
351 (if (and (ring-p ring) (> (ring-length ring) 0))
|
|
352 (progn
|
|
353 (setcar ring (cond ((> dir 0)
|
|
354 (ring-plus1 (car ring) (ring-length ring)))
|
|
355 ((< dir 0)
|
|
356 (ring-minus1 (car ring) (ring-length ring)))
|
|
357 ;; don't rotate if dir = 0
|
|
358 (t (car ring))))
|
|
359 (vip-current-ring-item ring)
|
|
360 )))
|
|
361
|
|
362 (defun vip-special-ring-rotate1 (ring dir)
|
|
363 (if (memq vip-intermediate-command
|
|
364 '(repeating-display-destructive-command
|
|
365 repeating-insertion-from-ring))
|
|
366 (vip-ring-rotate1 ring dir)
|
|
367 ;; don't rotate otherwise
|
|
368 (vip-ring-rotate1 ring 0)))
|
|
369
|
|
370 ;; current ring item; if N is given, then so many items back from the
|
|
371 ;; current
|
|
372 (defun vip-current-ring-item (ring &optional n)
|
|
373 (setq n (or n 0))
|
|
374 (if (and (ring-p ring) (> (ring-length ring) 0))
|
|
375 (aref (cdr (cdr ring)) (mod (- (car ring) 1 n) (ring-length ring)))))
|
|
376
|
|
377 ;; push item onto ring. the second argument is a ring-variable, not value.
|
|
378 (defun vip-push-onto-ring (item ring-var)
|
|
379 (or (ring-p (eval ring-var))
|
|
380 (set ring-var (make-ring (eval (intern (format "%S-size" ring-var))))))
|
|
381 (or (null item) ; don't push nil
|
|
382 (and (stringp item) (string= item "")) ; or empty strings
|
|
383 (equal item (vip-current-ring-item (eval ring-var))) ; or old stuff
|
|
384 ;; Since vip-set-destructive-command checks if we are inside vip-repeat,
|
|
385 ;; we don't check whether this-command-keys is a `.'.
|
|
386 ;; The cmd vip-repeat makes a call to the current function only if
|
|
387 ;; `.' is executing a command from the command history. It doesn't
|
|
388 ;; call the push-onto-ring function if `.' is simply repeating the
|
|
389 ;; last destructive command.
|
|
390 ;; We only check for ESC (which happens when we do insert with a
|
|
391 ;; prefix argument, or if this-command-keys doesn't give anything
|
|
392 ;; meaningful (in that case we don't know what to show to the user).
|
|
393 (and (eq ring-var 'vip-command-ring)
|
|
394 (string-match "\\([0-9]*\e\\|^[ \t]*$\\|escape\\)"
|
|
395 (vip-array-to-string (this-command-keys))))
|
|
396 (vip-ring-insert (eval ring-var) item))
|
|
397 )
|
|
398
|
|
399
|
|
400 ;; removing elts from ring seems to break it
|
|
401 (defun vip-cleanup-ring (ring)
|
|
402 (or (< (ring-length ring) 2)
|
|
403 (null (vip-current-ring-item ring))
|
|
404 ;; last and previous equal
|
|
405 (if (equal (vip-current-ring-item ring) (vip-current-ring-item ring 1))
|
|
406 (vip-ring-pop ring))))
|
|
407
|
|
408 ;; ring-remove seems to be buggy, so we concocted this for our purposes.
|
|
409 (defun vip-ring-pop (ring)
|
|
410 (let* ((ln (ring-length ring))
|
|
411 (vec (cdr (cdr ring)))
|
|
412 (veclen (length vec))
|
|
413 (hd (car ring))
|
|
414 (idx (max 0 (ring-minus1 hd ln)))
|
|
415 (top-elt (aref vec idx)))
|
|
416
|
|
417 ;; shift elements
|
|
418 (while (< (1+ idx) veclen)
|
|
419 (aset vec idx (aref vec (1+ idx)))
|
|
420 (setq idx (1+ idx)))
|
|
421 (aset vec idx nil)
|
|
422
|
|
423 (setq hd (max 0 (ring-minus1 hd ln)))
|
|
424 (if (= hd (1- ln)) (setq hd 0))
|
|
425 (setcar ring hd) ; move head
|
|
426 (setcar (cdr ring) (max 0 (1- ln))) ; adjust length
|
|
427 top-elt
|
|
428 ))
|
|
429
|
|
430 (defun vip-ring-insert (ring item)
|
|
431 (let* ((ln (ring-length ring))
|
|
432 (vec (cdr (cdr ring)))
|
|
433 (veclen (length vec))
|
|
434 (hd (car ring))
|
|
435 (vecpos-after-hd (if (= hd 0) ln hd))
|
|
436 (idx ln))
|
|
437
|
|
438 (if (= ln veclen)
|
|
439 (progn
|
|
440 (aset vec hd item) ; hd is always 1+ the actual head index in vec
|
|
441 (setcar ring (ring-plus1 hd ln)))
|
|
442 (setcar (cdr ring) (1+ ln))
|
|
443 (setcar ring (ring-plus1 vecpos-after-hd (1+ ln)))
|
|
444 (while (and (>= idx vecpos-after-hd) (> ln 0))
|
|
445 (aset vec idx (aref vec (1- idx)))
|
|
446 (setq idx (1- idx)))
|
|
447 (aset vec vecpos-after-hd item))
|
|
448 item))
|
|
449
|
|
450
|
|
451 ;;; String utilities
|
|
452
|
|
453 ;; If STRING is longer than MAX-LEN, truncate it and print ...... instead
|
|
454 ;; PRE-STRING is a string to prepend to the abbrev string.
|
|
455 ;; POST-STRING is a string to append to the abbrev string.
|
|
456 ;; ABBREV_SIGN is a string to be inserted before POST-STRING
|
|
457 ;; if the orig string was truncated.
|
|
458 (defun vip-abbreviate-string (string max-len
|
|
459 pre-string post-string abbrev-sign)
|
|
460 (let (truncated-str)
|
|
461 (setq truncated-str
|
|
462 (if (stringp string)
|
|
463 (substring string 0 (min max-len (length string)))))
|
|
464 (cond ((null truncated-str) "")
|
|
465 ((> (length string) max-len)
|
|
466 (format "%s%s%s%s"
|
|
467 pre-string truncated-str abbrev-sign post-string))
|
|
468 (t (format "%s%s%s" pre-string truncated-str post-string)))))
|
|
469
|
|
470
|
|
471 ;;; Saving settings in custom file
|
|
472
|
|
473 (defun vip-save-setting (var message custom-file &optional erase-msg)
|
|
474 "Save the current setting of VAR in CUSTOM-FILE.
|
|
475 If given, MESSAGE is a message to be displayed after that.
|
|
476 This message is erased after 2 secs, if erase-msg is non-nil.
|
|
477 Arguments: (vip-save-setting var message custom-file &optional erase-message)"
|
|
478 (let* ((var-name (symbol-name var))
|
|
479 (var-val (if (boundp var) (eval var)))
|
|
480 (regexp (format "^[^;]*%s[ \t\n]*[a-zA-Z---_']*[ \t\n)]" var-name))
|
|
481 (buf (find-file-noselect (substitute-in-file-name custom-file)))
|
|
482 )
|
|
483 (message message)
|
|
484 (save-excursion
|
|
485 (set-buffer buf)
|
|
486 (goto-char (point-min))
|
|
487 (if (re-search-forward regexp nil t)
|
|
488 (let ((reg-end (1- (match-end 0))))
|
|
489 (search-backward var-name)
|
|
490 (delete-region (match-beginning 0) reg-end)
|
|
491 (goto-char (match-beginning 0))
|
|
492 (insert (format "%s '%S" var-name var-val)))
|
|
493 (goto-char (point-max))
|
|
494 (if (not (bolp)) (insert "\n"))
|
|
495 (insert (format "(setq %s '%S)\n" var-name var-val)))
|
|
496 (save-buffer))
|
|
497 (kill-buffer buf)
|
|
498 (if erase-msg
|
|
499 (progn
|
|
500 (sit-for 2)
|
|
501 (message "")))
|
|
502 ))
|
|
503
|
|
504 ;; Save STRING in CUSTOM-FILE. If PATTERN is non-nil, remove strings that
|
|
505 ;; match this pattern.
|
|
506 (defun vip-save-string-in-file (string custom-file &optional pattern)
|
|
507 (let ((buf (find-file-noselect (substitute-in-file-name custom-file))))
|
|
508 (save-excursion
|
|
509 (set-buffer buf)
|
|
510 (goto-char (point-min))
|
|
511 (if pattern (delete-matching-lines pattern))
|
|
512 (goto-char (point-max))
|
|
513 (if string (insert string))
|
|
514 (save-buffer))
|
|
515 (kill-buffer buf)
|
|
516 ))
|
|
517
|
|
518
|
|
519 ;;; Overlays
|
|
520
|
|
521 ;; Search
|
|
522
|
|
523 (defun vip-flash-search-pattern ()
|
|
524 (if (vip-overlay-p vip-search-overlay)
|
|
525 (vip-move-overlay vip-search-overlay (match-beginning 0) (match-end 0))
|
|
526 (setq vip-search-overlay
|
|
527 (vip-make-overlay
|
|
528 (match-beginning 0) (match-end 0) (current-buffer))))
|
|
529
|
|
530 (vip-overlay-put vip-search-overlay 'priority vip-search-overlay-priority)
|
|
531 (if window-system
|
|
532 (progn
|
|
533 (vip-overlay-put vip-search-overlay 'face vip-search-face)
|
|
534 (sit-for 2)
|
|
535 (vip-overlay-put vip-search-overlay 'face nil))))
|
|
536
|
|
537 ;; Replace state
|
|
538
|
|
539 (defun vip-set-replace-overlay (beg end)
|
|
540 (if (vip-overlay-p vip-replace-overlay)
|
|
541 (vip-move-replace-overlay beg end)
|
|
542 (setq vip-replace-overlay (vip-make-overlay beg end (current-buffer)))
|
|
543 (vip-overlay-put vip-replace-overlay
|
|
544 'vip-start
|
|
545 (move-marker (make-marker)
|
|
546 (vip-overlay-start vip-replace-overlay)))
|
|
547 (vip-overlay-put vip-replace-overlay
|
|
548 'vip-end
|
|
549 (move-marker (make-marker)
|
|
550 (vip-overlay-end vip-replace-overlay)))
|
|
551 (vip-overlay-put
|
|
552 vip-replace-overlay 'priority vip-replace-overlay-priority))
|
|
553 (if window-system
|
|
554 (vip-overlay-put vip-replace-overlay 'face vip-replace-overlay-face))
|
|
555 (vip-save-cursor-color)
|
|
556 (vip-change-cursor-color vip-replace-overlay-cursor-color)
|
|
557 )
|
|
558
|
|
559
|
|
560 (defsubst vip-hide-replace-overlay ()
|
|
561 (vip-restore-cursor-color)
|
|
562 (if window-system
|
|
563 (vip-overlay-put vip-replace-overlay 'face nil)))
|
|
564
|
|
565
|
|
566
|
|
567 (defsubst vip-replace-start ()
|
|
568 (vip-overlay-get vip-replace-overlay 'vip-start))
|
|
569 (defsubst vip-replace-end ()
|
|
570 (vip-overlay-get vip-replace-overlay 'vip-end))
|
|
571
|
|
572 (defsubst vip-move-replace-overlay (beg end)
|
|
573 (vip-move-overlay vip-replace-overlay beg end)
|
|
574 (move-marker (vip-replace-start) (vip-overlay-start vip-replace-overlay))
|
|
575 (move-marker (vip-replace-end) (vip-overlay-end vip-replace-overlay)))
|
|
576
|
|
577
|
|
578 ;; Minibuffer
|
|
579
|
|
580 (defun vip-set-minibuffer-overlay ()
|
|
581 (vip-check-minibuffer-overlay)
|
|
582 ;; We always move the minibuffer overlay, since in XEmacs
|
|
583 ;; this overlay may get detached. Moving will reattach it.
|
|
584 ;; This overlay is also moved via the post-command-hook,
|
|
585 ;; to insure taht it covers the whole minibuffer.
|
|
586 (vip-move-minibuffer-overlay)
|
|
587 (if window-system
|
|
588 (progn
|
|
589 (vip-overlay-put
|
|
590 vip-minibuffer-overlay 'face vip-minibuffer-current-face)
|
|
591 (vip-overlay-put
|
|
592 vip-minibuffer-overlay 'priority vip-minibuffer-overlay-priority))
|
|
593 ))
|
|
594
|
|
595 (defun vip-check-minibuffer-overlay ()
|
|
596 (if (vip-overlay-p vip-minibuffer-overlay)
|
|
597 ()
|
|
598 (setq vip-minibuffer-overlay
|
|
599 (vip-make-overlay 1 (1+ (buffer-size)) (current-buffer)))))
|
|
600
|
|
601 ;; arguments to this function are dummies. they are needed just because
|
|
602 ;; it is used as a insert-in-front-hook to vip-minibuffer-overlay, and such
|
|
603 ;; hooks require 3 arguments.
|
|
604 (defun vip-move-minibuffer-overlay (&optional overl beg end)
|
|
605 (if (vip-is-in-minibuffer)
|
|
606 (progn
|
|
607 (vip-check-minibuffer-overlay)
|
|
608 (vip-move-overlay vip-minibuffer-overlay 1 (1+ (buffer-size))))))
|
|
609
|
|
610 (defsubst vip-is-in-minibuffer ()
|
|
611 (string-match "\*Minibuf-" (buffer-name)))
|
|
612
|
|
613
|
|
614
|
|
615 ;;; XEmacs compatibility
|
|
616
|
|
617 ;; Sit for VAL miliseconds. XEmacs doesn't support the milisecond arg to
|
|
618 ;; sit-for, so this is for compatibility.
|
|
619 (defsubst vip-sit-for-short (val &optional nodisp)
|
|
620 (if vip-xemacs-p
|
|
621 (sit-for (/ val 1000.0) nodisp)
|
|
622 (sit-for 0 val nodisp)))
|
|
623
|
|
624 ;; EVENT may be a single event of a sequence of events
|
|
625 (defsubst vip-ESC-event-p (event)
|
|
626 (let ((ESC-keys '(?\e (control \[) escape))
|
|
627 (key (vip-event-key event)))
|
|
628 (member key ESC-keys)))
|
|
629
|
|
630 ;; like (set-mark-command nil) but doesn't push twice, if (car mark-ring)
|
|
631 ;; is the same as (mark t).
|
|
632 (defsubst vip-set-mark-if-necessary ()
|
|
633 (setq mark-ring (delete (vip-mark-marker) mark-ring))
|
|
634 (set-mark-command nil))
|
|
635
|
|
636 (defsubst vip-mark-marker ()
|
|
637 (if vip-xemacs-p
|
|
638 (mark-marker t)
|
|
639 (mark-marker)))
|
|
640
|
|
641 ;; In transient mark mode (zmacs mode), it is annoying when regions become
|
|
642 ;; highlighted due to Viper's pushing marks. So, we deactivate marks, unless
|
|
643 ;; the user explicitly wants highlighting, e.g., by hitting '' or ``
|
|
644 (defun vip-deactivate-mark ()
|
|
645 (if vip-xemacs-p
|
|
646 (zmacs-deactivate-region)
|
|
647 (deactivate-mark)))
|
|
648
|
|
649
|
|
650 (defsubst vip-events-to-keys (events)
|
|
651 (cond (vip-xemacs-p (events-to-keys events))
|
|
652 (t events)))
|
|
653
|
|
654
|
|
655 (defun vip-eval-after-load (file form)
|
|
656 (if vip-emacs-p
|
|
657 (eval-after-load file form)
|
|
658 (or (assoc file after-load-alist)
|
|
659 (setq after-load-alist (cons (list file) after-load-alist)))
|
|
660 (let ((elt (assoc file after-load-alist)))
|
|
661 (or (member form (cdr elt))
|
|
662 (setq elt (nconc elt (list form)))))
|
|
663 form
|
|
664 ))
|
|
665
|
|
666
|
|
667 ;; like read-event, but in XEmacs also try to convert to char, if possible
|
|
668 (defun vip-read-event-convert-to-char ()
|
|
669 (let (event)
|
|
670 (if vip-emacs-p
|
|
671 (read-event)
|
|
672 (setq event (next-command-event))
|
|
673 (or (event-to-character event)
|
|
674 event))
|
|
675 ))
|
|
676
|
|
677
|
|
678 ;; Enacs has a bug in eventp, which causes (eventp nil) to return (nil)
|
|
679 ;; instead of nil, if '(nil) was previously inadvertantly assigned to
|
|
680 ;; unread-command-events
|
|
681 (defun vip-event-key (event)
|
|
682 (or (and event (eventp event))
|
|
683 (error "vip-event-key: Wrong type argument, eventp, %S" event))
|
|
684 (let ((mod (event-modifiers event))
|
|
685 basis)
|
|
686 (setq basis
|
|
687 (cond
|
|
688 (vip-xemacs-p
|
|
689 (cond ((key-press-event-p event)
|
|
690 (event-key event))
|
|
691 ((button-event-p event)
|
|
692 (concat "mouse-" (event-button event)))
|
|
693 (t
|
|
694 (error "vip-event-key: Unknown event, %S" event))))
|
|
695 (t
|
|
696 ;; Emacs doesn't handle capital letters correctly, since
|
|
697 ;; \S-a isn't considered the same as A (it behaves as
|
|
698 ;; plain `a' instead). So we take care of this here
|
|
699 (if (and (numberp event) (<= ?A event) (<= event ?Z))
|
|
700 (setq mod nil
|
|
701 event event)
|
|
702 (event-basic-type event)))))
|
|
703
|
|
704 (if (numberp basis)
|
|
705 (setq basis
|
|
706 (if (= basis ?\C-?)
|
|
707 (list 'control '\?) ; taking care of an emacs bug
|
|
708 (intern (char-to-string basis)))))
|
|
709
|
|
710 (if mod
|
|
711 (append mod (list basis))
|
|
712 basis)
|
|
713 ))
|
|
714
|
|
715 (defun vip-key-to-emacs-key (key)
|
|
716 (let (key-name char-p modifiers mod-char-list base-key base-key-name)
|
|
717 (cond (vip-xemacs-p key)
|
|
718 ((symbolp key)
|
|
719 (setq key-name (symbol-name key))
|
|
720 (if (= (length key-name) 1) ; character event
|
|
721 (string-to-char key-name)
|
|
722 key))
|
|
723 ((listp key)
|
|
724 (setq modifiers (subseq key 0 (1- (length key)))
|
|
725 base-key (vip-seq-last-elt key)
|
|
726 base-key-name (symbol-name base-key)
|
|
727 char-p (= (length base-key-name) 1))
|
|
728 (setq mod-char-list
|
|
729 (mapcar
|
|
730 '(lambda (elt) (upcase (substring (symbol-name elt) 0 1)))
|
|
731 modifiers))
|
|
732 (if char-p
|
|
733 (setq key-name
|
|
734 (car (read-from-string
|
|
735 (concat
|
|
736 "?\\"
|
|
737 (mapconcat 'identity mod-char-list "-\\")
|
|
738 "-"
|
|
739 base-key-name))))
|
|
740 (setq key-name
|
|
741 (intern
|
|
742 (concat
|
|
743 (mapconcat 'identity mod-char-list "-")
|
|
744 "-"
|
|
745 base-key-name))))))
|
|
746 ))
|
|
747
|
|
748
|
|
749 ;; Args can be a sequence of events, a string, or a Viper macro. Will try to
|
|
750 ;; convert events to keys and, if all keys are regular printable
|
|
751 ;; characters, will return a string. Otherwise, will return a string
|
|
752 ;; representing a vector of converted events. If the input was a Viper macro,
|
|
753 ;; will return a string that represents this macro as a vector.
|
|
754 (defun vip-array-to-string (event-seq &optional representation)
|
|
755 (let (temp)
|
|
756 (cond ((stringp event-seq) event-seq)
|
|
757 ((vip-event-vector-p event-seq)
|
|
758 (setq temp (mapcar 'vip-event-key event-seq))
|
|
759 (if (vip-char-symbol-sequence-p temp)
|
|
760 (mapconcat 'symbol-name temp "")
|
|
761 (prin1-to-string (vconcat temp))))
|
|
762 ((vip-char-symbol-sequence-p event-seq)
|
|
763 (mapconcat 'symbol-name event-seq ""))
|
|
764 (t (prin1-to-string event-seq)))))
|
|
765
|
|
766
|
|
767 (defsubst vip-fast-keysequence-p ()
|
|
768 (not (vip-sit-for-short vip-fast-keyseq-timeout t)))
|
|
769
|
|
770 (defun vip-read-char-exclusive ()
|
|
771 (let (char
|
|
772 (echo-keystrokes 1))
|
|
773 (while (null char)
|
|
774 (condition-case nil
|
|
775 (setq char (read-char))
|
|
776 (error
|
|
777 ;; skip event if not char
|
|
778 (vip-read-event))))
|
|
779 char))
|
|
780
|
|
781
|
|
782
|
|
783 (defun vip-setup-master-buffer (&rest other-files-or-buffers)
|
|
784 "Set up the current buffer as a master buffer.
|
|
785 Arguments become related buffers. This function should normally be used in
|
|
786 the `Local variables' section of a file."
|
|
787 (setq vip-related-files-and-buffers-ring
|
|
788 (make-ring (1+ (length other-files-or-buffers))))
|
|
789 (mapcar '(lambda (elt)
|
|
790 (vip-ring-insert vip-related-files-and-buffers-ring elt))
|
|
791 other-files-or-buffers)
|
|
792 (vip-ring-insert vip-related-files-and-buffers-ring (buffer-name))
|
|
793 )
|
|
794
|
|
795
|
|
796 (provide 'viper-util)
|
|
797
|
|
798 ;;; viper-util.el ends here
|