2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-1999, 2001-2012 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions.
9 A @dfn{marker} is a Lisp object used to specify a position in a buffer
10 relative to the surrounding text. A marker changes its offset from the
11 beginning of the buffer automatically whenever text is inserted or
12 deleted, so that it stays with the two characters on either side of it.
15 * Overview of Markers:: The components of a marker, and how it relocates.
16 * Predicates on Markers:: Testing whether an object is a marker.
17 * Creating Markers:: Making empty markers or markers at certain places.
18 * Information from Markers:: Finding the marker's buffer or character position.
19 * Marker Insertion Types:: Two ways a marker can relocate when you
20 insert where it points.
21 * Moving Markers:: Moving the marker to a new buffer or position.
22 * The Mark:: How "the mark" is implemented with a marker.
23 * The Region:: How to access "the region".
26 @node Overview of Markers
27 @section Overview of Markers
29 A marker specifies a buffer and a position in that buffer. A
30 marker can be used to represent a position in functions that
31 require one, just as an integer could be used. In that case, the
32 marker's buffer is normally ignored. Of course, a marker used in this
33 way usually points to a position in the buffer that the function
34 operates on, but that is entirely the programmer's responsibility.
35 @xref{Positions}, for a complete description of positions.
37 A marker has three attributes: the marker position, the marker
38 buffer, and the insertion type. The marker position is an integer
39 that is equivalent (at a given time) to the marker as a position in
40 that buffer. But the marker's position value can change during
41 the life of the marker, and often does. Insertion and deletion of
42 text in the buffer relocate the marker. The idea is that a marker
43 positioned between two characters remains between those two characters
44 despite insertion and deletion elsewhere in the buffer. Relocation
45 changes the integer equivalent of the marker.
47 @cindex marker relocation
48 Deleting text around a marker's position leaves the marker between the
49 characters immediately before and after the deleted text. Inserting
50 text at the position of a marker normally leaves the marker either in
51 front of or after the new text, depending on the marker's @dfn{insertion
52 type} (@pxref{Marker Insertion Types})---unless the insertion is done
53 with @code{insert-before-markers} (@pxref{Insertion}).
55 @cindex marker garbage collection
56 Insertion and deletion in a buffer must check all the markers and
57 relocate them if necessary. This slows processing in a buffer with a
58 large number of markers. For this reason, it is a good idea to make a
59 marker point nowhere if you are sure you don't need it any more.
60 Markers that can no longer be accessed are eventually removed
61 (@pxref{Garbage Collection}).
63 @cindex markers as numbers
64 Because it is common to perform arithmetic operations on a marker
65 position, most of these operations (including @code{+} and
66 @code{-}) accept markers as arguments. In such cases, the marker
67 stands for its current position.
69 Here are examples of creating markers, setting markers, and moving point
74 ;; @r{Make a new marker that initially does not point anywhere:}
75 (setq m1 (make-marker))
76 @result{} #<marker in no buffer>
80 ;; @r{Set @code{m1} to point between the 99th and 100th characters}
81 ;; @r{in the current buffer:}
83 @result{} #<marker at 100 in markers.texi>
87 ;; @r{Now insert one character at the beginning of the buffer:}
88 (goto-char (point-min))
95 ;; @r{@code{m1} is updated appropriately.}
97 @result{} #<marker at 101 in markers.texi>
101 ;; @r{Two markers that point to the same position}
102 ;; @r{are not @code{eq}, but they are @code{equal}.}
103 (setq m2 (copy-marker m1))
104 @result{} #<marker at 101 in markers.texi>
112 ;; @r{When you are finished using a marker, make it point nowhere.}
114 @result{} #<marker in no buffer>
118 @node Predicates on Markers
119 @section Predicates on Markers
121 You can test an object to see whether it is a marker, or whether it is
122 either an integer or a marker. The latter test is useful in connection
123 with the arithmetic functions that work with both markers and integers.
125 @defun markerp object
126 This function returns @code{t} if @var{object} is a marker, @code{nil}
127 otherwise. Note that integers are not markers, even though many
128 functions will accept either a marker or an integer.
131 @defun integer-or-marker-p object
132 This function returns @code{t} if @var{object} is an integer or a marker,
133 @code{nil} otherwise.
136 @defun number-or-marker-p object
137 This function returns @code{t} if @var{object} is a number (either
138 integer or floating point) or a marker, @code{nil} otherwise.
141 @node Creating Markers
142 @section Functions that Create Markers
144 When you create a new marker, you can make it point nowhere, or point
145 to the present position of point, or to the beginning or end of the
146 accessible portion of the buffer, or to the same place as another given
149 The next four functions all return markers with insertion type
150 @code{nil}. @xref{Marker Insertion Types}.
153 This function returns a newly created marker that does not point
159 @result{} #<marker in no buffer>
165 This function returns a new marker that points to the present position
166 of point in the current buffer. @xref{Point}. For an example, see
167 @code{copy-marker}, below.
170 @defun point-min-marker
171 This function returns a new marker that points to the beginning of the
172 accessible portion of the buffer. This will be the beginning of the
173 buffer unless narrowing is in effect. @xref{Narrowing}.
176 @defun point-max-marker
177 This function returns a new marker that points to the end of the
178 accessible portion of the buffer. This will be the end of the buffer
179 unless narrowing is in effect. @xref{Narrowing}.
181 Here are examples of this function and @code{point-min-marker}, shown in
182 a buffer containing a version of the source file for the text of this
188 @result{} #<marker at 1 in markers.texi>
190 @result{} #<marker at 24080 in markers.texi>
194 (narrow-to-region 100 200)
199 @result{} #<marker at 100 in markers.texi>
203 @result{} #<marker at 200 in markers.texi>
208 @defun copy-marker &optional marker-or-integer insertion-type
209 If passed a marker as its argument, @code{copy-marker} returns a
210 new marker that points to the same place and the same buffer as does
211 @var{marker-or-integer}. If passed an integer as its argument,
212 @code{copy-marker} returns a new marker that points to position
213 @var{marker-or-integer} in the current buffer.
215 The new marker's insertion type is specified by the argument
216 @var{insertion-type}. @xref{Marker Insertion Types}.
218 If passed an integer argument less than 1, @code{copy-marker} returns a
219 new marker that points to the beginning of the current buffer. If
220 passed an integer argument greater than the length of the buffer,
221 @code{copy-marker} returns a new marker that points to the end of the
227 @result{} #<marker at 1 in markers.texi>
232 @result{} #<marker at 24080 in markers.texi>
236 An error is signaled if @var{marker} is neither a marker nor an
240 Two distinct markers are considered @code{equal} (even though not
241 @code{eq}) to each other if they have the same position and buffer, or
242 if they both point nowhere.
246 (setq p (point-marker))
247 @result{} #<marker at 2139 in markers.texi>
251 (setq q (copy-marker p))
252 @result{} #<marker at 2139 in markers.texi>
266 @node Information from Markers
267 @section Information from Markers
269 This section describes the functions for accessing the components of a
272 @defun marker-position marker
273 This function returns the position that @var{marker} points to, or
274 @code{nil} if it points nowhere.
277 @defun marker-buffer marker
278 This function returns the buffer that @var{marker} points into, or
279 @code{nil} if it points nowhere.
283 (setq m (make-marker))
284 @result{} #<marker in no buffer>
296 (set-marker m 3770 (current-buffer))
297 @result{} #<marker at 3770 in markers.texi>
301 @result{} #<buffer markers.texi>
310 @node Marker Insertion Types
311 @section Marker Insertion Types
313 @cindex insertion type of a marker
314 When you insert text directly at the place where a marker points,
315 there are two possible ways to relocate that marker: it can point before
316 the inserted text, or point after it. You can specify which one a given
317 marker should do by setting its @dfn{insertion type}. Note that use of
318 @code{insert-before-markers} ignores markers' insertion types, always
319 relocating a marker to point after the inserted text.
321 @defun set-marker-insertion-type marker type
322 This function sets the insertion type of marker @var{marker} to
323 @var{type}. If @var{type} is @code{t}, @var{marker} will advance when
324 text is inserted at its position. If @var{type} is @code{nil},
325 @var{marker} does not advance when text is inserted there.
328 @defun marker-insertion-type marker
329 This function reports the current insertion type of @var{marker}.
332 Most functions that create markers, without an argument allowing to
333 specify the insertion type, create them with insertion type
334 @code{nil}. Also, the mark has, by default, insertion type
338 @section Moving Marker Positions
340 This section describes how to change the position of an existing
341 marker. When you do this, be sure you know whether the marker is used
342 outside of your program, and, if so, what effects will result from
343 moving it---otherwise, confusing things may happen in other parts of
346 @defun set-marker marker position &optional buffer
347 This function moves @var{marker} to @var{position}
348 in @var{buffer}. If @var{buffer} is not provided, it defaults to
351 If @var{position} is less than 1, @code{set-marker} moves @var{marker}
352 to the beginning of the buffer. If @var{position} is greater than the
353 size of the buffer (@pxref{Point}), @code{set-marker} moves marker to
354 the end of the buffer. If @var{position} is @code{nil} or a marker
355 that points nowhere, then @var{marker} is set to point nowhere.
357 The value returned is @var{marker}.
361 (setq m (point-marker))
362 @result{} #<marker at 4714 in markers.texi>
366 @result{} #<marker at 55 in markers.texi>
369 (setq b (get-buffer "foo"))
370 @result{} #<buffer foo>
374 @result{} #<marker at 1 in foo>
379 @defun move-marker marker position &optional buffer
380 This is another name for @code{set-marker}.
388 Each buffer has a special marker, which is designated @dfn{the
389 mark}. When a buffer is newly created, this marker exists but does
390 not point anywhere; this means that the mark ``doesn't exist'' in that
391 buffer yet. Subsequent commands can set the mark.
393 The mark specifies a position to bound a range of text for many
394 commands, such as @code{kill-region} and @code{indent-rigidly}. These
395 commands typically act on the text between point and the mark, which
396 is called the @dfn{region}. If you are writing a command that
397 operates on the region, don't examine the mark directly; instead, use
398 @code{interactive} with the @samp{r} specification. This provides the
399 values of point and the mark as arguments to the command in an
400 interactive call, but permits other Lisp programs to specify arguments
401 explicitly. @xref{Interactive Codes}.
403 Some commands set the mark as a side-effect. Commands should do
404 this only if it has a potential use to the user, and never for their
405 own internal purposes. For example, the @code{replace-regexp} command
406 sets the mark to the value of point before doing any replacements,
407 because this enables the user to move back there conveniently after
408 the replace is finished.
410 Once the mark ``exists'' in a buffer, it normally never ceases to
411 exist. However, it may become @dfn{inactive}, if Transient Mark mode
412 is enabled. The buffer-local variable @code{mark-active}, if
413 non-@code{nil}, means that the mark is active. A command can call the
414 function @code{deactivate-mark} to deactivate the mark directly, or it
415 can request deactivation of the mark upon return to the editor command
416 loop by setting the variable @code{deactivate-mark} to a
417 non-@code{nil} value.
419 If Transient Mark mode is enabled, certain editing commands that
420 normally apply to text near point, apply instead to the region when
421 the mark is active. This is the main motivation for using Transient
422 Mark mode. (Another is that this enables highlighting of the region
423 when the mark is active. @xref{Display}.)
425 In addition to the mark, each buffer has a @dfn{mark ring} which is a
426 list of markers containing previous values of the mark. When editing
427 commands change the mark, they should normally save the old value of the
428 mark on the mark ring. The variable @code{mark-ring-max} specifies the
429 maximum number of entries in the mark ring; once the list becomes this
430 long, adding a new element deletes the last element.
432 There is also a separate global mark ring, but that is used only in a
433 few particular user-level commands, and is not relevant to Lisp
434 programming. So we do not describe it here.
436 @defun mark &optional force
437 @cindex current buffer mark
438 This function returns the current buffer's mark position as an integer,
439 or @code{nil} if no mark has ever been set in this buffer.
441 If Transient Mark mode is enabled, and @code{mark-even-if-inactive} is
442 @code{nil}, @code{mark} signals an error if the mark is inactive.
443 However, if @var{force} is non-@code{nil}, then @code{mark} disregards
444 inactivity of the mark, and returns the mark position (or @code{nil})
449 This function returns the marker that represents the current buffer's
450 mark. It is not a copy, it is the marker used internally. Therefore,
451 changing this marker's position will directly affect the buffer's
452 mark. Don't do that unless that is the effect you want.
456 (setq m (mark-marker))
457 @result{} #<marker at 3420 in markers.texi>
461 @result{} #<marker at 100 in markers.texi>
465 @result{} #<marker at 100 in markers.texi>
469 Like any marker, this marker can be set to point at any buffer you
470 like. If you make it point at any buffer other than the one of which
471 it is the mark, it will yield perfectly consistent, but rather odd,
472 results. We recommend that you not do it!
475 @defun set-mark position
476 This function sets the mark to @var{position}, and activates the mark.
477 The old value of the mark is @emph{not} pushed onto the mark ring.
479 @strong{Please note:} Use this function only if you want the user to
480 see that the mark has moved, and you want the previous mark position to
481 be lost. Normally, when a new mark is set, the old one should go on the
482 @code{mark-ring}. For this reason, most applications should use
483 @code{push-mark} and @code{pop-mark}, not @code{set-mark}.
485 Novice Emacs Lisp programmers often try to use the mark for the wrong
486 purposes. The mark saves a location for the user's convenience. An
487 editing command should not alter the mark unless altering the mark is
488 part of the user-level functionality of the command. (And, in that
489 case, this effect should be documented.) To remember a location for
490 internal use in the Lisp program, store it in a Lisp variable. For
497 (delete-region beg (point))).
502 @defun push-mark &optional position nomsg activate
503 This function sets the current buffer's mark to @var{position}, and
504 pushes a copy of the previous mark onto @code{mark-ring}. If
505 @var{position} is @code{nil}, then the value of point is used.
506 @c Doesn't seem relevant.
507 @c @code{push-mark} returns @code{nil}.
509 The function @code{push-mark} normally @emph{does not} activate the
510 mark. To do that, specify @code{t} for the argument @var{activate}.
512 A @samp{Mark set} message is displayed unless @var{nomsg} is
517 This function pops off the top element of @code{mark-ring} and makes
518 that mark become the buffer's actual mark. This does not move point in
519 the buffer, and it does nothing if @code{mark-ring} is empty. It
520 deactivates the mark.
522 @c Seems even less relevant.
523 @c The return value is not meaningful.
526 @defopt transient-mark-mode
527 This variable, if non-@code{nil}, enables Transient Mark mode. In
528 Transient Mark mode, every buffer-modifying primitive sets
529 @code{deactivate-mark}. As a consequence, most commands that modify
530 the buffer also deactivate the mark.
532 When Transient Mark mode is enabled and the mark is active, many
533 commands that normally apply to the text near point instead apply to
534 the region. Such commands should use the function @code{use-region-p}
535 to test whether they should operate on the region. @xref{The Region}.
537 Lisp programs can set @code{transient-mark-mode} to non-@code{nil},
538 non-@code{t} values to enable Transient Mark mode temporarily. If the
539 value is @code{lambda}, Transient Mark mode is automatically turned
540 off after any action, such as buffer modification, that would normally
541 deactivate the mark. If the value is @w{@code{(only . @var{oldval})}},
542 then @code{transient-mark-mode} is set to the value @var{oldval} after
543 any subsequent command that moves point and is not shift-translated
544 (@pxref{Key Sequence Input, shift-translation}), or after any other
545 action that would normally deactivate the mark.
548 @defopt mark-even-if-inactive
549 If this is non-@code{nil}, Lisp programs and the Emacs user can use the
550 mark even when it is inactive. This option affects the behavior of
551 Transient Mark mode. When the option is non-@code{nil}, deactivation of
552 the mark turns off region highlighting, but commands that use the mark
553 behave as if the mark were still active.
556 @defvar deactivate-mark
557 If an editor command sets this variable non-@code{nil}, then the editor
558 command loop deactivates the mark after the command returns (if
559 Transient Mark mode is enabled). All the primitives that change the
560 buffer set @code{deactivate-mark}, to deactivate the mark when the
563 To write Lisp code that modifies the buffer without causing
564 deactivation of the mark at the end of the command, bind
565 @code{deactivate-mark} to @code{nil} around the code that does the
566 modification. For example:
569 (let (deactivate-mark)
574 @defun deactivate-mark &optional force
575 If Transient Mark mode is enabled or @var{force} is non-@code{nil},
576 this function deactivates the mark and runs the normal hook
577 @code{deactivate-mark-hook}. Otherwise, it does nothing.
581 The mark is active when this variable is non-@code{nil}. This
582 variable is always buffer-local in each buffer. Do @emph{not} use the
583 value of this variable to decide whether a command that normally
584 operates on text near point should operate on the region instead. Use
585 the function @code{use-region-p} for that (@pxref{The Region}).
588 @defvar activate-mark-hook
589 @defvarx deactivate-mark-hook
590 These normal hooks are run, respectively, when the mark becomes active
591 and when it becomes inactive. The hook @code{activate-mark-hook} is
592 also run at the end of the command loop if the mark is active and it
593 is possible that the region may have changed.
595 This piece of command_loop_1, run unless deactivating the mark:
596 if (current_buffer != prev_buffer || MODIFF != prev_modiff)
598 Lisp_Object hook = intern ("activate-mark-hook");
599 Frun_hooks (1, &hook);
604 @defun handle-shift-selection
605 This function implements the ``shift-selection'' behavior of
606 point-motion commands. @xref{Shift Selection,,, emacs, The GNU Emacs
607 Manual}. It is called automatically by the Emacs command loop
608 whenever a command with a @samp{^} character in its @code{interactive}
609 spec is invoked, before the command itself is executed
610 (@pxref{Interactive Codes, ^}).
612 If @code{shift-select-mode} is non-@code{nil} and the current command
613 was invoked via shift translation (@pxref{Key Sequence Input,
614 shift-translation}), this function sets the mark and temporarily
615 activates the region, unless the region was already temporarily
616 activated in this way. Otherwise, if the region has been activated
617 temporarily, it deactivates the mark and restores the variable
618 @code{transient-mark-mode} to its earlier value.
622 The value of this buffer-local variable is the list of saved former
623 marks of the current buffer, most recent first.
628 @result{} (#<marker at 11050 in markers.texi>
629 #<marker at 10832 in markers.texi>
635 @defopt mark-ring-max
636 The value of this variable is the maximum size of @code{mark-ring}. If
637 more marks than this are pushed onto the @code{mark-ring},
638 @code{push-mark} discards an old mark when it adds a new one.
641 @c There is also global-mark-ring-max, but this chapter explicitly
642 @c does not talk about the global mark.
646 @cindex region (between point and mark)
648 The text between point and the mark is known as @dfn{the region}.
649 Various functions operate on text delimited by point and the mark, but
650 only those functions specifically related to the region itself are
653 The next two functions signal an error if the mark does not point
654 anywhere. If Transient Mark mode is enabled and
655 @code{mark-even-if-inactive} is @code{nil}, they also signal an error
656 if the mark is inactive.
658 @defun region-beginning
659 This function returns the position of the beginning of the region (as
660 an integer). This is the position of either point or the mark,
661 whichever is smaller.
665 This function returns the position of the end of the region (as an
666 integer). This is the position of either point or the mark, whichever is
670 Instead of using @code{region-beginning} and @code{region-end}, a
671 command designed to operate on a region should normally use
672 @code{interactive} with the @samp{r} specification to find the
673 beginning and end of the region. This lets other Lisp programs
674 specify the bounds explicitly as arguments. @xref{Interactive Codes}.
677 This function returns @code{t} if Transient Mark mode is enabled, the
678 mark is active, and there is a valid region in the buffer. This
679 function is intended to be used by commands that operate on the
680 region, instead of on text near point, when the mark is active.
682 A region is valid if it has a non-zero size, or if the user option
683 @code{use-empty-active-region} is non-@code{nil} (by default, it is
684 @code{nil}). The function @code{region-active-p} is similar to
685 @code{use-region-p}, but considers all regions as valid. In most
686 cases, you should not use @code{region-active-p}, since if the region
687 is empty it is often more appropriate to operate on point.