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 | |
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. | |
56 | This is used in `help-with-tutorial'. The information includes | |
57 | the key sequence that no longer has a default binding, the | |
58 | default binding and the current binding. It also tells in what | |
59 | keymap the new binding has been done and how to access the | |
60 | function in the default binding from the keyboard. | |
61 | ||
62 | For `cua-mode' key bindings that try to combine CUA key bindings | |
63 | with default Emacs bindings information about this is shown. | |
64 | ||
65 | VALUE should have either of these formats: | |
66 | ||
67 | \(cua-mode) | |
68 | \(current-binding KEY-FUN DEF-FUN KEY WHERE) | |
69 | ||
70 | Where | |
71 | KEY is a key sequence whose standard binding has been changed | |
72 | KEY-FUN is the actual binding for KEY | |
73 | DEF-FUN is the standard binding of KEY | |
74 | WHERE is a text describing the key sequences to which DEF-FUN is | |
75 | bound now (or, if it is remapped, a key sequence | |
76 | for the function it is remapped to)" | |
77 | (with-output-to-temp-buffer (help-buffer) | |
78 | (help-setup-xref (list #'tutorial--describe-nonstandard-key value) | |
79 | (interactive-p)) | |
80 | (with-current-buffer (help-buffer) | |
81 | (insert | |
82 | "Your Emacs customizations override the default binding for this key:" | |
83 | "\n\n") | |
84 | (let ((inhibit-read-only t)) | |
85 | (cond | |
86 | ((eq (car value) 'cua-mode) | |
87 | (insert | |
88 | "CUA mode is enabled. | |
89 | ||
90 | When CUA mode is enabled, you can use C-z, C-x, C-c, and C-v to | |
91 | undo, cut, copy, and paste in addition to the normal Emacs | |
92 | bindings. The C-x and C-c keys only do cut and copy when the | |
93 | region is active, so in most cases, they do not conflict with the | |
94 | normal function of these prefix keys. | |
95 | ||
96 | If you really need to perform a command which starts with one of | |
97 | the prefix keys even when the region is active, you have three | |
98 | options: | |
99 | - press the prefix key twice very quickly (within 0.2 seconds), | |
100 | - press the prefix key and the following key within 0.2 seconds, or | |
101 | - use the SHIFT key with the prefix key, i.e. C-S-x or C-S-c.")) | |
102 | ((eq (car value) 'current-binding) | |
103 | (let ((cb (nth 1 value)) | |
104 | (db (nth 2 value)) | |
105 | (key (nth 3 value)) | |
106 | (where (nth 4 value)) | |
107 | map | |
108 | (maps (current-active-maps)) | |
109 | mapsym) | |
110 | ;; Look at the currently active keymaps and try to find | |
111 | ;; first the keymap where the current binding occurs: | |
112 | (while maps | |
113 | (let* ((m (car maps)) | |
114 | (mb (lookup-key m key t))) | |
115 | (setq maps (cdr maps)) | |
116 | (when (eq mb cb) | |
117 | (setq map m) | |
118 | (setq maps nil)))) | |
119 | ;; Now, if a keymap was found we must found the symbol | |
120 | ;; name for it to display to the user. This can not | |
121 | ;; always be found since all keymaps does not have a | |
122 | ;; symbol pointing to them, but here they should have | |
123 | ;; that: | |
124 | (when map | |
125 | (mapatoms (lambda (s) | |
126 | (and | |
127 | ;; If not already found | |
128 | (not mapsym) | |
129 | ;; and if s is a keymap | |
130 | (and (boundp s) | |
131 | (keymapp (symbol-value s))) | |
132 | ;; and not the local symbol map | |
133 | (not (eq s 'map)) | |
134 | ;; and the value of s is map | |
135 | (eq map (symbol-value s)) | |
136 | ;; then save this value in mapsym | |
137 | (setq mapsym s))))) | |
138 | (insert "The default Emacs binding for the key " | |
139 | (key-description key) | |
140 | " is the command `") | |
141 | (insert (format "%s" db)) | |
142 | (insert "'. " | |
143 | "However, your customizations have rebound it to the command `") | |
144 | (insert (format "%s" cb)) | |
145 | (insert "'.") | |
146 | (when mapsym | |
147 | (insert " (For the more advanced user:" | |
148 | " This binding is in the keymap `" | |
149 | (format "%s" mapsym) | |
150 | "'.)")) | |
151 | (if (string= where "") | |
152 | (unless (keymapp db) | |
153 | (insert "\n\nYou can use M-x " | |
154 | (format "%s" db) | |
155 | " RET instead.")) | |
156 | (insert "\n\nWith 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'. | |
167 | This is a predicate function to `sort'. | |
168 | ||
169 | The sorting is for presentation purpose only and is done on the | |
170 | key sequence. | |
171 | ||
172 | LEFT and RIGHT are the elements to compare." | |
173 | (let ((x (append (cadr left) nil)) | |
174 | (y (append (cadr right) nil))) | |
175 | ;; Skip the front part of the key sequences if they are equal: | |
176 | (while (and x y | |
177 | (listp x) (listp y) | |
178 | (equal (car x) (car y))) | |
179 | (setq x (cdr x)) | |
180 | (setq y (cdr y))) | |
181 | ;; Try to make a comparision that is useful for presentation (this | |
182 | ;; could be made nicer perhaps): | |
183 | (let ((cx (car x)) | |
184 | (cy (car y))) | |
185 | ;;(message "x=%s, y=%s;;;; cx=%s, cy=%s" x y cx cy) | |
186 | (cond | |
187 | ;; Lists? Then call this again | |
188 | ((and cx cy | |
189 | (listp cx) | |
190 | (listp cy)) | |
191 | (tutorial--sort-keys cx cy)) | |
192 | ;; Are both numbers? Then just compare them | |
193 | ((and (wholenump cx) | |
194 | (wholenump cy)) | |
195 | (> cx cy)) | |
196 | ;; Is one of them a number? Let that be bigger then. | |
197 | ((wholenump cx) | |
198 | t) | |
199 | ((wholenump cy) | |
200 | nil) | |
201 | ;; Are both symbols? Compare the names then. | |
202 | ((and (symbolp cx) | |
203 | (symbolp cy)) | |
204 | (string< (symbol-name cy) | |
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 | |
87fe23ee | 339 | from the Emacs default in the " (buffer-name tutorial-buffer) " buffer:\n\n" ) |
6db93af0 CY |
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 | 393 | It is OK to change key bindings, but changed bindings do not |
87fe23ee | 394 | correspond to what the tutorial says.\n\n") |
6db93af0 CY |
395 | (print-help-return-message))))) |
396 | ||
6db93af0 | 397 | (defun tutorial--find-changed-keys (default-keys) |
87fe23ee CY |
398 | "Find the key bindings used in the tutorial that have changed. |
399 | Return a list with elements of the form | |
6db93af0 | 400 | |
87fe23ee | 401 | '(KEY DEF-FUN DEF-FUN-TXT WHERE REMARK QUIET) |
6db93af0 | 402 | |
87fe23ee | 403 | where |
6db93af0 | 404 | |
6db93af0 CY |
405 | KEY is a key sequence whose standard binding has been changed |
406 | DEF-FUN is the standard binding of KEY | |
407 | DEF-FUN-TXT is a short descriptive text for DEF-FUN | |
408 | WHERE is a text describing the key sequences to which DEF-FUN is | |
409 | bound now (or, if it is remapped, a key sequence | |
410 | for the function it is remapped to) | |
411 | REMARK is a list with info about rebinding. It has either of these | |
412 | formats: | |
413 | ||
414 | \(TEXT cua-mode) | |
415 | \(TEXT current-binding KEY-FUN DEF-FUN KEY WHERE) | |
416 | ||
417 | Here TEXT is a link text to show to the user. The | |
418 | rest of the list is used to show information when | |
419 | the user clicks the link. | |
420 | ||
87fe23ee CY |
421 | KEY-FUN is the actual binding for KEY. |
422 | QUIET is t if this changed keybinding should be handled quietly. | |
423 | This is used by `tutorial--display-changes'." | |
cb753f52 | 424 | (let (changed-keys remark) |
6db93af0 CY |
425 | (dolist (kdf default-keys) |
426 | ;; The variables below corresponds to those with the same names | |
427 | ;; described in the doc string. | |
428 | (let* ((key (nth 1 kdf)) | |
429 | (def-fun (nth 0 kdf)) | |
430 | (def-fun-txt (format "%s" def-fun)) | |
431 | (rem-fun (command-remapping def-fun)) | |
03e3eb4d CY |
432 | (key-fun (if (eq def-fun 'ESC-prefix) |
433 | (lookup-key global-map [27]) | |
434 | (key-binding key))) | |
6db93af0 | 435 | (where (where-is-internal (if rem-fun rem-fun def-fun)))) |
6db93af0 CY |
436 | (if where |
437 | (progn | |
438 | (setq where (key-description (car where))) | |
439 | (when (and (< 10 (length where)) | |
440 | (string= (substring where 0 (length "<menu-bar>")) | |
441 | "<menu-bar>")) | |
03e3eb4d | 442 | (setq where "the menus"))) |
6db93af0 CY |
443 | (setq where "")) |
444 | (setq remark nil) | |
445 | (unless | |
446 | (cond ((eq key-fun def-fun) | |
447 | ;; No rebinding, return t | |
448 | t) | |
449 | ((eq key-fun (command-remapping def-fun)) | |
450 | ;; Just a remapping, return t | |
451 | t) | |
452 | ;; cua-mode specials: | |
453 | ((and cua-mode | |
454 | (or (and | |
455 | (equal key [?\C-v]) | |
456 | (eq key-fun 'cua-paste)) | |
457 | (and | |
458 | (equal key [?\C-z]) | |
459 | (eq key-fun 'undo)))) | |
460 | (setq remark (list "cua-mode, more info" 'cua-mode)) | |
461 | nil) | |
462 | ((and cua-mode | |
87fe23ee CY |
463 | (or (and (eq def-fun 'ESC-prefix) |
464 | (equal key-fun | |
6db93af0 | 465 | `(keymap |
87fe23ee CY |
466 | (118 . cua-repeat-replace-region))) |
467 | (setq def-fun-txt "\"ESC prefix\"")) | |
468 | (and (eq def-fun 'mode-specific-command-prefix) | |
469 | (equal key-fun | |
470 | '(keymap | |
471 | (timeout . copy-region-as-kill))) | |
472 | (setq def-fun-txt "\"C-c prefix\"")) | |
473 | (and (eq def-fun 'Control-X-prefix) | |
474 | (equal key-fun | |
475 | '(keymap (timeout . kill-region))) | |
476 | (setq def-fun-txt "\"C-x prefix\"")))) | |
6db93af0 | 477 | (setq remark (list "cua-mode replacement" 'cua-mode)) |
6db93af0 CY |
478 | (setq where "Same key") |
479 | nil) | |
480 | ;; viper-mode specials: | |
481 | ((and (boundp 'viper-mode-string) | |
cb753f52 | 482 | (boundp 'viper-current-state) |
6db93af0 CY |
483 | (eq viper-current-state 'vi-state) |
484 | (or (and (eq def-fun 'isearch-forward) | |
485 | (eq key-fun 'viper-isearch-forward)) | |
486 | (and (eq def-fun 'isearch-backward) | |
487 | (eq key-fun 'viper-isearch-backward)))) | |
488 | ;; These bindings works as the default bindings, | |
489 | ;; return t | |
490 | t) | |
491 | ((when normal-erase-is-backspace | |
492 | (or (and (equal key [C-delete]) | |
493 | (equal key-fun 'kill-word)) | |
494 | (and (equal key [C-backspace]) | |
495 | (equal key-fun 'backward-kill-word)))) | |
496 | ;; This is the strange handling of C-delete and | |
497 | ;; C-backspace, return t | |
498 | t) | |
499 | (t | |
500 | ;; This key has indeed been rebound. Put information | |
501 | ;; in `remark' and return nil | |
502 | (setq remark | |
503 | (list "more info" 'current-binding | |
504 | key-fun def-fun key where)) | |
505 | nil)) | |
506 | (add-to-list 'changed-keys | |
87fe23ee | 507 | (list key def-fun def-fun-txt where remark nil))))) |
6db93af0 CY |
508 | changed-keys)) |
509 | ||
c1881005 CY |
510 | (defun tutorial--key-description (key) |
511 | (let ((desc (key-description key))) | |
512 | (cond ((string= "ESC" desc) "<ESC>") | |
513 | ((string= "RET" desc) "<Return>") | |
514 | ((string= "DEL" desc) "<Delback>") | |
515 | (t desc)))) | |
516 | ||
517 | (defun tutorial--display-changes () | |
6db93af0 CY |
518 | "Display changes to some default key bindings. |
519 | If some of the default key bindings that the tutorial depends on | |
520 | have been changed then display the changes in the tutorial buffer | |
c1881005 CY |
521 | with some explanatory links." |
522 | (let* ((changed-keys (tutorial--find-changed-keys | |
523 | tutorial--default-keys)) | |
524 | ;; Alist of element (DESC . CK) where DESC is the | |
525 | ;; key-description of a changed key and CK is the | |
526 | ;; corresponding element in `changed-keys'. | |
527 | (changed-keys-alist | |
528 | (mapcar (lambda (ck) (cons (tutorial--key-description (car ck)) ck)) | |
529 | changed-keys)) | |
87fe23ee | 530 | changed-key |
c1881005 CY |
531 | (start (point)) |
532 | (case-fold-search nil) | |
533 | (keybindings-regexp | |
534 | (concat "[[:space:]]\\(" | |
87fe23ee CY |
535 | (mapconcat (lambda (kdf) (regexp-quote |
536 | (tutorial--key-description | |
537 | (nth 1 kdf)))) | |
c1881005 CY |
538 | tutorial--default-keys |
539 | "\\|") | |
540 | "\\)[[:punct:][:space:]]"))) | |
6db93af0 | 541 | ;; Need the custom button face for viper buttons: |
c1881005 CY |
542 | (if (boundp 'viper-mode-string) (require 'cus-edit)) |
543 | ||
544 | (if (or changed-keys (boundp 'viper-mode-string)) | |
545 | (let ((head (get-lang-string tutorial--lang 'tut-chgdhead)) | |
546 | (head2 (get-lang-string tutorial--lang 'tut-chgdhead2))) | |
547 | (when (and head head2) | |
548 | (goto-char tutorial--point-before-chkeys) | |
87fe23ee | 549 | (insert head " [") |
c1881005 CY |
550 | (insert-button head2 'tutorial-buffer (current-buffer) |
551 | 'action 'tutorial--detailed-help | |
552 | 'follow-link t 'face 'link) | |
553 | (insert "]\n\n") | |
554 | (add-text-properties tutorial--point-before-chkeys (point) | |
87fe23ee CY |
555 | '(tutorial-remark remark |
556 | face tutorial-warning-face | |
557 | read-only t))))) | |
c1881005 CY |
558 | |
559 | ;; Scan the tutorial for all key sequences. | |
560 | (goto-char (point-min)) | |
561 | (while (re-search-forward keybindings-regexp (point-max) t) | |
562 | ;; Then highlight each rebound key sequence. | |
563 | ;; This avoids issuing a warning for, e.g., C-x C-b if C-b is rebound. | |
87fe23ee CY |
564 | (setq changed-key (assoc (match-string 1) changed-keys-alist)) |
565 | (and changed-key | |
566 | (not (get-text-property (match-beginning 1) 'tutorial-remark)) | |
567 | (let* ((desc (car changed-key)) | |
568 | (ck (cdr changed-key)) | |
569 | (key (nth 0 ck)) | |
570 | (def-fun (nth 1 ck)) | |
571 | (where (nth 3 ck)) | |
572 | s1 s2 help-string) | |
573 | (unless (string= where "Same key") | |
574 | (setq tutorial--point-after-chkeys (point-marker) | |
575 | s1 (get-lang-string tutorial--lang 'tut-chgdkey) | |
576 | s2 (get-lang-string tutorial--lang 'tut-chgdkey2) | |
577 | help-string (and s1 s2 (format s1 desc where))) | |
578 | (add-text-properties (match-beginning 1) (match-end 1) | |
579 | '(face tutorial-warning-face | |
580 | tutorial-remark key-sequence)) | |
581 | (if help-string | |
582 | (if (nth 5 ck) | |
583 | ;; Put help string in the tooltip. | |
584 | (put-text-property (match-beginning 1) (match-end 1) | |
585 | 'help-echo help-string) | |
586 | ;; Put help string in the buffer. | |
587 | (save-excursion | |
588 | (setcar (nthcdr 5 ck) t) | |
589 | (forward-line) | |
590 | ;; Two or more changed keys were on the same line. | |
591 | (while (eq (get-text-property (point) 'tutorial-remark) | |
592 | 'remark) | |
593 | (forward-line)) | |
594 | (setq start (point)) | |
595 | (insert "** " help-string " [") | |
c1881005 CY |
596 | (insert-button s2 'tutorial-buffer (current-buffer) |
597 | 'action 'tutorial--detailed-help | |
598 | 'explain-key-desc desc 'follow-link t | |
599 | 'face 'link) | |
600 | (insert "] **\n") | |
601 | (add-text-properties start (point) | |
87fe23ee CY |
602 | '(tutorial-remark remark |
603 | rear-nonsticky t | |
c1881005 | 604 | face tutorial-warning-face |
87fe23ee | 605 | read-only t))))))))))) |
6db93af0 | 606 | |
6db93af0 | 607 | (defun tutorial--saved-dir () |
c1881005 CY |
608 | "Directory to which tutorials are saved." |
609 | (expand-file-name "tutorial" | |
610 | (if (eq system-type 'ms-dos) "~/_emacs.d/" "~/.emacs.d/"))) | |
6db93af0 CY |
611 | |
612 | (defun tutorial--saved-file () | |
613 | "File name in which to save tutorials." | |
614 | (let ((file-name tutorial--lang) | |
615 | (ext (file-name-extension tutorial--lang))) | |
616 | (when (or (not ext) | |
617 | (string= ext "")) | |
618 | (setq file-name (concat file-name ".tut"))) | |
619 | (expand-file-name file-name (tutorial--saved-dir)))) | |
620 | ||
621 | (defun tutorial--remove-remarks() | |
622 | "Remove the remark lines that was added to the tutorial buffer." | |
623 | (save-excursion | |
624 | (goto-char (point-min)) | |
625 | (let (prop-start | |
626 | prop-end | |
627 | prop-val) | |
628 | ;; Catch the case when we already are on a remark line | |
629 | (while (if (get-text-property (point) 'tutorial-remark) | |
630 | (setq prop-start (point)) | |
631 | (setq prop-start (next-single-property-change (point) 'tutorial-remark))) | |
632 | (setq prop-end (next-single-property-change prop-start 'tutorial-remark)) | |
633 | (setq prop-val (get-text-property prop-start 'tutorial-remark)) | |
634 | (unless prop-end | |
635 | (setq prop-end (point-max))) | |
636 | (goto-char prop-end) | |
87fe23ee CY |
637 | (unless (eq prop-val 'key-sequence) |
638 | (delete-region prop-start prop-end)))))) | |
6db93af0 CY |
639 | |
640 | (defun tutorial--save-tutorial () | |
641 | "Save the tutorial buffer. | |
642 | This saves the part of the tutorial before and after the area | |
643 | showing changed keys. It also saves the point position and the | |
644 | position where the display of changed bindings was inserted." | |
645 | ;; This runs in a hook so protect it: | |
646 | (condition-case err | |
647 | (tutorial--save-tutorial-to (tutorial--saved-file)) | |
648 | (error (message "Error saving tutorial state: %s" (error-message-string err)) | |
649 | (sit-for 4)))) | |
650 | ||
651 | (defun tutorial--save-tutorial-to (saved-file) | |
652 | "Save the tutorial buffer to SAVED-FILE. | |
653 | See `tutorial--save-tutorial' for more information." | |
654 | ;; Anything to save? | |
655 | (when (or (buffer-modified-p) | |
656 | (< 1 (point))) | |
657 | (let ((tutorial-dir (tutorial--saved-dir)) | |
658 | save-err) | |
659 | ;; The tutorial is saved in a subdirectory in the user home | |
660 | ;; directory. Create this subdirectory first. | |
661 | (unless (file-directory-p tutorial-dir) | |
662 | (condition-case err | |
663 | (make-directory tutorial-dir nil) | |
664 | (error (setq save-err t) | |
665 | (warn "Could not create directory %s: %s" tutorial-dir | |
666 | (error-message-string err))))) | |
667 | ;; Make sure we have that directory. | |
668 | (if (file-directory-p tutorial-dir) | |
669 | (let ((tut-point (if (= 0 tutorial--point-after-chkeys) | |
670 | ;; No info about changed keys is | |
671 | ;; displayed. | |
672 | (point) | |
673 | (if (< (point) tutorial--point-after-chkeys) | |
674 | (- (point)) | |
675 | (- (point) tutorial--point-after-chkeys)))) | |
676 | (old-point (point)) | |
677 | ;; Use a special undo list so that we easily can undo | |
678 | ;; the changes we make to the tutorial buffer. This is | |
679 | ;; currently not needed since we now delete the buffer | |
680 | ;; after saving, but kept for possible future use of | |
681 | ;; this function. | |
682 | buffer-undo-list | |
683 | (inhibit-read-only t)) | |
684 | ;; Delete the area displaying info about changed keys. | |
685 | ;; (when (< 0 tutorial--point-after-chkeys) | |
686 | ;; (delete-region tutorial--point-before-chkeys | |
687 | ;; tutorial--point-after-chkeys)) | |
688 | ;; Delete the remarks: | |
689 | (tutorial--remove-remarks) | |
690 | ;; Put the value of point first in the buffer so it will | |
691 | ;; be saved with the tutorial. | |
692 | (goto-char (point-min)) | |
693 | (insert (number-to-string tut-point) | |
694 | "\n" | |
695 | (number-to-string (marker-position | |
696 | tutorial--point-before-chkeys)) | |
697 | "\n") | |
698 | (condition-case err | |
699 | (write-region nil nil saved-file) | |
700 | (error (setq save-err t) | |
701 | (warn "Could not save tutorial to %s: %s" | |
702 | saved-file | |
703 | (error-message-string err)))) | |
704 | ;; An error is raised here?? Is this a bug? | |
705 | (condition-case err | |
706 | (undo-only) | |
707 | (error nil)) | |
708 | ;; Restore point | |
709 | (goto-char old-point) | |
710 | (if save-err | |
711 | (message "Could not save tutorial state.") | |
712 | (message "Saved tutorial state."))) | |
713 | (message "Can't save tutorial: %s is not a directory" | |
714 | tutorial-dir))))) | |
715 | ||
716 | ||
717 | ;;;###autoload | |
718 | (defun help-with-tutorial (&optional arg dont-ask-for-revert) | |
719 | "Select the Emacs learn-by-doing tutorial. | |
720 | If there is a tutorial version written in the language | |
721 | of the selected language environment, that version is used. | |
722 | If there's no tutorial in that language, `TUTORIAL' is selected. | |
723 | With ARG, you are asked to choose which language. | |
724 | If DONT-ASK-FOR-REVERT is non-nil the buffer is reverted without | |
725 | any question when restarting the tutorial. | |
726 | ||
727 | If any of the standard Emacs key bindings that are used in the | |
728 | tutorial have been changed then an explanatory note about this is | |
729 | shown in the beginning of the tutorial buffer. | |
730 | ||
731 | When the tutorial buffer is killed the content and the point | |
732 | position in the buffer is saved so that the tutorial may be | |
733 | resumed later." | |
734 | (interactive "P") | |
735 | (if (boundp 'viper-current-state) | |
cb753f52 CY |
736 | (let ((prompt1 |
737 | "You can not run the Emacs tutorial directly because you have \ | |
738 | enabled Viper.") | |
739 | (prompt2 "\nThere is however a Viper tutorial you can run instead. | |
740 | Run the Viper tutorial? ")) | |
741 | (if (fboundp 'viper-tutorial) | |
742 | (if (y-or-n-p (concat prompt1 prompt2)) | |
743 | (progn (message "") | |
744 | (funcall 'viper-tutorial 0)) | |
745 | (message "Tutorial aborted by user")) | |
746 | (message prompt1))) | |
6db93af0 CY |
747 | (let* ((lang (if arg |
748 | (let ((minibuffer-setup-hook minibuffer-setup-hook)) | |
749 | (add-hook 'minibuffer-setup-hook | |
750 | 'minibuffer-completion-help) | |
751 | (read-language-name 'tutorial "Language: " "English")) | |
752 | (if (get-language-info current-language-environment 'tutorial) | |
753 | current-language-environment | |
754 | "English"))) | |
755 | (filename (get-language-info lang 'tutorial)) | |
756 | ;; Choose a buffer name including the language so that | |
757 | ;; several languages can be tested simultaneously: | |
758 | (tut-buf-name (concat "TUTORIAL (" lang ")")) | |
759 | (old-tut-buf (get-buffer tut-buf-name)) | |
760 | (old-tut-win (when old-tut-buf (get-buffer-window old-tut-buf t))) | |
761 | (old-tut-is-ok (when old-tut-buf | |
762 | (not (buffer-modified-p old-tut-buf)))) | |
763 | old-tut-file | |
764 | (old-tut-point 1)) | |
765 | (setq tutorial--point-after-chkeys (point-min)) | |
766 | ;; Try to display the tutorial buffer before asking to revert it. | |
767 | ;; If the tutorial buffer is shown in some window make sure it is | |
768 | ;; selected and displayed: | |
769 | (if old-tut-win | |
770 | (raise-frame | |
771 | (window-frame | |
772 | (select-window (get-buffer-window old-tut-buf t)))) | |
773 | ;; Else, is there an old tutorial buffer? Then display it: | |
774 | (when old-tut-buf | |
775 | (switch-to-buffer old-tut-buf))) | |
776 | ;; Use whole frame for tutorial | |
777 | (delete-other-windows) | |
778 | ;; If the tutorial buffer has been changed then ask if it should | |
779 | ;; be reverted: | |
780 | (when (and old-tut-buf | |
781 | (not old-tut-is-ok)) | |
782 | (setq old-tut-is-ok | |
783 | (if dont-ask-for-revert | |
784 | nil | |
785 | (not (y-or-n-p | |
786 | "You have changed the Tutorial buffer. Revert it? "))))) | |
787 | ;; (Re)build the tutorial buffer if it is not ok | |
788 | (unless old-tut-is-ok | |
789 | (switch-to-buffer (get-buffer-create tut-buf-name)) | |
790 | (unless old-tut-buf (text-mode)) | |
791 | (unless lang (error "Variable lang is nil")) | |
792 | (setq tutorial--lang lang) | |
793 | (setq old-tut-file (file-exists-p (tutorial--saved-file))) | |
794 | (let ((inhibit-read-only t)) | |
795 | (erase-buffer)) | |
796 | (message "Preparing tutorial ...") (sit-for 0) | |
797 | ||
798 | ;; Do not associate the tutorial buffer with a file. Instead use | |
799 | ;; a hook to save it when the buffer is killed. | |
800 | (setq buffer-auto-save-file-name nil) | |
801 | (add-hook 'kill-buffer-hook 'tutorial--save-tutorial nil t) | |
802 | ||
803 | ;; Insert the tutorial. First offer to resume last tutorial | |
804 | ;; editing session. | |
805 | (when dont-ask-for-revert | |
806 | (setq old-tut-file nil)) | |
807 | (when old-tut-file | |
808 | (setq old-tut-file | |
809 | (y-or-n-p "Resume your last saved tutorial? "))) | |
810 | (if old-tut-file | |
811 | (progn | |
812 | (insert-file-contents (tutorial--saved-file)) | |
813 | (goto-char (point-min)) | |
814 | (setq old-tut-point | |
815 | (string-to-number | |
816 | (buffer-substring-no-properties | |
817 | (line-beginning-position) (line-end-position)))) | |
818 | (forward-line) | |
819 | (setq tutorial--point-before-chkeys | |
820 | (string-to-number | |
821 | (buffer-substring-no-properties | |
822 | (line-beginning-position) (line-end-position)))) | |
823 | (forward-line) | |
824 | (delete-region (point-min) (point)) | |
825 | (goto-char tutorial--point-before-chkeys) | |
826 | (setq tutorial--point-before-chkeys (point-marker))) | |
827 | (insert-file-contents (expand-file-name filename data-directory)) | |
828 | (forward-line) | |
829 | (setq tutorial--point-before-chkeys (point-marker))) | |
830 | ||
c1881005 | 831 | (tutorial--display-changes) |
6db93af0 CY |
832 | |
833 | ;; Clear message: | |
834 | (unless dont-ask-for-revert | |
835 | (message "") (sit-for 0)) | |
836 | ||
837 | ||
838 | (if old-tut-file | |
839 | ;; Just move to old point in saved tutorial. | |
840 | (let ((old-point | |
841 | (if (> 0 old-tut-point) | |
842 | (- old-tut-point) | |
843 | (+ old-tut-point tutorial--point-after-chkeys)))) | |
844 | (when (< old-point 1) | |
845 | (setq old-point 1)) | |
846 | (goto-char old-point)) | |
847 | (goto-char (point-min)) | |
848 | (search-forward "\n<<") | |
849 | (beginning-of-line) | |
850 | ;; Convert the <<...>> line to the proper [...] line, | |
851 | ;; or just delete the <<...>> line if a [...] line follows. | |
852 | (cond ((save-excursion | |
853 | (forward-line 1) | |
854 | (looking-at "\\[")) | |
855 | (delete-region (point) (progn (forward-line 1) (point)))) | |
856 | ((looking-at "<<Blank lines inserted.*>>") | |
857 | (replace-match "[Middle of page left blank for didactic purposes. Text continues below]")) | |
858 | (t | |
859 | (looking-at "<<") | |
860 | (replace-match "[") | |
861 | (search-forward ">>") | |
862 | (replace-match "]"))) | |
863 | (beginning-of-line) | |
864 | (let ((n (- (window-height (selected-window)) | |
865 | (count-lines (point-min) (point)) | |
866 | 6))) | |
867 | (if (< n 8) | |
868 | (progn | |
869 | ;; For a short gap, we don't need the [...] line, | |
870 | ;; so delete it. | |
871 | (delete-region (point) (progn (end-of-line) (point))) | |
872 | (newline n)) | |
873 | ;; Some people get confused by the large gap. | |
874 | (newline (/ n 2)) | |
875 | ||
876 | ;; Skip the [...] line (don't delete it). | |
877 | (forward-line 1) | |
878 | (newline (- n (/ n 2))))) | |
879 | (goto-char (point-min))) | |
880 | (setq buffer-undo-list nil) | |
881 | (set-buffer-modified-p nil))))) | |
882 | ||
883 | ||
884 | ;; Below is some attempt to handle language specific strings. These | |
885 | ;; are currently only used in the tutorial. | |
886 | ||
887 | (defconst lang-strings | |
c1881005 | 888 | '(("English" . |
87fe23ee | 889 | ((tut-chgdkey . "%s has been rebound, but you can use %s instead") |
c1881005 | 890 | (tut-chgdkey2 . "More") |
6db93af0 CY |
891 | (tut-chgdhead . " |
892 | NOTICE: The main purpose of the Emacs tutorial is to teach you | |
893 | the most important standard Emacs commands (key bindings). | |
894 | However, your Emacs has been customized by changing some of | |
895 | these basic editing commands, so it doesn't correspond to the | |
896 | tutorial. We have inserted colored notices where the altered | |
87fe23ee | 897 | commands have been introduced.") |
c1881005 | 898 | (tut-chgdhead2 . "More")))) |
6db93af0 CY |
899 | "Language specific strings for Emacs. |
900 | This is an association list with the keys equal to the strings | |
901 | that can be returned by `read-language-name'. The elements in | |
902 | the list are themselves association lists with keys that are | |
903 | string ids and values that are the language specific strings. | |
904 | ||
905 | See `get-lang-string' for more information.") | |
906 | ||
907 | (defun get-lang-string(lang stringid &optional no-eng-fallback) | |
908 | "Get a language specific string for Emacs. | |
909 | In certain places Emacs can replace a string showed to the user with a language specific string. | |
910 | This function retrieves such strings. | |
911 | ||
912 | LANG is the language specification. It should be one of those | |
913 | strings that can be returned by `read-language-name'. STRINGID | |
914 | is a symbol that specifies the string to retrieve. | |
915 | ||
916 | If no string is found for STRINGID in the choosen language then | |
917 | the English string is returned unless NO-ENG-FALLBACK is non-nil. | |
918 | ||
919 | See `lang-strings' for more information. | |
920 | ||
921 | Currently this feature is only used in `help-with-tutorial'." | |
922 | (let ((my-lang-strings (assoc lang lang-strings)) | |
923 | (found-string)) | |
924 | (when my-lang-strings | |
925 | (let ((entry (assoc stringid (cdr my-lang-strings)))) | |
926 | (when entry | |
927 | (setq found-string (cdr entry))))) | |
928 | ;; Fallback to English strings | |
929 | (unless (or found-string | |
930 | no-eng-fallback) | |
931 | (setq found-string (get-lang-string "English" stringid t))) | |
932 | found-string)) | |
933 | ||
934 | ;;(get-lang-string "English" 'tut-chgdkey) | |
935 | ||
936 | (provide 'tutorial) | |
937 | ||
24b86c51 | 938 | ;; arch-tag: c8e80aef-c3bb-4ffb-8af6-22171bf0c100 |
6db93af0 | 939 | ;;; tutorial.el ends here |