flex-autopair

https://github.com/uk-ar/flex-autopair.git

git clone 'git://github.com/uk-ar/flex-autopair.git'
11

Notice

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.

Features

Default setting

Demo video

Setup

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)

Customizing action in specific condition

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

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

  1. Executing “pair” when you press open pair (open bracket, open paren, or open quote).
  2. Executing “skip” when you press close pair and the character at point is same as you press.
  3. 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.

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"))
 )

Adding pair character.

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.

  1. Adding new pair of character
  2. Adding condition of the pair.
  3. 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)

Adding conditions for the pair

Flex-autopair inserts a matching pair when you press open pair by default. So you should add 2 settings for “<” in c-mode.

  1. Executing “pair” when “#include” directive exist.
  2. 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.