summary refs log tree commit diff
path: root/.vim/init.lua
blob: 774a0982206c63a599b36e11e9cd7c8675d70e70 (plain) (blame)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
-- vim: fdm=marker fdl=0
local cmd = vim.cmd
local g = vim.g

g.mapleader = ' ' -- <leader> key to <space>
g.maplocalleader = '\\'

-- OPTIONS -- {{{
local opt = vim.opt

opt.backspace = 'indent,eol,start'
opt.clipboard:prepend { 'unnamedplus' }
opt.cmdheight = 1
opt.colorcolumn = {80} -- depends on 'textwidth'
-- opt.complete = '.,w,b,u'
opt.completeopt = {'menu','menuone','noinsert'}
opt.concealcursor = ''
opt.conceallevel = 2
opt.cursorline = true
opt.expandtab = false
opt.exrc = true
opt.fillchars:append {
  fold = '─',
  horiz = '─',
}
opt.foldenable = true
opt.foldmethod = 'expr'
opt.foldexpr = 'nvim_treesitter#foldexpr()'
-- opt.foldlevel = 0
opt.foldlevelstart = 1
opt.hidden = true
opt.hlsearch = false
opt.ignorecase = true
opt.inccommand = 'split'
opt.incsearch = true
-- 3; https://github.com/neovim/neovim/pull/17266
opt.laststatus = 3
opt.lazyredraw = true
opt.list = true
opt.listchars:append {
  trail = '¬',
  tab = '> ',
  -- space/lead = '⋅',
}
opt.mouse = 'a'
opt.number = true
opt.relativenumber = false
opt.pumheight = 0 -- use available screenspace
opt.redrawtime = 1500
opt.ruler = true
opt.scrolloff = 3
opt.shiftround = true
opt.shortmess:append { c = true }
opt.breakindent = true
opt.breakindentopt = { shift = 2 }
opt.showbreak = '↪ '
opt.showcmd = false
opt.showmode = true
opt.showmatch = true
opt.signcolumn = 'auto'
opt.smartcase = true
opt.spell = false
opt.spelloptions = 'camel'
opt.termguicolors = true
opt.textwidth = 0
opt.timeoutlen = 400
opt.title = true
opt.ttimeoutlen = 10
opt.undolevels = 1000
opt.updatetime = 200
opt.wrap = true
opt.path = {'', '.', '.;$HOME', '/usr/include/**'}
opt.isfname:remove ':'
opt.guicursor = {
  -- block shape in normal/visual/command
  'n-c-v:block',
  -- bar in insert/cmdline-insert/visual-exclusive
  'i-ci-ve:ver15',
  -- underline in replace/cmdline-replace/operator-pending
  'r-cr-o:hor10',

  'sm:block-blinkwait175-blinkoff150-blinkon175',
  -- 'a:blinkwait700-blinkoff400-blinkon250-Cursor/lCursor',
  'a:blinkon0-Cursor',
}

-- configure :grep to use ripgrep if it's available
if vim.fn.executable('rg') then
  opt.grepprg = 'rg --vimgrep --smart-case --hidden'
  opt.grepformat = '%f:%l:%c:%m'
end

-- persistent undo
local undodir = vim.fn.stdpath('cache') .. '/undodir_' .. vim.env.USER
if vim.fn.isdirectory(undodir) == 0 then
  vim.fn.mkdir(undodir, 'p', '0700')
end
g.undodir = undodir
opt.undofile = true
-- }}}

g.do_filetype_lua = 1
g.did_load_filetypes = 0

-- COLORS -- {{{
-- g.nihoncolors_transparent_background = true
cmd [[ colorscheme lush ]]
-- }}}

require('globals')
require('plugins')
require('hardmode')

-- Configure builtin diagnositics {{{
vim.diagnostic.config {
  underline        = true,
  virtual_text     = false,
  -- virtual_text     = {
  --   severity = {min=vim.diagnostic.severity.WARN},
  --   source = true,
  --   prefix = '',
  -- },
  signs            = true,
  float            = { scope = 'line' },
  virtual_lines    = { prefix = '⬐  ' },

  update_in_insert = false,
  severity_sort    = true,
}
do
  local group = vim.api.nvim_create_augroup('DiagnosticOpenFloat', { clear = true })
  vim.api.nvim_create_autocmd('CursorHold', { callback = function()
    if not _G.diagnostic_hidden then vim.diagnostic.open_float() end
  end, group = group })
end

vim.fn.sign_define({
  {name='DiagnosticSignError', text='✗', texthl='DiagnosticSignError'},
  -- {name='DiagnosticSignWarn',  text='⚡', texthl='DiagnosticSignWarn'},
  {name='DiagnosticSignWarn',  text='⚐',  texthl='DiagnosticSignWarn'},
  {name='DiagnosticSignInfo',  text='ℹ',  texthl='DiagnosticSignInfo'},
  {name='DiagnosticSignHint',  text='🞶',  texthl='DiagnosticSignHint'}
})
vim.keymap.set('n',  ']d', vim.diagnostic.goto_next)
vim.keymap.set('n',  '[d', vim.diagnostic.goto_prev)
vim.keymap.set('n', '<leader>d', vim.diagnostic.open_float)
vim.keymap.set('n', '<leader>D', function()
    if not _G.diagnostic_hidden then
      vim.diagnostic.hide()
    else
      vim.diagnostic.show()
    end
    _G.diagnostic_hidden = not _G.diagnostic_hidden
  end)
-- }}}

