]> code.delx.au - gnu-emacs/blob - lispref/text.texi
*** empty log message ***
[gnu-emacs] / lispref / text.texi
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/text
6 @node Text, Searching and Matching, Markers, Top
7 @chapter Text
8 @cindex text
9
10 This chapter describes the functions that deal with the text in a
11 buffer. Most examine, insert or delete text in the current buffer,
12 often in the vicinity of point. Many are interactive. All the
13 functions that change the text provide for undoing the changes
14 (@pxref{Undo}).
15
16 Many text-related functions operate on a region of text defined by two
17 buffer positions passed in arguments named @var{start} and @var{end}.
18 These arguments should be either markers (@pxref{Markers}) or numeric
19 character positions (@pxref{Positions}). The order of these arguments
20 does not matter; it is all right for @var{start} to be the end of the
21 region and @var{end} the beginning. For example, @code{(delete-region 1
22 10)} and @code{(delete-region 10 1)} are equivalent. An
23 @code{args-out-of-range} error is signaled if either @var{start} or
24 @var{end} is outside the accessible portion of the buffer. In an
25 interactive call, point and the mark are used for these arguments.
26
27 @cindex buffer contents
28 Throughout this chapter, ``text'' refers to the characters in the
29 buffer.
30
31 @menu
32 * Near Point:: Examining text in the vicinity of point.
33 * Buffer Contents:: Examining text in a general fashion.
34 * Comparing Text:: Comparing substrings of buffers.
35 * Insertion:: Adding new text to a buffer.
36 * Commands for Insertion:: User-level commands to insert text.
37 * Deletion:: Removing text from a buffer.
38 * User-Level Deletion:: User-level commands to delete text.
39 * The Kill Ring:: Where removed text sometimes is saved for later use.
40 * Undo:: Undoing changes to the text of a buffer.
41 * Maintaining Undo:: How to enable and disable undo information.
42 How to control how much information is kept.
43 * Filling:: Functions for explicit filling.
44 * Auto Filling:: How auto-fill mode is implemented to break lines.
45 * Sorting:: Functions for sorting parts of the buffer.
46 * Columns:: Computing horizontal positions, and using them.
47 * Indentation:: Functions to insert or adjust indentation.
48 * Case Changes:: Case conversion of parts of the buffer.
49 * Text Properties:: Assigning Lisp property lists to text characters.
50 * Substitution:: Replacing a given character wherever it appears.
51 * Registers:: How registers are implemented. Accessing the text or
52 position stored in a register.
53 * Change Hooks:: Supplying functions to be run when text is changed.
54 @end menu
55
56 @node Near Point
57 @section Examining Text Near Point
58
59 Many functions are provided to look at the characters around point.
60 Several simple functions are described here. See also @code{looking-at}
61 in @ref{Regexp Search}.
62
63 @defun char-after position
64 This function returns the character in the current buffer at (i.e.,
65 immediately after) position @var{position}. If @var{position} is out of
66 range for this purpose, either before the beginning of the buffer, or at
67 or beyond the end, then the value is @code{nil}.
68
69 In the following example, assume that the first character in the
70 buffer is @samp{@@}:
71
72 @example
73 @group
74 (char-to-string (char-after 1))
75 @result{} "@@"
76 @end group
77 @end example
78 @end defun
79
80 @defun following-char
81 This function returns the character following point in the current
82 buffer. This is similar to @code{(char-after (point))}. However, if
83 point is at the end of the buffer, then @code{following-char} returns 0.
84
85 Remember that point is always between characters, and the terminal
86 cursor normally appears over the character following point. Therefore,
87 the character returned by @code{following-char} is the character the
88 cursor is over.
89
90 In this example, point is between the @samp{a} and the @samp{c}.
91
92 @example
93 @group
94 ---------- Buffer: foo ----------
95 Gentlemen may cry ``Pea@point{}ce! Peace!,''
96 but there is no peace.
97 ---------- Buffer: foo ----------
98 @end group
99
100 @group
101 (char-to-string (preceding-char))
102 @result{} "a"
103 (char-to-string (following-char))
104 @result{} "c"
105 @end group
106 @end example
107 @end defun
108
109 @defun preceding-char
110 This function returns the character preceding point in the current
111 buffer. See above, under @code{following-char}, for an example. If
112 point is at the beginning of the buffer, @code{preceding-char} returns
113 0.
114 @end defun
115
116 @defun bobp
117 This function returns @code{t} if point is at the beginning of the
118 buffer. If narrowing is in effect, this means the beginning of the
119 accessible portion of the text. See also @code{point-min} in
120 @ref{Point}.
121 @end defun
122
123 @defun eobp
124 This function returns @code{t} if point is at the end of the buffer.
125 If narrowing is in effect, this means the end of accessible portion of
126 the text. See also @code{point-max} in @xref{Point}.
127 @end defun
128
129 @defun bolp
130 This function returns @code{t} if point is at the beginning of a line.
131 @xref{Text Lines}. The beginning of the buffer (or its accessible
132 portion always counts as the beginning of a line.
133 @end defun
134
135 @defun eolp
136 This function returns @code{t} if point is at the end of a line. The
137 end of the buffer (or of its accessible portion) is always considered
138 the end of a line.
139 @end defun
140
141 @node Buffer Contents
142 @section Examining Buffer Contents
143
144 This section describes two functions that allow a Lisp program to
145 convert any portion of the text in the buffer into a string.
146
147 @defun buffer-substring start end
148 This function returns a string containing a copy of the text of the
149 region defined by positions @var{start} and @var{end} in the current
150 buffer. If the arguments are not positions in the accessible portion of
151 the buffer, @code{buffer-substring} signals an @code{args-out-of-range}
152 error.
153
154 It is not necessary for @var{start} to be less than @var{end}; the
155 arguments can be given in either order. But most often the smaller
156 argument is written first.
157
158 @example
159 @group
160 ---------- Buffer: foo ----------
161 This is the contents of buffer foo
162
163 ---------- Buffer: foo ----------
164 @end group
165
166 @group
167 (buffer-substring 1 10)
168 @result{} "This is t"
169 @end group
170 @group
171 (buffer-substring (point-max) 10)
172 @result{} "he contents of buffer foo
173 "
174 @end group
175 @end example
176 @end defun
177
178 @defun buffer-string
179 This function returns the contents of the accessible portion of the
180 current buffer as a string. This is the portion between
181 @code{(point-min)} and @code{(point-max)} (@pxref{Narrowing}).
182
183 @example
184 @group
185 ---------- Buffer: foo ----------
186 This is the contents of buffer foo
187
188 ---------- Buffer: foo ----------
189
190 (buffer-string)
191 @result{} "This is the contents of buffer foo
192 "
193 @end group
194 @end example
195 @end defun
196
197 @node Comparing Text
198 @section Comparing Text
199 @cindex comparing buffer text
200
201 This function lets you compare portions of the text in a buffer, without
202 copying them into strings first.
203
204 @defun compare-buffer-substrings buffer1 start1 end1 buffer2 start2 end2
205 This function lets you compare two substrings of the same buffer or two
206 different buffers. The first three arguments specify one substring,
207 giving a buffer and two positions within the buffer. The last three
208 arguments specify the other substring in the same way. You can use
209 @code{nil} for @var{buffer1}, @var{buffer2} or both to stand for the
210 current buffer.
211
212 The value is negative if the first substring is less, positive if the
213 first is greater, and zero if they are equal. The absolute value of
214 the result is one plus the index of the first differing characters
215 within the substrings.
216
217 This function ignores case when comparing characters
218 if @code{case-fold-search} is non-@code{nil}.
219
220 Suppose the current buffer contains the text @samp{foobarbar
221 haha!rara!}; then in this example the two substrings are @samp{rbar }
222 and @samp{rara!}. The value is 2 because the first substring is greater
223 at the second character.
224
225 @example
226 (compare-buffer-substring nil 6 11 nil 16 21)
227 @result{} 2
228 @end example
229
230 This function does not exist in Emacs version 18 and earlier.
231 @end defun
232
233 @node Insertion
234 @section Insertion
235 @cindex insertion of text
236 @cindex text insertion
237
238 @dfn{Insertion} means adding new text to a buffer. The inserted text
239 goes at point---between the character before point and the character
240 after point.
241
242 Insertion relocates markers that point at positions after the
243 insertion point, so that they stay with the surrounding text
244 (@pxref{Markers}). When a marker points at the place of insertion,
245 insertion normally doesn't relocate the marker, so that it points to the
246 beginning of the inserted text; however, certain special functions such
247 as @code{insert-before-markers} relocate such markers to point after the
248 inserted text.
249
250 @cindex insertion before point
251 @cindex before point, insertion
252 Some insertion functions leave point before the inserted text, while
253 other functions leave it after. We call the latter insertion
254 @dfn{before point}.
255
256 Insertion functions signal an error if the current buffer is
257 read-only.
258
259 @defun insert &rest args
260 This function inserts the strings and/or characters @var{args} into the
261 current buffer, at point, moving point forward. An error is signaled
262 unless all @var{args} are either strings or characters. The value is
263 @code{nil}.
264 @end defun
265
266 @defun insert-before-markers &rest args
267 This function inserts the strings and/or characters @var{args} into the
268 current buffer, at point, moving point forward. An error is signaled
269 unless all @var{args} are either strings or characters. The value is
270 @code{nil}.
271
272 This function is unlike the other insertion functions in that it
273 relocates markers initially pointing at the insertion point, to point
274 after the inserted text.
275 @end defun
276
277 @defun insert-char character count
278 This function inserts @var{count} instances of @var{character} into the
279 current buffer before point. The argument @var{count} must be a number,
280 and @var{character} must be a character. The value is @code{nil}.
281 @c It's unfortunate that count comes second. Not like make-string, etc.
282 @end defun
283
284 @defun insert-buffer-substring from-buffer-or-name &optional start end
285 This function inserts a portion of buffer @var{from-buffer-or-name}
286 (which must already exist) into the current buffer before point. The
287 text inserted is the region from @var{start} and @var{end}. (These
288 arguments default to the beginning and end of the accessible portion of
289 that buffer.) This function returns @code{nil}.
290
291 In this example, the form is executed with buffer @samp{bar} as the
292 current buffer. We assume that buffer @samp{bar} is initially empty.
293
294 @example
295 @group
296 ---------- Buffer: foo ----------
297 We hold these truths to be self-evident, that all
298 ---------- Buffer: foo ----------
299 @end group
300
301 @group
302 (insert-buffer-substring "foo" 1 20)
303 @result{} nil
304
305 ---------- Buffer: bar ----------
306 We hold these truth
307 ---------- Buffer: bar ----------
308 @end group
309 @end example
310 @end defun
311
312 @xref{Sticky Properties}, for other insertion functions that inherit
313 text properties from the nearby text.
314
315 @node Commands for Insertion
316 @section User-Level Insertion Commands
317
318 This section describes higher-level commands for inserting text,
319 commands intended primarily for the user but useful also in Lisp
320 programs.
321
322 @deffn Command insert-buffer from-buffer-or-name
323 This command inserts the entire contents of @var{from-buffer-or-name}
324 (which must exist) into the current buffer after point. It leaves
325 the mark after the inserted text. The value is @code{nil}.
326 @end deffn
327
328 @deffn Command self-insert-command count
329 @cindex character insertion
330 @cindex self-insertion
331 This command inserts the last character typed @var{count} times and
332 returns @code{nil}. Most printing characters are bound to this command.
333 In routine use, @code{self-insert-command} is the most frequently called
334 function in Emacs, but programs rarely use it except to install it on a
335 keymap.
336
337 In an interactive call, @var{count} is the numeric prefix argument.
338
339 This function calls @code{auto-fill-function} if the current column number
340 is greater than the value of @code{fill-column} and the character
341 inserted is a space (@pxref{Auto Filling}).
342
343 @c Cross refs reworded to prevent overfull hbox. --rjc 15mar92
344 This function performs abbrev expansion if Abbrev mode is enabled and
345 the inserted character does not have word-constituent
346 syntax. (@xref{Abbrevs}, and @ref{Syntax Class Table}.)
347
348 This function is also responsible for calling
349 @code{blink-paren-function} when the inserted character has close
350 parenthesis syntax (@pxref{Blinking}).
351 @end deffn
352
353 @deffn Command newline &optional number-of-newlines
354 This command inserts newlines into the current buffer before point.
355 If @var{number-of-newlines} is supplied, that many newline characters
356 are inserted.
357
358 @cindex newline and Auto Fill mode
359 In Auto Fill mode, @code{newline} can break the preceding line if
360 @var{number-of-newlines} is not supplied. When this happens, it
361 actually inserts two newlines at different places: one at point, and
362 another earlier in the line. @code{newline} does not auto-fill if
363 @var{number-of-newlines} is non-@code{nil}.
364
365 The value returned is @code{nil}. In an interactive call, @var{count}
366 is the numeric prefix argument.
367 @end deffn
368
369 @deffn Command split-line
370 This command splits the current line, moving the portion of the line
371 after point down vertically, so that it is on the next line directly
372 below where it was before. Whitespace is inserted as needed at the
373 beginning of the lower line, using the @code{indent-to} function.
374 @code{split-line} returns the position of point.
375
376 Programs hardly ever use this function.
377 @end deffn
378
379 @defvar overwrite-mode
380 This variable controls whether overwrite mode is in effect: a
381 non-@code{nil} value enables the mode. It is automatically made
382 buffer-local when set in any fashion.
383 @end defvar
384
385 @node Deletion
386 @section Deletion of Text
387
388 @cindex deletion vs killing
389 Deletion means removing part of the text in a buffer, without saving
390 it in the kill ring (@pxref{The Kill Ring}). Deleted text can't be
391 yanked, but can be reinserted using the undo mechanism (@pxref{Undo}).
392 Some deletion functions save text in the kill ring in some cases
393 but not in the usual case.
394
395 All of the deletion functions operate on the current buffer, and all
396 return a value of @code{nil}.
397
398 @defun erase-buffer
399 This function deletes the entire text of the current buffer, leaving it
400 empty. If the buffer is read-only, it signals a @code{buffer-read-only}
401 error. Otherwise, it deletes the text without asking for any
402 confirmation. It returns @code{nil}.
403
404 Normally, deleting a large amount of text from a buffer inhibits further
405 auto-saving of that buffer ``because it has shrunk''. However,
406 @code{erase-buffer} does not do this, the idea being that the future
407 text is not really related to the former text, and its size should not
408 be compared with that of the former text.
409 @end defun
410
411 @deffn Command delete-region start end
412 This command deletes the text in the current buffer in the region
413 defined by @var{start} and @var{end}. The value is @code{nil}.
414 @end deffn
415
416 @deffn Command delete-char count &optional killp
417 This command deletes @var{count} characters directly after point, or
418 before point if @var{count} is negative. If @var{killp} is
419 non-@code{nil}, then it saves the deleted characters in the kill ring.
420
421 In an interactive call, @var{count} is the numeric prefix argument, and
422 @var{killp} is the unprocessed prefix argument. Therefore, if a prefix
423 argument is supplied, the text is saved in the kill ring. If no prefix
424 argument is supplied, then one character is deleted, but not saved in
425 the kill ring.
426
427 The value returned is always @code{nil}.
428 @end deffn
429
430 @deffn Command delete-backward-char count &optional killp
431 @cindex delete previous char
432 This command deletes @var{count} characters directly before point, or
433 after point if @var{count} is negative. If @var{killp} is
434 non-@code{nil}, then it saves the deleted characters in the kill ring.
435
436 In an interactive call, @var{count} is the numeric prefix argument, and
437 @var{killp} is the unprocessed prefix argument. Therefore, if a prefix
438 argument is supplied, the text is saved in the kill ring. If no prefix
439 argument is supplied, then one character is deleted, but not saved in
440 the kill ring.
441
442 The value returned is always @code{nil}.
443 @end deffn
444
445 @deffn Command backward-delete-char-untabify count &optional killp
446 @cindex tab deletion
447 This command deletes @var{count} characters backward, changing tabs
448 into spaces. When the next character to be deleted is a tab, it is
449 first replaced with the proper number of spaces to preserve alignment
450 and then one of those spaces is deleted instead of the tab. If
451 @var{killp} is non-@code{nil}, then the command saves the deleted
452 characters in the kill ring.
453
454 Conversion of tabs to spaces happens only if @var{count} is positive.
455 If it is negative, exactly @minus{}@var{count} characters after point
456 are deleted.
457
458 In an interactive call, @var{count} is the numeric prefix argument, and
459 @var{killp} is the unprocessed prefix argument. Therefore, if a prefix
460 argument is supplied, the text is saved in the kill ring. If no prefix
461 argument is supplied, then one character is deleted, but not saved in
462 the kill ring.
463
464 The value returned is always @code{nil}.
465 @end deffn
466
467 @node User-Level Deletion
468 @section User-Level Deletion Commands
469
470 This section describes higher-level commands for deleting text,
471 commands intended primarily for the user but useful also in Lisp
472 programs.
473
474 @deffn Command delete-horizontal-space
475 @cindex deleting whitespace
476 This function deletes all spaces and tabs around point. It returns
477 @code{nil}.
478
479 In the following examples, we call @code{delete-horizontal-space} four
480 times, once on each line, with point between the second and third
481 characters on the successive line.
482
483 @example
484 @group
485 ---------- Buffer: foo ----------
486 I @point{}thought
487 I @point{} thought
488 We@point{} thought
489 Yo@point{}u thought
490 ---------- Buffer: foo ----------
491 @end group
492
493 @group
494 (delete-horizontal-space) ; @r{Four times.}
495 @result{} nil
496
497 ---------- Buffer: foo ----------
498 Ithought
499 Ithought
500 Wethought
501 You thought
502 ---------- Buffer: foo ----------
503 @end group
504 @end example
505 @end deffn
506
507 @deffn Command delete-indentation &optional join-following-p
508 This function joins the line point is on to the previous line, deleting
509 any whitespace at the join and in some cases replacing it with one
510 space. If @var{join-following-p} is non-@code{nil},
511 @code{delete-indentation} joins this line to the following line
512 instead. The value is @code{nil}.
513
514 If there is a fill prefix, and the second of the lines being joined
515 starts with the prefix, then @code{delete-indentation} deletes the
516 fill prefix before joining the lines.
517
518 In the example below, point is located on the line starting
519 @samp{events}, and it makes no difference if there are trailing spaces
520 in the preceding line.
521
522 @smallexample
523 ---------- Buffer: foo ----------
524 When in the course of human
525 @point{} events, it becomes necessary
526 ---------- Buffer: foo ----------
527
528 (delete-indentation)
529 @result{} nil
530
531 ---------- Buffer: foo ----------
532 When in the course of human@point{} events, it becomes necessary
533 ---------- Buffer: foo ----------
534 @end smallexample
535
536 After the lines are joined, the function @code{fixup-whitespace} is
537 responsible for deciding whether to leave a space at the junction.
538 @end deffn
539
540 @defun fixup-whitespace
541 This function replaces all the white space surrounding point with either
542 one space or no space, according to the context. It returns @code{nil}.
543
544 At the beginning or end of a line, the appropriate amount of space is
545 none. Before a character with close parenthesis syntax, or after a
546 character with open parenthesis or expression-prefix syntax, no space is
547 also appropriate. Otherwise, one space is appropriate. @xref{Syntax
548 Class Table}.
549
550 In the example below, @code{fixup-whitespace} is called the first time
551 with point before the word @samp{spaces} in the first line. for the
552 second invocation, Point is directly after the @samp{(}.
553
554 @smallexample
555 @group
556 ---------- Buffer: foo ----------
557 This has too many @point{}spaces
558 This has too many spaces at the start of (@point{} this list)
559 ---------- Buffer: foo ----------
560 @end group
561
562 @group
563 (fixup-whitespace)
564 @result{} nil
565 (fixup-whitespace)
566 @result{} nil
567 @end group
568
569 @group
570 ---------- Buffer: foo ----------
571 This has too many spaces
572 This has too many spaces at the start of (this list)
573 ---------- Buffer: foo ----------
574 @end group
575 @end smallexample
576 @end defun
577
578 @deffn Command just-one-space
579 @comment !!SourceFile simple.el
580 This command replaces any spaces and tabs around point with a single
581 space. It returns @code{nil}.
582 @end deffn
583
584 @deffn Command delete-blank-lines
585 This function deletes blank lines surrounding point. If point is on a
586 blank line with one or more blank lines before or after it, then all but
587 one of them are deleted. If point is on an isolated blank line, then it
588 is deleted. If point is on a nonblank line, the command deletes all
589 blank lines following it.
590
591 A blank line is defined as a line containing only tabs and spaces.
592
593 @code{delete-blank-lines} returns @code{nil}.
594 @end deffn
595
596 @node The Kill Ring
597 @section The Kill Ring
598 @cindex kill ring
599
600 @dfn{Kill} functions delete text like the deletion functions, but save
601 it so that the user can reinsert it by @dfn{yanking}. Most of these
602 functions have @samp{kill-} in their name. By contrast, the functions
603 whose names start with @samp{delete-} normally do not save text for
604 yanking (though they can still be undone); these are ``deletion''
605 functions.
606
607 Most of the kill commands are primarily for interactive use, and are
608 not described here. What we do describe are the functions provided for
609 use in writing such commands. You can use these functions to write
610 commands for killing text. When you need to deleting text for internal
611 purposes within a Lisp function, you should normally use deletion
612 functions, so as not to disturb the kill ring contents.
613 @xref{Deletion}.
614
615 Killed text is saved for later yanking in the @dfn{kill ring}. This
616 is a list which holds, not just the last text kill, but a number of
617 recent kills. We call this a ``ring'' because yanking treats it as a
618 cyclic order. The list is kept in the variable @code{kill-ring}, and
619 can be operated on with the usual functions for lists; there are also
620 specialized functions, described in this section, which treat it as a
621 ring.
622
623 Some people think this use of the word ``kill'' is unfortunate, since
624 it refers to operations which specifically @emph{do not} destroy the
625 entities ``killed''. This is in sharp contrast to ordinary life, in
626 which death is permanent and ``killed'' entities do not come back to
627 life. Therefore, other metaphors have been proposed. For example, the
628 term ``cut ring'' makes sense to people who, in pre-computer days, used
629 scissors and paste to cut up and rearrange manuscripts. However, it
630 would be difficult to change the terminology now.
631
632 @menu
633 * Kill Ring Concepts:: What text looks like in the kill ring.
634 * Kill Functions:: Functions that kill text.
635 * Yank Commands:: Commands that access the kill ring.
636 * Low Level Kill Ring:: Functions and variables for kill ring access.
637 * Internals of Kill Ring:: Variables that hold kill-ring data.
638 @end menu
639
640 @node Kill Ring Concepts
641 @comment node-name, next, previous, up
642 @subsection Kill Ring Concepts
643
644 The kill ring records killed text as strings in a list, most recent
645 first. A short kill ring, for example, might look like this:
646
647 @example
648 ("some text" "a different piece of text" "even older text")
649 @end example
650
651 @noindent
652 When the list reaches @code{kill-ring-max} entries in length, adding a
653 new entry automatically deletes the last entry.
654
655 When kill commands are interwoven with other commands, each kill
656 command makes a new entry in the kill ring. Multiple kill commands in
657 succession build up a single entry in the kill ring, which would be
658 yanked as a unit. The second and subsequent consecutive kill commands
659 add text to the entry made by the first one.
660
661 For yanking, one entry in the kill ring is designated the ``front'' of
662 the ring. Some yank commands ``rotate'' the ring by designating a
663 different element as the ``front.'' But this virtual rotation doesn't
664 change the list itself---the most recent entry always comes first in the
665 list.
666
667 @node Kill Functions
668 @comment node-name, next, previous, up
669 @subsection Functions for Killing
670
671 @code{kill-region} is the usual subroutine for killing text. Any
672 command that calls this function is a ``kill command'' (and should
673 probably have @samp{kill} in its name). @code{kill-region} puts the
674 newly killed text in a new element at the beginning of the kill ring or
675 adds it to the most recent element. It uses the @code{last-command}
676 variable to determine whether the previous was a kill command, and if so
677 appends the killed text to the most recent entry.
678
679 @deffn Command kill-region start end
680 This function kills the text in the region defined by @var{start} and
681 @var{end}. The text is deleted but saved in the kill ring. The value
682 is always @code{nil}.
683
684 In an interactive call, @var{start} and @var{end} are point and
685 the mark.
686
687 @c Emacs 19 feature
688 If the buffer is read-only, @code{kill-region} modifies the kill ring
689 just the same, then signals an error without modifying the buffer. This
690 is convenient because it lets the user use all the kill commands to copy
691 text into the kill ring from a read-only buffer.
692 @end deffn
693
694 @deffn Command copy-region-as-kill start end
695 This command saves the region defined by @var{start} and @var{end} on
696 the kill ring, but does not delete the text from the buffer. It returns
697 @code{nil}. It also indicates the extent of the text copied by moving
698 the cursor momentarily, or by displaying a message in the echo area.
699
700 Don't call @code{copy-region-as-kill} in Lisp programs unless you aim to
701 support Emacs 18. For Emacs 19, it is better to use @code{kill-new} or
702 @code{kill-append} instead. @xref{Low Level Kill Ring}.
703 @end deffn
704
705 @node Yank Commands
706 @comment node-name, next, previous, up
707 @subsection Functions for Yanking
708
709 @dfn{Yanking} means reinserting an entry of previously killed text
710 from the kill ring.
711
712 @deffn Command yank &optional arg
713 @cindex inserting killed text
714 This command inserts before point the text in the first entry in the
715 kill ring. It positions the mark at the beginning of that text, and
716 point at the end.
717
718 If @var{arg} is a list (which occurs interactively when the user
719 types @kbd{C-u} with no digits), then @code{yank} inserts the text as
720 described above, but puts point before the yanked text and puts the mark
721 after it.
722
723 If @var{arg} is a number, then @code{yank} inserts the @var{arg}th most
724 recently killed text---the @var{arg}th element of the kill ring list.
725
726 @code{yank} does not alter the contents of the kill ring or rotate it.
727 It returns @code{nil}.
728 @end deffn
729
730 @deffn Command yank-pop arg
731 This command replaces the just-yanked entry from the kill ring with a
732 different entry from the kill ring.
733
734 This is allowed only immediately after a @code{yank} or another
735 @code{yank-pop}. At such a time, the region contains text that was just
736 inserted by yanking. @code{yank-pop} deletes that text and inserts in
737 its place a different piece of killed text. It does not add the deleted
738 text to the kill ring, since it is already in the kill ring somewhere.
739
740 If @var{arg} is @code{nil}, then the replacement text is the previous
741 element of the kill ring. If @var{arg} is numeric, the replacement is
742 the @var{arg}th previous kill. If @var{arg} is negative, a more recent
743 kill is the replacement.
744
745 The sequence of kills in the kill ring wraps around, so that after the
746 oldest one comes the newest one, and before the newest one goes the
747 oldest.
748
749 The value is always @code{nil}.
750 @end deffn
751
752 @node Low Level Kill Ring
753 @subsection Low Level Kill Ring
754
755 These functions and variables provide access to the kill ring at a lower
756 level, but still convenient for use in Lisp programs. They take care of
757 interaction with X Window selections. They do not exist in Emacs
758 version 18.
759
760 @defun current-kill n &optional do-not-move
761 The function @code{current-kill} rotates the yanking pointer in the
762 kill ring by @var{n} places, and returns the text at that place in the
763 ring.
764
765 If the optional second argument @var{do-not-move} is non-@code{nil},
766 then @code{current-kill} doesn't alter the yanking pointer; it just
767 returns the @var{n}th kill forward from the current yanking pointer.
768
769 If @var{n} is zero, indicating a request for the latest kill,
770 @code{current-kill} calls the value of
771 @code{interprogram-paste-function} (documented below) before consulting
772 the kill ring.
773 @end defun
774
775 @defun kill-new string
776 This function puts the text @var{string} into the kill ring as a new
777 entry at the front of the ring. It discards the oldest entry if
778 appropriate. It also invokes the value of
779 @code{interprogram-cut-function} (see below).
780 @end defun
781
782 @defun kill-append string before-p
783 This function appends the text @var{string} to the first entry in the
784 kill ring. Normally @var{string} goes at the end of the entry, but if
785 @var{before-p} is non-@code{nil}, it goes at the beginning. This
786 function also invokes the value of @code{interprogram-cut-function} (see
787 below).
788 @end defun
789
790 @defvar interprogram-paste-function
791 This variable provides a way of transferring killed text from other
792 programs, when you are using a window system. Its value should be
793 @code{nil} or a function of no arguments.
794
795 If the value is a function, @code{current-kill} calls it to get the
796 ``most recent kill''. If the function returns a non-@code{nil} value,
797 then that value is used as the ``most recent kill''. If it returns
798 @code{nil}, then the first element of @code{kill-ring} is used.
799
800 The normal use of this hook is to get the X server's primary selection
801 as the most recent kill, even if the selection belongs to another X
802 client. @xref{X Selections}.
803 @end defvar
804
805 @defvar interprogram-cut-function
806 This variable provides a way of communicating killed text to and from
807 other programs, when you are using a window system. Its value should be
808 @code{nil} or a function of one argument.
809
810 If the value is a function, @code{kill-new} and @code{kill-append} call
811 it with the new first element of the kill ring as an argument.
812
813 The normal use of this hook is to set the X server's primary selection
814 to the newly killed text.
815 @end defvar
816
817 @node Internals of Kill Ring
818 @comment node-name, next, previous, up
819 @subsection Internals of the Kill Ring
820
821 The variable @code{kill-ring} holds the kill ring contents, in the
822 form of a list of strings. The most recent kill is always at the front
823 of the list.
824
825 The @code{kill-ring-yank-pointer} variable points to a link in the
826 kill ring list, whose @sc{car} is the text to yank next. Moving
827 @code{kill-ring-yank-pointer} to a different link is called
828 @dfn{rotating the kill ring}; we say it identifies the ``front'' of the
829 ring. We call the kill ring a ``ring'' because the functions that move
830 the yank pointer wrap around from the end of the list to the beginning,
831 or vice-versa. Rotation of the kill ring is virtual; it does not change
832 the value of @code{kill-ring}.
833
834 Both @code{kill-ring} and @code{kill-ring-yank-pointer} are Lisp
835 variables whose values are normally lists. The word ``pointer'' in the
836 name of the @code{kill-ring-yank-pointer} indicates that the variable's
837 purpose is to identify one element of the list for use by the next yank
838 command.
839
840 The value of @code{kill-ring-yank-pointer} is always @code{eq} to one
841 of the links in the kill ring list. The element it identifies is the
842 @sc{car} of that link. Kill commands, which change the kill ring, also
843 set this variable from @code{kill-ring}. The effect is to rotate the
844 ring so that the newly killed text is at front.
845
846 Here is a diagram that shows the variable @code{kill-ring-yank-pointer}
847 pointing to the second entry in the kill ring @code{("some text" "a
848 different piece of text" "yet older text")}.
849
850 @example
851 @group
852 kill-ring kill-ring-yank-pointer
853 | |
854 | ___ ___ ---> ___ ___ ___ ___
855 --> |___|___|------> |___|___|--> |___|___|--> nil
856 | | |
857 | | |
858 | | -->"yet older text"
859 | |
860 | --> "a different piece of text"
861 |
862 --> "some text"
863 @end group
864 @end example
865
866 @noindent
867 This state of affairs might occur after @kbd{C-y} (@code{yank})
868 immediately followed by @kbd{M-y} (@code{yank-pop}).
869
870 @defvar kill-ring
871 This variable holds list of killed text sequences, most recently killed
872 first.
873 @end defvar
874
875 @defvar kill-ring-yank-pointer
876 This variable's value indicates which element of the kill ring is at the
877 ``front'' of the ring for yanking. More precisely, the value is a tail
878 of the value of @code{kill-ring}, and its @sc{car} is the kill string
879 that @kbd{C-y} should yank.
880 @end defvar
881
882 @defopt kill-ring-max
883 The value of this variable is the maximum length to which the kill
884 ring can grow, before elements are thrown away at the end. The default
885 value for @code{kill-ring-max} is 30.
886 @end defopt
887
888 @node Undo
889 @comment node-name, next, previous, up
890 @section Undo
891 @cindex redo
892
893 Most buffers have an @dfn{undo list} which records all changes made to
894 the buffer's text so that they can be undone. (The buffers which don't
895 have one are usually special-purpose buffers for which Emacs assumes
896 that undoing is not useful.) All the primitives which modify the text
897 in the buffer automatically add elements to the front of the undo list,
898 which is in the variable @code{buffer-undo-list}.
899
900 @defvar buffer-undo-list
901 This variable's value is the undo list of the current buffer.
902 A value of @code{t} disables the recording of undo information.
903 @end defvar
904
905 Here are the kinds of elements an undo list can have:
906
907 @table @code
908 @item @var{integer}
909 This kind of element records a previous value of point. Ordinary cursor
910 motion does not get any sort of undo record, but deletion commands use
911 these entries to record where point was before the command.
912
913 @item (@var{beg} . @var{end})
914 This kind of element indicates how to delete text that was inserted.
915 Upon insertion, the text occupied the range @var{beg}--@var{end} in the
916 buffer.
917
918 @item (@var{pos} . @var{deleted})
919 This kind of element indicates how to reinsert text that was deleted.
920 The deleted text itself is the string @var{deleted}. The place to
921 reinsert it is @var{pos}.
922
923 @item (t @var{high} . @var{low})
924 This kind of element indicates that an unmodified buffer became
925 modified. The elements @var{high} and @var{low} are two integers, each
926 recording 16 bits of the visited file's modification time as of when it
927 was previously visited or saved. @code{primitive-undo} uses those
928 values to determine whether to mark the buffer as unmodified once again;
929 it does so only if the file's modification time matches those numbers.
930
931 @item (nil @var{property} @var{value} @var{beg} . @var{end})
932 This kind of element records a change in a text property.
933 Here's how you might undo the change:
934
935 @example
936 (put-text-property @var{beg} @var{end} @var{property} @var{value})
937 @end example
938
939 @item nil
940 This element is a boundary. The elements between two boundaries are
941 called a @dfn{change group}; normally, each change group corresponds to
942 one keyboard command, and undo commands normally undo an entire group as
943 a unit.
944 @end table
945
946 @defun undo-boundary
947 This function places a boundary element in the undo list. The undo
948 command stops at such a boundary, and successive undo commands undo
949 to earlier and earlier boundaries. This function returns @code{nil}.
950
951 The editor command loop automatically creates an undo boundary between
952 keystroke commands. Thus, each undo normally undoes the effects of one
953 command. Calling this function explicitly is useful for splitting the
954 effects of a command into more than one unit. For example,
955 @code{query-replace} calls this function after each replacement so that
956 the user can undo individual replacements one by one.
957 @end defun
958
959 @defun primitive-undo count list
960 This is the basic function for undoing elements of an undo list.
961 It undoes the first @var{count} elements of @var{list}, returning
962 the rest of @var{list}. You could write this function in Lisp,
963 but it is convenient to have it in C.
964
965 @code{primitive-undo} adds elements to the buffer's undo list when it
966 changes the buffer. Undo commands avoid confusion by saving the undo
967 list value at the beginning of a sequence of undo operations. Then the
968 undo operations use and update the saved value. The new elements added
969 by undoing are not part of the saved value, so they don't interfere with
970 continuing to undo.
971 @end defun
972
973 @node Maintaining Undo
974 @section Maintaining Undo Lists
975
976 This section describes how to enable and disable undo information for
977 a given buffer. It also explains how the undo list is truncated
978 automatically so it doesn't get too big.
979
980 Recording of undo information in a newly created buffer is normally
981 enabled to start with; but if the buffer name starts with a space, the
982 undo recording is initially disabled. You can explicitly enable or
983 disable undo recording with the following two functions, or by setting
984 @code{buffer-undo-list} yourself.
985
986 @deffn Command buffer-enable-undo &optional buffer-or-name
987 This command enables recording undo information for buffer
988 @var{buffer-or-name}, so that subsequent changes can be undone. If no
989 argument is supplied, then the current buffer is used. This function
990 does nothing if undo recording is already enabled in the buffer. It
991 returns @code{nil}.
992
993 In an interactive call, @var{buffer-or-name} is the current buffer.
994 You cannot specify any other buffer.
995 @end deffn
996
997 @defun buffer-disable-undo &optional buffer
998 @defunx buffer-flush-undo &optional buffer
999 @cindex disable undo
1000 This function discards the undo list of @var{buffer}, and disables
1001 further recording of undo information. As a result, it is no longer
1002 possible to undo either previous changes or any subsequent changes. If
1003 the undo list of @var{buffer} is already disabled, this function
1004 has no effect.
1005
1006 This function returns @code{nil}. It cannot be called interactively.
1007
1008 The name @code{buffer-flush-undo} is not considered obsolete, but the
1009 preferred name @code{buffer-disable-undo} is new as of Emacs versions
1010 19.
1011 @end defun
1012
1013 As editing continues, undo lists get longer and longer. To prevent
1014 them from using up all available memory space, garbage collection trims
1015 them back to size limits you can set. (For this purpose, the ``size''
1016 of an undo list measures the cons cells that make up the list, plus the
1017 strings of deleted text.) Two variables control the range of acceptable
1018 sizes: @code{undo-limit} and @code{undo-strong-limit}.
1019
1020 @defvar undo-limit
1021 This is the soft limit for the acceptable size of an undo list. The
1022 change group at which this size is exceeded is the last one kept.
1023 @end defvar
1024
1025 @defvar undo-strong-limit
1026 The upper limit for the acceptable size of an undo list. The change
1027 group at which this size is exceeded is discarded itself (along with all
1028 subsequent changes). There is one exception: garbage collection always
1029 keeps the very latest change group no matter how big it is.
1030 @end defvar
1031
1032 @node Filling
1033 @comment node-name, next, previous, up
1034 @section Filling
1035 @cindex filling, explicit
1036
1037 @dfn{Filling} means adjusting the lengths of lines (by moving the line
1038 breaks) so that they are nearly (but no greater than) a specified
1039 maximum width. Additionally, lines can be @dfn{justified}, which means
1040 that spaces are inserted between words to make the line exactly the
1041 specified width. The width is controlled by the variable
1042 @code{fill-column}. For ease of reading, lines should be no longer than
1043 70 or so columns.
1044
1045 You can use Auto Fill mode (@pxref{Auto Filling}) to fill text
1046 automatically as you insert it, but changes to existing text may leave
1047 it improperly filled. Then you must fill the text explicitly.
1048
1049 Most of the functions in this section return values that are not
1050 meaningful.
1051
1052 @deffn Command fill-paragraph justify-flag
1053 @cindex filling a paragraph
1054 This command fills the paragraph at or after point. If
1055 @var{justify-flag} is non-@code{nil}, each line is justified as well.
1056 It uses the ordinary paragraph motion commands to find paragraph
1057 boundaries. @xref{Paragraphs,,, emacs, The Emacs Manual}.
1058 @end deffn
1059
1060 @deffn Command fill-region start end &optional justify-flag
1061 This command fills each of the paragraphs in the region from @var{start}
1062 to @var{end}. It justifies as well if @var{justify-flag} is
1063 non-@code{nil}.
1064
1065 The variable @code{paragraph-separate} controls how to distinguish
1066 paragraphs. @xref{Standard Regexps}.
1067 @end deffn
1068
1069 @deffn Command fill-individual-paragraphs start end &optional justify-flag mail-flag
1070 This command fills each paragraph in the region according to its
1071 individual fill prefix. Thus, if the lines of a paragraph were indented
1072 with spaces, the filled paragraph will remain indented in the same
1073 fashion.
1074
1075 The first two arguments, @var{start} and @var{end}, are the beginning
1076 and end of the region to be filled. The third and fourth arguments,
1077 @var{justify-flag} and @var{mail-flag}, are optional. If
1078 @var{justify-flag} is non-@code{nil}, the paragraphs are justified as
1079 well as filled. If @var{mail-flag} is non-@code{nil}, it means the
1080 function is operating on a mail message and therefore should not fill
1081 the header lines.
1082
1083 Ordinarily, @code{fill-individual-paragraphs} regards each change in
1084 indentation as starting a new paragraph. If
1085 @code{fill-individual-varying-indent} is non-@code{nil}, then only
1086 separator lines separate paragraphs. That mode can handle paragraphs
1087 with extra indentation on the first line.
1088 @end deffn
1089
1090 @defopt fill-individual-varying-indent
1091 This variable alters the action of @code{fill-individual-paragraphs} as
1092 described above.
1093 @end defopt
1094
1095 @deffn Command fill-region-as-paragraph start end &optional justify-flag
1096 This command considers a region of text as a paragraph and fills it. If
1097 the region was made up of many paragraphs, the blank lines between
1098 paragraphs are removed. This function justifies as well as filling when
1099 @var{justify-flag} is non-@code{nil}. In an interactive call, any
1100 prefix argument requests justification.
1101
1102 In Adaptive Fill mode, which is enabled by default,
1103 @code{fill-region-as-paragraph} on an indented paragraph when there is
1104 no fill prefix uses the indentation of the second line of the paragraph
1105 as the fill prefix.
1106 @end deffn
1107
1108 @deffn Command justify-current-line
1109 This command inserts spaces between the words of the current line so
1110 that the line ends exactly at @code{fill-column}. It returns
1111 @code{nil}.
1112 @end deffn
1113
1114 @defopt fill-column
1115 This buffer-local variable specifies the maximum width of filled
1116 lines. Its value should be an integer, which is a number of columns.
1117 All the filling, justification and centering commands are affected by
1118 this variable, including Auto Fill mode (@pxref{Auto Filling}).
1119
1120 As a practical matter, if you are writing text for other people to
1121 read, you should set @code{fill-column} to no more than 70. Otherwise
1122 the line will be too long for people to read comfortably, and this can
1123 make the text seem clumsy.
1124 @end defopt
1125
1126 @defvar default-fill-column
1127 The value of this variable is the default value for @code{fill-column} in
1128 buffers that do not override it. This is the same as
1129 @code{(default-value 'fill-column)}.
1130
1131 The default value for @code{default-fill-column} is 70.
1132 @end defvar
1133
1134 @node Auto Filling
1135 @comment node-name, next, previous, up
1136 @section Auto Filling
1137 @cindex filling, automatic
1138 @cindex Auto Fill mode
1139
1140 Auto Fill mode is a minor mode which fills lines automatically as text
1141 as inserted. This section describes the hook and the two variables used
1142 by Auto Fill mode. For a description of functions that you can call
1143 explicitly to fill and justify existing text, see @ref{Filling}.
1144
1145 @defvar auto-fill-function
1146 The value of this variable should be a function (of no arguments) to
1147 be called after self-inserting a space at a column beyond
1148 @code{fill-column}. It may be @code{nil}, in which case nothing
1149 special is done.
1150
1151 The value of @code{auto-fill-function} is @code{do-auto-fill} when
1152 Auto-Fill mode is enabled. That is a function whose sole purpose is to
1153 implement the usual strategy for breaking a line.
1154
1155 @quotation
1156 In older Emacs versions, this variable was named @code{auto-fill-hook},
1157 but since it is not called with the standard convention for hooks, it
1158 was renamed to @code{auto-fill-function} in version 19.
1159 @end quotation
1160 @end defvar
1161
1162 @node Sorting
1163 @section Sorting Text
1164 @cindex sorting text
1165
1166 The sorting functions described in this section all rearrange text in
1167 a buffer. This is in contrast to the function @code{sort}, which
1168 rearranges the order of the elements of a list (@pxref{Rearrangement}).
1169 The values returned by these functions are not meaningful.
1170
1171 @defun sort-subr reverse nextrecfun endrecfun &optional startkeyfun endkeyfun
1172 This function is the general text sorting routine that divides a buffer
1173 into records and sorts them. Most of the commands in this section use
1174 this function.
1175
1176 To understand how @code{sort-subr} works, consider the whole accessible
1177 portion of the buffer as being divided into disjoint pieces called
1178 @dfn{sort records}. The records may or may not be contiguous; they may
1179 not overlap. A portion of each sort record (perhaps all of it) is
1180 designated as the sort key. Sorting rearranges the records in order by
1181 their sort keys.
1182
1183 Usually, the records are rearranged in order of ascending sort key.
1184 If the first argument to the @code{sort-subr} function, @var{reverse},
1185 is non-@code{nil}, the sort records are rearranged in order of
1186 descending sort key.
1187
1188 The next four arguments to @code{sort-subr} are functions that are
1189 called to move point across a sort record. They are called many times
1190 from within @code{sort-subr}.
1191
1192 @enumerate
1193 @item
1194 @var{nextrecfun} is called with point at the end of a record. This
1195 function moves point to the start of the next record. The first record
1196 is assumed to start at the position of point when @code{sort-subr} is
1197 called. Therefore, you should usually move point to the beginning of
1198 the buffer before calling @code{sort-subr}.
1199
1200 This function can indicate there are no more sort records by leaving
1201 point at the end of the buffer.
1202
1203 @item
1204 @var{endrecfun} is called with point within a record. It moves point to
1205 the end of the record.
1206
1207 @item
1208 @var{startkeyfun} is called to move point from the start of a record to
1209 the start of the sort key. This argument is optional; if it is omitted,
1210 the whole record is the sort key. If supplied, the function should
1211 either return a non-@code{nil} value to be used as the sort key, or
1212 return @code{nil} to indicate that the sort key is in the buffer
1213 starting at point. In the latter case, @var{endkeyfun} is called to
1214 find the end of the sort key.
1215
1216 @item
1217 @var{endkeyfun} is called to move point from the start of the sort key
1218 to the end of the sort key. This argument is optional. If
1219 @var{startkeyfun} returns @code{nil} and this argument is omitted (or
1220 @code{nil}), then the sort key extends to the end of the record. There
1221 is no need for @var{endkeyfun} if @var{startkeyfun} returns a
1222 non-@code{nil} value.
1223 @end enumerate
1224
1225 As an example of @code{sort-subr}, here is the complete function
1226 definition for @code{sort-lines}:
1227
1228 @example
1229 @group
1230 ;; @r{Note that the first two lines of doc string}
1231 ;; @r{are effectively one line when viewed by a user.}
1232 (defun sort-lines (reverse beg end)
1233 "Sort lines in region alphabetically.
1234 Called from a program, there are three arguments:
1235 @end group
1236 @group
1237 REVERSE (non-nil means reverse order),
1238 and BEG and END (the region to sort)."
1239 (interactive "P\nr")
1240 (save-restriction
1241 (narrow-to-region beg end)
1242 (goto-char (point-min))
1243 (sort-subr reverse
1244 'forward-line
1245 'end-of-line)))
1246 @end group
1247 @end example
1248
1249 Here @code{forward-line} moves point to the start of the next record,
1250 and @code{end-of-line} moves point to the end of record. We do not pass
1251 the arguments @var{startkeyfun} and @var{endkeyfun}, because the entire
1252 record is used as the sort key.
1253
1254 The @code{sort-paragraphs} function is very much the same, except that
1255 its @code{sort-subr} call looks like this:
1256
1257 @example
1258 @group
1259 (sort-subr reverse
1260 (function
1261 (lambda ()
1262 (skip-chars-forward "\n \t\f")))
1263 'forward-paragraph)
1264 @end group
1265 @end example
1266 @end defun
1267
1268 @deffn Command sort-regexp-fields reverse record-regexp key-regexp start end
1269 This command sorts the region between @var{start} and @var{end}
1270 alphabetically as specified by @var{record-regexp} and @var{key-regexp}.
1271 If @var{reverse} is a negative integer, then sorting is in reverse
1272 order.
1273
1274 Alphabetical sorting means that two sort keys are compared by
1275 comparing the first characters of each, the second characters of each,
1276 and so on. If a mismatch is found, it means that the sort keys are
1277 unequal; the sort key whose character is less at the point of first
1278 mismatch is the lesser sort key. The individual characters are compared
1279 according to their numerical values. Since Emacs uses the @sc{ASCII}
1280 character set, the ordering in that set determines alphabetical order.
1281 @c version 19 change
1282
1283 The value of the @var{record-regexp} argument specifies how to divide
1284 the buffer into sort records. At the end of each record, a search is
1285 done for this regular expression, and the text that matches it is the
1286 next record. For example, the regular expression @samp{^.+$}, which
1287 matches lines with at least one character besides a newline, would make
1288 each such line into a sort record. @xref{Regular Expressions}, for a
1289 description of the syntax and meaning of regular expressions.
1290
1291 The value of the @var{key-regexp} argument specifies what part of each
1292 record is the sort key. The @var{key-regexp} could match the whole
1293 record, or only a part. In the latter case, the rest of the record has
1294 no effect on the sorted order of records, but it is carried along when
1295 the record moves to its new position.
1296
1297 The @var{key-regexp} argument can refer to the text matched by a
1298 subexpression of @var{record-regexp}, or it can be a regular expression
1299 on its own.
1300
1301 If @var{key-regexp} is:
1302
1303 @table @asis
1304 @item @samp{\@var{digit}}
1305 then the text matched by the @var{digit}th @samp{\(...\)} parenthesis
1306 grouping in @var{record-regexp} is the sort key.
1307
1308 @item @samp{\&}
1309 then the whole record is the sort key.
1310
1311 @item a regular expression
1312 then @code{sort-regexp-fields} searches for a match for the regular
1313 expression within the record. If such a match is found, it is the sort
1314 key. If there is no match for @var{key-regexp} within a record then
1315 that record is ignored, which means its position in the buffer is not
1316 changed. (The other records may move around it.)
1317 @end table
1318
1319 For example, if you plan to sort all the lines in the region by the
1320 first word on each line starting with the letter @samp{f}, you should
1321 set @var{record-regexp} to @samp{^.*$} and set @var{key-regexp} to
1322 @samp{\<f\w*\>}. The resulting expression looks like this:
1323
1324 @example
1325 @group
1326 (sort-regexp-fields nil "^.*$" "\\<f\\w*\\>"
1327 (region-beginning)
1328 (region-end))
1329 @end group
1330 @end example
1331
1332 If you call @code{sort-regexp-fields} interactively, it prompts for
1333 @var{record-regexp} and @var{key-regexp} in the minibuffer.
1334 @end deffn
1335
1336 @deffn Command sort-lines reverse start end
1337 This command alphabetically sorts lines in the region between
1338 @var{start} and @var{end}. If @var{reverse} is non-@code{nil}, the sort
1339 is in reverse order.
1340 @end deffn
1341
1342 @deffn Command sort-paragraphs reverse start end
1343 This command alphabetically sorts paragraphs in the region between
1344 @var{start} and @var{end}. If @var{reverse} is non-@code{nil}, the sort
1345 is in reverse order.
1346 @end deffn
1347
1348 @deffn Command sort-pages reverse start end
1349 This command alphabetically sorts pages in the region between
1350 @var{start} and @var{end}. If @var{reverse} is non-@code{nil}, the sort
1351 is in reverse order.
1352 @end deffn
1353
1354 @deffn Command sort-fields field start end
1355 This command sorts lines in the region between @var{start} and
1356 @var{end}, comparing them alphabetically by the @var{field}th field
1357 of each line. Fields are separated by whitespace and numbered starting
1358 from 1. If @var{field} is negative, sorting is by the
1359 @w{@minus{}@var{field}th} field from the end of the line. This command
1360 is useful for sorting tables.
1361 @end deffn
1362
1363 @deffn Command sort-numeric-fields field start end
1364 This command sorts lines in the region between @var{start} and
1365 @var{end}, comparing them numerically by the @var{field}th field of each
1366 line. The specified field must contain a number in each line of the
1367 region. Fields are separated by whitespace and numbered starting from
1368 1. If @var{field} is negative, sorting is by the
1369 @w{@minus{}@var{field}th} field from the end of the line. This command
1370 is useful for sorting tables.
1371 @end deffn
1372
1373 @deffn Command sort-columns reverse &optional beg end
1374 This command sorts the lines in the region between @var{beg} and
1375 @var{end}, comparing them alphabetically by a certain range of columns.
1376 The column positions of @var{beg} and @var{end} bound the range of
1377 columns to sort on.
1378
1379 If @var{reverse} is non-@code{nil}, the sort is in reverse order.
1380
1381 One unusual thing about this command is that the entire line
1382 containing position @var{beg}, and the entire line containing position
1383 @var{end}, are included in the region sorted.
1384
1385 Note that @code{sort-columns} uses the @code{sort} utility program,
1386 and so cannot work properly on text containing tab characters. Use
1387 @kbd{M-x @code{untabify}} to convert tabs to spaces before sorting.
1388
1389 The @code{sort-columns} function did not work on VMS prior to Emacs 19.
1390 @end deffn
1391
1392 @node Columns
1393 @comment node-name, next, previous, up
1394 @section Counting Columns
1395 @cindex columns
1396 @cindex counting columns
1397 @cindex horizontal position
1398
1399 The column functions convert between a character position (counting
1400 characters from the beginning of the buffer) and a column position
1401 (counting screen characters from the beginning of a line).
1402
1403 A character counts according to the number of columns it occupies on
1404 the screen. This means control characters count as occupying 2 or 4
1405 columns, depending upon the value of @code{ctl-arrow}, and tabs count as
1406 occupying a number of columns that depends on the value of
1407 @code{tab-width} and on the column where the tab begins. @xref{Usual Display}.
1408
1409 Column number computations ignore the width of the window and the
1410 amount of horizontal scrolling. Consequently, a column value can be
1411 arbitrarily high. The first (or leftmost) column is numbered 0.
1412
1413 @defun current-column
1414 This function returns the horizontal position of point, measured in
1415 columns, counting from 0 at the left margin. The column position is the
1416 sum of the widths of all the displayed representations of the characters
1417 between the start of the current line and point.
1418
1419 For an example of using @code{current-column}, see the description of
1420 @code{count-lines} in @ref{Text Lines}.
1421 @end defun
1422
1423 @defun move-to-column column &optional force
1424 This function moves point to @var{column} in the current line. The
1425 calculation of @var{column} takes into account the widths of the
1426 displayed representations of the characters between the start of the
1427 line and point.
1428
1429 If column @var{column} is beyond the end of the line, point moves to the
1430 end of the line. If @var{column} is negative, point moves to the
1431 beginning of the line.
1432
1433 If it is impossible to move to column @var{column} because that is in
1434 the middle of a multicolumn character such as a tab, point moves to the
1435 end of that character. However, if @var{force} is non-@code{nil}, and
1436 @var{column} is in the middle of a tab, then @code{move-to-column}
1437 converts the tab into spaces so that it can move precisely to column
1438 @var{column}. Other multicolumn characters can cause anomalies despite
1439 @var{force}, since there is no way to split them.
1440
1441 The argument @var{force} also has an effect if the line isn't long
1442 enough to reach column @var{column}; in that case, it says to indent at
1443 the end of the line to reach that column.
1444
1445 If @var{column} is not an integer, an error is signaled.
1446
1447 The return value is the column number actually moved to.
1448 @end defun
1449
1450 @node Indentation
1451 @section Indentation
1452 @cindex indentation
1453
1454 The indentation functions are used to examine, move to, and change
1455 whitespace that is at the beginning of a line. Some of the functions
1456 can also change whitespace elsewhere on a line. Columns and indentation
1457 count from zero at the left margin.
1458
1459 @menu
1460 * Primitive Indent:: Functions used to count and insert indentation.
1461 * Mode-Specific Indent:: Customize indentation for different modes.
1462 * Region Indent:: Indent all the lines in a region.
1463 * Relative Indent:: Indent the current line based on previous lines.
1464 * Indent Tabs:: Adjustable, typewriter-like tab stops.
1465 * Motion by Indent:: Move to first non-blank character.
1466 @end menu
1467
1468 @node Primitive Indent
1469 @subsection Indentation Primitives
1470
1471 This section describes the primitive functions used to count and
1472 insert indentation. The functions in the following sections use these
1473 primitives.
1474
1475 @defun current-indentation
1476 @comment !!Type Primitive Function
1477 @comment !!SourceFile indent.c
1478 This function returns the indentation of the current line, which is
1479 the horizontal position of the first nonblank character. If the
1480 contents are entirely blank, then this is the horizontal position of the
1481 end of the line.
1482 @end defun
1483
1484 @deffn Command indent-to column &optional minimum
1485 @comment !!Type Primitive Function
1486 @comment !!SourceFile indent.c
1487 This function indents from point with tabs and spaces until
1488 @var{column} is reached. If @var{minimum} is specified and
1489 non-@code{nil}, then at least that many spaces are inserted even if this
1490 requires going beyond @var{column}. The value is the column at which
1491 the inserted indentation ends.
1492 @end deffn
1493
1494 @defopt indent-tabs-mode
1495 @comment !!SourceFile indent.c
1496 If this variable is non-@code{nil}, indentation functions can insert
1497 tabs as well as spaces. Otherwise, they insert only spaces. Setting
1498 this variable automatically makes it local to the current buffer.
1499 @end defopt
1500
1501 @node Mode-Specific Indent
1502 @subsection Indentation Controlled by Major Mode
1503
1504 An important function of each major mode is to customize the @key{TAB}
1505 key to indent properly for the language being edited. This section
1506 describes the mechanism of the @key{TAB} key and how to control it.
1507 The functions in this section return unpredictable values.
1508
1509 @defvar indent-line-function
1510 This variable's value is the function to be used by @key{TAB} (and
1511 various commands) to indent the current line. The command
1512 @code{indent-according-to-mode} does no more than call this function.
1513
1514 In Lisp mode, the value is the symbol @code{lisp-indent-line}; in C
1515 mode, @code{c-indent-line}; in Fortran mode, @code{fortran-indent-line}.
1516 In Fundamental mode, Text mode, and many other modes with no standard
1517 for indentation, the value is @code{indent-to-left-margin} (which is the
1518 default value).
1519 @end defvar
1520
1521 @deffn Command indent-according-to-mode
1522 This command calls the function in @code{indent-line-function} to
1523 indent the current line in a way appropriate for the current major mode.
1524 @end deffn
1525
1526 @deffn Command indent-for-tab-command
1527 This command calls the function in @code{indent-line-function} to indent
1528 the current line; except that if that function is
1529 @code{indent-to-left-margin}, it calls @code{insert-tab} instead. (That
1530 is a trivial command which inserts a tab character.)
1531 @end deffn
1532
1533 @defvar left-margin
1534 This variable is the column to which the default
1535 @code{indent-line-function} will indent. (That function is
1536 @code{indent-to-left-margin}.) In Fundamental mode, @key{LFD} indents
1537 to this column. This variable automatically becomes buffer-local when
1538 set in any fashion.
1539 @end defvar
1540
1541 @defun indent-to-left-margin
1542 This is the default @code{indent-line-function}, used in Fundamental
1543 mode, Text mode, etc. Its effect is to adjust the indentation at the
1544 beginning of the current line to the value specified by the variable
1545 @code{left-margin}. This may involve either inserting or deleting
1546 whitespace.
1547 @end defun
1548
1549 @deffn Command newline-and-indent
1550 @comment !!SourceFile simple.el
1551 This function inserts a newline, then indents the new line (the one
1552 following the newline just inserted) according to the major mode.
1553
1554 It does indentation by calling the current @code{indent-line-function}.
1555 In programming language modes, this is the same thing @key{TAB} does,
1556 but in some text modes, where @key{TAB} inserts a tab,
1557 @code{newline-and-indent} indents to the column specified by
1558 @code{left-margin}.
1559 @end deffn
1560
1561 @deffn Command reindent-then-newline-and-indent
1562 @comment !!SourceFile simple.el
1563 This command reindents the current line, inserts a newline at point,
1564 and then reindents the new line (the one following the newline just
1565 inserted).
1566
1567 This command does indentation on both lines according to the current
1568 major mode, by calling the current value of @code{indent-line-function}.
1569 In programming language modes, this is the same thing @key{TAB} does,
1570 but in some text modes, where @key{TAB} inserts a tab,
1571 @code{reindent-then-newline-and-indent} indents to the column specified
1572 by @code{left-margin}.
1573 @end deffn
1574
1575 @node Region Indent
1576 @subsection Indenting an Entire Region
1577
1578 This section describes commands which indent all the lines in the
1579 region. They return unpredictable values.
1580
1581 @deffn Command indent-region start end to-column
1582 This command indents each nonblank line starting between @var{start}
1583 (inclusive) and @var{end} (exclusive). If @var{to-column} is
1584 @code{nil}, @code{indent-region} indents each nonblank line by calling
1585 the current mode's indentation function, the value of
1586 @code{indent-line-function}.
1587
1588 If @var{to-column} is non-@code{nil}, it should be an integer
1589 specifying the number of columns of indentation; then this function
1590 gives each line exactly that much indentation, by either adding or
1591 deleting whitespace.
1592
1593 If there is a fill prefix, @code{indent-region} indents each line
1594 by making it start with the fill prefix.
1595 @end deffn
1596
1597 @defvar indent-region-function
1598 The value of this variable is a function that can be used by
1599 @code{indent-region} as a short cut. You should design the function so
1600 that it will produce the same results as indenting the lines of the
1601 region one by one, but presumably faster.
1602
1603 If the value is @code{nil}, there is no short cut, and
1604 @code{indent-region} actually works line by line.
1605
1606 A short cut function is useful in modes such as C mode and Lisp mode,
1607 where the @code{indent-line-function} must scan from the beginning of
1608 the function: applying it to each line would be quadratic in time. The
1609 short cut can update the scan information as it moves through the lines
1610 indenting them; this takes linear time. In a mode where indenting a
1611 line individually is fast, there is no need for a short cut.
1612
1613 @code{indent-region} with a non-@code{nil} argument has a different
1614 meaning and does not use this variable.
1615 @end defvar
1616
1617 @deffn Command indent-rigidly start end count
1618 @comment !!SourceFile indent.el
1619 This command indents all lines starting between @var{start}
1620 (inclusive) and @var{end} (exclusive) sideways by @var{count} columns.
1621 This ``preserves the shape'' of the affected region, moving it as a
1622 rigid unit. Consequently, this command is useful not only for indenting
1623 regions of unindented text, but also for indenting regions of formatted
1624 code.
1625
1626 For example, if @var{count} is 3, this command adds 3 columns of
1627 indentation to each of the lines beginning in the region specified.
1628
1629 In Mail mode, @kbd{C-c C-y} (@code{mail-yank-original}) uses
1630 @code{indent-rigidly} to indent the text copied from the message being
1631 replied to.
1632 @end deffn
1633
1634 @defun indent-code-rigidly start end columns &optional nochange-regexp
1635 This is like @code{indent-rigidly}, except that it doesn't alter lines
1636 that start within strings or comments.
1637
1638 In addition, it doesn't alter a line if @var{nochange-regexp} matches at
1639 the beginning of the line (if @var{nochange-regexp} is non-@code{nil}).
1640 @end defun
1641
1642 @node Relative Indent
1643 @subsection Indentation Relative to Previous Lines
1644
1645 This section describes two commands which indent the current line
1646 based on the contents of previous lines.
1647
1648 @deffn Command indent-relative &optional unindented-ok
1649 This command inserts whitespace at point, extending to the same
1650 column as the next @dfn{indent point} of the previous nonblank line. An
1651 indent point is a non-whitespace character following whitespace. The
1652 next indent point is the first one at a column greater than the current
1653 column of point. For example, if point is underneath and to the left of
1654 the first non-blank character of a line of text, it moves to that column
1655 by inserting whitespace.
1656
1657 If the previous nonblank line has no next indent point (i.e., none at a
1658 great enough column position), @code{indent-relative} either does
1659 nothing (if @var{unindented-ok} is non-@code{nil}) or calls
1660 @code{tab-to-tab-stop}. Thus, if point is underneath and to the right
1661 of the last column of a short line of text, this command ordinarily
1662 moves point to the next tab stop by inserting whitespace.
1663
1664 The return value of @code{indent-relative} is unpredictable.
1665
1666 In the following example, point is at the beginning of the second
1667 line:
1668
1669 @example
1670 @group
1671 This line is indented twelve spaces.
1672 @point{}The quick brown fox jumped.
1673 @end group
1674 @end example
1675
1676 @noindent
1677 Evaluation of the expression @code{(indent-relative nil)} produces the
1678 following:
1679
1680 @example
1681 @group
1682 This line is indented twelve spaces.
1683 @point{}The quick brown fox jumped.
1684 @end group
1685 @end example
1686
1687 In this example, point is between the @samp{m} and @samp{p} of
1688 @samp{jumped}:
1689
1690 @example
1691 @group
1692 This line is indented twelve spaces.
1693 The quick brown fox jum@point{}ped.
1694 @end group
1695 @end example
1696
1697 @noindent
1698 Evaluation of the expression @code{(indent-relative nil)} produces the
1699 following:
1700
1701 @example
1702 @group
1703 This line is indented twelve spaces.
1704 The quick brown fox jum @point{}ped.
1705 @end group
1706 @end example
1707 @end deffn
1708
1709 @deffn Command indent-relative-maybe
1710 @comment !!SourceFile indent.el
1711 This command indents the current line like the previous nonblank line.
1712 It calls @code{indent-relative} with @code{t} as the @var{unindented-ok}
1713 argument. The return value is unpredictable.
1714
1715 If the previous nonblank line has no indent points beyond the current
1716 column, this command does nothing.
1717 @end deffn
1718
1719 @node Indent Tabs
1720 @comment node-name, next, previous, up
1721 @subsection Adjustable ``Tab Stops''
1722 @cindex tabs stops for indentation
1723
1724 This section explains the mechanism for user-specified ``tab stops''
1725 and the mechanisms which use and set them. The name ``tab stops'' is
1726 used because the feature is similar to that of the tab stops on a
1727 typewriter. The feature works by inserting an appropriate number of
1728 spaces and tab characters to reach the next tab stop column; it does not
1729 affect the display of tab characters in the buffer (@pxref{Usual
1730 Display}). Note that the @key{TAB} character as input uses this tab
1731 stop feature only in a few major modes, such as Text mode.
1732
1733 @deffn Command tab-to-tab-stop
1734 This command inserts spaces or tabs up to the next tab stop column
1735 defined by @code{tab-stop-list}. It searches the list for an element
1736 greater than the current column number, and uses that element as the
1737 column to indent to. It does nothing if no such element is found.
1738 @end deffn
1739
1740 @defopt tab-stop-list
1741 This variable is the list of tab stop columns used by
1742 @code{tab-to-tab-stops}. The elements should be integers in increasing
1743 order. The tab stop columns need not be evenly spaced.
1744
1745 Use @kbd{M-x edit-tab-stops} to edit the location of tab stops
1746 interactively.
1747 @end defopt
1748
1749 @node Motion by Indent
1750 @subsection Indentation-Based Motion Commands
1751
1752 These commands, primarily for interactive use, act based on the
1753 indentation in the text.
1754
1755 @deffn Command back-to-indentation
1756 @comment !!SourceFile simple.el
1757 This command moves point to the first non-whitespace character in the
1758 current line (which is the line in which point is located). It returns
1759 @code{nil}.
1760 @end deffn
1761
1762 @deffn Command backward-to-indentation arg
1763 @comment !!SourceFile simple.el
1764 This command moves point backward @var{arg} lines and then to the
1765 first nonblank character on that line. It returns @code{nil}.
1766 @end deffn
1767
1768 @deffn Command forward-to-indentation arg
1769 @comment !!SourceFile simple.el
1770 This command moves point forward @var{arg} lines and then to the first
1771 nonblank character on that line. It returns @code{nil}.
1772 @end deffn
1773
1774 @node Case Changes
1775 @comment node-name, next, previous, up
1776 @section Case Changes
1777 @cindex case changes
1778
1779 The case change commands described here work on text in the current
1780 buffer. @xref{Character Case}, for case conversion commands that work
1781 on strings and characters. @xref{Case Table}, for how to customize
1782 which characters are upper or lower case and how to convert them.
1783
1784 @deffn Command capitalize-region start end
1785 This function capitalizes all words in the region defined by
1786 @var{start} and @var{end}. To capitalize means to convert each word's
1787 first character to upper case and convert the rest of each word to lower
1788 case. The function returns @code{nil}.
1789
1790 If one end of the region is in the middle of a word, the part of the
1791 word within the region is treated as an entire word.
1792
1793 When @code{capitalize-region} is called interactively, @var{start} and
1794 @var{end} are point and the mark, with the smallest first.
1795
1796 @example
1797 @group
1798 ---------- Buffer: foo ----------
1799 This is the contents of the 5th foo.
1800 ---------- Buffer: foo ----------
1801 @end group
1802
1803 @group
1804 (capitalize-region 1 44)
1805 @result{} nil
1806
1807 ---------- Buffer: foo ----------
1808 This Is The Contents Of The 5th Foo.
1809 ---------- Buffer: foo ----------
1810 @end group
1811 @end example
1812 @end deffn
1813
1814 @deffn Command downcase-region start end
1815 This function converts all of the letters in the region defined by
1816 @var{start} and @var{end} to lower case. The function returns
1817 @code{nil}.
1818
1819 When @code{downcase-region} is called interactively, @var{start} and
1820 @var{end} are point and the mark, with the smallest first.
1821 @end deffn
1822
1823 @deffn Command upcase-region start end
1824 This function converts all of the letters in the region defined by
1825 @var{start} and @var{end} to upper case. The function returns
1826 @code{nil}.
1827
1828 When @code{upcase-region} is called interactively, @var{start} and
1829 @var{end} are point and the mark, with the smallest first.
1830 @end deffn
1831
1832 @deffn Command capitalize-word count
1833 This function capitalizes @var{count} words after point, moving point
1834 over as it does. To capitalize means to convert each word's first
1835 character to upper case and convert the rest of each word to lower case.
1836 If @var{count} is negative, the function capitalizes the
1837 @minus{}@var{count} previous words but does not move point. The value
1838 is @code{nil}.
1839
1840 If point is in the middle of a word, the part of word the before point
1841 (if moving forward) or after point (if operating backward) is ignored.
1842 The rest is treated as an entire word.
1843
1844 When @code{capitalize-word} is called interactively, @var{count} is
1845 set to the numeric prefix argument.
1846 @end deffn
1847
1848 @deffn Command downcase-word count
1849 This function converts the @var{count} words after point to all lower
1850 case, moving point over as it does. If @var{count} is negative, it
1851 converts the @minus{}@var{count} previous words but does not move point.
1852 The value is @code{nil}.
1853
1854 When @code{downcase-word} is called interactively, @var{count} is set
1855 to the numeric prefix argument.
1856 @end deffn
1857
1858 @deffn Command upcase-word count
1859 This function converts the @var{count} words after point to all upper
1860 case, moving point over as it does. If @var{count} is negative, it
1861 converts the @minus{}@var{count} previous words but does not move point.
1862 The value is @code{nil}.
1863
1864 When @code{upcase-word} is called interactively, @var{count} is set to
1865 the numeric prefix argument.
1866 @end deffn
1867
1868 @node Text Properties
1869 @section Text Properties
1870 @cindex text properties
1871 @cindex attributes of text
1872 @cindex properties of text
1873
1874 Each character position in a buffer or a string can have a @dfn{text
1875 property list}, much like the property list of a symbol (@pxref{Property
1876 Lists}). The properties belong to a particular character at a
1877 particular place, such as, the letter @samp{T} at the beginning of this
1878 sentence or the first @samp{o} in @samp{foo}---if the same character
1879 occurs in two different places, the two occurrences generally have
1880 different properties.
1881
1882 Each property has a name and a value. Both of these can be any Lisp
1883 object, but the name is normally a symbol. The usual way to access the
1884 property list is to specify a name and ask what value corresponds to it.
1885
1886 If a character has a @code{category} property, we call it the
1887 @dfn{category} of the character. It should be a symbol. The properties
1888 of the symbol serve as defaults for the properties of the character.
1889
1890 Copying text between strings and buffers preserves the properties
1891 along with the characters; this includes such diverse functions as
1892 @code{substring}, @code{insert}, and @code{buffer-substring}.
1893
1894 @menu
1895 * Examining Properties:: Looking at the properties of one character.
1896 * Changing Properties:: Setting the properties of a range of text.
1897 * Property Search:: Searching for where a property changes value.
1898 * Special Properties:: Particular properties with special meanings.
1899 * Sticky Properties:: How inserted text gets properties from
1900 neighboring text.
1901 * Saving Properties:: Saving text properties in files, and reading
1902 them back.
1903 * Not Intervals:: Why text properties do not use
1904 Lisp-visible text intervals.
1905 @end menu
1906
1907 @node Examining Properties
1908 @subsection Examining Text Properties
1909
1910 The simplest way to examine text properties is to ask for the value of
1911 a particular property of a particular character. For that, use
1912 @code{get-text-property}. Use @code{text-properties-at} to get the
1913 entire property list of a character. @xref{Property Search}, for
1914 functions to examine the properties of a number of characters at once.
1915
1916 These functions handle both strings and buffers. Keep in mind that
1917 positions in a string start from 0, whereas positions in a buffer start
1918 from 1.
1919
1920 @defun get-text-property pos prop &optional object
1921 This function returns the value of the @var{prop} property of the
1922 character after position @var{pos} in @var{object} (a buffer or
1923 string). The argument @var{object} is optional and defaults to the
1924 current buffer.
1925
1926 If there is no @var{prop} property strictly speaking, but the character
1927 has a category which is a symbol, then @code{get-text-property} returns
1928 the @var{prop} property of that symbol.
1929 @end defun
1930
1931 @defun get-char-property pos prop &optional object
1932 This function is like @code{get-text-property}, except that it checks
1933 overlays first and then text properties. @xref{Overlays}.
1934
1935 The argument @var{object} may be a string, a buffer, or a window. If it
1936 is a window, then the buffer displayed in that window is used for text
1937 properties and overlays, but only the overlays active for that window
1938 are considered. If @var{object} is a buffer, then all overlays in that
1939 buffer are considered, as well as text properties. If @var{object} is a
1940 string, only text properties are considered, since strings never have
1941 overlays.
1942 @end defun
1943
1944 @defun text-properties-at position &optional object
1945 This function returns the entire property list of the character at
1946 @var{position} in the string or buffer @var{object}. If @var{object} is
1947 @code{nil}, it defaults to the current buffer.
1948 @end defun
1949
1950 @node Changing Properties
1951 @subsection Changing Text Properties
1952
1953 The primitives for changing properties apply to a specified range of
1954 text. The function @code{set-text-properties} (see end of section) sets
1955 the entire property list of the text in that range; more often, it is
1956 useful to add, change, or delete just certain properties specified by
1957 name.
1958
1959 Since text properties are considered part of the buffer's contents, and
1960 can affect how the buffer looks on the screen, any change in the text
1961 properties is considered a buffer modification. Buffer text property
1962 changes are undoable (@pxref{Undo}).
1963
1964 @defun add-text-properties start end props &optional object
1965 This function modifies the text properties for the text between
1966 @var{start} and @var{end} in the string or buffer @var{object}. If
1967 @var{object} is @code{nil}, it defaults to the current buffer.
1968
1969 The argument @var{props} specifies which properties to change. It
1970 should have the form of a property list (@pxref{Property Lists}): a list
1971 whose elements include the property names followed alternately by the
1972 corresponding values.
1973
1974 The return value is @code{t} if the function actually changed some
1975 property's value; @code{nil} otherwise (if @var{props} is @code{nil} or
1976 its values agree with those in the text).
1977
1978 For example, here is how to set the @code{comment} and @code{face}
1979 properties of a range of text:
1980
1981 @example
1982 (add-text-properties @var{start} @var{end}
1983 '(comment t face highlight))
1984 @end example
1985 @end defun
1986
1987 @defun put-text-property start end prop value &optional object
1988 This function sets the @var{prop} property to @var{value} for the text
1989 between @var{start} and @var{end} in the string or buffer @var{object}.
1990 If @var{object} is @code{nil}, it defaults to the current buffer.
1991 @end defun
1992
1993 @defun remove-text-properties start end props &optional object
1994 This function deletes specified text properties from the text between
1995 @var{start} and @var{end} in the string or buffer @var{object}. If
1996 @var{object} is @code{nil}, it defaults to the current buffer.
1997
1998 The argument @var{props} specifies which properties to delete. It
1999 should have the form of a property list (@pxref{Property Lists}): a list
2000 whose elements are property names alternating with corresponding values.
2001 But only the names matter---the values that accompany them are ignored.
2002 For example, here's how to remove the @code{face} property.
2003
2004 @example
2005 (remove-text-properties @var{start} @var{end} '(face nil))
2006 @end example
2007
2008 The return value is @code{t} if the function actually changed some
2009 property's value; @code{nil} otherwise (if @var{props} is @code{nil} or
2010 if no character in the specified text had any of those properties).
2011 @end defun
2012
2013 @defun set-text-properties start end props &optional object
2014 This function completely replaces the text property list for the text
2015 between @var{start} and @var{end} in the string or buffer @var{object}.
2016 If @var{object} is @code{nil}, it defaults to the current buffer.
2017
2018 The argument @var{props} is the new property list. It should be a list
2019 whose elements are property names alternating with corresponding values.
2020
2021 After @code{set-text-properties} returns, all the characters in the
2022 specified range have identical properties.
2023
2024 If @var{props} is @code{nil}, the effect is to get rid of all properties
2025 from the specified range of text. Here's an example:
2026
2027 @example
2028 (set-text-properties @var{start} @var{end} nil)
2029 @end example
2030 @end defun
2031
2032 @node Property Search
2033 @subsection Property Search Functions
2034
2035 In typical use of text properties, most of the time several or many
2036 consecutive characters have the same value for a property. Rather than
2037 writing your programs to examine characters one by one, it is much
2038 faster to process chunks of text that have the same property value.
2039
2040 Here are functions you can use to do this. In all cases, @var{object}
2041 defaults to the current buffer.
2042
2043 For high performance, it's very important to use the @var{limit}
2044 argument to these functions, especially the ones that search for a
2045 single property---otherwise, they may spend a long time considering
2046 changes in other properties while scanning to the end of the buffer.
2047
2048 @defun next-property-change pos &optional object limit
2049 The function scans the text forward from position @var{pos} in the
2050 string or buffer @var{object} till it finds a change in some text
2051 property, then returns the position of the change. In other words, it
2052 returns the position of the first character beyond @var{pos} whose
2053 properties are not identical to those of the character just after
2054 @var{pos}.
2055
2056 If @var{limit} is non-@code{nil}, then the scan ends at position
2057 @var{limit}. If there is no property change before that point,
2058 @code{next-property-change} returns @var{limit}.
2059
2060 The value is @code{nil} if the properties remain unchanged all the way
2061 to the end of @var{object} and @var{limit} is @code{nil}.
2062
2063 If the value is non-@code{nil}, it is a position greater than or equal
2064 to @var{pos}. The value equals @var{pos} only when @var{limit} equals
2065 @var{pos}.
2066
2067 Here is an example of how to scan the buffer by chunks of text within
2068 which all properties are constant:
2069
2070 @smallexample
2071 (while (not (eobp))
2072 (let ((plist (text-properties-at (point)))
2073 (next-change
2074 (or (next-property-change (point) (current-buffer))
2075 (point-max))))
2076 @r{Process text from point to @var{next-change}@dots{}}
2077 (goto-char next-change)))
2078 @end smallexample
2079 @end defun
2080
2081 @defun next-single-property-change pos prop &optional object limit
2082 The function scans the text forward from position @var{pos} in the
2083 string or buffer @var{object} till it finds a change in the @var{prop}
2084 property, then returns the position of the change. In other words, it
2085 returns the position of the first character beyond @var{pos} whose
2086 @var{prop} property differs from that of the character just after
2087 @var{pos}.
2088
2089 If @var{limit} is non-@code{nil}, then the scan ends at position
2090 @var{limit}. If there is no property change before that point,
2091 @code{next-single-property-change} returns @var{limit}.
2092
2093 The value is @code{nil} if the property remains unchanged all the way to
2094 the end of @var{object} and @var{limit} is @code{nil}. If the value is
2095 non-@code{nil}, it is a position greater than or equal to @var{pos}; it
2096 equals @var{pos} only if @var{limit} equals @var{pos}.
2097 @end defun
2098
2099 @defun previous-property-change pos &optional object limit
2100 This is like @code{next-property-change}, but scans back from @var{pos}
2101 instead of forward. If the value is non-@code{nil}, it is a position
2102 less than or equal to @var{pos}; it equals @var{pos} only if @var{limit}
2103 equals @var{pos}.
2104
2105 Remember that a position is always between two characters; the position
2106 returned by this function is between two characters with different
2107 properties.
2108 @end defun
2109
2110 @defun previous-single-property-change pos prop &optional object limit
2111 This is like @code{next-property-change}, but scans back from @var{pos}
2112 instead of forward. If the value is non-@code{nil}, it is a position
2113 less than or equal to @var{pos}; it equals @var{pos} only if @var{limit}
2114 equals @var{pos}.
2115 @end defun
2116
2117 @defun text-property-any start end prop value &optional object
2118 This function returns non-@code{nil} if at least one character between
2119 @var{start} and @var{end} has a property @var{prop} whose value is
2120 @var{value}. More precisely, it returns the position of the first such
2121 character. Otherwise, it returns @code{nil}.
2122
2123 The optional fifth argument, @var{object}, specifies the string or
2124 buffer to scan. Positions are relative to @var{object}. The default
2125 for @var{object} is the current buffer.
2126 @end defun
2127
2128 @defun text-property-not-all start end prop value &optional object
2129 This function returns non-@code{nil} if at least one character between
2130 @var{start} and @var{end} has a property @var{prop} whose value differs
2131 from @var{value}. More precisely, it returns the position of the
2132 first such character. Otherwise, it returns @code{nil}.
2133
2134 The optional fifth argument, @var{object}, specifies the string or
2135 buffer to scan. Positions are relative to @var{object}. The default
2136 for @var{object} is the current buffer.
2137 @end defun
2138
2139 @node Special Properties
2140 @subsection Properties with Special Meanings
2141
2142 @table @code
2143 @cindex category of text character
2144 @kindex category @r{(text property)}
2145 @item category
2146 If a character has a @code{category} property, we call it the
2147 @dfn{category} of the character. It should be a symbol. The properties
2148 of the symbol serve as defaults for the properties of the character.
2149
2150 @item face
2151 @cindex face codes of text
2152 @kindex face @r{(text property)}
2153 You can use the property @code{face} to control the font and color of
2154 text. @xref{Faces}, for more information. This feature is temporary;
2155 in the future, we may replace it with other ways of specifying how to
2156 display text.
2157
2158 @item mouse-face
2159 @kindex mouse-face @r{(text property)}
2160 The property @code{mouse-face} is used instead of @code{face} when the
2161 mouse is on or near the character. For this purpose, ``near'' means
2162 that all text between the character and where the mouse is have the same
2163 @code{mouse-face} property value.
2164
2165 @item local-map
2166 @cindex keymap of character
2167 @kindex local-map @r{(text property)}
2168 You can specify a different keymap for a portion of the text by means
2169 of a @code{local-map} property. The property's value, for the character
2170 after point, replaces the buffer's local map. @xref{Active Keymaps}.
2171
2172 @item read-only
2173 @cindex read-only character
2174 @kindex read-only @r{(text property)}
2175 If a character has the property @code{read-only}, then modifying that
2176 character is not allowed. Any command that would do so gets an error.
2177
2178 Insertion next to a read-only character is an error if inserting
2179 ordinary text there would inherit the @code{read-only} property due to
2180 stickiness. Thus, you can control permission to insert next to
2181 read-only text by controlling the stickiness. @xref{Sticky Properties}.
2182
2183 Since changing properties counts as modifying the buffer, it is not
2184 possible to remove a @code{read-only} property unless you know the
2185 special trick: bind @code{inhibit-read-only} to a non-@code{nil} value
2186 and then remove the property. @xref{Read Only Buffers}.
2187
2188 @item invisible
2189 @kindex invisible @r{(text property)}
2190 A non-@code{nil} @code{invisible} property means a character does not
2191 appear on the screen. This works much like selective display. Details
2192 of this feature are likely to change in future versions, so check the
2193 @file{etc/NEWS} file in the version you are using.
2194
2195 @item intangible
2196 @kindex intangible @r{(text property)}
2197 A non-@code{nil} @code{intangible} property on a character prevents
2198 putting point before that character. If you try, point actually goes
2199 after the character (and after all succeeding intangible characters).
2200
2201 @item modification-hooks
2202 @cindex change hooks for a character
2203 @cindex hooks for changing a character
2204 @kindex modification-hooks @r{(text property)}
2205 If a character has the property @code{modification-hooks}, then its
2206 value should be a list of functions; modifying that character calls all
2207 of those functions. Each function receives two arguments: the beginning
2208 and end of the part of the buffer being modified. Note that if a
2209 particular modification hook function appears on several characters
2210 being modified by a single primitive, you can't predict how many times
2211 the function will be called.
2212
2213 @item insert-in-front-hooks
2214 @itemx insert-behind-hooks
2215 @kindex insert-in-front-hooks @r{(text property)}
2216 @kindex insert-behind-hooks @r{(text property)}
2217 Assuming insertion is allowed, it then calls the functions
2218 listed in the @code{insert-in-front-hooks} property of the following
2219 character and in the @code{insert-behind-hooks} property of the
2220 preceding character. These functions receive two arguments, the
2221 beginning and end of the inserted text.
2222
2223 See also @ref{Change Hooks}, for other hooks that are called
2224 when you change text in a buffer.
2225
2226 @item point-entered
2227 @itemx point-left
2228 @cindex hooks for motion of point
2229 @kindex point-entered @r{(text property)}
2230 @kindex point-left @r{(text property)}
2231 The special properties @code{point-entered} and @code{point-left}
2232 record hook functions that report motion of point. Each time point
2233 moves, Emacs compares these two property values:
2234
2235 @itemize @bullet
2236 @item
2237 the @code{point-left} property of the character after the old location,
2238 and
2239 @item
2240 the @code{point-entered} property of the character after the new
2241 location.
2242 @end itemize
2243
2244 @noindent
2245 If these two values differ, each of them is called (if not @code{nil})
2246 with two arguments: the old value of point, and the new one.
2247
2248 The same comparison is made for the characters before the old and new
2249 locations. The result may be to execute two @code{point-left} functions
2250 (which may be the same function) and/or two @code{point-entered}
2251 functions (which may be the same function). The @code{point-left}
2252 functions are always called before the @code{point-entered} functions.
2253
2254 A primitive function may examine characters at various positions
2255 without moving point to those positions. Only an actual change in the
2256 value of point runs these hook functions.
2257 @end table
2258
2259 @defvar inhibit-point-motion-hooks
2260 When this variable is non-@code{nil}, @code{point-left} and
2261 @code{point-entered} hooks are not run.
2262 @end defvar
2263
2264 @node Sticky Properties
2265 @subsection Stickiness of Text Properties
2266 @cindex sticky text properties
2267 @cindex inheritance of text properties
2268
2269 Self-inserting characters normally take on the same properties as the
2270 preceding character. This is called @dfn{inheritance} of properties.
2271
2272 In a Lisp program, you can do insertion with inheritance or without,
2273 depending on your choice of insertion primitive. The ordinary text
2274 insertion functions such as @code{insert} do not inherit any properties.
2275 They insert text with precisely the properties of the string being
2276 inserted, and no others. This is correct for programs that copy text
2277 from one context to another---for example, into or out of the kill
2278 ring. To insert with inheritance, use the special primatives described
2279 in this section.
2280
2281 When you do insertion with inheritance, @emph{which} properties are
2282 inherited depends on two specific properties: @code{front-sticky} and
2283 @code{rear-nonsticky}.
2284
2285 Insertion after a character inherits those of its properties that are
2286 @dfn{rear-sticky}. Insertion before a character inherits those of its
2287 properties that are @dfn{front-sticky}. By default, a text property is
2288 rear-sticky but not front-sticky. Thus, the default is to inherit all
2289 the properties of the preceding character, and nothing from the
2290 following character. You can request different behavior by specifying
2291 the stickiness of certain properties.
2292
2293 If a character's @code{front-sticky} property is @code{t}, then all
2294 its properties are front-sticky. If the @code{front-sticky} property is
2295 a list, then the sticky properties of the character are those whose
2296 names are in the list. For example, if a character has a
2297 @code{front-sticky} property whose value is @code{(face read-only)},
2298 then insertion before the character can inherit its @code{face} property
2299 and its @code{read-only} property, but no others.
2300
2301 The @code{rear-nonsticky} works the opposite way. Every property is
2302 rear-sticky by default, so the @code{rear-nonsticky} property says which
2303 properties are @emph{not} rear-sticky. If a character's
2304 @code{rear-nonsticky} property is @code{t}, then none of its properties
2305 are rear-sticky. If the @code{rear-nonsticky} property is a list,
2306 properties are rear-sticky @emph{unless} their names are in the list.
2307
2308 When you insert text with inheritance, it inherits all the rear-sticky
2309 properties of the preceding character, and all the front-sticky
2310 properties of the following character. The previous character's
2311 properties take precedence when both sides offer different sticky values
2312 for the same property.
2313
2314 Here are the functions that insert text with inheritance of properties:
2315
2316 @defun insert-and-inherit &rest strings
2317 Insert the strings @var{strings}, just like the function @code{insert},
2318 but inherit any sticky properties from the adjoining text.
2319 @end defun
2320
2321 @defun insert-before-markers-and-inherit &rest strings
2322 Insert the strings @var{strings}, just like the function
2323 @code{insert-before-markers}, but inherit any sticky properties from the
2324 adjoining text.
2325 @end defun
2326
2327 @node Saving Properties
2328 @subsection Saving Text Properites in Files
2329 @cindex text properties in files
2330 @cindex saving text properties
2331
2332 You can save text properties in files, and restore text properties
2333 when inserting the files, using these two hooks:
2334
2335 @defvar write-region-annotation-functions
2336 This variable's value is a list of functions for @code{write-region} to
2337 run to encode text properties in some fashion as annotations to the text
2338 being written in the file. @xref{Writing to Files}.
2339
2340 Each function in the list is called with two arguments: the start and
2341 end of the region to be written. These functions should not alter the
2342 contents of the buffer. Instead, they should return lists indicating
2343 annotations to write in the file in addition to the text in the
2344 buffer.
2345
2346 Each function should return a list of elements of the form
2347 @code{(@var{position} . @var{string})}, where @var{position} is an
2348 integer specifying the relative position in the text to be written, and
2349 @var{string} is the annotation to add there.
2350
2351 Each list returned by one of these functions must be already sorted in
2352 increasing order by @var{position}. If there is more than one function,
2353 @code{write-region} merges the lists destructively into one sorted list.
2354
2355 When @code{write-region} actually writes the text from the buffer to the
2356 file, it intermixes the specified annotations at the corresponding
2357 positions. All this takes place without modifying the buffer.
2358 @end defvar
2359
2360 @defvar after-insert-file-functions
2361 This variable holds a list of functions for @code{insert-file-contents}
2362 to call after inserting a file's contents. These functions should scan
2363 the inserted text for annotations, and convert them to the text
2364 properties they stand for.
2365
2366 Each function receives one argument, the length of the inserted text;
2367 point indicates the start of that text. The function should scan that
2368 text for annotations, delete them, and create the text properties that
2369 the annotations specify. The function should return the updated length
2370 of the inserted text, as it stands after those changes. The value
2371 returned by one function becomes the argument to the next function.
2372
2373 These functions should always return with point at the beginning of
2374 the inserted text.
2375
2376 The intended use of @code{after-insert-file-functions} is for converting
2377 some sort of textual annotations into actual text properties. But other
2378 uses may be possible.
2379 @end defvar
2380
2381 We invite users to write Lisp programs to store and retrieve text
2382 properties in files, using these hooks, and thus to experiment with
2383 various data formats and find good ones. Eventually we hope users
2384 will produce good, general extensions we can install in Emacs.
2385
2386 We suggest not trying to handle arbitrary Lisp objects as property
2387 names or property values---because a program that general is probably
2388 difficult to write, and slow. Instead, choose a set of possible data
2389 types that are reasonably flexible, and not too hard to encode.
2390
2391 @node Not Intervals
2392 @subsection Why Text Properties are not Intervals
2393 @cindex intervals
2394
2395 Some editors that support adding attributes to text in the buffer do
2396 so by letting the user specify ``intervals'' within the text, and adding
2397 the properties to the intervals. Those editors permit the user or the
2398 programmer to determine where individual intervals start and end. We
2399 deliberately provided a different sort of interface in Emacs Lisp to
2400 avoid certain paradoxical behavior associated with text modification.
2401
2402 If the actual subdivision into intervals is meaningful, that means you
2403 can distinguish between a buffer that is just one interval with a
2404 certain property, and a buffer containing the same text subdivided into
2405 two intervals, both of which have that property.
2406
2407 Suppose you take the buffer with just one interval and kill part of
2408 the text. The text remaining in the buffer is one interval, and the
2409 copy in the kill ring (and the undo list) becomes a separate interval.
2410 Then if you yank back the killed text, you get two intervals with the
2411 same properties. Thus, editing does not preserve the distinction
2412 between one interval and two.
2413
2414 Suppose we ``fix'' this problem by coalescing the two intervals when
2415 the text is inserted. That works fine if the buffer originally was a
2416 single interval. But suppose instead that we have two adjacent
2417 intervals with the same properties, and we kill the text of one interval
2418 and yank it back. The same interval-coalescence feature that rescues
2419 the other case causes trouble in this one: after yanking, we have just
2420 one interval. One again, editing does not preserve the distinction
2421 between one interval and two.
2422
2423 Insertion of text at the border between intervals also raises
2424 questions that have no satisfactory answer.
2425
2426 However, it is easy to arrange for editing to behave consistently for
2427 questions of the form, ``What are the properties of this character?''
2428 So we have decided these are the only questions that make sense; we have
2429 not implemented asking questions about where intervals start or end.
2430
2431 In practice, you can usually use the property search functions in
2432 place of explicit interval boundaries. You can think of them as finding
2433 the boundaries of intervals, assuming that intervals are always
2434 coalesced whenever possible. @xref{Property Search}.
2435
2436 Emacs also provides explicit intervals as a presentation feature; see
2437 @ref{Overlays}.
2438
2439 @node Substitution
2440 @section Substituting for a Character Code
2441
2442 The following functions replace characters within a specified region
2443 based on their character codes.
2444
2445 @defun subst-char-in-region start end old-char new-char &optional noundo
2446 @cindex replace characters
2447 This function replaces all occurrences of the character @var{old-char}
2448 with the character @var{new-char} in the region of the current buffer
2449 defined by @var{start} and @var{end}.
2450
2451 @cindex Outline mode
2452 @cindex undo avoidance
2453 If @var{noundo} is non-@code{nil}, then @code{subst-char-in-region}
2454 does not record the change for undo and does not mark the buffer as
2455 modified. This feature is useful for changes which are not considered
2456 significant, such as when Outline mode changes visible lines to
2457 invisible lines and vice versa.
2458
2459 @code{subst-char-in-region} does not move point and returns
2460 @code{nil}.
2461
2462 @example
2463 @group
2464 ---------- Buffer: foo ----------
2465 This is the contents of the buffer before.
2466 ---------- Buffer: foo ----------
2467 @end group
2468
2469 @group
2470 (subst-char-in-region 1 20 ?i ?X)
2471 @result{} nil
2472
2473 ---------- Buffer: foo ----------
2474 ThXs Xs the contents of the buffer before.
2475 ---------- Buffer: foo ----------
2476 @end group
2477 @end example
2478 @end defun
2479
2480 @defun translate-region start end table
2481 This function applies a translation table to the characters in the
2482 buffer between positions @var{start} and @var{end}.
2483
2484 The translation table @var{table} is a string; @code{(aref @var{table}
2485 @var{ochar})} gives the translated character corresponding to
2486 @var{ochar}. If the length of @var{table} is less than 256, any
2487 characters with codes larger than the length of @var{table} are not
2488 altered by the translation.
2489
2490 The return value of @code{translate-region} is the number of
2491 characters which were actually changed by the translation. This does
2492 not count characters which were mapped into themselves in the
2493 translation table.
2494
2495 This function is available in Emacs versions 19 and later.
2496 @end defun
2497
2498 @node Registers
2499 @section Registers
2500 @cindex registers
2501
2502 A register is a sort of variable used in Emacs editing that can hold a
2503 marker, a string, a rectangle, a window configuration (of one frame), or
2504 a frame configuration (of all frames). Each register is named by a
2505 single character. All characters, including control and meta characters
2506 (but with the exception of @kbd{C-g}), can be used to name registers.
2507 Thus, there are 255 possible registers. A register is designated in
2508 Emacs Lisp by a character which is its name.
2509
2510 The functions in this section return unpredictable values unless
2511 otherwise stated.
2512 @c Will change in version 19
2513
2514 @defvar register-alist
2515 This variable is an alist of elements of the form @code{(@var{name} .
2516 @var{contents})}. Normally, there is one element for each Emacs
2517 register that has been used.
2518
2519 The object @var{name} is a character (an integer) identifying the
2520 register. The object @var{contents} is a string, marker, or list
2521 representing the register contents. A string represents text stored in
2522 the register. A marker represents a position. A list represents a
2523 rectangle; its elements are strings, one per line of the rectangle.
2524 @end defvar
2525
2526 @defun get-register reg
2527 This function returns the contents of the register
2528 @var{reg}, or @code{nil} if it has no contents.
2529 @end defun
2530
2531 @defun set-register reg value
2532 This function sets the contents of register @var{reg} to @var{value}.
2533 A register can be set to any value, but the other register functions
2534 expect only certain data types. The return value is @var{value}.
2535 @end defun
2536
2537 @deffn Command view-register reg
2538 This command displays what is contained in register @var{reg}.
2539 @end deffn
2540
2541 @ignore
2542 @deffn Command point-to-register reg
2543 This command stores both the current location of point and the current
2544 buffer in register @var{reg} as a marker.
2545 @end deffn
2546
2547 @deffn Command jump-to-register reg
2548 @deffnx Command register-to-point reg
2549 @comment !!SourceFile register.el
2550 This command restores the status recorded in register @var{reg}.
2551
2552 If @var{reg} contains a marker, it moves point to the position stored in
2553 the marker. Since both the buffer and the location within the buffer
2554 are stored by the @code{point-to-register} function, this command can
2555 switch you to another buffer.
2556
2557 If @var{reg} contains a window configuration or a frame configuration.
2558 @code{jump-to-register} restores that configuration.
2559 @end deffn
2560 @end ignore
2561
2562 @deffn Command insert-register reg &optional beforep
2563 This command inserts contents of register @var{reg} into the current
2564 buffer.
2565
2566 Normally, this command puts point before the inserted text, and the
2567 mark after it. However, if the optional second argument @var{beforep}
2568 is non-@code{nil}, it puts the mark before and point after.
2569 You can pass a non-@code{nil} second argument @var{beforep} to this
2570 function interactively by supplying any prefix argument.
2571
2572 If the register contains a rectangle, then the rectangle is inserted
2573 with its upper left corner at point. This means that text is inserted
2574 in the current line and underneath it on successive lines.
2575
2576 If the register contains something other than saved text (a string) or
2577 a rectangle (a list), currently useless things happen. This may be
2578 changed in the future.
2579 @end deffn
2580
2581 @ignore
2582 @deffn Command copy-to-register reg start end &optional delete-flag
2583 This command copies the region from @var{start} to @var{end} into
2584 register @var{reg}. If @var{delete-flag} is non-@code{nil}, it deletes
2585 the region from the buffer after copying it into the register.
2586 @end deffn
2587
2588 @deffn Command prepend-to-register reg start end &optional delete-flag
2589 This command prepends the region from @var{start} to @var{end} into
2590 register @var{reg}. If @var{delete-flag} is non-@code{nil}, it deletes
2591 the region from the buffer after copying it to the register.
2592 @end deffn
2593
2594 @deffn Command append-to-register reg start end &optional delete-flag
2595 This command appends the region from @var{start} to @var{end} to the
2596 text already in register @var{reg}. If @var{delete-flag} is
2597 non-@code{nil}, it deletes the region from the buffer after copying it
2598 to the register.
2599 @end deffn
2600
2601 @deffn Command copy-rectangle-to-register reg start end &optional delete-flag
2602 This command copies a rectangular region from @var{start} to @var{end}
2603 into register @var{reg}. If @var{delete-flag} is non-@code{nil}, it
2604 deletes the region from the buffer after copying it to the register.
2605 @end deffn
2606
2607 @deffn Command window-configuration-to-register reg
2608 This function stores the window configuration of the selected frame in
2609 register @var{reg}.
2610 @end deffn
2611
2612 @deffn Command frame-configuration-to-register reg
2613 This function stores the current frame configuration in register
2614 @var{reg}.
2615 @end deffn
2616 @end ignore
2617
2618 @node Change Hooks
2619 @section Change Hooks
2620 @cindex change hooks
2621 @cindex hooks for text changes
2622
2623 These hook variables let you arrange to take notice of all changes in
2624 all buffers (or in a particular buffer, if you make them buffer-local).
2625 See also @ref{Special Properties}, for how to detect changes to specific
2626 parts of the text.
2627
2628 The functions you use in these hooks should save and restore the match
2629 data if they do anything that uses regular expressions; otherwise, they
2630 will interfere in bizarre ways with the editing operations that call
2631 them.
2632
2633 @defvar before-change-functions
2634 This variable holds a list of a functions to call before any buffer
2635 modification. Each function gets two arguments, the beginning and end
2636 of the region that is about to change, represented as integers. The
2637 buffer that is about to change is always the current buffer.
2638 @end defvar
2639
2640 @defvar after-change-functions
2641 This variable holds a list of a functions to call after any buffer
2642 modification. Each function receives three arguments: the beginning and
2643 end of the region just changed, and the length of the text that existed
2644 before the change. (To get the current length, subtract the region
2645 beginning from the region end.) All three arguments are integers. The
2646 buffer that's about to change is always the current buffer.
2647 @end defvar
2648
2649 @defvar before-change-function
2650 This variable holds one function to call before any buffer modification
2651 (or @code{nil} for no function). It is called just like the functions
2652 in @code{before-change-functions}.
2653 @end defvar
2654
2655 @defvar after-change-function
2656 This variable holds one function to call after any buffer modification
2657 (or @code{nil} for no function). It is called just like the functions in
2658 @code{after-change-functions}.
2659 @end defvar
2660
2661 The four variables above are temporarily bound to @code{nil} during the
2662 time that any of these functions is running. This means that if one of
2663 these functions changes the buffer, that change won't run these
2664 functions. If you do want a hook function to make changes that run
2665 these functions, make it bind these variables back to their usual
2666 values.
2667
2668 @defvar first-change-hook
2669 This variable is a normal hook that is run whenever a buffer is changed
2670 that was previously in the unmodified state.
2671 @end defvar
2672
2673 The variables described in this section are meaningful only starting
2674 with Emacs version 19.