*** empty log message ***
[bpt/emacs.git] / lispref / text.texi
CommitLineData
73804d4b
RS
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
9db3a582 3@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2000, 2001
fd897522 4@c Free Software Foundation, Inc.
73804d4b
RS
5@c See the file elisp.texi for copying conditions.
6@setfilename ../info/text
f9f59935 7@node Text, Non-ASCII Characters, Markers, Top
73804d4b
RS
8@chapter Text
9@cindex text
10
11 This chapter describes the functions that deal with the text in a
61cfa852 12buffer. Most examine, insert, or delete text in the current buffer,
b6954afd
RS
13often operating at point or on text adjacent to point. Many are
14interactive. All the functions that change the text provide for undoing
15the changes (@pxref{Undo}).
73804d4b
RS
16
17 Many text-related functions operate on a region of text defined by two
18buffer positions passed in arguments named @var{start} and @var{end}.
19These arguments should be either markers (@pxref{Markers}) or numeric
20character positions (@pxref{Positions}). The order of these arguments
21does not matter; it is all right for @var{start} to be the end of the
22region and @var{end} the beginning. For example, @code{(delete-region 1
2310)} and @code{(delete-region 10 1)} are equivalent. An
24@code{args-out-of-range} error is signaled if either @var{start} or
25@var{end} is outside the accessible portion of the buffer. In an
26interactive call, point and the mark are used for these arguments.
27
28@cindex buffer contents
29 Throughout this chapter, ``text'' refers to the characters in the
b6954afd
RS
30buffer, together with their properties (when relevant). Keep in mind
31that point is always between two characters, and the cursor appears on
32the character after point.
73804d4b
RS
33
34@menu
35* Near Point:: Examining text in the vicinity of point.
36* Buffer Contents:: Examining text in a general fashion.
37* Comparing Text:: Comparing substrings of buffers.
38* Insertion:: Adding new text to a buffer.
39* Commands for Insertion:: User-level commands to insert text.
40* Deletion:: Removing text from a buffer.
41* User-Level Deletion:: User-level commands to delete text.
42* The Kill Ring:: Where removed text sometimes is saved for later use.
43* Undo:: Undoing changes to the text of a buffer.
44* Maintaining Undo:: How to enable and disable undo information.
45 How to control how much information is kept.
46* Filling:: Functions for explicit filling.
bfe721d1 47* Margins:: How to specify margins for filling commands.
e75ecfec 48* Adaptive Fill:: Adaptive Fill mode chooses a fill prefix from context.
73804d4b
RS
49* Auto Filling:: How auto-fill mode is implemented to break lines.
50* Sorting:: Functions for sorting parts of the buffer.
51* Columns:: Computing horizontal positions, and using them.
52* Indentation:: Functions to insert or adjust indentation.
53* Case Changes:: Case conversion of parts of the buffer.
54* Text Properties:: Assigning Lisp property lists to text characters.
55* Substitution:: Replacing a given character wherever it appears.
b22f3a19 56* Transposition:: Swapping two portions of a buffer.
73804d4b
RS
57* Registers:: How registers are implemented. Accessing the text or
58 position stored in a register.
b6954afd 59* Base 64:: Conversion to or from base 64 encoding.
944351c0 60* MD5 Checksum:: Compute the MD5 ``message digest''/``checksum''.
73804d4b
RS
61* Change Hooks:: Supplying functions to be run when text is changed.
62@end menu
63
64@node Near Point
65@section Examining Text Near Point
66
67 Many functions are provided to look at the characters around point.
68Several simple functions are described here. See also @code{looking-at}
69in @ref{Regexp Search}.
70
1911e6e5 71@defun char-after &optional position
73804d4b
RS
72This function returns the character in the current buffer at (i.e.,
73immediately after) position @var{position}. If @var{position} is out of
74range for this purpose, either before the beginning of the buffer, or at
1911e6e5
RS
75or beyond the end, then the value is @code{nil}. The default for
76@var{position} is point.
73804d4b
RS
77
78In the following example, assume that the first character in the
79buffer is @samp{@@}:
80
81@example
82@group
83(char-to-string (char-after 1))
84 @result{} "@@"
85@end group
86@end example
87@end defun
88
1911e6e5 89@defun char-before &optional position
969fe9b5
RS
90This function returns the character in the current buffer immediately
91before position @var{position}. If @var{position} is out of range for
92this purpose, either before the beginning of the buffer, or at or beyond
1911e6e5
RS
93the end, then the value is @code{nil}. The default for
94@var{position} is point.
969fe9b5
RS
95@end defun
96
73804d4b
RS
97@defun following-char
98This function returns the character following point in the current
99buffer. This is similar to @code{(char-after (point))}. However, if
100point is at the end of the buffer, then @code{following-char} returns 0.
101
102Remember that point is always between characters, and the terminal
103cursor normally appears over the character following point. Therefore,
104the character returned by @code{following-char} is the character the
105cursor is over.
106
107In this example, point is between the @samp{a} and the @samp{c}.
108
109@example
110@group
111---------- Buffer: foo ----------
112Gentlemen may cry ``Pea@point{}ce! Peace!,''
113but there is no peace.
114---------- Buffer: foo ----------
115@end group
116
117@group
118(char-to-string (preceding-char))
119 @result{} "a"
120(char-to-string (following-char))
121 @result{} "c"
122@end group
123@end example
124@end defun
125
126@defun preceding-char
127This function returns the character preceding point in the current
128buffer. See above, under @code{following-char}, for an example. If
129point is at the beginning of the buffer, @code{preceding-char} returns
1300.
131@end defun
132
133@defun bobp
134This function returns @code{t} if point is at the beginning of the
135buffer. If narrowing is in effect, this means the beginning of the
136accessible portion of the text. See also @code{point-min} in
137@ref{Point}.
138@end defun
139
140@defun eobp
141This function returns @code{t} if point is at the end of the buffer.
142If narrowing is in effect, this means the end of accessible portion of
143the text. See also @code{point-max} in @xref{Point}.
144@end defun
145
146@defun bolp
147This function returns @code{t} if point is at the beginning of a line.
f9f59935 148@xref{Text Lines}. The beginning of the buffer (or of its accessible
61cfa852 149portion) always counts as the beginning of a line.
73804d4b
RS
150@end defun
151
152@defun eolp
153This function returns @code{t} if point is at the end of a line. The
154end of the buffer (or of its accessible portion) is always considered
155the end of a line.
156@end defun
157
158@node Buffer Contents
159@section Examining Buffer Contents
160
161 This section describes two functions that allow a Lisp program to
162convert any portion of the text in the buffer into a string.
163
164@defun buffer-substring start end
165This function returns a string containing a copy of the text of the
166region defined by positions @var{start} and @var{end} in the current
167buffer. If the arguments are not positions in the accessible portion of
168the buffer, @code{buffer-substring} signals an @code{args-out-of-range}
169error.
170
171It is not necessary for @var{start} to be less than @var{end}; the
172arguments can be given in either order. But most often the smaller
173argument is written first.
174
22697dac
KH
175If the text being copied has any text properties, these are copied into
176the string along with the characters they belong to. @xref{Text
177Properties}. However, overlays (@pxref{Overlays}) in the buffer and
178their properties are ignored, not copied.
179
73804d4b
RS
180@example
181@group
182---------- Buffer: foo ----------
183This is the contents of buffer foo
184
185---------- Buffer: foo ----------
186@end group
187
188@group
189(buffer-substring 1 10)
190@result{} "This is t"
191@end group
192@group
193(buffer-substring (point-max) 10)
194@result{} "he contents of buffer foo
195"
196@end group
197@end example
198@end defun
199
371f8cd0 200@defun buffer-substring-no-properties start end
22697dac
KH
201This is like @code{buffer-substring}, except that it does not copy text
202properties, just the characters themselves. @xref{Text Properties}.
22697dac
KH
203@end defun
204
73804d4b 205@defun buffer-string
f9f59935 206This function returns the contents of the entire accessible portion of
a9f0a989
RS
207the current buffer as a string. It is equivalent to
208
209@example
210(buffer-substring (point-min) (point-max))
211@end example
73804d4b
RS
212
213@example
214@group
215---------- Buffer: foo ----------
216This is the contents of buffer foo
217
218---------- Buffer: foo ----------
219
220(buffer-string)
221 @result{} "This is the contents of buffer foo
222"
223@end group
224@end example
225@end defun
226
f9f59935
RS
227@defun thing-at-point thing
228Return the @var{thing} around or next to point, as a string.
229
230The argument @var{thing} is a symbol which specifies a kind of syntactic
231entity. Possibilities include @code{symbol}, @code{list}, @code{sexp},
232@code{defun}, @code{filename}, @code{url}, @code{word}, @code{sentence},
233@code{whitespace}, @code{line}, @code{page}, and others.
234
235@example
236---------- Buffer: foo ----------
237Gentlemen may cry ``Pea@point{}ce! Peace!,''
238but there is no peace.
239---------- Buffer: foo ----------
240
241(thing-at-point 'word)
242 @result{} "Peace"
243(thing-at-point 'line)
244 @result{} "Gentlemen may cry ``Peace! Peace!,''\n"
245(thing-at-point 'whitespace)
246 @result{} nil
247@end example
248@end defun
249
73804d4b
RS
250@node Comparing Text
251@section Comparing Text
252@cindex comparing buffer text
253
254 This function lets you compare portions of the text in a buffer, without
255copying them into strings first.
256
257@defun compare-buffer-substrings buffer1 start1 end1 buffer2 start2 end2
258This function lets you compare two substrings of the same buffer or two
259different buffers. The first three arguments specify one substring,
260giving a buffer and two positions within the buffer. The last three
261arguments specify the other substring in the same way. You can use
61cfa852 262@code{nil} for @var{buffer1}, @var{buffer2}, or both to stand for the
73804d4b
RS
263current buffer.
264
265The value is negative if the first substring is less, positive if the
266first is greater, and zero if they are equal. The absolute value of
267the result is one plus the index of the first differing characters
268within the substrings.
269
270This function ignores case when comparing characters
bfe721d1
KH
271if @code{case-fold-search} is non-@code{nil}. It always ignores
272text properties.
73804d4b
RS
273
274Suppose the current buffer contains the text @samp{foobarbar
275haha!rara!}; then in this example the two substrings are @samp{rbar }
276and @samp{rara!}. The value is 2 because the first substring is greater
277at the second character.
278
279@example
8241495d 280(compare-buffer-substrings nil 6 11 nil 16 21)
73804d4b
RS
281 @result{} 2
282@end example
73804d4b
RS
283@end defun
284
285@node Insertion
bfe721d1 286@section Inserting Text
73804d4b
RS
287@cindex insertion of text
288@cindex text insertion
289
969fe9b5
RS
290@cindex insertion before point
291@cindex before point, insertion
73804d4b
RS
292 @dfn{Insertion} means adding new text to a buffer. The inserted text
293goes at point---between the character before point and the character
969fe9b5
RS
294after point. Some insertion functions leave point before the inserted
295text, while other functions leave it after. We call the former
296insertion @dfn{after point} and the latter insertion @dfn{before point}.
73804d4b
RS
297
298 Insertion relocates markers that point at positions after the
299insertion point, so that they stay with the surrounding text
300(@pxref{Markers}). When a marker points at the place of insertion,
969fe9b5
RS
301insertion may or may not relocate the marker, depending on the marker's
302insertion type (@pxref{Marker Insertion Types}). Certain special
303functions such as @code{insert-before-markers} relocate all such markers
304to point after the inserted text, regardless of the markers' insertion
305type.
73804d4b
RS
306
307 Insertion functions signal an error if the current buffer is
2468d0c0 308read-only or if they insert within read-only text.
73804d4b 309
bfe721d1
KH
310 These functions copy text characters from strings and buffers along
311with their properties. The inserted characters have exactly the same
312properties as the characters they were copied from. By contrast,
313characters specified as separate arguments, not part of a string or
314buffer, inherit their text properties from the neighboring text.
315
a9f0a989
RS
316 The insertion functions convert text from unibyte to multibyte in
317order to insert in a multibyte buffer, and vice versa---if the text
318comes from a string or from a buffer. However, they do not convert
319unibyte character codes 128 through 255 to multibyte characters, not
320even if the current buffer is a multibyte buffer. @xref{Converting
321Representations}.
322
73804d4b
RS
323@defun insert &rest args
324This function inserts the strings and/or characters @var{args} into the
61cfa852
RS
325current buffer, at point, moving point forward. In other words, it
326inserts the text before point. An error is signaled unless all
327@var{args} are either strings or characters. The value is @code{nil}.
73804d4b
RS
328@end defun
329
330@defun insert-before-markers &rest args
331This function inserts the strings and/or characters @var{args} into the
332current buffer, at point, moving point forward. An error is signaled
333unless all @var{args} are either strings or characters. The value is
334@code{nil}.
335
336This function is unlike the other insertion functions in that it
337relocates markers initially pointing at the insertion point, to point
f9f59935 338after the inserted text. If an overlay begins the insertion point, the
4ba35d16
RS
339inserted text falls outside the overlay; if a nonempty overlay ends at
340the insertion point, the inserted text falls inside that overlay.
73804d4b
RS
341@end defun
342
969fe9b5 343@defun insert-char character &optional count inherit
73804d4b 344This function inserts @var{count} instances of @var{character} into the
969fe9b5
RS
345current buffer before point. The argument @var{count} should be a
346number (@code{nil} means 1), and @var{character} must be a character.
347The value is @code{nil}.
33acbad2 348
a9f0a989
RS
349This function does not convert unibyte character codes 128 through 255
350to multibyte characters, not even if the current buffer is a multibyte
351buffer. @xref{Converting Representations}.
352
33acbad2
RS
353If @var{inherit} is non-@code{nil}, then the inserted characters inherit
354sticky text properties from the two characters before and after the
355insertion point. @xref{Sticky Properties}.
73804d4b
RS
356@end defun
357
358@defun insert-buffer-substring from-buffer-or-name &optional start end
359This function inserts a portion of buffer @var{from-buffer-or-name}
360(which must already exist) into the current buffer before point. The
361text inserted is the region from @var{start} and @var{end}. (These
362arguments default to the beginning and end of the accessible portion of
363that buffer.) This function returns @code{nil}.
364
365In this example, the form is executed with buffer @samp{bar} as the
366current buffer. We assume that buffer @samp{bar} is initially empty.
367
368@example
369@group
370---------- Buffer: foo ----------
371We hold these truths to be self-evident, that all
372---------- Buffer: foo ----------
373@end group
374
375@group
376(insert-buffer-substring "foo" 1 20)
377 @result{} nil
378
379---------- Buffer: bar ----------
61cfa852 380We hold these truth@point{}
73804d4b
RS
381---------- Buffer: bar ----------
382@end group
383@end example
384@end defun
385
386 @xref{Sticky Properties}, for other insertion functions that inherit
61cfa852 387text properties from the nearby text in addition to inserting it.
33acbad2
RS
388Whitespace inserted by indentation functions also inherits text
389properties.
73804d4b
RS
390
391@node Commands for Insertion
392@section User-Level Insertion Commands
393
394 This section describes higher-level commands for inserting text,
395commands intended primarily for the user but useful also in Lisp
396programs.
397
398@deffn Command insert-buffer from-buffer-or-name
399This command inserts the entire contents of @var{from-buffer-or-name}
400(which must exist) into the current buffer after point. It leaves
401the mark after the inserted text. The value is @code{nil}.
402@end deffn
403
404@deffn Command self-insert-command count
405@cindex character insertion
406@cindex self-insertion
61cfa852
RS
407This command inserts the last character typed; it does so @var{count}
408times, before point, and returns @code{nil}. Most printing characters
409are bound to this command. In routine use, @code{self-insert-command}
410is the most frequently called function in Emacs, but programs rarely use
411it except to install it on a keymap.
73804d4b
RS
412
413In an interactive call, @var{count} is the numeric prefix argument.
414
22697dac 415This command calls @code{auto-fill-function} whenever that is
ca3be36e
DL
416non-@code{nil} and the character inserted is in the table
417@code{auto-fill-chars} (@pxref{Auto Filling}).
73804d4b
RS
418
419@c Cross refs reworded to prevent overfull hbox. --rjc 15mar92
22697dac 420This command performs abbrev expansion if Abbrev mode is enabled and
73804d4b
RS
421the inserted character does not have word-constituent
422syntax. (@xref{Abbrevs}, and @ref{Syntax Class Table}.)
423
22697dac
KH
424This is also responsible for calling @code{blink-paren-function} when
425the inserted character has close parenthesis syntax (@pxref{Blinking}).
a40d4712
PR
426
427Do not try substituting your own definition of
428@code{self-insert-command} for the standard one. The editor command
429loop handles this function specially.
73804d4b
RS
430@end deffn
431
432@deffn Command newline &optional number-of-newlines
433This command inserts newlines into the current buffer before point.
434If @var{number-of-newlines} is supplied, that many newline characters
435are inserted.
436
437@cindex newline and Auto Fill mode
61cfa852
RS
438This function calls @code{auto-fill-function} if the current column
439number is greater than the value of @code{fill-column} and
440@var{number-of-newlines} is @code{nil}. Typically what
441@code{auto-fill-function} does is insert a newline; thus, the overall
442result in this case is to insert two newlines at different places: one
443at point, and another earlier in the line. @code{newline} does not
444auto-fill if @var{number-of-newlines} is non-@code{nil}.
73804d4b 445
bfe721d1
KH
446This command indents to the left margin if that is not zero.
447@xref{Margins}.
448
73804d4b
RS
449The value returned is @code{nil}. In an interactive call, @var{count}
450is the numeric prefix argument.
451@end deffn
452
453@deffn Command split-line
454This command splits the current line, moving the portion of the line
61cfa852 455after point down vertically so that it is on the next line directly
73804d4b
RS
456below where it was before. Whitespace is inserted as needed at the
457beginning of the lower line, using the @code{indent-to} function.
458@code{split-line} returns the position of point.
459
460Programs hardly ever use this function.
461@end deffn
462
463@defvar overwrite-mode
1911e6e5
RS
464This variable controls whether overwrite mode is in effect. The value
465should be @code{overwrite-mode-textual}, @code{overwrite-mode-binary},
466or @code{nil}. @code{overwrite-mode-textual} specifies textual
467overwrite mode (treats newlines and tabs specially), and
468@code{overwrite-mode-binary} specifies binary overwrite mode (treats
469newlines and tabs like any other characters).
73804d4b
RS
470@end defvar
471
472@node Deletion
bfe721d1 473@section Deleting Text
73804d4b
RS
474
475@cindex deletion vs killing
476 Deletion means removing part of the text in a buffer, without saving
477it in the kill ring (@pxref{The Kill Ring}). Deleted text can't be
478yanked, but can be reinserted using the undo mechanism (@pxref{Undo}).
61cfa852
RS
479Some deletion functions do save text in the kill ring in some special
480cases.
73804d4b
RS
481
482 All of the deletion functions operate on the current buffer, and all
483return a value of @code{nil}.
484
1911e6e5 485@deffn Command erase-buffer
73804d4b
RS
486This function deletes the entire text of the current buffer, leaving it
487empty. If the buffer is read-only, it signals a @code{buffer-read-only}
2468d0c0
DL
488error; if some of the text in it is read-only, it signals a
489@code{text-read-only} error. Otherwise, it deletes the text without
490asking for any confirmation. It returns @code{nil}.
8241495d 491
73804d4b
RS
492Normally, deleting a large amount of text from a buffer inhibits further
493auto-saving of that buffer ``because it has shrunk''. However,
494@code{erase-buffer} does not do this, the idea being that the future
495text is not really related to the former text, and its size should not
496be compared with that of the former text.
1911e6e5 497@end deffn
73804d4b
RS
498
499@deffn Command delete-region start end
1dffc5db
RS
500This command deletes the text between positions @var{start} and
501@var{end} in the current buffer, and returns @code{nil}. If point was
502inside the deleted region, its value afterward is @var{start}.
68002b5f 503Otherwise, point relocates with the surrounding text, as markers do.
73804d4b
RS
504@end deffn
505
1dffc5db
RS
506@defun delete-and-extract-region start end
507@tindex delete-and-extract-region
508This function deletes the text between positions @var{start} and
509@var{end} in the current buffer, and returns a string containing the
510text just deleted.
511
512If point was inside the deleted region, its value afterward is
513@var{start}. Otherwise, point relocates with the surrounding text, as
514markers do.
515@end defun
516
73804d4b
RS
517@deffn Command delete-char count &optional killp
518This command deletes @var{count} characters directly after point, or
519before point if @var{count} is negative. If @var{killp} is
520non-@code{nil}, then it saves the deleted characters in the kill ring.
521
522In an interactive call, @var{count} is the numeric prefix argument, and
523@var{killp} is the unprocessed prefix argument. Therefore, if a prefix
524argument is supplied, the text is saved in the kill ring. If no prefix
525argument is supplied, then one character is deleted, but not saved in
526the kill ring.
527
528The value returned is always @code{nil}.
529@end deffn
530
531@deffn Command delete-backward-char count &optional killp
532@cindex delete previous char
533This command deletes @var{count} characters directly before point, or
534after point if @var{count} is negative. If @var{killp} is
535non-@code{nil}, then it saves the deleted characters in the kill ring.
536
537In an interactive call, @var{count} is the numeric prefix argument, and
538@var{killp} is the unprocessed prefix argument. Therefore, if a prefix
539argument is supplied, the text is saved in the kill ring. If no prefix
540argument is supplied, then one character is deleted, but not saved in
541the kill ring.
542
543The value returned is always @code{nil}.
544@end deffn
545
546@deffn Command backward-delete-char-untabify count &optional killp
547@cindex tab deletion
548This command deletes @var{count} characters backward, changing tabs
549into spaces. When the next character to be deleted is a tab, it is
550first replaced with the proper number of spaces to preserve alignment
551and then one of those spaces is deleted instead of the tab. If
552@var{killp} is non-@code{nil}, then the command saves the deleted
553characters in the kill ring.
554
555Conversion of tabs to spaces happens only if @var{count} is positive.
556If it is negative, exactly @minus{}@var{count} characters after point
557are deleted.
558
559In an interactive call, @var{count} is the numeric prefix argument, and
560@var{killp} is the unprocessed prefix argument. Therefore, if a prefix
561argument is supplied, the text is saved in the kill ring. If no prefix
562argument is supplied, then one character is deleted, but not saved in
563the kill ring.
564
565The value returned is always @code{nil}.
566@end deffn
567
969fe9b5
RS
568@defopt backward-delete-char-untabify-method
569This option specifies how @code{backward-delete-char-untabify} should
570deal with whitespace. Possible values include @code{untabify}, the
571default, meaning convert a tab to many spaces and delete one;
572@code{hungry}, meaning delete all the whitespace characters before point
573with one command, and @code{nil}, meaning do nothing special for
574whitespace characters.
575@end defopt
576
73804d4b
RS
577@node User-Level Deletion
578@section User-Level Deletion Commands
579
580 This section describes higher-level commands for deleting text,
581commands intended primarily for the user but useful also in Lisp
582programs.
583
584@deffn Command delete-horizontal-space
585@cindex deleting whitespace
586This function deletes all spaces and tabs around point. It returns
587@code{nil}.
588
589In the following examples, we call @code{delete-horizontal-space} four
590times, once on each line, with point between the second and third
61cfa852 591characters on the line each time.
73804d4b
RS
592
593@example
594@group
595---------- Buffer: foo ----------
596I @point{}thought
597I @point{} thought
598We@point{} thought
599Yo@point{}u thought
600---------- Buffer: foo ----------
601@end group
602
603@group
604(delete-horizontal-space) ; @r{Four times.}
605 @result{} nil
606
607---------- Buffer: foo ----------
608Ithought
609Ithought
610Wethought
611You thought
612---------- Buffer: foo ----------
613@end group
614@end example
615@end deffn
616
617@deffn Command delete-indentation &optional join-following-p
618This function joins the line point is on to the previous line, deleting
619any whitespace at the join and in some cases replacing it with one
620space. If @var{join-following-p} is non-@code{nil},
621@code{delete-indentation} joins this line to the following line
a9f0a989 622instead. The function returns @code{nil}.
73804d4b
RS
623
624If there is a fill prefix, and the second of the lines being joined
625starts with the prefix, then @code{delete-indentation} deletes the
bfe721d1 626fill prefix before joining the lines. @xref{Margins}.
73804d4b
RS
627
628In the example below, point is located on the line starting
629@samp{events}, and it makes no difference if there are trailing spaces
630in the preceding line.
631
632@smallexample
b22f3a19 633@group
73804d4b
RS
634---------- Buffer: foo ----------
635When in the course of human
636@point{} events, it becomes necessary
637---------- Buffer: foo ----------
b22f3a19 638@end group
73804d4b
RS
639
640(delete-indentation)
641 @result{} nil
642
b22f3a19 643@group
73804d4b
RS
644---------- Buffer: foo ----------
645When in the course of human@point{} events, it becomes necessary
646---------- Buffer: foo ----------
b22f3a19 647@end group
73804d4b
RS
648@end smallexample
649
650After the lines are joined, the function @code{fixup-whitespace} is
651responsible for deciding whether to leave a space at the junction.
652@end deffn
653
654@defun fixup-whitespace
a9f0a989 655This function replaces all the whitespace surrounding point with either
73804d4b
RS
656one space or no space, according to the context. It returns @code{nil}.
657
658At the beginning or end of a line, the appropriate amount of space is
659none. Before a character with close parenthesis syntax, or after a
660character with open parenthesis or expression-prefix syntax, no space is
661also appropriate. Otherwise, one space is appropriate. @xref{Syntax
662Class Table}.
663
664In the example below, @code{fixup-whitespace} is called the first time
61cfa852
RS
665with point before the word @samp{spaces} in the first line. For the
666second invocation, point is directly after the @samp{(}.
73804d4b
RS
667
668@smallexample
669@group
670---------- Buffer: foo ----------
671This has too many @point{}spaces
672This has too many spaces at the start of (@point{} this list)
673---------- Buffer: foo ----------
674@end group
675
676@group
677(fixup-whitespace)
678 @result{} nil
679(fixup-whitespace)
680 @result{} nil
681@end group
682
683@group
684---------- Buffer: foo ----------
685This has too many spaces
686This has too many spaces at the start of (this list)
687---------- Buffer: foo ----------
688@end group
689@end smallexample
690@end defun
691
692@deffn Command just-one-space
693@comment !!SourceFile simple.el
694This command replaces any spaces and tabs around point with a single
695space. It returns @code{nil}.
696@end deffn
697
698@deffn Command delete-blank-lines
699This function deletes blank lines surrounding point. If point is on a
700blank line with one or more blank lines before or after it, then all but
701one of them are deleted. If point is on an isolated blank line, then it
702is deleted. If point is on a nonblank line, the command deletes all
703blank lines following it.
704
705A blank line is defined as a line containing only tabs and spaces.
706
707@code{delete-blank-lines} returns @code{nil}.
708@end deffn
709
710@node The Kill Ring
711@section The Kill Ring
712@cindex kill ring
713
969fe9b5 714 @dfn{Kill functions} delete text like the deletion functions, but save
73804d4b
RS
715it so that the user can reinsert it by @dfn{yanking}. Most of these
716functions have @samp{kill-} in their name. By contrast, the functions
717whose names start with @samp{delete-} normally do not save text for
718yanking (though they can still be undone); these are ``deletion''
719functions.
720
721 Most of the kill commands are primarily for interactive use, and are
722not described here. What we do describe are the functions provided for
723use in writing such commands. You can use these functions to write
61cfa852 724commands for killing text. When you need to delete text for internal
73804d4b
RS
725purposes within a Lisp function, you should normally use deletion
726functions, so as not to disturb the kill ring contents.
727@xref{Deletion}.
728
729 Killed text is saved for later yanking in the @dfn{kill ring}. This
61cfa852
RS
730is a list that holds a number of recent kills, not just the last text
731kill. We call this a ``ring'' because yanking treats it as having
732elements in a cyclic order. The list is kept in the variable
733@code{kill-ring}, and can be operated on with the usual functions for
734lists; there are also specialized functions, described in this section,
735that treat it as a ring.
73804d4b
RS
736
737 Some people think this use of the word ``kill'' is unfortunate, since
61cfa852 738it refers to operations that specifically @emph{do not} destroy the
73804d4b
RS
739entities ``killed''. This is in sharp contrast to ordinary life, in
740which death is permanent and ``killed'' entities do not come back to
741life. Therefore, other metaphors have been proposed. For example, the
742term ``cut ring'' makes sense to people who, in pre-computer days, used
743scissors and paste to cut up and rearrange manuscripts. However, it
744would be difficult to change the terminology now.
745
746@menu
747* Kill Ring Concepts:: What text looks like in the kill ring.
748* Kill Functions:: Functions that kill text.
749* Yank Commands:: Commands that access the kill ring.
61cfa852 750* Low-Level Kill Ring:: Functions and variables for kill ring access.
73804d4b
RS
751* Internals of Kill Ring:: Variables that hold kill-ring data.
752@end menu
753
754@node Kill Ring Concepts
755@comment node-name, next, previous, up
756@subsection Kill Ring Concepts
757
758 The kill ring records killed text as strings in a list, most recent
759first. A short kill ring, for example, might look like this:
760
761@example
762("some text" "a different piece of text" "even older text")
763@end example
764
765@noindent
766When the list reaches @code{kill-ring-max} entries in length, adding a
767new entry automatically deletes the last entry.
768
769 When kill commands are interwoven with other commands, each kill
770command makes a new entry in the kill ring. Multiple kill commands in
a9f0a989
RS
771succession build up a single kill-ring entry, which would be yanked as a
772unit; the second and subsequent consecutive kill commands add text to
773the entry made by the first one.
73804d4b
RS
774
775 For yanking, one entry in the kill ring is designated the ``front'' of
776the ring. Some yank commands ``rotate'' the ring by designating a
777different element as the ``front.'' But this virtual rotation doesn't
778change the list itself---the most recent entry always comes first in the
779list.
780
781@node Kill Functions
782@comment node-name, next, previous, up
783@subsection Functions for Killing
784
785 @code{kill-region} is the usual subroutine for killing text. Any
786command that calls this function is a ``kill command'' (and should
787probably have @samp{kill} in its name). @code{kill-region} puts the
788newly killed text in a new element at the beginning of the kill ring or
f9f59935
RS
789adds it to the most recent element. It determines automatically (using
790@code{last-command}) whether the previous command was a kill command,
61cfa852 791and if so appends the killed text to the most recent entry.
73804d4b
RS
792
793@deffn Command kill-region start end
794This function kills the text in the region defined by @var{start} and
bfe721d1
KH
795@var{end}. The text is deleted but saved in the kill ring, along with
796its text properties. The value is always @code{nil}.
73804d4b
RS
797
798In an interactive call, @var{start} and @var{end} are point and
799the mark.
800
801@c Emacs 19 feature
2468d0c0
DL
802If the buffer or text is read-only, @code{kill-region} modifies the kill
803ring just the same, then signals an error without modifying the buffer.
804This is convenient because it lets the user use a series of kill
805commands to copy text from a read-only buffer into the kill ring.
73804d4b
RS
806@end deffn
807
1911e6e5 808@defopt kill-read-only-ok
2468d0c0
DL
809If this option is non-@code{nil}, @code{kill-region} does not signal an
810error if the buffer or text is read-only. Instead, it simply returns,
811updating the kill ring but not changing the buffer.
1911e6e5
RS
812@end defopt
813
73804d4b
RS
814@deffn Command copy-region-as-kill start end
815This command saves the region defined by @var{start} and @var{end} on
bfe721d1
KH
816the kill ring (including text properties), but does not delete the text
817from the buffer. It returns @code{nil}. It also indicates the extent
818of the text copied by moving the cursor momentarily, or by displaying a
819message in the echo area.
73804d4b 820
22697dac
KH
821The command does not set @code{this-command} to @code{kill-region}, so a
822subsequent kill command does not append to the same kill ring entry.
823
73804d4b 824Don't call @code{copy-region-as-kill} in Lisp programs unless you aim to
f9f59935
RS
825support Emacs 18. For newer Emacs versions, it is better to use
826@code{kill-new} or @code{kill-append} instead. @xref{Low-Level Kill
827Ring}.
73804d4b
RS
828@end deffn
829
830@node Yank Commands
831@comment node-name, next, previous, up
832@subsection Functions for Yanking
833
834 @dfn{Yanking} means reinserting an entry of previously killed text
bfe721d1 835from the kill ring. The text properties are copied too.
73804d4b
RS
836
837@deffn Command yank &optional arg
838@cindex inserting killed text
839This command inserts before point the text in the first entry in the
840kill ring. It positions the mark at the beginning of that text, and
841point at the end.
842
843If @var{arg} is a list (which occurs interactively when the user
844types @kbd{C-u} with no digits), then @code{yank} inserts the text as
845described above, but puts point before the yanked text and puts the mark
846after it.
847
848If @var{arg} is a number, then @code{yank} inserts the @var{arg}th most
849recently killed text---the @var{arg}th element of the kill ring list.
850
851@code{yank} does not alter the contents of the kill ring or rotate it.
852It returns @code{nil}.
853@end deffn
854
855@deffn Command yank-pop arg
856This command replaces the just-yanked entry from the kill ring with a
857different entry from the kill ring.
858
859This is allowed only immediately after a @code{yank} or another
860@code{yank-pop}. At such a time, the region contains text that was just
861inserted by yanking. @code{yank-pop} deletes that text and inserts in
862its place a different piece of killed text. It does not add the deleted
863text to the kill ring, since it is already in the kill ring somewhere.
864
865If @var{arg} is @code{nil}, then the replacement text is the previous
866element of the kill ring. If @var{arg} is numeric, the replacement is
867the @var{arg}th previous kill. If @var{arg} is negative, a more recent
868kill is the replacement.
869
870The sequence of kills in the kill ring wraps around, so that after the
871oldest one comes the newest one, and before the newest one goes the
872oldest.
873
a9f0a989 874The return value is always @code{nil}.
73804d4b
RS
875@end deffn
876
61cfa852
RS
877@node Low-Level Kill Ring
878@subsection Low-Level Kill Ring
73804d4b 879
969fe9b5
RS
880 These functions and variables provide access to the kill ring at a
881lower level, but still convenient for use in Lisp programs, because they
882take care of interaction with window system selections
883(@pxref{Window System Selections}).
73804d4b
RS
884
885@defun current-kill n &optional do-not-move
a9f0a989
RS
886The function @code{current-kill} rotates the yanking pointer, which
887designates the ``front'' of the kill ring, by @var{n} places (from newer
61cfa852 888kills to older ones), and returns the text at that place in the ring.
73804d4b
RS
889
890If the optional second argument @var{do-not-move} is non-@code{nil},
891then @code{current-kill} doesn't alter the yanking pointer; it just
61cfa852 892returns the @var{n}th kill, counting from the current yanking pointer.
73804d4b
RS
893
894If @var{n} is zero, indicating a request for the latest kill,
895@code{current-kill} calls the value of
896@code{interprogram-paste-function} (documented below) before consulting
897the kill ring.
898@end defun
899
900@defun kill-new string
901This function puts the text @var{string} into the kill ring as a new
902entry at the front of the ring. It discards the oldest entry if
903appropriate. It also invokes the value of
904@code{interprogram-cut-function} (see below).
905@end defun
906
907@defun kill-append string before-p
908This function appends the text @var{string} to the first entry in the
909kill ring. Normally @var{string} goes at the end of the entry, but if
910@var{before-p} is non-@code{nil}, it goes at the beginning. This
911function also invokes the value of @code{interprogram-cut-function} (see
912below).
913@end defun
914
915@defvar interprogram-paste-function
916This variable provides a way of transferring killed text from other
917programs, when you are using a window system. Its value should be
918@code{nil} or a function of no arguments.
919
920If the value is a function, @code{current-kill} calls it to get the
921``most recent kill''. If the function returns a non-@code{nil} value,
922then that value is used as the ``most recent kill''. If it returns
923@code{nil}, then the first element of @code{kill-ring} is used.
924
969fe9b5
RS
925The normal use of this hook is to get the window system's primary
926selection as the most recent kill, even if the selection belongs to
927another application. @xref{Window System Selections}.
73804d4b
RS
928@end defvar
929
930@defvar interprogram-cut-function
61cfa852
RS
931This variable provides a way of communicating killed text to other
932programs, when you are using a window system. Its value should be
73804d4b
RS
933@code{nil} or a function of one argument.
934
935If the value is a function, @code{kill-new} and @code{kill-append} call
936it with the new first element of the kill ring as an argument.
937
969fe9b5
RS
938The normal use of this hook is to set the window system's primary
939selection from the newly killed text. @xref{Window System Selections}.
73804d4b
RS
940@end defvar
941
942@node Internals of Kill Ring
943@comment node-name, next, previous, up
944@subsection Internals of the Kill Ring
945
946 The variable @code{kill-ring} holds the kill ring contents, in the
947form of a list of strings. The most recent kill is always at the front
948of the list.
949
950 The @code{kill-ring-yank-pointer} variable points to a link in the
61cfa852
RS
951kill ring list, whose @sc{car} is the text to yank next. We say it
952identifies the ``front'' of the ring. Moving
73804d4b 953@code{kill-ring-yank-pointer} to a different link is called
61cfa852
RS
954@dfn{rotating the kill ring}. We call the kill ring a ``ring'' because
955the functions that move the yank pointer wrap around from the end of the
956list to the beginning, or vice-versa. Rotation of the kill ring is
957virtual; it does not change the value of @code{kill-ring}.
73804d4b
RS
958
959 Both @code{kill-ring} and @code{kill-ring-yank-pointer} are Lisp
960variables whose values are normally lists. The word ``pointer'' in the
961name of the @code{kill-ring-yank-pointer} indicates that the variable's
962purpose is to identify one element of the list for use by the next yank
963command.
964
965 The value of @code{kill-ring-yank-pointer} is always @code{eq} to one
966of the links in the kill ring list. The element it identifies is the
967@sc{car} of that link. Kill commands, which change the kill ring, also
61cfa852
RS
968set this variable to the value of @code{kill-ring}. The effect is to
969rotate the ring so that the newly killed text is at the front.
73804d4b
RS
970
971 Here is a diagram that shows the variable @code{kill-ring-yank-pointer}
972pointing to the second entry in the kill ring @code{("some text" "a
973different piece of text" "yet older text")}.
974
975@example
976@group
969fe9b5
RS
977kill-ring ---- kill-ring-yank-pointer
978 | |
979 | v
980 | --- --- --- --- --- ---
981 --> | | |------> | | |--> | | |--> nil
982 --- --- --- --- --- ---
73804d4b
RS
983 | | |
984 | | |
985 | | -->"yet older text"
986 | |
987 | --> "a different piece of text"
988 |
989 --> "some text"
990@end group
991@end example
992
993@noindent
994This state of affairs might occur after @kbd{C-y} (@code{yank})
995immediately followed by @kbd{M-y} (@code{yank-pop}).
996
997@defvar kill-ring
61cfa852
RS
998This variable holds the list of killed text sequences, most recently
999killed first.
73804d4b
RS
1000@end defvar
1001
1002@defvar kill-ring-yank-pointer
1003This variable's value indicates which element of the kill ring is at the
1004``front'' of the ring for yanking. More precisely, the value is a tail
1005of the value of @code{kill-ring}, and its @sc{car} is the kill string
1006that @kbd{C-y} should yank.
1007@end defvar
1008
1009@defopt kill-ring-max
1010The value of this variable is the maximum length to which the kill
1011ring can grow, before elements are thrown away at the end. The default
1012value for @code{kill-ring-max} is 30.
1013@end defopt
1014
1015@node Undo
1016@comment node-name, next, previous, up
1017@section Undo
1018@cindex redo
1019
61cfa852
RS
1020 Most buffers have an @dfn{undo list}, which records all changes made
1021to the buffer's text so that they can be undone. (The buffers that
1022don't have one are usually special-purpose buffers for which Emacs
1023assumes that undoing is not useful.) All the primitives that modify the
1024text in the buffer automatically add elements to the front of the undo
1025list, which is in the variable @code{buffer-undo-list}.
73804d4b
RS
1026
1027@defvar buffer-undo-list
1028This variable's value is the undo list of the current buffer.
1029A value of @code{t} disables the recording of undo information.
1030@end defvar
1031
1032Here are the kinds of elements an undo list can have:
1033
1034@table @code
1911e6e5
RS
1035@item @var{position}
1036This kind of element records a previous value of point; undoing this
1037element moves point to @var{position}. Ordinary cursor motion does not
1038make any sort of undo record, but deletion operations use these entries
1039to record where point was before the command.
73804d4b
RS
1040
1041@item (@var{beg} . @var{end})
1042This kind of element indicates how to delete text that was inserted.
1043Upon insertion, the text occupied the range @var{beg}--@var{end} in the
1044buffer.
1045
d1756ab9 1046@item (@var{text} . @var{position})
73804d4b 1047This kind of element indicates how to reinsert text that was deleted.
d1756ab9
RS
1048The deleted text itself is the string @var{text}. The place to
1049reinsert it is @code{(abs @var{position})}.
73804d4b
RS
1050
1051@item (t @var{high} . @var{low})
1052This kind of element indicates that an unmodified buffer became
1053modified. The elements @var{high} and @var{low} are two integers, each
1054recording 16 bits of the visited file's modification time as of when it
1055was previously visited or saved. @code{primitive-undo} uses those
1056values to determine whether to mark the buffer as unmodified once again;
1057it does so only if the file's modification time matches those numbers.
1058
1059@item (nil @var{property} @var{value} @var{beg} . @var{end})
1060This kind of element records a change in a text property.
1061Here's how you might undo the change:
1062
1063@example
1064(put-text-property @var{beg} @var{end} @var{property} @var{value})
1065@end example
d1756ab9 1066
61ee3601
RS
1067@item (@var{marker} . @var{adjustment})
1068This kind of element records the fact that the marker @var{marker} was
1069relocated due to deletion of surrounding text, and that it moved
1070@var{adjustment} character positions. Undoing this element moves
1071@var{marker} @minus{} @var{adjustment} characters.
1072
73804d4b
RS
1073@item nil
1074This element is a boundary. The elements between two boundaries are
1075called a @dfn{change group}; normally, each change group corresponds to
1076one keyboard command, and undo commands normally undo an entire group as
1077a unit.
1078@end table
1079
1080@defun undo-boundary
1081This function places a boundary element in the undo list. The undo
1082command stops at such a boundary, and successive undo commands undo
1083to earlier and earlier boundaries. This function returns @code{nil}.
1084
b6a786ce
RS
1085The editor command loop automatically creates an undo boundary before
1086each key sequence is executed. Thus, each undo normally undoes the
1087effects of one command. Self-inserting input characters are an
1088exception. The command loop makes a boundary for the first such
1089character; the next 19 consecutive self-inserting input characters do
1090not make boundaries, and then the 20th does, and so on as long as
1091self-inserting characters continue.
1092
1093All buffer modifications add a boundary whenever the previous undoable
a9f0a989
RS
1094change was made in some other buffer. This is to ensure that
1095each command makes a boundary in each buffer where it makes changes.
b6a786ce
RS
1096
1097Calling this function explicitly is useful for splitting the effects of
1098a command into more than one unit. For example, @code{query-replace}
1099calls @code{undo-boundary} after each replacement, so that the user can
1100undo individual replacements one by one.
73804d4b
RS
1101@end defun
1102
1103@defun primitive-undo count list
1104This is the basic function for undoing elements of an undo list.
1105It undoes the first @var{count} elements of @var{list}, returning
1106the rest of @var{list}. You could write this function in Lisp,
1107but it is convenient to have it in C.
1108
1109@code{primitive-undo} adds elements to the buffer's undo list when it
1110changes the buffer. Undo commands avoid confusion by saving the undo
1111list value at the beginning of a sequence of undo operations. Then the
1112undo operations use and update the saved value. The new elements added
bfe721d1 1113by undoing are not part of this saved value, so they don't interfere with
73804d4b
RS
1114continuing to undo.
1115@end defun
1116
1117@node Maintaining Undo
1118@section Maintaining Undo Lists
1119
1120 This section describes how to enable and disable undo information for
1121a given buffer. It also explains how the undo list is truncated
1122automatically so it doesn't get too big.
1123
1124 Recording of undo information in a newly created buffer is normally
1125enabled to start with; but if the buffer name starts with a space, the
1126undo recording is initially disabled. You can explicitly enable or
1127disable undo recording with the following two functions, or by setting
1128@code{buffer-undo-list} yourself.
1129
1130@deffn Command buffer-enable-undo &optional buffer-or-name
1131This command enables recording undo information for buffer
1132@var{buffer-or-name}, so that subsequent changes can be undone. If no
1133argument is supplied, then the current buffer is used. This function
1134does nothing if undo recording is already enabled in the buffer. It
1135returns @code{nil}.
1136
1137In an interactive call, @var{buffer-or-name} is the current buffer.
1138You cannot specify any other buffer.
1139@end deffn
1140
a9f0a989
RS
1141@deffn Command buffer-disable-undo &optional buffer
1142@deffnx Command buffer-flush-undo &optional buffer
73804d4b
RS
1143@cindex disable undo
1144This function discards the undo list of @var{buffer}, and disables
1145further recording of undo information. As a result, it is no longer
1146possible to undo either previous changes or any subsequent changes. If
1147the undo list of @var{buffer} is already disabled, this function
1148has no effect.
1149
a9f0a989 1150This function returns @code{nil}.
73804d4b
RS
1151
1152The name @code{buffer-flush-undo} is not considered obsolete, but the
969fe9b5 1153preferred name is @code{buffer-disable-undo}.
a9f0a989 1154@end deffn
73804d4b
RS
1155
1156 As editing continues, undo lists get longer and longer. To prevent
1157them from using up all available memory space, garbage collection trims
1158them back to size limits you can set. (For this purpose, the ``size''
1159of an undo list measures the cons cells that make up the list, plus the
1160strings of deleted text.) Two variables control the range of acceptable
1161sizes: @code{undo-limit} and @code{undo-strong-limit}.
1162
1163@defvar undo-limit
1164This is the soft limit for the acceptable size of an undo list. The
1165change group at which this size is exceeded is the last one kept.
1166@end defvar
1167
1168@defvar undo-strong-limit
61cfa852
RS
1169This is the upper limit for the acceptable size of an undo list. The
1170change group at which this size is exceeded is discarded itself (along
1171with all older change groups). There is one exception: the very latest
bda144f4 1172change group is never discarded no matter how big it is.
73804d4b
RS
1173@end defvar
1174
1175@node Filling
1176@comment node-name, next, previous, up
1177@section Filling
1178@cindex filling, explicit
1179
1180 @dfn{Filling} means adjusting the lengths of lines (by moving the line
1181breaks) so that they are nearly (but no greater than) a specified
1182maximum width. Additionally, lines can be @dfn{justified}, which means
bfe721d1
KH
1183inserting spaces to make the left and/or right margins line up
1184precisely. The width is controlled by the variable @code{fill-column}.
1185For ease of reading, lines should be no longer than 70 or so columns.
73804d4b
RS
1186
1187 You can use Auto Fill mode (@pxref{Auto Filling}) to fill text
1188automatically as you insert it, but changes to existing text may leave
1189it improperly filled. Then you must fill the text explicitly.
1190
22697dac
KH
1191 Most of the commands in this section return values that are not
1192meaningful. All the functions that do filling take note of the current
bfe721d1
KH
1193left margin, current right margin, and current justification style
1194(@pxref{Margins}). If the current justification style is
1195@code{none}, the filling functions don't actually do anything.
1196
1197 Several of the filling functions have an argument @var{justify}.
1198If it is non-@code{nil}, that requests some kind of justification. It
1199can be @code{left}, @code{right}, @code{full}, or @code{center}, to
1200request a specific style of justification. If it is @code{t}, that
1201means to use the current justification style for this part of the text
969fe9b5
RS
1202(see @code{current-justification}, below). Any other value is treated
1203as @code{full}.
bfe721d1
KH
1204
1205 When you call the filling functions interactively, using a prefix
1206argument implies the value @code{full} for @var{justify}.
73804d4b 1207
bfe721d1 1208@deffn Command fill-paragraph justify
73804d4b
RS
1209@cindex filling a paragraph
1210This command fills the paragraph at or after point. If
bfe721d1 1211@var{justify} is non-@code{nil}, each line is justified as well.
73804d4b 1212It uses the ordinary paragraph motion commands to find paragraph
0b2fb045 1213boundaries. @xref{Paragraphs,,, emacs, The GNU Emacs Manual}.
73804d4b
RS
1214@end deffn
1215
15da7853 1216@deffn Command fill-region start end &optional justify nosqueeze to-eop
73804d4b 1217This command fills each of the paragraphs in the region from @var{start}
bfe721d1 1218to @var{end}. It justifies as well if @var{justify} is
73804d4b
RS
1219non-@code{nil}.
1220
1911e6e5
RS
1221If @var{nosqueeze} is non-@code{nil}, that means to leave whitespace
1222other than line breaks untouched. If @var{to-eop} is non-@code{nil},
ebc6903b 1223that means to keep filling to the end of the paragraph---or the next hard
1911e6e5
RS
1224newline, if @code{use-hard-newlines} is enabled (see below).
1225
73804d4b
RS
1226The variable @code{paragraph-separate} controls how to distinguish
1227paragraphs. @xref{Standard Regexps}.
1228@end deffn
1229
2468d0c0 1230@deffn Command fill-individual-paragraphs start end &optional justify citation-regexp
73804d4b
RS
1231This command fills each paragraph in the region according to its
1232individual fill prefix. Thus, if the lines of a paragraph were indented
1233with spaces, the filled paragraph will remain indented in the same
1234fashion.
1235
1236The first two arguments, @var{start} and @var{end}, are the beginning
1237and end of the region to be filled. The third and fourth arguments,
2468d0c0 1238@var{justify} and @var{citation-regexp}, are optional. If
bfe721d1 1239@var{justify} is non-@code{nil}, the paragraphs are justified as
2468d0c0 1240well as filled. If @var{citation-regexp} is non-@code{nil}, it means the
73804d4b 1241function is operating on a mail message and therefore should not fill
2468d0c0
DL
1242the header lines. If @var{citation-regexp} is a string, it is used as
1243a regular expression; if it matches the beginning of a line, that line
1244is treated as a citation marker.
73804d4b
RS
1245
1246Ordinarily, @code{fill-individual-paragraphs} regards each change in
1247indentation as starting a new paragraph. If
1248@code{fill-individual-varying-indent} is non-@code{nil}, then only
61cfa852
RS
1249separator lines separate paragraphs. That mode can handle indented
1250paragraphs with additional indentation on the first line.
73804d4b
RS
1251@end deffn
1252
1253@defopt fill-individual-varying-indent
1254This variable alters the action of @code{fill-individual-paragraphs} as
1255described above.
1256@end defopt
1257
1911e6e5 1258@deffn Command fill-region-as-paragraph start end &optional justify nosqueeze squeeze-after
f9f59935
RS
1259This command considers a region of text as a single paragraph and fills
1260it. If the region was made up of many paragraphs, the blank lines
1261between paragraphs are removed. This function justifies as well as
1262filling when @var{justify} is non-@code{nil}.
22697dac
KH
1263
1264In an interactive call, any prefix argument requests justification.
73804d4b 1265
1911e6e5
RS
1266If @var{nosqueeze} is non-@code{nil}, that means to leave whitespace
1267other than line breaks untouched. If @var{squeeze-after} is
ebc6903b 1268non-@code{nil}, it specifies a position in the region, and means don't
1911e6e5
RS
1269canonicalize spaces before that position.
1270
1271In Adaptive Fill mode, this command calls @code{fill-context-prefix} to
1272choose a fill prefix by default. @xref{Adaptive Fill}.
73804d4b
RS
1273@end deffn
1274
2468d0c0 1275@deffn Command justify-current-line &optional how eop nosqueeze
73804d4b
RS
1276This command inserts spaces between the words of the current line so
1277that the line ends exactly at @code{fill-column}. It returns
1278@code{nil}.
22697dac
KH
1279
1280The argument @var{how}, if non-@code{nil} specifies explicitly the style
1281of justification. It can be @code{left}, @code{right}, @code{full},
1282@code{center}, or @code{none}. If it is @code{t}, that means to do
1283follow specified justification style (see @code{current-justification},
1284below). @code{nil} means to do full justification.
1285
9e2b495b 1286If @var{eop} is non-@code{nil}, that means do left-justification if
22697dac
KH
1287@code{current-justification} specifies full justification. This is used
1288for the last line of a paragraph; even if the paragraph as a whole is
1289fully justified, the last line should not be.
1290
1291If @var{nosqueeze} is non-@code{nil}, that means do not change interior
1292whitespace.
73804d4b
RS
1293@end deffn
1294
22697dac
KH
1295@defopt default-justification
1296This variable's value specifies the style of justification to use for
1297text that doesn't specify a style with a text property. The possible
1298values are @code{left}, @code{right}, @code{full}, @code{center}, or
bfe721d1 1299@code{none}. The default value is @code{left}.
22697dac
KH
1300@end defopt
1301
1302@defun current-justification
1303This function returns the proper justification style to use for filling
1304the text around point.
1305@end defun
1306
1911e6e5
RS
1307@defopt sentence-end-double-space
1308If this variable is non-@code{nil}, a period followed by just one space
1309does not count as the end of a sentence, and the filling functions
1310avoid breaking the line at such a place.
1311@end defopt
1312
bfe721d1
KH
1313@defvar fill-paragraph-function
1314This variable provides a way for major modes to override the filling of
1315paragraphs. If the value is non-@code{nil}, @code{fill-paragraph} calls
1316this function to do the work. If the function returns a non-@code{nil}
1317value, @code{fill-paragraph} assumes the job is done, and immediately
1318returns that value.
1319
1320The usual use of this feature is to fill comments in programming
1321language modes. If the function needs to fill a paragraph in the usual
1322way, it can do so as follows:
1323
1324@example
1325(let ((fill-paragraph-function nil))
1326 (fill-paragraph arg))
1327@end example
1328@end defvar
1329
1330@defvar use-hard-newlines
1331If this variable is non-@code{nil}, the filling functions do not delete
1332newlines that have the @code{hard} text property. These ``hard
1333newlines'' act as paragraph separators.
1334@end defvar
1335
1336@node Margins
1337@section Margins for Filling
1338
61cfa852 1339@defopt fill-prefix
a9f0a989
RS
1340This buffer-local variable specifies a string of text that appears at
1341the beginning
61cfa852
RS
1342of normal text lines and should be disregarded when filling them. Any
1343line that fails to start with the fill prefix is considered the start of
1344a paragraph; so is any line that starts with the fill prefix followed by
1345additional whitespace. Lines that start with the fill prefix but no
1346additional whitespace are ordinary text lines that can be filled
1347together. The resulting filled lines also start with the fill prefix.
bfe721d1
KH
1348
1349The fill prefix follows the left margin whitespace, if any.
61cfa852
RS
1350@end defopt
1351
1911e6e5 1352@defopt fill-column
f9f59935
RS
1353This buffer-local variable specifies the maximum width of filled lines.
1354Its value should be an integer, which is a number of columns. All the
1355filling, justification, and centering commands are affected by this
1356variable, including Auto Fill mode (@pxref{Auto Filling}).
73804d4b
RS
1357
1358As a practical matter, if you are writing text for other people to
1359read, you should set @code{fill-column} to no more than 70. Otherwise
1360the line will be too long for people to read comfortably, and this can
1361make the text seem clumsy.
1911e6e5 1362@end defopt
73804d4b
RS
1363
1364@defvar default-fill-column
1365The value of this variable is the default value for @code{fill-column} in
1366buffers that do not override it. This is the same as
1367@code{(default-value 'fill-column)}.
1368
1369The default value for @code{default-fill-column} is 70.
1370@end defvar
1371
22697dac
KH
1372@deffn Command set-left-margin from to margin
1373This sets the @code{left-margin} property on the text from @var{from} to
1374@var{to} to the value @var{margin}. If Auto Fill mode is enabled, this
1375command also refills the region to fit the new margin.
1376@end deffn
1377
1378@deffn Command set-right-margin from to margin
bfe721d1
KH
1379This sets the @code{right-margin} property on the text from @var{from}
1380to @var{to} to the value @var{margin}. If Auto Fill mode is enabled,
1381this command also refills the region to fit the new margin.
22697dac
KH
1382@end deffn
1383
1384@defun current-left-margin
1385This function returns the proper left margin value to use for filling
1386the text around point. The value is the sum of the @code{left-margin}
1387property of the character at the start of the current line (or zero if
bfe721d1 1388none), and the value of the variable @code{left-margin}.
22697dac
KH
1389@end defun
1390
1391@defun current-fill-column
1392This function returns the proper fill column value to use for filling
1393the text around point. The value is the value of the @code{fill-column}
1394variable, minus the value of the @code{right-margin} property of the
1395character after point.
1396@end defun
1397
1398@deffn Command move-to-left-margin &optional n force
1399This function moves point to the left margin of the current line. The
1400column moved to is determined by calling the function
bfe721d1 1401@code{current-left-margin}. If the argument @var{n} is non-@code{nil},
22697dac
KH
1402@code{move-to-left-margin} moves forward @var{n}@minus{}1 lines first.
1403
1404If @var{force} is non-@code{nil}, that says to fix the line's
1405indentation if that doesn't match the left margin value.
1406@end deffn
1407
2468d0c0
DL
1408@defun delete-to-left-margin &optional from to
1409This function removes left margin indentation from the text between
1410@var{from} and @var{to}. The amount of indentation to delete is
1411determined by calling @code{current-left-margin}. In no case does this
1412function delete non-whitespace. If @var{from} and @var{to} are omitted,
1413they default to the whole buffer.
22697dac
KH
1414@end defun
1415
bfe721d1
KH
1416@defun indent-to-left-margin
1417This is the default @code{indent-line-function}, used in Fundamental
1418mode, Text mode, etc. Its effect is to adjust the indentation at the
1419beginning of the current line to the value specified by the variable
1420@code{left-margin}. This may involve either inserting or deleting
1421whitespace.
1422@end defun
1423
1424@defvar left-margin
1425This variable specifies the base left margin column. In Fundamental
969fe9b5 1426mode, @kbd{C-j} indents to this column. This variable automatically
bfe721d1
KH
1427becomes buffer-local when set in any fashion.
1428@end defvar
1429
969fe9b5
RS
1430@defvar fill-nobreak-predicate
1431This variable gives major modes a way to specify not to break a line at
1432certain places. Its value should be a function. This function is
1433called during filling, with no arguments and with point located at the
1434place where a break is being considered. If the function returns
1435non-@code{nil}, then the line won't be broken there.
1436@end defvar
1437
1911e6e5
RS
1438@node Adaptive Fill
1439@section Adaptive Fill Mode
1440@cindex Adaptive Fill mode
1441
1442 Adaptive Fill mode chooses a fill prefix automatically from the text
1443in each paragraph being filled.
1444
1445@defopt adaptive-fill-mode
1446Adaptive Fill mode is enabled when this variable is non-@code{nil}.
1447It is @code{t} by default.
1448@end defopt
1449
1450@defun fill-context-prefix from to
1451This function implements the heart of Adaptive Fill mode; it chooses a
1452fill prefix based on the text between @var{from} and @var{to}. It does
1453this by looking at the first two lines of the paragraph, based on the
1454variables described below.
2468d0c0
DL
1455@c The optional argument first-line-regexp is not documented
1456@c because it exists for internal purposes and might be eliminated
1457@c in the future.
1911e6e5
RS
1458@end defun
1459
1460@defopt adaptive-fill-regexp
1461This variable holds a regular expression to control Adaptive Fill mode.
ebc6903b
RS
1462Adaptive Fill mode matches this regular expression against the text
1463starting after the left margin whitespace (if any) on a line; the
1464characters it matches are that line's candidate for the fill prefix.
1911e6e5
RS
1465@end defopt
1466
1467@defopt adaptive-fill-first-line-regexp
ebc6903b
RS
1468In a one-line paragraph, if the candidate fill prefix matches this
1469regular expression, or if it matches @code{comment-start-skip}, then it
1470is used---otherwise, spaces amounting to the same width are used
1471instead.
1911e6e5
RS
1472
1473However, the fill prefix is never taken from a one-line paragraph
1474if it would act as a paragraph starter on subsequent lines.
1475@end defopt
1476
1477@defopt adaptive-fill-function
1478You can specify more complex ways of choosing a fill prefix
1479automatically by setting this variable to a function. The function is
1480called when @code{adaptive-fill-regexp} does not match, with point after
1481the left margin of a line, and it should return the appropriate fill
1482prefix based on that line. If it returns @code{nil}, that means it sees
1483no fill prefix in that line.
1484@end defopt
1485
73804d4b
RS
1486@node Auto Filling
1487@comment node-name, next, previous, up
1488@section Auto Filling
1489@cindex filling, automatic
1490@cindex Auto Fill mode
1491
61cfa852 1492 Auto Fill mode is a minor mode that fills lines automatically as text
bda144f4 1493is inserted. This section describes the hook used by Auto Fill mode.
61cfa852
RS
1494For a description of functions that you can call explicitly to fill and
1495justify existing text, see @ref{Filling}.
73804d4b 1496
bfe721d1
KH
1497 Auto Fill mode also enables the functions that change the margins and
1498justification style to refill portions of the text. @xref{Margins}.
1499
73804d4b 1500@defvar auto-fill-function
22697dac 1501The value of this variable should be a function (of no arguments) to be
ca3be36e
DL
1502called after self-inserting a character from the table
1503@code{auto-fill-chars}. It may be @code{nil}, in which case nothing
1504special is done in that case.
73804d4b
RS
1505
1506The value of @code{auto-fill-function} is @code{do-auto-fill} when
1507Auto-Fill mode is enabled. That is a function whose sole purpose is to
1508implement the usual strategy for breaking a line.
1509
1510@quotation
1511In older Emacs versions, this variable was named @code{auto-fill-hook},
1512but since it is not called with the standard convention for hooks, it
1513was renamed to @code{auto-fill-function} in version 19.
1514@end quotation
1515@end defvar
1516
910bc071
RS
1517@defvar normal-auto-fill-function
1518This variable specifies the function to use for
1519@code{auto-fill-function}, if and when Auto Fill is turned on. Major
969fe9b5
RS
1520modes can set buffer-local values for this variable to alter how Auto
1521Fill works.
910bc071
RS
1522@end defvar
1523
ca3be36e
DL
1524@defvar auto-fill-chars
1525A char table of characters which invoke @code{auto-fill-function} when
1526self-inserted---space and newline in most language environments. They
1527have an entry @code{t} in the table.
1528@end defvar
1529
73804d4b
RS
1530@node Sorting
1531@section Sorting Text
1532@cindex sorting text
1533
1534 The sorting functions described in this section all rearrange text in
1535a buffer. This is in contrast to the function @code{sort}, which
1536rearranges the order of the elements of a list (@pxref{Rearrangement}).
1537The values returned by these functions are not meaningful.
1538
1539@defun sort-subr reverse nextrecfun endrecfun &optional startkeyfun endkeyfun
f9f59935
RS
1540This function is the general text-sorting routine that subdivides a
1541buffer into records and then sorts them. Most of the commands in this
1542section use this function.
73804d4b
RS
1543
1544To understand how @code{sort-subr} works, consider the whole accessible
1545portion of the buffer as being divided into disjoint pieces called
f9f59935
RS
1546@dfn{sort records}. The records may or may not be contiguous, but they
1547must not overlap. A portion of each sort record (perhaps all of it) is
73804d4b
RS
1548designated as the sort key. Sorting rearranges the records in order by
1549their sort keys.
1550
1551Usually, the records are rearranged in order of ascending sort key.
1552If the first argument to the @code{sort-subr} function, @var{reverse},
1553is non-@code{nil}, the sort records are rearranged in order of
1554descending sort key.
1555
1556The next four arguments to @code{sort-subr} are functions that are
1557called to move point across a sort record. They are called many times
1558from within @code{sort-subr}.
1559
1560@enumerate
1561@item
1562@var{nextrecfun} is called with point at the end of a record. This
1563function moves point to the start of the next record. The first record
1564is assumed to start at the position of point when @code{sort-subr} is
1565called. Therefore, you should usually move point to the beginning of
1566the buffer before calling @code{sort-subr}.
1567
1568This function can indicate there are no more sort records by leaving
1569point at the end of the buffer.
1570
1571@item
1572@var{endrecfun} is called with point within a record. It moves point to
1573the end of the record.
1574
1575@item
1576@var{startkeyfun} is called to move point from the start of a record to
1577the start of the sort key. This argument is optional; if it is omitted,
1578the whole record is the sort key. If supplied, the function should
1579either return a non-@code{nil} value to be used as the sort key, or
1580return @code{nil} to indicate that the sort key is in the buffer
1581starting at point. In the latter case, @var{endkeyfun} is called to
1582find the end of the sort key.
1583
1584@item
1585@var{endkeyfun} is called to move point from the start of the sort key
1586to the end of the sort key. This argument is optional. If
1587@var{startkeyfun} returns @code{nil} and this argument is omitted (or
1588@code{nil}), then the sort key extends to the end of the record. There
1589is no need for @var{endkeyfun} if @var{startkeyfun} returns a
1590non-@code{nil} value.
1591@end enumerate
1592
1593As an example of @code{sort-subr}, here is the complete function
1594definition for @code{sort-lines}:
1595
1596@example
1597@group
1598;; @r{Note that the first two lines of doc string}
1599;; @r{are effectively one line when viewed by a user.}
1600(defun sort-lines (reverse beg end)
f9f59935
RS
1601 "Sort lines in region alphabetically;\
1602 argument means descending order.
73804d4b
RS
1603Called from a program, there are three arguments:
1604@end group
1605@group
f9f59935
RS
1606REVERSE (non-nil means reverse order),\
1607 BEG and END (region to sort).
1608The variable `sort-fold-case' determines\
1609 whether alphabetic case affects
1610the sort order.
1611@end group
1612@group
73804d4b 1613 (interactive "P\nr")
1911e6e5
RS
1614 (save-excursion
1615 (save-restriction
1616 (narrow-to-region beg end)
1617 (goto-char (point-min))
1618 (sort-subr reverse 'forward-line 'end-of-line))))
73804d4b
RS
1619@end group
1620@end example
1621
1622Here @code{forward-line} moves point to the start of the next record,
1623and @code{end-of-line} moves point to the end of record. We do not pass
1624the arguments @var{startkeyfun} and @var{endkeyfun}, because the entire
1625record is used as the sort key.
1626
1627The @code{sort-paragraphs} function is very much the same, except that
1628its @code{sort-subr} call looks like this:
1629
1630@example
1631@group
1632(sort-subr reverse
1911e6e5
RS
1633 (function
1634 (lambda ()
1635 (while (and (not (eobp))
1636 (looking-at paragraph-separate))
1637 (forward-line 1))))
73804d4b
RS
1638 'forward-paragraph)
1639@end group
1640@end example
f9f59935
RS
1641
1642Markers pointing into any sort records are left with no useful
1643position after @code{sort-subr} returns.
73804d4b
RS
1644@end defun
1645
1911e6e5
RS
1646@defopt sort-fold-case
1647If this variable is non-@code{nil}, @code{sort-subr} and the other
1648buffer sorting functions ignore case when comparing strings.
1649@end defopt
1650
73804d4b
RS
1651@deffn Command sort-regexp-fields reverse record-regexp key-regexp start end
1652This command sorts the region between @var{start} and @var{end}
1653alphabetically as specified by @var{record-regexp} and @var{key-regexp}.
1654If @var{reverse} is a negative integer, then sorting is in reverse
1655order.
1656
1657Alphabetical sorting means that two sort keys are compared by
1658comparing the first characters of each, the second characters of each,
1659and so on. If a mismatch is found, it means that the sort keys are
1660unequal; the sort key whose character is less at the point of first
1661mismatch is the lesser sort key. The individual characters are compared
f9f59935 1662according to their numerical character codes in the Emacs character set.
73804d4b
RS
1663
1664The value of the @var{record-regexp} argument specifies how to divide
1665the buffer into sort records. At the end of each record, a search is
f9f59935
RS
1666done for this regular expression, and the text that matches it is taken
1667as the next record. For example, the regular expression @samp{^.+$},
1668which matches lines with at least one character besides a newline, would
1669make each such line into a sort record. @xref{Regular Expressions}, for
1670a description of the syntax and meaning of regular expressions.
73804d4b
RS
1671
1672The value of the @var{key-regexp} argument specifies what part of each
1673record is the sort key. The @var{key-regexp} could match the whole
1674record, or only a part. In the latter case, the rest of the record has
1675no effect on the sorted order of records, but it is carried along when
1676the record moves to its new position.
1677
1678The @var{key-regexp} argument can refer to the text matched by a
1679subexpression of @var{record-regexp}, or it can be a regular expression
1680on its own.
1681
1682If @var{key-regexp} is:
1683
1684@table @asis
1685@item @samp{\@var{digit}}
1686then the text matched by the @var{digit}th @samp{\(...\)} parenthesis
1687grouping in @var{record-regexp} is the sort key.
1688
1689@item @samp{\&}
1690then the whole record is the sort key.
1691
1692@item a regular expression
1693then @code{sort-regexp-fields} searches for a match for the regular
1694expression within the record. If such a match is found, it is the sort
1695key. If there is no match for @var{key-regexp} within a record then
1696that record is ignored, which means its position in the buffer is not
1697changed. (The other records may move around it.)
1698@end table
1699
1700For example, if you plan to sort all the lines in the region by the
1701first word on each line starting with the letter @samp{f}, you should
1702set @var{record-regexp} to @samp{^.*$} and set @var{key-regexp} to
1703@samp{\<f\w*\>}. The resulting expression looks like this:
1704
1705@example
1706@group
1707(sort-regexp-fields nil "^.*$" "\\<f\\w*\\>"
1708 (region-beginning)
1709 (region-end))
1710@end group
1711@end example
1712
1713If you call @code{sort-regexp-fields} interactively, it prompts for
1714@var{record-regexp} and @var{key-regexp} in the minibuffer.
1715@end deffn
1716
1717@deffn Command sort-lines reverse start end
1718This command alphabetically sorts lines in the region between
1719@var{start} and @var{end}. If @var{reverse} is non-@code{nil}, the sort
1720is in reverse order.
1721@end deffn
1722
1723@deffn Command sort-paragraphs reverse start end
1724This command alphabetically sorts paragraphs in the region between
1725@var{start} and @var{end}. If @var{reverse} is non-@code{nil}, the sort
1726is in reverse order.
1727@end deffn
1728
1729@deffn Command sort-pages reverse start end
1730This command alphabetically sorts pages in the region between
1731@var{start} and @var{end}. If @var{reverse} is non-@code{nil}, the sort
1732is in reverse order.
1733@end deffn
1734
1735@deffn Command sort-fields field start end
1736This command sorts lines in the region between @var{start} and
1737@var{end}, comparing them alphabetically by the @var{field}th field
1738of each line. Fields are separated by whitespace and numbered starting
1739from 1. If @var{field} is negative, sorting is by the
1740@w{@minus{}@var{field}th} field from the end of the line. This command
1741is useful for sorting tables.
1742@end deffn
1743
1744@deffn Command sort-numeric-fields field start end
1745This command sorts lines in the region between @var{start} and
1746@var{end}, comparing them numerically by the @var{field}th field of each
1747line. The specified field must contain a number in each line of the
1748region. Fields are separated by whitespace and numbered starting from
17491. If @var{field} is negative, sorting is by the
1750@w{@minus{}@var{field}th} field from the end of the line. This command
1751is useful for sorting tables.
1752@end deffn
1753
1754@deffn Command sort-columns reverse &optional beg end
1755This command sorts the lines in the region between @var{beg} and
1756@var{end}, comparing them alphabetically by a certain range of columns.
1757The column positions of @var{beg} and @var{end} bound the range of
1758columns to sort on.
1759
1760If @var{reverse} is non-@code{nil}, the sort is in reverse order.
1761
1762One unusual thing about this command is that the entire line
1763containing position @var{beg}, and the entire line containing position
1764@var{end}, are included in the region sorted.
1765
1766Note that @code{sort-columns} uses the @code{sort} utility program,
1767and so cannot work properly on text containing tab characters. Use
969fe9b5 1768@kbd{M-x untabify} to convert tabs to spaces before sorting.
73804d4b
RS
1769@end deffn
1770
1771@node Columns
1772@comment node-name, next, previous, up
1773@section Counting Columns
1774@cindex columns
1775@cindex counting columns
1776@cindex horizontal position
1777
1778 The column functions convert between a character position (counting
1779characters from the beginning of the buffer) and a column position
1780(counting screen characters from the beginning of a line).
1781
f9f59935
RS
1782 These functions count each character according to the number of
1783columns it occupies on the screen. This means control characters count
1784as occupying 2 or 4 columns, depending upon the value of
1785@code{ctl-arrow}, and tabs count as occupying a number of columns that
1786depends on the value of @code{tab-width} and on the column where the tab
1787begins. @xref{Usual Display}.
73804d4b
RS
1788
1789 Column number computations ignore the width of the window and the
1790amount of horizontal scrolling. Consequently, a column value can be
1791arbitrarily high. The first (or leftmost) column is numbered 0.
1792
1793@defun current-column
1794This function returns the horizontal position of point, measured in
1795columns, counting from 0 at the left margin. The column position is the
1796sum of the widths of all the displayed representations of the characters
1797between the start of the current line and point.
1798
1799For an example of using @code{current-column}, see the description of
1800@code{count-lines} in @ref{Text Lines}.
1801@end defun
1802
1803@defun move-to-column column &optional force
1804This function moves point to @var{column} in the current line. The
1805calculation of @var{column} takes into account the widths of the
1806displayed representations of the characters between the start of the
1807line and point.
1808
1809If column @var{column} is beyond the end of the line, point moves to the
1810end of the line. If @var{column} is negative, point moves to the
1811beginning of the line.
1812
1813If it is impossible to move to column @var{column} because that is in
1814the middle of a multicolumn character such as a tab, point moves to the
1815end of that character. However, if @var{force} is non-@code{nil}, and
1816@var{column} is in the middle of a tab, then @code{move-to-column}
1817converts the tab into spaces so that it can move precisely to column
1818@var{column}. Other multicolumn characters can cause anomalies despite
1819@var{force}, since there is no way to split them.
1820
1821The argument @var{force} also has an effect if the line isn't long
8241495d
RS
1822enough to reach column @var{column}; if it is @code{t}, that means to
1823add whitespace at the end of the line to reach that column.
73804d4b
RS
1824
1825If @var{column} is not an integer, an error is signaled.
1826
1827The return value is the column number actually moved to.
1828@end defun
1829
1830@node Indentation
1831@section Indentation
1832@cindex indentation
1833
1834 The indentation functions are used to examine, move to, and change
1835whitespace that is at the beginning of a line. Some of the functions
1836can also change whitespace elsewhere on a line. Columns and indentation
1837count from zero at the left margin.
1838
1839@menu
1840* Primitive Indent:: Functions used to count and insert indentation.
1841* Mode-Specific Indent:: Customize indentation for different modes.
1842* Region Indent:: Indent all the lines in a region.
1843* Relative Indent:: Indent the current line based on previous lines.
1844* Indent Tabs:: Adjustable, typewriter-like tab stops.
1845* Motion by Indent:: Move to first non-blank character.
1846@end menu
1847
1848@node Primitive Indent
1849@subsection Indentation Primitives
1850
1851 This section describes the primitive functions used to count and
1852insert indentation. The functions in the following sections use these
969fe9b5 1853primitives. @xref{Width}, for related functions.
73804d4b
RS
1854
1855@defun current-indentation
1856@comment !!Type Primitive Function
1857@comment !!SourceFile indent.c
1858This function returns the indentation of the current line, which is
1859the horizontal position of the first nonblank character. If the
1860contents are entirely blank, then this is the horizontal position of the
1861end of the line.
1862@end defun
1863
1864@deffn Command indent-to column &optional minimum
1865@comment !!Type Primitive Function
1866@comment !!SourceFile indent.c
61cfa852
RS
1867This function indents from point with tabs and spaces until @var{column}
1868is reached. If @var{minimum} is specified and non-@code{nil}, then at
1869least that many spaces are inserted even if this requires going beyond
1870@var{column}. Otherwise the function does nothing if point is already
1871beyond @var{column}. The value is the column at which the inserted
1872indentation ends.
33acbad2
RS
1873
1874The inserted whitespace characters inherit text properties from the
1875surrounding text (usually, from the preceding text only). @xref{Sticky
1876Properties}.
73804d4b
RS
1877@end deffn
1878
1879@defopt indent-tabs-mode
1880@comment !!SourceFile indent.c
1881If this variable is non-@code{nil}, indentation functions can insert
1882tabs as well as spaces. Otherwise, they insert only spaces. Setting
969fe9b5 1883this variable automatically makes it buffer-local in the current buffer.
73804d4b
RS
1884@end defopt
1885
1886@node Mode-Specific Indent
1887@subsection Indentation Controlled by Major Mode
1888
1889 An important function of each major mode is to customize the @key{TAB}
1890key to indent properly for the language being edited. This section
1891describes the mechanism of the @key{TAB} key and how to control it.
1892The functions in this section return unpredictable values.
1893
1894@defvar indent-line-function
1895This variable's value is the function to be used by @key{TAB} (and
1896various commands) to indent the current line. The command
1897@code{indent-according-to-mode} does no more than call this function.
1898
1899In Lisp mode, the value is the symbol @code{lisp-indent-line}; in C
1900mode, @code{c-indent-line}; in Fortran mode, @code{fortran-indent-line}.
1901In Fundamental mode, Text mode, and many other modes with no standard
1902for indentation, the value is @code{indent-to-left-margin} (which is the
1903default value).
1904@end defvar
1905
1906@deffn Command indent-according-to-mode
1907This command calls the function in @code{indent-line-function} to
1908indent the current line in a way appropriate for the current major mode.
1909@end deffn
1910
1911@deffn Command indent-for-tab-command
1912This command calls the function in @code{indent-line-function} to indent
a9f0a989
RS
1913the current line; however, if that function is
1914@code{indent-to-left-margin}, @code{insert-tab} is called instead. (That
61cfa852 1915is a trivial command that inserts a tab character.)
73804d4b
RS
1916@end deffn
1917
73804d4b
RS
1918@deffn Command newline-and-indent
1919@comment !!SourceFile simple.el
1920This function inserts a newline, then indents the new line (the one
1921following the newline just inserted) according to the major mode.
1922
1923It does indentation by calling the current @code{indent-line-function}.
1924In programming language modes, this is the same thing @key{TAB} does,
1925but in some text modes, where @key{TAB} inserts a tab,
1926@code{newline-and-indent} indents to the column specified by
1927@code{left-margin}.
1928@end deffn
1929
1930@deffn Command reindent-then-newline-and-indent
1931@comment !!SourceFile simple.el
1932This command reindents the current line, inserts a newline at point,
1911e6e5 1933and then indents the new line (the one following the newline just
73804d4b
RS
1934inserted).
1935
1936This command does indentation on both lines according to the current
1937major mode, by calling the current value of @code{indent-line-function}.
1938In programming language modes, this is the same thing @key{TAB} does,
1939but in some text modes, where @key{TAB} inserts a tab,
1940@code{reindent-then-newline-and-indent} indents to the column specified
1941by @code{left-margin}.
1942@end deffn
1943
1944@node Region Indent
1945@subsection Indenting an Entire Region
1946
61cfa852 1947 This section describes commands that indent all the lines in the
73804d4b
RS
1948region. They return unpredictable values.
1949
1950@deffn Command indent-region start end to-column
1951This command indents each nonblank line starting between @var{start}
1952(inclusive) and @var{end} (exclusive). If @var{to-column} is
1953@code{nil}, @code{indent-region} indents each nonblank line by calling
1954the current mode's indentation function, the value of
1955@code{indent-line-function}.
1956
1957If @var{to-column} is non-@code{nil}, it should be an integer
1958specifying the number of columns of indentation; then this function
1959gives each line exactly that much indentation, by either adding or
1960deleting whitespace.
1961
1962If there is a fill prefix, @code{indent-region} indents each line
1963by making it start with the fill prefix.
1964@end deffn
1965
1966@defvar indent-region-function
1967The value of this variable is a function that can be used by
a9f0a989
RS
1968@code{indent-region} as a short cut. It should take two arguments, the
1969start and end of the region. You should design the function so
73804d4b
RS
1970that it will produce the same results as indenting the lines of the
1971region one by one, but presumably faster.
1972
1973If the value is @code{nil}, there is no short cut, and
1974@code{indent-region} actually works line by line.
1975
61cfa852 1976A short-cut function is useful in modes such as C mode and Lisp mode,
73804d4b 1977where the @code{indent-line-function} must scan from the beginning of
61cfa852
RS
1978the function definition: applying it to each line would be quadratic in
1979time. The short cut can update the scan information as it moves through
1980the lines indenting them; this takes linear time. In a mode where
1981indenting a line individually is fast, there is no need for a short cut.
73804d4b 1982
61cfa852
RS
1983@code{indent-region} with a non-@code{nil} argument @var{to-column} has
1984a different meaning and does not use this variable.
73804d4b
RS
1985@end defvar
1986
1987@deffn Command indent-rigidly start end count
1988@comment !!SourceFile indent.el
1989This command indents all lines starting between @var{start}
1990(inclusive) and @var{end} (exclusive) sideways by @var{count} columns.
1991This ``preserves the shape'' of the affected region, moving it as a
1992rigid unit. Consequently, this command is useful not only for indenting
1993regions of unindented text, but also for indenting regions of formatted
1994code.
1995
1996For example, if @var{count} is 3, this command adds 3 columns of
1997indentation to each of the lines beginning in the region specified.
1998
1999In Mail mode, @kbd{C-c C-y} (@code{mail-yank-original}) uses
2000@code{indent-rigidly} to indent the text copied from the message being
2001replied to.
2002@end deffn
2003
2004@defun indent-code-rigidly start end columns &optional nochange-regexp
2005This is like @code{indent-rigidly}, except that it doesn't alter lines
2006that start within strings or comments.
2007
2008In addition, it doesn't alter a line if @var{nochange-regexp} matches at
2009the beginning of the line (if @var{nochange-regexp} is non-@code{nil}).
2010@end defun
2011
2012@node Relative Indent
2013@subsection Indentation Relative to Previous Lines
2014
61cfa852 2015 This section describes two commands that indent the current line
73804d4b
RS
2016based on the contents of previous lines.
2017
2018@deffn Command indent-relative &optional unindented-ok
2019This command inserts whitespace at point, extending to the same
2020column as the next @dfn{indent point} of the previous nonblank line. An
2021indent point is a non-whitespace character following whitespace. The
2022next indent point is the first one at a column greater than the current
2023column of point. For example, if point is underneath and to the left of
2024the first non-blank character of a line of text, it moves to that column
2025by inserting whitespace.
2026
2027If the previous nonblank line has no next indent point (i.e., none at a
2028great enough column position), @code{indent-relative} either does
2029nothing (if @var{unindented-ok} is non-@code{nil}) or calls
2030@code{tab-to-tab-stop}. Thus, if point is underneath and to the right
2031of the last column of a short line of text, this command ordinarily
2032moves point to the next tab stop by inserting whitespace.
2033
2034The return value of @code{indent-relative} is unpredictable.
2035
2036In the following example, point is at the beginning of the second
2037line:
2038
2039@example
2040@group
2041 This line is indented twelve spaces.
2042@point{}The quick brown fox jumped.
2043@end group
2044@end example
2045
2046@noindent
2047Evaluation of the expression @code{(indent-relative nil)} produces the
2048following:
2049
2050@example
2051@group
2052 This line is indented twelve spaces.
2053 @point{}The quick brown fox jumped.
2054@end group
2055@end example
2056
f9f59935 2057 In this next example, point is between the @samp{m} and @samp{p} of
73804d4b
RS
2058@samp{jumped}:
2059
2060@example
2061@group
2062 This line is indented twelve spaces.
2063The quick brown fox jum@point{}ped.
2064@end group
2065@end example
2066
2067@noindent
2068Evaluation of the expression @code{(indent-relative nil)} produces the
2069following:
2070
2071@example
2072@group
2073 This line is indented twelve spaces.
2074The quick brown fox jum @point{}ped.
2075@end group
2076@end example
2077@end deffn
2078
2079@deffn Command indent-relative-maybe
2080@comment !!SourceFile indent.el
f9f59935
RS
2081This command indents the current line like the previous nonblank line,
2082by calling @code{indent-relative} with @code{t} as the
2083@var{unindented-ok} argument. The return value is unpredictable.
73804d4b
RS
2084
2085If the previous nonblank line has no indent points beyond the current
2086column, this command does nothing.
2087@end deffn
2088
2089@node Indent Tabs
2090@comment node-name, next, previous, up
2091@subsection Adjustable ``Tab Stops''
2092@cindex tabs stops for indentation
2093
2094 This section explains the mechanism for user-specified ``tab stops''
61cfa852 2095and the mechanisms that use and set them. The name ``tab stops'' is
73804d4b
RS
2096used because the feature is similar to that of the tab stops on a
2097typewriter. The feature works by inserting an appropriate number of
2098spaces and tab characters to reach the next tab stop column; it does not
2099affect the display of tab characters in the buffer (@pxref{Usual
2100Display}). Note that the @key{TAB} character as input uses this tab
2101stop feature only in a few major modes, such as Text mode.
2102
2103@deffn Command tab-to-tab-stop
f9f59935
RS
2104This command inserts spaces or tabs before point, up to the next tab
2105stop column defined by @code{tab-stop-list}. It searches the list for
2106an element greater than the current column number, and uses that element
2107as the column to indent to. It does nothing if no such element is
2108found.
73804d4b
RS
2109@end deffn
2110
2111@defopt tab-stop-list
2112This variable is the list of tab stop columns used by
2113@code{tab-to-tab-stops}. The elements should be integers in increasing
2114order. The tab stop columns need not be evenly spaced.
2115
2116Use @kbd{M-x edit-tab-stops} to edit the location of tab stops
2117interactively.
2118@end defopt
2119
2120@node Motion by Indent
2121@subsection Indentation-Based Motion Commands
2122
2123 These commands, primarily for interactive use, act based on the
2124indentation in the text.
2125
2126@deffn Command back-to-indentation
2127@comment !!SourceFile simple.el
2128This command moves point to the first non-whitespace character in the
2129current line (which is the line in which point is located). It returns
2130@code{nil}.
2131@end deffn
2132
2133@deffn Command backward-to-indentation arg
2134@comment !!SourceFile simple.el
2135This command moves point backward @var{arg} lines and then to the
2136first nonblank character on that line. It returns @code{nil}.
2137@end deffn
2138
2139@deffn Command forward-to-indentation arg
2140@comment !!SourceFile simple.el
2141This command moves point forward @var{arg} lines and then to the first
2142nonblank character on that line. It returns @code{nil}.
2143@end deffn
2144
2145@node Case Changes
2146@comment node-name, next, previous, up
2147@section Case Changes
969fe9b5 2148@cindex case conversion in buffers
73804d4b
RS
2149
2150 The case change commands described here work on text in the current
969fe9b5
RS
2151buffer. @xref{Case Conversion}, for case conversion functions that work
2152on strings and characters. @xref{Case Tables}, for how to customize
73804d4b
RS
2153which characters are upper or lower case and how to convert them.
2154
2155@deffn Command capitalize-region start end
2156This function capitalizes all words in the region defined by
2157@var{start} and @var{end}. To capitalize means to convert each word's
2158first character to upper case and convert the rest of each word to lower
2159case. The function returns @code{nil}.
2160
2161If one end of the region is in the middle of a word, the part of the
2162word within the region is treated as an entire word.
2163
2164When @code{capitalize-region} is called interactively, @var{start} and
2165@var{end} are point and the mark, with the smallest first.
2166
2167@example
2168@group
2169---------- Buffer: foo ----------
2170This is the contents of the 5th foo.
2171---------- Buffer: foo ----------
2172@end group
2173
2174@group
2175(capitalize-region 1 44)
2176@result{} nil
2177
2178---------- Buffer: foo ----------
2179This Is The Contents Of The 5th Foo.
2180---------- Buffer: foo ----------
2181@end group
2182@end example
2183@end deffn
2184
2185@deffn Command downcase-region start end
2186This function converts all of the letters in the region defined by
2187@var{start} and @var{end} to lower case. The function returns
2188@code{nil}.
2189
2190When @code{downcase-region} is called interactively, @var{start} and
2191@var{end} are point and the mark, with the smallest first.
2192@end deffn
2193
2194@deffn Command upcase-region start end
2195This function converts all of the letters in the region defined by
2196@var{start} and @var{end} to upper case. The function returns
2197@code{nil}.
2198
2199When @code{upcase-region} is called interactively, @var{start} and
2200@var{end} are point and the mark, with the smallest first.
2201@end deffn
2202
2203@deffn Command capitalize-word count
2204This function capitalizes @var{count} words after point, moving point
2205over as it does. To capitalize means to convert each word's first
2206character to upper case and convert the rest of each word to lower case.
2207If @var{count} is negative, the function capitalizes the
2208@minus{}@var{count} previous words but does not move point. The value
2209is @code{nil}.
2210
61cfa852
RS
2211If point is in the middle of a word, the part of the word before point
2212is ignored when moving forward. The rest is treated as an entire word.
73804d4b
RS
2213
2214When @code{capitalize-word} is called interactively, @var{count} is
2215set to the numeric prefix argument.
2216@end deffn
2217
2218@deffn Command downcase-word count
2219This function converts the @var{count} words after point to all lower
2220case, moving point over as it does. If @var{count} is negative, it
2221converts the @minus{}@var{count} previous words but does not move point.
2222The value is @code{nil}.
2223
2224When @code{downcase-word} is called interactively, @var{count} is set
2225to the numeric prefix argument.
2226@end deffn
2227
2228@deffn Command upcase-word count
2229This function converts the @var{count} words after point to all upper
2230case, moving point over as it does. If @var{count} is negative, it
2231converts the @minus{}@var{count} previous words but does not move point.
2232The value is @code{nil}.
2233
2234When @code{upcase-word} is called interactively, @var{count} is set to
2235the numeric prefix argument.
2236@end deffn
2237
2238@node Text Properties
2239@section Text Properties
2240@cindex text properties
2241@cindex attributes of text
2242@cindex properties of text
2243
2244 Each character position in a buffer or a string can have a @dfn{text
2245property list}, much like the property list of a symbol (@pxref{Property
2246Lists}). The properties belong to a particular character at a
2247particular place, such as, the letter @samp{T} at the beginning of this
2248sentence or the first @samp{o} in @samp{foo}---if the same character
2249occurs in two different places, the two occurrences generally have
2250different properties.
2251
2252 Each property has a name and a value. Both of these can be any Lisp
2253object, but the name is normally a symbol. The usual way to access the
2254property list is to specify a name and ask what value corresponds to it.
2255
2256 If a character has a @code{category} property, we call it the
2257@dfn{category} of the character. It should be a symbol. The properties
2258of the symbol serve as defaults for the properties of the character.
2259
2260 Copying text between strings and buffers preserves the properties
2261along with the characters; this includes such diverse functions as
2262@code{substring}, @code{insert}, and @code{buffer-substring}.
2263
2264@menu
a6b73788
RS
2265* Examining Properties:: Looking at the properties of one character.
2266* Changing Properties:: Setting the properties of a range of text.
2267* Property Search:: Searching for where a property changes value.
2268* Special Properties:: Particular properties with special meanings.
2269* Format Properties:: Properties for representing formatting of text.
2270* Sticky Properties:: How inserted text gets properties from
2271 neighboring text.
2272* Saving Properties:: Saving text properties in files, and reading
2273 them back.
2274* Lazy Properties:: Computing text properties in a lazy fashion
2275 only when text is examined.
2276* Clickable Text:: Using text properties to make regions of text
2277 do something when you click on them.
2278* Fields:: The @code{field} property defines
2279 fields within the buffer.
2280* Not Intervals:: Why text properties do not use
2281 Lisp-visible text intervals.
73804d4b
RS
2282@end menu
2283
2284@node Examining Properties
2285@subsection Examining Text Properties
2286
2287 The simplest way to examine text properties is to ask for the value of
2288a particular property of a particular character. For that, use
2289@code{get-text-property}. Use @code{text-properties-at} to get the
2290entire property list of a character. @xref{Property Search}, for
2291functions to examine the properties of a number of characters at once.
2292
2293 These functions handle both strings and buffers. Keep in mind that
2294positions in a string start from 0, whereas positions in a buffer start
2295from 1.
2296
2297@defun get-text-property pos prop &optional object
2298This function returns the value of the @var{prop} property of the
2299character after position @var{pos} in @var{object} (a buffer or
2300string). The argument @var{object} is optional and defaults to the
2301current buffer.
2302
2303If there is no @var{prop} property strictly speaking, but the character
61cfa852 2304has a category that is a symbol, then @code{get-text-property} returns
73804d4b
RS
2305the @var{prop} property of that symbol.
2306@end defun
2307
2308@defun get-char-property pos prop &optional object
2309This function is like @code{get-text-property}, except that it checks
2310overlays first and then text properties. @xref{Overlays}.
2311
2312The argument @var{object} may be a string, a buffer, or a window. If it
2313is a window, then the buffer displayed in that window is used for text
2314properties and overlays, but only the overlays active for that window
2315are considered. If @var{object} is a buffer, then all overlays in that
2316buffer are considered, as well as text properties. If @var{object} is a
2317string, only text properties are considered, since strings never have
2318overlays.
2319@end defun
2320
2321@defun text-properties-at position &optional object
2322This function returns the entire property list of the character at
2323@var{position} in the string or buffer @var{object}. If @var{object} is
2324@code{nil}, it defaults to the current buffer.
2325@end defun
2326
22697dac
KH
2327@defvar default-text-properties
2328This variable holds a property list giving default values for text
2329properties. Whenever a character does not specify a value for a
bfe721d1
KH
2330property, neither directly nor through a category symbol, the value
2331stored in this list is used instead. Here is an example:
22697dac
KH
2332
2333@example
2334(setq default-text-properties '(foo 69))
2335;; @r{Make sure character 1 has no properties of its own.}
2336(set-text-properties 1 2 nil)
2337;; @r{What we get, when we ask, is the default value.}
2338(get-text-property 1 'foo)
2339 @result{} 69
2340@end example
2341@end defvar
2342
73804d4b
RS
2343@node Changing Properties
2344@subsection Changing Text Properties
2345
2346 The primitives for changing properties apply to a specified range of
e78ea3db
RS
2347text in a buffer or string. The function @code{set-text-properties}
2348(see end of section) sets the entire property list of the text in that
2349range; more often, it is useful to add, change, or delete just certain
2350properties specified by name.
73804d4b 2351
e78ea3db
RS
2352 Since text properties are considered part of the contents of the
2353buffer (or string), and can affect how a buffer looks on the screen, any
2468d0c0 2354change in buffer text properties marks the buffer as modified. Buffer
e78ea3db 2355text property changes are undoable also (@pxref{Undo}).
73804d4b 2356
bfe721d1
KH
2357@defun put-text-property start end prop value &optional object
2358This function sets the @var{prop} property to @var{value} for the text
2359between @var{start} and @var{end} in the string or buffer @var{object}.
2360If @var{object} is @code{nil}, it defaults to the current buffer.
2361@end defun
2362
73804d4b 2363@defun add-text-properties start end props &optional object
f9f59935 2364This function adds or overrides text properties for the text between
73804d4b
RS
2365@var{start} and @var{end} in the string or buffer @var{object}. If
2366@var{object} is @code{nil}, it defaults to the current buffer.
2367
f9f59935
RS
2368The argument @var{props} specifies which properties to add. It should
2369have the form of a property list (@pxref{Property Lists}): a list whose
2370elements include the property names followed alternately by the
73804d4b
RS
2371corresponding values.
2372
2373The return value is @code{t} if the function actually changed some
2374property's value; @code{nil} otherwise (if @var{props} is @code{nil} or
2375its values agree with those in the text).
2376
2377For example, here is how to set the @code{comment} and @code{face}
2378properties of a range of text:
2379
2380@example
2381(add-text-properties @var{start} @var{end}
2382 '(comment t face highlight))
2383@end example
2384@end defun
2385
73804d4b
RS
2386@defun remove-text-properties start end props &optional object
2387This function deletes specified text properties from the text between
2388@var{start} and @var{end} in the string or buffer @var{object}. If
2389@var{object} is @code{nil}, it defaults to the current buffer.
2390
2391The argument @var{props} specifies which properties to delete. It
2392should have the form of a property list (@pxref{Property Lists}): a list
2393whose elements are property names alternating with corresponding values.
2394But only the names matter---the values that accompany them are ignored.
2395For example, here's how to remove the @code{face} property.
2396
2397@example
2398(remove-text-properties @var{start} @var{end} '(face nil))
2399@end example
2400
2401The return value is @code{t} if the function actually changed some
2402property's value; @code{nil} otherwise (if @var{props} is @code{nil} or
2403if no character in the specified text had any of those properties).
e78ea3db
RS
2404
2405To remove all text properties from certain text, use
2406@code{set-text-properties} and specify @code{nil} for the new property
2407list.
73804d4b
RS
2408@end defun
2409
2410@defun set-text-properties start end props &optional object
2411This function completely replaces the text property list for the text
2412between @var{start} and @var{end} in the string or buffer @var{object}.
2413If @var{object} is @code{nil}, it defaults to the current buffer.
2414
2415The argument @var{props} is the new property list. It should be a list
2416whose elements are property names alternating with corresponding values.
2417
2418After @code{set-text-properties} returns, all the characters in the
2419specified range have identical properties.
2420
2421If @var{props} is @code{nil}, the effect is to get rid of all properties
2422from the specified range of text. Here's an example:
2423
2424@example
2425(set-text-properties @var{start} @var{end} nil)
2426@end example
a40d4712
PR
2427@end defun
2428
2429 The easiest way to make a string with text properties
2430is with @code{propertize}:
2431
2432@defun propertize string &rest properties
2433@tindex propertize
2434This function returns a copy of @var{string} which has the text
2435properties @var{properties}. These properties apply to all the
2436characters in the string that is returned. Here is an example that
2437constructs a string with a @code{face} property and a @code{mouse-face}
2438property:
2439
2440@smallexample
2441(propertize "foo" 'face 'italic
2442 'mouse-face 'bold-italic)
2443 @result{} #("foo" 0 3 (mouse-face bold-italic face italic))
2444@end smallexample
2445
2446To put different properties on various parts of a string, you can
2447construct each part with @code{propertize} and then combine them with
2448@code{concat}:
2449
2450@smallexample
2451(concat
2452 (propertize "foo" 'face 'italic
2453 'mouse-face 'bold-italic)
2454 " and "
2455 (propertize "bar" 'face 'italic
2456 'mouse-face 'bold-italic))
2457 @result{} #("foo and bar"
2458 0 3 (face italic mouse-face bold-italic)
2459 3 8 nil
2460 8 11 (face italic mouse-face bold-italic))
2461@end smallexample
73804d4b
RS
2462@end defun
2463
f9f59935 2464 See also the function @code{buffer-substring-no-properties}
22697dac
KH
2465(@pxref{Buffer Contents}) which copies text from the buffer
2466but does not copy its properties.
2467
73804d4b 2468@node Property Search
f9f59935 2469@subsection Text Property Search Functions
73804d4b 2470
f9f59935 2471 In typical use of text properties, most of the time several or many
73804d4b
RS
2472consecutive characters have the same value for a property. Rather than
2473writing your programs to examine characters one by one, it is much
2474faster to process chunks of text that have the same property value.
2475
f9f59935 2476 Here are functions you can use to do this. They use @code{eq} for
bfe721d1
KH
2477comparing property values. In all cases, @var{object} defaults to the
2478current buffer.
73804d4b 2479
f9f59935 2480 For high performance, it's very important to use the @var{limit}
73804d4b 2481argument to these functions, especially the ones that search for a
bfe721d1
KH
2482single property---otherwise, they may spend a long time scanning to the
2483end of the buffer, if the property you are interested in does not change.
73804d4b 2484
f9f59935
RS
2485 These functions do not move point; instead, they return a position (or
2486@code{nil}). Remember that a position is always between two characters;
2487the position returned by these functions is between two characters with
2488different properties.
61cfa852 2489
73804d4b
RS
2490@defun next-property-change pos &optional object limit
2491The function scans the text forward from position @var{pos} in the
2492string or buffer @var{object} till it finds a change in some text
2493property, then returns the position of the change. In other words, it
2494returns the position of the first character beyond @var{pos} whose
2495properties are not identical to those of the character just after
2496@var{pos}.
2497
2498If @var{limit} is non-@code{nil}, then the scan ends at position
2499@var{limit}. If there is no property change before that point,
2500@code{next-property-change} returns @var{limit}.
2501
2502The value is @code{nil} if the properties remain unchanged all the way
61cfa852
RS
2503to the end of @var{object} and @var{limit} is @code{nil}. If the value
2504is non-@code{nil}, it is a position greater than or equal to @var{pos}.
2505The value equals @var{pos} only when @var{limit} equals @var{pos}.
73804d4b
RS
2506
2507Here is an example of how to scan the buffer by chunks of text within
2508which all properties are constant:
2509
2510@smallexample
2511(while (not (eobp))
2512 (let ((plist (text-properties-at (point)))
2513 (next-change
2514 (or (next-property-change (point) (current-buffer))
2515 (point-max))))
2516 @r{Process text from point to @var{next-change}@dots{}}
2517 (goto-char next-change)))
2518@end smallexample
2519@end defun
2520
2521@defun next-single-property-change pos prop &optional object limit
2522The function scans the text forward from position @var{pos} in the
2523string or buffer @var{object} till it finds a change in the @var{prop}
2524property, then returns the position of the change. In other words, it
2525returns the position of the first character beyond @var{pos} whose
2526@var{prop} property differs from that of the character just after
2527@var{pos}.
2528
2529If @var{limit} is non-@code{nil}, then the scan ends at position
2530@var{limit}. If there is no property change before that point,
2531@code{next-single-property-change} returns @var{limit}.
2532
2533The value is @code{nil} if the property remains unchanged all the way to
2534the end of @var{object} and @var{limit} is @code{nil}. If the value is
2535non-@code{nil}, it is a position greater than or equal to @var{pos}; it
2536equals @var{pos} only if @var{limit} equals @var{pos}.
2537@end defun
2538
2539@defun previous-property-change pos &optional object limit
2540This is like @code{next-property-change}, but scans back from @var{pos}
2541instead of forward. If the value is non-@code{nil}, it is a position
2542less than or equal to @var{pos}; it equals @var{pos} only if @var{limit}
2543equals @var{pos}.
73804d4b
RS
2544@end defun
2545
2546@defun previous-single-property-change pos prop &optional object limit
61cfa852
RS
2547This is like @code{next-single-property-change}, but scans back from
2548@var{pos} instead of forward. If the value is non-@code{nil}, it is a
2549position less than or equal to @var{pos}; it equals @var{pos} only if
2550@var{limit} equals @var{pos}.
73804d4b
RS
2551@end defun
2552
9db3a582 2553@defun next-char-property-change pos &optional limit
f9f59935 2554This is like @code{next-property-change} except that it considers
9db3a582
MB
2555overlay properties as well as text properties, and if no change is
2556found before the end of the buffer, it returns the maximum buffer
2557position rather than @code{nil} (in this sense, it resembles the
2558corresponding overlay function @code{next-overlay-change}, rather than
2559@code{next-property-change}). There is no @var{object} operand
2560because this function operates only on the current buffer. It returns
2561the next address at which either kind of property changes.
f9f59935
RS
2562@end defun
2563
9db3a582 2564@defun previous-char-property-change pos &optional limit
f9f59935 2565This is like @code{next-char-property-change}, but scans back from
9db3a582
MB
2566@var{pos} instead of forward, and returns the minimum buffer
2567position if no change is found.
2568@end defun
2569
2570@defun next-single-char-property-change pos prop &optional object limit
adce7d83 2571@tindex next-single-char-property-change
9db3a582
MB
2572This is like @code{next-single-property-change} except that it
2573considers overlay properties as well as text properties, and if no
2574change is found before the end of the @var{object}, it returns the
2575maximum valid position in @var{object} rather than @code{nil}. Unlike
2576@code{next-char-property-change}, this function @emph{does} have an
2577@var{object} operand; if @var{object} is not a buffer, only
2578text-properties are considered.
2579@end defun
2580
2581@defun previous-single-char-property-change pos prop &optional object limit
adce7d83 2582@tindex previous-single-char-property-change
9db3a582
MB
2583This is like @code{next-single-char-property-change}, but scans back
2584from @var{pos} instead of forward, and returns the minimum valid
2585position in @var{object} if no change is found.
f9f59935
RS
2586@end defun
2587
73804d4b
RS
2588@defun text-property-any start end prop value &optional object
2589This function returns non-@code{nil} if at least one character between
2590@var{start} and @var{end} has a property @var{prop} whose value is
2591@var{value}. More precisely, it returns the position of the first such
2592character. Otherwise, it returns @code{nil}.
2593
2594The optional fifth argument, @var{object}, specifies the string or
2595buffer to scan. Positions are relative to @var{object}. The default
2596for @var{object} is the current buffer.
2597@end defun
2598
2599@defun text-property-not-all start end prop value &optional object
2600This function returns non-@code{nil} if at least one character between
f9f59935
RS
2601@var{start} and @var{end} does not have a property @var{prop} with value
2602@var{value}. More precisely, it returns the position of the first such
2603character. Otherwise, it returns @code{nil}.
73804d4b
RS
2604
2605The optional fifth argument, @var{object}, specifies the string or
2606buffer to scan. Positions are relative to @var{object}. The default
2607for @var{object} is the current buffer.
2608@end defun
2609
2610@node Special Properties
2611@subsection Properties with Special Meanings
2612
bfe721d1 2613 Here is a table of text property names that have special built-in
969fe9b5
RS
2614meanings. The following sections list a few additional special property
2615names that control filling and property inheritance. All other names
2616have no standard meaning, and you can use them as you like.
bfe721d1 2617
73804d4b
RS
2618@table @code
2619@cindex category of text character
2620@kindex category @r{(text property)}
2621@item category
2622If a character has a @code{category} property, we call it the
2623@dfn{category} of the character. It should be a symbol. The properties
2624of the symbol serve as defaults for the properties of the character.
2625
2626@item face
2627@cindex face codes of text
2628@kindex face @r{(text property)}
2629You can use the property @code{face} to control the font and color of
8241495d
RS
2630text. @xref{Faces}, for more information.
2631
2632In the simplest case, the value is a face name. It can also be a list;
2633then each element can be any of these possibilities;
2634
2635@itemize @bullet
2636@item
2637A face name (a symbol or string).
f9f59935 2638
8241495d
RS
2639@item
2640Starting in Emacs 21, a property list of face attributes. This has the
2641form (@var{keyword} @var{value} @dots{}), where each @var{keyword} is a
2642face attribute name and @var{value} is a meaningful value for that
2643attribute. With this feature, you do not need to create a face each
2644time you want to specify a particular attribute for certain text.
2645@xref{Face Attributes}.
2646
2647@item
2648A cons cell of the form @code{(foreground-color . @var{color-name})} or
2649@code{(background-color . @var{color-name})}. These elements specify
2650just the foreground color or just the background color.
2651
2652@code{(foreground-color . @var{color-name})} is equivalent to
2653@code{(:foreground @var{color-name})}, and likewise for the background.
2654@end itemize
f9f59935
RS
2655
2656@xref{Font Lock Mode}, for information on how to update @code{face}
2657properties automatically based on the contents of the text.
73804d4b
RS
2658
2659@item mouse-face
2660@kindex mouse-face @r{(text property)}
2661The property @code{mouse-face} is used instead of @code{face} when the
2662mouse is on or near the character. For this purpose, ``near'' means
2663that all text between the character and where the mouse is have the same
2664@code{mouse-face} property value.
2665
8241495d
RS
2666@item fontified
2667@kindex fontified @r{(text property)}
2668This property, if non-@code{nil}, says that text in the buffer has
2669had faces assigned automatically by a feature such as Font-Lock mode.
2670@xref{Auto Faces}.
2671
2672@item display
2673@kindex display @r{(text property)}
2674This property activates various features that change the
2675way text is displayed. For example, it can make text appear taller
75708135 2676or shorter, higher or lower, wider or narrow, or replaced with an image.
8241495d
RS
2677@xref{Display Property}.
2678
2679@item help-echo
2680@kindex help-echo @r{(text property)}
b6dd5963 2681@anchor{Text help-echo}
8241495d
RS
2682If text has a string as its @code{help-echo} property, then when you
2683move the mouse onto that text, Emacs displays that string in the echo
b6dd5963
DL
2684area, or in the tooltip window.
2685
05cae5d0 2686If the value of the @code{help-echo} property is a function, that
b6dd5963 2687function is called with three arguments, @var{window}, @var{object} and
05cae5d0 2688@var{position} and should return a help string or @var{nil} for
b6dd5963
DL
2689none. The first argument, @var{window} is the window in which
2690the help was found. The second, @var{object}, is the buffer, overlay or
2691string which had the @code{help-echo} property. The @var{position}
2692argument is as follows:
2693
2694@itemize @bullet{}
2695@item
2696If @var{object} is a buffer, @var{pos} is the position in the buffer
2697where the @code{help-echo} text property was found.
2698@item
2699If @var{object} is an overlay, that overlay has a @code{help-echo}
2700property, and @var{pos} is the position in the overlay's buffer under
2701the mouse.
2702@item
2703If @var{object} is a string (an overlay string or a string displayed
05cae5d0 2704with the @code{display} property), @var{pos} is the position in that
b6dd5963
DL
2705string under the mouse.
2706@end itemize
2707
2708If the value of the @code{help-echo} property is neither a function nor
2709a string, it is evaluated to obtain a help string.
2710
05cae5d0
DL
2711You can alter the way help text is displayed by setting the variable
2712@code{show-help-function} (@pxref{Help display}).
b6dd5963
DL
2713
2714This feature is used in the mode line and for other active text. It is
2715available starting in Emacs 21.
8241495d 2716
ce75fd23 2717@item local-map
73804d4b 2718@cindex keymap of character
ce75fd23 2719@kindex local-map @r{(text property)}
f9f59935 2720You can specify a different keymap for some of the text in a buffer by
ce75fd23 2721means of the @code{local-map} property. The property's value for the
f9f59935
RS
2722character after point, if non-@code{nil}, is used for key lookup instead
2723of the buffer's local map. If the property value is a symbol, the
2724symbol's function definition is used as the keymap. @xref{Active
2725Keymaps}.
2726
0b3da4ec
DL
2727@item keymap
2728@kindex keymap @r{(text property)}
2729The @code{keymap} property is similar to @code{local-map} but overrides the
2730buffer's local map (and the map specified by the @code{local-map}
2731property) rather than replacing it.
2732
f9f59935
RS
2733@item syntax-table
2734The @code{syntax-table} property overrides what the syntax table says
2735about this particular character. @xref{Syntax Properties}.
73804d4b
RS
2736
2737@item read-only
2738@cindex read-only character
2739@kindex read-only @r{(text property)}
2740If a character has the property @code{read-only}, then modifying that
2468d0c0
DL
2741character is not allowed. Any command that would do so gets an error,
2742@code{text-read-only}.
73804d4b
RS
2743
2744Insertion next to a read-only character is an error if inserting
2745ordinary text there would inherit the @code{read-only} property due to
2746stickiness. Thus, you can control permission to insert next to
2747read-only text by controlling the stickiness. @xref{Sticky Properties}.
2748
2749Since changing properties counts as modifying the buffer, it is not
2750possible to remove a @code{read-only} property unless you know the
2751special trick: bind @code{inhibit-read-only} to a non-@code{nil} value
2752and then remove the property. @xref{Read Only Buffers}.
2753
2754@item invisible
2755@kindex invisible @r{(text property)}
22697dac
KH
2756A non-@code{nil} @code{invisible} property can make a character invisible
2757on the screen. @xref{Invisible Text}, for details.
73804d4b 2758
72654a3c
RS
2759@item intangible
2760@kindex intangible @r{(text property)}
22697dac
KH
2761If a group of consecutive characters have equal and non-@code{nil}
2762@code{intangible} properties, then you cannot place point between them.
bfe721d1
KH
2763If you try to move point forward into the group, point actually moves to
2764the end of the group. If you try to move point backward into the group,
22697dac
KH
2765point actually moves to the start of the group.
2766
2767When the variable @code{inhibit-point-motion-hooks} is non-@code{nil},
2768the @code{intangible} property is ignored.
72654a3c 2769
2468d0c0
DL
2770@item field
2771@kindex field @r{(text property)}
2772Consecutive characters with the same @code{field} property constitute a
2773@dfn{field}. Some motion functions including @code{forward-word} and
2774@code{beginning-of-line} stop moving at a field boundary.
2775@xref{Fields}.
2776
73804d4b
RS
2777@item modification-hooks
2778@cindex change hooks for a character
2779@cindex hooks for changing a character
2780@kindex modification-hooks @r{(text property)}
2781If a character has the property @code{modification-hooks}, then its
2782value should be a list of functions; modifying that character calls all
2783of those functions. Each function receives two arguments: the beginning
2784and end of the part of the buffer being modified. Note that if a
2785particular modification hook function appears on several characters
2786being modified by a single primitive, you can't predict how many times
2787the function will be called.
2788
2789@item insert-in-front-hooks
2790@itemx insert-behind-hooks
2791@kindex insert-in-front-hooks @r{(text property)}
2792@kindex insert-behind-hooks @r{(text property)}
9ea65df9
RS
2793The operation of inserting text in a buffer also calls the functions
2794listed in the @code{insert-in-front-hooks} property of the following
2795character and in the @code{insert-behind-hooks} property of the
2796preceding character. These functions receive two arguments, the
2797beginning and end of the inserted text. The functions are called
2798@emph{after} the actual insertion takes place.
73804d4b
RS
2799
2800See also @ref{Change Hooks}, for other hooks that are called
2801when you change text in a buffer.
2802
2803@item point-entered
2804@itemx point-left
2805@cindex hooks for motion of point
2806@kindex point-entered @r{(text property)}
2807@kindex point-left @r{(text property)}
2808The special properties @code{point-entered} and @code{point-left}
2809record hook functions that report motion of point. Each time point
2810moves, Emacs compares these two property values:
2811
2812@itemize @bullet
2813@item
2814the @code{point-left} property of the character after the old location,
2815and
2816@item
2817the @code{point-entered} property of the character after the new
2818location.
2819@end itemize
2820
2821@noindent
2822If these two values differ, each of them is called (if not @code{nil})
2823with two arguments: the old value of point, and the new one.
2824
2825The same comparison is made for the characters before the old and new
2826locations. The result may be to execute two @code{point-left} functions
2827(which may be the same function) and/or two @code{point-entered}
61cfa852
RS
2828functions (which may be the same function). In any case, all the
2829@code{point-left} functions are called first, followed by all the
2830@code{point-entered} functions.
73804d4b 2831
f9f59935
RS
2832It is possible using @code{char-after} to examine characters at various
2833positions without moving point to those positions. Only an actual
2834change in the value of point runs these hook functions.
73804d4b
RS
2835@end table
2836
2837@defvar inhibit-point-motion-hooks
2838When this variable is non-@code{nil}, @code{point-left} and
22697dac 2839@code{point-entered} hooks are not run, and the @code{intangible}
f9f59935
RS
2840property has no effect. Do not set this variable globally; bind it with
2841@code{let}.
73804d4b
RS
2842@end defvar
2843
b6dd5963
DL
2844@defvar show-help-function
2845@tindex show-help-function
05cae5d0
DL
2846@anchor{Help display} If this variable is non-@code{nil}, it specifies a
2847function called to display help strings. These may be @code{help-echo}
2848properties, menu help strings (@pxref{Simple Menu Items},
2849@pxref{Extended Menu Items}), or tool bar help strings (@pxref{Tool
2850Bar}). The specified function is called with one argument, the help
0b2fb045
WL
2851string to display. Tooltip mode (@pxref{Tooltips,,, emacs, The GNU Emacs
2852Manual}) provides an example.
b6dd5963
DL
2853@end defvar
2854
22697dac 2855@node Format Properties
bfe721d1 2856@subsection Formatted Text Properties
22697dac
KH
2857
2858 These text properties affect the behavior of the fill commands. They
bfe721d1
KH
2859are used for representing formatted text. @xref{Filling}, and
2860@ref{Margins}.
22697dac 2861
bfe721d1 2862@table @code
22697dac
KH
2863@item hard
2864If a newline character has this property, it is a ``hard'' newline.
2865The fill commands do not alter hard newlines and do not move words
2866across them. However, this property takes effect only if the variable
2867@code{use-hard-newlines} is non-@code{nil}.
2868
2869@item right-margin
bfe721d1 2870This property specifies an extra right margin for filling this part of the
22697dac
KH
2871text.
2872
2873@item left-margin
bfe721d1 2874This property specifies an extra left margin for filling this part of the
22697dac
KH
2875text.
2876
2877@item justification
2878This property specifies the style of justification for filling this part
2879of the text.
2880@end table
2881
73804d4b
RS
2882@node Sticky Properties
2883@subsection Stickiness of Text Properties
2884@cindex sticky text properties
2885@cindex inheritance of text properties
2886
2887 Self-inserting characters normally take on the same properties as the
2888preceding character. This is called @dfn{inheritance} of properties.
2889
2890 In a Lisp program, you can do insertion with inheritance or without,
2891depending on your choice of insertion primitive. The ordinary text
2892insertion functions such as @code{insert} do not inherit any properties.
2893They insert text with precisely the properties of the string being
2894inserted, and no others. This is correct for programs that copy text
61cfa852
RS
2895from one context to another---for example, into or out of the kill ring.
2896To insert with inheritance, use the special primitives described in this
2897section. Self-inserting characters inherit properties because they work
2898using these primitives.
73804d4b
RS
2899
2900 When you do insertion with inheritance, @emph{which} properties are
2468d0c0
DL
2901inherited, and from where, depends on which properties are @dfn{sticky}.
2902Insertion after a character inherits those of its properties that are
73804d4b 2903@dfn{rear-sticky}. Insertion before a character inherits those of its
2468d0c0
DL
2904properties that are @dfn{front-sticky}. When both sides offer different
2905sticky values for the same property, the previous character's value
2906takes precedence.
2907
2908 By default, a text property is rear-sticky but not front-sticky; thus,
2909the default is to inherit all the properties of the preceding character,
2910and nothing from the following character.
2911
2912 You can control the stickiness of various text properties with two
2913specific text properties, @code{front-sticky} and @code{rear-nonsticky},
2914and with the variable @code{text-property-default-nonsticky}. You can
2915use the variable to specify a different default for a given property.
2916You can use those two text properties to make any specific properties
2917sticky or nonsticky in any particular part of the text.
73804d4b
RS
2918
2919 If a character's @code{front-sticky} property is @code{t}, then all
2920its properties are front-sticky. If the @code{front-sticky} property is
2921a list, then the sticky properties of the character are those whose
2922names are in the list. For example, if a character has a
2923@code{front-sticky} property whose value is @code{(face read-only)},
2924then insertion before the character can inherit its @code{face} property
2925and its @code{read-only} property, but no others.
2926
75708135
RS
2927 The @code{rear-nonsticky} property works the opposite way. Most
2928properties are rear-sticky by default, so the @code{rear-nonsticky}
2929property says which properties are @emph{not} rear-sticky. If a
2930character's @code{rear-nonsticky} property is @code{t}, then none of its
2931properties are rear-sticky. If the @code{rear-nonsticky} property is a
2932list, properties are rear-sticky @emph{unless} their names are in the
2933list.
73804d4b 2934
2468d0c0
DL
2935@defvar text-property-default-nonsticky
2936@tindex text-property-default-nonsticky
2937This variable holds an alist which defines the default rear-stickiness
2938of various text properties. Each element has the form
2939@code{(@var{property} . @var{nonstickiness})}, and it defines the
2940stickiness of a particular text property, @var{property}.
2941
2942If @var{nonstickiness} is non-@code{nil}, this means that the property
2943@var{property} is rear-nonsticky by default. Since all properties are
2944front-nonsticky by default, this makes @var{property} nonsticky in both
2945directions by default.
2946
2947The text properties @code{front-sticky} and @code{rear-nonsticky}, when
2948used, take precedence over the default @var{nonstickiness} specifed in
2949@code{text-property-default-nonsticky}.
2950@end defvar
73804d4b
RS
2951
2952 Here are the functions that insert text with inheritance of properties:
2953
2954@defun insert-and-inherit &rest strings
2955Insert the strings @var{strings}, just like the function @code{insert},
2956but inherit any sticky properties from the adjoining text.
2957@end defun
2958
2959@defun insert-before-markers-and-inherit &rest strings
2960Insert the strings @var{strings}, just like the function
2961@code{insert-before-markers}, but inherit any sticky properties from the
2962adjoining text.
2963@end defun
2964
f9f59935
RS
2965 @xref{Insertion}, for the ordinary insertion functions which do not
2966inherit.
2967
73804d4b 2968@node Saving Properties
61cfa852 2969@subsection Saving Text Properties in Files
73804d4b
RS
2970@cindex text properties in files
2971@cindex saving text properties
2972
f9f59935
RS
2973 You can save text properties in files (along with the text itself),
2974and restore the same text properties when visiting or inserting the
2975files, using these two hooks:
73804d4b 2976
bfe721d1 2977@defvar write-region-annotate-functions
73804d4b
RS
2978This variable's value is a list of functions for @code{write-region} to
2979run to encode text properties in some fashion as annotations to the text
2980being written in the file. @xref{Writing to Files}.
2981
2982Each function in the list is called with two arguments: the start and
2983end of the region to be written. These functions should not alter the
2984contents of the buffer. Instead, they should return lists indicating
2985annotations to write in the file in addition to the text in the
2986buffer.
2987
2988Each function should return a list of elements of the form
2989@code{(@var{position} . @var{string})}, where @var{position} is an
f9f59935
RS
2990integer specifying the relative position within the text to be written,
2991and @var{string} is the annotation to add there.
73804d4b
RS
2992
2993Each list returned by one of these functions must be already sorted in
2994increasing order by @var{position}. If there is more than one function,
2995@code{write-region} merges the lists destructively into one sorted list.
2996
2997When @code{write-region} actually writes the text from the buffer to the
2998file, it intermixes the specified annotations at the corresponding
2999positions. All this takes place without modifying the buffer.
3000@end defvar
3001
3002@defvar after-insert-file-functions
3003This variable holds a list of functions for @code{insert-file-contents}
3004to call after inserting a file's contents. These functions should scan
3005the inserted text for annotations, and convert them to the text
3006properties they stand for.
3007
3008Each function receives one argument, the length of the inserted text;
3009point indicates the start of that text. The function should scan that
3010text for annotations, delete them, and create the text properties that
3011the annotations specify. The function should return the updated length
3012of the inserted text, as it stands after those changes. The value
3013returned by one function becomes the argument to the next function.
3014
3015These functions should always return with point at the beginning of
3016the inserted text.
3017
3018The intended use of @code{after-insert-file-functions} is for converting
3019some sort of textual annotations into actual text properties. But other
3020uses may be possible.
3021@end defvar
3022
3023We invite users to write Lisp programs to store and retrieve text
3024properties in files, using these hooks, and thus to experiment with
3025various data formats and find good ones. Eventually we hope users
3026will produce good, general extensions we can install in Emacs.
3027
f9f59935
RS
3028We suggest not trying to handle arbitrary Lisp objects as text property
3029names or values---because a program that general is probably difficult
3030to write, and slow. Instead, choose a set of possible data types that
3031are reasonably flexible, and not too hard to encode.
73804d4b 3032
bfe721d1
KH
3033@xref{Format Conversion}, for a related feature.
3034
3035@c ??? In next edition, merge this info Format Conversion.
3036
61ee3601
RS
3037@node Lazy Properties
3038@subsection Lazy Computation of Text Properties
3039
3040 Instead of computing text properties for all the text in the buffer,
3041you can arrange to compute the text properties for parts of the text
3042when and if something depends on them.
3043
3044 The primitive that extracts text from the buffer along with its
3045properties is @code{buffer-substring}. Before examining the properties,
3046this function runs the abnormal hook @code{buffer-access-fontify-functions}.
3047
3048@defvar buffer-access-fontify-functions
3049This variable holds a list of functions for computing text properties.
3050Before @code{buffer-substring} copies the text and text properties for a
3051portion of the buffer, it calls all the functions in this list. Each of
3052the functions receives two arguments that specify the range of the
3053buffer being accessed. (The buffer itself is always the current
3054buffer.)
3055@end defvar
3056
3057 The function @code{buffer-substring-no-properties} does not call these
3058functions, since it ignores text properties anyway.
3059
3060 In order to prevent the hook functions from being called more than
3061once for the same part of the buffer, you can use the variable
3062@code{buffer-access-fontified-property}.
3063
3064@defvar buffer-access-fontified-property
3065If this value's variable is non-@code{nil}, it is a symbol which is used
3066as a text property name. A non-@code{nil} value for that text property
3067means, ``the other text properties for this character have already been
3068computed.''
3069
3070If all the characters in the range specified for @code{buffer-substring}
3071have a non-@code{nil} value for this property, @code{buffer-substring}
3072does not call the @code{buffer-access-fontify-functions} functions. It
3073assumes these characters already have the right text properties, and
3074just copies the properties they already have.
3075
3076The normal way to use this feature is that the
3077@code{buffer-access-fontify-functions} functions add this property, as
3078well as others, to the characters they operate on. That way, they avoid
3079being called over and over for the same text.
3080@end defvar
3081
f9f59935
RS
3082@node Clickable Text
3083@subsection Defining Clickable Text
3084@cindex clickable text
3085
3086 There are two ways to set up @dfn{clickable text} in a buffer.
3087There are typically two parts of this: to make the text highlight
3088when the mouse is over it, and to make a mouse button do something
3089when you click it on that part of the text.
3090
3091 Highlighting is done with the @code{mouse-face} text property.
3092Here is an example of how Dired does it:
3093
3094@smallexample
3095(condition-case nil
3096 (if (dired-move-to-filename)
3097 (put-text-property (point)
3098 (save-excursion
3099 (dired-move-to-end-of-filename)
3100 (point))
3101 'mouse-face 'highlight))
3102 (error nil))
3103@end smallexample
3104
3105@noindent
3106The first two arguments to @code{put-text-property} specify the
3107beginning and end of the text.
3108
3109 The usual way to make the mouse do something when you click it
3110on this text is to define @code{mouse-2} in the major mode's
3111keymap. The job of checking whether the click was on clickable text
3112is done by the command definition. Here is how Dired does it:
3113
3114@smallexample
3115(defun dired-mouse-find-file-other-window (event)
3116 "In dired, visit the file or directory name you click on."
3117 (interactive "e")
3118 (let (file)
3119 (save-excursion
3120 (set-buffer (window-buffer (posn-window (event-end event))))
3121 (save-excursion
969fe9b5
RS
3122 (goto-char (posn-point (event-end event)))
3123 (setq file (dired-get-filename))))
f9f59935
RS
3124 (select-window (posn-window (event-end event)))
3125 (find-file-other-window (file-name-sans-versions file t))))
3126@end smallexample
3127
3128@noindent
3129The reason for the outer @code{save-excursion} construct is to avoid
3130changing the current buffer; the reason for the inner one is to avoid
3131permanently altering point in the buffer you click on. In this case,
3132Dired uses the function @code{dired-get-filename} to determine which
3133file to visit, based on the position found in the event.
3134
3135 Instead of defining a mouse command for the major mode, you can define
c26a99e8 3136a key binding for the clickable text itself, using the @code{keymap}
ce75fd23 3137text property:
f9f59935
RS
3138
3139@example
3140(let ((map (make-sparse-keymap)))
c26a99e8 3141 (define-key map [mouse-2] 'operate-this-button)
f9f59935
RS
3142 (put-text-property (point)
3143 (save-excursion
3144 (dired-move-to-end-of-filename)
3145 (point))
c26a99e8 3146 'keymap map))
f9f59935
RS
3147@end example
3148
3149@noindent
3150This method makes it possible to define different commands for various
3151clickable pieces of text. Also, the major mode definition (or the
3152global definition) remains available for the rest of the text in the
3153buffer.
3154
2468d0c0
DL
3155@node Fields
3156@subsection Defining and Using Fields
3157@cindex fields
3158
3159 A field is a range of consecutive characters in the buffer that are
3160identified by having the same value (comparing with @code{eq}) of the
35627f72
MB
3161@code{field} property (either a text-property or an overlay property).
3162This section describes special functions that are available for
3163operating on fields.
2468d0c0
DL
3164
3165 You specify a field with a buffer position, @var{pos}. We think of
3166each field as containing a range of buffer positions, so the position
3167you specify stands for the field containing that position.
3168
3169 When the characters before and after @var{pos} are part of the same
3170field, there is no doubt which field contains @var{pos}: the one those
3171characters both belong to. When @var{pos} is at a boundary between
3172fields, which field it belongs to depends on the stickiness of the
3173@code{field} properties of the two surrounding characters (@pxref{Sticky
3174Properties}). The field whose property would be inherited by text
3175inserted at @var{pos} is the field that contains @var{pos}.
3176
3177 There is an anomalous case where newly inserted text at @var{pos}
3178would not inherit the @code{field} property from either side. This
3179happens if the previous character's @code{field} property is not
3180rear-sticky, and the following character's @code{field} property is not
3181front-sticky. In this case, @var{pos} belongs to neither the preceding
3182field nor the following field; the field functions treat it as belonging
3183to an empty field whose beginning and end are both at @var{pos}.
3184
3185 In all of these functions, if @var{pos} is omitted or @code{nil}, the
3186value of point is used by default.
3187
3188@defun field-beginning &optional pos escape-from-edge
3189@tindex field-beginning
3190This function returns the beginning of the field specified by @var{pos}.
3191
35627f72
MB
3192If @var{pos} is at the beginning of its field, and
3193@var{escape-from-edge} is non-@code{nil}, then the return value is
3194always the beginning of the preceding field that @emph{ends} at @var{pos},
3195regardless of the stickiness of the @code{field} properties around
3196@var{pos}.
2468d0c0
DL
3197@end defun
3198
3199@defun field-end &optional pos escape-from-edge
3200@tindex field-end
3201This function returns the end of the field specified by @var{pos}.
3202
35627f72
MB
3203If @var{pos} is at the end of its field, and @var{escape-from-edge} is
3204non-@code{nil}, then the return value is always the end of the following
3205field that @emph{begins} at @var{pos}, regardless of the stickiness of
3206the @code{field} properties around @var{pos}.
2468d0c0
DL
3207@end defun
3208
3209@defun field-string &optional pos
3210@tindex field-string
3211This function returns the contents of the field specified by @var{pos},
3212as a string.
3213@end defun
3214
3215@defun field-string-no-properties &optional pos
3216@tindex field-string-no-properties
3217This function returns the contents of the field specified by @var{pos},
3218as a string, discarding text properties.
3219@end defun
3220
3221@defun delete-field &optional pos
3222@tindex delete-field
3223This function deletes the text of the field specified by @var{pos}.
3224@end defun
3225
35627f72 3226@defun constrain-to-field new-pos old-pos &optional escape-from-edge only-in-line inhibit-capture-property
2468d0c0
DL
3227@tindex constrain-to-field
3228This function ``constrains'' @var{new-pos} to the field that
3229@var{old-pos} belongs to---in other words, it returns the position
3230closest to @var{new-pos} that is in the same field as @var{old-pos}.
3231
3232If @var{new-pos} is @code{nil}, then @code{constrain-to-field} uses
3233the value of point instead, and moves point to the resulting position.
3234
75708135
RS
3235If @var{old-pos} is at the boundary of two fields, then the acceptable
3236positions for @var{new-pos} depend on the value of the optional argument
3237@var{escape-from-edge}. If @var{escape-from-edge} is @code{nil}, then
3238@var{new-pos} is constrained to the field that has the same @code{field}
35627f72
MB
3239property (either a text-property or an overlay property) that new
3240characters inserted at @var{old-pos} would get. (This depends on the
3241stickiness of the @code{field} property for the characters before and
3242after @var{old-pos}.) If @var{escape-from-edge} is non-@code{nil},
3243@var{new-pos} is constrained to the union of the two adjacent fields.
3244Additionally, if two fields are separated by another field with the
3245special value @code{boundary}, then any point within this special field
3246is also considered to be ``on the boundary.''
2468d0c0
DL
3247
3248If the optional argument @var{only-in-line} is non-@code{nil}, and
3249constraining @var{new-pos} in the usual way would move it to a different
3250line, @var{new-pos} is returned unconstrained. This used in commands
3251that move by line, such as @code{next-line} and
3252@code{beginning-of-line}, so that they respect field boundaries only in
3253the case where they can still move to the right line.
35627f72
MB
3254
3255If the optional argument @var{inhibit-capture-property} is
3256non-@code{nil}, and @var{old-pos} has a non-@code{nil} property of that
3257name, then any field boundaries are ignored.
3258
3259You can cause @code{constrain-to-field} to ignore all field boundaries
3260(and so never constrain anything) by binding the variable
3261@code{inhibit-field-text-motion} to a non-nil value.
2468d0c0
DL
3262@end defun
3263
73804d4b
RS
3264@node Not Intervals
3265@subsection Why Text Properties are not Intervals
3266@cindex intervals
3267
3268 Some editors that support adding attributes to text in the buffer do
3269so by letting the user specify ``intervals'' within the text, and adding
3270the properties to the intervals. Those editors permit the user or the
3271programmer to determine where individual intervals start and end. We
3272deliberately provided a different sort of interface in Emacs Lisp to
3273avoid certain paradoxical behavior associated with text modification.
3274
3275 If the actual subdivision into intervals is meaningful, that means you
3276can distinguish between a buffer that is just one interval with a
3277certain property, and a buffer containing the same text subdivided into
3278two intervals, both of which have that property.
3279
3280 Suppose you take the buffer with just one interval and kill part of
3281the text. The text remaining in the buffer is one interval, and the
3282copy in the kill ring (and the undo list) becomes a separate interval.
3283Then if you yank back the killed text, you get two intervals with the
3284same properties. Thus, editing does not preserve the distinction
3285between one interval and two.
3286
3287 Suppose we ``fix'' this problem by coalescing the two intervals when
3288the text is inserted. That works fine if the buffer originally was a
3289single interval. But suppose instead that we have two adjacent
3290intervals with the same properties, and we kill the text of one interval
3291and yank it back. The same interval-coalescence feature that rescues
3292the other case causes trouble in this one: after yanking, we have just
3293one interval. One again, editing does not preserve the distinction
3294between one interval and two.
3295
3296 Insertion of text at the border between intervals also raises
3297questions that have no satisfactory answer.
3298
3299 However, it is easy to arrange for editing to behave consistently for
3300questions of the form, ``What are the properties of this character?''
3301So we have decided these are the only questions that make sense; we have
3302not implemented asking questions about where intervals start or end.
3303
f9f59935 3304 In practice, you can usually use the text property search functions in
73804d4b
RS
3305place of explicit interval boundaries. You can think of them as finding
3306the boundaries of intervals, assuming that intervals are always
3307coalesced whenever possible. @xref{Property Search}.
3308
3309 Emacs also provides explicit intervals as a presentation feature; see
3310@ref{Overlays}.
3311
3312@node Substitution
3313@section Substituting for a Character Code
3314
3315 The following functions replace characters within a specified region
3316based on their character codes.
3317
3318@defun subst-char-in-region start end old-char new-char &optional noundo
3319@cindex replace characters
3320This function replaces all occurrences of the character @var{old-char}
3321with the character @var{new-char} in the region of the current buffer
3322defined by @var{start} and @var{end}.
3323
73804d4b 3324@cindex undo avoidance
bfe721d1
KH
3325If @var{noundo} is non-@code{nil}, then @code{subst-char-in-region} does
3326not record the change for undo and does not mark the buffer as modified.
2468d0c0
DL
3327This was useful for controlling the old selective display feature
3328(@pxref{Selective Display}).
73804d4b
RS
3329
3330@code{subst-char-in-region} does not move point and returns
3331@code{nil}.
3332
3333@example
3334@group
3335---------- Buffer: foo ----------
3336This is the contents of the buffer before.
3337---------- Buffer: foo ----------
3338@end group
3339
3340@group
3341(subst-char-in-region 1 20 ?i ?X)
3342 @result{} nil
3343
3344---------- Buffer: foo ----------
3345ThXs Xs the contents of the buffer before.
3346---------- Buffer: foo ----------
3347@end group
3348@end example
3349@end defun
3350
3351@defun translate-region start end table
3352This function applies a translation table to the characters in the
3353buffer between positions @var{start} and @var{end}.
3354
3355The translation table @var{table} is a string; @code{(aref @var{table}
3356@var{ochar})} gives the translated character corresponding to
3357@var{ochar}. If the length of @var{table} is less than 256, any
3358characters with codes larger than the length of @var{table} are not
3359altered by the translation.
3360
3361The return value of @code{translate-region} is the number of
61cfa852
RS
3362characters that were actually changed by the translation. This does
3363not count characters that were mapped into themselves in the
73804d4b 3364translation table.
73804d4b
RS
3365@end defun
3366
3367@node Registers
3368@section Registers
3369@cindex registers
3370
3371 A register is a sort of variable used in Emacs editing that can hold a
969fe9b5 3372variety of different kinds of values. Each register is named by a
75708135
RS
3373single character. All @sc{ascii} characters and their meta variants
3374(but with the exception of @kbd{C-g}) can be used to name registers.
3375Thus, there are 255 possible registers. A register is designated in
3376Emacs Lisp by the character that is its name.
73804d4b
RS
3377
3378@defvar register-alist
3379This variable is an alist of elements of the form @code{(@var{name} .
3380@var{contents})}. Normally, there is one element for each Emacs
3381register that has been used.
3382
3383The object @var{name} is a character (an integer) identifying the
969fe9b5 3384register.
73804d4b
RS
3385@end defvar
3386
969fe9b5
RS
3387 The @var{contents} of a register can have several possible types:
3388
3389@table @asis
3390@item a number
3391A number stands for itself. If @code{insert-register} finds a number
3392in the register, it converts the number to decimal.
3393
3394@item a marker
3395A marker represents a buffer position to jump to.
3396
3397@item a string
3398A string is text saved in the register.
3399
3400@item a rectangle
3401A rectangle is represented by a list of strings.
3402
3403@item @code{(@var{window-configuration} @var{position})}
3404This represents a window configuration to restore in one frame, and a
3405position to jump to in the current buffer.
3406
3407@item @code{(@var{frame-configuration} @var{position})}
3408This represents a frame configuration to restore, and a position
3409to jump to in the current buffer.
3410
3411@item (file @var{filename})
3412This represents a file to visit; jumping to this value visits file
3413@var{filename}.
3414
3415@item (file-query @var{filename} @var{position})
3416This represents a file to visit and a position in it; jumping to this
3417value visits file @var{filename} and goes to buffer position
3418@var{position}. Restoring this type of position asks the user for
3419confirmation first.
3420@end table
3421
3422 The functions in this section return unpredictable values unless
3423otherwise stated.
3424
73804d4b
RS
3425@defun get-register reg
3426This function returns the contents of the register
3427@var{reg}, or @code{nil} if it has no contents.
3428@end defun
3429
3430@defun set-register reg value
3431This function sets the contents of register @var{reg} to @var{value}.
3432A register can be set to any value, but the other register functions
3433expect only certain data types. The return value is @var{value}.
3434@end defun
3435
3436@deffn Command view-register reg
3437This command displays what is contained in register @var{reg}.
3438@end deffn
3439
3440@ignore
3441@deffn Command point-to-register reg
3442This command stores both the current location of point and the current
3443buffer in register @var{reg} as a marker.
3444@end deffn
3445
3446@deffn Command jump-to-register reg
3447@deffnx Command register-to-point reg
3448@comment !!SourceFile register.el
3449This command restores the status recorded in register @var{reg}.
3450
3451If @var{reg} contains a marker, it moves point to the position stored in
3452the marker. Since both the buffer and the location within the buffer
3453are stored by the @code{point-to-register} function, this command can
3454switch you to another buffer.
3455
3456If @var{reg} contains a window configuration or a frame configuration.
3457@code{jump-to-register} restores that configuration.
3458@end deffn
3459@end ignore
3460
3461@deffn Command insert-register reg &optional beforep
3462This command inserts contents of register @var{reg} into the current
3463buffer.
3464
3465Normally, this command puts point before the inserted text, and the
3466mark after it. However, if the optional second argument @var{beforep}
3467is non-@code{nil}, it puts the mark before and point after.
3468You can pass a non-@code{nil} second argument @var{beforep} to this
3469function interactively by supplying any prefix argument.
3470
3471If the register contains a rectangle, then the rectangle is inserted
3472with its upper left corner at point. This means that text is inserted
3473in the current line and underneath it on successive lines.
3474
3475If the register contains something other than saved text (a string) or
3476a rectangle (a list), currently useless things happen. This may be
3477changed in the future.
3478@end deffn
3479
3480@ignore
3481@deffn Command copy-to-register reg start end &optional delete-flag
3482This command copies the region from @var{start} to @var{end} into
3483register @var{reg}. If @var{delete-flag} is non-@code{nil}, it deletes
3484the region from the buffer after copying it into the register.
3485@end deffn
3486
3487@deffn Command prepend-to-register reg start end &optional delete-flag
3488This command prepends the region from @var{start} to @var{end} into
3489register @var{reg}. If @var{delete-flag} is non-@code{nil}, it deletes
3490the region from the buffer after copying it to the register.
3491@end deffn
3492
3493@deffn Command append-to-register reg start end &optional delete-flag
3494This command appends the region from @var{start} to @var{end} to the
3495text already in register @var{reg}. If @var{delete-flag} is
3496non-@code{nil}, it deletes the region from the buffer after copying it
3497to the register.
3498@end deffn
3499
3500@deffn Command copy-rectangle-to-register reg start end &optional delete-flag
3501This command copies a rectangular region from @var{start} to @var{end}
3502into register @var{reg}. If @var{delete-flag} is non-@code{nil}, it
3503deletes the region from the buffer after copying it to the register.
3504@end deffn
3505
3506@deffn Command window-configuration-to-register reg
3507This function stores the window configuration of the selected frame in
3508register @var{reg}.
3509@end deffn
3510
3511@deffn Command frame-configuration-to-register reg
3512This function stores the current frame configuration in register
3513@var{reg}.
3514@end deffn
3515@end ignore
3516
b22f3a19
RS
3517@node Transposition
3518@section Transposition of Text
3519
3520 This subroutine is used by the transposition commands.
3521
3522@defun transpose-regions start1 end1 start2 end2 &optional leave-markers
3523This function exchanges two nonoverlapping portions of the buffer.
3524Arguments @var{start1} and @var{end1} specify the bounds of one portion
3525and arguments @var{start2} and @var{end2} specify the bounds of the
3526other portion.
3527
3528Normally, @code{transpose-regions} relocates markers with the transposed
3529text; a marker previously positioned within one of the two transposed
3530portions moves along with that portion, thus remaining between the same
3531two characters in their new position. However, if @var{leave-markers}
3532is non-@code{nil}, @code{transpose-regions} does not do this---it leaves
3533all markers unrelocated.
3534@end defun
3535
b6954afd
RS
3536@node Base 64
3537@section Base 64 Encoding
3538@cindex base 64 encoding
3539
ae9b6b4a
RS
3540 Base 64 code is used in email to encode a sequence of 8-bit bytes as
3541a longer sequence of @sc{ascii} graphic characters. It is defined in
944351c0
EZ
3542Internet RFC@footnote{
3543An RFC, an acronym for @dfn{Request for Comments}, is a numbered
3544Internet informational document describing a standard. RFCs are
3545usually written by technical experts acting on their own initiative,
3546and are traditionally written in a pragmatic, experience-driven
3547manner.
3548}2045. This section describes the functions for
ae9b6b4a 3549converting to and from this code.
b6954afd
RS
3550
3551@defun base64-encode-region beg end &optional no-line-break
3552@tindex base64-encode-region
b25e2fb5
DL
3553This function converts the region from @var{beg} to @var{end} into base
355464 code. It returns the length of the encoded text. An error is
3555signaled if a character in the region is multibyte, i.e.@: in a
3556multibyte buffer the region must contain only characters from the
3557charsets @code{ascii}, @code{eight-bit-control} and
3558@code{eight-bit-graphic}.
b6954afd
RS
3559
3560Normally, this function inserts newline characters into the encoded
3561text, to avoid overlong lines. However, if the optional argument
3562@var{no-line-break} is non-@code{nil}, these newlines are not added, so
3563the output is just one long line.
3564@end defun
3565
3566@defun base64-encode-string string &optional no-line-break
3567@tindex base64-encode-string
3568This function converts the string @var{string} into base 64 code. It
b25e2fb5
DL
3569returns a string containing the encoded text. As for
3570@code{base64-encode-region}, an error is signaled if a character in the
3571string is multibyte.
b6954afd
RS
3572
3573Normally, this function inserts newline characters into the encoded
3574text, to avoid overlong lines. However, if the optional argument
3575@var{no-line-break} is non-@code{nil}, these newlines are not added, so
3576the result string is just one long line.
3577@end defun
3578
3579@defun base64-decode-region beg end
3580@tindex base64-decode-region
3581This function converts the region from @var{beg} to @var{end} from base
358264 code into the corresponding decoded text. It returns the length of
3583the decoded text.
3584
3585The decoding functions ignore newline characters in the encoded text.
3586@end defun
3587
3588@defun base64-decode-string string
3589@tindex base64-decode-string
3590This function converts the string @var{string} from base 64 code into
3591the corresponding decoded text. It returns a string containing the
3592decoded text.
3593
3594The decoding functions ignore newline characters in the encoded text.
3595@end defun
3596
944351c0
EZ
3597@node MD5 Checksum
3598@section MD5 Checksum
3599@cindex MD5 checksum
3600@cindex message digest computation
3601
3602 MD5 cryptographic checksums, or @dfn{message digests}, are 128-bit
3603``fingerprints'' of a document or program. They are used to verify
3604that you have an exact and unaltered copy of the data. The algorithm
3605to calculate the MD5 message digest is defined in Internet
3606RFC@footnote{
3607For an explanation of what is an RFC, see the footnote in @ref{Base
360864}.
3609}1321. This section describes the Emacs facilities for computing
3610message digests.
3611
3612@defun md5 object &optional start end coding-system noerror
3613This function returns the MD5 message digest of @var{object}, which
3614should be a buffer or a string.
3615
3616The two optional arguments @var{start} and @var{end} are character
3617positions specifying the portion of @var{object} to compute the
3618message digest for. If they are @code{nil} or omitted, the digest is
3619computed for the whole of @var{object}.
3620
3621The function @code{md5} does not compute the message digest directly
3622from the internal Emacs representation of the text (@pxref{Text
3623Representations}). Instead, it encodes the text using a coding
3624system, and computes the message digest from the encoded text. The
3625optional fourth argument @var{coding-system} specifies which coding
3626system to use for encoding the text. It should be the same coding
3627system that you used to read the text, or that you used or will use
3628when saving or sending the text. @xref{Coding Systems}, for more
3629information about coding systems.
3630
3631If @var{coding-system} is @code{nil} or omitted, the default depends
3632on @var{object}. If @var{object} is a buffer, the default for
3633@var{coding-system} is whatever coding system would be chosen by
3634default for writing this text into a file. If @var{object} is a
3635string, the user's most preferred coding system (@pxref{Recognize
3636Coding, prefer-coding-system, the description of
3637@code{prefer-coding-system}, emacs, GNU Emacs Manual}) is used.
3638
3639Normally, @code{md5} signals an error if the text can't be encoded
3640using the specified or chosen coding system. However, if
3641@var{noerror} is non-@code{nil}, it silently uses @code{raw-text}
3642coding instead.
3643@end defun
3644
73804d4b
RS
3645@node Change Hooks
3646@section Change Hooks
3647@cindex change hooks
3648@cindex hooks for text changes
3649
3650 These hook variables let you arrange to take notice of all changes in
3651all buffers (or in a particular buffer, if you make them buffer-local).
3652See also @ref{Special Properties}, for how to detect changes to specific
3653parts of the text.
3654
3655 The functions you use in these hooks should save and restore the match
3656data if they do anything that uses regular expressions; otherwise, they
3657will interfere in bizarre ways with the editing operations that call
3658them.
3659
72654a3c 3660@defvar before-change-functions
f9f59935 3661This variable holds a list of functions to call before any buffer
72654a3c
RS
3662modification. Each function gets two arguments, the beginning and end
3663of the region that is about to change, represented as integers. The
3664buffer that is about to change is always the current buffer.
3665@end defvar
3666
3667@defvar after-change-functions
f9f59935 3668This variable holds a list of functions to call after any buffer
72654a3c
RS
3669modification. Each function receives three arguments: the beginning and
3670end of the region just changed, and the length of the text that existed
584b8104
RS
3671before the change. All three arguments are integers. The buffer that's
3672about to change is always the current buffer.
3673
1911e6e5 3674The length of the old text is the difference between the buffer positions
a9f0a989
RS
3675before and after that text as it was before the change. As for the
3676changed text, its length is simply the difference between the first two
3677arguments.
72654a3c
RS
3678@end defvar
3679
f9f59935
RS
3680@defmac combine-after-change-calls body...
3681The macro executes @var{body} normally, but arranges to call the
3682after-change functions just once for a series of several changes---if
3683that seems safe.
3684
3685If a program makes several text changes in the same area of the buffer,
3686using the macro @code{combine-after-change-calls} around that part of
3687the program can make it run considerably faster when after-change hooks
969fe9b5
RS
3688are in use. When the after-change hooks are ultimately called, the
3689arguments specify a portion of the buffer including all of the changes
3690made within the @code{combine-after-change-calls} body.
f9f59935
RS
3691
3692@strong{Warning:} You must not alter the values of
b6dd5963 3693@code{after-change-functions} within
f9f59935 3694the body of a @code{combine-after-change-calls} form.
969fe9b5
RS
3695
3696@strong{Note:} If the changes you combine occur in widely scattered
3697parts of the buffer, this will still work, but it is not advisable,
3698because it may lead to inefficient behavior for some change hook
3699functions.
f9f59935
RS
3700@end defmac
3701
b6dd5963 3702The two variables above are temporarily bound to @code{nil} during the
72654a3c 3703time that any of these functions is running. This means that if one of
73804d4b 3704these functions changes the buffer, that change won't run these
72654a3c
RS
3705functions. If you do want a hook function to make changes that run
3706these functions, make it bind these variables back to their usual
73804d4b
RS
3707values.
3708
b22f3a19
RS
3709One inconvenient result of this protective feature is that you cannot
3710have a function in @code{after-change-functions} or
3711@code{before-change-functions} which changes the value of that variable.
3712But that's not a real limitation. If you want those functions to change
3713the list of functions to run, simply add one fixed function to the hook,
3714and code that function to look in another variable for other functions
3715to call. Here is an example:
3716
3717@example
3718(setq my-own-after-change-functions nil)
3719(defun indirect-after-change-function (beg end len)
3720 (let ((list my-own-after-change-functions))
3721 (while list
3722 (funcall (car list) beg end len)
3723 (setq list (cdr list)))))
969fe9b5
RS
3724
3725@group
b22f3a19
RS
3726(add-hooks 'after-change-functions
3727 'indirect-after-change-function)
969fe9b5 3728@end group
b22f3a19
RS
3729@end example
3730
73804d4b
RS
3731@defvar first-change-hook
3732This variable is a normal hook that is run whenever a buffer is changed
3733that was previously in the unmodified state.
3734@end defvar
8241495d
RS
3735
3736@defvar inhibit-modification-hooks
3737@tindex inhibit-modification-hooks
3738If this variable is non-@code{nil}, all of the change hooks are
3739disabled; none of them run. This affects all the hook variables
3740described above in this section, as well as the hooks attached to
3741certain special text properties (@pxref{Special Properties}) and overlay
3742properties (@pxref{Overlay Properties}).
3743
3744This variable is available starting in Emacs 21.
3745@end defvar