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