2010-06-24

Emacs Form Feed (^L) Display Suggestion and Tips

Perm url with updates: http://xahlee.org/emacs/modernization_formfeed.html

Emacs Form Feed (^L) Display Suggestion and Tips

Xah Lee, 2010-06-24

This page discusses some problems involving the Form feed character (^L) in emacs, and gives you some tips for better solution.

In emacs lisp source code, sometimes you'll see “^L”. That's the “form feed” character, ascii 12. For example, type “Alt+x describe-function”, then dired, then click on the “dired.el” link to view the source code. Scroll around and you'll see it.

Problem

The form feed char is used in 1990s or earlier to cause printer to start with a new page. Printers today no longer use that in their protocol. However, sometimes it is also used by programer as a section marker. Many emacs lisp source code still have it. It is also sometimes seen in Python source code.

The displaying of ^L is hard to read, and is mysterious to modern programers.

Solution

You can make emacs display a horizontal line instead. You need to install Pretty Control-L minor mode, by Drew Adams. Download at: emacswiki.org PrettyControlL.

To install, place the file in your “~/.emacs.d/” dir, then put the following in your emacs init file (“~/.emacs”):

;; add the dir to load path
(add-to-list 'load-path "~/.emacs.d/")

;; display horizontal line for the Form Feed char (ASCII 12, ^L) The
;; Form Feed char is often used in elisp source code for marking
;; sections. The command forward-page (and backward-page) moves to the
;; next form feed char.
(require 'pp-c-l)
(setq pp^L-^L-string "                                                           ")
(pretty-control-l-mode 1)

By default, it'll display a line, but also with the annoying text “Section (Printable Page)”.

The line “(setq pp^L-^L-string ...)” above solves that problem.

Hotkey to Jump to Form Feed

Also, emacs support a hotkey to jump to the form feed char. By default, the key is “C-x [” and “C-x ]”. Very hard to use and hard to remember. You can set a more convenient key. For example:

;; shortcut for moving to prev/next form feed 
(global-set-key (kbd "<M-S-next>") 'forward-page) ; Alt+Shift+PageDown 
(global-set-key (kbd "<M-S-prior>") 'backward-page) ; Alt+Shift+PageUp

Note that emacs already uses Ctrl+Page up/down for scrolling left/right, and Meta+page up/down for paging up/down the next pane, and Shift+Page up/down selects text. You should not use Ctrl+Shift combo because Ctrl+Shift+‹letter› is avoided because text based terminals have problems distinguishing the key from non-shifted letters.

If you have Hyper key set, you can use the following more easier keys:

(global-set-key (kbd "<H-next>") 'forward-page)
(global-set-key (kbd "<H-prior>") 'backward-page)

If you don't use the number pad keys, you can set it to those keys, like this:

(global-set-key (kbd "<kp-9>") 'backward-page) ; keypad 9
(global-set-key (kbd "<kp-6>") 'forward-page) ; keypad 6

For how to set up hyper key and others, see: How to Define Keyboard Shortcuts in Emacs.

Typing the Form Feed Char

In emacs, you can type the char by pressing “Ctrl+q” then “Ctrl+l”.

(For explanation of this, see: The Confusion of Emacs's Keystroke Representation.)

Advantages of Using Form Feed As Section Marker in Source Code

Normally, programer mark section in source code with lots of comment chars. For example:

;--------------------------------------------------

#__________________________________________________

###################################################

///////////////////////////////////////////////////

These are not as elegant or convenient as the form feed char. First problem is typing them. Even if your editor provide a easy way to type the char repeatedly, such as emacs's “C-u 50” prefix, that's still 4 or 5 extra keys to press.

It is also hard to search. Because the style varies, some source code repeat the comment chars such as “##########”, some start with a comment char then followed by dashes “#----------”, some uses underline, some draws a ascii art box, like this:

##############################
#      functions             #
##############################

All these variations makes it hard to jump to the section. Typically, you search the string, for example, search a sequence of 5 #, but because the line has more than 5, your search will linger on the same line. Instead, you have to search almost the exact the number of chars used for this, and it is common that the number of chars for such line is not consistent. Or, you may use regex, but again, much more typing and the result is not precise.

The form feed char has several advantages over this. If you use it for section mark, it makes it precise, easy to locate, and easy to navigate to all different sections. All this because there's a single ^L char as a delimiter, and it's meaning is basically universal.

Problems of Using Form Feed As Section Marker

In lisp, the form feed char is ignored by the compiler. This is probably true for vast majority of languages today still. Though, make sure that if you use the char, your language will have no problem with it. You could also insert one comment char in front of it.

A major problem of using form feed char for section break is simply the visibility. Almost no editors display the form feed as a line. So, for vast majority of coders, a sequence of dash “----------” is simply the more practical solution. Also, today, few programers know how to insert a form feed character in their editor.

For reasons of why it is displayed in emacs as ^L, see: The Confusion of Emacs's Keystroke Representation.