Emacs Refactor (EMR) provides language-specific refactoring support for Emacs. It has a simple declarative interface for easy extension.
To use EMR when editing, simply move point to an expression and invoke the refactor menu.
EMR ships with many refactoring commands, and pull requests for extensions are welcome. See Extension for details on extending EMR to other language modes. It's easy (honest!).
Tested on Emacs 24.3.
emr
is available on MELPA. This is the easiest way to install.
If you haven't set up MELPA, you'll need to add the following to your init.el
;;; Initialize packages.
(require 'package)
(add-to-list 'package-archives '("melpa" . "http://melpa.milkbox.net/packages/"))
(package-initialize)
(unless package-archive-contents (package-refresh-contents))
Once MELPA is configured:
-
M-x package-install emr
-
Configure your init.el:
(define-key prog-mode-map (kbd "M-RET") 'emr-show-refactor-menu)
(add-hook 'prog-mode-hook 'emr-initialize)
Most EMR commands are context-sensitive and are available through the refactor menu. Some actions affect the whole buffer and are available in the menu bar.
These commands are available for all programming languages.
The following context-sensitive refactoring commands are available:
- comment region
- uncomment region
The following context-sensitive refactoring commands are available:
- tidy includes
The following buffer-wide actions are available:
- insert include
Refactoring support for C is a work in progress. Contributions are welcome.
These commands are available to all Lisp dialects, including Clojure, Elisp and Common Lisp.
The following context-sensitive refactoring commands are available:
- comment form
- uncomment block
The following context-sensitive refactoring commands are available:
- delete unused definition
- delete unused let binding form
- eval and replace
- extract autoload
- extract constant
- extract function
- extract to let
- extract variable
- implement function
- inline function
- inline let variable
- inline variable
- insert autoload directive
- tidy autoloads
The following buffer-wide actions are available:
- find unused definitions
JavaScript refactoring support requires js2 refactor.
The following refactoring commands are available:
- extract function
- extract method
- extract variable
- add parameter
- local variable to instance variable
- log region
Ruby refactoring support requires ruby refactor.
The following refactoring commands are available:
- extract function
- extract variable
- extract constant
- add parameter
- extract to let
The following refactoring commands are available:
- extract function
- extract variable
Use the emr-declare-command
function to declare a refactoring action. The
action will automatically become available in the refactoring popup menu.
This function supports predicate expressions, allowing the options displayed to be context-sensitive.
As an example, here is the declaration for a refactoring command that ships with EMR:
(emr-declare-command 'emr-el-extract-constant
:title "constant"
:description "defconst"
:modes 'emacs-lisp-mode
:predicate (lambda ()
(not (or (emr-el:looking-at-definition?)
(emr-el:looking-at-let-binding-symbol?)))))
This wires the emr-el-extract-constant
function to be displayed in
emacs-lisp-mode
, provided point is not looking at an Elisp definition or
let-binding form.
If your favourite language mode already offers refactoring commands, it is simple to wire them up with EMR using this interface.
You will need Cask, make and git to build the project.
-
Install Cask:
curl -fsSkL https://raw.github.com/cask/cask/master/go | python
-
Clone and install with
make && make install
:cd git clone git@github.com:chrisbarrett/emacs-refactor.git cd emacs-refactor make && make install
-
Configure your init.el:
(autoload 'emr-show-refactor-menu "emr")
(define-key prog-mode-map (kbd "M-RET") 'emr-show-refactor-menu)
(add-hook 'prog-mode-hook 'emr-initialize)
These will be installed automatically by Cask.
Shout out to @magnars for his awesome libraries.
Yes, please do. See CONTRIBUTING for guidelines.
Thanks to the following contributors:
- Sreenath Nannat (@tacticiankerala) for adding Ruby and JavaScript bindings
- Elisp:
- Simplify let statements when inlining functions
- Use destructuring-bind when inlining functions that use destructuring in their arglists.
- C: More useful refactorings, eg, inline function/variable
See COPYING. Copyright (c) 2014-2015 Chris Barrett.