summaryrefslogtreecommitdiffstats
path: root/runtime/syntax/dircolors.vim
blob: 24bc31ff7ebf7f05190059a17bbb03d3d35fb372 (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
" Vim syntax file
" Language:        dircolors(1) input file
" Maintainer:      Jan Larres <jan@majutsushi.net>
" Previous Maintainer: Nikolai Weibull <now@bitwi.se>
" Latest Revision: 2018-02-19

if exists("b:current_syntax")
    finish
endif

let s:cpo_save = &cpo
set cpo&vim

syntax keyword dircolorsTodo    FIXME TODO XXX NOTE contained

syntax region  dircolorsComment start='#' end='$' contains=dircolorsTodo,@Spell

syntax keyword dircolorsKeyword TERM LEFT LEFTCODE RIGHT RIGHTCODE END ENDCODE

syntax keyword dircolorsKeyword NORMAL NORM FILE RESET DIR LNK LINK SYMLINK
                              \ MULTIHARDLINK FIFO SOCK DOOR BLK CHR ORPHAN
                              \ MISSING PIPE BLOCK CHR EXEC SETUID SETGID
                              \ CAPABILITY STICKY_OTHER_WRITABLE
                              \ OTHER_WRITABLE STICKY

" Slackware only, ignored by GNU dircolors.
syntax keyword dircolorsKeyword COLOR OPTIONS EIGHTBIT

syntax match dircolorsExtension '^\s*\zs[.*]\S\+'

syntax match dircolorsEscape '\\[abefnrtv?_\\^#]'
syntax match dircolorsEscape '\\[0-9]\{3}'
syntax match dircolorsEscape '\\x[0-9a-f]\{3}'

if !has('gui_running') && &t_Co == ''
    syntax match dircolorsNumber '\<\d\+\>'
    highlight default link dircolorsNumber Number
endif

highlight default link dircolorsTodo      Todo
highlight default link dircolorsComment   Comment
highlight default link dircolorsKeyword   Keyword
highlight default link dircolorsExtension Identifier
highlight default link dircolorsEscape    Special

function! s:set_guicolors() abort
    let s:termguicolors = {}

    let s:termguicolors[0]  = "Black"
    let s:termguicolors[1]  = "DarkRed"
    let s:termguicolors[2]  = "DarkGreen"
    let s:termguicolors[3]  = "DarkYellow"
    let s:termguicolors[4]  = "DarkBlue"
    let s:termguicolors[5]  = "DarkMagenta"
    let s:termguicolors[6]  = "DarkCyan"
    let s:termguicolors[7]  = "Gray"
    let s:termguicolors[8]  = "DarkGray"
    let s:termguicolors[9]  = "Red"
    let s:termguicolors[10] = "Green"
    let s:termguicolors[11] = "Yellow"
    let s:termguicolors[12] = "Blue"
    let s:termguicolors[13] = "Magenta"
    let s:termguicolors[14] = "Cyan"
    let s:termguicolors[15] = "White"

    let xterm_palette = ["00", "5f", "87", "af", "d7", "ff"]

    let cur_col = 16

    for r in xterm_palette
        for g in xterm_palette
            for b in xterm_palette
                let s:termguicolors[cur_col] = '#' . r . g . b
                let cur_col += 1
            endfor
        endfor
    endfor

    for i in range(24)
        let g = i * 0xa + 8
        let s:termguicolors[i + 232] = '#' . g . g . g
    endfor
endfunction

function! s:get_hi_str(color, place) abort
    if a:color >= 0 && a:color <= 255
        if has('gui_running')
            return ' gui' . a:place . '=' . s:termguicolors[a:color]
        elseif a:color <= 7 || &t_Co == 256 || &t_Co == 88
            return ' cterm' . a:place . '=' . a:color
        endif
    endif
    return ''
endfunction

function! s:get_256color(colors) abort
    if len(a:colors) >= 2 " May be fewer while editing
        let [_five, color] = remove(a:colors, 0, 1)
        if _five != '5' || color == ''
            return -1
        else
            return str2nr(color)
        endif
    else
        return -1
    endif
endfunction

function! s:preview_color(linenr) abort
    let line = getline(a:linenr)
    let defline = matchlist(line, '^\v([A-Z_]+|[*.]\S+)\s+([0-9;]+)')
    if empty(defline)
        return
    endif

    let colordef = defline[2]

    let colors = split(colordef, ';')

    let hi_str = ''
    let hi_attrs = []
    while len(colors) > 0
        let item = str2nr(remove(colors, 0))
        if item == 1
            call add(hi_attrs, 'bold')
        elseif item == 3
            call add(hi_attrs, 'italic')
        elseif item == 4
            call add(hi_attrs, 'underline')
        elseif item == 7
            call add(hi_attrs, 'inverse')
        elseif item >= 30 && item <= 37
            " ANSI SGR foreground color
            let hi_str .= s:get_hi_str(item - 30, 'fg')
        elseif item >= 40 && item <= 47
            " ANSI SGR background color
            let hi_str .= s:get_hi_str(item - 40, 'bg')
        elseif item >= 90 && item <= 97
            " ANSI SGR+8 foreground color (xterm 16-color support)
            let hi_str .= s:get_hi_str(item - 82, 'fg')
        elseif item >= 100 && item <= 107
            " ANSI SGR+8 background color (xterm 16-color support)
            let hi_str .= s:get_hi_str(item - 92, 'bg')
        elseif item == 38
            " Foreground for terminals with 88/256 color support
            let color = s:get_256color(colors)
            if color == -1
                break
            endif
            let hi_str .= s:get_hi_str(color, 'fg')
        elseif item == 48
            " Background for terminals with 88/256 color support
            let color = s:get_256color(colors)
            if color == -1
                break
            endif
            let hi_str .= s:get_hi_str(color, 'bg')
        endif
    endwhile

    if hi_str == '' && empty(hi_attrs)
        return
    endif

    " Check whether we have already defined this color
    redir => s:currentmatch
    silent! execute 'syntax list'
    redir END

    if s:currentmatch !~# '\/\\_s\\zs' . colordef . '\\ze\\_s\/'
        " Append the buffer number to avoid problems with other dircolors
        " buffers interfering
        let bufnr = bufnr('%')
        execute 'syntax match dircolorsColor' . b:dc_next_index . '_' . bufnr .
              \ ' "\_s\zs' . colordef . '\ze\_s"'
        let hi_attrs_str = ''
        if !empty(hi_attrs)
            if has('gui_running')
                let hi_attrs_str = ' gui=' . join(hi_attrs, ',')
            else
                let hi_attrs_str = ' cterm=' . join(hi_attrs, ',')
            endif
        endif
        execute 'highlight default dircolorsColor' . b:dc_next_index . '_' .
              \ bufnr . hi_str . hi_attrs_str
        let b:dc_next_index += 1
    endif
endfunction

" Avoid accumulating too many definitions while editing
function! s:reset_colors() abort
    if b:dc_next_index > 0
        let bufnr = bufnr('%')
        for i in range(b:dc_next_index)
            execute 'syntax clear dircolorsColor' . i . '_' . bufnr
            execute 'highlight clear dircolorsColor' . i . '_' . bufnr
        endfor
        let b:dc_next_index = 0
    endif

    for linenr in range(1, line('$'))
        call s:preview_color(linenr)
    endfor
endfunction

let b:dc_next_index = 0

if has('gui_running')
    call s:set_guicolors()
endif

if has('gui_running') || &t_Co != ''
    call s:reset_colors()

    autocmd CursorMoved,CursorMovedI <buffer> call s:preview_color('.')
    autocmd CursorHold,CursorHoldI   <buffer> call s:reset_colors()
endif

let b:current_syntax = "dircolors"

let &cpo = s:cpo_save
unlet s:cpo_save