-- KEY BINDS -- {{{
vim.keymap.set('n',     'Q', [[ :quitall<cr> ]], {silent = true})
vim.keymap.set('n',    'gb', [[ :bnext<cr> ]], {silent = true})
vim.keymap.set('n',    'gB', [[ :bprevious<cr> ]], {silent = true})
vim.keymap.set('n', '<tab>', [[ :normal za<cr> ]], {silent = true})
-- vim.keymap.set('n', '<tab>', [[ :exe 'normal za' | IndentBlanklineRefresh<cr> ]], {silent = true})

-- swap gf and gF
vim.keymap.set('n', 'gf', [[ gF ]])
vim.keymap.set('v', 'gf', [[ gF ]])

-- vim.keymap.set('n', '{', [[ {zz ]])
-- vim.keymap.set('n', '}', [[ }zz ]])
-- this makes c<anything> jump to the start of the line
-- vim.keymap.set({'n', 'v'}, 'c', '_c')
-- vim.keymap.set({'n', 'v'}, 'C', '_C')

vim.keymap.set('t',      '<esc>', [[ <c-\><c-n> ]])
vim.keymap.set('t', '<leader>qq', [[ <c-\><c-n> :bdelete!<cr> ]])
vim.keymap.set('v',          'T',
  function()
    vim.cmd [[retab!]]
    vim.api.nvim_input("<esc>")
  end)

local misc = require('misc')
vim.keymap.set('n',  'gd', function() misc.open_under_cursor(nil, true) end)
-- vim.keymap.set('n',  'gf', function() misc.file_under_cursor(function(s) vim.cmd('edit ' .. s) end) end)
vim.keymap.set('n',  'gx', function() misc.open_under_cursor('url-launcher') end)
vim.keymap.set('n',  'gu', function() misc.open_under_cursor('url-launcher') end)
vim.keymap.set('n', 'gGw', function() misc.word_under_cursor(function(s) require('telescope.builtin.files').grep_string({ search = s }) end) end)
vim.keymap.set('n', 'gGe', function() misc.expr_under_cursor(function(s) require('telescope.builtin.files').grep_string({ search = s }) end) end)
vim.keymap.set('n', 'gGf', function() misc.file_under_cursor(function(s) require('telescope.builtin.files').grep_string({ search = s }) end) end)
vim.keymap.set('n',  'gss', function() misc.word_under_cursor(function(s) vim.fn.feedkeys(':%s/' .. s .. '//g'); cmd [[ call feedkeys("\<left>\<left>") ]] end) end)

vim.keymap.set('n', '<leader>r', misc.live_grep)
vim.keymap.set('n', '<leader>f', misc.find_files)

vim.keymap.set('n', 'dgl', 'diffget LOCAL')
vim.keymap.set('n', 'dgr', 'diffget REMOTE')
vim.keymap.set('n', 'dpl', 'diffpush LOCAL')
vim.keymap.set('n', 'dpr', 'diffpush REMOTE')

vim.keymap.set('x', '<leader>bf',
  [[<ESC><CMD>lua require("misc").fold_block()<CR>]],
  { noremap = true, silent = true })
-- }}}

-- AUTO GROUPS -- {{{
augroup {
  yank_highlight = {
    {'TextYankPost', '*', [[ silent! lua require("vim.highlight").on_yank() ]]}
  },
  obvious_active_win = {
    {'WinEnter', '*', [[ set cul ]]},
    {'WinLeave', '*', [[ set nocul ]]}
  },
  c_makeprg = {
    {'VimEnter', '*.c', [[ setlocal makeprg=ninja\ -C\ build ]]}
  },
  open_qf = {
    {'QuickFixCmdPost', '*', [[ :clist ]]}
  },
  -- packer_auto_compile = {{'BufWritePost', 'plugins.lua', [[ PackerCompile ]]}},
  commentstring = {
    {'FileType', 'i3config', [[ setlocal commentstring=#\ %s ]]},
    {'FileType', 'jsonc', [[ setlocal commentstring=//\ %s ]]},
  },
  folds = {
    {'VimEnter', '.vim/*', [[ setlocal foldenable ]]},
    {'FileType', 'json,jsonc,yaml,toml', [[ setlocal nofoldenable ]]},
  },
  easy_quit = {
    {'FileType', 'help,neorg', [[ nnoremap <buffer><silent> q <cmd>close<cr> ]]},
    {'FileType', 'alpha', [[ nnoremap <buffer><silent> q <cmd>bdel<cr> ]]},
  },
  detect_gomod = {
    {'BufRead,BufNewFile', 'go.mod', [[setlocal filetype=gomod]]}
  },
  detect_bindir_scripts = {
    {'BufRead,BufNewFile', '/home/robert/bin/*', [[setlocal filetype=sh]]}
  }
}
-- }}}

-- USER COMMANDS -- {{{
cmd [[ command! BalenaDiagnosticsFold call balena_diagnostics_fold#apply() ]]
cmd [[ command! -range SortLine <line1>,<line2>lua require('misc').sort_line() ]]
cmd [[ command! -nargs=* Make lua require('job').make({<f-args>}) ]]
cmd [[ command! -nargs=* Sh lua require('job').sh(table.concat({<f-args>}, ' ')) ]]
-- }}}

_G.statusline_enable_notifysend = false
require('statusline')

-- source .nvimrc if it exists
local local_vimrc = vim.fn.getcwd()..'/.nvimrc'
if vim.loop.fs_stat(local_vimrc) then
  cmd('source ' .. local_vimrc)
end