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