Next: Predicates on Markers, Previous: Markers, Up: Markers [Contents][Index]
A marker specifies a buffer and a position in that buffer. The marker can be used to represent a position in the functions that require one, just as an integer could be used. See Positions, for a complete description of positions.
A marker has two attributes: the marker position, and the marker buffer. The marker position is an integer that is equivalent (at a given time) to the marker as a position in that buffer. But the marker’s position value can change often during the life of the marker. Insertion and deletion of text in the buffer relocate the marker. The idea is that a marker positioned between two characters remains between those two characters despite insertion and deletion elsewhere in the buffer. Relocation changes the integer equivalent of the marker.
Deleting text around a marker’s position leaves the marker between the
characters immediately before and after the deleted text. Inserting
text at the position of a marker normally leaves the marker in front of
the new text—unless it is inserted with insert-before-markers
(see Insertion).
Insertion and deletion in a buffer must check all the markers and relocate them if necessary. This slows processing in a buffer with a large number of markers. For this reason, it is a good idea to make a marker point nowhere if you are sure you don’t need it any more. Unreferenced markers are garbage collected eventually, but until then will continue to use time if they do point somewhere.
Because it is common to perform arithmetic operations on a marker
position, most of the arithmetic operations (including +
and
-
) accept markers as arguments. In such cases, the marker
stands for its current position.
Note that you can use extents to achieve the same functionality, and
more, as markers. (Markers were defined before extents, which is why
they both continue to exist.) A zero-length extent with the
detachable
property removed is almost identical to a marker.
(See Extent Endpoints, for more information on zero-length extents.)
In particular:
detachable
property must be removed (otherwise, the extent
will disappear when text near it is deleted) and exactly one
endpoint must be closed (if both endpoints are closed, the extent
will expand to contain text inserted where it is located).
end-open
property but not
the start-open
property (this is the default), text inserted
at the extent’s location causes the extent to move forward, just
like a marker.
start-open
property but not
the end-open
property, text inserted at the extent’s location
causes the extent to remain before the text, like what happens to
markers when insert-before-markers
is used.
insert
or insert-before-markers
was called. These
functions do not affect zero-length extents differently; instead,
the presence or absence of the start-open
and end-open
extent properties determines this, as just described.
Here are examples of creating markers, setting markers, and moving point to markers:
;; Make a new marker that initially does not point anywhere:
(setq m1 (make-marker))
⇒ #<marker in no buffer>
;; Set m1
to point between the 99th and 100th characters
;; in the current buffer:
(set-marker m1 100)
⇒ #<marker at 100 in markers.texi>
;; Now insert one character at the beginning of the buffer:
(goto-char (point-min))
⇒ 1
(insert "Q")
⇒ nil
;; m1
is updated appropriately.
m1
⇒ #<marker at 101 in markers.texi>
;; Two markers that point to the same position ;; are noteq
, but they areequal
. (setq m2 (copy-marker m1)) ⇒ #<marker at 101 in markers.texi> (eq m1 m2) ⇒ nil (equal m1 m2) ⇒ t
;; When you are finished using a marker, make it point nowhere.
(set-marker m1 nil)
⇒ #<marker in no buffer>
Next: Predicates on Markers, Previous: Markers, Up: Markers [Contents][Index]