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