Frames and X Windows
********************

When using the X Window System, you can create multiple windows at the X level in a single Emacs session.  Each X window that belongs to Emacs displays a frame which can contain one or several Emacs windows. A frame initially contains a single general-purpose Emacs window which you can subdivide vertically or horizontally into smaller windows.  A frame normally contains its own echo area and minibuffer, but you can make frames that don't have these---they use the echo area and minibuffer of another frame.

Editing you do in one frame also affects the other frames.  For instance, if you put text in the kill ring in one frame, you can yank it in another frame.  If you exit Emacs through C-x C-c in one frame, it terminates all the frames.  To delete just one frame, use C-x 5 0.

To avoid confusion, we reserve the word ``window'' for the subdivisions that Emacs implements, and never use it to refer to a frame.

Emacs compiled for MS-DOS emulates some aspects of the window system so that you can use many of the features described in this chapter. See MS-DOS Input, for more information.

Emacs compiled for MS Windows mostly supports the same features as under X.

Features which rely on text in multiple faces (such as Font Lock mode) will also work on non-windowed terminals that can display more than one face, whether by colors or underlining and emboldening, such as the Linux console.  Emacs determines automatically whether the terminal has such support.

Mouse Commands for Editing
==========================

The mouse commands for selecting and copying a region are mostly compatible with the xterm program.  You can use the same mouse commands for copying between Emacs and other X client programs.

If you select a region with any of these mouse commands, and then immediately afterward type the DELETE function key, it deletes the region that you selected.  The BACKSPACE function key and the ASCII character DEL do not do this; if you type any other key in between the mouse command and DELETE, it does not do this.

Mouse-1
     Move point to where you click (mouse-set-point). This is normally the left button.

Drag-Mouse-1
     Set the region to the text you select by dragging, and copy it to the kill ring (mouse-set-region).  You can specify both ends of the region with this single command.

     If you move the mouse off the top or bottom of the window while dragging, the window scrolls at a steady rate until you move the mouse back into the window.  This way, you can select regions that don't fit entirely on the screen.  The number of lines scrolled per step depends on how far away from the window edge the mouse has gone; the variable mouse-scroll-min-lines specifies a minimum step size.

Mouse-2
     Yank the last killed text, where you click (mouse-yank-at-click). This is normally the middle button.

Mouse-3
     This command, mouse-save-then-kill, has several functions depending on where you click and the status of the region.

     The most basic case is when you click Mouse-1 in one place and then Mouse-3 in another.  This selects the text between those two positions as the region.  It also copies the new region to the kill ring, so that you can copy it to someplace else.

     If you click Mouse-1 in the text, scroll with the scroll bar, and then click Mouse-3, it remembers where point was before scrolling (where you put it with Mouse-1), and uses that position as the other end of the region.  This is so that you can select a region that doesn't fit entirely on the screen.

     More generally, if you do not have a highlighted region, Mouse-3 selects the text between point and the click position as the region.  It does this by setting the mark where point was, and moving point to where you click.

     If you have a highlighted region, or if the region was set just before by dragging button 1, Mouse-3 adjusts the nearer end of the region by moving it to where you click.  The adjusted region's text also replaces the old region's text in the kill ring.

     If you originally specified the region using a double or triple Mouse-1, so that the region is defined to consist of entire words or lines, then adjusting the region with Mouse-3 also proceeds by entire words or lines.

     If you use Mouse-3 a second time consecutively, at the same place, that kills the region already selected.

Double-Mouse-1
     This key sets the region around the word which you click on.  If you click on a character with ``symbol'' syntax (such as underscore, in C mode), it sets the region around the symbol surrounding that character.

     If you click on a character with open-parenthesis or close-parenthesis syntax, it sets the region around the parenthetical grouping (sexp) which that character starts or ends.  If you click on a character with string-delimiter syntax (such as a singlequote or doublequote in C), it sets the region around the string constant (using heuristics to figure out whether that character is the beginning or the end of it).

Double-Drag-Mouse-1
     This key selects a region made up of the words you drag across.

Triple-Mouse-1
     This key sets the region around the line you click on.

Triple-Drag-Mouse-1
     This key selects a region made up of the lines you drag across.

The simplest way to kill text with the mouse is to press Mouse-1 at one end, then press Mouse-3 twice at the other end. See Killing.  To copy the text into the kill ring without deleting it from the buffer, press Mouse-3 just once---or just drag across the text with Mouse-1.  Then you can copy it elsewhere by yanking it.

To yank the killed or copied text somewhere else, move the mouse there and press Mouse-2.  See Yanking.  However, if mouse-yank-at-point is non-nil, Mouse-2 yanks at point.  Then it does not matter where you click, or even which of the frame's windows you click on.  The default value is nil.  This variable also affects yanking the secondary selection.

