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"
157 " you can use the key "
159 " to get the function `"
162 (fill-region (point-min) (point)))))
163 (print-help-return-message))))
165 (defun tutorial--sort-keys (left right
)
166 "Sort predicate for use with `tutorial--default-keys'.
167 This is a predicate function to `sort'.
169 The sorting is for presentation purpose only and is done on the
172 LEFT and RIGHT are the elements to compare."
173 (let ((x (append (cadr left
) nil
))
174 (y (append (cadr right
) nil
)))
175 ;; Skip the front part of the key sequences if they are equal:
178 (equal (car x
) (car y
)))
181 ;; Try to make a comparision that is useful for presentation (this
182 ;; could be made nicer perhaps):
185 ;;(message "x=%s, y=%s;;;; cx=%s, cy=%s" x y cx cy)
187 ;; Lists? Then call this again
191 (tutorial--sort-keys cx cy
))
192 ;; Are both numbers? Then just compare them
196 ;; Is one of them a number? Let that be bigger then.
201 ;; Are both symbols? Compare the names then.
204 (string< (symbol-name cy
)
205 (symbol-name cx
)))))))
207 (defconst tutorial--default-keys
208 ;; On window system, `suspend-emacs' is replaced in the default
210 (let* ((suspend-emacs (if window-system
211 'iconify-or-deiconify-frame
215 (Control-X-prefix [?\C-x])
216 (mode-specific-command-prefix [?\C-c])
217 (save-buffers-kill-emacs [?\C-x ?\C-c])
221 (scroll-down [?\M-v])
224 ;; * BASIC CURSOR CONTROL
225 (forward-char [?\C-f])
226 (backward-char [?\C-b])
227 (forward-word [?\M-f])
228 (backward-word [?\M-b])
230 (previous-line [?\C-p])
231 (move-beginning-of-line [?\C-a])
232 (move-end-of-line [?\C-e])
233 (backward-sentence [?\M-a])
234 (forward-sentence [?\M-e])
236 (beginning-of-buffer [?\M-<])
237 (end-of-buffer [?\M->])
238 (universal-argument [?\C-u])
240 ;; * WHEN EMACS IS HUNG
241 (keyboard-quit [?\C-g])
243 ;; * DISABLED COMMANDS
244 (downcase-region [?\C-x ?\C-l])
247 (delete-other-windows [?\C-x ?1])
249 ;; Type CONTROL-h k CONTROL-f.
251 ;; * INSERTING AND DELETING
252 ;; C-u 8 * to insert ********.
253 (delete-backward-char "\d")
254 (delete-char [?\C-d])
255 (backward-kill-word [?\M-\d])
258 (kill-sentence [?\M-k])
259 (set-mark-command [?\C-@])
260 (set-mark-command [?\C- ])
261 (kill-region [?\C-w])
266 (advertised-undo [?\C-x ?u])
267 (advertised-undo [?\C-x ?u])
270 (find-file [?\C-x ?\C-f])
271 (save-buffer [?\C-x ?\C-s])
274 (list-buffers [?\C-x ?\C-b])
275 (switch-to-buffer [?\C-x ?b])
276 (save-some-buffers [?\C-x ?s])
278 ;; * EXTENDING THE COMMAND SET
279 ;; C-x Character eXtend. Followed by one character.
280 (execute-extended-command [?\M-x])
283 ;; C-x s Save some buffers
284 ;; C-x C-b List buffers
285 ;; C-x b Switch buffer
286 ;; C-x C-c Quit Emacs
287 ;; C-x 1 Delete all but one window
291 (describe-mode [?\C-h ?m])
292 (set-fill-column [?\C-x ?f])
293 (fill-paragraph [?\M-q])
296 (isearch-forward [?\C-s])
297 (isearch-backward [?\C-r])
299 ;; * MULTIPLE WINDOWS
300 (split-window-vertically [?\C-x ?2])
301 (scroll-other-window [?\C-\M-v])
302 (other-window [?\C-x ?o])
303 (find-file-other-window [?\C-x ?4 ?\C-f])
305 ;; * RECURSIVE EDITING LEVELS
306 (keyboard-escape-quit [27 27 27])
308 ;; * GETTING MORE HELP
309 ;; The most basic HELP feature is C-h c
310 (describe-key-briefly [?\C-h ?c])
311 (describe-key [?\C-h ?k])
317 ;;(iconify-or-deiconify-frame [?\C-z])
318 (,suspend-emacs [?\C-z]))))
319 (sort default-keys 'tutorial--sort-keys))
320 "Default Emacs key bindings that the tutorial depends on.")
322 (defun tutorial--detailed-help (button)
323 "Give detailed help about changed keys."
324 (with-output-to-temp-buffer (help-buffer)
325 (help-setup-xref (list #'tutorial--detailed-help button)
327 (with-current-buffer (help-buffer)
328 (let* ((tutorial-buffer (button-get button 'tutorial-buffer))
329 (explain-key-desc (button-get button 'explain-key-desc))
330 (changed-keys (with-current-buffer tutorial-buffer
332 (goto-char (point-min))
333 (tutorial--find-changed-keys
334 tutorial--default-keys)))))
337 "The following key bindings used in the tutorial have been changed
338 from the Emacs default:\n\n" )
339 (let ((frm " %-14s %-27s %-16s\n"))
341 "Standard Key" "Command" "In Your Emacs")))
342 (dolist (tk changed-keys)
343 (let* ((def-fun (nth 1 tk))
345 (def-fun-txt (nth 2 tk))
348 (rem-fun (command-remapping def-fun))
349 (key-txt (key-description key))
350 (key-fun (with-current-buffer tutorial-buffer (key-binding key)))
352 (unless (eq def-fun key-fun)
353 ;; Insert key binding description:
354 (when (string= key-txt explain-key-desc)
355 (put-text-property 0 (length key-txt)
356 'face 'tutorial-warning-face key-txt))
357 (insert " " key-txt " ")
359 ;; Insert a link describing the old binding:
360 (insert-button def-fun-txt
363 (lambda (button) (interactive)
365 (button-get button 'value)))
370 ;; Tell where the old binding is now:
371 (insert (format " %-16s "
372 (if (string= "" where)
373 (format "M-x %s" def-fun-txt)
375 ;; Insert a link with more information, for example
376 ;; current binding and keymap or information about
377 ;; cua-mode replacements:
378 (insert-button (car remark)
380 (lambda (b) (interactive)
381 (let ((value (button-get b 'value)))
382 (tutorial--describe-nonstandard-key value)))
388 It is OK to change key bindings, but changed bindings do not
389 correspond to what the tutorial says.\n\n")
390 (print-help-return-message)))))
392 (defun tutorial--find-changed-keys (default-keys)
393 "Find the key bindings used in the tutorial that have changed.
394 Return a list with elements of the form
396 '(KEY DEF-FUN DEF-FUN-TXT WHERE REMARK QUIET)
400 KEY is a key sequence whose standard binding has been changed
401 DEF-FUN is the standard binding of KEY
402 DEF-FUN-TXT is a short descriptive text for DEF-FUN
403 WHERE is a text describing the key sequences to which DEF-FUN is
404 bound now (or, if it is remapped, a key sequence
405 for the function it is remapped to)
406 REMARK is a list with info about rebinding. It has either of these
410 \(TEXT current-binding KEY-FUN DEF-FUN KEY WHERE)
412 Here TEXT is a link text to show to the user. The
413 rest of the list is used to show information when
414 the user clicks the link.
416 KEY-FUN is the actual binding for KEY.
417 QUIET is t if this changed keybinding should be handled quietly.
418 This is used by `tutorial--display-changes'."
419 (let (changed-keys remark)
420 ;; Look up the bindings in a Fundamental mode buffer
421 ;; so we do not get fooled by some other major mode.
424 (dolist (kdf default-keys)
425 ;; The variables below corresponds to those with the same names
426 ;; described in the doc string.
427 (let* ((key (nth 1 kdf))
428 (def-fun (nth 0 kdf))
429 (def-fun-txt (format "%s" def-fun))
430 (rem-fun (command-remapping def-fun))
431 (key-fun (if (eq def-fun 'ESC-prefix)
432 (lookup-key global-map [27])
434 (where (where-is-internal (if rem-fun rem-fun def-fun
))))
437 (setq where
(key-description (car where
)))
438 (when (and (< 10 (length where
))
439 (string= (substring where
0 (length "<menu-bar>"))
441 (setq where
"the menus")))
445 (cond ((eq key-fun def-fun
)
446 ;; No rebinding, return t
449 (eq key-fun
(command-remapping def-fun
)))
450 ;; Just a remapping, return t
452 ;; cua-mode specials:
456 (eq key-fun
'cua-paste
))
459 (eq key-fun
'undo
))))
460 (setq remark
(list "cua-mode, more info" 'cua-mode
))
463 (or (and (eq def-fun
'ESC-prefix
)
466 (118 . cua-repeat-replace-region
)))
467 (setq def-fun-txt
"\"ESC prefix\""))
468 (and (eq def-fun
'mode-specific-command-prefix
)
471 (timeout . copy-region-as-kill
)))
472 (setq def-fun-txt
"\"C-c prefix\""))
473 (and (eq def-fun
'Control-X-prefix
)
475 '(keymap (timeout . kill-region
)))
476 (setq def-fun-txt
"\"C-x prefix\""))))
477 (setq remark
(list "cua-mode replacement" 'cua-mode
))
478 (setq where
"Same key")
480 ;; viper-mode specials:
481 ((and (boundp 'viper-mode-string
)
482 (boundp 'viper-current-state
)
483 (eq viper-current-state
'vi-state
)
484 (or (and (eq def-fun
'isearch-forward
)
485 (eq key-fun
'viper-isearch-forward
))
486 (and (eq def-fun
'isearch-backward
)
487 (eq key-fun
'viper-isearch-backward
))))
488 ;; These bindings works as the default bindings,
491 ((when normal-erase-is-backspace
492 (or (and (equal key
[C-delete
])
493 (equal key-fun
'kill-word
))
494 (and (equal key
[C-backspace
])
495 (equal key-fun
'backward-kill-word
))))
496 ;; This is the strange handling of C-delete and
497 ;; C-backspace, return t
500 ;; This key has indeed been rebound. Put information
501 ;; in `remark' and return nil
503 (list "more info" 'current-binding
504 key-fun def-fun key where
))
506 (add-to-list 'changed-keys
507 (list key def-fun def-fun-txt where remark nil
))))))
510 (defun tutorial--key-description (key)
511 (let ((desc (key-description key
)))
512 (cond ((string= "ESC" desc
) "<ESC>")
513 ((string= "RET" desc
) "<Return>")
514 ((string= "DEL" desc
) "<Delback>")
517 (defun tutorial--display-changes ()
518 "Display changes to some default key bindings.
519 If some of the default key bindings that the tutorial depends on
520 have been changed then display the changes in the tutorial buffer
521 with some explanatory links."
522 (let* ((changed-keys (tutorial--find-changed-keys
523 tutorial--default-keys
))
524 ;; Alist of element (DESC . CK) where DESC is the
525 ;; key-description of a changed key and CK is the
526 ;; corresponding element in `changed-keys'.
528 (mapcar (lambda (ck) (cons (tutorial--key-description (car ck
)) ck
))
532 (case-fold-search nil
)
534 (concat "[[:space:]]\\("
535 (mapconcat (lambda (kdf) (regexp-quote
536 (tutorial--key-description
538 tutorial--default-keys
540 "\\)[[:punct:][:space:]]")))
541 ;; Need the custom button face for viper buttons:
542 (if (boundp 'viper-mode-string
) (require 'cus-edit
))
544 (if (or changed-keys
(boundp 'viper-mode-string
))
545 (let ((head (get-lang-string tutorial--lang
'tut-chgdhead
))
546 (head2 (get-lang-string tutorial--lang
'tut-chgdhead2
)))
547 (when (and head head2
)
548 (goto-char tutorial--point-before-chkeys
)
550 (insert-button head2
'tutorial-buffer
(current-buffer)
551 'action
'tutorial--detailed-help
552 'follow-link t
'face
'link
)
554 (add-text-properties tutorial--point-before-chkeys
(point)
555 '(tutorial-remark remark
556 face tutorial-warning-face
559 ;; Scan the tutorial for all key sequences.
560 (goto-char (point-min))
561 (while (re-search-forward keybindings-regexp
(point-max) t
)
562 ;; Then highlight each rebound key sequence.
563 ;; This avoids issuing a warning for, e.g., C-x C-b if C-b is rebound.
564 (setq changed-key
(assoc (match-string 1) changed-keys-alist
))
566 (not (get-text-property (match-beginning 1) 'tutorial-remark
))
567 (let* ((desc (car changed-key
))
568 (ck (cdr changed-key
))
573 (unless (string= where
"Same key")
574 (when (string= where
"")
575 (setq where
(format "M-x %s" def-fun
)))
576 (setq tutorial--point-after-chkeys
(point-marker)
577 s1
(get-lang-string tutorial--lang
'tut-chgdkey
)
578 s2
(get-lang-string tutorial--lang
'tut-chgdkey2
)
579 help-string
(and s1 s2
(format s1 desc where
)))
580 (add-text-properties (match-beginning 1) (match-end 1)
581 '(face tutorial-warning-face
582 tutorial-remark key-sequence
))
585 ;; Put help string in the tooltip.
586 (put-text-property (match-beginning 1) (match-end 1)
587 'help-echo help-string
)
588 ;; Put help string in the buffer.
590 (setcar (nthcdr 5 ck
) t
)
592 ;; Two or more changed keys were on the same line.
593 (while (eq (get-text-property (point) 'tutorial-remark
)
597 (insert "** " help-string
" [")
598 (insert-button s2
'tutorial-buffer
(current-buffer)
599 'action
'tutorial--detailed-help
600 'explain-key-desc desc
'follow-link t
603 (add-text-properties start
(point)
604 '(tutorial-remark remark
606 face tutorial-warning-face
607 read-only t
)))))))))))
609 (defun tutorial--saved-dir ()
610 "Directory to which tutorials are saved."
611 (expand-file-name "tutorial"
612 (if (eq system-type
'ms-dos
) "~/_emacs.d/" "~/.emacs.d/")))
614 (defun tutorial--saved-file ()
615 "File name in which to save tutorials."
616 (let ((file-name tutorial--lang
)
617 (ext (file-name-extension tutorial--lang
)))
620 (setq file-name
(concat file-name
".tut")))
621 (expand-file-name file-name
(tutorial--saved-dir))))
623 (defun tutorial--remove-remarks ()
624 "Remove the remark lines that was added to the tutorial buffer."
626 (goto-char (point-min))
630 ;; Catch the case when we already are on a remark line
631 (while (if (get-text-property (point) 'tutorial-remark
)
632 (setq prop-start
(point))
633 (setq prop-start
(next-single-property-change (point) 'tutorial-remark
)))
634 (setq prop-end
(next-single-property-change prop-start
'tutorial-remark
))
635 (setq prop-val
(get-text-property prop-start
'tutorial-remark
))
637 (setq prop-end
(point-max)))
639 (unless (eq prop-val
'key-sequence
)
640 (delete-region prop-start prop-end
))))))
642 (defun tutorial--save-tutorial ()
643 "Save the tutorial buffer.
644 This saves the part of the tutorial before and after the area
645 showing changed keys. It also saves the point position and the
646 position where the display of changed bindings was inserted."
647 ;; This runs in a hook so protect it:
649 (if (y-or-n-p "Save your position in the tutorial? ")
650 (tutorial--save-tutorial-to (tutorial--saved-file)))
651 (error (message "Error saving tutorial state: %s"
652 (error-message-string err
)))))
654 (defun tutorial--save-tutorial-to (saved-file)
655 "Save the tutorial buffer to SAVED-FILE.
656 See `tutorial--save-tutorial' for more information."
658 (when (or (buffer-modified-p)
660 (let ((tutorial-dir (tutorial--saved-dir))
662 ;; The tutorial is saved in a subdirectory in the user home
663 ;; directory. Create this subdirectory first.
664 (unless (file-directory-p tutorial-dir
)
666 (make-directory tutorial-dir nil
)
667 (error (setq save-err t
)
668 (warn "Could not create directory %s: %s" tutorial-dir
669 (error-message-string err
)))))
670 ;; Make sure we have that directory.
671 (if (file-directory-p tutorial-dir
)
672 (let ((tut-point (if (= 0 tutorial--point-after-chkeys
)
673 ;; No info about changed keys is
676 (if (< (point) tutorial--point-after-chkeys
)
678 (- (point) tutorial--point-after-chkeys
))))
680 ;; Use a special undo list so that we easily can undo
681 ;; the changes we make to the tutorial buffer. This is
682 ;; currently not needed since we now delete the buffer
683 ;; after saving, but kept for possible future use of
686 (inhibit-read-only t
))
687 ;; Delete the area displaying info about changed keys.
688 ;; (when (< 0 tutorial--point-after-chkeys)
689 ;; (delete-region tutorial--point-before-chkeys
690 ;; tutorial--point-after-chkeys))
691 ;; Delete the remarks:
692 (tutorial--remove-remarks)
693 ;; Put the value of point first in the buffer so it will
694 ;; be saved with the tutorial.
695 (goto-char (point-min))
696 (insert (number-to-string tut-point
)
698 (number-to-string (marker-position
699 tutorial--point-before-chkeys
))
702 (write-region nil nil saved-file
)
703 (error (setq save-err t
)
704 (warn "Could not save tutorial to %s: %s"
706 (error-message-string err
))))
707 ;; An error is raised here?? Is this a bug?
712 (goto-char old-point
)
714 (message "Could not save tutorial state.")
715 (message "Saved tutorial state.")))
716 (message "Can't save tutorial: %s is not a directory"
721 (defun help-with-tutorial (&optional arg dont-ask-for-revert
)
722 "Select the Emacs learn-by-doing tutorial.
723 If there is a tutorial version written in the language
724 of the selected language environment, that version is used.
725 If there's no tutorial in that language, `TUTORIAL' is selected.
726 With ARG, you are asked to choose which language.
727 If DONT-ASK-FOR-REVERT is non-nil the buffer is reverted without
728 any question when restarting the tutorial.
730 If any of the standard Emacs key bindings that are used in the
731 tutorial have been changed then an explanatory note about this is
732 shown in the beginning of the tutorial buffer.
734 When the tutorial buffer is killed the content and the point
735 position in the buffer is saved so that the tutorial may be
738 (if (boundp 'viper-current-state
)
740 "You can not run the Emacs tutorial directly because you have \
742 (prompt2 "\nThere is however a Viper tutorial you can run instead.
743 Run the Viper tutorial? "))
744 (if (fboundp 'viper-tutorial
)
745 (if (y-or-n-p (concat prompt1 prompt2
))
747 (funcall 'viper-tutorial
0))
748 (message "Tutorial aborted by user"))
751 (let ((minibuffer-setup-hook minibuffer-setup-hook
))
752 (add-hook 'minibuffer-setup-hook
753 'minibuffer-completion-help
)
754 (read-language-name 'tutorial
"Language: " "English"))
755 (if (get-language-info current-language-environment
'tutorial
)
756 current-language-environment
758 (filename (get-language-info lang
'tutorial
))
759 ;; Choose a buffer name including the language so that
760 ;; several languages can be tested simultaneously:
761 (tut-buf-name (concat "TUTORIAL (" lang
")"))
762 (old-tut-buf (get-buffer tut-buf-name
))
763 (old-tut-win (when old-tut-buf
(get-buffer-window old-tut-buf t
)))
764 (old-tut-is-ok (when old-tut-buf
765 (not (buffer-modified-p old-tut-buf
))))
768 (setq tutorial--point-after-chkeys
(point-min))
769 ;; Try to display the tutorial buffer before asking to revert it.
770 ;; If the tutorial buffer is shown in some window make sure it is
771 ;; selected and displayed:
775 (select-window (get-buffer-window old-tut-buf t
))))
776 ;; Else, is there an old tutorial buffer? Then display it:
778 (switch-to-buffer old-tut-buf
)))
779 ;; Use whole frame for tutorial
780 (delete-other-windows)
781 ;; If the tutorial buffer has been changed then ask if it should
783 (when (and old-tut-buf
786 (if dont-ask-for-revert
789 "You have changed the Tutorial buffer. Revert it? ")))))
790 ;; (Re)build the tutorial buffer if it is not ok
791 (unless old-tut-is-ok
792 (switch-to-buffer (get-buffer-create tut-buf-name
))
793 (unless old-tut-buf
(text-mode))
794 (unless lang
(error "Variable lang is nil"))
795 (setq tutorial--lang lang
)
796 (setq old-tut-file
(file-exists-p (tutorial--saved-file)))
797 (let ((inhibit-read-only t
))
799 (message "Preparing tutorial ...") (sit-for 0)
801 ;; Do not associate the tutorial buffer with a file. Instead use
802 ;; a hook to save it when the buffer is killed.
803 (setq buffer-auto-save-file-name nil
)
804 (add-hook 'kill-buffer-hook
'tutorial--save-tutorial nil t
)
806 ;; Insert the tutorial. First offer to resume last tutorial
808 (when dont-ask-for-revert
809 (setq old-tut-file nil
))
812 (y-or-n-p "Resume your last saved tutorial? ")))
815 (insert-file-contents (tutorial--saved-file))
816 (goto-char (point-min))
819 (buffer-substring-no-properties
820 (line-beginning-position) (line-end-position))))
822 (setq tutorial--point-before-chkeys
824 (buffer-substring-no-properties
825 (line-beginning-position) (line-end-position))))
827 (delete-region (point-min) (point))
828 (goto-char tutorial--point-before-chkeys
)
829 (setq tutorial--point-before-chkeys
(point-marker)))
830 (insert-file-contents (expand-file-name filename data-directory
))
832 (setq tutorial--point-before-chkeys
(point-marker)))
834 (tutorial--display-changes)
837 (unless dont-ask-for-revert
838 (message "") (sit-for 0))
842 ;; Just move to old point in saved tutorial.
844 (if (> 0 old-tut-point
)
846 (+ old-tut-point tutorial--point-after-chkeys
))))
847 (when (< old-point
1)
849 (goto-char old-point
))
850 (goto-char (point-min))
851 (search-forward "\n<<")
853 ;; Convert the <<...>> line to the proper [...] line,
854 ;; or just delete the <<...>> line if a [...] line follows.
855 (cond ((save-excursion
858 (delete-region (point) (progn (forward-line 1) (point))))
859 ((looking-at "<<Blank lines inserted.*>>")
860 (replace-match "[Middle of page left blank for didactic purposes. Text continues below]"))
864 (search-forward ">>")
865 (replace-match "]")))
867 (let ((n (- (window-height (selected-window))
868 (count-lines (point-min) (point))
872 ;; For a short gap, we don't need the [...] line,
874 (delete-region (point) (progn (end-of-line) (point)))
876 ;; Some people get confused by the large gap.
879 ;; Skip the [...] line (don't delete it).
881 (newline (- n
(/ n
2)))))
882 (goto-char (point-min)))
883 (setq buffer-undo-list nil
)
884 (set-buffer-modified-p nil
)))))
887 ;; Below is some attempt to handle language specific strings. These
888 ;; are currently only used in the tutorial.
890 (defconst lang-strings
892 ((tut-chgdkey .
"%s has been rebound, but you can use %s instead")
893 (tut-chgdkey2 .
"More")
895 NOTICE: The main purpose of the Emacs tutorial is to teach you
896 the most important standard Emacs commands (key bindings).
897 However, your Emacs has been customized by changing some of
898 these basic editing commands, so it doesn't correspond to the
899 tutorial. We have inserted colored notices where the altered
900 commands have been introduced.")
901 (tut-chgdhead2 .
"More"))))
902 "Language specific strings for Emacs.
903 This is an association list with the keys equal to the strings
904 that can be returned by `read-language-name'. The elements in
905 the list are themselves association lists with keys that are
906 string ids and values that are the language specific strings.
908 See `get-lang-string' for more information.")
910 (defun get-lang-string (lang stringid
&optional no-eng-fallback
)
911 "Get a language specific string for Emacs.
912 In certain places Emacs can replace a string showed to the user with a language specific string.
913 This function retrieves such strings.
915 LANG is the language specification. It should be one of those
916 strings that can be returned by `read-language-name'. STRINGID
917 is a symbol that specifies the string to retrieve.
919 If no string is found for STRINGID in the choosen language then
920 the English string is returned unless NO-ENG-FALLBACK is non-nil.
922 See `lang-strings' for more information.
924 Currently this feature is only used in `help-with-tutorial'."
925 (let ((my-lang-strings (assoc lang lang-strings
))
927 (when my-lang-strings
928 (let ((entry (assoc stringid
(cdr my-lang-strings
))))
930 (setq found-string
(cdr entry
)))))
931 ;; Fallback to English strings
932 (unless (or found-string
934 (setq found-string
(get-lang-string "English" stringid t
)))
937 ;;(get-lang-string "English" 'tut-chgdkey)
941 ;; arch-tag: c8e80aef-c3bb-4ffb-8af6-22171bf0c100
942 ;;; tutorial.el ends here