= HTML tags.
Luckily it's trivial to teach it. It does know about == HTML
tags, and basically I just want == tags to be treated almost as
== tags, so to do that we just have to define a =shr-tag-code=
function. I've copied the =shr-tag-pre= function and removed the calls
to =ensure-newline=, because == tags are inline tags.
In order to remain a little future-proof, it should only be done if
it doesn't already exist.
*Note:* See my [[Vacuous defvar][note]] on vacuous defvar for this use of =defvar=.
*Note:* See my [[Function declarations][note]] on function declarations about the use of
=declare-function=.
#+BEGIN_SRC emacs-lisp
(defvar shr-folding-mode)
(declare-function shr-indent "shr")
(declare-function shr-generic "shr")
(with-eval-after-load 'shr
(unless (fboundp 'shr-tag-code)
(defun shr-tag-code (cont)
(let ((shr-folding-mode 'none))
(shr-indent)
(shr-generic cont)))))
#+END_SRC
* Use scheme-mode for scsh interpreted files
Set the major mode for files interpreted by scsh (for example, by
having ~#!/usr/local/bin/scsh~ at the top) to use =scheme-mode=.
#+BEGIN_SRC emacs-lisp
(add-to-list 'interpreter-mode-alist '("scsh" . scheme-mode))
#+END_SRC
* Set default scheme implementation
Set the default implementation for geiser to guile so it doesn't ask
which implementation to use every time.
#+BEGIN_SRC emacs-lisp
(defvar geiser-default-implementation)
(with-eval-after-load 'geiser
(setq geiser-default-implementation 'guile))
#+END_SRC
* Setup eww-lnum
As recommended in the [[https://github.com/m00natic/eww-lnum][README]], set the keys in the =eww-mode-map=.
#+BEGIN_SRC emacs-lisp
(defvar eww-mode-map)
(with-eval-after-load 'eww
(define-key eww-mode-map "f" 'eww-lnum-follow)
(define-key eww-mode-map "F" 'eww-lnum-universal))
#+END_SRC
* Don't compile scss files
By default =scss-mode= tries compiling a file each time it's saved. I
don't have SCSS properly installed globally so this always fails,
highly annoying.
#+BEGIN_SRC emacs-lisp
(defvar scss-compile-at-save)
(with-eval-after-load 'scss-mode
(setq scss-compile-at-save nil))
#+END_SRC
* Change listings in dired
The number of bytes a file is doesn't usually tell me much when it's
something like ~292837~. I prefer seeing just how many Kb or Mb a
certain file is. I also don't need to see the ~.~ and ~..~ directories
when I insert directories into the current dired buffer, as there is
a great chance that the current and parent directory are already
shown in the buffer.
#+BEGIN_SRC emacs-lisp
(defvar dired-subdir-switches)
(with-eval-after-load 'dired
(setq dired-listing-switches "-alh"
dired-subdir-switches "-Alh"))
#+END_SRC
* Useful functions
During your editing in Emacs you will undoubtedly find the need to
define your own editing functions or macros. Here are mine.
** Delete the contents of the string at point
First we define the function. It was inspired by [[http://www.masteringemacs.org/][Mickey's post on
swapping quote symbols]], mostly copied even. First we check if we
are even in a string, and if not we throw an error, after that we
move back to the beginning of the string, store that point, go to
the end of the string (using =forward-sexp=) and then delete the
region between the two points (non-inclusive).
#+BEGIN_SRC emacs-lisp
(defun oni:delete-string-contents ()
(interactive)
(unless (in-string-p)
(error "You must be in a string for this command to work"))
(save-excursion
(while (in-string-p) (forward-char -1))
(let ((bos (point)))
(forward-sexp)
(delete-region (1+ bos) (1- (point))))))
#+END_SRC
Since for interactive functions it's kind of a pain to have to use
a personal "namespace" I prefer naming them regularly as if they're
just part of the environment. If ever Emacs comes up with a similar
function with the same name, I'd prefer using the built-in version.
#+BEGIN_SRC emacs-lisp
(unless (fboundp 'delete-string-contents)
(defalias 'delete-string-contents 'oni:delete-string-contents))
#+END_SRC
Lastly, any function worth using often should probably be easily
accessible with a keybinding. In my case the {{{key(C-c i s)}}} is
inspired by the Vim keybindings like {{{key(ci")}}}.
#+BEGIN_SRC emacs-lisp
(global-set-key (kbd "C-c i s") 'delete-string-contents)
#+END_SRC
* Show eldoc when evaluating expressions
Thanks to [[http://endlessparentheses.com/sweet-new-features-in-24-4.html][this post]] it was brought to my attention that eldoc mode
can be enabled when evaluating expressions using {{{key(M-:)}}}.
I vaguely remember having had this before, I just don't know how or
why it stopped working.
#+BEGIN_SRC emacs-lisp
(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
* Local minor mode for =electric-pair-mode=
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
* Remove whitespace when closing delimiters
In =electric-pair-mode=, skip over and delete white space if it stands
between the cursor and the closing delimiter.
#+BEGIN_SRC emacs-lisp
(setq electric-pair-skip-whitespace 'chomp)
#+END_SRC
* Reject SSL/TLS certificates that don't check out
You just can't trust the internet. And I'd prefer to know if
something goes wrong.
#+BEGIN_SRC emacs-lisp
(defvar gnutls-verify-error)
(setq gnutls-verify-error t)
#+END_SRC
* Programming
Emacs is a real programmer's editor, especially so because it's so
programmable itself. It also offers modes for a lot of programming
languages and 3rd-party packages offer even more.
** SCSS
- Automatically match parentheses and other delimiters.
#+BEGIN_SRC emacs-lisp
(add-hook 'scss-mode-hook #'oni:electric-pair-local-mode)
#+END_SRC
- Automatically indent code.
#+BEGIN_SRC emacs-lisp
(add-hook 'scss-mode-hook #'electric-indent-local-mode)
#+END_SRC
- Enable auto-completion
#+BEGIN_SRC emacs-lisp
(declare-function auto-complete-mode "auto-complete")
(add-hook 'scss-mode-hook #'auto-complete-mode)
#+END_SRC
** Haml
- Automatically match parentheses and other delimiters.
#+BEGIN_SRC emacs-lisp
(add-hook 'haml-mode-hook #'oni:electric-pair-local-mode)
#+END_SRC
- Check syntax on the fly
#+BEGIN_SRC emacs-lisp
(declare-function flycheck-mode "flycheck")
(add-hook 'haml-mode-hook #'flycheck-mode)
#+END_SRC
** Ruby
- Automatically match parentheses and other delimiters.
#+BEGIN_SRC emacs-lisp
(add-hook 'ruby-mode-hook #'oni:electric-pair-local-mode)
#+END_SRC
- Enable flycheck
#+BEGIN_SRC emacs-lisp
(add-hook 'ruby-mode-hook #'flycheck-mode)
#+END_SRC
- Align chained method calls
#+BEGIN_SRC emacs-lisp
(defvar ruby-align-chained-calls)
(setq ruby-align-chained-calls t)
#+END_SRC
- Automatically indent code.
#+BEGIN_SRC emacs-lisp
(add-hook 'ruby-mode-hook #'electric-indent-local-mode)
#+END_SRC
** Coffee
- Automatically match parentheses and other delimiters.
#+BEGIN_SRC emacs-lisp
(add-hook 'coffee-mode-hook #'oni:electric-pair-local-mode)
#+END_SRC
** PHP
- Automatically indent code
#+BEGIN_SRC emacs-lisp
(add-hook 'php-mode-hook #'electric-indent-local-mode)
#+END_SRC
*** Show tabs and spaces in indent
I'm working with some WordPress plugins nowadays and their style
guide insists on using tabs, not spaces... I'd like to know that
I'm following this rule.
#+BEGIN_SRC emacs-lisp
(add-hook 'php-mode-hook #'oni:whitespace-only-tabs)
#+END_SRC
*** Use web-mode for HTML-heavy files
I have to work with a lot of PHP and HTML interspersed. This makes
a difficult case since ~php-mode~ very deliberately doesn't support
that very well. On the other hand I really don't like ~web-mode~ for
PHP /without/ any HTML in it. So I decided to name the files that
contain mostly HTML with some PHP ~.html.php~ and have them load
~web-mode~ instead of ~php-mode~, whilst keeping the association for
plain ~.php~ files as it is.
Something tricky about doing this is that if this setting gets
evaluated /before/ ~php-mode~ is loaded it'll be further down the list
from ~php-mode~'s definition. This would cause the ~php-mode~ auto
mode definition from being accepted first (since ~.html.php~ also
matches ~.php~) and consequently render this definition useless.
#+BEGIN_SRC emacs-lisp
(oni:eval-after-init
(add-to-list 'auto-mode-alist '("\\.html\\.php$" . web-mode)))
#+END_SRC
** Web
~web-mode~ has some quirks, such as not being able to handle the
fact that ~fci-mode~ puts a red line at the 80-column margin. This is
annoying to say the least.
#+BEGIN_SRC emacs-lisp
(declare-function fci-mode "fci-mode")
(add-hook 'web-mode-hook (turn-off fci-mode))
#+END_SRC
Just like in ~php-mode~ I want to see the tabs.
#+BEGIN_SRC emacs-lisp
(add-hook 'web-mode-hook #'oni:whitespace-only-tabs)
#+END_SRC
Set =indent-tabs-mode= for ~web-mode~ as well.
#+BEGIN_SRC emacs-lisp
(defvar web-mode-code-indent-offset)
(defvar web-mode-markup-indent-offset)
(add-hook 'web-mode-hook
(change-settings indent-tabs-mode t
web-mode-code-indent-offset 4
web-mode-markup-indent-offset 4))
#+END_SRC
Use it for ~.html.erb~ files.
#+BEGIN_SRC emacs-lisp
(oni:eval-after-init
(add-to-list 'auto-mode-alist '("\\.html\\.erb$" . web-mode)))
#+END_SRC
** JavaScript
Turn on electric indenting for JavaScript.
#+BEGIN_SRC emacs-lisp
(add-hook 'js2-mode-hook #'electric-indent-local-mode)
#+END_SRC
** CSS
Turn on electric indent and electric pair modes for CSS.
#+BEGIN_SRC emacs-lisp
(add-hook 'css-mode-hook #'electric-indent-local-mode)
(add-hook 'css-mode-hook #'oni:electric-pair-local-mode)
#+END_SRC
* Load custom file
I don't really use the Emacs customization interface much, but I
have used it as a kind-of persistent datastore, specifically for
desktop-registry[fn:9]. I do very much like the idea of it, it's a
very cool thing to have. I also use ~custom.el~ for storing some
things that I really can't store in a public viewing location like
this file or the git repository it lives in.
#+BEGIN_SRC emacs-lisp
(setq custom-file "~/.emacs.d/custom.el")
(load custom-file)
#+END_SRC
* Notes
Here are some random or somewhat general notes about things you may
run into when looking through my Emacs init.
** Vacuous defvar
A =defvar= without a value like =(defvar some-variable)= tells the
byte-compiler that the variable will appear, but doesn't give it a
value. It should only count for the file where it is used and once
the file with the /actual/ =defvar= is loaded it will be populated with
its value, contrary to what would happen if you'd given it a value
before loading its original file.
** Function declarations
The function =declare-function= tells the byte-compiler where to find
a certain function. This keeps the byte-compiler from complaining
about certain functions possibly not being defined at run-time.
* Footnotes
[fn:1] This runs =tmm-menubar=, which lets you navigate the menubar in a
text-driven way. I don't ever use it because I know what all my
favorite functions are called, but it seems a great deal more
efficient than having to click on everything.
[fn:2] https://github.com/sabof/svg-mode-line-themes
[fn:3] It works fine for me with something like jabber chats and the
like.
[fn:4] I like to program in Lisp, parentheses are important to me!
Parentheses should be nice and round, not almost like bars!
[fn:5] https://github.com/belluzj/fantasque-sans
[fn:6] Used to be Cosmic Sans Neue Mono, the name changed because
people misread it as "Comic" (me included, which was the original
reason I checked it out, for laughs) and hate Comic Sans, and also
because there was already a Cosmic Sans font as well, which could
cause confusion.
[fn:7] http://stackoverflow.com/questions/22175214/automatically-switch-language-in-gnus-depending-on-recipient
[fn:8] I still use 2 spaces for some languages, like HTML.
[fn:9] http://code.ryuslash.org/desktop-registry/about/