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