blob: 2c6674f05189948c1551a27120db53ebd265a7ea [file] [log] [blame]
Bram Moolenaar8024f932020-01-14 19:29:13 +01001*textprop.txt* For Vim version 8.2. Last change: 2020 Jan 10
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
Bram Moolenaara6c27c42019-05-09 19:16:22 +02007Displaying text with properties attached. *textprop* *text-properties*
Bram Moolenaar98aefe72018-12-13 22:20:09 +01008
Bram Moolenaar98aefe72018-12-13 22:20:09 +01009
101. Introduction |text-prop-intro|
112. Functions |text-prop-functions|
Bram Moolenaard09091d2019-01-17 16:07:22 +0100123. When text changes |text-prop-changes|
Bram Moolenaar98aefe72018-12-13 22:20:09 +010013
14
Bram Moolenaar98aefe72018-12-13 22:20:09 +010015{not able to use text properties when the |+textprop| feature was
16disabled at compile time}
17
18==============================================================================
191. Introduction *text-prop-intro*
20
21Text properties can be attached to text in a buffer. They will move with the
22text: If lines are deleted or inserted the properties move with the text they
23are attached to. Also when inserting/deleting text in the line before the
24text property. And when inserting/deleting text inside the text property, it
25will increase/decrease in size.
26
27The main use for text properties is to highlight text. This can be seen as a
28replacement for syntax highlighting. Instead of defining patterns to match
29the text, the highlighting is set by a script, possibly using the output of an
30external parser. This only needs to be done once, not every time when
31redrawing the screen, thus can be much faster, after the initial cost of
32attaching the text properties.
33
34Text properties can also be used for other purposes to identify text. For
35example, add a text property on a function name, so that a search can be
36defined to jump to the next/previous function.
37
38A text property is attached at a specific line and column, and has a specified
39length. The property can span multiple lines.
40
41A text property has these fields:
42 "id" a number to be used as desired
43 "type" the name of a property type
44
45
46Property Types ~
47 *E971*
Bram Moolenaar664f3cf2019-12-07 16:03:51 +010048A text property normally has the name of a property type, which defines
Bram Moolenaar98aefe72018-12-13 22:20:09 +010049how to highlight the text. The property type can have these entries:
50 "highlight" name of the highlight group to use
Bram Moolenaar574ee7b2019-11-13 23:04:29 +010051 "combine" when omitted or TRUE the text property highlighting is
52 combined with any syntax highlighting; when FALSE the
Bram Moolenaarde24a872019-05-05 15:48:00 +020053 text property highlighting replaces the syntax
54 highlighting
Bram Moolenaar98aefe72018-12-13 22:20:09 +010055 "priority" when properties overlap, the one with the highest
56 priority will be used.
57 "start_incl" when TRUE inserts at the start position will be
58 included in the text property
59 "end_incl" when TRUE inserts at the end position will be
60 included in the text property
61
62
63Example ~
64
65Suppose line 11 in a buffer has this text (excluding the indent):
66
67 The number 123 is smaller than 4567.
68
Bram Moolenaar4c05fa02019-01-01 15:32:17 +010069To highlight the numbers in this text: >
Bram Moolenaar98aefe72018-12-13 22:20:09 +010070 call prop_type_add('number', {'highlight': 'Constant'})
Bram Moolenaar9d87a372018-12-18 21:41:50 +010071 call prop_add(11, 12, {'length': 3, 'type': 'number'})
72 call prop_add(11, 32, {'length': 4, 'type': 'number'})
Bram Moolenaar98aefe72018-12-13 22:20:09 +010073
Bram Moolenaar4c05fa02019-01-01 15:32:17 +010074Try inserting or deleting lines above the text, you will see that the text
75properties stick to the text, thus the line number is adjusted as needed.
76
Bram Moolenaar98aefe72018-12-13 22:20:09 +010077Setting "start_incl" and "end_incl" is useful when white space surrounds the
78text, e.g. for a function name. Using false is useful when the text starts
79and/or ends with a specific character, such as the quote surrounding a string.
80
81 func FuncName(arg) ~
82 ^^^^^^^^ property with start_incl and end_incl set
83
84 var = "text"; ~
85 ^^^^^^ property with start_incl and end_incl not set
86
87Nevertheless, when text is inserted or deleted the text may need to be parsed
Bram Moolenaar9d87a372018-12-18 21:41:50 +010088and the text properties updated. But this can be done asynchronously.
Bram Moolenaar98aefe72018-12-13 22:20:09 +010089
Bram Moolenaar96f45c02019-10-26 19:53:45 +020090
91Internal error *E967*
92
93If you see E967, please report the bug. You can do this at Github:
94https://github.com/vim/vim/issues/new
95
Bram Moolenaar98aefe72018-12-13 22:20:09 +010096==============================================================================
972. Functions *text-prop-functions*
98
99Manipulating text property types:
100
101prop_type_add({name}, {props}) define a new property type
102prop_type_change({name}, {props}) change an existing property type
103prop_type_delete({name} [, {props}]) delete a property type
104prop_type_get([{name} [, {props}]) get property type values
105prop_type_list([{props}]) get list of property types
106
107
108Manipulating text properties:
109
110prop_add({lnum}, {col}, {props}) add a text property
Bram Moolenaarc8c88492018-12-27 23:59:26 +0100111prop_clear({lnum} [, {lnum-end} [, {bufnr}]])
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100112 remove all text properties
113prop_find({props} [, {direction}]) search for a text property
114prop_list({lnum} [, {props}) text properties in {lnum}
Bram Moolenaarc8c88492018-12-27 23:59:26 +0100115prop_remove({props} [, {lnum} [, {lnum-end}]])
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100116 remove a text property
117
Bram Moolenaared997ad2019-07-21 16:42:00 +0200118 *prop_add()* *E965*
119prop_add({lnum}, {col}, {props})
120 Attach a text property at position {lnum}, {col}. {col} is
121 counted in bytes, use one for the first column.
122 If {lnum} is invalid an error is given. *E966*
123 If {col} is invalid an error is given. *E964*
124
125 {props} is a dictionary with these fields:
126 length length of text in bytes, can only be used
127 for a property that does not continue in
128 another line; can be zero
129 end_lnum line number for the end of text
130 end_col column just after the text; not used when
131 "length" is present; when {col} and "end_col"
132 are equal, and "end_lnum" is omitted or equal
133 to {lnum}, this is a zero-width text property
134 bufnr buffer to add the property to; when omitted
135 the current buffer is used
136 id user defined ID for the property; when omitted
137 zero is used
138 type name of the text property type
139 All fields except "type" are optional.
140
141 It is an error when both "length" and "end_lnum" or "end_col"
142 are given. Either use "length" or "end_col" for a property
143 within one line, or use "end_lnum" and "end_col" for a
144 property that spans more than one line.
145 When neither "length" nor "end_col" are given the property
146 will be zero-width. That means it will not be highlighted but
147 will move with the text, as a kind of mark.
148 The property can end exactly at the last character of the
149 text, or just after it. In the last case, if text is appended
150 to the line, the text property size will increase, also when
151 the property type does not have "end_incl" set.
152
153 "type" will first be looked up in the buffer the property is
154 added to. When not found, the global property types are used.
155 If not found an error is given.
156
Bram Moolenaara5a78822019-09-04 21:57:18 +0200157 Can also be used as a |method|: >
158 GetLnum()->prop_add(col, props)
159
Bram Moolenaared997ad2019-07-21 16:42:00 +0200160
161prop_clear({lnum} [, {lnum-end} [, {props}]]) *prop_clear()*
162 Remove all text properties from line {lnum}.
163 When {lnum-end} is given, remove all text properties from line
164 {lnum} to {lnum-end} (inclusive).
165
166 When {props} contains a "bufnr" item use this buffer,
167 otherwise use the current buffer.
168
Bram Moolenaara5a78822019-09-04 21:57:18 +0200169 Can also be used as a |method|: >
170 GetLnum()->prop_clear()
171<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200172 *prop_find()*
173prop_find({props} [, {direction}])
Bram Moolenaared997ad2019-07-21 16:42:00 +0200174 Search for a text property as specified with {props}:
175 id property with this ID
176 type property with this type name
177 bufnr buffer to search in; when present a
178 start position with "lnum" and "col"
179 must be given; when omitted the
180 current buffer is used
181 lnum start in this line (when omitted start
182 at the cursor)
183 col start at this column (when omitted
184 and "lnum" is given: use column 1,
185 otherwise start at the cursor)
186 skipstart do not look for a match at the start
187 position
188
189 {direction} can be "f" for forward and "b" for backward. When
190 omitted forward search is performed.
191
192 If a match is found then a Dict is returned with the entries
193 as with prop_list(), and additionally an "lnum" entry.
194 If no match is found then an empty Dict is returned.
195
Bram Moolenaared997ad2019-07-21 16:42:00 +0200196
197prop_list({lnum} [, {props}]) *prop_list()*
198 Return a List with all text properties in line {lnum}.
199
200 When {props} contains a "bufnr" item, use this buffer instead
201 of the current buffer.
202
203 The properties are ordered by starting column and priority.
204 Each property is a Dict with these entries:
205 col starting column
206 length length in bytes, one more if line break is
207 included
208 id property ID
209 type name of the property type, omitted if
210 the type was deleted
211 start when TRUE property starts in this line
212 end when TRUE property ends in this line
213
214 When "start" is zero the property started in a previous line,
215 the current one is a continuation.
216 When "end" is zero the property continues in the next line.
217 The line break after this line is included.
218
Bram Moolenaara5a78822019-09-04 21:57:18 +0200219 Can also be used as a |method|: >
220 GetLnum()->prop_list()
221<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200222 *prop_remove()* *E968*
223prop_remove({props} [, {lnum} [, {lnum-end}]])
224 Remove a matching text property from line {lnum}. When
225 {lnum-end} is given, remove matching text properties from line
226 {lnum} to {lnum-end} (inclusive).
227 When {lnum} is omitted remove matching text properties from
228 all lines.
229
230 {props} is a dictionary with these fields:
231 id remove text properties with this ID
232 type remove text properties with this type name
233 bufnr use this buffer instead of the current one
234 all when TRUE remove all matching text properties,
235 not just the first one
236 A property matches when either "id" or "type" matches.
237 If buffer "bufnr" does not exist you get an error message.
238 If buffer "bufnr" is not loaded then nothing happens.
239
240 Returns the number of properties that were removed.
241
Bram Moolenaara5a78822019-09-04 21:57:18 +0200242 Can also be used as a |method|: >
243 GetProps()->prop_remove()
244
Bram Moolenaared997ad2019-07-21 16:42:00 +0200245
246prop_type_add({name}, {props}) *prop_type_add()* *E969* *E970*
247 Add a text property type {name}. If a property type with this
Bram Moolenaar06fe74a2019-08-31 16:20:32 +0200248 name already exists an error is given. Nothing is returned.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200249 {props} is a dictionary with these optional fields:
250 bufnr define the property only for this buffer; this
251 avoids name collisions and automatically
252 clears the property types when the buffer is
253 deleted.
254 highlight name of highlight group to use
255 priority when a character has multiple text
256 properties the one with the highest priority
257 will be used; negative values can be used, the
258 default priority is zero
259 combine when TRUE combine the highlight with any
260 syntax highlight; when omitted or FALSE syntax
261 highlight will not be used
262 start_incl when TRUE inserts at the start position will
263 be included in the text property
264 end_incl when TRUE inserts at the end position will be
265 included in the text property
266
Bram Moolenaara5a78822019-09-04 21:57:18 +0200267 Can also be used as a |method|: >
268 GetPropName()->prop_type_add(props)
Bram Moolenaared997ad2019-07-21 16:42:00 +0200269
270prop_type_change({name}, {props}) *prop_type_change()*
271 Change properties of an existing text property type. If a
272 property with this name does not exist an error is given.
273 The {props} argument is just like |prop_type_add()|.
274
Bram Moolenaara5a78822019-09-04 21:57:18 +0200275 Can also be used as a |method|: >
276 GetPropName()->prop_type_change(props)
Bram Moolenaared997ad2019-07-21 16:42:00 +0200277
278prop_type_delete({name} [, {props}]) *prop_type_delete()*
279 Remove the text property type {name}. When text properties
280 using the type {name} are still in place, they will not have
281 an effect and can no longer be removed by name.
282
283 {props} can contain a "bufnr" item. When it is given, delete
284 a property type from this buffer instead of from the global
285 property types.
286
287 When text property type {name} is not found there is no error.
288
Bram Moolenaara5a78822019-09-04 21:57:18 +0200289 Can also be used as a |method|: >
290 GetPropName()->prop_type_delete()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200291
292prop_type_get([{name} [, {props}]) *prop_type_get()*
293 Returns the properties of property type {name}. This is a
294 dictionary with the same fields as was given to
295 prop_type_add().
296 When the property type {name} does not exist, an empty
297 dictionary is returned.
298
299 {props} can contain a "bufnr" item. When it is given, use
300 this buffer instead of the global property types.
301
Bram Moolenaara5a78822019-09-04 21:57:18 +0200302 Can also be used as a |method|: >
303 GetPropName()->prop_type_get()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200304
305prop_type_list([{props}]) *prop_type_list()*
306 Returns a list with all property type names.
307
308 {props} can contain a "bufnr" item. When it is given, use
309 this buffer instead of the global property types.
310
Bram Moolenaared997ad2019-07-21 16:42:00 +0200311
Bram Moolenaard09091d2019-01-17 16:07:22 +0100312==============================================================================
3133. When text changes *text-prop-changes*
314
315Vim will do its best to keep the text properties on the text where it was
316attached. When inserting or deleting text the properties after the change
317will move accordingly.
318
319When text is deleted and a text property no longer includes any text, it is
320deleted. However, a text property that was defined as zero-width will remain,
321unless the whole line is deleted.
Bram Moolenaar30e9b3c2019-09-07 16:24:12 +0200322 *E275*
Bram Moolenaar45311b52019-08-13 22:27:32 +0200323When a buffer is unloaded, all the text properties are gone. There is no way
324to store the properties in a file. You can only re-create them. When a
325buffer is hidden the text is preserved and so are the text properties. It is
326not possible to add text properties to an unloaded buffer.
Bram Moolenaard09091d2019-01-17 16:07:22 +0100327
328When using replace mode, the text properties stay on the same character
329positions, even though the characters themselves change.
330
Bram Moolenaar68e65602019-05-26 21:33:31 +0200331To update text properties after the text was changed, install a callback with
332`listener_add()`. E.g, if your plugin does spell checking, you can have the
333callback update spelling mistakes in the changed text. Vim will move the
334properties below the changed text, so that they still highlight the same text,
335thus you don't need to update these.
336
Bram Moolenaard09091d2019-01-17 16:07:22 +0100337
Bram Moolenaara6c27c42019-05-09 19:16:22 +0200338Text property columns are not updated: ~
Bram Moolenaard09091d2019-01-17 16:07:22 +0100339
340- When setting the line with |setline()| or through an interface, such as Lua,
Bram Moolenaara6c27c42019-05-09 19:16:22 +0200341 Tcl or Python. Vim does not know what text got inserted or deleted.
Bram Moolenaard09091d2019-01-17 16:07:22 +0100342
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100343
344 vim:tw=78:ts=8:noet:ft=help:norl: