15 Slices
Medium 9781565922617

2. Simple New Commands

Bob Glickstein O'Reilly Media ePub

In this chapter:

Traversing Windows

Line-at-a-Time Scrolling

Other Cursor and Text Motion Commands

Clobbering Symbolic Links

Advised Buffer Switching

Addendum: Raw Prefix Argument

In this chapter we'll develop several very small Lisp functions and commands, introducing a wealth of concepts that will serve us when we tackle larger tasks in the chapters to follow.

When I started using Emacs, I was dissatisfied with the keybinding C-x o, other-window. It moves the cursor from one Emacs window into the next. If I wanted to move the cursor to the previous window instead, I had to invoke other-window with -1 as an argument by typing C-u - 1 C-x o, which is cumbersome. Just as cumbersome was pressing C-x o repeatedly until I cycled through all the windows and came back around to what had been the "previous" one.

What I really wanted was one keybinding meaning "next window" and a different keybinding meaning "previous window." I knew I could do this by writing some new Emacs Lisp code and binding my new functions to new keybindings. First I had to choose those keybindings. "Next" and "previous" naturally suggested C-n and C-p, but those keys are bound to next-line and previous-line and I didn't want to change them. The next best option was to use some prefix key, followed by C-n and C-p. Emacs already uses C-x as a prefix for many two-keystroke commands (such as C-x o itself), so I chose C-x C-n for "next window" and C-x C-p for "previous window."

See All Chapters
Medium 9781565922617

C. Debugging and Profiling

Bob Glickstein O'Reilly Media ePub

In this appendix:

Evaluation

The Debugger

Edebug

The Profiler

This appendix describes some facilities in Emacs for testing and debugging your Lisp programs.

A Lisp expression in any buffer can be evaluated by placing the cursor at the end of the expression and pressing C-x C-e (eval-last-sexp). The keystroke M-: (eval-expression) prompts for a Lisp expression to evaluate in the minibuffer. You can also use the commands eval-region and eval-current-buffer.

The *scratch* buffer is normally in Lisp Interaction mode (and if it isn't, it can be put in that mode with M-x lisp-interaction-mode RET). In that mode, C-j is normally eval-print-last-sexp, which is like eval-last-sexp except that it also inserts the result of evaluation into the buffer. Also in Lisp Interaction mode is C-M-x, eval-defun, which evaluates the "defun" that point is in. The meaning of "defun" in this context is broad; it means the enclosing Lisp expression (if there is one) that begins with an open-parenthesis at the left margin. Finally, Lisp Interaction mode allows you to type partial Lisp symbols and complete them with M-TAB.

See All Chapters
Medium 9781565922617

3. Cooperating Commands

Bob Glickstein O'Reilly Media ePub

In this chapter:

The Symptom

A Cure

Generalizing the Solution

This chapter shows how to get different commands to work together by saving information in one command and retrieving it in another. The simplest way to share information is to create a variable and store a value in it. We'll certainly do that in this chapter. For instance, we'll store the current buffer position and reuse it in a later command. But we'll also learn some more sophisticated ways to preserve state, notably markers and symbol properties. We'll combine these techniques with information about buffers and windows to write a set of functions that allow you to "undo" scrolling.

You're deep into editing some complicated Lisp code. You're concentrating, juggling the tenuous connections between the conceptual structures in your brain and the glyphs that represent them on the screen. You're in a particularly tricky part when you notice a typo a few characters to the left. You mean to press C-b C-b C-b to back up and correct it, but insteadhorrors!you press C-v C-v C-v, paging the Emacs window three times, ending up light years away from the code you were editing. Your mental context is ruined as you try to figure out where the cursor was before your mistake, and why, and what you were doing there. As you scroll, or search, or cycle through the mark-ring or the undo-list trying to get back to where you were, you forget about that original typo you were trying to correct, and much later it turns into a bug in your code that takes hours to find.

See All Chapters
Medium 9781565922617

B. Lisp Quick Reference

Bob Glickstein O'Reilly Media ePub

In this appendix:

Basics

Data Types

Control Structures

Code Objects

This appendix summarizes general Lisp syntax as used in Emacs, and some important Lisp functions. It does not summarize Emacs-specific features such as buffers, hook variables, keymaps, modes, and so on. For a complete Emacs Lisp reference, see The GNU Emacs Lisp Reference Manual. Details on obtaining it are in AppendixE.

A Lisp expression is a unit of data that can be evaluated. The expression may be composed of subexpressions, as in the cases of lists and vectors.

Every Lisp expression has a way to produce a value when evaluated. Most kinds of expression are self-evaluating, which means that they are their own value.

A Lisp expression can be treated as literal data instead of being evaluated. Non-self-evaluating expressions must be quoted in order to use them as literals and prevent them from being evaluated.

The symbol nil denotes falsehood. It is exactly the same object as the empty list, (). Every other Lisp object denotes truth, but the symbol t is reserved to mean truth anyway.

See All Chapters
Medium 9781565922617

7. Minor Mode

Bob Glickstein O'Reilly Media ePub

In this chapter:

Paragraph Filling

Modes

Defining a Minor Mode

Mode Meat

In this chapter we'll ratchet our Emacs programming dexterity up a notch by considering times when we don't want extensions to apply to all buffers, but just to a particular type of buffer. For instance, when you're in Lisp mode it's nice to press C-M-a and have Emacs jump backwards to the beginning of the nearest function definition, but you don't want or need that ability when you're editing a textual document. The Emacs "mode" mechanism arranges things so that C-M-a does its magic only when you're in Lisp mode.

The subject of modes in Emacs is a complex one. We'll ease into it by first studying so-called minor modes. A minor mode coexists with a major mode in a buffer, adding a typically small amount of new editing behavior. Every Emacs user is familiar with major modes like Lisp and C and Text, but they may not be aware of little strings that appear on the "mode line" saying things like Fill when you're also in Auto Fill minor mode.

See All Chapters

See All Slices