summaryrefslogtreecommitdiff
path: root/nvim/lsp.vim
blob: 80ac5c28d4f238d75f5d7d12d20fdf4486187c5c (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
"                                                      .     :-. .
"    author:  marcellus </var/spool/mail/marcellus>    :*==*%%%#%+=---:
"                                                       :#%%%*+%#=+%*:.
"    created: Sun, 14 May 2023 01:07:47 +0200            :%%%. .   -*%-
"    updated: Sun, 14 May 2023 01:08:07 +0200             =## .   :#%*=:.
"                                                         -#*#%:=#%%%#-
"    description:                                        *:*%%%%%%%#-
"                                                         .-#%%%%%%+
"                                                          %%%--%%%%*-
"                                                         ##%=  +%%=:..
"                                                          #*    #%#
"                                                         :#     -==*
"                                                         ::        .:

"                                                      .     :-. .
"    author:  ratakor <ratakor@disroot.org>            :*==*%%%#%+=---:
"                                                       :#%%%*+%#=+%*:.
"    created: Sat, 06 May 2023 18:54:06 +0200            :%%%. .   -*%-
"    updated: Sat, 13 May 2023 13:16:03 +0200             =## .   :#%*=:.
"                                                         -#*#%:=#%%%#-
"    description:                                        *:*%%%%%%%#-
"    script to produce an header like this one            .-#%%%%%%+
"    use .local/bin/updateheader to change the             %%%--%%%%*-
"    letter shown                                         ##%=  +%%=:..
"                                                          #*    #%#
"                                                         :#     -==*
"                                                         ::        .:

let s:ascii = [
              \"",
	      \".     :-. .",
              \":*==*%%%#%+=---:",
              \" :#%%%*+%#=+%*:.",
              \"  :%%%. .   -*%-",
              \"   =## .   :#%*=:.",
              \"   -#*#%:=#%%%#-",
              \"  *:*%%%%%%%#-",
              \"   .-#%%%%%%+",
              \"    %%%--%%%%*-",
              \"   ##%=  +%%=:..",
              \"    #*    #%#",
              \"   :#     -==*",
              \"   ::        .:",
              \]

let s:start   = '#'
let s:mid     = '#'
let s:end     = ''
let s:size    = 20 " ascii width
let s:length  = len(s:ascii) " header length
let s:width   = 80 " header width
let s:margin  = 5

let s:types   = {
                \'\.c$\|\.h$\|\.cc$\|\.hh$\|\.cpp$\|\.hpp$\|\.cs$\|\.php$':
                \['/*', ' *', ' */'],
                \'\.htm$\|\.html$\|\.xml$':
                \['<!--', ' ', '-->'],
                \'\.js$':
                \['//', '//', ''],
                \'\.tex$':
                \['%', '%', ''],
                \'\.ml$\|\.mli$\|\.mll$\|\.mly$':
                \['(*', ' ', '*)'],
                \'\.vim$\|\vimrc$':
                \['"', '"', ''],
                \'\.f90$\|\.f95$\|\.f03$\|\.f$\|\.for$':
                \['!', '!', ''],
                \}

function! s:filetype()
	let l:f = expand("%:t")

	for type in keys(s:types)
		if l:f =~ type
			let s:start = s:types[type][0]
			let s:mid   = s:types[type][1]
			let s:end   = s:types[type][2]
		endif
	endfor

endfunction

function! s:textline(txt, pos)
	let l:txt = strpart(a:txt, 0, s:width - s:margin * 2 - s:size)

	if a:pos == 1
		return s:start . repeat(' ', s:margin - strlen(s:start)) . l:txt . repeat(' ', s:width - s:margin * 2 - strlen(l:txt) - s:size) . s:ascii[a:pos]
	elseif a:pos == s:length
		return s:end
	else
		return s:mid . repeat(' ', s:margin - strlen(s:start)) . l:txt . repeat(' ', s:width - s:margin * 2 - strlen(l:txt) - s:size) . s:ascii[a:pos]
endfunction

function! s:line(n)
	if a:n == 2
		return s:textline("author:  " . s:user() . " <" . s:mail() . ">", a:n)
"	elseif a:n == 3
"		return s:textline("license: " . "ICS license", a:n)
	elseif a:n == 4
		return s:textline("created: " . s:date(), a:n)
	elseif a:n == 5
		return s:textline("updated: " . s:date(), a:n)
	elseif a:n == 7
		return s:textline("description: ", a:n)
	else
		return s:textline('',  a:n)
	endif
endfunction

function! s:user()
	let l:user = $USER
	if strlen(l:user) == 0
		let l:user = "user"
	endif
	return l:user
endfunction

function! s:mail()
	let l:mail = $MAIL
	if strlen(l:mail) == 0
		let l:mail = "user@mail.org"
	endif
	return l:mail
endfunction

function! s:date()
	return strftime("%a, %d %b %Y %H:%M:%S %z")
endfunction

function! s:insert()
	let l:line = s:length

	while l:line > 0
		call append(0, s:line(l:line))
		let l:line = l:line - 1
	endwhile
endfunction

function! s:update()
	let line = 0
	call s:filetype()
	if getline(5) =~ s:start . repeat(' ', s:margin - strlen(s:start)) . "updated: "
		let line = 5
	elseif getline(6) =~ s:start . repeat(' ', s:margin - strlen(s:start)) . "updated: "
		let line = 6
	endif
	if line > 0 && &mod
		call setline(line, s:line(5))
		return 0
	endif
	return 1
endfunction

function! s:header()
	if s:update()
		call s:insert()
	endif
endfunction

command! Header call s:header ()
autocmd BufNewFile  * call s:header ()
autocmd BufWritePre * call s:update ()