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