Bram Moolenaar | de24a87 | 2019-05-05 15:48:00 +0200 | [diff] [blame] | 1 | *textprop.txt* For Vim version 8.1. Last change: 2019 May 05 |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2 | |
| 3 | |
| 4 | VIM REFERENCE MANUAL by Bram Moolenaar |
| 5 | |
| 6 | |
| 7 | Displaying text with properties attached. *text-properties* |
| 8 | |
| 9 | THIS IS UNDER DEVELOPMENT - ANYTHING MAY STILL CHANGE *E967* |
| 10 | |
| 11 | What is not working yet: |
| 12 | - Adjusting column/length when inserting text |
| 13 | - Text properties spanning more than one line |
| 14 | - prop_find() |
| 15 | - callbacks when text properties are outdated |
| 16 | |
| 17 | |
| 18 | 1. Introduction |text-prop-intro| |
| 19 | 2. Functions |text-prop-functions| |
Bram Moolenaar | d09091d | 2019-01-17 16:07:22 +0100 | [diff] [blame] | 20 | 3. When text changes |text-prop-changes| |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 21 | |
| 22 | |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 23 | {not able to use text properties when the |+textprop| feature was |
| 24 | disabled at compile time} |
| 25 | |
| 26 | ============================================================================== |
| 27 | 1. Introduction *text-prop-intro* |
| 28 | |
| 29 | Text properties can be attached to text in a buffer. They will move with the |
| 30 | text: If lines are deleted or inserted the properties move with the text they |
| 31 | are attached to. Also when inserting/deleting text in the line before the |
| 32 | text property. And when inserting/deleting text inside the text property, it |
| 33 | will increase/decrease in size. |
| 34 | |
| 35 | The main use for text properties is to highlight text. This can be seen as a |
| 36 | replacement for syntax highlighting. Instead of defining patterns to match |
| 37 | the text, the highlighting is set by a script, possibly using the output of an |
| 38 | external parser. This only needs to be done once, not every time when |
| 39 | redrawing the screen, thus can be much faster, after the initial cost of |
| 40 | attaching the text properties. |
| 41 | |
| 42 | Text properties can also be used for other purposes to identify text. For |
| 43 | example, add a text property on a function name, so that a search can be |
| 44 | defined to jump to the next/previous function. |
| 45 | |
| 46 | A text property is attached at a specific line and column, and has a specified |
| 47 | length. The property can span multiple lines. |
| 48 | |
| 49 | A text property has these fields: |
| 50 | "id" a number to be used as desired |
| 51 | "type" the name of a property type |
| 52 | |
| 53 | |
| 54 | Property Types ~ |
| 55 | *E971* |
| 56 | A text property normally has the name of a property type, which defines |
| 57 | how to highlight the text. The property type can have these entries: |
| 58 | "highlight" name of the highlight group to use |
Bram Moolenaar | de24a87 | 2019-05-05 15:48:00 +0200 | [diff] [blame] | 59 | "combine" when TRUE the text property highlighting is combined |
| 60 | with any syntax highligting, when omitted or FALSE the |
| 61 | text property highlighting replaces the syntax |
| 62 | highlighting |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 63 | "priority" when properties overlap, the one with the highest |
| 64 | priority will be used. |
| 65 | "start_incl" when TRUE inserts at the start position will be |
| 66 | included in the text property |
| 67 | "end_incl" when TRUE inserts at the end position will be |
| 68 | included in the text property |
| 69 | |
| 70 | |
| 71 | Example ~ |
| 72 | |
| 73 | Suppose line 11 in a buffer has this text (excluding the indent): |
| 74 | |
| 75 | The number 123 is smaller than 4567. |
| 76 | |
Bram Moolenaar | 4c05fa0 | 2019-01-01 15:32:17 +0100 | [diff] [blame] | 77 | To highlight the numbers in this text: > |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 78 | call prop_type_add('number', {'highlight': 'Constant'}) |
Bram Moolenaar | 9d87a37 | 2018-12-18 21:41:50 +0100 | [diff] [blame] | 79 | call prop_add(11, 12, {'length': 3, 'type': 'number'}) |
| 80 | call prop_add(11, 32, {'length': 4, 'type': 'number'}) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 81 | |
Bram Moolenaar | 4c05fa0 | 2019-01-01 15:32:17 +0100 | [diff] [blame] | 82 | Try inserting or deleting lines above the text, you will see that the text |
| 83 | properties stick to the text, thus the line number is adjusted as needed. |
| 84 | |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 85 | Setting "start_incl" and "end_incl" is useful when white space surrounds the |
| 86 | text, e.g. for a function name. Using false is useful when the text starts |
| 87 | and/or ends with a specific character, such as the quote surrounding a string. |
| 88 | |
| 89 | func FuncName(arg) ~ |
| 90 | ^^^^^^^^ property with start_incl and end_incl set |
| 91 | |
| 92 | var = "text"; ~ |
| 93 | ^^^^^^ property with start_incl and end_incl not set |
| 94 | |
| 95 | Nevertheless, when text is inserted or deleted the text may need to be parsed |
Bram Moolenaar | 9d87a37 | 2018-12-18 21:41:50 +0100 | [diff] [blame] | 96 | and the text properties updated. But this can be done asynchronously. |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 97 | |
| 98 | ============================================================================== |
| 99 | 2. Functions *text-prop-functions* |
| 100 | |
| 101 | Manipulating text property types: |
| 102 | |
| 103 | prop_type_add({name}, {props}) define a new property type |
| 104 | prop_type_change({name}, {props}) change an existing property type |
| 105 | prop_type_delete({name} [, {props}]) delete a property type |
| 106 | prop_type_get([{name} [, {props}]) get property type values |
| 107 | prop_type_list([{props}]) get list of property types |
| 108 | |
| 109 | |
| 110 | Manipulating text properties: |
| 111 | |
| 112 | prop_add({lnum}, {col}, {props}) add a text property |
Bram Moolenaar | c8c8849 | 2018-12-27 23:59:26 +0100 | [diff] [blame] | 113 | prop_clear({lnum} [, {lnum-end} [, {bufnr}]]) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 114 | remove all text properties |
| 115 | prop_find({props} [, {direction}]) search for a text property |
| 116 | prop_list({lnum} [, {props}) text properties in {lnum} |
Bram Moolenaar | c8c8849 | 2018-12-27 23:59:26 +0100 | [diff] [blame] | 117 | prop_remove({props} [, {lnum} [, {lnum-end}]]) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 118 | remove a text property |
| 119 | |
Bram Moolenaar | d09091d | 2019-01-17 16:07:22 +0100 | [diff] [blame] | 120 | ============================================================================== |
| 121 | 3. When text changes *text-prop-changes* |
| 122 | |
| 123 | Vim will do its best to keep the text properties on the text where it was |
| 124 | attached. When inserting or deleting text the properties after the change |
| 125 | will move accordingly. |
| 126 | |
| 127 | When text is deleted and a text property no longer includes any text, it is |
| 128 | deleted. However, a text property that was defined as zero-width will remain, |
| 129 | unless the whole line is deleted. |
| 130 | |
| 131 | When using replace mode, the text properties stay on the same character |
| 132 | positions, even though the characters themselves change. |
| 133 | |
| 134 | |
| 135 | When text property columns are not updated ~ |
| 136 | |
| 137 | - When setting the line with |setline()| or through an interface, such as Lua, |
| 138 | Tcl or Python. |
| 139 | |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 140 | |
| 141 | vim:tw=78:ts=8:noet:ft=help:norl: |