Commit | Line | Data |
---|---|---|
0abf66c5 RS |
1 | @c -*-texinfo-*- |
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 | |
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 | * 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''. | |
23 | @end menu | |
24 | ||
25 | @node Overview of Markers | |
26 | @section Overview of Markers | |
27 | ||
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. | |
32 | ||
33 | A marker has two attributes: the marker position, and the marker | |
29679a81 | 34 | buffer. The marker position is an integer that is equivalent (at a |
0abf66c5 RS |
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. | |
41 | ||
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} | |
47 | (@pxref{Insertion}). | |
48 | ||
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. | |
56 | ||
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. | |
62 | ||
63 | Here are examples of creating markers, setting markers, and moving point | |
64 | to markers: | |
65 | ||
66 | @example | |
67 | @group | |
68 | ;; @r{Make a new marker that initially does not point anywhere:} | |
69 | (setq m1 (make-marker)) | |
70 | @result{} #<marker in no buffer> | |
71 | @end group | |
72 | ||
73 | @group | |
74 | ;; @r{Set @code{m1} to point between the 99th and 100th characters} | |
75 | ;; @r{in the current buffer:} | |
76 | (set-marker m1 100) | |
77 | @result{} #<marker at 100 in markers.texi> | |
78 | @end group | |
79 | ||
80 | @group | |
81 | ;; @r{Now insert one character at the beginning of the buffer:} | |
82 | (goto-char (point-min)) | |
83 | @result{} 1 | |
84 | (insert "Q") | |
85 | @result{} nil | |
86 | @end group | |
87 | ||
88 | @group | |
89 | ;; @r{@code{m1} is updated appropriately.} | |
90 | m1 | |
91 | @result{} #<marker at 101 in markers.texi> | |
92 | @end group | |
93 | ||
94 | @group | |
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> | |
99 | (eq m1 m2) | |
100 | @result{} nil | |
101 | (equal m1 m2) | |
102 | @result{} t | |
103 | @end group | |
104 | ||
105 | @group | |
106 | ;; @r{When you are finished using a marker, make it point nowhere.} | |
107 | (set-marker m1 nil) | |
108 | @result{} #<marker in no buffer> | |
109 | @end group | |
110 | @end example | |
111 | ||
112 | @node Predicates on Markers | |
113 | @section Predicates on Markers | |
114 | ||
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. | |
118 | ||
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. | |
123 | @end defun | |
124 | ||
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. | |
128 | @end defun | |
129 | ||
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. | |
133 | @end defun | |
134 | ||
135 | @node Creating Markers | |
136 | @section Functions That Create Markers | |
137 | ||
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 | |
141 | marker. | |
142 | ||
143 | @defun make-marker | |
29679a81 | 144 | This functions returns a newly created marker that does not point |
0abf66c5 RS |
145 | anywhere. |
146 | ||
147 | @example | |
148 | @group | |
149 | (make-marker) | |
150 | @result{} #<marker in no buffer> | |
151 | @end group | |
152 | @end example | |
153 | @end defun | |
154 | ||
155 | @defun point-marker | |
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. | |
159 | @end defun | |
160 | ||
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}. | |
165 | @end defun | |
166 | ||
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}. | |
172 | ||
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 | |
175 | chapter. | |
176 | ||
177 | @example | |
178 | @group | |
179 | (point-min-marker) | |
180 | @result{} #<marker at 1 in markers.texi> | |
181 | (point-max-marker) | |
182 | @result{} #<marker at 15573 in markers.texi> | |
183 | @end group | |
184 | ||
185 | @group | |
186 | (narrow-to-region 100 200) | |
187 | @result{} nil | |
188 | @end group | |
189 | @group | |
190 | (point-min-marker) | |
191 | @result{} #<marker at 100 in markers.texi> | |
192 | @end group | |
193 | @group | |
194 | (point-max-marker) | |
195 | @result{} #<marker at 200 in markers.texi> | |
196 | @end group | |
197 | @end example | |
198 | @end defun | |
199 | ||
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. | |
206 | ||
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 | |
211 | buffer. | |
212 | ||
213 | An error is signaled if @var{marker} is neither a marker nor an | |
214 | integer. | |
215 | ||
216 | @example | |
217 | @group | |
218 | (setq p (point-marker)) | |
219 | @result{} #<marker at 2139 in markers.texi> | |
220 | @end group | |
221 | ||
222 | @group | |
223 | (setq q (copy-marker p)) | |
224 | @result{} #<marker at 2139 in markers.texi> | |
225 | @end group | |
226 | ||
227 | @group | |
228 | (eq p q) | |
229 | @result{} nil | |
230 | @end group | |
231 | ||
232 | @group | |
233 | (equal p q) | |
234 | @result{} t | |
235 | @end group | |
236 | ||
237 | @group | |
238 | (copy-marker 0) | |
239 | @result{} #<marker at 1 in markers.texi> | |
240 | @end group | |
241 | ||
242 | @group | |
243 | (copy-marker 20000) | |
244 | @result{} #<marker at 7572 in markers.texi> | |
245 | @end group | |
246 | @end example | |
247 | @end defun | |
248 | ||
249 | @node Information from Markers | |
250 | @section Information from Markers | |
251 | ||
252 | This section describes the functions for accessing the components of a | |
253 | marker object. | |
254 | ||
255 | @defun marker-position marker | |
256 | This function returns the position that @var{marker} points to, or | |
257 | @code{nil} if it points nowhere. | |
258 | @end defun | |
259 | ||
260 | @defun marker-buffer marker | |
261 | This function returns the buffer that @var{marker} points into, or | |
262 | @code{nil} if it points nowhere. | |
263 | ||
264 | @example | |
265 | @group | |
266 | (setq m (make-marker)) | |
267 | @result{} #<marker in no buffer> | |
268 | @end group | |
269 | @group | |
270 | (marker-position m) | |
271 | @result{} nil | |
272 | @end group | |
273 | @group | |
274 | (marker-buffer m) | |
275 | @result{} nil | |
276 | @end group | |
277 | ||
278 | @group | |
279 | (set-marker m 3770 (current-buffer)) | |
280 | @result{} #<marker at 3770 in markers.texi> | |
281 | @end group | |
282 | @group | |
283 | (marker-buffer m) | |
284 | @result{} #<buffer markers.texi> | |
285 | @end group | |
286 | @group | |
287 | (marker-position m) | |
288 | @result{} 3770 | |
289 | @end group | |
290 | @end example | |
291 | @end defun | |
292 | ||
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. | |
296 | ||
297 | @node Changing Markers | |
298 | @section Changing Marker Positions | |
299 | ||
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 | |
304 | Emacs. | |
305 | ||
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 | |
309 | the current buffer. | |
310 | ||
311 | If @var{position} is less than 1, @code{set-marker} moves @var{marker} | |
29679a81 RS |
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. | |
0abf66c5 RS |
316 | |
317 | The value returned is @var{marker}. | |
318 | ||
319 | @example | |
320 | @group | |
321 | (setq m (point-marker)) | |
322 | @result{} #<marker at 4714 in markers.texi> | |
323 | @end group | |
324 | @group | |
325 | (set-marker m 55) | |
326 | @result{} #<marker at 55 in markers.texi> | |
327 | @end group | |
328 | @group | |
329 | (setq b (get-buffer "foo")) | |
330 | @result{} #<buffer foo> | |
331 | @end group | |
332 | @group | |
333 | (set-marker m 0 b) | |
334 | @result{} #<marker at 1 in foo> | |
335 | @end group | |
336 | @end example | |
337 | @end defun | |
338 | ||
339 | @defun move-marker marker position &optional buffer | |
340 | This is another name for @code{set-marker}. | |
341 | @end defun | |
342 | ||
343 | @node The Mark | |
344 | @section The Mark | |
345 | @cindex mark, the | |
346 | @cindex mark ring | |
347 | ||
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. | |
356 | ||
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}. | |
364 | ||
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 | |
29679a81 | 368 | absence of a mark in that buffer.'' |
0abf66c5 RS |
369 | |
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 | |
29679a81 RS |
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). | |
0abf66c5 RS |
377 | |
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. | |
380 | @xref{Display}. | |
381 | ||
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. | |
388 | ||
389 | @defun mark &optional force | |
390 | @cindex current buffer mark | |
391 | This function returns the current buffer's mark position as an integer. | |
392 | ||
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 | |
396 | this buffer. | |
397 | @end defun | |
398 | ||
399 | @defun mark-marker | |
400 | This function returns the current buffer's mark. This is the very marker | |
29679a81 | 401 | that records the mark location inside Emacs, not a copy. Therefore, |
0abf66c5 RS |
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. | |
404 | ||
405 | @example | |
406 | @group | |
407 | (setq m (mark-marker)) | |
408 | @result{} #<marker at 3420 in markers.texi> | |
409 | @end group | |
410 | @group | |
411 | (set-marker m 100) | |
412 | @result{} #<marker at 100 in markers.texi> | |
413 | @end group | |
414 | @group | |
415 | (mark-marker) | |
416 | @result{} #<marker at 100 in markers.texi> | |
417 | @end group | |
418 | @end example | |
419 | ||
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. | |
424 | @end defun | |
425 | ||
426 | @ignore | |
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. | |
431 | ||
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. | |
435 | ||
436 | This function is @emph{only} intended for interactive use. | |
437 | @end deffn | |
438 | @end ignore | |
439 | ||
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. | |
443 | ||
ec221d13 | 444 | @strong{Please note:} Use this function only if you want the user to |
0abf66c5 RS |
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}. | |
449 | ||
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 | |
456 | example: | |
457 | ||
458 | @example | |
459 | @group | |
460 | (let ((beg (point))) | |
461 | (forward-line 1) | |
462 | (delete-region beg (point))). | |
463 | @end group | |
464 | @end example | |
465 | @end defun | |
466 | ||
467 | @c for interactive use only | |
468 | @ignore | |
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. | |
472 | @end deffn | |
473 | @end ignore | |
474 | ||
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}. | |
480 | ||
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}. | |
483 | ||
484 | A @samp{Mark set} message is displayed unless @var{nomsg} is | |
485 | non-@code{nil}. | |
486 | @end defun | |
487 | ||
488 | @defun pop-mark | |
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. | |
493 | ||
494 | The return value is not meaningful. | |
495 | @end defun | |
496 | ||
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 | |
502 | inactive. | |
503 | @end defopt | |
504 | ||
505 | @defvar deactivate-mark | |
506 | If an editor command sets this variable non-@code{nil}, then the editor | |
29679a81 RS |
507 | command loop deactivates the mark after the command returns, but only if |
508 | Transient Mark mode is enabled. | |
0abf66c5 RS |
509 | @end defvar |
510 | ||
29679a81 RS |
511 | @defun deactivate-mark |
512 | This function deactivates the mark, but only if Transient Mark mode | |
513 | is enabled. | |
514 | @end defun | |
515 | ||
0abf66c5 RS |
516 | @defvar mark-active |
517 | The mark is active when this variable is non-@code{nil}. This variable | |
518 | is always local in each buffer. | |
519 | @end defvar | |
520 | ||
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 | |
526 | have changed. | |
527 | @end defvar | |
528 | ||
529 | @defvar mark-ring | |
530 | The value of this buffer-local variable is the list of saved former | |
531 | marks of the current buffer, most recent first. | |
532 | ||
533 | @example | |
534 | @group | |
535 | mark-ring | |
536 | @result{} (#<marker at 11050 in markers.texi> | |
537 | #<marker at 10832 in markers.texi> | |
538 | @dots{}) | |
539 | @end group | |
540 | @end example | |
541 | @end defvar | |
542 | ||
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. | |
547 | @end defopt | |
548 | ||
549 | @node The Region | |
550 | @section The Region | |
551 | @cindex region, the | |
552 | ||
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 | |
556 | described here. | |
557 | ||
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. | |
562 | ||
563 | If the mark does not point anywhere, an error is signaled. | |
564 | @end defun | |
565 | ||
566 | @defun region-end | |
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 | |
569 | larger. | |
570 | ||
571 | If the mark does not point anywhere, an error is signaled. | |
572 | @end defun | |
573 | ||
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 | |
579 | Codes}.) |