-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathhighlight.fnl
93 lines (80 loc) · 4.13 KB
/
highlight.fnl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
(local util (require "leap.util"))
(local {: inc : dec} util)
(local api vim.api)
(local map vim.tbl_map)
(local M {:ns (api.nvim_create_namespace "")
:extmarks []
:group {:label-primary "LeapLabelPrimary"
:label-secondary "LeapLabelSecondary"
:label-selected "LeapLabelSelected"
:match "LeapMatch"
:backdrop "LeapBackdrop"}
:priority {:label 65535
:cursor 65534
:backdrop 65533}})
(fn M.cleanup [self affected-windows]
; Clear beacons & cursor.
(each [_ [bufnr id] (ipairs self.extmarks)]
(api.nvim_buf_del_extmark bufnr self.ns id))
(set self.extmarks [])
; Clear backdrop.
(when (pcall api.nvim_get_hl_by_name self.group.backdrop false) ; group exists?
(each [_ winid (ipairs affected-windows)]
(local wininfo (. (vim.fn.getwininfo winid) 1))
(api.nvim_buf_clear_namespace
wininfo.bufnr self.ns (dec wininfo.topline) wininfo.botline))
; Safety measure for scrolloff > 0: we always clean up the current view too.
(api.nvim_buf_clear_namespace 0 self.ns
(dec (vim.fn.line "w0"))
(vim.fn.line "w$"))))
(fn M.apply-backdrop [self ranges]
(when (pcall api.nvim_get_hl_by_name self.group.backdrop false) ; group exists?
(each [_ range (ipairs ranges)]
(vim.highlight.range range.bufnr self.ns self.group.backdrop
[range.startrow range.startcol]
[range.endrow range.endcol]
{:priority self.priority.backdrop}))))
(fn M.highlight-cursor [self ?pos]
"The cursor is down on the command line during `getchar`,
so we set a temporary highlight on it to see where we are."
(let [[line col &as pos] (or ?pos (util.get-cursor-pos))
; nil means the cursor is on an empty line.
ch-at-curpos (or (util.get-char-at pos {}) " ") ; get-char-at needs 1,1-idx
; (Ab)using extmarks even here, to be able to highlight the cursor on empty lines too.
id (api.nvim_buf_set_extmark 0 self.ns (dec line) (dec col)
{:virt_text [[ch-at-curpos :Cursor]]
:virt_text_pos "overlay"
:hl_mode "combine"
:priority self.priority.cursor})]
(table.insert self.extmarks [(api.nvim_get_current_buf) id])))
(fn M.init-highlight [self force?]
(let [bg vim.o.background
defaults {self.group.match {:fg (case bg
:light "#222222"
_ "#ccff88")
:ctermfg "red"
:underline true
:nocombine true}
self.group.label-primary {:fg "black"
:bg (case bg
:light "#ff8877"
_ "#ccff88")
:ctermfg "black"
:ctermbg "red"
:nocombine true}
self.group.label-secondary {:fg "black"
:bg (case bg
:light "#77aaff"
_ "#99ccff")
:ctermfg "black"
:ctermbg "blue"
:nocombine true}
self.group.label-selected {:fg "black"
:bg "magenta"
:ctermfg "black"
:ctermbg "magenta"
:nocombine true}}]
(each [group-name def-map (pairs defaults)]
(when (not force?) (set def-map.default true))
(api.nvim_set_hl 0 group-name def-map))))
M