textprop.txt 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. *textprop.txt* For Vim version 9.0. Last change: 2022 Nov 18
  2. VIM REFERENCE MANUAL by Bram Moolenaar
  3. Displaying text with properties attached. *textprop* *text-properties*
  4. 1. Introduction |text-prop-intro|
  5. 2. Functions |text-prop-functions|
  6. 3. When text changes |text-prop-changes|
  7. {not able to use text properties when the |+textprop| feature was
  8. disabled at compile time}
  9. ==============================================================================
  10. 1. Introduction *text-prop-intro*
  11. Text properties can be attached to text in a buffer. They will move with the
  12. text: If lines are deleted or inserted the properties move with the text they
  13. are attached to. Also when inserting/deleting text in the line before the
  14. text property. And when inserting/deleting text inside the text property, it
  15. will increase/decrease in size.
  16. The main use for text properties is to highlight text. This can be seen as a
  17. replacement for syntax highlighting. Instead of defining patterns to match
  18. the text, the highlighting is set by a script, possibly using the output of an
  19. external parser. This only needs to be done once, not every time when
  20. redrawing the screen, thus can be much faster, after the initial cost of
  21. attaching the text properties.
  22. Text properties can also be used for other purposes to identify text. For
  23. example, add a text property on a function name, so that a search can be
  24. defined to jump to the next/previous function.
  25. A text property is attached at a specific line and column, and has a specified
  26. length. The property can span multiple lines.
  27. A text property has these fields:
  28. "id" a number to be used as desired
  29. "type" the name of a property type
  30. Property Types ~
  31. *E971*
  32. A text property normally has the name of a property type, which defines
  33. how to highlight the text. The property type can have these entries:
  34. "highlight" name of the highlight group to use
  35. "combine" when omitted or TRUE the text property highlighting is
  36. combined with any syntax highlighting; when FALSE the
  37. text property highlighting replaces the syntax
  38. highlighting
  39. "priority" when properties overlap, the one with the highest
  40. priority will be used.
  41. "start_incl" when TRUE inserts at the start position will be
  42. included in the text property
  43. "end_incl" when TRUE inserts at the end position will be
  44. included in the text property
  45. Example ~
  46. Suppose line 11 in a buffer has this text (excluding the indent):
  47. The number 123 is smaller than 4567.
  48. To highlight the numbers in this text: >
  49. call prop_type_add('number', {'highlight': 'Constant'})
  50. call prop_add(11, 12, {'length': 3, 'type': 'number'})
  51. call prop_add(11, 32, {'length': 4, 'type': 'number'})
  52. Try inserting or deleting lines above the text, you will see that the text
  53. properties stick to the text, thus the line number is adjusted as needed.
  54. Setting "start_incl" and "end_incl" is useful when white space surrounds the
  55. text, e.g. for a function name. Using false is useful when the text starts
  56. and/or ends with a specific character, such as the quote surrounding a string.
  57. func FuncName(arg) ~
  58. ^^^^^^^^ property with start_incl and end_incl set
  59. var = "text"; ~
  60. ^^^^^^ property with start_incl and end_incl not set
  61. Nevertheless, when text is inserted or deleted the text may need to be parsed
  62. and the text properties updated. But this can be done asynchronously.
  63. Internal error *E967*
  64. If you see E967, please report the bug. You can do this at Github:
  65. https://github.com/vim/vim/issues/new
  66. ==============================================================================
  67. 2. Functions *text-prop-functions*
  68. Manipulating text property types:
  69. prop_type_add({name}, {props}) define a new property type
  70. prop_type_change({name}, {props}) change an existing property type
  71. prop_type_delete({name} [, {props}]) delete a property type
  72. prop_type_get({name} [, {props}]) get property type values
  73. prop_type_list([{props}]) get list of property types
  74. Manipulating text properties:
  75. prop_add({lnum}, {col}, {props}) add a text property
  76. prop_add_list({props}, [[{lnum}, {col}, {end-lnum}, {end-col}], ...])
  77. add a text property at multiple
  78. positions.
  79. prop_clear({lnum} [, {lnum-end} [, {bufnr}]])
  80. remove all text properties
  81. prop_find({props} [, {direction}]) search for a text property
  82. prop_list({lnum} [, {props}]) text properties in {lnum}
  83. prop_remove({props} [, {lnum} [, {lnum-end}]])
  84. remove a text property
  85. *prop_add()* *E965*
  86. prop_add({lnum}, {col}, {props})
  87. Attach a text property at position {lnum}, {col}. {col} is
  88. counted in bytes, use one for the first column.
  89. If {lnum} is invalid an error is given. *E966*
  90. If {col} is invalid an error is given. *E964*
  91. {props} is a dictionary with these fields:
  92. type name of the text property type
  93. length length of text in bytes, can only be used
  94. for a property that does not continue in
  95. another line; can be zero
  96. end_lnum line number for the end of text (inclusive)
  97. end_col column just after the text; not used when
  98. "length" is present; when {col} and "end_col"
  99. are equal, and "end_lnum" is omitted or equal
  100. to {lnum}, this is a zero-width text property
  101. bufnr buffer to add the property to; when omitted
  102. the current buffer is used
  103. id user defined ID for the property; must be a
  104. number, should be positive; when using "text"
  105. then "id" must not be present and will be set
  106. automatically to a negative number; otherwise
  107. zero is used
  108. *E1305*
  109. text text to be displayed before {col}, or
  110. above/below the line if {col} is zero; prepend
  111. and/or append spaces for padding with
  112. highlighting; cannot be used with "length",
  113. "end_lnum" and "end_col"
  114. See |virtual-text| for more information.
  115. *E1294*
  116. text_align when "text" is present and {col} is zero;
  117. specifies where to display the text:
  118. after after the end of the line
  119. right right aligned in the window (unless
  120. the text wraps to the next screen
  121. line)
  122. below in the next screen line
  123. above just above the line
  124. When omitted "after" is used. Only one
  125. "right" property can fit in each line, if
  126. there are two or more these will go in a
  127. separate line (still right aligned).
  128. text_padding_left *E1296*
  129. used when "text" is present and {col} is zero;
  130. padding between the end of the text line
  131. (leftmost column for "below") and the virtual
  132. text, not highlighted
  133. text_wrap when "text" is present and {col} is zero,
  134. specifies what happens if the text doesn't
  135. fit:
  136. wrap wrap the text to the next line
  137. truncate truncate the text to make it fit
  138. When omitted "truncate" is used.
  139. Note that this applies to the individual text
  140. property, the 'wrap' option sets the overall
  141. behavior
  142. All fields except "type" are optional.
  143. It is an error when both "length" and "end_lnum" or "end_col"
  144. are given. Either use "length" or "end_col" for a property
  145. within one line, or use "end_lnum" and "end_col" for a
  146. property that spans more than one line.
  147. When neither "length" nor "end_col" are given the property
  148. will be zero-width. That means it will move with the text, as
  149. a kind of mark. One character will be highlighted, if the
  150. type specifies highlighting.
  151. The property can end exactly at the last character of the
  152. text, or just after it. In the last case, if text is appended
  153. to the line, the text property size will increase, also when
  154. the property type does not have "end_incl" set.
  155. "type" will first be looked up in the buffer the property is
  156. added to. When not found, the global property types are used.
  157. If not found an error is given.
  158. *virtual-text*
  159. When "text" is used and the column is non-zero then this text
  160. will be displayed at the specified start location of the text
  161. property. The text of the buffer line will be shifted to make
  162. room. This is called "virtual text".
  163. When the column is zero the virtual text will appear above,
  164. after or below the buffer text. The "text_align" and
  165. "text_wrap" arguments determine how it is displayed.
  166. To separate the virtual text from the buffer text prepend
  167. and/or append spaces to the "text" field or use the
  168. "text_padding_left" value.
  169. Make sure to use a highlight that makes clear to the user that
  170. this is virtual text, otherwise it will be very confusing that
  171. the text cannot be edited. When using "above" you need to
  172. make clear this text belongs to the text line below it, when
  173. using "below" you need to make sure it belongs to the text
  174. line above it.
  175. The text will be displayed but it is not part of the actual
  176. buffer line, the cursor cannot be placed on it. A mouse click
  177. in the text will move the cursor to the first character after
  178. the text, or the last character of the line.
  179. Any Tab and other control character in the text will be
  180. changed to a space (Rationale: otherwise the size of the text
  181. is difficult to compute).
  182. A negative "id" will be chosen and is returned. Once a
  183. property with "text" has been added for a buffer then using a
  184. negative "id" for any other property will give an error:
  185. *E1293*
  186. Can also be used as a |method|: >
  187. GetLnum()->prop_add(col, props)
  188. <
  189. *prop_add_list()*
  190. prop_add_list({props}, [{item}, ...])
  191. Similar to prop_add(), but attaches a text property at
  192. multiple positions in a buffer.
  193. {props} is a dictionary with these fields:
  194. bufnr buffer to add the property to; when omitted
  195. the current buffer is used
  196. id user defined ID for the property; must be a
  197. number; when omitted zero is used
  198. type name of the text property type
  199. All fields except "type" are optional.
  200. The second argument is a List of items, where each {item} is a
  201. list that specifies the starting and ending position of the
  202. text: [{lnum}, {col}, {end-lnum}, {end-col}]
  203. or: [{lnum}, {col}, {end-lnum}, {end-col}, {id}]
  204. The first two items {lnum} and {col} specify the starting
  205. position of the text where the property will be attached.
  206. The next two items {end-lnum} and {end-col} specify the
  207. position just after the text.
  208. An optional fifth item {id} can be used to give a different ID
  209. to a property. When omitted the ID from {props} is used,
  210. falling back to zero if none are present.
  211. It is not possible to add a text property with a "text" field
  212. here.
  213. Example:
  214. call prop_add_list(#{type: 'MyProp', id: 2},
  215. \ [[1, 4, 1, 7],
  216. \ [1, 15, 1, 20],
  217. \ [2, 30, 3, 30]]
  218. Can also be used as a |method|: >
  219. GetProp()->prop_add_list([[1, 1, 1, 2], [1, 4, 1, 8]])
  220. prop_clear({lnum} [, {lnum-end} [, {props}]]) *prop_clear()*
  221. Remove all text properties from line {lnum}.
  222. When {lnum-end} is given, remove all text properties from line
  223. {lnum} to {lnum-end} (inclusive).
  224. When {props} contains a "bufnr" item use this buffer,
  225. otherwise use the current buffer.
  226. Can also be used as a |method|: >
  227. GetLnum()->prop_clear()
  228. <
  229. *prop_find()*
  230. prop_find({props} [, {direction}])
  231. Search for a text property as specified with {props}:
  232. id property with this ID
  233. type property with this type name
  234. both "id" and "type" must both match
  235. bufnr buffer to search in; when present a
  236. start position with "lnum" and "col"
  237. must be given; when omitted the
  238. current buffer is used
  239. lnum start in this line (when omitted start
  240. at the cursor)
  241. col start at this column (when omitted
  242. and "lnum" is given: use column 1,
  243. otherwise start at the cursor)
  244. skipstart do not look for a match at the start
  245. position
  246. A property matches when either "id" or "type" matches.
  247. {direction} can be "f" for forward and "b" for backward. When
  248. omitted forward search is performed.
  249. If a match is found then a Dict is returned with the entries
  250. as with prop_list(), and additionally an "lnum" entry.
  251. If no match is found then an empty Dict is returned.
  252. prop_list({lnum} [, {props}]) *prop_list()*
  253. Returns a List with all the text properties in line {lnum}.
  254. The following optional items are supported in {props}:
  255. bufnr use this buffer instead of the current buffer
  256. end_lnum return text properties in all the lines
  257. between {lnum} and {end_lnum} (inclusive).
  258. A negative value is used as an offset from the
  259. last buffer line; -1 refers to the last buffer
  260. line.
  261. types List of property type names. Return only text
  262. properties that match one of the type names.
  263. ids List of property identifiers. Return only text
  264. properties with one of these identifiers.
  265. The properties are ordered by starting column and priority.
  266. Each property is a Dict with these entries:
  267. lnum starting line number. Present only when
  268. returning text properties between {lnum} and
  269. {end_lnum}.
  270. col starting column
  271. length length in bytes, one more if line break is
  272. included
  273. id property ID
  274. type name of the property type, omitted if
  275. the type was deleted
  276. type_bufnr buffer number for which this type was defined;
  277. 0 if the type is global
  278. start when TRUE property starts in this line
  279. end when TRUE property ends in this line
  280. When "start" is zero the property started in a previous line,
  281. the current one is a continuation.
  282. When "end" is zero the property continues in the next line.
  283. The line break after this line is included.
  284. Returns an empty list on error.
  285. Examples:
  286. " get text properties placed in line 5
  287. echo prop_list(5)
  288. " get text properties placed in line 20 in buffer 4
  289. echo prop_list(20, {'bufnr': 4})
  290. " get all the text properties between line 1 and 20
  291. echo prop_list(1, {'end_lnum': 20})
  292. " get all the text properties of type 'myprop'
  293. echo prop_list(1, {'types': ['myprop'],
  294. \ 'end_lnum': -1})
  295. " get all the text properties of type 'prop1' or 'prop2'
  296. echo prop_list(1, {'types': ['prop1', 'prop2'],
  297. \ 'end_lnum': -1})
  298. " get all the text properties with ID 8
  299. echo prop_list(1, {'ids': [8], 'end_lnum': line('$')})
  300. " get all the text properties with ID 10 and 20
  301. echo prop_list(1, {'ids': [10, 20], 'end_lnum': -1})
  302. " get text properties with type 'myprop' and ID 100
  303. " in buffer 4.
  304. echo prop_list(1, {'bufnr': 4, 'types': ['myprop'],
  305. \ 'ids': [100], 'end_lnum': -1})
  306. Can also be used as a |method|: >
  307. GetLnum()->prop_list()
  308. <
  309. *prop_remove()* *E968* *E860*
  310. prop_remove({props} [, {lnum} [, {lnum-end}]])
  311. Remove a matching text property from line {lnum}. When
  312. {lnum-end} is given, remove matching text properties from line
  313. {lnum} to {lnum-end} (inclusive).
  314. When {lnum} is omitted remove matching text properties from
  315. all lines (this requires going over all lines, thus will be a
  316. bit slow for a buffer with many lines).
  317. {props} is a dictionary with these fields:
  318. id remove text properties with this ID
  319. type remove text properties with this type name
  320. types remove text properties with type names in this
  321. List
  322. both "id" and "type"/"types" must both match
  323. bufnr use this buffer instead of the current one
  324. all when TRUE remove all matching text properties,
  325. not just the first one
  326. Only one of "type" and "types" may be supplied. *E1295*
  327. A property matches when either "id" or one of the supplied
  328. types matches.
  329. If buffer "bufnr" does not exist you get an error message.
  330. If buffer "bufnr" is not loaded then nothing happens.
  331. Returns the number of properties that were removed.
  332. Can also be used as a |method|: >
  333. GetProps()->prop_remove()
  334. prop_type_add({name}, {props}) *prop_type_add()* *E969* *E970*
  335. Add a text property type {name}. If a property type with this
  336. name already exists an error is given. Nothing is returned.
  337. {props} is a dictionary with these optional fields:
  338. bufnr define the property only for this buffer; this
  339. avoids name collisions and automatically
  340. clears the property types when the buffer is
  341. deleted.
  342. highlight name of highlight group to use
  343. priority when a character has multiple text
  344. properties the one with the highest priority
  345. will be used; negative values can be used, the
  346. default priority is zero
  347. combine when omitted or TRUE combine the highlight
  348. with any syntax highlight; when FALSE syntax
  349. highlight will not be used
  350. override when TRUE the highlight overrides any other,
  351. including 'cursorline' and Visual
  352. start_incl when TRUE inserts at the start position will
  353. be included in the text property
  354. end_incl when TRUE inserts at the end position will be
  355. included in the text property
  356. Can also be used as a |method|: >
  357. GetPropName()->prop_type_add(props)
  358. prop_type_change({name}, {props}) *prop_type_change()*
  359. Change properties of an existing text property type. If a
  360. property with this name does not exist an error is given.
  361. The {props} argument is just like |prop_type_add()|.
  362. Can also be used as a |method|: >
  363. GetPropName()->prop_type_change(props)
  364. prop_type_delete({name} [, {props}]) *prop_type_delete()*
  365. Remove the text property type {name}. When text properties
  366. using the type {name} are still in place, they will not have
  367. an effect and can no longer be removed by name.
  368. {props} can contain a "bufnr" item. When it is given, delete
  369. a property type from this buffer instead of from the global
  370. property types.
  371. When text property type {name} is not found there is no error.
  372. Can also be used as a |method|: >
  373. GetPropName()->prop_type_delete()
  374. prop_type_get({name} [, {props}]) *prop_type_get()*
  375. Returns the properties of property type {name}. This is a
  376. dictionary with the same fields as was given to
  377. prop_type_add().
  378. When the property type {name} does not exist, an empty
  379. dictionary is returned.
  380. {props} can contain a "bufnr" item. When it is given, use
  381. this buffer instead of the global property types.
  382. Can also be used as a |method|: >
  383. GetPropName()->prop_type_get()
  384. prop_type_list([{props}]) *prop_type_list()*
  385. Returns a list with all property type names.
  386. {props} can contain a "bufnr" item. When it is given, use
  387. this buffer instead of the global property types.
  388. ==============================================================================
  389. 3. When text changes *text-prop-changes*
  390. Vim will do its best to keep the text properties on the text where it was
  391. attached. When inserting or deleting text the properties after the change
  392. will move accordingly.
  393. When text is deleted and a text property no longer includes any text, it is
  394. deleted. However, a text property that was defined as zero-width will remain,
  395. unless the whole line is deleted.
  396. *E275*
  397. When a buffer is unloaded, all the text properties are gone. There is no way
  398. to store the properties in a file. You can only re-create them. When a
  399. buffer is hidden the text is preserved and so are the text properties. It is
  400. not possible to add text properties to an unloaded buffer.
  401. When using replace mode, the text properties stay on the same character
  402. positions, even though the characters themselves change.
  403. To update text properties after the text was changed, install a callback with
  404. `listener_add()`. E.g, if your plugin does spell checking, you can have the
  405. callback update spelling mistakes in the changed text. Vim will move the
  406. properties below the changed text, so that they still highlight the same text,
  407. thus you don't need to update these.
  408. Text property columns are not updated or copied: ~
  409. - When setting the line with |setline()| or through an interface, such as Lua,
  410. Tcl or Python. Vim does not know what text got inserted or deleted.
  411. - With a command like `:move`, which takes a line of text out of context.
  412. vim:tw=78:ts=8:noet:ft=help:norl: