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