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