Ciao Emacs

1 Conventions

Emacs is built on a LisP interpreter called elisp. Within an emacs session, you can run any elisp command by typing Alt-x which will put the cursor into a minibuffer at the bottom of the screen where you enter the command (e.g. isearch-forward) followed by RETURN. Alternatively most commands have bindings to Control key or Metakey sequences. When you type a key-binding sequence, emacs executes the elisp command bound to the key. Emacs documentation uses the convention that

2 Emacs

GNU Emacs is a text editor written by Richard Smallman of GNU fa,e. Emacs is build on a LisP interpreter called elisp. Whereas vi(m) has 2 modes: command and insert, emacs is always in insert mode and relies on conrtol key-bindings to execute commands (which are implemented in the underlying elisp language). But emacs has many so-called major modes which set behavious considered appropriate for editing different languages and/or applications such as C, perl, mail, shell, etc. The Ciao Development Environment (CiaoDE) has created a ciao-mode to facilitate Constraint Logic Programming(CLP) for Ciao Prolog and other Prologs. The ciao-mode will be invoked whenever you edit a file suffixed the .pl from an emacs session.

2.1 Minibuffer

2.1.1 M-x and C-q

Withib an emacs session, you can run any elisp command by typing M-x which will put you into a minibuffer at the bottom of the screen where you enter the command (e.g. isearch-forward) followed by RETURN. While in the minibuffer if you wish to embed or TAB with C-q (for quote) otherwise emacs will attempt to complete or lookjup the string sa far typed in.
Alternatively most commands have binding to Control key or Metakey sequences. When you type a key-binding sequence, emacs executes the eliosp command bound to the key.

3 Elisp args

You can prefix an elisp command with a numeric argument. e.g. to arrow down 5 lines, you want to supply the argument 5 to the C-n command. You type either of the following
Note: C-u not followed by a numeric argument defaults its argument to 4. So for example, C-u C-n move the cursor down 4 lines. A numeric argument is a sequence of digits optionally preceded by a - sign.

4 Emacs help

Within emacs you can invoke emacs documentation by hitting C-h followed one of several possible letters:
Binding elisp Action
C-h ? List all possible help options
C-h b describe-bindings List all key-bindings for this buffer
C-h t help-with-tutorial Run the emacs tutorial
C-h m describe-mode Describe the mode for the current buffer
C-h c describe-key-briefly What elisp command does this key run
C-h k describe-key What elisp command and action
C-h a command-apropos Which command involves this concept
C-h v describe-variable Show value of variable

5 Major Modes

Emacs has several major modes which offer customised support for different languages and file formats (such as C, perl, python, pascal, ruby, etc). Emacs tries to guess from builtin file properties such as suffices which mode is suitable. People who develop in emacs tend to live in the emacs program instead of leaving it to run shell commands, send and receive mail, etc. For instance you can run a shell session from within emacs by typing M-x shell

5.1 Shell

The buffer created by the emacs shell major mode melds the shell prompt, the shell command you type in, and the output of shell command into the shell buffer. This mostly behaves just like the command line shell except that the up arrow goes to the previous line in the buffer rather than the previous command entered. To get the previous command type: M-p

5.2 Other major modes

Emacs has over the years developed and distributed major modes for all the common development languages such as C, pascal, perl, python, ruby, etc. as well as for builtin apps such as mail, shell etc. These offer syntax highlighting, appropriate indentation, auto-completion of keywords and so on.

6 Ciao

Ciao has created an emacs major mode (i.e. its own set of bindings for the purposes of Ciao programming, debugging, compiling, etc). They are mostly keyed with a prefix of C-c. Editing any file with a .pl suffix should cause emacs to load the Ciao bindings and display a Ciao menu. If not, type M-x ciao-mode
Binding Mnemonic Action
C-c t top (Re)Start top level
C-c l load (Re)Load buffer to top level
C-c f fix? (Re)Load & Check buffer to top level
C-c L Load (Re)Load main and other buffers to top level
C-c x xecute Make executable buffer if main
C-c ‘ goto next error
C-c e erase remove error mark(s) from buffer
C-c q query Set default query
C-c Q Query Call default query
C-c s set (Un)Set current buffer as main
C-c I S Insert Script Insert Unix script header
C-c o object compile current buffer to .o file
C-c a active make current buffer active?
C-c d debug (Un)debug buffer source
C-c m mode Select debugging mode
C-c M m Multi-mode Select multiple buffers for debug
C-c S b source break set breakpoint on current symbol
C-c S v remove breakpoint from current symbol
C-c S n source none? remove all breakpoints
C-c S l source list list all breakpoints
C-c S d source debug toggle debug mode
C-c r region load region for debug?
C-c p predicate load predicate for debug
C-c D ... LPDoc commands
C-c D B document buffer generate documentation for buffer
C-c D V document view view documentation for this buffer
C-c D F document format change format of document (e.g. html, info, )
C-c D S document settings visit/create LPSETTINGS.pl file
C-c D G document generate generate documentation
C-c C-a activate Activate version control for file
C-c C-s set Insert a changelog entry
C-c n no Increase version no
C-c C-n next goto next changelog entry
C-c TAB Find help for symbol under cursor
C-c / Find a completion for symbol under cursor
C-c C-M manual goto info dir containing Ciao manuals
C-h m mode emacs help for modes
C-c C-u Undo kill all text from last stuff output to point
C-c C-w word kill backwards to enf of word (arg times)
C-c C-c like bash ^C interrupt current job
C-c C-Z like bash ^Z stop current sub-job
C-c C-\ like bash ^\ send quit signal

