Update overriding-local-map-menu-flag.
[bpt/emacs.git] / lispref / markers.texi
CommitLineData
0abf66c5
RS
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
3@c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4@c See the file elisp.texi for copying conditions.
5@setfilename ../info/markers
6@node Markers, Text, Positions, Top
7@chapter Markers
8@cindex markers
9
10 A @dfn{marker} is a Lisp object used to specify a position in a buffer
11relative to the surrounding text. A marker changes its offset from the
12beginning of the buffer automatically whenever text is inserted or
13deleted, so that it stays with the two characters on either side of it.
14
15@menu
16* Overview of Markers:: The components of a marker, and how it relocates.
17* Predicates on Markers:: Testing whether an object is a marker.
18* Creating Markers:: Making empty markers or markers at certain places.
19* Information from Markers:: Finding the marker's buffer or character position.
20* Changing Markers:: Moving the marker to a new buffer or position.
21* The Mark:: How ``the mark'' is implemented with a marker.
22* The Region:: How to access ``the region''.
23@end menu
24
25@node Overview of Markers
26@section Overview of Markers
27
28 A marker specifies a buffer and a position in that buffer. The marker
29can be used to represent a position in the functions that require one,
30just as an integer could be used. @xref{Positions}, for a complete
31description of positions.
32
33 A marker has two attributes: the marker position, and the marker
29679a81 34buffer. The marker position is an integer that is equivalent (at a
0abf66c5
RS
35given time) to the marker as a position in that buffer. But the
36marker's position value can change often during the life of the marker.
37Insertion and deletion of text in the buffer relocate the marker. The
38idea is that a marker positioned between two characters remains between
39those two characters despite insertion and deletion elsewhere in the
40buffer. Relocation changes the integer equivalent of the marker.
41
42@cindex marker relocation
43 Deleting text around a marker's position leaves the marker between the
44characters immediately before and after the deleted text. Inserting
45text at the position of a marker normally leaves the marker in front of
46the new text---unless it is inserted with @code{insert-before-markers}
47(@pxref{Insertion}).
48
49@cindex marker garbage collection
50 Insertion and deletion in a buffer must check all the markers and
51relocate them if necessary. This slows processing in a buffer with a
52large number of markers. For this reason, it is a good idea to make a
53marker point nowhere if you are sure you don't need it any more.
54Unreferenced markers are garbage collected eventually, but until then
55will continue to use time if they do point somewhere.
56
57@cindex markers as numbers
58 Because it is common to perform arithmetic operations on a marker
59position, most of the arithmetic operations (including @code{+} and
60@code{-}) accept markers as arguments. In such cases, the marker
61stands for its current position.
62
63Here are examples of creating markers, setting markers, and moving point
64to markers:
65
66@example
67@group
68;; @r{Make a new marker that initially does not point anywhere:}
69(setq m1 (make-marker))
70 @result{} #<marker in no buffer>
71@end group
72
73@group
74;; @r{Set @code{m1} to point between the 99th and 100th characters}
75;; @r{in the current buffer:}
76(set-marker m1 100)
77 @result{} #<marker at 100 in markers.texi>
78@end group
79
80@group
81;; @r{Now insert one character at the beginning of the buffer:}
82(goto-char (point-min))
83 @result{} 1
84(insert "Q")
85 @result{} nil
86@end group
87
88@group
89;; @r{@code{m1} is updated appropriately.}
90m1
91 @result{} #<marker at 101 in markers.texi>
92@end group
93
94@group
95;; @r{Two markers that point to the same position}
96;; @r{are not @code{eq}, but they are @code{equal}.}
97(setq m2 (copy-marker m1))
98 @result{} #<marker at 101 in markers.texi>
99(eq m1 m2)
100 @result{} nil
101(equal m1 m2)
102 @result{} t
103@end group
104
105@group
106;; @r{When you are finished using a marker, make it point nowhere.}
107(set-marker m1 nil)
108 @result{} #<marker in no buffer>
109@end group
110@end example
111
112@node Predicates on Markers
113@section Predicates on Markers
114
115 You can test an object to see whether it is a marker, or whether it is
116either an integer or a marker. The latter test is useful in connection
117with the arithmetic functions that work with both markers and integers.
118
119@defun markerp object
120This function returns @code{t} if @var{object} is a marker, @code{nil}
121otherwise. Note that integers are not markers, even though many
122functions will accept either a marker or an integer.
123@end defun
124
125@defun integer-or-marker-p object
126This function returns @code{t} if @var{object} is an integer or a marker,
127@code{nil} otherwise.
128@end defun
129
130@defun number-or-marker-p object
131This function returns @code{t} if @var{object} is a number (either kind)
132or a marker, @code{nil} otherwise.
133@end defun
134
135@node Creating Markers
136@section Functions That Create Markers
137
138 When you create a new marker, you can make it point nowhere, or point
139to the present position of point, or to the beginning or end of the
140accessible portion of the buffer, or to the same place as another given
141marker.
142
143@defun make-marker
29679a81 144This functions returns a newly created marker that does not point
0abf66c5
RS
145anywhere.
146
147@example
148@group
149(make-marker)
150 @result{} #<marker in no buffer>
151@end group
152@end example
153@end defun
154
155@defun point-marker
156This function returns a new marker that points to the present position
157of point in the current buffer. @xref{Point}. For an example, see
158@code{copy-marker}, below.
159@end defun
160
161@defun point-min-marker
162This function returns a new marker that points to the beginning of the
163accessible portion of the buffer. This will be the beginning of the
164buffer unless narrowing is in effect. @xref{Narrowing}.
165@end defun
166
167@defun point-max-marker
168@cindex end of buffer marker
169This function returns a new marker that points to the end of the
170accessible portion of the buffer. This will be the end of the buffer
171unless narrowing is in effect. @xref{Narrowing}.
172
173Here are examples of this function and @code{point-min-marker}, shown in
174a buffer containing a version of the source file for the text of this
175chapter.
176
177@example
178@group
179(point-min-marker)
180 @result{} #<marker at 1 in markers.texi>
181(point-max-marker)
182 @result{} #<marker at 15573 in markers.texi>
183@end group
184
185@group
186(narrow-to-region 100 200)
187 @result{} nil
188@end group
189@group
190(point-min-marker)
191 @result{} #<marker at 100 in markers.texi>
192@end group
193@group
194(point-max-marker)
195 @result{} #<marker at 200 in markers.texi>
196@end group
197@end example
198@end defun
199
200@defun copy-marker marker-or-integer
201If passed a marker as its argument, @code{copy-marker} returns a
202new marker that points to the same place and the same buffer as does
203@var{marker-or-integer}. If passed an integer as its argument,
204@code{copy-marker} returns a new marker that points to position
205@var{marker-or-integer} in the current buffer.
206
207If passed an integer argument less than 1, @code{copy-marker} returns a
208new marker that points to the beginning of the current buffer. If
209passed an integer argument greater than the length of the buffer,
210@code{copy-marker} returns a new marker that points to the end of the
211buffer.
212
213An error is signaled if @var{marker} is neither a marker nor an
214integer.
215
216@example
217@group
218(setq p (point-marker))
219 @result{} #<marker at 2139 in markers.texi>
220@end group
221
222@group
223(setq q (copy-marker p))
224 @result{} #<marker at 2139 in markers.texi>
225@end group
226
227@group
228(eq p q)
229 @result{} nil
230@end group
231
232@group
233(equal p q)
234 @result{} t
235@end group
236
237@group
238(copy-marker 0)
239 @result{} #<marker at 1 in markers.texi>
240@end group
241
242@group
243(copy-marker 20000)
244 @result{} #<marker at 7572 in markers.texi>
245@end group
246@end example
247@end defun
248
249@node Information from Markers
250@section Information from Markers
251
252 This section describes the functions for accessing the components of a
253marker object.
254
255@defun marker-position marker
256This function returns the position that @var{marker} points to, or
257@code{nil} if it points nowhere.
258@end defun
259
260@defun marker-buffer marker
261This function returns the buffer that @var{marker} points into, or
262@code{nil} if it points nowhere.
263
264@example
265@group
266(setq m (make-marker))
267 @result{} #<marker in no buffer>
268@end group
269@group
270(marker-position m)
271 @result{} nil
272@end group
273@group
274(marker-buffer m)
275 @result{} nil
276@end group
277
278@group
279(set-marker m 3770 (current-buffer))
280 @result{} #<marker at 3770 in markers.texi>
281@end group
282@group
283(marker-buffer m)
284 @result{} #<buffer markers.texi>
285@end group
286@group
287(marker-position m)
288 @result{} 3770
289@end group
290@end example
291@end defun
292
293 Two distinct markers are considered @code{equal} (even though not
294@code{eq}) to each other if they have the same position and buffer, or
295if they both point nowhere.
296
297@node Changing Markers
298@section Changing Marker Positions
299
300 This section describes how to change the position of an existing
301marker. When you do this, be sure you know whether the marker is used
302outside of your program, and, if so, what effects will result from
303moving it---otherwise, confusing things may happen in other parts of
304Emacs.
305
306@defun set-marker marker position &optional buffer
307This function moves @var{marker} to @var{position}
308in @var{buffer}. If @var{buffer} is not provided, it defaults to
309the current buffer.
310
311If @var{position} is less than 1, @code{set-marker} moves @var{marker}
29679a81
RS
312to the beginning of the buffer. If @var{position} is greater than the
313size of the buffer, @code{set-marker} moves marker to the end of the
314buffer. If @var{position} is @code{nil} or a marker that points
315nowhere, then @var{marker} is set to point nowhere.
0abf66c5
RS
316
317The value returned is @var{marker}.
318
319@example
320@group
321(setq m (point-marker))
322 @result{} #<marker at 4714 in markers.texi>
323@end group
324@group
325(set-marker m 55)
326 @result{} #<marker at 55 in markers.texi>
327@end group
328@group
329(setq b (get-buffer "foo"))
330 @result{} #<buffer foo>
331@end group
332@group
333(set-marker m 0 b)
334 @result{} #<marker at 1 in foo>
335@end group
336@end example
337@end defun
338
339@defun move-marker marker position &optional buffer
340This is another name for @code{set-marker}.
341@end defun
342
343@node The Mark
344@section The Mark
345@cindex mark, the
346@cindex mark ring
347
348 One special marker in each buffer is designated @dfn{the mark}. It
349records a position for the user for the sake of commands such as
350@kbd{C-w} and @kbd{C-x @key{TAB}}. Lisp programs should set the mark
351only to values that have a potential use to the user, and never for
352their own internal purposes. For example, the @code{replace-regexp}
353command sets the mark to the value of point before doing any
354replacements, because this enables the user to move back there
355conveniently after the replace is finished.
356
357 Many commands are designed so that when called interactively they
358operate on the text between point and the mark. If you are writing such
359a command, don't examine the mark directly; instead, use
360@code{interactive} with the @samp{r} specification. This provides the
361values of point and the mark as arguments to the command in an
362interactive call, but permits other Lisp programs to specify arguments
363explicitly. @xref{Interactive Codes}.
364
365 Each buffer has its own value of the mark that is independent of the
366value of the mark in other buffers. When a buffer is created, the mark
367exists but does not point anywhere. We consider this state as ``the
29679a81 368absence of a mark in that buffer.''
0abf66c5
RS
369
370 Once the mark ``exists'' in a buffer, it normally never ceases to
371exist. However, it may become @dfn{inactive}, if Transient Mark mode is
372enabled. The variable @code{mark-active}, which is always local in all
29679a81
RS
373buffers, indicates whether the mark is active: non-@code{nil} means yes.
374A command can request deactivation of the mark upon return to the editor
375command loop by setting @code{deactivate-mark} to a non-@code{nil} value
376(but this causes deactivation only if Transient Mark mode is enabled).
0abf66c5
RS
377
378 The main motivation for using Transient Mark mode is that this mode
379also enables highlighting of the region when the mark is active.
380@xref{Display}.
381
382 In addition to the mark, each buffer has a @dfn{mark ring} which is a
383list of markers containing previous values of the mark. When editing
384commands change the mark, they should normally save the old value of the
385mark on the mark ring. The variable @code{mark-ring-max} specifies the
386maximum number of entries in the mark ring; once the list becomes this
387long, adding a new element deletes the last element.
388
389@defun mark &optional force
390@cindex current buffer mark
391This function returns the current buffer's mark position as an integer.
392
393If the mark is inactive, @code{mark} normally signals an error.
394However, if @var{force} is non-@code{nil}, then @code{mark} returns the
395mark position anyway---or @code{nil}, if the mark is not yet set for
396this buffer.
397@end defun
398
399@defun mark-marker
400This function returns the current buffer's mark. This is the very marker
29679a81 401that records the mark location inside Emacs, not a copy. Therefore,
0abf66c5
RS
402changing this marker's position will directly affect the position of the mark.
403Don't do it unless that is the effect you want.
404
405@example
406@group
407(setq m (mark-marker))
408 @result{} #<marker at 3420 in markers.texi>
409@end group
410@group
411(set-marker m 100)
412 @result{} #<marker at 100 in markers.texi>
413@end group
414@group
415(mark-marker)
416 @result{} #<marker at 100 in markers.texi>
417@end group
418@end example
419
420Like any marker, this marker can be set to point at any buffer you like.
421We don't recommend that you make it point at any buffer other than the
422one of which it is the mark. If you do, it will yield perfectly
423consistent, but rather odd, results.
424@end defun
425
426@ignore
427@deffn Command set-mark-command jump
428If @var{jump} is @code{nil}, this command sets the mark to the value
429of point and pushes the previous value of the mark on the mark ring. The
430message @samp{Mark set} is also displayed in the echo area.
431
432If @var{jump} is not @code{nil}, this command sets point to the value
433of the mark, and sets the mark to the previous saved mark value, which
434is popped off the mark ring.
435
436This function is @emph{only} intended for interactive use.
437@end deffn
438@end ignore
439
440@defun set-mark position
441This function sets the mark to @var{position}, and activates the mark.
442The old value of the mark is @emph{not} pushed onto the mark ring.
443
ec221d13 444@strong{Please note:} Use this function only if you want the user to
0abf66c5
RS
445see that the mark has moved, and you want the previous mark position to
446be lost. Normally, when a new mark is set, the old one should go on the
447@code{mark-ring}. For this reason, most applications should use
448@code{push-mark} and @code{pop-mark}, not @code{set-mark}.
449
450Novice Emacs Lisp programmers often try to use the mark for the wrong
451purposes. The mark saves a location for the user's convenience. An
452editing command should not alter the mark unless altering the mark is
453part of the user-level functionality of the command. (And, in that
454case, this effect should be documented.) To remember a location for
455internal use in the Lisp program, store it in a Lisp variable. For
456example:
457
458@example
459@group
460(let ((beg (point)))
461 (forward-line 1)
462 (delete-region beg (point))).
463@end group
464@end example
465@end defun
466
467@c for interactive use only
468@ignore
469@deffn Command exchange-point-and-mark
470This function exchanges the positions of point and the mark.
471It is intended for interactive use.
472@end deffn
473@end ignore
474
475@defun push-mark &optional position nomsg activate
476This function sets the current buffer's mark to @var{position}, and
477pushes a copy of the previous mark onto @code{mark-ring}. If
478@var{position} is @code{nil}, then the value of point is used.
479@code{push-mark} returns @code{nil}.
480
481The function @code{push-mark} normally @emph{does not} activate the
482mark. To do that, specify @code{t} for the argument @var{activate}.
483
484A @samp{Mark set} message is displayed unless @var{nomsg} is
485non-@code{nil}.
486@end defun
487
488@defun pop-mark
489This function pops off the top element of @code{mark-ring} and makes
490that mark become the buffer's actual mark. This does not move point in
491the buffer, and it does nothing if @code{mark-ring} is empty. It
492deactivates the mark.
493
494The return value is not meaningful.
495@end defun
496
497@defopt transient-mark-mode
498@cindex Transient Mark mode
bfe721d1
KH
499This variable if non-@code{nil} enables Transient Mark mode, in which
500every buffer-modifying primitive sets @code{deactivate-mark}. The
501consequence of this is that commands that modify the buffer normally
502make the mark inactive.
0abf66c5
RS
503@end defopt
504
505@defvar deactivate-mark
506If an editor command sets this variable non-@code{nil}, then the editor
29679a81
RS
507command loop deactivates the mark after the command returns, but only if
508Transient Mark mode is enabled.
0abf66c5
RS
509@end defvar
510
29679a81
RS
511@defun deactivate-mark
512This function deactivates the mark, but only if Transient Mark mode
513is enabled.
514@end defun
515
0abf66c5
RS
516@defvar mark-active
517The mark is active when this variable is non-@code{nil}. This variable
518is always local in each buffer.
519@end defvar
520
521@defvar activate-mark-hook
522@defvarx deactivate-mark-hook
523These normal hooks are run, respectively, when the mark becomes active
524and when it becomes inactive. The hook @code{activate-mark-hook} is also
525run at the end of a command if the mark is active and the region may
526have changed.
527@end defvar
528
529@defvar mark-ring
530The value of this buffer-local variable is the list of saved former
531marks of the current buffer, most recent first.
532
533@example
534@group
535mark-ring
536@result{} (#<marker at 11050 in markers.texi>
537 #<marker at 10832 in markers.texi>
538 @dots{})
539@end group
540@end example
541@end defvar
542
543@defopt mark-ring-max
544The value of this variable is the maximum size of @code{mark-ring}. If
545more marks than this are pushed onto the @code{mark-ring},
546@code{push-mark} discards an old mark when it adds a new one.
547@end defopt
548
549@node The Region
550@section The Region
551@cindex region, the
552
553 The text between point and the mark is known as @dfn{the region}.
554Various functions operate on text delimited by point and the mark, but
555only those functions specifically related to the region itself are
556described here.
557
558@defun region-beginning
559This function returns the position of the beginning of the region (as
560an integer). This is the position of either point or the mark,
561whichever is smaller.
562
563If the mark does not point anywhere, an error is signaled.
564@end defun
565
566@defun region-end
567This function returns the position of the end of the region (as an
568integer). This is the position of either point or the mark, whichever is
569larger.
570
571If the mark does not point anywhere, an error is signaled.
572@end defun
573
574 Few programs need to use the @code{region-beginning} and
575@code{region-end} functions. A command designed to operate on a region
576should normally use @code{interactive} with the @samp{r} specification
577to find the beginning and end of the region. This lets other Lisp
578programs specify the bounds explicitly as arguments. (@xref{Interactive
579Codes}.)