comparison lispref/text.texi @ 21007:66d807bdc5b4

*** empty log message ***
author Richard M. Stallman <rms@gnu.org>
date Sat, 28 Feb 1998 01:53:53 +0000
parents d76f57ca7aba
children 90da2489c498
comparison
equal deleted inserted replaced
21006:00022857f529 21007:66d807bdc5b4
1 @c -*-texinfo-*- 1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual. 2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. 3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions. 4 @c See the file elisp.texi for copying conditions.
5 @setfilename ../info/text 5 @setfilename ../info/text
6 @node Text, Searching and Matching, Markers, Top 6 @node Text, Non-ASCII Characters, Markers, Top
7 @chapter Text 7 @chapter Text
8 @cindex text 8 @cindex text
9 9
10 This chapter describes the functions that deal with the text in a 10 This chapter describes the functions that deal with the text in a
11 buffer. Most examine, insert, or delete text in the current buffer, 11 buffer. Most examine, insert, or delete text in the current buffer,
128 the text. See also @code{point-max} in @xref{Point}. 128 the text. See also @code{point-max} in @xref{Point}.
129 @end defun 129 @end defun
130 130
131 @defun bolp 131 @defun bolp
132 This function returns @code{t} if point is at the beginning of a line. 132 This function returns @code{t} if point is at the beginning of a line.
133 @xref{Text Lines}. The beginning of the buffer (or its accessible 133 @xref{Text Lines}. The beginning of the buffer (or of its accessible
134 portion) always counts as the beginning of a line. 134 portion) always counts as the beginning of a line.
135 @end defun 135 @end defun
136 136
137 @defun eolp 137 @defun eolp
138 This function returns @code{t} if point is at the end of a line. The 138 This function returns @code{t} if point is at the end of a line. The
188 Here's an example of using this function to get a word to look up in an 188 Here's an example of using this function to get a word to look up in an
189 alist: 189 alist:
190 190
191 @example 191 @example
192 (setq flammable 192 (setq flammable
193 (assoc (buffer-substring start end) 193 (assoc (buffer-substring-no-properties start end)
194 '(("wood" . t) ("paper" . t) 194 '(("wood" . t) ("paper" . t)
195 ("steel" . nil) ("asbestos" . nil)))) 195 ("steel" . nil) ("asbestos" . nil))))
196 @end example 196 @end example
197 197
198 If this were written using @code{buffer-substring} instead, it would not 198 If this were written using @code{buffer-substring} instead, it would not
199 work reliably; any text properties that happened to be in the word 199 work reliably; any text properties that happened to be in the word
200 copied from the buffer would make the comparisons fail. 200 copied from the buffer would make the comparisons fail.
201 @end defun 201 @end defun
202 202
203 @defun buffer-string 203 @defun buffer-string
204 This function returns the contents of the accessible portion of the 204 This function returns the contents of the entire accessible portion of
205 current buffer as a string. This is the portion between 205 the current buffer as a string. This is the portion between
206 @code{(point-min)} and @code{(point-max)} (@pxref{Narrowing}). 206 @code{(point-min)} and @code{(point-max)} (@pxref{Narrowing}).
207 207
208 @example 208 @example
209 @group 209 @group
210 ---------- Buffer: foo ---------- 210 ---------- Buffer: foo ----------
214 214
215 (buffer-string) 215 (buffer-string)
216 @result{} "This is the contents of buffer foo 216 @result{} "This is the contents of buffer foo
217 " 217 "
218 @end group 218 @end group
219 @end example
220 @end defun
221
222 @defun thing-at-point thing
223 Return the @var{thing} around or next to point, as a string.
224
225 The argument @var{thing} is a symbol which specifies a kind of syntactic
226 entity. Possibilities include @code{symbol}, @code{list}, @code{sexp},
227 @code{defun}, @code{filename}, @code{url}, @code{word}, @code{sentence},
228 @code{whitespace}, @code{line}, @code{page}, and others.
229
230 @example
231 ---------- Buffer: foo ----------
232 Gentlemen may cry ``Pea@point{}ce! Peace!,''
233 but there is no peace.
234 ---------- Buffer: foo ----------
235
236 (thing-at-point 'word)
237 @result{} "Peace"
238 (thing-at-point 'line)
239 @result{} "Gentlemen may cry ``Peace! Peace!,''\n"
240 (thing-at-point 'whitespace)
241 @result{} nil
219 @end example 242 @end example
220 @end defun 243 @end defun
221 244
222 @node Comparing Text 245 @node Comparing Text
223 @section Comparing Text 246 @section Comparing Text
299 unless all @var{args} are either strings or characters. The value is 322 unless all @var{args} are either strings or characters. The value is
300 @code{nil}. 323 @code{nil}.
301 324
302 This function is unlike the other insertion functions in that it 325 This function is unlike the other insertion functions in that it
303 relocates markers initially pointing at the insertion point, to point 326 relocates markers initially pointing at the insertion point, to point
304 after the inserted text. If an overlat begins the insertion point, the 327 after the inserted text. If an overlay begins the insertion point, the
305 inserted text falls outside the overlay; if a nonempty overlay ends at 328 inserted text falls outside the overlay; if a nonempty overlay ends at
306 the insertion point, the inserted text falls inside that overlay. 329 the insertion point, the inserted text falls inside that overlay.
307 @end defun 330 @end defun
308 331
309 @defun insert-char character count &optional inherit 332 @defun insert-char character count &optional inherit
718 741
719 @code{kill-region} is the usual subroutine for killing text. Any 742 @code{kill-region} is the usual subroutine for killing text. Any
720 command that calls this function is a ``kill command'' (and should 743 command that calls this function is a ``kill command'' (and should
721 probably have @samp{kill} in its name). @code{kill-region} puts the 744 probably have @samp{kill} in its name). @code{kill-region} puts the
722 newly killed text in a new element at the beginning of the kill ring or 745 newly killed text in a new element at the beginning of the kill ring or
723 adds it to the most recent element. It uses the @code{last-command} 746 adds it to the most recent element. It determines automatically (using
724 variable to determine whether the previous command was a kill command, 747 @code{last-command}) whether the previous command was a kill command,
725 and if so appends the killed text to the most recent entry. 748 and if so appends the killed text to the most recent entry.
726 749
727 @deffn Command kill-region start end 750 @deffn Command kill-region start end
728 This function kills the text in the region defined by @var{start} and 751 This function kills the text in the region defined by @var{start} and
729 @var{end}. The text is deleted but saved in the kill ring, along with 752 @var{end}. The text is deleted but saved in the kill ring, along with
748 771
749 The command does not set @code{this-command} to @code{kill-region}, so a 772 The command does not set @code{this-command} to @code{kill-region}, so a
750 subsequent kill command does not append to the same kill ring entry. 773 subsequent kill command does not append to the same kill ring entry.
751 774
752 Don't call @code{copy-region-as-kill} in Lisp programs unless you aim to 775 Don't call @code{copy-region-as-kill} in Lisp programs unless you aim to
753 support Emacs 18. For Emacs 19, it is better to use @code{kill-new} or 776 support Emacs 18. For newer Emacs versions, it is better to use
754 @code{kill-append} instead. @xref{Low-Level Kill Ring}. 777 @code{kill-new} or @code{kill-append} instead. @xref{Low-Level Kill
778 Ring}.
755 @end deffn 779 @end deffn
756 780
757 @node Yank Commands 781 @node Yank Commands
758 @comment node-name, next, previous, up 782 @comment node-name, next, previous, up
759 @subsection Functions for Yanking 783 @subsection Functions for Yanking
861 885
862 If the value is a function, @code{kill-new} and @code{kill-append} call 886 If the value is a function, @code{kill-new} and @code{kill-append} call
863 it with the new first element of the kill ring as an argument. 887 it with the new first element of the kill ring as an argument.
864 888
865 The normal use of this hook is to set the X server's primary selection 889 The normal use of this hook is to set the X server's primary selection
866 to the newly killed text. 890 from the newly killed text.
867 @end defvar 891 @end defvar
868 892
869 @node Internals of Kill Ring 893 @node Internals of Kill Ring
870 @comment node-name, next, previous, up 894 @comment node-name, next, previous, up
871 @subsection Internals of the Kill Ring 895 @subsection Internals of the Kill Ring
957 Here are the kinds of elements an undo list can have: 981 Here are the kinds of elements an undo list can have:
958 982
959 @table @code 983 @table @code
960 @item @var{integer} 984 @item @var{integer}
961 This kind of element records a previous value of point. Ordinary cursor 985 This kind of element records a previous value of point. Ordinary cursor
962 motion does not get any sort of undo record, but deletion commands use 986 motion does not get any sort of undo record, but deletion operations use
963 these entries to record where point was before the command. 987 these entries to record where point was before the command.
964 988
965 @item (@var{beg} . @var{end}) 989 @item (@var{beg} . @var{end})
966 This kind of element indicates how to delete text that was inserted. 990 This kind of element indicates how to delete text that was inserted.
967 Upon insertion, the text occupied the range @var{beg}--@var{end} in the 991 Upon insertion, the text occupied the range @var{beg}--@var{end} in the
1176 This variable alters the action of @code{fill-individual-paragraphs} as 1200 This variable alters the action of @code{fill-individual-paragraphs} as
1177 described above. 1201 described above.
1178 @end defopt 1202 @end defopt
1179 1203
1180 @deffn Command fill-region-as-paragraph start end &optional justify 1204 @deffn Command fill-region-as-paragraph start end &optional justify
1181 This command considers a region of text as a paragraph and fills it. If 1205 This command considers a region of text as a single paragraph and fills
1182 the region was made up of many paragraphs, the blank lines between 1206 it. If the region was made up of many paragraphs, the blank lines
1183 paragraphs are removed. This function justifies as well as filling when 1207 between paragraphs are removed. This function justifies as well as
1184 @var{justify} is non-@code{nil}. 1208 filling when @var{justify} is non-@code{nil}.
1185 1209
1186 In an interactive call, any prefix argument requests justification. 1210 In an interactive call, any prefix argument requests justification.
1187 1211
1188 In Adaptive Fill mode, which is enabled by default, calling the function 1212 In Adaptive Fill mode, which is enabled by default, calling the function
1189 @code{fill-region-as-paragraph} on an indented paragraph when there is 1213 @code{fill-region-as-paragraph} on an indented paragraph when there is
1260 1284
1261 The fill prefix follows the left margin whitespace, if any. 1285 The fill prefix follows the left margin whitespace, if any.
1262 @end defopt 1286 @end defopt
1263 1287
1264 @defopt fill-column 1288 @defopt fill-column
1265 This buffer-local variable specifies the maximum width of filled 1289 This buffer-local variable specifies the maximum width of filled lines.
1266 lines. Its value should be an integer, which is a number of columns. 1290 Its value should be an integer, which is a number of columns. All the
1267 All the filling, justification and centering commands are affected by 1291 filling, justification, and centering commands are affected by this
1268 this variable, including Auto Fill mode (@pxref{Auto Filling}). 1292 variable, including Auto Fill mode (@pxref{Auto Filling}).
1269 1293
1270 As a practical matter, if you are writing text for other people to 1294 As a practical matter, if you are writing text for other people to
1271 read, you should set @code{fill-column} to no more than 70. Otherwise 1295 read, you should set @code{fill-column} to no more than 70. Otherwise
1272 the line will be too long for people to read comfortably, and this can 1296 the line will be too long for people to read comfortably, and this can
1273 make the text seem clumsy. 1297 make the text seem clumsy.
1382 a buffer. This is in contrast to the function @code{sort}, which 1406 a buffer. This is in contrast to the function @code{sort}, which
1383 rearranges the order of the elements of a list (@pxref{Rearrangement}). 1407 rearranges the order of the elements of a list (@pxref{Rearrangement}).
1384 The values returned by these functions are not meaningful. 1408 The values returned by these functions are not meaningful.
1385 1409
1386 @defun sort-subr reverse nextrecfun endrecfun &optional startkeyfun endkeyfun 1410 @defun sort-subr reverse nextrecfun endrecfun &optional startkeyfun endkeyfun
1387 This function is the general text-sorting routine that divides a buffer 1411 This function is the general text-sorting routine that subdivides a
1388 into records and sorts them. Most of the commands in this section use 1412 buffer into records and then sorts them. Most of the commands in this
1389 this function. 1413 section use this function.
1390 1414
1391 To understand how @code{sort-subr} works, consider the whole accessible 1415 To understand how @code{sort-subr} works, consider the whole accessible
1392 portion of the buffer as being divided into disjoint pieces called 1416 portion of the buffer as being divided into disjoint pieces called
1393 @dfn{sort records}. The records may or may not be contiguous; they may 1417 @dfn{sort records}. The records may or may not be contiguous, but they
1394 not overlap. A portion of each sort record (perhaps all of it) is 1418 must not overlap. A portion of each sort record (perhaps all of it) is
1395 designated as the sort key. Sorting rearranges the records in order by 1419 designated as the sort key. Sorting rearranges the records in order by
1396 their sort keys. 1420 their sort keys.
1397 1421
1398 Usually, the records are rearranged in order of ascending sort key. 1422 Usually, the records are rearranged in order of ascending sort key.
1399 If the first argument to the @code{sort-subr} function, @var{reverse}, 1423 If the first argument to the @code{sort-subr} function, @var{reverse},
1443 @example 1467 @example
1444 @group 1468 @group
1445 ;; @r{Note that the first two lines of doc string} 1469 ;; @r{Note that the first two lines of doc string}
1446 ;; @r{are effectively one line when viewed by a user.} 1470 ;; @r{are effectively one line when viewed by a user.}
1447 (defun sort-lines (reverse beg end) 1471 (defun sort-lines (reverse beg end)
1448 "Sort lines in region alphabetically. 1472 "Sort lines in region alphabetically;\
1473 argument means descending order.
1449 Called from a program, there are three arguments: 1474 Called from a program, there are three arguments:
1450 @end group 1475 @end group
1451 @group 1476 @group
1452 REVERSE (non-nil means reverse order), 1477 REVERSE (non-nil means reverse order),\
1453 and BEG and END (the region to sort)." 1478 BEG and END (region to sort).
1479 The variable `sort-fold-case' determines\
1480 whether alphabetic case affects
1481 the sort order.
1482 @end group
1483 @group
1454 (interactive "P\nr") 1484 (interactive "P\nr")
1455 (save-restriction 1485 (save-restriction
1456 (narrow-to-region beg end) 1486 (narrow-to-region beg end)
1457 (goto-char (point-min)) 1487 (goto-char (point-min))
1458 (sort-subr reverse 1488 (sort-subr reverse
1476 (lambda () 1506 (lambda ()
1477 (skip-chars-forward "\n \t\f"))) 1507 (skip-chars-forward "\n \t\f")))
1478 'forward-paragraph) 1508 'forward-paragraph)
1479 @end group 1509 @end group
1480 @end example 1510 @end example
1511
1512 Markers pointing into any sort records are left with no useful
1513 position after @code{sort-subr} returns.
1481 @end defun 1514 @end defun
1482 1515
1483 @deffn Command sort-regexp-fields reverse record-regexp key-regexp start end 1516 @deffn Command sort-regexp-fields reverse record-regexp key-regexp start end
1484 This command sorts the region between @var{start} and @var{end} 1517 This command sorts the region between @var{start} and @var{end}
1485 alphabetically as specified by @var{record-regexp} and @var{key-regexp}. 1518 alphabetically as specified by @var{record-regexp} and @var{key-regexp}.
1489 Alphabetical sorting means that two sort keys are compared by 1522 Alphabetical sorting means that two sort keys are compared by
1490 comparing the first characters of each, the second characters of each, 1523 comparing the first characters of each, the second characters of each,
1491 and so on. If a mismatch is found, it means that the sort keys are 1524 and so on. If a mismatch is found, it means that the sort keys are
1492 unequal; the sort key whose character is less at the point of first 1525 unequal; the sort key whose character is less at the point of first
1493 mismatch is the lesser sort key. The individual characters are compared 1526 mismatch is the lesser sort key. The individual characters are compared
1494 according to their numerical values. Since Emacs uses the @sc{ASCII} 1527 according to their numerical character codes in the Emacs character set.
1495 character set, the ordering in that set determines alphabetical order.
1496 @c version 19 change
1497 1528
1498 The value of the @var{record-regexp} argument specifies how to divide 1529 The value of the @var{record-regexp} argument specifies how to divide
1499 the buffer into sort records. At the end of each record, a search is 1530 the buffer into sort records. At the end of each record, a search is
1500 done for this regular expression, and the text that matches it is the 1531 done for this regular expression, and the text that matches it is taken
1501 next record. For example, the regular expression @samp{^.+$}, which 1532 as the next record. For example, the regular expression @samp{^.+$},
1502 matches lines with at least one character besides a newline, would make 1533 which matches lines with at least one character besides a newline, would
1503 each such line into a sort record. @xref{Regular Expressions}, for a 1534 make each such line into a sort record. @xref{Regular Expressions}, for
1504 description of the syntax and meaning of regular expressions. 1535 a description of the syntax and meaning of regular expressions.
1505 1536
1506 The value of the @var{key-regexp} argument specifies what part of each 1537 The value of the @var{key-regexp} argument specifies what part of each
1507 record is the sort key. The @var{key-regexp} could match the whole 1538 record is the sort key. The @var{key-regexp} could match the whole
1508 record, or only a part. In the latter case, the rest of the record has 1539 record, or only a part. In the latter case, the rest of the record has
1509 no effect on the sorted order of records, but it is carried along when 1540 no effect on the sorted order of records, but it is carried along when
1611 1642
1612 The column functions convert between a character position (counting 1643 The column functions convert between a character position (counting
1613 characters from the beginning of the buffer) and a column position 1644 characters from the beginning of the buffer) and a column position
1614 (counting screen characters from the beginning of a line). 1645 (counting screen characters from the beginning of a line).
1615 1646
1616 A character counts according to the number of columns it occupies on 1647 These functions count each character according to the number of
1617 the screen. This means control characters count as occupying 2 or 4 1648 columns it occupies on the screen. This means control characters count
1618 columns, depending upon the value of @code{ctl-arrow}, and tabs count as 1649 as occupying 2 or 4 columns, depending upon the value of
1619 occupying a number of columns that depends on the value of 1650 @code{ctl-arrow}, and tabs count as occupying a number of columns that
1620 @code{tab-width} and on the column where the tab begins. @xref{Usual Display}. 1651 depends on the value of @code{tab-width} and on the column where the tab
1652 begins. @xref{Usual Display}.
1621 1653
1622 Column number computations ignore the width of the window and the 1654 Column number computations ignore the width of the window and the
1623 amount of horizontal scrolling. Consequently, a column value can be 1655 amount of horizontal scrolling. Consequently, a column value can be
1624 arbitrarily high. The first (or leftmost) column is numbered 0. 1656 arbitrarily high. The first (or leftmost) column is numbered 0.
1625 1657
1884 This line is indented twelve spaces. 1916 This line is indented twelve spaces.
1885 @point{}The quick brown fox jumped. 1917 @point{}The quick brown fox jumped.
1886 @end group 1918 @end group
1887 @end example 1919 @end example
1888 1920
1889 In this example, point is between the @samp{m} and @samp{p} of 1921 In this next example, point is between the @samp{m} and @samp{p} of
1890 @samp{jumped}: 1922 @samp{jumped}:
1891 1923
1892 @example 1924 @example
1893 @group 1925 @group
1894 This line is indented twelve spaces. 1926 This line is indented twelve spaces.
1908 @end example 1940 @end example
1909 @end deffn 1941 @end deffn
1910 1942
1911 @deffn Command indent-relative-maybe 1943 @deffn Command indent-relative-maybe
1912 @comment !!SourceFile indent.el 1944 @comment !!SourceFile indent.el
1913 This command indents the current line like the previous nonblank line. 1945 This command indents the current line like the previous nonblank line,
1914 It calls @code{indent-relative} with @code{t} as the @var{unindented-ok} 1946 by calling @code{indent-relative} with @code{t} as the
1915 argument. The return value is unpredictable. 1947 @var{unindented-ok} argument. The return value is unpredictable.
1916 1948
1917 If the previous nonblank line has no indent points beyond the current 1949 If the previous nonblank line has no indent points beyond the current
1918 column, this command does nothing. 1950 column, this command does nothing.
1919 @end deffn 1951 @end deffn
1920 1952
1931 affect the display of tab characters in the buffer (@pxref{Usual 1963 affect the display of tab characters in the buffer (@pxref{Usual
1932 Display}). Note that the @key{TAB} character as input uses this tab 1964 Display}). Note that the @key{TAB} character as input uses this tab
1933 stop feature only in a few major modes, such as Text mode. 1965 stop feature only in a few major modes, such as Text mode.
1934 1966
1935 @deffn Command tab-to-tab-stop 1967 @deffn Command tab-to-tab-stop
1936 This command inserts spaces or tabs up to the next tab stop column 1968 This command inserts spaces or tabs before point, up to the next tab
1937 defined by @code{tab-stop-list}. It searches the list for an element 1969 stop column defined by @code{tab-stop-list}. It searches the list for
1938 greater than the current column number, and uses that element as the 1970 an element greater than the current column number, and uses that element
1939 column to indent to. It does nothing if no such element is found. 1971 as the column to indent to. It does nothing if no such element is
1972 found.
1940 @end deffn 1973 @end deffn
1941 1974
1942 @defopt tab-stop-list 1975 @defopt tab-stop-list
1943 This variable is the list of tab stop columns used by 1976 This variable is the list of tab stop columns used by
1944 @code{tab-to-tab-stops}. The elements should be integers in increasing 1977 @code{tab-to-tab-stops}. The elements should be integers in increasing
2102 neighboring text. 2135 neighboring text.
2103 * Saving Properties:: Saving text properties in files, and reading 2136 * Saving Properties:: Saving text properties in files, and reading
2104 them back. 2137 them back.
2105 * Lazy Properties:: Computing text properties in a lazy fashion 2138 * Lazy Properties:: Computing text properties in a lazy fashion
2106 only when text is examined. 2139 only when text is examined.
2140 * Clickable Text:: Using text properties to make regions of text
2141 do something when you click on them.
2107 * Not Intervals:: Why text properties do not use 2142 * Not Intervals:: Why text properties do not use
2108 Lisp-visible text intervals. 2143 Lisp-visible text intervals.
2109 @end menu 2144 @end menu
2110 2145
2111 @node Examining Properties 2146 @node Examining Properties
2186 between @var{start} and @var{end} in the string or buffer @var{object}. 2221 between @var{start} and @var{end} in the string or buffer @var{object}.
2187 If @var{object} is @code{nil}, it defaults to the current buffer. 2222 If @var{object} is @code{nil}, it defaults to the current buffer.
2188 @end defun 2223 @end defun
2189 2224
2190 @defun add-text-properties start end props &optional object 2225 @defun add-text-properties start end props &optional object
2191 This function modifies the text properties for the text between 2226 This function adds or overrides text properties for the text between
2192 @var{start} and @var{end} in the string or buffer @var{object}. If 2227 @var{start} and @var{end} in the string or buffer @var{object}. If
2193 @var{object} is @code{nil}, it defaults to the current buffer. 2228 @var{object} is @code{nil}, it defaults to the current buffer.
2194 2229
2195 The argument @var{props} specifies which properties to change. It 2230 The argument @var{props} specifies which properties to add. It should
2196 should have the form of a property list (@pxref{Property Lists}): a list 2231 have the form of a property list (@pxref{Property Lists}): a list whose
2197 whose elements include the property names followed alternately by the 2232 elements include the property names followed alternately by the
2198 corresponding values. 2233 corresponding values.
2199 2234
2200 The return value is @code{t} if the function actually changed some 2235 The return value is @code{t} if the function actually changed some
2201 property's value; @code{nil} otherwise (if @var{props} is @code{nil} or 2236 property's value; @code{nil} otherwise (if @var{props} is @code{nil} or
2202 its values agree with those in the text). 2237 its values agree with those in the text).
2251 @example 2286 @example
2252 (set-text-properties @var{start} @var{end} nil) 2287 (set-text-properties @var{start} @var{end} nil)
2253 @end example 2288 @end example
2254 @end defun 2289 @end defun
2255 2290
2256 See also the function @code{buffer-substring-no-properties} 2291 See also the function @code{buffer-substring-no-properties}
2257 (@pxref{Buffer Contents}) which copies text from the buffer 2292 (@pxref{Buffer Contents}) which copies text from the buffer
2258 but does not copy its properties. 2293 but does not copy its properties.
2259 2294
2260 @node Property Search 2295 @node Property Search
2261 @subsection Property Search Functions 2296 @subsection Text Property Search Functions
2262 2297
2263 In typical use of text properties, most of the time several or many 2298 In typical use of text properties, most of the time several or many
2264 consecutive characters have the same value for a property. Rather than 2299 consecutive characters have the same value for a property. Rather than
2265 writing your programs to examine characters one by one, it is much 2300 writing your programs to examine characters one by one, it is much
2266 faster to process chunks of text that have the same property value. 2301 faster to process chunks of text that have the same property value.
2267 2302
2268 Here are functions you can use to do this. They use @code{eq} for 2303 Here are functions you can use to do this. They use @code{eq} for
2269 comparing property values. In all cases, @var{object} defaults to the 2304 comparing property values. In all cases, @var{object} defaults to the
2270 current buffer. 2305 current buffer.
2271 2306
2272 For high performance, it's very important to use the @var{limit} 2307 For high performance, it's very important to use the @var{limit}
2273 argument to these functions, especially the ones that search for a 2308 argument to these functions, especially the ones that search for a
2274 single property---otherwise, they may spend a long time scanning to the 2309 single property---otherwise, they may spend a long time scanning to the
2275 end of the buffer, if the property you are interested in does not change. 2310 end of the buffer, if the property you are interested in does not change.
2276 2311
2277 Remember that a position is always between two characters; the position 2312 These functions do not move point; instead, they return a position (or
2278 returned by these functions is between two characters with different 2313 @code{nil}). Remember that a position is always between two characters;
2279 properties. 2314 the position returned by these functions is between two characters with
2315 different properties.
2280 2316
2281 @defun next-property-change pos &optional object limit 2317 @defun next-property-change pos &optional object limit
2282 The function scans the text forward from position @var{pos} in the 2318 The function scans the text forward from position @var{pos} in the
2283 string or buffer @var{object} till it finds a change in some text 2319 string or buffer @var{object} till it finds a change in some text
2284 property, then returns the position of the change. In other words, it 2320 property, then returns the position of the change. In other words, it
2339 @var{pos} instead of forward. If the value is non-@code{nil}, it is a 2375 @var{pos} instead of forward. If the value is non-@code{nil}, it is a
2340 position less than or equal to @var{pos}; it equals @var{pos} only if 2376 position less than or equal to @var{pos}; it equals @var{pos} only if
2341 @var{limit} equals @var{pos}. 2377 @var{limit} equals @var{pos}.
2342 @end defun 2378 @end defun
2343 2379
2380 @tindex next-char-property-change
2381 @defun next-char-property-change position &optional limit
2382 This is like @code{next-property-change} except that it considers
2383 overlay properties as well as text properties. There is no @var{object}
2384 operand because this function operates only on the current buffer. It
2385 returns the next address at which either kind of property changes.
2386 @end defun
2387
2388 @tindex previous-char-property-change
2389 @defun previous-char-property-change position &optional limit
2390 This is like @code{next-char-property-change}, but scans back from
2391 @var{position} instead of forward.
2392 @end defun
2393
2344 @defun text-property-any start end prop value &optional object 2394 @defun text-property-any start end prop value &optional object
2345 This function returns non-@code{nil} if at least one character between 2395 This function returns non-@code{nil} if at least one character between
2346 @var{start} and @var{end} has a property @var{prop} whose value is 2396 @var{start} and @var{end} has a property @var{prop} whose value is
2347 @var{value}. More precisely, it returns the position of the first such 2397 @var{value}. More precisely, it returns the position of the first such
2348 character. Otherwise, it returns @code{nil}. 2398 character. Otherwise, it returns @code{nil}.
2352 for @var{object} is the current buffer. 2402 for @var{object} is the current buffer.
2353 @end defun 2403 @end defun
2354 2404
2355 @defun text-property-not-all start end prop value &optional object 2405 @defun text-property-not-all start end prop value &optional object
2356 This function returns non-@code{nil} if at least one character between 2406 This function returns non-@code{nil} if at least one character between
2357 @var{start} and @var{end} has a property @var{prop} whose value differs 2407 @var{start} and @var{end} does not have a property @var{prop} with value
2358 from @var{value}. More precisely, it returns the position of the 2408 @var{value}. More precisely, it returns the position of the first such
2359 first such character. Otherwise, it returns @code{nil}. 2409 character. Otherwise, it returns @code{nil}.
2360 2410
2361 The optional fifth argument, @var{object}, specifies the string or 2411 The optional fifth argument, @var{object}, specifies the string or
2362 buffer to scan. Positions are relative to @var{object}. The default 2412 buffer to scan. Positions are relative to @var{object}. The default
2363 for @var{object} is the current buffer. 2413 for @var{object} is the current buffer.
2364 @end defun 2414 @end defun
2382 @item face 2432 @item face
2383 @cindex face codes of text 2433 @cindex face codes of text
2384 @kindex face @r{(text property)} 2434 @kindex face @r{(text property)}
2385 You can use the property @code{face} to control the font and color of 2435 You can use the property @code{face} to control the font and color of
2386 text. Its value is a face name or a list of face names. @xref{Faces}, 2436 text. Its value is a face name or a list of face names. @xref{Faces},
2387 for more information. This feature may be temporary; in the future, we 2437 for more information.
2388 may replace it with other ways of specifying how to display text. 2438
2439 If the property value is a list, elements may also have the form
2440 @code{(foreground-color . @var{color-name})} or @code{(background-color
2441 . @var{color-name})}. These elements specify just the foreground color
2442 or just the background color; therefore, there is no need to create a
2443 face for each color that you want to use.
2444
2445 @xref{Font Lock Mode}, for information on how to update @code{face}
2446 properties automatically based on the contents of the text.
2389 2447
2390 @item mouse-face 2448 @item mouse-face
2391 @kindex mouse-face @r{(text property)} 2449 @kindex mouse-face @r{(text property)}
2392 The property @code{mouse-face} is used instead of @code{face} when the 2450 The property @code{mouse-face} is used instead of @code{face} when the
2393 mouse is on or near the character. For this purpose, ``near'' means 2451 mouse is on or near the character. For this purpose, ``near'' means
2395 @code{mouse-face} property value. 2453 @code{mouse-face} property value.
2396 2454
2397 @item local-map 2455 @item local-map
2398 @cindex keymap of character 2456 @cindex keymap of character
2399 @kindex local-map @r{(text property)} 2457 @kindex local-map @r{(text property)}
2400 You can specify a different keymap for a portion of the text by means of 2458 You can specify a different keymap for some of the text in a buffer by
2401 a @code{local-map} property. The property's value for the character 2459 means of the @code{local-map} property. The property's value for the
2402 after point, if non-@code{nil}, replaces the buffer's local map. 2460 character after point, if non-@code{nil}, is used for key lookup instead
2403 @xref{Active Keymaps}. 2461 of the buffer's local map. If the property value is a symbol, the
2462 symbol's function definition is used as the keymap. @xref{Active
2463 Keymaps}.
2464
2465 @item syntax-table
2466 The @code{syntax-table} property overrides what the syntax table says
2467 about this particular character. @xref{Syntax Properties}.
2404 2468
2405 @item read-only 2469 @item read-only
2406 @cindex read-only character 2470 @cindex read-only character
2407 @kindex read-only @r{(text property)} 2471 @kindex read-only @r{(text property)}
2408 If a character has the property @code{read-only}, then modifying that 2472 If a character has the property @code{read-only}, then modifying that
2487 (which may be the same function) and/or two @code{point-entered} 2551 (which may be the same function) and/or two @code{point-entered}
2488 functions (which may be the same function). In any case, all the 2552 functions (which may be the same function). In any case, all the
2489 @code{point-left} functions are called first, followed by all the 2553 @code{point-left} functions are called first, followed by all the
2490 @code{point-entered} functions. 2554 @code{point-entered} functions.
2491 2555
2492 A primitive function may examine characters at various positions 2556 It is possible using @code{char-after} to examine characters at various
2493 without moving point to those positions. Only an actual change in the 2557 positions without moving point to those positions. Only an actual
2494 value of point runs these hook functions. 2558 change in the value of point runs these hook functions.
2495 @end table 2559 @end table
2496 2560
2497 @defvar inhibit-point-motion-hooks 2561 @defvar inhibit-point-motion-hooks
2498 When this variable is non-@code{nil}, @code{point-left} and 2562 When this variable is non-@code{nil}, @code{point-left} and
2499 @code{point-entered} hooks are not run, and the @code{intangible} 2563 @code{point-entered} hooks are not run, and the @code{intangible}
2500 property has no effect. 2564 property has no effect. Do not set this variable globally; bind it with
2565 @code{let}.
2501 @end defvar 2566 @end defvar
2502 2567
2503 @node Format Properties 2568 @node Format Properties
2504 @subsection Formatted Text Properties 2569 @subsection Formatted Text Properties
2505 2570
2589 Insert the strings @var{strings}, just like the function 2654 Insert the strings @var{strings}, just like the function
2590 @code{insert-before-markers}, but inherit any sticky properties from the 2655 @code{insert-before-markers}, but inherit any sticky properties from the
2591 adjoining text. 2656 adjoining text.
2592 @end defun 2657 @end defun
2593 2658
2659 @xref{Insertion}, for the ordinary insertion functions which do not
2660 inherit.
2661
2594 @node Saving Properties 2662 @node Saving Properties
2595 @subsection Saving Text Properties in Files 2663 @subsection Saving Text Properties in Files
2596 @cindex text properties in files 2664 @cindex text properties in files
2597 @cindex saving text properties 2665 @cindex saving text properties
2598 2666
2599 You can save text properties in files, and restore text properties 2667 You can save text properties in files (along with the text itself),
2600 when inserting the files, using these two hooks: 2668 and restore the same text properties when visiting or inserting the
2669 files, using these two hooks:
2601 2670
2602 @defvar write-region-annotate-functions 2671 @defvar write-region-annotate-functions
2603 This variable's value is a list of functions for @code{write-region} to 2672 This variable's value is a list of functions for @code{write-region} to
2604 run to encode text properties in some fashion as annotations to the text 2673 run to encode text properties in some fashion as annotations to the text
2605 being written in the file. @xref{Writing to Files}. 2674 being written in the file. @xref{Writing to Files}.
2610 annotations to write in the file in addition to the text in the 2679 annotations to write in the file in addition to the text in the
2611 buffer. 2680 buffer.
2612 2681
2613 Each function should return a list of elements of the form 2682 Each function should return a list of elements of the form
2614 @code{(@var{position} . @var{string})}, where @var{position} is an 2683 @code{(@var{position} . @var{string})}, where @var{position} is an
2615 integer specifying the relative position in the text to be written, and 2684 integer specifying the relative position within the text to be written,
2616 @var{string} is the annotation to add there. 2685 and @var{string} is the annotation to add there.
2617 2686
2618 Each list returned by one of these functions must be already sorted in 2687 Each list returned by one of these functions must be already sorted in
2619 increasing order by @var{position}. If there is more than one function, 2688 increasing order by @var{position}. If there is more than one function,
2620 @code{write-region} merges the lists destructively into one sorted list. 2689 @code{write-region} merges the lists destructively into one sorted list.
2621 2690
2648 We invite users to write Lisp programs to store and retrieve text 2717 We invite users to write Lisp programs to store and retrieve text
2649 properties in files, using these hooks, and thus to experiment with 2718 properties in files, using these hooks, and thus to experiment with
2650 various data formats and find good ones. Eventually we hope users 2719 various data formats and find good ones. Eventually we hope users
2651 will produce good, general extensions we can install in Emacs. 2720 will produce good, general extensions we can install in Emacs.
2652 2721
2653 We suggest not trying to handle arbitrary Lisp objects as property 2722 We suggest not trying to handle arbitrary Lisp objects as text property
2654 names or property values---because a program that general is probably 2723 names or values---because a program that general is probably difficult
2655 difficult to write, and slow. Instead, choose a set of possible data 2724 to write, and slow. Instead, choose a set of possible data types that
2656 types that are reasonably flexible, and not too hard to encode. 2725 are reasonably flexible, and not too hard to encode.
2657 2726
2658 @xref{Format Conversion}, for a related feature. 2727 @xref{Format Conversion}, for a related feature.
2659 2728
2660 @c ??? In next edition, merge this info Format Conversion. 2729 @c ??? In next edition, merge this info Format Conversion.
2661 2730
2702 @code{buffer-access-fontify-functions} functions add this property, as 2771 @code{buffer-access-fontify-functions} functions add this property, as
2703 well as others, to the characters they operate on. That way, they avoid 2772 well as others, to the characters they operate on. That way, they avoid
2704 being called over and over for the same text. 2773 being called over and over for the same text.
2705 @end defvar 2774 @end defvar
2706 2775
2776 @node Clickable Text
2777 @subsection Defining Clickable Text
2778 @cindex clickable text
2779
2780 There are two ways to set up @dfn{clickable text} in a buffer.
2781 There are typically two parts of this: to make the text highlight
2782 when the mouse is over it, and to make a mouse button do something
2783 when you click it on that part of the text.
2784
2785 Highlighting is done with the @code{mouse-face} text property.
2786 Here is an example of how Dired does it:
2787
2788 @smallexample
2789 (condition-case nil
2790 (if (dired-move-to-filename)
2791 (put-text-property (point)
2792 (save-excursion
2793 (dired-move-to-end-of-filename)
2794 (point))
2795 'mouse-face 'highlight))
2796 (error nil))
2797 @end smallexample
2798
2799 @noindent
2800 The first two arguments to @code{put-text-property} specify the
2801 beginning and end of the text.
2802
2803 The usual way to make the mouse do something when you click it
2804 on this text is to define @code{mouse-2} in the major mode's
2805 keymap. The job of checking whether the click was on clickable text
2806 is done by the command definition. Here is how Dired does it:
2807
2808 @smallexample
2809 (defun dired-mouse-find-file-other-window (event)
2810 "In dired, visit the file or directory name you click on."
2811 (interactive "e")
2812 (let (file)
2813 (save-excursion
2814 (set-buffer (window-buffer (posn-window (event-end event))))
2815 (save-excursion
2816 (goto-char (posn-point (event-end event)))
2817 (setq file (dired-get-filename))))
2818 (select-window (posn-window (event-end event)))
2819 (find-file-other-window (file-name-sans-versions file t))))
2820 @end smallexample
2821
2822 @noindent
2823 The reason for the outer @code{save-excursion} construct is to avoid
2824 changing the current buffer; the reason for the inner one is to avoid
2825 permanently altering point in the buffer you click on. In this case,
2826 Dired uses the function @code{dired-get-filename} to determine which
2827 file to visit, based on the position found in the event.
2828
2829 Instead of defining a mouse command for the major mode, you can define
2830 a key binding for the clickable text itself, using the @code{local-map}
2831 text property:
2832
2833 @example
2834 (let ((map (make-sparse-keymap)))
2835 (define-key-binding map [mouse-2] 'operate-this-button)
2836 (put-text-property (point)
2837 (save-excursion
2838 (dired-move-to-end-of-filename)
2839 (point))
2840 'local-map map))
2841 @end example
2842
2843 @noindent
2844 This method makes it possible to define different commands for various
2845 clickable pieces of text. Also, the major mode definition (or the
2846 global definition) remains available for the rest of the text in the
2847 buffer.
2848
2707 @node Not Intervals 2849 @node Not Intervals
2708 @subsection Why Text Properties are not Intervals 2850 @subsection Why Text Properties are not Intervals
2709 @cindex intervals 2851 @cindex intervals
2710 2852
2711 Some editors that support adding attributes to text in the buffer do 2853 Some editors that support adding attributes to text in the buffer do
2742 However, it is easy to arrange for editing to behave consistently for 2884 However, it is easy to arrange for editing to behave consistently for
2743 questions of the form, ``What are the properties of this character?'' 2885 questions of the form, ``What are the properties of this character?''
2744 So we have decided these are the only questions that make sense; we have 2886 So we have decided these are the only questions that make sense; we have
2745 not implemented asking questions about where intervals start or end. 2887 not implemented asking questions about where intervals start or end.
2746 2888
2747 In practice, you can usually use the property search functions in 2889 In practice, you can usually use the text property search functions in
2748 place of explicit interval boundaries. You can think of them as finding 2890 place of explicit interval boundaries. You can think of them as finding
2749 the boundaries of intervals, assuming that intervals are always 2891 the boundaries of intervals, assuming that intervals are always
2750 coalesced whenever possible. @xref{Property Search}. 2892 coalesced whenever possible. @xref{Property Search}.
2751 2893
2752 Emacs also provides explicit intervals as a presentation feature; see 2894 Emacs also provides explicit intervals as a presentation feature; see
2820 Thus, there are 255 possible registers. A register is designated in 2962 Thus, there are 255 possible registers. A register is designated in
2821 Emacs Lisp by a character that is its name. 2963 Emacs Lisp by a character that is its name.
2822 2964
2823 The functions in this section return unpredictable values unless 2965 The functions in this section return unpredictable values unless
2824 otherwise stated. 2966 otherwise stated.
2825 @c Will change in version 19
2826 2967
2827 @defvar register-alist 2968 @defvar register-alist
2828 This variable is an alist of elements of the form @code{(@var{name} . 2969 This variable is an alist of elements of the form @code{(@var{name} .
2829 @var{contents})}. Normally, there is one element for each Emacs 2970 @var{contents})}. Normally, there is one element for each Emacs
2830 register that has been used. 2971 register that has been used.
2831 2972
2832 The object @var{name} is a character (an integer) identifying the 2973 The object @var{name} is a character (an integer) identifying the
2833 register. The object @var{contents} is a string, marker, or list 2974 register. The object @var{contents} is a string, marker, window
2834 representing the register contents. A string represents text stored in 2975 configuration, frame configuration, or list representing the register
2835 the register. A marker represents a position. A list represents a 2976 contents. A string represents text stored in the register. A marker
2836 rectangle; its elements are strings, one per line of the rectangle. 2977 represents a position. A list represents a rectangle; its elements are
2978 strings, one per line of the rectangle.
2837 @end defvar 2979 @end defvar
2838 2980
2839 @defun get-register reg 2981 @defun get-register reg
2840 This function returns the contents of the register 2982 This function returns the contents of the register
2841 @var{reg}, or @code{nil} if it has no contents. 2983 @var{reg}, or @code{nil} if it has no contents.
2961 data if they do anything that uses regular expressions; otherwise, they 3103 data if they do anything that uses regular expressions; otherwise, they
2962 will interfere in bizarre ways with the editing operations that call 3104 will interfere in bizarre ways with the editing operations that call
2963 them. 3105 them.
2964 3106
2965 @defvar before-change-functions 3107 @defvar before-change-functions
2966 This variable holds a list of a functions to call before any buffer 3108 This variable holds a list of functions to call before any buffer
2967 modification. Each function gets two arguments, the beginning and end 3109 modification. Each function gets two arguments, the beginning and end
2968 of the region that is about to change, represented as integers. The 3110 of the region that is about to change, represented as integers. The
2969 buffer that is about to change is always the current buffer. 3111 buffer that is about to change is always the current buffer.
2970 @end defvar 3112 @end defvar
2971 3113
2972 @defvar after-change-functions 3114 @defvar after-change-functions
2973 This variable holds a list of a functions to call after any buffer 3115 This variable holds a list of functions to call after any buffer
2974 modification. Each function receives three arguments: the beginning and 3116 modification. Each function receives three arguments: the beginning and
2975 end of the region just changed, and the length of the text that existed 3117 end of the region just changed, and the length of the text that existed
2976 before the change. All three arguments are integers. The buffer that's 3118 before the change. All three arguments are integers. The buffer that's
2977 about to change is always the current buffer. 3119 about to change is always the current buffer.
2978 3120
2982 difference between the first two arguments. If you want the length 3124 difference between the first two arguments. If you want the length
2983 in @emph{characters} of the text before the change, you should use 3125 in @emph{characters} of the text before the change, you should use
2984 a @code{before-change-functions} function that calls @code{chars-in-region} 3126 a @code{before-change-functions} function that calls @code{chars-in-region}
2985 (@pxref{Chars and Bytes}). 3127 (@pxref{Chars and Bytes}).
2986 @end defvar 3128 @end defvar
3129
3130 @tindex combine-after-change-calls
3131 @defmac combine-after-change-calls body...
3132 The macro executes @var{body} normally, but arranges to call the
3133 after-change functions just once for a series of several changes---if
3134 that seems safe.
3135
3136 If a program makes several text changes in the same area of the buffer,
3137 using the macro @code{combine-after-change-calls} around that part of
3138 the program can make it run considerably faster when after-change hooks
3139 are in use.
3140
3141 @strong{Warning:} You must not alter the values of
3142 @code{after-change-functions} and @code{after-change-function} within
3143 the body of a @code{combine-after-change-calls} form.
3144 @end defmac
2987 3145
2988 @defvar before-change-function 3146 @defvar before-change-function
2989 This obsolete variable holds one function to call before any buffer 3147 This obsolete variable holds one function to call before any buffer
2990 modification (or @code{nil} for no function). It is called just like 3148 modification (or @code{nil} for no function). It is called just like
2991 the functions in @code{before-change-functions}. 3149 the functions in @code{before-change-functions}.