summaryrefslogtreecommitdiffstats
path: root/emacs.d/nxhtml/util/viper-tut.el
blob: a94104563aff64c177302734e7086d8c09e0581d (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
;;; viper-tut.el --- Viper tutorial
;;
;; Author: Lennart Borgman
;; Created: Fri Sep 08 2006
(defconst viper-tut:version "0.2") ;;Version: 0.2
;; Last-Updated:
;; Keywords:
;; Compatibility: Emacs 22
;;
;; Features that might be required by this library:
;;
;;   `button', `cus-edit', `cus-face', `cus-load', `cus-start',
;;   `help-mode', `tutorial', `view', `wid-edit'.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change log:
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This program 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.
;;
;; This program 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 this program; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:

(eval-when-compile (require 'mumamo))
(eval-when-compile (require 'ourcomments-util))
(require 'tutorial)
(require 'cus-edit)

(defface viper-tut-header-top
  '((t (:foreground "black" :background "goldenrod3")))
  "Face for headers."
  :group 'web-vcs)

(defface viper-tut-header
  '((t (:foreground "black" :background "goldenrod2" :height 1.8)))
  "Face for headers."
  :group 'web-vcs)

(defvar tutorial--tab-map
  (let ((map (make-sparse-keymap)))
    (define-key map [tab] 'forward-button)
    (define-key map [(shift tab)] 'backward-button)
    (define-key map [(meta tab)] 'backward-button)
    map)
  "Keymap that allows tabbing between buttons.")

(defconst viper-tut--emacs-part 6)

(defconst viper-tut--default-keys
  `(
;;;;;;;;;;;;;; Part 1
    ;; ^D	Move DOWN one half-screen
    ;;(viper-scroll-up [(control ?d)])
    (viper-scroll-up [?\C-d])

    ;; ^U	Move UP one half-screen
    ;;(viper-scroll-down [(control ?u)])
    (viper-scroll-down [?\C-u])

    ;; h	Move left one character
    (viper-backward-char [?h])

    ;; j	Move down one line
    (viper-next-line [?j])

    ;; k	Move up one line
    (viper-previous-line [?k])

    ;; l	Move right one character
    (viper-forward-char [?l])

    ;; dd	DELETE one line
    (viper-command-argument [?d])

    ;; x	X-OUT one character
    (viper-delete-char [?x])

    ;; u	UNDO last change
    (viper-undo [?u])

    ;; :q!<RETURN>	QUIT without saving changes
    (viper-ex [?:])

    ;; ZZ	Exit and save any changes
    (viper-save-kill-buffer [?Z ?Z])

    ;; o	OPEN a line for inserting text
    (viper-open-line [?o])

    ;; i	INSERT starting at the cursor
    (viper-insert [?i])

    ;; ESC	ESCAPE from insert mode
    ;;(viper-intercept-ESC-key [(escape)])
                                        ;(viper-intercept-ESC-key [27])
    (viper-intercept-ESC-key [escape])
    ;; chagned-keys=
    ;;       (([27]
    ;;         viper-intercept-ESC-key
    ;;         viper-intercept-ESC-key
    ;;         <escape>
    ;;         (more info current-binding (keymap (118 . cua-repeat-replace-region)) viper-intercept-ESC-key [27] <escape>)))


;;;;;;;;;;;;;; Part 2
    ;; w       Move to the beginning of the next WORD
    (viper-forward-word [?w])
    ;; e       Move to the END of the next word
    (viper-end-of-word [?e])
    ;; b       Move BACK to the beginning to the previous word
    (viper-backward-word [?b])

    ;; $       Move to the end of the line
    (viper-goto-eol [?$])

    ;; ^       Move to the first non-white character on the line
    (viper-bol-and-skip-white [?^])

    ;; 0       Move to the first column on the line (column zero)
    (viper-beginning-of-line [?0])
    ;; #|      Move to an exact column on the line (column #) e.g.  5| 12|
    (viper-goto-col [?|])

    ;; f char	FIND the next occurrence of char on the line
    (viper-find-char-forward [?f])
    ;; t char	Move 'TIL the next occurrence of char on the line
    (viper-goto-char-forward [?t])

    ;; F char	FIND the previous occurrence of char on the line
    (viper-find-char-backward [?F])
    ;; T char	Move 'TIL the previous occurrence of char on the line
    (viper-goto-char-backward [?T])

    ;; ;	Repeat the last  f, t, F, or T
    (viper-repeat-find [?\;])
    ;; ,	Reverse the last  f, t, F, or T
    (viper-repeat-find-opposite [?,])

    ;; %       Show matching () or {} or []
    (viper-exec-mapped-kbd-macro [?%])

    ;; H	Move to the HIGHEST position in the window
    (viper-window-top [?H])
    ;; M	Move to the MIDDLE position in the window
    (viper-window-middle [?M])
    ;; L	Move to the LOWEST position in the window
    (viper-window-bottom [?L])

    ;; m char	MARK this location and name it char
    (viper-mark-point [?m])
    ;; ' char	(quote character) return to line named char
    ;; ''		(quote quote) return from last movement
    (viper-goto-mark-and-skip-white [?'])

    ;; G      GO to the last line in the file
    ;; #G      GO to line #.  (e.g., 3G , 5G , 175G )
    (viper-goto-line [?G])

    ;; {	(left brace) Move to the beginning of a paragraph
    ;; }	(right brace) Move to the end of a paragraph
    (viper-backward-paragraph [?{])
    (viper-forward-paragraph [?}])

    ;; (	(left paren) Move to the beginning of a sentence
    ;; )	(right paren) Move to the beginning of the next sentence
    (viper-backward-sentence [?\(])
    (viper-forward-sentence [?\)])

    ;; [[      Move to the beginning of a section
    ;; ]]      Move to the end of a section
    (viper-brac-function [?\[])
    (viper-ket-function [?\]])

    ;; /string	 Find string looking forward
    (viper-exec-mapped-kbd-macro [?/])
    ;; ?string	 Find string looking backward
    (viper-search-backward [??])

    ;; n	 Repeat last / or ? command
    ;; N	 Reverse last / or ? command
    (viper-search-next [?n])
    (viper-search-Next [?N])


;;;;;;;;;;;;;; Part 3

    ;; #movement	repeat movement # times
    (viper-digit-argument [?1])
    (viper-digit-argument [?2])
    (viper-digit-argument [?3])
    (viper-digit-argument [?4])
    (viper-digit-argument [?5])
    (viper-digit-argument [?6])
    (viper-digit-argument [?7])
    (viper-digit-argument [?8])
    (viper-digit-argument [?9])

    ;; dmovement	DELETE to where "movement" command specifies
    ;; d#movement	DELETE to where the  #movement  command specifies
    ;; d runs the command viper-command-argument

    ;; ymovement	YANK to where "movement" command specifies
    ;; y#movement	YANK to where the  #movement  command specifies
    (viper-command-argument [?y])

    ;; P	(upper p) PUT the contents of the buffer before the cursor
    ;; p	(lower p) PUT the contents of the buffer after the cursor
    (viper-put-back [?p])
    (viper-Put-back [?P])

    ;; "#P	(upper p) PUT contents of buffer # before the cursor
    ;; "#p	(lower p) PUT contents of buffer # after the cursor
    ;;
    ;; "aDELETE	DELETE text into buffer a
    ;; "aYANK	YANK text into buffer a
    ;; "aPUT	PUT text from named buffer a
    (viper-command-argument [?\"])

    ;; :w<RETURN>	WRITE contents of the file (without quitting)

    ;; :e filename<RETURN>    Begin EDITing the file called "filename"



;;;;;;;;;;;;;; Part 4


    ;; o	OPEN a line below the cursor
    ;; O	OPEN a line above the cursor
    (viper-open-line [?o])
    (viper-Open-line [?O])

    ;; i	INSERT starting before the cursor
    ;; I	INSERT at the beginning of the line
    (viper-insert [?i])
    (viper-Insert [?I])

    ;; a	APPEND starting after the cursor
    ;; A	APPEND at the end of the line
    (viper-append [?a])
    (viper-Append [?A])

    ;; ESC	ESCAPE from insert mode
    (viper-intercept-ESC-key [(escape)])

    ;; J	JOIN two lines
    (viper-join-lines [?J])

    ;; #s	SUBSTITUTE for # characters
    ;; #S	SUBSTITUTE for # whole lines
    (viper-substitute [?s])
    (viper-substitute-line [?S])

    ;; r	REPLACE character (NO need to press ESC)
    ;; R	enter over-type mode
    (viper-replace-char [?r])
    (viper-overwrite [?R])

    ;; cmovement	CHANGE to where the movement commands specifies
    (viper-command-argument [?c])


;;;;;;;;;;;;;; Part 5

    ;; ~	(tilde) Convert case of current character
    (viper-toggle-case [?~])
    ;; U	(upper u) UNDO all changes made to the current line
    ;; not implemented
    ;;(viper-undo [?U])

    ;; .	(dot) repeat last change
    (viper-repeat [?.])

    ;; ^F	Move FORWARD one full-screen
    ;; ^B	Move BACKWARD one full-screen
    ;;(viper-scroll-screen [(control ?f)])
    (viper-scroll-screen [?\C-f])
    ;;(viper-scroll-screen-back [(control ?b)])
    (viper-scroll-screen-back [?\C-b])

    ;; ^E	Move the window down one line without moving cursor
    ;; ^Y	Move the window up one line without moving cursor
    ;;(viper-scroll-up-one [(control ?e)])
    (viper-scroll-up-one [?\C-e])
    ;;(viper-scroll-down-one [(control ?y)])
    (viper-scroll-down-one [?\C-y])

    ;; z<RETURN>	Position the current line to top of window
    ;; z.	Position the current line to middle of window
    ;; z-	Position the current line to bottom of window
    (viper-line-to-top "z\C-m")
    (viper-line-to-middle [?z ?.])
    (viper-line-to-bottom [?z ?-])

    ;; ^G	Show status of current file
    ;;(viper-info-on-file [(control ?c)(control ?g)])
    (viper-info-on-file [?\C-c ?\C-g])
    ;; ^L	Refresh screen
    ;;(recenter [(control ?l)])
    (recenter-top-bottom [?\C-l])

    ;; !}fmt	Format the paragraph, joining and filling lines to
    ;; !}sort	Sort lines of a paragraph alphabetically
    (viper-command-argument [?!])

    ;; >movement	Shift right to where the movement command specifies
    ;; <movement	Shift left to where the movement command specifies
    (viper-command-argument [?>])
    (viper-command-argument [?<])

    ))

(defun viper-tut--detailed-help (button)
  "Give detailed help about changed keys."
  (with-output-to-temp-buffer (help-buffer)
    (help-setup-xref (list #'viper-tut--detailed-help button)
                     (interactive-p))
    (with-current-buffer (help-buffer)
      (let* ((tutorial-buffer  (button-get button 'tutorial-buffer))
             ;;(tutorial-arg     (button-get button 'tutorial-arg))
             (explain-key-desc (button-get button 'explain-key-desc))
             (part             (button-get button 'part))
             (changed-keys (with-current-buffer tutorial-buffer
                             (let ((tutorial--lang "English"))
                               (tutorial--find-changed-keys
                                (if (= part viper-tut--emacs-part)
                                    tutorial--default-keys
                                  viper-tut--default-keys))))))
        (when changed-keys
          (insert
           "The following key bindings used in the tutorial had been changed\n"
           (if (= part viper-tut--emacs-part)
               "from Emacs default in the "
             "from Viper default in the ")
           (buffer-name tutorial-buffer) " buffer:\n\n" )
          (let ((frm "   %-9s %-27s %-11s %s\n"))
            (insert (format frm "Key" "Standard Binding" "Is Now On" "Remark")))
          (dolist (tk changed-keys)
            (let* ((def-fun     (nth 1 tk))
                   (key         (nth 0 tk))
                   (def-fun-txt (nth 2 tk))
                   (where       (nth 3 tk))
                   (remark      (nth 4 tk))
                   (rem-fun (command-remapping def-fun))
                   (key-txt (key-description key))
                   (key-fun (with-current-buffer tutorial-buffer (key-binding key)))
                   tot-len)
              (unless (eq def-fun key-fun)
                ;; Insert key binding description:
                (when (string= key-txt explain-key-desc)
                  (put-text-property 0 (length key-txt) 'face '(:background "yellow") key-txt))
                (insert "   " key-txt " ")
                (setq tot-len (length key-txt))
                (when (> 9 tot-len)
                  (insert (make-string (- 9 tot-len) ? ))
                  (setq tot-len 9))
                ;; Insert a link describing the old binding:
                (insert-button def-fun-txt
                               'help-echo (format "Describe function '%s" def-fun-txt)
                               'action `(lambda(button) (interactive)
                                         (describe-function ',def-fun))
                               'follow-link t)
                (setq tot-len (+ tot-len (length def-fun-txt)))
                (when (> 36 tot-len)
                  (insert (make-string (- 36 tot-len) ? )))
                (when (listp where)
                  (setq where "list"))
                ;; Tell where the old binding is now:
                (insert (format " %-11s " where))
                ;; Insert a link with more information, for example
                ;; current binding and keymap or information about
                ;; cua-mode replacements:
                (insert-button (car remark)
                               'help-echo "Give more information about the changed key binding"
                               'action `(lambda(b) (interactive)
                                          (let ((value ,(cdr remark)))
                                            ;; Fix-me:
                                            (tutorial--describe-nonstandard-key value)))
                               'follow-link t)
                (insert "\n")))))



        (insert "
It is legitimate to change key bindings, but changed bindings do not
correspond to what the tutorial says.
\(See also " )
        (insert-button "Key Binding Conventions"
                       'action
                       (lambda(button) (interactive)
                         (info
                          "(elisp) Key Binding Conventions")
                         (message "Type C-x 0 to close the new window"))
                       'follow-link t)
        (insert ".)\n\n")
        (with-no-warnings (print-help-return-message))))))


(defvar viper-tut--part nil
  "Viper tutorial part.")
(make-variable-buffer-local 'viper-tut--part)

(defun viper-tut--saved-file ()
  "File name in which to save tutorials."
  (let* ((file-name
          (file-name-nondirectory (viper-tut--file viper-tut--part)))
         (ext (file-name-extension file-name)))
    (when (or (not ext)
              (string= ext ""))
      (setq file-name (concat file-name ".tut")))
    (expand-file-name file-name (tutorial--saved-dir))))

(defun viper-tut--save-tutorial ()
  "Save the tutorial buffer.
This saves the part of the tutorial before and after the area
showing changed keys.  It also saves point position and the
position where the display of changed bindings was inserted.

Do not save anything if not `viper-mode' is enabled in the
tutorial buffer."
  ;; This runs in a hook so protect it:
  (condition-case err
      (when (boundp 'viper-mode-string)
        (tutorial--save-tutorial-to (viper-tut--saved-file)))
    (error (warn "Error saving tutorial state: %s" (error-message-string err)))))


(defvar viper-tut--parts
  '(
    (0 "0intro" "Introduction")
    (1 "1basics" "Basic Editing")
    (2 "2moving" "Moving Efficiently")
    (3 "3cutpaste" "Cutting and Pasting")
    (4 "4inserting" "Inserting Techniques")
    (5 "5tricks" "Tricks and Timesavers")
    (6 "(no file)" "Emacs tutorial for Viper Users")
    ))

(defcustom viper-tut-directory
  (let* ((this-file (if load-file-name
                        load-file-name
                      (buffer-file-name)))
         (this-dir (file-name-directory this-file)))
    (file-name-as-directory
     (expand-file-name "../etc/viper-tut" this-dir)))
  "Directory where the Viper tutorial files lives."
  :type 'directory
  :group 'viper)

(defun viper-tut--file(part)
  "Get file name for part."
  (let ((tut-file))
    (mapc (lambda(rec)
            (when (= part (nth 0 rec))
              (setq tut-file
                    (if (= part viper-tut--emacs-part)
                        (let ((tf (expand-file-name (get-language-info "English" 'tutorial) tutorial-directory)))
                          (unless (file-exists-p tf)
                            (error "Can't find the English tutorial file for Emacs: %S" tf))
                          tf)
                      (expand-file-name (nth 1 rec) viper-tut-directory)))))
          viper-tut--parts)
    tut-file))

(defun viper-tut-viper-is-on ()
  ;;(message "viper-tut-viper-is-on, vms=%s, cb=%s" (boundp 'viper-mode-string) (current-buffer))
  ;;(boundp 'viper-mode-string)
  (boundp 'viper-current-state))

(defun viper-tut--display-changes (changed-keys part)
  "Display changes to some default Viper key bindings.
If some of the default key bindings that the Viper tutorial
depends on have been changed then display the changes in the
tutorial buffer with some explanatory links.

CHANGED-KEYS should be a list in the format returned by
`tutorial--find-changed-keys'."
  (when (or changed-keys
            (viper-tut-viper-is-on))
    ;; Need the custom button face for viper buttons:
    ;;(when (and (boundp 'viper-mode) viper-mode) (require 'cus-edit))
    (goto-char tutorial--point-before-chkeys)
    (let* ((start (point))
           end
           (head
            (if (viper-tut-viper-is-on)
                (if (= part viper-tut--emacs-part)
                    "
 NOTICE: This part of the Viper tutorial runs the Emacs tutorial.
 Several keybindings are changed from Emacs default (either
 because of Viper or some other customization) and doesn't
 correspond to the tutorial.

 We have inserted colored notices where the altered commands have
 been introduced.  If you change Viper state (vi state, insert
 state, etc) these notices will be changed to reflect the new
 state. ["
                  "
 NOTICE: The main purpose of the Viper tutorial is to teach you
 the most important vi commands (key bindings).  However, your
 Emacs has been customized by changing some of these basic Viper
 editing commands, so it doesn't correspond to the tutorial.  We
 have inserted colored notices where the altered commands have
 been introduced. [")
              "
  NOTICE: You have currently not turned on Viper. Nothing in this
  tutorial \(the Viper Tutorial\) will work unless you do that. ["
              ))
           (head2 (if (viper-tut-viper-is-on)
                      (get-lang-string tutorial--lang 'tut-chgdhead2)
                    "More information")))
      (when (and head head2)
        (insert head)
        (insert-button head2
                       'tutorial-buffer
                       (current-buffer)
                       ;;'tutorial-arg arg
                       'part part
                       'action
                       (if (viper-tut-viper-is-on)
                           'viper-tut--detailed-help
                         'go-home-blaha)
                       'follow-link t
                       'echo "Click for more information"
                       'face '(:inherit link :background "yellow"))
        (insert "]\n\n" )
        (when changed-keys
          (dolist (tk changed-keys)
            (let* ((def-fun     (nth 1 tk))
                   (key         (nth 0 tk))
                   (def-fun-txt (nth 2 tk))
                   (where       (nth 3 tk))
                   (remark      (nth 4 tk))
                   (rem-fun (command-remapping def-fun))
                   (key-txt (key-description key))
                   (key-fun (key-binding key))
                   tot-len)
              (unless (eq def-fun key-fun)
                ;; Mark the key in the tutorial text
                (unless (string= "Same key" where)
                  (let* ((here (point))
                         (key-desc (key-description key))
                         (vi-char (= 1 (length key-desc)))
                         vi-char-pos
                         hit)
                    (when (string= "RET" key-desc)
                      (setq key-desc "Return"))
                    (when (string= "DEL" key-desc)
                      (setq key-desc "Delback"))
                    (while (if (not vi-char)
                               (unless hit ;; Only tell once
                                 (setq hit t)
                                 (re-search-forward
                                  (concat "[^[:alpha:]]\\("
                                          (regexp-quote key-desc)
                                          "\\)[^[:alpha:]]") nil t))
                             (setq vi-char-pos
                                   (next-single-property-change
                                    (point) 'vi-char)))
                      (if (not vi-char)
                          (put-text-property (match-beginning 0)
                                             (match-end 0)
                                             'tutorial-remark nil) ;;'only-colored)
                        (put-text-property (match-beginning 0)
                                           (match-end 0)
                                           'face '(:background "yellow"))
                        (goto-char (1+ vi-char-pos))
                        (setq hit (string= key-desc (char-to-string (char-before))))
                        (when hit
                          (put-text-property vi-char-pos (1+ vi-char-pos)
                                             'face '(:background "yellow"))))
                      (when hit
                        (forward-line)
                        (let ((s  (get-lang-string tutorial--lang 'tut-chgdkey))
                              (s2 (get-lang-string tutorial--lang 'tut-chgdkey2))
                              (start (point))
                              end)
                          ;; key-desc " has been rebound, but you can use " where " instead ["))
                          (when (and s s2)
                            (when (or (not where) (= 0 (length where)))
                              (setq where (concat "`M-x " def-fun-txt "'")))
                            (setq s (format s key-desc where s2))
                            (insert s " [")
                            (insert-button s2
                                           'tutorial-buffer
                                           (current-buffer)
                                           ;;'tutorial-arg arg
                                           'part part
                                           'action
                                           'viper-tut--detailed-help
                                           'explain-key-desc key-desc
                                           'follow-link t
                                           'face '(:inherit link :background "yellow"))
                            (insert "] **")
                            (insert "\n")
                            (setq end (point))
                            (put-text-property start end 'local-map tutorial--tab-map)
                            (put-text-property start end 'tutorial-remark t)
                            (put-text-property start end
                                               'face '(:background "yellow" :foreground "#c00"))
                            (put-text-property start end 'read-only t)))))
                    (goto-char here)))))))


        (setq end (point))
        ;; Make the area with information about change key
        ;; bindings stand out:
        (put-text-property start end
                           'face
                           ;; The default warning face does not
                           ;;look good in this situation. Instead
                           ;;try something that could be
                           ;;recognized from warnings in normal
                           ;;life:
                           ;; 'font-lock-warning-face
                           (list :background "yellow" :foreground "#c00"))
        ;; Make it possible to use Tab/S-Tab between fields in
        ;; this area:
        (put-text-property start end 'local-map tutorial--tab-map)
        (put-text-property start end 'tutorial-remark t)
        (setq tutorial--point-after-chkeys (point-marker))
        ;; Make this area read-only:
        (put-text-property start end 'read-only t)))))

(defun viper-tut--at-change-state()
  (condition-case err
      (progn
        (let ((inhibit-read-only t)
              (here (point)))
          ;; Delete the remarks:
          ;;(tutorial--remove-remarks)
          ;; Add them again
          ;;(viper-tut--add-remarks)
          (goto-char here)
          )
        )
    (error (message "error in viper-tut--at-change-state: %s" (error-message-string err)))))


;;;###autoload
(defun viper-tutorial(part &optional dont-ask-for-revert)
  "Run a tutorial for Viper.

A simple classic tutorial in 5 parts that have been used by many
people starting to learn vi keys.  You may learn enough to start
using `viper-mode' in Emacs.

Some people find that vi keys helps against repetetive strain
injury, see URL

  `http://www.emacswiki.org/emacs/RepeatedStrainInjury'.

Note: There might be a few clashes between vi key binding and
Emacs standard key bindings.  You will be notified about those in
the tutorial.  Even more, if your own key bindings comes in
between you will be notified about that too."
  (interactive (list
                ;;                 (condition-case nil
                ;;                     (widget-choose "The following viper tutorials are available"
                ;;                                    (mapcar (lambda(rec)
                ;;                                              (cons (nth 2 rec) (nth 0 rec)))
                ;;                                            viper-tut--parts))
                ;;                   (error nil))
                0
                ))
  (if (not (boundp 'viper-current-state))
      (let ((prompt
             "
  You can not run the Viper tutorial in this Emacs because you
  have not enabled Viper.

  Do you want to run the Viper tutorial in a new Emacs? "))
        (if (y-or-n-p prompt)
            (let ((ret (funcall 'emacs--no-desktop
                                "-eval"
                                (concat
                                 "(progn"
                                 " (setq viper-mode t)"
                                 " (require 'viper)"
                                 " (require 'viper-tut)"
                                 " (call-interactively 'viper-tutorial))"))))
              (message "Starting Viper tutorial in a new Emacs"))
          (message "Viper tutorial aborted by user")))

    (let* ((filename (viper-tut--file part))
           ;; Choose a buffer name including the language so that
           ;; several languages can be tested simultaneously:
           (tut-buf-name "Viper TUTORIAL")
           (old-tut-buf (get-buffer tut-buf-name))
           (old-tut-part (when old-tut-buf
                           (with-current-buffer old-tut-buf
                             viper-tut--part)))
           (old-tut-win (when old-tut-buf (get-buffer-window old-tut-buf t)))
           (old-tut-is-ok (when old-tut-buf
                            (and
                             (= part old-tut-part)
                             (not (buffer-modified-p old-tut-buf)))))
           old-tut-file
           (old-tut-point 1))
      (unless (file-exists-p filename) (error "Can't fine %s" filename))
      (setq tutorial--point-after-chkeys (point-min))
      ;; Try to display the tutorial buffer before asking to revert it.
      ;; If the tutorial buffer is shown in some window make sure it is
      ;; selected and displayed:
      (if old-tut-win
          (raise-frame
           (window-frame
            (select-window (get-buffer-window old-tut-buf t))))
        ;; Else, is there an old tutorial buffer? Then display it:
        (when old-tut-buf
          (switch-to-buffer old-tut-buf)))
      ;; Use whole frame for tutorial
      ;;(delete-other-windows)
      ;; If the tutorial buffer has been changed then ask if it should
      ;; be reverted:
      (when (and old-tut-buf
                 (not old-tut-is-ok)
                 (= part old-tut-part))
        (setq old-tut-is-ok
              (if dont-ask-for-revert
                  nil
                (not (y-or-n-p
                      "You have changed the Tutorial buffer.  Revert it? ")))))
      ;; (Re)build the tutorial buffer if it is not ok
      (unless old-tut-is-ok
        (switch-to-buffer (get-buffer-create tut-buf-name))
        (unless old-tut-buf (text-mode))
        (setq viper-tut--part part)
        (setq old-tut-file (file-exists-p (viper-tut--saved-file)))
        (when (= part 0) (setq old-tut-file nil)) ;; You do not edit in the intro
        (setq buffer-read-only nil)
        (let ((inhibit-read-only t)) ;; For the text property
          (erase-buffer))
        (message "Preparing Viper tutorial ...") (sit-for 0)

        ;; Do not associate the tutorial buffer with a file. Instead use
        ;; a hook to save it when the buffer is killed.
        (setq buffer-auto-save-file-name nil)
        (add-hook 'kill-buffer-hook 'viper-tut--save-tutorial nil t)

        ;; Insert the tutorial. First offer to resume last tutorial
        ;; editing session.
        (when dont-ask-for-revert
          (setq old-tut-file nil))
        (when old-tut-file
          (setq old-tut-file
                (y-or-n-p
                 (format
                  "Resume your last saved Viper tutorial part %s? "
                  part))))
        (if old-tut-file
            (progn
              (insert-file-contents (viper-tut--saved-file))
              (goto-char (point-min))
              (setq old-tut-point
                    (string-to-number
                     (buffer-substring-no-properties
                      (line-beginning-position) (line-end-position))))
              (forward-line)
              (setq tutorial--point-before-chkeys
                    (string-to-number
                     (buffer-substring-no-properties
                      (line-beginning-position) (line-end-position))))
              (forward-line)
              (delete-region (point-min) (point))
              (goto-char tutorial--point-before-chkeys)
              (setq tutorial--point-before-chkeys (point-marker)))
          ;;(insert-file-contents (expand-file-name filename data-directory))
          (insert-file-contents filename)
          (viper-tut--replace-links)
          (save-excursion
            (goto-char (point-min))
            (while (re-search-forward "'\\([][+a-zA-Z~<>!;,:.'\"%/?(){}$^0|-]\\)'" nil t)
              (let ((matched-char (match-string 1))
                    (inhibit-read-only t))
                (put-text-property 0 1 'vi-char t matched-char)
                (put-text-property 0 1 'face '(:foreground "blue") matched-char)
                (replace-match matched-char))))
          (forward-line)
          (setq tutorial--point-before-chkeys (point-marker)))

        (viper-tut--add-remarks)

        (goto-char (point-min))
        (when old-tut-file
          ;; Just move to old point in saved tutorial.
          (let ((old-point
                 (if (> 0 old-tut-point)
                     (- old-tut-point)
                   (+ old-tut-point tutorial--point-after-chkeys))))
            (when (< old-point 1)
              (setq old-point 1))
            (goto-char old-point)))

        (viper-tut-fix-header-and-footer)

        ;; Clear message:
        (message "") (sit-for 0)

        (setq buffer-undo-list nil)
        (set-buffer-modified-p nil))
      (setq buffer-read-only (= 0 part)))))

;;(tutorial--find-changed-keys '((scroll-up [?\C-v])))
(defun viper-tut--add-remarks()
  ;; Check if there are key bindings that may disturb the
  ;; tutorial.  If so tell the user.
  (let* ((tutorial--lang "English")
         (changed-keys
          (if (= viper-tut--part viper-tut--emacs-part)
              (tutorial--find-changed-keys tutorial--default-keys)
            (tutorial--find-changed-keys viper-tut--default-keys))))
    (viper-tut--display-changes changed-keys viper-tut--part))

  (if (= viper-tut--part viper-tut--emacs-part)
      (progn
        (add-hook 'viper-vi-state-hook 'viper-tut--at-change-state nil t)
        (add-hook 'viper-insert-state-hook 'viper-tut--at-change-state nil t)
        (add-hook 'viper-replace-state-hook 'viper-tut--at-change-state nil t)
        (add-hook 'viper-emacs-state-hook 'viper-tut--at-change-state nil t)
        )
    (remove-hook 'viper-vi-state-hook 'viper-tut--at-change-state t)
    (remove-hook 'viper-insert-statehook 'viper-tut--at-change-state t)
    (remove-hook 'viper-replace-state-hook 'viper-tut--at-change-state t)
    (remove-hook 'viper-emacs-state-hook 'viper-tut--at-change-state t)
    ))

(defun viper-tut-fix-header-and-footer ()
  (save-excursion
    (goto-char (point-min))
    (add-text-properties (point) (1+ (line-end-position))
                         '( read-only t face viper-tut-header))
    (goto-char (point-min))
    (viper-tut--insert-goto-row nil)
    (goto-char (point-max))
    (viper-tut--insert-goto-row t)))

(defun viper-tut--insert-goto-row(last)
  (let ((start (point))
        end)
    (insert " Go to part: ")
    (dolist (rec viper-tut--parts)
      (let ((n (nth 0 rec))
            (file (nth 1 rec))
            (title (nth 2 rec)))
        (if (= n viper-tut--part)
            (insert (format "%s" n))
          (insert-button (format "%s" n)
                         'help-echo (concat "Go to part: " title)
                         'follow-link t
                         'action
                         `(lambda (button)
                            (viper-tutorial ,n t))))
        (insert "  ")))
    (insert "   ")
    (insert-button "Exit Tutorial"
                   'help-echo "Exit tutorial and close tutorial buffer"
                   'follow-link t
                   'action
                   (lambda (button)
                     (kill-buffer (current-buffer))))
    (unless last (insert "\n"))
    (setq end (point))
    (put-text-property start end 'local-map tutorial--tab-map)
    (put-text-property start end 'tutorial-remark t)
    (put-text-property start end
                       'face 'viper-tut-header-top)
    (put-text-property start end 'read-only t)))

(defun viper-tut--replace-links()
  "Replace markers for links with actual links."
  (let ((re-links (regexp-opt '("VIPER-MANUAL"
                                "README-FILE"
                                "DIGIT-ARGUMENT"
                                "KILL-BUFFER"
                                "ISEARCH-FORWARD"
                                "UNIVERSAL-ARGUMENT"
                                "SEARCH-COMMANDS"
                                "R-AND-R"
                                "CUA-MODE"
                                "KEYBOARD-MACROS"
                                "VIPER-TOGGLE-KEY"
                                "* EMACS-NOTICE:")))
        (case-fold-search nil)
        (inhibit-read-only t))
    (save-excursion
      (goto-char (point-min))
      (while (re-search-forward re-links nil t)
        (let ((matched (match-string 0))
              start
              end)
          (replace-match "")
          (setq start (point))
          (cond
           ((string= matched "VIPER-TOGGLE-KEY")
            (insert-button "viper-toggle-key"
                           'action
                           (lambda(button) (interactive)
                             (describe-variable 'viper-toggle-key))
                           'follow-link t))
           ((string= matched "CUA-MODE")
            (insert-button "cua-mode"
                           'action
                           (lambda(button) (interactive)
                             (describe-function 'cua-mode))
                           'follow-link t))
           ((string= matched "ISEARCH-FORWARD")
            (insert-button "isearch-forward"
                           'action
                           (lambda(button) (interactive)
                             (describe-function 'isearch-forward))
                           'follow-link t))
           ((string= matched "KILL-BUFFER")
            (insert-button "kill-buffer"
                           'action
                           (lambda(button) (interactive)
                             (describe-function 'kill-buffer))
                           'follow-link t))
           ((string= matched "UNIVERSAL-ARGUMENT")
            (insert-button "universal-argument"
                           'action
                           (lambda(button) (interactive)
                             (describe-function 'universal-argument))
                           'follow-link t))
           ((string= matched "DIGIT-ARGUMENT")
            (insert-button "digit-argument"
                           'action
                           (lambda(button) (interactive)
                             (describe-function 'digit-argument))
                           'follow-link t))
           ((string= matched "* EMACS-NOTICE:")
            (insert "* Emacs NOTICE:")
            (while (progn
                     (forward-line 1)
                     (not (looking-at "^$"))))
            (put-text-property start (point)
                               'face '(:background
                                       "#ffe4b5"
                                       :foreground "#999999"))
            (put-text-property start (point) 'read-only t)
            )
           ((string= matched "SEARCH-COMMANDS")
            (insert-button "search commands"
                           'action
                           (lambda(button) (interactive)
                             (info-other-window "(emacs) Search")
                             (message "Type C-x 0 to close the new window"))
                           'follow-link t))
           ((string= matched "KEYBOARD-MACROS")
            (insert-button "keyboard macros"
                           'action
                           (lambda(button) (interactive)
                             (info-other-window "(emacs) Keyboard Macros")
                             (message "Type C-x 0 to close the new window"))
                           'follow-link t))
           ((string= matched "VIPER-MANUAL")
            (insert-button "Viper manual"
                           'action
                           (lambda(button) (interactive)
                             (info-other-window "(viper)")
                             (message "Type C-x 0 to close the new window"))
                           'follow-link t))
           ((string= matched "R-AND-R")
            (insert-button "r and R"
                           'action
                           (lambda(button) (interactive)
                             (info-other-window "(viper) Basics")
                             (message "Type C-x 0 to close the new window"))
                           'follow-link t))
           ((string= matched "README-FILE")
            (insert-button "README file"
                           'action
                           (lambda(button) (interactive)
                             (find-file-other-window (expand-file-name "README" viper-tut-directory))
                             (message "Type C-x 0 to close the new window"))
                           'follow-link t))
           (t
            (error "Unmatched text: %s" matched)))
          (put-text-property start (point) 'tutorial-remark t)
          (put-text-property start (point) 'tutorial-orig matched)
          (put-text-property start (point) 'local-map tutorial--tab-map)
          (put-text-property start (point) 'read-only t))))))

(provide 'viper-tut)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; viper-tut.el ends here