-
Notifications
You must be signed in to change notification settings - Fork 2
/
vip.el
3222 lines (2909 loc) · 101 KB
/
vip.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
;;; vip.el --- a VI Package for GNU Emacs
;; Author: Masahiko Sato <masahiko@kuis.kyoto-u.ac.jp>
;; Maintainer: SKK Development Team <skk@ring.gr.jp>
;; Version: 3.7
;; Keywords: emulations
;; Last Modified: $Date: 2007/04/22 02:38:27 $
;; Previous versions:
;; Version 3.5: September 15, 1987
;; 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, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; A full-featured vi(1) emulator.
;;
;; Send suggestions and bug reports to one of the above addresses.
;; When you report a bug, be sure to include the version number of VIP and
;; Emacs you are using.
;; Execute info command by typing "M-x info" to get information on VIP.
;;; Code:
;; APEL 9.22 required.
(require 'poe)
;; external variables
;; Not so easy to work on XEmacs...
;;(defconst vip-xemacs-p (string-match "XEmacs" emacs-version))
(defvar vip-insert-point nil
"Remember insert point as a marker. (Buffer-specific.)")
(set-default 'vip-insert-point nil)
(make-variable-buffer-local 'vip-insert-point)
(defvar vip-com-point nil
"Remember com point as a marker. (Buffer-specific.)")
(set-default 'vip-com-point nil)
(make-variable-buffer-local 'vip-com-point)
(defvar vip-current-mode nil
"Current mode. One of `emacs-mode', `vi-mode', `insert-mode'.")
(make-variable-buffer-local 'vip-current-mode)
(setq-default vip-current-mode 'emacs-mode)
(defvar vip-emacs-mode-line-buffer-identification nil
"Value of mode-line-buffer-identification in Emacs mode within vip.")
(make-variable-buffer-local 'vip-emacs-mode-line-buffer-identification)
(setq-default vip-emacs-mode-line-buffer-identification
'("Emacs: %17b"))
(defvar vip-current-major-mode nil
"vip-current-major-mode is the major-mode vi considers it is now.
\(buffer specific\)")
(make-variable-buffer-local 'vip-current-major-mode)
(defvar vip-last-shell-com nil
"Last shell command executed by ! command.")
(defvar vip-use-register nil
"Name of register to store deleted or yanked strings.")
(defvar vip-d-com nil
"How to reexecute last destructive command. Value is list (M-COM VAL COM).")
(defconst vip-shift-width 8
"*The number of columns shifted by > and < command.")
(defconst vip-re-replace nil
"*If t then do regexp replace, if nil then do string replace.")
(defvar vip-d-char nil
"The character remembered by the vi \"r\" 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
"For use by \";\" command.")
(defconst vip-search-wrap-around t
"*if t, search wraps around.")
(defconst vip-re-search nil
"*if t, search is reg-exp search, otherwise vanilla search.")
(defvar vip-s-string nil
"Last vip search string.")
(defvar vip-s-forward nil
"If t, search is forward.")
(defconst vip-case-fold-search nil
"*If t, search ignores cases.")
(defconst vip-re-query-replace nil
"*If t then do regexp replace, if nil then do string replace.")
(defconst vip-open-with-indent nil
"*If t, indent when open a new line.")
(defconst vip-help-in-insert-mode nil
"*If t then C-h is bound to help-command in insert mode.
If nil then it is bound to `delete-backward-char'.")
(defvar vip-quote-string "> "
"String inserted at the beginning of region.")
(defvar vip-tags-file-name "TAGS")
(defvar vip-inhibit-startup-message nil)
(defvar vip-startup-file (convert-standard-filename "~/.vip")
"Filename used as startup file for vip.")
;; SKK related variables
(defvar vip-skk-latin-mode nil)
(make-variable-buffer-local 'vip-skk-latin-mode)
(defvar vip-skk-j-mode nil)
(make-variable-buffer-local 'vip-skk-j-mode)
(defvar vip-skk-jisx0208-latin-mode nil)
(make-variable-buffer-local 'vip-skk-jisx0208-mode)
(defvar vip-skk-katakana nil)
(make-variable-buffer-local 'vip-skk-katakana)
(defvar vip-vi-mode nil)
(make-variable-buffer-local 'vip-vi-mode)
(defvar vip-insert-mode nil)
(make-variable-buffer-local 'vip-insert-mode)
;; basic set up
(defmacro vip-move-marker-locally (marker position &optional buffer)
(list 'progn
(list 'if (list 'not marker)
(list 'setq marker (list 'make-marker)))
(list 'set-marker marker position buffer)))
(global-set-key "\C-z" 'vip-change-mode-to-vi)
(defmacro vip-loop (count body)
"(COUNT BODY) Execute BODY COUNT times."
(list 'let (list (list 'count count))
(list 'while (list '> 'count 0)
body
(list 'setq 'count (list '1- 'count)))))
(defun vip-push-mark-silent (&optional location)
"Set mark at LOCATION (point, by default) and push old mark on mark ring.
No message."
(if (null (mark t))
nil
(setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
(if (> (length mark-ring) mark-ring-max)
(progn
(move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
(setcdr (nthcdr (1- mark-ring-max) mark-ring) nil))))
(set-mark (or location (point))))
(defun vip-goto-col (arg)
"Go to ARG's column."
(interactive "P")
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(save-excursion
(end-of-line)
(if (> val (1+ (current-column))) (error "")))
(if com (vip-move-marker-locally vip-com-point (point)))
(beginning-of-line)
(forward-char (1- val))
(if com (vip-execute-com 'vip-goto-col val com))))
(defun vip-copy-keymap (map)
(if (null map) (make-sparse-keymap) (copy-keymap map)))
;; changing mode
;; the two useule functions below are taken from viper and
;; modified.
(defun vip-normalize-minor-mode-map-alist ()
(setq minor-mode-map-alist
(vip-append-filter-alist
(list
(cons 'vip-vi-mode vip-vi-mode-map)
(cons 'vip-insert-mode vip-insert-mode-map)
)
minor-mode-map-alist)))
;; Append LIS2 to LIS1, both alists, by side-effect and returns LIS1
;; LIS2 is modified by filtering it: deleting its members of the form
;; \(car elt\) such that (car elt') is in LIS1.
(defun vip-append-filter-alist (lis1 lis2)
(let ((temp lis1)
elt)
;;filter-append the second list
(while temp
;; delete all occurrences
(while (setq elt (assoc (car (car temp)) lis2))
(setq lis2 (delq elt lis2)))
(setq temp (cdr temp)))
(nconc lis1 lis2)))
(defun vip-change-mode (new-mode)
"change mode to NEW-MODE. NEW-MODE will be either emacs-mode, vi-mode or
insert-mode."
(let ((skk-mode (if (boundp 'skk-mode) skk-mode nil)))
(cond ((eq new-mode 'vi-mode)
(if (eq vip-current-mode 'insert-mode)
(progn
(if skk-mode (vip-skk-mode-off))
(vip-copy-region-as-kill (point) vip-insert-point)
(vip-repeat-insert-command))
(if (eq vip-current-mode 'emacs-mode)
(setq vip-emacs-mode-line-buffer-identification
mode-line-buffer-identification)))
(vip-change-mode-line "Vi: ")
(setq vip-vi-mode t
vip-insert-mode nil)
)
((eq new-mode 'insert-mode)
(vip-move-marker-locally vip-insert-point (point))
(if (eq vip-current-mode 'emacs-mode)
(setq vip-emacs-mode-line-buffer-identification
mode-line-buffer-identification))
(vip-change-mode-line "Insrt")
(if skk-mode (vip-skk-mode-on))
(setq vip-vi-mode nil
vip-insert-mode t))
((eq new-mode 'emacs-mode)
(vip-change-mode-line "Emacs:")
;;(vip-skk-mode-off)
(setq vip-vi-mode nil
vip-insert-mode nil)))
(setq vip-current-mode new-mode)
(vip-normalize-minor-mode-map-alist)
(force-mode-line-update)
))
;; SKK related functions
;;;###autoload
(defun vip-skk-mode (arg)
"Turn on both VIP-MODE and SKK-MODE. if ARG is nil, then toggle
SKK-MODE. Then, change mode to insert mode."
(interactive "P")
(or vip-current-mode (vip-mode))
(cond ((eq vip-current-mode 'vi-mode)
(skk-mode arg)
(setq vip-skk-latin-mode skk-latin-mode
vip-skk-j-mode skk-j-mode
vip-skk-jisx0208-latin-mode skk-jisx0208-latin-mode
vip-skk-katakana skk-katakana)
(vip-change-mode-to-insert))
((eq vip-current-mode 'emacs-mode)
;; in this case, alwasy enter skk mode
(skk-mode 1)
(vip-change-mode-to-insert))
((eq vip-current-mode 'insert-mode)
(skk-mode arg)
(vip-change-mode-to-insert))))
(defun vip-skk-mode-off ()
(if skk-abbrev-mode (skk-j-mode-on))
(let ((str (substring (skk-indicator-to-string
skk-modeline-input-mode t) 1)))
(setq vip-skk-latin-mode skk-latin-mode
vip-skk-j-mode skk-j-mode
vip-skk-jisx0208-latin-mode skk-jisx0208-latin-mode
vip-skk-katakana skk-katakana
vip-skk-input-mode-string)
(skk-kakutei)
(skk-mode-off)
(setq skk-modeline-input-mode
;; There was no MODE argument...?
;;(skk-mode-string-to-indicator (concat " [" str "]"))
(concat " [" str "]"))))
(defun vip-skk-mode-on ()
(add-hook 'pre-command-hook 'skk-pre-command nil 'local)
;; we need to go back to the mode stored in VIP-SKK-*-MODE.
(cond (vip-skk-latin-mode (skk-latin-mode-on))
(vip-skk-j-mode (skk-j-mode-on vip-skk-katakana))
(vip-skk-jisx0208-latin-mode (skk-jisx0208-latin-mode-on))))
(require 'advice)
(defadvice skk-pre-command (around vip-ad activate)
(if (eq this-command 'vip-delete-backward-char)
;; do nothing
nil
ad-do-it))
;; end SKK related functions
(defun vip-copy-region-as-kill (beg end)
"If BEG and END do not belong to the same buffer, it copies empty region."
(condition-case nil
(copy-region-as-kill beg end)
(error (copy-region-as-kill beg beg))))
(defun vip-change-mode-line (string)
"Assuming that the mode line format contains the string \"Emacs:\", this
function replaces the string by \"Vi: \" etc."
(setq mode-line-buffer-identification
(if (string= string "Emacs:")
vip-emacs-mode-line-buffer-identification
(list (concat string " %12b")))))
;;;###autoload
(defun Vip-mode ()
"Turn on VIP emulation of VI."
(interactive)
(if (not vip-inhibit-startup-message)
(progn
(switch-to-buffer "VIP Startup Message")
(erase-buffer)
(insert
"VIP is a Vi emulation package for GNU Emacs. VIP provides most Vi commands
including Ex commands. VIP is however different from Vi in several points.
You can get more information on VIP by:
1. Typing `M-x info' and selecting menu item \"vip\".
2. Typing `C-h k' followed by a key whose description you want.
3. Printing VIP manual which can be found as GNU/man/vip.texinfo
4. Printing VIP Reference Card which can be found as GNU/etc/vipcard.tex
This startup message appears whenever you load VIP unless you type `y' now.
Type `n' to quit this window for now.\n")
(goto-char (point-min))
(if (y-or-n-p "Inhibit VIP startup message? ")
(progn
(save-excursion
(set-buffer
(find-file-noselect
(substitute-in-file-name vip-startup-file)))
(goto-char (point-max))
(insert "\n(setq vip-inhibit-startup-message t)\n")
(save-buffer)
(kill-buffer (current-buffer)))
(message "VIP startup message inhibited.")
(sit-for 2)))
(kill-buffer (current-buffer))
(message "")
(setq vip-inhibit-startup-message t)))
(vip-change-mode-to-vi))
(defalias 'vip-mode 'Vip-mode)
(defun vip-change-mode-to-vi ()
"Change mode to vi mode."
(interactive)
(vip-change-mode 'vi-mode))
(defun vip-change-mode-to-insert ()
"Change mode to insert mode."
(interactive)
(vip-change-mode 'insert-mode))
(defun vip-change-mode-to-emacs ()
"Change mode to emacs mode."
(interactive)
(vip-change-mode 'emacs-mode))
;; escape to emacs mode temporarily
(defun vip-escape-to-emacs (arg &optional events)
"Escape to Emacs mode for one Emacs command.
ARG is used as the prefix value for the executed command. If
EVENTS is a list of events, which become the beginning of the command."
(interactive "P")
(let ((change-mode-to-vi nil)
(change-mode-to-insert nil)
(change-mode-to-emacs nil)
(save-vi-mode vip-vi-mode)
(save-insert-mode vip-insert-mode)
(old-buff (current-buffer))
)
(let (com key (vip-vi-mode nil) (vip-insert-mode nil))
(if events (setq unread-command-events events))
(setq prefix-arg arg)
;;(use-local-map vip-emacs-local-map)
(unwind-protect
(setq com (key-binding (setq key
;;(if vip-xemacs-p
;; (read-key-sequence nil)
;; (read-key-sequence nil t)))))
(read-key-sequence nil t))))
nil)
(command-execute com prefix-arg)
(setq prefix-arg nil);; reset prefix arg
)
;; we must check if the current buffer is the same after executing
;; the command. if not, we have to restore the values of
;; VIP-VI-MODE and VIP-INSERT-MODE
(if (eq (current-buffer) old-buff)
;; in case one of the values of CHANGE-MODE-TO-VI/INSERT/EMACS was
;; changed dynamically in executing the command COM, then
;; change mode to the specified mode. otherwise keep the mode.
(progn
(cond (change-mode-to-vi (vip-change-mode-to-vi))
(change-mode-to-insert
(vip-change-mode-to-insert)
)
(change-mode-to-emacs (vip-change-mode-to-emacs))
(t (vip-change-mode vip-current-mode))))
;; since OLD-BUFF may not exist anymore, we have to first
;; check if it still exists. we can check this by the function
;; BUFFER-NAME which return nil for a killed buffer.
(if (buffer-name old-buff)
(save-excursion
(set-buffer old-buff)
(setq vip-vi-mode save-vi-mode
vip-insert-mode save-insert-mode)))
;; in the new buffer we enter the mode spcified by
;; the local value of VIP-CURRNT-MODE, unless CHANGE-MODE-TO-*
;; is set
(cond (change-mode-to-vi (vip-change-mode-to-vi))
(change-mode-to-insert (vip-change-mode-to-insert))
(change-mode-to-emacs (vip-change-mode-to-emacs))
(t (vip-change-mode vip-current-mode)))
)
))
(defun vip-message-conditions (conditions)
"Print CONDITIONS as a message."
(let ((case (car conditions)) (msg (cdr conditions)))
(if (null msg)
(message "%s" case)
(message "%s %s" case (prin1-to-string msg)))
(ding)))
(defun vip-ESC (arg)
"Emulate ESC key in Emacs mode."
(interactive "P")
(vip-escape-to-emacs arg '(?\e)))
(defun vip-ctl-c (arg)
"Emulate C-c key in Emacs mode."
(interactive "P")
(vip-escape-to-emacs arg '(?\C-c)))
(defun vip-ctl-x (arg)
"Emulate C-x key in Emacs mode."
(interactive "P")
(vip-escape-to-emacs arg '(?\C-x)))
(defun vip-ctl-h (arg)
"Emulate C-h key in Emacs mode."
(interactive "P")
(vip-escape-to-emacs arg '(?\C-h)))
;; prefix argument for vi mode
;; In vi mode, prefix argument is a dotted pair (NUM . COM) where NUM
;; represents the numeric value of the prefix argument and COM represents
;; command prefix such as "c", "d", "m" and "y".
(defun vip-prefix-arg-value (char value com)
"Compute numeric prefix arg value. Invoked by CHAR. VALUE is the value
obtained so far, and COM is the command part obtained so far."
(while (and (>= char ?0) (<= char ?9))
(setq value (+ (* (if (numberp value) value 0) 10) (- char ?0)))
(setq char (read-char)))
(setq prefix-arg value)
(if com (setq prefix-arg (cons prefix-arg com)))
(while (eq char ?U)
(vip-describe-arg prefix-arg)
(setq char (read-char)))
(setq unread-command-events (list char)))
(defun vip-prefix-arg-com (char value com)
"Vi operator as prefix argument."
(let ((cont t))
(while (and cont (memq char '(?c ?d ?y ?! ?< ?> ?= ?# ?r ?R ?\")))
(if com
;; this means that we already have a command character, so we
;; construct a com list and exit while. however, if char is "
;; it is an error.
(progn
;; new com is (CHAR . OLDCOM)
(if (memq char '(?# ?\")) (error ""))
(setq com (cons char com))
(setq cont nil))
;; if com is nil we set com as char, and read more. again, if char
;; is ", we read the name of register and store it in vip-use-register.
;; if char is !, =, or #, a complete com is formed so we exit while.
(cond ((memq char '(?! ?=))
(setq com char)
(setq char (read-char))
(setq cont nil))
((eq char ?#)
;; read a char and encode it as com
(setq com (+ 128 (read-char)))
(setq char (read-char))
(setq cont nil))
((memq char '(?< ?>))
(setq com char)
(setq char (read-char))
(if (eq com char) (setq com (cons char com)))
(setq cont nil))
((eq char ?\")
(let ((reg (read-char)))
(if (or (and (<= ?A reg) (<= reg ?z))
(and (<= ?1 reg) (<= reg ?9)))
(setq vip-use-register reg)
(error ""))
(setq char (read-char))))
(t
(setq com char)
(setq char (read-char)))))))
(if (atom com)
;; com is a single char, so we construct prefix-arg
;; and if char is ?, describe prefix arg, otherwise exit by
;; pushing the char back
(progn
(setq prefix-arg (cons value com))
(while (eq char ?U)
(vip-describe-arg prefix-arg)
(setq char (read-char)))
(setq unread-command-events (list char)))
;; as com is non-nil, this means that we have a command to execute
(if (memq (car com) '(?r ?R))
;; execute appropriate region command.
(let ((char (car com)) (com (cdr com)))
(setq prefix-arg (cons value com))
(if (eq char ?r) (vip-region prefix-arg)
(vip-Region prefix-arg))
;; reset prefix-arg
(setq prefix-arg nil))
;; otherwise, reset prefix arg and call appropriate command
(setq value (if (null value) 1 value))
(setq prefix-arg nil)
(cond ((equal com '(?c . ?c)) (vip-line (cons value ?C)))
((equal com '(?d . ?d)) (vip-line (cons value ?D)))
((equal com '(?d . ?y)) (vip-yank-defun))
((equal com '(?y . ?y)) (vip-line (cons value ?Y)))
((equal com '(?< . ?<)) (vip-line (cons value ?<)))
((equal com '(?> . ?>)) (vip-line (cons value ?>)))
((equal com '(?! . ?!)) (vip-line (cons value ?!)))
((equal com '(?= . ?=)) (vip-line (cons value ?=)))
(t (error ""))))))
(defun vip-describe-arg (arg)
(let (val com)
(setq val (vip-P-val arg)
com (vip-getcom arg))
(if (null val)
(if (null com)
(message "Value is nil, and command is nil.")
(message "Value is nil, and command is %c." com))
(if (null com)
(message "Value is %d, and command is nil." val)
(message "Value is %d, and command is %c." val com)))))
(defun vip-digit-argument (arg)
"Begin numeric argument for the next command."
(interactive "P")
(vip-prefix-arg-value last-command-char nil
(if (consp arg) (cdr arg) nil)))
(defun vip-command-argument (arg)
"Accept a motion command as an argument."
(interactive "P")
(condition-case conditions
(vip-prefix-arg-com
last-command-char
(cond ((null arg) nil)
((consp arg) (car arg))
((numberp arg) arg)
(t (error "strange arg")))
(cond ((null arg) nil)
((consp arg) (cdr arg))
((numberp arg) nil)
(t (error "strange arg"))))
(quit
(setq vip-use-register nil)
(signal 'quit nil))))
(defun vip-p-val (arg)
"Get value part of prefix-argument ARG."
(cond ((null arg) 1)
((consp arg) (if (null (car arg)) 1 (car arg)))
(t arg)))
(defun vip-P-val (arg)
"Get value part of prefix-argument ARG."
(cond ((consp arg) (car arg))
(t arg)))
(defun vip-getcom (arg)
"Get com part of prefix-argument ARG."
(cond ((null arg) nil)
((consp arg) (cdr arg))
(t nil)))
(defun vip-getCom (arg)
"Get com part of prefix-argument ARG and modify it."
(let ((com (vip-getcom arg)))
(cond ((eq com ?c) ?C)
((eq com ?d) ?D)
((eq com ?y) ?Y)
(t com))))
;; repeat last destructive command
(defun vip-append-to-register (reg start end)
"Append region to text in register REG.
START and END are buffer positions indicating what to append."
(set-register reg (concat (or (get-register reg) "")
(buffer-substring start end))))
(defun vip-execute-com (m-com val com)
"(M-COM VAL COM) Execute command COM. The list (M-COM VAL COM) is set
to vip-d-com for later use by vip-repeat"
(let ((reg vip-use-register))
(if com
(cond ((eq com ?c) (vip-change vip-com-point (point)))
((eq com (- ?c)) (vip-change-subr vip-com-point (point)))
((eq (abs com) ?C)
(save-excursion
(set-mark vip-com-point)
(vip-enlarge-region (mark 'force) (point))
(if vip-use-register
(progn
(cond ((and (<= ?a vip-use-register)
(<= vip-use-register ?z))
(copy-to-register
vip-use-register (mark 'force) (point) nil))
((and (<= ?A vip-use-register)
(<= vip-use-register ?Z))
(vip-append-to-register
(+ vip-use-register 32) (mark 'force) (point)))
(t (setq vip-use-register nil)
(error "")))
(setq vip-use-register nil)))
(delete-region (mark 'force) (point)))
(open-line 1)
(if (eq com ?C) (vip-change-mode-to-insert) (yank)))
((eq com ?d)
(if vip-use-register
(progn
(cond ((and (<= ?a vip-use-register)
(<= vip-use-register ?z))
(copy-to-register
vip-use-register vip-com-point (point) nil))
((and (<= ?A vip-use-register)
(<= vip-use-register ?Z))
(vip-append-to-register
(+ vip-use-register 32) vip-com-point (point)))
(t (setq vip-use-register nil)
(error "")))
(setq vip-use-register nil)))
(setq last-command
(if (eq last-command 'd-command) 'kill-region nil))
(kill-region vip-com-point (point))
(setq this-command 'd-command))
((eq com ?D)
(save-excursion
(set-mark vip-com-point)
(vip-enlarge-region (mark 'force) (point))
(if vip-use-register
(progn
(cond ((and (<= ?a vip-use-register)
(<= vip-use-register ?z))
(copy-to-register
vip-use-register (mark 'force) (point) nil))
((and (<= ?A vip-use-register)
(<= vip-use-register ?Z))
(vip-append-to-register
(+ vip-use-register 32) (mark 'force) (point)))
(t (setq vip-use-register nil)
(error "")))
(setq vip-use-register nil)))
(setq last-command
(if (eq last-command 'D-command) 'kill-region nil))
(kill-region (mark 'force) (point))
(if (eq m-com 'vip-line) (setq this-command 'D-command)))
(back-to-indentation))
((eq com ?y)
(if vip-use-register
(progn
(cond ((and (<= ?a vip-use-register)
(<= vip-use-register ?z))
(copy-to-register
vip-use-register vip-com-point (point) nil))
((and (<= ?A vip-use-register)
(<= vip-use-register ?Z))
(vip-append-to-register
(+ vip-use-register 32) vip-com-point (point)))
(t (setq vip-use-register nil)
(error "")))
(setq vip-use-register nil)))
(setq last-command nil)
(copy-region-as-kill vip-com-point (point))
(goto-char vip-com-point))
((eq com ?Y)
(save-excursion
(set-mark vip-com-point)
(vip-enlarge-region (mark 'force) (point))
(if vip-use-register
(progn
(cond ((and (<= ?a vip-use-register)
(<= vip-use-register ?z))
(copy-to-register
vip-use-register (mark 'force) (point) nil))
((and (<= ?A vip-use-register)
(<= vip-use-register ?Z))
(vip-append-to-register
(+ vip-use-register 32) (mark 'force) (point)))
(t (setq vip-use-register nil)
(error "")))
(setq vip-use-register nil)))
(setq last-command nil)
(copy-region-as-kill (mark 'force) (point)))
(goto-char vip-com-point))
((eq (abs com) ?!)
(save-excursion
(set-mark vip-com-point)
(vip-enlarge-region (mark 'force) (point))
(shell-command-on-region
(mark 'force) (point)
(if (eq com ?!)
(setq vip-last-shell-com (vip-read-string "!"))
vip-last-shell-com)
t)))
((eq com ?=)
(save-excursion
(set-mark vip-com-point)
(vip-enlarge-region (mark 'force) (point))
(if (> (mark 'force) (point)) (exchange-point-and-mark))
(indent-region (mark 'force) (point) nil)))
((eq com ?<)
(save-excursion
(set-mark vip-com-point)
(vip-enlarge-region (mark 'force) (point))
(indent-rigidly (mark 'force) (point) (- vip-shift-width)))
(goto-char vip-com-point))
((eq com ?>)
(save-excursion
(set-mark vip-com-point)
(vip-enlarge-region (mark 'force) (point))
(indent-rigidly (mark 'force) (point) vip-shift-width))
(goto-char vip-com-point))
((>= com 128)
;; this is special command #
(vip-special-prefix-com (- com 128)))))
(setq vip-d-com (list m-com val (if (memq com '(?c ?C ?!))
(- com) com)
reg))))
(defun vip-repeat (arg)
"(ARG) Re-execute last destructive command. vip-d-com has the form
\(COM ARG CH REG), where COM is the command to be re-executed, ARG is the
argument for COM, CH is a flag for repeat, and REG is optional and if exists
is the name of the register for COM."
(interactive "P")
(if (eq last-command 'vip-undo)
;; if the last command was vip-undo, then undo-more
(vip-undo-more)
;; otherwise execute the command stored in vip-d-com. if arg is non-nil
;; its prefix value is used as new prefix value for the command.
(let ((m-com (car vip-d-com))
(val (vip-P-val arg))
(com (car (cdr (cdr vip-d-com))))
(reg (nth 3 vip-d-com)))
(if (null val) (setq val (car (cdr vip-d-com))))
(if (null m-com) (error "No previous command to repeat."))
(setq vip-use-register reg)
(funcall m-com (cons val com)))))
(defun vip-special-prefix-com (char)
"This command is invoked interactively by the key sequence #<char>"
(cond ((eq char ?c)
(downcase-region (min vip-com-point (point))
(max vip-com-point (point))))
((eq char ?C)
(upcase-region (min vip-com-point (point))
(max vip-com-point (point))))
((eq char ?g)
(set-mark vip-com-point)
(vip-global-execute))
((eq char ?q)
(set-mark vip-com-point)
(vip-quote-region))
((eq char ?s) (spell-region vip-com-point (point)))))
;; undoing
(defun vip-undo ()
"Undo previous change."
(interactive)
(message "undo!")
(undo-start)
(undo-more 2)
(setq this-command 'vip-undo))
(defun vip-undo-more ()
"Continue undoing previous changes."
(message "undo more!")
(undo-more 1)
(setq this-command 'vip-undo))
;; utilities
(defun vip-string-tail (str)
(if (or (null str) (string= str "")) nil
(substring str 1)))
(defun vip-yank-defun ()
(mark-defun)
(copy-region-as-kill (point) (mark 'force)))
(defun vip-enlarge-region (beg end)
"Enlarge region between BEG and END."
(if (< beg end)
(progn (goto-char beg) (set-mark end))
(goto-char end)
(set-mark beg))
(beginning-of-line)
(exchange-point-and-mark)
(if (or (not (eobp)) (not (bolp))) (next-line 1))
(beginning-of-line)
(if (> beg end) (exchange-point-and-mark)))
(defun vip-global-execute ()
"Call last keyboad macro for each line in the region."
(if (> (point) (mark 'force)) (exchange-point-and-mark))
(beginning-of-line)
(call-last-kbd-macro)
(while (< (point) (mark 'force))
(forward-line 1)
(beginning-of-line)
(call-last-kbd-macro)))
(defun vip-quote-region ()
"Quote region by inserting the user supplied string at the beginning of
each line in the region."
(setq vip-quote-string
(let ((str
(vip-read-string (format "quote string \(default \"%s\"\): "
vip-quote-string))))
(if (string= str "") vip-quote-string str)))
(vip-enlarge-region (point) (mark 'force))
(if (> (point) (mark 'force)) (exchange-point-and-mark))
(insert vip-quote-string)
(beginning-of-line)
(forward-line 1)
(while (and (< (point) (mark 'force)) (bolp))
(insert vip-quote-string)
(beginning-of-line)
(forward-line 1)))
(defun vip-end-with-a-newline-p (string)
"Check if the string ends with a newline."
(or (string= string "")
(eq (aref string (1- (length string))) ?\n)))
(defun vip-read-string (prompt &optional init skk)
"Setup MINIBUFFER-LOCAL-MAP appropriately and call READ-STRING. If
SKK is on, then read string with SKK-J-MODE on."
(setq save-minibuffer-local-map (copy-keymap minibuffer-local-map))
(let (str
(input-mode-str
(and (boundp 'skk-modeline-input-mode)
(skk-indicator-to-string skk-modeline-input-mode t))))
(if (and skk (boundp 'skk-mode) skk-mode)
(add-hook 'minibuffer-setup-hook 'skk-j-mode-on))
(define-key minibuffer-local-map "\C-h" 'backward-char)
(define-key minibuffer-local-map "\C-w" 'backward-word)
(define-key minibuffer-local-map "\e" 'exit-minibuffer)
(condition-case conditions
(setq str (read-string prompt init))
(quit
(setq minibuffer-local-map save-minibuffer-local-map)
(if input-mode-str (setq skk-modeline-input-mode
(skk-mode-string-to-indicator input-mode-str)))
(signal 'quit nil)))
(setq minibuffer-local-map save-minibuffer-local-map)
(if input-mode-str (setq skk-modeline-input-mode
(skk-mode-string-to-indicator input-mode-str)))
str))
;; insertion commands
(defun vip-repeat-insert-command ()
"This function is called when mode changes from insertion mode to
vi command mode. It will repeat the insertion command if original insertion
command was invoked with argument > 1."
(let ((i-com (car vip-d-com)) (val (car (cdr vip-d-com))))
(if (and val (> val 1)) ;; first check that val is non-nil
(progn
(setq vip-d-com (list i-com (1- val) ?r))
(vip-repeat nil)
(setq vip-d-com (list i-com val ?r))))))
(defun vip-insert (arg) ""
(interactive "P")
(let ((val (vip-p-val arg)) (com (vip-getcom arg)))
(setq vip-d-com (list 'vip-insert val ?r))
(if com (vip-loop val (yank))
(vip-change-mode-to-insert))))
(defun vip-append (arg)
"Append after point."
(interactive "P")
(let ((val (vip-p-val arg)) (com (vip-getcom arg)))
(setq vip-d-com (list 'vip-append val ?r))
(if (not (eolp)) (forward-char))
(if (eq com ?r)
(vip-loop val (yank))
(vip-change-mode-to-insert))))
(defun vip-Append (arg)
"Append at end of line."
(interactive "P")
(let ((val (vip-p-val arg)) (com (vip-getcom arg)))
(setq vip-d-com (list 'vip-Append val ?r))
(end-of-line)
(if (eq com ?r)
(vip-loop val (yank))
(vip-change-mode-to-insert))))
(defun vip-Insert (arg)
"Insert before first non-white."
(interactive "P")
(let ((val (vip-p-val arg)) (com (vip-getcom arg)))
(setq vip-d-com (list 'vip-Insert val ?r))
(back-to-indentation)
(if (eq com ?r)
(vip-loop val (yank))
(vip-change-mode-to-insert))))
(defun vip-open-line (arg)
"Open line below."
(interactive "P")
(let ((val (vip-p-val arg)) (com (vip-getcom arg)))
(setq vip-d-com (list 'vip-open-line val ?r))
(let ((col (current-indentation)))
(if (eq com ?r)
(vip-loop val
(progn
(end-of-line)
(newline 1)
(if vip-open-with-indent (indent-to col))
(yank)))
(end-of-line)
(newline 1)
(if vip-open-with-indent (indent-to col))
(vip-change-mode-to-insert)))))
(defun vip-Open-line (arg)
"Open line above."
(interactive "P")
(let ((val (vip-p-val arg)) (com (vip-getcom arg)))
(setq vip-d-com (list 'vip-Open-line val ?r))
(let ((col (current-indentation)))
(if (eq com ?r)
(vip-loop val
(progn
(beginning-of-line)
(open-line 1)
(if vip-open-with-indent (indent-to col))
(yank)))
(beginning-of-line)
(open-line 1)
(if vip-open-with-indent (indent-to col))
(vip-change-mode-to-insert)))))
(defun vip-open-line-at-point (arg)
"Open line at point."