Skip to content

Commit 1adc4eb

Browse files
feat(highlights)!: Use treesitter highlights
1. Leverage default syntax highlights from treesitter, like @markup.bold, @markup.italic, etc. 2. Require treesitter highlights to be enabled and remove legacy Vim syntax
1 parent f52ce3d commit 1adc4eb

File tree

21 files changed

+377
-612
lines changed

21 files changed

+377
-612
lines changed

DOCS.md

+73-49
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ Enabled:
224224
If this highlight group does not suit you, you can apply different highlight group to it:
225225

226226
```lua
227-
vim.cmd[[autocmd ColorScheme * hi link OrgHideLeadingStars MyCustomHlGroup]]
227+
vim.cmd[[autocmd ColorScheme * hi link @org.leading.stars MyCustomHlGroup]]
228228
```
229229

230230
#### **org_hide_emphasis_markers**
@@ -1315,67 +1315,91 @@ Currently, these things are formatted:
13151315
## User interface
13161316

13171317
### Colors
1318+
Most of the highlight groups are linked to treesitter highlights where applicable (see `:h treesitter-highlight`).
1319+
1320+
The following highlight groups are used:
1321+
1322+
* `@org.headline.level.1`: Headline at level 1 - `linked to Title`
1323+
* `@org.headline.level.2`: Headline at level 2 - `linked to Constant`
1324+
* `@org.headline.level.3`: Headline at level 3 - `linked to Identifier`
1325+
* `@org.headline.level.4`: Headline at level 4 - `linked to Statement`
1326+
* `@org.headline.level.5`: Headline at level 5 - `linked to PreProc`
1327+
* `@org.headline.level.6`: Headline at level 6 - `linked to Type`
1328+
* `@org.headline.level.7`: Headline at level 7 - `linked to Special`
1329+
* `@org.headline.level.8`: Headline at level 8 - `linked to String`
1330+
* `@org.timestamp.active`: An active timestamp - linked to `@keyword`
1331+
* `@org.timestamp.inactive`: An inactive timestamp - linked to `@comment`
1332+
* `@org.keyword.todo`: TODO keywords color - Parsed from `Error` (see note below)
1333+
* `@org.keyword.done`: DONE keywords color - Parsed from `DiffAdd` (see note below)
1334+
* `@org.bullet`: A normal bullet under a header item - linked to `@markup.list`
1335+
* `@org.properties`: Property drawer start/end delimiters - linked to `@property`
1336+
* `@org.drawer`: Drawer start/end delimiters - linked to `@property`
1337+
* `@org.tag`: A tag for a headline item, shown on the righthand side like `:foo:` - linked to `@tag.attribute`
1338+
* `@org.plan`: `SCHEDULED`, `DEADLINE`, `CLOSED`, etc. keywords - linked to `Constant`
1339+
* `@org.comment`: A comment block - linked to `@comment`
1340+
* `@org.latex_env`: LaTeX block - linked to `@markup.environment`
1341+
* `@org.directive`: Blocks starting with `#+` - linked to `@comment`
1342+
* `@org.checkbox`: The default checkbox highlight, including square brackets - linked to `@markup.list.unchecked`
1343+
* `@org.checkbox.halfchecked`: A checkbox status (marker between `[]`) checked with `[-]` - linked to `@markup.list.unchecked`
1344+
* `@org.checkbox.checked`: A checkbox status (marker between `[]`) checked with either `[x]` or `[X]` - linked to `@markup.list.checked`
1345+
* `@org.bold`: **bold** text - linked to `@markup.strong`,
1346+
* `@org.bold.delimiter`: bold text delimiter `*` - linked to `@markup.strong`,
1347+
* `@org.italic`: *italic* text - linked to `@markup.italic`,
1348+
* `@org.italic.delimiter`: italic text delimiter `/` - linked to `@markup.italic`,
1349+
* `@org.strikethrough`: ~strikethrough~ text - linked to `@markup.strikethrough`,
1350+
* `@org.strikethrough.delimiter`: strikethrough text delimiter `+` - linked to `@markup.strikethrough`,
1351+
* `@org.underline`: <u>underline<u/> text - linked to `@markup.underline`,
1352+
* `@org.underline.delimiter`: underline text delimiter `_` - linked to `@markup.underline`,
1353+
* `@org.code`: `code` text - linked to `@markup.raw`,
1354+
* `@org.code.delimiter`: code text delimiter `~` - linked to `@markup.raw`,
1355+
* `@org.verbatim`: `verbatim` text - linked to `@markup.raw`,
1356+
* `@org.verbatim.delimiter`: verbatim text delimiter `=` - linked to `@markup.raw`,
1357+
* `@org.hyperlink`: [link](link) text - linked to `@markup.link.url`,
1358+
* `@org.latex`: Inline latex - linked to `@markup.math`,
1359+
* `@org.table.delimiter` - `|` and `-` delimiters in tables - linked to `@punctuation.special`,
1360+
* `@org.table.heading` - Table headings - linked to `@markup.heading`,
1361+
* `@org.edit_src` - The highlight for the source content in an _Org_ buffer while it is being edited in an edit special buffer - linked to `Visual`,
1362+
* `@org.agenda.deadline`: A item deadline in the agenda view - Parsed from `Error` (see note below)
1363+
* `@org.agenda.scheduled`: A scheduled item in the agenda view - Parsed from `DiffAdd` (see note dbelow)
1364+
* `@org.agenda.scheduled_past`: A item past its scheduled date in the agenda view - Parsed from `WarningMsg` (see note below)
1365+
1366+
Note:
1367+
13181368
Colors used for todo keywords and agenda states (deadline, schedule ok, schedule warning)
1319-
are parsed from the current colorsheme from several highlight groups (Error, WarningMsg, DiffAdd, etc.).
1320-
If those colors are not suitable you can override them like this:
1369+
are parsed from the current colorscheme from several highlight groups (Error, WarningMsg, DiffAdd, etc.).
13211370

