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