• This repository has been archived on 11/Apr/2021
  • Stars
    star
    208
  • Rank 189,015 (Top 4 %)
  • Language
    Emacs Lisp
  • Created over 12 years ago
  • Updated almost 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Automagically pair braces and quotes in emacs like TextMate

Autopair is an extension to the Emacs text editor that automatically pairs braces and quotes:

  • Opening braces/quotes are autopaired;
  • Closing braces/quotes are autoskipped;
  • Backspacing an opening brace/quote autodeletes its pair.
  • Newline between newly-opened brace pairs open an extra indented line.

Autopair works well across all Emacs major-modes, deduces from the language's syntax table which characters to pair, skip or delete. It should work even with extensions that redefine such keys. It also works with YASnippet, another package I maintain.

Important: in Emacs 24.4 you can try electric-pair-mode as an alternative to autopair. See below

Installation and basic use:

To try it out, download the latest version add to your .emacs

(add-to-list 'load-path "/path/to/autopair") ;; comment if autopair.el is in standard load path 
(require 'autopair)
(autopair-global-mode) ;; enable autopair in all buffers

Alternatives to autopair

I developed autopair to work just like Textmate or better, be minimally intrusive to my existing hacks and need very little customization. You might prefer it to the following:

Differences to smartparens

Note in particular that smartparens claims in its README that it provides "the basic funcionality [of autopair]". I don't know why this claim is made, since at time of writing:

  1. it does not automatically set itself up for any mode according to the mode's syntax table.
  2. it does not attempt to automatically balance parentheses

Autopair provides these things out-of-the-box. Smartparens provides other features, that I personally use Yasnippet for.

electric-pair-mode in Emacs 24.4

electric-pair-mode is a minor mode in Emacs 24.3, but it's not very useful or widely used. In Emacs 24.4, electric-pair-mode minor-mode is much improved and actually supersedes autopair in functionality and general all-around Emacs integration, and I recommend you use it instead.

Neat tricks

  • autopair-autowrap tells autopair to automatically wrap the selection region with the delimiters you're trying to insert.

  • autopair-blink makes the cursor quickly blink over matching braces and quotes just inserted or skipped over. If you find this behaviour annoying, set this to nil.

  • autopair-skip-whitespace, when set to non-nil, tells autopair to skip over whitespace when attempting to skip over a closing brace. If you set this to 'chomp, the whitespace is not only jumped over but also deleted.

Autopair's idea is to always do-what-you-mean, but since some people are never satisfied, have a look at the following:

Optional tricks

You shouldn't need this, but to enable autopair in just some types of buffers, comment out the autopair-global-mode and turn on autopair-mode in some major-mode hook, like:

;; use autopair just in c buffers
 
(add-hook 'c-mode-common-hook 
          #'(lambda () (autopair-mode)))

Alternatively, do use autopair-global-mode and create exceptions using the major mode hooks (watch out for the change in behaviour emacs 24).

;; use autopair everywhere but c buffers
 
(add-hook 'c-mode-common-hook
           #'(lambda () 
               (setq autopair-dont-activate t) ;; for emacsen < 24
               (autopair-mode -1))             ;; for emacsen >= 24
)

More tricks

autopair-dont-pair lets you define special cases of characters you don't want paired. Its default value skips pairing single-quote characters when inside a comment literal, even if the language syntax tables does pair these characters.

As a further example, to also prevent the { (opening brace) character from being autopaired in C++ comments use this in your .emacs.

(add-hook 'c++-mode-hook
          #'(lambda ()
               (push ?{
                     (getf autopair-dont-pair :comment))))

autopair-handle-action-fns lets you write some emacs-lisp that overrides/extends the actions taken by autopair after it decides something must be paired, skipped or deleted. To work with triple quoting in python mode, you can use this for example:

(add-hook 'python-mode-hook
          #'(lambda ()
              (setq autopair-handle-action-fns
                    (list #'autopair-default-handle-action
                          #'autopair-python-triple-quote-action))))

where autopair-python-triple-quote-action is an example of a user-written function (which is bundled in autopair.el).

See this issue for an example of clever use of this variable (thanks Alex Duller.

autopair-extra-pairs lets you define extra pairing and skipping behaviour for pairs not programmed into the syntax table. Watch out, this is work-in-progress, a little unstable and does not help balancing at all. To have <= and =>= pair in =c++-mode buffers, but only in code, use:

(add-hook 'c++-mode-hook
          #'(lambda ()
              (push '(?< . ?>)
                    (getf autopair-extra-pairs :code))))

if you program in emacs-lisp you might also like the following to pair backtick (``=) and quote (='`).

(add-hook 'emacs-lisp-mode-hook
          #'(lambda ()
              (push '(?` . ?')
                    (getf autopair-extra-pairs :comment))
              (push '(?` . ?')
                    (getf autopair-extra-pairs :string))))

Workarounds

Once you set autopair-global-mode everything mostly just works but a few extensions use tricks that interfere with autopair's own tricks, disabling autopair or some of the extension's functionality. Using the customization techniques described above, there are plenty of very good workarounds for slime-mode, latex-mode, term-mode and even viper-mode.

See this workaround list

How it works

The extension works by rebinding the braces and quotes keys, but can still be minimally intrusive, since the original binding is always called as if autopair did not exist.

The decision of which keys to actually rebind is taken at minor-mode activation time, based on the current major mode's syntax tables. To achieve this kind of behaviour, an Emacs variable emulation-mode-map-alists was used.

If you set autopair-pair-criteria and autopair-skip-criteria to the symbol help-balance (which, by the way, is the default), braces are not autopaired/autoskipped in all situations; the decision to autopair or autoskip a brace is taken according to the following table:

+---------+------------+-----------+-------------------+
| 1234567 | autopair?  | autoskip? | notes             |
+---------+------------+-----------+-------------------+
|  (())   |  yyyyyyy   |  ---yy--  | balanced          |
+---------+------------+-----------+-------------------+
|  (()))  |  ------y   |  ---yyy-  | too many closings |
+---------+------------+-----------+-------------------+
|  ((())  |  yyyyyyy   |  -------  | too many openings |
+---------+------------+-----------+-------------------+

The table is read like this: in a buffer with 7 characters laid out like the first column, an "y" marks points where an opening brace is autopaired and in which places would a closing brace be autoskipped. Quote pairing tries to support similar "intelligence".

More Repositories

1

yasnippet

A template system for Emacs
Emacs Lisp
2,770
star
2

eglot

A client for Language Server Protocol servers
Emacs Lisp
2,256
star
3

sly

Sylvester the Cat's Common Lisp IDE
Common Lisp
1,256
star
4

snooze

Common Lisp RESTful web development
Common Lisp
206
star
5

breadcrumb

Emacs headerline indication of where you are in a large project
Emacs Lisp
189
star
6

darkroom

Simple distraction-free editing
Emacs Lisp
147
star
7

hunchensocket

RFC6455 compliant WebSockets for Common Lisp
Common Lisp
109
star
8

beardbolt

Compiler Explorer clone
Emacs Lisp
86
star
9

fiasco

A test framework for Common Lisp
Common Lisp
55
star
10

snippet

A rewrite of Yasnippet's engine (incomplete, but many parts done)
Emacs Lisp
30
star
11

sly-quicklisp

Quicklisp support for SLY
Emacs Lisp
29
star
12

sly-macrostep

Expand CL macros inside source files
Emacs Lisp
28
star
13

sly-stepper

sly-slepper
TeX
27
star
14

yasmate

Convert textmate bundles to yasnippet format
Emacs Lisp
24
star
15

eslack

Slack client for Emacs
Emacs Lisp
12
star
16

sly-hello-world

A dummy template for writing SLY contribs
Emacs Lisp
9
star
17

sly-named-readtables

NAMED-READTABLES support for SLY
Emacs Lisp
9
star
18

zapp

Zebugger Adapter Protocol Plugin
Emacs Lisp
8
star
19

emacs-livereload

A livereload server running on Emacs
JavaScript
7
star
20

mac-key-mode

Provide mac-style key bindings on Carbon Emacs
Emacs Lisp
6
star
21

sinatra-thumbnails

Dynamic generation, serving and caching of thumbnail images for simple file-based CMS's written in Sinatra.
Ruby
6
star
22

efire

A campfire client for emacs
Emacs Lisp
5
star
23

nestor

A common-lisp clone of the nesta ruby cms
Common Lisp
5
star
24

cl-guard

A uniform interface to your file and event watching needs in common lisp.
Common Lisp
4
star
25

eel

Emacs interface to voidtools Everything file search
Emacs Lisp
4
star
26

ecco

ecco is a port of docco for emacs
Emacs Lisp
4
star
27

elisp-shorthand

Pretty dumb namespacing system
Emacs Lisp
3
star
28

hello-world-2000

Simplest possible C++ Hello World with CMake+Conan
CMake
3
star
29

tcpppl4

Exercise solutions for The C++ Programming Language 4th edition
C++
2
star
30

st

My fork of simple terminal
C
2
star
31

treeconf

C++ command-line parser for tree-like options
C++
2
star
32

blip

Automatically find and run tests in emacs
Emacs Lisp
2
star
33

lsplex

Experimental Language Server proxy/multiplexer
C++
2
star
34

whopper

A bare bones fork of http://common-lisp.net/project/bese/yaclml.html
Common Lisp
2
star
35

edice

Why would you want to roll dice in Emacs?
Emacs Lisp
2
star
36

vulkan-man-pages

Archlinux package for vulkan man pages
Shell
1
star
37

minns

a minimal name server, written as an exercise in C++
C++
1
star
38

revbufs

Reverts all out-of-date buffers safely
Emacs Lisp
1
star
39

raytracing-one-weekend

working through https://raytracing.github.io/books/RayTracingInOneWeekend.html
C++
1
star
40

nih

A shrubbery!!!
Emacs Lisp
1
star
41

blinky-template

A bare-bones Makefile-based template for ESP32 projects
C
1
star