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