Emacs Form Feed (^L) Display Suggestions & Tips

, , …,

This page discusses the problems of the Form feed character (^L) used in emacs as source code section marker. If you just want practical tips, see: Emacs: Navigating Code Section & Form Feed.

In emacs lisp source code, sometimes you'll see “^L”. That's the “Form Feed” character (ASCII 12).

In Emacs Lisp source code, the Form Feed is used as a page break marker. For example, call describe-functionF1 f】, then dired, then click on the “dired.el” link to view the source code. Scroll around and you'll see it.

It is also used in C source code from GNU, or old Python source code. In general, most code after 2000 no longer follow this convention.

The Form Feed character is used in 1990s or earlier to cause printer to start a new page. Printers today no longer use that in their protocol.

In emacs, there's a built-in command backward-pageCtrl+x [】 and forward-pageCtrl+x ]】 to page thru sections. You can insert the Form Feed character by pressing 【Ctrl+q Ctrl+l】.

Problems of Form Feed as Section Marker in Emacs Lisp

The convention of using Form Feed character as code section marker has several problems:

emacs page break lines mode screenshot 2012-11-15
Emacs screenshot , displaying the Form Feed as ^L and as line.

So, in the end, using the Form Feed as section indicator has 2 problems. One is technical.

Suggested Solution

Emacs should display the Form Feed character as a horizontal line. For example, bundle and turn on the package page-break-lines.el. 〔☛ Emacs: How to Move Around Source Code Section (^L) and Text Blocks

Problems of Section Navigation Key in Emacs

Emacs has keys to jump to the Form Feed char. By default, the keys are:

It's hard to use and hard to remember. For example, you cannot hold on a key to keep paging.

Suggested Solution

Emacs should make the keys 【Ctrl+Alt+⇞ Page △】 and 【Ctrl+Alt+⇟ Page ▽】 as default. These would be compatible with lisp code navigation keys, all use【Ctrl+Alt】. 〔☛ How to Edit Lisp Code with Emacs

;; keys for moving to prev/next code section (Form Feed; ^L)
(global-set-key (kbd "<C-M-prior>") 'backward-page) ; Ctrl+Alt+PageUp
(global-set-key (kbd "<C-M-next>") 'forward-page)   ; Ctrl+Alt+PageDown

Design Analysis of Using Form Feed Character as Code Section Marker

A single specialized character as a universal markup for source code section break, is quite elegant and useful.

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:

;--------------------------------------------------
; emacs lisp
#__________________________________________________
# perl, ruby, python, bash, …
###################################################
# perl, ruby, python, bash, …
///////////////////////////////////////////////////
// C, C++, Java, …

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:

##############################
#         PRETTY!            #
##############################

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 unique single character as a delimiter, and its 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. However, it causes problems in some languages.

Cygwin Bash

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

bash: $'\f': command not found

AutoHotkey

AutoHotkey scripting language generates compiler error if you have a uncommented Form Feed char in source code.

PHP

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.

Emacs: Form Feed Must be on a Line by Itself

If your language does not allow uncomment Form Feed char, and if you put a comment character in front, but emacs's {forward-page, backward-page} commands won't recognize Form Feed char as section break if they are not the only char in a line.

Other Issues

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

A Universal Source Code Section Markup

Unfortunately, there's no good or standard replacement. Usually, people just add a bunch of hyphen like this ----- or equal sing ===== or underscore _____ or just a sequence of comment char. These are hackish. The problem with these approach is you cannot have a key to do paging because the page marker is not standardized.

So, a workaround i came up is to use the Unicode SECTION SIGN §, followed by the Unicode BOX DRAWINGS LIGHT HORIZONTAL (U+2500) . Like this:

; § ────────── ────────── ────────── ────────── ──────────

This approach is designed for all source code. It doesn't have the invisible character problem, nor the compiler choke problem. The disadvantage is the requirement of Unicode, and some inconvenience of input. (the inconvenience of input can be easily solved by editor.) 〔☛ Unicode Popularity on Web by Google

Input

For inputting the section marker, you can define a abbrev, like this:

("8s" "§ ────────── ────────── ────────── ────────── ──────────")

〔☛ Using Emacs Abbrev Mode for Abbreviation〕 You can also easily define a function that insert this, with a key. 〔☛ Emacs Lisp Examples ₁〕 Or, use any of the template systems. 〔☛ Emacs: Using Templates with YASnippet

Navigation

Here's lisp code that lets you navigate the new section marker.

(defun forward-section ()
  "Move cursor forward to next occurrence of the SECTION SIGN § character."
  (interactive)
  (when (not (search-forward "§" nil t))
    (goto-char (point-max)) ) )

(defun backward-section ()
  "Move cursor backward to previous occurrence of the SECTION SIGN § character."
  (interactive)
  (when (not (search-backward "§" nil t))
    (goto-char (point-min)) ) )

Here's the keys:

(global-set-key (kbd "<s-prior>") 'backward-section) ; Win+PageUp
(global-set-key (kbd "<s-next>") 'forward-section) ; Win+PageDown

(global-set-key (kbd "<C-M-prior>") 'backward-page) ; Ctrl+Alt+PageUp
(global-set-key (kbd "<C-M-next>") 'forward-page) ; Ctrl+Alt+PageDown

Note: I have tried to use this section marker for all my code. I used it for about a year. I'm not sure it can ever be popular. There are many obstacles in adopting this:

(thanks to Miura Masahiro for the tip on inputting §. 【Ctrl+x 8 S】 〔☛ Emacs & Unicode Tips〕 )

blog comments powered by Disqus