tutorial.el 37 KB

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