1322-
```vim
1323-
autocmd ColorScheme * call s:setup_org_colors()
13241371

1325-
function! s:setup_org_colors() abort
1326-
hi OrgAgendaDeadline guifg=#FFAAAA
1327-
hi OrgAgendaScheduled guifg=#AAFFAA
1328-
hi OrgAgendaScheduledPast guifg=Orange
1329-
endfunction
1372+
#### Overriding colors
1373+
All colors can be overridden by either setting new values or linking to another highlight group:
1374+
```lua
1375+
vim.api.nvim_create_autocmd('ColorScheme', {
1376+
pattern = '*',
1377+
callback = function()
1378+
-- Define own colors
1379+
vim.api.nvim_set_hl(0, '@org.agenda.deadline', { fg = '#FFAAAA' })
1380+
vim.api.nvim_set_hl(0, '@org.agenda.scheduled', { fg = '#AAFFAA' })
1381+
-- Link to another highlight group
1382+
vim.api.nvim_set_hl(0, '@org.agenda.scheduled_past', { link = 'Statement' })
1383+
end
1384+
})
13301385
```
13311386

1332-
or you can link it to another highlight group:
1387+
Or in Vimscript:
13331388

13341389
```vim
1390+
autocmd ColorScheme * call s:setup_org_colors()
1391+
13351392
function! s:setup_org_colors() abort
1336-
hi link OrgAgendaDeadline Error
1337-
hi link OrgAgendaScheduled DiffAdd
1338-
hi link OrgAgendaScheduledPast Statement
1393+
" Define own colors
1394+
hi @org.agenda.deadline guifg=#FFAAAA
1395+
hi @org.agenda.scheduled guifg=#AAFFAA
1396+
" Link to another highlight group
1397+
hi link @org.agenda.scheduled_past Statement
13391398
endfunction
13401399
```
13411400

13421401
For adding/changing TODO keyword colors see [org-todo-keyword-faces](#org_todo_keyword_faces)
13431402

1344-
#### Highlight Groups
1345-
1346-
* The following highlight groups are based on _Treesitter_ query results, hence when setting up _Orgmode_ these
1347-
highlights must be enabled by removing `disable = {'org'}` from the default recommended _Treesitter_ configuration.
1348-
1349-
* `OrgTSTimestampActive`: An active timestamp
1350-
* `OrgTSTimestampInactive`: An inactive timestamp
1351-
* `OrgTSBullet`: A normal bullet under a header item
1352-
* `OrgTSPropertyDrawer`: Property drawer start/end delimiters
1353-
* `OrgTSDrawer`: Drawer start/end delimiters
1354-
* `OrgTSTag`: A tag for a headline item, shown on the righthand side like `:foo:`
1355-
* `OrgTSPlan`: `SCHEDULED`, `DEADLINE`, `CLOSED`, etc. keywords
1356-
* `OrgTSComment`: A comment block
1357-
* `OrgTSLatex`: LaTeX block
1358-
* `OrgTSDirective`: Blocks starting with `#+`
1359-
* `OrgTSCheckbox`: The default checkbox highlight, overridden if any of the below groups are specified
1360-
* `OrgTSCheckboxChecked`: A checkbox checked with either `[x]` or `[X]`
1361-
* `OrgTSCheckboxHalfChecked`: A checkbox checked with `[-]`
1362-
* `OrgTSCheckboxUnchecked`: A empty checkbox
1363-
* `OrgTSHeadlineLevel1`: Headline at level 1
1364-
* `OrgTSHeadlineLevel2`: Headline at level 2
1365-
* `OrgTSHeadlineLevel3`: Headline at level 3
1366-
* `OrgTSHeadlineLevel4`: Headline at level 4
1367-
* `OrgTSHeadlineLevel5`: Headline at level 5
1368-
* `OrgTSHeadlineLevel6`: Headline at level 6
1369-
* `OrgTSHeadlineLevel7`: Headline at level 7
1370-
* `OrgTSHeadlineLevel8`: Headline at level 8
1371-
1372-
* The following use vanilla _Vim_ syntax matching, and will work without _Treesitter_ highlighting enabled.
1373-
1374-
* `OrgEditSrcHighlight`: The highlight for the source content in an _Org_ buffer while it is being edited in an edit special buffer
1375-
* `OrgAgendaDeadline`: A item deadline in the agenda view
1376-
* `OrgAgendaScheduled`: A scheduled item in the agenda view
1377-
* `OrgAgendaScheduledPast`: A item past its scheduled date in the agenda view
1378-
13791403
### Menu
13801404

