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