I'm not good at English.
So please correct my English.
Flex Autopair automatically insert pair braces and quotes, insertion conditions & actions are highly customizable.
- Less configuration: Auto detect pairs from syntax table.
- More customizable: Insertion conditions & actions are highly customizable.
- Auto insert matching pair(e.g. quote bracket paren)
- Undo can cancel only auto insertion
- Skip close pair when you press by mistake
- Wrap something(e.g. word symbol url) with pairs In c like languages and lisp languages have additional behavior. It is explained in demo video.
You can install from github.
(auto-install-from-url "https://raw.github.com/uk-ar/flex-autopair/master/flex-autopair.el")
Or you can install from marmalade.
M-x package-install flex-autopair
Then modify your .emacs like this.
(require 'flex-autopair)
(flex-autopair-mode 1)
You can highly customize insertion conditions & actions in this elisp. These behavior can be described by flex-autopair-conditions & flex-autopair-actions variables.
flex-autopair-conditions means conditions when inserting action should execute. This variable is a association list of conditional expressions vs corresponding action names(symbol). An ordinary element of the alist looks like (sexp . action-name)
Here is the example code which is a part of default flex-autopair-conditions.
(setq flex-autopair-conditions
`(;; Insert matching pair.
(openp . pair)
;; Skip self.
((and closep
(eq (char-after) last-command-event)) . skip)
(closep . self)
))
flex-autopair.el searches flex-autopair-conditions for the first element in which conditional expression is true, then executes action of the element. In the setting show above behave like this
- Executing "pair" when you press open pair (open bracket, open paren, or open quote).
- Executing "skip" when you press close pair and the character at point is same as you press.
- Executing "self" when you press close pair and the other conditions.
openp is a variable which is set to t when you press open pair. closep is the opposite of openp: it is set to t if when you press close pair.
Those actions(pair, skip, and self) are mapped to actual behavior by flex-autopair-actions.
This variable is an association list of action names(symbol) vs corresponding behavior(any S-expressions). An ordinary element of the alist looks like (action-name . sexp)
Here is the example code which is a part of default flex-autopair-actions.
(setq flex-autopair-actions
'((pair . (progn (call-interactively 'self-insert-command)
(save-excursion
(insert closer))))
(skip . (forward-char 1))
(self . (call-interactively 'self-insert-command))
))
flex-autopair.el looks up the action name in flex-autopair-actions, and evaluates its associated S expression. The setting shown above behaves like this
- pair: Insert matching pair.
- skip: Only move forward one character.
- self: inserts the last character typed. You can add your setting to flex-autopair-conditions.
(setq flex-autopair-user-conditions-high
`((openp . hoge)
(closep . fuga)))
(flex-autopair-reload-conditions)
flex-autopair-reload-conditions applies flex-autopair-user-conditions-high to flex-autopair-conditions with proper priority. And you can add your setting in flex-autopair-conditions.
(add-to-list
'flex-autopair-actions
'(hoge . (message "this is hoge"))
)
Flex-autopair automatically detects pairs from the syntax table. But there are some situations when you want to insert a pair, and others when you only want to insert what you typed. For example, "<" should be treated as pair only after a "#include" directive in the C language.
You can do this with the following steps.
- Adding new pair of character
- Adding condition of the pair.
- Adding new pair of character
flex-autopair-pairs is a association list of open pair character vs close pair character. An ordinary element of the alist looks like (?open-pair . ?close-pair). flex-autopair-pairs is buffer local variable so that you can set pairs each major mode.
Here is the example code which enables "<" and ">" pair in c-mode.
(defun my-hook-function ()
(add-to-list 'flex-autopair-pairs '(?\< . ?\>)))
(add-hook 'c-mode-hook 'my-hook-function)
Flex-autopair inserts a matching pair when you press open pair by default. So you should add 2 settings for "<" in c-mode.
- Executing "pair" when "#include" directive exist.
- Executing "self" when "#include" directive is not exist. Sample code is like this.
(setq flex-autopair-user-conditions-high
`(((and
(eq major-mode 'c-mode)
(eq last-command-event ?<)
(save-excursion (re-search-backward "#include" (point-at-bol) t)))
. pair)
((and
(eq major-mode 'c-mode)
(eq last-command-event ?<))
. self)
))
(flex-autopair-reload-conditions)
That's all.