13811405
The menu is used when selecting further actions in `agenda`, `capture` and `export`. Here is an example of the menu you see when opening `agenda`:

lua/orgmode/agenda/views/agenda.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ function AgendaView:build()
180180

181181
if is_today or is_weekend then
182182
table.insert(highlights, {
183-
hlgroup = 'OrgBold',
183+
hlgroup = '@org.bold',
184184
range = Range:new({
185185
start_line = #content + 1,
186186
end_line = #content + 1,

lua/orgmode/clock/report.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function ClockReport:draw_for_agenda(start_line)
5656
range.start_col = range.start_col + 1
5757
range.end_col = range.end_col + 2
5858
table.insert(highlights, {
59-
hlgroup = 'OrgBold',
59+
hlgroup = '@org.bold',
6060
range = range,
6161
})
6262
end
@@ -67,7 +67,7 @@ function ClockReport:draw_for_agenda(start_line)
6767
range.start_col = range.start_col + 1
6868
range.end_col = range.end_col + 2
6969
table.insert(highlights, {
70-
hlgroup = 'OrgUnderline',
70+
hlgroup = '@org.hyperlink',
7171
range = range,
7272
})
7373
end

lua/orgmode/colors/highlighter/init.lua

-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ end
2424

2525
---@private
2626
function OrgHighlighter:_setup()
27-
local ts_highlights_enabled = config:ts_highlights_enabled()
28-
if not ts_highlights_enabled then
29-
return
30-
end
3127
self.stars = require('orgmode.colors.highlighter.stars'):new({ highlighter = self })
3228
self.markup = require('orgmode.colors.highlighter.markup'):new({ highlighter = self })
3329
self.todos = require('orgmode.colors.highlighter.todos'):new()

lua/orgmode/colors/highlighter/markup/dates.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function OrgDates:highlight(highlights, bufnr)
135135
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col, {
136136
ephemeral = ephemeral,
137137
end_col = entry.to.end_col,
138-
hl_group = entry.char == '>' and 'OrgTSTimestampActive' or 'OrgTSTimestampInactive',
138+
hl_group = entry.char == '>' and '@org.timestamp.active' or '@org.timestamp.inactive',
139139
priority = 110,
140140
})
141141
end

lua/orgmode/colors/highlighter/markup/emphasis.lua

+8-25
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,28 @@ local valid_post_marker_chars =
1111

1212
local markers = {
1313
['*'] = {
14-
hl_name = 'org_bold',
15-
hl_cmd = 'hi def %s term=bold cterm=bold gui=bold',
14+
hl_name = '@org.bold',
1615
nestable = true,
1716
},
1817
['/'] = {
19-
hl_name = 'org_italic',
20-
hl_cmd = 'hi def %s term=italic cterm=italic gui=italic',
18+
hl_name = '@org.italic',
2119
nestable = true,
2220
},
2321
['_'] = {
24-
hl_name = 'org_underline',
25-
hl_cmd = 'hi def %s term=underline cterm=underline gui=underline',
22+
hl_name = '@org.underline',
2623
nestable = true,
2724
},
2825
['+'] = {
29-
hl_name = 'org_strikethrough',
30-
hl_cmd = 'hi def %s term=strikethrough cterm=strikethrough gui=strikethrough',
26+
hl_name = '@org.strikethrough',
3127
nestable = true,
3228
},
3329
['~'] = {
34-
hl_name = 'org_code',
35-
hl_cmd = 'hi def link %s String',
30+
hl_name = '@org.code',
3631
nestable = false,
3732
spell = false,
3833
},
3934
['='] = {
40-
hl_name = 'org_verbatim',
41-
hl_cmd = 'hi def link %s String',
35+
hl_name = '@org.verbatim',
4236
nestable = false,
4337
spell = false,
4438
},
@@ -51,7 +45,6 @@ function OrgEmphasis:new(opts)
5145
}
5246
setmetatable(data, self)
5347
self.__index = self
54-
data:_add_hl_groups()
5548
return data
5649
end
5750

@@ -68,7 +61,7 @@ function OrgEmphasis:highlight(highlights, bufnr)
6861
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col, {
6962
ephemeral = ephemeral,
7063
end_col = entry.from.end_col,
71-
hl_group = markers[entry.char].hl_name .. '_delimiter',
64+
hl_group = markers[entry.char].hl_name .. '.delimiter',
7265
spell = markers[entry.char].spell,
7366
priority = 110 + entry.from.start_col,
7467
conceal = conceal,
@@ -78,7 +71,7 @@ function OrgEmphasis:highlight(highlights, bufnr)
7871
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.to.start_col, {
7972
ephemeral = ephemeral,
8073
end_col = entry.to.end_col,
81-
hl_group = markers[entry.char].hl_name .. '_delimiter',
74+
hl_group = markers[entry.char].hl_name .. '.delimiter',
8275
spell = markers[entry.char].spell,
8376
priority = 110 + entry.from.start_col,
8477
conceal = conceal,
@@ -136,14 +129,4 @@ function OrgEmphasis:is_valid_end_node(entry, bufnr)
136129
and end_text:sub(1, 1) ~= ' '
137130
end
138131

139-
function OrgEmphasis:_add_hl_groups()
140-
for _, marker in pairs(markers) do
141-
vim.cmd(string.format(marker.hl_cmd, marker.hl_name))
142-
if marker.delimiter_hl then
143-
vim.cmd(string.format(marker.hl_cmd, marker.hl_name .. '_delimiter'))
144-
end
145-
end
146-
vim.cmd('hi def link org_hyperlink Underlined')
147-
end
148-
149132
return OrgEmphasis

lua/orgmode/colors/highlighter/markup/latex.lua

+1-6
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ function OrgLatex:new(opts)
3333
}
3434
setmetatable(data, self)
3535
self.__index = self
36-
data:_add_hl_groups()
3736
return data
3837
end
3938

@@ -89,10 +88,6 @@ function OrgLatex:parse_node(node)
8988
return info
9089
end
9190

92-
function OrgLatex:_add_hl_groups()
93-
vim.cmd('hi def link org_latex OrgTSLatex')
94-
end
95-
9691
---@param highlights OrgMarkupHighlight[]
9792
---@param bufnr number
9893
function OrgLatex:highlight(highlights, bufnr)
@@ -102,7 +97,7 @@ function OrgLatex:highlight(highlights, bufnr)
10297
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col - 1, {
10398
ephemeral = ephemeral,
10499
end_col = entry.to.end_col,
105-
hl_group = 'org_latex',
100+
hl_group = '@org.latex',
106101
spell = false,
107102
priority = 110 + entry.from.start_col,
108103
})

lua/orgmode/colors/highlighter/markup/link.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function OrgLink:highlight(highlights, bufnr)
103103
vim.api.nvim_buf_set_extmark(bufnr, namespace, entry.from.line, entry.from.start_col, {
104104
ephemeral = ephemeral,
105105
end_col = entry.to.end_col,
106-
hl_group = 'org_hyperlink',
106+
hl_group = '@org.hyperlink',
107107
priority = 110,
108108
})
109109

lua/orgmode/colors/highlighter/stars.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function OrgStars:on_line(bufnr, line)
3333
vim.api.nvim_buf_set_extmark(bufnr, self.highlighter.namespace, line, 0, {
3434
end_line = line,
3535
end_col = end_col - 1,
36-
hl_group = 'OrgHideLeadingStars',
36+
hl_group = '@org.leading_stars',
3737
ephemeral = true,
3838
})
3939
end

lua/orgmode/colors/highlighter/todos.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function OrgTodos:_add_highlights()
1919
if not query_files or #query_files == 0 then
2020
return
2121
end
22-
local faces = highlights.parse_todo_keyword_faces()
22+
local faces = highlights.define_todo_keyword_faces()
2323
if not faces or vim.tbl_isempty(faces) then
2424
return
2525
end
@@ -35,7 +35,7 @@ function OrgTodos:_add_highlights()
3535
for face_name, face_hl in pairs(faces) do
3636
table.insert(
3737
lines,
38-
string.format([[(item . (expr) @%s @nospell (#eq? @%s %s))]], face_hl, face_hl, face_name)
38+
string.format([[(item . (expr) %s @nospell (#eq? %s %s))]], face_hl, face_hl, face_name)
3939
)
4040
end
4141
return lines

0 commit comments

Comments
 (0)