multi-line aims to provide a flexible framework for automatically multi-lining and single-lining function invocations and definitions, array and map literals and more. It relies on functions that are defined on a per major mode basis wherever it can so that it operates correctly across many different programming languages.
The following languages are officially supported by multi-line
- C/C++
- clojure
- emacs-lisp
- golang
- java
- javascript
- python
- ruby
- rust
- scala
It is likely that multi-line will function pretty well in any language using
typical brace/parenthesis/comma syntax, provided that the major mode has
properly defined forward-sexp
and indent-line
. If you find that multi-line
works well without modification in your language of choice please file an issue
or submit a pull request to have it added to the list of officially supported
languages.
Install from MELPA with M-x package-install multi-line
. See the melpa
repository for details about how to set up MELPA if you have not already done
so.
(require 'multi-line)
(global-set-key (kbd "C-c d") 'multi-line)
Execute the multi-line
command (with M-x or a keybinding) inside the body of
the expression you wish to multi-line, as show in the demo above. Invoking
multi-line multiple times on the same definition will cycle between the
different respacing styles that are configured for the current buffer.
When invoked with a prefix argument (C-u
), multi-line will “single-line” the
expression at point, removing all new lines and replacing them with spaces (or
the character configured for single lining).
multi-line can be configured to behave differently depending on the major mode
of the current-buffer. The behavior of multi-line is described with a
multi-line-strategy object that has three components: a find-strategy, an
enter-strategy and a replace-strategy. The multi-line-defhook
macro can be
used to set a major mode specific multi-lining strategy. The strategy defined
with the defhook will become active in any buffers with the specified mode.
In the following example we set the default multi-lining behavior for the clojure programming language.
(multi-line-defhook clojure
(make-instance multi-line-strategy
:find (make-instance multi-line-forward-sexp-find-strategy
:split-regex "[[:space:]\n]+"
:done-regex "[[:space:]]*)]}"
:split-advance-fn 'multi-line-lisp-advance-fn)
:respace multi-line-lisp-respacer))
This expands to
(eval-and-compile
(defvar multi-line-clojure-strategy)
(setq multi-line-clojure-strategy
(make-instance multi-line-strategy
:find
(make-instance multi-line-forward-sexp-find-strategy
:split-regex "[[:space:]]+"
:done-regex "[[:space:]]*)]}"
:split-advance-fn 'multi-line-lisp-advance-fn)
:respace multi-line-lisp-respacer))
(defun multi-line-clojure-mode-hook nil
(setq-local multi-line-current-strategy multi-line-clojure-strategy))
(add-hook 'clojure-mode-hook 'multi-line-clojure-mode-hook t))
Users will most often want to configure the respacing portion of the multi-line strategy. If you prefer to always add a newline at every available split point you might set up the default multi-line strategy as follows:
(setq-default multi-line-current-strategy
(multi-line-strategy
:respace (multi-line-default-respacers
(make-instance multi-line-always-newline))))
multi-line has some built in mode specific behavior that is enabled by default.
The interactive function multi-line-disable-mode-hooks
disables this mode
specific behavior.