]> code.delx.au - gnu-emacs/blob - lisp/tutorial.el
Merged from emacs@sv.gnu.org
[gnu-emacs] / lisp / tutorial.el
1 ;;; tutorial.el --- tutorial for Emacs
2
3 ;; Copyright (C) 2006 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: help, internal
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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.
24
25 ;;; Commentary:
26
27 ;; Code for running the Emacs tutorial.
28
29 ;;; History:
30
31 ;; File was created 2006-09.
32
33 ;;; Code:
34
35 (require 'help-mode) ;; for function help-buffer
36
37 (defface tutorial-warning-face
38 '((t :inherit font-lock-warning-face))
39 "Face used to highlight warnings in the tutorial."
40 :group 'help)
41
42 (defvar tutorial--point-before-chkeys 0
43 "Point before display of key changes.")
44 (make-variable-buffer-local 'tutorial--point-before-chkeys)
45
46 (defvar tutorial--point-after-chkeys 0
47 "Point after display of key changes.")
48 (make-variable-buffer-local 'tutorial--point-after-chkeys)
49
50 (defvar tutorial--lang nil
51 "Tutorial language.")
52 (make-variable-buffer-local 'tutorial--lang)
53
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.
61
62 For `cua-mode' key bindings that try to combine CUA key bindings
63 with default Emacs bindings information about this is shown.
64
65 VALUE should have either of these formats:
66
67 \(cua-mode)
68 \(current-binding KEY-FUN DEF-FUN KEY WHERE)
69
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.
89
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.
95
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))
107 map
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 the key "
158 where
159 " to get the function `"
160 (format "%s" db)
161 "'.")))
162 (fill-region (point-min) (point)))))
163 (print-help-return-message))))
164
165 (defun tutorial--sort-keys (left right)
166 "Sort predicate for use with `tutorial--default-keys'.
167 This is a predicate function to `sort'.
168
169 The sorting is for presentation purpose only and is done on the
170 key sequence.
171
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:
176 (while (and x y
177 (listp x) (listp y)
178 (equal (car x) (car y)))
179 (setq x (cdr x))
180 (setq y (cdr y)))
181 ;; Try to make a comparision that is useful for presentation (this
182 ;; could be made nicer perhaps):
183 (let ((cx (car x))
184 (cy (car y)))
185 ;;(message "x=%s, y=%s;;;; cx=%s, cy=%s" x y cx cy)
186 (cond
187 ;; Lists? Then call this again
188 ((and cx cy
189 (listp cx)
190 (listp cy))
191 (tutorial--sort-keys cx cy))
192 ;; Are both numbers? Then just compare them
193 ((and (wholenump cx)
194 (wholenump cy))
195 (> cx cy))
196 ;; Is one of them a number? Let that be bigger then.
197 ((wholenump cx)
198 t)
199 ((wholenump cy)
200 nil)
201 ;; Are both symbols? Compare the names then.
202 ((and (symbolp cx)
203 (symbolp cy))
204 (string< (symbol-name cy)
205 (symbol-name cx)))))))
206
207 (defconst tutorial--default-keys
208 ;; On window system, `suspend-emacs' is replaced in the default
209 ;; keymap
210 (let* ((suspend-emacs (if window-system
211 'iconify-or-deiconify-frame
212 'suspend-emacs))
213 (default-keys
214 `((ESC-prefix [27])
215 (Control-X-prefix [?\C-x])
216 (mode-specific-command-prefix [?\C-c])
217 (save-buffers-kill-emacs [?\C-x ?\C-c])
218
219 ;; * SUMMARY
220 (scroll-up [?\C-v])
221 (scroll-down [?\M-v])
222 (recenter [?\C-l])
223
224 ;; * BASIC CURSOR CONTROL
225 (forward-char [?\C-f])
226 (backward-char [?\C-b])
227 (forward-word [?\M-f])
228 (backward-word [?\M-b])
229 (next-line [?\C-n])
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])
235 (newline "\r")
236 (beginning-of-buffer [?\M-<])
237 (end-of-buffer [?\M->])
238 (universal-argument [?\C-u])
239
240 ;; * WHEN EMACS IS HUNG
241 (keyboard-quit [?\C-g])
242
243 ;; * DISABLED COMMANDS
244 (downcase-region [?\C-x ?\C-l])
245
246 ;; * WINDOWS
247 (delete-other-windows [?\C-x ?1])
248 ;; C-u 0 C-l
249 ;; Type CONTROL-h k CONTROL-f.
250
251 ;; * INSERTING AND DELETING
252 ;; C-u 8 * to insert ********.
253 (delete-backward-char [backspace])
254 (delete-backward-char "\d")
255 (delete-char [?\C-d])
256 (backward-kill-word [(meta backspace)])
257 (kill-word [?\M-d])
258 (kill-line [?\C-k])
259 (kill-sentence [?\M-k])
260 (set-mark-command [?\C-@])
261 (set-mark-command [?\C- ])
262 (kill-region [?\C-w])
263 (yank [?\C-y])
264 (yank-pop [?\M-y])
265
266 ;; * UNDO
267 (advertised-undo [?\C-x ?u])
268 (advertised-undo [?\C-x ?u])
269
270 ;; * FILES
271 (find-file [?\C-x ?\C-f])
272 (save-buffer [?\C-x ?\C-s])
273
274 ;; * BUFFERS
275 (list-buffers [?\C-x ?\C-b])
276 (switch-to-buffer [?\C-x ?b])
277 (save-some-buffers [?\C-x ?s])
278
279 ;; * EXTENDING THE COMMAND SET
280 ;; C-x Character eXtend. Followed by one character.
281 (execute-extended-command [?\M-x])
282 ;; C-x C-f Find file
283 ;; C-x C-s Save file
284 ;; C-x s Save some buffers
285 ;; C-x C-b List buffers
286 ;; C-x b Switch buffer
287 ;; C-x C-c Quit Emacs
288 ;; C-x 1 Delete all but one window
289 ;; C-x u Undo
290
291 ;; * MODE LINE
292 (describe-mode [?\C-h ?m])
293 (set-fill-column [?\C-x ?f])
294 (fill-paragraph [?\M-q])
295
296 ;; * SEARCHING
297 (isearch-forward [?\C-s])
298 (isearch-backward [?\C-r])
299
300 ;; * MULTIPLE WINDOWS
301 (split-window-vertically [?\C-x ?2])
302 (scroll-other-window [?\C-\M-v])
303 (other-window [?\C-x ?o])
304 (find-file-other-window [?\C-x ?4 ?\C-f])
305
306 ;; * RECURSIVE EDITING LEVELS
307 (keyboard-escape-quit [27 27 27])
308
309 ;; * GETTING MORE HELP
310 ;; The most basic HELP feature is C-h c
311 (describe-key-briefly [?\C-h ?c])
312 (describe-key [?\C-h ?k])
313
314 ;; * MORE FEATURES
315 ;; F10
316
317 ;; * CONCLUSION
318 ;;(iconify-or-deiconify-frame [?\C-z])
319 (,suspend-emacs [?\C-z]))))
320 (sort default-keys 'tutorial--sort-keys))
321 "Default Emacs key bindings that the tutorial depends on.")
322
323 (defun tutorial--detailed-help (button)
324 "Give detailed help about changed keys."
325 (with-output-to-temp-buffer (help-buffer)
326 (help-setup-xref (list #'tutorial--detailed-help button)
327 (interactive-p))
328 (with-current-buffer (help-buffer)
329 (let* ((tutorial-buffer (button-get button 'tutorial-buffer))
330 (explain-key-desc (button-get button 'explain-key-desc))
331 (changed-keys (with-current-buffer tutorial-buffer
332 (save-excursion
333 (goto-char (point-min))
334 (tutorial--find-changed-keys
335 tutorial--default-keys)))))
336 (when changed-keys
337 (insert
338 "The following key bindings used in the tutorial had been changed
339 from the Emacs default in the " (buffer-name tutorial-buffer) " buffer:\n\n" )
340 (let ((frm " %-9s %-27s %-11s %s\n"))
341 (insert (format frm "Key" "Standard Binding" "Is Now On" "Remark")))
342 (dolist (tk changed-keys)
343 (let* ((def-fun (nth 1 tk))
344 (key (nth 0 tk))
345 (def-fun-txt (nth 2 tk))
346 (where (nth 3 tk))
347 (remark (nth 4 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)))
351 tot-len)
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 " ")
358 (setq tot-len (length key-txt))
359 (when (> 9 tot-len)
360 (insert (make-string (- 9 tot-len) ?\s))
361 (setq tot-len 9))
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 (setq tot-len (+ tot-len (length def-fun-txt)))
371 (when (> 36 tot-len)
372 (insert (make-string (- 36 tot-len) ?\s)))
373 (when (listp where)
374 (setq where "list"))
375 ;; Tell where the old binding is now:
376 (insert (format " %-11s "
377 (if (string= "" where)
378 (format "M-x %s" def-fun-txt)
379 where)))
380 ;; Insert a link with more information, for example
381 ;; current binding and keymap or information about
382 ;; cua-mode replacements:
383 (insert-button (car remark)
384 'action
385 (lambda(b) (interactive)
386 (let ((value (button-get b 'value)))
387 (tutorial--describe-nonstandard-key value)))
388 'value (cdr remark)
389 'follow-link t)
390 (insert "\n")))))
391
392 (insert "
393 It is OK to change key bindings, but changed bindings do not
394 correspond to what the tutorial says.\n\n")
395 (print-help-return-message)))))
396
397 (defun tutorial--find-changed-keys (default-keys)
398 "Find the key bindings used in the tutorial that have changed.
399 Return a list with elements of the form
400
401 '(KEY DEF-FUN DEF-FUN-TXT WHERE REMARK QUIET)
402
403 where
404
405 KEY is a key sequence whose standard binding has been changed
406 DEF-FUN is the standard binding of KEY
407 DEF-FUN-TXT is a short descriptive text for DEF-FUN
408 WHERE is a text describing the key sequences to which DEF-FUN is
409 bound now (or, if it is remapped, a key sequence
410 for the function it is remapped to)
411 REMARK is a list with info about rebinding. It has either of these
412 formats:
413
414 \(TEXT cua-mode)
415 \(TEXT current-binding KEY-FUN DEF-FUN KEY WHERE)
416
417 Here TEXT is a link text to show to the user. The
418 rest of the list is used to show information when
419 the user clicks the link.
420
421 KEY-FUN is the actual binding for KEY.
422 QUIET is t if this changed keybinding should be handled quietly.
423 This is used by `tutorial--display-changes'."
424 (let (changed-keys remark)
425 (dolist (kdf default-keys)
426 ;; The variables below corresponds to those with the same names
427 ;; described in the doc string.
428 (let* ((key (nth 1 kdf))
429 (def-fun (nth 0 kdf))
430 (def-fun-txt (format "%s" def-fun))
431 (rem-fun (command-remapping def-fun))
432 (key-fun (if (eq def-fun 'ESC-prefix)
433 (lookup-key global-map [27])
434 (key-binding key)))
435 (where (where-is-internal (if rem-fun rem-fun def-fun))))
436 (if where
437 (progn
438 (setq where (key-description (car where)))
439 (when (and (< 10 (length where))
440 (string= (substring where 0 (length "<menu-bar>"))
441 "<menu-bar>"))
442 (setq where "the menus")))
443 (setq where ""))
444 (setq remark nil)
445 (unless
446 (cond ((eq key-fun def-fun)
447 ;; No rebinding, return t
448 t)
449 ((and key-fun
450 (eq key-fun (command-remapping def-fun)))
451 ;; Just a remapping, return t
452 t)
453 ;; cua-mode specials:
454 ((and cua-mode
455 (or (and
456 (equal key [?\C-v])
457 (eq key-fun 'cua-paste))
458 (and
459 (equal key [?\C-z])
460 (eq key-fun 'undo))))
461 (setq remark (list "cua-mode, more info" 'cua-mode))
462 nil)
463 ((and cua-mode
464 (or (and (eq def-fun 'ESC-prefix)
465 (equal key-fun
466 `(keymap
467 (118 . cua-repeat-replace-region)))
468 (setq def-fun-txt "\"ESC prefix\""))
469 (and (eq def-fun 'mode-specific-command-prefix)
470 (equal key-fun
471 '(keymap
472 (timeout . copy-region-as-kill)))
473 (setq def-fun-txt "\"C-c prefix\""))
474 (and (eq def-fun 'Control-X-prefix)
475 (equal key-fun
476 '(keymap (timeout . kill-region)))
477 (setq def-fun-txt "\"C-x prefix\""))))
478 (setq remark (list "cua-mode replacement" 'cua-mode))
479 (setq where "Same key")
480 nil)
481 ;; viper-mode specials:
482 ((and (boundp 'viper-mode-string)
483 (boundp 'viper-current-state)
484 (eq viper-current-state 'vi-state)
485 (or (and (eq def-fun 'isearch-forward)
486 (eq key-fun 'viper-isearch-forward))
487 (and (eq def-fun 'isearch-backward)
488 (eq key-fun 'viper-isearch-backward))))
489 ;; These bindings works as the default bindings,
490 ;; return t
491 t)
492 ((when normal-erase-is-backspace
493 (or (and (equal key [C-delete])
494 (equal key-fun 'kill-word))
495 (and (equal key [C-backspace])
496 (equal key-fun 'backward-kill-word))))
497 ;; This is the strange handling of C-delete and
498 ;; C-backspace, return t
499 t)
500 (t
501 ;; This key has indeed been rebound. Put information
502 ;; in `remark' and return nil
503 (setq remark
504 (list "more info" 'current-binding
505 key-fun def-fun key where))
506 nil))
507 (add-to-list 'changed-keys
508 (list key def-fun def-fun-txt where remark nil)))))
509 changed-keys))
510
511 (defun tutorial--key-description (key)
512 (let ((desc (key-description key)))
513 (cond ((string= "ESC" desc) "<ESC>")
514 ((string= "RET" desc) "<Return>")
515 ((string= "DEL" desc) "<Delback>")
516 (t desc))))
517
518 (defun tutorial--display-changes ()
519 "Display changes to some default key bindings.
520 If some of the default key bindings that the tutorial depends on
521 have been changed then display the changes in the tutorial buffer
522 with some explanatory links."
523 (let* ((changed-keys (tutorial--find-changed-keys
524 tutorial--default-keys))
525 ;; Alist of element (DESC . CK) where DESC is the
526 ;; key-description of a changed key and CK is the
527 ;; corresponding element in `changed-keys'.
528 (changed-keys-alist
529 (mapcar (lambda (ck) (cons (tutorial--key-description (car ck)) ck))
530 changed-keys))
531 changed-key
532 (start (point))
533 (case-fold-search nil)
534 (keybindings-regexp
535 (concat "[[:space:]]\\("
536 (mapconcat (lambda (kdf) (regexp-quote
537 (tutorial--key-description
538 (nth 1 kdf))))
539 tutorial--default-keys
540 "\\|")
541 "\\)[[:punct:][:space:]]")))
542 ;; Need the custom button face for viper buttons:
543 (if (boundp 'viper-mode-string) (require 'cus-edit))
544
545 (if (or changed-keys (boundp 'viper-mode-string))
546 (let ((head (get-lang-string tutorial--lang 'tut-chgdhead))
547 (head2 (get-lang-string tutorial--lang 'tut-chgdhead2)))
548 (when (and head head2)
549 (goto-char tutorial--point-before-chkeys)
550 (insert head " [")
551 (insert-button head2 'tutorial-buffer (current-buffer)
552 'action 'tutorial--detailed-help
553 'follow-link t 'face 'link)
554 (insert "]\n\n")
555 (add-text-properties tutorial--point-before-chkeys (point)
556 '(tutorial-remark remark
557 face tutorial-warning-face
558 read-only t)))))
559
560 ;; Scan the tutorial for all key sequences.
561 (goto-char (point-min))
562 (while (re-search-forward keybindings-regexp (point-max) t)
563 ;; Then highlight each rebound key sequence.
564 ;; This avoids issuing a warning for, e.g., C-x C-b if C-b is rebound.
565 (setq changed-key (assoc (match-string 1) changed-keys-alist))
566 (and changed-key
567 (not (get-text-property (match-beginning 1) 'tutorial-remark))
568 (let* ((desc (car changed-key))
569 (ck (cdr changed-key))
570 (key (nth 0 ck))
571 (def-fun (nth 1 ck))
572 (where (nth 3 ck))
573 s1 s2 help-string)
574 (unless (string= where "Same key")
575 (when (string= where "")
576 (setq where (format "M-x %s" def-fun)))
577 (setq tutorial--point-after-chkeys (point-marker)
578 s1 (get-lang-string tutorial--lang 'tut-chgdkey)
579 s2 (get-lang-string tutorial--lang 'tut-chgdkey2)
580 help-string (and s1 s2 (format s1 desc where)))
581 (add-text-properties (match-beginning 1) (match-end 1)
582 '(face tutorial-warning-face
583 tutorial-remark key-sequence))
584 (if help-string
585 (if (nth 5 ck)
586 ;; Put help string in the tooltip.
587 (put-text-property (match-beginning 1) (match-end 1)
588 'help-echo help-string)
589 ;; Put help string in the buffer.
590 (save-excursion
591 (setcar (nthcdr 5 ck) t)
592 (forward-line)
593 ;; Two or more changed keys were on the same line.
594 (while (eq (get-text-property (point) 'tutorial-remark)
595 'remark)
596 (forward-line))
597 (setq start (point))
598 (insert "** " help-string " [")
599 (insert-button s2 'tutorial-buffer (current-buffer)
600 'action 'tutorial--detailed-help
601 'explain-key-desc desc 'follow-link t
602 'face 'link)
603 (insert "] **\n")
604 (add-text-properties start (point)
605 '(tutorial-remark remark
606 rear-nonsticky t
607 face tutorial-warning-face
608 read-only t)))))))))))
609
610 (defun tutorial--saved-dir ()
611 "Directory to which tutorials are saved."
612 (expand-file-name "tutorial"
613 (if (eq system-type 'ms-dos) "~/_emacs.d/" "~/.emacs.d/")))
614
615 (defun tutorial--saved-file ()
616 "File name in which to save tutorials."
617 (let ((file-name tutorial--lang)
618 (ext (file-name-extension tutorial--lang)))
619 (when (or (not ext)
620 (string= ext ""))
621 (setq file-name (concat file-name ".tut")))
622 (expand-file-name file-name (tutorial--saved-dir))))
623
624 (defun tutorial--remove-remarks()
625 "Remove the remark lines that was added to the tutorial buffer."
626 (save-excursion
627 (goto-char (point-min))
628 (let (prop-start
629 prop-end
630 prop-val)
631 ;; Catch the case when we already are on a remark line
632 (while (if (get-text-property (point) 'tutorial-remark)
633 (setq prop-start (point))
634 (setq prop-start (next-single-property-change (point) 'tutorial-remark)))
635 (setq prop-end (next-single-property-change prop-start 'tutorial-remark))
636 (setq prop-val (get-text-property prop-start 'tutorial-remark))
637 (unless prop-end
638 (setq prop-end (point-max)))
639 (goto-char prop-end)
640 (unless (eq prop-val 'key-sequence)
641 (delete-region prop-start prop-end))))))
642
643 (defun tutorial--save-tutorial ()
644 "Save the tutorial buffer.
645 This saves the part of the tutorial before and after the area
646 showing changed keys. It also saves the point position and the
647 position where the display of changed bindings was inserted."
648 ;; This runs in a hook so protect it:
649 (condition-case err
650 (if (y-or-n-p "Save your position in the tutorial? ")
651 (tutorial--save-tutorial-to (tutorial--saved-file)))
652 (error (message "Error saving tutorial state: %s"
653 (error-message-string err)))))
654
655 (defun tutorial--save-tutorial-to (saved-file)
656 "Save the tutorial buffer to SAVED-FILE.
657 See `tutorial--save-tutorial' for more information."
658 ;; Anything to save?
659 (when (or (buffer-modified-p)
660 (< 1 (point)))
661 (let ((tutorial-dir (tutorial--saved-dir))
662 save-err)
663 ;; The tutorial is saved in a subdirectory in the user home
664 ;; directory. Create this subdirectory first.
665 (unless (file-directory-p tutorial-dir)
666 (condition-case err
667 (make-directory tutorial-dir nil)
668 (error (setq save-err t)
669 (warn "Could not create directory %s: %s" tutorial-dir
670 (error-message-string err)))))
671 ;; Make sure we have that directory.
672 (if (file-directory-p tutorial-dir)
673 (let ((tut-point (if (= 0 tutorial--point-after-chkeys)
674 ;; No info about changed keys is
675 ;; displayed.
676 (point)
677 (if (< (point) tutorial--point-after-chkeys)
678 (- (point))
679 (- (point) tutorial--point-after-chkeys))))
680 (old-point (point))
681 ;; Use a special undo list so that we easily can undo
682 ;; the changes we make to the tutorial buffer. This is
683 ;; currently not needed since we now delete the buffer
684 ;; after saving, but kept for possible future use of
685 ;; this function.
686 buffer-undo-list
687 (inhibit-read-only t))
688 ;; Delete the area displaying info about changed keys.
689 ;; (when (< 0 tutorial--point-after-chkeys)
690 ;; (delete-region tutorial--point-before-chkeys
691 ;; tutorial--point-after-chkeys))
692 ;; Delete the remarks:
693 (tutorial--remove-remarks)
694 ;; Put the value of point first in the buffer so it will
695 ;; be saved with the tutorial.
696 (goto-char (point-min))
697 (insert (number-to-string tut-point)
698 "\n"
699 (number-to-string (marker-position
700 tutorial--point-before-chkeys))
701 "\n")
702 (condition-case err
703 (write-region nil nil saved-file)
704 (error (setq save-err t)
705 (warn "Could not save tutorial to %s: %s"
706 saved-file
707 (error-message-string err))))
708 ;; An error is raised here?? Is this a bug?
709 (condition-case err
710 (undo-only)
711 (error nil))
712 ;; Restore point
713 (goto-char old-point)
714 (if save-err
715 (message "Could not save tutorial state.")
716 (message "Saved tutorial state.")))
717 (message "Can't save tutorial: %s is not a directory"
718 tutorial-dir)))))
719
720
721 ;;;###autoload
722 (defun help-with-tutorial (&optional arg dont-ask-for-revert)
723 "Select the Emacs learn-by-doing tutorial.
724 If there is a tutorial version written in the language
725 of the selected language environment, that version is used.
726 If there's no tutorial in that language, `TUTORIAL' is selected.
727 With ARG, you are asked to choose which language.
728 If DONT-ASK-FOR-REVERT is non-nil the buffer is reverted without
729 any question when restarting the tutorial.
730
731 If any of the standard Emacs key bindings that are used in the
732 tutorial have been changed then an explanatory note about this is
733 shown in the beginning of the tutorial buffer.
734
735 When the tutorial buffer is killed the content and the point
736 position in the buffer is saved so that the tutorial may be
737 resumed later."
738 (interactive "P")
739 (if (boundp 'viper-current-state)
740 (let ((prompt1
741 "You can not run the Emacs tutorial directly because you have \
742 enabled Viper.")
743 (prompt2 "\nThere is however a Viper tutorial you can run instead.
744 Run the Viper tutorial? "))
745 (if (fboundp 'viper-tutorial)
746 (if (y-or-n-p (concat prompt1 prompt2))
747 (progn (message "")
748 (funcall 'viper-tutorial 0))
749 (message "Tutorial aborted by user"))
750 (message prompt1)))
751 (let* ((lang (if arg
752 (let ((minibuffer-setup-hook minibuffer-setup-hook))
753 (add-hook 'minibuffer-setup-hook
754 'minibuffer-completion-help)
755 (read-language-name 'tutorial "Language: " "English"))
756 (if (get-language-info current-language-environment 'tutorial)
757 current-language-environment
758 "English")))
759 (filename (get-language-info lang 'tutorial))
760 ;; Choose a buffer name including the language so that
761 ;; several languages can be tested simultaneously:
762 (tut-buf-name (concat "TUTORIAL (" lang ")"))
763 (old-tut-buf (get-buffer tut-buf-name))
764 (old-tut-win (when old-tut-buf (get-buffer-window old-tut-buf t)))
765 (old-tut-is-ok (when old-tut-buf
766 (not (buffer-modified-p old-tut-buf))))
767 old-tut-file
768 (old-tut-point 1))
769 (setq tutorial--point-after-chkeys (point-min))
770 ;; Try to display the tutorial buffer before asking to revert it.
771 ;; If the tutorial buffer is shown in some window make sure it is
772 ;; selected and displayed:
773 (if old-tut-win
774 (raise-frame
775 (window-frame
776 (select-window (get-buffer-window old-tut-buf t))))
777 ;; Else, is there an old tutorial buffer? Then display it:
778 (when old-tut-buf
779 (switch-to-buffer old-tut-buf)))
780 ;; Use whole frame for tutorial
781 (delete-other-windows)
782 ;; If the tutorial buffer has been changed then ask if it should
783 ;; be reverted:
784 (when (and old-tut-buf
785 (not old-tut-is-ok))
786 (setq old-tut-is-ok
787 (if dont-ask-for-revert
788 nil
789 (not (y-or-n-p
790 "You have changed the Tutorial buffer. Revert it? ")))))
791 ;; (Re)build the tutorial buffer if it is not ok
792 (unless old-tut-is-ok
793 (switch-to-buffer (get-buffer-create tut-buf-name))
794 (unless old-tut-buf (text-mode))
795 (unless lang (error "Variable lang is nil"))
796 (setq tutorial--lang lang)
797 (setq old-tut-file (file-exists-p (tutorial--saved-file)))
798 (let ((inhibit-read-only t))
799 (erase-buffer))
800 (message "Preparing tutorial ...") (sit-for 0)
801
802 ;; Do not associate the tutorial buffer with a file. Instead use
803 ;; a hook to save it when the buffer is killed.
804 (setq buffer-auto-save-file-name nil)
805 (add-hook 'kill-buffer-hook 'tutorial--save-tutorial nil t)
806
807 ;; Insert the tutorial. First offer to resume last tutorial
808 ;; editing session.
809 (when dont-ask-for-revert
810 (setq old-tut-file nil))
811 (when old-tut-file
812 (setq old-tut-file
813 (y-or-n-p "Resume your last saved tutorial? ")))
814 (if old-tut-file
815 (progn
816 (insert-file-contents (tutorial--saved-file))
817 (goto-char (point-min))
818 (setq old-tut-point
819 (string-to-number
820 (buffer-substring-no-properties
821 (line-beginning-position) (line-end-position))))
822 (forward-line)
823 (setq tutorial--point-before-chkeys
824 (string-to-number
825 (buffer-substring-no-properties
826 (line-beginning-position) (line-end-position))))
827 (forward-line)
828 (delete-region (point-min) (point))
829 (goto-char tutorial--point-before-chkeys)
830 (setq tutorial--point-before-chkeys (point-marker)))
831 (insert-file-contents (expand-file-name filename data-directory))
832 (forward-line)
833 (setq tutorial--point-before-chkeys (point-marker)))
834
835 (tutorial--display-changes)
836
837 ;; Clear message:
838 (unless dont-ask-for-revert
839 (message "") (sit-for 0))
840
841
842 (if old-tut-file
843 ;; Just move to old point in saved tutorial.
844 (let ((old-point
845 (if (> 0 old-tut-point)
846 (- old-tut-point)
847 (+ old-tut-point tutorial--point-after-chkeys))))
848 (when (< old-point 1)
849 (setq old-point 1))
850 (goto-char old-point))
851 (goto-char (point-min))
852 (search-forward "\n<<")
853 (beginning-of-line)
854 ;; Convert the <<...>> line to the proper [...] line,
855 ;; or just delete the <<...>> line if a [...] line follows.
856 (cond ((save-excursion
857 (forward-line 1)
858 (looking-at "\\["))
859 (delete-region (point) (progn (forward-line 1) (point))))
860 ((looking-at "<<Blank lines inserted.*>>")
861 (replace-match "[Middle of page left blank for didactic purposes. Text continues below]"))
862 (t
863 (looking-at "<<")
864 (replace-match "[")
865 (search-forward ">>")
866 (replace-match "]")))
867 (beginning-of-line)
868 (let ((n (- (window-height (selected-window))
869 (count-lines (point-min) (point))
870 6)))
871 (if (< n 8)
872 (progn
873 ;; For a short gap, we don't need the [...] line,
874 ;; so delete it.
875 (delete-region (point) (progn (end-of-line) (point)))
876 (newline n))
877 ;; Some people get confused by the large gap.
878 (newline (/ n 2))
879
880 ;; Skip the [...] line (don't delete it).
881 (forward-line 1)
882 (newline (- n (/ n 2)))))
883 (goto-char (point-min)))
884 (setq buffer-undo-list nil)
885 (set-buffer-modified-p nil)))))
886
887
888 ;; Below is some attempt to handle language specific strings. These
889 ;; are currently only used in the tutorial.
890
891 (defconst lang-strings
892 '(("English" .
893 ((tut-chgdkey . "%s has been rebound, but you can use %s instead")
894 (tut-chgdkey2 . "More")
895 (tut-chgdhead . "
896 NOTICE: The main purpose of the Emacs tutorial is to teach you
897 the most important standard Emacs commands (key bindings).
898 However, your Emacs has been customized by changing some of
899 these basic editing commands, so it doesn't correspond to the
900 tutorial. We have inserted colored notices where the altered
901 commands have been introduced.")
902 (tut-chgdhead2 . "More"))))
903 "Language specific strings for Emacs.
904 This is an association list with the keys equal to the strings
905 that can be returned by `read-language-name'. The elements in
906 the list are themselves association lists with keys that are
907 string ids and values that are the language specific strings.
908
909 See `get-lang-string' for more information.")
910
911 (defun get-lang-string(lang stringid &optional no-eng-fallback)
912 "Get a language specific string for Emacs.
913 In certain places Emacs can replace a string showed to the user with a language specific string.
914 This function retrieves such strings.
915
916 LANG is the language specification. It should be one of those
917 strings that can be returned by `read-language-name'. STRINGID
918 is a symbol that specifies the string to retrieve.
919
920 If no string is found for STRINGID in the choosen language then
921 the English string is returned unless NO-ENG-FALLBACK is non-nil.
922
923 See `lang-strings' for more information.
924
925 Currently this feature is only used in `help-with-tutorial'."
926 (let ((my-lang-strings (assoc lang lang-strings))
927 (found-string))
928 (when my-lang-strings
929 (let ((entry (assoc stringid (cdr my-lang-strings))))
930 (when entry
931 (setq found-string (cdr entry)))))
932 ;; Fallback to English strings
933 (unless (or found-string
934 no-eng-fallback)
935 (setq found-string (get-lang-string "English" stringid t)))
936 found-string))
937
938 ;;(get-lang-string "English" 'tut-chgdkey)
939
940 (provide 'tutorial)
941
942 ;; arch-tag: c8e80aef-c3bb-4ffb-8af6-22171bf0c100
943 ;;; tutorial.el ends here