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