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