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