markers.texi 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Emacs Lisp Reference Manual.
  3. @c Copyright (C) 1990-1995, 1998-1999, 2001-2016 Free Software
  4. @c Foundation, Inc.
  5. @c See the file elisp.texi for copying conditions.
  6. @node Markers
  7. @chapter Markers
  8. @cindex markers
  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.
  13. @menu
  14. * Overview of Markers:: The components of a marker, and how it relocates.
  15. * Predicates on Markers:: Testing whether an object is a marker.
  16. * Creating Markers:: Making empty markers or markers at certain places.
  17. * Information from Markers:: Finding the marker's buffer or character position.
  18. * Marker Insertion Types:: Two ways a marker can relocate when you
  19. insert where it points.
  20. * Moving 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. @node Overview of Markers
  25. @section Overview of Markers
  26. A marker specifies a buffer and a position in that buffer. A
  27. marker can be used to represent a position in functions that
  28. require one, just as an integer could be used. In that case, the
  29. marker's buffer is normally ignored. Of course, a marker used in this
  30. way usually points to a position in the buffer that the function
  31. operates on, but that is entirely the programmer's responsibility.
  32. @xref{Positions}, for a complete description of positions.
  33. A marker has three attributes: the marker position, the marker
  34. buffer, and the insertion type. The marker position is an integer
  35. that is equivalent (at a given time) to the marker as a position in
  36. that buffer. But the marker's position value can change during
  37. the life of the marker, and often does. Insertion and deletion of
  38. text in the buffer relocate the marker. The idea is that a marker
  39. positioned between two characters remains between those two characters
  40. despite insertion and deletion elsewhere in the buffer. Relocation
  41. changes the integer equivalent of the marker.
  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 either in
  46. front of or after the new text, depending on the marker's @dfn{insertion
  47. type} (@pxref{Marker Insertion Types})---unless the insertion is done
  48. with @code{insert-before-markers} (@pxref{Insertion}).
  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. Markers that can no longer be accessed are eventually removed
  55. (@pxref{Garbage Collection}).
  56. @cindex markers as numbers
  57. Because it is common to perform arithmetic operations on a marker
  58. position, most of these operations (including @code{+} and
  59. @code{-}) accept markers as arguments. In such cases, the marker
  60. stands for its current position.
  61. Here are examples of creating markers, setting markers, and moving point
  62. to markers:
  63. @example
  64. @group
  65. ;; @r{Make a new marker that initially does not point anywhere:}
  66. (setq m1 (make-marker))
  67. @result{} #<marker in no buffer>
  68. @end group
  69. @group
  70. ;; @r{Set @code{m1} to point between the 99th and 100th characters}
  71. ;; @r{in the current buffer:}
  72. (set-marker m1 100)
  73. @result{} #<marker at 100 in markers.texi>
  74. @end group
  75. @group
  76. ;; @r{Now insert one character at the beginning of the buffer:}
  77. (goto-char (point-min))
  78. @result{} 1
  79. (insert "Q")
  80. @result{} nil
  81. @end group
  82. @group
  83. ;; @r{@code{m1} is updated appropriately.}
  84. m1
  85. @result{} #<marker at 101 in markers.texi>
  86. @end group
  87. @group
  88. ;; @r{Two markers that point to the same position}
  89. ;; @r{are not @code{eq}, but they are @code{equal}.}
  90. (setq m2 (copy-marker m1))
  91. @result{} #<marker at 101 in markers.texi>
  92. (eq m1 m2)
  93. @result{} nil
  94. (equal m1 m2)
  95. @result{} t
  96. @end group
  97. @group
  98. ;; @r{When you are finished using a marker, make it point nowhere.}
  99. (set-marker m1 nil)
  100. @result{} #<marker in no buffer>
  101. @end group
  102. @end example
  103. @node Predicates on Markers
  104. @section Predicates on Markers
  105. @cindex predicates for markers
  106. @cindex markers, predicates for
  107. You can test an object to see whether it is a marker, or whether it is
  108. either an integer or a marker. The latter test is useful in connection
  109. with the arithmetic functions that work with both markers and integers.
  110. @defun markerp object
  111. This function returns @code{t} if @var{object} is a marker, @code{nil}
  112. otherwise. Note that integers are not markers, even though many
  113. functions will accept either a marker or an integer.
  114. @end defun
  115. @defun integer-or-marker-p object
  116. This function returns @code{t} if @var{object} is an integer or a marker,
  117. @code{nil} otherwise.
  118. @end defun
  119. @defun number-or-marker-p object
  120. This function returns @code{t} if @var{object} is a number (either
  121. integer or floating point) or a marker, @code{nil} otherwise.
  122. @end defun
  123. @node Creating Markers
  124. @section Functions that Create Markers
  125. @cindex creating markers
  126. @cindex marker creation
  127. When you create a new marker, you can make it point nowhere, or point
  128. to the present position of point, or to the beginning or end of the
  129. accessible portion of the buffer, or to the same place as another given
  130. marker.
  131. The next four functions all return markers with insertion type
  132. @code{nil}. @xref{Marker Insertion Types}.
  133. @defun make-marker
  134. This function returns a newly created marker that does not point
  135. anywhere.
  136. @example
  137. @group
  138. (make-marker)
  139. @result{} #<marker in no buffer>
  140. @end group
  141. @end example
  142. @end defun
  143. @defun point-marker
  144. This function returns a new marker that points to the present position
  145. of point in the current buffer. @xref{Point}. For an example, see
  146. @code{copy-marker}, below.
  147. @end defun
  148. @defun point-min-marker
  149. This function returns a new marker that points to the beginning of the
  150. accessible portion of the buffer. This will be the beginning of the
  151. buffer unless narrowing is in effect. @xref{Narrowing}.
  152. @end defun
  153. @defun point-max-marker
  154. This function returns a new marker that points to the end of the
  155. accessible portion of the buffer. This will be the end of the buffer
  156. unless narrowing is in effect. @xref{Narrowing}.
  157. Here are examples of this function and @code{point-min-marker}, shown in
  158. a buffer containing a version of the source file for the text of this
  159. chapter.
  160. @example
  161. @group
  162. (point-min-marker)
  163. @result{} #<marker at 1 in markers.texi>
  164. (point-max-marker)
  165. @result{} #<marker at 24080 in markers.texi>
  166. @end group
  167. @group
  168. (narrow-to-region 100 200)
  169. @result{} nil
  170. @end group
  171. @group
  172. (point-min-marker)
  173. @result{} #<marker at 100 in markers.texi>
  174. @end group
  175. @group
  176. (point-max-marker)
  177. @result{} #<marker at 200 in markers.texi>
  178. @end group
  179. @end example
  180. @end defun
  181. @defun copy-marker &optional marker-or-integer insertion-type
  182. If passed a marker as its argument, @code{copy-marker} returns a
  183. new marker that points to the same place and the same buffer as does
  184. @var{marker-or-integer}. If passed an integer as its argument,
  185. @code{copy-marker} returns a new marker that points to position
  186. @var{marker-or-integer} in the current buffer.
  187. The new marker's insertion type is specified by the argument
  188. @var{insertion-type}. @xref{Marker Insertion Types}.
  189. @c This behavior used to be documented until 2013/08.
  190. @ignore
  191. If passed an integer argument less than 1, @code{copy-marker} returns a
  192. new marker that points to the beginning of the current buffer. If
  193. passed an integer argument greater than the length of the buffer,
  194. @code{copy-marker} returns a new marker that points to the end of the
  195. buffer.
  196. @end ignore
  197. @example
  198. @group
  199. (copy-marker 0)
  200. @result{} #<marker at 1 in markers.texi>
  201. @end group
  202. @group
  203. (copy-marker 90000)
  204. @result{} #<marker at 24080 in markers.texi>
  205. @end group
  206. @end example
  207. An error is signaled if @var{marker} is neither a marker nor an
  208. integer.
  209. @end defun
  210. Two distinct markers are considered @code{equal} (even though not
  211. @code{eq}) to each other if they have the same position and buffer, or
  212. if they both point nowhere.
  213. @example
  214. @group
  215. (setq p (point-marker))
  216. @result{} #<marker at 2139 in markers.texi>
  217. @end group
  218. @group
  219. (setq q (copy-marker p))
  220. @result{} #<marker at 2139 in markers.texi>
  221. @end group
  222. @group
  223. (eq p q)
  224. @result{} nil
  225. @end group
  226. @group
  227. (equal p q)
  228. @result{} t
  229. @end group
  230. @end example
  231. @node Information from Markers
  232. @section Information from Markers
  233. @cindex marker information
  234. This section describes the functions for accessing the components of a
  235. marker object.
  236. @defun marker-position marker
  237. This function returns the position that @var{marker} points to, or
  238. @code{nil} if it points nowhere.
  239. @end defun
  240. @defun marker-buffer marker
  241. This function returns the buffer that @var{marker} points into, or
  242. @code{nil} if it points nowhere.
  243. @c FIXME: The 'buffer' argument of 'set-marker' already defaults to
  244. @c the current buffer, why use '(current-buffer)' explicitly here?
  245. @example
  246. @group
  247. (setq m (make-marker))
  248. @result{} #<marker in no buffer>
  249. @end group
  250. @group
  251. (marker-position m)
  252. @result{} nil
  253. @end group
  254. @group
  255. (marker-buffer m)
  256. @result{} nil
  257. @end group
  258. @group
  259. (set-marker m 3770 (current-buffer))
  260. @result{} #<marker at 3770 in markers.texi>
  261. @end group
  262. @group
  263. (marker-buffer m)
  264. @result{} #<buffer markers.texi>
  265. @end group
  266. @group
  267. (marker-position m)
  268. @result{} 3770
  269. @end group
  270. @end example
  271. @end defun
  272. @node Marker Insertion Types
  273. @section Marker Insertion Types
  274. @cindex insertion type of a marker
  275. When you insert text directly at the place where a marker points,
  276. there are two possible ways to relocate that marker: it can point before
  277. the inserted text, or point after it. You can specify which one a given
  278. marker should do by setting its @dfn{insertion type}. Note that use of
  279. @code{insert-before-markers} ignores markers' insertion types, always
  280. relocating a marker to point after the inserted text.
  281. @defun set-marker-insertion-type marker type
  282. This function sets the insertion type of marker @var{marker} to
  283. @var{type}. If @var{type} is @code{t}, @var{marker} will advance when
  284. text is inserted at its position. If @var{type} is @code{nil},
  285. @var{marker} does not advance when text is inserted there.
  286. @end defun
  287. @defun marker-insertion-type marker
  288. This function reports the current insertion type of @var{marker}.
  289. @end defun
  290. Most functions that create markers, without an argument allowing to
  291. specify the insertion type, create them with insertion type
  292. @code{nil}. Also, the mark has, by default, insertion type
  293. @code{nil}.
  294. @node Moving Markers
  295. @section Moving Marker Positions
  296. @cindex moving markers
  297. @cindex marker, how to move position
  298. This section describes how to change the position of an existing
  299. marker. When you do this, be sure you know whether the marker is used
  300. outside of your program, and, if so, what effects will result from
  301. moving it---otherwise, confusing things may happen in other parts of
  302. Emacs.
  303. @defun set-marker marker position &optional buffer
  304. This function moves @var{marker} to @var{position}
  305. in @var{buffer}. If @var{buffer} is not provided, it defaults to
  306. the current buffer.
  307. @c This behavior used to be documented until 2013/08.
  308. @ignore
  309. If @var{position} is less than 1, @code{set-marker} moves @var{marker}
  310. to the beginning of the buffer. If @var{position} is greater than the
  311. size of the buffer (@pxref{Point}), @code{set-marker} moves marker to
  312. the end of the buffer.
  313. @end ignore
  314. If @var{position} is @code{nil} or a marker that points nowhere, then
  315. @var{marker} is set to point nowhere.
  316. The value returned is @var{marker}.
  317. @example
  318. @group
  319. (setq m (point-marker))
  320. @result{} #<marker at 4714 in markers.texi>
  321. @end group
  322. @group
  323. (set-marker m 55)
  324. @result{} #<marker at 55 in markers.texi>
  325. @end group
  326. @group
  327. (setq b (get-buffer "foo"))
  328. @result{} #<buffer foo>
  329. @end group
  330. @group
  331. (set-marker m 0 b)
  332. @result{} #<marker at 1 in foo>
  333. @end group
  334. @end example
  335. @end defun
  336. @defun move-marker marker position &optional buffer
  337. This is another name for @code{set-marker}.
  338. @end defun
  339. @node The Mark
  340. @section The Mark
  341. @cindex mark, the
  342. @c @cindex the mark?
  343. Each buffer has a special marker, which is designated @dfn{the
  344. mark}. When a buffer is newly created, this marker exists but does
  345. not point anywhere; this means that the mark doesn't exist in that
  346. buffer yet. Subsequent commands can set the mark.
  347. The mark specifies a position to bound a range of text for many
  348. commands, such as @code{kill-region} and @code{indent-rigidly}. These
  349. commands typically act on the text between point and the mark, which
  350. is called the @dfn{region}. If you are writing a command that
  351. operates on the region, don't examine the mark directly; instead, use
  352. @code{interactive} with the @samp{r} specification. This provides the
  353. values of point and the mark as arguments to the command in an
  354. interactive call, but permits other Lisp programs to specify arguments
  355. explicitly. @xref{Interactive Codes}.
  356. Some commands set the mark as a side-effect. Commands should do
  357. this only if it has a potential use to the user, and never for their
  358. own internal purposes. For example, the @code{replace-regexp} command
  359. sets the mark to the value of point before doing any replacements,
  360. because this enables the user to move back there conveniently after
  361. the replace is finished.
  362. Once the mark exists in a buffer, it normally never ceases to
  363. exist. However, it may become @dfn{inactive}, if Transient Mark mode
  364. is enabled. The buffer-local variable @code{mark-active}, if
  365. non-@code{nil}, means that the mark is active. A command can call the
  366. function @code{deactivate-mark} to deactivate the mark directly, or it
  367. can request deactivation of the mark upon return to the editor command
  368. loop by setting the variable @code{deactivate-mark} to a
  369. non-@code{nil} value.
  370. If Transient Mark mode is enabled, certain editing commands that
  371. normally apply to text near point, apply instead to the region when
  372. the mark is active. This is the main motivation for using Transient
  373. Mark mode. (Another is that this enables highlighting of the region
  374. when the mark is active. @xref{Display}.)
  375. @cindex mark ring
  376. In addition to the mark, each buffer has a @dfn{mark ring} which is a
  377. list of markers containing previous values of the mark. When editing
  378. commands change the mark, they should normally save the old value of the
  379. mark on the mark ring. The variable @code{mark-ring-max} specifies the
  380. maximum number of entries in the mark ring; once the list becomes this
  381. long, adding a new element deletes the last element.
  382. There is also a separate global mark ring, but that is used only in a
  383. few particular user-level commands, and is not relevant to Lisp
  384. programming. So we do not describe it here.
  385. @defun mark &optional force
  386. @cindex current buffer mark
  387. This function returns the current buffer's mark position as an integer,
  388. or @code{nil} if no mark has ever been set in this buffer.
  389. If Transient Mark mode is enabled, and @code{mark-even-if-inactive} is
  390. @code{nil}, @code{mark} signals an error if the mark is inactive.
  391. However, if @var{force} is non-@code{nil}, then @code{mark} disregards
  392. inactivity of the mark, and returns the mark position (or @code{nil})
  393. anyway.
  394. @end defun
  395. @defun mark-marker
  396. This function returns the marker that represents the current buffer's
  397. mark. It is not a copy, it is the marker used internally. Therefore,
  398. changing this marker's position will directly affect the buffer's
  399. mark. Don't do that unless that is the effect you want.
  400. @example
  401. @group
  402. (setq m (mark-marker))
  403. @result{} #<marker at 3420 in markers.texi>
  404. @end group
  405. @group
  406. (set-marker m 100)
  407. @result{} #<marker at 100 in markers.texi>
  408. @end group
  409. @group
  410. (mark-marker)
  411. @result{} #<marker at 100 in markers.texi>
  412. @end group
  413. @end example
  414. Like any marker, this marker can be set to point at any buffer you
  415. like. If you make it point at any buffer other than the one of which
  416. it is the mark, it will yield perfectly consistent, but rather odd,
  417. results. We recommend that you not do it!
  418. @end defun
  419. @defun set-mark position
  420. This function sets the mark to @var{position}, and activates the mark.
  421. The old value of the mark is @emph{not} pushed onto the mark ring.
  422. @strong{Please note:} Use this function only if you want the user to
  423. see that the mark has moved, and you want the previous mark position to
  424. be lost. Normally, when a new mark is set, the old one should go on the
  425. @code{mark-ring}. For this reason, most applications should use
  426. @code{push-mark} and @code{pop-mark}, not @code{set-mark}.
  427. Novice Emacs Lisp programmers often try to use the mark for the wrong
  428. purposes. The mark saves a location for the user's convenience. An
  429. editing command should not alter the mark unless altering the mark is
  430. part of the user-level functionality of the command. (And, in that
  431. case, this effect should be documented.) To remember a location for
  432. internal use in the Lisp program, store it in a Lisp variable. For
  433. example:
  434. @example
  435. @group
  436. (let ((beg (point)))
  437. (forward-line 1)
  438. (delete-region beg (point))).
  439. @end group
  440. @end example
  441. @end defun
  442. @defun push-mark &optional position nomsg activate
  443. This function sets the current buffer's mark to @var{position}, and
  444. pushes a copy of the previous mark onto @code{mark-ring}. If
  445. @var{position} is @code{nil}, then the value of point is used.
  446. @c Doesn't seem relevant.
  447. @c @code{push-mark} returns @code{nil}.
  448. The function @code{push-mark} normally @emph{does not} activate the
  449. mark. To do that, specify @code{t} for the argument @var{activate}.
  450. A @samp{Mark set} message is displayed unless @var{nomsg} is
  451. non-@code{nil}.
  452. @end defun
  453. @defun pop-mark
  454. This function pops off the top element of @code{mark-ring} and makes
  455. that mark become the buffer's actual mark. This does not move point in
  456. the buffer, and it does nothing if @code{mark-ring} is empty. It
  457. deactivates the mark.
  458. @c
  459. @c Seems even less relevant.
  460. @c The return value is not meaningful.
  461. @end defun
  462. @defopt transient-mark-mode
  463. This variable, if non-@code{nil}, enables Transient Mark mode. In
  464. Transient Mark mode, every buffer-modifying primitive sets
  465. @code{deactivate-mark}. As a consequence, most commands that modify
  466. the buffer also deactivate the mark.
  467. When Transient Mark mode is enabled and the mark is active, many
  468. commands that normally apply to the text near point instead apply to
  469. the region. Such commands should use the function @code{use-region-p}
  470. to test whether they should operate on the region. @xref{The Region}.
  471. Lisp programs can set @code{transient-mark-mode} to non-@code{nil},
  472. non-@code{t} values to enable Transient Mark mode temporarily. If the
  473. value is @code{lambda}, Transient Mark mode is automatically turned
  474. off after any action, such as buffer modification, that would normally
  475. deactivate the mark. If the value is @w{@code{(only . @var{oldval})}},
  476. then @code{transient-mark-mode} is set to the value @var{oldval} after
  477. any subsequent command that moves point and is not shift-translated
  478. (@pxref{Key Sequence Input, shift-translation}), or after any other
  479. action that would normally deactivate the mark.
  480. @end defopt
  481. @defopt mark-even-if-inactive
  482. If this is non-@code{nil}, Lisp programs and the Emacs user can use the
  483. mark even when it is inactive. This option affects the behavior of
  484. Transient Mark mode. When the option is non-@code{nil}, deactivation of
  485. the mark turns off region highlighting, but commands that use the mark
  486. behave as if the mark were still active.
  487. @end defopt
  488. @defvar deactivate-mark
  489. If an editor command sets this variable non-@code{nil}, then the editor
  490. command loop deactivates the mark after the command returns (if
  491. Transient Mark mode is enabled). All the primitives that change the
  492. buffer set @code{deactivate-mark}, to deactivate the mark when the
  493. command is finished. Setting this variable makes it buffer-local.
  494. To write Lisp code that modifies the buffer without causing
  495. deactivation of the mark at the end of the command, bind
  496. @code{deactivate-mark} to @code{nil} around the code that does the
  497. modification. For example:
  498. @example
  499. (let (deactivate-mark)
  500. (insert " "))
  501. @end example
  502. @end defvar
  503. @defun deactivate-mark &optional force
  504. If Transient Mark mode is enabled or @var{force} is non-@code{nil},
  505. this function deactivates the mark and runs the normal hook
  506. @code{deactivate-mark-hook}. Otherwise, it does nothing.
  507. @end defun
  508. @defvar mark-active
  509. The mark is active when this variable is non-@code{nil}. This
  510. variable is always buffer-local in each buffer. Do @emph{not} use the
  511. value of this variable to decide whether a command that normally
  512. operates on text near point should operate on the region instead. Use
  513. the function @code{use-region-p} for that (@pxref{The Region}).
  514. @end defvar
  515. @defvar activate-mark-hook
  516. @defvarx deactivate-mark-hook
  517. These normal hooks are run, respectively, when the mark becomes active
  518. and when it becomes inactive. The hook @code{activate-mark-hook} is
  519. also run at the end of the command loop if the mark is active and it
  520. is possible that the region may have changed.
  521. @ignore
  522. This piece of command_loop_1, run unless deactivating the mark:
  523. if (current_buffer != prev_buffer || MODIFF != prev_modiff)
  524. {
  525. Lisp_Object hook = intern ("activate-mark-hook");
  526. Frun_hooks (1, &hook);
  527. }
  528. @end ignore
  529. @end defvar
  530. @defun handle-shift-selection
  531. This function implements the shift-selection behavior of
  532. point-motion commands. @xref{Shift Selection,,, emacs, The GNU Emacs
  533. Manual}. It is called automatically by the Emacs command loop
  534. whenever a command with a @samp{^} character in its @code{interactive}
  535. spec is invoked, before the command itself is executed
  536. (@pxref{Interactive Codes, ^}).
  537. If @code{shift-select-mode} is non-@code{nil} and the current command
  538. was invoked via shift translation (@pxref{Key Sequence Input,
  539. shift-translation}), this function sets the mark and temporarily
  540. activates the region, unless the region was already temporarily
  541. activated in this way. Otherwise, if the region has been activated
  542. temporarily, it deactivates the mark and restores the variable
  543. @code{transient-mark-mode} to its earlier value.
  544. @end defun
  545. @defvar mark-ring
  546. The value of this buffer-local variable is the list of saved former
  547. marks of the current buffer, most recent first.
  548. @example
  549. @group
  550. mark-ring
  551. @result{} (#<marker at 11050 in markers.texi>
  552. #<marker at 10832 in markers.texi>
  553. @dots{})
  554. @end group
  555. @end example
  556. @end defvar
  557. @defopt mark-ring-max
  558. The value of this variable is the maximum size of @code{mark-ring}. If
  559. more marks than this are pushed onto the @code{mark-ring},
  560. @code{push-mark} discards an old mark when it adds a new one.
  561. @end defopt
  562. @c There is also global-mark-ring-max, but this chapter explicitly
  563. @c does not talk about the global mark.
  564. @cindex @code{delete-selection}, symbol property
  565. @findex delete-selection-helper
  566. @findex delete-selection-pre-hook
  567. When Delete Selection mode (@pxref{Using Region, Delete Selection, ,
  568. emacs, The GNU Emacs Manual}) is enabled, commands that operate on the
  569. active region (a.k.a.@: ``selection'') behave slightly differently.
  570. This works by adding the function @code{delete-selection-pre-hook} to
  571. the @code{pre-command-hook} (@pxref{Command Overview}). That function
  572. calls @code{delete-selection-helper} to delete the selection as
  573. appropriate for the command. If you want to adapt a command to Delete
  574. Selection mode, put the @code{delete-selection} property on the
  575. function's symbol (@pxref{Symbol Plists}); commands that don't have
  576. this property on their symbol won't delete the selection. This
  577. property can have one of several values to tailor the behavior to what
  578. the command is supposed to do; see the doc strings of
  579. @code{delete-selection-pre-hook} and @code{delete-selection-helper}
  580. for the details.
  581. @node The Region
  582. @section The Region
  583. @c The index entry must be just "region" to make it the first hit
  584. @c when the user types "i region RET", because otherwise the Info
  585. @c reader will present substring matches in alphabetical order,
  586. @c putting this one near the end, with something utterly unrelated as
  587. @c the first hit.
  588. @cindex region
  589. The text between point and the mark is known as @dfn{the region}.
  590. Various functions operate on text delimited by point and the mark, but
  591. only those functions specifically related to the region itself are
  592. described here.
  593. The next two functions signal an error if the mark does not point
  594. anywhere. If Transient Mark mode is enabled and
  595. @code{mark-even-if-inactive} is @code{nil}, they also signal an error
  596. if the mark is inactive.
  597. @defun region-beginning
  598. This function returns the position of the beginning of the region (as
  599. an integer). This is the position of either point or the mark,
  600. whichever is smaller.
  601. @end defun
  602. @defun region-end
  603. This function returns the position of the end of the region (as an
  604. integer). This is the position of either point or the mark, whichever is
  605. larger.
  606. @end defun
  607. @c FIXME: Mention it in tips.texi?
  608. Instead of using @code{region-beginning} and @code{region-end}, a
  609. command designed to operate on a region should normally use
  610. @code{interactive} with the @samp{r} specification to find the
  611. beginning and end of the region. This lets other Lisp programs
  612. specify the bounds explicitly as arguments. @xref{Interactive Codes}.
  613. @defun use-region-p
  614. This function returns @code{t} if Transient Mark mode is enabled, the
  615. mark is active, and there is a valid region in the buffer. This
  616. function is intended to be used by commands that operate on the
  617. region, instead of on text near point, when the mark is active.
  618. @cindex empty region
  619. @vindex use-empty-active-region
  620. A region is valid if it has a non-zero size, or if the user option
  621. @code{use-empty-active-region} is non-@code{nil} (by default, it is
  622. @code{nil}). The function @code{region-active-p} is similar to
  623. @code{use-region-p}, but considers all regions as valid. In most
  624. cases, you should not use @code{region-active-p}, since if the region
  625. is empty it is often more appropriate to operate on point.
  626. @end defun