Merge from emacs--devo--0
[bpt/emacs.git] / lisp / emulation / viper-init.el
1 ;;; viper-init.el --- some common definitions for Viper
2
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
5
6 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 ;; compiler pacifier
30 (defvar mark-even-if-inactive)
31 (defvar quail-mode)
32 (defvar iso-accents-mode)
33 (defvar viper-current-state)
34 (defvar viper-version)
35 (defvar viper-expert-level)
36 (defvar current-input-method)
37 (defvar default-input-method)
38 (defvar describe-current-input-method-function)
39 (defvar bar-cursor)
40 (defvar default-cursor-type)
41 (defvar cursor-type)
42 ;; end pacifier
43
44
45 ;; Viper version
46 (defun viper-version ()
47 (interactive)
48 (message "Viper version is %s" viper-version))
49
50 ;; Tell whether we are running as a window application or on a TTY
51
52 ;; This is used to avoid compilation warnings. When emacs/xemacs forms can
53 ;; generate compile time warnings, we use this macro.
54 ;; In this case, the macro will expand into the form that is appropriate to the
55 ;; compiler at hand.
56 ;; Suggested by rms.
57 (defmacro viper-cond-compile-for-xemacs-or-emacs (xemacs-form emacs-form)
58 (if (featurep 'xemacs)
59 xemacs-form emacs-form))
60
61
62 (defsubst viper-device-type ()
63 (viper-cond-compile-for-xemacs-or-emacs
64 (device-type (selected-device))
65 window-system
66 ))
67
68 (defun viper-color-display-p ()
69 (condition-case nil
70 (viper-cond-compile-for-xemacs-or-emacs
71 (eq (device-class (selected-device)) 'color) ; xemacs form
72 (if (fboundp 'display-color-p) ; emacs form
73 (display-color-p)
74 (x-display-color-p))
75 )
76 (error nil)))
77
78 ;; in XEmacs: device-type is tty on tty and stream in batch.
79 (defun viper-window-display-p ()
80 (and (viper-device-type) (not (memq (viper-device-type) '(tty stream pc)))))
81
82 (defcustom viper-ms-style-os-p (memq system-type
83 '(ms-dos windows-nt windows-95))
84 "Tells if Emacs is running under an MS-style OS: ms-dos, windows-nt, W95."
85 :type 'boolean
86 :tag "Is it Microsoft-made OS?"
87 :group 'viper-misc)
88 (defcustom viper-vms-os-p (memq system-type '(vax-vms axp-vms))
89 "Tells if Emacs is running under VMS."
90 :type 'boolean
91 :tag "Is it VMS?"
92 :group 'viper-misc)
93
94 (defcustom viper-suppress-input-method-change-message nil
95 "If t, the message notifying about changes in the input method is not displayed.
96 Normally, a message is displayed each time on enters the vi, insert or replace
97 state."
98 :type 'boolean
99 :group 'viper-misc)
100
101 (defcustom viper-force-faces nil
102 "If t, Viper will think that it is running on a display that supports faces.
103 This is provided as a temporary relief for users of graphics-capable terminals
104 that Viper doesn't know about.
105 In all likelihood, you don't need to bother with this setting."
106 :type 'boolean
107 :group 'viper-highlighting)
108
109 (defun viper-has-face-support-p ()
110 (cond ((viper-window-display-p))
111 (viper-force-faces)
112 ((viper-color-display-p))
113 ((featurep 'emacs) (memq (viper-device-type) '(pc)))
114 ((featurep 'xemacs) (memq (viper-device-type) '(tty pc)))))
115
116 \f
117 ;;; Macros
118
119 (defmacro viper-deflocalvar (var default-value &optional documentation)
120 `(progn
121 (defvar ,var ,default-value
122 ,(format "%s\n\(buffer local\)" documentation))
123 (make-variable-buffer-local ',var)))
124
125 ;; (viper-loop COUNT BODY) Execute BODY COUNT times.
126 (defmacro viper-loop (count &rest body)
127 `(let ((count ,count))
128 (while (> count 0)
129 ,@body
130 (setq count (1- count)))))
131
132 (defmacro viper-buffer-live-p (buf)
133 `(and ,buf (get-buffer ,buf) (buffer-name (get-buffer ,buf))))
134
135 ;; return buffer-specific macro definition, given a full macro definition
136 (defmacro viper-kbd-buf-alist (macro-elt)
137 `(nth 1 ,macro-elt))
138 ;; get a pair: (curr-buffer . macro-definition)
139 (defmacro viper-kbd-buf-pair (macro-elt)
140 `(assoc (buffer-name) (viper-kbd-buf-alist ,macro-elt)))
141 ;; get macro definition for current buffer
142 (defmacro viper-kbd-buf-definition (macro-elt)
143 `(cdr (viper-kbd-buf-pair ,macro-elt)))
144
145 ;; return mode-specific macro definitions, given a full macro definition
146 (defmacro viper-kbd-mode-alist (macro-elt)
147 `(nth 2 ,macro-elt))
148 ;; get a pair: (major-mode . macro-definition)
149 (defmacro viper-kbd-mode-pair (macro-elt)
150 `(assoc major-mode (viper-kbd-mode-alist ,macro-elt)))
151 ;; get macro definition for the current major mode
152 (defmacro viper-kbd-mode-definition (macro-elt)
153 `(cdr (viper-kbd-mode-pair ,macro-elt)))
154
155 ;; return global macro definition, given a full macro definition
156 (defmacro viper-kbd-global-pair (macro-elt)
157 `(nth 3 ,macro-elt))
158 ;; get global macro definition from an elt of macro-alist
159 (defmacro viper-kbd-global-definition (macro-elt)
160 `(cdr (viper-kbd-global-pair ,macro-elt)))
161
162 ;; last elt of a sequence
163 (defsubst viper-seq-last-elt (seq)
164 (elt seq (1- (length seq))))
165
166 (defsubst viper-string-to-list (string)
167 (append (vconcat string) nil))
168
169 (defsubst viper-charlist-to-string (list)
170 (mapconcat 'char-to-string list ""))
171
172 ;; like char-after/before, but saves typing
173 (defun viper-char-at-pos (direction &optional offset)
174 (or (integerp offset) (setq offset 0))
175 (if (eq direction 'forward)
176 (char-after (+ (point) offset))
177 (char-before (- (point) offset))))
178
179 \f
180 (defvar viper-minibuffer-overlay-priority 300)
181 (defvar viper-replace-overlay-priority 400)
182 (defvar viper-search-overlay-priority 500)
183
184 \f
185 ;;; Viper minor modes
186
187 ;; Mode for vital things like \e, C-z.
188 (viper-deflocalvar viper-vi-intercept-minor-mode nil)
189
190 (viper-deflocalvar viper-vi-basic-minor-mode nil
191 "Viper's minor mode for Vi bindings.")
192
193 (viper-deflocalvar viper-vi-local-user-minor-mode nil
194 "Auxiliary minor mode for user-defined local bindings in Vi state.")
195
196 (viper-deflocalvar viper-vi-global-user-minor-mode nil
197 "Auxiliary minor mode for user-defined global bindings in Vi state.")
198
199 (viper-deflocalvar viper-vi-state-modifier-minor-mode nil
200 "Minor mode used to make major-mode-specific modification to Vi state.")
201
202 (viper-deflocalvar viper-vi-diehard-minor-mode nil
203 "This minor mode is in effect when the user wants Viper to be Vi.")
204
205 (viper-deflocalvar viper-vi-kbd-minor-mode nil
206 "Minor mode for Ex command macros in Vi state.
207 The corresponding keymap stores key bindings of Vi macros defined with
208 the Ex command :map.")
209
210 ;; Mode for vital things like \e, C-z.
211 (viper-deflocalvar viper-insert-intercept-minor-mode nil)
212
213 (viper-deflocalvar viper-insert-basic-minor-mode nil
214 "Viper's minor mode for bindings in Insert mode.")
215
216 (viper-deflocalvar viper-insert-local-user-minor-mode nil
217 "Auxiliary minor mode for buffer-local user-defined bindings in Insert state.
218 This is a way to overshadow normal Insert mode bindings locally to certain
219 designated buffers.")
220
221 (viper-deflocalvar viper-insert-global-user-minor-mode nil
222 "Auxiliary minor mode for global user-defined bindings in Insert state.")
223
224 (viper-deflocalvar viper-insert-state-modifier-minor-mode nil
225 "Minor mode used to make major-mode-specific modification to Insert state.")
226
227 (viper-deflocalvar viper-insert-diehard-minor-mode nil
228 "Minor mode that simulates Vi very closely.
229 Not recommened, except for the novice user.")
230
231 (viper-deflocalvar viper-insert-kbd-minor-mode nil
232 "Minor mode for Ex command macros Insert state.
233 The corresponding keymap stores key bindings of Vi macros defined with
234 the Ex command :map!.")
235
236 (viper-deflocalvar viper-replace-minor-mode nil
237 "Minor mode in effect in replace state (cw, C, and the like commands).")
238
239 ;; Mode for vital things like \C-z and \C-x) This is set to t, when viper-mode
240 ;; is invoked. So, any new buffer will have C-z defined as switch to Vi,
241 ;; unless we switched states in this buffer
242 (viper-deflocalvar viper-emacs-intercept-minor-mode nil)
243
244 (viper-deflocalvar viper-emacs-local-user-minor-mode nil
245 "Minor mode for local user bindings effective in Emacs state.
246 Users can use it to override Emacs bindings when Viper is in its Emacs
247 state.")
248
249 (viper-deflocalvar viper-emacs-global-user-minor-mode nil
250 "Minor mode for global user bindings in effect in Emacs state.
251 Users can use it to override Emacs bindings when Viper is in its Emacs
252 state.")
253
254 (viper-deflocalvar viper-emacs-kbd-minor-mode nil
255 "Minor mode for Vi style macros in Emacs state.
256 The corresponding keymap stores key bindings of Vi macros defined with
257 `viper-record-kbd-macro' command. There is no Ex-level command to do this
258 interactively.")
259
260 (viper-deflocalvar viper-emacs-state-modifier-minor-mode nil
261 "Minor mode used to make major-mode-specific modification to Emacs state.
262 For instance, a Vi purist may want to bind `dd' in Dired mode to a function
263 that deletes a file.")
264
265 (viper-deflocalvar viper-vi-minibuffer-minor-mode nil
266 "Minor mode that forces Vi-style when the Minibuffer is in Vi state.")
267
268 (viper-deflocalvar viper-insert-minibuffer-minor-mode nil
269 "Minor mode that forces Vi-style when the Minibuffer is in Insert state.")
270
271 \f
272
273 ;; Some common error messages
274
275 (defconst viper-SpuriousText "Spurious text after command" "")
276 (defconst viper-BadExCommand "Not an editor command" "")
277 (defconst viper-InvalidCommandArgument "Invalid command argument" "")
278 (defconst viper-NoPrevSearch "No previous search string" "")
279 (defconst viper-EmptyRegister "`%c': Nothing in this register" "")
280 (defconst viper-InvalidRegister "`%c': Invalid register" "")
281 (defconst viper-EmptyTextmarker "`%c': Text marker doesn't point anywhere" "")
282 (defconst viper-InvalidTextmarker "`%c': Invalid text marker" "")
283 (defconst viper-InvalidViCommand "Invalid command" "")
284 (defconst viper-BadAddress "Ill-formed address" "")
285 (defconst viper-FirstAddrExceedsSecond "First address exceeds second" "")
286 (defconst viper-NoFileSpecified "No file specified" "")
287
288 ;; Is t until viper-mode executes for the very first time.
289 ;; Prevents recursive descend into startup messages.
290 (defvar viper-first-time t)
291
292 (defvar viper-expert-level (if (boundp 'viper-expert-level) viper-expert-level 0)
293 "User's expert level.
294 The minor mode viper-vi-diehard-minor-mode is in effect when
295 viper-expert-level is 1 or 2 or when viper-want-emacs-keys-in-vi is t.
296 The minor mode viper-insert-diehard-minor-mode is in effect when
297 viper-expert-level is 1 or 2 or if viper-want-emacs-keys-in-insert is t.
298 Use `M-x viper-set-expert-level' to change this.")
299
300 ;; Max expert level supported by Viper. This is NOT a user option.
301 ;; It is here to make it hard for the user from resetting it.
302 (defconst viper-max-expert-level 5)
303
304
305 ;;; ISO characters and MULE
306
307 ;; If non-nil, ISO accents will be turned on in insert/replace emacs states and
308 ;; turned off in vi-state. For some users, this behavior may be too
309 ;; primitive. In this case, use insert/emacs/vi state hooks.
310 (viper-deflocalvar viper-automatic-iso-accents nil "")
311 ;; Set iso-accents-mode to ARG. Check if it is bound first
312 (defsubst viper-set-iso-accents-mode (arg)
313 (if (boundp 'iso-accents-mode)
314 (setq iso-accents-mode arg)))
315
316 ;; Internal flag used to control when viper mule hooks are run.
317 ;; Don't change this!
318 (defvar viper-mule-hook-flag t)
319 ;; If non-nil, the default intl. input method is turned on.
320 (viper-deflocalvar viper-special-input-method nil "")
321
322 ;; viper hook to run on input-method activation
323 (defun viper-activate-input-method-action ()
324 (if (null viper-mule-hook-flag)
325 ()
326 (setq viper-special-input-method t)
327 ;; turn off special input methods in vi-state
328 (if (eq viper-current-state 'vi-state)
329 (viper-set-input-method nil))
330 (if (and (memq viper-current-state '(vi-state insert-state replace-state))
331 (not viper-suppress-input-method-change-message))
332 (message "Viper special input method%s: on"
333 (if (or current-input-method default-input-method)
334 (format " %S"
335 (or current-input-method default-input-method))
336 "")))
337 ))
338
339 ;; viper hook to run on input-method deactivation
340 (defun viper-inactivate-input-method-action ()
341 (if (null viper-mule-hook-flag)
342 ()
343 (setq viper-special-input-method nil)
344 (if (and (memq viper-current-state '(vi-state insert-state replace-state))
345 (not viper-suppress-input-method-change-message))
346 (message "Viper special input method%s: off"
347 (if (or current-input-method default-input-method)
348 (format " %S"
349 (or current-input-method default-input-method))
350 "")))))
351
352 (defun viper-inactivate-input-method ()
353 (cond ((and (featurep 'emacs) (fboundp 'inactivate-input-method))
354 (inactivate-input-method))
355 ((and (featurep 'xemacs) (boundp 'current-input-method))
356 ;; XEmacs had broken quil-mode for some time, so we are working around
357 ;; it here
358 (setq quail-mode nil)
359 (if (featurep 'quail)
360 (quail-delete-overlays))
361 (setq describe-current-input-method-function nil)
362 (setq current-input-method nil)
363 (run-hooks 'input-method-inactivate-hook)
364 (force-mode-line-update))
365 ))
366 (defun viper-activate-input-method ()
367 (cond ((and (featurep 'emacs) (fboundp 'activate-input-method))
368 (activate-input-method default-input-method))
369 ((featurep 'xemacs)
370 (if (fboundp 'quail-mode) (quail-mode 1)))))
371
372 ;; Set quail-mode to ARG
373 (defun viper-set-input-method (arg)
374 (setq viper-mule-hook-flag t) ; just a precaution
375 (let (viper-mule-hook-flag) ; temporarily deactivate viper mule hooks
376 (cond ((and arg (> (prefix-numeric-value arg) 0) default-input-method)
377 ;; activate input method
378 (viper-activate-input-method))
379 (t ; deactivate input method
380 (viper-inactivate-input-method)))
381 ))
382
383
384 ;; VI-style Undo
385
386 ;; Used to 'undo' complex commands, such as replace and insert commands.
387 (viper-deflocalvar viper-undo-needs-adjustment nil)
388 (put 'viper-undo-needs-adjustment 'permanent-local t)
389
390 ;; A mark that Viper puts on buffer-undo-list. Marks the beginning of a
391 ;; complex command that must be undone atomically. If inserted, it is
392 ;; erased by viper-change-state-to-vi and viper-repeat.
393 (defconst viper-buffer-undo-list-mark 'viper)
394
395 (defcustom viper-keep-point-on-undo nil
396 "*Non-nil means not to move point while undoing commands.
397 This style is different from Emacs and Vi. Try it to see if
398 it better fits your working style."
399 :type 'boolean
400 :tag "Preserve Position of Point After Undo"
401 :group 'viper)
402
403 ;; Replace mode and changing text
404
405 ;; Hack used to pass global states around for short period of time
406 (viper-deflocalvar viper-intermediate-command nil "")
407
408 ;; This is used to pass the right Vi command key sequence to
409 ;; viper-set-destructive-command whenever (this-command-keys) doesn't give the
410 ;; right result. For instance, in commands like c/bla<RET>,
411 ;; (this-command-keys) will return ^M, which invoked exit-minibuffer, while we
412 ;; need "c/"
413 (defconst viper-this-command-keys nil)
414
415 ;; Indicates that the current destructive command has started in replace mode.
416 (viper-deflocalvar viper-began-as-replace nil "")
417
418 (defcustom viper-allow-multiline-replace-regions t
419 "If non-nil, Viper will allow multi-line replace regions.
420 This is an extension to standard Vi.
421 If nil, commands that attempt to replace text spanning multiple lines first
422 delete the text being replaced, as in standard Vi."
423 :type 'boolean
424 :group 'viper)
425
426 (defcustom viper-replace-overlay-cursor-color "Red"
427 "*Cursor color when Viper is in Replace state."
428 :type 'string
429 :group 'viper)
430
431 (defcustom viper-insert-state-cursor-color "Green"
432 "Cursor color when Viper is in insert state."
433 :type 'string
434 :group 'viper)
435
436 ;; viper-emacs-state-cursor-color doesn't work well. Causes cursor colors to be
437 ;; confused in some cases. So, this var is nulled for now.
438 ;; (defcustom viper-emacs-state-cursor-color "Magenta"
439 (defcustom viper-emacs-state-cursor-color nil
440 "Cursor color when Viper is in Emacs state."
441 :type 'string
442 :group 'viper)
443
444 ;; internal var, used to remember the default cursor color of emacs frames
445 (defvar viper-vi-state-cursor-color nil)
446
447 (if (fboundp 'make-variable-frame-local)
448 (dolist (v '(viper-replace-overlay-cursor-color
449 viper-insert-state-cursor-color viper-emacs-state-cursor-color
450 viper-vi-state-cursor-color))
451 (make-variable-frame-local v)))
452
453 (viper-deflocalvar viper-replace-overlay nil "")
454 (put 'viper-replace-overlay 'permanent-local t)
455
456 (defcustom viper-replace-region-end-delimiter "$"
457 "A string marking the end of replacement regions.
458 It is used only with TTYs or if `viper-use-replace-region-delimiters'
459 is non-nil."
460 :type 'string
461 :group 'viper)
462 (defcustom viper-replace-region-start-delimiter ""
463 "A string marking the beginning of replacement regions.
464 It is used only with TTYs or if `viper-use-replace-region-delimiters'
465 is non-nil."
466 :type 'string
467 :group 'viper)
468 (defcustom viper-use-replace-region-delimiters
469 (or (not (viper-has-face-support-p))
470 (and (featurep 'xemacs) (eq (viper-device-type) 'tty)))
471 "*If non-nil, Viper will always use `viper-replace-region-end-delimiter' and
472 `viper-replace-region-start-delimiter' to delimit replacement regions, even on
473 color displays. By default, the delimiters are used only on TTYs."
474 :type 'boolean
475 :group 'viper)
476
477 (defcustom viper-read-buffer-function 'read-buffer
478 "Function to use for prompting the user for a buffer name."
479 :type 'symbol
480 :group 'viper)
481
482 ;; XEmacs requires glyphs
483 (viper-cond-compile-for-xemacs-or-emacs
484 (progn ; xemacs
485 (or (glyphp viper-replace-region-end-delimiter)
486 (setq viper-replace-region-end-delimiter
487 (make-glyph viper-replace-region-end-delimiter)))
488 (or (glyphp viper-replace-region-start-delimiter)
489 (setq viper-replace-region-start-delimiter
490 (make-glyph viper-replace-region-start-delimiter)))
491 )
492 nil ; emacs
493 )
494
495
496
497 ;; These are local marker that must be initialized to nil and moved with
498 ;; `viper-move-marker-locally'
499 ;;
500 ;; Remember the last position inside the replace region.
501 (viper-deflocalvar viper-last-posn-in-replace-region nil)
502 ;; Remember the last position while inserting
503 (viper-deflocalvar viper-last-posn-while-in-insert-state nil)
504 (put 'viper-last-posn-in-replace-region 'permanent-local t)
505 (put 'viper-last-posn-while-in-insert-state 'permanent-local t)
506
507 (viper-deflocalvar viper-sitting-in-replace nil "")
508 (put 'viper-sitting-in-replace 'permanent-local t)
509
510 ;; Remember the number of characters that have to be deleted in replace
511 ;; mode to compensate for the inserted characters.
512 (viper-deflocalvar viper-replace-chars-to-delete 0 "")
513 ;; This variable is used internally by the before/after changed functions to
514 ;; determine how many chars were deleted by the change. This can't be
515 ;; determined inside after-change-functions because those get the length of the
516 ;; deleted region, not the number of chars deleted (which are two different
517 ;; things under MULE).
518 (viper-deflocalvar viper-replace-region-chars-deleted 0 "")
519
520 ;; Insertion ring and command ring
521 (defcustom viper-insertion-ring-size 14
522 "The size of history of inserted text.
523 This is a list where Viper keeps the history of previously inserted pieces of
524 text."
525 :type 'integer
526 :group 'viper-misc)
527 ;; The insertion ring.
528 (defvar viper-insertion-ring nil)
529 ;; This is temp insertion ring. Used to do rotation for display purposes.
530 ;; When rotation just started, it is initialized to viper-insertion-ring.
531 (defvar viper-temp-insertion-ring nil)
532 (defvar viper-last-inserted-string-from-insertion-ring "")
533
534 (defcustom viper-command-ring-size 14
535 "The size of history of Vi commands repeatable with dot."
536 :type 'integer
537 :group 'viper-misc)
538 ;; The command ring.
539 (defvar viper-command-ring nil)
540 ;; This is temp command ring. Used to do rotation for display purposes.
541 ;; When rotation just started, it is initialized to viper-command-ring.
542 (defvar viper-temp-command-ring nil)
543
544 ;; Fast keyseq and ESC keyseq timeouts
545 (defcustom viper-fast-keyseq-timeout 200
546 "*Key sequence separated by no more than this many milliseconds is viewed as a Vi-style macro, if such a macro is defined.
547 Setting this too high may slow down your typing. Setting this value too low
548 will make it hard to use Vi-style timeout macros."
549 :type 'integer
550 :group 'viper-misc)
551
552 (defcustom viper-ESC-keyseq-timeout (if (viper-window-display-p)
553 0 viper-fast-keyseq-timeout)
554 "*Key sequence beginning with ESC and separated by no more than this many milliseconds is considered to be generated by a keyboard function key.
555 Setting this too high may slow down switching from insert to vi state. Setting
556 this value too low will make it impossible to use function keys in insert mode
557 on a dumb terminal."
558 :type 'integer
559 :group 'viper-misc)
560
561 (defcustom viper-translate-all-ESC-keysequences (not (viper-window-display-p))
562 "Allow translation of all key sequences into commands.
563 Normally, Viper lets Emacs translate only those ESC key sequences that are
564 defined in the low-level key-translation-map or function-key-map, such as those
565 emitted by the arrow and function keys. Other sequences, e.g., \\e/, are
566 treated as ESC command followed by a `/'. This is done for people who type fast
567 and tend to hit other characters right after they hit ESC. Other people like
568 Emacs to translate ESC sequences all the time.
569 The default is to translate all sequences only when using a dumb terminal.
570 This permits you to use ESC as a meta key in insert mode."
571 :type 'boolean
572 :group 'viper-misc)
573
574 ;; Modes and related variables
575
576 ;; Current mode. One of: `emacs-state', `vi-state', `insert-state'
577 (viper-deflocalvar viper-current-state 'emacs-state)
578
579
580 ;; Autoindent in insert
581
582 ;; Variable that keeps track of whether C-t has been pressed.
583 (viper-deflocalvar viper-cted nil "")
584
585 ;; Preserve the indent value, used by C-d in insert mode.
586 (viper-deflocalvar viper-current-indent 0)
587
588 ;; Whether to preserve the indent, used by C-d in insert mode.
589 (viper-deflocalvar viper-preserve-indent nil)
590
591 (viper-deflocalvar viper-auto-indent nil "")
592 (defcustom viper-auto-indent nil
593 "*Enable autoindent, if t.
594 This is a buffer-local variable."
595 :type 'boolean
596 :group 'viper)
597
598 (viper-deflocalvar viper-electric-mode t "")
599 (defcustom viper-electric-mode t
600 "*If t, electrify Viper.
601 Currently, this only electrifies auto-indentation, making it appropriate to the
602 mode of the buffer.
603 This means that auto-indentation will depart from standard Vi and will indent
604 appropriate to the mode of the buffer. This is especially useful for editing
605 programs and LaTeX documents."
606 :type 'boolean
607 :group 'viper)
608
609 (defcustom viper-shift-width 8
610 "*The value of the shiftwidth.
611 This determines the number of columns by which the Ctl-t moves the cursor in
612 the Insert state."
613 :type 'integer
614 :group 'viper)
615
616 ;; Variables for repeating destructive commands
617
618 (defcustom viper-keep-point-on-repeat t
619 "*If t, don't move point when repeating previous command.
620 This is useful for doing repeated changes with the '.' key.
621 The user can change this to nil, if she likes when the cursor moves
622 to a new place after repeating previous Vi command."
623 :type 'boolean
624 :group 'viper)
625
626 ;; Remember insert point as a marker. This is a local marker that must be
627 ;; initialized to nil and moved with `viper-move-marker-locally'.
628 (viper-deflocalvar viper-insert-point nil)
629 (put 'viper-insert-point 'permanent-local t)
630
631 ;; This remembers the point before dabbrev-expand was called.
632 ;; If viper-insert-point turns out to be bigger than that, it is reset
633 ;; back to viper-pre-command-point.
634 ;; The reason this is needed is because dabbrev-expand (and possibly
635 ;; others) may jump to before the insertion point, delete something and
636 ;; then reinsert a bigger piece. For instance: bla^blo
637 ;; If dabbrev-expand is called after `blo' and ^ undicates viper-insert-point,
638 ;; then point jumps to the beginning of `blo'. If expansion is found, `blablo'
639 ;; is deleted, and we have |^, where | denotes point. Next, dabbrev-expand
640 ;; will insert the expansion, and we get: blablo^
641 ;; Whatever we insert next goes before the ^, i.e., before the
642 ;; viper-insert-point marker. So, Viper will think that nothing was
643 ;; inserted. Remembering the orig position of the marker circumvents the
644 ;; problem.
645 ;; We don't know of any command, except dabbrev-expand, that has the same
646 ;; problem. However, the same trick can be used if such a command is
647 ;; discovered later.
648 ;;
649 (viper-deflocalvar viper-pre-command-point nil)
650 (put 'viper-pre-command-point 'permanent-local t) ; this is probably an overkill
651
652 ;; This is used for saving inserted text.
653 (defvar viper-last-insertion nil)
654
655 ;; Remembers the last replaced region.
656 (defvar viper-last-replace-region "")
657
658 ;; Remember com point as a marker.
659 ;; This is a local marker. Should be moved with `viper-move-marker-locally'
660 (viper-deflocalvar viper-com-point nil)
661
662 ;; If non-nil, the value is a list (M-COM VAL COM REG inserted-text cmd-keys)
663 ;; It is used to re-execute last destructive command.
664 ;; M-COM is a Lisp symbol representing the function to be executed.
665 ;; VAL is the prefix argument that was used with that command.
666 ;; COM is an internal descriptor, such as ?r, ?c, ?C, which contains
667 ;; additional information on how the function in M-COM is to be handled.
668 ;; REG is the register used by command
669 ;; INSERTED-TEXT is text inserted by that command (in case of o, c, C, i, r
670 ;; commands).
671 ;; COMMAND-KEYS are the keys that were typed to invoke the command.
672 (defvar viper-d-com nil)
673
674 ;; The character remembered by the Vi `r' command.
675 (defvar viper-d-char nil)
676
677 ;; Name of register to store deleted or yanked strings
678 (defvar viper-use-register nil)
679
680
681 ;;; Variables for Moves and Searches
682
683 (defgroup viper-search nil
684 "Variables that define the search and query-replace behavior of Viper."
685 :prefix "viper-"
686 :group 'viper)
687
688 ;; For use by `;' command.
689 (defvar viper-f-char nil)
690
691 ;; For use by `.' command.
692 (defvar viper-F-char nil)
693
694 ;; For use by `;' command.
695 (defvar viper-f-forward nil)
696
697 ;; For use by `;' command.
698 (defvar viper-f-offset nil)
699
700 ;; Last search string
701 (defvar viper-s-string "")
702
703 (defcustom viper-quote-string "> "
704 "String inserted at the beginning of quoted region."
705 :type 'string
706 :group 'viper)
707
708 ;; If t, search is forward.
709 (defvar viper-s-forward nil)
710
711 (defcustom viper-case-fold-search nil
712 "*If not nil, search ignores cases."
713 :type 'boolean
714 :group 'viper-search)
715
716 (defcustom viper-re-search t
717 "*If not nil, search is regexp search, otherwise vanilla search."
718 :type 'boolean
719 :tag "Regexp Search"
720 :group 'viper-search)
721
722 (defcustom viper-search-scroll-threshold 2
723 "*If search lands within this threshnold from the window top/bottom,
724 the window will be scrolled up or down appropriately, to reveal context.
725 If you want Viper search to behave as usual in Vi, set this variable to a
726 negative number."
727 :type 'boolean
728 :group 'viper-search)
729
730 (defcustom viper-re-query-replace t
731 "*If t then do regexp replace, if nil then do string replace."
732 :type 'boolean
733 :tag "Regexp Query Replace"
734 :group 'viper-search)
735
736 (defcustom viper-re-replace t
737 "*If t, do regexp replace. nil means do string replace."
738 :type 'boolean
739 :tag "Regexp Replace"
740 :group 'viper-search)
741
742 (defcustom viper-parse-sexp-ignore-comments t
743 "*If t, `%' ignores the parentheses that occur inside comments."
744 :type 'boolean
745 :group 'viper)
746
747 (viper-deflocalvar viper-ex-style-motion t "")
748 (defcustom viper-ex-style-motion t
749 "*If t, the commands l,h do not cross lines, etc (Ex-style).
750 If nil, these commands cross line boundaries."
751 :type 'boolean
752 :group 'viper)
753
754 (viper-deflocalvar viper-ex-style-editing t "")
755 (defcustom viper-ex-style-editing t
756 "*If t, Ex-style behavior while editing in Vi command and insert states.
757 `Backspace' and `Delete' don't cross line boundaries in insert.
758 `X' and `x' can't delete characters across line boundary in Vi, etc.
759 Note: this doesn't preclude `Backspace' and `Delete' from deleting characters
760 by moving past the insertion point. This is a feature, not a bug.
761
762 If nil, the above commands can work across lines."
763 :type 'boolean
764 :group 'viper)
765
766 (viper-deflocalvar viper-ESC-moves-cursor-back viper-ex-style-editing "")
767 (defcustom viper-ESC-moves-cursor-back nil
768 "*If t, ESC moves cursor back when changing from insert to vi state.
769 If nil, the cursor stays where it was when ESC was hit."
770 :type 'boolean
771 :group 'viper)
772
773 (viper-deflocalvar viper-delete-backwards-in-replace nil "")
774 (defcustom viper-delete-backwards-in-replace nil
775 "*If t, DEL key will delete characters while moving the cursor backwards.
776 If nil, the cursor will move backwards without deleting anything."
777 :type 'boolean
778 :group 'viper)
779
780 (defcustom viper-buffer-search-char nil
781 "*Key used for buffer-searching. Must be a character type, e.g., ?g."
782 :type '(choice (const nil) character)
783 :group 'viper-search)
784
785 (defcustom viper-search-wrap-around-t t
786 "*If t, search wraps around."
787 :type 'boolean
788 :tag "Search Wraps Around"
789 :group 'viper-search)
790
791 (viper-deflocalvar viper-related-files-and-buffers-ring nil "")
792 (defcustom viper-related-files-and-buffers-ring nil
793 "*List of file and buffer names that are considered to be related to the current buffer.
794 Related buffers can be cycled through via :R and :P commands."
795 :type 'boolean
796 :group 'viper-misc)
797 (put 'viper-related-files-and-buffers-ring 'permanent-local t)
798
799 ;; Used to find out if we are done with searching the current buffer.
800 (viper-deflocalvar viper-local-search-start-marker nil)
801 ;; As above, but global
802 (defvar viper-search-start-marker (make-marker))
803
804 ;; the search overlay
805 (viper-deflocalvar viper-search-overlay nil)
806
807
808 (defvar viper-heading-start
809 (concat "^\\s-*(\\s-*defun\\s-\\|" ; lisp
810 "^{\\s-*$\\|^[_a-zA-Z][^()]*[()].*{\\s-*$\\|" ; C/C++
811 "^\\s-*class.*{\\|^\\s-*struct.*{\\|^\\s-*enum.*{\\|"
812 "^\\\\[sb][a-z]*{.*}\\s-*$\\|" ; latex
813 "^@node\\|@table\\|^@m?enu\\|^@itemize\\|^@if\\|" ; texinfo
814 "^.+:-") ; prolog
815 "*Regexps for Headings. Used by \[\[ and \]\].")
816
817 (defvar viper-heading-end
818 (concat "^}\\|" ; C/C++
819 "^\\\\end{\\|" ; latex
820 "^@end \\|" ; texinfo
821 ")\n\n[ \t\n]*\\|" ; lisp
822 "\\.\\s-*$") ; prolog
823 "*Regexps to end Headings/Sections. Used by \[\].")
824
825
826 ;; These two vars control the interaction of jumps performed by ' and `.
827 ;; In this new version, '' doesn't erase the marks set by ``, so one can
828 ;; use both kinds of jumps interchangeably and without loosing positions
829 ;; inside the lines.
830
831 ;; Remembers position of the last jump done using ``'.
832 (viper-deflocalvar viper-last-jump nil)
833 ;; Remembers position of the last jump done using `''.
834 (viper-deflocalvar viper-last-jump-ignore 0)
835
836 ;; History variables
837
838 ;; History of search strings.
839 (defvar viper-search-history (list ""))
840 ;; History of query-replace strings used as a source.
841 (defvar viper-replace1-history nil)
842 ;; History of query-replace strings used as replacement.
843 (defvar viper-replace2-history nil)
844 ;; History of region quoting strings.
845 (defvar viper-quote-region-history (list viper-quote-string))
846 ;; History of Ex-style commands.
847 (defvar viper-ex-history nil)
848 ;; History of shell commands.
849 (defvar viper-shell-history nil)
850
851
852 ;; Last shell command. There are two of these, one for Ex (in viper-ex)
853 ;; and one for Vi.
854
855 ;; Last shell command executed with ! command.
856 (defvar viper-last-shell-com nil)
857
858 \f
859 ;;; Face-saving tricks
860
861 (defgroup viper-highlighting nil
862 "Hilighting of replace region, search pattern, minibuffer, etc."
863 :prefix "viper-"
864 :group 'viper)
865
866
867 (defface viper-search
868 '((((class color)) (:foreground "Black" :background "khaki"))
869 (t (:underline t :stipple "gray3")))
870 "*Face used to flash out the search pattern."
871 :group 'viper-highlighting)
872 ;; An internal variable. Viper takes the face from here.
873 (defvar viper-search-face 'viper-search
874 "Face used to flash out the search pattern.
875 DO NOT CHANGE this variable. Instead, use the customization widget
876 to customize the actual face object `viper-search'
877 this variable represents.")
878
879 (defface viper-replace-overlay
880 '((((class color)) (:foreground "Black" :background "darkseagreen2"))
881 (t (:underline t :stipple "gray3")))
882 "*Face for highlighting replace regions on a window display."
883 :group 'viper-highlighting)
884 ;; An internal variable. Viper takes the face from here.
885 (defvar viper-replace-overlay-face 'viper-replace-overlay
886 "Face for highlighting replace regions on a window display.
887 DO NOT CHANGE this variable. Instead, use the customization widget
888 to customize the actual face object `viper-replace-overlay'
889 this variable represents.")
890
891 (defface viper-minibuffer-emacs
892 '((((class color)) (:foreground "Black" :background "darkseagreen2"))
893 (t (:weight bold)))
894 "Face used in the Minibuffer when it is in Emacs state."
895 :group 'viper-highlighting)
896 ;; An internal variable. Viper takes the face from here.
897 (defvar viper-minibuffer-emacs-face 'viper-minibuffer-emacs
898 "Face used in the Minibuffer when it is in Emacs state.
899 DO NOT CHANGE this variable. Instead, use the customization widget
900 to customize the actual face object `viper-minibuffer-emacs'
901 this variable represents.")
902
903 (defface viper-minibuffer-insert
904 '((((class color)) (:foreground "Black" :background "pink"))
905 (t (:slant italic)))
906 "Face used in the Minibuffer when it is in Insert state."
907 :group 'viper-highlighting)
908 ;; An internal variable. Viper takes the face from here.
909 (defvar viper-minibuffer-insert-face 'viper-minibuffer-insert
910 "Face used in the Minibuffer when it is in Insert state.
911 DO NOT CHANGE this variable. Instead, use the customization widget
912 to customize the actual face object `viper-minibuffer-insert'
913 this variable represents.")
914
915 (defface viper-minibuffer-vi
916 '((((class color)) (:foreground "DarkGreen" :background "grey"))
917 (t (:inverse-video t)))
918 "Face used in the Minibuffer when it is in Vi state."
919 :group 'viper-highlighting)
920 ;; An internal variable. Viper takes the face from here.
921 (defvar viper-minibuffer-vi-face 'viper-minibuffer-vi
922 "Face used in the Minibuffer when it is in Vi state.
923 DO NOT CHANGE this variable. Instead, use the customization widget
924 to customize the actual face object `viper-minibuffer-vi'
925 this variable represents.")
926
927 ;; the current face to be used in the minibuffer
928 (viper-deflocalvar
929 viper-minibuffer-current-face viper-minibuffer-emacs-face "")
930
931 \f
932 ;;; Miscellaneous
933
934 (defvar viper-inhibit-startup-message nil
935 "Whether Viper startup message should be inhibited.")
936
937 (defcustom viper-spell-function 'ispell-region
938 "Spell function used by #s<move> command to spell."
939 :type 'function
940 :group 'viper-misc)
941
942 (defcustom viper-tags-file-name "TAGS"
943 "The tags file used by Viper."
944 :type 'string
945 :group 'viper-misc)
946
947 (defcustom viper-change-notification-threshold 1
948 "Notify the user when this many lines or characters have been deleted/yanked.
949 For line-deleting/yanking commands (like `dd', `yy'), the value denotes the
950 number of lines. For character-based commands (such as `x', `dw', etc.), the
951 value refers to the number of characters affected."
952 :type 'integer
953 :group 'viper-misc)
954
955 ;; Minibuffer
956
957 (defcustom viper-vi-style-in-minibuffer t
958 "If t, use vi-style editing in minibuffer.
959 Should be set in `~/.viper' file."
960 :type 'boolean
961 :group 'viper)
962
963 ;; overlay used in the minibuffer to indicate which state it is in
964 (viper-deflocalvar viper-minibuffer-overlay nil)
965 (put 'viper-minibuffer-overlay 'permanent-local t)
966
967 ;; Hook, specific to Viper, which is run just *before* exiting the minibuffer.
968 ;; This is needed because beginning with Emacs 19.26, the standard
969 ;; `minibuffer-exit-hook' is run *after* exiting the minibuffer
970 (defvar viper-minibuffer-exit-hook nil)
971
972
973 ;; Mode line
974 (defconst viper-vi-state-id "<V> "
975 "Mode line tag identifying the Vi mode of Viper.")
976 (defconst viper-emacs-state-id "<E> "
977 "Mode line tag identifying the Emacs mode of Viper.")
978 (defconst viper-insert-state-id "<I> "
979 "Mode line tag identifying the Insert mode of Viper.")
980 (defconst viper-replace-state-id "<R> "
981 "Mode line tag identifying the Replace mode of Viper.")
982
983
984 (defgroup viper-hooks nil
985 "Viper hooks."
986 :prefix "viper-"
987 :group 'viper)
988
989 (defcustom viper-vi-state-hook 'viper-restore-cursor-type
990 "*Hooks run just before the switch to Vi mode is completed."
991 :type 'hook
992 :group 'viper-hooks)
993 (defcustom viper-insert-state-hook 'viper-set-insert-cursor-type
994 "*Hooks run just before the switch to Insert mode is completed."
995 :type 'hook
996 :group 'viper-hooks)
997 (defcustom viper-replace-state-hook 'viper-restore-cursor-type
998 "*Hooks run just before the switch to Replace mode is completed."
999 :type 'hook
1000 :group 'viper-hooks)
1001 (defcustom viper-emacs-state-hook 'viper-restore-cursor-type
1002 "*Hooks run just before the switch to Emacs mode is completed."
1003 :type 'hook
1004 :group 'viper-hooks)
1005
1006 (defcustom viper-load-hook nil
1007 "Hooks run just after loading Viper."
1008 :type 'hook
1009 :group 'viper-hooks)
1010
1011 (defun viper-restore-cursor-type ()
1012 (condition-case nil
1013 (if (featurep 'xemacs)
1014 (set (make-local-variable 'bar-cursor) nil)
1015 (setq cursor-type default-cursor-type))
1016 (error nil)))
1017
1018 (defun viper-set-insert-cursor-type ()
1019 (if (featurep 'xemacs)
1020 (set (make-local-variable 'bar-cursor) 2)
1021 (setq cursor-type '(bar . 2))))
1022
1023
1024 (provide 'viper-init)
1025
1026
1027 ;; Local Variables:
1028 ;; eval: (put 'viper-deflocalvar 'lisp-indent-hook 'defun)
1029 ;; End:
1030
1031 ;; arch-tag: 4efa2416-1fcb-4690-be10-1a2a0248d250
1032 ;;; viper-init.el ends here