Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / tutorial.el
CommitLineData
6db93af0
CY
1;;; tutorial.el --- tutorial for Emacs
2
409cc4a3 3;; Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
6db93af0
CY
4
5;; Maintainer: FSF
6;; Keywords: help, internal
7
8;; This file is part of GNU Emacs.
9
eb3fa2cf 10;; GNU Emacs is free software: you can redistribute it and/or modify
6db93af0 11;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
6db93af0
CY
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
eb3fa2cf 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
6db93af0
CY
22
23;;; Commentary:
24
25;; Code for running the Emacs tutorial.
26
27;;; History:
28
29;; File was created 2006-09.
30
31;;; Code:
32
33(require 'help-mode) ;; for function help-buffer
6db93af0 34
03e3eb4d 35(defface tutorial-warning-face
c1881005 36 '((t :inherit font-lock-warning-face))
03e3eb4d 37 "Face used to highlight warnings in the tutorial."
c1881005 38 :group 'help)
03e3eb4d 39
cb753f52
CY
40(defvar tutorial--point-before-chkeys 0
41 "Point before display of key changes.")
42(make-variable-buffer-local 'tutorial--point-before-chkeys)
43
44(defvar tutorial--point-after-chkeys 0
45 "Point after display of key changes.")
46(make-variable-buffer-local 'tutorial--point-after-chkeys)
47
48(defvar tutorial--lang nil
49 "Tutorial language.")
50(make-variable-buffer-local 'tutorial--lang)
51
b3fcf4f5
CY
52(defun tutorial--describe-nonstandard-key (value)
53 "Give more information about a changed key binding.
54This is used in `help-with-tutorial'. The information includes
55the key sequence that no longer has a default binding, the
56default binding and the current binding. It also tells in what
57keymap the new binding has been done and how to access the
58function in the default binding from the keyboard.
59
60For `cua-mode' key bindings that try to combine CUA key bindings
61with default Emacs bindings information about this is shown.
62
63VALUE should have either of these formats:
64
65 \(cua-mode)
66 \(current-binding KEY-FUN DEF-FUN KEY WHERE)
67
68Where
69 KEY is a key sequence whose standard binding has been changed
70 KEY-FUN is the actual binding for KEY
71 DEF-FUN is the standard binding of KEY
72 WHERE is a text describing the key sequences to which DEF-FUN is
73 bound now (or, if it is remapped, a key sequence
74 for the function it is remapped to)"
75 (with-output-to-temp-buffer (help-buffer)
76 (help-setup-xref (list #'tutorial--describe-nonstandard-key value)
77 (interactive-p))
78 (with-current-buffer (help-buffer)
79 (insert
80 "Your Emacs customizations override the default binding for this key:"
81 "\n\n")
82 (let ((inhibit-read-only t))
83 (cond
84 ((eq (car value) 'cua-mode)
85 (insert
86 "CUA mode is enabled.
87
88When CUA mode is enabled, you can use C-z, C-x, C-c, and C-v to
89undo, cut, copy, and paste in addition to the normal Emacs
90bindings. The C-x and C-c keys only do cut and copy when the
91region is active, so in most cases, they do not conflict with the
92normal function of these prefix keys.
93
94If you really need to perform a command which starts with one of
95the prefix keys even when the region is active, you have three
96options:
97- press the prefix key twice very quickly (within 0.2 seconds),
98- press the prefix key and the following key within 0.2 seconds, or
99- use the SHIFT key with the prefix key, i.e. C-S-x or C-S-c."))
100 ((eq (car value) 'current-binding)
101 (let ((cb (nth 1 value))
102 (db (nth 2 value))
103 (key (nth 3 value))
104 (where (nth 4 value))
105 map
106 (maps (current-active-maps))
107 mapsym)
108 ;; Look at the currently active keymaps and try to find
109 ;; first the keymap where the current binding occurs:
110 (while maps
111 (let* ((m (car maps))
112 (mb (lookup-key m key t)))
113 (setq maps (cdr maps))
114 (when (eq mb cb)
115 (setq map m)
116 (setq maps nil))))
117 ;; Now, if a keymap was found we must found the symbol
118 ;; name for it to display to the user. This can not
119 ;; always be found since all keymaps does not have a
120 ;; symbol pointing to them, but here they should have
121 ;; that:
122 (when map
123 (mapatoms (lambda (s)
124 (and
125 ;; If not already found
126 (not mapsym)
127 ;; and if s is a keymap
128 (and (boundp s)
129 (keymapp (symbol-value s)))
130 ;; and not the local symbol map
131 (not (eq s 'map))
132 ;; and the value of s is map
133 (eq map (symbol-value s))
134 ;; then save this value in mapsym
135 (setq mapsym s)))))
136 (insert "The default Emacs binding for the key "
137 (key-description key)
138 " is the command `")
139 (insert (format "%s" db))
140 (insert "'. "
141 "However, your customizations have rebound it to the command `")
142 (insert (format "%s" cb))
143 (insert "'.")
144 (when mapsym
145 (insert " (For the more advanced user:"
146 " This binding is in the keymap `"
147 (format "%s" mapsym)
148 "'.)"))
149 (if (string= where "")
150 (unless (keymapp db)
151 (insert "\n\nYou can use M-x "
152 (format "%s" db)
153 " RET instead."))
c3b1f01f 154 (insert "\n\nWith your current key bindings"
f9b9b6b1 155 " you can use "
04cc80ae 156 (if (string-match "^the .*menus?$" where)
f9b9b6b1 157 ""
04cc80ae 158 "the key")
b3fcf4f5
CY
159 where
160 " to get the function `"
161 (format "%s" db)
c1881005 162 "'.")))
b3fcf4f5
CY
163 (fill-region (point-min) (point)))))
164 (print-help-return-message))))
165
166(defun tutorial--sort-keys (left right)
167 "Sort predicate for use with `tutorial--default-keys'.
168This is a predicate function to `sort'.
169
170The sorting is for presentation purpose only and is done on the
171key sequence.
172
173LEFT and RIGHT are the elements to compare."
174 (let ((x (append (cadr left) nil))
175 (y (append (cadr right) nil)))
176 ;; Skip the front part of the key sequences if they are equal:
177 (while (and x y
178 (listp x) (listp y)
179 (equal (car x) (car y)))
180 (setq x (cdr x))
181 (setq y (cdr y)))
182 ;; Try to make a comparision that is useful for presentation (this
183 ;; could be made nicer perhaps):
184 (let ((cx (car x))
185 (cy (car y)))
186 ;;(message "x=%s, y=%s;;;; cx=%s, cy=%s" x y cx cy)
187 (cond
188 ;; Lists? Then call this again
189 ((and cx cy
190 (listp cx)
191 (listp cy))
192 (tutorial--sort-keys cx cy))
193 ;; Are both numbers? Then just compare them
194 ((and (wholenump cx)
195 (wholenump cy))
196 (> cx cy))
197 ;; Is one of them a number? Let that be bigger then.
198 ((wholenump cx)
199 t)
200 ((wholenump cy)
201 nil)
202 ;; Are both symbols? Compare the names then.
203 ((and (symbolp cx)
204 (symbolp cy))
205 (string< (symbol-name cy)
c1881005 206 (symbol-name cx)))))))
b3fcf4f5 207
cb753f52 208(defconst tutorial--default-keys
c1881005
CY
209 ;; On window system, `suspend-emacs' is replaced in the default
210 ;; keymap
7017d784 211 (let* ((suspend-emacs 'suspend-frame)
cb753f52 212 (default-keys
c1881005 213 `((ESC-prefix [27])
cb753f52
CY
214 (Control-X-prefix [?\C-x])
215 (mode-specific-command-prefix [?\C-c])
ed16281f 216 (save-buffers-kill-terminal [?\C-x ?\C-c])
cb753f52 217
cb753f52
CY
218 ;; * SUMMARY
219 (scroll-up [?\C-v])
220 (scroll-down [?\M-v])
96f22160 221 (recenter-top-bottom [?\C-l])
cb753f52 222
cb753f52
CY
223 ;; * BASIC CURSOR CONTROL
224 (forward-char [?\C-f])
225 (backward-char [?\C-b])
cb753f52
CY
226 (forward-word [?\M-f])
227 (backward-word [?\M-b])
cb753f52
CY
228 (next-line [?\C-n])
229 (previous-line [?\C-p])
cb753f52
CY
230 (move-beginning-of-line [?\C-a])
231 (move-end-of-line [?\C-e])
cb753f52
CY
232 (backward-sentence [?\M-a])
233 (forward-sentence [?\M-e])
d166ca6d 234 (newline "\r")
cb753f52
CY
235 (beginning-of-buffer [?\M-<])
236 (end-of-buffer [?\M->])
cb753f52
CY
237 (universal-argument [?\C-u])
238
cb753f52
CY
239 ;; * WHEN EMACS IS HUNG
240 (keyboard-quit [?\C-g])
241
cb753f52
CY
242 ;; * DISABLED COMMANDS
243 (downcase-region [?\C-x ?\C-l])
244
cb753f52
CY
245 ;; * WINDOWS
246 (delete-other-windows [?\C-x ?1])
247 ;; C-u 0 C-l
248 ;; Type CONTROL-h k CONTROL-f.
249
cb753f52
CY
250 ;; * INSERTING AND DELETING
251 ;; C-u 8 * to insert ********.
d166ca6d 252 (delete-backward-char "\d")
cb753f52 253 (delete-char [?\C-d])
7378b2f9 254 (backward-kill-word [?\M-\d])
cb753f52 255 (kill-word [?\M-d])
cb753f52
CY
256 (kill-line [?\C-k])
257 (kill-sentence [?\M-k])
cb753f52
CY
258 (set-mark-command [?\C-@])
259 (set-mark-command [?\C- ])
260 (kill-region [?\C-w])
261 (yank [?\C-y])
262 (yank-pop [?\M-y])
263
cb753f52
CY
264 ;; * UNDO
265 (advertised-undo [?\C-x ?u])
266 (advertised-undo [?\C-x ?u])
267
cb753f52
CY
268 ;; * FILES
269 (find-file [?\C-x ?\C-f])
270 (save-buffer [?\C-x ?\C-s])
271
cb753f52
CY
272 ;; * BUFFERS
273 (list-buffers [?\C-x ?\C-b])
274 (switch-to-buffer [?\C-x ?b])
275 (save-some-buffers [?\C-x ?s])
276
cb753f52
CY
277 ;; * EXTENDING THE COMMAND SET
278 ;; C-x Character eXtend. Followed by one character.
279 (execute-extended-command [?\M-x])
cb753f52
CY
280 ;; C-x C-f Find file
281 ;; C-x C-s Save file
282 ;; C-x s Save some buffers
283 ;; C-x C-b List buffers
284 ;; C-x b Switch buffer
285 ;; C-x C-c Quit Emacs
286 ;; C-x 1 Delete all but one window
287 ;; C-x u Undo
288
cb753f52
CY
289 ;; * MODE LINE
290 (describe-mode [?\C-h ?m])
cb753f52 291 (set-fill-column [?\C-x ?f])
09e8c671 292 (fill-paragraph [?\M-q])
cb753f52 293
cb753f52
CY
294 ;; * SEARCHING
295 (isearch-forward [?\C-s])
296 (isearch-backward [?\C-r])
297
cb753f52
CY
298 ;; * MULTIPLE WINDOWS
299 (split-window-vertically [?\C-x ?2])
300 (scroll-other-window [?\C-\M-v])
301 (other-window [?\C-x ?o])
302 (find-file-other-window [?\C-x ?4 ?\C-f])
303
cb753f52
CY
304 ;; * RECURSIVE EDITING LEVELS
305 (keyboard-escape-quit [27 27 27])
306
cb753f52
CY
307 ;; * GETTING MORE HELP
308 ;; The most basic HELP feature is C-h c
309 (describe-key-briefly [?\C-h ?c])
310 (describe-key [?\C-h ?k])
311
cb753f52
CY
312 ;; * MORE FEATURES
313 ;; F10
314
cb753f52
CY
315 ;; * CONCLUSION
316 ;;(iconify-or-deiconify-frame [?\C-z])
c1881005 317 (,suspend-emacs [?\C-z]))))
cb753f52
CY
318 (sort default-keys 'tutorial--sort-keys))
319 "Default Emacs key bindings that the tutorial depends on.")
6db93af0
CY
320
321(defun tutorial--detailed-help (button)
322 "Give detailed help about changed keys."
323 (with-output-to-temp-buffer (help-buffer)
324 (help-setup-xref (list #'tutorial--detailed-help button)
325 (interactive-p))
326 (with-current-buffer (help-buffer)
327 (let* ((tutorial-buffer (button-get button 'tutorial-buffer))
6db93af0
CY
328 (explain-key-desc (button-get button 'explain-key-desc))
329 (changed-keys (with-current-buffer tutorial-buffer
d166ca6d
CY
330 (save-excursion
331 (goto-char (point-min))
332 (tutorial--find-changed-keys
333 tutorial--default-keys)))))
6db93af0
CY
334 (when changed-keys
335 (insert
5a192d7c
RS
336 "The following key bindings used in the tutorial have been changed
337from the Emacs default:\n\n" )
338 (let ((frm " %-14s %-27s %-16s\n"))
339 (insert (format frm
340 "Standard Key" "Command" "In Your Emacs")))
6db93af0
CY
341 (dolist (tk changed-keys)
342 (let* ((def-fun (nth 1 tk))
343 (key (nth 0 tk))
344 (def-fun-txt (nth 2 tk))
345 (where (nth 3 tk))
346 (remark (nth 4 tk))
347 (rem-fun (command-remapping def-fun))
348 (key-txt (key-description key))
349 (key-fun (with-current-buffer tutorial-buffer (key-binding key)))
350 tot-len)
351 (unless (eq def-fun key-fun)
352 ;; Insert key binding description:
353 (when (string= key-txt explain-key-desc)
03e3eb4d
CY
354 (put-text-property 0 (length key-txt)
355 'face 'tutorial-warning-face key-txt))
6db93af0 356 (insert " " key-txt " ")
5a192d7c 357 (indent-to 18)
6db93af0
CY
358 ;; Insert a link describing the old binding:
359 (insert-button def-fun-txt
360 'value def-fun
361 'action
5a192d7c 362 (lambda (button) (interactive)
6db93af0
CY
363 (describe-function
364 (button-get button 'value)))
365 'follow-link t)
5a192d7c 366 (indent-to 45)
6db93af0
CY
367 (when (listp where)
368 (setq where "list"))
369 ;; Tell where the old binding is now:
5a192d7c 370 (insert (format " %-16s "
d166ca6d
CY
371 (if (string= "" where)
372 (format "M-x %s" def-fun-txt)
373 where)))
6db93af0
CY
374 ;; Insert a link with more information, for example
375 ;; current binding and keymap or information about
376 ;; cua-mode replacements:
377 (insert-button (car remark)
378 'action
5a192d7c 379 (lambda (b) (interactive)
6db93af0
CY
380 (let ((value (button-get b 'value)))
381 (tutorial--describe-nonstandard-key value)))
382 'value (cdr remark)
383 'follow-link t)
384 (insert "\n")))))
385
386 (insert "
c1881005 387It is OK to change key bindings, but changed bindings do not
87fe23ee 388correspond to what the tutorial says.\n\n")
6db93af0
CY
389 (print-help-return-message)))))
390
6db93af0 391(defun tutorial--find-changed-keys (default-keys)
87fe23ee
CY
392 "Find the key bindings used in the tutorial that have changed.
393Return a list with elements of the form
6db93af0 394
87fe23ee 395 '(KEY DEF-FUN DEF-FUN-TXT WHERE REMARK QUIET)
6db93af0 396
87fe23ee 397where
6db93af0 398
6db93af0
CY
399 KEY is a key sequence whose standard binding has been changed
400 DEF-FUN is the standard binding of KEY
401 DEF-FUN-TXT is a short descriptive text for DEF-FUN
402 WHERE is a text describing the key sequences to which DEF-FUN is
403 bound now (or, if it is remapped, a key sequence
404 for the function it is remapped to)
04cc80ae 405 REMARK is a list with info about rebinding. It has either of
a1e49a96 406 these formats:
6db93af0
CY
407
408 \(TEXT cua-mode)
409 \(TEXT current-binding KEY-FUN DEF-FUN KEY WHERE)
410
411 Here TEXT is a link text to show to the user. The
412 rest of the list is used to show information when
413 the user clicks the link.
414
87fe23ee
CY
415 KEY-FUN is the actual binding for KEY.
416 QUIET is t if this changed keybinding should be handled quietly.
417 This is used by `tutorial--display-changes'."
cb753f52 418 (let (changed-keys remark)
7378b2f9
RS
419 ;; Look up the bindings in a Fundamental mode buffer
420 ;; so we do not get fooled by some other major mode.
421 (with-temp-buffer
422 (fundamental-mode)
423 (dolist (kdf default-keys)
424 ;; The variables below corresponds to those with the same names
425 ;; described in the doc string.
426 (let* ((key (nth 1 kdf))
427 (def-fun (nth 0 kdf))
428 (def-fun-txt (format "%s" def-fun))
429 (rem-fun (command-remapping def-fun))
0e01e4af
RS
430 ;; Handle prefix definitions specially
431 ;; so that a mode that rebinds some subcommands
432 ;; won't make it appear that the whole prefix is gone.
7378b2f9
RS
433 (key-fun (if (eq def-fun 'ESC-prefix)
434 (lookup-key global-map [27])
0e01e4af
RS
435 (if (eq def-fun 'Control-X-prefix)
436 (lookup-key global-map [24])
437 (key-binding key))))
04cc80ae
GM
438 (where (where-is-internal (if rem-fun rem-fun def-fun)))
439 cwhere)
0e01e4af 440
7378b2f9
RS
441 (if where
442 (progn
04cc80ae
GM
443 (setq cwhere (car where)
444 where (key-description cwhere))
7378b2f9
RS
445 (when (and (< 10 (length where))
446 (string= (substring where 0 (length "<menu-bar>"))
447 "<menu-bar>"))
04cc80ae
GM
448 (setq where
449 (if (and (vectorp cwhere)
450 (setq cwhere (elt cwhere 1))
451 (setq cwhere
452 (cadr
453 (assoc cwhere
454 (lookup-key global-map
455 [menu-bar]))))
456 (stringp cwhere))
457 (format "the `%s' menu" cwhere)
458 "the menus"))))
7378b2f9
RS
459 (setq where ""))
460 (setq remark nil)
461 (unless
462 (cond ((eq key-fun def-fun)
463 ;; No rebinding, return t
464 t)
465 ((and key-fun
466 (eq key-fun (command-remapping def-fun)))
467 ;; Just a remapping, return t
468 t)
469 ;; cua-mode specials:
470 ((and cua-mode
471 (or (and
472 (equal key [?\C-v])
473 (eq key-fun 'cua-paste))
474 (and
475 (equal key [?\C-z])
476 (eq key-fun 'undo))))
477 (setq remark (list "cua-mode, more info" 'cua-mode))
478 nil)
479 ((and cua-mode
480 (or (and (eq def-fun 'ESC-prefix)
481 (equal key-fun
482 `(keymap
483 (118 . cua-repeat-replace-region)))
484 (setq def-fun-txt "\"ESC prefix\""))
485 (and (eq def-fun 'mode-specific-command-prefix)
486 (equal key-fun
487 '(keymap
488 (timeout . copy-region-as-kill)))
489 (setq def-fun-txt "\"C-c prefix\""))
490 (and (eq def-fun 'Control-X-prefix)
491 (equal key-fun
492 '(keymap (timeout . kill-region)))
493 (setq def-fun-txt "\"C-x prefix\""))))
494 (setq remark (list "cua-mode replacement" 'cua-mode))
495 (setq where "Same key")
496 nil)
497 ;; viper-mode specials:
498 ((and (boundp 'viper-mode-string)
499 (boundp 'viper-current-state)
500 (eq viper-current-state 'vi-state)
501 (or (and (eq def-fun 'isearch-forward)
502 (eq key-fun 'viper-isearch-forward))
503 (and (eq def-fun 'isearch-backward)
504 (eq key-fun 'viper-isearch-backward))))
505 ;; These bindings works as the default bindings,
506 ;; return t
507 t)
508 ((when normal-erase-is-backspace
509 (or (and (equal key [C-delete])
510 (equal key-fun 'kill-word))
511 (and (equal key [C-backspace])
512 (equal key-fun 'backward-kill-word))))
513 ;; This is the strange handling of C-delete and
514 ;; C-backspace, return t
515 t)
516 (t
517 ;; This key has indeed been rebound. Put information
518 ;; in `remark' and return nil
519 (setq remark
520 (list "more info" 'current-binding
521 key-fun def-fun key where))
522 nil))
523 (add-to-list 'changed-keys
524 (list key def-fun def-fun-txt where remark nil))))))
6db93af0
CY
525 changed-keys))
526
c1881005
CY
527(defun tutorial--key-description (key)
528 (let ((desc (key-description key)))
529 (cond ((string= "ESC" desc) "<ESC>")
530 ((string= "RET" desc) "<Return>")
531 ((string= "DEL" desc) "<Delback>")
532 (t desc))))
533
534(defun tutorial--display-changes ()
6db93af0
CY
535 "Display changes to some default key bindings.
536If some of the default key bindings that the tutorial depends on
537have been changed then display the changes in the tutorial buffer
c1881005
CY
538with some explanatory links."
539 (let* ((changed-keys (tutorial--find-changed-keys
540 tutorial--default-keys))
541 ;; Alist of element (DESC . CK) where DESC is the
542 ;; key-description of a changed key and CK is the
543 ;; corresponding element in `changed-keys'.
544 (changed-keys-alist
545 (mapcar (lambda (ck) (cons (tutorial--key-description (car ck)) ck))
546 changed-keys))
87fe23ee 547 changed-key
c1881005
CY
548 (start (point))
549 (case-fold-search nil)
550 (keybindings-regexp
551 (concat "[[:space:]]\\("
87fe23ee
CY
552 (mapconcat (lambda (kdf) (regexp-quote
553 (tutorial--key-description
554 (nth 1 kdf))))
c1881005
CY
555 tutorial--default-keys
556 "\\|")
557 "\\)[[:punct:][:space:]]")))
6db93af0 558 ;; Need the custom button face for viper buttons:
c1881005
CY
559 (if (boundp 'viper-mode-string) (require 'cus-edit))
560
561 (if (or changed-keys (boundp 'viper-mode-string))
562 (let ((head (get-lang-string tutorial--lang 'tut-chgdhead))
563 (head2 (get-lang-string tutorial--lang 'tut-chgdhead2)))
564 (when (and head head2)
565 (goto-char tutorial--point-before-chkeys)
87fe23ee 566 (insert head " [")
c1881005
CY
567 (insert-button head2 'tutorial-buffer (current-buffer)
568 'action 'tutorial--detailed-help
569 'follow-link t 'face 'link)
570 (insert "]\n\n")
571 (add-text-properties tutorial--point-before-chkeys (point)
87fe23ee
CY
572 '(tutorial-remark remark
573 face tutorial-warning-face
574 read-only t)))))
c1881005
CY
575
576 ;; Scan the tutorial for all key sequences.
577 (goto-char (point-min))
578 (while (re-search-forward keybindings-regexp (point-max) t)
579 ;; Then highlight each rebound key sequence.
580 ;; This avoids issuing a warning for, e.g., C-x C-b if C-b is rebound.
87fe23ee
CY
581 (setq changed-key (assoc (match-string 1) changed-keys-alist))
582 (and changed-key
583 (not (get-text-property (match-beginning 1) 'tutorial-remark))
584 (let* ((desc (car changed-key))
585 (ck (cdr changed-key))
586 (key (nth 0 ck))
587 (def-fun (nth 1 ck))
588 (where (nth 3 ck))
589 s1 s2 help-string)
590 (unless (string= where "Same key")
cec8b27d
CY
591 (when (string= where "")
592 (setq where (format "M-x %s" def-fun)))
87fe23ee
CY
593 (setq tutorial--point-after-chkeys (point-marker)
594 s1 (get-lang-string tutorial--lang 'tut-chgdkey)
595 s2 (get-lang-string tutorial--lang 'tut-chgdkey2)
596 help-string (and s1 s2 (format s1 desc where)))
597 (add-text-properties (match-beginning 1) (match-end 1)
598 '(face tutorial-warning-face
599 tutorial-remark key-sequence))
600 (if help-string
601 (if (nth 5 ck)
602 ;; Put help string in the tooltip.
603 (put-text-property (match-beginning 1) (match-end 1)
604 'help-echo help-string)
605 ;; Put help string in the buffer.
606 (save-excursion
607 (setcar (nthcdr 5 ck) t)
608 (forward-line)
609 ;; Two or more changed keys were on the same line.
610 (while (eq (get-text-property (point) 'tutorial-remark)
611 'remark)
612 (forward-line))
613 (setq start (point))
614 (insert "** " help-string " [")
c1881005
CY
615 (insert-button s2 'tutorial-buffer (current-buffer)
616 'action 'tutorial--detailed-help
617 'explain-key-desc desc 'follow-link t
618 'face 'link)
619 (insert "] **\n")
620 (add-text-properties start (point)
87fe23ee
CY
621 '(tutorial-remark remark
622 rear-nonsticky t
c1881005 623 face tutorial-warning-face
87fe23ee 624 read-only t)))))))))))
6db93af0 625
6db93af0 626(defun tutorial--saved-dir ()
c1881005 627 "Directory to which tutorials are saved."
c6bd31a9 628 (expand-file-name "tutorial" user-emacs-directory))
6db93af0
CY
629
630(defun tutorial--saved-file ()
631 "File name in which to save tutorials."
632 (let ((file-name tutorial--lang)
633 (ext (file-name-extension tutorial--lang)))
634 (when (or (not ext)
635 (string= ext ""))
636 (setq file-name (concat file-name ".tut")))
637 (expand-file-name file-name (tutorial--saved-dir))))
638
7378b2f9 639(defun tutorial--remove-remarks ()
6db93af0
CY
640 "Remove the remark lines that was added to the tutorial buffer."
641 (save-excursion
642 (goto-char (point-min))
643 (let (prop-start
644 prop-end
645 prop-val)
646 ;; Catch the case when we already are on a remark line
647 (while (if (get-text-property (point) 'tutorial-remark)
648 (setq prop-start (point))
649 (setq prop-start (next-single-property-change (point) 'tutorial-remark)))
650 (setq prop-end (next-single-property-change prop-start 'tutorial-remark))
651 (setq prop-val (get-text-property prop-start 'tutorial-remark))
652 (unless prop-end
653 (setq prop-end (point-max)))
654 (goto-char prop-end)
87fe23ee
CY
655 (unless (eq prop-val 'key-sequence)
656 (delete-region prop-start prop-end))))))
6db93af0
CY
657
658(defun tutorial--save-tutorial ()
659 "Save the tutorial buffer.
660This saves the part of the tutorial before and after the area
661showing changed keys. It also saves the point position and the
662position where the display of changed bindings was inserted."
663 ;; This runs in a hook so protect it:
664 (condition-case err
9ccc1a31 665 (if (y-or-n-p "Save your position in the tutorial? ")
57581fcc
VJL
666 (tutorial--save-tutorial-to (tutorial--saved-file))
667 (message "Tutorial position not saved"))
9ccc1a31
CY
668 (error (message "Error saving tutorial state: %s"
669 (error-message-string err)))))
6db93af0
CY
670
671(defun tutorial--save-tutorial-to (saved-file)
672 "Save the tutorial buffer to SAVED-FILE.
673See `tutorial--save-tutorial' for more information."
674 ;; Anything to save?
675 (when (or (buffer-modified-p)
676 (< 1 (point)))
677 (let ((tutorial-dir (tutorial--saved-dir))
678 save-err)
679 ;; The tutorial is saved in a subdirectory in the user home
680 ;; directory. Create this subdirectory first.
681 (unless (file-directory-p tutorial-dir)
682 (condition-case err
683 (make-directory tutorial-dir nil)
684 (error (setq save-err t)
685 (warn "Could not create directory %s: %s" tutorial-dir
686 (error-message-string err)))))
687 ;; Make sure we have that directory.
688 (if (file-directory-p tutorial-dir)
689 (let ((tut-point (if (= 0 tutorial--point-after-chkeys)
690 ;; No info about changed keys is
691 ;; displayed.
692 (point)
693 (if (< (point) tutorial--point-after-chkeys)
694 (- (point))
695 (- (point) tutorial--point-after-chkeys))))
696 (old-point (point))
697 ;; Use a special undo list so that we easily can undo
698 ;; the changes we make to the tutorial buffer. This is
699 ;; currently not needed since we now delete the buffer
700 ;; after saving, but kept for possible future use of
701 ;; this function.
702 buffer-undo-list
703 (inhibit-read-only t))
704 ;; Delete the area displaying info about changed keys.
705 ;; (when (< 0 tutorial--point-after-chkeys)
706 ;; (delete-region tutorial--point-before-chkeys
707 ;; tutorial--point-after-chkeys))
708 ;; Delete the remarks:
709 (tutorial--remove-remarks)
710 ;; Put the value of point first in the buffer so it will
711 ;; be saved with the tutorial.
712 (goto-char (point-min))
713 (insert (number-to-string tut-point)
714 "\n"
715 (number-to-string (marker-position
716 tutorial--point-before-chkeys))
717 "\n")
718 (condition-case err
719 (write-region nil nil saved-file)
720 (error (setq save-err t)
721 (warn "Could not save tutorial to %s: %s"
722 saved-file
723 (error-message-string err))))
724 ;; An error is raised here?? Is this a bug?
725 (condition-case err
726 (undo-only)
727 (error nil))
728 ;; Restore point
729 (goto-char old-point)
730 (if save-err
731 (message "Could not save tutorial state.")
732 (message "Saved tutorial state.")))
733 (message "Can't save tutorial: %s is not a directory"
734 tutorial-dir)))))
735
736
737;;;###autoload
738(defun help-with-tutorial (&optional arg dont-ask-for-revert)
739 "Select the Emacs learn-by-doing tutorial.
740If there is a tutorial version written in the language
741of the selected language environment, that version is used.
742If there's no tutorial in that language, `TUTORIAL' is selected.
743With ARG, you are asked to choose which language.
744If DONT-ASK-FOR-REVERT is non-nil the buffer is reverted without
745any question when restarting the tutorial.
746
747If any of the standard Emacs key bindings that are used in the
748tutorial have been changed then an explanatory note about this is
749shown in the beginning of the tutorial buffer.
750
751When the tutorial buffer is killed the content and the point
752position in the buffer is saved so that the tutorial may be
753resumed later."
754 (interactive "P")
755 (if (boundp 'viper-current-state)
cb753f52
CY
756 (let ((prompt1
757 "You can not run the Emacs tutorial directly because you have \
758enabled Viper.")
759 (prompt2 "\nThere is however a Viper tutorial you can run instead.
760Run the Viper tutorial? "))
761 (if (fboundp 'viper-tutorial)
762 (if (y-or-n-p (concat prompt1 prompt2))
763 (progn (message "")
764 (funcall 'viper-tutorial 0))
765 (message "Tutorial aborted by user"))
766 (message prompt1)))
6db93af0
CY
767 (let* ((lang (if arg
768 (let ((minibuffer-setup-hook minibuffer-setup-hook))
769 (add-hook 'minibuffer-setup-hook
770 'minibuffer-completion-help)
771 (read-language-name 'tutorial "Language: " "English"))
772 (if (get-language-info current-language-environment 'tutorial)
773 current-language-environment
774 "English")))
775 (filename (get-language-info lang 'tutorial))
776 ;; Choose a buffer name including the language so that
777 ;; several languages can be tested simultaneously:
778 (tut-buf-name (concat "TUTORIAL (" lang ")"))
779 (old-tut-buf (get-buffer tut-buf-name))
780 (old-tut-win (when old-tut-buf (get-buffer-window old-tut-buf t)))
781 (old-tut-is-ok (when old-tut-buf
782 (not (buffer-modified-p old-tut-buf))))
783 old-tut-file
784 (old-tut-point 1))
785 (setq tutorial--point-after-chkeys (point-min))
786 ;; Try to display the tutorial buffer before asking to revert it.
787 ;; If the tutorial buffer is shown in some window make sure it is
788 ;; selected and displayed:
789 (if old-tut-win
790 (raise-frame
791 (window-frame
792 (select-window (get-buffer-window old-tut-buf t))))
793 ;; Else, is there an old tutorial buffer? Then display it:
794 (when old-tut-buf
795 (switch-to-buffer old-tut-buf)))
796 ;; Use whole frame for tutorial
797 (delete-other-windows)
798 ;; If the tutorial buffer has been changed then ask if it should
799 ;; be reverted:
800 (when (and old-tut-buf
801 (not old-tut-is-ok))
802 (setq old-tut-is-ok
803 (if dont-ask-for-revert
804 nil
805 (not (y-or-n-p
806 "You have changed the Tutorial buffer. Revert it? ")))))
807 ;; (Re)build the tutorial buffer if it is not ok
808 (unless old-tut-is-ok
809 (switch-to-buffer (get-buffer-create tut-buf-name))
810 (unless old-tut-buf (text-mode))
811 (unless lang (error "Variable lang is nil"))
812 (setq tutorial--lang lang)
813 (setq old-tut-file (file-exists-p (tutorial--saved-file)))
814 (let ((inhibit-read-only t))
815 (erase-buffer))
816 (message "Preparing tutorial ...") (sit-for 0)
817
818 ;; Do not associate the tutorial buffer with a file. Instead use
819 ;; a hook to save it when the buffer is killed.
820 (setq buffer-auto-save-file-name nil)
821 (add-hook 'kill-buffer-hook 'tutorial--save-tutorial nil t)
822
823 ;; Insert the tutorial. First offer to resume last tutorial
824 ;; editing session.
825 (when dont-ask-for-revert
826 (setq old-tut-file nil))
827 (when old-tut-file
828 (setq old-tut-file
829 (y-or-n-p "Resume your last saved tutorial? ")))
830 (if old-tut-file
831 (progn
832 (insert-file-contents (tutorial--saved-file))
833 (goto-char (point-min))
834 (setq old-tut-point
835 (string-to-number
836 (buffer-substring-no-properties
837 (line-beginning-position) (line-end-position))))
838 (forward-line)
839 (setq tutorial--point-before-chkeys
840 (string-to-number
841 (buffer-substring-no-properties
842 (line-beginning-position) (line-end-position))))
843 (forward-line)
844 (delete-region (point-min) (point))
845 (goto-char tutorial--point-before-chkeys)
846 (setq tutorial--point-before-chkeys (point-marker)))
bc43a859 847 (insert-file-contents (expand-file-name filename tutorial-directory))
6db93af0
CY
848 (forward-line)
849 (setq tutorial--point-before-chkeys (point-marker)))
850
c1881005 851 (tutorial--display-changes)
6db93af0
CY
852
853 ;; Clear message:
854 (unless dont-ask-for-revert
855 (message "") (sit-for 0))
856
857
858 (if old-tut-file
859 ;; Just move to old point in saved tutorial.
860 (let ((old-point
861 (if (> 0 old-tut-point)
862 (- old-tut-point)
863 (+ old-tut-point tutorial--point-after-chkeys))))
864 (when (< old-point 1)
865 (setq old-point 1))
866 (goto-char old-point))
867 (goto-char (point-min))
868 (search-forward "\n<<")
869 (beginning-of-line)
870 ;; Convert the <<...>> line to the proper [...] line,
871 ;; or just delete the <<...>> line if a [...] line follows.
872 (cond ((save-excursion
873 (forward-line 1)
874 (looking-at "\\["))
875 (delete-region (point) (progn (forward-line 1) (point))))
876 ((looking-at "<<Blank lines inserted.*>>")
877 (replace-match "[Middle of page left blank for didactic purposes. Text continues below]"))
878 (t
879 (looking-at "<<")
880 (replace-match "[")
881 (search-forward ">>")
882 (replace-match "]")))
883 (beginning-of-line)
884 (let ((n (- (window-height (selected-window))
885 (count-lines (point-min) (point))
886 6)))
887 (if (< n 8)
888 (progn
889 ;; For a short gap, we don't need the [...] line,
890 ;; so delete it.
891 (delete-region (point) (progn (end-of-line) (point)))
892 (newline n))
893 ;; Some people get confused by the large gap.
894 (newline (/ n 2))
895
896 ;; Skip the [...] line (don't delete it).
897 (forward-line 1)
898 (newline (- n (/ n 2)))))
899 (goto-char (point-min)))
900 (setq buffer-undo-list nil)
901 (set-buffer-modified-p nil)))))
902
903
904;; Below is some attempt to handle language specific strings. These
905;; are currently only used in the tutorial.
906
907(defconst lang-strings
c1881005 908 '(("English" .
87fe23ee 909 ((tut-chgdkey . "%s has been rebound, but you can use %s instead")
c1881005 910 (tut-chgdkey2 . "More")
6db93af0
CY
911 (tut-chgdhead . "
912 NOTICE: The main purpose of the Emacs tutorial is to teach you
913 the most important standard Emacs commands (key bindings).
914 However, your Emacs has been customized by changing some of
915 these basic editing commands, so it doesn't correspond to the
916 tutorial. We have inserted colored notices where the altered
87fe23ee 917 commands have been introduced.")
c1881005 918 (tut-chgdhead2 . "More"))))
6db93af0
CY
919 "Language specific strings for Emacs.
920This is an association list with the keys equal to the strings
921that can be returned by `read-language-name'. The elements in
922the list are themselves association lists with keys that are
923string ids and values that are the language specific strings.
924
925See `get-lang-string' for more information.")
926
7378b2f9 927(defun get-lang-string (lang stringid &optional no-eng-fallback)
6db93af0 928 "Get a language specific string for Emacs.
a1e49a96
JB
929In certain places Emacs can replace a string shown to the user with
930a language specific string. This function retrieves such strings.
6db93af0 931
04cc80ae 932LANG is the language specification. It should be one of those
6db93af0
CY
933strings that can be returned by `read-language-name'. STRINGID
934is a symbol that specifies the string to retrieve.
935
a1e49a96 936If no string is found for STRINGID in the chosen language then
6db93af0
CY
937the English string is returned unless NO-ENG-FALLBACK is non-nil.
938
939See `lang-strings' for more information.
940
941Currently this feature is only used in `help-with-tutorial'."
942 (let ((my-lang-strings (assoc lang lang-strings))
943 (found-string))
944 (when my-lang-strings
945 (let ((entry (assoc stringid (cdr my-lang-strings))))
946 (when entry
947 (setq found-string (cdr entry)))))
948 ;; Fallback to English strings
949 (unless (or found-string
950 no-eng-fallback)
951 (setq found-string (get-lang-string "English" stringid t)))
952 found-string))
953
954;;(get-lang-string "English" 'tut-chgdkey)
955
956(provide 'tutorial)
957
24b86c51 958;; arch-tag: c8e80aef-c3bb-4ffb-8af6-22171bf0c100
6db93af0 959;;; tutorial.el ends here