-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
145 lines (122 loc) · 4.16 KB
/
init.lua
File metadata and controls
145 lines (122 loc) · 4.16 KB
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
vim.g.mapleader = ' '
-- Load local config if it exists
pcall(require, 'config.local')
for _, mod in ipairs {
'config.lazy',
'config.autocmd.lua',
'config.colorscheme',
'config.python',
'config.mappings',
'config.mappings.clipboard',
'config.mappings.commentary',
'config.mappings.nvimtree',
'config.mappings.telescope',
'config.settings',
'config.settings.mouse',
'config.settings.undo',
'config.ruby',
} do
require(mod)
end
vim.cmd 'source ~/.config/nvim/vim/autocommands.vim'
vim.cmd 'source ~/.config/nvim/vim/functions.vim'
function _G.AIReplace()
local start_pos = vim.fn.getpos "'<"
local end_pos = vim.fn.getpos "'>"
local lines = vim.fn.getline(start_pos[2], end_pos[2])
lines[1] = string.sub(lines[1], start_pos[3])
lines[#lines] = string.sub(lines[#lines], 1, end_pos[3])
local input_text = table.concat(lines, '\n')
local prompt = vim.fn.input 'Prompt: '
local buffers = vim.api.nvim_exec('ls', true):gsub('\n', '\\n')
local current_file = vim.fn.expand '%:p'
local filetype = vim.bo.filetype
local home = os.getenv 'HOME'
local script = home .. '/ai_playground/openai_replace.py'
local python = vim.g.python3_host_prog or 'python3'
local output = vim.fn.system({
python,
script,
prompt,
'--current-file',
current_file,
'--filetype',
filetype,
'--vim-buffers',
buffers,
}, input_text)
vim.api.nvim_buf_set_lines(0, start_pos[2] - 1, end_pos[2], false, vim.split(output, '\n', { trimempty = true }))
end
vim.api.nvim_set_keymap('v', '<leader>ai', ':lua AIReplace()<CR>', { noremap = true, silent = true })
vim.keymap.set('n', '<leader>ut', function()
local line = vim.api.nvim_get_current_line()
local utc = line:match '%d%d%d%d%-%d%d%-%d%dT%d%d:%d%d:%d%dZ'
if not utc then
vim.api.nvim_echo({ { 'No UTC timestamp found on line', 'WarningMsg' } }, false, {})
return
end
local is_macos = vim.loop.os_uname().sysname == 'Darwin'
local cleaned = utc:gsub('Z', '')
local cmd = is_macos and { 'date', '-j', '-u', '-f', '%Y-%m-%dT%H:%M:%S', cleaned }
or { 'date', '-d', cleaned .. ' UTC' }
vim.fn.jobstart(cmd, {
stdout_buffered = true,
stderr_buffered = true,
on_stdout = function(_, data)
if data and data[1] ~= '' then
vim.api.nvim_echo({ { 'Local time: ' .. table.concat(data, ' '), 'Normal' } }, false, {})
end
end,
on_stderr = function(_, err)
if err and err[1] ~= '' then
vim.api.nvim_echo({ { 'Error: ' .. table.concat(err, ' '), 'ErrorMsg' } }, false, {})
end
end,
})
end, { desc = 'Show local time for UTC timestamp' })
local last_line = nil
vim.api.nvim_create_autocmd('CursorHold', {
pattern = '*',
callback = function()
local line = vim.api.nvim_get_current_line()
if line == last_line then return end
last_line = line
local utc = line:match '%d%d%d%d%-%d%d%-%d%dT%d%d:%d%d:%d%dZ'
if not utc then return end
local is_macos = vim.loop.os_uname().sysname == 'Darwin'
local cleaned = utc:gsub('Z', '')
local cmd = is_macos and { 'date', '-j', '-u', '-f', '%Y-%m-%dT%H:%M:%S', cleaned }
or { 'date', '-d', cleaned .. ' UTC' }
vim.fn.jobstart(cmd, {
stdout_buffered = true,
on_stdout = function(_, data)
if data and data[1] ~= '' then
vim.api.nvim_echo({ { 'Local time: ' .. table.concat(data, ' '), 'Normal' } }, false, {})
end
end,
})
end,
})
vim.o.updatetime = 100
vim.opt.formatlistpat = [[^\s*\d\+[\]:.)}\t ]\s*\|^\s*[-+*]\s*]]
vim.g.loaded_perl_provider = 0
-- The following is a mess; definitely clean it up at some point
vim.api.nvim_create_autocmd('FileType', {
pattern = 'markdown',
callback = function()
vim.opt_local.formatlistpat = [[^\s*\d\+[\]:.)}\t ]\s*\|^\s*[-+*]\s*]]
vim.opt_local.formatoptions = 'tcqnj'
vim.opt_local.textwidth = 78
vim.opt_local.formatexpr = ''
vim.opt_local.autoindent = true
vim.opt_local.indentexpr = ''
end,
})
vim.api.nvim_create_autocmd('CursorHold', {
callback = function()
local opts = { focusable = false }
if #vim.diagnostic.get(0, { lnum = vim.api.nvim_win_get_cursor(0)[1] - 1 }) > 0 then
vim.diagnostic.open_float(nil, opts)
end
end,
})