6.1 Trouble Shooting

If editing a .pl file does not cause emacs to invoke ciao-mode then you need to do the following:
Test that the ciao-mode has been defined (in elisp) and is available to emacs by running the command:
M-x ciao-mode
This should result in the word (ciao) showing in the emacs frame header. If not there may be an error message hinting at what is wrong. When ciao-mode is running you should see a Ciao set of menu options on the menubar of emacs.

7 Standard Emacs

7.1 Cursor and Point

Emacs executes its commands at the point (which is an imaginary point just before (i.e. to the left of) the cursor).

7.2 Cut and Paste (or Copy and Paste)

7.2.1 Cut or Copy

To identify chunks of text, you place a mark using C-@ then move the cursor to another position and execute a copy (M-w) or delete (C-w) command to save or move the text into emacs’ kill-ring. You can then move the point elsewhere (say to another file buffer). With a mouse, you can click and drag to select a region for copying or deleting.

7.2.2 Paste

To paste what is in the kill-ring, you use emacs’ yank command C-y to copy. In both cases the contents of the kill-ring are inserted at the point (just before the cursor).

7.2.3 Cut, Copy, and Paste on the Mac

On the Mac if you are using Aquamacs, the Command key can be used in ways consistent with OS X. Specifically Command with C, X, and V can be used for copy, delete, and paste.

8 Files and Buffers

When you edit a file with emacs using the File menu or C-x C-f, emacs creates a frame buffer in which the changes you make are done. Nothing happens to the file until you Save with C-x C-s.
The buffer has a menu on its top line with options Buffers, Files, Tools, Edit, Search, Help and others if a major mode such as Ciao is invoked.
The buffer has a status line at the bottom which is in inverse video and has the following format:
-----Emacs: filename (major minor)--Ln--All----------------------------------
where
You will have a buffer for each file you are editing with emacs.

8.2.1 Minibuffer

At the bottom of the screen there is also a one line buffer called the minibuffer where error messages and emacs prompts for input are displayed or where you can execute elisp command with M-x.

9 Standard Emacs

9.1 Navigation

You can use the usual cursor keys (left and right, up and down arrows) or mouse click to place the cursor somewhere in a buffer. In addition there are the following key-bindings:
Binding elisp Action
C-f forward-char as right arrow
C-b backward-char as left arrow
C-p previous-line as up arrow
C-n next-line as down arrow
M-f forward-word
M-b backward-word
C-a beginning-of-line
C-e end-of-line
C-v scroll-up
M-v scroll-down
M-< beginning-of-buffer
M-> end-of-buffer
C-l recenter

9.2 Yank and Put

The emacs equivalent of vi’s y and p are kill and yank. (Note yank is the opposite of vi’s yank and pulls from the kill ring into the buffer). Emac’s various kill commands move the string killed into a kill ring.
Binding elisp Action
C-d delete-char Delete char (not into kill ring)
DEL delete-backward-char Delete previous char (not into kill ring)
M-d kill-word Delete next word into kill ring
M-DEL backward-kill-word Delete previous word
C-k kill-line Delete from cursor to eol
M-k kill-sentence Delete next sentence
C-x DEL backward-kill-sentence Delete prev sentence
C-w kill-region Delete a region (if marked)
M-w kill-ring-save Copy region to kill ring
kill-paragraph Delete next paragraph
backward-kill-paragraph Delete previous paragraph
C-y yank copy from kill ring to point
M-y yank-pop delete from kill ring to point
For the 2 commands where there is no binding you have to use M-x then type the elisp command into the minibuffer.

9.3 Marks & Regions

You can place a single mark in a buffer using the binding: C-@ (or C-SPACE). When you navigate elsewhere, the area between your current cursor and the mark is a region. To apply commands to the region you use the binding C-w to delete or M-w to save to the kill ring. Subsequent C-y will paste (yank) to the current cursor. To check the region (if your version of emacs does not mark it on the screen, use C-x C-x to jump back and forth (elisp: exchange-mark-and-point).
With the mouse, left click and drag to mark a region.
Binding elisp Action
C-@ set-mark-command Mark boundary of region
M-@ mark-word Set mark after next word
C-x C-x exchange-point-and-mark Exchange location of cursor and mark
C-x u undo
M-h mark-paragraph Set mark at eop, cursor at bop
C-x h mark-whole-buffer Set mark at eob, cursor at bob
C-x C-p mark-page
C-w kill-region Delete marked region into kill ring
M-w kill-ring-save Copy maked region into kill ring
M-! cmd RET shell-command Run shell cmd (no input)
M-1 M-! cmd RET shell-command As above inserting output at point
M-| cmd RET shell-command-on-region pipe region to shell cmd
M-1 M-| cmd RET shell-command-on-region As above replacing region with output

9.4 Searches

Whereas vi has the / n . sequence for search and replace, Emacs has different kind of searches, each with its own bindings.

9.5 Query-Replace loop

M-% string RET replacement RET: puts you into a search loop, stopping at next instance each time. Within the loop you are prompted for the action you want or you can leave the loop for some other action with C-r ... C-M-c which returns you to the loop.
Within the loop:
Char Action
y Yes, replace
n No skip to next instance
. Replace and exit loop
, Replace and stay (until I hit y)
! Replace all the rest and don’t ask
C-r Create a subsession for other emacs commands. Return to query loop using C-M-c
q Exit loop
You can use SPACE, DEL, and RETURN respectively for y, n, and q inside the query loop.