\chapter{The Emacs editor}
\label{Emacs}
\label{editor}

Almost all of your interaction with the Bobcat Scheme system will be
via typing at the Emacs text editor.  The Emacs editor was originally
designed and implemented in the 1970s at MIT by Richard Stallman and
it has been widely copied and reimplemented for many machines.  The
Emacs used in 6.003 is GNU Emacs, which is a free version of Emacs
distributed by Stallman and the Free Software
Foundation.\footnote{{\em Free} means that everyone is free to use GNU
Emacs and free to redistribute it on a free basis.  The Free Software
Foundation is a non-profit, tax-exempt organization dedicated to the
development and support of high-quality free software.  For more
information on free software, read the {\em GNU Manifesto}.  This is
included in the {\em GNU Emacs Manual}~\cite{GnuEmacs} and can be read
on-line at a 6.003 computer using the {\tt INFO} documentation
reader.}\mainindex{Free Software Foundation} GNU Emacs for 6.003 is
similar to the NMODE editor used in 6.001 and is (hopefully) identical
to the version of GNU Emacs used on Project Athena workstations.

Emacs is a sophisticated and extensive editor, with many commands.
This manual provides only very sparse documentation because good Emacs
documentation is readily available elsewhere and Emacs contains
extensive on-line documentation and help facilities.\footnote{For
6.003, we have made some customizations to GNU Emacs for running
Scheme.  These are not included in the {\em GNU Emacs Manual} or in
the on-line {\tt INFO} documentation.  These commands include the
extended commands mentioned throughout this manual and also Scheme
evaluation commands described below in section~\ref{schemeemacs}.}

\begin{itemize}

\item The {\em GNU Emacs Manual} by Richard Stallman provides a complete
description of GNU Emacs.  The manual can be purchased from MIT
Information Services in Room 11-209.

\item The Emacs command \ctlh{I} will run the {\tt INFO}
documentation reader.  This includes the {\em GNU Manifesto} and
all of the {\em GNU Emacs Manual} in on-line form.

\item If you have never used Emacs, the command \ctlh{T} will run an
on-line Emacs tutorial.

\item The Emacs command \ctlh{?} will show you a list of help commands
included in Emacs.

\item When you are typing an Emacs extended command (using \esc{X}),
typing a question mark at any point will show you all the
commands that begin with the characters you have typed.

\end{itemize}

\noindent
The command summary in this chapter is adapted from the {\em GNU Emacs
Reference Card} originally designed by Neal Ziring of Washington Univ.
Copies of the card are included in the {\em GNU Emacs Manual} and can
be purchased separately from Information Services.

\section{Basic Emacs concepts}

\begin{description}
\item [Insertion] You are always in 
insert mode in Emacs.
Ordinary text is inserted
as it is typed.

\item [Commands] Emacs commands are invoked by
typing sequences of control 
characters.  Control
chars are written \ctl{\var{char}},
escape sequences \esc{\var{char}}.

\item [Extended commands]  These commands are named by
text strings.
To invoke an extended command, type \esc{X} followed by the command
name.  Your typing will appear at the bottom of the screen in the echo
area.

\item [Buffer] A block of 
text that you may 
examine and modify
in the editor.

\item [Window] An area on the screen.
A buffer can be displayed
in one or more windows.

\item [File] A block of text that can be read into
a buffer or written from a buffer.

\item [Kills] Text that is {\em killed} is placed
in a special kill-buffer.  
Killed text can be retrieved with a {\em yank}.
The most recent sixteen kills are kept in the {\em kill-ring}.

\item [Point] The current cursor location 
in each buffer is called
{\em point}.

\item [Mark] The {\em mark} is an invisible pointer in
each buffer.
It is set by many commands to the
location where they were performed.

\item [Region] Text between the {\em point}
and the {\em mark} is called the
{\em region}.

\item [Registers] Emacs has 26
general registers, named {\em a}
through {\em z}.  Registers may hold
commands, locations, numbers, or text.

\item [Modes] Every buffer has a major mode,
and maybe some minor modes.
{\em Major} modes determine
language-sensitive behavior
and define special mode commands.
{\em Minor} modes add special features
without adding new commands.

\end{description}

\section{Getting help}
\label{EmacsHelp}

\begin{description}

\item [\esc{?}]	Invoke the self-documenter.
The self-documenter function will prompt
for a help command.  Most help commands
ask for a name or character, which they
will then describe for you.

\end{description}

