A couple of years ago I
lamented over the lack of frame focus hooks in Emacs. This
prohibited us from implemented a feature like IntelliJ IDEA’s “auto-save on
focus lost”, which was kind of frustrating as we’re generally assuming that everything is doable in Emacs!
All this changes in Emacs 24.4 with the introduction of two new hooks - focus-in-hook and focus-out-hook.
The first is triggered when the current frame gains focus and the second when the frame loses focus.
So, if we want to save our active buffer when the Emacs frame loses focus we can simply do it like this:
Or you can go a step further and save all the buffers:
While Emacs has a pretty good hash-table API, two functions were
rather mysteriously absent from it - hash-table-keys and
hash-table-values (which would return a list of all keys/values in a
Many people who needed them simply defined them in their code
directly (usually in terms of maphash), but that’s no longer
necessary in Emacs 24.4, as they are now part of the new built-in
;; all functions in the library are defined as inline, so you don't;; need to require the library at runtime(eval-when-compile(require'subr-x))(setqh(make-hash-table))(puthash"Batman""Bruce Wayne"h)(puthash"Spiderman""Peter Parker"h)(puthash"Superman""Clark Kent"h)(hash-table-keysh); => ("Batman" "Spiderman" "Superman")(hash-table-valuesh); => ("Bruce Wayne" "Peter Parker" "Clark Kent")
Emacs has often been criticized for failing to provide a more
extensive string manipulation API (compared to that of programming
languages like Ruby and Perl, for instance). As many programs
(extensions) running on top of it are doing quite a lot of string
manipulation, having a good string API is important. To compensate the
lack of certain primitives in Emacs itself a lot of package authors
are using these days packages like
s.el or simply adding the string
functions they need directly to their packages (to reduce the number
of third-party deps).
In Emacs 24.4 finally the situation is improving. Finally, we’re getting
string-suffix-p, which was mysteriously missing even though string-prefix-p
has been part of Emacs for years:
More importantly, Emacs 24.4 ships with a new built-in library called subr-x, which features
a bunch of other string manipulation functions:
Here’s a brief demo of them in action:
;; all functions in the library are defined as inline, so you don't;; need to require the library at runtime(eval-when-compile(require'subr-x))(string-empty-p""); => t(string-empty-p" "); => nil(string-blank-p" "); => 0 (#o0, #x0, ?\C-@)(string-reverse"Batman"); => "namtaB"(string-join'("one""two""three")); => "onetwothree"(string-join'("one""two""three")","); => "one,two,three"(string-trim" Peter Parker "); => "Peter Parker"(string-remove-prefix"Mr. ""Mr. Smith"); => "Smith"(string-remove-suffix"Smith""Mr. Smith"); => "Mr. "
Sure, subr-x is not as extensive as s.el (and will never be), but
I think that it’s a big step in the right direction. It’s likely that
subr-x will be extended in subsequent Emacs versions and some of the
functions from it will be promoted to built-in.
I’ve written in the past about
which was added in Emacs 24.1. In Emacs 24.4 one of the most prominent
user visible changes is that it’s enabled out-of-the box. That’s a
huge step towards the “modernization” of Emacs and one of the bigger
changes to the defaults in recent times. Let’s review briefly how the
mode works with a couple of Ruby examples (| signifies the cursor
position). Without electric-indent-mode:
After you press Return you’ll get:
After you press Return you’ll get:
One problem with electric-indent-mode is that it doesn’t play nice
with some (mostly third-party) modes (yaml-mode, slim-mode,
etc). I guess the situation will improve over time, but for now you
can simply disable the mode in such problematic cases:
A while ago I wrote about a
colour theme problem specific to OS X.
Thankfully in Emacs 24.4 the OS X (a.k.a. NS) port of Emacs uses sRGB
colours by default (at least on OS X Lion (10.7) and newer), so you’ll
no longer need to install Emacs from homebrew (or apply an sRGB
colour support patch manually). If for some reason you want to stick
to the colours add this to your Emacs config:
I doubt that anyone would want to do that, but the option is there for those of you who want it.
I’m extremely fond of rectangle editing (the ability to select a
rectangular region and apply editing commands only to it), but I’ve
always disliked the fact there was no way to visually highlight the
exact rectangle region you’ve currently selected. Let’s delete some
text in a rectangular region (with C-x r d), so you can see where the
It’s not immediately clear where the rectangle bounds are (it’s clear
where the rectangle region starts, but it’s not so clear where it
ends), which is a source of constant confusion for many people.
Emacs 24.4 finally fixed this with the introduction of
rectangle-mark-mode (bound by default to C-x SPC). Let’s see it in
Some time ago I wrote about
highlighting matching delimiters with show-smartparens-mode. In
Emacs 24.4, the built-in show-paren-mode is capable of highlighting
more complex paired delimiters as well (like do/end in Ruby for
instance). There is one limitation to show-paren-mode (compared to
show-smartparens-mode) - it requires that the major mode, that it’s
used together with (e.g. ruby-mode), is implemented in terms of the
new(ish) SMIE (Simple Minded Indentation Engine). SMIE has been around
since 23.3, but hasn’t seen much adoption until quite recently. Prior
to Emacs 24.4 very few built-in modes were using it (and just about no
third-party major modes). In Emacs 24.4, however, a lot of modes were
updated to use SMIE (like ruby-mode), so you’ll be able to enjoy the
show-paren-mode improvement with them.
Here’s a glimpse of the enhanced show-paren-mode in action:
Long term, I still think that betting on smartparens-mode is a good idea, but if
you prefer to stick with built-in modes - show-paren-mode is now more capable than ever.
Emacs 24.4 is now feature frozen and its release is (probably) just a
few months away at this point. This makes it a good time to start
talking about some of the exciting and not so exciting changes that
will be introduced in it.
Emacs 24.4 is a special for me, since it’s the first Emacs release to
which I contributed code. On a related note - I’ve tracked its development
much closer than I used to in previous years and I feel I have lots of interesting
things to share with you.
In a series of smallish posts I’ll highlight some of the many new features that are
coming in 24.4. Keep in mind that since we’re talking about unreleased code, there’s
the chance of changes in the final version of 24.4.
P.S. Emacs 24.4 is fairly stable at this point, so can start using
it today. I’ve been using it exclusively for about half an year now
and I’ve rarely come across any issues whatsoever. If you’re a Prelude
user - it is 24.4 compatible.