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