-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinit.lua
626 lines (540 loc) · 22.1 KB
/
init.lua
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
--[[
Main Neovim configuration. Aims to be mostly language agnostic. Code which is
language specific and buffer-local was moved to corresponding ftplugins in
the directory "ftplugin" instead. Also, larger chunks of coherent code were
refactored into libraries in "lua" or plugins in "plugin" to not clutter the
main configuration file.
--]]
-------------------------------------------------------------------------------
-- {{{ Generic Configuration
-------------------------------------------------------------------------------
-- Options
-------------------------------------------------------------------------------
-- smart search
vim.opt.ignorecase = true
vim.opt.smartcase = true
-- tabs are two spaces
vim.opt.tabstop = 2
vim.opt.softtabstop = 2
vim.opt.shiftwidth = 2
vim.opt.expandtab = true
-- line break configuration
vim.opt.textwidth = 79
vim.opt.colorcolumn = {80, 120}
vim.opt.breakindent = true
vim.opt.linebreak = true
vim.opt.smoothscroll = true
vim.opt.showbreak = "> "
-- set list chars for horizontal scrolling
vim.opt.listchars:append{tab = "» ", precedes = "<", extends = ">"}
vim.opt.list = true
-- built-in completion & tag search
vim.opt.completeopt:append{"menuone", "noinsert", "popup"}
vim.opt.complete:remove{"t"}
vim.opt.completefunc = "v:lua.require'snipcomp'" -- custom snippet completion defined in lua/snipcomp.lua
-- show line numbers and highlight cursor line number
vim.opt.number = true
vim.opt.relativenumber = true
vim.opt.cursorline = true
vim.opt.cursorlineopt = "number"
-- spell checking
vim.opt.spell = true
vim.opt.spelllang = {"en_us", "de_de", "cjk"}
vim.opt.spellfile = ("%s/spell/spf.%s.add"):format(vim.fn.stdpath("config"), vim.o.encoding)
vim.opt.thesaurusfunc = "v:lua.require'mythes'" -- support openoffice thesauri, see lua/mythes.lua
vim.opt.thesaurus = {
-- archlinux packages extra/mythes-{en,de,..}
"/usr/share/mythes/th_en_US_v2.dat",
"/usr/share/mythes/th_de_DE_v2.dat"
}
-- mouse and clipboard integration
vim.opt.clipboard = "unnamedplus"
vim.opt.mouse = "a"
-- set an alternative layout that can be switched to in insert mode with CTRL-^
vim.opt.keymap = "kana"
vim.opt.iminsert = 0
vim.opt.undofile = true -- persistent undo history
vim.opt.showmode = false -- do not show mode message on last line
vim.opt.hidden = true -- switch buffers without having to save changes
vim.opt.joinspaces = false -- insert one space when joining two sentences
vim.opt.confirm = true -- raise dialog asking to save changes when commands like ':q' fail
vim.opt.title = true -- set terminal window title to something descriptive
vim.opt.foldlevel = 99 -- do not automatically close folds when editing a file
vim.opt.inccommand = "nosplit" -- show incremental changes of commands such as search & replace
vim.opt.virtualedit = "block" -- virtual editing in visual block mode
vim.opt.shortmess:append("I") -- don't give intro message when starting vim
-- Variables
-------------------------------------------------------------------------------
-- define leaders for use in keybindings
vim.g.mapleader = " "
vim.g.maplocalleader = " "
-- syntax/indent related global variables
vim.g.is_bash = true
vim.g.sh_no_error = true
vim.g.readline_has_bash = true
vim.g.tex_stylish = true
vim.g.tex_flavor = "latex"
vim.g.markdown_fenced_languages = {"python", "lua", "sh", "bash", "tex", "latex=tex"}
vim.g.python_indent = {open_paren = "shiftwidth()", continue = "shiftwidth()", closed_paren_align_last_line = false}
-- setup netrw and viewer for 'gx' mapping
vim.g.netrw_banner = 0
vim.g.netrw_liststyle = 3
vim.g.netrw_browse_split = 4
vim.g.netrw_winsize = -30
vim.g.netrw_special_syntax = 1
vim.g.netrw_browsex_viewer = "xdg-open"
vim.g.netrw_list_hide = [[\(^\|\s\s\)\zs\.\S\+]]
vim.g.netrw_sort_options = "i"
vim.g.python3_host_prog = "/usr/bin/python3" -- use system python (useful when working with virualenvs)
vim.g.vga_compatible = vim.env.TERM == "linux" -- VGA textmode fallback (with CP437 character set) for legacy terminals
-- Automatic commands
-------------------------------------------------------------------------------
local au = require("au") -- small wrapper around lua autocmd api
-- jump to last position when opening a file
local open = au("user_open")
function open.BufReadPost()
local last_cursor_pos, last_line = vim.fn.line([['"]]), vim.fn.line("$")
if last_cursor_pos > 1 and last_cursor_pos <= last_line then
vim.fn.cursor(last_cursor_pos, 1)
end
end
-- briefly highlight a selection on yank
local yank = au("user_yank")
function yank.TextYankPost()
vim.highlight.on_yank()
end
-- automatically toggle between relative and absolute line numbers depending on mode
local number = au{"user_number",
Relative = {"BufEnter", "FocusGained", "InsertLeave", "TermLeave", "WinEnter"},
Absolute = {"BufLeave", "FocusLost", "InsertEnter", "TermEnter", "WinLeave"}
}
function number.Relative()
if vim.opt_local.number:get() and vim.fn.mode() ~= "i" then
vim.opt_local.relativenumber = true
end
end
function number.Absolute()
if vim.opt_local.number:get() then
vim.opt_local.relativenumber = false
end
end
-- close preview window when completion is finished
local preview = au("user_preview")
function preview.CompleteDone()
if vim.fn.pumvisible() == 0 and vim.fn.getcmdwintype() == "" then
vim.cmd.pclose()
end
end
-- restore view of current window when switching buffers
local view = au("user_view")
function view.BufWinLeave()
vim.b.view = vim.fn.winsaveview()
end
function view.BufWinEnter()
if vim.b.view then
vim.fn.winrestview(vim.b.view)
vim.b.view = nil
end
end
-- enable syntax highlighting for bash's edit-and-execute-command
vim.filetype.add{pattern = {["bash%-fc%.%w+"] = "sh"}}
-- Commands
-------------------------------------------------------------------------------
local cmd = vim.api.nvim_create_user_command
-- open (new) terminal at bottom of the current tab (using default instance)
cmd("Terminal", function(tbl)
require("term"):open{cmd = #tbl.fargs > 0 and tbl.fargs or nil}
end, {nargs = "*", complete = "shellcmd"}
)
cmd("Cd", "cd %:p:h", {}) -- set cwd to directory of current file
cmd("Run", '!"%:p"', {}) -- Execute current file
cmd("Bd", "bp|bd #", {}) -- delete buffer without closing split
cmd("Config", "edit $MYVIMRC", {}) -- open config file with :Config
cmd("Reload", "source $MYVIMRC", {}) -- reload config file with :Reload
-- Mappings
-------------------------------------------------------------------------------
local map, opts = vim.keymap.set, {noremap = true, silent = true}
-- navigate buffers like tabs (gt & gT)
map("n", "gb", function() vim.cmd.bnext{count = vim.v.count1} end, opts)
map("n", "gB", function() vim.cmd.bprev{count = vim.v.count1} end, opts)
-- diagnostics mappings
map("n", "<leader>ll", vim.diagnostic.setloclist, opts)
map("n", "<leader>ld", vim.diagnostic.open_float, opts)
map("n", "[d", vim.diagnostic.goto_prev, opts)
map("n", "]d", vim.diagnostic.goto_next, opts)
-- open netrw file explorer and terminal
map("n", "<leader>fe", vim.cmd.Lexplore, opts)
map("n", "<leader>tm", vim.cmd.Terminal, opts)
-- language server mappings
local function lsp_mappings(_, buf)
local bufopts = {buffer = buf, unpack(opts)}
map("n", "gd", vim.lsp.buf.definition, bufopts)
map("n", "gD", vim.lsp.buf.declaration, bufopts)
map("n", "<leader>gi", vim.lsp.buf.implementation, bufopts)
map("n", "<leader>gt", vim.lsp.buf.type_definition, bufopts)
map("n", "K", vim.lsp.buf.hover, bufopts)
map("n", "<C-k>", vim.lsp.buf.signature_help, bufopts)
map("n", "<leader>wa", vim.lsp.buf.add_workspace_folder, bufopts)
map("n", "<leader>wr", vim.lsp.buf.remove_workspace_folder, bufopts)
map("n", "<leader>wl", function() vim.print(vim.lsp.buf.list_workspace_folders()) end, bufopts)
map("n", "<leader>rn", vim.lsp.buf.rename, bufopts)
map("n", "<leader>ca", vim.lsp.buf.code_action, bufopts)
map("n", "<leader>rf", vim.lsp.buf.references, bufopts)
map("n", "<leader>fm", function() vim.lsp.buf.format{async = true} end, bufopts)
map("v", "<leader>fm", ":lua vim.lsp.formatexpr()<cr>", bufopts) -- return to normal mode
end
-- Diagnostics
-------------------------------------------------------------------------------
local prefix = "DiagnosticSign"
-- when not on the console set some nice signs
if not vim.g.vga_compatible then
vim.fn.sign_define{
{name = prefix .. "Error", text = "▌", texthl = prefix .. "Error"},
{name = prefix .. "Warn", text = "▌", texthl = prefix .. "Warn"},
{name = prefix .. "Hint", text = "▌", texthl = prefix .. "Hint"},
{name = prefix .. "Info", text = "▌", texthl = prefix .. "Info"}
}
vim.diagnostic.config{
virtual_text = {prefix = "▪"}
}
end
-- }}}
-------------------------------------------------------------------------------
-- {{{ Plugin-Specific Configuration
-------------------------------------------------------------------------------
local autopaq = require("autopaq")
-- small, custom wrapper around paq-nvim which installs paq automatically when
-- it is missing
autopaq.bootstrap{
"savq/paq-nvim",
"unblevable/quick-scope",
"NvChad/nvim-colorizer.lua",
"neovim/nvim-lspconfig",
"mfussenegger/nvim-dap",
"tpope/vim-fugitive",
"potamides/pantran.nvim",
"lewis6991/gitsigns.nvim",
"nvim-lualine/lualine.nvim",
"ibhagwan/fzf-lua",
"robitx/gp.nvim",
"ellisonleao/gruvbox.nvim",
{"L3MON4D3/LuaSnip", build = "make install_jsregexp"},
-- dependencies
"rafamadriz/friendly-snippets", -- LuaSnip
"nvim-tree/nvim-web-devicons", -- lualine.nvim, fzf-lua
}
-- shorthand for updating packages with paq-nvim and showing (new) updates
cmd("Update", "silent PaqLogClean | PaqSync | autocmd User PaqDoneSync ++once ++nested PaqLogOpen", {})
local function lazy_require(module)
return setmetatable({}, {
__index = function(_, k) return require(module)[k] end,
__newindex = function(_, k, v) require(module)[k] = v end
})
end
-- gruvbox.nvim
-------------------------------------------------------------------------------
local gruvbox = require("gruvbox")
gruvbox.setup{
italic = {strings = false},
overrides = {
-- diagnostic highlighting
DiagnosticHint = {link = "GruvboxPurple"},
DiagnosticSignHint = {link = "GruvboxPurpleSign"},
DiagnosticUnderlineHint = {link = "GruvboxPurpleUnderline"},
DiagnosticFloatingHint = {link = "GruvboxPurple"},
DiagnosticVirtualTextHint = {link = "GruvboxPurple"},
-- spelling highlighting
SpellBad = {link = "GruvboxBlueUnderline"},
SpellCap = {link = "GruvboxOrangeUnderline"},
SpellRare = {link = "GruvboxGreenUnderline"},
-- git signs
GitSignsAdd = {link = "GruvboxGreenSign"},
GitSignsChange = {link = "GruvboxAquaSign"},
GitSignsDelete = {link = "GruvboxRedSign"},
GitSignsTopdelete = {link = "GruvboxRedSign"},
GitSignsChangedelete = {link = "GruvboxAquaSign"},
GitSignsUntracked = {link = "GruvboxGreenSign"},
-- misc
NormalFloat = {link = "Pmenu"},
Todo = {link = "htmlBoldItalic"},
}
}
-- only enable this color scheme when supported by terminal
if not vim.g.vga_compatible then
vim.cmd.colorscheme("gruvbox")
end
-- lualine.nvim
-------------------------------------------------------------------------------
local statusline, devicons = require("statusline"), require("nvim-web-devicons")
local function vga(regular, fallback)
if vim.g.vga_compatible then return fallback else return regular end
end
devicons.setup{
default = true,
override = {default_icon = {icon = ""}}
}
statusline.setup{
theme = vim.g.colors_name or "ansi",
icons_enabled = not vim.g.vga_compatible,
separators = {
component = {left = "│", right = "│"},
section = {left = "▌", right = "▐"},
},
symbols = {
edit = vga(""),
lock = vga(""),
git = vga(""),
line = vga(""),
error = vga(""),
warning = vga(""),
info = vga(""),
hint = vga(""),
dap = vga(""),
more = vga("…"),
spinner = vga{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}
}
}
-- quick-scope
-------------------------------------------------------------------------------
local quickscope = au{"user_quickscope", LoadPost = {"ColorScheme", "VimEnter"}}
vim.g.qs_highlight_on_keys = {"f", "F", "t", "T"}
function quickscope.LoadPost()
for group, color in pairs{QuickScopePrimary = 10, QuickScopeSecondary = 13} do
vim.api.nvim_set_hl(0, group, {
sp = vim.g["terminal_color_" .. color],
ctermfg = color,
bold = true,
underline = true
})
end
end
-- gitsigns.nvim
-------------------------------------------------------------------------------
local gitsigns = require("gitsigns")
gitsigns.setup{
signs_staged_enable = false,
signs = {
add = {text = vga("▌")},
change = {text = vga("▌")},
delete = {text = vga("▖")},
topdelete = {text = vga("▘")},
changedelete = {text = vga("▌")},
untracked = {text = vga("▌")},
},
preview_config = {
border = "none"
},
on_attach = function(buf)
local bufopts = {buffer = buf, unpack(opts)}
local function jump(direction)
if vim.wo.diff then
return "]c"
end
vim.schedule(direction)
return "<Ignore>"
end
-- Navigation
map("n", "]c", function() return jump(gitsigns.next_hunk) end, {expr = true, unpack(bufopts)})
map("n", "[c", function() return jump(gitsigns.prev_hunk) end, {expr = true, unpack(bufopts)})
-- Actions
map({"n", "v"}, "<leader>hr", ":Gitsigns reset_hunk<CR>", bufopts)
map("n", "<leader>hp", gitsigns.preview_hunk, bufopts)
map("n", "<leader>bl", gitsigns.blame_line, bufopts)
end
}
-- nvim-colorizer.lua
------------------------------------------------------------------------------
local colorizer = require("colorizer")
-- colorize color specifications like '#aabbcc' in virtualtext
colorizer.setup{
filetypes = {"*"},
user_default_options = {
names = false,
mode = "virtualtext"
}
}
-- nvim-lspconfig
-------------------------------------------------------------------------------
local lsputil = require("lspconfig.util")
-- setup calls to specific language servers are located in ftplugins
lsputil.on_setup = lsputil.add_hook_before(lsputil.on_setup, function(config)
config.on_attach = lsputil.add_hook_before(config.on_attach, lsp_mappings)
end)
-- nvim-dap
-------------------------------------------------------------------------------
local dap = require("dap")
local dapterm = require("term").instance()
vim.fn.sign_define{
{name = "DapBreakpoint", texthl = "debugBreakpoint", text = vga("")},
{name = "DapBreakpointCondition", texthl = "debugBreakpoint", text = vga("")},
{name = "DapLogPoint", texthl = "debugBreakpoint", text = vga("")},
{name = "DapBreakpointRejected", texthl = "debugBreakpoint", text = vga("")},
{name = "DapStopped", texthl = "debugBreakpoint", text = vga("")}
}
-- integrate our own terminal wrapper with dap
function dap.defaults.fallback.terminal_win_cmd()
return dapterm:open{cmd = false, nofocus = true}
end
local function repl_open()
local _, win = dap.repl.open(nil, "lua require('term'):open{noinsert = true}")
vim.api.nvim_set_current_win(win)
vim.cmd.startinsert()
end
local function try_call(func, ...)
if dap.session() then func(...) else vim.notify("No active session") end
end
map("n", "<leader>cc", dap.continue, opts)
map("n", "<leader>ss", dap.step_over, opts)
map("n", "<leader>si", dap.step_into, opts)
map("n", "<leader>so", dap.step_out, opts)
map("n", "<leader>rc", dap.run_to_cursor, opts)
map("n", "<leader>br", dap.toggle_breakpoint, opts)
map("n", "<leader>bc", function() dap.set_breakpoint(vim.fn.input("Breakpoint condition: ")) end, opts)
map("n", "<leader>bm", function() dap.set_breakpoint(nil, nil, vim.fn.input("Log point message: ")) end, opts)
map("n", "<leader>bd", dap.clear_breakpoints, opts)
map("n", "<leader>bs", function() dap.list_breakpoints() vim.cmd.copen() end, opts)
map("n", "<leader>ro", function() try_call(repl_open) end, opts)
map("n", "<leader>to", function() try_call(function() dapterm:open{nofocus = true} end) end, opts)
map("n", "<leader>rl", dap.run_last, opts)
map("n", "<leader>te", dap.terminate, opts)
map("n", "<leader>dp", function() require("dap.ui.widgets").hover(nil, {border = "none"}) end, opts)
-- LuaSnip
-------------------------------------------------------------------------------
local luasnip = lazy_require("luasnip")
require("luasnip.loaders.from_vscode").lazy_load()
local function try_change_choice(direction)
if luasnip.choice_active() then
luasnip.change_choice(direction)
end
end
-- we only define LuaSnip mappings for jumping around, expansion is handled by
-- insert mode completion (see help-page for 'ins-completion' and
-- 'completefunc' defined above).
map({"n", "i", "s"}, "<C-s><C-n>", function() luasnip.jump(1) end, opts)
map({"n", "i", "s"}, "<C-s><C-p>", function() luasnip.jump(-1) end, opts)
map({"n", "i", "s"}, "<C-s><C-j>", function() try_change_choice(1) end, opts)
map({"n", "i", "s"}, "<C-s><C-k>", function() try_change_choice(-1) end, opts)
-- fzf-lua
-------------------------------------------------------------------------------
local fzf = require("fzf-lua")
fzf.setup{
hls = {
title = "PantranTitle",
preview_title = "PantranTitle"
},
winopts = {
backdrop = 100,
border = "single",
preview = {
scrollbar = false,
vertical = "up:45%",
winopts = {number = false}
},
on_close = function()
-- make lualine return to normal mode immediately
vim.api.nvim_input("<Ignore>")
end
},
diagnostics = {
signs = {
Error = {text = statusline.opts.symbols.error, texthl = "DiagnosticError"},
Warn = {text = statusline.opts.symbols.warning, texthl = "DiagnosticWarn"},
Info = {text = statusline.opts.symbols.info, texthl = "DiagnosticInfo"},
Hint = {text = statusline.opts.symbols.hint, texthl = "DiagnosticHint"},
}
},
lsp = {jump_to_single_result = true},
fzf_opts = {["--layout"] = "default"},
defaults = {file_icons = not vim.g.vga_compatible}
}
-- when a count N is given to a fzf mapping called through the following
-- function, the search is started in the Nth parent directory
local function fzf_cwd(picker, args)
local target_dir = vim.loop.fs_realpath(("../"):rep(vim.v.count) .. ".")
fzf[picker](vim.tbl_extend("error", args or {}, {cwd = target_dir}))
end
map("n", "<leader>ff", function() fzf_cwd("files") end, opts)
map("n", "<leader>rg", function() fzf_cwd("live_grep") end, opts)
map("n", "<leader>ds", fzf.lsp_document_symbols, opts)
map("n", "<leader>ws", fzf.lsp_live_workspace_symbols, opts)
map("n", "<leader>fz", fzf.builtin, opts)
-- if fzf binary is available patch ui.select and some previous mappings
if vim.fn.executable("fzf") == 1 then
fzf.register_ui_select()
map("n", "<leader>ll", fzf.diagnostics_document, opts)
map("n", "<leader>bs", fzf.dap_breakpoints, opts)
lsp_mappings = lsputil.add_hook_after(lsp_mappings, function(_, buf)
local bufopts = {buffer = buf, unpack(opts)}
map("n", "gd", fzf.lsp_definitions, bufopts)
map("n", "gD", fzf.lsp_declarations, bufopts)
map("n", "<leader>gi", fzf.lsp_implementations, bufopts)
map("n", "<leader>gt", fzf.lsp_typedefs, bufopts)
map("n", "<leader>ca", fzf.lsp_code_actions, bufopts)
map("n", "<leader>rf", fzf.lsp_references, bufopts)
end)
end
-- pantran.nvim
-------------------------------------------------------------------------------
local pantran = require("pantran")
pantran.setup{
default_engine = vim.env.DEEPL_AUTH_KEY and "deepl" or nil,
controls = {
mappings = {
edit = {
n = {
["j"] = "gj",
["k"] = "gk"
}
}
}
}
}
map({"n", "v"}, "<leader>tr", pantran.motion_translate, {expr = true, unpack(opts)})
map("n", "<leader>trr", function() return pantran.motion_translate() .. "_" end, {expr = true, unpack(opts)})
map("n", "<leader>tro", vim.cmd.Pantran, opts)
-- gp.nvim
-------------------------------------------------------------------------------
local def = require("gp.defaults")
def.chat_system_prompt = (def.chat_system_prompt):match("(.-)\n")
local gp = require("gp")
gp.setup{
chat_user_prefix = "## User",
chat_assistant_prefix = {"## Assistant", " ({{agent}})"},
command_prompt_prefix_template = "{{agent}}: ",
prompt_prefix_template = "{{agent}}: ",
prompt_save = "Directory: ",
toggle_target = "popup",
chat_confirm_delete = false,
chat_shortcut_respond = {modes = {"n", "v"}, shortcut = "<cr>"},
chat_shortcut_delete = {modes = {"n", "v"}, shortcut = "<leader>gd"},
chat_shortcut_stop = {modes = {"n", "v"}, shortcut = "<leader>gx"},
chat_shortcut_new = {modes = {"n", "v"}, shortcut = "<leader>gn"},
}
for mode, key in pairs{n = "<cmd>", v = ":"} do
map(mode, "<leader>gp", key .. "GpChatToggle popup<cr>", opts)
map(mode, "<leader>gg", key .. "GpPopup<cr>", opts)
map(mode, "<leader>gs", key .. "GpChatToggle vsplit<cr>", opts)
map(mode, "<leader>g/", key .. "GpChatFinder<cr>", opts)
map(mode, "<leader>gm", "<cmd>GpImage<cr>", opts)
map(mode, "<leader>gx", "<cmd>GpStop<cr>", opts)
end
-- see :h :map-operator
local function motion_cmd(command, suffix)
return function()
vim.opt.operatorfunc = ([[{ -> execute("'[,']%s")}]]):format(command)
return "g@" .. (suffix or "")
end
end
for mapping, key in pairs{GpImplement = "<leader>gc", GpChatPaste = "<leader>gy"} do
map({"n", "v"}, key, motion_cmd(mapping), {expr = true, unpack(opts)})
map("n", key .. key:sub(-1), motion_cmd(mapping, "_"), {expr = true, unpack(opts)})
end
-- hack to modify style of popup window
local popup = gp.render.popup
function gp.render.popup(optbuf, title, ...)
local buf, win, close, resize = popup(optbuf, (" %s "):format(title), ...)
vim.opt_local.winhighlight = {Normal = "PantranNormal", FloatTitle = "PantranTitle", FloatBorder = "PantranBorder"}
return buf, win, close, resize
end
-- }}}
-- vim: foldmethod=marker foldmarker=--\ {{{,--\ }}}