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