]> code.delx.au - gnu-emacs/blob - lisp/emulation/viper-util.el
*** empty log message ***
[gnu-emacs] / lisp / emulation / viper-util.el
1 ;;; viper-util.el --- Utilities used by viper.el
2
3 ;; Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
4
5 ;; This file is part of GNU Emacs.
6
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; any later version.
11
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
16
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
21
22
23 ;; Code
24
25 (require 'ring)
26
27 ;; Compiler pacifier
28 (defvar vip-overriding-map)
29 (defvar pm-color-alist)
30 (defvar zmacs-region-stays)
31 (defvar vip-search-face)
32 (defvar vip-minibuffer-current-face)
33 (defvar vip-minibuffer-insert-face)
34 (defvar vip-minibuffer-vi-face)
35 (defvar vip-minibuffer-emacs-face)
36 (defvar vip-replace-overlay-face)
37 (defvar vip-minibuffer-overlay)
38 (defvar vip-replace-overlay)
39 (defvar vip-search-overlay)
40 (defvar vip-replace-overlay-cursor-color)
41 (defvar vip-intermediate-command)
42 (defvar vip-use-replace-region-delimiters)
43 (defvar vip-fast-keyseq-timeout)
44 (defvar vip-related-files-and-buffers-ring)
45 ;; end compiler pacifier
46
47 ;; Is it XEmacs?
48 (defconst vip-xemacs-p (string-match "\\(Lucid\\|XEmacs\\)" emacs-version))
49 ;; Is it Emacs?
50 (defconst vip-emacs-p (not vip-xemacs-p))
51 ;; Tell whether we are running as a window application or on a TTY
52 (defsubst vip-device-type ()
53 (if vip-emacs-p
54 window-system
55 (device-type (selected-device))))
56 ;; in XEmacs: device-type is tty on tty and stream in batch.
57 (defun vip-window-display-p ()
58 (and (vip-device-type) (not (memq (vip-device-type) '(tty stream pc)))))
59
60 (defvar vip-ms-style-os-p (memq system-type '(ms-dos windows-nt windows-95))
61 "Tells if Emacs is running under an MS-style OS: ms-dos, windows-nt, W95.")
62 (defvar vip-vms-os-p (memq system-type '(vax-vms axp-vms))
63 "Tells if Emacs is running under VMS.")
64
65 (defvar vip-force-faces nil
66 "If t, Viper will think that it is running on a display that supports faces.
67 This is provided as a temporary relief for users of face-capable displays
68 that Viper doesn't know about.")
69
70 (defun vip-has-face-support-p ()
71 (cond ((vip-window-display-p))
72 (vip-force-faces)
73 (vip-emacs-p (memq (vip-device-type) '(pc)))
74 (vip-xemacs-p (memq (vip-device-type) '(tty pc)))))
75
76 \f
77 ;;; Macros
78
79 (defmacro vip-deflocalvar (var default-value &optional documentation)
80 (` (progn
81 (defvar (, var) (, default-value)
82 (, (format "%s\n\(buffer local\)" documentation)))
83 (make-variable-buffer-local '(, var))
84 )))
85
86 (defmacro vip-loop (count body)
87 "(vip-loop COUNT BODY) Execute BODY COUNT times."
88 (list 'let (list (list 'count count))
89 (list 'while '(> count 0)
90 body
91 '(setq count (1- count))
92 )))
93
94 (defmacro vip-buffer-live-p (buf)
95 (` (and (, buf) (get-buffer (, buf)) (buffer-name (get-buffer (, buf))))))
96
97 ;; return buffer-specific macro definition, given a full macro definition
98 (defmacro vip-kbd-buf-alist (macro-elt)
99 (` (nth 1 (, macro-elt))))
100 ;; get a pair: (curr-buffer . macro-definition)
101 (defmacro vip-kbd-buf-pair (macro-elt)
102 (` (assoc (buffer-name) (vip-kbd-buf-alist (, macro-elt)))))
103 ;; get macro definition for current buffer
104 (defmacro vip-kbd-buf-definition (macro-elt)
105 (` (cdr (vip-kbd-buf-pair (, macro-elt)))))
106
107 ;; return mode-specific macro definitions, given a full macro definition
108 (defmacro vip-kbd-mode-alist (macro-elt)
109 (` (nth 2 (, macro-elt))))
110 ;; get a pair: (major-mode . macro-definition)
111 (defmacro vip-kbd-mode-pair (macro-elt)
112 (` (assoc major-mode (vip-kbd-mode-alist (, macro-elt)))))
113 ;; get macro definition for the current major mode
114 (defmacro vip-kbd-mode-definition (macro-elt)
115 (` (cdr (vip-kbd-mode-pair (, macro-elt)))))
116
117 ;; return global macro definition, given a full macro definition
118 (defmacro vip-kbd-global-pair (macro-elt)
119 (` (nth 3 (, macro-elt))))
120 ;; get global macro definition from an elt of macro-alist
121 (defmacro vip-kbd-global-definition (macro-elt)
122 (` (cdr (vip-kbd-global-pair (, macro-elt)))))
123
124 ;; last elt of a sequence
125 (defsubst vip-seq-last-elt (seq)
126 (elt seq (1- (length seq))))
127
128 ;; Check if arg is a valid character for register
129 ;; TYPE is a list that can contain `letter', `Letter', and `digit'.
130 ;; Letter means lowercase letters, Letter means uppercase letters, and
131 ;; digit means digits from 1 to 9.
132 ;; If TYPE is nil, then down/uppercase letters and digits are allowed.
133 (defun vip-valid-register (reg &optional type)
134 (or type (setq type '(letter Letter digit)))
135 (or (if (memq 'letter type)
136 (and (<= ?a reg) (<= reg ?z)))
137 (if (memq 'digit type)
138 (and (<= ?1 reg) (<= reg ?9)))
139 (if (memq 'Letter type)
140 (and (<= ?A reg) (<= reg ?Z)))
141 ))
142
143 ;; checks if object is a marker, has a buffer, and points to within that buffer
144 (defun vip-valid-marker (marker)
145 (if (and (markerp marker) (marker-buffer marker))
146 (let ((buf (marker-buffer marker))
147 (pos (marker-position marker)))
148 (save-excursion
149 (set-buffer buf)
150 (and (<= pos (point-max)) (<= (point-min) pos))))))
151
152 \f
153 (defvar vip-minibuffer-overlay-priority 300)
154 (defvar vip-replace-overlay-priority 400)
155 (defvar vip-search-overlay-priority 500)
156
157 \f
158 ;;; XEmacs support
159
160 (if vip-xemacs-p
161 (progn
162 (fset 'vip-read-event (symbol-function 'next-command-event))
163 (fset 'vip-make-overlay (symbol-function 'make-extent))
164 (fset 'vip-overlay-start (symbol-function 'extent-start-position))
165 (fset 'vip-overlay-end (symbol-function 'extent-end-position))
166 (fset 'vip-overlay-put (symbol-function 'set-extent-property))
167 (fset 'vip-overlay-p (symbol-function 'extentp))
168 (fset 'vip-overlay-get (symbol-function 'extent-property))
169 (fset 'vip-move-overlay (symbol-function 'set-extent-endpoints))
170 (if (vip-window-display-p)
171 (fset 'vip-iconify (symbol-function 'iconify-frame)))
172 (cond ((vip-has-face-support-p)
173 (fset 'vip-get-face (symbol-function 'get-face))
174 (fset 'vip-color-defined-p
175 (symbol-function 'valid-color-name-p))
176 )))
177 (fset 'vip-read-event (symbol-function 'read-event))
178 (fset 'vip-make-overlay (symbol-function 'make-overlay))
179 (fset 'vip-overlay-start (symbol-function 'overlay-start))
180 (fset 'vip-overlay-end (symbol-function 'overlay-end))
181 (fset 'vip-overlay-put (symbol-function 'overlay-put))
182 (fset 'vip-overlay-p (symbol-function 'overlayp))
183 (fset 'vip-overlay-get (symbol-function 'overlay-get))
184 (fset 'vip-move-overlay (symbol-function 'move-overlay))
185 (if (vip-window-display-p)
186 (fset 'vip-iconify (symbol-function 'iconify-or-deiconify-frame)))
187 (cond ((vip-has-face-support-p)
188 (fset 'vip-get-face (symbol-function 'internal-get-face))
189 (fset 'vip-color-defined-p (symbol-function 'x-color-defined-p))
190 )))
191
192 (fset 'vip-characterp
193 (symbol-function
194 (if vip-xemacs-p 'characterp 'integerp)))
195
196 (defsubst vip-color-display-p ()
197 (if vip-emacs-p
198 (x-display-color-p)
199 (eq (device-class (selected-device)) 'color)))
200
201 (defsubst vip-get-cursor-color ()
202 (if vip-emacs-p
203 (cdr (assoc 'cursor-color (frame-parameters)))
204 (color-instance-name (frame-property (selected-frame) 'cursor-color))))
205
206 (defun vip-set-face-pixmap (face pixmap)
207 "Set face pixmap on a monochrome display."
208 (if (and (vip-window-display-p) (not (vip-color-display-p)))
209 (condition-case nil
210 (set-face-background-pixmap face pixmap)
211 (error
212 (message "Pixmap not found for %S: %s" (face-name face) pixmap)
213 (sit-for 1)))))
214
215
216 ;; OS/2
217 (cond ((eq (vip-device-type) 'pm)
218 (fset 'vip-color-defined-p
219 (function (lambda (color) (assoc color pm-color-alist))))))
220
221 ;; needed to smooth out the difference between Emacs and XEmacs
222 (defsubst vip-italicize-face (face)
223 (if vip-xemacs-p
224 (make-face-italic face)
225 (make-face-italic face nil 'noerror)))
226
227 ;; test if display is color and the colors are defined
228 (defsubst vip-can-use-colors (&rest colors)
229 (if (vip-color-display-p)
230 (not (memq nil (mapcar 'vip-color-defined-p colors)))
231 ))
232
233 (defun vip-hide-face (face)
234 (if (and (vip-has-face-support-p) vip-emacs-p)
235 (add-to-list 'facemenu-unlisted-faces face)))
236
237 ;; cursor colors
238 (defun vip-change-cursor-color (new-color)
239 (if (and (vip-window-display-p) (vip-color-display-p)
240 (stringp new-color) (vip-color-defined-p new-color)
241 (not (string= new-color (vip-get-cursor-color))))
242 (modify-frame-parameters
243 (selected-frame) (list (cons 'cursor-color new-color)))))
244
245 (defsubst vip-save-cursor-color ()
246 (if (and (vip-window-display-p) (vip-color-display-p))
247 (let ((color (vip-get-cursor-color)))
248 (if (and (stringp color) (vip-color-defined-p color)
249 (not (string= color vip-replace-overlay-cursor-color)))
250 (vip-overlay-put vip-replace-overlay 'vip-cursor-color color)))))
251
252 ;; restore cursor color from replace overlay
253 (defsubst vip-restore-cursor-color-after-replace ()
254 (vip-change-cursor-color
255 (vip-overlay-get vip-replace-overlay 'vip-cursor-color)))
256 (defsubst vip-restore-cursor-color-after-insert ()
257 (vip-change-cursor-color vip-saved-cursor-color))
258
259 \f
260 ;; Check the current version against the major and minor version numbers
261 ;; using op: cur-vers op major.minor If emacs-major-version or
262 ;; emacs-minor-version are not defined, we assume that the current version
263 ;; is hopelessly outdated. We assume that emacs-major-version and
264 ;; emacs-minor-version are defined. Otherwise, for Emacs/XEmacs 19, if the
265 ;; current minor version is < 10 (xemacs) or < 23 (emacs) the return value
266 ;; will be nil (when op is =, >, or >=) and t (when op is <, <=), which may be
267 ;; incorrect. However, this gives correct result in our cases, since we are
268 ;; testing for sufficiently high Emacs versions.
269 (defun vip-check-version (op major minor &optional type-of-emacs)
270 (if (and (boundp 'emacs-major-version) (boundp 'emacs-minor-version))
271 (and (cond ((eq type-of-emacs 'xemacs) vip-xemacs-p)
272 ((eq type-of-emacs 'emacs) vip-emacs-p)
273 (t t))
274 (cond ((eq op '=) (and (= emacs-minor-version minor)
275 (= emacs-major-version major)))
276 ((memq op '(> >= < <=))
277 (and (or (funcall op emacs-major-version major)
278 (= emacs-major-version major))
279 (if (= emacs-major-version major)
280 (funcall op emacs-minor-version minor)
281 t)))
282 (t
283 (error "%S: Invalid op in vip-check-version" op))))
284 (cond ((memq op '(= > >=)) nil)
285 ((memq op '(< <=)) t))))
286
287 ;;;; warn if it is a wrong version of emacs
288 ;;(if (or (vip-check-version '< 19 29 'emacs)
289 ;; (vip-check-version '< 19 12 'xemacs))
290 ;; (progn
291 ;; (with-output-to-temp-buffer " *vip-info*"
292 ;; (switch-to-buffer " *vip-info*")
293 ;; (insert
294 ;; (format "
295 ;;
296 ;;This version of Viper requires
297 ;;
298 ;;\t Emacs 19.29 and higher
299 ;;\t OR
300 ;;\t XEmacs 19.12 and higher
301 ;;
302 ;;It is unlikely to work under Emacs version %s
303 ;;that you are using... " emacs-version))
304 ;;
305 ;; (if noninteractive
306 ;; ()
307 ;; (beep 1)
308 ;; (beep 1)
309 ;; (insert "\n\nType any key to continue... ")
310 ;; (vip-read-event)))
311 ;; (kill-buffer " *vip-info*")))
312
313
314 (defun vip-get-visible-buffer-window (wind)
315 (if vip-xemacs-p
316 (get-buffer-window wind t)
317 (get-buffer-window wind 'visible)))
318
319
320 ;; Return line position.
321 ;; If pos is 'start then returns position of line start.
322 ;; If pos is 'end, returns line end. If pos is 'mid, returns line center.
323 ;; Pos = 'indent returns beginning of indentation.
324 ;; Otherwise, returns point. Current point is not moved in any case."
325 (defun vip-line-pos (pos)
326 (let ((cur-pos (point))
327 (result))
328 (cond
329 ((equal pos 'start)
330 (beginning-of-line))
331 ((equal pos 'end)
332 (end-of-line))
333 ((equal pos 'mid)
334 (goto-char (+ (vip-line-pos 'start) (vip-line-pos 'end) 2)))
335 ((equal pos 'indent)
336 (back-to-indentation))
337 (t nil))
338 (setq result (point))
339 (goto-char cur-pos)
340 result))
341
342
343 ;; Like move-marker but creates a virgin marker if arg isn't already a marker.
344 ;; The first argument must eval to a variable name.
345 ;; Arguments: (var-name position &optional buffer).
346 ;;
347 ;; This is useful for moving markers that are supposed to be local.
348 ;; For this, VAR-NAME should be made buffer-local with nil as a default.
349 ;; Then, each time this var is used in `vip-move-marker-locally' in a new
350 ;; buffer, a new marker will be created.
351 (defun vip-move-marker-locally (var pos &optional buffer)
352 (if (markerp (eval var))
353 ()
354 (set var (make-marker)))
355 (move-marker (eval var) pos buffer))
356
357
358 ;; Print CONDITIONS as a message.
359 (defun vip-message-conditions (conditions)
360 (let ((case (car conditions)) (msg (cdr conditions)))
361 (if (null msg)
362 (message "%s" case)
363 (message "%s: %s" case (mapconcat 'prin1-to-string msg " ")))
364 (beep 1)))
365
366
367 \f
368 ;;; List/alist utilities
369
370 ;; Convert LIST to an alist
371 (defun vip-list-to-alist (lst)
372 (let ((alist))
373 (while lst
374 (setq alist (cons (list (car lst)) alist))
375 (setq lst (cdr lst)))
376 alist))
377
378 ;; Convert ALIST to a list.
379 (defun vip-alist-to-list (alst)
380 (let ((lst))
381 (while alst
382 (setq lst (cons (car (car alst)) lst))
383 (setq alst (cdr alst)))
384 lst))
385
386 ;; Filter ALIST using REGEXP. Return alist whose elements match the regexp.
387 (defun vip-filter-alist (regexp alst)
388 (interactive "s x")
389 (let ((outalst) (inalst alst))
390 (while (car inalst)
391 (if (string-match regexp (car (car inalst)))
392 (setq outalst (cons (car inalst) outalst)))
393 (setq inalst (cdr inalst)))
394 outalst))
395
396 ;; Filter LIST using REGEXP. Return list whose elements match the regexp.
397 (defun vip-filter-list (regexp lst)
398 (interactive "s x")
399 (let ((outlst) (inlst lst))
400 (while (car inlst)
401 (if (string-match regexp (car inlst))
402 (setq outlst (cons (car inlst) outlst)))
403 (setq inlst (cdr inlst)))
404 outlst))
405
406
407 ;; Append LIS2 to LIS1, both alists, by side-effect and returns LIS1
408 ;; LIS2 is modified by filtering it: deleting its members of the form
409 ;; \(car elt\) such that (car elt') is in LIS1.
410 (defun vip-append-filter-alist (lis1 lis2)
411 (let ((temp lis1)
412 elt)
413
414 ;;filter-append the second list
415 (while temp
416 ;; delete all occurrences
417 (while (setq elt (assoc (car (car temp)) lis2))
418 (setq lis2 (delq elt lis2)))
419 (setq temp (cdr temp)))
420
421 (nconc lis1 lis2)))
422
423 \f
424 ;;; Support for :e and file globbing
425
426 (defun vip-ex-nontrivial-find-file-unix (filespec)
427 "Glob the file spec and visit all files matching the spec.
428 This function is designed to work under Unix. It may also work under VMS.
429
430 Users who prefer other types of shells should write their own version of this
431 function and set the variable `ex-nontrivial-find-file-function'
432 appropriately."
433 (let ((gshell
434 (cond (ex-unix-type-shell shell-file-name)
435 ((memq system-type '(vax-vms axp-vms)) "*dcl*") ; VAX VMS
436 (t "sh"))) ; probably Unix anyway
437 (gshell-options
438 ;; using cond in anticipation of further additions
439 (cond (ex-unix-type-shell-options)
440 ))
441 (command (cond (vip-ms-style-os-p (format "\"ls -1 -d %s\"" filespec))
442 (t (format "ls -1 -d %s" filespec))))
443 file-list status)
444 (save-excursion
445 (set-buffer (get-buffer-create vip-ex-tmp-buf-name))
446 (erase-buffer)
447 (setq status
448 (if gshell-options
449 (call-process gshell nil t nil
450 gshell-options
451 "-c"
452 command)
453 (call-process gshell nil t nil
454 "-c"
455 command)))
456 (goto-char (point-min))
457 ;; Issue an error, if no match.
458 (if (> status 0)
459 (save-excursion
460 (skip-chars-forward " \t\n\j")
461 (if (looking-at "ls:")
462 (vip-forward-Word 1))
463 (error "%s: %s"
464 (if (stringp gshell)
465 gshell
466 "shell")
467 (buffer-substring (point) (vip-line-pos 'end)))
468 ))
469 (goto-char (point-min))
470 (setq file-list (vip-get-filenames-from-buffer 'one-per-line)))
471
472 (mapcar 'find-file file-list)
473 ))
474
475 (defun vip-ex-nontrivial-find-file-ms (filespec)
476 "Glob the file spec and visit all files matching the spec.
477 This function is designed to work under MS type systems, such as NT, W95, and
478 DOS. It may also work under OS/2.
479
480 The users of Unix-type shells should be able to use
481 `vip-ex-nontrivial-find-file-unix', making it into the value of the variable
482 `ex-nontrivial-find-file-function'. If this doesn't work, the user may have
483 to write a custom function, similar to `vip-ex-nontrivial-find-file-unix'."
484 (save-excursion
485 (set-buffer (get-buffer-create vip-ex-tmp-buf-name))
486 (erase-buffer)
487 (insert filespec)
488 (goto-char (point-min))
489 (mapcar 'find-file
490 (vip-glob-ms-windows-files (vip-get-filenames-from-buffer)))
491 ))
492
493
494 ;; Interpret the stuff in the buffer as a list of file names
495 ;; return a list of file names listed in the buffer beginning at point
496 ;; If optional arg is supplied, assume each filename is listed on a separate
497 ;; line
498 (defun vip-get-filenames-from-buffer (&optional one-per-line)
499 (let ((skip-chars (if one-per-line "\t\n" " \t\n"))
500 result fname delim)
501 (skip-chars-forward skip-chars)
502 (while (not (eobp))
503 (if (cond ((looking-at "\"")
504 (setq delim ?\")
505 (re-search-forward "[^\"]+" nil t)) ; noerror
506 ((looking-at "'")
507 (setq delim ?')
508 (re-search-forward "[^']+" nil t)) ; noerror
509 (t
510 (re-search-forward
511 (concat "[^" skip-chars "]+") nil t))) ;noerror
512 (setq fname
513 (buffer-substring (match-beginning 0) (match-end 0))))
514 (if delim
515 (forward-char 1))
516 (skip-chars-forward " \t\n")
517 (setq result (cons fname result)))
518 result))
519
520 ;; convert MS-DOS wildcards to regexp
521 (defun vip-wildcard-to-regexp (wcard)
522 (save-excursion
523 (set-buffer (get-buffer-create vip-ex-tmp-buf-name))
524 (erase-buffer)
525 (insert wcard)
526 (goto-char (point-min))
527 (while (not (eobp))
528 (skip-chars-forward "^*?.\\\\")
529 (cond ((eq (char-after (point)) ?*) (insert ".")(forward-char 1))
530 ((eq (char-after (point)) ?.) (insert "\\")(forward-char 1))
531 ((eq (char-after (point)) ?\\) (insert "\\")(forward-char 1))
532 ((eq (char-after (point)) ??) (delete-char 1)(insert ".")))
533 )
534 (buffer-string)
535 ))
536
537
538 ;; glob windows files
539 ;; LIST is expected to be in reverse order
540 (defun vip-glob-ms-windows-files (list)
541 (let ((tmp list)
542 (case-fold-search t)
543 tmp2)
544 (while tmp
545 (setq tmp2 (cons (directory-files
546 ;; the directory part
547 (or (file-name-directory (car tmp))
548 "")
549 t ; return full names
550 ;; the regexp part: globs the file names
551 (concat "^"
552 (vip-wildcard-to-regexp
553 (file-name-nondirectory (car tmp)))
554 "$"))
555 tmp2))
556 (setq tmp (cdr tmp)))
557 (reverse (apply 'append tmp2))))
558
559 (defun vip-convert-standard-file-name (fname)
560 (if vip-emacs-p
561 (convert-standard-filename fname)
562 ;; hopefully, XEmacs adds this functionality
563 fname))
564
565
566 \f
567 ;;; Insertion ring
568
569 ;; Rotate RING's index. DIRection can be positive or negative.
570 (defun vip-ring-rotate1 (ring dir)
571 (if (and (ring-p ring) (> (ring-length ring) 0))
572 (progn
573 (setcar ring (cond ((> dir 0)
574 (ring-plus1 (car ring) (ring-length ring)))
575 ((< dir 0)
576 (ring-minus1 (car ring) (ring-length ring)))
577 ;; don't rotate if dir = 0
578 (t (car ring))))
579 (vip-current-ring-item ring)
580 )))
581
582 (defun vip-special-ring-rotate1 (ring dir)
583 (if (memq vip-intermediate-command
584 '(repeating-display-destructive-command
585 repeating-insertion-from-ring))
586 (vip-ring-rotate1 ring dir)
587 ;; don't rotate otherwise
588 (vip-ring-rotate1 ring 0)))
589
590 ;; current ring item; if N is given, then so many items back from the
591 ;; current
592 (defun vip-current-ring-item (ring &optional n)
593 (setq n (or n 0))
594 (if (and (ring-p ring) (> (ring-length ring) 0))
595 (aref (cdr (cdr ring)) (mod (- (car ring) 1 n) (ring-length ring)))))
596
597 ;; push item onto ring. the second argument is a ring-variable, not value.
598 (defun vip-push-onto-ring (item ring-var)
599 (or (ring-p (eval ring-var))
600 (set ring-var (make-ring (eval (intern (format "%S-size" ring-var))))))
601 (or (null item) ; don't push nil
602 (and (stringp item) (string= item "")) ; or empty strings
603 (equal item (vip-current-ring-item (eval ring-var))) ; or old stuff
604 ;; Since vip-set-destructive-command checks if we are inside vip-repeat,
605 ;; we don't check whether this-command-keys is a `.'.
606 ;; The cmd vip-repeat makes a call to the current function only if
607 ;; `.' is executing a command from the command history. It doesn't
608 ;; call the push-onto-ring function if `.' is simply repeating the
609 ;; last destructive command.
610 ;; We only check for ESC (which happens when we do insert with a
611 ;; prefix argument, or if this-command-keys doesn't give anything
612 ;; meaningful (in that case we don't know what to show to the user).
613 (and (eq ring-var 'vip-command-ring)
614 (string-match "\\([0-9]*\e\\|^[ \t]*$\\|escape\\)"
615 (vip-array-to-string (this-command-keys))))
616 (vip-ring-insert (eval ring-var) item))
617 )
618
619
620 ;; removing elts from ring seems to break it
621 (defun vip-cleanup-ring (ring)
622 (or (< (ring-length ring) 2)
623 (null (vip-current-ring-item ring))
624 ;; last and previous equal
625 (if (equal (vip-current-ring-item ring) (vip-current-ring-item ring 1))
626 (vip-ring-pop ring))))
627
628 ;; ring-remove seems to be buggy, so we concocted this for our purposes.
629 (defun vip-ring-pop (ring)
630 (let* ((ln (ring-length ring))
631 (vec (cdr (cdr ring)))
632 (veclen (length vec))
633 (hd (car ring))
634 (idx (max 0 (ring-minus1 hd ln)))
635 (top-elt (aref vec idx)))
636
637 ;; shift elements
638 (while (< (1+ idx) veclen)
639 (aset vec idx (aref vec (1+ idx)))
640 (setq idx (1+ idx)))
641 (aset vec idx nil)
642
643 (setq hd (max 0 (ring-minus1 hd ln)))
644 (if (= hd (1- ln)) (setq hd 0))
645 (setcar ring hd) ; move head
646 (setcar (cdr ring) (max 0 (1- ln))) ; adjust length
647 top-elt
648 ))
649
650 (defun vip-ring-insert (ring item)
651 (let* ((ln (ring-length ring))
652 (vec (cdr (cdr ring)))
653 (veclen (length vec))
654 (hd (car ring))
655 (vecpos-after-hd (if (= hd 0) ln hd))
656 (idx ln))
657
658 (if (= ln veclen)
659 (progn
660 (aset vec hd item) ; hd is always 1+ the actual head index in vec
661 (setcar ring (ring-plus1 hd ln)))
662 (setcar (cdr ring) (1+ ln))
663 (setcar ring (ring-plus1 vecpos-after-hd (1+ ln)))
664 (while (and (>= idx vecpos-after-hd) (> ln 0))
665 (aset vec idx (aref vec (1- idx)))
666 (setq idx (1- idx)))
667 (aset vec vecpos-after-hd item))
668 item))
669
670 \f
671 ;;; String utilities
672
673 ;; If STRING is longer than MAX-LEN, truncate it and print ...... instead
674 ;; PRE-STRING is a string to prepend to the abbrev string.
675 ;; POST-STRING is a string to append to the abbrev string.
676 ;; ABBREV_SIGN is a string to be inserted before POST-STRING
677 ;; if the orig string was truncated.
678 (defun vip-abbreviate-string (string max-len
679 pre-string post-string abbrev-sign)
680 (let (truncated-str)
681 (setq truncated-str
682 (if (stringp string)
683 (substring string 0 (min max-len (length string)))))
684 (cond ((null truncated-str) "")
685 ((> (length string) max-len)
686 (format "%s%s%s%s"
687 pre-string truncated-str abbrev-sign post-string))
688 (t (format "%s%s%s" pre-string truncated-str post-string)))))
689
690 ;; tells if we are over a whitespace-only line
691 (defsubst vip-over-whitespace-line ()
692 (save-excursion
693 (beginning-of-line)
694 (looking-at "^[ \t]*$")))
695
696 \f
697 ;;; Saving settings in custom file
698
699 ;; Save the current setting of VAR in CUSTOM-FILE.
700 ;; If given, MESSAGE is a message to be displayed after that.
701 ;; This message is erased after 2 secs, if erase-msg is non-nil.
702 ;; Arguments: var message custom-file &optional erase-message
703 (defun vip-save-setting (var message custom-file &optional erase-msg)
704 (let* ((var-name (symbol-name var))
705 (var-val (if (boundp var) (eval var)))
706 (regexp (format "^[^;]*%s[ \t\n]*[a-zA-Z---_']*[ \t\n)]" var-name))
707 (buf (find-file-noselect (substitute-in-file-name custom-file)))
708 )
709 (message message)
710 (save-excursion
711 (set-buffer buf)
712 (goto-char (point-min))
713 (if (re-search-forward regexp nil t)
714 (let ((reg-end (1- (match-end 0))))
715 (search-backward var-name)
716 (delete-region (match-beginning 0) reg-end)
717 (goto-char (match-beginning 0))
718 (insert (format "%s '%S" var-name var-val)))
719 (goto-char (point-max))
720 (if (not (bolp)) (insert "\n"))
721 (insert (format "(setq %s '%S)\n" var-name var-val)))
722 (save-buffer))
723 (kill-buffer buf)
724 (if erase-msg
725 (progn
726 (sit-for 2)
727 (message "")))
728 ))
729
730 ;; Save STRING in CUSTOM-FILE. If PATTERN is non-nil, remove strings that
731 ;; match this pattern.
732 (defun vip-save-string-in-file (string custom-file &optional pattern)
733 (let ((buf (find-file-noselect (substitute-in-file-name custom-file))))
734 (save-excursion
735 (set-buffer buf)
736 (goto-char (point-min))
737 (if pattern (delete-matching-lines pattern))
738 (goto-char (point-max))
739 (if string (insert string))
740 (save-buffer))
741 (kill-buffer buf)
742 ))
743
744 \f
745 ;;; Overlays
746
747 ;; Search
748
749 (defun vip-flash-search-pattern ()
750 (if (vip-overlay-p vip-search-overlay)
751 (vip-move-overlay vip-search-overlay (match-beginning 0) (match-end 0))
752 (setq vip-search-overlay
753 (vip-make-overlay
754 (match-beginning 0) (match-end 0) (current-buffer))))
755
756 (vip-overlay-put vip-search-overlay 'priority vip-search-overlay-priority)
757 (if (vip-has-face-support-p)
758 (progn
759 (vip-overlay-put vip-search-overlay 'face vip-search-face)
760 (sit-for 2)
761 (vip-overlay-put vip-search-overlay 'face nil))))
762
763
764 ;; Replace state
765
766 (defsubst vip-move-replace-overlay (beg end)
767 (vip-move-overlay vip-replace-overlay beg end))
768
769 (defun vip-set-replace-overlay (beg end)
770 (if (vip-overlay-p vip-replace-overlay)
771 (vip-move-replace-overlay beg end)
772 (setq vip-replace-overlay (vip-make-overlay beg end (current-buffer)))
773 ;; never detach
774 (vip-overlay-put
775 vip-replace-overlay (if vip-emacs-p 'evaporate 'detachable) nil)
776 (vip-overlay-put
777 vip-replace-overlay 'priority vip-replace-overlay-priority))
778 (if (vip-has-face-support-p)
779 (vip-overlay-put vip-replace-overlay 'face vip-replace-overlay-face))
780 (vip-save-cursor-color)
781 (vip-change-cursor-color vip-replace-overlay-cursor-color)
782 )
783
784
785 (defsubst vip-set-replace-overlay-glyphs (before-glyph after-glyph)
786 (if (or (not (vip-has-face-support-p))
787 vip-use-replace-region-delimiters)
788 (let ((before-name (if vip-xemacs-p 'begin-glyph 'before-string))
789 (after-name (if vip-xemacs-p 'end-glyph 'after-string)))
790 (vip-overlay-put vip-replace-overlay before-name before-glyph)
791 (vip-overlay-put vip-replace-overlay after-name after-glyph))))
792
793 (defsubst vip-hide-replace-overlay ()
794 (vip-set-replace-overlay-glyphs nil nil)
795 (vip-restore-cursor-color-after-replace)
796 (vip-restore-cursor-color-after-insert)
797 (if (vip-has-face-support-p)
798 (vip-overlay-put vip-replace-overlay 'face nil)))
799
800
801 (defsubst vip-replace-start ()
802 (vip-overlay-start vip-replace-overlay))
803 (defsubst vip-replace-end ()
804 (vip-overlay-end vip-replace-overlay))
805
806
807 ;; Minibuffer
808
809 (defun vip-set-minibuffer-overlay ()
810 (vip-check-minibuffer-overlay)
811 (if (vip-has-face-support-p)
812 (progn
813 (vip-overlay-put
814 vip-minibuffer-overlay 'face vip-minibuffer-current-face)
815 (vip-overlay-put
816 vip-minibuffer-overlay 'priority vip-minibuffer-overlay-priority)
817 ;; never detach
818 (vip-overlay-put
819 vip-minibuffer-overlay (if vip-emacs-p 'evaporate 'detachable) nil)
820 ;; make vip-minibuffer-overlay open-ended
821 ;; In emacs, it is made open ended at creation time
822 (if vip-xemacs-p
823 (progn
824 (vip-overlay-put vip-minibuffer-overlay 'start-open nil)
825 (vip-overlay-put vip-minibuffer-overlay 'end-open nil)))
826 )))
827
828 (defun vip-check-minibuffer-overlay ()
829 (or (vip-overlay-p vip-minibuffer-overlay)
830 (setq vip-minibuffer-overlay
831 (if vip-xemacs-p
832 (vip-make-overlay 1 (1+ (buffer-size)) (current-buffer))
833 ;; make overlay open-ended
834 (vip-make-overlay
835 1 (1+ (buffer-size)) (current-buffer) nil 'rear-advance)))
836 ))
837
838
839 (defsubst vip-is-in-minibuffer ()
840 (string-match "\*Minibuf-" (buffer-name)))
841
842
843 \f
844 ;;; XEmacs compatibility
845
846 (defun vip-abbreviate-file-name (file)
847 (if vip-emacs-p
848 (abbreviate-file-name file)
849 ;; XEmacs requires addl argument
850 (abbreviate-file-name file t)))
851
852 ;; Sit for VAL milliseconds. XEmacs doesn't support the millisecond arg
853 ;; in sit-for, so this function smoothes out the differences.
854 (defsubst vip-sit-for-short (val &optional nodisp)
855 (if vip-xemacs-p
856 (sit-for (/ val 1000.0) nodisp)
857 (sit-for 0 val nodisp)))
858
859 ;; EVENT may be a single event of a sequence of events
860 (defsubst vip-ESC-event-p (event)
861 (let ((ESC-keys '(?\e (control \[) escape))
862 (key (vip-event-key event)))
863 (member key ESC-keys)))
864
865
866 (defsubst vip-mark-marker ()
867 (if vip-xemacs-p
868 (mark-marker t)
869 (mark-marker)))
870
871 ;; like (set-mark-command nil) but doesn't push twice, if (car mark-ring)
872 ;; is the same as (mark t).
873 (defsubst vip-set-mark-if-necessary ()
874 (setq mark-ring (delete (vip-mark-marker) mark-ring))
875 (set-mark-command nil))
876
877 ;; In transient mark mode (zmacs mode), it is annoying when regions become
878 ;; highlighted due to Viper's pushing marks. So, we deactivate marks, unless
879 ;; the user explicitly wants highlighting, e.g., by hitting '' or ``
880 (defun vip-deactivate-mark ()
881 (if vip-xemacs-p
882 (zmacs-deactivate-region)
883 (deactivate-mark)))
884
885 (defsubst vip-leave-region-active ()
886 (if vip-xemacs-p
887 (setq zmacs-region-stays t)))
888
889
890 (defsubst vip-events-to-keys (events)
891 (cond (vip-xemacs-p (events-to-keys events))
892 (t events)))
893
894
895 (defun vip-eval-after-load (file form)
896 (if vip-emacs-p
897 (eval-after-load file form)
898 (or (assoc file after-load-alist)
899 (setq after-load-alist (cons (list file) after-load-alist)))
900 (let ((elt (assoc file after-load-alist)))
901 (or (member form (cdr elt))
902 (setq elt (nconc elt (list form)))))
903 form
904 ))
905
906 ;; This is here because Emacs changed the way local hooks work.
907 ;;
908 ;;Add to the value of HOOK the function FUNCTION.
909 ;;FUNCTION is not added if already present.
910 ;;FUNCTION is added (if necessary) at the beginning of the hook list
911 ;;unless the optional argument APPEND is non-nil, in which case
912 ;;FUNCTION is added at the end.
913 ;;
914 ;;HOOK should be a symbol, and FUNCTION may be any valid function. If
915 ;;HOOK is void, it is first set to nil. If HOOK's value is a single
916 ;;function, it is changed to a list of functions."
917 (defun vip-add-hook (hook function &optional append)
918 (if (not (boundp hook)) (set hook nil))
919 ;; If the hook value is a single function, turn it into a list.
920 (let ((old (symbol-value hook)))
921 (if (or (not (listp old)) (eq (car old) 'lambda))
922 (setq old (list old)))
923 (if (member function old)
924 nil
925 (set hook (if append
926 (append old (list function)) ; don't nconc
927 (cons function old))))))
928
929 ;; This is here because of Emacs's changes in the semantics of add/remove-hooks
930 ;; and due to the bugs they introduced.
931 ;;
932 ;; Remove from the value of HOOK the function FUNCTION.
933 ;; HOOK should be a symbol, and FUNCTION may be any valid function. If
934 ;; FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
935 ;; list of hooks to run in HOOK, then nothing is done. See `vip-add-hook'."
936 (defun vip-remove-hook (hook function)
937 (if (or (not (boundp hook)) ;unbound symbol, or
938 (null (symbol-value hook)) ;value is nil, or
939 (null function)) ;function is nil, then
940 nil ;Do nothing.
941 (let ((hook-value (symbol-value hook)))
942 (if (consp hook-value)
943 ;; don't side-effect the list
944 (setq hook-value (delete function (copy-sequence hook-value)))
945 (if (equal hook-value function)
946 (setq hook-value nil)))
947 (set hook hook-value))))
948
949
950
951 ;; like read-event, but in XEmacs also try to convert to char, if possible
952 (defun vip-read-event-convert-to-char ()
953 (let (event)
954 (if vip-emacs-p
955 (read-event)
956 (setq event (next-command-event))
957 (or (event-to-character event)
958 event))
959 ))
960
961 ;; This function lets function-key-map convert key sequences into logical
962 ;; keys. This does a better job than vip-read-event when it comes to kbd
963 ;; macros, since it enables certain macros to be shared between X and TTY modes
964 ;; by correctly mapping key sequences for Left/Right/... (one an ascii
965 ;; terminal) into logical keys left, right, etc.
966 (defun vip-read-key ()
967 (let ((overriding-local-map vip-overriding-map)
968 (inhibit-quit t)
969 key)
970 (use-global-map vip-overriding-map)
971 (setq key (elt (read-key-sequence nil) 0))
972 (use-global-map global-map)
973 key))
974
975
976 ;; Emacs has a bug in eventp, which causes (eventp nil) to return (nil)
977 ;; instead of nil, if '(nil) was previously inadvertently assigned to
978 ;; unread-command-events
979 (defun vip-event-key (event)
980 (or (and event (eventp event))
981 (error "vip-event-key: Wrong type argument, eventp, %S" event))
982 (let ((mod (event-modifiers event))
983 basis)
984 (setq basis
985 (cond
986 (vip-xemacs-p
987 (cond ((key-press-event-p event)
988 (event-key event))
989 ((button-event-p event)
990 (concat "mouse-" (prin1-to-string (event-button event))))
991 (t
992 (error "vip-event-key: Unknown event, %S" event))))
993 (t
994 ;; Emacs doesn't handle capital letters correctly, since
995 ;; \S-a isn't considered the same as A (it behaves as
996 ;; plain `a' instead). So we take care of this here
997 (cond ((and (vip-characterp event) (<= ?A event) (<= event ?Z))
998 (setq mod nil
999 event event))
1000 ;; Emacs has the oddity whereby characters 128+char
1001 ;; represent M-char *if* this appears inside a string.
1002 ;; So, we convert them manually to (meta char).
1003 ((and (vip-characterp event) (< ?\C-? event) (<= event 255))
1004 (setq mod '(meta)
1005 event (- event ?\C-? 1)))
1006 (t (event-basic-type event)))
1007 )))
1008 (if (vip-characterp basis)
1009 (setq basis
1010 (if (= basis ?\C-?)
1011 (list 'control '\?) ; taking care of an emacs bug
1012 (intern (char-to-string basis)))))
1013 (if mod
1014 (append mod (list basis))
1015 basis)))
1016
1017 (defun vip-key-to-emacs-key (key)
1018 (let (key-name char-p modifiers mod-char-list base-key base-key-name)
1019 (cond (vip-xemacs-p key)
1020 ((symbolp key)
1021 (setq key-name (symbol-name key))
1022 (if (= (length key-name) 1) ; character event
1023 (string-to-char key-name)
1024 key))
1025 ((listp key)
1026 (setq modifiers (subseq key 0 (1- (length key)))
1027 base-key (vip-seq-last-elt key)
1028 base-key-name (symbol-name base-key)
1029 char-p (= (length base-key-name) 1))
1030 (setq mod-char-list
1031 (mapcar
1032 '(lambda (elt) (upcase (substring (symbol-name elt) 0 1)))
1033 modifiers))
1034 (if char-p
1035 (setq key-name
1036 (car (read-from-string
1037 (concat
1038 "?\\"
1039 (mapconcat 'identity mod-char-list "-\\")
1040 "-"
1041 base-key-name))))
1042 (setq key-name
1043 (intern
1044 (concat
1045 (mapconcat 'identity mod-char-list "-")
1046 "-"
1047 base-key-name))))))
1048 ))
1049
1050
1051 ;; Args can be a sequence of events, a string, or a Viper macro. Will try to
1052 ;; convert events to keys and, if all keys are regular printable
1053 ;; characters, will return a string. Otherwise, will return a string
1054 ;; representing a vector of converted events. If the input was a Viper macro,
1055 ;; will return a string that represents this macro as a vector.
1056 (defun vip-array-to-string (event-seq)
1057 (let (temp temp2)
1058 (cond ((stringp event-seq) event-seq)
1059 ((vip-event-vector-p event-seq)
1060 (setq temp (mapcar 'vip-event-key event-seq))
1061 (cond ((vip-char-symbol-sequence-p temp)
1062 (mapconcat 'symbol-name temp ""))
1063 ((and (vip-char-array-p
1064 (setq temp2 (mapcar 'vip-key-to-character temp))))
1065 (mapconcat 'char-to-string temp2 ""))
1066 (t (prin1-to-string (vconcat temp)))))
1067 ((vip-char-symbol-sequence-p event-seq)
1068 (mapconcat 'symbol-name event-seq ""))
1069 ((and (vectorp event-seq)
1070 (vip-char-array-p
1071 (setq temp (mapcar 'vip-key-to-character event-seq))))
1072 (mapconcat 'char-to-string temp ""))
1073 (t (prin1-to-string event-seq)))))
1074
1075 (defun vip-key-press-events-to-chars (events)
1076 (mapconcat (if vip-emacs-p
1077 'char-to-string
1078 (function
1079 (lambda (elt) (char-to-string (event-to-character elt)))))
1080 events
1081 ""))
1082
1083
1084 (defsubst vip-fast-keysequence-p ()
1085 (not (vip-sit-for-short vip-fast-keyseq-timeout t)))
1086
1087 (defun vip-read-char-exclusive ()
1088 (let (char
1089 (echo-keystrokes 1))
1090 (while (null char)
1091 (condition-case nil
1092 (setq char (read-char))
1093 (error
1094 ;; skip event if not char
1095 (vip-read-event))))
1096 char))
1097
1098 ;; key is supposed to be in viper's representation, e.g., (control l), a
1099 ;; character, etc.
1100 (defun vip-key-to-character (key)
1101 (cond ((eq key 'space) ?\ )
1102 ((eq key 'delete) ?\C-?)
1103 ((eq key 'backspace) ?\C-h)
1104 ((and (symbolp key)
1105 (= 1 (length (symbol-name key))))
1106 (string-to-char (symbol-name key)))
1107 ((and (listp key)
1108 (eq (car key) 'control)
1109 (symbol-name (nth 1 key))
1110 (= 1 (length (symbol-name (nth 1 key)))))
1111 (read (format "?\\C-%s" (symbol-name (nth 1 key)))))
1112 (t key)))
1113
1114
1115 (defun vip-setup-master-buffer (&rest other-files-or-buffers)
1116 "Set up the current buffer as a master buffer.
1117 Arguments become related buffers. This function should normally be used in
1118 the `Local variables' section of a file."
1119 (setq vip-related-files-and-buffers-ring
1120 (make-ring (1+ (length other-files-or-buffers))))
1121 (mapcar '(lambda (elt)
1122 (vip-ring-insert vip-related-files-and-buffers-ring elt))
1123 other-files-or-buffers)
1124 (vip-ring-insert vip-related-files-and-buffers-ring (buffer-name))
1125 )
1126
1127 ;;; Movement utilities
1128
1129 (defvar vip-syntax-preference 'strict-vi
1130 "*Syntax type characterizing Viper's alphanumeric symbols.
1131 `emacs' means only word constituents are considered to be alphanumeric.
1132 Word constituents are symbols specified as word constituents by the current
1133 syntax table.
1134 `extended' means word and symbol constituents.
1135 `reformed-vi' means Vi-ish behavior: word constituents and the symbol `_'.
1136 However, word constituents are determined according to Emacs syntax tables,
1137 which may be different from Vi in some major modes.
1138 `strict-vi' means Viper words are exactly as in Vi.")
1139
1140 (vip-deflocalvar vip-ALPHA-char-class "w"
1141 "String of syntax classes characterizing Viper's alphanumeric symbols.
1142 In addition, the symbol `_' may be considered alphanumeric if
1143 `vip-syntax-preference'is `reformed-vi'.")
1144
1145 (vip-deflocalvar vip-strict-ALPHA-chars "a-zA-Z0-9_"
1146 "Regexp matching the set of alphanumeric characters acceptable to strict
1147 Vi.")
1148 (vip-deflocalvar vip-strict-SEP-chars " \t\n"
1149 "Regexp matching the set of alphanumeric characters acceptable to strict
1150 Vi.")
1151
1152 (vip-deflocalvar vip-SEP-char-class " -"
1153 "String of syntax classes for Vi separators.
1154 Usually contains ` ', linefeed, TAB or formfeed.")
1155
1156 (defun vip-update-alphanumeric-class ()
1157 "Set the syntax class of Viper alphanumerals according to `vip-syntax-preference'.
1158 Must be called in order for changes to `vip-syntax-preference' to take effect."
1159 (interactive)
1160 (setq-default
1161 vip-ALPHA-char-class
1162 (cond ((eq vip-syntax-preference 'emacs) "w") ; only word constituents
1163 ((eq vip-syntax-preference 'extended) "w_") ; word & symbol chars
1164 (t "w")))) ; vi syntax: word constituents and the symbol `_'
1165
1166 ;; addl-chars are characters to be temporarily considered as alphanumerical
1167 (defun vip-looking-at-alpha (&optional addl-chars)
1168 (or (stringp addl-chars) (setq addl-chars ""))
1169 (if (eq vip-syntax-preference 'reformed-vi)
1170 (setq addl-chars (concat addl-chars "_")))
1171 (let ((char (char-after (point))))
1172 (if char
1173 (if (eq vip-syntax-preference 'strict-vi)
1174 (looking-at (concat "[" vip-strict-ALPHA-chars addl-chars "]"))
1175 (or (memq char
1176 ;; convert string to list
1177 (append (vconcat addl-chars) nil))
1178 (memq (char-syntax char)
1179 (append (vconcat vip-ALPHA-char-class) nil)))))
1180 ))
1181
1182 (defsubst vip-looking-at-separator ()
1183 (let ((char (char-after (point))))
1184 (if char
1185 (or (eq char ?\n) ; RET is always a separator in Vi
1186 (memq (char-syntax char)
1187 (append (vconcat vip-SEP-char-class) nil))))))
1188
1189 (defsubst vip-looking-at-alphasep (&optional addl-chars)
1190 (or (vip-looking-at-separator) (vip-looking-at-alpha addl-chars)))
1191
1192 (defsubst vip-skip-alpha-forward (&optional addl-chars)
1193 (or (stringp addl-chars) (setq addl-chars ""))
1194 (vip-skip-syntax
1195 'forward
1196 (cond ((eq vip-syntax-preference 'strict-vi)
1197 "")
1198 (t vip-ALPHA-char-class ))
1199 (cond ((eq vip-syntax-preference 'strict-vi)
1200 (concat vip-strict-ALPHA-chars addl-chars))
1201 (t addl-chars))))
1202
1203 (defsubst vip-skip-alpha-backward (&optional addl-chars)
1204 (or (stringp addl-chars) (setq addl-chars ""))
1205 (vip-skip-syntax
1206 'backward
1207 (cond ((eq vip-syntax-preference 'strict-vi)
1208 "")
1209 (t vip-ALPHA-char-class ))
1210 (cond ((eq vip-syntax-preference 'strict-vi)
1211 (concat vip-strict-ALPHA-chars addl-chars))
1212 (t addl-chars))))
1213
1214 ;; weird syntax tables may confuse strict-vi style
1215 (defsubst vip-skip-all-separators-forward (&optional within-line)
1216 (vip-skip-syntax 'forward
1217 vip-SEP-char-class
1218 (or within-line "\n")
1219 (if within-line (vip-line-pos 'end))))
1220 (defsubst vip-skip-all-separators-backward (&optional within-line)
1221 (vip-skip-syntax 'backward
1222 vip-SEP-char-class
1223 (or within-line "\n")
1224 (if within-line (vip-line-pos 'start))))
1225 (defun vip-skip-nonseparators (direction)
1226 (let ((func (intern (format "skip-syntax-%S" direction))))
1227 (funcall func (concat "^" vip-SEP-char-class)
1228 (vip-line-pos (if (eq direction 'forward) 'end 'start)))))
1229
1230 (defsubst vip-skip-nonalphasep-forward ()
1231 (if (eq vip-syntax-preference 'strict-vi)
1232 (skip-chars-forward
1233 (concat "^" vip-strict-SEP-chars vip-strict-ALPHA-chars))
1234 (skip-syntax-forward
1235 (concat
1236 "^" vip-ALPHA-char-class vip-SEP-char-class) (vip-line-pos 'end))))
1237 (defsubst vip-skip-nonalphasep-backward ()
1238 (if (eq vip-syntax-preference 'strict-vi)
1239 (skip-chars-backward
1240 (concat "^" vip-strict-SEP-chars vip-strict-ALPHA-chars))
1241 (skip-syntax-backward
1242 (concat
1243 "^" vip-ALPHA-char-class vip-SEP-char-class) (vip-line-pos 'start))))
1244
1245 ;; Skip SYNTAX like skip-syntax-* and ADDL-CHARS like skip-chars-*
1246 ;; Return the number of chars traveled.
1247 ;; Either SYNTAX or ADDL-CHARS can be nil, in which case they are interpreted
1248 ;; as an empty string.
1249 (defun vip-skip-syntax (direction syntax addl-chars &optional limit)
1250 (let ((total 0)
1251 (local 1)
1252 (skip-chars-func (intern (format "skip-chars-%S" direction)))
1253 (skip-syntax-func (intern (format "skip-syntax-%S" direction))))
1254 (or (stringp addl-chars) (setq addl-chars ""))
1255 (or (stringp syntax) (setq syntax ""))
1256 (while (and (not (= local 0)) (not (eobp)))
1257 (setq local
1258 (+ (funcall skip-syntax-func syntax limit)
1259 (funcall skip-chars-func addl-chars limit)))
1260 (setq total (+ total local)))
1261 total
1262 ))
1263
1264
1265
1266
1267 (provide 'viper-util)
1268
1269 ;;; viper-util.el ends here