Complete rewrite based on xfaces.c
[bpt/emacs.git] / man / mark.texi
CommitLineData
6bf7aab6
DL
1@c This is part of the Emacs manual.
2@c Copyright (C) 1985, 86, 87, 93, 94, 95, 1997 Free Software Foundation, Inc.
3@c See file emacs.texi for copying conditions.
4@node Mark, Killing, Help, Top
5@chapter The Mark and the Region
6@cindex mark
7@cindex setting a mark
8@cindex region
9
10 Many Emacs commands operate on an arbitrary contiguous part of the
11current buffer. To specify the text for such a command to operate on,
12you set @dfn{the mark} at one end of it, and move point to the other
13end. The text between point and the mark is called @dfn{the region}.
14Emacs highlights the region whenever there is one, if you enable
15Transient Mark mode (@pxref{Transient Mark}).
16
17 You can move point or the mark to adjust the boundaries of the region.
18It doesn't matter which one is set first chronologically, or which one
19comes earlier in the text. Once the mark has been set, it remains where
20you put it until you set it again at another place. Each Emacs buffer
21has its own mark, so that when you return to a buffer that had been
22selected previously, it has the same mark it had before.
23
24 Many commands that insert text, such as @kbd{C-y} (@code{yank}) and
25@kbd{M-x insert-buffer}, position point and the mark at opposite ends of
26the inserted text, so that the region contains the text just inserted.
27
28 Aside from delimiting the region, the mark is also useful for
29remembering a spot that you may want to go back to. To make this
30feature more useful, each buffer remembers 16 previous locations of the
31mark in the @dfn{mark ring}.
32
33@menu
34* Setting Mark:: Commands to set the mark.
35* Transient Mark:: How to make Emacs highlight the region--
36 when there is one.
37* Using Region:: Summary of ways to operate on contents of the region.
38* Marking Objects:: Commands to put region around textual units.
39* Mark Ring:: Previous mark positions saved so you can go back there.
40* Global Mark Ring:: Previous mark positions in various buffers.
41@end menu
42
43@node Setting Mark
44@section Setting the Mark
45
46 Here are some commands for setting the mark:
47
48@c WideCommands
49@table @kbd
50@item C-@key{SPC}
51Set the mark where point is (@code{set-mark-command}).
52@item C-@@
53The same.
54@item C-x C-x
55Interchange mark and point (@code{exchange-point-and-mark}).
56@item Drag-Mouse-1
57Set point and the mark around the text you drag across.
58@item Mouse-3
59Set the mark where point is, then move point to where you click
60(@code{mouse-save-then-kill}).
61@end table
62
63 For example, suppose you wish to convert part of the buffer to
64upper case, using the @kbd{C-x C-u} (@code{upcase-region}) command,
65which operates on the text in the region. You can first go to the
66beginning of the text to be capitalized, type @kbd{C-@key{SPC}} to put
67the mark there, move to the end, and then type @kbd{C-x C-u}. Or, you
68can set the mark at the end of the text, move to the beginning, and then
69type @kbd{C-x C-u}.
70
71@kindex C-SPC
72@findex set-mark-command
73 The most common way to set the mark is with the @kbd{C-@key{SPC}} command
74(@code{set-mark-command}). This sets the mark where point is. Then you
75can move point away, leaving the mark behind.
76
77 There are two ways to set the mark with the mouse. You can drag mouse
78button one across a range of text; that puts point where you release the
79mouse button, and sets the mark at the other end of that range. Or you
80can click mouse button three, which sets the mark at point (like
81@kbd{C-@key{SPC}}) and then moves point (like @kbd{Mouse-1}). Both of
82these methods copy the region into the kill ring in addition to setting
83the mark; that gives behavior consistent with other window-driven
84applications, but if you don't want to modify the kill ring, you must
85use keyboard commands to set the mark. @xref{Mouse Commands}.
86
87@kindex C-x C-x
88@findex exchange-point-and-mark
89 Ordinary terminals have only one cursor, so there is no way for Emacs
90to show you where the mark is located. You have to remember. The usual
91solution to this problem is to set the mark and then use it soon, before
92you forget where it is. Alternatively, you can see where the mark is
93with the command @kbd{C-x C-x} (@code{exchange-point-and-mark}) which
94puts the mark where point was and point where the mark was. The extent
95of the region is unchanged, but the cursor and point are now at the
96previous position of the mark. In Transient Mark mode, this command
97reactivates the mark.
98
99 @kbd{C-x C-x} is also useful when you are satisfied with the position
100of point but want to move the other end of the region (where the mark
101is); do @kbd{C-x C-x} to put point at that end of the region, and then
102move it. A second use of @kbd{C-x C-x}, if necessary, puts the mark at
103the new position with point back at its original position.
104
105@kindex C-@@
106 There is no such character as @kbd{C-@key{SPC}} in ASCII; when you
107type @key{SPC} while holding down @key{CTRL}, what you get on most
108ordinary terminals is the character @kbd{C-@@}. This key is actually
109bound to @code{set-mark-command}. But unless you are unlucky enough to
110have a terminal where typing @kbd{C-@key{SPC}} does not produce
111@kbd{C-@@}, you might as well think of this character as
112@kbd{C-@key{SPC}}. Under X, @kbd{C-@key{SPC}} is actually a distinct
113character, but its binding is still @code{set-mark-command}.
114
115@node Transient Mark
116@section Transient Mark Mode
117@cindex mode, Transient Mark
118@cindex Transient Mark mode
119@cindex highlighting region
120@cindex region highlighting
121
41812703
DL
122 Emacs can highlight the current region on a terminal which supports
123colors. But normally it does not. Why not?
6bf7aab6
DL
124
125 Highlighting the region doesn't work well ordinarily in Emacs, because
126once you have set a mark, there is @emph{always} a region (in that
127buffer). And highlighting the region all the time would be a nuisance.
128
129 You can turn on region highlighting by enabling Transient Mark mode.
130This is a more rigid mode of operation in which the region ``lasts''
131only temporarily, so you must set up a region for each command that uses
132one. In Transient Mark mode, most of the time there is no region;
133therefore, highlighting the region when it exists is convenient.
134
135@findex transient-mark-mode
136 To enable Transient Mark mode, type @kbd{M-x transient-mark-mode}.
137This command toggles the mode, so you can repeat the command to turn off
138the mode.
139
140 Here are the details of Transient Mark mode:
141
142@itemize @bullet
143@item
144To set the mark, type @kbd{C-@key{SPC}} (@code{set-mark-command}).
145This makes the mark active; as you move point, you will see the region
146highlighting grow and shrink.
147
148@item
149The mouse commands for specifying the mark also make it active. So do
150keyboard commands whose purpose is to specify a region, including
151@kbd{M-@@}, @kbd{C-M-@@}, @kbd{M-h}, @kbd{C-M-h}, @kbd{C-x C-p}, and
152@kbd{C-x h}.
153
154@item
155When the mark is active, you can execute commands that operate on the
156region, such as killing, indenting, or writing to a file.
157
158@item
159Any change to the buffer, such as inserting or deleting a character,
160deactivates the mark. This means any subsequent command that operates
161on a region will get an error and refuse to operate. You can make the
162region active again by typing @kbd{C-x C-x}.
163
164@item
165Commands like @kbd{M->} and @kbd{C-s} that ``leave the mark behind'' in
166addition to some other primary purpose do not activate the new mark.
167You can activate the new region by executing @kbd{C-x C-x}
168(@code{exchange-point-and-mark}).
169
170@item
171@kbd{C-s} when the mark is active does not alter the mark.
172
173@item
174Quitting with @kbd{C-g} deactivates the mark.
175@end itemize
176
177 Highlighting of the region uses the @code{region} face; you can
178customize how the region is highlighted by changing this face.
179@xref{Face Customization}.
180
181@vindex highlight-nonselected-windows
182 When multiple windows show the same buffer, they can have different
183regions, because they can have different values of point (though they
184all share one common mark position). Ordinarily, only the selected
185window highlights its region (@pxref{Windows}). However, if the
186variable @code{highlight-nonselected-windows} is non-@code{nil}, then
187each window highlights its own region (provided that Transient Mark mode
188is enabled and the window's buffer's mark is active).
189
190 When Transient Mark mode is not enabled, every command that sets the
191mark also activates it, and nothing ever deactivates it.
192
193@vindex mark-even-if-inactive
194 If the variable @code{mark-even-if-inactive} is non-@code{nil} in
195Transient Mark mode, then commands can use the mark and the region
196even when it is inactive. Region highlighting appears and disappears
197just as it normally does in Transient Mark mode, but the mark doesn't
198really go away when the highlighting disappears.
199
200@cindex Zmacs mode
201 Transient Mark mode is also sometimes known as ``Zmacs mode''
202because the Zmacs editor on the MIT Lisp Machine handled the mark in a
203similar way.
204
205@node Using Region
206@section Operating on the Region
207
208@cindex operations on a marked region
209 Once you have a region and the mark is active, here are some of the
210ways you can operate on the region:
211
212@itemize @bullet
213@item
214Kill it with @kbd{C-w} (@pxref{Killing}).
215@item
216Save it in a register with @kbd{C-x r s} (@pxref{Registers}).
217@item
218Save it in a buffer or a file (@pxref{Accumulating Text}).
219@item
220Convert case with @kbd{C-x C-l} or @kbd{C-x C-u} (@pxref{Case}).
221@item
222Indent it with @kbd{C-x @key{TAB}} or @kbd{C-M-\} (@pxref{Indentation}).
223@item
224Fill it as text with @kbd{M-x fill-region} (@pxref{Filling}).
225@item
226Print hardcopy with @kbd{M-x print-region} (@pxref{Hardcopy}).
227@item
228Evaluate it as Lisp code with @kbd{M-x eval-region} (@pxref{Lisp Eval}).
229@end itemize
230
231 Most commands that operate on the text in the
232region have the word @code{region} in their names.
233
234@node Marking Objects
235@section Commands to Mark Textual Objects
236
237@cindex marking sections of text
238 Here are the commands for placing point and the mark around a textual
239object such as a word, list, paragraph or page.
240
241@table @kbd
242@item M-@@
243Set mark after end of next word (@code{mark-word}). This command and
244the following one do not move point.
245@item C-M-@@
246Set mark after end of next Lisp expression (@code{mark-sexp}).
247@item M-h
248Put region around current paragraph (@code{mark-paragraph}).
249@item C-M-h
250Put region around current Lisp defun (@code{mark-defun}).
251@item C-x h
252Put region around entire buffer (@code{mark-whole-buffer}).
253@item C-x C-p
254Put region around current page (@code{mark-page}).
255@end table
256
257@kbd{M-@@} (@code{mark-word}) puts the mark at the end of the next word,
258while @kbd{C-M-@@} (@code{mark-sexp}) puts it at the end of the next Lisp
259expression. These commands handle arguments just like @kbd{M-f} and
260@kbd{C-M-f}.
261
262@kindex C-x h
263@findex mark-whole-buffer
264 Other commands set both point and mark, to delimit an object in the
265buffer. For example, @kbd{M-h} (@code{mark-paragraph}) moves point to
266the beginning of the paragraph that surrounds or follows point, and puts
267the mark at the end of that paragraph (@pxref{Paragraphs}). It prepares
268the region so you can indent, case-convert, or kill a whole paragraph.
269
270 @kbd{C-M-h} (@code{mark-defun}) similarly puts point before and the
271mark after the current or following defun (@pxref{Defuns}). @kbd{C-x
272C-p} (@code{mark-page}) puts point before the current page, and mark at
273the end (@pxref{Pages}). The mark goes after the terminating page
274delimiter (to include it), while point goes after the preceding page
275delimiter (to exclude it). A numeric argument specifies a later page
276(if positive) or an earlier page (if negative) instead of the current
277page.
278
279 Finally, @kbd{C-x h} (@code{mark-whole-buffer}) sets up the entire
280buffer as the region, by putting point at the beginning and the mark at
281the end.
282
283 In Transient Mark mode, all of these commands activate the mark.
284
285@node Mark Ring
286@section The Mark Ring
287
288@kindex C-u C-SPC
289@cindex mark ring
290@kindex C-u C-@@
291 Aside from delimiting the region, the mark is also useful for
292remembering a spot that you may want to go back to. To make this
293feature more useful, each buffer remembers 16 previous locations of the
294mark, in the @dfn{mark ring}. Commands that set the mark also push the
295old mark onto this ring. To return to a marked location, use @kbd{C-u
296C-@key{SPC}} (or @kbd{C-u C-@@}); this is the command
297@code{set-mark-command} given a numeric argument. It moves point to
298where the mark was, and restores the mark from the ring of former
299marks. Thus, repeated use of this command moves point to all of the old
300marks on the ring, one by one. The mark positions you move through in
301this way are not lost; they go to the end of the ring.
302
303 Each buffer has its own mark ring. All editing commands use the current
304buffer's mark ring. In particular, @kbd{C-u C-@key{SPC}} always stays in
305the same buffer.
306
307 Many commands that can move long distances, such as @kbd{M-<}
308(@code{beginning-of-buffer}), start by setting the mark and saving the
309old mark on the mark ring. This is to make it easier for you to move
310back later. Searches set the mark if they move point. You can tell
311when a command sets the mark because it displays @samp{Mark Set} in the
312echo area.
313
314 If you want to move back to the same place over and over, the mark
315ring may not be convenient enough. If so, you can record the position
316in a register for later retrieval (@pxref{RegPos}).
317
318@vindex mark-ring-max
319 The variable @code{mark-ring-max} specifies the maximum number of
320entries to keep in the mark ring. If that many entries exist and
321another one is pushed, the last one in the list is discarded. Repeating
322@kbd{C-u C-@key{SPC}} cycles through the positions currently in the
323ring.
324
325@vindex mark-ring
326 The variable @code{mark-ring} holds the mark ring itself, as a list of
327marker objects, with the most recent first. This variable is local in
328every buffer.
329
330@node Global Mark Ring
331@section The Global Mark Ring
332@cindex global mark ring
333
334 In addition to the ordinary mark ring that belongs to each buffer,
335Emacs has a single @dfn{global mark ring}. It records a sequence of
336buffers in which you have recently set the mark, so you can go back
337to those buffers.
338
339 Setting the mark always makes an entry on the current buffer's mark
340ring. If you have switched buffers since the previous mark setting, the
341new mark position makes an entry on the global mark ring also. The
342result is that the global mark ring records a sequence of buffers that
343you have been in, and, for each buffer, a place where you set the mark.
344
345@kindex C-x C-@key{SPC}
346@findex pop-global-mark
347 The command @kbd{C-x C-@key{SPC}} (@code{pop-global-mark}) jumps to
348the buffer and position of the latest entry in the global ring. It also
349rotates the ring, so that successive uses of @kbd{C-x C-@key{SPC}} take
350you to earlier and earlier buffers.
351