Emacs Power Usage

getting help:

The first source for help on emacs is(surprise, surprise) The Emacs manual.
We can find it in the Help menu, and can be navigated through easily in emacs.
This is an info page, so you can also navigate with your favorite info
viewer. (I prefer using emacs)

In order to get help on a certain topic we may use: M-x apropos
and search for a reg-exp
This will search for functions variables etc. matching regexp
(all bound symbols)
C-h v : help on variable
C-h k : help on key
C-h f : help on function.
C-h a : apropos command.

(* demonstrate navigating help *)


emacs buffers.

A buffer in emacs is not a file!
a buffer is some data it may represent a file, some internal
emacs data, or it may provide an interface to an inferior process
running under emacs.

Buffers which do not correspond to a file usually have a name
which starts and ends with an * (*scratch* *help*...)

(* demonstrate M-x shell, explain "inferior" *)

emacs may have several windows. emacs starts off with a single
window and we can split an each window into 2 windows vertically with (C-x 2)
We close a window with C-x 0. And maximize a window with C-x 1.
We can also split a window horizontely with C-x 3.
Traveling between windows can easily be done with mouse click or C-x o.

emacs may have more than one frame, each frame having windows as
described above. C-x 5 2 will open a new frame. C-x 5 0 will close it.

point, mark, region:

Many emacs operations can be preformed on regions, every buffer has a region defined
which is a section of the buffer. The region is defined by to coordinates:
point and mark. point is the current cursor position, and mark is a special pointer which
we place wherever we like.
We can place it explicitly at point, using C-space.
or we can use one of many alternate methods for marking a region.
such as: M-space which marks the current paragraph by placing mark at the end of the
paragraph and point at the beginning.
The region is usually not visiable, it is visiable only when mark is active and transient-mark-mode
is enabled. All commands that are meant for marking a region also activate mark, mark is deactivated
automaticly. Commands working on a region will normally not work when mark is not active, this
however is controlled by the variable: mark-even-if-inactive.
when using the mouse region is highlighted regardless of transient-mark-mode.


When copying text, or doing cut and paste with emacs, using commands such as C-k and C-y
we make use of the emacs kill-ring, when clipping text, historicly named killing, we cut the
text out from the current buffer, and push it into the kill-ring. When yanking text we take
the last thing we put in the kill-ring.
When clipping emacs checks the previous command if the previous command was also clipping text,
emacs will concatenate the new kill after the previous kill, and use the same entry in the kill-ring.
The kill-ring keeps previously clipped strings and we can access them.

After yanking with C-y we can replace the yanked text with a different member of the kill ring
using M-y, hitting M-y sequentially will let us travel through the kill ring and choose which text we
want to yank.
When we reach the last element we will continue to the first hence the name ring.(it is implemented as a flat list).

emacs modes:

emacs is good for a large variety of tasks, and we may want
special behavior for various tasks, for example we want
different indentation, we may want some keys to behave
differently. This is accomplished by having Modes.

Each emacs buffer has a major mode(exactly one no more no less)
in addition each buffer may have several minor modes.

Major modes can be classified into three groups:
* "Normal" text, either plain or with mark-up. It
includes Text mode, HTML mode, SGML mode, TeX mode and Outline mode.
* Programming modes. These are modes designed for specific programming language
each programming language has a major-mode designed for it, supported language
include: Lisp mode (various variants), C mode, Fortran, bash, tc shell, Perl
Pascal, prolog, and many more...
* others, which are not for handling files, such as mail mode, help mode,
interactive shell etc.

Selecting modes, minor modes

Emacs will normally select modes correctly automaticly according to file name,
or buffer contents. But we may want to change modes ourselves,
this is most easily accomplished by M-x .

Different Emacs modes create different key strokes to behave differently
even though most keys will remain the same.

Minor modes are optional features which you can turn on or off.
All the minor modes are independent of each other.
minor modes apply to buffers, but some are designed to work on
all buffers, and are activated or deactivated according to a global
variable(one can change this by making the variable local for
one or more buffers)

Variables And Customization:

emacs obviously has its own variables.
for those who do not write lisp, there are two kinds
of scopes a variable may have: global or local.
variables will normally have a global value, and some of the
buffers may have a local variable of that name, hiding the global
value for that buffer.

various variables customize various options, one
way of customization is setting the values of variables.

we can set the value of a variable using the set-variable command:
`M-x set-variable VAR VALUE '

we can also use lisp code:
(set-variable VAR VALUE)

Customization options

another way is using the customize options, which will help us
setting variables and saving the settings:

We can access customization through the menu-bar
Options -> Customize Emacs

We can also use M-x customize.
we navigate and customize variables using the customization buffer
(which has it's own mode "custom")

emacs variables may contain any value. emacs uses dynamic binding
and is strongly typed.

The mode line:
gives us information regarding our current buffer.
it will normally contain the buffer name the buffer mode
and some indication as to where we are in the buffer (this too
can be customized)

Customizing emacs:

emacs has an easy customizing system based on setting special custom
variables, So no lisp knowledge is necessary for creating lisp code in .emacs

We can run emacs customization as seen before, this actually
creates lisp code in .emacs file.
something like this:

'(pc-select-selection-keys-only t)
'(load-home-init-file t t)
'(gnuserv-program (concat exec-directory "/gnuserv"))
'(pc-select-meta-moves-sexps t)
'(pc-selection-mode t))

Keyboard Macros:

A keyboard macro is a series of emacs commands which we group together
to run quickly many times.
(a keyboard macro is not a lisp macro!)

The simple way to create a macro is to record your own key strokes.
we start recording with C-x ( and end with C-x ).
to run the last keyboard macro defined use: C-x e.

In order to keep many macros we must give macros name,
we will use: M-x name-last-kbd-macro.

Now we can use the macro by running M-x macro-name.

If we just wrote a very clever macro and we wish to save it
for further use in future sessions we will generate e-lisp
code which defines the macro we recorded:

M-x insert-kbd-macro.

if we put this in our .emacs file we will have the macro defined
any time we run emacs.

The .emacs file

the .emacs file contains lisp code which is executed every time
you run emacs, on startup.

In the .emacs file we will put macro definitions,
variable customizations, key bindings, adding hooks
and actually any lisp code you want.

key maps:

In emacs we can bind key sequences, to any lisp function. (any interactive
lisp function)

In order to bind a key for the global context we use the lisp
command global-set-key.
for example:

(global-set-key "\C-cs" 'flyspell-mode)
Will bind the sequence Ctrl+C followed by 's to the command
flyspell-mode, which is an interatctive function which toggles
flyspell-mode (a rather nice spell checker).

sometimes we find ourselves accedently hitting key sequuences we
do not want to, it is sometimes a good idea to unbind them,
for example:
(global-unset-key "\C-xf")
will unbind Ctrl+x followed by 'f, which is normally bound
to set fill coloumn, which is much less usefull than open file
bound to "C-xC-f" which we all use much much more.

What to bind?

Which keys to bind?
all sequences which are Ctrl+c followed by a letter, are unbound
initially and are reserved for your own use.

Sometimes you will want to change a binding of an existing key,
do so carefully.
A samall example when this makes sense is:

(global-set-key "\C-x\C-b" 'buffer-menu-other-window)

This makes a small change, instead of "\C-x\C-b" to open the buffer
list in this window, it will open it in the other window and place the cursor,
in that window.
Meir Maor