summaryrefslogtreecommitdiff
path: root/mut/neovim/lua/my/init.lua
blob: 27476c305a990cd9a7e262e06bc1a9f28756e3d1 (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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
_G.P = function(...)
  vim.iter({...}):map(vim.inspect):each(print)
end
_G.ternary = function ( cond , T , F )
    if cond then return T else return F end
end

vim.schedule(function()
  vim.cmd "colorscheme kanagawa-wave"
end)

vim.cmd("filetype plugin on")
vim.cmd("filetype indent on")
vim.cmd("highlight WinSeparator guibg=None")
vim.cmd("packadd cfilter")

vim.api.nvim_set_hl(0, "VirtualTextWarning", {link= "Grey"})
vim.api.nvim_set_hl(0, "VirtualTextError", {link= "DiffDelete"})
vim.api.nvim_set_hl(0, "VirtualTextInfo", {link= "DiffChange"})
vim.api.nvim_set_hl(0, "VirtualTextHint", {link= "DiffAdd"})

vim.opt.clipboard:append({"unnamedplus"})

local osc52 = require("vim.ui.clipboard.osc52")

function paste()
  return {
    vim.fn.split(vim.fn.getreg(""), "\n"),
    vim.fn.getregtype("")
  }
end
function xclip(lines)
   vim.system({"xclip"}, {text= true, stdin=lines}, function(exit) end)
   vim.system({"xclip", "-selection", "clipboard"}, {text= true, stdin=lines}, function(exit) end)
end
vim.g.clipboard = {
  name = "OSC 52",
  copy =  {
    ["+"] = xclip, ["*"] = xclip
  },
  paste = {
    ["+"] = paste, ["*"] = paste
  }
}

local oil = require("oil.actions")
local fzf = require("fzf-lua")
local action = (require "fzf-lua.actions")
fzf.setup {"max-perf"}
fzf.register_ui_select()
require("lsp_signature").setup()
require("nvim-treesitter.configs").setup({highlight =  {enable = true}})
require("gitsigns").setup({
  current_line_blame = false, -- Toggle with `:Gitsigns toggle_current_line_blame`
  current_line_blame_opts = {
    virt_text = true,
    virt_text_pos = 'right_align', -- 'eol' | 'overlay' | 'right_align'
    delay = 1000,
    ignore_whitespace = false,
    virt_text_priority = 100,
    use_focus = true,
  },
})
require("nvim_comment").setup()

-- (local
--  draw
--  (fn [toggle]
--    (if
--      toggle
--      (do
--        (vim.cmd "set virtualedit=all")
--        (vim.keymap.set :v "<leader>;" "<esc>:VBox<CR>")
--        (vim.keymap.set "n" "J" "<C-v>j:VBox<CR>")
--        (vim.keymap.set "n" "K" "<C-v>k:VBox<CR>")
--        (vim.keymap.set "n" "L" "<C-v>l:VBox<CR>")
--        (vim.keymap.set "n" "H" "<C-v>h:VBox<CR>"))
--      (do
--        (vim.cmd "set virtualedit=")
--        (vim.keymap.del :v "<leader>;")
--        (vim.keymap.del "n" "J")
--        (vim.keymap.del "n" "K")
--        (vim.keymap.del "n" "L")
--        (vim.keymap.del "n" "H")))))

function i_grep(word, file)
  vim.api.nvim_feedkeys(
    vim.api.nvim_replace_termcodes(
      ":silent grep "
      .. ternary(not (word == ""), word .. " ", "")
      .. file:gsub("oil://", "")
      .. "<c-f>B<left>i<space>",
      true, false, true
    ),
    "n", false
  )
end

function cope()
  vim.cmd(":botright copen " .. math.floor(vim.o.lines / 2.1))
end

local map = vim.keymap.set
map("n", "gb", ":GBrowse<CR>")
map("n", "g<cr>", ":G<cr>")
map("n", "ge", function() vim.diagnostic.open_float() end)
-- (vim.diagnostic.config {:virtual_text false})
map("n", "-", ":Oil<cr>")
map("n", "_", oil.open_cwd.callback)

map("n", "<leader>qf", cope)
map("n", "<leader>q<BS>", ":cclose<cr>")
map("n", "<leader>ll", ":lopen<cr>")
map("n", "<leader>l<BS>", ":lclose<cr>")
map("n", "<M-h>", cope)
map("n", "<C-n>", ":cnext<cr>")
map("n", "<C-p>", ":cprev<cr>")
map("n", "<C-a>", ":Rerun<CR>")
map("n", "<C-s>", function()
  vim.api.nvim_feedkeys(
    vim.api.nvim_replace_termcodes(
      ":Sh<up><c-f>",
      true, false, true
    ),
    "n", false
  )
  vim.schedule(function()
    vim.cmd("let v:searchforward = 0")
    map("n","/","/Sh.*",{buffer=true})
    map("n","?","?Sh.*",{buffer=true})
  end)
end)
map("n", "<C-x>", function()
  vim.api.nvim_feedkeys(
    vim.api.nvim_replace_termcodes(
      ":Compile<up><c-f>",
      true, false, true
    ),
    "n", false
  )
  vim.schedule(function()
    vim.cmd("let v:searchforward = 0")
    map("n","/","/Compile.*",{buffer=true})
    map("n","?","?Compile.*",{buffer=true})
  end)
end)
map("n", "[q",":cprevious<cr>")
map("n", "]q",":cnext<cr>")
map("n", "[x",":lprevious<cr>")
map("n", "]x",":lnext<cr>")
map("n", "[g",":GV<cr>")
map("n", "]g",":GV?<cr>")
map("n", "]G",":GV!<cr>")
map("n", "<leader>xp", fzf.files)
map("n", "<leader>:", function() i_grep("<c-r><c-w>", vim.fn.bufname("%")) end)
map("v", "<leader>:", ":Vgrep!<cr>")
map("n", "<leader>;", function() i_grep("", vim.fn.fnamemodify(vim.fn.bufname("%"), ":h")) end)
map("v", "<leader>;",  ":Vgrep<cr>")
map("n", "<leader>'", ":silent args `fd `<left>")
map("n", "<leader>xa", fzf.args)
map("n", "<leader>x;", fzf.quickfix)
map("n", "<leader>xb", function()
  fzf.buffers({
    actions={default={fn=action.buf_edit_or_qf}}
  })
end)
map("n", "<leader>x<cr>", function() vim.cmd "b #" end)

local obsidian = require("obsidian")
obsidian.setup { workspaces = {
  { name =  "notes", path = ternary(vim.fn.isdirectory(vim.fn.expand("~/Sync/my/notes")) == 1, "~/Sync/my/notes",  "~/sync/my/notes")}
}}


vim.api.nvim_create_user_command(
  "Vgrep",
function(cmd)
  local buf, lrow, lcol = unpack(vim.fn.getpos("'<"))
  local buf, rrow, rcol = unpack(vim.fn.getpos("'>"))
  -- (local [line & rest] (vim.api.nvim_buf_get_text 0 (- <row 1) (- <col 1) (- >row 1) >col {}))
  local firstline =
     vim.iter(vim.api.nvim_buf_get_text(0, lrow-1, lcol-1, rrow-1, rcol, {}))
      :next()
  if cmd.bang then
    i_grep(firstline, vim.fn.bufname("%"))
  else
    i_grep(firstline, vim.fn.fnamemodify(vim.fn.bufname("%"), ":h"))
  end
end,
  {range= 1, bang=true}
)

vim.api.nvim_create_user_command(
  "NixEdit",
function(cmd)
  local f = io.popen("nix eval --raw /nix-config#nixosConfigurations." .. vim.fn.hostname() .. ".pkgs." .. cmd.args)
  vim.cmd("e " .. f:read())
end,
  {nargs=1}
)


function qf(inputs)
  local id, title = inputs.id, inputs.title
  local prettify = function(line)
    local l = line:gsub("%c+%[[0-9:;<=>?]*[!\"#$%%&'()*+,-./]*[@A-Z%[%]^_`a-z{|}~]*;?[A-Z]?", "")
    return l
  end
  local in_qf = function()
    return vim.opt_local.buftype:get() == "quickfix"
  end
  local is_at_last_line = function()
    local row, _ = vim.api.nvim_win_get_cursor(0)
    local last_line = vim.api.nvim_buf_line_count(0)
    return row == last_line
  end
  return function(lines)
    lines = vim.iter(lines):map(prettify):totable()
    vim.schedule(function()
      vim.fn.setqflist(
        {}, "a", {
          id=id, title=title,
          lines=lines
        }
      )
      if (not in_qf()) or (is_at_last_line() and in_qf()) then
        vim.cmd ":cbottom"
      end
    end)
  end
end

local last_job_state = nil
local last_job_thunk = nil
function qfjob(cmd, stdin)
  local title = table.concat(cmd, " ")
  vim.fn.setqflist({}, " ", {title=title})
  local append_lines = qf(vim.fn.getqflist({id=0,title=1}))
  last_job_state = vim.system(
    cmd, {
      stdin=stdin,
      stdout=function(err,data)
        if data then
          append_lines(data:gmatch("[^\n]+"))
        end
      end,
      stderr=function(err,data)
        if data then
          append_lines(data:gmatch("[^\n]+"))
        end
      end,
    },
    function(job)
      vim.schedule(function()
        local winnr = vim.fn.winnr()
        if not (job.code == 0) then
          cope()
          if not (winnr == vim.fn.winnr()) then
            vim.notify([["]] .. title .. [[" failed!]])
            vim.cmd "wincmd p | cbot"
          end
        else
          vim.notify([["]] .. title .. [[" succeeded!]])
        end
      end)
    end)
end

vim.api.nvim_create_user_command(
  "Sh",
  function(cmd)
    local thunk = function() qfjob({ "zshcmd", cmd.args }, nil) end
    last_job_thunk = thunk
    thunk()
  end,
  {nargs="*", bang=true, complete="shellcmd"})

vim.api.nvim_create_user_command(
  "Rerun",
  function(cmd)
    if not last_job_state then
      vim.notify "nothing to rerun"
    else
      if not last_job_state:is_closing() then
        vim.notify "Last job not finished"
      else
        last_job_thunk()
      end
    end
  end, {bang=true})

vim.api.nvim_create_user_command(
  "Stop",
  function()
    if last_job_state then
      last_job_state:kill()
      vim.notify "killed job"
    else
        vim.notify "nothing to do"
    end
  end, {bang=true})

-- (fn browse_git_remote
--   [data]
--   (P data)
--   (local
--     {: commit
--      : git_dir
--      : line1
--      : line2
--      : path
--      : remote
--      : remote_name
--      : repo
--      : type } data)
--
--   (local
--     oilpath
--     (case (vim.fn.bufname "%")
--       (where oilbuf (vim.startswith oilbuf "oil://"))
--       (do
--         (local d (.. "oil://" (vim.fs.dirname git_dir) "/"))
--         (oilbuf:sub (+ 1 (d:len)) (oilbuf:len)))
--       _
--       ""))
--
--   (local [home repo]
--     (case remote
--       (where s (vim.startswith s "git@"))
--       (do
--         (or
--           (case [(s:match "(git@)([^:]+):(.*)(%.git)")]
--               ["git@" home repo ".git"]
--               [home repo])
--           (case [(s:match "(git@)([^:]+):.*/(.*)/(.*)/(.*)")]
--               ["git@" home org project repo]
--               [(home:gsub "ssh%." "") [(.. org "/" project) repo]])))))
--
--   (case [home repo]
--     (where ["bitbucket.org" repo])
--     (do
--       (case [path type]
--         ["" "tree"]
--         (.. "https://" home "/" repo "/src/" commit "/" (or oilpath path ""))
--         [path "blob"]
--         (.. "https://" home "/" repo "/src/" commit "/" path)
--         [path "commit"]
--         (.. "https://" home "/" repo "/commits/" commit)
--         [path "ref"]
--         (.. "https://" home "/" repo "/commits/" commit)))
--     (where ["dev.azure.com" [org repo]])
--     (do
--       (case [path type]
--         ["" "tree"]
--         (.. "https://" home "/" org "/_git/" repo "?version=GB" commit "&path=/" (or oilpath path ""))
--         [path "blob"]
--         (.. "https://" home "/" org "/_git/" repo "?version=GB" commit "&path=/" path)
--         [path "commit"]
--         (.. "https://" home "/" org "/_git/" repo "/commit/" commit)
--         [path "ref"]
--         (.. "https://" home "/" org "/_git/" repo "/commit/" commit)))
--     (where ["gitlab.com" repo])
--     (do
--       (case [path type]
--         ["" "tree"]
--         (.. "https://" home "/" repo "/-/tree/" commit "/" (or oilpath ""))
--         [path "commit"]
--         (.. "https://" home "/" repo "/-/commit/" commit)
--         [path "ref"]
--         (.. "https://" home "/" repo "/-/commit/" commit)
--         [path "blob"]
--         (.. "https://" home "/" repo "/-/blob/" commit "/" path)))
--     (where ["github.com" repo])
--     (do
--       (case [path type]
--         ["" "tree"]
--         (.. "https://" home "/" repo "/tree/" commit "/" (or oilpath ""))
--         [path "commit"]
--         (.. "https://" home "/" repo "/commit/" commit)
--         [path "ref"]
--         (.. "https://" home "/" repo "/commit/" commit)
--         [path "blob"]
--         (.. "https://" home "/" repo "/blob/" commit "/" path)))))
--
-- (vim.api.nvim_create_user_command
--   :Browse
--   (fn [{: args}] (vim.system ["xdg-open" args] {} (fn [])))
--   {:nargs 1})
--
-- (set vim.g.fugitive_browse_handlers
--      [browse_git_remote])

require("my.settings")
require("my.events")
require("my.packages")