blob: 9a148455e0ff1e45bc89c7faf432212fb6b47e0a [file] [log] [blame]
Bram Moolenaareb490412022-06-28 13:44:46 +01001*textprop.txt* For Vim version 9.0. Last change: 2021 Nov 23
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
Bram Moolenaarcb80aa22020-10-26 21:12:46 +0100104prop_type_get({name} [, {props}]) get property type values
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100105prop_type_list([{props}]) get list of property types
106
107
108Manipulating text properties:
109
110prop_add({lnum}, {col}, {props}) add a text property
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200111prop_add_list({props}, [[{lnum}, {col}, {end-lnum}, {end-col}], ...])
112 add a text property at multiple
113 positions.
Bram Moolenaarc8c88492018-12-27 23:59:26 +0100114prop_clear({lnum} [, {lnum-end} [, {bufnr}]])
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100115 remove all text properties
116prop_find({props} [, {direction}]) search for a text property
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200117prop_list({lnum} [, {props}]) text properties in {lnum}
Bram Moolenaarc8c88492018-12-27 23:59:26 +0100118prop_remove({props} [, {lnum} [, {lnum-end}]])
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100119 remove a text property
120
Bram Moolenaared997ad2019-07-21 16:42:00 +0200121 *prop_add()* *E965*
122prop_add({lnum}, {col}, {props})
123 Attach a text property at position {lnum}, {col}. {col} is
124 counted in bytes, use one for the first column.
125 If {lnum} is invalid an error is given. *E966*
126 If {col} is invalid an error is given. *E964*
127
128 {props} is a dictionary with these fields:
129 length length of text in bytes, can only be used
130 for a property that does not continue in
131 another line; can be zero
Bram Moolenaard2ea7cf2021-05-30 20:54:13 +0200132 end_lnum line number for the end of text (inclusive)
Bram Moolenaared997ad2019-07-21 16:42:00 +0200133 end_col column just after the text; not used when
134 "length" is present; when {col} and "end_col"
135 are equal, and "end_lnum" is omitted or equal
136 to {lnum}, this is a zero-width text property
137 bufnr buffer to add the property to; when omitted
138 the current buffer is used
Bram Moolenaarb17893a2020-03-14 08:19:51 +0100139 id user defined ID for the property; must be a
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100140 number, should be positive; when using "text"
141 then "id" must not be present and will be set
142 automatically to a negative number; otherwise
143 zero is used
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100144 text text to be displayed before {col}, or after the
145 line if {col} is zero
Bram Moolenaare1f3fd12022-08-15 18:51:32 +0100146 *E1294*
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100147 text_align when "text" is present and {col} is zero
148 specifies where to display the text:
149 after after the end of the line
Bram Moolenaare1f3fd12022-08-15 18:51:32 +0100150 right right aligned in the window (unless
151 the text wraps to the next screen
152 line)
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100153 below in the next screen line
Bram Moolenaare1f3fd12022-08-15 18:51:32 +0100154 When omitted "after" is used. Only one
155 "right" property can fit in earch line.
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100156 text_wrap when "text" is present and {col} is zero,
157 specifies what happens if the text doesn't
158 fit:
159 wrap wrap the text to the next line
160 truncate truncate the text to make it fit
161 When omitted "truncate" is used.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200162 type name of the text property type
163 All fields except "type" are optional.
164
165 It is an error when both "length" and "end_lnum" or "end_col"
166 are given. Either use "length" or "end_col" for a property
167 within one line, or use "end_lnum" and "end_col" for a
168 property that spans more than one line.
169 When neither "length" nor "end_col" are given the property
Bram Moolenaarbc93ceb2020-02-26 13:36:21 +0100170 will be zero-width. That means it will move with the text, as
171 a kind of mark. One character will be highlighted, if the
172 type specifies highlighting.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200173 The property can end exactly at the last character of the
174 text, or just after it. In the last case, if text is appended
175 to the line, the text property size will increase, also when
176 the property type does not have "end_incl" set.
177
178 "type" will first be looked up in the buffer the property is
179 added to. When not found, the global property types are used.
180 If not found an error is given.
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100181 *virtual-text*
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100182 When "text" is used and the column is non-zero then this text
183 will be displayed at the start location of the text property
184 after the text. The text of the buffer line will be shifted
185 to make room. This is called "virtual text".
186 When the column is zero the virtual text will appear after the
187 buffer text. The "text_align" and "text_wrap" arguments
188 determine how it is displayed.
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100189 The text will be displayed but it is not part of the actual
190 buffer line, the cursor cannot be placed on it. A mouse click
191 in the text will move the cursor to the first character after
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100192 the text, or the last character of the line.
Bram Moolenaare1f3fd12022-08-15 18:51:32 +0100193 Any Tab and other control character in the text will be
194 changed to a space (Rationale: otherwise the size of the text
195 is difficult to compute).
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100196 A negative "id" will be chosen and is returned. Once a
197 property with "text" has been added for a buffer then using a
Bram Moolenaar2ecbe532022-07-29 21:36:21 +0100198 negative "id" for any other property will give an error:
199 *E1293*
Bram Moolenaarb7963df2022-07-31 17:12:43 +0100200 Make sure to use a highlight that makes clear to the user that
201 this is virtual text, otherwise it will be very confusing that
202 the text cannot be edited.
203 To separate the virtual text from the buffer text prepend
204 and/or append spaces to the "text" field.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200205
Bram Moolenaara5a78822019-09-04 21:57:18 +0200206 Can also be used as a |method|: >
207 GetLnum()->prop_add(col, props)
Yegappan Lakshmanan820d5522021-09-17 21:07:35 +0200208<
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200209 *prop_add_list()*
210prop_add_list({props}, [[{lnum}, {col}, {end-lnum}, {end-col}], ...])
211 Similar to prop_add(), but attaches a text property at
212 multiple positions in a buffer.
213
214 {props} is a dictionary with these fields:
215 bufnr buffer to add the property to; when omitted
216 the current buffer is used
217 id user defined ID for the property; must be a
218 number; when omitted zero is used
219 type name of the text property type
220 All fields except "type" are optional.
221
222 The second argument is a List of Lists where each list
223 specifies the starting and ending position of the text. The
224 first two items {lnum} and {col} specify the starting position
225 of the text where the property will be attached and the last
226 two items {end-lnum} and {end-col} specify the position just
227 after the text.
228
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100229 It is not possible to add a text property with a "text" field
230 here.
231
Yegappan Lakshmananccfb7c62021-08-16 21:39:09 +0200232 Example:
233 call prop_add_list(#{type: 'MyProp', id: 2},
234 \ [[1, 4, 1, 7],
235 \ [1, 15, 1, 20],
236 \ [2, 30, 3, 30]]
237
238 Can also be used as a |method|: >
239 GetProp()->prop_add_list([[1, 1, 1, 2], [1, 4, 1, 8]])
240
Bram Moolenaared997ad2019-07-21 16:42:00 +0200241
242prop_clear({lnum} [, {lnum-end} [, {props}]]) *prop_clear()*
243 Remove all text properties from line {lnum}.
244 When {lnum-end} is given, remove all text properties from line
245 {lnum} to {lnum-end} (inclusive).
246
247 When {props} contains a "bufnr" item use this buffer,
248 otherwise use the current buffer.
249
Bram Moolenaara5a78822019-09-04 21:57:18 +0200250 Can also be used as a |method|: >
251 GetLnum()->prop_clear()
252<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200253 *prop_find()*
254prop_find({props} [, {direction}])
Bram Moolenaared997ad2019-07-21 16:42:00 +0200255 Search for a text property as specified with {props}:
256 id property with this ID
257 type property with this type name
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100258 both "id" and "type" must both match
Bram Moolenaared997ad2019-07-21 16:42:00 +0200259 bufnr buffer to search in; when present a
260 start position with "lnum" and "col"
261 must be given; when omitted the
262 current buffer is used
263 lnum start in this line (when omitted start
264 at the cursor)
265 col start at this column (when omitted
266 and "lnum" is given: use column 1,
267 otherwise start at the cursor)
268 skipstart do not look for a match at the start
269 position
270
Bram Moolenaar24f21fd2021-03-27 22:07:29 +0100271 A property matches when either "id" or "type" matches.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200272 {direction} can be "f" for forward and "b" for backward. When
273 omitted forward search is performed.
274
275 If a match is found then a Dict is returned with the entries
276 as with prop_list(), and additionally an "lnum" entry.
277 If no match is found then an empty Dict is returned.
278
Bram Moolenaared997ad2019-07-21 16:42:00 +0200279
280prop_list({lnum} [, {props}]) *prop_list()*
Yegappan Lakshmanane0216622021-11-23 11:46:32 +0000281 Returns a List with all the text properties in line {lnum}.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200282
Yegappan Lakshmanane0216622021-11-23 11:46:32 +0000283 The following optional items are supported in {props}:
284 bufnr use this buffer instead of the current buffer
285 end_lnum return text properties in all the lines
286 between {lnum} and {end_lnum} (inclusive).
287 A negative value is used as an offset from the
288 last buffer line; -1 refers to the last buffer
289 line.
290 types List of property type names. Return only text
291 properties that match one of the type names.
292 ids List of property identifiers. Return only text
293 properties with one of these identifiers.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200294
295 The properties are ordered by starting column and priority.
296 Each property is a Dict with these entries:
Yegappan Lakshmanane0216622021-11-23 11:46:32 +0000297 lnum starting line number. Present only when
298 returning text properties between {lnum} and
299 {end_lnum}.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200300 col starting column
301 length length in bytes, one more if line break is
302 included
303 id property ID
304 type name of the property type, omitted if
305 the type was deleted
Martin Tournoije2390c72021-07-28 13:30:16 +0200306 type_bufnr buffer number for which this type was defined;
307 0 if the type is global
Bram Moolenaared997ad2019-07-21 16:42:00 +0200308 start when TRUE property starts in this line
309 end when TRUE property ends in this line
310
311 When "start" is zero the property started in a previous line,
312 the current one is a continuation.
313 When "end" is zero the property continues in the next line.
314 The line break after this line is included.
315
Yegappan Lakshmanane0216622021-11-23 11:46:32 +0000316 Returns an empty list on error.
317
318 Examples:
319 " get text properties placed in line 5
320 echo prop_list(5)
321 " get text properties placed in line 20 in buffer 4
322 echo prop_list(20, {'bufnr': 4})
323 " get all the text properties between line 1 and 20
324 echo prop_list(1, {'end_lnum': 20})
325 " get all the text properties of type 'myprop'
326 echo prop_list(1, {'types': ['myprop'],
327 \ 'end_lnum': -1})
328 " get all the text properties of type 'prop1' or 'prop2'
329 echo prop_list(1, {'types': ['prop1', 'prop2'],
330 \ 'end_lnum': -1})
331 " get all the text properties with ID 8
332 echo prop_list(1, {'ids': [8], 'end_lnum': line('$')})
333 " get all the text properties with ID 10 and 20
334 echo prop_list(1, {'ids': [10, 20], 'end_lnum': -1})
335 " get text properties with type 'myprop' and ID 100
336 " in buffer 4.
337 echo prop_list(1, {'bufnr': 4, 'types': ['myprop'],
338 \ 'ids': [100], 'end_lnum': -1})
339
Bram Moolenaara5a78822019-09-04 21:57:18 +0200340 Can also be used as a |method|: >
341 GetLnum()->prop_list()
342<
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200343 *prop_remove()* *E968* *E860*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200344prop_remove({props} [, {lnum} [, {lnum-end}]])
345 Remove a matching text property from line {lnum}. When
346 {lnum-end} is given, remove matching text properties from line
347 {lnum} to {lnum-end} (inclusive).
348 When {lnum} is omitted remove matching text properties from
Bram Moolenaard2ea7cf2021-05-30 20:54:13 +0200349 all lines (this requires going over all lines, thus will be a
350 bit slow for a buffer with many lines).
Bram Moolenaared997ad2019-07-21 16:42:00 +0200351
352 {props} is a dictionary with these fields:
353 id remove text properties with this ID
354 type remove text properties with this type name
Bram Moolenaar49b79bd2020-03-05 21:52:55 +0100355 both "id" and "type" must both match
Bram Moolenaared997ad2019-07-21 16:42:00 +0200356 bufnr use this buffer instead of the current one
357 all when TRUE remove all matching text properties,
358 not just the first one
359 A property matches when either "id" or "type" matches.
360 If buffer "bufnr" does not exist you get an error message.
361 If buffer "bufnr" is not loaded then nothing happens.
362
363 Returns the number of properties that were removed.
364
Bram Moolenaara5a78822019-09-04 21:57:18 +0200365 Can also be used as a |method|: >
366 GetProps()->prop_remove()
367
Bram Moolenaared997ad2019-07-21 16:42:00 +0200368
369prop_type_add({name}, {props}) *prop_type_add()* *E969* *E970*
370 Add a text property type {name}. If a property type with this
Bram Moolenaar06fe74a2019-08-31 16:20:32 +0200371 name already exists an error is given. Nothing is returned.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200372 {props} is a dictionary with these optional fields:
373 bufnr define the property only for this buffer; this
374 avoids name collisions and automatically
375 clears the property types when the buffer is
376 deleted.
377 highlight name of highlight group to use
378 priority when a character has multiple text
379 properties the one with the highest priority
380 will be used; negative values can be used, the
381 default priority is zero
Bram Moolenaardad44732021-03-31 20:07:33 +0200382 combine when omitted or TRUE combine the highlight
383 with any syntax highlight; when FALSE syntax
Bram Moolenaared997ad2019-07-21 16:42:00 +0200384 highlight will not be used
Bram Moolenaarf4ba8bc2022-08-05 17:05:04 +0100385 override when TRUE the highlight overrides any other,
386 including 'cursorline' and Visual
Bram Moolenaared997ad2019-07-21 16:42:00 +0200387 start_incl when TRUE inserts at the start position will
388 be included in the text property
389 end_incl when TRUE inserts at the end position will be
390 included in the text property
391
Bram Moolenaara5a78822019-09-04 21:57:18 +0200392 Can also be used as a |method|: >
393 GetPropName()->prop_type_add(props)
Bram Moolenaared997ad2019-07-21 16:42:00 +0200394
395prop_type_change({name}, {props}) *prop_type_change()*
396 Change properties of an existing text property type. If a
397 property with this name does not exist an error is given.
398 The {props} argument is just like |prop_type_add()|.
399
Bram Moolenaara5a78822019-09-04 21:57:18 +0200400 Can also be used as a |method|: >
401 GetPropName()->prop_type_change(props)
Bram Moolenaared997ad2019-07-21 16:42:00 +0200402
403prop_type_delete({name} [, {props}]) *prop_type_delete()*
404 Remove the text property type {name}. When text properties
405 using the type {name} are still in place, they will not have
406 an effect and can no longer be removed by name.
407
408 {props} can contain a "bufnr" item. When it is given, delete
409 a property type from this buffer instead of from the global
410 property types.
411
412 When text property type {name} is not found there is no error.
413
Bram Moolenaara5a78822019-09-04 21:57:18 +0200414 Can also be used as a |method|: >
415 GetPropName()->prop_type_delete()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200416
Bram Moolenaarcb80aa22020-10-26 21:12:46 +0100417prop_type_get({name} [, {props}]) *prop_type_get()*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200418 Returns the properties of property type {name}. This is a
419 dictionary with the same fields as was given to
420 prop_type_add().
421 When the property type {name} does not exist, an empty
422 dictionary is returned.
423
424 {props} can contain a "bufnr" item. When it is given, use
425 this buffer instead of the global property types.
426
Bram Moolenaara5a78822019-09-04 21:57:18 +0200427 Can also be used as a |method|: >
428 GetPropName()->prop_type_get()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200429
430prop_type_list([{props}]) *prop_type_list()*
431 Returns a list with all property type names.
432
433 {props} can contain a "bufnr" item. When it is given, use
434 this buffer instead of the global property types.
435
Bram Moolenaared997ad2019-07-21 16:42:00 +0200436
Bram Moolenaard09091d2019-01-17 16:07:22 +0100437==============================================================================
4383. When text changes *text-prop-changes*
439
440Vim will do its best to keep the text properties on the text where it was
441attached. When inserting or deleting text the properties after the change
442will move accordingly.
443
444When text is deleted and a text property no longer includes any text, it is
445deleted. However, a text property that was defined as zero-width will remain,
446unless the whole line is deleted.
Bram Moolenaar30e9b3c2019-09-07 16:24:12 +0200447 *E275*
Bram Moolenaar45311b52019-08-13 22:27:32 +0200448When a buffer is unloaded, all the text properties are gone. There is no way
449to store the properties in a file. You can only re-create them. When a
450buffer is hidden the text is preserved and so are the text properties. It is
451not possible to add text properties to an unloaded buffer.
Bram Moolenaard09091d2019-01-17 16:07:22 +0100452
453When using replace mode, the text properties stay on the same character
454positions, even though the characters themselves change.
455
Bram Moolenaar68e65602019-05-26 21:33:31 +0200456To update text properties after the text was changed, install a callback with
457`listener_add()`. E.g, if your plugin does spell checking, you can have the
458callback update spelling mistakes in the changed text. Vim will move the
459properties below the changed text, so that they still highlight the same text,
460thus you don't need to update these.
461
Bram Moolenaard09091d2019-01-17 16:07:22 +0100462
Bram Moolenaarbc93ceb2020-02-26 13:36:21 +0100463Text property columns are not updated or copied: ~
Bram Moolenaard09091d2019-01-17 16:07:22 +0100464
465- When setting the line with |setline()| or through an interface, such as Lua,
Bram Moolenaara6c27c42019-05-09 19:16:22 +0200466 Tcl or Python. Vim does not know what text got inserted or deleted.
Bram Moolenaarbc93ceb2020-02-26 13:36:21 +0100467- With a command like `:move`, which takes a line of text out of context.
Bram Moolenaard09091d2019-01-17 16:07:22 +0100468
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100469
470 vim:tw=78:ts=8:noet:ft=help:norl: