easy-mmode.el 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. ;;; easy-mmode.el --- easy definition for major and minor modes
  2. ;; Copyright (C) 1997, 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Georges Brun-Cottan <Georges.Brun-Cottan@inria.fr>
  4. ;; Maintainer: Stefan Monnier <monnier@gnu.org>
  5. ;; Package: emacs
  6. ;; Keywords: extensions lisp
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Minor modes are useful and common. This package makes defining a
  20. ;; minor mode easy, by focusing on the writing of the minor mode
  21. ;; functionalities themselves. Moreover, this package enforces a
  22. ;; conventional naming of user interface primitives, making things
  23. ;; natural for the minor-mode end-users.
  24. ;; For each mode, easy-mmode defines the following:
  25. ;; <mode> : The minor mode predicate. A buffer-local variable.
  26. ;; <mode>-map : The keymap possibly associated to <mode>.
  27. ;; see `define-minor-mode' documentation
  28. ;;
  29. ;; eval
  30. ;; (pp (macroexpand '(define-minor-mode <your-mode> <doc>)))
  31. ;; to check the result before using it.
  32. ;; The order in which minor modes are installed is important. Keymap
  33. ;; lookup proceeds down minor-mode-map-alist, and the order there
  34. ;; tends to be the reverse of the order in which the modes were
  35. ;; installed. Perhaps there should be a feature to let you specify
  36. ;; orderings.
  37. ;; Additionally to `define-minor-mode', the package provides convenient
  38. ;; ways to define keymaps, and other helper functions for major and minor modes.
  39. ;;; Code:
  40. (defun easy-mmode-pretty-mode-name (mode &optional lighter)
  41. "Turn the symbol MODE into a string intended for the user.
  42. If provided, LIGHTER will be used to help choose capitalization by,
  43. replacing its case-insensitive matches with the literal string in LIGHTER."
  44. (let* ((case-fold-search t)
  45. ;; Produce "Foo-Bar minor mode" from foo-bar-minor-mode.
  46. (name (concat (replace-regexp-in-string
  47. ;; If the original mode name included "-minor" (some
  48. ;; of them don't, e.g. auto-revert-mode), then
  49. ;; replace it with " minor".
  50. "-Minor" " minor"
  51. ;; "foo-bar-minor" -> "Foo-Bar-Minor"
  52. (capitalize (replace-regexp-in-string
  53. ;; "foo-bar-minor-mode" -> "foo-bar-minor"
  54. "-mode\\'" "" (symbol-name mode))))
  55. " mode")))
  56. (if (not (stringp lighter)) name
  57. ;; Strip leading and trailing whitespace from LIGHTER.
  58. (setq lighter (replace-regexp-in-string "\\`\\s-+\\|\\s-+\\'" ""
  59. lighter))
  60. ;; Replace any (case-insensitive) matches for LIGHTER in NAME
  61. ;; with a literal LIGHTER. E.g., if NAME is "Iimage mode" and
  62. ;; LIGHTER is " iImag", then this will produce "iImage mode".
  63. ;; (LIGHTER normally comes from the mode-line string passed to
  64. ;; define-minor-mode, and normally includes at least one leading
  65. ;; space.)
  66. (replace-regexp-in-string (regexp-quote lighter) lighter name t t))))
  67. ;;;###autoload
  68. (defalias 'easy-mmode-define-minor-mode 'define-minor-mode)
  69. ;;;###autoload
  70. (defmacro define-minor-mode (mode doc &optional init-value lighter keymap &rest body)
  71. "Define a new minor mode MODE.
  72. This defines the toggle command MODE and (by default) a control variable
  73. MODE (you can override this with the :variable keyword, see below).
  74. DOC is the documentation for the mode toggle command.
  75. The defined mode command takes one optional (prefix) argument.
  76. Interactively with no prefix argument it toggles the mode.
  77. With a prefix argument, it enables the mode if the argument is
  78. positive and otherwise disables it. When called from Lisp, it
  79. enables the mode if the argument is omitted or nil, and toggles
  80. the mode if the argument is `toggle'. If DOC is nil this
  81. function adds a basic doc-string stating these facts.
  82. Optional INIT-VALUE is the initial value of the mode's variable.
  83. Optional LIGHTER is displayed in the mode line when the mode is on.
  84. Optional KEYMAP is the default keymap bound to the mode keymap.
  85. If non-nil, it should be a variable name (whose value is a keymap),
  86. or an expression that returns either a keymap or a list of
  87. arguments for `easy-mmode-define-keymap'. If you supply a KEYMAP
  88. argument that is not a symbol, this macro defines the variable
  89. MODE-map and gives it the value that KEYMAP specifies.
  90. BODY contains code to execute each time the mode is enabled or disabled.
  91. It is executed after toggling the mode, and before running MODE-hook.
  92. Before the actual body code, you can write keyword arguments, i.e.
  93. alternating keywords and values. These following special keywords
  94. are supported (other keywords are passed to `defcustom' if the minor
  95. mode is global):
  96. :group GROUP Custom group name to use in all generated `defcustom' forms.
  97. Defaults to MODE without the possible trailing \"-mode\".
  98. Don't use this default group name unless you have written a
  99. `defgroup' to define that group properly.
  100. :global GLOBAL If non-nil specifies that the minor mode is not meant to be
  101. buffer-local, so don't make the variable MODE buffer-local.
  102. By default, the mode is buffer-local.
  103. :init-value VAL Same as the INIT-VALUE argument.
  104. Not used if you also specify :variable.
  105. :lighter SPEC Same as the LIGHTER argument.
  106. :keymap MAP Same as the KEYMAP argument.
  107. :require SYM Same as in `defcustom'.
  108. :variable PLACE The location to use instead of the variable MODE to store
  109. the state of the mode. This can be simply a different
  110. named variable, or more generally anything that can be used
  111. with the CL macro `setf'. PLACE can also be of the form
  112. \(GET . SET), where GET is an expression that returns the
  113. current state, and SET is a function that takes one argument,
  114. the new state, and sets it. If you specify a :variable,
  115. this function does not define a MODE variable (nor any of
  116. the terms used in :variable).
  117. :after-hook A single lisp form which is evaluated after the mode hooks
  118. have been run. It should not be quoted.
  119. For example, you could write
  120. (define-minor-mode foo-mode \"If enabled, foo on you!\"
  121. :lighter \" Foo\" :require 'foo :global t :group 'hassle :version \"27.5\"
  122. ...BODY CODE...)"
  123. (declare (doc-string 2)
  124. (debug (&define name stringp
  125. [&optional [&not keywordp] sexp
  126. &optional [&not keywordp] sexp
  127. &optional [&not keywordp] sexp]
  128. [&rest [keywordp sexp]]
  129. def-body)))
  130. ;; Allow skipping the first three args.
  131. (cond
  132. ((keywordp init-value)
  133. (setq body `(,init-value ,lighter ,keymap ,@body)
  134. init-value nil lighter nil keymap nil))
  135. ((keywordp lighter)
  136. (setq body `(,lighter ,keymap ,@body) lighter nil keymap nil))
  137. ((keywordp keymap) (push keymap body) (setq keymap nil)))
  138. (let* ((last-message (make-symbol "last-message"))
  139. (mode-name (symbol-name mode))
  140. (pretty-name (easy-mmode-pretty-mode-name mode lighter))
  141. (globalp nil)
  142. (set nil)
  143. (initialize nil)
  144. (group nil)
  145. (type nil)
  146. (extra-args nil)
  147. (extra-keywords nil)
  148. (variable nil) ;The PLACE where the state is stored.
  149. (setter nil) ;The function (if any) to set the mode var.
  150. (modefun mode) ;The minor mode function name we're defining.
  151. (require t)
  152. (after-hook nil)
  153. (hook (intern (concat mode-name "-hook")))
  154. (hook-on (intern (concat mode-name "-on-hook")))
  155. (hook-off (intern (concat mode-name "-off-hook")))
  156. keyw keymap-sym tmp)
  157. ;; Check keys.
  158. (while (keywordp (setq keyw (car body)))
  159. (setq body (cdr body))
  160. (pcase keyw
  161. (`:init-value (setq init-value (pop body)))
  162. (`:lighter (setq lighter (purecopy (pop body))))
  163. (`:global (setq globalp (pop body)))
  164. (`:extra-args (setq extra-args (pop body)))
  165. (`:set (setq set (list :set (pop body))))
  166. (`:initialize (setq initialize (list :initialize (pop body))))
  167. (`:group (setq group (nconc group (list :group (pop body)))))
  168. (`:type (setq type (list :type (pop body))))
  169. (`:require (setq require (pop body)))
  170. (`:keymap (setq keymap (pop body)))
  171. (`:variable (setq variable (pop body))
  172. (if (not (and (setq tmp (cdr-safe variable))
  173. (or (symbolp tmp)
  174. (functionp tmp))))
  175. ;; PLACE is not of the form (GET . SET).
  176. (setq mode variable)
  177. (setq mode (car variable))
  178. (setq setter (cdr variable))))
  179. (`:after-hook (setq after-hook (pop body)))
  180. (_ (push keyw extra-keywords) (push (pop body) extra-keywords))))
  181. (setq keymap-sym (if (and keymap (symbolp keymap)) keymap
  182. (intern (concat mode-name "-map"))))
  183. (unless set (setq set '(:set 'custom-set-minor-mode)))
  184. (unless initialize
  185. (setq initialize '(:initialize 'custom-initialize-default)))
  186. (unless group
  187. ;; We might as well provide a best-guess default group.
  188. (setq group
  189. `(:group ',(intern (replace-regexp-in-string
  190. "-mode\\'" "" mode-name)))))
  191. ;; TODO? Mark booleans as safe if booleanp? Eg abbrev-mode.
  192. (unless type (setq type '(:type 'boolean)))
  193. `(progn
  194. ;; Define the variable to enable or disable the mode.
  195. ,(cond
  196. ;; If :variable is specified, then the var will be
  197. ;; declared elsewhere.
  198. (variable nil)
  199. ((not globalp)
  200. `(progn
  201. :autoload-end
  202. (defvar ,mode ,init-value ,(format "Non-nil if %s is enabled.
  203. Use the command `%s' to change this variable." pretty-name mode))
  204. (make-variable-buffer-local ',mode)))
  205. (t
  206. (let ((base-doc-string
  207. (concat "Non-nil if %s is enabled.
  208. See the command `%s' for a description of this minor mode."
  209. (if body "
  210. Setting this variable directly does not take effect;
  211. either customize it (see the info node `Easy Customization')
  212. or call the function `%s'."))))
  213. `(defcustom ,mode ,init-value
  214. ,(format base-doc-string pretty-name mode mode)
  215. ,@set
  216. ,@initialize
  217. ,@group
  218. ,@type
  219. ,@(unless (eq require t) `(:require ,require))
  220. ,@(nreverse extra-keywords)))))
  221. ;; The actual function.
  222. (defun ,modefun (&optional arg ,@extra-args)
  223. ,(or doc
  224. (format (concat "Toggle %s on or off.
  225. With a prefix argument ARG, enable %s if ARG is
  226. positive, and disable it otherwise. If called from Lisp, enable
  227. the mode if ARG is omitted or nil, and toggle it if ARG is `toggle'.
  228. \\{%s}") pretty-name pretty-name keymap-sym))
  229. ;; Use `toggle' rather than (if ,mode 0 1) so that using
  230. ;; repeat-command still does the toggling correctly.
  231. (interactive (list (or current-prefix-arg 'toggle)))
  232. (let ((,last-message (current-message)))
  233. (,@(if setter `(funcall #',setter)
  234. (list (if (symbolp mode) 'setq 'setf) mode))
  235. (if (eq arg 'toggle)
  236. (not ,mode)
  237. ;; A nil argument also means ON now.
  238. (> (prefix-numeric-value arg) 0)))
  239. ,@body
  240. ;; The on/off hooks are here for backward compatibility only.
  241. (run-hooks ',hook (if ,mode ',hook-on ',hook-off))
  242. (if (called-interactively-p 'any)
  243. (progn
  244. ,(if (and globalp (symbolp mode))
  245. `(customize-mark-as-set ',mode))
  246. ;; Avoid overwriting a message shown by the body,
  247. ;; but do overwrite previous messages.
  248. (unless (and (current-message)
  249. (not (equal ,last-message
  250. (current-message))))
  251. (message ,(format "%s %%sabled" pretty-name)
  252. (if ,mode "en" "dis")))))
  253. ,@(when after-hook `(,after-hook)))
  254. (force-mode-line-update)
  255. ;; Return the new setting.
  256. ,mode)
  257. ;; Autoloading a define-minor-mode autoloads everything
  258. ;; up-to-here.
  259. :autoload-end
  260. ;; Define the minor-mode keymap.
  261. ,(unless (symbolp keymap) ;nil is also a symbol.
  262. `(defvar ,keymap-sym
  263. (let ((m ,keymap))
  264. (cond ((keymapp m) m)
  265. ((listp m) (easy-mmode-define-keymap m))
  266. (t (error "Invalid keymap %S" m))))
  267. ,(format "Keymap for `%s'." mode-name)))
  268. ,(if (not (symbolp mode))
  269. (if (or lighter keymap)
  270. (error ":lighter and :keymap unsupported with mode expression %s" mode))
  271. `(with-no-warnings
  272. (add-minor-mode ',mode ',lighter
  273. ,(if keymap keymap-sym
  274. `(if (boundp ',keymap-sym) ,keymap-sym))
  275. nil
  276. ,(unless (eq mode modefun) `',modefun)))))))
  277. ;;;
  278. ;;; make global minor mode
  279. ;;;
  280. ;;;###autoload
  281. (defalias 'easy-mmode-define-global-mode 'define-globalized-minor-mode)
  282. ;;;###autoload
  283. (defalias 'define-global-minor-mode 'define-globalized-minor-mode)
  284. ;;;###autoload
  285. (defmacro define-globalized-minor-mode (global-mode mode turn-on &rest keys)
  286. "Make a global mode GLOBAL-MODE corresponding to buffer-local minor MODE.
  287. TURN-ON is a function that will be called with no args in every buffer
  288. and that should try to turn MODE on if applicable for that buffer.
  289. KEYS is a list of CL-style keyword arguments. As the minor mode
  290. defined by this function is always global, any :global keyword is
  291. ignored. Other keywords have the same meaning as in `define-minor-mode',
  292. which see. In particular, :group specifies the custom group.
  293. The most useful keywords are those that are passed on to the
  294. `defcustom'. It normally makes no sense to pass the :lighter
  295. or :keymap keywords to `define-globalized-minor-mode', since these
  296. are usually passed to the buffer-local version of the minor mode.
  297. If MODE's set-up depends on the major mode in effect when it was
  298. enabled, then disabling and reenabling MODE should make MODE work
  299. correctly with the current major mode. This is important to
  300. prevent problems with derived modes, that is, major modes that
  301. call another major mode in their body."
  302. (declare (doc-string 2))
  303. (let* ((global-mode-name (symbol-name global-mode))
  304. (pretty-name (easy-mmode-pretty-mode-name mode))
  305. (pretty-global-name (easy-mmode-pretty-mode-name global-mode))
  306. (group nil)
  307. (extra-keywords nil)
  308. (MODE-buffers (intern (concat global-mode-name "-buffers")))
  309. (MODE-enable-in-buffers
  310. (intern (concat global-mode-name "-enable-in-buffers")))
  311. (MODE-check-buffers
  312. (intern (concat global-mode-name "-check-buffers")))
  313. (MODE-cmhh (intern (concat global-mode-name "-cmhh")))
  314. (MODE-major-mode (intern (concat (symbol-name mode) "-major-mode")))
  315. keyw)
  316. ;; Check keys.
  317. (while (keywordp (setq keyw (car keys)))
  318. (setq keys (cdr keys))
  319. (pcase keyw
  320. (`:group (setq group (nconc group (list :group (pop keys)))))
  321. (`:global (setq keys (cdr keys)))
  322. (_ (push keyw extra-keywords) (push (pop keys) extra-keywords))))
  323. (unless group
  324. ;; We might as well provide a best-guess default group.
  325. (setq group
  326. `(:group ',(intern (replace-regexp-in-string
  327. "-mode\\'" "" (symbol-name mode))))))
  328. `(progn
  329. (progn
  330. :autoload-end
  331. (defvar ,MODE-major-mode nil)
  332. (make-variable-buffer-local ',MODE-major-mode))
  333. ;; The actual global minor-mode
  334. (define-minor-mode ,global-mode
  335. ;; Very short lines to avoid too long lines in the generated
  336. ;; doc string.
  337. ,(format "Toggle %s in all buffers.
  338. With prefix ARG, enable %s if ARG is positive;
  339. otherwise, disable it. If called from Lisp, enable the mode if
  340. ARG is omitted or nil.
  341. %s is enabled in all buffers where
  342. \`%s' would do it.
  343. See `%s' for more information on %s."
  344. pretty-name pretty-global-name
  345. pretty-name turn-on mode pretty-name)
  346. :global t ,@group ,@(nreverse extra-keywords)
  347. ;; Setup hook to handle future mode changes and new buffers.
  348. (if ,global-mode
  349. (progn
  350. (add-hook 'after-change-major-mode-hook
  351. ',MODE-enable-in-buffers)
  352. (add-hook 'change-major-mode-after-body-hook
  353. ',MODE-enable-in-buffers)
  354. (add-hook 'find-file-hook ',MODE-check-buffers)
  355. (add-hook 'change-major-mode-hook ',MODE-cmhh))
  356. (remove-hook 'after-change-major-mode-hook ',MODE-enable-in-buffers)
  357. (remove-hook 'change-major-mode-after-body-hook
  358. ',MODE-enable-in-buffers)
  359. (remove-hook 'find-file-hook ',MODE-check-buffers)
  360. (remove-hook 'change-major-mode-hook ',MODE-cmhh))
  361. ;; Go through existing buffers.
  362. (dolist (buf (buffer-list))
  363. (with-current-buffer buf
  364. (if ,global-mode (,turn-on) (when ,mode (,mode -1))))))
  365. ;; Autoloading define-globalized-minor-mode autoloads everything
  366. ;; up-to-here.
  367. :autoload-end
  368. ;; List of buffers left to process.
  369. (defvar ,MODE-buffers nil)
  370. ;; The function that calls TURN-ON in each buffer.
  371. (defun ,MODE-enable-in-buffers ()
  372. (dolist (buf ,MODE-buffers)
  373. (when (buffer-live-p buf)
  374. (with-current-buffer buf
  375. (unless (eq ,MODE-major-mode major-mode)
  376. (if ,mode
  377. (progn
  378. (,mode -1)
  379. (,turn-on)
  380. (setq ,MODE-major-mode major-mode))
  381. (,turn-on)
  382. (setq ,MODE-major-mode major-mode)))))))
  383. (put ',MODE-enable-in-buffers 'definition-name ',global-mode)
  384. (defun ,MODE-check-buffers ()
  385. (,MODE-enable-in-buffers)
  386. (setq ,MODE-buffers nil)
  387. (remove-hook 'post-command-hook ',MODE-check-buffers))
  388. (put ',MODE-check-buffers 'definition-name ',global-mode)
  389. ;; The function that catches kill-all-local-variables.
  390. (defun ,MODE-cmhh ()
  391. (add-to-list ',MODE-buffers (current-buffer))
  392. (add-hook 'post-command-hook ',MODE-check-buffers))
  393. (put ',MODE-cmhh 'definition-name ',global-mode))))
  394. ;;;
  395. ;;; easy-mmode-defmap
  396. ;;;
  397. (eval-and-compile
  398. (if (fboundp 'set-keymap-parents)
  399. (defalias 'easy-mmode-set-keymap-parents 'set-keymap-parents)
  400. (defun easy-mmode-set-keymap-parents (m parents)
  401. (set-keymap-parent
  402. m
  403. (cond
  404. ((not (consp parents)) parents)
  405. ((not (cdr parents)) (car parents))
  406. (t (let ((m (copy-keymap (pop parents))))
  407. (easy-mmode-set-keymap-parents m parents)
  408. m)))))))
  409. ;;;###autoload
  410. (defun easy-mmode-define-keymap (bs &optional name m args)
  411. "Return a keymap built from bindings BS.
  412. BS must be a list of (KEY . BINDING) where
  413. KEY and BINDINGS are suitable for `define-key'.
  414. Optional NAME is passed to `make-sparse-keymap'.
  415. Optional map M can be used to modify an existing map.
  416. ARGS is a list of additional keyword arguments.
  417. Valid keywords and arguments are:
  418. :name Name of the keymap; overrides NAME argument.
  419. :dense Non-nil for a dense keymap.
  420. :inherit Parent keymap.
  421. :group Ignored.
  422. :suppress Non-nil to call `suppress-keymap' on keymap,
  423. 'nodigits to suppress digits as prefix arguments."
  424. (let (inherit dense suppress)
  425. (while args
  426. (let ((key (pop args))
  427. (val (pop args)))
  428. (pcase key
  429. (`:name (setq name val))
  430. (`:dense (setq dense val))
  431. (`:inherit (setq inherit val))
  432. (`:suppress (setq suppress val))
  433. (`:group)
  434. (_ (message "Unknown argument %s in defmap" key)))))
  435. (unless (keymapp m)
  436. (setq bs (append m bs))
  437. (setq m (if dense (make-keymap name) (make-sparse-keymap name))))
  438. (when suppress
  439. (suppress-keymap m (eq suppress 'nodigits)))
  440. (dolist (b bs)
  441. (let ((keys (car b))
  442. (binding (cdr b)))
  443. (dolist (key (if (consp keys) keys (list keys)))
  444. (cond
  445. ((symbolp key)
  446. (substitute-key-definition key binding m global-map))
  447. ((null binding)
  448. (unless (keymapp (lookup-key m key)) (define-key m key binding)))
  449. ((let ((o (lookup-key m key)))
  450. (or (null o) (numberp o) (eq o 'undefined)))
  451. (define-key m key binding))))))
  452. (cond
  453. ((keymapp inherit) (set-keymap-parent m inherit))
  454. ((consp inherit) (easy-mmode-set-keymap-parents m inherit)))
  455. m))
  456. ;;;###autoload
  457. (defmacro easy-mmode-defmap (m bs doc &rest args)
  458. "Define a constant M whose value is the result of `easy-mmode-define-keymap'.
  459. The M, BS, and ARGS arguments are as per that function. DOC is
  460. the constant's documentation."
  461. `(defconst ,m
  462. (easy-mmode-define-keymap ,bs nil (if (boundp ',m) ,m) ,(cons 'list args))
  463. ,doc))
  464. ;;;
  465. ;;; easy-mmode-defsyntax
  466. ;;;
  467. (defun easy-mmode-define-syntax (css args)
  468. (let ((st (make-syntax-table (plist-get args :copy)))
  469. (parent (plist-get args :inherit)))
  470. (dolist (cs css)
  471. (let ((char (car cs))
  472. (syntax (cdr cs)))
  473. (if (sequencep char)
  474. (mapc (lambda (c) (modify-syntax-entry c syntax st)) char)
  475. (modify-syntax-entry char syntax st))))
  476. (if parent (set-char-table-parent
  477. st (if (symbolp parent) (symbol-value parent) parent)))
  478. st))
  479. ;;;###autoload
  480. (defmacro easy-mmode-defsyntax (st css doc &rest args)
  481. "Define variable ST as a syntax-table.
  482. CSS contains a list of syntax specifications of the form (CHAR . SYNTAX)."
  483. `(progn
  484. (autoload 'easy-mmode-define-syntax "easy-mmode")
  485. (defconst ,st (easy-mmode-define-syntax ,css ,(cons 'list args)) ,doc)))
  486. ;;;
  487. ;;; easy-mmode-define-navigation
  488. ;;;
  489. (defmacro easy-mmode-define-navigation (base re &optional name endfun narrowfun
  490. &rest body)
  491. "Define BASE-next and BASE-prev to navigate in the buffer.
  492. RE determines the places the commands should move point to.
  493. NAME should describe the entities matched by RE. It is used to build
  494. the docstrings of the two functions.
  495. BASE-next also tries to make sure that the whole entry is visible by
  496. searching for its end (by calling ENDFUN if provided or by looking for
  497. the next entry) and recentering if necessary.
  498. ENDFUN should return the end position (with or without moving point).
  499. NARROWFUN non-nil means to check for narrowing before moving, and if
  500. found, do `widen' first and then call NARROWFUN with no args after moving.
  501. BODY is executed after moving to the destination location."
  502. (declare (indent 5) (debug (exp exp exp def-form def-form &rest def-body)))
  503. (let* ((base-name (symbol-name base))
  504. (prev-sym (intern (concat base-name "-prev")))
  505. (next-sym (intern (concat base-name "-next")))
  506. (when-narrowed
  507. (lambda (body)
  508. (if (null narrowfun) body
  509. `(let ((was-narrowed
  510. (prog1 (or (< (- (point-max) (point-min)) (buffer-size)))
  511. (widen))))
  512. ,body
  513. (when was-narrowed (,narrowfun)))))))
  514. (unless name (setq name base-name))
  515. `(progn
  516. (defun ,next-sym (&optional count)
  517. ,(format "Go to the next COUNT'th %s." name)
  518. (interactive "p")
  519. (unless count (setq count 1))
  520. (if (< count 0) (,prev-sym (- count))
  521. (if (looking-at ,re) (setq count (1+ count)))
  522. ,(funcall when-narrowed
  523. `(if (not (re-search-forward ,re nil t count))
  524. (if (looking-at ,re)
  525. (goto-char (or ,(if endfun `(,endfun)) (point-max)))
  526. (user-error "No next %s" ,name))
  527. (goto-char (match-beginning 0))
  528. (when (and (eq (current-buffer) (window-buffer (selected-window)))
  529. (called-interactively-p 'interactive))
  530. (let ((endpt (or (save-excursion
  531. ,(if endfun `(,endfun)
  532. `(re-search-forward ,re nil t 2)))
  533. (point-max))))
  534. (unless (pos-visible-in-window-p endpt nil t)
  535. (recenter '(0)))))))
  536. ,@body))
  537. (put ',next-sym 'definition-name ',base)
  538. (defun ,prev-sym (&optional count)
  539. ,(format "Go to the previous COUNT'th %s" (or name base-name))
  540. (interactive "p")
  541. (unless count (setq count 1))
  542. (if (< count 0) (,next-sym (- count))
  543. ,(funcall when-narrowed
  544. `(unless (re-search-backward ,re nil t count)
  545. (user-error "No previous %s" ,name)))
  546. ,@body))
  547. (put ',prev-sym 'definition-name ',base))))
  548. (provide 'easy-mmode)
  549. ;;; easy-mmode.el ends here