1 ;;; tutorial.el --- tutorial for Emacs
3 ;; Copyright (C) 2006 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
36 (eval-when-compile (require 'cl
))
38 (defvar tutorial--point-before-chkeys
0
39 "Point before display of key changes.")
40 (make-variable-buffer-local 'tutorial--point-before-chkeys
)
42 (defvar tutorial--point-after-chkeys
0
43 "Point after display of key changes.")
44 (make-variable-buffer-local 'tutorial--point-after-chkeys
)
46 (defvar tutorial--lang nil
48 (make-variable-buffer-local 'tutorial--lang
)
50 (defun tutorial--describe-nonstandard-key (value)
51 "Give more information about a changed key binding.
52 This is used in `help-with-tutorial'. The information includes
53 the key sequence that no longer has a default binding, the
54 default binding and the current binding. It also tells in what
55 keymap the new binding has been done and how to access the
56 function in the default binding from the keyboard.
58 For `cua-mode' key bindings that try to combine CUA key bindings
59 with default Emacs bindings information about this is shown.
61 VALUE should have either of these formats:
64 \(current-binding KEY-FUN DEF-FUN KEY WHERE)
67 KEY is a key sequence whose standard binding has been changed
68 KEY-FUN is the actual binding for KEY
69 DEF-FUN is the standard binding of KEY
70 WHERE is a text describing the key sequences to which DEF-FUN is
71 bound now (or, if it is remapped, a key sequence
72 for the function it is remapped to)"
73 (with-output-to-temp-buffer (help-buffer)
74 (help-setup-xref (list #'tutorial--describe-nonstandard-key value
)
76 (with-current-buffer (help-buffer)
78 "Your Emacs customizations override the default binding for this key:"
80 (let ((inhibit-read-only t
))
82 ((eq (car value
) 'cua-mode
)
86 When CUA mode is enabled, you can use C-z, C-x, C-c, and C-v to
87 undo, cut, copy, and paste in addition to the normal Emacs
88 bindings. The C-x and C-c keys only do cut and copy when the
89 region is active, so in most cases, they do not conflict with the
90 normal function of these prefix keys.
92 If you really need to perform a command which starts with one of
93 the prefix keys even when the region is active, you have three
95 - press the prefix key twice very quickly (within 0.2 seconds),
96 - press the prefix key and the following key within 0.2 seconds, or
97 - use the SHIFT key with the prefix key, i.e. C-S-x or C-S-c."))
98 ((eq (car value
) 'current-binding
)
99 (let ((cb (nth 1 value
))
102 (where (nth 4 value
))
104 (maps (current-active-maps))
106 ;; Look at the currently active keymaps and try to find
107 ;; first the keymap where the current binding occurs:
109 (let* ((m (car maps
))
110 (mb (lookup-key m key t
)))
111 (setq maps
(cdr maps
))
115 ;; Now, if a keymap was found we must found the symbol
116 ;; name for it to display to the user. This can not
117 ;; always be found since all keymaps does not have a
118 ;; symbol pointing to them, but here they should have
121 (mapatoms (lambda (s)
123 ;; If not already found
125 ;; and if s is a keymap
127 (keymapp (symbol-value s
)))
128 ;; and not the local symbol map
130 ;; and the value of s is map
131 (eq map
(symbol-value s
))
132 ;; then save this value in mapsym
134 (insert "The default Emacs binding for the key "
135 (key-description key
)
137 (insert (format "%s" db
))
139 "However, your customizations have rebound it to the command `")
140 (insert (format "%s" cb
))
143 (insert " (For the more advanced user:"
144 " This binding is in the keymap `"
147 (if (string= where
"")
149 (insert "\n\nYou can use M-x "
152 (insert "\n\nWith you current key bindings"
153 " you can use the key "
155 " to get the function `"
159 (fill-region (point-min) (point)))))
160 (print-help-return-message))))
162 (defun tutorial--sort-keys (left right
)
163 "Sort predicate for use with `tutorial--default-keys'.
164 This is a predicate function to `sort'.
166 The sorting is for presentation purpose only and is done on the
169 LEFT and RIGHT are the elements to compare."
170 (let ((x (append (cadr left
) nil
))
171 (y (append (cadr right
) nil
)))
172 ;; Skip the front part of the key sequences if they are equal:
175 (equal (car x
) (car y
)))
178 ;; Try to make a comparision that is useful for presentation (this
179 ;; could be made nicer perhaps):
182 ;;(message "x=%s, y=%s;;;; cx=%s, cy=%s" x y cx cy)
184 ;; Lists? Then call this again
188 (tutorial--sort-keys cx cy
))
189 ;; Are both numbers? Then just compare them
193 ;; Is one of them a number? Let that be bigger then.
198 ;; Are both symbols? Compare the names then.
201 (string< (symbol-name cy
)
205 (defconst tutorial--default-keys
207 ;; On window system suspend Emacs is replaced in the
208 ;; default keymap so honor this here.
209 (suspend-emacs (if window-system
210 'iconify-or-deiconify-frame
214 ;; These are not mentioned but are basic:
216 (Control-X-prefix [?\C-x])
217 (mode-specific-command-prefix [?\C-c])
219 (save-buffers-kill-emacs [?\C-x ?\C-c])
224 (scroll-down [?\M-v])
228 ;; * BASIC CURSOR CONTROL
229 (forward-char [?\C-f])
230 (backward-char [?\C-b])
232 (forward-word [?\M-f])
233 (backward-word [?\M-b])
236 (previous-line [?\C-p])
238 (move-beginning-of-line [?\C-a])
239 (move-end-of-line [?\C-e])
241 (backward-sentence [?\M-a])
242 (forward-sentence [?\M-e])
245 (beginning-of-buffer [?\M-<])
246 (end-of-buffer [?\M->])
248 (universal-argument [?\C-u])
251 ;; * WHEN EMACS IS HUNG
252 (keyboard-quit [?\C-g])
255 ;; * DISABLED COMMANDS
256 (downcase-region [?\C-x ?\C-l])
260 (delete-other-windows [?\C-x ?1])
262 ;; Type CONTROL-h k CONTROL-f.
265 ;; * INSERTING AND DELETING
266 ;; C-u 8 * to insert ********.
268 (delete-backward-char [backspace])
269 (delete-char [?\C-d])
271 (backward-kill-word [(meta backspace)])
275 (kill-sentence [?\M-k])
277 (set-mark-command [?\C-@])
278 (set-mark-command [?\C- ])
279 (kill-region [?\C-w])
285 (advertised-undo [?\C-x ?u])
286 (advertised-undo [?\C-x ?u])
290 (find-file [?\C-x ?\C-f])
291 (save-buffer [?\C-x ?\C-s])
295 (list-buffers [?\C-x ?\C-b])
296 (switch-to-buffer [?\C-x ?b])
297 (save-some-buffers [?\C-x ?s])
300 ;; * EXTENDING THE COMMAND SET
301 ;; C-x Character eXtend. Followed by one character.
302 (execute-extended-command [?\M-x])
306 ;; C-x s Save some buffers
307 ;; C-x C-b List buffers
308 ;; C-x b Switch buffer
309 ;; C-x C-c Quit Emacs
310 ;; C-x 1 Delete all but one window
315 (describe-mode [?\C-h ?m])
317 (set-fill-column [?\C-x ?f])
318 (fill-paragraph [?\M-q])
322 (isearch-forward [?\C-s])
323 (isearch-backward [?\C-r])
326 ;; * MULTIPLE WINDOWS
327 (split-window-vertically [?\C-x ?2])
328 (scroll-other-window [?\C-\M-v])
329 (other-window [?\C-x ?o])
330 (find-file-other-window [?\C-x ?4 ?\C-f])
333 ;; * RECURSIVE EDITING LEVELS
334 (keyboard-escape-quit [27 27 27])
337 ;; * GETTING MORE HELP
338 ;; The most basic HELP feature is C-h c
339 (describe-key-briefly [?\C-h ?c])
340 (describe-key [?\C-h ?k])
348 ;;(iconify-or-deiconify-frame [?\C-z])
349 (,suspend-emacs [?\C-z])
351 (sort default-keys 'tutorial--sort-keys))
352 "Default Emacs key bindings that the tutorial depends on.")
354 (defun tutorial--detailed-help (button)
355 "Give detailed help about changed keys."
356 (with-output-to-temp-buffer (help-buffer)
357 (help-setup-xref (list #'tutorial--detailed-help button)
359 (with-current-buffer (help-buffer)
360 (let* ((tutorial-buffer (button-get button 'tutorial-buffer))
361 ;;(tutorial-arg (button-get button 'tutorial-arg))
362 (explain-key-desc (button-get button 'explain-key-desc))
363 (changed-keys (with-current-buffer tutorial-buffer
364 (tutorial--find-changed-keys tutorial--default-keys))))
367 "The following key bindings used in the tutorial had been changed
368 from Emacs default in the " (buffer-name tutorial-buffer) " buffer:\n\n" )
369 (let ((frm " %-9s %-27s %-11s %s\n"))
370 (insert (format frm "Key" "Standard Binding" "Is Now On" "Remark")))
371 (dolist (tk changed-keys)
372 (let* ((def-fun (nth 1 tk))
374 (def-fun-txt (nth 2 tk))
377 (rem-fun (command-remapping def-fun))
378 (key-txt (key-description key))
379 (key-fun (with-current-buffer tutorial-buffer (key-binding key)))
381 (unless (eq def-fun key-fun)
382 ;; Insert key binding description:
383 (when (string= key-txt explain-key-desc)
384 (put-text-property 0 (length key-txt) 'face '(:background "yellow") key-txt))
385 (insert " " key-txt " ")
386 (setq tot-len (length key-txt))
388 (insert (make-string (- 9 tot-len) ? ))
390 ;; Insert a link describing the old binding:
391 (insert-button def-fun-txt
394 (lambda(button) (interactive)
396 (button-get button 'value)))
398 (setq tot-len (+ tot-len (length def-fun-txt)))
400 (insert (make-string (- 36 tot-len) ? )))
403 ;; Tell where the old binding is now:
404 (insert (format " %-11s " where))
405 ;; Insert a link with more information, for example
406 ;; current binding and keymap or information about
407 ;; cua-mode replacements:
408 (insert-button (car remark)
410 (lambda(b) (interactive)
411 (let ((value (button-get b 'value)))
412 (tutorial--describe-nonstandard-key value)))
418 It is legitimate to change key bindings, but changed bindings do not
419 correspond to what the tutorial says. (See also " )
420 (insert-button "Key Binding Conventions"
422 (lambda(button) (interactive)
424 "(elisp) Key Binding Conventions")
425 (message "Type C-x 0 to close the new window"))
428 (print-help-return-message)))))
430 (defun tutorial--find-changed-keys (default-keys)
431 "Find the key bindings that have changed.
432 Check if the default Emacs key bindings that the tutorial depends
433 on have been changed.
435 Return a list with the keys that have been changed. The element
436 of this list have the following format:
438 \(list KEY DEF-FUN DEF-FUN-TXT WHERE REMARK)
441 KEY is a key sequence whose standard binding has been changed
442 DEF-FUN is the standard binding of KEY
443 DEF-FUN-TXT is a short descriptive text for DEF-FUN
444 WHERE is a text describing the key sequences to which DEF-FUN is
445 bound now (or, if it is remapped, a key sequence
446 for the function it is remapped to)
447 REMARK is a list with info about rebinding. It has either of these
451 \(TEXT current-binding KEY-FUN DEF-FUN KEY WHERE)
453 Here TEXT is a link text to show to the user. The
454 rest of the list is used to show information when
455 the user clicks the link.
457 KEY-FUN is the actual binding for KEY."
458 (let (changed-keys remark)
459 ;; (default-keys tutorial--default-keys))
460 (dolist (kdf default-keys)
461 ;; The variables below corresponds to those with the same names
462 ;; described in the doc string.
463 (let* ((key (nth 1 kdf))
464 (def-fun (nth 0 kdf))
465 (def-fun-txt (format "%s" def-fun))
466 (rem-fun (command-remapping def-fun))
467 (key-fun (key-binding key))
468 (where (where-is-internal (if rem-fun rem-fun def-fun))))
469 (when (eq key-fun 'ESC-prefix)
470 (message "ESC-prefix!!!!"))
473 (setq where (key-description (car where)))
474 (when (and (< 10 (length where))
475 (string= (substring where 0 (length "<menu-bar>"))
477 (setq where "The menus")))
481 (cond ((eq key-fun def-fun)
482 ;; No rebinding, return t
484 ((eq key-fun (command-remapping def-fun))
485 ;; Just a remapping, return t
487 ;; cua-mode specials:
491 (eq key-fun 'cua-paste))
494 (eq key-fun 'undo))))
495 (setq remark (list "cua-mode, more info" 'cua-mode))
499 (and (eq def-fun 'ESC-prefix)
502 (118 . cua-repeat-replace-region))))
503 (and (eq def-fun 'mode-specific-command-prefix)
506 (timeout . copy-region-as-kill))))
507 (and (eq def-fun 'Control-X-prefix)
509 '(keymap (timeout . kill-region))))))
510 (setq remark (list "cua-mode replacement" 'cua-mode))
512 ((eq def-fun 'mode-specific-command-prefix)
513 (setq def-fun-txt "\"C-c prefix\""))
514 ((eq def-fun 'Control-X-prefix)
515 (setq def-fun-txt "\"C-x prefix\""))
516 ((eq def-fun 'ESC-prefix)
517 (setq def-fun-txt "\"ESC prefix\"")))
518 (setq where "Same key")
520 ;; viper-mode specials:
521 ((and (boundp 'viper-mode-string)
522 (boundp 'viper-current-state)
523 (eq viper-current-state 'vi-state)
524 (or (and (eq def-fun 'isearch-forward)
525 (eq key-fun 'viper-isearch-forward))
526 (and (eq def-fun 'isearch-backward)
527 (eq key-fun 'viper-isearch-backward))))
528 ;; These bindings works as the default bindings,
531 ((when normal-erase-is-backspace
532 (or (and (equal key [C-delete])
533 (equal key-fun 'kill-word))
534 (and (equal key [C-backspace])
535 (equal key-fun 'backward-kill-word))))
536 ;; This is the strange handling of C-delete and
537 ;; C-backspace, return t
540 ;; This key has indeed been rebound. Put information
541 ;; in `remark' and return nil
543 (list "more info" 'current-binding
544 key-fun def-fun key where))
546 (add-to-list 'changed-keys
547 (list key def-fun def-fun-txt where remark)))))
550 (defvar tutorial--tab-map
551 (let ((map (make-sparse-keymap)))
552 (define-key map [tab] 'forward-button)
553 (define-key map [(shift tab)] 'backward-button)
554 (define-key map [(meta tab)] 'backward-button)
556 "Keymap that allows tabbing between buttons.")
558 (defun tutorial--display-changes (changed-keys)
559 "Display changes to some default key bindings.
560 If some of the default key bindings that the tutorial depends on
561 have been changed then display the changes in the tutorial buffer
562 with some explanatory links.
564 CHANGED-KEYS should be a list in the format returned by
565 `tutorial--find-changed-keys'."
566 (when (or changed-keys
567 (boundp 'viper-mode-string))
568 ;; Need the custom button face for viper buttons:
569 (when (boundp 'viper-mode-string)
571 (let ((start (point))
573 (head (get-lang-string tutorial--lang 'tut-chgdhead))
574 (head2 (get-lang-string tutorial--lang 'tut-chgdhead2)))
575 (when (and head head2)
576 (goto-char tutorial--point-before-chkeys)
583 'tutorial--detailed-help
585 'face '(:inherit link :background "yellow"))
588 (dolist (tk changed-keys)
589 (let* ((def-fun (nth 1 tk))
591 (def-fun-txt (nth 2 tk))
594 (rem-fun (command-remapping def-fun))
595 (key-txt (key-description key))
596 (key-fun (key-binding key))
598 (unless (eq def-fun key-fun)
599 ;; Mark the key in the tutorial text
600 (unless (string= "Same key" where)
602 (key-desc (key-description key)))
603 (while (search-forward key-desc nil t)
604 (put-text-property (match-beginning 0)
606 'tutorial-remark 'only-colored)
607 (put-text-property (match-beginning 0)
609 'face '(:background "yellow"))
611 (let ((s (get-lang-string tutorial--lang 'tut-chgdkey))
612 (s2 (get-lang-string tutorial--lang 'tut-chgdkey2))
615 ;;(concat "** The key " key-desc " has been rebound, but you can use " where " instead ["))
617 (setq s (format s key-desc where s2))
624 'tutorial--detailed-help
625 'explain-key-desc key-desc
627 'face '(:inherit link :background "yellow"))
631 (put-text-property start end 'local-map tutorial--tab-map)
632 ;; Add a property so we can remove the remark:
633 (put-text-property start end 'tutorial-remark t)
634 (put-text-property start end
635 'face '(:background "yellow" :foreground "#c00"))
636 (put-text-property start end 'read-only t))))
637 (goto-char here)))))))
641 ;; Make the area with information about change key
642 ;; bindings stand out:
643 (put-text-property start end 'tutorial-remark t)
644 (put-text-property start end
646 ;; The default warning face does not
647 ;;look good in this situation. Instead
648 ;;try something that could be
649 ;;recognized from warnings in normal
651 ;; 'font-lock-warning-face
652 (list :background "yellow" :foreground "#c00"))
653 ;; Make it possible to use Tab/S-Tab between fields in
655 (put-text-property start end 'local-map tutorial--tab-map)
656 (setq tutorial--point-after-chkeys (point-marker))
657 ;; Make this area read-only:
658 (put-text-property start end 'read-only t)))))
660 (defun tutorial--saved-dir ()
661 "Directory where to save tutorials."
662 (expand-file-name ".emacstut" "~/"))
664 (defun tutorial--saved-file ()
665 "File name in which to save tutorials."
666 (let ((file-name tutorial--lang)
667 (ext (file-name-extension tutorial--lang)))
670 (setq file-name (concat file-name ".tut")))
671 (expand-file-name file-name (tutorial--saved-dir))))
673 (defun tutorial--remove-remarks()
674 "Remove the remark lines that was added to the tutorial buffer."
676 (goto-char (point-min))
680 ;; Catch the case when we already are on a remark line
681 (while (if (get-text-property (point) 'tutorial-remark)
682 (setq prop-start (point))
683 (setq prop-start (next-single-property-change (point) 'tutorial-remark)))
684 (setq prop-end (next-single-property-change prop-start 'tutorial-remark))
685 (setq prop-val (get-text-property prop-start 'tutorial-remark))
687 (setq prop-end (point-max)))
689 (if (eq prop-val 'only-colored)
690 (put-text-property prop-start prop-end 'face '(:background nil))
691 (let ((orig-text (get-text-property prop-start 'tutorial-orig)))
692 (delete-region prop-start prop-end)
693 (when orig-text (insert orig-text))))))))
695 (defun tutorial--save-tutorial ()
696 "Save the tutorial buffer.
697 This saves the part of the tutorial before and after the area
698 showing changed keys. It also saves the point position and the
699 position where the display of changed bindings was inserted."
700 ;; This runs in a hook so protect it:
702 (tutorial--save-tutorial-to (tutorial--saved-file))
703 (error (message "Error saving tutorial state: %s" (error-message-string err))
706 (defun tutorial--save-tutorial-to (saved-file)
707 "Save the tutorial buffer to SAVED-FILE.
708 See `tutorial--save-tutorial' for more information."
710 (when (or (buffer-modified-p)
712 (let ((tutorial-dir (tutorial--saved-dir))
714 ;; The tutorial is saved in a subdirectory in the user home
715 ;; directory. Create this subdirectory first.
716 (unless (file-directory-p tutorial-dir)
718 (make-directory tutorial-dir nil)
719 (error (setq save-err t)
720 (warn "Could not create directory %s: %s" tutorial-dir
721 (error-message-string err)))))
722 ;; Make sure we have that directory.
723 (if (file-directory-p tutorial-dir)
724 (let ((tut-point (if (= 0 tutorial--point-after-chkeys)
725 ;; No info about changed keys is
728 (if (< (point) tutorial--point-after-chkeys)
730 (- (point) tutorial--point-after-chkeys))))
732 ;; Use a special undo list so that we easily can undo
733 ;; the changes we make to the tutorial buffer. This is
734 ;; currently not needed since we now delete the buffer
735 ;; after saving, but kept for possible future use of
738 (inhibit-read-only t))
739 ;; Delete the area displaying info about changed keys.
740 ;; (when (< 0 tutorial--point-after-chkeys)
741 ;; (delete-region tutorial--point-before-chkeys
742 ;; tutorial--point-after-chkeys))
743 ;; Delete the remarks:
744 (tutorial--remove-remarks)
745 ;; Put the value of point first in the buffer so it will
746 ;; be saved with the tutorial.
747 (goto-char (point-min))
748 (insert (number-to-string tut-point)
750 (number-to-string (marker-position
751 tutorial--point-before-chkeys))
754 (write-region nil nil saved-file)
755 (error (setq save-err t)
756 (warn "Could not save tutorial to %s: %s"
758 (error-message-string err))))
759 ;; An error is raised here?? Is this a bug?
764 (goto-char old-point)
766 (message "Could not save tutorial state.")
767 (message "Saved tutorial state.")))
768 (message "Can't save tutorial: %s is not a directory"
773 (defun help-with-tutorial (&optional arg dont-ask-for-revert)
774 "Select the Emacs learn-by-doing tutorial.
775 If there is a tutorial version written in the language
776 of the selected language environment, that version is used.
777 If there's no tutorial in that language, `TUTORIAL' is selected.
778 With ARG, you are asked to choose which language.
779 If DONT-ASK-FOR-REVERT is non-nil the buffer is reverted without
780 any question when restarting the tutorial.
782 If any of the standard Emacs key bindings that are used in the
783 tutorial have been changed then an explanatory note about this is
784 shown in the beginning of the tutorial buffer.
786 When the tutorial buffer is killed the content and the point
787 position in the buffer is saved so that the tutorial may be
790 (if (boundp 'viper-current-state)
792 "You can not run the Emacs tutorial directly because you have \
794 (prompt2 "\nThere is however a Viper tutorial you can run instead.
795 Run the Viper tutorial? "))
796 (if (fboundp 'viper-tutorial)
797 (if (y-or-n-p (concat prompt1 prompt2))
799 (funcall 'viper-tutorial 0))
800 (message "Tutorial aborted by user"))
803 (let ((minibuffer-setup-hook minibuffer-setup-hook))
804 (add-hook 'minibuffer-setup-hook
805 'minibuffer-completion-help)
806 (read-language-name 'tutorial "Language: " "English"))
807 (if (get-language-info current-language-environment 'tutorial)
808 current-language-environment
810 (filename (get-language-info lang 'tutorial))
811 ;; Choose a buffer name including the language so that
812 ;; several languages can be tested simultaneously:
813 (tut-buf-name (concat "TUTORIAL (" lang ")"))
814 (old-tut-buf (get-buffer tut-buf-name))
815 (old-tut-win (when old-tut-buf (get-buffer-window old-tut-buf t)))
816 (old-tut-is-ok (when old-tut-buf
817 (not (buffer-modified-p old-tut-buf))))
820 (setq tutorial--point-after-chkeys (point-min))
821 ;; Try to display the tutorial buffer before asking to revert it.
822 ;; If the tutorial buffer is shown in some window make sure it is
823 ;; selected and displayed:
827 (select-window (get-buffer-window old-tut-buf t))))
828 ;; Else, is there an old tutorial buffer? Then display it:
830 (switch-to-buffer old-tut-buf)))
831 ;; Use whole frame for tutorial
832 (delete-other-windows)
833 ;; If the tutorial buffer has been changed then ask if it should
835 (when (and old-tut-buf
838 (if dont-ask-for-revert
841 "You have changed the Tutorial buffer. Revert it? ")))))
842 ;; (Re)build the tutorial buffer if it is not ok
843 (unless old-tut-is-ok
844 (switch-to-buffer (get-buffer-create tut-buf-name))
845 (unless old-tut-buf (text-mode))
846 (unless lang (error "Variable lang is nil"))
847 (setq tutorial--lang lang)
848 (setq old-tut-file (file-exists-p (tutorial--saved-file)))
849 (let ((inhibit-read-only t))
851 (message "Preparing tutorial ...") (sit-for 0)
853 ;; Do not associate the tutorial buffer with a file. Instead use
854 ;; a hook to save it when the buffer is killed.
855 (setq buffer-auto-save-file-name nil)
856 (add-hook 'kill-buffer-hook 'tutorial--save-tutorial nil t)
858 ;; Insert the tutorial. First offer to resume last tutorial
860 (when dont-ask-for-revert
861 (setq old-tut-file nil))
864 (y-or-n-p "Resume your last saved tutorial? ")))
867 (insert-file-contents (tutorial--saved-file))
868 (goto-char (point-min))
871 (buffer-substring-no-properties
872 (line-beginning-position) (line-end-position))))
874 (setq tutorial--point-before-chkeys
876 (buffer-substring-no-properties
877 (line-beginning-position) (line-end-position))))
879 (delete-region (point-min) (point))
880 (goto-char tutorial--point-before-chkeys)
881 (setq tutorial--point-before-chkeys (point-marker)))
882 (insert-file-contents (expand-file-name filename data-directory))
884 (setq tutorial--point-before-chkeys (point-marker)))
887 ;; Check if there are key bindings that may disturb the
888 ;; tutorial. If so tell the user.
889 (let ((changed-keys (tutorial--find-changed-keys tutorial--default-keys)))
891 (tutorial--display-changes changed-keys)))
895 (unless dont-ask-for-revert
896 (message "") (sit-for 0))
900 ;; Just move to old point in saved tutorial.
902 (if (> 0 old-tut-point)
904 (+ old-tut-point tutorial--point-after-chkeys))))
905 (when (< old-point 1)
907 (goto-char old-point))
908 (goto-char (point-min))
909 (search-forward "\n<<")
911 ;; Convert the <<...>> line to the proper [...] line,
912 ;; or just delete the <<...>> line if a [...] line follows.
913 (cond ((save-excursion
916 (delete-region (point) (progn (forward-line 1) (point))))
917 ((looking-at "<<Blank lines inserted.*>>")
918 (replace-match "[Middle of page left blank for didactic purposes. Text continues below]"))
922 (search-forward ">>")
923 (replace-match "]")))
925 (let ((n (- (window-height (selected-window))
926 (count-lines (point-min) (point))
930 ;; For a short gap, we don't need the [...] line,
932 (delete-region (point) (progn (end-of-line) (point)))
934 ;; Some people get confused by the large gap.
937 ;; Skip the [...] line (don't delete it).
939 (newline (- n (/ n 2)))))
940 (goto-char (point-min)))
941 (setq buffer-undo-list nil)
942 (set-buffer-modified-p nil)))))
945 ;; Below is some attempt to handle language specific strings. These
946 ;; are currently only used in the tutorial.
948 (defconst lang-strings
952 (tut-chgdkey . "** The key %s has been rebound, but you can use %s instead [")
953 (tut-chgdkey2 . "More information")
955 NOTICE: The main purpose of the Emacs tutorial is to teach you
956 the most important standard Emacs commands (key bindings).
957 However, your Emacs has been customized by changing some of
958 these basic editing commands, so it doesn't correspond to the
959 tutorial. We have inserted colored notices where the altered
960 commands have been introduced. [")
961 (tut-chgdhead2 . "Details")
965 "Language specific strings for Emacs.
966 This is an association list with the keys equal to the strings
967 that can be returned by `read-language-name'. The elements in
968 the list are themselves association lists with keys that are
969 string ids and values that are the language specific strings.
971 See `get-lang-string' for more information.")
973 (defun get-lang-string(lang stringid &optional no-eng-fallback)
974 "Get a language specific string for Emacs.
975 In certain places Emacs can replace a string showed to the user with a language specific string.
976 This function retrieves such strings.
978 LANG is the language specification. It should be one of those
979 strings that can be returned by `read-language-name'. STRINGID
980 is a symbol that specifies the string to retrieve.
982 If no string is found for STRINGID in the choosen language then
983 the English string is returned unless NO-ENG-FALLBACK is non-nil.
985 See `lang-strings' for more information.
987 Currently this feature is only used in `help-with-tutorial'."
988 (let ((my-lang-strings (assoc lang lang-strings))
990 (when my-lang-strings
991 (let ((entry (assoc stringid (cdr my-lang-strings))))
993 (setq found-string (cdr entry)))))
994 ;; Fallback to English strings
995 (unless (or found-string
997 (setq found-string (get-lang-string "English" stringid t)))
1000 ;;(get-lang-string "English" 'tut-chgdkey)
1004 ;; arch-tag: c8e80aef-c3bb-4ffb-8af6-22171bf0c100
1005 ;;; tutorial.el ends here