gruvbox_material.vim 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. " =============================================================================
  2. " URL: https://github.com/sainnhe/gruvbox-material
  3. " Filename: autoload/gruvbox_material.vim
  4. " Author: sainnhe
  5. " Email: sainnhe@gmail.com
  6. " License: MIT License
  7. " =============================================================================
  8. function! gruvbox_material#get_configuration() "{{{
  9. return {
  10. \ 'background': get(g:, 'gruvbox_material_background', 'medium'),
  11. \ 'palette': get(g:, 'gruvbox_material_palette', 'material'),
  12. \ 'transparent_background': get(g:, 'gruvbox_material_transparent_background', 0),
  13. \ 'disable_italic_comment': get(g:, 'gruvbox_material_disable_italic_comment', 0),
  14. \ 'enable_bold': get(g:, 'gruvbox_material_enable_bold', 0),
  15. \ 'enable_italic': get(g:, 'gruvbox_material_enable_italic', 0),
  16. \ 'cursor': get(g:, 'gruvbox_material_cursor', 'auto'),
  17. \ 'visual': get(g:, 'gruvbox_material_visual', 'grey background'),
  18. \ 'menu_selection_background': get(g:, 'gruvbox_material_menu_selection_background', 'grey'),
  19. \ 'sign_column_background': get(g:, 'gruvbox_material_sign_column_background', 'default'),
  20. \ 'ui_contrast': get(g:, 'gruvbox_material_ui_contrast', 'low'),
  21. \ 'show_eob': get(g:, 'gruvbox_material_show_eob', 1),
  22. \ 'current_word': get(g:, 'gruvbox_material_current_word', get(g:, 'gruvbox_material_transparent_background', 0) == 0 ? 'grey background' : 'bold'),
  23. \ 'statusline_style': get(g:, 'gruvbox_material_statusline_style', 'default'),
  24. \ 'lightline_disable_bold': get(g:, 'gruvbox_material_lightline_disable_bold', 0),
  25. \ 'diagnostic_text_highlight': get(g:, 'gruvbox_material_diagnostic_text_highlight', 0),
  26. \ 'diagnostic_line_highlight': get(g:, 'gruvbox_material_diagnostic_line_highlight', 0),
  27. \ 'diagnostic_virtual_text': get(g:, 'gruvbox_material_diagnostic_virtual_text', 'grey'),
  28. \ 'better_performance': get(g:, 'gruvbox_material_better_performance', 0),
  29. \ }
  30. endfunction "}}}
  31. function! gruvbox_material#get_palette(background, palette) "{{{
  32. if type(a:palette) == 4
  33. return a:palette
  34. endif
  35. if a:background ==# 'hard' "{{{
  36. if &background ==# 'dark'
  37. let palette1 = {
  38. \ 'bg0': ['#1d2021', '234'],
  39. \ 'bg1': ['#282828', '235'],
  40. \ 'bg2': ['#282828', '235'],
  41. \ 'bg3': ['#3c3836', '237'],
  42. \ 'bg4': ['#3c3836', '237'],
  43. \ 'bg5': ['#504945', '239'],
  44. \ 'bg_statusline1': ['#282828', '235'],
  45. \ 'bg_statusline2': ['#32302f', '235'],
  46. \ 'bg_statusline3': ['#504945', '239'],
  47. \ 'bg_diff_green': ['#32361a', '22'],
  48. \ 'bg_visual_green': ['#333e34', '22'],
  49. \ 'bg_diff_red': ['#3c1f1e', '52'],
  50. \ 'bg_visual_red': ['#442e2d', '52'],
  51. \ 'bg_diff_blue': ['#0d3138', '17'],
  52. \ 'bg_visual_blue': ['#2e3b3b', '17'],
  53. \ 'bg_visual_yellow': ['#473c29', '94'],
  54. \ 'bg_current_word': ['#32302f', '236']
  55. \ }
  56. else
  57. let palette1 = {
  58. \ 'bg0': ['#f9f5d7', '230'],
  59. \ 'bg1': ['#f5edca', '229'],
  60. \ 'bg2': ['#f3eac7', '229'],
  61. \ 'bg3': ['#f2e5bc', '228'],
  62. \ 'bg4': ['#eee0b7', '223'],
  63. \ 'bg5': ['#ebdbb2', '223'],
  64. \ 'bg_statusline1': ['#f5edca', '223'],
  65. \ 'bg_statusline2': ['#f3eac7', '223'],
  66. \ 'bg_statusline3': ['#eee0b7', '250'],
  67. \ 'bg_diff_green': ['#e4edc8', '194'],
  68. \ 'bg_visual_green': ['#dde5c2', '194'],
  69. \ 'bg_diff_red': ['#f8e4c9', '217'],
  70. \ 'bg_visual_red': ['#f0ddc3', '217'],
  71. \ 'bg_diff_blue': ['#e0e9d3', '117'],
  72. \ 'bg_visual_blue': ['#d9e1cc', '117'],
  73. \ 'bg_visual_yellow': ['#f9eabf', '226'],
  74. \ 'bg_current_word': ['#f3eac7', '229']
  75. \ }
  76. endif "}}}
  77. elseif a:background ==# 'medium' "{{{
  78. if &background ==# 'dark'
  79. let palette1 = {
  80. \ 'bg0': ['#282828', '235'],
  81. \ 'bg1': ['#32302f', '236'],
  82. \ 'bg2': ['#32302f', '236'],
  83. \ 'bg3': ['#45403d', '237'],
  84. \ 'bg4': ['#45403d', '237'],
  85. \ 'bg5': ['#5a524c', '239'],
  86. \ 'bg_statusline1': ['#32302f', '236'],
  87. \ 'bg_statusline2': ['#3a3735', '236'],
  88. \ 'bg_statusline3': ['#504945', '240'],
  89. \ 'bg_diff_green': ['#34381b', '22'],
  90. \ 'bg_visual_green': ['#3b4439', '22'],
  91. \ 'bg_diff_red': ['#402120', '52'],
  92. \ 'bg_visual_red': ['#4c3432', '52'],
  93. \ 'bg_diff_blue': ['#0e363e', '17'],
  94. \ 'bg_visual_blue': ['#374141', '17'],
  95. \ 'bg_visual_yellow': ['#4f422e', '94'],
  96. \ 'bg_current_word': ['#3c3836', '237']
  97. \ }
  98. else
  99. let palette1 = {
  100. \ 'bg0': ['#fbf1c7', '229'],
  101. \ 'bg1': ['#f4e8be', '228'],
  102. \ 'bg2': ['#f2e5bc', '228'],
  103. \ 'bg3': ['#eee0b7', '223'],
  104. \ 'bg4': ['#e5d5ad', '223'],
  105. \ 'bg5': ['#ddccab', '250'],
  106. \ 'bg_statusline1': ['#f2e5bc', '223'],
  107. \ 'bg_statusline2': ['#f2e5bc', '223'],
  108. \ 'bg_statusline3': ['#e5d5ad', '250'],
  109. \ 'bg_diff_green': ['#e6eabc', '194'],
  110. \ 'bg_visual_green': ['#dee2b6', '194'],
  111. \ 'bg_diff_red': ['#f9e0bb', '217'],
  112. \ 'bg_visual_red': ['#f1d9b5', '217'],
  113. \ 'bg_diff_blue': ['#e2e6c7', '117'],
  114. \ 'bg_visual_blue': ['#dadec0', '117'],
  115. \ 'bg_visual_yellow': ['#fae7b3', '226'],
  116. \ 'bg_current_word': ['#f2e5bc', '228']
  117. \ }
  118. endif "}}}
  119. else "{{{
  120. if &background ==# 'dark'
  121. let palette1 = {
  122. \ 'bg0': ['#32302f', '236'],
  123. \ 'bg1': ['#3c3836', '237'],
  124. \ 'bg2': ['#3c3836', '237'],
  125. \ 'bg3': ['#504945', '239'],
  126. \ 'bg4': ['#504945', '239'],
  127. \ 'bg5': ['#665c54', '241'],
  128. \ 'bg_statusline1': ['#3c3836', '237'],
  129. \ 'bg_statusline2': ['#46413e', '237'],
  130. \ 'bg_statusline3': ['#5b534d', '241'],
  131. \ 'bg_diff_green': ['#3d4220', '22'],
  132. \ 'bg_visual_green': ['#424a3e', '22'],
  133. \ 'bg_diff_red': ['#472322', '52'],
  134. \ 'bg_visual_red': ['#543937', '52'],
  135. \ 'bg_diff_blue': ['#0f3a42', '17'],
  136. \ 'bg_visual_blue': ['#404946', '17'],
  137. \ 'bg_visual_yellow': ['#574833', '94'],
  138. \ 'bg_current_word': ['#45403d', '238']
  139. \ }
  140. else
  141. let palette1 = {
  142. \ 'bg0': ['#f2e5bc', '228'],
  143. \ 'bg1': ['#eddeb5', '223'],
  144. \ 'bg2': ['#ebdbb2', '228'],
  145. \ 'bg3': ['#e6d5ae', '223'],
  146. \ 'bg4': ['#dac9a5', '250'],
  147. \ 'bg5': ['#d5c4a1', '250'],
  148. \ 'bg_statusline1': ['#ebdbb2', '223'],
  149. \ 'bg_statusline2': ['#ebdbb2', '223'],
  150. \ 'bg_statusline3': ['#dac9a5', '250'],
  151. \ 'bg_diff_green': ['#dfe1b4', '194'],
  152. \ 'bg_visual_green': ['#d7d9ae', '194'],
  153. \ 'bg_diff_red': ['#f7d9b9', '217'],
  154. \ 'bg_visual_red': ['#efd2b3', '217'],
  155. \ 'bg_diff_blue': ['#dbddbf', '117'],
  156. \ 'bg_visual_blue': ['#d3d5b8', '117'],
  157. \ 'bg_visual_yellow': ['#f3deaa', '226'],
  158. \ 'bg_current_word': ['#ebdbb2', '227']
  159. \ }
  160. endif
  161. endif "}}}
  162. if a:palette ==# 'material' "{{{
  163. if &background ==# 'dark'
  164. let palette2 = {
  165. \ 'fg0': ['#d4be98', '223'],
  166. \ 'fg1': ['#ddc7a1', '223'],
  167. \ 'red': ['#ea6962', '167'],
  168. \ 'orange': ['#e78a4e', '208'],
  169. \ 'yellow': ['#d8a657', '214'],
  170. \ 'green': ['#a9b665', '142'],
  171. \ 'aqua': ['#89b482', '108'],
  172. \ 'blue': ['#7daea3', '109'],
  173. \ 'purple': ['#d3869b', '175'],
  174. \ 'bg_red': ['#ea6962', '167'],
  175. \ 'bg_green': ['#a9b665', '142'],
  176. \ 'bg_yellow': ['#d8a657', '214']
  177. \ }
  178. else
  179. let palette2 = {
  180. \ 'fg0': ['#654735', '237'],
  181. \ 'fg1': ['#4f3829', '237'],
  182. \ 'red': ['#c14a4a', '88'],
  183. \ 'orange': ['#c35e0a', '130'],
  184. \ 'yellow': ['#b47109', '136'],
  185. \ 'green': ['#6c782e', '100'],
  186. \ 'aqua': ['#4c7a5d', '165'],
  187. \ 'blue': ['#45707a', '24'],
  188. \ 'purple': ['#945e80', '96'],
  189. \ 'bg_red': ['#ae5858', '88'],
  190. \ 'bg_green': ['#6f8352', '100'],
  191. \ 'bg_yellow': ['#a96b2c', '130']
  192. \ }
  193. endif "}}}
  194. elseif a:palette ==# 'mix' "{{{
  195. if &background ==# 'dark'
  196. let palette2 = {
  197. \ 'fg0': ['#e2cca9', '223'],
  198. \ 'fg1': ['#e2cca9', '223'],
  199. \ 'red': ['#f2594b', '167'],
  200. \ 'orange': ['#f28534', '208'],
  201. \ 'yellow': ['#e9b143', '214'],
  202. \ 'green': ['#b0b846', '142'],
  203. \ 'aqua': ['#8bba7f', '108'],
  204. \ 'blue': ['#80aa9e', '109'],
  205. \ 'purple': ['#d3869b', '175'],
  206. \ 'bg_red': ['#db4740', '167'],
  207. \ 'bg_green': ['#b0b846', '142'],
  208. \ 'bg_yellow': ['#e9b143', '214']
  209. \ }
  210. else
  211. let palette2 = {
  212. \ 'fg0': ['#514036', '237'],
  213. \ 'fg1': ['#514036', '237'],
  214. \ 'red': ['#af2528', '88'],
  215. \ 'orange': ['#b94c07', '130'],
  216. \ 'yellow': ['#b4730e', '136'],
  217. \ 'green': ['#72761e', '100'],
  218. \ 'aqua': ['#477a5b', '165'],
  219. \ 'blue': ['#266b79', '24'],
  220. \ 'purple': ['#924f79', '96'],
  221. \ 'bg_red': ['#ae5858', '88'],
  222. \ 'bg_green': ['#6f8352', '100'],
  223. \ 'bg_yellow': ['#a96b2c', '130']
  224. \ }
  225. endif "}}}
  226. else "{{{
  227. if &background ==# 'dark'
  228. let palette2 = {
  229. \ 'fg0': ['#ebdbb2', '223'],
  230. \ 'fg1': ['#ebdbb2', '223'],
  231. \ 'red': ['#fb4934', '167'],
  232. \ 'orange': ['#fe8019', '208'],
  233. \ 'yellow': ['#fabd2f', '214'],
  234. \ 'green': ['#b8bb26', '142'],
  235. \ 'aqua': ['#8ec07c', '108'],
  236. \ 'blue': ['#83a598', '109'],
  237. \ 'purple': ['#d3869b', '175'],
  238. \ 'bg_red': ['#cc241d', '124'],
  239. \ 'bg_green': ['#b8bb26', '106'],
  240. \ 'bg_yellow': ['#fabd2f', '172']
  241. \ }
  242. else
  243. let palette2 = {
  244. \ 'fg0': ['#3c3836', '237'],
  245. \ 'fg1': ['#3c3836', '237'],
  246. \ 'red': ['#9d0006', '88'],
  247. \ 'orange': ['#af3a03', '130'],
  248. \ 'yellow': ['#b57614', '136'],
  249. \ 'green': ['#79740e', '100'],
  250. \ 'aqua': ['#427b58', '165'],
  251. \ 'blue': ['#076678', '24'],
  252. \ 'purple': ['#8f3f71', '96'],
  253. \ 'bg_red': ['#ae5858', '88'],
  254. \ 'bg_green': ['#6f8352', '100'],
  255. \ 'bg_yellow': ['#a96b2c', '130']
  256. \ }
  257. endif
  258. endif "}}}
  259. if &background ==# 'dark' "{{{
  260. let palette3 = {
  261. \ 'grey0': ['#7c6f64', '243'],
  262. \ 'grey1': ['#928374', '245'],
  263. \ 'grey2': ['#a89984', '246'],
  264. \ 'none': ['NONE', 'NONE']
  265. \ } "}}}
  266. else "{{{
  267. let palette3 = {
  268. \ 'grey0': ['#a89984', '246'],
  269. \ 'grey1': ['#928374', '245'],
  270. \ 'grey2': ['#7c6f64', '243'],
  271. \ 'none': ['NONE', 'NONE']
  272. \ }
  273. endif "}}}
  274. return extend(extend(palette1, palette2), palette3)
  275. endfunction "}}}
  276. function! gruvbox_material#highlight(group, fg, bg, ...) "{{{
  277. execute 'highlight' a:group
  278. \ 'guifg=' . a:fg[0]
  279. \ 'guibg=' . a:bg[0]
  280. \ 'ctermfg=' . a:fg[1]
  281. \ 'ctermbg=' . a:bg[1]
  282. \ 'gui=' . (a:0 >= 1 ?
  283. \ a:1 :
  284. \ 'NONE')
  285. \ 'cterm=' . (a:0 >= 1 ?
  286. \ (a:1 ==# 'undercurl' ?
  287. \ 'underline' :
  288. \ a:1) :
  289. \ 'NONE')
  290. \ 'guisp=' . (a:0 >= 2 ?
  291. \ a:2[0] :
  292. \ 'NONE')
  293. endfunction "}}}
  294. function! gruvbox_material#ft_gen(path, last_modified, msg) "{{{
  295. " Generate the `after/ftplugin` directory.
  296. let full_content = join(readfile(a:path), "\n") " Get the content of `colors/gruvbox-material.vim`
  297. let ft_content = []
  298. let rootpath = gruvbox_material#ft_rootpath(a:path) " Get the path to place the `after/ftplugin` directory.
  299. call substitute(full_content, '" ft_begin.\{-}ft_end', '\=add(ft_content, submatch(0))', 'g') " Search for 'ft_begin.\{-}ft_end' (non-greedy) and put all the search results into a list.
  300. for content in ft_content
  301. let ft_list = []
  302. call substitute(matchstr(matchstr(content, 'ft_begin:.\{-}{{{'), ':.\{-}{{{'), '\(\w\|-\)\+', '\=add(ft_list, submatch(0))', 'g') " Get the file types. }}}}}}
  303. for ft in ft_list
  304. call gruvbox_material#ft_write(rootpath, ft, content) " Write the content.
  305. endfor
  306. endfor
  307. call gruvbox_material#ft_write(rootpath, 'text', "let g:gruvbox_material_last_modified = '" . a:last_modified . "'") " Write the last modified time to `after/ftplugin/text/gruvbox_material.vim`
  308. if a:msg ==# 'update'
  309. echohl WarningMsg | echom '[gruvbox-material] Updated ' . rootpath . '/after/ftplugin' | echohl None
  310. else
  311. echohl WarningMsg | echom '[gruvbox-material] Generated ' . rootpath . '/after/ftplugin' | echohl None
  312. endif
  313. endfunction "}}}
  314. function! gruvbox_material#ft_write(rootpath, ft, content) "{{{
  315. " Write the content.
  316. let ft_path = a:rootpath . '/after/ftplugin/' . a:ft . '/gruvbox_material.vim' " The path of a ftplugin file.
  317. " create a new file if it doesn't exist
  318. if !filereadable(ft_path)
  319. call mkdir(a:rootpath . '/after/ftplugin/' . a:ft, 'p')
  320. call writefile([
  321. \ "if !exists('g:colors_name') || g:colors_name !=# 'gruvbox-material'",
  322. \ ' finish',
  323. \ 'endif'
  324. \ ], ft_path, 'a') " Abort if the current color scheme is not gruvbox-material.
  325. call writefile([
  326. \ "if index(g:gruvbox_material_loaded_file_types, '" . a:ft . "') ==# -1",
  327. \ " call add(g:gruvbox_material_loaded_file_types, '" . a:ft . "')",
  328. \ 'else',
  329. \ ' finish',
  330. \ 'endif'
  331. \ ], ft_path, 'a') " Abort if this file type has already been loaded.
  332. endif
  333. " If there is something like `call gruvbox_material#highlight()`, then add
  334. " code to initialize the palette and configuration.
  335. if matchstr(a:content, 'gruvbox_material#highlight') !=# ''
  336. call writefile([
  337. \ 'let s:configuration = gruvbox_material#get_configuration()',
  338. \ 'let s:palette = gruvbox_material#get_palette(s:configuration.background, s:configuration.palette)'
  339. \ ], ft_path, 'a')
  340. endif
  341. " Append the content.
  342. call writefile(split(a:content, "\n"), ft_path, 'a')
  343. " Add modeline.
  344. call writefile(['" vim: set sw=2 ts=2 sts=2 et tw=80 ft=vim fdm=marker fmr={{{,}}}:'], ft_path, 'a')
  345. endfunction "}}}
  346. function! gruvbox_material#ft_rootpath(path) "{{{
  347. " Get the directory where `after/ftplugin` is generated.
  348. if (matchstr(a:path, '^/usr/share') ==# '') || has('win32') " Return the plugin directory. The `after/ftplugin` directory should never be generated in `/usr/share`, even if you are a root user.
  349. return fnamemodify(a:path, ':p:h:h')
  350. else " Use vim home directory.
  351. if has('nvim')
  352. return stdpath('config')
  353. else
  354. if has('win32') || has ('win64')
  355. return $VIM . '/vimfiles'
  356. else
  357. return $HOME . '/.vim'
  358. endif
  359. endif
  360. endif
  361. endfunction "}}}
  362. function! gruvbox_material#ft_newest(path, last_modified) "{{{
  363. " Determine whether the current ftplugin files are up to date by comparing the last modified time in `colors/gruvbox-material.vim` and `after/ftplugin/text/gruvbox_material.vim`.
  364. let rootpath = gruvbox_material#ft_rootpath(a:path)
  365. execute 'source ' . rootpath . '/after/ftplugin/text/gruvbox_material.vim'
  366. return a:last_modified ==# g:gruvbox_material_last_modified ? 1 : 0
  367. endfunction "}}}
  368. function! gruvbox_material#ft_clean(path, msg) "{{{
  369. " Clean the `after/ftplugin` directory.
  370. let rootpath = gruvbox_material#ft_rootpath(a:path)
  371. " Remove `after/ftplugin/**/gruvbox_material.vim`.
  372. let file_list = split(globpath(rootpath, 'after/ftplugin/**/gruvbox_material.vim'), "\n")
  373. for file in file_list
  374. call delete(file)
  375. endfor
  376. " Remove empty directories.
  377. let dir_list = split(globpath(rootpath, 'after/ftplugin/*'), "\n")
  378. for dir in dir_list
  379. if globpath(dir, '*') ==# ''
  380. call delete(dir, 'd')
  381. endif
  382. endfor
  383. if globpath(rootpath . '/after/ftplugin', '*') ==# ''
  384. call delete(rootpath . '/after/ftplugin', 'd')
  385. endif
  386. if globpath(rootpath . '/after', '*') ==# ''
  387. call delete(rootpath . '/after', 'd')
  388. endif
  389. if a:msg
  390. echohl WarningMsg | echom '[gruvbox-material] Cleaned ' . rootpath . '/after/ftplugin' | echohl None
  391. endif
  392. endfunction "}}}
  393. function! gruvbox_material#ft_exists(path) "{{{
  394. return filereadable(gruvbox_material#ft_rootpath(a:path) . '/after/ftplugin/text/gruvbox_material.vim')
  395. endfunction "}}}
  396. " vim: set sw=2 ts=2 sts=2 et tw=80 ft=vim fdm=marker fmr={{{,}}}: