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