derived.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. ;;; derived.el --- allow inheritance of major modes
  2. ;; (formerly mode-clone.el)
  3. ;; Copyright (C) 1993-1994, 1999, 2001-2012 Free Software Foundation, Inc.
  4. ;; Author: David Megginson (dmeggins@aix1.uottawa.ca)
  5. ;; Maintainer: FSF
  6. ;; Keywords: extensions
  7. ;; Package: emacs
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; GNU Emacs is already, in a sense, object oriented -- each object
  21. ;; (buffer) belongs to a class (major mode), and that class defines
  22. ;; the relationship between messages (input events) and methods
  23. ;; (commands) by means of a keymap.
  24. ;;
  25. ;; The only thing missing is a good scheme of inheritance. It is
  26. ;; possible to simulate a single level of inheritance with generous
  27. ;; use of hooks and a bit of work -- sgml-mode, for example, also runs
  28. ;; the hooks for text-mode, and keymaps can inherit from other keymaps
  29. ;; -- but generally, each major mode ends up reinventing the wheel.
  30. ;; Ideally, someone should redesign all of Emacs's major modes to
  31. ;; follow a more conventional object-oriented system: when defining a
  32. ;; new major mode, the user should need only to name the existing mode
  33. ;; it is most similar to, then list the (few) differences.
  34. ;;
  35. ;; In the mean time, this package offers most of the advantages of
  36. ;; full inheritance with the existing major modes. The macro
  37. ;; `define-derived-mode' allows the user to make a variant of an existing
  38. ;; major mode, with its own keymap. The new mode will inherit the key
  39. ;; bindings of its parent, and will, in fact, run its parent first
  40. ;; every time it is called. For example, the commands
  41. ;;
  42. ;; (define-derived-mode hypertext-mode text-mode "Hypertext"
  43. ;; "Major mode for hypertext.\n\n\\{hypertext-mode-map}"
  44. ;; (setq case-fold-search nil))
  45. ;;
  46. ;; (define-key hypertext-mode-map [down-mouse-3] 'do-hyper-link)
  47. ;;
  48. ;; will create a function `hypertext-mode' with its own (sparse)
  49. ;; keymap `hypertext-mode-map.' The command M-x hypertext-mode will
  50. ;; perform the following actions:
  51. ;;
  52. ;; - run the command (text-mode) to get its default setup
  53. ;; - replace the current keymap with 'hypertext-mode-map,' which will
  54. ;; inherit from 'text-mode-map'.
  55. ;; - replace the current syntax table with
  56. ;; 'hypertext-mode-syntax-table', which will borrow its defaults
  57. ;; from the current text-mode-syntax-table.
  58. ;; - replace the current abbrev table with
  59. ;; 'hypertext-mode-abbrev-table', which will borrow its defaults
  60. ;; from the current text-mode-abbrev table
  61. ;; - change the mode line to read "Hypertext"
  62. ;; - assign the value 'hypertext-mode' to the 'major-mode' variable
  63. ;; - run the body of commands provided in the macro -- in this case,
  64. ;; set the local variable `case-fold-search' to nil.
  65. ;;
  66. ;; The advantages of this system are threefold. First, text mode is
  67. ;; untouched -- if you had added the new keystroke to `text-mode-map,'
  68. ;; possibly using hooks, you would have added it to all text buffers
  69. ;; -- here, it appears only in hypertext buffers, where it makes
  70. ;; sense. Second, it is possible to build even further, and make
  71. ;; a derived mode from a derived mode. The commands
  72. ;;
  73. ;; (define-derived-mode html-mode hypertext-mode "HTML")
  74. ;; [various key definitions]
  75. ;;
  76. ;; will add a new major mode for HTML with very little fuss.
  77. ;;
  78. ;; Note also the function `derived-mode-p' which can tell if the current
  79. ;; mode derives from another. In a hypertext-mode, buffer, for example,
  80. ;; (derived-mode-p 'text-mode) would return non-nil. This should always
  81. ;; be used in place of (eq major-mode 'text-mode).
  82. ;;; Code:
  83. (eval-when-compile (require 'cl))
  84. ;;; PRIVATE: defsubst must be defined before they are first used
  85. (defsubst derived-mode-hook-name (mode)
  86. "Construct a mode-hook name based on a MODE name."
  87. (intern (concat (symbol-name mode) "-hook")))
  88. (defsubst derived-mode-map-name (mode)
  89. "Construct a map name based on a MODE name."
  90. (intern (concat (symbol-name mode) "-map")))
  91. (defsubst derived-mode-syntax-table-name (mode)
  92. "Construct a syntax-table name based on a MODE name."
  93. (intern (concat (symbol-name mode) "-syntax-table")))
  94. (defsubst derived-mode-abbrev-table-name (mode)
  95. "Construct an abbrev-table name based on a MODE name."
  96. (intern (concat (symbol-name mode) "-abbrev-table")))
  97. ;; PUBLIC: define a new major mode which inherits from an existing one.
  98. ;;;###autoload
  99. (defmacro define-derived-mode (child parent name &optional docstring &rest body)
  100. "Create a new mode as a variant of an existing mode.
  101. The arguments to this command are as follow:
  102. CHILD: the name of the command for the derived mode.
  103. PARENT: the name of the command for the parent mode (e.g. `text-mode')
  104. or nil if there is no parent.
  105. NAME: a string which will appear in the status line (e.g. \"Hypertext\")
  106. DOCSTRING: an optional documentation string--if you do not supply one,
  107. the function will attempt to invent something useful.
  108. BODY: forms to execute just before running the
  109. hooks for the new mode. Do not use `interactive' here.
  110. BODY can start with a bunch of keyword arguments. The following keyword
  111. arguments are currently understood:
  112. :group GROUP
  113. Declare the customization group that corresponds to this mode.
  114. The command `customize-mode' uses this.
  115. :syntax-table TABLE
  116. Use TABLE instead of the default (CHILD-syntax-table).
  117. A nil value means to simply use the same syntax-table as the parent.
  118. :abbrev-table TABLE
  119. Use TABLE instead of the default (CHILD-abbrev-table).
  120. A nil value means to simply use the same abbrev-table as the parent.
  121. Here is how you could define LaTeX-Thesis mode as a variant of LaTeX mode:
  122. (define-derived-mode LaTeX-thesis-mode LaTeX-mode \"LaTeX-Thesis\")
  123. You could then make new key bindings for `LaTeX-thesis-mode-map'
  124. without changing regular LaTeX mode. In this example, BODY is empty,
  125. and DOCSTRING is generated by default.
  126. On a more complicated level, the following command uses `sgml-mode' as
  127. the parent, and then sets the variable `case-fold-search' to nil:
  128. (define-derived-mode article-mode sgml-mode \"Article\"
  129. \"Major mode for editing technical articles.\"
  130. (setq case-fold-search nil))
  131. Note that if the documentation string had been left out, it would have
  132. been generated automatically, with a reference to the keymap.
  133. The new mode runs the hook constructed by the function
  134. `derived-mode-hook-name'.
  135. See Info node `(elisp)Derived Modes' for more details."
  136. (declare (debug (&define name symbolp sexp [&optional stringp]
  137. [&rest keywordp sexp] def-body))
  138. (doc-string 4))
  139. (when (and docstring (not (stringp docstring)))
  140. ;; Some trickiness, since what appears to be the docstring may really be
  141. ;; the first element of the body.
  142. (push docstring body)
  143. (setq docstring nil))
  144. (when (eq parent 'fundamental-mode) (setq parent nil))
  145. (let ((map (derived-mode-map-name child))
  146. (syntax (derived-mode-syntax-table-name child))
  147. (abbrev (derived-mode-abbrev-table-name child))
  148. (declare-abbrev t)
  149. (declare-syntax t)
  150. (hook (derived-mode-hook-name child))
  151. (group nil))
  152. ;; Process the keyword args.
  153. (while (keywordp (car body))
  154. (case (pop body)
  155. (:group (setq group (pop body)))
  156. (:abbrev-table (setq abbrev (pop body)) (setq declare-abbrev nil))
  157. (:syntax-table (setq syntax (pop body)) (setq declare-syntax nil))
  158. (t (pop body))))
  159. (setq docstring (derived-mode-make-docstring
  160. parent child docstring syntax abbrev))
  161. `(progn
  162. (unless (get ',hook 'variable-documentation)
  163. (put ',hook 'variable-documentation
  164. (purecopy ,(format "Hook run when entering %s mode.
  165. No problems result if this variable is not bound.
  166. `add-hook' automatically binds it. (This is true for all hook variables.)"
  167. name))))
  168. (unless (boundp ',map)
  169. (put ',map 'definition-name ',child))
  170. (with-no-warnings (defvar ,map (make-sparse-keymap)))
  171. (unless (get ',map 'variable-documentation)
  172. (put ',map 'variable-documentation
  173. (purecopy ,(format "Keymap for `%s'." child))))
  174. ,(if declare-syntax
  175. `(progn
  176. (unless (boundp ',syntax)
  177. (put ',syntax 'definition-name ',child))
  178. (defvar ,syntax (make-syntax-table))
  179. (unless (get ',syntax 'variable-documentation)
  180. (put ',syntax 'variable-documentation
  181. (purecopy ,(format "Syntax table for `%s'." child))))))
  182. ,(if declare-abbrev
  183. `(progn
  184. (put ',abbrev 'definition-name ',child)
  185. (defvar ,abbrev
  186. (progn (define-abbrev-table ',abbrev nil) ,abbrev))
  187. (unless (get ',abbrev 'variable-documentation)
  188. (put ',abbrev 'variable-documentation
  189. (purecopy ,(format "Abbrev table for `%s'." child))))))
  190. (put ',child 'derived-mode-parent ',parent)
  191. ,(if group `(put ',child 'custom-mode-group ,group))
  192. (defun ,child ()
  193. ,docstring
  194. (interactive)
  195. ; Run the parent.
  196. (delay-mode-hooks
  197. (,(or parent 'kill-all-local-variables))
  198. ; Identify the child mode.
  199. (setq major-mode (quote ,child))
  200. (setq mode-name ,name)
  201. ; Identify special modes.
  202. ,(when parent
  203. `(progn
  204. (if (get (quote ,parent) 'mode-class)
  205. (put (quote ,child) 'mode-class
  206. (get (quote ,parent) 'mode-class)))
  207. ; Set up maps and tables.
  208. (unless (keymap-parent ,map)
  209. ;; It would probably be better to set the keymap's parent
  210. ;; at the toplevel rather than inside the mode function,
  211. ;; but this is not easy for at least the following reasons:
  212. ;; - the parent (and its keymap) may not yet be loaded.
  213. ;; - the parent's keymap name may be called something else
  214. ;; than <parent>-mode-map.
  215. (set-keymap-parent ,map (current-local-map)))
  216. ,(when declare-syntax
  217. `(let ((parent (char-table-parent ,syntax)))
  218. (unless (and parent
  219. (not (eq parent (standard-syntax-table))))
  220. (set-char-table-parent ,syntax (syntax-table)))))
  221. ,(when declare-abbrev
  222. `(unless (or (abbrev-table-get ,abbrev :parents)
  223. ;; This can happen if the major mode defines
  224. ;; the abbrev-table to be its parent's.
  225. (eq ,abbrev local-abbrev-table))
  226. (abbrev-table-put ,abbrev :parents
  227. (list local-abbrev-table))))))
  228. (use-local-map ,map)
  229. ,(when syntax `(set-syntax-table ,syntax))
  230. ,(when abbrev `(setq local-abbrev-table ,abbrev))
  231. ; Splice in the body (if any).
  232. ,@body
  233. )
  234. ;; Run the hooks, if any.
  235. (run-mode-hooks ',hook)))))
  236. ;; PUBLIC: find the ultimate class of a derived mode.
  237. (defun derived-mode-class (mode)
  238. "Find the class of a major MODE.
  239. A mode's class is the first ancestor which is NOT a derived mode.
  240. Use the `derived-mode-parent' property of the symbol to trace backwards.
  241. Since major-modes might all derive from `fundamental-mode', this function
  242. is not very useful."
  243. (while (get mode 'derived-mode-parent)
  244. (setq mode (get mode 'derived-mode-parent)))
  245. mode)
  246. (make-obsolete 'derived-mode-class 'derived-mode-p "22.1")
  247. ;;; PRIVATE
  248. (defun derived-mode-make-docstring (parent child &optional
  249. docstring syntax abbrev)
  250. "Construct a docstring for a new mode if none is provided."
  251. (let ((map (derived-mode-map-name child))
  252. (hook (derived-mode-hook-name child)))
  253. (unless (stringp docstring)
  254. ;; Use a default docstring.
  255. (setq docstring
  256. (if (null parent)
  257. (format "Major-mode.
  258. Uses keymap `%s', abbrev table `%s' and syntax-table `%s'." map abbrev syntax)
  259. (format "Major mode derived from `%s' by `define-derived-mode'.
  260. It inherits all of the parent's attributes, but has its own keymap,
  261. abbrev table and syntax table:
  262. `%s', `%s' and `%s'
  263. which more-or-less shadow %s's corresponding tables."
  264. parent map abbrev syntax parent))))
  265. (unless (string-match (regexp-quote (symbol-name hook)) docstring)
  266. ;; Make sure the docstring mentions the mode's hook.
  267. (setq docstring
  268. (concat docstring
  269. (if (null parent)
  270. "\n\nThis mode "
  271. (concat
  272. "\n\nIn addition to any hooks its parent mode "
  273. (if (string-match (regexp-quote (format "`%s'" parent))
  274. docstring) nil
  275. (format "`%s' " parent))
  276. "might have run,\nthis mode "))
  277. (format "runs the hook `%s'" hook)
  278. ", as the final step\nduring initialization.")))
  279. (unless (string-match "\\\\[{[]" docstring)
  280. ;; And don't forget to put the mode's keymap.
  281. (setq docstring (concat docstring "\n\n\\{" (symbol-name map) "}")))
  282. docstring))
  283. ;;; OBSOLETE
  284. ;; The functions below are only provided for backward compatibility with
  285. ;; code byte-compiled with versions of derived.el prior to Emacs-21.
  286. (defsubst derived-mode-setup-function-name (mode)
  287. "Construct a setup-function name based on a MODE name."
  288. (intern (concat (symbol-name mode) "-setup")))
  289. ;; Utility functions for defining a derived mode.
  290. ;;;###autoload
  291. (defun derived-mode-init-mode-variables (mode)
  292. "Initialize variables for a new MODE.
  293. Right now, if they don't already exist, set up a blank keymap, an
  294. empty syntax table, and an empty abbrev table -- these will be merged
  295. the first time the mode is used."
  296. (if (boundp (derived-mode-map-name mode))
  297. t
  298. (eval `(defvar ,(derived-mode-map-name mode)
  299. (make-sparse-keymap)
  300. ,(format "Keymap for %s." mode)))
  301. (put (derived-mode-map-name mode) 'derived-mode-unmerged t))
  302. (if (boundp (derived-mode-syntax-table-name mode))
  303. t
  304. (eval `(defvar ,(derived-mode-syntax-table-name mode)
  305. ;; Make a syntax table which doesn't specify anything
  306. ;; for any char. Valid data will be merged in by
  307. ;; derived-mode-merge-syntax-tables.
  308. (make-char-table 'syntax-table nil)
  309. ,(format "Syntax table for %s." mode)))
  310. (put (derived-mode-syntax-table-name mode) 'derived-mode-unmerged t))
  311. (if (boundp (derived-mode-abbrev-table-name mode))
  312. t
  313. (eval `(defvar ,(derived-mode-abbrev-table-name mode)
  314. (progn
  315. (define-abbrev-table (derived-mode-abbrev-table-name mode) nil)
  316. (make-abbrev-table))
  317. ,(format "Abbrev table for %s." mode)))))
  318. ;; Utility functions for running a derived mode.
  319. (defun derived-mode-set-keymap (mode)
  320. "Set the keymap of the new MODE, maybe merging with the parent."
  321. (let* ((map-name (derived-mode-map-name mode))
  322. (new-map (eval map-name))
  323. (old-map (current-local-map)))
  324. (and old-map
  325. (get map-name 'derived-mode-unmerged)
  326. (derived-mode-merge-keymaps old-map new-map))
  327. (put map-name 'derived-mode-unmerged nil)
  328. (use-local-map new-map)))
  329. (defun derived-mode-set-syntax-table (mode)
  330. "Set the syntax table of the new MODE, maybe merging with the parent."
  331. (let* ((table-name (derived-mode-syntax-table-name mode))
  332. (old-table (syntax-table))
  333. (new-table (eval table-name)))
  334. (if (get table-name 'derived-mode-unmerged)
  335. (derived-mode-merge-syntax-tables old-table new-table))
  336. (put table-name 'derived-mode-unmerged nil)
  337. (set-syntax-table new-table)))
  338. (defun derived-mode-set-abbrev-table (mode)
  339. "Set the abbrev table for MODE if it exists.
  340. Always merge its parent into it, since the merge is non-destructive."
  341. (let* ((table-name (derived-mode-abbrev-table-name mode))
  342. (old-table local-abbrev-table)
  343. (new-table (eval table-name)))
  344. (derived-mode-merge-abbrev-tables old-table new-table)
  345. (setq local-abbrev-table new-table)))
  346. (defun derived-mode-run-hooks (mode)
  347. "Run the mode hook for MODE."
  348. (let ((hooks-name (derived-mode-hook-name mode)))
  349. (if (boundp hooks-name)
  350. (run-hooks hooks-name))))
  351. ;; Functions to merge maps and tables.
  352. (defun derived-mode-merge-keymaps (old new)
  353. "Merge an OLD keymap into a NEW one.
  354. The old keymap is set to be the last cdr of the new one, so that there will
  355. be automatic inheritance."
  356. ;; ?? Can this just use `set-keymap-parent'?
  357. (let ((tail new))
  358. ;; Scan the NEW map for prefix keys.
  359. (while (consp tail)
  360. (and (consp (car tail))
  361. (let* ((key (vector (car (car tail))))
  362. (subnew (lookup-key new key))
  363. (subold (lookup-key old key)))
  364. ;; If KEY is a prefix key in both OLD and NEW, merge them.
  365. (and (keymapp subnew) (keymapp subold)
  366. (derived-mode-merge-keymaps subold subnew))))
  367. (and (vectorp (car tail))
  368. ;; Search a vector of ASCII char bindings for prefix keys.
  369. (let ((i (1- (length (car tail)))))
  370. (while (>= i 0)
  371. (let* ((key (vector i))
  372. (subnew (lookup-key new key))
  373. (subold (lookup-key old key)))
  374. ;; If KEY is a prefix key in both OLD and NEW, merge them.
  375. (and (keymapp subnew) (keymapp subold)
  376. (derived-mode-merge-keymaps subold subnew)))
  377. (setq i (1- i)))))
  378. (setq tail (cdr tail))))
  379. (setcdr (nthcdr (1- (length new)) new) old))
  380. (defun derived-mode-merge-syntax-tables (old new)
  381. "Merge an OLD syntax table into a NEW one.
  382. Where the new table already has an entry, nothing is copied from the old one."
  383. (set-char-table-parent new old))
  384. ;; Merge an old abbrev table into a new one.
  385. ;; This function requires internal knowledge of how abbrev tables work,
  386. ;; presuming that they are obarrays with the abbrev as the symbol, the expansion
  387. ;; as the value of the symbol, and the hook as the function definition.
  388. (defun derived-mode-merge-abbrev-tables (old new)
  389. (if old
  390. (mapatoms
  391. (lambda (symbol)
  392. (or (intern-soft (symbol-name symbol) new)
  393. (define-abbrev new (symbol-name symbol)
  394. (symbol-value symbol) (symbol-function symbol))))
  395. old)))
  396. (provide 'derived)
  397. ;;; derived.el ends here