Add electric-indent and electric pair modes to init.org

This commit is contained in:
Tom Willemse 2014-10-23 16:38:45 +02:00
parent 05c68a9e2e
commit 4baea6bdac
2 changed files with 59 additions and 15 deletions

View file

@ -752,21 +752,6 @@ If no direction is given, don't split."
(define-key yas-minor-mode-map [(tab)] nil)
(define-key yas-minor-mode-map (kbd "C-\\") 'yas-expand))
;;;; Modes
;; Copied from electric.el, modified from `electric-indent-local-mode'.
(define-minor-mode oni:electric-pair-local-mode
"Toggle `electric-pair-mode' only in this buffer."
:variable (buffer-local-value 'electric-pair-mode (current-buffer))
(cond
((eq electric-pair-mode (default-value 'electric-pair-mode))
(kill-local-variable 'electric-pair-mode))
((not (default-value 'electric-pair-mode))
;; Locally enabled, but globally disabled.
(electric-pair-mode 1) ; Setup the hooks.
(setq-default electric-pair-mode nil) ; But keep it globally disabled.
)))
;;;; Tests
(stante-after ert

View file

@ -955,6 +955,65 @@
(add-hook 'eval-expression-minibuffer-setup-hook #'eldoc-mode)
#+END_SRC
* Return to {{{key(C-j)}}} in electric indent mode
When =electric-indent-mode= is enabled the default function bound to
{{{key(C-j)}}} (=electric-newline-and-maybe-indent=) stops indenting
after adding a newline, whilst {{{key(RET)}}} starts doing it. Since
I use {{{key(C-j)}}} almost exclusively and don't use {{{key(RET)}}}
at all, it's really not useful to me. So when =electric-indent-mode=
is enabled, switch the two.
#+BEGIN_SRC emacs-lisp
(add-hook 'electric-indent-local-mode-hook
(lambda ()
(if electric-indent-mode
(progn
(local-set-key (kbd "C-j") 'newline)
(local-set-key (kbd "RET") 'electric-newline-and-maybe-indent))
(local-unset-key (kbd "C-j"))
(local-unset-key (kbd "RET")))))
#+END_SRC
* Automatically indent some modes
Some modes, not all, benefit from using =electric-indent-mode=. So
enable it locally for those modes.
#+BEGIN_SRC emacs-lisp
(add-hook 'scss-mode-hook #'electric-indent-local-mode)
#+END_SRC
* Automatically pair in some modes
Almost all modes benefit from automatic delimiter pairing, but not
all modes are equal. =paredit= works awesomely for anything lisp-like,
but not so much for other modes, that's where =electric-pair-mode=
comes in. Unfortunately it has no local version, so I copied the
code from =electric-indent-local-mode= and changed it work with
=electric-pair-mode=.
#+BEGIN_SRC emacs-lisp
;; Copied from electric.el, modified from `electric-indent-local-mode'.
(define-minor-mode oni:electric-pair-local-mode
"Toggle `electric-pair-mode' only in this buffer."
:variable (buffer-local-value 'electric-pair-mode (current-buffer))
(cond
((eq electric-pair-mode (default-value 'electric-pair-mode))
(kill-local-variable 'electric-pair-mode))
((not (default-value 'electric-pair-mode))
;; Locally enabled, but globally disabled.
(electric-pair-mode 1) ; Setup the hooks.
(setq-default electric-pair-mode nil) ; But keep it globally disabled.
)))
#+END_SRC
And then we just enable it for the interesting modes.
#+BEGIN_SRC emacs-lisp
(add-hook 'scss-mode-hook #'oni:electric-pair-local-mode)
#+END_SRC
* Load custom file
I don't really use the Emacs customization interface much, but I