]> code.delx.au - gnu-emacs/blob - lisp/emulation/viper-util.el
Merge from emacs--rel--22
[gnu-emacs] / lisp / emulation / viper-util.el
1 ;;; viper-util.el --- Utilities used by viper.el
2
3 ;; Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (provide 'viper-util)
28
29
30 ;; Compiler pacifier
31 (defvar viper-overriding-map)
32 (defvar pm-color-alist)
33 (defvar viper-minibuffer-current-face)
34 (defvar viper-minibuffer-insert-face)
35 (defvar viper-minibuffer-vi-face)
36 (defvar viper-minibuffer-emacs-face)
37 (defvar viper-replace-overlay-face)
38 (defvar viper-fast-keyseq-timeout)
39 (defvar ex-unix-type-shell)
40 (defvar ex-unix-type-shell-options)
41 (defvar viper-ex-tmp-buf-name)
42 (defvar viper-syntax-preference)
43 (defvar viper-saved-mark)
44
45 (require 'ring)
46
47 (eval-and-compile
48 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
49
50 ;; end pacifier
51
52 (require 'viper-init)
53
54
55 ;; A fix for NeXT Step
56 ;; Should go away, when NS people fix the design flaw, which leaves the
57 ;; two x-* functions undefined.
58 (if (and (not (fboundp 'x-display-color-p)) (fboundp 'ns-display-color-p))
59 (fset 'x-display-color-p (symbol-function 'ns-display-color-p)))
60 (if (and (not (fboundp 'x-color-defined-p)) (fboundp 'ns-color-defined-p))
61 (fset 'x-color-defined-p (symbol-function 'ns-color-defined-p)))
62
63 \f
64 (defalias 'viper-overlay-p
65 (if (featurep 'xemacs) 'extentp 'overlayp))
66 (defalias 'viper-make-overlay
67 (if (featurep 'xemacs) 'make-extent 'make-overlay))
68 (defalias 'viper-overlay-live-p
69 (if (featurep 'xemacs) 'extent-live-p 'overlayp))
70 (defalias 'viper-move-overlay
71 (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay))
72 (defalias 'viper-overlay-start
73 (if (featurep 'xemacs) 'extent-start-position 'overlay-start))
74 (defalias 'viper-overlay-end
75 (if (featurep 'xemacs) 'extent-end-position 'overlay-end))
76 (defalias 'viper-overlay-get
77 (if (featurep 'xemacs) 'extent-property 'overlay-get))
78 (defalias 'viper-overlay-put
79 (if (featurep 'xemacs) 'set-extent-property 'overlay-put))
80 (defalias 'viper-read-event
81 (if (featurep 'xemacs) 'next-command-event 'read-event))
82 (defalias 'viper-characterp
83 (if (featurep 'xemacs) 'characterp 'integerp))
84 (defalias 'viper-int-to-char
85 (if (featurep 'xemacs) 'int-to-char 'identity))
86 (defalias 'viper-get-face
87 (if (featurep 'xemacs) 'get-face 'internal-get-face))
88 (defalias 'viper-color-defined-p
89 (if (featurep 'xemacs) 'valid-color-name-p 'x-color-defined-p))
90 (defalias 'viper-iconify
91 (if (featurep 'xemacs) 'iconify-frame 'iconify-or-deiconify-frame))
92
93
94 ;; CHAR is supposed to be a char or an integer (positive or negative)
95 ;; LIST is a list of chars, nil, and negative numbers
96 ;; Check if CHAR is a member by trying to convert in characters, if necessary.
97 ;; Introduced for compatibility with XEmacs, where integers are not the same as
98 ;; chars.
99 (defun viper-memq-char (char list)
100 (cond ((and (integerp char) (>= char 0))
101 (memq (viper-int-to-char char) list))
102 ((memq char list))))
103
104 ;; Check if char-or-int and char are the same as characters
105 (defun viper-char-equal (char-or-int char)
106 (cond ((and (integerp char-or-int) (>= char-or-int 0))
107 (= (viper-int-to-char char-or-int) char))
108 ((eq char-or-int char))))
109
110 ;; Like =, but accommodates null and also is t for eq-objects
111 (defun viper= (char char1)
112 (cond ((eq char char1) t)
113 ((and (viper-characterp char) (viper-characterp char1))
114 (= char char1))
115 (t nil)))
116
117 (defsubst viper-color-display-p ()
118 (if (featurep 'xemacs) (eq (device-class (selected-device)) 'color)
119 (x-display-color-p)))
120
121 (defun viper-get-cursor-color (&optional frame)
122 (if (featurep 'xemacs)
123 (color-instance-name
124 (frame-property (or frame (selected-frame)) 'cursor-color))
125 (cdr (assoc 'cursor-color (frame-parameters)))))
126
127 (defmacro viper-frame-value (variable)
128 "Return the value of VARIABLE local to the current frame, if there is one.
129 Otherwise return the normal value."
130 `(if (featurep 'xemacs)
131 ,variable
132 ;; Frame-local variables are obsolete from Emacs 22.2 onwards,
133 ;; so we do it by hand instead.
134 ;; Buffer-local values take precedence over frame-local ones.
135 (if (local-variable-p ',variable)
136 ,variable
137 ;; Distinguish between no frame parameter and a frame parameter
138 ;; with a value of nil.
139 (let ((fp (assoc ',variable (frame-parameters))))
140 (if fp (cdr fp)
141 ,variable)))))
142
143 ;; OS/2
144 (cond ((eq (viper-device-type) 'pm)
145 (fset 'viper-color-defined-p
146 (lambda (color) (assoc color pm-color-alist)))))
147
148
149 ;; cursor colors
150 (defun viper-change-cursor-color (new-color &optional frame)
151 (if (and (viper-window-display-p) (viper-color-display-p)
152 (stringp new-color) (viper-color-defined-p new-color)
153 (not (string= new-color (viper-get-cursor-color))))
154 (if (featurep 'xemacs)
155 (set-frame-property
156 (or frame (selected-frame))
157 'cursor-color (make-color-instance new-color))
158 (modify-frame-parameters
159 (or frame (selected-frame))
160 (list (cons 'cursor-color new-color))))))
161
162 ;; Note that the colors this function uses might not be those
163 ;; associated with FRAME, if there are frame-local values.
164 ;; This was equally true before the advent of viper-frame-value.
165 ;; Now it could be changed by passing frame to v-f-v.
166 (defun viper-set-cursor-color-according-to-state (&optional frame)
167 (cond ((eq viper-current-state 'replace-state)
168 (viper-change-cursor-color
169 (viper-frame-value viper-replace-overlay-cursor-color)
170 frame))
171 ((and (eq viper-current-state 'emacs-state)
172 (viper-frame-value viper-emacs-state-cursor-color))
173 (viper-change-cursor-color
174 (viper-frame-value viper-emacs-state-cursor-color)
175 frame))
176 ((eq viper-current-state 'insert-state)
177 (viper-change-cursor-color
178 (viper-frame-value viper-insert-state-cursor-color)
179 frame))
180 (t
181 (viper-change-cursor-color
182 (viper-frame-value viper-vi-state-cursor-color)
183 frame))))
184
185 ;; By default, saves current frame cursor color in the
186 ;; viper-saved-cursor-color-in-replace-mode property of viper-replace-overlay
187 (defun viper-save-cursor-color (before-which-mode)
188 (if (and (viper-window-display-p) (viper-color-display-p))
189 (let ((color (viper-get-cursor-color)))
190 (if (and (stringp color) (viper-color-defined-p color)
191 (not (string= color
192 (viper-frame-value
193 viper-replace-overlay-cursor-color))))
194 (modify-frame-parameters
195 (selected-frame)
196 (list
197 (cons
198 (cond ((eq before-which-mode 'before-replace-mode)
199 'viper-saved-cursor-color-in-replace-mode)
200 ((eq before-which-mode 'before-emacs-mode)
201 'viper-saved-cursor-color-in-emacs-mode)
202 (t
203 'viper-saved-cursor-color-in-insert-mode))
204 color)))))))
205
206
207 (defsubst viper-get-saved-cursor-color-in-replace-mode ()
208 (or
209 (funcall
210 (if (featurep 'emacs) 'frame-parameter 'frame-property)
211 (selected-frame)
212 'viper-saved-cursor-color-in-replace-mode)
213 (or (and (eq viper-current-state 'emacs-mode)
214 (viper-frame-value viper-emacs-state-cursor-color))
215 (viper-frame-value viper-vi-state-cursor-color))))
216
217 (defsubst viper-get-saved-cursor-color-in-insert-mode ()
218 (or
219 (funcall
220 (if (featurep 'emacs) 'frame-parameter 'frame-property)
221 (selected-frame)
222 'viper-saved-cursor-color-in-insert-mode)
223 (or (and (eq viper-current-state 'emacs-mode)
224 (viper-frame-value viper-emacs-state-cursor-color))
225 (viper-frame-value viper-vi-state-cursor-color))))
226
227 (defsubst viper-get-saved-cursor-color-in-emacs-mode ()
228 (or
229 (funcall
230 (if (featurep 'emacs) 'frame-parameter 'frame-property)
231 (selected-frame)
232 'viper-saved-cursor-color-in-emacs-mode)
233 (viper-frame-value viper-vi-state-cursor-color)))
234
235 ;; restore cursor color from replace overlay
236 (defun viper-restore-cursor-color(after-which-mode)
237 (if (viper-overlay-p viper-replace-overlay)
238 (viper-change-cursor-color
239 (cond ((eq after-which-mode 'after-replace-mode)
240 (viper-get-saved-cursor-color-in-replace-mode))
241 ((eq after-which-mode 'after-emacs-mode)
242 (viper-get-saved-cursor-color-in-emacs-mode))
243 (t (viper-get-saved-cursor-color-in-insert-mode)))
244 )))
245
246 \f
247 ;; Check the current version against the major and minor version numbers
248 ;; using op: cur-vers op major.minor If emacs-major-version or
249 ;; emacs-minor-version are not defined, we assume that the current version
250 ;; is hopelessly outdated. We assume that emacs-major-version and
251 ;; emacs-minor-version are defined. Otherwise, for Emacs/XEmacs 19, if the
252 ;; current minor version is < 10 (xemacs) or < 23 (emacs) the return value
253 ;; will be nil (when op is =, >, or >=) and t (when op is <, <=), which may be
254 ;; incorrect. However, this gives correct result in our cases, since we are
255 ;; testing for sufficiently high Emacs versions.
256 (defun viper-check-version (op major minor &optional type-of-emacs)
257 (if (and (boundp 'emacs-major-version) (boundp 'emacs-minor-version))
258 (and (cond ((eq type-of-emacs 'xemacs) (featurep 'xemacs))
259 ((eq type-of-emacs 'emacs) (featurep 'emacs))
260 (t t))
261 (cond ((eq op '=) (and (= emacs-minor-version minor)
262 (= emacs-major-version major)))
263 ((memq op '(> >= < <=))
264 (and (or (funcall op emacs-major-version major)
265 (= emacs-major-version major))
266 (if (= emacs-major-version major)
267 (funcall op emacs-minor-version minor)
268 t)))
269 (t
270 (error "%S: Invalid op in viper-check-version" op))))
271 (cond ((memq op '(= > >=)) nil)
272 ((memq op '(< <=)) t))))
273
274
275 (defun viper-get-visible-buffer-window (wind)
276 (if (featurep 'xemacs)
277 (get-buffer-window wind t)
278 (get-buffer-window wind 'visible)))
279
280
281 ;; Return line position.
282 ;; If pos is 'start then returns position of line start.
283 ;; If pos is 'end, returns line end. If pos is 'mid, returns line center.
284 ;; Pos = 'indent returns beginning of indentation.
285 ;; Otherwise, returns point. Current point is not moved in any case."
286 (defun viper-line-pos (pos)
287 (let ((cur-pos (point))
288 (result))
289 (cond
290 ((equal pos 'start)
291 (beginning-of-line))
292 ((equal pos 'end)
293 (end-of-line))
294 ((equal pos 'mid)
295 (goto-char (+ (viper-line-pos 'start) (viper-line-pos 'end) 2)))
296 ((equal pos 'indent)
297 (back-to-indentation))
298 (t nil))
299 (setq result (point))
300 (goto-char cur-pos)
301 result))
302
303 ;; Emacs used to count each multibyte character as several positions in the buffer,
304 ;; so we had to use Emacs' chars-in-region to count characters. Since 20.3,
305 ;; Emacs counts multibyte characters as 1 position. XEmacs has always been
306 ;; counting each char as just one pos. So, now we can simply subtract beg from
307 ;; end to determine the number of characters in a region.
308 (defun viper-chars-in-region (beg end &optional preserve-sign)
309 ;;(let ((count (abs (if (fboundp 'chars-in-region)
310 ;; (chars-in-region beg end)
311 ;; (- end beg)))))
312 (let ((count (abs (- end beg))))
313 (if (and (< end beg) preserve-sign)
314 (- count)
315 count)))
316
317 ;; Test if POS is between BEG and END
318 (defsubst viper-pos-within-region (pos beg end)
319 (and (>= pos (min beg end)) (>= (max beg end) pos)))
320
321
322 ;; Like move-marker but creates a virgin marker if arg isn't already a marker.
323 ;; The first argument must eval to a variable name.
324 ;; Arguments: (var-name position &optional buffer).
325 ;;
326 ;; This is useful for moving markers that are supposed to be local.
327 ;; For this, VAR-NAME should be made buffer-local with nil as a default.
328 ;; Then, each time this var is used in `viper-move-marker-locally' in a new
329 ;; buffer, a new marker will be created.
330 (defun viper-move-marker-locally (var pos &optional buffer)
331 (if (markerp (eval var))
332 ()
333 (set var (make-marker)))
334 (move-marker (eval var) pos buffer))
335
336
337 ;; Print CONDITIONS as a message.
338 (defun viper-message-conditions (conditions)
339 (let ((case (car conditions)) (msg (cdr conditions)))
340 (if (null msg)
341 (message "%s" case)
342 (message "%s: %s" case (mapconcat 'prin1-to-string msg " ")))
343 (beep 1)))
344
345
346 \f
347 ;;; List/alist utilities
348
349 ;; Convert LIST to an alist
350 (defun viper-list-to-alist (lst)
351 (let ((alist))
352 (while lst
353 (setq alist (cons (list (car lst)) alist))
354 (setq lst (cdr lst)))
355 alist))
356
357 ;; Convert ALIST to a list.
358 (defun viper-alist-to-list (alst)
359 (let ((lst))
360 (while alst
361 (setq lst (cons (car (car alst)) lst))
362 (setq alst (cdr alst)))
363 lst))
364
365 ;; Filter ALIST using REGEXP. Return alist whose elements match the regexp.
366 (defun viper-filter-alist (regexp alst)
367 (interactive "s x")
368 (let ((outalst) (inalst alst))
369 (while (car inalst)
370 (if (string-match regexp (car (car inalst)))
371 (setq outalst (cons (car inalst) outalst)))
372 (setq inalst (cdr inalst)))
373 outalst))
374
375 ;; Filter LIST using REGEXP. Return list whose elements match the regexp.
376 (defun viper-filter-list (regexp lst)
377 (interactive "s x")
378 (let ((outlst) (inlst lst))
379 (while (car inlst)
380 (if (string-match regexp (car inlst))
381 (setq outlst (cons (car inlst) outlst)))
382 (setq inlst (cdr inlst)))
383 outlst))
384
385
386 ;; Append LIS2 to LIS1, both alists, by side-effect and returns LIS1
387 ;; LIS2 is modified by filtering it: deleting its members of the form
388 ;; \(car elt\) such that (car elt') is in LIS1.
389 (defun viper-append-filter-alist (lis1 lis2)
390 (let ((temp lis1)
391 elt)
392 ;;filter-append the second list
393 (while temp
394 ;; delete all occurrences
395 (while (setq elt (assoc (car (car temp)) lis2))
396 (setq lis2 (delq elt lis2)))
397 (setq temp (cdr temp)))
398
399 (append lis1 lis2)))
400
401
402 \f
403 (declare-function viper-forward-Word "viper-cmd" (arg))
404
405 ;;; Support for :e, :r, :w file globbing
406
407 ;; Glob the file spec.
408 ;; This function is designed to work under Unix. It might also work under VMS.
409 (defun viper-glob-unix-files (filespec)
410 (let ((gshell
411 (cond (ex-unix-type-shell shell-file-name)
412 ((memq system-type '(vax-vms axp-vms)) "*dcl*") ; VAX VMS
413 (t "sh"))) ; probably Unix anyway
414 (gshell-options
415 ;; using cond in anticipation of further additions
416 (cond (ex-unix-type-shell-options)
417 ))
418 (command (cond (viper-ms-style-os-p (format "\"ls -1 -d %s\"" filespec))
419 (t (format "ls -1 -d %s" filespec))))
420 status)
421 (save-excursion
422 (set-buffer (get-buffer-create viper-ex-tmp-buf-name))
423 (erase-buffer)
424 (setq status
425 (if gshell-options
426 (call-process gshell nil t nil
427 gshell-options
428 "-c"
429 command)
430 (call-process gshell nil t nil
431 "-c"
432 command)))
433 (goto-char (point-min))
434 ;; Issue an error, if no match.
435 (unless (eq 0 status)
436 (save-excursion
437 (skip-chars-forward " \t\n\j")
438 (if (looking-at "ls:")
439 (viper-forward-Word 1))
440 (error "%s: %s"
441 (if (stringp gshell)
442 gshell
443 "shell")
444 (buffer-substring (point) (viper-line-pos 'end)))
445 ))
446 (goto-char (point-min))
447 (viper-get-filenames-from-buffer 'one-per-line))
448 ))
449
450
451 ;; Interpret the stuff in the buffer as a list of file names
452 ;; return a list of file names listed in the buffer beginning at point
453 ;; If optional arg is supplied, assume each filename is listed on a separate
454 ;; line
455 (defun viper-get-filenames-from-buffer (&optional one-per-line)
456 (let ((skip-chars (if one-per-line "\t\n" " \t\n"))
457 result fname delim)
458 (skip-chars-forward skip-chars)
459 (while (not (eobp))
460 (if (cond ((looking-at "\"")
461 (setq delim ?\")
462 (re-search-forward "[^\"]+" nil t)) ; noerror
463 ((looking-at "'")
464 (setq delim ?')
465 (re-search-forward "[^']+" nil t)) ; noerror
466 (t
467 (re-search-forward
468 (concat "[^" skip-chars "]+") nil t))) ;noerror
469 (setq fname
470 (buffer-substring (match-beginning 0) (match-end 0))))
471 (if delim
472 (forward-char 1))
473 (skip-chars-forward " \t\n")
474 (setq result (cons fname result)))
475 result))
476
477 ;; convert MS-DOS wildcards to regexp
478 (defun viper-wildcard-to-regexp (wcard)
479 (save-excursion
480 (set-buffer (get-buffer-create viper-ex-tmp-buf-name))
481 (erase-buffer)
482 (insert wcard)
483 (goto-char (point-min))
484 (while (not (eobp))
485 (skip-chars-forward "^*?.\\\\")
486 (cond ((eq (char-after (point)) ?*) (insert ".")(forward-char 1))
487 ((eq (char-after (point)) ?.) (insert "\\")(forward-char 1))
488 ((eq (char-after (point)) ?\\) (insert "\\")(forward-char 1))
489 ((eq (char-after (point)) ??) (delete-char 1)(insert ".")))
490 )
491 (buffer-string)
492 ))
493
494
495 ;; glob windows files
496 ;; LIST is expected to be in reverse order
497 (defun viper-glob-mswindows-files (filespec)
498 (let ((case-fold-search t)
499 tmp tmp2)
500 (save-excursion
501 (set-buffer (get-buffer-create viper-ex-tmp-buf-name))
502 (erase-buffer)
503 (insert filespec)
504 (goto-char (point-min))
505 (setq tmp (viper-get-filenames-from-buffer))
506 (while tmp
507 (setq tmp2 (cons (directory-files
508 ;; the directory part
509 (or (file-name-directory (car tmp))
510 "")
511 t ; return full names
512 ;; the regexp part: globs the file names
513 (concat "^"
514 (viper-wildcard-to-regexp
515 (file-name-nondirectory (car tmp)))
516 "$"))
517 tmp2))
518 (setq tmp (cdr tmp)))
519 (reverse (apply 'append tmp2)))))
520
521 \f
522 ;;; Insertion ring
523
524 ;; Rotate RING's index. DIRection can be positive or negative.
525 (defun viper-ring-rotate1 (ring dir)
526 (if (and (ring-p ring) (> (ring-length ring) 0))
527 (progn
528 (setcar ring (cond ((> dir 0)
529 (ring-plus1 (car ring) (ring-length ring)))
530 ((< dir 0)
531 (ring-minus1 (car ring) (ring-length ring)))
532 ;; don't rotate if dir = 0
533 (t (car ring))))
534 (viper-current-ring-item ring)
535 )))
536
537 (defun viper-special-ring-rotate1 (ring dir)
538 (if (memq viper-intermediate-command
539 '(repeating-display-destructive-command
540 repeating-insertion-from-ring))
541 (viper-ring-rotate1 ring dir)
542 ;; don't rotate otherwise
543 (viper-ring-rotate1 ring 0)))
544
545 ;; current ring item; if N is given, then so many items back from the
546 ;; current
547 (defun viper-current-ring-item (ring &optional n)
548 (setq n (or n 0))
549 (if (and (ring-p ring) (> (ring-length ring) 0))
550 (aref (cdr (cdr ring)) (mod (- (car ring) 1 n) (ring-length ring)))))
551
552 ;; Push item onto ring. The second argument is a ring-variable, not value.
553 (defun viper-push-onto-ring (item ring-var)
554 (or (ring-p (eval ring-var))
555 (set ring-var (make-ring (eval (intern (format "%S-size" ring-var))))))
556 (or (null item) ; don't push nil
557 (and (stringp item) (string= item "")) ; or empty strings
558 (equal item (viper-current-ring-item (eval ring-var))) ; or old stuff
559 ;; Since viper-set-destructive-command checks if we are inside
560 ;; viper-repeat, we don't check whether this-command-keys is a `.'. The
561 ;; cmd viper-repeat makes a call to the current function only if `.' is
562 ;; executing a command from the command history. It doesn't call the
563 ;; push-onto-ring function if `.' is simply repeating the last
564 ;; destructive command. We only check for ESC (which happens when we do
565 ;; insert with a prefix argument, or if this-command-keys doesn't give
566 ;; anything meaningful (in that case we don't know what to show to the
567 ;; user).
568 (and (eq ring-var 'viper-command-ring)
569 (string-match "\\([0-9]*\e\\|^[ \t]*$\\|escape\\)"
570 (viper-array-to-string (this-command-keys))))
571 (viper-ring-insert (eval ring-var) item))
572 )
573
574
575 ;; removing elts from ring seems to break it
576 (defun viper-cleanup-ring (ring)
577 (or (< (ring-length ring) 2)
578 (null (viper-current-ring-item ring))
579 ;; last and previous equal
580 (if (equal (viper-current-ring-item ring)
581 (viper-current-ring-item ring 1))
582 (viper-ring-pop ring))))
583
584 ;; ring-remove seems to be buggy, so we concocted this for our purposes.
585 (defun viper-ring-pop (ring)
586 (let* ((ln (ring-length ring))
587 (vec (cdr (cdr ring)))
588 (veclen (length vec))
589 (hd (car ring))
590 (idx (max 0 (ring-minus1 hd ln)))
591 (top-elt (aref vec idx)))
592
593 ;; shift elements
594 (while (< (1+ idx) veclen)
595 (aset vec idx (aref vec (1+ idx)))
596 (setq idx (1+ idx)))
597 (aset vec idx nil)
598
599 (setq hd (max 0 (ring-minus1 hd ln)))
600 (if (= hd (1- ln)) (setq hd 0))
601 (setcar ring hd) ; move head
602 (setcar (cdr ring) (max 0 (1- ln))) ; adjust length
603 top-elt
604 ))
605
606 (defun viper-ring-insert (ring item)
607 (let* ((ln (ring-length ring))
608 (vec (cdr (cdr ring)))
609 (veclen (length vec))
610 (hd (car ring))
611 (vecpos-after-hd (if (= hd 0) ln hd))
612 (idx ln))
613
614 (if (= ln veclen)
615 (progn
616 (aset vec hd item) ; hd is always 1+ the actual head index in vec
617 (setcar ring (ring-plus1 hd ln)))
618 (setcar (cdr ring) (1+ ln))
619 (setcar ring (ring-plus1 vecpos-after-hd (1+ ln)))
620 (while (and (>= idx vecpos-after-hd) (> ln 0))
621 (aset vec idx (aref vec (1- idx)))
622 (setq idx (1- idx)))
623 (aset vec vecpos-after-hd item))
624 item))
625
626 \f
627 ;;; String utilities
628
629 ;; If STRING is longer than MAX-LEN, truncate it and print ...... instead
630 ;; PRE-STRING is a string to prepend to the abbrev string.
631 ;; POST-STRING is a string to append to the abbrev string.
632 ;; ABBREV_SIGN is a string to be inserted before POST-STRING
633 ;; if the orig string was truncated.
634 (defun viper-abbreviate-string (string max-len
635 pre-string post-string abbrev-sign)
636 (let (truncated-str)
637 (setq truncated-str
638 (if (stringp string)
639 (substring string 0 (min max-len (length string)))))
640 (cond ((null truncated-str) "")
641 ((> (length string) max-len)
642 (format "%s%s%s%s"
643 pre-string truncated-str abbrev-sign post-string))
644 (t (format "%s%s%s" pre-string truncated-str post-string)))))
645
646 ;; tells if we are over a whitespace-only line
647 (defsubst viper-over-whitespace-line ()
648 (save-excursion
649 (beginning-of-line)
650 (looking-at "^[ \t]*$")))
651
652 \f
653 ;;; Saving settings in custom file
654
655 ;; Save the current setting of VAR in CUSTOM-FILE.
656 ;; If given, MESSAGE is a message to be displayed after that.
657 ;; This message is erased after 2 secs, if erase-msg is non-nil.
658 ;; Arguments: var message custom-file &optional erase-message
659 (defun viper-save-setting (var message custom-file &optional erase-msg)
660 (let* ((var-name (symbol-name var))
661 (var-val (if (boundp var) (eval var)))
662 (regexp (format "^[^;]*%s[ \t\n]*[a-zA-Z---_']*[ \t\n)]" var-name))
663 (buf (find-file-noselect (substitute-in-file-name custom-file)))
664 )
665 (message "%s" (or message ""))
666 (save-excursion
667 (set-buffer buf)
668 (goto-char (point-min))
669 (if (re-search-forward regexp nil t)
670 (let ((reg-end (1- (match-end 0))))
671 (search-backward var-name)
672 (delete-region (match-beginning 0) reg-end)
673 (goto-char (match-beginning 0))
674 (insert (format "%s '%S" var-name var-val)))
675 (goto-char (point-max))
676 (if (not (bolp)) (insert "\n"))
677 (insert (format "(setq %s '%S)\n" var-name var-val)))
678 (save-buffer))
679 (kill-buffer buf)
680 (if erase-msg
681 (progn
682 (sit-for 2)
683 (message "")))
684 ))
685
686 ;; Save STRING in CUSTOM-FILE. If PATTERN is non-nil, remove strings that
687 ;; match this pattern.
688 (defun viper-save-string-in-file (string custom-file &optional pattern)
689 (let ((buf (find-file-noselect (substitute-in-file-name custom-file))))
690 (save-excursion
691 (set-buffer buf)
692 (let (buffer-read-only)
693 (goto-char (point-min))
694 (if pattern (delete-matching-lines pattern))
695 (goto-char (point-max))
696 (if string (insert string))
697 (save-buffer)))
698 (kill-buffer buf)
699 ))
700
701
702 ;; This is a simple-minded check for whether a file is under version control.
703 ;; If file,v exists but file doesn't, this file is considered to be not checked
704 ;; in and not checked out for the purpose of patching (since patch won't be
705 ;; able to read such a file anyway).
706 ;; FILE is a string representing file name
707 ;;(defun viper-file-under-version-control (file)
708 ;; (let* ((filedir (file-name-directory file))
709 ;; (file-nondir (file-name-nondirectory file))
710 ;; (trial (concat file-nondir ",v"))
711 ;; (full-trial (concat filedir trial))
712 ;; (full-rcs-trial (concat filedir "RCS/" trial)))
713 ;; (and (stringp file)
714 ;; (file-exists-p file)
715 ;; (or
716 ;; (and
717 ;; (file-exists-p full-trial)
718 ;; ;; in FAT FS, `file,v' and `file' may turn out to be the same!
719 ;; ;; don't be fooled by this!
720 ;; (not (equal (file-attributes file)
721 ;; (file-attributes full-trial))))
722 ;; ;; check if a version is in RCS/ directory
723 ;; (file-exists-p full-rcs-trial)))
724 ;; ))
725
726
727 (defsubst viper-file-checked-in-p (file)
728 (and (featurep 'vc-hooks)
729 ;; CVS files are considered not checked in
730 ;; FIXME: Should this deal with more than CVS?
731 (not (memq (vc-backend file) '(nil CVS)))
732 (if (fboundp 'vc-state)
733 (and
734 (not (memq (vc-state file) '(edited needs-merge)))
735 (not (stringp (vc-state file))))
736 ;; XEmacs has no vc-state
737 (if (featurep 'xemacs) (not (vc-locking-user file))))))
738
739 ;; checkout if visited file is checked in
740 (defun viper-maybe-checkout (buf)
741 (let ((file (expand-file-name (buffer-file-name buf)))
742 (checkout-function (key-binding "\C-x\C-q")))
743 (if (and (viper-file-checked-in-p file)
744 (or (beep 1) t)
745 (y-or-n-p
746 (format
747 "File %s is checked in. Check it out? "
748 (viper-abbreviate-file-name file))))
749 (with-current-buffer buf
750 (command-execute checkout-function)))))
751
752
753
754 \f
755 ;;; Overlays
756 (defun viper-put-on-search-overlay (beg end)
757 (if (viper-overlay-p viper-search-overlay)
758 (viper-move-overlay viper-search-overlay beg end)
759 (setq viper-search-overlay (viper-make-overlay beg end (current-buffer)))
760 (viper-overlay-put
761 viper-search-overlay 'priority viper-search-overlay-priority))
762 (viper-overlay-put viper-search-overlay 'face viper-search-face))
763
764 ;; Search
765
766 (defun viper-flash-search-pattern ()
767 (if (not (viper-has-face-support-p))
768 nil
769 (viper-put-on-search-overlay (match-beginning 0) (match-end 0))
770 (sit-for 2)
771 (viper-overlay-put viper-search-overlay 'face nil)))
772
773 (defun viper-hide-search-overlay ()
774 (if (not (viper-overlay-p viper-search-overlay))
775 (progn
776 (setq viper-search-overlay
777 (viper-make-overlay (point-min) (point-min) (current-buffer)))
778 (viper-overlay-put
779 viper-search-overlay 'priority viper-search-overlay-priority)))
780 (viper-overlay-put viper-search-overlay 'face nil))
781
782 ;; Replace state
783
784 (defsubst viper-move-replace-overlay (beg end)
785 (viper-move-overlay viper-replace-overlay beg end))
786
787 (defun viper-set-replace-overlay (beg end)
788 (if (viper-overlay-live-p viper-replace-overlay)
789 (viper-move-replace-overlay beg end)
790 (setq viper-replace-overlay (viper-make-overlay beg end (current-buffer)))
791 ;; never detach
792 (viper-overlay-put
793 viper-replace-overlay (if (featurep 'emacs) 'evaporate 'detachable) nil)
794 (viper-overlay-put
795 viper-replace-overlay 'priority viper-replace-overlay-priority)
796 ;; If Emacs will start supporting overlay maps, as it currently supports
797 ;; text-property maps, we could do away with viper-replace-minor-mode and
798 ;; just have keymap attached to replace overlay.
799 ;;(viper-overlay-put
800 ;; viper-replace-overlay
801 ;; (if (featurep 'xemacs) 'keymap 'local-map)
802 ;; viper-replace-map)
803 )
804 (if (viper-has-face-support-p)
805 (viper-overlay-put
806 viper-replace-overlay 'face viper-replace-overlay-face))
807 (viper-save-cursor-color 'before-replace-mode)
808 (viper-change-cursor-color
809 (viper-frame-value viper-replace-overlay-cursor-color)))
810
811
812 (defun viper-set-replace-overlay-glyphs (before-glyph after-glyph)
813 (or (viper-overlay-live-p viper-replace-overlay)
814 (viper-set-replace-overlay (point-min) (point-min)))
815 (if (or (not (viper-has-face-support-p))
816 viper-use-replace-region-delimiters)
817 (let ((before-name (if (featurep 'xemacs) 'begin-glyph 'before-string))
818 (after-name (if (featurep 'xemacs) 'end-glyph 'after-string)))
819 (viper-overlay-put viper-replace-overlay before-name before-glyph)
820 (viper-overlay-put viper-replace-overlay after-name after-glyph))))
821
822 (defun viper-hide-replace-overlay ()
823 (viper-set-replace-overlay-glyphs nil nil)
824 (viper-restore-cursor-color 'after-replace-mode)
825 (viper-restore-cursor-color 'after-insert-mode)
826 (if (viper-has-face-support-p)
827 (viper-overlay-put viper-replace-overlay 'face nil)))
828
829
830 (defsubst viper-replace-start ()
831 (viper-overlay-start viper-replace-overlay))
832 (defsubst viper-replace-end ()
833 (viper-overlay-end viper-replace-overlay))
834
835
836 ;; Minibuffer
837
838 (defun viper-set-minibuffer-overlay ()
839 (viper-check-minibuffer-overlay)
840 (when (viper-has-face-support-p)
841 (viper-overlay-put
842 viper-minibuffer-overlay 'face viper-minibuffer-current-face)
843 (viper-overlay-put
844 viper-minibuffer-overlay 'priority viper-minibuffer-overlay-priority)
845 ;; never detach
846 (viper-overlay-put
847 viper-minibuffer-overlay
848 (if (featurep 'emacs) 'evaporate 'detachable)
849 nil)
850 ;; make viper-minibuffer-overlay open-ended
851 ;; In emacs, it is made open ended at creation time
852 (when (featurep 'xemacs)
853 (viper-overlay-put viper-minibuffer-overlay 'start-open nil)
854 (viper-overlay-put viper-minibuffer-overlay 'end-open nil))))
855
856 (defun viper-check-minibuffer-overlay ()
857 (if (viper-overlay-live-p viper-minibuffer-overlay)
858 (viper-move-overlay
859 viper-minibuffer-overlay
860 (if (fboundp 'minibuffer-prompt-end) (minibuffer-prompt-end) 1)
861 (1+ (buffer-size)))
862 (setq viper-minibuffer-overlay
863 (if (featurep 'xemacs)
864 (viper-make-overlay 1 (1+ (buffer-size)) (current-buffer))
865 ;; make overlay open-ended
866 (viper-make-overlay
867 (if (fboundp 'minibuffer-prompt-end) (minibuffer-prompt-end) 1)
868 (1+ (buffer-size))
869 (current-buffer) nil 'rear-advance)))))
870
871
872 (defsubst viper-is-in-minibuffer ()
873 (save-match-data
874 (string-match "\*Minibuf-" (buffer-name))))
875
876
877 \f
878 ;;; XEmacs compatibility
879
880 (defun viper-abbreviate-file-name (file)
881 (if (featurep 'xemacs)
882 (abbreviate-file-name file t) ; XEmacs requires addl argument
883 (abbreviate-file-name file)))
884
885 ;; Sit for VAL milliseconds. XEmacs doesn't support the millisecond arg
886 ;; in sit-for, so this function smoothes out the differences.
887 (defsubst viper-sit-for-short (val &optional nodisp)
888 (sit-for (/ val 1000.0) nodisp))
889
890 ;; EVENT may be a single event of a sequence of events
891 (defsubst viper-ESC-event-p (event)
892 (let ((ESC-keys '(?\e (control \[) escape))
893 (key (viper-event-key event)))
894 (member key ESC-keys)))
895
896 ;; checks if object is a marker, has a buffer, and points to within that buffer
897 (defun viper-valid-marker (marker)
898 (if (and (markerp marker) (marker-buffer marker))
899 (let ((buf (marker-buffer marker))
900 (pos (marker-position marker)))
901 (save-excursion
902 (set-buffer buf)
903 (and (<= pos (point-max)) (<= (point-min) pos))))))
904
905 (defsubst viper-mark-marker ()
906 (if (featurep 'xemacs) (mark-marker t)
907 (mark-marker)))
908
909 ;; like (set-mark-command nil) but doesn't push twice, if (car mark-ring)
910 ;; is the same as (mark t).
911 (defsubst viper-set-mark-if-necessary ()
912 (setq mark-ring (delete (viper-mark-marker) mark-ring))
913 (set-mark-command nil)
914 (setq viper-saved-mark (point)))
915
916 ;; In transient mark mode (zmacs mode), it is annoying when regions become
917 ;; highlighted due to Viper's pushing marks. So, we deactivate marks, unless
918 ;; the user explicitly wants highlighting, e.g., by hitting '' or ``
919 (defun viper-deactivate-mark ()
920 (if (featurep 'xemacs)
921 (zmacs-deactivate-region)
922 (deactivate-mark)))
923
924 (defsubst viper-leave-region-active ()
925 (if (featurep 'xemacs) (setq zmacs-region-stays t)))
926
927 ;; Check if arg is a valid character for register
928 ;; TYPE is a list that can contain `letter', `Letter', and `digit'.
929 ;; Letter means lowercase letters, Letter means uppercase letters, and
930 ;; digit means digits from 1 to 9.
931 ;; If TYPE is nil, then down/uppercase letters and digits are allowed.
932 (defun viper-valid-register (reg &optional type)
933 (or type (setq type '(letter Letter digit)))
934 (or (if (memq 'letter type)
935 (and (<= ?a reg) (<= reg ?z)))
936 (if (memq 'digit type)
937 (and (<= ?1 reg) (<= reg ?9)))
938 (if (memq 'Letter type)
939 (and (<= ?A reg) (<= reg ?Z)))
940 ))
941
942
943
944 ;; it is suggested that an event must be copied before it is assigned to
945 ;; last-command-event in XEmacs
946 (defun viper-copy-event (event)
947 (if (featurep 'xemacs) (copy-event event)
948 event))
949
950 ;; Uses different timeouts for ESC-sequences and others
951 (defsubst viper-fast-keysequence-p ()
952 (not (viper-sit-for-short
953 (if (viper-ESC-event-p last-input-event)
954 viper-ESC-keyseq-timeout
955 viper-fast-keyseq-timeout)
956 t)))
957
958 ;; like read-event, but in XEmacs also try to convert to char, if possible
959 (defun viper-read-event-convert-to-char ()
960 (let (event)
961 (if (featurep 'xemacs)
962 (progn
963 (setq event (next-command-event))
964 (or (event-to-character event)
965 event))
966 (read-event))))
967
968 ;; Viperized read-key-sequence
969 (defun viper-read-key-sequence (prompt &optional continue-echo)
970 (let (inhibit-quit event keyseq)
971 (setq keyseq (read-key-sequence prompt continue-echo))
972 (setq event (if (featurep 'xemacs)
973 (elt keyseq 0) ; XEmacs returns vector of events
974 (elt (listify-key-sequence keyseq) 0)))
975 (if (viper-ESC-event-p event)
976 (let (unread-command-events)
977 (if (viper-fast-keysequence-p)
978 (let ((viper-vi-global-user-minor-mode nil)
979 (viper-vi-local-user-minor-mode nil)
980 (viper-vi-intercept-minor-mode nil)
981 (viper-insert-intercept-minor-mode nil)
982 (viper-replace-minor-mode nil) ; actually unnecessary
983 (viper-insert-global-user-minor-mode nil)
984 (viper-insert-local-user-minor-mode nil))
985 ;; Note: set unread-command-events only after testing for fast
986 ;; keysequence. Otherwise, viper-fast-keysequence-p will be
987 ;; always t -- whether there is anything after ESC or not
988 (viper-set-unread-command-events keyseq)
989 (setq keyseq (read-key-sequence nil)))
990 (viper-set-unread-command-events keyseq)
991 (setq keyseq (read-key-sequence nil)))))
992 keyseq))
993
994
995 ;; This function lets function-key-map convert key sequences into logical
996 ;; keys. This does a better job than viper-read-event when it comes to kbd
997 ;; macros, since it enables certain macros to be shared between X and TTY modes
998 ;; by correctly mapping key sequences for Left/Right/... (on an ascii
999 ;; terminal) into logical keys left, right, etc.
1000 (defun viper-read-key ()
1001 (let ((overriding-local-map viper-overriding-map)
1002 (inhibit-quit t)
1003 help-char key)
1004 (use-global-map viper-overriding-map)
1005 (unwind-protect
1006 (setq key (elt (viper-read-key-sequence nil) 0))
1007 (use-global-map global-map))
1008 key))
1009
1010
1011 ;; Emacs has a bug in eventp, which causes (eventp nil) to return (nil)
1012 ;; instead of nil, if '(nil) was previously inadvertently assigned to
1013 ;; unread-command-events
1014 (defun viper-event-key (event)
1015 (or (and event (eventp event))
1016 (error "viper-event-key: Wrong type argument, eventp, %S" event))
1017 (when (if (featurep 'xemacs)
1018 (or (key-press-event-p event) (mouse-event-p event)) ; xemacs
1019 t ; emacs
1020 )
1021 (let ((mod (event-modifiers event))
1022 basis)
1023 (setq basis
1024 (if (featurep 'xemacs)
1025 ;; XEmacs
1026 (cond ((key-press-event-p event)
1027 (event-key event))
1028 ((button-event-p event)
1029 (concat "mouse-" (prin1-to-string (event-button event))))
1030 (t
1031 (error "viper-event-key: Unknown event, %S" event)))
1032 ;; Emacs doesn't handle capital letters correctly, since
1033 ;; \S-a isn't considered the same as A (it behaves as
1034 ;; plain `a' instead). So we take care of this here
1035 (cond ((and (viper-characterp event) (<= ?A event) (<= event ?Z))
1036 (setq mod nil
1037 event event))
1038 ;; Emacs has the oddity whereby characters 128+char
1039 ;; represent M-char *if* this appears inside a string.
1040 ;; So, we convert them manually to (meta char).
1041 ((and (viper-characterp event)
1042 (< ?\C-? event) (<= event 255))
1043 (setq mod '(meta)
1044 event (- event ?\C-? 1)))
1045 ((and (null mod) (eq event 'return))
1046 (setq event ?\C-m))
1047 ((and (null mod) (eq event 'space))
1048 (setq event ?\ ))
1049 ((and (null mod) (eq event 'delete))
1050 (setq event ?\C-?))
1051 ((and (null mod) (eq event 'backspace))
1052 (setq event ?\C-h))
1053 (t (event-basic-type event)))
1054 ) ; (featurep 'xemacs)
1055 )
1056 (if (viper-characterp basis)
1057 (setq basis
1058 (if (viper= basis ?\C-?)
1059 (list 'control '\?) ; taking care of an emacs bug
1060 (intern (char-to-string basis)))))
1061 (if mod
1062 (append mod (list basis))
1063 basis))))
1064
1065 (defun viper-key-to-emacs-key (key)
1066 (let (key-name char-p modifiers mod-char-list base-key base-key-name)
1067 (cond ((featurep 'xemacs) key)
1068
1069 ((symbolp key)
1070 (setq key-name (symbol-name key))
1071 (cond ((= (length key-name) 1) ; character event
1072 (string-to-char key-name))
1073 ;; Emacs doesn't recognize `return' and `escape' as events on
1074 ;; dumb terminals, so we translate them into characters
1075 ((and (featurep 'emacs) (not (viper-window-display-p))
1076 (string= key-name "return"))
1077 ?\C-m)
1078 ((and (featurep 'emacs) (not (viper-window-display-p))
1079 (string= key-name "escape"))
1080 ?\e)
1081 ;; pass symbol-event as is
1082 (t key)))
1083
1084 ((listp key)
1085 (setq modifiers (viper-subseq key 0 (1- (length key)))
1086 base-key (viper-seq-last-elt key)
1087 base-key-name (symbol-name base-key)
1088 char-p (= (length base-key-name) 1))
1089 (setq mod-char-list
1090 (mapcar
1091 '(lambda (elt) (upcase (substring (symbol-name elt) 0 1)))
1092 modifiers))
1093 (if char-p
1094 (setq key-name
1095 (car (read-from-string
1096 (concat
1097 "?\\"
1098 (mapconcat 'identity mod-char-list "-\\")
1099 "-"
1100 base-key-name))))
1101 (setq key-name
1102 (intern
1103 (concat
1104 (mapconcat 'identity mod-char-list "-")
1105 "-"
1106 base-key-name))))))
1107 ))
1108
1109
1110 ;; LIS is assumed to be a list of events of characters
1111 (defun viper-eventify-list-xemacs (lis)
1112 (if (featurep 'xemacs)
1113 (mapcar
1114 (lambda (elt)
1115 (cond ((viper-characterp elt) (character-to-event elt))
1116 ((eventp elt) elt)
1117 (t (error
1118 "viper-eventify-list-xemacs: can't convert to event, %S"
1119 elt))))
1120 lis)))
1121
1122
1123 ;; Smoothes out the difference between Emacs' unread-command-events
1124 ;; and XEmacs unread-command-event. Arg is a character, an event, a list of
1125 ;; events or a sequence of keys.
1126 ;;
1127 ;; Due to the way unread-command-events in Emacs (not XEmacs), a non-event
1128 ;; symbol in unread-command-events list may cause Emacs to turn this symbol
1129 ;; into an event. Below, we delete nil from event lists, since nil is the most
1130 ;; common symbol that might appear in this wrong context.
1131 (defun viper-set-unread-command-events (arg)
1132 (if (featurep 'emacs)
1133 (setq
1134 unread-command-events
1135 (let ((new-events
1136 (cond ((eventp arg) (list arg))
1137 ((listp arg) arg)
1138 ((sequencep arg)
1139 (listify-key-sequence arg))
1140 (t (error
1141 "viper-set-unread-command-events: Invalid argument, %S"
1142 arg)))))
1143 (if (not (eventp nil))
1144 (setq new-events (delq nil new-events)))
1145 (append new-events unread-command-events)))
1146 ;; XEmacs
1147 (setq
1148 unread-command-events
1149 (append
1150 (cond ((viper-characterp arg) (list (character-to-event arg)))
1151 ((eventp arg) (list arg))
1152 ((stringp arg) (mapcar 'character-to-event arg))
1153 ((vectorp arg) (append arg nil)) ; turn into list
1154 ((listp arg) (viper-eventify-list-xemacs arg))
1155 (t (error
1156 "viper-set-unread-command-events: Invalid argument, %S" arg)))
1157 unread-command-events))))
1158
1159
1160 ;; Check if vec is a vector of key-press events representing characters
1161 ;; XEmacs only
1162 (defun viper-event-vector-p (vec)
1163 (and (vectorp vec)
1164 (eval (cons 'and (mapcar '(lambda (elt) (if (eventp elt) t)) vec)))))
1165
1166
1167 ;; check if vec is a vector of character symbols
1168 (defun viper-char-symbol-sequence-p (vec)
1169 (and
1170 (sequencep vec)
1171 (eval
1172 (cons 'and
1173 (mapcar (lambda (elt)
1174 (and (symbolp elt) (= (length (symbol-name elt)) 1)))
1175 vec)))))
1176
1177
1178 (defun viper-char-array-p (array)
1179 (eval (cons 'and (mapcar 'viper-characterp array))))
1180
1181
1182 ;; Args can be a sequence of events, a string, or a Viper macro. Will try to
1183 ;; convert events to keys and, if all keys are regular printable
1184 ;; characters, will return a string. Otherwise, will return a string
1185 ;; representing a vector of converted events. If the input was a Viper macro,
1186 ;; will return a string that represents this macro as a vector.
1187 (defun viper-array-to-string (event-seq)
1188 (let (temp temp2)
1189 (cond ((stringp event-seq) event-seq)
1190 ((viper-event-vector-p event-seq)
1191 (setq temp (mapcar 'viper-event-key event-seq))
1192 (cond ((viper-char-symbol-sequence-p temp)
1193 (mapconcat 'symbol-name temp ""))
1194 ((and (viper-char-array-p
1195 (setq temp2 (mapcar 'viper-key-to-character temp))))
1196 (mapconcat 'char-to-string temp2 ""))
1197 (t (prin1-to-string (vconcat temp)))))
1198 ((viper-char-symbol-sequence-p event-seq)
1199 (mapconcat 'symbol-name event-seq ""))
1200 ((and (vectorp event-seq)
1201 (viper-char-array-p
1202 (setq temp (mapcar 'viper-key-to-character event-seq))))
1203 (mapconcat 'char-to-string temp ""))
1204 (t (prin1-to-string event-seq)))))
1205
1206 (defun viper-key-press-events-to-chars (events)
1207 (mapconcat (if (featurep 'xemacs)
1208 (lambda (elt) (char-to-string (event-to-character elt))) ; xemacs
1209 'char-to-string ; emacs
1210 )
1211 events
1212 ""))
1213
1214
1215 (defun viper-read-char-exclusive ()
1216 (let (char
1217 (echo-keystrokes 1))
1218 (while (null char)
1219 (condition-case nil
1220 (setq char (read-char))
1221 (error
1222 ;; skip event if not char
1223 (viper-read-event))))
1224 char))
1225
1226 ;; key is supposed to be in viper's representation, e.g., (control l), a
1227 ;; character, etc.
1228 (defun viper-key-to-character (key)
1229 (cond ((eq key 'space) ?\ )
1230 ((eq key 'delete) ?\C-?)
1231 ((eq key 'return) ?\C-m)
1232 ((eq key 'backspace) ?\C-h)
1233 ((and (symbolp key)
1234 (= 1 (length (symbol-name key))))
1235 (string-to-char (symbol-name key)))
1236 ((and (listp key)
1237 (eq (car key) 'control)
1238 (symbol-name (nth 1 key))
1239 (= 1 (length (symbol-name (nth 1 key)))))
1240 (read (format "?\\C-%s" (symbol-name (nth 1 key)))))
1241 (t key)))
1242
1243
1244 (defun viper-setup-master-buffer (&rest other-files-or-buffers)
1245 "Set up the current buffer as a master buffer.
1246 Arguments become related buffers. This function should normally be used in
1247 the `Local variables' section of a file."
1248 (setq viper-related-files-and-buffers-ring
1249 (make-ring (1+ (length other-files-or-buffers))))
1250 (mapc '(lambda (elt)
1251 (viper-ring-insert viper-related-files-and-buffers-ring elt))
1252 other-files-or-buffers)
1253 (viper-ring-insert viper-related-files-and-buffers-ring (buffer-name))
1254 )
1255
1256 ;;; Movement utilities
1257
1258 ;; Characters that should not be considered as part of the word, in reformed-vi
1259 ;; syntax mode.
1260 ;; Note: \\ (quoted \) must appear before `-' because this string is listified
1261 ;; into characters at some point and then put back to string. The result is
1262 ;; used in skip-chars-forward, which treats - specially. Here we achieve the
1263 ;; effect of quoting - and preventing it from being special.
1264 (defconst viper-non-word-characters-reformed-vi
1265 "!@#$%^&*()\\-+=|\\~`{}[];:'\",<.>/?")
1266 ;; These are characters that are not to be considered as parts of a word in
1267 ;; Viper.
1268 ;; Set each time state changes and at loading time
1269 (viper-deflocalvar viper-non-word-characters nil)
1270
1271 ;; must be buffer-local
1272 (viper-deflocalvar viper-ALPHA-char-class "w"
1273 "String of syntax classes characterizing Viper's alphanumeric symbols.
1274 In addition, the symbol `_' may be considered alphanumeric if
1275 `viper-syntax-preference' is `strict-vi' or `reformed-vi'.")
1276
1277 (defconst viper-strict-ALPHA-chars "a-zA-Z0-9_"
1278 "Regexp matching the set of alphanumeric characters acceptable to strict
1279 Vi.")
1280 (defconst viper-strict-SEP-chars " \t\n"
1281 "Regexp matching the set of alphanumeric characters acceptable to strict
1282 Vi.")
1283 (defconst viper-strict-SEP-chars-sans-newline " \t"
1284 "Regexp matching the set of alphanumeric characters acceptable to strict
1285 Vi.")
1286
1287 (defconst viper-SEP-char-class " -"
1288 "String of syntax classes for Vi separators.
1289 Usually contains ` ', linefeed, TAB or formfeed.")
1290
1291
1292 ;; Set Viper syntax classes and related variables according to
1293 ;; `viper-syntax-preference'.
1294 (defun viper-update-syntax-classes (&optional set-default)
1295 (let ((preference (cond ((eq viper-syntax-preference 'emacs)
1296 "w") ; Viper words have only Emacs word chars
1297 ((eq viper-syntax-preference 'extended)
1298 "w_") ; Viper words have Emacs word & symbol chars
1299 (t "w"))) ; Viper words are Emacs words plus `_'
1300 (non-word-chars (cond ((eq viper-syntax-preference 'reformed-vi)
1301 (viper-string-to-list
1302 viper-non-word-characters-reformed-vi))
1303 (t nil))))
1304 (if set-default
1305 (setq-default viper-ALPHA-char-class preference
1306 viper-non-word-characters non-word-chars)
1307 (setq viper-ALPHA-char-class preference
1308 viper-non-word-characters non-word-chars))
1309 ))
1310
1311 ;; SYMBOL is used because customize requires it, but it is ignored, unless it
1312 ;; is `nil'. If nil, use setq.
1313 (defun viper-set-syntax-preference (&optional symbol value)
1314 "Set Viper syntax preference.
1315 If called interactively or if SYMBOL is nil, sets syntax preference in current
1316 buffer. If called non-interactively, preferably via the customization widget,
1317 sets the default value."
1318 (interactive)
1319 (or value
1320 (setq value
1321 (completing-read
1322 "Viper syntax preference: "
1323 '(("strict-vi") ("reformed-vi") ("extended") ("emacs"))
1324 nil 'require-match)))
1325 (if (stringp value) (setq value (intern value)))
1326 (or (memq value '(strict-vi reformed-vi extended emacs))
1327 (error "Invalid Viper syntax preference, %S" value))
1328 (if symbol
1329 (setq-default viper-syntax-preference value)
1330 (setq viper-syntax-preference value))
1331 (viper-update-syntax-classes))
1332
1333 (defcustom viper-syntax-preference 'reformed-vi
1334 "*Syntax type characterizing Viper's alphanumeric symbols.
1335 Affects movement and change commands that deal with Vi-style words.
1336 Works best when set in the hooks to various major modes.
1337
1338 `strict-vi' means Viper words are (hopefully) exactly as in Vi.
1339
1340 `reformed-vi' means Viper words are like Emacs words \(as determined using
1341 Emacs syntax tables, which are different for different major modes\) with two
1342 exceptions: the symbol `_' is always part of a word and typical Vi non-word
1343 symbols, such as `,',:,\",),{, etc., are excluded.
1344 This behaves very close to `strict-vi', but also works well with non-ASCII
1345 characters from various alphabets.
1346
1347 `extended' means Viper word constituents are symbols that are marked as being
1348 parts of words OR symbols in Emacs syntax tables.
1349 This is most appropriate for major modes intended for editing programs.
1350
1351 `emacs' means Viper words are the same as Emacs words as specified by Emacs
1352 syntax tables.
1353 This option is appropriate if you like Emacs-style words."
1354 :type '(radio (const strict-vi) (const reformed-vi)
1355 (const extended) (const emacs))
1356 :set 'viper-set-syntax-preference
1357 :group 'viper)
1358 (make-variable-buffer-local 'viper-syntax-preference)
1359
1360
1361 ;; addl-chars are characters to be temporarily considered as alphanumerical
1362 (defun viper-looking-at-alpha (&optional addl-chars)
1363 (or (stringp addl-chars) (setq addl-chars ""))
1364 (if (eq viper-syntax-preference 'reformed-vi)
1365 (setq addl-chars (concat addl-chars "_")))
1366 (let ((char (char-after (point))))
1367 (if char
1368 (if (eq viper-syntax-preference 'strict-vi)
1369 (looking-at (concat "[" viper-strict-ALPHA-chars addl-chars "]"))
1370 (or
1371 ;; or one of the additional chars being asked to include
1372 (viper-memq-char char (viper-string-to-list addl-chars))
1373 (and
1374 ;; not one of the excluded word chars (note:
1375 ;; viper-non-word-characters is a list)
1376 (not (viper-memq-char char viper-non-word-characters))
1377 ;; char of the Viper-word syntax class
1378 (viper-memq-char (char-syntax char)
1379 (viper-string-to-list viper-ALPHA-char-class))))))
1380 ))
1381
1382 (defun viper-looking-at-separator ()
1383 (let ((char (char-after (point))))
1384 (if char
1385 (if (eq viper-syntax-preference 'strict-vi)
1386 (viper-memq-char char (viper-string-to-list viper-strict-SEP-chars))
1387 (or (eq char ?\n) ; RET is always a separator in Vi
1388 (viper-memq-char (char-syntax char)
1389 (viper-string-to-list viper-SEP-char-class)))))
1390 ))
1391
1392 (defsubst viper-looking-at-alphasep (&optional addl-chars)
1393 (or (viper-looking-at-separator) (viper-looking-at-alpha addl-chars)))
1394
1395 (defun viper-skip-alpha-forward (&optional addl-chars)
1396 (or (stringp addl-chars) (setq addl-chars ""))
1397 (viper-skip-syntax
1398 'forward
1399 (cond ((eq viper-syntax-preference 'strict-vi)
1400 "")
1401 (t viper-ALPHA-char-class))
1402 (cond ((eq viper-syntax-preference 'strict-vi)
1403 (concat viper-strict-ALPHA-chars addl-chars))
1404 (t addl-chars))))
1405
1406 (defun viper-skip-alpha-backward (&optional addl-chars)
1407 (or (stringp addl-chars) (setq addl-chars ""))
1408 (viper-skip-syntax
1409 'backward
1410 (cond ((eq viper-syntax-preference 'strict-vi)
1411 "")
1412 (t viper-ALPHA-char-class))
1413 (cond ((eq viper-syntax-preference 'strict-vi)
1414 (concat viper-strict-ALPHA-chars addl-chars))
1415 (t addl-chars))))
1416
1417 ;; weird syntax tables may confuse strict-vi style
1418 (defsubst viper-skip-all-separators-forward (&optional within-line)
1419 (if (eq viper-syntax-preference 'strict-vi)
1420 (if within-line
1421 (skip-chars-forward viper-strict-SEP-chars-sans-newline)
1422 (skip-chars-forward viper-strict-SEP-chars))
1423 (viper-skip-syntax 'forward
1424 viper-SEP-char-class
1425 (or within-line "\n")
1426 (if within-line (viper-line-pos 'end)))))
1427
1428 (defsubst viper-skip-all-separators-backward (&optional within-line)
1429 (if (eq viper-syntax-preference 'strict-vi)
1430 (if within-line
1431 (skip-chars-backward viper-strict-SEP-chars-sans-newline)
1432 (skip-chars-backward viper-strict-SEP-chars))
1433 (viper-skip-syntax 'backward
1434 viper-SEP-char-class
1435 (or within-line "\n")
1436 (if within-line (viper-line-pos 'start)))))
1437 (defun viper-skip-nonseparators (direction)
1438 (viper-skip-syntax
1439 direction
1440 (concat "^" viper-SEP-char-class)
1441 nil
1442 (viper-line-pos (if (eq direction 'forward) 'end 'start))))
1443
1444
1445 ;; skip over non-word constituents and non-separators
1446 (defun viper-skip-nonalphasep-forward ()
1447 (if (eq viper-syntax-preference 'strict-vi)
1448 (skip-chars-forward
1449 (concat "^" viper-strict-SEP-chars viper-strict-ALPHA-chars))
1450 (viper-skip-syntax
1451 'forward
1452 (concat "^" viper-ALPHA-char-class viper-SEP-char-class)
1453 ;; Emacs may consider some of these as words, but we don't want them
1454 viper-non-word-characters
1455 (viper-line-pos 'end))))
1456
1457 (defun viper-skip-nonalphasep-backward ()
1458 (if (eq viper-syntax-preference 'strict-vi)
1459 (skip-chars-backward
1460 (concat "^" viper-strict-SEP-chars viper-strict-ALPHA-chars))
1461 (viper-skip-syntax
1462 'backward
1463 (concat "^" viper-ALPHA-char-class viper-SEP-char-class)
1464 ;; Emacs may consider some of these as words, but we don't want them
1465 viper-non-word-characters
1466 (viper-line-pos 'start))))
1467
1468 ;; Skip SYNTAX like skip-syntax-* and ADDL-CHARS like skip-chars-*
1469 ;; Return the number of chars traveled.
1470 ;; Both SYNTAX or ADDL-CHARS can be strings or lists of characters.
1471 ;; When SYNTAX is "w", then viper-non-word-characters are not considered to be
1472 ;; words, even if Emacs syntax table says they are.
1473 (defun viper-skip-syntax (direction syntax addl-chars &optional limit)
1474 (let ((total 0)
1475 (local 1)
1476 (skip-chars-func
1477 (if (eq direction 'forward)
1478 'skip-chars-forward 'skip-chars-backward))
1479 (skip-syntax-func
1480 (if (eq direction 'forward)
1481 'viper-forward-char-carefully 'viper-backward-char-carefully))
1482 char-looked-at syntax-of-char-looked-at negated-syntax)
1483 (setq addl-chars
1484 (cond ((listp addl-chars) (viper-charlist-to-string addl-chars))
1485 ((stringp addl-chars) addl-chars)
1486 (t "")))
1487 (setq syntax
1488 (cond ((listp syntax) syntax)
1489 ((stringp syntax) (viper-string-to-list syntax))
1490 (t nil)))
1491 (if (memq ?^ syntax) (setq negated-syntax t))
1492
1493 (while (and (not (= local 0))
1494 (cond ((eq direction 'forward)
1495 (not (eobp)))
1496 (t (not (bobp)))))
1497 (setq char-looked-at (viper-char-at-pos direction)
1498 ;; if outside the range, set to nil
1499 syntax-of-char-looked-at (if char-looked-at
1500 (char-syntax char-looked-at)))
1501 (setq local
1502 (+ (if (and
1503 (cond ((and limit (eq direction 'forward))
1504 (< (point) limit))
1505 (limit ; backward & limit
1506 (> (point) limit))
1507 (t t)) ; no limit
1508 ;; char under/before cursor has appropriate syntax
1509 (if negated-syntax
1510 (not (memq syntax-of-char-looked-at syntax))
1511 (memq syntax-of-char-looked-at syntax))
1512 ;; if char-syntax class is "word", make sure it is not one
1513 ;; of the excluded characters
1514 (if (and (eq syntax-of-char-looked-at ?w)
1515 (not negated-syntax))
1516 (not (viper-memq-char
1517 char-looked-at viper-non-word-characters))
1518 t))
1519 (funcall skip-syntax-func 1)
1520 0)
1521 (funcall skip-chars-func addl-chars limit)))
1522 (setq total (+ total local)))
1523 total
1524 ))
1525
1526 ;; tells when point is at the beginning of field
1527 (defun viper-beginning-of-field ()
1528 (or (bobp)
1529 (not (eq (get-char-property (point) 'field)
1530 (get-char-property (1- (point)) 'field)))))
1531
1532
1533 ;; this is copied from cl-extra.el
1534 ;; Return the subsequence of SEQ from START to END.
1535 ;; If END is omitted, it defaults to the length of the sequence.
1536 ;; If START or END is negative, it counts from the end.
1537 (defun viper-subseq (seq start &optional end)
1538 (if (stringp seq) (substring seq start end)
1539 (let (len)
1540 (and end (< end 0) (setq end (+ end (setq len (length seq)))))
1541 (if (< start 0) (setq start (+ start (or len (setq len (length seq))))))
1542 (cond ((listp seq)
1543 (if (> start 0) (setq seq (nthcdr start seq)))
1544 (if end
1545 (let ((res nil))
1546 (while (>= (setq end (1- end)) start)
1547 (push (pop seq) res))
1548 (nreverse res))
1549 (copy-sequence seq)))
1550 (t
1551 (or end (setq end (or len (length seq))))
1552 (let ((res (make-vector (max (- end start) 0) nil))
1553 (i 0))
1554 (while (< start end)
1555 (aset res i (aref seq start))
1556 (setq i (1+ i) start (1+ start)))
1557 res))))))
1558
1559
1560
1561 ;;; Local Variables:
1562 ;;; eval: (put 'viper-deflocalvar 'lisp-indent-hook 'defun)
1563 ;;; End:
1564
1565 ;; arch-tag: 7f023fd5-dd9e-4378-a397-9c179553b0e3
1566 ;;; viper-util.el ends here