Emacs Form Feed (^L) Display Suggestion and Tips

Advertise Here

, 2010-06-24, 2010-08-09

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 Description

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:

;; 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

Emacs has hotkeys to jump to the form feed char. By default, the key is 【Ctrl+x [】 and 【Ctrl+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-prior>") 'backward-page) ; Alt+Shift+PageUp
(global-set-key (kbd "<M-S-next>") 'forward-page) ; Alt+Shift+PageDown 

Note that emacs already uses 【Ctrl+PageUp】/【Ctrl+PageDown】 for scrolling left/right, and 【Alt+PageUp】/【Alt+PageDown】 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 Hyper as the modifier, which is one less modifier than 【Alt+Shift】. Like this:

(global-set-key (kbd "<H-prior>") 'backward-page) ; Hyper+PageUp
(global-set-key (kbd "<H-next>") 'forward-page) ; Hyper+PageDown

If you don't use the number pad keys, you can set it to do the paging. Even easier. 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: Emacs: How to Define Keyboard Shortcuts.

Typing the Form Feed Char

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

(For explanation of this, see: Emacs's Key Notations Explained (/r, ^M, C-m, RET, <return>, M-, meta).)

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 【Ctrl+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 make 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.

The fundamental reason of the form feed char as section marker advantage over ASCII text, is due to the semantic vs presentation issue. For discussion of various common issues on this, see:

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.

Cygwin Bash will generate this error if your shell script contains the formfeed char that's not in comment.

bash: $'\f': command not found

AutoHotkey scripting lang generates compiler error if you have a uncommented formfeed char in source code.

PHP would print a warning about it: “Warning: Unexpected character in the input: '^L' (ASCII=12)”. This is in PHP bug database php.net bug#5912.

Also, if your language does not allow uncomment formfeed char, and you put a comment char in front, but emacs's forward-page/backward-page command won't recognize formfeed char that are commented out.

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: Emacs's Key Notations Explained (/r, ^M, C-m, RET, <return>, M-, meta).

blog comments powered by Disqus