Emacs Redux

Return to the Essence of Text Editing

New Emacs Blogs

| Comments

The problem with Emacs blogs is one can never have enough of them (especially the good ones).

I’d like to draw my readers attention to two new blogs:

lunarsite

lunarsite is not exactly new (it was started in March 2014), but I don’t think enough people have come across it, so I’m mentioning it here anyway. It’s operated by one of the most prolific members of the Emacs community in recent years - the great Sebastian Wiesner (of flycheck and cask fame).

If you’re thinking of writing your first major mode - you’ll find some great advice there.

Endless Parentheses

Endless Parentheses is a brand new blog started by Arthur Malabarba. It’s dedicated to concise posts on improving your Emacs productivity.

The content so far is similar to the articles I write here, so if you like Emacs Redux you’ll likely enjoy Endless Parentheses as well.

Quickly Find Emacs Lisp Sources

| Comments

One thing Emacs users deal all the time is looking for the source of various libraries and function or variable definitions. Typically this involves the use of commands like C-h f (describe-function), C-h v (describe-variable) and C-h k (describe-key) and jumping to the source from the help buffer those commands produce. Let’s see if we can improve upon this workflow.

find-func

Enter the built-in Emacs library find-func. It provides a myriad of useful commands to help us quickly locate sources. Let’s take a brief look at some of them.

  • You can find the source of a library with M-x find-library. If you want to find the source of ido you can do so like this:
1
M-x find-library RET ido RET

I’d suggest binding the command to C-h C-l for quick access:

1
(define-key 'help-command (kbd "C-l") 'find-library)
  • You can find the source of a function definition with M-x find-function. Here’s an example:
1
M-x find-function RET find-function RET

I’d suggest binding the command to C-h C-f for quick access:

1
(define-key 'help-command (kbd "C-f") 'find-function)

We can actually do one better - we can directly jump to a command definition using a keybinding of the command with M-x find-function-on-key. Here’s how we can find the source the command bound to C-a (beginning-of-line):

1
M-x find-function-on-key RET C-a

I’d suggest binding the command to C-h C-k for quick access:

1
(define-key 'help-command (kbd "C-k") 'find-function-on-key)
  • You can find the source of a variable definition with M-x find-variable. Here’s an example:
1
M-x find-variable RET large-file-warning-threshold RET

I’d suggest binding the command to C-h C-v for quick access:

1
(define-key 'help-command (kbd "C-v") 'find-variable)

The library provides other useful commands as well - like find-function-at-point and find-variable-at-point.

If you don’t like the keybindings I suggested you can use find-function-setup-keys instead. This will give you keybindings like C-x F, C-x V, C-f K (plus a few extra for commands like find-funtion-other-window).

elisp-slime-nav

Another really cool way to browse Elisp sources (and documentation) is the third-party package elisp-slime-nav. Assuming you’ve installed it already you can enabled it like this:

1
2
3
(require 'elisp-slime-nav)
(dolist (hook '(emacs-lisp-mode-hook ielm-mode-hook))
(add-hook hook 'elisp-slime-nav-mode))

Once this is done you’ll be able to jump to the source of the Emacs Lisp object at point (function or variable) with M-. (as in SLIME and CIDER for Common Lisp and Clojure respectively) and jump back with M-,. You can also see the description of the object at point using C-c C-d (or C-c C-d d).

That’s all for today, folks!

P.S.

All the suggested keybindings are present out-of-the-box in Prelude. elisp-slime-nav is also enabled out-of-the box in Prelude.

MELPA Stable

| Comments

We all know and love the MELPA package repository. At least we all know it, but not everyone loves the idea of using package snapshots. Some people would rather use only stable package releases and that’s understandable. Unfortunately the major community repository for stable packages Marmalade is often plagued by technical issues (that should be fixed once its elnode-powered rewrite is ready) and stale packages (due to the fact the sometimes people upload packages they don’t own there and stop updating them after a while).

Now there’s an alternative to Marmalade, which offers a MELPA-like experience - MELPA stable. It offers stable packages built automatically from git tags, meaning the only thing you have to do to release a new version of a package (assuming there’s a MELPA recipe for it, of course) is to tag the release in git. No more stale packages! No more manual package uploads!

To use the new repo add this to your Emacs config:

1
2
(add-to-list 'package-archives
'("melpa-stable" . "http://melpa-stable.milkbox.net/packages/") t)

Currently more than 500 packages are available for installation there. That number is bound to grow (a lot).

By the way, the maintainers of MELPA are looking for help to update the home page of the stable package archive. Currently it’s the same as the one of the snapshot archive, which is a bit confusing (especially regarding installation instructions). If you’d like to help fork and improve the page’s code on GitHub.

Opening Large Files

| Comments

I guess everyone has seen the following warning (at some point or another) when trying to open a relatively large file in Emacs:

1
File foo.bar is large (XX.XM), really open?

By default Emacs will display this warning for every file bigger than 10MB, which is quite annoying (conservative), especially given the fact that on any modern computer even much bigger files will be loaded almost instantaneously and won’t slow down Emacs significantly.

You can adjust this behavior by altering the value of the large-file-warning-threshold variable. I’d suggest setting it to something like 100MB:

1
2
;; warn when opening files bigger than 100MB
(setq large-file-warning-threshold 100000000)

Alternative, you can set it to nil, which will suppress the warning permanently, regardless of the size of the file you’re trying to open.

P.S. By the way, it’s not particularly wise to edit huge files (say bigger than 1GB) in Emacs directly. If you need to do something like this I’d suggest taking a look at the excellent vlfi package.

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.