/
viper.el
5607 lines (4871 loc) · 188 KB
/
viper.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; viper.el --- A full-featured Vi emulator for GNU Emacs 19 and XEmacs 19,
;; a VI Plan for Emacs Rescue,
;; and a venomous VI PERil.
;; Viper Is also a Package for Emacs Rebels.
;;
;; Keywords: emulations
;; Author: Michael Kifer <kifer@cs.sunysb.edu>
(defconst viper-version "2.85 of February 14, 1996"
"The current version of Viper")
;; Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
;; This file is part of GNU Emacs.
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
;;; Commentary:
;; Viper is a full-featured Vi emulator for Emacs 19. It emulates and
;; improves upon the standard features of Vi and, at the same time, allows
;; full access to all Emacs facilities. Viper supports multiple undo,
;; file name completion, command, file, and search history and it extends
;; Vi in many other ways. Viper is highly customizable through the various
;; hooks, user variables, and keymaps. It is implemented as a collection
;; of minor modes and it is designed to provide full access to all Emacs
;; major and minor modes.
;;
;;; History
;;
;; Viper is a new name for a package formerly known as VIP-19,
;; which was a successor of VIP version 3.5 by Masahiko Sato
;; <ms@sail.stanford.edu> and VIP version 4.2 by Aamod Sane
;; <sane@cs.uiuc.edu>. Some ideas from vip 4.4.2 by Aamod Sane
;; were also shamelessly plagiarized.
;;
;; Viper maintains some degree of compatibility with these older
;; packages. See the documentation for customization.
;;
;; The main difference between Viper and these older packages are:
;;
;; 1. Viper emulates Vi at several levels, from almost complete conformity
;; to a rather loose Vi-compliance.
;;
;; 2. Viper provides full access to all major and minor modes of Emacs
;; without the need to type extra keys.
;; The older versions of VIP (and other Vi emulators) do not work with
;; some major and minor modes.
;;
;; 3. Viper supports vi-style undo.
;;
;; 4. Viper fully emulates (and improves upon) vi's replacement mode.
;;
;; 5. Viper has a better interface to ex, including command, variable, and
;; file name completion.
;;
;; 6. Viper uses native Emacs history and completion features; it doesn't
;; rely on other packages (such as gmhist.el and completer.el) to provide
;; these features.
;;
;; 7. Viper supports Vi-style editing in the minibuffer, by allowing the
;; user to switch from Insert state to Vi state to Replace state, etc.
;;
;; 8. Viper keeps history of recently inserted pieces of text and recently
;; executed Vi-style destructive commands, such as `i', `d', etc.
;; These pieces of text can be inserted in later insertion commands;
;; the previous destructive commands can be re-executed.
;;
;; 9. Viper has Vi-style keyboard macros, which enhances the similar
;; facility in the original Vi.
;; First, one can execute any Emacs command while defining a
;; macro, not just the Vi commands. Second, macros are defined in a
;; WYSYWYG mode, using an interface to Emacs' WYSIWYG style of defining
;; macros. Third, in Viper, one can define macros that are specific to
;; a given buffer, a given major mode, or macros defined for all buffers.
;; The same macro name can have several different definitions:
;; one global, several definitions for various major modes, and
;; definitions for specific buffers.
;; Bffer-specific definitions override mode-specific
;; definitions, which, in turn, override global definitions.
;;
;;
;;; Installation:
;; -------------
;;
;; (require 'viper)
;;
;;; Acknowledgements:
;; -----------------
;; Bug reports and ideas contributed by the following users
;; have helped improve Viper and the various versions of VIP.
;; See the on-line manual for a complete list of contributors.
;;
;;
;;; Notes:
;;
;; 1. Major modes.
;; In most cases, Viper handles major modes correctly, i.e., they come up
;; in the right state (either vi-state or emacs-state). For instance, text
;; files come up in vi-state, while, say, Dired appears in emacs-state by
;; default.
;; However, some modes do not appear in the right mode in the beginning,
;; usually because they neglect to follow Emacs conventions (e.g., they don't
;; use kill-all-local-variables when they start). Some major modes
;; may fail to come up in emacs-state if they call hooks, such as
;; text-hook, for no good reason.
;;
;; As an immediate solution, you can hit C-z to bring about the right mode.
;; An interim solution is to add an appropriate hook to the mode like this:
;;
;; (add-hook 'your-favorite-mode 'viper-mode)
;; or
;; (add-hook 'your-favorite-mode 'vip-change-state-to-emacs)
;;
;; whichever applies. The right thing to do, however, is to complain to the
;; author of the respective package. (Sometimes they also neglect to equip
;; their modes with hooks, which is one more reason for complaining.)
;;
;; 2. Keymap handling
;; Because Emacs 19 has an elegant mechanism for turning minor mode keymaps
;; on and off, implementation of Viper has been greatly simplified. Viper
;; has several minor modes.
;;
;; Viper's Vi state consists of seven minor modes:
;;
;; vip-vi-intercept-minor-mode
;; vip-vi-local-user-minor-mode
;; vip-vi-global-user-minor-mode
;; vip-vi-kbd-minor-mode
;; vip-vi-state-modifier-minor-mode
;; vip-vi-diehard-minor-mode
;; vip-vi-basic-minor-mode
;;
;; Bindings done to the keymap of the first mode overshadow those done to
;; the second, which, in turn, overshadows those done to the third, etc.
;;
;; The last vip-vi-basic-minor-mode contains most of the usual Vi bindings
;; in its edit mode. This mode provides access to all Emacs facilities.
;; Novice users, however, may want to set their vip-expert-level to 1
;; in their .vip file. This will enable vip-vi-diehard-minor-mode. This
;; minor mode's bindings make Viper simulate the usual Vi very closely.
;; For instance, C-c will not have its standard Emacs binding
;; and so many of the goodies of Emacs are not available.
;;
;; An skilled user, should set vip-expert-level to at least 3. This will
;; enable ;; C-c and many Emacs facilities will become available.
;; In this case, vip-vi-diehard-minor-mode is inactive.
;;
;; Viper gurus should have at least
;; (setq vip-expert-level 4)
;; in their ~/.vip files. This will unsuppress all Emacs keys that are not
;; essential for VI-style editing.
;; Pick-and-choose users may want to put
;; (setq vip-expert-level 5)
;; in ~/.vip. Viper will then leave it up to the user to set the variables
;; vip-want-* See vip-set-expert-level for details.
;;
;; The very first minor mode, vip-vi-intercept-minor-mode, is of no
;; concern for the user. It is needed to bind Viper's vital keys, such as
;; ESC and C-z.
;;
;; The second mode, vip-vi-local-user-minor-mode, usually has an
;; empty keymap. However, the user can set bindings in this keymap, which
;; will overshadow the corresponding bindings in the other two minor
;; modes. This is useful, for example, for setting up ZZ in gnus,
;; rmail, mh-e, etc., to send message instead of saving it in a file.
;; Likewise, in Dired mode, you may want to bind ZN and ZP to commands
;; that would visit the next or the previous file in the Dired buffer.
;; Setting local keys is tricky, so don't do it directly. Instead, use
;; vip-add-local-keys function (see its doc).
;;
;; The third minor mode, vip-vi-global-user-minor-mode, is also intended
;; for the users but, unlike vip-vi-local-user-minor-mode, its key
;; bindings are seen in all Viper buffers. This mode keys can be done
;; with define-key command.
;;
;; The fourth minor mode, vip-vi-kbd-minor-mode, is used by keyboard
;; macros. Users are NOT supposed to modify this keymap directly.
;;
;; The fifth mode, vip-vi-state-modifier-minor-mode, can be used to set
;; key bindings that are visible in some major modes but not in others.
;;
;; Users are allowed to modify keymaps that belong to
;; vip-vi-local-user-minor-mode, vip-vi-global-user-minor-mode,
;; and vip-vi-state-modifier-minor-mode only.
;;
;; Viper's Insert state also has seven minor modes:
;;
;; vip-insert-intercept-minor-mode
;; vip-insert-local-user-minor-mode
;; vip-insert-global-user-minor-mode
;; vip-insert-kbd-minor-mode
;; vip-insert-state-modifier-minor-mode
;; vip-insert-diehard-minor-mode
;; vip-insert-basic-minor-mode
;;
;; As with VI's editing modes, the first mode, vip-insert-intercept-minor-mode
;; is used to bind vital keys that are not to be changed by the user.
;;
;; The next mode, vip-insert-local-user-minor-mode, is used to customize
;; bindings in the insert state of Viper. The third mode,
;; vip-insert-global-user-minor-mode is like
;; vip-insert-local-user-minor-mode, except that its bindings are seen in
;; all Viper buffers. As with vip-vi-local-user-minor-mode, its bindings
;; should be done via the function vip-add-local-keys. Bindings for
;; vip-insert-global-user-minor-mode can be set with the define-key command.
;;
;; The next minor mode, vip-insert-kbd-minor-mode,
;; is used for keyboard VI-style macros defined with :map!.
;;
;; The fifth minor mode, vip-insert-state-modifier-minor-mode, is like
;; vip-vi-state-modifier-minor-mode, except that it is used in the Insert
;; state; it can be used to modify keys in a mode-specific fashion.
;;
;; The minor mode vip-insert-diehard-minor-mode is in effect when
;; the user wants a high degree of Vi compatibility (a bad idea, really!).
;; The last minor mode, vip-insert-basic-minor-mode, is always in effect
;; when Viper is in insert state. It binds a small number of keys needed for
;; Viper's operation.
;;
;; Finally, Viper provides minor modes for overriding bindings set by Emacs
;; modes when Viper is in Emacs state:
;;
;; vip-emacs-local-user-minor-mode
;; vip-emacs-global-user-minor-mode
;; vip-emacs-kbd-minor-mode
;; vip-emacs-state-modifier-minor-mode
;;
;; These minor modes are in effect when Viper is in Emacs state. The keymap
;; associated with vip-emacs-global-user-minor-mode,
;; vip-emacs-global-user-map, overrides the global and local keymaps as
;; well as the minor mode keymaps set by other modes. The keymap of
;; vip-emacs-local-user-minor-mode, vip-emacs-local-user-map, overrides
;; everything, but it is used on a per buffer basis.
;; The keymap associated with vip-emacs-state-modifier-minor-mode
;; overrides keys on a per-major-mode basis. The mode
;; vip-emacs-kbd-minor-mode is used to define Vi-style macros in Emacs
;; state.
;;
;; 3. There is also one minor mode that is used when Viper is in its
;; replace-state (used for commands like cw, C, etc.). This mode is
;; called
;;
;; vip-replace-minor-mode
;;
;; and its keymap is vip-replace-map. Replace minor mode is always
;; used in conjunction with the minor modes for insert-state, and its
;; keymap overshadows the keymaps for insert minor modes.
;;
;; 4. Defining buffer-local bindings in Vi and Insert modes.
;; As mentioned before, sometimes, it is convenient to have
;; buffer-specific of mode-specific key bindings in Vi and insert modes.
;; Viper provides a special function, vip-add-local-keys, to do precisely
;; this. For instance, is you need to add couple of mode-specific bindings
;; to Insert mode, you can put
;;
;; (vip-add-local-keys 'insert-state '((key1 . func1) (key2 .func2)))
;;
;; somewhere in a hook of this major mode. If you put something like this
;; in your own elisp function, this will define bindings specific to the
;; buffer that was current at the time of the call to vip-add-local-keys.
;; The only thing to make sure here is that the major mode of this buffer
;; is written according to Emacs conventions, which includes a call to
;; (kill-all-local-variables). See vip-add-local-keys for more details.
;;
;;
;; TO DO (volunteers?):
;;
;; 1. Some of the code that is inherited from VIP-3.5 is rather
;; convoluted. Instead of vip-command-argument, keymaps should bind the
;; actual commands. E.g., "dw" should be bound to a generic command
;; vip-delete that will delete things based on the value of
;; last-command-char. This would greatly simplify the logic and the code.
;;
;; 2. Somebody should venture to write a customization package a la
;; options.el that would allow the user to change values of variables
;; that meet certain specs (e.g., match a regexp) and whose doc string
;; starts with a '*'. Then, the user should be offered to save
;; variables that were changed. This will make user's customization job
;; much easier.
;;
(require 'advice)
(require 'cl)
(require 'ring)
(require 'viper-util)
;;; Variables
;; Is t until viper-mode executes for the very first time.
;; Prevents recursive descend into startup messages.
(defvar vip-first-time t)
(defvar vip-expert-level 0
"User's expert level.
The minor mode vip-vi-diehard-minor-mode is in effect when
vip-expert-level is 1 or 2 or when vip-want-emacs-keys-in-vi is t.
The minor mode vip-insert-diehard-minor-mode is in effect when
vip-expert-level is 1 or 2 or if vip-want-emacs-keys-in-insert is t.
Use `M-x vip-set-expert-level' to change this.")
;; Max expert level supported by Viper. This is NOT a user option.
;; It is here to make it hard for the user from resetting it.
(defconst vip-max-expert-level 5)
;; Contains user settings for vars affected by vip-set-expert-level function.
;; Not a user option.
(defvar vip-saved-user-settings nil)
;;; Viper minor modes
;; This is not local in Emacs, so we make it local.
(make-variable-buffer-local 'minor-mode-map-alist)
;; Mode for vital things like \e, C-z.
(vip-deflocalvar vip-vi-intercept-minor-mode nil)
(vip-deflocalvar vip-vi-basic-minor-mode nil
"Viper's minor mode for Vi bindings.")
(vip-deflocalvar vip-vi-local-user-minor-mode nil
"Auxiliary minor mode for user-defined local bindings in Vi state.")
(vip-deflocalvar vip-vi-global-user-minor-mode nil
"Auxiliary minor mode for user-defined global bindings in Vi state.")
(vip-deflocalvar vip-vi-state-modifier-minor-mode nil
"Minor mode used to make major-mode-specific modification to Vi state.")
(vip-deflocalvar vip-vi-diehard-minor-mode nil
"This minor mode is in effect when the user wants Viper to be Vi.")
(vip-deflocalvar vip-vi-kbd-minor-mode nil
"Minor mode for Ex command macros in Vi state.
The corresponding keymap stores key bindings of Vi macros defined with
the Ex command :map.")
;; Mode for vital things like \e, C-z.
(vip-deflocalvar vip-insert-intercept-minor-mode nil)
(vip-deflocalvar vip-insert-basic-minor-mode nil
"Viper's minor mode for bindings in Insert mode.")
(vip-deflocalvar vip-insert-local-user-minor-mode nil
"Auxiliary minor mode for buffer-local user-defined bindings in Insert state.
This is a way to overshadow normal Insert mode bindings locally to certain
designated buffers.")
(vip-deflocalvar vip-insert-global-user-minor-mode nil
"Auxiliary minor mode for global user-defined bindings in Insert state.")
(vip-deflocalvar vip-insert-state-modifier-minor-mode nil
"Minor mode used to make major-mode-specific modification to Insert state.")
(vip-deflocalvar vip-insert-diehard-minor-mode nil
"Minor mode that simulates Vi very closely.
Not recommened, except for the novice user.")
(vip-deflocalvar vip-insert-kbd-minor-mode nil
"Minor mode for Ex command macros Insert state.
The corresponding keymap stores key bindings of Vi macros defined with
the Ex command :map!.")
(vip-deflocalvar vip-replace-minor-mode nil
"Minor mode in effect in replace state (cw, C, and the like commands).")
;; Mode for vital things like \C-z and \C-x)
;; This is t, by default. So, any new buffer will have C-z defined as
;; switch to Vi, unless we switched states in this buffer
(vip-deflocalvar vip-emacs-intercept-minor-mode t)
(vip-deflocalvar vip-emacs-local-user-minor-mode t
"Minor mode for local user bindings effective in Emacs state.
Users can use it to override Emacs bindings when Viper is in its Emacs
state.")
(vip-deflocalvar vip-emacs-global-user-minor-mode t
"Minor mode for global user bindings in effect in Emacs state.
Users can use it to override Emacs bindings when Viper is in its Emacs
state.")
(vip-deflocalvar vip-emacs-kbd-minor-mode t
"Minor mode for Vi style macros in Emacs state.
The corresponding keymap stores key bindings of Vi macros defined with
`vip-record-kbd-macro' command. There is no Ex-level command to do this
interactively.")
(vip-deflocalvar vip-emacs-state-modifier-minor-mode t
"Minor mode used to make major-mode-specific modification to Emacs state.
For instance, a Vi purist may want to bind `dd' in Dired mode to a function
that deletes a file.")
;;; ISO characters
(vip-deflocalvar vip-automatic-iso-accents nil
"*If non-nil, ISO accents will be turned on in insert/replace emacs states and turned off in vi-state.
For some users, this behavior may be too primitive. In this case, use
insert/emacs/vi state hooks.")
;;; Emacs keys in other states.
(defvar vip-want-emacs-keys-in-insert t
"*Set to nil if you want complete Vi compatibility in insert mode.
Complete compatibility with Vi is not recommended for power use of Viper.")
(defvar vip-want-emacs-keys-in-vi t
"*Set to nil if you want complete Vi compatibility in Vi mode.
Full Vi compatibility is not recommended for power use of Viper.")
;; VI-style Undo
;; Used to 'undo' complex commands, such as replace and insert commands.
(vip-deflocalvar vip-undo-needs-adjustment nil)
(put 'vip-undo-needs-adjustment 'permanent-local t)
;; A mark that Viper puts on buffer-undo-list. Marks the beginning of a
;; complex command that must be undone atomically. If inserted, it is
;; erased by vip-change-state-to-vi and vip-repeat.
(defconst vip-buffer-undo-list-mark 'viper)
(defvar vip-keep-point-on-undo nil
"*Non-nil means not to move point while undoing commands.
This style is different from Emacs and Vi. Try it to see if
it better fits your working style.")
;; Replace mode and changing text
;; Viper's own after/before change functions, which get vip-add-hook'ed to
;; Emacs's
(vip-deflocalvar vip-after-change-functions nil "")
(vip-deflocalvar vip-before-change-functions nil "")
(vip-deflocalvar vip-post-command-hooks nil "")
(vip-deflocalvar vip-pre-command-hooks nil "")
;; Can be used to pass global states around for short period of time
(vip-deflocalvar vip-intermediate-command nil "")
;; Indicates that the current destructive command has started in replace mode.
(vip-deflocalvar vip-began-as-replace nil "")
(defvar vip-replace-overlay-cursor-color "Red"
"*Cursor color to use in Replace state")
(vip-deflocalvar vip-replace-overlay nil "")
(put 'vip-replace-overlay 'permanent-local t)
(if (vip-has-face-support-p)
(progn
(make-face 'vip-replace-overlay-face)
(vip-hide-face 'vip-replace-overlay-face)
(or (face-differs-from-default-p 'vip-replace-overlay-face)
(progn
(if (vip-can-use-colors "darkseagreen2" "Black")
(progn
(set-face-background
'vip-replace-overlay-face "darkseagreen2")
(set-face-foreground 'vip-replace-overlay-face "Black")))
(set-face-underline-p 'vip-replace-overlay-face t))
)))
(defvar vip-replace-overlay-face 'vip-replace-overlay-face
"*Face for highlighting replace regions on a window display.")
(defvar vip-replace-region-end-delimiter "$"
"A string marking the end of replacement regions.
It is used only with TTYs or if `vip-use-replace-region-delimiters'
is non-nil.")
(defvar vip-replace-region-start-delimiter ""
"A string marking the beginning of replacement regions.
It is used only with TTYs or if `vip-use-replace-region-delimiters'
is non-nil.")
(defvar vip-use-replace-region-delimiters
(or (not (vip-has-face-support-p)) (not (vip-color-display-p)))
"*If non-nil, Viper will always use `vip-replace-region-end-delimiter' and
`vip-replace-region-start-delimiter' to delimit replacement regions, even on
color displays. By default, the delimiters are used only on TTYs or
monochrome displays.")
;; XEmacs requires glyphs
(if vip-xemacs-p
(progn
(or (glyphp vip-replace-region-end-delimiter)
(setq vip-replace-region-end-delimiter
(make-glyph vip-replace-region-end-delimiter)))
(or (glyphp vip-replace-region-start-delimiter)
(setq vip-replace-region-start-delimiter
(make-glyph vip-replace-region-start-delimiter)))
))
;; These are local marker that must be initialized to nil and moved with
;; `vip-move-marker-locally'
;;
;; Remember the last position inside the replace region.
(vip-deflocalvar vip-last-posn-in-replace-region nil)
;; Remember the last position while inserting
(vip-deflocalvar vip-last-posn-while-in-insert-state nil)
(put 'vip-last-posn-in-replace-region 'permanent-local t)
(put 'vip-last-posn-while-in-insert-state 'permanent-local t)
(vip-deflocalvar vip-sitting-in-replace nil "")
(put 'vip-sitting-in-replace 'permanent-local t)
;; Remember the number of characters that have to be deleted in replace
;; mode to compensate for the inserted characters.
(vip-deflocalvar vip-replace-chars-to-delete 0 "")
(vip-deflocalvar vip-replace-chars-deleted 0 "")
;; Insertion ring and command ring
(defvar vip-insertion-ring-size 14
"The size of the insertion ring.")
;; The insertion ring.
(defvar vip-insertion-ring nil)
;; This is temp insertion ring. Used to do rotation for display purposes.
;; When rotation just started, it is initialized to vip-insertion-ring.
(defvar vip-temp-insertion-ring nil)
(defvar vip-last-inserted-string-from-insertion-ring "")
(defvar vip-command-ring-size 14
"The size of the command ring.")
;; The command ring.
(defvar vip-command-ring nil)
;; This is temp command ring. Used to do rotation for display purposes.
;; When rotation just started, it is initialized to vip-command-ring.
(defvar vip-temp-command-ring nil)
;; Modes and related variables
;; Current mode. One of: `emacs-state', `vi-state', `insert-state'
(vip-deflocalvar vip-current-state 'emacs-state)
(defvar vip-toggle-key "\C-z"
"The key used to change states from emacs to Vi and back.
In insert mode, this key also functions as Meta.
Must be set in .vip file or prior to loading Viper.
This setting cannot be changed interactively.")
(defvar vip-ESC-key "\e"
"Key used to ESC.
Must be set in .vip file or prior to loading Viper.
This setting cannot be changed interactively.")
(defvar vip-no-multiple-ESC t
"*If true, multiple ESC in Vi mode will cause bell to ring.
\_ is then mapped to Meta.
This is set to t on a windowing terminal and to 'twice on a dumb
terminal (unless the user level is 1, 2, or 5). On a dumb terminal, this
enables cursor keys and is generally more convenient, as terminals usually
don't have a convenient Meta key.
Setting vip-no-multiple-ESC to nil will allow as many multiple ESC,
as is allowed by the major mode in effect.")
(defvar vip-want-ctl-h-help nil
"*If t then C-h is bound to help-command in insert mode, if nil then it is
bound to delete-backward-char.")
;; Autoindent in insert
;; Variable that keeps track of whether C-t has been pressed.
(vip-deflocalvar vip-cted nil "")
;; Preserve the indent value, used by C-d in insert mode.
(vip-deflocalvar vip-current-indent 0)
;; Whether to preserve the indent, used by C-d in insert mode.
(vip-deflocalvar vip-preserve-indent nil)
(vip-deflocalvar vip-auto-indent nil
"*Autoindent if t.")
(vip-deflocalvar vip-electric-mode t
"*If t, enable electric behavior.
Currently only enables auto-indentation `according to mode'.")
(defconst vip-shift-width 8
"*The shiftwidth variable.")
;; Variables for repeating destructive commands
(defconst vip-keep-point-on-repeat t
"*If t, don't move point when repeating previous command.
This is useful for doing repeated changes with the '.' key.
The user can change this to nil, if she likes when the cursor moves
to a new place after repeating previous Vi command.")
;; Remember insert point as a marker. This is a local marker that must be
;; initialized to nil and moved with `vip-move-marker-locally'.
(vip-deflocalvar vip-insert-point nil)
(put 'vip-insert-point 'permanent-local t)
;; This remembers the point before dabbrev-expand was called.
;; If vip-insert-point turns out to be bigger than that, it is reset
;; back to vip-pre-command-point.
;; The reason this is needed is because dabbrev-expand (and possibly
;; others) may jump to before the insertion point, delete something and
;; then reinsert a bigger piece. For instance: bla^blo
;; If dabbrev-expand is called after `blo' and ^ undicates vip-insert-point,
;; then point jumps to the beginning of `blo'. If expansion is found, `blablo'
;; is deleted, and we have |^, where | denotes point. Next, dabbrev-expand
;; will insert the expansion, and we get: blablo^
;; Whatever we insert next goes before the ^, i.e., before the
;; vip-insert-point marker. So, Viper will think that nothing was
;; inserted. Remembering the orig position of the marker circumvents the
;; problem.
;; We don't know of any command, except dabbrev-expand, that has the same
;; problem. However, the same trick can be used if such a command is
;; discovered later.
;;
(vip-deflocalvar vip-pre-command-point nil)
(put 'vip-pre-command-point 'permanent-local t) ; this is probably an overkill
;; This is used for saving inserted text.
(defvar vip-last-insertion nil)
;; Remembers the last replaced region.
(defvar vip-last-replace-region "")
;; Remember com point as a marker.
;; This is a local marker. Should be moved with `vip-move-marker-locally'
(vip-deflocalvar vip-com-point nil)
;; If non-nil, the value is a list (M-COM VAL COM REG inserted-text cmd-keys)
;; It is used to re-execute last destructive command.
;; M-COM is a Lisp symbol representing the function to be executed.
;; VAL is the prefix argument that was used with that command.
;; COM is an internal descriptor, such as ?r, ?c, ?C, which contains
;; additional information on how the function in M-COM is to be handled.
;; REG is the register used by command
;; INSERTED-TEXT is text inserted by that command (in case of o, c, C, i, r
;; commands).
;; COMMAND-KEYS are the keys that were typed to invoke the command.
(defvar vip-d-com nil)
;; The character remembered by the Vi `r' command.
(defvar vip-d-char nil)
;; Name of register to store deleted or yanked strings
(defvar vip-use-register nil)
;; Variables for Moves and Searches
;; For use by `;' command.
(defvar vip-f-char nil)
;; For use by `.' command.
(defvar vip-F-char nil)
;; For use by `;' command.
(defvar vip-f-forward nil)
;; For use by `;' command.
(defvar vip-f-offset nil)
;; Last search string
(defvar vip-s-string "")
(defvar vip-quote-string "> "
"String inserted at the beginning of quoted region.")
;; If t, search is forward.
(defvar vip-s-forward nil)
(defconst vip-case-fold-search nil
"*If t, search ignores cases.")
(defconst vip-re-search t
"*If t, search is reg-exp search, otherwise vanilla search.")
(defconst vip-re-query-replace t
"*If t then do regexp replace, if nil then do string replace.")
(defconst vip-re-replace t
"*If t, do regexp replace. nil means do string replace.")
(vip-deflocalvar vip-ex-style-motion t
"*Ex-style: the commands l,h do not cross lines, etc.")
(vip-deflocalvar vip-ex-style-editing-in-insert t
"*The keys ^H, ^? don't jump lines in insert, ESC moves cursor back, etc.
Note: this doesn't preclude ^H and ^? from deleting characters by moving
past the insertion point. This is a feature, not a bug. ")
(vip-deflocalvar vip-delete-backwards-in-replace nil
"*If t, DEL key will delete characters while moving the cursor backwards.
If nil, the cursor will move backwards without deleting anything.")
(defconst vip-buffer-search-char nil
"*Key bound for buffer-searching.")
(defconst vip-search-wrap-around-t t
"*If t, search wraps around.")
(vip-deflocalvar vip-related-files-and-buffers-ring nil
"*Ring of file and buffer names that are considered to be related to the
current buffer.
These buffers can be cycled through via :R and :P commands.")
(put 'vip-related-files-and-buffers-ring 'permanent-local t)
;; Used to find out if we are done with searching the current buffer.
(vip-deflocalvar vip-local-search-start-marker nil)
;; As above, but global
(defvar vip-search-start-marker (make-marker))
;; the search overlay
(vip-deflocalvar vip-search-overlay nil)
(defvar vip-heading-start
(concat "^\\s-*(\\s-*defun\\s-\\|" ; lisp
"^{\\s-*$\\|^[_a-zA-Z][^()]*[()].*{\\s-*$\\|" ; C/C++
"^\\s-*class.*{\\|^\\s-*struct.*{\\|^\\s-*enum.*{\\|"
"^\\\\[sb][a-z]*{.*}\\s-*$\\|" ; latex
"^@node\\|@table\\|^@m?enu\\|^@itemize\\|^@if\\|" ; texinfo
"^.+:-") ; prolog
"*Regexps for Headings. Used by \[\[ and \]\].")
(defvar vip-heading-end
(concat "^}\\|" ; C/C++
"^\\\\end{\\|" ; latex
"^@end \\|" ; texinfo
")\n\n[ \t\n]*\\|" ; lisp
"\\.\\s-*$") ; prolog
"*Regexps to end Headings/Sections. Used by \[\].")
;; These two vars control the interaction of jumps performed by ' and `.
;; In this new version, '' doesn't erase the marks set by ``, so one can
;; use both kinds of jumps interchangeably and without loosing positions
;; inside the lines.
;; Remembers position of the last jump done using ``'.
(vip-deflocalvar vip-last-jump nil)
;; Remembers position of the last jump done using `''.
(vip-deflocalvar vip-last-jump-ignore 0)
;; Some common error messages
(defconst vip-SpuriousText "Spurious text after command" "")
(defconst vip-BadExCommand "Not an editor command" "")
(defconst vip-InvalidCommandArgument "Invalid command argument" "")
(defconst vip-NoPrevSearch "No previous search string" "")
(defconst vip-EmptyRegister "`%c': Nothing in this register" "")
(defconst vip-InvalidRegister "`%c': Invalid register" "")
(defconst vip-EmptyTextmarker "`%c': Text marker doesn't point anywhere" "")
(defconst vip-InvalidTextmarker "`%c': Invalid text marker" "")
(defconst vip-InvalidViCommand "Invalid command" "")
(defconst vip-BadAddress "Ill-formed address" "")
(defconst vip-FirstAddrExceedsSecond "First address exceeds second" "")
(defconst vip-NoFileSpecified "No file specified" "")
;; History variables
;; History of search strings.
(defvar vip-search-history (list ""))
;; History of query-replace strings used as a source.
(defvar vip-replace1-history nil)
;; History of query-replace strings used as replacement.
(defvar vip-replace2-history nil)
;; History of region quoting strings.
(defvar vip-quote-region-history (list vip-quote-string))
;; History of Ex-style commands.
(defvar vip-ex-history nil)
;; History of shell commands.
(defvar vip-shell-history nil)
;; Last shell command. There are two of these, one for Ex (in viper-ex)
;; and one for Vi.
;; Last shell command executed with ! command.
(defvar vip-last-shell-com nil)
;;; Miscellaneous
;; don't bark when mark is inactive
(setq mark-even-if-inactive t)
(defvar vip-inhibit-startup-message nil
"Whether Viper startup message should be inhibited.")
(defvar vip-always t
"t means, arrange that vi-state will be a default.")
(defvar vip-custom-file-name (cond ((memq system-type '(vax-vms axp-vms))
"sys$login:.vip")
((memq system-type '(emx ms-dos))
"/_vip")
((memq system-type '(windows-nt windows-95))
"~/_vip")
(t ; Unix
"~/.vip"))
"Viper customisation file.
This variable must be set _before_ loading Viper.")
(defvar vip-spell-function 'ispell-region
"Spell function used by #s<move> command to spell.")
(defvar vip-tags-file-name "TAGS"
"The tags file used by Viper.")
;; Minibuffer
(defvar vip-vi-style-in-minibuffer t
"If t, use vi-style editing in minibuffer.
Should be set in `~/.vip' file.")
;; overlay used in the minibuffer to indicate which state it is in
(vip-deflocalvar vip-minibuffer-overlay nil)
;; Hook, specific to Viper, which is run just *before* exiting the minibuffer.
;; Beginning with Emacs 19.26, the standard `minibuffer-exit-hook' is run
;; *after* exiting the minibuffer
(defvar vip-minibuffer-exit-hook nil)
(vip-deflocalvar vip-vi-minibuffer-minor-mode nil
"Minor mode that forces Vi-style when the Minibuffer is in Vi state.")
(vip-deflocalvar vip-insert-minibuffer-minor-mode nil
"Minor mode that forces Vi-style when the Minibuffer is in Insert state.")
;; setup emacs-supported vi-style feel
(setq next-line-add-newlines nil
require-final-newline t)
(make-variable-buffer-local 'require-final-newline)
;; Mode line
(defconst vip-vi-state-id "<V> "
"Mode line tag identifying the Vi mode of Viper.")
(defconst vip-emacs-state-id "<E> "
"Mode line tag identifying the Emacs mode of Viper.")
(defconst vip-insert-state-id "<I> "
"Mode line tag identifying the Insert mode of Viper.")
(defconst vip-replace-state-id "<R> "
"Mode line tag identifying the Replace mode of Viper.")
;; Viper changes the default mode-line-buffer-identification
(setq-default mode-line-buffer-identification '(" %b"))
;; Variable displaying the current Viper state in the mode line.
(vip-deflocalvar vip-mode-string vip-emacs-state-id)
(or (memq 'vip-mode-string global-mode-string)
(setq global-mode-string
(append '("" vip-mode-string) (cdr global-mode-string))))
(defvar vip-vi-state-hook nil
"*Hooks run just before the switch to Vi mode is completed.")
(defvar vip-insert-state-hook nil
"*Hooks run just before the switch to Insert mode is completed.")
(defvar vip-replace-state-hook nil
"*Hooks run just before the switch to Replace mode is completed.")
(defvar vip-emacs-state-hook nil
"*Hooks run just before the switch to Emacs mode is completed.")
(defvar vip-load-hook nil
"Hooks run just after loading Viper.")
;; Generic predicates
;; These test functions are shamelessly lifted from vip 4.4.2 by Aamod Sane
;; generate test functions
;; given symbol foo, foo-p is the test function, foos is the set of
;; Viper command keys
;; (macroexpand '(vip-test-com-defun foo))
;; (defun foo-p (com) (consp (memq (if (< com 0) (- com) com) foos)))
(defmacro vip-test-com-defun (name)
(let* ((snm (symbol-name name))
(nm-p (intern (concat snm "-p")))
(nms (intern (concat snm "s"))))
(` (defun (, nm-p) (com)
(consp (memq (if (< com 0) (- com) com) (, nms)))))))
;; Variables for defining VI commands
;; Modifying commands that can be prefixes to movement commands
(defconst vip-prefix-commands '(?c ?d ?y ?! ?= ?# ?< ?> ?\"))
(vip-test-com-defun vip-prefix-command)
;; Commands that are pairs eg. dd. r and R here are a hack
(defconst vip-charpair-commands '(?c ?d ?y ?! ?= ?< ?> ?r ?R))
(vip-test-com-defun vip-charpair-command)
(defconst vip-movement-commands '(?b ?B ?e ?E ?f ?F ?G ?h ?H ?j ?k ?l
?H ?M ?n ?t ?T ?w ?W ?$ ?%
?^ ?( ?) ?- ?+ ?| ?{ ?} ?[ ?] ?' ?`
?; ?, ?0 ?? ?/
)
"Movement commands")
(vip-test-com-defun vip-movement-command)
;; Commands that can be repeated by .(dotted)
(defconst vip-dotable-commands '(?c ?d ?C ?D ?> ?<))
(vip-test-com-defun vip-dotable-command)
;; Commands that can follow a #
(defconst vip-hash-cmds '(?c ?C ?g ?q ?S))
(vip-test-com-defun vip-hash-cmd)
;; Commands that may have registers as prefix
(defconst vip-regsuffix-commands '(?d ?y ?Y ?D ?p ?P ?x ?X))
(vip-test-com-defun vip-regsuffix-command)
;;; Arrange the keymaps
(require 'viper-keym)
;;; CODE
;; sentinels
;; Runs vip-after-change-functions inside after-change-functions
(defun vip-after-change-sentinel (beg end len)
(let ((list vip-after-change-functions))
(while list
(funcall (car list) beg end len)
(setq list (cdr list)))))
;; Runs vip-before-change-functions inside before-change-functions
(defun vip-before-change-sentinel (beg end)
(let ((list vip-before-change-functions))
(while list
(funcall (car list) beg end)
(setq list (cdr list)))))
(defsubst vip-post-command-sentinel ()
(run-hooks 'vip-post-command-hooks))
(defsubst vip-pre-command-sentinel ()
(run-hooks 'vip-pre-command-hooks))
;; Needed so that Viper will be able to figure the last inserted
;; chunk of text with reasonable accuracy.
(defsubst vip-insert-state-post-command-sentinel ()
(if (and (memq vip-current-state '(insert-state replace-state))
vip-insert-point
(>= (point) vip-insert-point))
(setq vip-last-posn-while-in-insert-state (point-marker)))
(if (and (eq this-command 'dabbrev-expand)
(integerp vip-pre-command-point)
(> vip-insert-point vip-pre-command-point))
(move-marker vip-insert-point vip-pre-command-point))
)
(defsubst vip-insert-state-pre-command-sentinel ()
(if (and (eq this-command 'dabbrev-expand)
(markerp vip-insert-point)
(marker-position vip-insert-point))
(setq vip-pre-command-point (marker-position vip-insert-point))))
(defsubst vip-R-state-post-command-sentinel ()
;; Restoring cursor color is needed despite
;; vip-replace-state-pre-command-sentinel: When you jump to another buffer in
;; another frame, the pre-command hook won't change cursor color to default
;; in that other frame. So, if the second frame cursor was red and we set
;; the point outside the replacement region, then the cursor color will
;; remain red. Restoring the default, below, prevents this.
(if (and (<= (vip-replace-start) (point))
(<= (point) (vip-replace-end)))
(vip-change-cursor-color vip-replace-overlay-cursor-color)
(vip-restore-cursor-color)
))
;; to speed up, don't change cursor color before self-insert
;; and common move commands
(defsubst vip-replace-state-pre-command-sentinel ()