Improve index entries. Remove redundant/useless ones.
[emacs.git] / lisp / tutorial.el
blob8495f1db6d96ddee1c8e68e0ae120f66f98149e1
1 ;;; tutorial.el --- tutorial for Emacs
3 ;; Copyright (C) 2006, 2007 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 2, 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-equal "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 (if window-system
214 'iconify-or-deiconify-frame
215 'suspend-emacs))
216 (default-keys
217 `((ESC-prefix [27])
218 (Control-X-prefix [?\C-x])
219 (mode-specific-command-prefix [?\C-c])
220 (save-buffers-kill-emacs [?\C-x ?\C-c])
222 ;; * SUMMARY
223 (scroll-up [?\C-v])
224 (scroll-down [?\M-v])
225 (recenter [?\C-l])
227 ;; * BASIC CURSOR CONTROL
228 (forward-char [?\C-f])
229 (backward-char [?\C-b])
230 (forward-word [?\M-f])
231 (backward-word [?\M-b])
232 (next-line [?\C-n])
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])
238 (newline "\r")
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])
249 ;; * WINDOWS
250 (delete-other-windows [?\C-x ?1])
251 ;; C-u 0 C-l
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])
259 (kill-word [?\M-d])
260 (kill-line [?\C-k])
261 (kill-sentence [?\M-k])
262 (set-mark-command [?\C-@])
263 (set-mark-command [?\C- ])
264 (kill-region [?\C-w])
265 (yank [?\C-y])
266 (yank-pop [?\M-y])
268 ;; * UNDO
269 (advertised-undo [?\C-x ?u])
270 (advertised-undo [?\C-x ?u])
272 ;; * FILES
273 (find-file [?\C-x ?\C-f])
274 (save-buffer [?\C-x ?\C-s])
276 ;; * BUFFERS
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])
284 ;; C-x C-f Find file
285 ;; C-x C-s Save file
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
291 ;; C-x u Undo
293 ;; * MODE LINE
294 (describe-mode [?\C-h ?m])
295 (set-fill-column [?\C-x ?f])
296 (fill-paragraph [?\M-q])
298 ;; * SEARCHING
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])
316 ;; * MORE FEATURES
317 ;; F10
319 ;; * CONCLUSION
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)
329 (interactive-p))
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
334 (save-excursion
335 (goto-char (point-min))
336 (tutorial--find-changed-keys
337 tutorial--default-keys)))))
338 (when changed-keys
339 (insert
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"))
343 (insert (format frm
344 "Standard Key" "Command" "In Your Emacs")))
345 (dolist (tk changed-keys)
346 (let* ((def-fun (nth 1 tk))
347 (key (nth 0 tk))
348 (def-fun-txt (nth 2 tk))
349 (where (nth 3 tk))
350 (remark (nth 4 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)))
354 tot-len)
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 " ")
361 (indent-to 18)
362 ;; Insert a link describing the old binding:
363 (insert-button def-fun-txt
364 'value def-fun
365 'action
366 (lambda (button) (interactive)
367 (describe-function
368 (button-get button 'value)))
369 'follow-link t)
370 (indent-to 45)
371 (when (listp where)
372 (setq where "list"))
373 ;; Tell where the old binding is now:
374 (insert (format " %-16s "
375 (if (string= "" where)
376 (format "M-x %s" def-fun-txt)
377 where)))
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)
382 'action
383 (lambda (b) (interactive)
384 (let ((value (button-get b 'value)))
385 (tutorial--describe-nonstandard-key value)))
386 'value (cdr remark)
387 'follow-link t)
388 (insert "\n")))))
390 (insert "
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)
401 where
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 these
410 formats:
412 \(TEXT cua-mode)
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.
425 (with-temp-buffer
426 (fundamental-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])
436 (key-binding key)))
437 (where (where-is-internal (if rem-fun rem-fun def-fun))))
438 (if where
439 (progn
440 (setq where (key-description (car where)))
441 (when (and (< 10 (length where))
442 (string= (substring where 0 (length "<menu-bar>"))
443 "<menu-bar>"))
444 (setq where "the menus")))
445 (setq where ""))
446 (setq remark nil)
447 (unless
448 (cond ((eq key-fun def-fun)
449 ;; No rebinding, return t
451 ((and key-fun
452 (eq key-fun (command-remapping def-fun)))
453 ;; Just a remapping, return t
455 ;; cua-mode specials:
456 ((and cua-mode
457 (or (and
458 (equal key [?\C-v])
459 (eq key-fun 'cua-paste))
460 (and
461 (equal key [?\C-z])
462 (eq key-fun 'undo))))
463 (setq remark (list "cua-mode, more info" 'cua-mode))
464 nil)
465 ((and cua-mode
466 (or (and (eq def-fun 'ESC-prefix)
467 (equal key-fun
468 `(keymap
469 (118 . cua-repeat-replace-region)))
470 (setq def-fun-txt "\"ESC prefix\""))
471 (and (eq def-fun 'mode-specific-command-prefix)
472 (equal key-fun
473 '(keymap
474 (timeout . copy-region-as-kill)))
475 (setq def-fun-txt "\"C-c prefix\""))
476 (and (eq def-fun 'Control-X-prefix)
477 (equal key-fun
478 '(keymap (timeout . kill-region)))
479 (setq def-fun-txt "\"C-x prefix\""))))
480 (setq remark (list "cua-mode replacement" 'cua-mode))
481 (setq where "Same key")
482 nil)
483 ;; viper-mode specials:
484 ((and (boundp 'viper-mode-string)
485 (boundp 'viper-current-state)
486 (eq viper-current-state 'vi-state)
487 (or (and (eq def-fun 'isearch-forward)
488 (eq key-fun 'viper-isearch-forward))
489 (and (eq def-fun 'isearch-backward)
490 (eq key-fun 'viper-isearch-backward))))
491 ;; These bindings works as the default bindings,
492 ;; return t
494 ((when normal-erase-is-backspace
495 (or (and (equal key [C-delete])
496 (equal key-fun 'kill-word))
497 (and (equal key [C-backspace])
498 (equal key-fun 'backward-kill-word))))
499 ;; This is the strange handling of C-delete and
500 ;; C-backspace, return t
503 ;; This key has indeed been rebound. Put information
504 ;; in `remark' and return nil
505 (setq remark
506 (list "more info" 'current-binding
507 key-fun def-fun key where))
508 nil))
509 (add-to-list 'changed-keys
510 (list key def-fun def-fun-txt where remark nil))))))
511 changed-keys))
513 (defun tutorial--key-description (key)
514 (let ((desc (key-description key)))
515 (cond ((string= "ESC" desc) "<ESC>")
516 ((string= "RET" desc) "<Return>")
517 ((string= "DEL" desc) "<Delback>")
518 (t desc))))
520 (defun tutorial--display-changes ()
521 "Display changes to some default key bindings.
522 If some of the default key bindings that the tutorial depends on
523 have been changed then display the changes in the tutorial buffer
524 with some explanatory links."
525 (let* ((changed-keys (tutorial--find-changed-keys
526 tutorial--default-keys))
527 ;; Alist of element (DESC . CK) where DESC is the
528 ;; key-description of a changed key and CK is the
529 ;; corresponding element in `changed-keys'.
530 (changed-keys-alist
531 (mapcar (lambda (ck) (cons (tutorial--key-description (car ck)) ck))
532 changed-keys))
533 changed-key
534 (start (point))
535 (case-fold-search nil)
536 (keybindings-regexp
537 (concat "[[:space:]]\\("
538 (mapconcat (lambda (kdf) (regexp-quote
539 (tutorial--key-description
540 (nth 1 kdf))))
541 tutorial--default-keys
542 "\\|")
543 "\\)[[:punct:][:space:]]")))
544 ;; Need the custom button face for viper buttons:
545 (if (boundp 'viper-mode-string) (require 'cus-edit))
547 (if (or changed-keys (boundp 'viper-mode-string))
548 (let ((head (get-lang-string tutorial--lang 'tut-chgdhead))
549 (head2 (get-lang-string tutorial--lang 'tut-chgdhead2)))
550 (when (and head head2)
551 (goto-char tutorial--point-before-chkeys)
552 (insert head " [")
553 (insert-button head2 'tutorial-buffer (current-buffer)
554 'action 'tutorial--detailed-help
555 'follow-link t 'face 'link)
556 (insert "]\n\n")
557 (add-text-properties tutorial--point-before-chkeys (point)
558 '(tutorial-remark remark
559 face tutorial-warning-face
560 read-only t)))))
562 ;; Scan the tutorial for all key sequences.
563 (goto-char (point-min))
564 (while (re-search-forward keybindings-regexp (point-max) t)
565 ;; Then highlight each rebound key sequence.
566 ;; This avoids issuing a warning for, e.g., C-x C-b if C-b is rebound.
567 (setq changed-key (assoc (match-string 1) changed-keys-alist))
568 (and changed-key
569 (not (get-text-property (match-beginning 1) 'tutorial-remark))
570 (let* ((desc (car changed-key))
571 (ck (cdr changed-key))
572 (key (nth 0 ck))
573 (def-fun (nth 1 ck))
574 (where (nth 3 ck))
575 s1 s2 help-string)
576 (unless (string= where "Same key")
577 (when (string= where "")
578 (setq where (format "M-x %s" def-fun)))
579 (setq tutorial--point-after-chkeys (point-marker)
580 s1 (get-lang-string tutorial--lang 'tut-chgdkey)
581 s2 (get-lang-string tutorial--lang 'tut-chgdkey2)
582 help-string (and s1 s2 (format s1 desc where)))
583 (add-text-properties (match-beginning 1) (match-end 1)
584 '(face tutorial-warning-face
585 tutorial-remark key-sequence))
586 (if help-string
587 (if (nth 5 ck)
588 ;; Put help string in the tooltip.
589 (put-text-property (match-beginning 1) (match-end 1)
590 'help-echo help-string)
591 ;; Put help string in the buffer.
592 (save-excursion
593 (setcar (nthcdr 5 ck) t)
594 (forward-line)
595 ;; Two or more changed keys were on the same line.
596 (while (eq (get-text-property (point) 'tutorial-remark)
597 'remark)
598 (forward-line))
599 (setq start (point))
600 (insert "** " help-string " [")
601 (insert-button s2 'tutorial-buffer (current-buffer)
602 'action 'tutorial--detailed-help
603 'explain-key-desc desc 'follow-link t
604 'face 'link)
605 (insert "] **\n")
606 (add-text-properties start (point)
607 '(tutorial-remark remark
608 rear-nonsticky t
609 face tutorial-warning-face
610 read-only t)))))))))))
612 (defun tutorial--saved-dir ()
613 "Directory to which tutorials are saved."
614 (expand-file-name "tutorial"
615 (if (eq system-type 'ms-dos) "~/_emacs.d/" "~/.emacs.d/")))
617 (defun tutorial--saved-file ()
618 "File name in which to save tutorials."
619 (let ((file-name tutorial--lang)
620 (ext (file-name-extension tutorial--lang)))
621 (when (or (not ext)
622 (string= ext ""))
623 (setq file-name (concat file-name ".tut")))
624 (expand-file-name file-name (tutorial--saved-dir))))
626 (defun tutorial--remove-remarks ()
627 "Remove the remark lines that was added to the tutorial buffer."
628 (save-excursion
629 (goto-char (point-min))
630 (let (prop-start
631 prop-end
632 prop-val)
633 ;; Catch the case when we already are on a remark line
634 (while (if (get-text-property (point) 'tutorial-remark)
635 (setq prop-start (point))
636 (setq prop-start (next-single-property-change (point) 'tutorial-remark)))
637 (setq prop-end (next-single-property-change prop-start 'tutorial-remark))
638 (setq prop-val (get-text-property prop-start 'tutorial-remark))
639 (unless prop-end
640 (setq prop-end (point-max)))
641 (goto-char prop-end)
642 (unless (eq prop-val 'key-sequence)
643 (delete-region prop-start prop-end))))))
645 (defun tutorial--save-tutorial ()
646 "Save the tutorial buffer.
647 This saves the part of the tutorial before and after the area
648 showing changed keys. It also saves the point position and the
649 position where the display of changed bindings was inserted."
650 ;; This runs in a hook so protect it:
651 (condition-case err
652 (if (y-or-n-p "Save your position in the tutorial? ")
653 (tutorial--save-tutorial-to (tutorial--saved-file)))
654 (error (message "Error saving tutorial state: %s"
655 (error-message-string err)))))
657 (defun tutorial--save-tutorial-to (saved-file)
658 "Save the tutorial buffer to SAVED-FILE.
659 See `tutorial--save-tutorial' for more information."
660 ;; Anything to save?
661 (when (or (buffer-modified-p)
662 (< 1 (point)))
663 (let ((tutorial-dir (tutorial--saved-dir))
664 save-err)
665 ;; The tutorial is saved in a subdirectory in the user home
666 ;; directory. Create this subdirectory first.
667 (unless (file-directory-p tutorial-dir)
668 (condition-case err
669 (make-directory tutorial-dir nil)
670 (error (setq save-err t)
671 (warn "Could not create directory %s: %s" tutorial-dir
672 (error-message-string err)))))
673 ;; Make sure we have that directory.
674 (if (file-directory-p tutorial-dir)
675 (let ((tut-point (if (= 0 tutorial--point-after-chkeys)
676 ;; No info about changed keys is
677 ;; displayed.
678 (point)
679 (if (< (point) tutorial--point-after-chkeys)
680 (- (point))
681 (- (point) tutorial--point-after-chkeys))))
682 (old-point (point))
683 ;; Use a special undo list so that we easily can undo
684 ;; the changes we make to the tutorial buffer. This is
685 ;; currently not needed since we now delete the buffer
686 ;; after saving, but kept for possible future use of
687 ;; this function.
688 buffer-undo-list
689 (inhibit-read-only t))
690 ;; Delete the area displaying info about changed keys.
691 ;; (when (< 0 tutorial--point-after-chkeys)
692 ;; (delete-region tutorial--point-before-chkeys
693 ;; tutorial--point-after-chkeys))
694 ;; Delete the remarks:
695 (tutorial--remove-remarks)
696 ;; Put the value of point first in the buffer so it will
697 ;; be saved with the tutorial.
698 (goto-char (point-min))
699 (insert (number-to-string tut-point)
700 "\n"
701 (number-to-string (marker-position
702 tutorial--point-before-chkeys))
703 "\n")
704 (condition-case err
705 (write-region nil nil saved-file)
706 (error (setq save-err t)
707 (warn "Could not save tutorial to %s: %s"
708 saved-file
709 (error-message-string err))))
710 ;; An error is raised here?? Is this a bug?
711 (condition-case err
712 (undo-only)
713 (error nil))
714 ;; Restore point
715 (goto-char old-point)
716 (if save-err
717 (message "Could not save tutorial state.")
718 (message "Saved tutorial state.")))
719 (message "Can't save tutorial: %s is not a directory"
720 tutorial-dir)))))
723 ;;;###autoload
724 (defun help-with-tutorial (&optional arg dont-ask-for-revert)
725 "Select the Emacs learn-by-doing tutorial.
726 If there is a tutorial version written in the language
727 of the selected language environment, that version is used.
728 If there's no tutorial in that language, `TUTORIAL' is selected.
729 With ARG, you are asked to choose which language.
730 If DONT-ASK-FOR-REVERT is non-nil the buffer is reverted without
731 any question when restarting the tutorial.
733 If any of the standard Emacs key bindings that are used in the
734 tutorial have been changed then an explanatory note about this is
735 shown in the beginning of the tutorial buffer.
737 When the tutorial buffer is killed the content and the point
738 position in the buffer is saved so that the tutorial may be
739 resumed later."
740 (interactive "P")
741 (if (boundp 'viper-current-state)
742 (let ((prompt1
743 "You can not run the Emacs tutorial directly because you have \
744 enabled Viper.")
745 (prompt2 "\nThere is however a Viper tutorial you can run instead.
746 Run the Viper tutorial? "))
747 (if (fboundp 'viper-tutorial)
748 (if (y-or-n-p (concat prompt1 prompt2))
749 (progn (message "")
750 (funcall 'viper-tutorial 0))
751 (message "Tutorial aborted by user"))
752 (message prompt1)))
753 (let* ((lang (if arg
754 (let ((minibuffer-setup-hook minibuffer-setup-hook))
755 (add-hook 'minibuffer-setup-hook
756 'minibuffer-completion-help)
757 (read-language-name 'tutorial "Language: " "English"))
758 (if (get-language-info current-language-environment 'tutorial)
759 current-language-environment
760 "English")))
761 (filename (get-language-info lang 'tutorial))
762 ;; Choose a buffer name including the language so that
763 ;; several languages can be tested simultaneously:
764 (tut-buf-name (concat "TUTORIAL (" lang ")"))
765 (old-tut-buf (get-buffer tut-buf-name))
766 (old-tut-win (when old-tut-buf (get-buffer-window old-tut-buf t)))
767 (old-tut-is-ok (when old-tut-buf
768 (not (buffer-modified-p old-tut-buf))))
769 old-tut-file
770 (old-tut-point 1))
771 (setq tutorial--point-after-chkeys (point-min))
772 ;; Try to display the tutorial buffer before asking to revert it.
773 ;; If the tutorial buffer is shown in some window make sure it is
774 ;; selected and displayed:
775 (if old-tut-win
776 (raise-frame
777 (window-frame
778 (select-window (get-buffer-window old-tut-buf t))))
779 ;; Else, is there an old tutorial buffer? Then display it:
780 (when old-tut-buf
781 (switch-to-buffer old-tut-buf)))
782 ;; Use whole frame for tutorial
783 (delete-other-windows)
784 ;; If the tutorial buffer has been changed then ask if it should
785 ;; be reverted:
786 (when (and old-tut-buf
787 (not old-tut-is-ok))
788 (setq old-tut-is-ok
789 (if dont-ask-for-revert
791 (not (y-or-n-p
792 "You have changed the Tutorial buffer. Revert it? ")))))
793 ;; (Re)build the tutorial buffer if it is not ok
794 (unless old-tut-is-ok
795 (switch-to-buffer (get-buffer-create tut-buf-name))
796 (unless old-tut-buf (text-mode))
797 (unless lang (error "Variable lang is nil"))
798 (setq tutorial--lang lang)
799 (setq old-tut-file (file-exists-p (tutorial--saved-file)))
800 (let ((inhibit-read-only t))
801 (erase-buffer))
802 (message "Preparing tutorial ...") (sit-for 0)
804 ;; Do not associate the tutorial buffer with a file. Instead use
805 ;; a hook to save it when the buffer is killed.
806 (setq buffer-auto-save-file-name nil)
807 (add-hook 'kill-buffer-hook 'tutorial--save-tutorial nil t)
809 ;; Insert the tutorial. First offer to resume last tutorial
810 ;; editing session.
811 (when dont-ask-for-revert
812 (setq old-tut-file nil))
813 (when old-tut-file
814 (setq old-tut-file
815 (y-or-n-p "Resume your last saved tutorial? ")))
816 (if old-tut-file
817 (progn
818 (insert-file-contents (tutorial--saved-file))
819 (goto-char (point-min))
820 (setq old-tut-point
821 (string-to-number
822 (buffer-substring-no-properties
823 (line-beginning-position) (line-end-position))))
824 (forward-line)
825 (setq tutorial--point-before-chkeys
826 (string-to-number
827 (buffer-substring-no-properties
828 (line-beginning-position) (line-end-position))))
829 (forward-line)
830 (delete-region (point-min) (point))
831 (goto-char tutorial--point-before-chkeys)
832 (setq tutorial--point-before-chkeys (point-marker)))
833 (insert-file-contents (expand-file-name filename data-directory))
834 (forward-line)
835 (setq tutorial--point-before-chkeys (point-marker)))
837 (tutorial--display-changes)
839 ;; Clear message:
840 (unless dont-ask-for-revert
841 (message "") (sit-for 0))
844 (if old-tut-file
845 ;; Just move to old point in saved tutorial.
846 (let ((old-point
847 (if (> 0 old-tut-point)
848 (- old-tut-point)
849 (+ old-tut-point tutorial--point-after-chkeys))))
850 (when (< old-point 1)
851 (setq old-point 1))
852 (goto-char old-point))
853 (goto-char (point-min))
854 (search-forward "\n<<")
855 (beginning-of-line)
856 ;; Convert the <<...>> line to the proper [...] line,
857 ;; or just delete the <<...>> line if a [...] line follows.
858 (cond ((save-excursion
859 (forward-line 1)
860 (looking-at "\\["))
861 (delete-region (point) (progn (forward-line 1) (point))))
862 ((looking-at "<<Blank lines inserted.*>>")
863 (replace-match "[Middle of page left blank for didactic purposes. Text continues below]"))
865 (looking-at "<<")
866 (replace-match "[")
867 (search-forward ">>")
868 (replace-match "]")))
869 (beginning-of-line)
870 (let ((n (- (window-height (selected-window))
871 (count-lines (point-min) (point))
872 6)))
873 (if (< n 8)
874 (progn
875 ;; For a short gap, we don't need the [...] line,
876 ;; so delete it.
877 (delete-region (point) (progn (end-of-line) (point)))
878 (newline n))
879 ;; Some people get confused by the large gap.
880 (newline (/ n 2))
882 ;; Skip the [...] line (don't delete it).
883 (forward-line 1)
884 (newline (- n (/ n 2)))))
885 (goto-char (point-min)))
886 (setq buffer-undo-list nil)
887 (set-buffer-modified-p nil)))))
890 ;; Below is some attempt to handle language specific strings. These
891 ;; are currently only used in the tutorial.
893 (defconst lang-strings
894 '(("English" .
895 ((tut-chgdkey . "%s has been rebound, but you can use %s instead")
896 (tut-chgdkey2 . "More")
897 (tut-chgdhead . "
898 NOTICE: The main purpose of the Emacs tutorial is to teach you
899 the most important standard Emacs commands (key bindings).
900 However, your Emacs has been customized by changing some of
901 these basic editing commands, so it doesn't correspond to the
902 tutorial. We have inserted colored notices where the altered
903 commands have been introduced.")
904 (tut-chgdhead2 . "More"))))
905 "Language specific strings for Emacs.
906 This is an association list with the keys equal to the strings
907 that can be returned by `read-language-name'. The elements in
908 the list are themselves association lists with keys that are
909 string ids and values that are the language specific strings.
911 See `get-lang-string' for more information.")
913 (defun get-lang-string (lang stringid &optional no-eng-fallback)
914 "Get a language specific string for Emacs.
915 In certain places Emacs can replace a string showed to the user with a language specific string.
916 This function retrieves such strings.
918 LANG is the language specification. It should be one of those
919 strings that can be returned by `read-language-name'. STRINGID
920 is a symbol that specifies the string to retrieve.
922 If no string is found for STRINGID in the choosen language then
923 the English string is returned unless NO-ENG-FALLBACK is non-nil.
925 See `lang-strings' for more information.
927 Currently this feature is only used in `help-with-tutorial'."
928 (let ((my-lang-strings (assoc lang lang-strings))
929 (found-string))
930 (when my-lang-strings
931 (let ((entry (assoc stringid (cdr my-lang-strings))))
932 (when entry
933 (setq found-string (cdr entry)))))
934 ;; Fallback to English strings
935 (unless (or found-string
936 no-eng-fallback)
937 (setq found-string (get-lang-string "English" stringid t)))
938 found-string))
940 ;;(get-lang-string "English" 'tut-chgdkey)
942 (provide 'tutorial)
944 ;; arch-tag: c8e80aef-c3bb-4ffb-8af6-22171bf0c100
945 ;;; tutorial.el ends here