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
10 A @dfn{marker} is a Lisp object used to specify a position in a buffer
11 relative to the surrounding text. A marker changes its offset from the
12 beginning of the buffer automatically whenever text is inserted or
13 deleted, so that it stays with the two characters on either side of it.
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''.
25 @node Overview of Markers
26 @section Overview of Markers
28 A marker specifies a buffer and a position in that buffer. The marker
29 can be used to represent a position in the functions that require one,
30 just as an integer could be used. @xref{Positions}, for a complete
31 description of positions.
33 A marker has two attributes: the marker position, and the marker
34 buffer. The marker position is an integer that is equivalent (at a
35 given time) to the marker as a position in that buffer. But the
36 marker's position value can change often during the life of the marker.
37 Insertion and deletion of text in the buffer relocate the marker. The
38 idea is that a marker positioned between two characters remains between
39 those two characters despite insertion and deletion elsewhere in the
40 buffer. Relocation changes the integer equivalent of the marker.
42 @cindex marker relocation
43 Deleting text around a marker's position leaves the marker between the
44 characters immediately before and after the deleted text. Inserting
45 text at the position of a marker normally leaves the marker in front of
46 the new text---unless it is inserted with @code{insert-before-markers}
49 @cindex marker garbage collection
50 Insertion and deletion in a buffer must check all the markers and
51 relocate them if necessary. This slows processing in a buffer with a
52 large number of markers. For this reason, it is a good idea to make a
53 marker point nowhere if you are sure you don't need it any more.
54 Unreferenced markers are garbage collected eventually, but until then
55 will continue to use time if they do point somewhere.
57 @cindex markers as numbers
58 Because it is common to perform arithmetic operations on a marker
59 position, most of the arithmetic operations (including @code{+} and
60 @code{-}) accept markers as arguments. In such cases, the marker
61 stands for its current position.
63 Here are examples of creating markers, setting markers, and moving point
68 ;; @r{Make a new marker that initially does not point anywhere:}
69 (setq m1 (make-marker))
70 @result{} #<marker in no buffer>
74 ;; @r{Set @code{m1} to point between the 99th and 100th characters}
75 ;; @r{in the current buffer:}
77 @result{} #<marker at 100 in markers.texi>
81 ;; @r{Now insert one character at the beginning of the buffer:}
82 (goto-char (point-min))
89 ;; @r{@code{m1} is updated appropriately.}
91 @result{} #<marker at 101 in markers.texi>
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>
106 ;; @r{When you are finished using a marker, make it point nowhere.}
108 @result{} #<marker in no buffer>
112 @node Predicates on Markers
113 @section Predicates on Markers
115 You can test an object to see whether it is a marker, or whether it is
116 either an integer or a marker. The latter test is useful in connection
117 with the arithmetic functions that work with both markers and integers.
119 @defun markerp object
120 This function returns @code{t} if @var{object} is a marker, @code{nil}
121 otherwise. Note that integers are not markers, even though many
122 functions will accept either a marker or an integer.
125 @defun integer-or-marker-p object
126 This function returns @code{t} if @var{object} is an integer or a marker,
127 @code{nil} otherwise.
130 @defun number-or-marker-p object
131 This function returns @code{t} if @var{object} is a number (either kind)
132 or a marker, @code{nil} otherwise.
135 @node Creating Markers
136 @section Functions That Create Markers
138 When you create a new marker, you can make it point nowhere, or point
139 to the present position of point, or to the beginning or end of the
140 accessible portion of the buffer, or to the same place as another given
144 This functions returns a newly created marker that does not point
150 @result{} #<marker in no buffer>
156 This function returns a new marker that points to the present position
157 of point in the current buffer. @xref{Point}. For an example, see
158 @code{copy-marker}, below.
161 @defun point-min-marker
162 This function returns a new marker that points to the beginning of the
163 accessible portion of the buffer. This will be the beginning of the
164 buffer unless narrowing is in effect. @xref{Narrowing}.
167 @defun point-max-marker
168 @cindex end of buffer marker
169 This function returns a new marker that points to the end of the
170 accessible portion of the buffer. This will be the end of the buffer
171 unless narrowing is in effect. @xref{Narrowing}.
173 Here are examples of this function and @code{point-min-marker}, shown in
174 a buffer containing a version of the source file for the text of this
180 @result{} #<marker at 1 in markers.texi>
182 @result{} #<marker at 15573 in markers.texi>
186 (narrow-to-region 100 200)
191 @result{} #<marker at 100 in markers.texi>
195 @result{} #<marker at 200 in markers.texi>
200 @defun copy-marker marker-or-integer
201 If passed a marker as its argument, @code{copy-marker} returns a
202 new 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.
207 If passed an integer argument less than 1, @code{copy-marker} returns a
208 new marker that points to the beginning of the current buffer. If
209 passed 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
213 An error is signaled if @var{marker} is neither a marker nor an
218 (setq p (point-marker))
219 @result{} #<marker at 2139 in markers.texi>
223 (setq q (copy-marker p))
224 @result{} #<marker at 2139 in markers.texi>
239 @result{} #<marker at 1 in markers.texi>
244 @result{} #<marker at 7572 in markers.texi>
249 @node Information from Markers
250 @section Information from Markers
252 This section describes the functions for accessing the components of a
255 @defun marker-position marker
256 This function returns the position that @var{marker} points to, or
257 @code{nil} if it points nowhere.
260 @defun marker-buffer marker
261 This function returns the buffer that @var{marker} points into, or
262 @code{nil} if it points nowhere.
266 (setq m (make-marker))
267 @result{} #<marker in no buffer>
279 (set-marker m 3770 (current-buffer))
280 @result{} #<marker at 3770 in markers.texi>
284 @result{} #<buffer markers.texi>
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
295 if they both point nowhere.
297 @node Changing Markers
298 @section Changing Marker Positions
300 This section describes how to change the position of an existing
301 marker. When you do this, be sure you know whether the marker is used
302 outside of your program, and, if so, what effects will result from
303 moving it---otherwise, confusing things may happen in other parts of
306 @defun set-marker marker position &optional buffer
307 This function moves @var{marker} to @var{position}
308 in @var{buffer}. If @var{buffer} is not provided, it defaults to
311 If @var{position} is less than 1, @code{set-marker} moves @var{marker}
312 to the beginning of the buffer. If @var{position} is greater than the
313 size of the buffer, @code{set-marker} moves marker to the end of the
314 buffer. If @var{position} is @code{nil} or a marker that points
315 nowhere, then @var{marker} is set to point nowhere.
317 The value returned is @var{marker}.
321 (setq m (point-marker))
322 @result{} #<marker at 4714 in markers.texi>
326 @result{} #<marker at 55 in markers.texi>
329 (setq b (get-buffer "foo"))
330 @result{} #<buffer foo>
334 @result{} #<marker at 1 in foo>
339 @defun move-marker marker position &optional buffer
340 This is another name for @code{set-marker}.
348 One special marker in each buffer is designated @dfn{the mark}. It
349 records 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
351 only to values that have a potential use to the user, and never for
352 their own internal purposes. For example, the @code{replace-regexp}
353 command sets the mark to the value of point before doing any
354 replacements, because this enables the user to move back there
355 conveniently after the replace is finished.
357 Many commands are designed so that when called interactively they
358 operate on the text between point and the mark. If you are writing such
359 a command, don't examine the mark directly; instead, use
360 @code{interactive} with the @samp{r} specification. This provides the
361 values of point and the mark as arguments to the command in an
362 interactive call, but permits other Lisp programs to specify arguments
363 explicitly. @xref{Interactive Codes}.
365 Each buffer has its own value of the mark that is independent of the
366 value of the mark in other buffers. When a buffer is created, the mark
367 exists but does not point anywhere. We consider this state as ``the
368 absence of a mark in that buffer.''
370 Once the mark ``exists'' in a buffer, it normally never ceases to
371 exist. However, it may become @dfn{inactive}, if Transient Mark mode is
372 enabled. The variable @code{mark-active}, which is always local in all
373 buffers, indicates whether the mark is active: non-@code{nil} means yes.
374 A command can request deactivation of the mark upon return to the editor
375 command loop by setting @code{deactivate-mark} to a non-@code{nil} value
376 (but this causes deactivation only if Transient Mark mode is enabled).
378 The main motivation for using Transient Mark mode is that this mode
379 also enables highlighting of the region when the mark is active.
382 In addition to the mark, each buffer has a @dfn{mark ring} which is a
383 list of markers containing previous values of the mark. When editing
384 commands change the mark, they should normally save the old value of the
385 mark on the mark ring. The variable @code{mark-ring-max} specifies the
386 maximum number of entries in the mark ring; once the list becomes this
387 long, adding a new element deletes the last element.
389 @defun mark &optional force
390 @cindex current buffer mark
391 This function returns the current buffer's mark position as an integer.
393 If the mark is inactive, @code{mark} normally signals an error.
394 However, if @var{force} is non-@code{nil}, then @code{mark} returns the
395 mark position anyway---or @code{nil}, if the mark is not yet set for
400 This function returns the current buffer's mark. This is the very marker
401 that records the mark location inside Emacs, not a copy. Therefore,
402 changing this marker's position will directly affect the position of the mark.
403 Don't do it unless that is the effect you want.
407 (setq m (mark-marker))
408 @result{} #<marker at 3420 in markers.texi>
412 @result{} #<marker at 100 in markers.texi>
416 @result{} #<marker at 100 in markers.texi>
420 Like any marker, this marker can be set to point at any buffer you like.
421 We don't recommend that you make it point at any buffer other than the
422 one of which it is the mark. If you do, it will yield perfectly
423 consistent, but rather odd, results.
427 @deffn Command set-mark-command jump
428 If @var{jump} is @code{nil}, this command sets the mark to the value
429 of point and pushes the previous value of the mark on the mark ring. The
430 message @samp{Mark set} is also displayed in the echo area.
432 If @var{jump} is not @code{nil}, this command sets point to the value
433 of the mark, and sets the mark to the previous saved mark value, which
434 is popped off the mark ring.
436 This function is @emph{only} intended for interactive use.
440 @defun set-mark position
441 This function sets the mark to @var{position}, and activates the mark.
442 The old value of the mark is @emph{not} pushed onto the mark ring.
444 @strong{Please note:} Use this function only if you want the user to
445 see that the mark has moved, and you want the previous mark position to
446 be 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}.
450 Novice Emacs Lisp programmers often try to use the mark for the wrong
451 purposes. The mark saves a location for the user's convenience. An
452 editing command should not alter the mark unless altering the mark is
453 part of the user-level functionality of the command. (And, in that
454 case, this effect should be documented.) To remember a location for
455 internal use in the Lisp program, store it in a Lisp variable. For
462 (delete-region beg (point))).
467 @c for interactive use only
469 @deffn Command exchange-point-and-mark
470 This function exchanges the positions of point and the mark.
471 It is intended for interactive use.
475 @defun push-mark &optional position nomsg activate
476 This function sets the current buffer's mark to @var{position}, and
477 pushes 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}.
481 The function @code{push-mark} normally @emph{does not} activate the
482 mark. To do that, specify @code{t} for the argument @var{activate}.
484 A @samp{Mark set} message is displayed unless @var{nomsg} is
489 This function pops off the top element of @code{mark-ring} and makes
490 that mark become the buffer's actual mark. This does not move point in
491 the buffer, and it does nothing if @code{mark-ring} is empty. It
492 deactivates the mark.
494 The return value is not meaningful.
497 @defopt transient-mark-mode
498 @cindex Transient Mark mode
499 This variable enables Transient Mark mode, in which every
500 buffer-modifying primitive sets @code{deactivate-mark}. The consequence
501 of this is that commands that modify the buffer normally make the mark
505 @defvar deactivate-mark
506 If an editor command sets this variable non-@code{nil}, then the editor
507 command loop deactivates the mark after the command returns, but only if
508 Transient Mark mode is enabled.
511 @defun deactivate-mark
512 This function deactivates the mark, but only if Transient Mark mode
517 The mark is active when this variable is non-@code{nil}. This variable
518 is always local in each buffer.
521 @defvar activate-mark-hook
522 @defvarx deactivate-mark-hook
523 These normal hooks are run, respectively, when the mark becomes active
524 and when it becomes inactive. The hook @code{activate-mark-hook} is also
525 run at the end of a command if the mark is active and the region may
530 The value of this buffer-local variable is the list of saved former
531 marks of the current buffer, most recent first.
536 @result{} (#<marker at 11050 in markers.texi>
537 #<marker at 10832 in markers.texi>
543 @defopt mark-ring-max
544 The value of this variable is the maximum size of @code{mark-ring}. If
545 more marks than this are pushed onto the @code{mark-ring},
546 @code{push-mark} discards an old mark when it adds a new one.
553 The text between point and the mark is known as @dfn{the region}.
554 Various functions operate on text delimited by point and the mark, but
555 only those functions specifically related to the region itself are
558 @defun region-beginning
559 This function returns the position of the beginning of the region (as
560 an integer). This is the position of either point or the mark,
561 whichever is smaller.
563 If the mark does not point anywhere, an error is signaled.
567 This function returns the position of the end of the region (as an
568 integer). This is the position of either point or the mark, whichever is
571 If the mark does not point anywhere, an error is signaled.
574 Few programs need to use the @code{region-beginning} and
575 @code{region-end} functions. A command designed to operate on a region
576 should normally use @code{interactive} with the @samp{r} specification
577 to find the beginning and end of the region. This lets other Lisp
578 programs specify the bounds explicitly as arguments. (@xref{Interactive