tutorial.el 37 KB

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