-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeymaps.lua
More file actions
276 lines (238 loc) · 9.02 KB
/
Copy pathkeymaps.lua
File metadata and controls
276 lines (238 loc) · 9.02 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
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
-- NOTE: below are colemak remaps
-- [x] = y means x now does what y did originally
local remaps = {
['k'] = 'h',
['n'] = 'j',
['e'] = 'k',
['i'] = 'l',
['K'] = 'H',
['N'] = 'J',
['E'] = 'K',
['I'] = 'L',
-- notice, these are not symmetrical to above
['h'] = 'n',
['j'] = 'e',
['l'] = 'i',
['H'] = 'N',
['J'] = 'E',
['L'] = 'I',
}
local function alert_key_layout_change()
-- send an alert that other plugins can act on to change their binds
vim.api.nvim_exec_autocmds('User', {
pattern = 'KeyboardLayoutChanged',
modeline = false,
})
end
-- buffer jumping
local function buf_jump_set(set, remove)
-- set: a 4 letter string like 'knei' or 'hjkl'
-- remove: same as above but can be nil
-- this function sets the buffer jumps to be the letters of set (left, down, up, right respectively)
-- and also removes the keybinds in remove
if remove ~= nil then
for i = 1, 4 do
local c = remove:sub(i, i)
pcall(vim.keymap.del, 'n', '<C-' .. c .. '>')
end
end
local c = set:sub(1, 1)
vim.keymap.set('n', '<C-' .. c .. '>', '<C-w><C-h>', { desc = 'Move focus to the left window' })
c = set:sub(2, 2)
vim.keymap.set('n', '<C-' .. c .. '>', '<C-w><C-j>', { desc = 'Move focus to the bottom window' })
c = set:sub(3, 3)
vim.keymap.set('n', '<C-' .. c .. '>', '<C-w><C-k>', { desc = 'Move focus to the top window' })
c = set:sub(4, 4)
vim.keymap.set('n', '<C-' .. c .. '>', '<C-w><C-l>', { desc = 'Move focus to the right window' })
end
local function set_layout_persistence(new_layout)
local layout_path = vim.g.local_settings.keyboard_layout_path
local success = pcall(function()
return vim.fn.writefile({ new_layout }, layout_path)
end)
if not success then
print('ERROR (keymaps.lua): failed to update persistence for keyboard layout at: ' .. layout_path)
end
end
local function enable_colemak()
-- ALL MODES
for _, mode in ipairs { 'n', 'v', 'o' } do
-- NOTE: (x -> y) means: y now does what x did
-- (hjkl -> knei)
vim.keymap.set(mode, 'k', remaps['k'], { desc = 'Left' })
vim.keymap.set(mode, 'n', remaps['n'], { desc = 'Down' })
vim.keymap.set(mode, 'e', remaps['e'], { desc = 'Up' })
vim.keymap.set(mode, 'i', remaps['i'], { desc = 'Right' })
-- TODO: think of replacing (HL -> KI) with something more useful. H and L
-- have the same affect as <C-u> and <C-d> with scrolloff=999.
-- (HJKL -> KNEI)
vim.keymap.set(mode, 'K', remaps['K'], { desc = 'Top of screen' })
vim.keymap.set(mode, 'I', remaps['I'], { desc = 'Bottom of screen' })
-- (knei -> ehjl), not symmetrical!
vim.keymap.set(mode, 'j', remaps['j'], { desc = 'End of word' })
-- (NEIO -> KJLH), not symmetrical!
vim.keymap.set(mode, 'J', remaps['J'], { desc = 'End of next word' })
end
-- NORMAL AND VISUAL MODE
for _, mode in ipairs { 'n', 'v' } do
vim.keymap.set(mode, 'N', remaps['N'], { desc = 'Join line' })
-- instead of (K -> E) , we set it to lsp hover. this is like how we would
-- normally have set K to be lsp hover instead of it's vim native bind which
-- is just a :man command
vim.keymap.set(mode, 'E', vim.lsp.buf.hover, { desc = 'LSP Hover' })
vim.keymap.set(mode, 'h', remaps['h'], { desc = 'Next search result' })
vim.keymap.set(mode, 'H', remaps['H'], { desc = 'Previous search result' })
vim.keymap.set(mode, 'L', remaps['L'], { desc = 'Insert mode at line start' })
end
-- NORMAL MODE ONLY
vim.keymap.set('n', 'l', remaps['l'], { desc = 'Insert mode' })
-- OPERATOR AND VISUAL MODE
vim.keymap.set({ 'o', 'v' }, 'l', remaps['l'], { desc = 'Inner' })
buf_jump_set('knei', 'hjkl')
-- return to next position
vim.keymap.set('n', '<C-l>', '<C-i>')
-- set persistence in .localsettings.json
set_layout_persistence 'colemak'
local settings = vim.g.local_settings
settings.keyboard_layout = 'colemak'
vim.g.local_settings = settings
print 'Colemak-DH layout enabled'
end
local function enable_qwerty(startup)
-- remove colemak mappings, we are toggling colemak off
if not startup then
-- we could just set the keymaps to avoid startup check but i find this cleaner since we don't need to add any descriptions
vim.keymap.del({ 'n', 'v', 'o' }, 'k')
vim.keymap.del({ 'n', 'v', 'o' }, 'n')
vim.keymap.del({ 'n', 'v', 'o' }, 'e')
vim.keymap.del({ 'n', 'v', 'o' }, 'i')
vim.keymap.del({ 'n', 'v', 'o' }, 'K')
vim.keymap.del({ 'n', 'v' }, 'N')
vim.keymap.del({ 'n', 'v' }, 'E')
vim.keymap.del({ 'n', 'v', 'o' }, 'I')
vim.keymap.del({ 'n', 'v' }, 'h')
vim.keymap.del({ 'n', 'v', 'o' }, 'j')
vim.keymap.del({ 'n', 'o', 'v' }, 'l')
vim.keymap.del({ 'n', 'v' }, 'H')
vim.keymap.del({ 'n', 'v', 'o' }, 'J')
vim.keymap.del({ 'n', 'v' }, 'L')
end
buf_jump_set('hjkl', 'knei')
-- set K to be lsp hover
vim.keymap.set('n', 'K', vim.lsp.buf.hover, { desc = 'LSP Hover' })
set_layout_persistence 'qwerty'
local settings = vim.g.local_settings
settings.keyboard_layout = 'qwerty'
vim.g.local_settings = settings
print 'QWERTY layout enabled'
end
local function start_layout()
--[[
this function checks a file: .localsettings.json in the nvim config directory to determine what keyboard layout to start with.
the.localsettings.json file is expected to be of the format:
{
"layout": "colemak",
... other settings
}
or alternatively, "qwerty".
]]
-- check if settings is in right format
local settings = vim.g.local_settings
if not settings then
print 'ERROR (keymaps.lua): vim.g.local_settings is nil'
return
end
if not settings.keyboard_layout then
print 'ERROR (keymaps.lua): parsing vim.g.local_settings: keyboard_layout is nil'
return
end
if settings.keyboard_layout == 'colemak' then
-- colemak
enable_colemak()
elseif settings.keyboard_layout == 'qwerty' then
-- qwerty
enable_qwerty(1)
else
print('ERROR (keymaps.lua): unexpected settings layout: ' .. settings.keyboard_layout)
return
end
alert_key_layout_change()
end
local function disable_yanks()
-- disable yank for change operator. just use delete operator for cutting.
vim.keymap.set('n', 'c', '"_c')
vim.keymap.set('n', 'C', '"_C')
vim.keymap.set('v', 'c', '"_c')
vim.keymap.set('v', 'C', '"_C')
-- disable yank for del
vim.keymap.set('n', '<Del>', '"_<Del>')
end
local function set_message_maps()
-- copy the most recent message
vim.api.nvim_set_keymap(
'n', -- normal mode
'<leader>mm', -- key combination
"<cmd>lua require('core.utils').copy_recent_message()<CR>",
{
noremap = true,
silent = true,
desc = 'Copy [M]ost recent [M]essage',
}
)
-- create a new buffer with all the messages from :messages inside
vim.keymap.set('n', '<leader>mn', function()
local buf = vim.api.nvim_create_buf(false, true)
-- get messages and put into buffer
local messages = vim.api.nvim_exec2('messages', { output = true }).output
vim.api.nvim_buf_set_lines(buf, 0, -1, false, vim.split(messages, '\n'))
-- open buffer in new window
vim.cmd.split()
vim.api.nvim_win_set_buf(0, buf)
-- set buffer options
vim.bo[buf].modifiable = false
vim.bo[buf].buftype = 'nofile'
vim.bo[buf].bufhidden = 'wipe'
end, { desc = 'Open [M]essages [N]ew buffer' })
end
-- GENERAL
-- clear highlights on search (? or /) when pressing <esc> in normal mode
vim.keymap.set('n', '<Esc>', '<cmd>nohlsearch<CR>')
-- diagnostics
vim.keymap.set('n', '<leader>dd', vim.diagnostic.open_float, { desc = '[D]isplay [D]iagnostics Under Cursor' })
vim.keymap.set('n', '<leader>dl', vim.diagnostic.setloclist, { desc = 'Open [D]iagnostic [L]ist' })
vim.keymap.set('n', '<leader>dy', function()
local diagnostics = vim.diagnostic.get(0, { lnum = vim.fn.line '.' - 1 })
if #diagnostics > 0 then
local messages = {}
for _, diagnostic in ipairs(diagnostics) do
table.insert(messages, diagnostic.message)
end
local text = table.concat(messages, '\n')
vim.fn.setreg('+', text) -- copy to system clipboard
vim.fn.setreg('"', text) -- copy to unnamed register
vim.notify 'Diagnostic copied to clipboard'
else
vim.notify 'No diagnostics under cursor'
end
end, { desc = '[D]iagnostic [Y]ank/Copy' })
-- leader <Esc> if you also use vim inside terminal itself (set -o vi)
vim.keymap.set('t', '<Esc><Esc>', '<C-\\><C-n>', { desc = 'Exit terminal mode' })
start_layout()
set_message_maps()
disable_yanks()
-- function to toggle between layouts: <leader>tc
local function toggle_keyboard_layout()
local settings = vim.g.local_settings
if settings.keyboard_layout == 'colemak' then
enable_qwerty()
elseif settings.keyboard_layout == 'qwerty' then -- add colemak mappings, we are toggling it on
enable_colemak()
else
print('ERROR (keymaps.lua): unexpected settings layout: ' .. settings.keyboard_layout)
end
alert_key_layout_change()
end
-- create a command to toggle layouts
vim.api.nvim_create_user_command('ToggleKeyLayout', toggle_keyboard_layout, {})
vim.keymap.set('n', '<leader>tk', ':ToggleKeyLayout<CR>', { desc = '[T]oggle [K]eyboard Layout' })
return remaps