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