(vc-dir-mode-map): Bind "d" to vc-dir-delete-file.
[emacs.git] / lisp / tutorial.el
blob1e9c7712e6df440c61e41c46754bc9fa0f13de2c
1 ;;; tutorial.el --- tutorial for Emacs
3 ;; Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: help, internal
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
13 ;; any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
25 ;;; Commentary:
27 ;; Code for running the Emacs tutorial.
29 ;;; History:
31 ;; File was created 2006-09.
33 ;;; Code:
35 (require 'help-mode) ;; for function help-buffer
37 (defface tutorial-warning-face
38 '((t :inherit font-lock-warning-face))
39 "Face used to highlight warnings in the tutorial."
40 :group 'help)
42 (defvar tutorial--point-before-chkeys 0
43 "Point before display of key changes.")
44 (make-variable-buffer-local 'tutorial--point-before-chkeys)
46 (defvar tutorial--point-after-chkeys 0
47 "Point after display of key changes.")
48 (make-variable-buffer-local 'tutorial--point-after-chkeys)
50 (defvar tutorial--lang nil
51 "Tutorial language.")
52 (make-variable-buffer-local 'tutorial--lang)
54 (defun tutorial--describe-nonstandard-key (value)
55 "Give more information about a changed key binding.
56 This is used in `help-with-tutorial'. The information includes
57 the key sequence that no longer has a default binding, the
58 default binding and the current binding. It also tells in what
59 keymap the new binding has been done and how to access the
60 function in the default binding from the keyboard.
62 For `cua-mode' key bindings that try to combine CUA key bindings
63 with default Emacs bindings information about this is shown.
65 VALUE should have either of these formats:
67 \(cua-mode)
68 \(current-binding KEY-FUN DEF-FUN KEY WHERE)
70 Where
71 KEY is a key sequence whose standard binding has been changed
72 KEY-FUN is the actual binding for KEY
73 DEF-FUN is the standard binding of KEY
74 WHERE is a text describing the key sequences to which DEF-FUN is
75 bound now (or, if it is remapped, a key sequence
76 for the function it is remapped to)"
77 (with-output-to-temp-buffer (help-buffer)
78 (help-setup-xref (list #'tutorial--describe-nonstandard-key value)
79 (interactive-p))
80 (with-current-buffer (help-buffer)
81 (insert
82 "Your Emacs customizations override the default binding for this key:"
83 "\n\n")
84 (let ((inhibit-read-only t))
85 (cond
86 ((eq (car value) 'cua-mode)
87 (insert
88 "CUA mode is enabled.
90 When CUA mode is enabled, you can use C-z, C-x, C-c, and C-v to
91 undo, cut, copy, and paste in addition to the normal Emacs
92 bindings. The C-x and C-c keys only do cut and copy when the
93 region is active, so in most cases, they do not conflict with the
94 normal function of these prefix keys.
96 If you really need to perform a command which starts with one of
97 the prefix keys even when the region is active, you have three
98 options:
99 - press the prefix key twice very quickly (within 0.2 seconds),
100 - press the prefix key and the following key within 0.2 seconds, or
101 - use the SHIFT key with the prefix key, i.e. C-S-x or C-S-c."))
102 ((eq (car value) 'current-binding)
103 (let ((cb (nth 1 value))
104 (db (nth 2 value))
105 (key (nth 3 value))
106 (where (nth 4 value))
108 (maps (current-active-maps))
109 mapsym)
110 ;; Look at the currently active keymaps and try to find
111 ;; first the keymap where the current binding occurs:
112 (while maps
113 (let* ((m (car maps))
114 (mb (lookup-key m key t)))
115 (setq maps (cdr maps))
116 (when (eq mb cb)
117 (setq map m)
118 (setq maps nil))))
119 ;; Now, if a keymap was found we must found the symbol
120 ;; name for it to display to the user. This can not
121 ;; always be found since all keymaps does not have a
122 ;; symbol pointing to them, but here they should have
123 ;; that:
124 (when map
125 (mapatoms (lambda (s)
126 (and
127 ;; If not already found
128 (not mapsym)
129 ;; and if s is a keymap
130 (and (boundp s)
131 (keymapp (symbol-value s)))
132 ;; and not the local symbol map
133 (not (eq s 'map))
134 ;; and the value of s is map
135 (eq map (symbol-value s))
136 ;; then save this value in mapsym
137 (setq mapsym s)))))
138 (insert "The default Emacs binding for the key "
139 (key-description key)
140 " is the command `")
141 (insert (format "%s" db))
142 (insert "'. "
143 "However, your customizations have rebound it to the command `")
144 (insert (format "%s" cb))
145 (insert "'.")
146 (when mapsym
147 (insert " (For the more advanced user:"
148 " This binding is in the keymap `"
149 (format "%s" mapsym)
150 "'.)"))
151 (if (string= where "")
152 (unless (keymapp db)
153 (insert "\n\nYou can use M-x "
154 (format "%s" db)
155 " RET instead."))
156 (insert "\n\nWith your current key bindings"
157 " you can use "
158 (if (string-match "^the .*menus?$" where)
160 "the key")
161 where
162 " to get the function `"
163 (format "%s" db)
164 "'.")))
165 (fill-region (point-min) (point)))))
166 (print-help-return-message))))
168 (defun tutorial--sort-keys (left right)
169 "Sort predicate for use with `tutorial--default-keys'.
170 This is a predicate function to `sort'.
172 The sorting is for presentation purpose only and is done on the
173 key sequence.
175 LEFT and RIGHT are the elements to compare."
176 (let ((x (append (cadr left) nil))
177 (y (append (cadr right) nil)))
178 ;; Skip the front part of the key sequences if they are equal:
179 (while (and x y
180 (listp x) (listp y)
181 (equal (car x) (car y)))
182 (setq x (cdr x))
183 (setq y (cdr y)))
184 ;; Try to make a comparision that is useful for presentation (this
185 ;; could be made nicer perhaps):
186 (let ((cx (car x))
187 (cy (car y)))
188 ;;(message "x=%s, y=%s;;;; cx=%s, cy=%s" x y cx cy)
189 (cond
190 ;; Lists? Then call this again
191 ((and cx cy
192 (listp cx)
193 (listp cy))
194 (tutorial--sort-keys cx cy))
195 ;; Are both numbers? Then just compare them
196 ((and (wholenump cx)
197 (wholenump cy))
198 (> cx cy))
199 ;; Is one of them a number? Let that be bigger then.
200 ((wholenump cx)
202 ((wholenump cy)
203 nil)
204 ;; Are both symbols? Compare the names then.
205 ((and (symbolp cx)
206 (symbolp cy))
207 (string< (symbol-name cy)
208 (symbol-name cx)))))))
210 (defconst tutorial--default-keys
211 ;; On window system, `suspend-emacs' is replaced in the default
212 ;; keymap
213 (let* ((suspend-emacs 'suspend-frame)
214 (default-keys
215 `((ESC-prefix [27])
216 (Control-X-prefix [?\C-x])
217 (mode-specific-command-prefix [?\C-c])
218 (save-buffers-kill-terminal [?\C-x ?\C-c])
220 ;; * SUMMARY
221 (scroll-up [?\C-v])
222 (scroll-down [?\M-v])
223 (recenter-top-bottom [?\C-l])
225 ;; * BASIC CURSOR CONTROL
226 (forward-char [?\C-f])
227 (backward-char [?\C-b])
228 (forward-word [?\M-f])
229 (backward-word [?\M-b])
230 (next-line [?\C-n])
231 (previous-line [?\C-p])
232 (move-beginning-of-line [?\C-a])
233 (move-end-of-line [?\C-e])
234 (backward-sentence [?\M-a])
235 (forward-sentence [?\M-e])
236 (newline "\r")
237 (beginning-of-buffer [?\M-<])
238 (end-of-buffer [?\M->])
239 (universal-argument [?\C-u])
241 ;; * WHEN EMACS IS HUNG
242 (keyboard-quit [?\C-g])
244 ;; * DISABLED COMMANDS
245 (downcase-region [?\C-x ?\C-l])
247 ;; * WINDOWS
248 (delete-other-windows [?\C-x ?1])
249 ;; C-u 0 C-l
250 ;; Type CONTROL-h k CONTROL-f.
252 ;; * INSERTING AND DELETING
253 ;; C-u 8 * to insert ********.
254 (delete-backward-char "\d")
255 (delete-char [?\C-d])
256 (backward-kill-word [?\M-\d])
257 (kill-word [?\M-d])
258 (kill-line [?\C-k])
259 (kill-sentence [?\M-k])
260 (set-mark-command [?\C-@])
261 (set-mark-command [?\C- ])
262 (kill-region [?\C-w])
263 (yank [?\C-y])
264 (yank-pop [?\M-y])
266 ;; * UNDO
267 (advertised-undo [?\C-x ?u])
268 (advertised-undo [?\C-x ?u])
270 ;; * FILES
271 (find-file [?\C-x ?\C-f])
272 (save-buffer [?\C-x ?\C-s])
274 ;; * BUFFERS
275 (list-buffers [?\C-x ?\C-b])
276 (switch-to-buffer [?\C-x ?b])
277 (save-some-buffers [?\C-x ?s])
279 ;; * EXTENDING THE COMMAND SET
280 ;; C-x Character eXtend. Followed by one character.
281 (execute-extended-command [?\M-x])
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
291 ;; * MODE LINE
292 (describe-mode [?\C-h ?m])
293 (set-fill-column [?\C-x ?f])
294 (fill-paragraph [?\M-q])
296 ;; * SEARCHING
297 (isearch-forward [?\C-s])
298 (isearch-backward [?\C-r])
300 ;; * MULTIPLE WINDOWS
301 (split-window-vertically [?\C-x ?2])
302 (scroll-other-window [?\C-\M-v])
303 (other-window [?\C-x ?o])
304 (find-file-other-window [?\C-x ?4 ?\C-f])
306 ;; * RECURSIVE EDITING LEVELS
307 (keyboard-escape-quit [27 27 27])
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])
314 ;; * MORE FEATURES
315 ;; F10
317 ;; * CONCLUSION
318 ;;(iconify-or-deiconify-frame [?\C-z])
319 (,suspend-emacs [?\C-z]))))
320 (sort default-keys 'tutorial--sort-keys))
321 "Default Emacs key bindings that the tutorial depends on.")
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)
327 (interactive-p))
328 (with-current-buffer (help-buffer)
329 (let* ((tutorial-buffer (button-get button 'tutorial-buffer))
330 (explain-key-desc (button-get button 'explain-key-desc))
331 (changed-keys (with-current-buffer tutorial-buffer
332 (save-excursion
333 (goto-char (point-min))
334 (tutorial--find-changed-keys
335 tutorial--default-keys)))))
336 (when changed-keys
337 (insert
338 "The following key bindings used in the tutorial have been changed
339 from the Emacs default:\n\n" )
340 (let ((frm " %-14s %-27s %-16s\n"))
341 (insert (format frm
342 "Standard Key" "Command" "In Your Emacs")))
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)
356 (put-text-property 0 (length key-txt)
357 'face 'tutorial-warning-face key-txt))
358 (insert " " key-txt " ")
359 (indent-to 18)
360 ;; Insert a link describing the old binding:
361 (insert-button def-fun-txt
362 'value def-fun
363 'action
364 (lambda (button) (interactive)
365 (describe-function
366 (button-get button 'value)))
367 'follow-link t)
368 (indent-to 45)
369 (when (listp where)
370 (setq where "list"))
371 ;; Tell where the old binding is now:
372 (insert (format " %-16s "
373 (if (string= "" where)
374 (format "M-x %s" def-fun-txt)
375 where)))
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
381 (lambda (b) (interactive)
382 (let ((value (button-get b 'value)))
383 (tutorial--describe-nonstandard-key value)))
384 'value (cdr remark)
385 'follow-link t)
386 (insert "\n")))))
388 (insert "
389 It is OK to change key bindings, but changed bindings do not
390 correspond to what the tutorial says.\n\n")
391 (print-help-return-message)))))
393 (defun tutorial--find-changed-keys (default-keys)
394 "Find the key bindings used in the tutorial that have changed.
395 Return a list with elements of the form
397 '(KEY DEF-FUN DEF-FUN-TXT WHERE REMARK QUIET)
399 where
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)
407 REMARK is a list with info about rebinding. It has either of
408 these formats:
410 \(TEXT cua-mode)
411 \(TEXT current-binding KEY-FUN DEF-FUN KEY WHERE)
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.
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'."
420 (let (changed-keys remark)
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))
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.
435 (key-fun (if (eq def-fun 'ESC-prefix)
436 (lookup-key global-map [27])
437 (if (eq def-fun 'Control-X-prefix)
438 (lookup-key global-map [24])
439 (key-binding key))))
440 (where (where-is-internal (if rem-fun rem-fun def-fun)))
441 cwhere)
443 (if where
444 (progn
445 (setq cwhere (car where)
446 where (key-description cwhere))
447 (when (and (< 10 (length where))
448 (string= (substring where 0 (length "<menu-bar>"))
449 "<menu-bar>"))
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"))))
461 (setq where ""))
462 (setq remark nil)
463 (unless
464 (cond ((eq key-fun def-fun)
465 ;; No rebinding, return t
467 ((and key-fun
468 (eq key-fun (command-remapping def-fun)))
469 ;; Just a remapping, return 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
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
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))))))
527 changed-keys))
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))))
536 (defun tutorial--display-changes ()
537 "Display changes to some default key bindings.
538 If some of the default key bindings that the tutorial depends on
539 have been changed then display the changes in the tutorial buffer
540 with 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))
549 changed-key
550 (start (point))
551 (case-fold-search nil)
552 (keybindings-regexp
553 (concat "[[:space:]]\\("
554 (mapconcat (lambda (kdf) (regexp-quote
555 (tutorial--key-description
556 (nth 1 kdf))))
557 tutorial--default-keys
558 "\\|")
559 "\\)[[:punct:][:space:]]")))
560 ;; Need the custom button face for viper buttons:
561 (if (boundp 'viper-mode-string) (require 'cus-edit))
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)
568 (insert head " [")
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)
574 '(tutorial-remark remark
575 face tutorial-warning-face
576 read-only t)))))
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.
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))
588 (key (nth 0 ck))
589 (def-fun (nth 1 ck))
590 (where (nth 3 ck))
591 s1 s2 help-string)
592 (unless (string= where "Same key")
593 (when (string= where "")
594 (setq where (format "M-x %s" def-fun)))
595 (setq tutorial--point-after-chkeys (point-marker)
596 s1 (get-lang-string tutorial--lang 'tut-chgdkey)
597 s2 (get-lang-string tutorial--lang 'tut-chgdkey2)
598 help-string (and s1 s2 (format s1 desc where)))
599 (add-text-properties (match-beginning 1) (match-end 1)
600 '(face tutorial-warning-face
601 tutorial-remark key-sequence))
602 (if help-string
603 (if (nth 5 ck)
604 ;; Put help string in the tooltip.
605 (put-text-property (match-beginning 1) (match-end 1)
606 'help-echo help-string)
607 ;; Put help string in the buffer.
608 (save-excursion
609 (setcar (nthcdr 5 ck) t)
610 (forward-line)
611 ;; Two or more changed keys were on the same line.
612 (while (eq (get-text-property (point) 'tutorial-remark)
613 'remark)
614 (forward-line))
615 (setq start (point))
616 (insert "** " help-string " [")
617 (insert-button s2 'tutorial-buffer (current-buffer)
618 'action 'tutorial--detailed-help
619 'explain-key-desc desc 'follow-link t
620 'face 'link)
621 (insert "] **\n")
622 (add-text-properties start (point)
623 '(tutorial-remark remark
624 rear-nonsticky t
625 face tutorial-warning-face
626 read-only t)))))))))))
628 (defun tutorial--saved-dir ()
629 "Directory to which tutorials are saved."
630 (expand-file-name "tutorial" user-emacs-directory))
632 (defun tutorial--saved-file ()
633 "File name in which to save tutorials."
634 (let ((file-name tutorial--lang)
635 (ext (file-name-extension tutorial--lang)))
636 (when (or (not ext)
637 (string= ext ""))
638 (setq file-name (concat file-name ".tut")))
639 (expand-file-name file-name (tutorial--saved-dir))))
641 (defun tutorial--remove-remarks ()
642 "Remove the remark lines that was added to the tutorial buffer."
643 (save-excursion
644 (goto-char (point-min))
645 (let (prop-start
646 prop-end
647 prop-val)
648 ;; Catch the case when we already are on a remark line
649 (while (if (get-text-property (point) 'tutorial-remark)
650 (setq prop-start (point))
651 (setq prop-start (next-single-property-change (point) 'tutorial-remark)))
652 (setq prop-end (next-single-property-change prop-start 'tutorial-remark))
653 (setq prop-val (get-text-property prop-start 'tutorial-remark))
654 (unless prop-end
655 (setq prop-end (point-max)))
656 (goto-char prop-end)
657 (unless (eq prop-val 'key-sequence)
658 (delete-region prop-start prop-end))))))
660 (defun tutorial--save-tutorial ()
661 "Save the tutorial buffer.
662 This saves the part of the tutorial before and after the area
663 showing changed keys. It also saves the point position and the
664 position where the display of changed bindings was inserted."
665 ;; This runs in a hook so protect it:
666 (condition-case err
667 (if (y-or-n-p "Save your position in the tutorial? ")
668 (tutorial--save-tutorial-to (tutorial--saved-file))
669 (message "Tutorial position not saved"))
670 (error (message "Error saving tutorial state: %s"
671 (error-message-string err)))))
673 (defun tutorial--save-tutorial-to (saved-file)
674 "Save the tutorial buffer to SAVED-FILE.
675 See `tutorial--save-tutorial' for more information."
676 ;; Anything to save?
677 (when (or (buffer-modified-p)
678 (< 1 (point)))
679 (let ((tutorial-dir (tutorial--saved-dir))
680 save-err)
681 ;; The tutorial is saved in a subdirectory in the user home
682 ;; directory. Create this subdirectory first.
683 (unless (file-directory-p tutorial-dir)
684 (condition-case err
685 (make-directory tutorial-dir nil)
686 (error (setq save-err t)
687 (warn "Could not create directory %s: %s" tutorial-dir
688 (error-message-string err)))))
689 ;; Make sure we have that directory.
690 (if (file-directory-p tutorial-dir)
691 (let ((tut-point (if (= 0 tutorial--point-after-chkeys)
692 ;; No info about changed keys is
693 ;; displayed.
694 (point)
695 (if (< (point) tutorial--point-after-chkeys)
696 (- (point))
697 (- (point) tutorial--point-after-chkeys))))
698 (old-point (point))
699 ;; Use a special undo list so that we easily can undo
700 ;; the changes we make to the tutorial buffer. This is
701 ;; currently not needed since we now delete the buffer
702 ;; after saving, but kept for possible future use of
703 ;; this function.
704 buffer-undo-list
705 (inhibit-read-only t))
706 ;; Delete the area displaying info about changed keys.
707 ;; (when (< 0 tutorial--point-after-chkeys)
708 ;; (delete-region tutorial--point-before-chkeys
709 ;; tutorial--point-after-chkeys))
710 ;; Delete the remarks:
711 (tutorial--remove-remarks)
712 ;; Put the value of point first in the buffer so it will
713 ;; be saved with the tutorial.
714 (goto-char (point-min))
715 (insert (number-to-string tut-point)
716 "\n"
717 (number-to-string (marker-position
718 tutorial--point-before-chkeys))
719 "\n")
720 (condition-case err
721 (write-region nil nil saved-file)
722 (error (setq save-err t)
723 (warn "Could not save tutorial to %s: %s"
724 saved-file
725 (error-message-string err))))
726 ;; An error is raised here?? Is this a bug?
727 (condition-case err
728 (undo-only)
729 (error nil))
730 ;; Restore point
731 (goto-char old-point)
732 (if save-err
733 (message "Could not save tutorial state.")
734 (message "Saved tutorial state.")))
735 (message "Can't save tutorial: %s is not a directory"
736 tutorial-dir)))))
739 ;;;###autoload
740 (defun help-with-tutorial (&optional arg dont-ask-for-revert)
741 "Select the Emacs learn-by-doing tutorial.
742 If there is a tutorial version written in the language
743 of the selected language environment, that version is used.
744 If there's no tutorial in that language, `TUTORIAL' is selected.
745 With ARG, you are asked to choose which language.
746 If DONT-ASK-FOR-REVERT is non-nil the buffer is reverted without
747 any question when restarting the tutorial.
749 If any of the standard Emacs key bindings that are used in the
750 tutorial have been changed then an explanatory note about this is
751 shown in the beginning of the tutorial buffer.
753 When the tutorial buffer is killed the content and the point
754 position in the buffer is saved so that the tutorial may be
755 resumed later."
756 (interactive "P")
757 (if (boundp 'viper-current-state)
758 (let ((prompt1
759 "You can not run the Emacs tutorial directly because you have \
760 enabled Viper.")
761 (prompt2 "\nThere is however a Viper tutorial you can run instead.
762 Run the Viper tutorial? "))
763 (if (fboundp 'viper-tutorial)
764 (if (y-or-n-p (concat prompt1 prompt2))
765 (progn (message "")
766 (funcall 'viper-tutorial 0))
767 (message "Tutorial aborted by user"))
768 (message prompt1)))
769 (let* ((lang (if arg
770 (let ((minibuffer-setup-hook minibuffer-setup-hook))
771 (add-hook 'minibuffer-setup-hook
772 'minibuffer-completion-help)
773 (read-language-name 'tutorial "Language: " "English"))
774 (if (get-language-info current-language-environment 'tutorial)
775 current-language-environment
776 "English")))
777 (filename (get-language-info lang 'tutorial))
778 ;; Choose a buffer name including the language so that
779 ;; several languages can be tested simultaneously:
780 (tut-buf-name (concat "TUTORIAL (" lang ")"))
781 (old-tut-buf (get-buffer tut-buf-name))
782 (old-tut-win (when old-tut-buf (get-buffer-window old-tut-buf t)))
783 (old-tut-is-ok (when old-tut-buf
784 (not (buffer-modified-p old-tut-buf))))
785 old-tut-file
786 (old-tut-point 1))
787 (setq tutorial--point-after-chkeys (point-min))
788 ;; Try to display the tutorial buffer before asking to revert it.
789 ;; If the tutorial buffer is shown in some window make sure it is
790 ;; selected and displayed:
791 (if old-tut-win
792 (raise-frame
793 (window-frame
794 (select-window (get-buffer-window old-tut-buf t))))
795 ;; Else, is there an old tutorial buffer? Then display it:
796 (when old-tut-buf
797 (switch-to-buffer old-tut-buf)))
798 ;; Use whole frame for tutorial
799 (delete-other-windows)
800 ;; If the tutorial buffer has been changed then ask if it should
801 ;; be reverted:
802 (when (and old-tut-buf
803 (not old-tut-is-ok))
804 (setq old-tut-is-ok
805 (if dont-ask-for-revert
807 (not (y-or-n-p
808 "You have changed the Tutorial buffer. Revert it? ")))))
809 ;; (Re)build the tutorial buffer if it is not ok
810 (unless old-tut-is-ok
811 (switch-to-buffer (get-buffer-create tut-buf-name))
812 (unless old-tut-buf (text-mode))
813 (unless lang (error "Variable lang is nil"))
814 (setq tutorial--lang lang)
815 (setq old-tut-file (file-exists-p (tutorial--saved-file)))
816 (let ((inhibit-read-only t))
817 (erase-buffer))
818 (message "Preparing tutorial ...") (sit-for 0)
820 ;; Do not associate the tutorial buffer with a file. Instead use
821 ;; a hook to save it when the buffer is killed.
822 (setq buffer-auto-save-file-name nil)
823 (add-hook 'kill-buffer-hook 'tutorial--save-tutorial nil t)
825 ;; Insert the tutorial. First offer to resume last tutorial
826 ;; editing session.
827 (when dont-ask-for-revert
828 (setq old-tut-file nil))
829 (when old-tut-file
830 (setq old-tut-file
831 (y-or-n-p "Resume your last saved tutorial? ")))
832 (if old-tut-file
833 (progn
834 (insert-file-contents (tutorial--saved-file))
835 (goto-char (point-min))
836 (setq old-tut-point
837 (string-to-number
838 (buffer-substring-no-properties
839 (line-beginning-position) (line-end-position))))
840 (forward-line)
841 (setq tutorial--point-before-chkeys
842 (string-to-number
843 (buffer-substring-no-properties
844 (line-beginning-position) (line-end-position))))
845 (forward-line)
846 (delete-region (point-min) (point))
847 (goto-char tutorial--point-before-chkeys)
848 (setq tutorial--point-before-chkeys (point-marker)))
849 (insert-file-contents (expand-file-name filename tutorial-directory))
850 (forward-line)
851 (setq tutorial--point-before-chkeys (point-marker)))
853 (tutorial--display-changes)
855 ;; Clear message:
856 (unless dont-ask-for-revert
857 (message "") (sit-for 0))
860 (if old-tut-file
861 ;; Just move to old point in saved tutorial.
862 (let ((old-point
863 (if (> 0 old-tut-point)
864 (- old-tut-point)
865 (+ old-tut-point tutorial--point-after-chkeys))))
866 (when (< old-point 1)
867 (setq old-point 1))
868 (goto-char old-point))
869 (goto-char (point-min))
870 (search-forward "\n<<")
871 (beginning-of-line)
872 ;; Convert the <<...>> line to the proper [...] line,
873 ;; or just delete the <<...>> line if a [...] line follows.
874 (cond ((save-excursion
875 (forward-line 1)
876 (looking-at "\\["))
877 (delete-region (point) (progn (forward-line 1) (point))))
878 ((looking-at "<<Blank lines inserted.*>>")
879 (replace-match "[Middle of page left blank for didactic purposes. Text continues below]"))
881 (looking-at "<<")
882 (replace-match "[")
883 (search-forward ">>")
884 (replace-match "]")))
885 (beginning-of-line)
886 (let ((n (- (window-height (selected-window))
887 (count-lines (point-min) (point))
888 6)))
889 (if (< n 8)
890 (progn
891 ;; For a short gap, we don't need the [...] line,
892 ;; so delete it.
893 (delete-region (point) (progn (end-of-line) (point)))
894 (newline n))
895 ;; Some people get confused by the large gap.
896 (newline (/ n 2))
898 ;; Skip the [...] line (don't delete it).
899 (forward-line 1)
900 (newline (- n (/ n 2)))))
901 (goto-char (point-min)))
902 (setq buffer-undo-list nil)
903 (set-buffer-modified-p nil)))))
906 ;; Below is some attempt to handle language specific strings. These
907 ;; are currently only used in the tutorial.
909 (defconst lang-strings
910 '(("English" .
911 ((tut-chgdkey . "%s has been rebound, but you can use %s instead")
912 (tut-chgdkey2 . "More")
913 (tut-chgdhead . "
914 NOTICE: The main purpose of the Emacs tutorial is to teach you
915 the most important standard Emacs commands (key bindings).
916 However, your Emacs has been customized by changing some of
917 these basic editing commands, so it doesn't correspond to the
918 tutorial. We have inserted colored notices where the altered
919 commands have been introduced.")
920 (tut-chgdhead2 . "More"))))
921 "Language specific strings for Emacs.
922 This is an association list with the keys equal to the strings
923 that can be returned by `read-language-name'. The elements in
924 the list are themselves association lists with keys that are
925 string ids and values that are the language specific strings.
927 See `get-lang-string' for more information.")
929 (defun get-lang-string (lang stringid &optional no-eng-fallback)
930 "Get a language specific string for Emacs.
931 In certain places Emacs can replace a string shown to the user with
932 a language specific string. This function retrieves such strings.
934 LANG is the language specification. It should be one of those
935 strings that can be returned by `read-language-name'. STRINGID
936 is a symbol that specifies the string to retrieve.
938 If no string is found for STRINGID in the chosen language then
939 the English string is returned unless NO-ENG-FALLBACK is non-nil.
941 See `lang-strings' for more information.
943 Currently this feature is only used in `help-with-tutorial'."
944 (let ((my-lang-strings (assoc lang lang-strings))
945 (found-string))
946 (when my-lang-strings
947 (let ((entry (assoc stringid (cdr my-lang-strings))))
948 (when entry
949 (setq found-string (cdr entry)))))
950 ;; Fallback to English strings
951 (unless (or found-string
952 no-eng-fallback)
953 (setq found-string (get-lang-string "English" stringid t)))
954 found-string))
956 ;;(get-lang-string "English" 'tut-chgdkey)
958 (provide 'tutorial)
960 ;; arch-tag: c8e80aef-c3bb-4ffb-8af6-22171bf0c100
961 ;;; tutorial.el ends here