To copy text to another X window, kill it or save it in the kill ring. Under X, this also sets the primary selection.  Then use the ``paste'' or ``yank'' command of the program operating the other window to insert the text from the selection.

To copy text from another X window, use the ``cut'' or ``copy'' command of the program operating the other window, to select the text you want. Then yank it in Emacs with C-y or Mouse-2.

These cutting and pasting commands also work on MS-Windows.

When Emacs puts text into the kill ring, or rotates text to the front of the kill ring, it sets the primary selection in the X server. This is how other X clients can access the text.  Emacs also stores the text in the cut buffer, but only if the text is short enough (x-cut-buffer-max specifies the maximum number of characters); putting long strings in the cut buffer can be slow.

The commands to yank the first entry in the kill ring actually check first for a primary selection in another program; after that, they check for text in the cut buffer.  If neither of those sources provides text to yank, the kill ring contents are used.

Secondary Selection
===================

The secondary selection is another way of selecting text using X.  It does not use point or the mark, so you can use it to kill text without setting point or the mark.

M-Drag-Mouse-1
     Set the secondary selection, with one end at the place where you press down the button, and the other end at the place where you release it (mouse-set-secondary).  The highlighting appears and changes as you drag.

     If you move the mouse off the top or bottom of the window while dragging, the window scrolls at a steady rate until you move the mouse back into the window.  This way, you can mark regions that don't fit entirely on the screen.

M-Mouse-1
     Set one endpoint for the secondary selection (mouse-start-secondary).

M-Mouse-3
     Make a secondary selection, using the place specified with M-Mouse-1 as the other end (mouse-secondary-save-then-kill).  A second click at the same place kills the secondary selection just made.

M-Mouse-2
     Insert the secondary selection where you click (mouse-yank-secondary).  This places point at the end of the yanked text.

Double or triple clicking of M-Mouse-1 operates on words and lines, much like Mouse-1.

If mouse-yank-at-point is non-nil, M-Mouse-2 yanks at point.  Then it does not matter precisely where you click; all that matters is which window you click on.  See Mouse Commands.

Following References with the Mouse
===================================

Some Emacs buffers display lists of various sorts.  These include lists of files, of buffers, of possible completions, of matches for a pattern, and so on.

Since yanking text into these buffers is not very useful, most of them define Mouse-2 specially, as a command to use or view the item you click on.

For example, if you click Mouse-2 on a file name in a Dired buffer, you visit that file.  If you click Mouse-2 on an error message in the *Compilation* buffer, you go to the source code for that error message.  If you click Mouse-2 on a completion in the *Completions* buffer, you choose that completion.

You can usually tell when Mouse-2 has this special sort of meaning because the sensitive text highlights when you move the mouse over it.

Mouse Clicks for Menus
======================

Mouse clicks modified with the CTRL and SHIFT keys bring up menus.

