Use xfree, not free, to avoid crash with --enable-checking=xmallocoverrun.
[bpt/emacs.git] / doc / emacs / mark.texi
CommitLineData
6bf7aab6 1@c This is part of the Emacs manual.
73b0cd50 2@c Copyright (C) 1985-1987, 1993-1995, 1997, 2001-2011
8838673e 3@c Free Software Foundation, Inc.
6bf7aab6
DL
4@c See file emacs.texi for copying conditions.
5@node Mark, Killing, Help, Top
6@chapter The Mark and the Region
7@cindex mark
8@cindex setting a mark
9@cindex region
10
11 Many Emacs commands operate on an arbitrary contiguous part of the
12current buffer. To specify the text for such a command to operate on,
13you set @dfn{the mark} at one end of it, and move point to the other
14end. The text between point and the mark is called @dfn{the region}.
0fcca8ee
CY
15The region always extends between point and the mark, no matter which
16one comes earlier in the text; each time you move point, the region
17changes.
18
623cec4c
CY
19 Setting the mark at a position in the text also @dfn{activates} it.
20When the mark is active, Emacs indicates the extent of the region by
21highlighting the text within it, using the @code{region} face
22(@pxref{Face Customization}). After certain non-motion commands,
0fcca8ee
CY
23including any command that changes the text in the buffer, Emacs
24automatically @dfn{deactivates} the mark; this turns off the
25highlighting. You can also explicitly deactivate the mark at any
26time, by typing @kbd{C-g} (@pxref{Quitting}).
a8a31aae 27
e1a3f5b1
CY
28 This default behavior is known as Transient Mark mode. Disabling
29Transient Mark mode switches Emacs to an alternative behavior, in
30which the region is usually not highlighted. @xref{Disabled Transient Mark}.
702b10e1 31
a8a31aae 32@vindex highlight-nonselected-windows
0665a8b0
CY
33 Setting the mark in one buffer has no effect on the marks in other
34buffers. When you return to a buffer with an active mark, the mark is
35at the same place as before. When multiple windows show the same
36buffer, they can have different values of point, and thus different
37regions, but they all share one common mark position. @xref{Windows}.
38Ordinarily, only the selected window highlights its region; however,
39if the variable @code{highlight-nonselected-windows} is
40non-@code{nil}, each window highlights its own region.
6bf7aab6
DL
41
42@menu
e1a3f5b1
CY
43* Setting Mark:: Commands to set the mark.
44* Marking Objects:: Commands to put region around textual units.
45* Using Region:: Summary of ways to operate on contents of the region.
46* Mark Ring:: Previous mark positions saved so you can go back there.
47* Global Mark Ring:: Previous mark positions in various buffers.
48* Shift Selection:: Using shifted cursor motion keys.
49* Disabled Transient Mark:: Leaving regions unhighlighted by default.
6bf7aab6
DL
50@end menu
51
52@node Setting Mark
53@section Setting the Mark
54
55 Here are some commands for setting the mark:
56
6bf7aab6
DL
57@table @kbd
58@item C-@key{SPC}
623cec4c 59Set the mark at point, and activate it (@code{set-mark-command}).
6bf7aab6
DL
60@item C-@@
61The same.
62@item C-x C-x
e1a3f5b1
CY
63Set the mark at point, and activate it; then move point where the mark
64used to be (@code{exchange-point-and-mark}).
6bf7aab6
DL
65@item Drag-Mouse-1
66Set point and the mark around the text you drag across.
67@item Mouse-3
0665a8b0 68Set the mark at point, then move point to where you click
6bf7aab6 69(@code{mouse-save-then-kill}).
963578d3
CY
70@item @samp{Shifted cursor motion keys}
71Set the mark at point if the mark is inactive, then move point.
72@xref{Shift Selection}.
6bf7aab6
DL
73@end table
74
6bf7aab6 75@kindex C-SPC
0665a8b0 76@kindex C-@@
6bf7aab6 77@findex set-mark-command
a8a31aae 78 The most common way to set the mark is with @kbd{C-@key{SPC}}
623cec4c
CY
79(@code{set-mark-command})@footnote{There is no @kbd{C-@key{SPC}}
80character in @acronym{ASCII}; usually, typing @kbd{C-@key{SPC}} on a
81text terminal gives the character @kbd{C-@@}. This key is also bound
82to @code{set-mark-command}, so unless you are unlucky enough to have
da0bbbc4 83a text terminal that behaves differently, you might as well think of
623cec4c
CY
84@kbd{C-@@} as @kbd{C-@key{SPC}}.}. This sets the mark where point is,
85and activates it. You can then move point away, leaving the mark
86behind.
a8a31aae
CY
87
88 For example, suppose you wish to convert part of the buffer to upper
e1a3f5b1
CY
89case. To accomplish this, go to one end of the desired text, type
90@kbd{C-@key{SPC}}, and move point until the desired portion of text is
91highlighted. Now type @kbd{C-x C-u} (@code{upcase-region}). This
92converts the text in the region to upper case, and then deactivates
93the mark.
94
95 Whenever the mark is active, you can deactivate it by typing
96@kbd{C-g} (@pxref{Quitting}). Most commands that operate on the
97region also automatically deactivate the mark, like @kbd{C-x C-u} in
98the above example.
99
100 Instead of setting the mark in order to operate on a region, you can
101also use it to ``remember'' a position in the buffer (by typing
102@kbd{C-@key{SPC} C-@key{SPC}}), and later jump back there (by typing
103@kbd{C-u C-@key{SPC}}). @xref{Mark Ring}, for details.
6bf7aab6
DL
104
105@kindex C-x C-x
106@findex exchange-point-and-mark
0665a8b0 107 The command @kbd{C-x C-x} (@code{exchange-point-and-mark}) exchanges
e1a3f5b1
CY
108the positions of point and the mark. @kbd{C-x C-x} is useful when you
109are satisfied with the position of point but want to move the other
110end of the region (where the mark is). Using @kbd{C-x C-x} a second
111time, if necessary, puts the mark at the new position with point back
112at its original position. Normally, if the mark is inactive, this
113command first reactivates the mark wherever it was last set, to ensure
114that the region is left highlighted. However, if you call it with a
115prefix argument, it leaves the mark inactive and the region
116unhighlighted; you can use this to jump to the mark in a manner
117similar to @kbd{C-u C-@key{SPC}}.
0665a8b0 118
13abd5e3
CY
119 You can also set the mark with the mouse. If you press the left
120mouse button (@kbd{down-mouse-1}) and drag the mouse across a range of
e1a3f5b1
CY
121text, this sets the mark where you first pressed the mouse button and
122puts point where you release it. Alternatively, clicking the right
123mouse button (@kbd{mouse-3}) sets the mark at point and then moves
124point to where you clicked. @xref{Mouse Commands}, for a more
125detailed description of these mouse commands.
0665a8b0
CY
126
127@cindex shift-selection
128 Finally, you can set the mark by holding down the shift key while
129typing certain cursor motion commands (such as @kbd{S-@key{right}},
e1a3f5b1
CY
130@kbd{S-C-f}, @kbd{S-C-n}, etc.) This is called @dfn{shift-selection}.
131It sets the mark at point before moving point, but only if there is no
132active mark set via shift-selection. The mark set by mouse commands
133and by shift-selection behaves slightly differently from the usual
134mark: any subsequent unshifted cursor motion command deactivates it
135automatically. For details, @xref{Shift Selection}.
136
137 Many commands that insert text, such as @kbd{C-y} (@code{yank}), set
138the mark at the other end of the inserted text, without activating it.
139This lets you easily return to that position (@pxref{Mark Ring}). You
140can tell that a command does this when it shows @samp{Mark set} in the
141echo area.
6bf7aab6 142
e1a3f5b1
CY
143@cindex primary selection
144 Under X, every time the region changes while the mark is active,
145Emacs saves the text in the region to the @dfn{primary selection}.
146This lets you insert that text into other X applications with
147@kbd{mouse-2} clicks. @xref{Primary Selection}.
6bf7aab6
DL
148
149@node Marking Objects
150@section Commands to Mark Textual Objects
151
152@cindex marking sections of text
e1a3f5b1 153 Here are commands for placing point and the mark around a textual
a8a31aae 154object such as a word, list, paragraph or page:
6bf7aab6
DL
155
156@table @kbd
157@item M-@@
0665a8b0
CY
158Set mark after end of next word (@code{mark-word}). This does not
159move point.
6bf7aab6 160@item C-M-@@
0665a8b0
CY
161Set mark after end of following balanced expression
162(@code{mark-sexp}). This does not move point.
6bf7aab6 163@item M-h
a8a31aae
CY
164Move point to the beginning of the current paragraph, and set mark at
165the end (@code{mark-paragraph}).
6bf7aab6 166@item C-M-h
a8a31aae
CY
167Move point to the beginning of the current defun, and set mark at the
168end (@code{mark-defun}).
6bf7aab6 169@item C-x C-p
a8a31aae
CY
170Move point to the beginning of the current page, and set mark at the
171end (@code{mark-page}).
0665a8b0
CY
172@item C-x h
173Move point to the beginning of the buffer, and set mark at the end
174(@code{mark-whole-buffer}).
6bf7aab6
DL
175@end table
176
e1a3f5b1 177@kindex M-@@
0665a8b0 178@findex mark-word
e1a3f5b1
CY
179 @kbd{M-@@} (@code{mark-word}) sets the mark at the end of the next
180word (@pxref{Words}, for information about words). Repeated
181invocations of this command extend the region by advancing the mark
182one word at a time. As an exception, if the mark is active and
183located before point, @kbd{M-@@} moves the mark backwards from its
184current position one word at a time.
185
186 This command also accepts a numeric argument @var{n}, which tells it
187to advance the mark by @var{n} words. A negative argument moves the
188mark back by @var{n} words.
189
190@kindex C-M-@@
0665a8b0 191@findex mark-sexp
e1a3f5b1
CY
192 Similarly, @kbd{C-M-@@} (@code{mark-sexp}) puts the mark at the end
193of the next balanced expression (@pxref{Expressions}). Repeated
194invocations extend the region to subsequent expressions, while
195positive or negative numeric arguments move the mark forward or
196backward by the specified number of expressions.
6bf7aab6 197
0665a8b0
CY
198 The other commands in the above list set both point and mark, so as
199to delimit an object in the buffer. @kbd{M-h} (@code{mark-paragraph})
e1a3f5b1
CY
200marks paragraphs (@pxref{Paragraphs}), @kbd{C-M-h} (@code{mark-defun})
201marks top-level definitions (@pxref{Moving by Defuns}), and @kbd{C-x
202C-p} (@code{mark-page}) marks pages (@pxref{Pages}). Repeated
203invocations again play the same role, extending the region to
204consecutive objects; similarly, numeric arguments specify how many
205objects to move the mark by.
206
207@kindex C-x h
208@findex mark-whole-buffer
209@cindex select all
210 @kbd{C-x h} (@code{mark-whole-buffer}) sets up the entire buffer as
211the region, by putting point at the beginning and the mark at the end.
6bf7aab6 212
a8a31aae
CY
213@node Using Region
214@section Operating on the Region
215
216@cindex operations on a marked region
217 Once you have a region, here are some of the ways you can operate on
218it:
219
220@itemize @bullet
221@item
222Kill it with @kbd{C-w} (@pxref{Killing}).
223@item
224Copy it to the kill ring with @kbd{M-w} (@pxref{Yanking}).
225@item
226Convert case with @kbd{C-x C-l} or @kbd{C-x C-u} (@pxref{Case}).
227@item
228Undo changes within it using @kbd{C-u C-/} (@pxref{Undo}).
229@item
230Replace text within it using @kbd{M-%} (@pxref{Query Replace}).
231@item
232Indent it with @kbd{C-x @key{TAB}} or @kbd{C-M-\} (@pxref{Indentation}).
233@item
234Fill it as text with @kbd{M-x fill-region} (@pxref{Filling}).
235@item
e1a3f5b1 236Check the spelling of words within it with @kbd{M-$} (@pxref{Spelling}).
a8a31aae
CY
237@item
238Evaluate it as Lisp code with @kbd{M-x eval-region} (@pxref{Lisp Eval}).
239@item
240Save it in a register with @kbd{C-x r s} (@pxref{Registers}).
241@item
242Save it in a buffer or a file (@pxref{Accumulating Text}).
243@end itemize
244
0fcca8ee
CY
245 Some commands have a default behavior when the mark is inactive, but
246operate on the text in the region if the mark is active. For example,
247@kbd{M-$} (@code{ispell-word}) normally checks the spelling of the
248word at point, but it checks the text in the region if the region is
249active (@pxref{Spelling}). Normally, such commands use their default
250behavior if the region is empty (i.e., if mark and point are at the
251same position). If you want them to operate on the empty region,
252change the variable @code{use-empty-active-region} to @code{t}.
d01c3d68 253
e1a3f5b1
CY
254@vindex mark-even-if-inactive
255 Other commands always operate on the text in the region, and have no
256default behavior. Such commands usually have the word @code{region}
257in their names, like @kbd{C-w} (@code{kill-region}) and @code{C-x C-u}
258(@code{upcase-region}). If the mark is inactive, they operate on the
259``inactive region''---that is, on the text between point and the
260position at which the mark was last set (@pxref{Mark Ring}). To
261disable this behavior, change the variable
262@code{mark-even-if-inactive} to @code{nil}. Then these commands will
263instead signal an error if the mark is inactive.
264
1e680ac9
CY
265@cindex Delete Selection mode
266@cindex mode, Delete Selection
267@findex delete-selection-mode
e1a3f5b1
CY
268 By default, text insertion occurs normally even if the mark is
269active---for example, typing @kbd{a} inserts the character @samp{a},
270then deactivates the mark. If you enable Delete Selection mode, a
271minor mode, then inserting text while the mark is active causes the
272text in the region to be deleted first. To toggle Delete Selection
273mode on or off, type @kbd{M-x delete-selection-mode}.
1e680ac9 274
0665a8b0
CY
275@node Mark Ring
276@section The Mark Ring
277
278@cindex mark ring
e1a3f5b1
CY
279 Each buffer remembers previous locations of the mark, in the
280@dfn{mark ring}. Commands that set the mark also push the old mark
281onto this ring. One of the uses of the mark ring is to remember spots
282that you may want to go back to.
0665a8b0
CY
283
284@table @kbd
285@item C-@key{SPC} C-@key{SPC}
286Set the mark, pushing it onto the mark ring, without activating it.
287@item C-u C-@key{SPC}
288Move point to where the mark was, and restore the mark from the ring
289of former marks.
290@end table
291
292@kindex C-SPC C-SPC
e1a3f5b1
CY
293 The command @kbd{C-@key{SPC} C-@key{SPC}} is handy when you want to
294use the mark to remember a position to which you may wish to return.
295It pushes the current point onto the mark ring, without activating the
0665a8b0
CY
296mark (which would cause Emacs to highlight the region). This is
297actually two consecutive invocations of @kbd{C-@key{SPC}}
298(@code{set-mark-command}); the first @kbd{C-@key{SPC}} sets the mark,
299and the second @kbd{C-@key{SPC}} deactivates it. (When Transient Mark
e1a3f5b1
CY
300mode is off, @kbd{C-@key{SPC} C-@key{SPC}} instead activates Transient
301Mark mode temporarily; @pxref{Disabled Transient Mark}.)
0665a8b0
CY
302
303@kindex C-u C-SPC
304 To return to a marked position, use @code{set-mark-command} with a
305prefix argument: @kbd{C-u C-@key{SPC}}. This moves point to where the
306mark was, and deactivates the mark if it was active. Each subsequent
307@kbd{C-u C-@key{SPC}} jumps to a prior position stored in the mark
308ring. The positions you move through in this way are not lost; they
309go to the end of the ring.
310
311@vindex set-mark-command-repeat-pop
312 If you set @code{set-mark-command-repeat-pop} to non-@code{nil},
313then immediately after you type @kbd{C-u C-@key{SPC}}, you can type
314@kbd{C-@key{SPC}} instead of @kbd{C-u C-@key{SPC}} to cycle through
315the mark ring. By default, @code{set-mark-command-repeat-pop} is
316@code{nil}.
317
318 Each buffer has its own mark ring. All editing commands use the
319current buffer's mark ring. In particular, @kbd{C-u C-@key{SPC}}
320always stays in the same buffer.
321
322@vindex mark-ring-max
323 The variable @code{mark-ring-max} specifies the maximum number of
e1a3f5b1
CY
324entries to keep in the mark ring. This defaults to 16 entries. If
325that many entries exist and another one is pushed, the earliest one in
326the list is discarded. Repeating @kbd{C-u C-@key{SPC}} cycles through
327the positions currently in the ring.
0665a8b0
CY
328
329 If you want to move back to the same place over and over, the mark
330ring may not be convenient enough. If so, you can record the position
a30ed87b
LMI
331in a register for later retrieval (@pxref{Position Registers,, Saving
332Positions in Registers}).
0665a8b0
CY
333
334@node Global Mark Ring
335@section The Global Mark Ring
336@cindex global mark ring
337
c7588364 338@vindex global-mark-ring-max
0665a8b0
CY
339 In addition to the ordinary mark ring that belongs to each buffer,
340Emacs has a single @dfn{global mark ring}. Each time you set a mark,
341in any buffer, this is recorded in the global mark ring in addition to
c7588364
LMI
342the current buffer's own mark ring. The length of this ring can be
343controlled by @code{global-mark-ring-max}, and is 16 by default.
0665a8b0
CY
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 buffers and mark positions.
351
13abd5e3
CY
352@node Shift Selection
353@section Shift Selection
0665a8b0
CY
354@cindex shift-selection
355
356 If you hold down the shift key while typing a cursor motion command,
357this sets the mark before moving point, so that the region extends
e1a3f5b1
CY
358from the original position of point to its new position. This feature
359is referred to as @dfn{shift-selection}. It is similar to the way
360text is selected in other editors.
0665a8b0
CY
361
362 The mark set via shift-selection behaves a little differently from
363what we have described above. Firstly, in addition to the usual ways
364of deactivating the mark (such as changing the buffer text or typing
365@kbd{C-g}), the mark is deactivated by any @emph{unshifted} cursor
366motion command. Secondly, any subsequent @emph{shifted} cursor motion
367command avoids setting the mark anew. Therefore, a series of shifted
e1a3f5b1 368cursor motion commands will continuously adjust the region.
0665a8b0
CY
369
370 Shift-selection only works if the shifted cursor motion key is not
371already bound to a separate command (@pxref{Customization}). For
372example, if you bind @kbd{S-C-f} to another command, typing
373@kbd{S-C-f} runs that command instead of performing a shift-selected
374version of @kbd{C-f} (@code{forward-char}).
375
376 A mark set via mouse commands behaves the same as a mark set via
377shift-selection (@pxref{Setting Mark}). For example, if you specify a
378region by dragging the mouse, you can continue to extend the region
379using shifted cursor motion commands. In either case, any unshifted
380cursor motion command deactivates the mark.
381
382 To turn off shift-selection, set @code{shift-select-mode} to
e1a3f5b1 383@code{nil}. Doing so does not disable setting the mark via mouse
0665a8b0
CY
384commands.
385
e1a3f5b1
CY
386@node Disabled Transient Mark
387@section Disabling Transient Mark Mode
a8a31aae
CY
388@cindex mode, Transient Mark
389@cindex Transient Mark mode
390@cindex highlighting region
391@cindex region highlighting
392@cindex Zmacs mode
0fcca8ee 393@findex transient-mark-mode
a8a31aae 394
e1a3f5b1
CY
395 The default behavior of the mark and region, in which setting the
396mark activates it and highlights the region, is called Transient Mark
397mode. This is a minor mode that is enabled by default. It can be
398toggled with @kbd{M-x transient-mark-mode}, or with the @samp{Active
399Region Highlighting} menu item in the @samp{Options} menu. Turning it
400off switches Emacs to an alternative mode of operation:
a8a31aae
CY
401
402@itemize @bullet
403@item
e1a3f5b1
CY
404Setting the mark, with commands like @kbd{C-@key{SPC}} or @kbd{C-x
405C-x}, does not highlight the region. Therefore, you can't tell by
406looking where the mark is located; you have to remember.
a8a31aae 407
e1a3f5b1
CY
408The usual solution to this problem is to set the mark and then use it
409soon, before you forget where it is. You can also check where the
410mark is by using @kbd{C-x C-x}, which exchanges the positions of the
411point and the mark (@pxref{Setting Mark}).
a8a31aae
CY
412
413@item
414Many commands that move point long distances, like @kbd{M-<} and
415@kbd{C-s}, first set the mark where point was.
416
417@item
e59c4233
CY
418Some commands, which ordinarily act on the region when the mark is
419active, no longer do so. For example, normally @kbd{M-%}
420(@code{query-replace}) performs replacements within the region, if the
421mark is active. When Transient Mark mode is off, it always operates
422from point to the end of the buffer. Commands that act this way are
423identified in their own documentation.
a8a31aae
CY
424@end itemize
425
426 While Transient Mark mode is off, you can activate it temporarily
427using @kbd{C-@key{SPC} C-@key{SPC}} or @kbd{C-u C-x C-x}.
428
429@table @kbd
430@item C-@key{SPC} C-@key{SPC}
431@kindex C-@key{SPC} C-@key{SPC}
432Set the mark at point (like plain @kbd{C-@key{SPC}}) and enable
433Transient Mark mode just once, until the mark is deactivated. (This
434is not really a separate command; you are using the @kbd{C-@key{SPC}}
435command twice.)
436
437@item C-u C-x C-x
438@kindex C-u C-x C-x
e59c4233
CY
439Activate the mark and enable Transient Mark mode temporarily, until
440the mark is next deactivated. (This is the @kbd{C-x C-x} command,
441@code{exchange-point-and-mark}, with a prefix argument.)
a8a31aae
CY
442@end table
443
444 These commands set or activate the mark, and enable Transient Mark
445mode only until the mark is deactivated. One reason you may want to
446use them is that some commands operate on the entire buffer instead of
447the region when Transient Mark mode is off. Enabling Transient Mark
448mode momentarily gives you a way to use these commands on the region.
e1a3f5b1
CY
449
450 When you specify a region with the mouse (@pxref{Setting Mark}), or
451with shift-selection (@pxref{Shift Selection}), this likewise
452activates Transient Mark mode temporarily and highlights the region.