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