]> code.delx.au - gnu-emacs/blob - lisp/tutorial.el
9fea316281084fe8259b098f0c7762ffdcc8dc3d
[gnu-emacs] / lisp / tutorial.el
1 ;;; tutorial.el --- tutorial for Emacs
2
3 ;; Copyright (C) 2006-2015 Free Software Foundation, Inc.
4
5 ;; Maintainer: emacs-devel@gnu.org
6 ;; Keywords: help, internal
7 ;; Package: emacs
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; Code for running the Emacs tutorial.
27
28 ;;; History:
29
30 ;; File was created 2006-09.
31
32 ;;; Code:
33
34 (require 'help-mode) ;; for function help-buffer
35
36 (defface tutorial-warning-face
37 '((t :inherit font-lock-warning-face))
38 "Face used to highlight warnings in the tutorial."
39 :group 'help)
40
41 (defvar tutorial--point-before-chkeys 0
42 "Point before display of key changes.")
43 (make-variable-buffer-local 'tutorial--point-before-chkeys)
44
45 (defvar tutorial--point-after-chkeys 0
46 "Point after display of key changes.")
47 (make-variable-buffer-local 'tutorial--point-after-chkeys)
48
49 (defvar tutorial--lang nil
50 "Tutorial language.")
51 (make-variable-buffer-local 'tutorial--lang)
52
53 (defun tutorial--describe-nonstandard-key (value)
54 "Give more information about a changed key binding.
55 This is used in `help-with-tutorial'. The information includes
56 the key sequence that no longer has a default binding, the
57 default binding and the current binding. It also tells in what
58 keymap the new binding has been done and how to access the
59 function in the default binding from the keyboard.
60
61 For `cua-mode' key bindings that try to combine CUA key bindings
62 with default Emacs bindings information about this is shown.
63
64 VALUE should have either of these formats:
65
66 \(cua-mode)
67 \(current-binding KEY-FUN DEF-FUN KEY WHERE)
68
69 Where
70 KEY is a key sequence whose standard binding has been changed
71 KEY-FUN is the actual binding for KEY
72 DEF-FUN is the standard binding of KEY
73 WHERE is a text describing the key sequences to which DEF-FUN is
74 bound now (or, if it is remapped, a key sequence
75 for the function it is remapped to)"
76 (with-output-to-temp-buffer (help-buffer)
77 (help-setup-xref (list #'tutorial--describe-nonstandard-key value)
78 (called-interactively-p 'interactive))
79 (with-current-buffer (help-buffer)
80 (insert
81 "Your Emacs customizations override the default binding for this key:"
82 "\n\n")
83 (let ((inhibit-read-only t))
84 (cond
85 ((eq (car value) 'cua-mode)
86 (insert
87 "CUA mode is enabled.
88
89 When CUA mode is enabled, you can use C-z, C-x, C-c, and C-v to
90 undo, cut, copy, and paste in addition to the normal Emacs
91 bindings. The C-x and C-c keys only do cut and copy when the
92 region is active, so in most cases, they do not conflict with the
93 normal function of these prefix keys.
94
95 If you really need to perform a command which starts with one of
96 the prefix keys even when the region is active, you have three
97 options:
98 - press the prefix key twice very quickly (within 0.2 seconds),
99 - press the prefix key and the following key within 0.2 seconds, or
100 - use the SHIFT key with the prefix key, i.e. C-S-x or C-S-c."))
101 ((eq (car value) 'current-binding)
102 (let ((cb (nth 1 value))
103 (db (nth 2 value))
104 (key (nth 3 value))
105 (where (nth 4 value))
106 map
107 (maps (current-active-maps))
108 mapsym)
109 ;; Look at the currently active keymaps and try to find
110 ;; first the keymap where the current binding occurs:
111 (while maps
112 (let* ((m (car maps))
113 (mb (lookup-key m key t)))
114 (setq maps (cdr maps))
115 (when (eq mb cb)
116 (setq map m)
117 (setq maps nil))))
118 ;; Now, if a keymap was found we must found the symbol
119 ;; name for it to display to the user. This can not
120 ;; always be found since all keymaps does not have a
121 ;; symbol pointing to them, but here they should have
122 ;; that:
123 (when map
124 (mapatoms (lambda (s)
125 (and
126 ;; If not already found
127 (not mapsym)
128 ;; and if s is a keymap
129 (and (boundp s)
130 (keymapp (symbol-value s)))
131 ;; and not the local symbol map
132 (not (eq s 'map))
133 ;; and the value of s is map
134 (eq map (symbol-value s))
135 ;; then save this value in mapsym
136 (setq mapsym s)))))
137 (insert
138 (format
139 "The default Emacs binding for the key %s is the command ‘%s’. "
140 (key-description key)
141 db))
142 (insert "However, your customizations have "
143 (if cb
144 (format "rebound it to the command ‘%s’" cb)
145 "unbound it"))
146 (insert ".")
147 (when mapsym
148 (insert " (For the more advanced user:"
149 (format " This binding is in the keymap ‘%s’.)" mapsym)))
150 (if (string= where "")
151 (unless (keymapp db)
152 (insert "\n\nYou can use M-x "
153 (format "%s" db)
154 " RET instead."))
155 (insert "\n\nWith your current key bindings"
156 " you can use "
157 (if (string-match-p "^the .*menus?$" where)
158 ""
159 "the key")
160 where
161 (format " to get the function ‘%s’." db))))
162 (fill-region (point-min) (point)))))
163 (help-print-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 comparison 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 keymap.
209 (let* ((suspend-emacs 'suspend-frame)
210 (default-keys
211 ;; The first few are not mentioned but are basic:
212 `((ESC-prefix [27])
213 (Control-X-prefix [?\C-x])
214 (mode-specific-command-prefix [?\C-c])
215 (save-buffers-kill-terminal [?\C-x ?\C-c])
216
217 ;; * SUMMARY
218 (scroll-up-command [?\C-v])
219 (scroll-down-command [?\M-v])
220 (recenter-top-bottom [?\C-l])
221
222 ;; * BASIC CURSOR CONTROL
223 (forward-char [?\C-f])
224 (backward-char [?\C-b])
225 (forward-word [?\M-f])
226 (backward-word [?\M-b])
227 (next-line [?\C-n])
228 (previous-line [?\C-p])
229 (move-beginning-of-line [?\C-a])
230 (move-end-of-line [?\C-e])
231 (backward-sentence [?\M-a])
232 (forward-sentence [?\M-e])
233 (newline "\r")
234 (beginning-of-buffer [?\M-<])
235 (end-of-buffer [?\M->])
236 (universal-argument [?\C-u])
237
238 ;; * WHEN EMACS IS HUNG
239 (keyboard-quit [?\C-g])
240
241 ;; * DISABLED COMMANDS
242 (downcase-region [?\C-x ?\C-l])
243
244 ;; * WINDOWS
245 (delete-other-windows [?\C-x ?1])
246 ;; C-u 0 C-l
247 ;; Type CONTROL-h k CONTROL-f.
248
249 ;; * INSERTING AND DELETING
250 ;; C-u 8 * to insert ********.
251 (delete-backward-char "\d")
252 (delete-char [?\C-d])
253 (backward-kill-word [?\M-\d])
254 (kill-word [?\M-d])
255 (kill-line [?\C-k])
256 (kill-sentence [?\M-k])
257 (set-mark-command [?\C-@])
258 (set-mark-command [?\C- ])
259 (kill-region [?\C-w])
260 (yank [?\C-y])
261 (yank-pop [?\M-y])
262
263 ;; * UNDO
264 (undo [?\C-x ?u])
265
266 ;; * FILES
267 (find-file [?\C-x ?\C-f])
268 (save-buffer [?\C-x ?\C-s])
269
270 ;; * BUFFERS
271 (list-buffers [?\C-x ?\C-b])
272 (switch-to-buffer [?\C-x ?b])
273 (save-some-buffers [?\C-x ?s])
274
275 ;; * EXTENDING THE COMMAND SET
276 ;; C-x Character eXtend. Followed by one character.
277 (execute-extended-command [?\M-x])
278 ;; C-x C-f Find file
279 ;; C-x C-s Save file
280 ;; C-x s Save some buffers
281 ;; C-x C-b List buffers
282 ;; C-x b Switch buffer
283 ;; C-x C-c Quit Emacs
284 ;; C-x 1 Delete all but one window
285 ;; C-x u Undo
286
287 ;; * MODE LINE
288 (describe-mode [?\C-h ?m])
289 (set-fill-column [?\C-x ?f])
290 (fill-paragraph [?\M-q])
291
292 ;; * SEARCHING
293 (isearch-forward [?\C-s])
294 (isearch-backward [?\C-r])
295
296 ;; * MULTIPLE WINDOWS
297 (split-window-below [?\C-x ?2])
298 (scroll-other-window [?\C-\M-v])
299 (other-window [?\C-x ?o])
300 (find-file-other-window [?\C-x ?4 ?\C-f])
301
302 ;; * RECURSIVE EDITING LEVELS
303 (keyboard-escape-quit [27 27 27])
304
305 ;; * GETTING MORE HELP
306 ;; The most basic HELP feature is C-h c
307 (describe-key-briefly [?\C-h ?c])
308 (describe-key [?\C-h ?k])
309
310 ;; * MORE FEATURES
311 ;; F10
312
313 ;; * CONCLUSION
314 ;;(iconify-or-deiconify-frame [?\C-z])
315 (,suspend-emacs [?\C-z]))))
316 (sort default-keys 'tutorial--sort-keys))
317 "Default Emacs key bindings that the tutorial depends on.")
318
319 (defun tutorial--detailed-help (button)
320 "Give detailed help about changed keys."
321 (with-output-to-temp-buffer (help-buffer)
322 (help-setup-xref (list #'tutorial--detailed-help button)
323 (called-interactively-p 'interactive))
324 (with-current-buffer (help-buffer)
325 (let* ((tutorial-buffer (button-get button 'tutorial-buffer))
326 (explain-key-desc (button-get button 'explain-key-desc))
327 (changed-keys (with-current-buffer tutorial-buffer
328 (save-excursion
329 (goto-char (point-min))
330 (tutorial--find-changed-keys
331 tutorial--default-keys)))))
332 (when changed-keys
333 (insert
334 "The following key bindings used in the tutorial have been changed
335 from the Emacs default:\n\n" )
336 (let ((frm " %-14s %-27s %-16s\n"))
337 (insert (format frm
338 "Standard Key" "Command" "In Your Emacs")))
339 (dolist (tk changed-keys)
340 (let* ((def-fun (nth 1 tk))
341 (key (nth 0 tk))
342 (def-fun-txt (nth 2 tk))
343 (where (nth 3 tk))
344 (remark (nth 4 tk))
345 (key-txt (key-description key))
346 (key-fun (with-current-buffer tutorial-buffer (key-binding key))))
347 (unless (eq def-fun key-fun)
348 ;; Insert key binding description:
349 (when (string= key-txt explain-key-desc)
350 (put-text-property 0 (length key-txt)
351 'face 'tutorial-warning-face key-txt))
352 (insert " " key-txt " ")
353 (indent-to 18)
354 ;; Insert a link describing the old binding:
355 (insert-button def-fun-txt
356 'value def-fun
357 'action
358 (lambda (button) (interactive)
359 (describe-function
360 (button-get button 'value)))
361 'follow-link t)
362 (indent-to 45)
363 (when (listp where)
364 (setq where "list"))
365 ;; Tell where the old binding is now:
366 (insert (format " %-16s "
367 (if (string= "" where)
368 (format "M-x %s" def-fun-txt)
369 where)))
370 ;; Insert a link with more information, for example
371 ;; current binding and keymap or information about
372 ;; cua-mode replacements:
373 (insert-button (car remark)
374 'action
375 (lambda (b) (interactive)
376 (let ((value (button-get b 'value)))
377 (tutorial--describe-nonstandard-key value)))
378 'value (cdr remark)
379 'follow-link t)
380 (insert "\n")))))
381
382 (insert "
383 It is OK to change key bindings, but changed bindings do not
384 correspond to what the tutorial says.\n\n")
385 (help-print-return-message)))))
386
387 (defun tutorial--find-changed-keys (default-keys)
388 "Find the key bindings used in the tutorial that have changed.
389 Return a list with elements of the form
390
391 '(KEY DEF-FUN DEF-FUN-TXT WHERE REMARK QUIET)
392
393 where
394
395 KEY is a key sequence whose standard binding has been changed
396 DEF-FUN is the standard binding of KEY
397 DEF-FUN-TXT is a short descriptive text for DEF-FUN
398 WHERE is a text describing the key sequences to which DEF-FUN is
399 bound now (or, if it is remapped, a key sequence
400 for the function it is remapped to)
401 REMARK is a list with info about rebinding. It has either of
402 these formats:
403
404 \(TEXT cua-mode)
405 \(TEXT current-binding KEY-FUN DEF-FUN KEY WHERE)
406
407 Here TEXT is a link text to show to the user. The
408 rest of the list is used to show information when
409 the user clicks the link.
410
411 KEY-FUN is the actual binding for KEY.
412 QUIET is t if this changed keybinding should be handled quietly.
413 This is used by `tutorial--display-changes'."
414 (let (changed-keys remark)
415 ;; Look up the bindings in a Fundamental mode buffer
416 ;; so we do not get fooled by some other major mode.
417 (with-temp-buffer
418 (fundamental-mode)
419 (dolist (kdf default-keys)
420 ;; The variables below corresponds to those with the same names
421 ;; described in the doc string.
422 (let* ((key (nth 1 kdf))
423 (def-fun (nth 0 kdf))
424 (def-fun-txt (format "%s" def-fun))
425 (rem-fun (command-remapping def-fun))
426 ;; Handle prefix definitions specially
427 ;; so that a mode that rebinds some subcommands
428 ;; won't make it appear that the whole prefix is gone.
429 (key-fun (if (eq def-fun 'ESC-prefix)
430 (lookup-key global-map [27])
431 (if (eq def-fun 'Control-X-prefix)
432 (lookup-key global-map [24])
433 (key-binding key))))
434 (where (where-is-internal (if rem-fun rem-fun def-fun)))
435 cwhere)
436
437 (if where
438 (progn
439 (setq cwhere (car where)
440 where (key-description cwhere))
441 (when (and (< 10 (length where))
442 (string= (substring where 0 (length "<menu-bar>"))
443 "<menu-bar>"))
444 (setq where
445 (if (and (vectorp cwhere)
446 (setq cwhere (elt cwhere 1))
447 (setq cwhere
448 (cadr
449 (assoc cwhere
450 (lookup-key global-map
451 [menu-bar]))))
452 (stringp cwhere))
453 (format "the ‘%s’ menu" cwhere)
454 "the menus"))))
455 (setq where ""))
456 (setq remark nil)
457 (unless
458 (cond ((eq key-fun def-fun)
459 ;; No rebinding, return t
460 t)
461 ((and key-fun
462 (eq key-fun (command-remapping def-fun)))
463 ;; Just a remapping, return t
464 t)
465 ;; cua-mode specials:
466 ((and cua-mode
467 (or (and
468 (equal key [?\C-v])
469 (eq key-fun 'cua-paste))
470 (and
471 (equal key [?\C-z])
472 (eq key-fun 'undo))))
473 (setq remark (list "cua-mode, more info" 'cua-mode))
474 nil)
475 ((and cua-mode
476 (or (and (eq def-fun 'ESC-prefix)
477 (equal key-fun
478 `(keymap
479 (118 . cua-repeat-replace-region)))
480 (setq def-fun-txt "\"ESC prefix\""))
481 (and (eq def-fun 'mode-specific-command-prefix)
482 (equal key-fun
483 '(keymap
484 (timeout . copy-region-as-kill)))
485 (setq def-fun-txt "\"C-c prefix\""))
486 (and (eq def-fun 'Control-X-prefix)
487 (equal key-fun
488 '(keymap (timeout . kill-region)))
489 (setq def-fun-txt "\"C-x prefix\""))))
490 (setq remark (list "cua-mode replacement" 'cua-mode))
491 (setq where "Same key")
492 nil)
493 ;; viper-mode specials:
494 ((and (boundp 'viper-mode-string)
495 (boundp 'viper-current-state)
496 (eq viper-current-state 'vi-state)
497 (or (and (eq def-fun 'isearch-forward)
498 (eq key-fun 'viper-isearch-forward))
499 (and (eq def-fun 'isearch-backward)
500 (eq key-fun 'viper-isearch-backward))))
501 ;; These bindings works as the default bindings,
502 ;; return t
503 t)
504 ((when normal-erase-is-backspace
505 (or (and (equal key [C-delete])
506 (equal key-fun 'kill-word))
507 (and (equal key [C-backspace])
508 (equal key-fun 'backward-kill-word))))
509 ;; This is the strange handling of C-delete and
510 ;; C-backspace, return t
511 t)
512 (t
513 ;; This key has indeed been rebound. Put information
514 ;; in `remark' and return nil
515 (setq remark
516 (list "more info" 'current-binding
517 key-fun def-fun key where))
518 nil))
519 (add-to-list 'changed-keys
520 (list key def-fun def-fun-txt where remark nil))))))
521 changed-keys))
522
523 (defun tutorial--key-description (key)
524 (let ((desc (key-description key)))
525 (cond ((string= "ESC" desc) "<ESC>")
526 ((string= "RET" desc) "<Return>")
527 ((string= "DEL" desc) "<Delback>")
528 (t desc))))
529
530 (defun tutorial--display-changes ()
531 "Display changes to some default key bindings.
532 If some of the default key bindings that the tutorial depends on
533 have been changed then display the changes in the tutorial buffer
534 with some explanatory links."
535 (let* ((changed-keys (tutorial--find-changed-keys
536 tutorial--default-keys))
537 ;; Alist of element (DESC . CK) where DESC is the
538 ;; key-description of a changed key and CK is the
539 ;; corresponding element in `changed-keys'.
540 (changed-keys-alist
541 (mapcar (lambda (ck) (cons (tutorial--key-description (car ck)) ck))
542 changed-keys))
543 changed-key
544 (start (point))
545 (case-fold-search nil)
546 (keybindings-regexp
547 ;; Accept either [:space:] or [:punct:] before the key
548 ;; binding because the Hebrew tutorial uses directional
549 ;; controls and Hebrew character maqaf, the Hebrew hyphen,
550 ;; immediately before the binding string.
551 (concat "\\(?:[[:space:]]\\|[[:punct:]]\\)\\("
552 (mapconcat (lambda (kdf) (regexp-quote
553 (tutorial--key-description
554 (nth 1 kdf))))
555 tutorial--default-keys
556 "\\|")
557 "\\)[[:punct:][:space:]]")))
558 ;; Need the custom button face for viper buttons:
559 (if (boundp 'viper-mode-string) (require 'cus-edit))
560
561 (if (or changed-keys (boundp 'viper-mode-string))
562 (let ((head (get-lang-string tutorial--lang 'tut-chgdhead))
563 (head2 (get-lang-string tutorial--lang 'tut-chgdhead2)))
564 (when (and head head2)
565 (goto-char tutorial--point-before-chkeys)
566 (insert head " [")
567 (insert-button head2 'tutorial-buffer (current-buffer)
568 'action 'tutorial--detailed-help
569 'follow-link t 'face 'link)
570 (insert "]\n\n")
571 (add-text-properties tutorial--point-before-chkeys (point)
572 '(tutorial-remark remark
573 face tutorial-warning-face
574 read-only t)))))
575
576 ;; Scan the tutorial for all key sequences.
577 (goto-char (point-min))
578 (while (re-search-forward keybindings-regexp (point-max) t)
579 ;; Then highlight each rebound key sequence.
580 ;; This avoids issuing a warning for, e.g., C-x C-b if C-b is rebound.
581 (setq changed-key (assoc (match-string 1) changed-keys-alist))
582 (and changed-key
583 (not (get-text-property (match-beginning 1) 'tutorial-remark))
584 (let* ((desc (car changed-key))
585 (ck (cdr changed-key))
586 (def-fun (nth 1 ck))
587 (where (nth 3 ck))
588 s1 s2 help-string)
589 (unless (string= where "Same key")
590 (when (string= where "")
591 (setq where (format "M-x %s" def-fun)))
592 (setq tutorial--point-after-chkeys (point-marker)
593 s1 (get-lang-string tutorial--lang 'tut-chgdkey)
594 s2 (get-lang-string tutorial--lang 'tut-chgdkey2)
595 help-string (and s1 s2 (format s1 desc where)))
596 (add-text-properties (match-beginning 1) (match-end 1)
597 '(face tutorial-warning-face
598 tutorial-remark key-sequence))
599 (if help-string
600 (if (nth 5 ck)
601 ;; Put help string in the tooltip.
602 (put-text-property (match-beginning 1) (match-end 1)
603 'help-echo help-string)
604 ;; Put help string in the buffer.
605 (save-excursion
606 (setcar (nthcdr 5 ck) t)
607 (forward-line)
608 ;; Two or more changed keys were on the same line.
609 (while (eq (get-text-property (point) 'tutorial-remark)
610 'remark)
611 (forward-line))
612 (setq start (point))
613 (insert "** " help-string " [")
614 (insert-button s2 'tutorial-buffer (current-buffer)
615 'action 'tutorial--detailed-help
616 'explain-key-desc desc 'follow-link t
617 'face 'link)
618 (insert "] **\n")
619 (add-text-properties start (point)
620 '(tutorial-remark remark
621 rear-nonsticky t
622 face tutorial-warning-face
623 read-only t)))))))))))
624
625 (defun tutorial--saved-dir ()
626 "Directory to which tutorials are saved."
627 (locate-user-emacs-file "tutorial/"))
628
629 (defun tutorial--saved-file ()
630 "File name in which to save tutorials."
631 (let ((file-name tutorial--lang)
632 (ext (file-name-extension tutorial--lang)))
633 (when (or (not ext)
634 (string= ext ""))
635 (setq file-name (concat file-name ".tut")))
636 (expand-file-name file-name (tutorial--saved-dir))))
637
638 (defun tutorial--remove-remarks ()
639 "Remove the remark lines that was added to the tutorial buffer."
640 (save-excursion
641 (goto-char (point-min))
642 (let (prop-start
643 prop-end
644 prop-val)
645 ;; Catch the case when we already are on a remark line
646 (while (if (get-text-property (point) 'tutorial-remark)
647 (setq prop-start (point))
648 (setq prop-start (next-single-property-change (point) 'tutorial-remark)))
649 (setq prop-end (next-single-property-change prop-start 'tutorial-remark))
650 (setq prop-val (get-text-property prop-start 'tutorial-remark))
651 (unless prop-end
652 (setq prop-end (point-max)))
653 (goto-char prop-end)
654 (unless (eq prop-val 'key-sequence)
655 (delete-region prop-start prop-end))))))
656
657 (defun tutorial--save-tutorial ()
658 "Save the tutorial buffer.
659 This saves the part of the tutorial before and after the area
660 showing changed keys. It also saves the point position and the
661 position where the display of changed bindings was inserted."
662 ;; This runs in a hook so protect it:
663 (condition-case err
664 (if (y-or-n-p "Save your position in the tutorial? ")
665 (tutorial--save-tutorial-to (tutorial--saved-file))
666 (message "Tutorial position not saved"))
667 (error (message "Error saving tutorial state: %s"
668 (error-message-string err)))))
669
670 (defun tutorial--save-tutorial-to (saved-file)
671 "Save the tutorial buffer to SAVED-FILE.
672 See `tutorial--save-tutorial' for more information."
673 ;; Anything to save?
674 (when (or (buffer-modified-p)
675 (< 1 (point)))
676 (let ((tutorial-dir (tutorial--saved-dir))
677 save-err)
678 ;; The tutorial is saved in a subdirectory in the user home
679 ;; directory. Create this subdirectory first.
680 (unless (file-directory-p tutorial-dir)
681 (condition-case err
682 (make-directory tutorial-dir nil)
683 (error (setq save-err t)
684 (warn "Could not create directory %s: %s" tutorial-dir
685 (error-message-string err)))))
686 ;; Make sure we have that directory.
687 (if (file-directory-p tutorial-dir)
688 (let ((tut-point (if (= 0 tutorial--point-after-chkeys)
689 ;; No info about changed keys is
690 ;; displayed.
691 (point)
692 (if (< (point) tutorial--point-after-chkeys)
693 (- (point))
694 (- (point) tutorial--point-after-chkeys))))
695 (old-point (point))
696 ;; Use a special undo list so that we easily can undo
697 ;; the changes we make to the tutorial buffer. This is
698 ;; currently not needed since we now delete the buffer
699 ;; after saving, but kept for possible future use of
700 ;; this function.
701 buffer-undo-list
702 (inhibit-read-only t))
703 ;; Delete the area displaying info about changed keys.
704 ;; (when (< 0 tutorial--point-after-chkeys)
705 ;; (delete-region tutorial--point-before-chkeys
706 ;; tutorial--point-after-chkeys))
707 ;; Delete the remarks:
708 (tutorial--remove-remarks)
709 ;; Put the value of point first in the buffer so it will
710 ;; be saved with the tutorial.
711 (goto-char (point-min))
712 (insert (number-to-string tut-point)
713 "\n"
714 (number-to-string (marker-position
715 tutorial--point-before-chkeys))
716 "\n")
717 (condition-case err
718 (write-region nil nil saved-file)
719 (error (setq save-err t)
720 (warn "Could not save tutorial to %s: %s"
721 saved-file
722 (error-message-string err))))
723 ;; An error is raised here?? Is this a bug?
724 (ignore-errors (undo-only))
725 ;; Restore point
726 (goto-char old-point)
727 (if save-err
728 (message "Could not save tutorial state.")
729 (message "Saved tutorial state.")))
730 (message "Can't save tutorial: %s is not a directory"
731 tutorial-dir)))))
732
733
734 ;;;###autoload
735 (defun help-with-tutorial (&optional arg dont-ask-for-revert)
736 "Select the Emacs learn-by-doing tutorial.
737 If there is a tutorial version written in the language
738 of the selected language environment, that version is used.
739 If there's no tutorial in that language, `TUTORIAL' is selected.
740 With ARG, you are asked to choose which language.
741 If DONT-ASK-FOR-REVERT is non-nil the buffer is reverted without
742 any question when restarting the tutorial.
743
744 If any of the standard Emacs key bindings that are used in the
745 tutorial have been changed then an explanatory note about this is
746 shown in the beginning of the tutorial buffer.
747
748 When the tutorial buffer is killed the content and the point
749 position in the buffer is saved so that the tutorial may be
750 resumed later."
751 (interactive "P")
752 (if (boundp 'viper-current-state)
753 (let ((prompt1
754 "You can not run the Emacs tutorial directly because you have \
755 enabled Viper.")
756 (prompt2 "\nThere is however a Viper tutorial you can run instead.
757 Run the Viper tutorial? "))
758 (if (fboundp 'viper-tutorial)
759 (if (y-or-n-p (concat prompt1 prompt2))
760 (progn (message "")
761 (funcall 'viper-tutorial 0))
762 (message "Tutorial aborted by user"))
763 (message prompt1)))
764 (let* ((lang (cond
765 (arg
766 (minibuffer-with-setup-hook #'minibuffer-completion-help
767 (read-language-name 'tutorial "Language: " "English")))
768 ((get-language-info current-language-environment 'tutorial)
769 current-language-environment)
770 (t "English")))
771 (filename (get-language-info lang 'tutorial))
772 (tut-buf-name filename)
773 (old-tut-buf (get-buffer tut-buf-name))
774 (old-tut-win (when old-tut-buf (get-buffer-window old-tut-buf t)))
775 (old-tut-is-ok (when old-tut-buf
776 (not (buffer-modified-p old-tut-buf))))
777 old-tut-file
778 (old-tut-point 1))
779 (setq tutorial--point-after-chkeys (point-min))
780 ;; Try to display the tutorial buffer before asking to revert it.
781 ;; If the tutorial buffer is shown in some window make sure it is
782 ;; selected and displayed:
783 (if old-tut-win
784 (raise-frame
785 (window-frame
786 (select-window (get-buffer-window old-tut-buf t))))
787 ;; Else, is there an old tutorial buffer? Then display it:
788 (when old-tut-buf
789 (switch-to-buffer old-tut-buf)))
790 ;; Use whole frame for tutorial
791 (delete-other-windows)
792 ;; If the tutorial buffer has been changed then ask if it should
793 ;; be reverted:
794 (when (and old-tut-buf
795 (not old-tut-is-ok))
796 (setq old-tut-is-ok
797 (if dont-ask-for-revert
798 nil
799 (not (y-or-n-p
800 "You have changed the Tutorial buffer. Revert it? ")))))
801 ;; (Re)build the tutorial buffer if it is not ok
802 (unless old-tut-is-ok
803 (switch-to-buffer (get-buffer-create tut-buf-name))
804 ;; (unless old-tut-buf (text-mode))
805 (unless lang (error "Variable lang is nil"))
806 (setq tutorial--lang lang)
807 (setq old-tut-file (file-exists-p (tutorial--saved-file)))
808 (let ((inhibit-read-only t))
809 (erase-buffer))
810 (message "Preparing tutorial ...") (sit-for 0)
811
812 ;; Do not associate the tutorial buffer with a file. Instead use
813 ;; a hook to save it when the buffer is killed.
814 (setq buffer-auto-save-file-name nil)
815 (add-hook 'kill-buffer-hook 'tutorial--save-tutorial nil t)
816
817 ;; Insert the tutorial. First offer to resume last tutorial
818 ;; editing session.
819 (when dont-ask-for-revert
820 (setq old-tut-file nil))
821 (when old-tut-file
822 (setq old-tut-file
823 (y-or-n-p "Resume your last saved tutorial? ")))
824 (if old-tut-file
825 (progn
826 (insert-file-contents (tutorial--saved-file))
827 (let ((enable-local-variables :safe)
828 (enable-local-eval nil)
829 (enable-dir-local-variables nil)) ; bug#11127
830 (hack-local-variables))
831 (goto-char (point-min))
832 (setq old-tut-point
833 (string-to-number
834 (buffer-substring-no-properties
835 (line-beginning-position) (line-end-position))))
836 (forward-line)
837 (setq tutorial--point-before-chkeys
838 (string-to-number
839 (buffer-substring-no-properties
840 (line-beginning-position) (line-end-position))))
841 (forward-line)
842 (delete-region (point-min) (point))
843 (goto-char tutorial--point-before-chkeys)
844 (setq tutorial--point-before-chkeys (point-marker)))
845 (insert-file-contents (expand-file-name filename tutorial-directory))
846 (let ((enable-local-variables :safe)
847 (enable-local-eval nil)
848 (enable-dir-local-variables nil)) ; bug#11127
849 (hack-local-variables))
850 (forward-line)
851 (setq tutorial--point-before-chkeys (point-marker)))
852
853 (tutorial--display-changes)
854
855 ;; Clear message:
856 (unless dont-ask-for-revert
857 (message "") (sit-for 0))
858
859
860 (if old-tut-file
861 ;; Just move to old point in saved tutorial.
862 (let ((old-point
863 (if (> 0 old-tut-point)
864 (- old-tut-point)
865 (+ old-tut-point tutorial--point-after-chkeys))))
866 (when (< old-point 1)
867 (setq old-point 1))
868 (goto-char old-point))
869 ;; Delete the arch-tag line, so as not to confuse readers.
870 (goto-char (point-max))
871 (if (search-backward ";;; arch-tag: " nil t)
872 (delete-region (point) (point-max)))
873 (goto-char (point-min))
874 (search-forward "\n<<")
875 (beginning-of-line)
876 ;; Convert the <<...>> line to the proper [...] line,
877 ;; or just delete the <<...>> line if a [...] line follows.
878 (cond ((save-excursion
879 (forward-line 1)
880 (looking-at-p "\\["))
881 (delete-region (point) (progn (forward-line 1) (point))))
882 ((looking-at "<<Blank lines inserted.*>>")
883 (replace-match "[Middle of page left blank for didactic purposes. Text continues below]"))
884 (t
885 (looking-at "<<")
886 (replace-match "[")
887 (search-forward ">>")
888 (replace-match "]")))
889 (beginning-of-line)
890 ;; FIXME: if the window is not tall, and especially if the
891 ;; big red "NOTICE: The main purpose..." text has been
892 ;; inserted at the start of the buffer, the "type C-v to
893 ;; move to the next screen" might not be visible on the
894 ;; first screen (n < 0). How will the novice know what to do?
895 (let ((n (- (window-height)
896 (count-lines (point-min) (point))
897 6)))
898 (if (< n 8)
899 (progn
900 ;; For a short gap, we don't need the [...] line,
901 ;; so delete it.
902 (delete-region (point) (progn (end-of-line) (point)))
903 (if (> n 0) (newline n)))
904 ;; Some people get confused by the large gap.
905 (newline (/ n 2))
906
907 ;; Skip the [...] line (don't delete it).
908 (forward-line 1)
909 (newline (- n (/ n 2)))))
910 (goto-char (point-min)))
911 (setq buffer-undo-list nil)
912 (set-buffer-modified-p nil)))))
913
914
915 ;; Below is some attempt to handle language specific strings. These
916 ;; are currently only used in the tutorial.
917
918 (defconst lang-strings
919 '(("English" .
920 ((tut-chgdkey . "%s has been rebound, but you can use %s instead")
921 (tut-chgdkey2 . "More")
922 (tut-chgdhead . "
923 NOTICE: The main purpose of the Emacs tutorial is to teach you
924 the most important standard Emacs commands (key bindings).
925 However, your Emacs has been customized by changing some of
926 these basic editing commands, so it doesn't correspond to the
927 tutorial. We have inserted colored notices where the altered
928 commands have been introduced.")
929 (tut-chgdhead2 . "More"))))
930 "Language specific strings for Emacs.
931 This is an association list with the keys equal to the strings
932 that can be returned by `read-language-name'. The elements in
933 the list are themselves association lists with keys that are
934 string ids and values that are the language specific strings.
935
936 See `get-lang-string' for more information.")
937
938 (defun get-lang-string (lang stringid &optional no-eng-fallback)
939 "Get a language specific string for Emacs.
940 In certain places Emacs can replace a string shown to the user with
941 a language specific string. This function retrieves such strings.
942
943 LANG is the language specification. It should be one of those
944 strings that can be returned by `read-language-name'. STRINGID
945 is a symbol that specifies the string to retrieve.
946
947 If no string is found for STRINGID in the chosen language then
948 the English string is returned unless NO-ENG-FALLBACK is non-nil.
949
950 See `lang-strings' for more information.
951
952 Currently this feature is only used in `help-with-tutorial'."
953 (let ((my-lang-strings (assoc lang lang-strings))
954 (found-string))
955 (when my-lang-strings
956 (let ((entry (assoc stringid (cdr my-lang-strings))))
957 (when entry
958 (setq found-string (cdr entry)))))
959 ;; Fallback to English strings
960 (unless (or found-string
961 no-eng-fallback)
962 (setq found-string (get-lang-string "English" stringid t)))
963 found-string))
964
965 ;;(get-lang-string "English" 'tut-chgdkey)
966
967 (provide 'tutorial)
968
969 ;;; tutorial.el ends here