]> code.delx.au - gnu-emacs/blob - doc/lispref/markers.texi
Fix EDE security flaw involving loading arbitrary Lisp from Project.ede.
[gnu-emacs] / doc / lispref / markers.texi
1 @c -*-texinfo-*-
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.
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
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.
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.
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.
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
31 marker can be used to represent a position in the functions that
32 require one, just as an integer could be used. In that case, the
33 marker's buffer is normally ignored. Of course, a marker used in this
34 way usually points to a position in the buffer that the function
35 operates on, but that is entirely the programmer's responsibility.
36 @xref{Positions}, for a complete description of positions.
37
38 A marker has three attributes: the marker position, the marker
39 buffer, and the insertion type. The marker position is an integer
40 that is equivalent (at a given time) to the marker as a position in
41 that buffer. But the marker's position value can change often during
42 the life of the marker. Insertion and deletion of text in the buffer
43 relocate the marker. The idea is that a marker positioned between two
44 characters remains between those two characters despite insertion and
45 deletion elsewhere in the buffer. Relocation changes the integer
46 equivalent of the marker.
47
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}).
55
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.
63
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.
69
70 Here are examples of creating markers, setting markers, and moving point
71 to markers:
72
73 @example
74 @group
75 ;; @r{Make a new marker that initially does not point anywhere:}
76 (setq m1 (make-marker))
77 @result{} #<marker in no buffer>
78 @end group
79
80 @group
81 ;; @r{Set @code{m1} to point between the 99th and 100th characters}
82 ;; @r{in the current buffer:}
83 (set-marker m1 100)
84 @result{} #<marker at 100 in markers.texi>
85 @end group
86
87 @group
88 ;; @r{Now insert one character at the beginning of the buffer:}
89 (goto-char (point-min))
90 @result{} 1
91 (insert "Q")
92 @result{} nil
93 @end group
94
95 @group
96 ;; @r{@code{m1} is updated appropriately.}
97 m1
98 @result{} #<marker at 101 in markers.texi>
99 @end group
100
101 @group
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>
106 (eq m1 m2)
107 @result{} nil
108 (equal m1 m2)
109 @result{} t
110 @end group
111
112 @group
113 ;; @r{When you are finished using a marker, make it point nowhere.}
114 (set-marker m1 nil)
115 @result{} #<marker in no buffer>
116 @end group
117 @end example
118
119 @node Predicates on Markers
120 @section Predicates on Markers
121
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.
125
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.
130 @end defun
131
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.
135 @end defun
136
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.
140 @end defun
141
142 @node Creating Markers
143 @section Functions that Create Markers
144
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
148 marker.
149
150 The next four functions all return markers with insertion type
151 @code{nil}. @xref{Marker Insertion Types}.
152
153 @defun make-marker
154 This function returns a newly created marker that does not point
155 anywhere.
156
157 @example
158 @group
159 (make-marker)
160 @result{} #<marker in no buffer>
161 @end group
162 @end example
163 @end defun
164
165 @defun point-marker
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.
169 @end defun
170
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}.
175 @end defun
176
177 @defun point-max-marker
178 This function returns a new marker that points to the end of the
179 accessible portion of the buffer. This will be the end of the buffer
180 unless narrowing is in effect. @xref{Narrowing}.
181
182 Here are examples of this function and @code{point-min-marker}, shown in
183 a buffer containing a version of the source file for the text of this
184 chapter.
185
186 @example
187 @group
188 (point-min-marker)
189 @result{} #<marker at 1 in markers.texi>
190 (point-max-marker)
191 @result{} #<marker at 15573 in markers.texi>
192 @end group
193
194 @group
195 (narrow-to-region 100 200)
196 @result{} nil
197 @end group
198 @group
199 (point-min-marker)
200 @result{} #<marker at 100 in markers.texi>
201 @end group
202 @group
203 (point-max-marker)
204 @result{} #<marker at 200 in markers.texi>
205 @end group
206 @end example
207 @end defun
208
209 @defun copy-marker marker-or-integer &optional insertion-type
210 If passed a marker as its argument, @code{copy-marker} returns a
211 new marker that points to the same place and the same buffer as does
212 @var{marker-or-integer}. If passed an integer as its argument,
213 @code{copy-marker} returns a new marker that points to position
214 @var{marker-or-integer} in the current buffer.
215
216 The new marker's insertion type is specified by the argument
217 @var{insertion-type}. @xref{Marker Insertion Types}.
218
219 If passed an integer argument less than 1, @code{copy-marker} returns a
220 new marker that points to the beginning of the current buffer. If
221 passed an integer argument greater than the length of the buffer,
222 @code{copy-marker} returns a new marker that points to the end of the
223 buffer.
224
225 @example
226 @group
227 (copy-marker 0)
228 @result{} #<marker at 1 in markers.texi>
229 @end group
230
231 @group
232 (copy-marker 20000)
233 @result{} #<marker at 7572 in markers.texi>
234 @end group
235 @end example
236
237 An error is signaled if @var{marker} is neither a marker nor an
238 integer.
239 @end defun
240
241 Two distinct markers are considered @code{equal} (even though not
242 @code{eq}) to each other if they have the same position and buffer, or
243 if they both point nowhere.
244
245 @example
246 @group
247 (setq p (point-marker))
248 @result{} #<marker at 2139 in markers.texi>
249 @end group
250
251 @group
252 (setq q (copy-marker p))
253 @result{} #<marker at 2139 in markers.texi>
254 @end group
255
256 @group
257 (eq p q)
258 @result{} nil
259 @end group
260
261 @group
262 (equal p q)
263 @result{} t
264 @end group
265 @end example
266
267 @node Information from Markers
268 @section Information from Markers
269
270 This section describes the functions for accessing the components of a
271 marker object.
272
273 @defun marker-position marker
274 This function returns the position that @var{marker} points to, or
275 @code{nil} if it points nowhere.
276 @end defun
277
278 @defun marker-buffer marker
279 This function returns the buffer that @var{marker} points into, or
280 @code{nil} if it points nowhere.
281
282 @example
283 @group
284 (setq m (make-marker))
285 @result{} #<marker in no buffer>
286 @end group
287 @group
288 (marker-position m)
289 @result{} nil
290 @end group
291 @group
292 (marker-buffer m)
293 @result{} nil
294 @end group
295
296 @group
297 (set-marker m 3770 (current-buffer))
298 @result{} #<marker at 3770 in markers.texi>
299 @end group
300 @group
301 (marker-buffer m)
302 @result{} #<buffer markers.texi>
303 @end group
304 @group
305 (marker-position m)
306 @result{} 3770
307 @end group
308 @end example
309 @end defun
310
311 @defun buffer-has-markers-at position
312 This function returns @code{t} if one or more markers
313 point at position @var{position} in the current buffer.
314 @end defun
315
316 @node Marker Insertion Types
317 @section Marker Insertion Types
318
319 @cindex insertion type of a marker
320 When you insert text directly at the place where a marker points,
321 there are two possible ways to relocate that marker: it can point before
322 the inserted text, or point after it. You can specify which one a given
323 marker should do by setting its @dfn{insertion type}. Note that use of
324 @code{insert-before-markers} ignores markers' insertion types, always
325 relocating a marker to point after the inserted text.
326
327 @defun set-marker-insertion-type marker type
328 This function sets the insertion type of marker @var{marker} to
329 @var{type}. If @var{type} is @code{t}, @var{marker} will advance when
330 text is inserted at its position. If @var{type} is @code{nil},
331 @var{marker} does not advance when text is inserted there.
332 @end defun
333
334 @defun marker-insertion-type marker
335 This function reports the current insertion type of @var{marker}.
336 @end defun
337
338 Most functions that create markers, without an argument allowing to
339 specify the insertion type, create them with insertion type
340 @code{nil}. Also, the mark has, by default, insertion type
341 @code{nil}.
342
343 @node Moving Markers
344 @section Moving Marker Positions
345
346 This section describes how to change the position of an existing
347 marker. When you do this, be sure you know whether the marker is used
348 outside of your program, and, if so, what effects will result from
349 moving it---otherwise, confusing things may happen in other parts of
350 Emacs.
351
352 @defun set-marker marker position &optional buffer
353 This function moves @var{marker} to @var{position}
354 in @var{buffer}. If @var{buffer} is not provided, it defaults to
355 the current buffer.
356
357 If @var{position} is less than 1, @code{set-marker} moves @var{marker}
358 to the beginning of the buffer. If @var{position} is greater than the
359 size of the buffer, @code{set-marker} moves marker to the end of the
360 buffer. If @var{position} is @code{nil} or a marker that points
361 nowhere, then @var{marker} is set to point nowhere.
362
363 The value returned is @var{marker}.
364
365 @example
366 @group
367 (setq m (point-marker))
368 @result{} #<marker at 4714 in markers.texi>
369 @end group
370 @group
371 (set-marker m 55)
372 @result{} #<marker at 55 in markers.texi>
373 @end group
374 @group
375 (setq b (get-buffer "foo"))
376 @result{} #<buffer foo>
377 @end group
378 @group
379 (set-marker m 0 b)
380 @result{} #<marker at 1 in foo>
381 @end group
382 @end example
383 @end defun
384
385 @defun move-marker marker position &optional buffer
386 This is another name for @code{set-marker}.
387 @end defun
388
389 @node The Mark
390 @section The Mark
391 @cindex mark, the
392 @cindex mark ring
393
394 Each buffer has a special marker, which is designated @dfn{the
395 mark}. When a buffer is newly created, this marker exists but does
396 not point anywhere; this means that the mark ``doesn't exist'' in that
397 buffer yet. Subsequent commands can set the mark.
398
399 The mark specifies a position to bound a range of text for many
400 commands, such as @code{kill-region} and @code{indent-rigidly}. These
401 commands typically act on the text between point and the mark, which
402 is called the @dfn{region}. If you are writing a command that
403 operates on the region, don't examine the mark directly; instead, use
404 @code{interactive} with the @samp{r} specification. This provides the
405 values of point and the mark as arguments to the command in an
406 interactive call, but permits other Lisp programs to specify arguments
407 explicitly. @xref{Interactive Codes}.
408
409 Some commands set the mark as a side-effect. Commands should do
410 this only if it has a potential use to the user, and never for their
411 own internal purposes. For example, the @code{replace-regexp} command
412 sets the mark to the value of point before doing any replacements,
413 because this enables the user to move back there conveniently after
414 the replace is finished.
415
416 Once the mark ``exists'' in a buffer, it normally never ceases to
417 exist. However, it may become @dfn{inactive}, if Transient Mark mode
418 is enabled. The buffer-local variable @code{mark-active}, if
419 non-@code{nil}, means that the mark is active. A command can call the
420 function @code{deactivate-mark} to deactivate the mark directly, or it
421 can request deactivation of the mark upon return to the editor command
422 loop by setting the variable @code{deactivate-mark} to a
423 non-@code{nil} value.
424
425 If Transient Mode is enabled, certain editing commands that normally
426 apply to text near point, apply instead to the region when the mark is
427 active. This is the main motivation for using Transient Mark mode.
428 (Another is that this enables highlighting of the region when the mark
429 is active. @xref{Display}.)
430
431 In addition to the mark, each buffer has a @dfn{mark ring} which is a
432 list of markers containing previous values of the mark. When editing
433 commands change the mark, they should normally save the old value of the
434 mark on the mark ring. The variable @code{mark-ring-max} specifies the
435 maximum number of entries in the mark ring; once the list becomes this
436 long, adding a new element deletes the last element.
437
438 There is also a separate global mark ring, but that is used only in a
439 few particular user-level commands, and is not relevant to Lisp
440 programming. So we do not describe it here.
441
442 @defun mark &optional force
443 @cindex current buffer mark
444 This function returns the current buffer's mark position as an integer,
445 or @code{nil} if no mark has ever been set in this buffer.
446
447 If 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.
449 However, if @var{force} is non-@code{nil}, then @code{mark} disregards
450 inactivity of the mark, and returns the mark position (or @code{nil})
451 anyway.
452 @end defun
453
454 @defun mark-marker
455 This function returns the marker that represents the current buffer's
456 mark. It is not a copy, it is the marker used internally. Therefore,
457 changing this marker's position will directly affect the buffer's
458 mark. Don't do that 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
475 Like any marker, this marker can be set to point at any buffer you
476 like. If you make it point at any buffer other than the one of which
477 it is the mark, it will yield perfectly consistent, but rather odd,
478 results. We recommend that you not do it!
479 @end defun
480
481 @defun set-mark position
482 This function sets the mark to @var{position}, and activates the mark.
483 The old value of the mark is @emph{not} pushed onto the mark ring.
484
485 @strong{Please note:} Use this function only if you want the user to
486 see that the mark has moved, and you want the previous mark position to
487 be lost. Normally, when a new mark is set, the old one should go on the
488 @code{mark-ring}. For this reason, most applications should use
489 @code{push-mark} and @code{pop-mark}, not @code{set-mark}.
490
491 Novice Emacs Lisp programmers often try to use the mark for the wrong
492 purposes. The mark saves a location for the user's convenience. An
493 editing command should not alter the mark unless altering the mark is
494 part of the user-level functionality of the command. (And, in that
495 case, this effect should be documented.) To remember a location for
496 internal use in the Lisp program, store it in a Lisp variable. For
497 example:
498
499 @example
500 @group
501 (let ((beg (point)))
502 (forward-line 1)
503 (delete-region beg (point))).
504 @end group
505 @end example
506 @end defun
507
508 @defun push-mark &optional position nomsg activate
509 This function sets the current buffer's mark to @var{position}, and
510 pushes 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
514 The function @code{push-mark} normally @emph{does not} activate the
515 mark. To do that, specify @code{t} for the argument @var{activate}.
516
517 A @samp{Mark set} message is displayed unless @var{nomsg} is
518 non-@code{nil}.
519 @end defun
520
521 @defun pop-mark
522 This function pops off the top element of @code{mark-ring} and makes
523 that mark become the buffer's actual mark. This does not move point in
524 the buffer, and it does nothing if @code{mark-ring} is empty. It
525 deactivates the mark.
526
527 The return value is not meaningful.
528 @end defun
529
530 @defopt transient-mark-mode
531 This variable, if non-@code{nil}, enables Transient Mark mode. In
532 Transient Mark mode, every buffer-modifying primitive sets
533 @code{deactivate-mark}. As a consequence, most commands that modify
534 the buffer also deactivate the mark.
535
536 When Transient Mark mode is enabled and the mark is active, many
537 commands that normally apply to the text near point instead apply to
538 the region. Such commands should use the function @code{use-region-p}
539 to test whether they should operate on the region. @xref{The Region}.
540
541 Lisp programs can set @code{transient-mark-mode} to non-@code{nil},
542 non-@code{t} values to enable Transient Mark mode temporarily. If the
543 value is @code{lambda}, Transient Mark mode is automatically turned
544 off after any action, such as buffer modification, that would normally
545 deactivate the mark. If the value is @w{@code{(only . @var{oldval})}},
546 then @code{transient-mark-mode} is set to the value @var{oldval} after
547 any subsequent command that moves point and is not shift-translated
548 (@pxref{Key Sequence Input, shift-translation}), or after any other
549 action that would normally deactivate the mark.
550 @end defopt
551
552 @defopt mark-even-if-inactive
553 If this is non-@code{nil}, Lisp programs and the Emacs user can use the
554 mark even when it is inactive. This option affects the behavior of
555 Transient Mark mode. When the option is non-@code{nil}, deactivation of
556 the mark turns off region highlighting, but commands that use the mark
557 behave as if the mark were still active.
558 @end defopt
559
560 @defvar deactivate-mark
561 If an editor command sets this variable non-@code{nil}, then the editor
562 command loop deactivates the mark after the command returns (if
563 Transient Mark mode is enabled). All the primitives that change the
564 buffer set @code{deactivate-mark}, to deactivate the mark when the
565 command is finished.
566
567 To write Lisp code that modifies the buffer without causing
568 deactivation of the mark at the end of the command, bind
569 @code{deactivate-mark} to @code{nil} around the code that does the
570 modification. For example:
571
572 @example
573 (let (deactivate-mark)
574 (insert " "))
575 @end example
576 @end defvar
577
578 @defun deactivate-mark &optional force
579 If Transient Mark mode is enabled or @var{force} is non-@code{nil},
580 this function deactivates the mark and runs the normal hook
581 @code{deactivate-mark-hook}. Otherwise, it does nothing.
582 @end defun
583
584 @defvar mark-active
585 The mark is active when this variable is non-@code{nil}. This
586 variable is always buffer-local in each buffer. Do @emph{not} use the
587 value of this variable to decide whether a command that normally
588 operates on text near point should operate on the region instead. Use
589 the function @code{use-region-p} for that (@pxref{The Region}).
590 @end defvar
591
592 @defvar activate-mark-hook
593 @defvarx deactivate-mark-hook
594 These normal hooks are run, respectively, when the mark becomes active
595 and when it becomes inactive. The hook @code{activate-mark-hook} is
596 also run at the end of a command if the mark is active and it is
597 possible that the region may have changed.
598 @end defvar
599
600 @defun handle-shift-selection
601 This function implements the ``shift-selection'' behavior of
602 point-motion commands. @xref{Shift Selection,,, emacs, The GNU Emacs
603 Manual}. It is called automatically by the Emacs command loop
604 whenever a command with a @samp{^} character in its @code{interactive}
605 spec is invoked, before the command itself is executed
606 (@pxref{Interactive Codes, ^}).
607
608 If @code{shift-select-mode} is non-@code{nil} and the current command
609 was invoked via shift translation (@pxref{Key Sequence Input,
610 shift-translation}), this function sets the mark and temporarily
611 activates the region, unless the region was already temporarily
612 activated in this way. Otherwise, if the region has been activated
613 temporarily, it deactivates the mark and restores the variable
614 @code{transient-mark-mode} to its earlier value.
615 @end defun
616
617 @defvar mark-ring
618 The value of this buffer-local variable is the list of saved former
619 marks of the current buffer, most recent first.
620
621 @example
622 @group
623 mark-ring
624 @result{} (#<marker at 11050 in markers.texi>
625 #<marker at 10832 in markers.texi>
626 @dots{})
627 @end group
628 @end example
629 @end defvar
630
631 @defopt mark-ring-max
632 The value of this variable is the maximum size of @code{mark-ring}. If
633 more marks than this are pushed onto the @code{mark-ring},
634 @code{push-mark} discards an old mark when it adds a new one.
635 @end defopt
636
637 @node The Region
638 @section The Region
639 @cindex region (between point and mark)
640
641 The text between point and the mark is known as @dfn{the region}.
642 Various functions operate on text delimited by point and the mark, but
643 only those functions specifically related to the region itself are
644 described here.
645
646 The next two functions signal an error if the mark does not point
647 anywhere. If Transient Mark mode is enabled and
648 @code{mark-even-if-inactive} is @code{nil}, they also signal an error
649 if the mark is inactive.
650
651 @defun region-beginning
652 This function returns the position of the beginning of the region (as
653 an integer). This is the position of either point or the mark,
654 whichever is smaller.
655 @end defun
656
657 @defun region-end
658 This function returns the position of the end of the region (as an
659 integer). This is the position of either point or the mark, whichever is
660 larger.
661 @end defun
662
663 Few programs need to use the @code{region-beginning} and
664 @code{region-end} functions. A command designed to operate on a region
665 should normally use @code{interactive} with the @samp{r} specification
666 to find the beginning and end of the region. This lets other Lisp
667 programs specify the bounds explicitly as arguments. (@xref{Interactive
668 Codes}.)
669
670 @defun use-region-p
671 This function returns @code{t} if Transient Mark mode is enabled, the
672 mark is active, and there's a valid region in the buffer. Commands
673 that operate on the region (instead of on text near point) when
674 there's an active mark should use this to test whether to do that.
675 @end defun