C-Mouse-1
     This menu is for selecting a buffer.

     The MSB (`mouse select buffer') global minor mode alters this menu to a form some people prefer and which is customizable.  See the Custom group msb.

C-Mouse-2
     This menu is for specifying faces and other text properties for editing formatted text.  See Formatted Text.

C-Mouse-3
     This menu is mode-specific.  For most modes if Menu-bar mode is on, this menu has the same items as all the mode-specific menu-bar menus put together.  Some modes may specify a different menu for this button. (Some systems use Mouse-3 for a mode-specific menu.  We took a survey of users, and found they preferred to keep Mouse-3 for selecting and killing regions.  Hence the decision to use C-Mouse-3 for this menu.)  If Menu-bar mode is off, this menu contains all the items which would be present in the menu bar---not just the mode-specific ones---so that you can access them without having to display the menu bar.

S-mouse-1
     This menu is for specifying the frame's principal font.

Mode Line Mouse Commands
========================

You can use mouse clicks on window mode lines to select and manipulate windows.

Mouse-1
     Mouse-1 on a mode line selects the window above.  By dragging Mouse-1 on the mode line, you can move it, thus changing the height of the windows above and below.

Mouse-2
     Mouse-2 on a mode line expands that window to fill its frame.

Mouse-3
     Mouse-3 on a mode line deletes the window above.

C-Mouse-2
     C-Mouse-2 on a mode line splits the window above horizontally, above the place in the mode line where you click.

C-Mouse-2 on a scroll bar splits the corresponding window vertically.  See Split Window.

The commands above apply to areas of the mode line which do not have mouse bindings of their own.  Normally some areas, such as those displaying the buffer name and the major mode name, have their own mouse bindings.  Help on these bindings is echoed when the mouse is positioned over them.

Creating Frames
===============

The prefix key C-x 5 is analogous to C-x 4, with parallel subcommands.  The difference is that C-x 5 commands create a new frame rather than just a new window in the selected frame (see Pop Up Window).  If an existing visible or iconified frame already displays the requested material, these commands use the existing frame, after raising or deiconifying as necessary.

The various C-x 5 commands differ in how they find or create the buffer to select:

C-x 5 1
     Delete all frames except the selected one (delete-other-frames).

C-x 5 2
     Create a new frame (make-frame-command).

C-x 5 b BUFNAME RET
     Select buffer BUFNAME in another frame.  This runs switch-to-buffer-other-frame.

C-x 5 f FILENAME RET
     Visit file FILENAME and select its buffer in another frame.  This runs find-file-other-frame.  See Visiting.

C-x 5 d DIRECTORY RET
     Select a Dired buffer for directory DIRECTORY in another frame. This runs dired-other-frame.  See Dired.

C-x 5 m
     Start composing a mail message in another frame.  This runs mail-other-frame.  It is the other-frame variant of C-x m. See Sending Mail.

C-x 5 .
     Find a tag in the current tag table in another frame.  This runs find-tag-other-frame, the multiple-frame variant of M-.. See Tags.

C-x 5 r FILENAME RET
     Visit file FILENAME read-only, and select its buffer in another frame.  This runs find-file-read-only-other-frame. See Visiting.

You can control the appearance of new frames you create by setting the frame parameters in default-frame-alist.  You can use the variable initial-frame-alist to specify parameters that affect only the initial frame.  See Initial Parameters (The Emacs Lisp Reference Manual), for more information.

The easiest way to specify the principal font for all your Emacs frames is with an X resource (see Font X), but you can also do it by modifying default-frame-alist to specify the font parameter, as shown here:

     (add-to-list 'default-frame-alist '(font . "10x20"))

Making and Using a Speedbar Frame
=================================

An Emacs frame can have a speedbar, which is a vertical window that serves as a scrollable menu of files you could visit and tags within those files.  To create a speedbar, type M-x speedbar; this creates a speedbar window for the selected frame.  From then on, you can click on a file name in the speedbar to visit that file in the corresponding Emacs frame, or click on a tag name to jump to that tag in the Emacs frame.

Initially the speedbar lists the immediate contents of the current directory, one file per line.  Each line also has a box, [+] or <+>, that you can click on with Mouse-2 to ``open up'' the contents of that item.  If the line names a directory, opening it adds the contents of that directory to the speedbar display, underneath the directory's own line.  If the line lists an ordinary file, opening it up adds a list of the tags in that file to the speedbar display.  When a file is opened up, the [+] changes to [-]; you can click on that box to ``close up'' that file (hide its contents).

Some major modes, including Rmail mode, Info, and GUD, have specialized ways of putting useful items into the speedbar for you to select.  For example, in Rmail mode, the speedbar shows a list of Rmail files, and lets you move the current message to another Rmail file by clicking on its <M> box.

A speedbar belongs to one Emacs frame, and always operates on that frame.  If you use multiple frames, you can make a speedbar for some or all of the frames; type M-x speedbar in any given frame to make a speedbar for it.

Multiple Displays
=================

A single Emacs can talk to more than one X Windows display. Initially, Emacs uses just one display---the one specified with the DISPLAY environment variable or with the --display option (see Initial Options).  To connect to another display, use the command make-frame-on-display:

M-x make-frame-on-display RET DISPLAY RET
     Create a new frame on display DISPLAY.

A single X server can handle more than one screen.  When you open frames on two screens belonging to one server, Emacs knows they share a single keyboard, and it treats all the commands arriving from these screens as a single stream of input.

When you open frames on different X servers, Emacs makes a separate input stream for each server.  This way, two users can type simultaneously on the two displays, and Emacs will not garble their input.  Each server also has its own selected frame.  The commands you enter with a particular X server apply to that server's selected frame.

Despite these features, people using the same Emacs job from different displays can still interfere with each other if they are not careful. For example, if any one types C-x C-c, that exits the Emacs job for all of them!

Special Buffer Frames
=====================

You can make certain chosen buffers, for which Emacs normally creates a second window when you have just one window, appear in special frames of their own.  To do this, set the variable special-display-buffer-names to a list of buffer names; any buffer whose name is in that list automatically gets a special frame, when an Emacs command wants to display it ``in another window.''

For example, if you set the variable this way,

     (setq special-display-buffer-names
           '("*Completions*" "*grep*" "*tex-shell*"))

then completion lists, grep output and the TeX mode shell buffer get individual frames of their own.  These frames, and the windows in them, are never automatically split or reused for any other buffers.  They continue to show the buffers they were created for, unless you alter them by hand.  Killing the special buffer deletes its frame automatically.

More generally, you can set special-display-regexps to a list of regular expressions; then a buffer gets its own frame if its name matches any of those regular expressions.  (Once again, this applies only to buffers that normally get displayed for you in a separate window.)

The variable special-display-frame-alist specifies the frame parameters for these frames.  It has a default value, so you don't need to set it.

For those who know Lisp, an element of special-display-buffer-names or special-display-regexps can also be a list.  Then the first element is the buffer name or regular expression; the rest of the list specifies how to create the frame.  It can be an association list specifying frame parameter values; these values take precedence over parameter values specified in special-display-frame-alist.  Alternatively, it can have this form:

     (FUNCTION ARGS...)

where FUNCTION is a symbol.  Then the frame is constructed by calling FUNCTION; its first argument is the buffer, and its remaining arguments are ARGS.

An analogous feature lets you specify buffers which should be displayed in the selected window.  See Force Same Window.  The same-window feature takes precedence over the special-frame feature; therefore, if you add a buffer name to special-display-buffer-names and it has no effect, check to see whether that feature is also in use for the same buffer name.

Setting Frame Parameters
========================

This section describes commands for altering the display style and window management behavior of the selected frame.

M-x set-foreground-color RET COLOR RET
     Specify color COLOR for the foreground of the selected frame. (This also changes the foreground color of the default face.)

M-x set-background-color RET COLOR RET
     Specify color COLOR for the background of the selected frame. (This also changes the background color of the default face.)

M-x set-cursor-color RET COLOR RET
     Specify color COLOR for the cursor of the selected frame.

M-x set-mouse-color RET COLOR RET
     Specify color COLOR for the mouse cursor when it is over the selected frame.

M-x set-border-color RET COLOR RET
     Specify color COLOR for the border of the selected frame.

M-x list-colors-display
     Display the defined color names and show what the colors look like. This command is somewhat slow.

M-x auto-raise-mode
     Toggle whether or not the selected frame should auto-raise.  Auto-raise means that every time you move the mouse onto the frame, it raises the frame.

     Note that this auto-raise feature is implemented by Emacs itself.  Some window managers also implement auto-raise.  If you enable auto-raise for Emacs frames in your X window manager, it should work, but it is beyond Emacs's control and therefore auto-raise-mode has no effect on it.

M-x auto-lower-mode
     Toggle whether or not the selected frame should auto-lower. Auto-lower means that every time you move the mouse off the frame, the frame moves to the bottom of the stack of X windows.

     The command auto-lower-mode has no effect on auto-lower implemented by the X window manager.  To control that, you must use the appropriate window manager features.

M-x set-frame-font RET FONT RET
     Specify font FONT as the principal font for the selected frame. The principal font controls several face attributes of the default face (see Faces).  For example, if the principal font has a height of 12 pt, all text will be drawn in 12 pt fonts, unless you use another face that specifies a different height.  See Font X, for ways to list the available fonts on your system.

     You can also set a frame's principal font through a pop-up menu. Press S-Mouse-1 to activate this menu.

In Emacs versions that use an X toolkit, the color-setting and font-setting functions don't affect menus and the menu bar, since they are displayed by their own widget classes.  To change the appearance of the menus and menu bar, you must use X resources (see Resources X). See Colors X, regarding colors.  See Font X, regarding choice of font.

For information on frame parameters and customization, see Frame Parameters (The Emacs Lisp Reference Manual).

Scroll Bars
===========

When using X, Emacs normally makes a scroll bar at the left of each Emacs window. (Placing it at the left is usually more useful with overlapping frames with text starting at the left margin.) The scroll bar runs the height of the window, and shows a moving rectangular inner box which represents the portion of the buffer currently displayed.  The entire height of the scroll bar represents the entire length of the buffer.

You can use Mouse-2 (normally, the middle button) in the scroll bar to move or drag the inner box up and down.  If you move it to the top of the scroll bar, you see the top of the buffer.  If you move it to the bottom of the scroll bar, you see the bottom of the buffer.

The left and right buttons in the scroll bar scroll by controlled increments.  Mouse-1 (normally, the left button) moves the line at the level where you click up to the top of the window.  Mouse-3 (normally, the right button) moves the line at the top of the window down to the level where you click.  By clicking repeatedly in the same place, you can scroll by the same distance over and over.

Aside from scrolling, you can also click C-Mouse-2 in the scroll bar to split a window vertically.  The split occurs on the line where you click.

You can enable or disable Scroll Bar mode with the command M-x scroll-bar-mode.  With no argument, it toggles the use of scroll bars. With an argument, it turns use of scroll bars on if and only if the argument is positive.  This command applies to all frames, including frames yet to be created.  Customize the option scroll-bar-mode to control the use of scroll bars at startup.  You can use it to specify that they are placed at the right of windows if you prefer that.  You can use the X resource verticalScrollBars to control the initial setting of Scroll Bar mode similarly.  See Resources X.

To enable or disable scroll bars for just the selected frame, use the M-x toggle-scroll-bar command.

Scrolling With `Wheeled' Mice
=============================

Some mice have a `wheel' instead of a third button.  You can usually click the wheel to act as mouse-3.  You can also use the wheel to scroll windows instead of using the scroll bar or keyboard commands. Use M-x mwheel-install to set up the wheel for scrolling or put (require 'mwheel) in your .emacs.  (Support for the wheel depends on the system generating appropriate events for Emacs.)

The variables mwheel-follow-mouse and mwheel-scroll-amount determine where and by how much buffers are scrolled.

Menu Bars
=========

You can turn display of menu bars on or off with M-x menu-bar-mode.  With no argument, this command toggles Menu Bar mode, a minor mode.  With an argument, the command turns Menu Bar mode on if the argument is positive, off if the argument is not positive.  You can use the X resource menuBarLines to control the initial setting of Menu Bar mode.  See Resources X.  Expert users often turn off the menu bar, especially on text-only terminals, where this makes one additional line available for text.

See Menu Bar, for information on how to invoke commands with the menu bar.

Using Dialog Boxes
==================

Certain operations invoked from menus will use a window system dialog box to get information via the mouse if such dialog boxes are supported. This includes yes/no questions and file selection under Motif/LessTif and MS Windows.  Customize the option use-dialog-box to suppress the use of dialog boxes.

Using Multiple Typefaces
========================

When using Emacs with X, you can set up multiple styles of displaying characters.  The aspects of style that you can control are the type font, the foreground color, the background color, and whether to underline.  Emacs on MS-DOS supports faces partially by letting you control the foreground and background colors of each face (see MS-DOS).  On non-windowed terminals faces are supported to the extent the terminal can display them.

The way you control display style is by defining named faces. Each face can specify a type font, a foreground color, a background color, and an underline flag; but it does not have to specify all of them.  Then by specifying the face or faces to use for a given part of the text in the buffer, you control how that text appears.

The style of display used for a given character in the text is determined by combining several faces.  Any aspect of the display style that isn't specified by overlays or text properties comes from the frame itself.

Enriched mode, the mode for editing formatted text, includes several commands and menus for specifying faces.  See Format Faces, for how to specify the font for text in the buffer.  See Format Colors, for how to specify the foreground and background color.

To alter the appearance of a face, use the customization buffer. See Face Customization.  You can also use X resources to specify attributes of particular faces (see Resources X).

To see what faces are currently defined, and what they look like, type M-x list-faces-display.  It's possible for a given face to look different in different frames; this command shows the appearance in the frame in which you type it.  Here's a list of the standardly defined faces:

default
     This face is used for ordinary text that doesn't specify any other face.

modeline
     This face is used for mode lines.  By default, it's drawn with shadows for a `raised' effect under X and set up as the inverse of the default face on non-windowed terminals.  See Display Vars.

header-line
     Similar to modeline for a window's header line.

highlight
     This face is used for highlighting portions of text, in various modes.

region
     This face is used for displaying a selected region (when Transient Mark mode is enabled---see below).

secondary-selection
     This face is used for displaying a secondary selection (see Secondary Selection).

bold
     This face uses a bold variant of the default font, if it has one.

italic
     This face uses an italic variant of the default font, if it has one.

bold-italic
     This face uses a bold italic variant of the default font, if it has one.

underline
     This face underlines text.

fixed-pitch
     The basic fixed-pitch face.

fringe
     The face for the fringes to the left and right of windows under X.

scroll-bar
     This face determines the colors of the scroll bar.

border
     This face determines the color of the frame border.

cursor
     This face determines the color of the cursor.

mouse
     This face determines the color of the mouse pointer.

tool-bar
     The basic tool-bar face.

menu
     This face determines the colors and font of Emacs's menus.  Setting the font of LessTif/Motif menus is currently not supported; attempts to set the font are ignored in this case.

trailing-whitespace
     The face for highlighting trailing whitespace when show-trailing-whitespace is non-nil.

variable-pitch
     The basic variable-pitch face.

When Transient Mark mode is enabled, the text of the region is highlighted when the mark is active.  This uses the face named region; you can control the style of highlighting by changing the style of this face (see Face Customization).  See Transient Mark, for more information about Transient Mark mode and activation and deactivation of the mark.

One easy way to use faces is to turn on Font Lock mode.  This minor mode, which is always local to a particular buffer, arranges to choose faces according to the syntax of the text you are editing.  It can recognize comments and strings in most languages; in several languages, it can also recognize and properly highlight various other important constructs.  See Font Lock, for more information about Font Lock mode and syntactic highlighting.

You can print out the buffer with the highlighting that appears on your screen using the command ps-print-buffer-with-faces. See PostScript.

Font Lock mode
==============

Font Lock mode is a minor mode, always local to a particular buffer, which highlights (or ``fontifies'') using various faces according to the syntax of the text you are editing.  It can recognize comments and strings in most languages; in several languages, it can also recognize and properly highlight various other important constructs---for example, names of functions being defined or reserved keywords.

The command M-x font-lock-mode turns Font Lock mode on or off according to the argument, and toggles the mode when it has no argument. The function turn-on-font-lock unconditionally enables Font Lock mode.  This is useful in mode-hook functions.  For example, to enable Font Lock mode whenever you edit a C file, you can do this:

     (add-hook 'c-mode-hook 'turn-on-font-lock)

To turn on Font Lock mode automatically in all modes which support it, customize the user option global-font-lock-mode or use the function global-font-lock-mode, like this:

     (global-font-lock-mode 1)

In Font Lock mode, when you edit the text, the highlighting updates automatically in the line that you changed.  Most changes don't affect the highlighting of subsequent lines, but occasionally they do.  To rehighlight a range of lines, use the command M-g M-g (font-lock-fontify-block).

In certain major modes, M-g M-g refontifies the entire current function.  (The variable font-lock-mark-block-function controls how to find the current function.)  In other major modes, M-g M-g refontifies 16 lines above and below point.

With a prefix argument N, M-g M-g refontifies N lines above and below point, regardless of the mode.

To get the full benefit of Font Lock mode, you need to choose a default font which has bold, italic, and bold-italic variants; or else you need to have a color or gray-scale screen.

The variable font-lock-maximum-decoration specifies the preferred level of fontification, for modes that provide multiple levels.  Level 1 is the least amount of fontification; some modes support levels as high as 3.  The normal default is ``as high as possible.''  You can specify an integer, which applies to all modes, or you can specify different numbers for particular major modes; for example, to use level 1 for C/C++ modes, and the default level otherwise, use this:

     (setq font-lock-maximum-decoration
           '((c-mode . 1) (c++-mode . 1)))

Fontification can be too slow for large buffers, so you can suppress it.  The variable font-lock-maximum-size specifies a buffer size, beyond which buffer fontification is suppressed.

Comment and string fontification (or ``syntactic'' fontification) relies on analysis of the syntactic structure of the buffer text.  For the purposes of speed, some modes including C mode and Lisp mode rely on a special convention: an open-parenthesis in the leftmost column always defines the beginning of a defun, and is thus always outside any string or comment.  (see Defuns.)  If you don't follow this convention, then Font Lock mode can misfontify the text after an open-parenthesis in the leftmost column that is inside a string or comment.

The variable font-lock-beginning-of-syntax-function (always buffer-local) specifies how Font Lock mode can find a position guaranteed to be outside any comment or string.  In modes which use the leftmost column parenthesis convention, the default value of the variable is beginning-of-defun---that tells Font Lock mode to use the convention.  If you set this variable to nil, Font Lock no longer relies on the convention.  This avoids incorrect results, but the price is that, in some cases, fontification for a changed text must rescan buffer text from the beginning of the buffer.

Font Lock highlighting patterns already exist for many modes, but you may want to fontify additional patterns.  You can use the function font-lock-add-keywords, to add your own highlighting patterns for a particular mode.  For example, to highlight FIXME: words in C comments, use this:

     (font-lock-add-keywords
      'c-mode
      '(("\\<\\(FIXME\\):" 1 font-lock-warning-face t)))

Font Lock Support Modes
=======================

Font Lock support modes make Font Lock mode faster for large buffers. There are two support modes: Fast Lock mode and Lazy Lock mode.  They use two different methods of speeding up Font Lock mode.

Fast Lock Mode
--------------

To make Font Lock mode faster for buffers visiting large files, you can use Fast Lock mode.  Fast Lock mode saves the font information for each file in a separate cache file; each time you visit the file, it rereads the font information from the cache file instead of refontifying the text from scratch.

The command M-x fast-lock-mode turns Fast Lock mode on or off, according to the argument (with no argument, it toggles).  You can also arrange to enable Fast Lock mode whenever you use Font Lock mode, like this:

     (setq font-lock-support-mode 'fast-lock-mode)

It is not worth writing a cache file for small buffers.  Therefore, the variable fast-lock-minimum-size specifies a minimum file size for caching font information.

The variable fast-lock-cache-directories specifies where to put the cache files.  Its value is a list of directories to try; "." means the same directory as the file being edited.  The default value is ("." "~/.emacs-flc"), which means to use the same directory if possible, and otherwise the directory ~/.emacs-flc.

The variable fast-lock-save-others specifies whether Fast Lock mode should save cache files for files that you do not own.  A non-nil value means yes (and that is the default).

Lazy Lock Mode
--------------

To make Font Lock mode faster for large buffers, you can use Lazy Lock mode to reduce the amount of text that is fontified.  In Lazy Lock mode, buffer fontification is demand-driven; it happens to portions of the buffer that are about to be displayed.  And fontification of your changes is deferred; it happens only when Emacs has been idle for a certain short period of time.

The command M-x lazy-lock-mode turns Lazy Lock mode on or off, according to the argument (with no argument, it toggles).  You can also arrange to enable Lazy Lock mode whenever you use Font Lock mode, like this:

     (setq font-lock-support-mode 'lazy-lock-mode)

It is not worth avoiding buffer fontification for small buffers. Therefore, the variable lazy-lock-minimum-size specifies a minimum buffer size for demand-driven buffer fontification.  Buffers smaller than that are fontified all at once, as in plain Font Lock mode.

When you alter the buffer, Lazy Lock mode defers fontification of the text you changed.  The variable lazy-lock-defer-time specifies how many seconds Emacs must be idle before it starts fontifying your changes.  If the value is 0, then changes are fontified immediately, as in plain Font Lock mode.

Lazy Lock mode normally fontifies newly visible portions of the buffer before they are first displayed.  However, if the value of lazy-lock-defer-on-scrolling is non-nil, newly visible text is fontified only when Emacs is idle for lazy-lock-defer-time seconds.

In some modes, including C mode and Emacs Lisp mode, changes in one line's contents can alter the context for subsequent lines, and thus change how they ought to be fontified.  Ordinarily, you must type M-g M-g to refontify the subsequent lines.  However, if you set the variable lazy-lock-defer-contextually to non-nil, Lazy Lock mode does this automatically, after lazy-lock-defer-time seconds.

When Emacs is idle for a long time, Lazy Lock fontifies additional portions of the buffer, not yet displayed, in case you will display them later.  This is called stealth fontification.

The variable lazy-lock-stealth-time specifies how many seconds Emacs has to be idle before stealth fontification starts.  A value of nil means no stealth fontification.  The variables lazy-lock-stealth-lines and lazy-lock-stealth-verbose specify the granularity and verbosity of stealth fontification.

JIT Lock Mode
-------------

This Just-In-time support mode is roughly equivalent to Lazy Lock but is generally faster and more robust.  It supports stealth and deferred contextual fontification.

Font-lock uses jit-lock-mode as default support mode, so you don't have to do anything to activate it.

Fast Lock or Lazy Lock?
-----------------------

Here is a simple guide to help you choose one of the Font Lock support modes.

  * Fast Lock mode intervenes only during file visiting and buffer killing (and related events); therefore buffer editing and window scrolling are no faster or slower than in plain Font Lock mode.

  * Fast Lock mode is slower at reading a cache file than Lazy Lock mode is at fontifying a window; therefore Fast Lock mode is slower at visiting a file than Lazy Lock mode.

  * Lazy Lock mode intervenes during window scrolling to fontify text that scrolls onto the screen; therefore, scrolling is slower than in plain Font Lock mode.

  * Lazy Lock mode doesn't fontify during buffer editing (it defers fontification of changes); therefore, editing is faster than in plain Font Lock mode.

  * Fast Lock mode can be fooled by a file that is kept under version control software; therefore buffer fontification may occur even when a cache file exists for the file.

  * Fast Lock mode only works with a buffer visiting a file; Lazy Lock mode works with any buffer.

  * Fast Lock mode generates cache files; Lazy Lock mode does not.

The variable font-lock-support-mode specifies which of these support modes to use; for example, to specify that Fast Lock mode is used for C/C++ modes, and Lazy Lock mode otherwise, set the variable like this:

     (setq font-lock-support-mode
           '((c-mode . fast-lock-mode) (c++-mode . fast-lock-mode)
             (t . lazy-lock-mode)))

Highlight Changes Mode
======================

Use M-x highlight-changes-mode to enable a minor mode that uses faces (colors, typically) to indicate which parts of the buffer were changed most recently.

Interactive Highlighting of Arbitrary Text
==========================================

Sometimes, you could need to highlight arbitrary strings in the buffer.  For example, you might wish to see all the references to a certain variable in a program source file or highlight certain parts in a voluminous output of some program, or make certain cliches stand out.

Use the M-x hi-lock-mode command to turn on a minor mode that allows you to specify regular expressions of the text to be highlighted. hi-lock-mode works like Font Lock (see Font Lock), except that it lets you control what parts of text are highlighted. hi-lock-mode provides several functions:

C-x w h
     Specify the regular expression (see Regexps) for the parts of buffer text that are to be highlighted (highlight-regexp).  It prompts for the regular expression, then for the name of the face with which to highlight the text that matches.

C-x w r
     Unhighlight REGEXP (unhighlight-regexp).  Prompts for the regular expression, and will accept only one of the regexps inserted by other hi-lock commands.

C-x w l
     Specify the regular expression (see Regexps) for the lines of buffer text that are to be highlighted (highlight-lines-matching-regexp).  It prompts for the regular expression, then for the name of the face with which to highlight the matching lines.

C-x w b
     This runs the hi-lock-write-interactive-patterns command which inserts the patterns added by M-x highlight-regexp and M-x highlight-lines-matching-regexp into the current buffer at point, as comments.  These patterns will be read the next time the file is visited, or when the M-x hi-lock-find-patterns command is issued.

C-x w i
     Re-read patterns stored in a buffer in the format produced by M-x hi-lock-write-interactive-patterns (hi-lock-write-interactive-patterns.  If you invoke this command in a buffer whose major mode is a member of the list that is the value of the variable hi-lock-exclude-modes, this command has no effect.

Trailing Whitespace
===================

The option show-trailing-whitespace can be customized so that Emacs displays trailing whitespace in the face trailing-whitespace.  Trailing whitespace is defined as spaces or tabs at the end of a line.  To avoid busy highlighting when entering new text, trailing whitespace is not displayed if point is at the end of the line containing the whitespace.

Tooltips (or `Balloon Help')
============================

Tooltips are small X windows displaying a help string at the current mouse position, typically over text---including the mode line---which can be activated with the mouse or other keys.  (This facility is sometimes known as `balloon help'.)  Tooltips may be available for menu items too.

To use tooltips, customize the user option tooltip-mode.  The customization group tooltip controls various aspects of their display.  If Tooltip mode is not activated, the help text is displayed in the echo area instead.

Mouse Avoidance
===============

Mouse Avoidance mode keeps the window system mouse pointer away from point to avoid obscuring text.  Whenever the mouse is moved, the frame is also raised.  To use it, customize the option mouse-avoidance-mode.  You can set this to various values to move the mouse in several ways:

banish
     Move the mouse to the upper-right corner on any keypress;

exile
     Move the mouse to the corner only if the cursor gets too close, and allow it to return once the cursor is out of the way;

jump
     If the cursor gets too close to the mouse, displace the mouse a random distance & direction;

animate
     As jump, but shows steps along the way for illusion of motion;

cat-and-mouse
     The same as animate;

proteus
     As animate, but changes the shape of the mouse pointer too.

You can also use the command M-x mouse-avoidance-mode to turn on the mode.

Miscellaneous X Window Features
===============================

The following commands let you create, delete and operate on frames:

C-z
     Iconify the selected Emacs frame (iconify-or-deiconify-frame). The normal meaning of C-z, to suspend Emacs, is not useful under a window system, so it has a different binding in that case.

     If you type this command on an Emacs frame's icon, it deiconifies the frame.

C-x 5 0
     Delete the selected frame (delete-frame).  This is not allowed if there is only one frame.

C-x 5 o
     Select another frame, raise it, and warp the mouse to it so that it stays selected.  If you repeat this command, it cycles through all the frames on your terminal.

C-x 5 1
     Delete all frames except the selected one.

Non-Window Terminals
====================

If your terminal does not have a window system that Emacs supports, then it can display only one Emacs frame at a time.  However, you can still create multiple Emacs frames, and switch between them.  Switching frames on these terminals is much like switching between different window configurations.

Use C-x 5 2 to create a new frame and switch to it; use C-x 5 o to cycle through the existing frames; use C-x 5 0 to delete the current frame.

Each frame has a number to distinguish it.  If your terminal can display only one frame at a time, the selected frame's number N appears near the beginning of the mode line, in the form FN.

FN is actually the frame's name.  You can also specify a different name if you wish, and you can select a frame by its name.  Use the command M-x set-frame-name RET NAME RET to specify a new name for the selected frame, and use M-x select-frame-by-name RET NAME RET to select a frame according to its name.  The name you specify appears in the mode line when the frame is selected.

Using a Mouse in Terminal Emulators
===================================

Some terminal emulators under X support mouse clicks in the terminal window.  In a terminal emulator which is compatible with xterm, you can use M-x xterm-mouse-mode to enable simple use of the mouse---only single clicks are supported.  The normal xterm mouse functionality is still available by holding down the SHIFT key when you press the mouse button.