Emacs Redux

Return to the Essence of Text Editing

Which-function-mode

| Comments

Every developer is sometimes in the following situation - you’re inside the definition of something like a class or method, but the name of the class or method is not currently into view, but you’d very much like to know/see it. You can obviously scroll up and see what you need to, but there’s a simpler way to see the name you’re looking for - the built-in mode which-function-mode.

When which-function-mode is active you’ll see in the center of your modeline the name of the definition your cursor is currently in. Here’s the mode in action:

And here’s how to enable it:

1
(which-function-mode)

That way which-function-mode will be active in all major modes that support it. If you want to enable it only in specific modes you can do so like this:

1
2
(add-to-list 'which-func-modes 'ruby-mode)
(add-to-list 'which-func-modes 'emacs-lisp-mode)

By default ??? will be displayed when which-function-mode cannot determine the name (perhaps because you’re not actually in a definition or due to implementation limitations). You can change this to something else like this:

1
(setq which-func-unknown "n/a")

And what if you don’t like to have the definition name displayed in the mode-line? There’s a solution for you as well (suggested by Sebastian Wiesner). Use the following bit of code:

1
2
3
4
5
6
7
8
;; Show the current function name in the header line
(which-function-mode)
(setq-default header-line-format
              '((which-func-mode ("" which-func-format " "))))
(setq mode-line-misc-info
            ;; We remove Which Function Mode from the mode line, because it's mostly
            ;; invisible here anyway.
            (assq-delete-all 'which-func-mode mode-line-misc-info))

And you’ll get the following result:

That’s all for today, folks! Hack Emacs Lisp & prosper!

A Peek at Emacs 24.4: Variadic Numeric Comparison Functions

| Comments

In most Lisps numeric comparison functions like =, <, >, <= and >= are variadic - meaning they accept variable number of arguments (as do functions like +, -, *, etc). Here’s an example:

1
2
3
;; let's assume that all those are numeric variables
(= x y z)
(< a b c d)

This is pretty cool and save you from writing code like:

1
2
(and (= x y) (= y z))
(and (< a b) (< b c) (< c d))

Prefix function position for the win!

In Emacs Lisp, however, all these comparison functions (unlike +, -, etc) accept just two arguments, which is somewhat unlispy. Luckily this is one of the things that’s going to change with Emacs 24.4. Emacs Lisp takes another small step to becoming a good Lisp dialect!

A Peek at Emacs 24.4: Focus Hooks

| Comments

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:

1
(add-hook 'focus-out-hook 'save-buffer)

Or you can go a step further and save all the buffers:

1
(add-hook 'focus-out-hook (lambda () (save-some-buffers t)))

I pretty sure you’ll find other creative ways to apply those new hooks to good use!

A Peek at Emacs 24.4: Delete-duplicate-lines

| Comments

Emacs 24.4 introduces the command delete-duplicate-lines.

By default M-x delete-duplicate-lines will delete all but one copy of any identical lines in the region. So assuming this is the region:

1
2
3
4
5
6
7
8
9
10
test
dup
dup
one
two
one
three
one
test
five

it will be reduced to this:

1
2
3
4
5
6
test
dup
one
two
three
five

When the command is invoked with a C-u prefix, it searches backwards and keeps the last instance of each repeated line. So this:

1
2
3
4
5
6
7
8
9
10
test
dup
dup
one
two
one
three
one
test
five

will become:

1
2
3
4
5
6
dup
two
three
one
test
five

Identical lines need not be adjacent, unless you’ve invoked the command with a C-u C-u prefix. When the command is invoked with a C-u C-u C-u prefix, it retains repeated blank lines.

A Peek at Emacs 24.4: Hash-table-keys & Hash-table-values

| Comments

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 hash-table).

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 library subr-x.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
;; 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))

(setq h (make-hash-table))

(puthash "Batman" "Bruce Wayne" h)
(puthash "Spiderman" "Peter Parker" h)
(puthash "Superman" "Clark Kent" h)

(hash-table-keys h)
; => ("Batman" "Spiderman" "Superman")

(hash-table-values h)
; => ("Bruce Wayne" "Peter Parker" "Clark Kent")

Nice and simple.

A Peek at Emacs 24.4: New String Manipulation Functions

| Comments

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:

1
2
3
4
(string-suffix-p "test" "my_test")
; => t
(string-suffix-p "tester" "my_test")
; => nil

More importantly, Emacs 24.4 ships with a new built-in library called subr-x, which features a bunch of other string manipulation functions:

  • string-blank-p
  • string-empty-p
  • string-join
  • string-reverse
  • string-trim-left
  • string-trim-right
  • string-trim
  • string-remove-prefix
  • string-remove-suffix

Here’s a brief demo of them in action:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
;; 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.

That’s all I have for now. Until next time!

A Peek at Emacs 24.4: Auto-indentation by Default

| Comments

I’ve written in the past about electric-indent-mode, 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:

1
def something|

After you press Return you’ll get:

1
2
def something
|

With it:

1
def something|

After you press Return you’ll get:

1
2
def something
  |

Nice, ah?

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:

1
(add-hook 'yaml-mode-hook (lambda () (electric-indent-local-mode -1)))

Note that electric-indent-local-mode was introduced in Emacs 24.4.

If you want to make a major mode electric-indent aware, have a look at the documentation of electric-indent-functions and electric-indent-chars.

P.S.

Dmitry Gutov recently wrote more on the topic in the context of ruby-mode in Emacs 24.4.

Ido-vertical-mode

| Comments

If you like ido, but don’t like the way it displays the available completion candidates you might want to have a look at ido-vertical-mode.

I won’t duplicate here the installation instructions, but I’ll show you how ido-vertical-mode displays the completion candidates:

Is this better than the regular ido? Depends on your personal preference I guess. But it’s good to have options, right?

That’s all for today, folks! Until next time.

A Peek at Emacs 24.4: sRGB Colours on OS X

| Comments

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:

1
(setq ns-use-srgb-colorspace nil)

I doubt that anyone would want to do that, but the option is there for those of you who want it.