1 ;;; tutorial.el --- tutorial for Emacs
3 ;; Copyright (C) 2006, 2007 Free Software Foundation, Inc.
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 2, or (at your option)
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.
27 ;; Code for running the Emacs tutorial.
31 ;; File was created 2006-09.
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."
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
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:
68 \(current-binding KEY-FUN DEF-FUN KEY 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
)
80 (with-current-buffer (help-buffer)
82 "Your Emacs customizations override the default binding for this key:"
84 (let ((inhibit-read-only t
))
86 ((eq (car value
) 'cua-mode
)
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
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
))
106 (where (nth 4 value
))
108 (maps (current-active-maps))
110 ;; Look at the currently active keymaps and try to find
111 ;; first the keymap where the current binding occurs:
113 (let* ((m (car maps
))
114 (mb (lookup-key m key t
)))
115 (setq maps
(cdr maps
))
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
125 (mapatoms (lambda (s)
127 ;; If not already found
129 ;; and if s is a keymap
131 (keymapp (symbol-value s
)))
132 ;; and not the local symbol map
134 ;; and the value of s is map
135 (eq map
(symbol-value s
))
136 ;; then save this value in mapsym
138 (insert "The default Emacs binding for the key "
139 (key-description key
)
141 (insert (format "%s" db
))
143 "However, your customizations have rebound it to the command `")
144 (insert (format "%s" cb
))
147 (insert " (For the more advanced user:"
148 " This binding is in the keymap `"
151 (if (string= where
"")
153 (insert "\n\nYou can use M-x "
156 (insert "\n\nWith your current key bindings"
158 (if (string-match "^the .*menus?$" where
)
162 " to get the function `"
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
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:
181 (equal (car x
) (car y
)))
184 ;; Try to make a comparision that is useful for presentation (this
185 ;; could be made nicer perhaps):
188 ;;(message "x=%s, y=%s;;;; cx=%s, cy=%s" x y cx cy)
190 ;; Lists? Then call this again
194 (tutorial--sort-keys cx cy
))
195 ;; Are both numbers? Then just compare them
199 ;; Is one of them a number? Let that be bigger then.
204 ;; Are both symbols? Compare the names then.
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
213 (let* ((suspend-emacs (if window-system
214 'iconify-or-deiconify-frame
218 (Control-X-prefix [?\C-x])
219 (mode-specific-command-prefix [?\C-c])
220 (save-buffers-kill-emacs [?\C-x ?\C-c])
224 (scroll-down [?\M-v])
227 ;; * BASIC CURSOR CONTROL
228 (forward-char [?\C-f])
229 (backward-char [?\C-b])
230 (forward-word [?\M-f])
231 (backward-word [?\M-b])
233 (previous-line [?\C-p])
234 (move-beginning-of-line [?\C-a])
235 (move-end-of-line [?\C-e])
236 (backward-sentence [?\M-a])
237 (forward-sentence [?\M-e])
239 (beginning-of-buffer [?\M-<])
240 (end-of-buffer [?\M->])
241 (universal-argument [?\C-u])
243 ;; * WHEN EMACS IS HUNG
244 (keyboard-quit [?\C-g])
246 ;; * DISABLED COMMANDS
247 (downcase-region [?\C-x ?\C-l])
250 (delete-other-windows [?\C-x ?1])
252 ;; Type CONTROL-h k CONTROL-f.
254 ;; * INSERTING AND DELETING
255 ;; C-u 8 * to insert ********.
256 (delete-backward-char "\d")
257 (delete-char [?\C-d])
258 (backward-kill-word [?\M-\d])
261 (kill-sentence [?\M-k])
262 (set-mark-command [?\C-@])
263 (set-mark-command [?\C- ])
264 (kill-region [?\C-w])
269 (advertised-undo [?\C-x ?u])
270 (advertised-undo [?\C-x ?u])
273 (find-file [?\C-x ?\C-f])
274 (save-buffer [?\C-x ?\C-s])
277 (list-buffers [?\C-x ?\C-b])
278 (switch-to-buffer [?\C-x ?b])
279 (save-some-buffers [?\C-x ?s])
281 ;; * EXTENDING THE COMMAND SET
282 ;; C-x Character eXtend. Followed by one character.
283 (execute-extended-command [?\M-x])
286 ;; C-x s Save some buffers
287 ;; C-x C-b List buffers
288 ;; C-x b Switch buffer
289 ;; C-x C-c Quit Emacs
290 ;; C-x 1 Delete all but one window
294 (describe-mode [?\C-h ?m])
295 (set-fill-column [?\C-x ?f])
296 (fill-paragraph [?\M-q])
299 (isearch-forward [?\C-s])
300 (isearch-backward [?\C-r])
302 ;; * MULTIPLE WINDOWS
303 (split-window-vertically [?\C-x ?2])
304 (scroll-other-window [?\C-\M-v])
305 (other-window [?\C-x ?o])
306 (find-file-other-window [?\C-x ?4 ?\C-f])
308 ;; * RECURSIVE EDITING LEVELS
309 (keyboard-escape-quit [27 27 27])
311 ;; * GETTING MORE HELP
312 ;; The most basic HELP feature is C-h c
313 (describe-key-briefly [?\C-h ?c])
314 (describe-key [?\C-h ?k])
320 ;;(iconify-or-deiconify-frame [?\C-z])
321 (,suspend-emacs [?\C-z]))))
322 (sort default-keys 'tutorial--sort-keys))
323 "Default Emacs key bindings that the tutorial depends on.")
325 (defun tutorial--detailed-help (button)
326 "Give detailed help about changed keys."
327 (with-output-to-temp-buffer (help-buffer)
328 (help-setup-xref (list #'tutorial--detailed-help button)
330 (with-current-buffer (help-buffer)
331 (let* ((tutorial-buffer (button-get button 'tutorial-buffer))
332 (explain-key-desc (button-get button 'explain-key-desc))
333 (changed-keys (with-current-buffer tutorial-buffer
335 (goto-char (point-min))
336 (tutorial--find-changed-keys
337 tutorial--default-keys)))))
340 "The following key bindings used in the tutorial have been changed
341 from the Emacs default:\n\n" )
342 (let ((frm " %-14s %-27s %-16s\n"))
344 "Standard Key" "Command" "In Your Emacs")))
345 (dolist (tk changed-keys)
346 (let* ((def-fun (nth 1 tk))
348 (def-fun-txt (nth 2 tk))
351 (rem-fun (command-remapping def-fun))
352 (key-txt (key-description key))
353 (key-fun (with-current-buffer tutorial-buffer (key-binding key)))
355 (unless (eq def-fun key-fun)
356 ;; Insert key binding description:
357 (when (string= key-txt explain-key-desc)
358 (put-text-property 0 (length key-txt)
359 'face 'tutorial-warning-face key-txt))
360 (insert " " key-txt " ")
362 ;; Insert a link describing the old binding:
363 (insert-button def-fun-txt
366 (lambda (button) (interactive)
368 (button-get button 'value)))
373 ;; Tell where the old binding is now:
374 (insert (format " %-16s "
375 (if (string= "" where)
376 (format "M-x %s" def-fun-txt)
378 ;; Insert a link with more information, for example
379 ;; current binding and keymap or information about
380 ;; cua-mode replacements:
381 (insert-button (car remark)
383 (lambda (b) (interactive)
384 (let ((value (button-get b 'value)))
385 (tutorial--describe-nonstandard-key value)))
391 It is OK to change key bindings, but changed bindings do not
392 correspond to what the tutorial says.\n\n")
393 (print-help-return-message)))))
395 (defun tutorial--find-changed-keys (default-keys)
396 "Find the key bindings used in the tutorial that have changed.
397 Return a list with elements of the form
399 '(KEY DEF-FUN DEF-FUN-TXT WHERE REMARK QUIET)
403 KEY is a key sequence whose standard binding has been changed
404 DEF-FUN is the standard binding of KEY
405 DEF-FUN-TXT is a short descriptive text for DEF-FUN
406 WHERE is a text describing the key sequences to which DEF-FUN is
407 bound now (or, if it is remapped, a key sequence
408 for the function it is remapped to)
409 REMARK is a list with info about rebinding. It has either of
413 \(TEXT current-binding KEY-FUN DEF-FUN KEY WHERE)
415 Here TEXT is a link text to show to the user. The
416 rest of the list is used to show information when
417 the user clicks the link.
419 KEY-FUN is the actual binding for KEY.
420 QUIET is t if this changed keybinding should be handled quietly.
421 This is used by `tutorial--display-changes'."
422 (let (changed-keys remark)
423 ;; Look up the bindings in a Fundamental mode buffer
424 ;; so we do not get fooled by some other major mode.
427 (dolist (kdf default-keys)
428 ;; The variables below corresponds to those with the same names
429 ;; described in the doc string.
430 (let* ((key (nth 1 kdf))
431 (def-fun (nth 0 kdf))
432 (def-fun-txt (format "%s" def-fun))
433 (rem-fun (command-remapping def-fun))
434 (key-fun (if (eq def-fun 'ESC-prefix)
435 (lookup-key global-map [27])
437 (where (where-is-internal (if rem-fun rem-fun def-fun
)))
441 (setq cwhere
(car where
)
442 where
(key-description cwhere
))
443 (when (and (< 10 (length where
))
444 (string= (substring where
0 (length "<menu-bar>"))
447 (if (and (vectorp cwhere
)
448 (setq cwhere
(elt cwhere
1))
452 (lookup-key global-map
455 (format "the `%s' menu" cwhere
)
460 (cond ((eq key-fun def-fun
)
461 ;; No rebinding, return t
464 (eq key-fun
(command-remapping def-fun
)))
465 ;; Just a remapping, return t
467 ;; cua-mode specials:
471 (eq key-fun
'cua-paste
))
474 (eq key-fun
'undo
))))
475 (setq remark
(list "cua-mode, more info" 'cua-mode
))
478 (or (and (eq def-fun
'ESC-prefix
)
481 (118 . cua-repeat-replace-region
)))
482 (setq def-fun-txt
"\"ESC prefix\""))
483 (and (eq def-fun
'mode-specific-command-prefix
)
486 (timeout . copy-region-as-kill
)))
487 (setq def-fun-txt
"\"C-c prefix\""))
488 (and (eq def-fun
'Control-X-prefix
)
490 '(keymap (timeout . kill-region
)))
491 (setq def-fun-txt
"\"C-x prefix\""))))
492 (setq remark
(list "cua-mode replacement" 'cua-mode
))
493 (setq where
"Same key")
495 ;; viper-mode specials:
496 ((and (boundp 'viper-mode-string
)
497 (boundp 'viper-current-state
)
498 (eq viper-current-state
'vi-state
)
499 (or (and (eq def-fun
'isearch-forward
)
500 (eq key-fun
'viper-isearch-forward
))
501 (and (eq def-fun
'isearch-backward
)
502 (eq key-fun
'viper-isearch-backward
))))
503 ;; These bindings works as the default bindings,
506 ((when normal-erase-is-backspace
507 (or (and (equal key
[C-delete
])
508 (equal key-fun
'kill-word
))
509 (and (equal key
[C-backspace
])
510 (equal key-fun
'backward-kill-word
))))
511 ;; This is the strange handling of C-delete and
512 ;; C-backspace, return t
515 ;; This key has indeed been rebound. Put information
516 ;; in `remark' and return nil
518 (list "more info" 'current-binding
519 key-fun def-fun key where
))
521 (add-to-list 'changed-keys
522 (list key def-fun def-fun-txt where remark nil
))))))
525 (defun tutorial--key-description (key)
526 (let ((desc (key-description key
)))
527 (cond ((string= "ESC" desc
) "<ESC>")
528 ((string= "RET" desc
) "<Return>")
529 ((string= "DEL" desc
) "<Delback>")
532 (defun tutorial--display-changes ()
533 "Display changes to some default key bindings.
534 If some of the default key bindings that the tutorial depends on
535 have been changed then display the changes in the tutorial buffer
536 with some explanatory links."
537 (let* ((changed-keys (tutorial--find-changed-keys
538 tutorial--default-keys
))
539 ;; Alist of element (DESC . CK) where DESC is the
540 ;; key-description of a changed key and CK is the
541 ;; corresponding element in `changed-keys'.
543 (mapcar (lambda (ck) (cons (tutorial--key-description (car ck
)) ck
))
547 (case-fold-search nil
)
549 (concat "[[:space:]]\\("
550 (mapconcat (lambda (kdf) (regexp-quote
551 (tutorial--key-description
553 tutorial--default-keys
555 "\\)[[:punct:][:space:]]")))
556 ;; Need the custom button face for viper buttons:
557 (if (boundp 'viper-mode-string
) (require 'cus-edit
))
559 (if (or changed-keys
(boundp 'viper-mode-string
))
560 (let ((head (get-lang-string tutorial--lang
'tut-chgdhead
))
561 (head2 (get-lang-string tutorial--lang
'tut-chgdhead2
)))
562 (when (and head head2
)
563 (goto-char tutorial--point-before-chkeys
)
565 (insert-button head2
'tutorial-buffer
(current-buffer)
566 'action
'tutorial--detailed-help
567 'follow-link t
'face
'link
)
569 (add-text-properties tutorial--point-before-chkeys
(point)
570 '(tutorial-remark remark
571 face tutorial-warning-face
574 ;; Scan the tutorial for all key sequences.
575 (goto-char (point-min))
576 (while (re-search-forward keybindings-regexp
(point-max) t
)
577 ;; Then highlight each rebound key sequence.
578 ;; This avoids issuing a warning for, e.g., C-x C-b if C-b is rebound.
579 (setq changed-key
(assoc (match-string 1) changed-keys-alist
))
581 (not (get-text-property (match-beginning 1) 'tutorial-remark
))
582 (let* ((desc (car changed-key
))
583 (ck (cdr changed-key
))
588 (unless (string= where
"Same key")
589 (when (string= where
"")
590 (setq where
(format "M-x %s" def-fun
)))
591 (setq tutorial--point-after-chkeys
(point-marker)
592 s1
(get-lang-string tutorial--lang
'tut-chgdkey
)
593 s2
(get-lang-string tutorial--lang
'tut-chgdkey2
)
594 help-string
(and s1 s2
(format s1 desc where
)))
595 (add-text-properties (match-beginning 1) (match-end 1)
596 '(face tutorial-warning-face
597 tutorial-remark key-sequence
))
600 ;; Put help string in the tooltip.
601 (put-text-property (match-beginning 1) (match-end 1)
602 'help-echo help-string
)
603 ;; Put help string in the buffer.
605 (setcar (nthcdr 5 ck
) t
)
607 ;; Two or more changed keys were on the same line.
608 (while (eq (get-text-property (point) 'tutorial-remark
)
612 (insert "** " help-string
" [")
613 (insert-button s2
'tutorial-buffer
(current-buffer)
614 'action
'tutorial--detailed-help
615 'explain-key-desc desc
'follow-link t
618 (add-text-properties start
(point)
619 '(tutorial-remark remark
621 face tutorial-warning-face
622 read-only t
)))))))))))
624 (defun tutorial--saved-dir ()
625 "Directory to which tutorials are saved."
626 (expand-file-name "tutorial" user-emacs-directory
))
628 (defun tutorial--saved-file ()
629 "File name in which to save tutorials."
630 (let ((file-name tutorial--lang
)
631 (ext (file-name-extension tutorial--lang
)))
634 (setq file-name
(concat file-name
".tut")))
635 (expand-file-name file-name
(tutorial--saved-dir))))
637 (defun tutorial--remove-remarks ()
638 "Remove the remark lines that was added to the tutorial buffer."
640 (goto-char (point-min))
644 ;; Catch the case when we already are on a remark line
645 (while (if (get-text-property (point) 'tutorial-remark
)
646 (setq prop-start
(point))
647 (setq prop-start
(next-single-property-change (point) 'tutorial-remark
)))
648 (setq prop-end
(next-single-property-change prop-start
'tutorial-remark
))
649 (setq prop-val
(get-text-property prop-start
'tutorial-remark
))
651 (setq prop-end
(point-max)))
653 (unless (eq prop-val
'key-sequence
)
654 (delete-region prop-start prop-end
))))))
656 (defun tutorial--save-tutorial ()
657 "Save the tutorial buffer.
658 This saves the part of the tutorial before and after the area
659 showing changed keys. It also saves the point position and the
660 position where the display of changed bindings was inserted."
661 ;; This runs in a hook so protect it:
663 (if (y-or-n-p "Save your position in the tutorial? ")
664 (tutorial--save-tutorial-to (tutorial--saved-file)))
665 (error (message "Error saving tutorial state: %s"
666 (error-message-string err
)))))
668 (defun tutorial--save-tutorial-to (saved-file)
669 "Save the tutorial buffer to SAVED-FILE.
670 See `tutorial--save-tutorial' for more information."
672 (when (or (buffer-modified-p)
674 (let ((tutorial-dir (tutorial--saved-dir))
676 ;; The tutorial is saved in a subdirectory in the user home
677 ;; directory. Create this subdirectory first.
678 (unless (file-directory-p tutorial-dir
)
680 (make-directory tutorial-dir nil
)
681 (error (setq save-err t
)
682 (warn "Could not create directory %s: %s" tutorial-dir
683 (error-message-string err
)))))
684 ;; Make sure we have that directory.
685 (if (file-directory-p tutorial-dir
)
686 (let ((tut-point (if (= 0 tutorial--point-after-chkeys
)
687 ;; No info about changed keys is
690 (if (< (point) tutorial--point-after-chkeys
)
692 (- (point) tutorial--point-after-chkeys
))))
694 ;; Use a special undo list so that we easily can undo
695 ;; the changes we make to the tutorial buffer. This is
696 ;; currently not needed since we now delete the buffer
697 ;; after saving, but kept for possible future use of
700 (inhibit-read-only t
))
701 ;; Delete the area displaying info about changed keys.
702 ;; (when (< 0 tutorial--point-after-chkeys)
703 ;; (delete-region tutorial--point-before-chkeys
704 ;; tutorial--point-after-chkeys))
705 ;; Delete the remarks:
706 (tutorial--remove-remarks)
707 ;; Put the value of point first in the buffer so it will
708 ;; be saved with the tutorial.
709 (goto-char (point-min))
710 (insert (number-to-string tut-point
)
712 (number-to-string (marker-position
713 tutorial--point-before-chkeys
))
716 (write-region nil nil saved-file
)
717 (error (setq save-err t
)
718 (warn "Could not save tutorial to %s: %s"
720 (error-message-string err
))))
721 ;; An error is raised here?? Is this a bug?
726 (goto-char old-point
)
728 (message "Could not save tutorial state.")
729 (message "Saved tutorial state.")))
730 (message "Can't save tutorial: %s is not a directory"
735 (defun help-with-tutorial (&optional arg dont-ask-for-revert
)
736 "Select the Emacs learn-by-doing tutorial.
737 If there is a tutorial version written in the language
738 of the selected language environment, that version is used.
739 If there's no tutorial in that language, `TUTORIAL' is selected.
740 With ARG, you are asked to choose which language.
741 If DONT-ASK-FOR-REVERT is non-nil the buffer is reverted without
742 any question when restarting the tutorial.
744 If any of the standard Emacs key bindings that are used in the
745 tutorial have been changed then an explanatory note about this is
746 shown in the beginning of the tutorial buffer.
748 When the tutorial buffer is killed the content and the point
749 position in the buffer is saved so that the tutorial may be
752 (if (boundp 'viper-current-state
)
754 "You can not run the Emacs tutorial directly because you have \
756 (prompt2 "\nThere is however a Viper tutorial you can run instead.
757 Run the Viper tutorial? "))
758 (if (fboundp 'viper-tutorial
)
759 (if (y-or-n-p (concat prompt1 prompt2
))
761 (funcall 'viper-tutorial
0))
762 (message "Tutorial aborted by user"))
765 (let ((minibuffer-setup-hook minibuffer-setup-hook
))
766 (add-hook 'minibuffer-setup-hook
767 'minibuffer-completion-help
)
768 (read-language-name 'tutorial
"Language: " "English"))
769 (if (get-language-info current-language-environment
'tutorial
)
770 current-language-environment
772 (filename (get-language-info lang
'tutorial
))
773 ;; Choose a buffer name including the language so that
774 ;; several languages can be tested simultaneously:
775 (tut-buf-name (concat "TUTORIAL (" lang
")"))
776 (old-tut-buf (get-buffer tut-buf-name
))
777 (old-tut-win (when old-tut-buf
(get-buffer-window old-tut-buf t
)))
778 (old-tut-is-ok (when old-tut-buf
779 (not (buffer-modified-p old-tut-buf
))))
782 (setq tutorial--point-after-chkeys
(point-min))
783 ;; Try to display the tutorial buffer before asking to revert it.
784 ;; If the tutorial buffer is shown in some window make sure it is
785 ;; selected and displayed:
789 (select-window (get-buffer-window old-tut-buf t
))))
790 ;; Else, is there an old tutorial buffer? Then display it:
792 (switch-to-buffer old-tut-buf
)))
793 ;; Use whole frame for tutorial
794 (delete-other-windows)
795 ;; If the tutorial buffer has been changed then ask if it should
797 (when (and old-tut-buf
800 (if dont-ask-for-revert
803 "You have changed the Tutorial buffer. Revert it? ")))))
804 ;; (Re)build the tutorial buffer if it is not ok
805 (unless old-tut-is-ok
806 (switch-to-buffer (get-buffer-create tut-buf-name
))
807 (unless old-tut-buf
(text-mode))
808 (unless lang
(error "Variable lang is nil"))
809 (setq tutorial--lang lang
)
810 (setq old-tut-file
(file-exists-p (tutorial--saved-file)))
811 (let ((inhibit-read-only t
))
813 (message "Preparing tutorial ...") (sit-for 0)
815 ;; Do not associate the tutorial buffer with a file. Instead use
816 ;; a hook to save it when the buffer is killed.
817 (setq buffer-auto-save-file-name nil
)
818 (add-hook 'kill-buffer-hook
'tutorial--save-tutorial nil t
)
820 ;; Insert the tutorial. First offer to resume last tutorial
822 (when dont-ask-for-revert
823 (setq old-tut-file nil
))
826 (y-or-n-p "Resume your last saved tutorial? ")))
829 (insert-file-contents (tutorial--saved-file))
830 (goto-char (point-min))
833 (buffer-substring-no-properties
834 (line-beginning-position) (line-end-position))))
836 (setq tutorial--point-before-chkeys
838 (buffer-substring-no-properties
839 (line-beginning-position) (line-end-position))))
841 (delete-region (point-min) (point))
842 (goto-char tutorial--point-before-chkeys
)
843 (setq tutorial--point-before-chkeys
(point-marker)))
844 (insert-file-contents (expand-file-name filename data-directory
))
846 (setq tutorial--point-before-chkeys
(point-marker)))
848 (tutorial--display-changes)
851 (unless dont-ask-for-revert
852 (message "") (sit-for 0))
856 ;; Just move to old point in saved tutorial.
858 (if (> 0 old-tut-point
)
860 (+ old-tut-point tutorial--point-after-chkeys
))))
861 (when (< old-point
1)
863 (goto-char old-point
))
864 (goto-char (point-min))
865 (search-forward "\n<<")
867 ;; Convert the <<...>> line to the proper [...] line,
868 ;; or just delete the <<...>> line if a [...] line follows.
869 (cond ((save-excursion
872 (delete-region (point) (progn (forward-line 1) (point))))
873 ((looking-at "<<Blank lines inserted.*>>")
874 (replace-match "[Middle of page left blank for didactic purposes. Text continues below]"))
878 (search-forward ">>")
879 (replace-match "]")))
881 (let ((n (- (window-height (selected-window))
882 (count-lines (point-min) (point))
886 ;; For a short gap, we don't need the [...] line,
888 (delete-region (point) (progn (end-of-line) (point)))
890 ;; Some people get confused by the large gap.
893 ;; Skip the [...] line (don't delete it).
895 (newline (- n
(/ n
2)))))
896 (goto-char (point-min)))
897 (setq buffer-undo-list nil
)
898 (set-buffer-modified-p nil
)))))
901 ;; Below is some attempt to handle language specific strings. These
902 ;; are currently only used in the tutorial.
904 (defconst lang-strings
906 ((tut-chgdkey .
"%s has been rebound, but you can use %s instead")
907 (tut-chgdkey2 .
"More")
909 NOTICE: The main purpose of the Emacs tutorial is to teach you
910 the most important standard Emacs commands (key bindings).
911 However, your Emacs has been customized by changing some of
912 these basic editing commands, so it doesn't correspond to the
913 tutorial. We have inserted colored notices where the altered
914 commands have been introduced.")
915 (tut-chgdhead2 .
"More"))))
916 "Language specific strings for Emacs.
917 This is an association list with the keys equal to the strings
918 that can be returned by `read-language-name'. The elements in
919 the list are themselves association lists with keys that are
920 string ids and values that are the language specific strings.
922 See `get-lang-string' for more information.")
924 (defun get-lang-string (lang stringid
&optional no-eng-fallback
)
925 "Get a language specific string for Emacs.
926 In certain places Emacs can replace a string shown to the user with
927 a language specific string. This function retrieves such strings.
929 LANG is the language specification. It should be one of those
930 strings that can be returned by `read-language-name'. STRINGID
931 is a symbol that specifies the string to retrieve.
933 If no string is found for STRINGID in the chosen language then
934 the English string is returned unless NO-ENG-FALLBACK is non-nil.
936 See `lang-strings' for more information.
938 Currently this feature is only used in `help-with-tutorial'."
939 (let ((my-lang-strings (assoc lang lang-strings
))
941 (when my-lang-strings
942 (let ((entry (assoc stringid
(cdr my-lang-strings
))))
944 (setq found-string
(cdr entry
)))))
945 ;; Fallback to English strings
946 (unless (or found-string
948 (setq found-string
(get-lang-string "English" stringid t
)))
951 ;;(get-lang-string "English" 'tut-chgdkey)
955 ;; arch-tag: c8e80aef-c3bb-4ffb-8af6-22171bf0c100
956 ;;; tutorial.el ends here