2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999
4 @c Free Software Foundation, Inc.
5 @c See the file elisp.texi for copying conditions.
6 @setfilename ../info/markers
7 @node Markers, Text, Positions, Top
11 A @dfn{marker} is a Lisp object used to specify a position in a buffer
12 relative to the surrounding text. A marker changes its offset from the
13 beginning of the buffer automatically whenever text is inserted or
14 deleted, so that it stays with the two characters on either side of it.
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.
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.
24 * The Mark:: How ``the mark'' is implemented with a marker.
25 * The Region:: How to access ``the region''.
28 @node Overview of Markers
29 @section Overview of Markers
31 A marker specifies a buffer and a position in that buffer. The
32 marker can be used to represent a position in the functions that
33 require one, just as an integer could be used. In that case, the
34 marker's buffer is normally ignored. Of course, a marker used in this
35 way usually points to a position in the buffer that the function
36 operates on, but that is entirely the programmer's responsibility.
37 @xref{Positions}, for a complete description of positions.
39 A marker has two attributes: the marker position, and the marker
40 buffer. The marker position is an integer that is equivalent (at a
41 given time) to the marker as a position in that buffer. But the
42 marker's position value can change often during the life of the marker.
43 Insertion and deletion of text in the buffer relocate the marker. The
44 idea is that a marker positioned between two characters remains between
45 those two characters despite insertion and deletion elsewhere in the
46 buffer. Relocation changes the integer equivalent of the marker.
48 @cindex marker relocation
49 Deleting text around a marker's position leaves the marker between the
50 characters immediately before and after the deleted text. Inserting
51 text at the position of a marker normally leaves the marker either in
52 front of or after the new text, depending on the marker's @dfn{insertion
53 type} (@pxref{Marker Insertion Types})---unless the insertion is done
54 with @code{insert-before-markers} (@pxref{Insertion}).
56 @cindex marker garbage collection
57 Insertion and deletion in a buffer must check all the markers and
58 relocate them if necessary. This slows processing in a buffer with a
59 large number of markers. For this reason, it is a good idea to make a
60 marker point nowhere if you are sure you don't need it any more.
61 Unreferenced markers are garbage collected eventually, but until then
62 will continue to use time if they do point somewhere.
64 @cindex markers as numbers
65 Because it is common to perform arithmetic operations on a marker
66 position, most of the arithmetic operations (including @code{+} and
67 @code{-}) accept markers as arguments. In such cases, the marker
68 stands for its current position.
70 Here are examples of creating markers, setting markers, and moving point
75 ;; @r{Make a new marker that initially does not point anywhere:}
76 (setq m1 (make-marker))
77 @result{} #<marker in no buffer>
81 ;; @r{Set @code{m1} to point between the 99th and 100th characters}
82 ;; @r{in the current buffer:}
84 @result{} #<marker at 100 in markers.texi>
88 ;; @r{Now insert one character at the beginning of the buffer:}
89 (goto-char (point-min))
96 ;; @r{@code{m1} is updated appropriately.}
98 @result{} #<marker at 101 in markers.texi>
102 ;; @r{Two markers that point to the same position}
103 ;; @r{are not @code{eq}, but they are @code{equal}.}
104 (setq m2 (copy-marker m1))
105 @result{} #<marker at 101 in markers.texi>
113 ;; @r{When you are finished using a marker, make it point nowhere.}
115 @result{} #<marker in no buffer>
119 @node Predicates on Markers
120 @section Predicates on Markers
122 You can test an object to see whether it is a marker, or whether it is
123 either an integer or a marker. The latter test is useful in connection
124 with the arithmetic functions that work with both markers and integers.
126 @defun markerp object
127 This function returns @code{t} if @var{object} is a marker, @code{nil}
128 otherwise. Note that integers are not markers, even though many
129 functions will accept either a marker or an integer.
132 @defun integer-or-marker-p object
133 This function returns @code{t} if @var{object} is an integer or a marker,
134 @code{nil} otherwise.
137 @defun number-or-marker-p object
138 This function returns @code{t} if @var{object} is a number (either
139 integer or floating point) or a marker, @code{nil} otherwise.
142 @node Creating Markers
143 @section Functions that Create Markers
145 When you create a new marker, you can make it point nowhere, or point
146 to the present position of point, or to the beginning or end of the
147 accessible portion of the buffer, or to the same place as another given
150 The next four functions all return markers with insertion type
151 @code{nil}. @xref{Marker Insertion Types}.
154 This function returns a newly created marker that does not point
160 @result{} #<marker in no buffer>
166 This function returns a new marker that points to the present position
167 of point in the current buffer. @xref{Point}. For an example, see
168 @code{copy-marker}, below.
171 @defun point-min-marker
172 This function returns a new marker that points to the beginning of the
173 accessible portion of the buffer. This will be the beginning of the
174 buffer unless narrowing is in effect. @xref{Narrowing}.
177 @defun point-max-marker
178 @cindex end of buffer marker
179 This function returns a new marker that points to the end of the
180 accessible portion of the buffer. This will be the end of the buffer
181 unless narrowing is in effect. @xref{Narrowing}.
183 Here are examples of this function and @code{point-min-marker}, shown in
184 a buffer containing a version of the source file for the text of this
190 @result{} #<marker at 1 in markers.texi>
192 @result{} #<marker at 15573 in markers.texi>
196 (narrow-to-region 100 200)
201 @result{} #<marker at 100 in markers.texi>
205 @result{} #<marker at 200 in markers.texi>
210 @defun copy-marker marker-or-integer &optional insertion-type
211 If passed a marker as its argument, @code{copy-marker} returns a
212 new marker that points to the same place and the same buffer as does
213 @var{marker-or-integer}. If passed an integer as its argument,
214 @code{copy-marker} returns a new marker that points to position
215 @var{marker-or-integer} in the current buffer.
217 The new marker's insertion type is specified by the argument
218 @var{insertion-type}. @xref{Marker Insertion Types}.
220 If passed an integer argument less than 1, @code{copy-marker} returns a
221 new marker that points to the beginning of the current buffer. If
222 passed an integer argument greater than the length of the buffer,
223 @code{copy-marker} returns a new marker that points to the end of the
229 @result{} #<marker at 1 in markers.texi>
234 @result{} #<marker at 7572 in markers.texi>
238 An error is signaled if @var{marker} is neither a marker nor an
242 Two distinct markers are considered @code{equal} (even though not
243 @code{eq}) to each other if they have the same position and buffer, or
244 if they both point nowhere.
248 (setq p (point-marker))
249 @result{} #<marker at 2139 in markers.texi>
253 (setq q (copy-marker p))
254 @result{} #<marker at 2139 in markers.texi>
268 @node Information from Markers
269 @section Information from Markers
271 This section describes the functions for accessing the components of a
274 @defun marker-position marker
275 This function returns the position that @var{marker} points to, or
276 @code{nil} if it points nowhere.
279 @defun marker-buffer marker
280 This function returns the buffer that @var{marker} points into, or
281 @code{nil} if it points nowhere.
285 (setq m (make-marker))
286 @result{} #<marker in no buffer>
298 (set-marker m 3770 (current-buffer))
299 @result{} #<marker at 3770 in markers.texi>
303 @result{} #<buffer markers.texi>
312 @defun buffer-has-markers-at position
313 @tindex buffer-has-markers-at
314 This function returns @code{t} if one or more markers
315 point at position @var{position} in the current buffer.
318 @node Marker Insertion Types
319 @section Marker Insertion Types
321 @cindex insertion type of a marker
322 When you insert text directly at the place where a marker points,
323 there are two possible ways to relocate that marker: it can point before
324 the inserted text, or point after it. You can specify which one a given
325 marker should do by setting its @dfn{insertion type}. Note that use of
326 @code{insert-before-markers} ignores markers' insertion types, always
327 relocating a marker to point after the inserted text.
329 Most functions that create markers, without explicitly specifying an
330 insertion type, create them with insertion type @code{nil}. Also, the
331 mark has, by default, insertion type @code{nil}.
333 @defun set-marker-insertion-type marker type
334 This function sets the insertion type of marker @var{marker} to
335 @var{type}. If @var{type} is @code{t}, @var{marker} will advance when
336 text is inserted at its position. If @var{type} is @code{nil},
337 @var{marker} does not advance when text is inserted there.
340 @defun marker-insertion-type marker
341 This function reports the current insertion type of @var{marker}.
345 @section Moving Marker Positions
347 This section describes how to change the position of an existing
348 marker. When you do this, be sure you know whether the marker is used
349 outside of your program, and, if so, what effects will result from
350 moving it---otherwise, confusing things may happen in other parts of
353 @defun set-marker marker position &optional buffer
354 This function moves @var{marker} to @var{position}
355 in @var{buffer}. If @var{buffer} is not provided, it defaults to
358 If @var{position} is less than 1, @code{set-marker} moves @var{marker}
359 to the beginning of the buffer. If @var{position} is greater than the
360 size of the buffer, @code{set-marker} moves marker to the end of the
361 buffer. If @var{position} is @code{nil} or a marker that points
362 nowhere, then @var{marker} is set to point nowhere.
364 The value returned is @var{marker}.
368 (setq m (point-marker))
369 @result{} #<marker at 4714 in markers.texi>
373 @result{} #<marker at 55 in markers.texi>
376 (setq b (get-buffer "foo"))
377 @result{} #<buffer foo>
381 @result{} #<marker at 1 in foo>
386 @defun move-marker marker position &optional buffer
387 This is another name for @code{set-marker}.
395 One special marker in each buffer is designated @dfn{the mark}. It
396 records a position for the user for the sake of commands such as
397 @code{kill-region} and @code{indent-rigidly}. Lisp programs should set
398 the mark only to values that have a potential use to the user, and never
399 for their own internal purposes. For example, the @code{replace-regexp}
400 command sets the mark to the value of point before doing any
401 replacements, because this enables the user to move back there
402 conveniently after the replace is finished.
404 Many commands are designed so that when called interactively they
405 operate on the text between point and the mark. If you are writing such
406 a command, don't examine the mark directly; instead, use
407 @code{interactive} with the @samp{r} specification. This provides the
408 values of point and the mark as arguments to the command in an
409 interactive call, but permits other Lisp programs to specify arguments
410 explicitly. @xref{Interactive Codes}.
412 Each buffer has its own value of the mark that is independent of the
413 value of the mark in other buffers. When a buffer is created, the mark
414 exists but does not point anywhere. We consider this state as ``the
415 absence of a mark in that buffer.''
417 Once the mark ``exists'' in a buffer, it normally never ceases to
418 exist. However, it may become @dfn{inactive}, if Transient Mark mode is
419 enabled. The variable @code{mark-active}, which is always buffer-local
420 in all buffers, indicates whether the mark is active: non-@code{nil}
421 means yes. A command can request deactivation of the mark upon return
422 to the editor command loop by setting @code{deactivate-mark} to a
423 non-@code{nil} value (but this causes deactivation only if Transient
424 Mark mode is enabled).
426 The main motivation for using Transient Mark mode is that this mode
427 also enables highlighting of the region when the mark is active.
430 In addition to the mark, each buffer has a @dfn{mark ring} which is a
431 list of markers containing previous values of the mark. When editing
432 commands change the mark, they should normally save the old value of the
433 mark on the mark ring. The variable @code{mark-ring-max} specifies the
434 maximum number of entries in the mark ring; once the list becomes this
435 long, adding a new element deletes the last element.
437 There is also a separate global mark ring, but that is used only in a
438 few particular user-level commands, and is not relevant to Lisp
439 programming. So we do not describe it here.
441 @defun mark &optional force
442 @cindex current buffer mark
443 This function returns the current buffer's mark position as an integer.
445 If Transient Mark mode is enabled, @code{mark-even-if-inactive} is
446 @code{nil} and and the mark is inactive, @code{mark} normally signals
447 an error. However, if @var{force} is non-@code{nil}, then @code{mark}
448 returns the mark position anyway---or @code{nil}, if the mark is not
449 yet set for this buffer.
453 This function returns the current buffer's mark. This is the very marker
454 that records the mark location inside Emacs, not a copy. Therefore,
455 changing this marker's position will directly affect the position of the mark.
456 Don't do it unless that is the effect you want.
460 (setq m (mark-marker))
461 @result{} #<marker at 3420 in markers.texi>
465 @result{} #<marker at 100 in markers.texi>
469 @result{} #<marker at 100 in markers.texi>
473 Like any marker, this marker can be set to point at any buffer you like.
474 We don't recommend that you make it point at any buffer other than the
475 one of which it is the mark. If you do, it will yield perfectly
476 consistent, but rather odd, results.
480 @deffn Command set-mark-command jump
481 If @var{jump} is @code{nil}, this command sets the mark to the value
482 of point and pushes the previous value of the mark on the mark ring. The
483 message @samp{Mark set} is also displayed in the echo area.
485 If @var{jump} is not @code{nil}, this command sets point to the value
486 of the mark, and sets the mark to the previous saved mark value, which
487 is popped off the mark ring.
489 This function is @emph{only} intended for interactive use.
493 @defun set-mark position
494 This function sets the mark to @var{position}, and activates the mark.
495 The old value of the mark is @emph{not} pushed onto the mark ring.
497 @strong{Please note:} Use this function only if you want the user to
498 see that the mark has moved, and you want the previous mark position to
499 be lost. Normally, when a new mark is set, the old one should go on the
500 @code{mark-ring}. For this reason, most applications should use
501 @code{push-mark} and @code{pop-mark}, not @code{set-mark}.
503 Novice Emacs Lisp programmers often try to use the mark for the wrong
504 purposes. The mark saves a location for the user's convenience. An
505 editing command should not alter the mark unless altering the mark is
506 part of the user-level functionality of the command. (And, in that
507 case, this effect should be documented.) To remember a location for
508 internal use in the Lisp program, store it in a Lisp variable. For
515 (delete-region beg (point))).
520 @c for interactive use only
522 @deffn Command exchange-point-and-mark
523 This function exchanges the positions of point and the mark.
524 It is intended for interactive use.
528 @defun push-mark &optional position nomsg activate
529 This function sets the current buffer's mark to @var{position}, and
530 pushes a copy of the previous mark onto @code{mark-ring}. If
531 @var{position} is @code{nil}, then the value of point is used.
532 @code{push-mark} returns @code{nil}.
534 The function @code{push-mark} normally @emph{does not} activate the
535 mark. To do that, specify @code{t} for the argument @var{activate}.
537 A @samp{Mark set} message is displayed unless @var{nomsg} is
542 This function pops off the top element of @code{mark-ring} and makes
543 that mark become the buffer's actual mark. This does not move point in
544 the buffer, and it does nothing if @code{mark-ring} is empty. It
545 deactivates the mark.
547 The return value is not meaningful.
550 @defopt transient-mark-mode
551 @cindex Transient Mark mode
552 This variable if non-@code{nil} enables Transient Mark mode, in which
553 every buffer-modifying primitive sets @code{deactivate-mark}. The
554 consequence of this is that commands that modify the buffer normally
555 make the mark inactive.
558 @defopt mark-even-if-inactive
559 If this is non-@code{nil}, Lisp programs and the Emacs user can use the
560 mark even when it is inactive. This option affects the behavior of
561 Transient Mark mode. When the option is non-@code{nil}, deactivation of
562 the mark turns off region highlighting, but commands that use the mark
563 behave as if the mark were still active.
566 @defvar deactivate-mark
567 If an editor command sets this variable non-@code{nil}, then the editor
568 command loop deactivates the mark after the command returns (if
569 Transient Mark mode is enabled). All the primitives that change the
570 buffer set @code{deactivate-mark}, to deactivate the mark when the
573 To write Lisp code that modifies the buffer without causing
574 deactivation of the mark at the end of the command, bind
575 @code{deactivate-mark} to @code{nil} around the code that does the
576 modification. For example:
579 (let (deactivate-mark)
584 @defun deactivate-mark
585 This function deactivates the mark, if Transient Mark mode is enabled.
586 Otherwise it does nothing.
590 The mark is active when this variable is non-@code{nil}. This variable
591 is always buffer-local in each buffer.
594 @defvar activate-mark-hook
595 @defvarx deactivate-mark-hook
596 These normal hooks are run, respectively, when the mark becomes active
597 and when it becomes inactive. The hook @code{activate-mark-hook} is
598 also run at the end of a command if the mark is active and it is
599 possible that the region may have changed.
603 The value of this buffer-local variable is the list of saved former
604 marks of the current buffer, most recent first.
609 @result{} (#<marker at 11050 in markers.texi>
610 #<marker at 10832 in markers.texi>
616 @defopt mark-ring-max
617 The value of this variable is the maximum size of @code{mark-ring}. If
618 more marks than this are pushed onto the @code{mark-ring},
619 @code{push-mark} discards an old mark when it adds a new one.
626 The text between point and the mark is known as @dfn{the region}.
627 Various functions operate on text delimited by point and the mark, but
628 only those functions specifically related to the region itself are
631 The next two functions signal an error if the mark does not point
632 anywhere. If Transient Mark mode is enabled and
633 @code{mark-even-if-inactive} is @code{nil}, they also signal an error
634 if the mark is inactive.
636 @defun region-beginning
637 This function returns the position of the beginning of the region (as
638 an integer). This is the position of either point or the mark,
639 whichever is smaller.
643 This function returns the position of the end of the region (as an
644 integer). This is the position of either point or the mark, whichever is
648 Few programs need to use the @code{region-beginning} and
649 @code{region-end} functions. A command designed to operate on a region
650 should normally use @code{interactive} with the @samp{r} specification
651 to find the beginning and end of the region. This lets other Lisp
652 programs specify the bounds explicitly as arguments. (@xref{Interactive
656 arch-tag: b1ba2e7a-a0f3-4c5e-875c-7d8e22d73299