\begin{tabular}{ll}
\ctlh{a} &  document commands apropos a word \\
\ctlh{b} &  list all key bindings \\
\ctlh{c} &  describe a key sequence briefly \\
\ctlh{f} &  describe a function \\
\ctlh{k} &  describe a key sequence completely	 \\
\ctlh{l} &  show last 100 characters typed \\
\ctlh{m} &  describe this major mode \\
\ctlh{v} &  describe a variable \\
\ctlh{?} &  show list of help commands \\
\ctlh{t} &  visit a friendly Emacs tutorial \\
\ctlh{i} &  invoke Info documentation reader 
\end{tabular}

\section{The display}

\begin{description}
\item [Text Area] A view onto a text buffer.
Each window has its own text area.
Your display always has at least
one text area on it.

\item [Mode Line] Status line displayed at the bottom of a window.
The mode line
displays the current buffer name, major and minor
editing modes, and current location in the file.
The variable {\em mode-line-format} defines the contents
and layout of the mode line.

\item [Echo Area] A one-line area at the bottom of the screen.
Error messages and progress messages are displayed
in the echo area.
This area is also called the {\em mini-buffer},
because Emacs command prompts and your responses are typed there.
\end{description}

\section {Simple motion}

\begin{tabular}{ll}
\ctl{a} & beginning-of-line \\
\ctl{b} & backward-char \\
\ctl{e} & end-of-line \\
\ctl{f} & forward-char \\
{\sc tab} & indent-for-tab-command \\
{\sc return} & newline \\
\ctl{j} & newline-and-indent \\
\ctl{n} & next-line \\
\ctl{o} & open-line \\
\ctl{p} & previous-line \\
{\sc backspace} & delete-backward-char \\
\esc{<} & beginning-of-buffer \\
\esc{>} & end-of-buffer 
\end{tabular}

\section{Scrolling}
\begin{tabular}{ll}
\ctl{v} & scroll-up \\
\ctlx{<} & scroll-left \\
\ctlx{>} & scroll-right \\
\escctl{v} & scroll-other-window \\
\esc{v} & scroll-down 
\end{tabular}

\section{Text motion}

\begin{tabular}{ll}
\ctlx{\char"5B{}} & backward-page \\
\ctlx{\char"5D{}} & forward-page \\
\esc{\char"5B{}} & backward-paragraph \\
\esc{\char"5D{}} & forward-paragraph \\
\esc{a} & backward-sentence \\
\esc{b} & backward-word \\
\esc{e} & forward-sentence \\
\esc{f} & forward-word 
\end{tabular}

\section{Transposition}

\begin{tabular}{ll}
\ctl{t} & transpose-chars \\
\ctlxctl{t} & transpose-lines \\
\esc{t} & transpose-words
\end{tabular}

\section{Queries and fixes} 

\begin{tabular}{ll}
\ctl{g} & quit immediately \\
\ctl{l} & recenter (and fix screen) \\
\escctl{l} & recenter and clear graphics \\
\ctl{h} & help-command \\
\ctl{q} & quoted-insert \\
\ctlx{=} & what-cursor-position \\
\ctlx{l} & count-lines-page \\
\ctlx{u} & undo previous command \\
\ctl{\char"5D} & abort-recursive-edit \\
\esc{=} & count-lines-region
\end{tabular}

\subsection{Query replace}

\begin{description}

\item [\esc{\%}] Invoke interactive replacement.
Query-replace will prompt you for a string
and another string with which to replace it.
Replacement proceeds from the current location (point)
to the end of the buffer.
\end{description}

{\samepage
\noindent
The following query responses are recognized.

\begin{tabular}{ll}
{\rm space} & replace this match \\
{\sc backspace} & skip to next match \\
{\sc esc} & exit query replace \\
{\tt .} & replace this match and exit \\
\ctl{r} & enter recursive edit \\
\ctl{w} & delete match and recursive edit \\
{\tt !} & replace all remaining matches \\
{\tt \char"5E} & move back to previous match
\end{tabular}

     }   %\samepage

\section{Deletes, kills, and yanks}

\begin{tabular}{ll}
\ctl{d} & delete-char \\
\ctl{k} & kill-line \\
\ctl{w} & kill-region \\
\ctl{y} & yank \\
{\sc backspace} & delete-backward-char \\
\ctlxctl{o} & delete-blank-lines \\
\ctlx{{\sc backspace}} & backward-kill-sentence \\
\escctl{w} & append-next-kill \\
\esc{\backwhack} & delete-horizontal-space \\
\esc{{\tt \char"5E}} & delete-indentation \\
\esc{d} & kill-word \\
\esc{k} & kill-sentence \\
\esc{w} & copy-region-as-kill \\
\esc{y} & yank-pop \\
\esc{{\sc backspace}} & backward-kill-word
\end{tabular}

\section{Manipulating Scheme expressions}

The term {\em sexp} stands for symbolic expression.  This refers to a
Lisp ``unit'' such as an atom or a list.  Thus, ``forward-sexp'' will
move past the next symbol if the cursor is at the beginning of a
symbol, and move to after the matching close parenthesis if the cursor
is at an open parenthesis.\mainindex{sexp (symbolic expression)}

\begin{tabular}{ll}
\escctl{a} & beginning-of-definition \\
\escctl{b} & backward-sexp \\
\escctl{e} & end-of-definition \\
\escctl{f} & forward-sexp \\
\escctl{k} & kill-sexp \\
\escctl{t} & transpose-sexps \\
\esc{(} & insert-parentheses \\
\esc{)} & move-past-close-and-reindent
\end{tabular}

\section{Evaluating Scheme expressions}
\index{evaluating Scheme expressions}
\label{schemeemacs}

\begin{tabular}{ll}
\ctlc{g} & stop evaluating \\
\ctl{z} & evaluate expression preceding cursor \\
\esc{z} & evaluate current definition \\
\escctl{z} & evaluate region \\
\esc{o} & evaluate buffer
\end{tabular}

\section{Buffers}

\begin{tabular}{ll}
\ctlxctl{b} & list-buffers \\
\ctlxctl{f} & find-file \\
\ctlxctl{s} & save-buffer \\
\ctlx{a} & append-to-buffer \\
\ctlx{b} & switch-to-buffer \\
\ctlx{h} & mark-whole-buffer \\
\ctlx{k} & kill-buffer \\
\ctlx{n} & narrow-to-region \\
\ctlx{s} & save-some-buffers \\
\ctlx{w} & widen \\
\esc{\char"7E} & mark buffer as not-modified
\end{tabular}

\section{Windows}

The extended commands for configuring the screen are described in
section~\ref{windows}.  In addition, Emacs has the following commands:

\begin{tabular}{ll}
\ctlx{o} & move to other-window \\
\escctl{v} & scroll-other-window
\end{tabular}

\section{Files}

\begin{tabular}{ll}
\ctlxctl{f} & find-file \\
\ctlxctl{r} & find-file-read-only \\
\ctlxctl{v} & find-alternate-file \\
\ctlxctl{w} & write-file \\
\ctlx{i} & insert-file
\end{tabular}

\section{Searching}

\begin{tabular}{ll}
\ctl{s} & isearch-forward \\
\ctl{r} & isearch-backward \\
\esc{\%} & query-replace \\
\escctl{s} & isearch-forward-regexp
\end{tabular}
\noindent
To terminate a search, press the {\sc esc} key.

\section{Case changes}

\begin{tabular}{ll}
\ctlxctl{l} & downcase-region \\
\ctlxctl{u} & upcase-region \\
\esc{c} & capitalize-word \\
\esc{l} & downcase-word \\
\esc{u} & upcase-word
\end{tabular}

\section{Filling and centering}

\begin{tabular}{ll}
\ctlx{f} & set-fill-column \\
\ctlx{.} & set-fill-prefix \\
\esc{q} & fill-paragraph \\
\end{tabular}

\section{Marks and registers}

\begin{tabular}{ll}
\ctl{@} & set-mark \\
\esc{\backwhack} & set-mark (same as \ctl{@}) \\
\ctlxctl{p} &  mark-page \\ 
\ctlxctl{x} & exchange-point-and-mark \\
\ctlx{/} & point-to-register \\
\ctlx{g} & insert-register \\
\ctlx{h} & mark-whole-buffer \\
\ctlx{j} & register-to-point \\
\ctlx{r} & copy-rectangle-to-register \\
\ctlx{x} & copy-to-register \\
\escctl{h} & mark-definition \\
\esc{@} & mark-word
\end{tabular}

\section{Macros and repetition}

\begin{tabular}{ll}
\ctl{u} & universal-argument \\
\ctlx{(} & start-kbd-macro \\
\ctlx{)} & end-kbd-macro \\
\ctlx{e} & call-last-kbd-macro \\
\ctlx{q} & kbd-macro-query \\
\esc{-} & negative-argument \\
\esc{0}\dotsfoo\esc{9} & digit-argument
\end{tabular}

\section{Subsystems}

\begin{tabular}{ll}
\ctlx{d} & dired (edit directory)
\end{tabular}

\section{Exiting}

\begin{tabular}{ll}
\escctl{c} & exit-recursive-edit (such as \ctl{r} from query replace)
\end{tabular}

{\samepage

\section{Some Editing Modes}

Invoke these as extended commands using \esc{X}.  Use the on-line
documentation to learn about these and other Emacs modes.

\begin{scheme}
scheme-mode               text-mode
abbrev-mode               auto-fill-mode
fundamental-mode          overwrite-mode
\end{scheme}

      }   %\samepage
