Skip to content

Commit

Permalink
部分配置文件去viml化
Browse files Browse the repository at this point in the history
  • Loading branch information
yaocccc committed Feb 6, 2023
1 parent a5524fc commit 9044ae3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 69 deletions.
119 changes: 51 additions & 68 deletions lua/keymap.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
local G = require('G')

G.cmd([[
func MagicSave()
" If the directory is not exited, create it
if empty(glob(expand("%:p:h")))
call system("mkdir -p " . expand("%:p:h"))
endif
" If the file is not writable, use sudo to write it
if &buftype == 'acwrite'
w !sudo tee > /dev/null %
else
w
endif
endf
]])
G.map({
-- 设置s t 无效 ;=: ,重复上一次宏操作
{ 'n', 's', '<nop>', {} },
Expand Down Expand Up @@ -53,8 +39,8 @@ G.map({
{ 'v', 'P', 'Pgvy', { noremap = true } },

-- S保存 Q退出
{ 'n', 'S', ':call MagicSave()<cr>', { noremap = true, silent = true } },
{ 'v', 'S', ':call MagicSave()<cr>', { noremap = true, silent = true } },
{ 'n', 'S', ':call v:lua.MagicSave()<cr>', { noremap = true, silent = true } },
{ 'v', 'S', ':call v:lua.MagicSave()<cr>', { noremap = true, silent = true } },
{ 'n', 'Q', ':q!<cr>', { noremap = true, silent = true } },

-- VISUAL SELECT模式 s-tab tab左右缩进
Expand Down Expand Up @@ -146,58 +132,55 @@ G.map({

-- 切换是否wrap
{ 'n', '\\w', "&wrap == 1 ? ':set nowrap<cr>' : ':set wrap<cr>'", { noremap = true, expr = true } },
})

-- 重设tab长度
G.cmd('command! -nargs=* SetTab call SwitchTab(<q-args>)')
G.cmd([[
fun! SwitchTab(tab_len)
if !empty(a:tab_len)
let [&shiftwidth, &softtabstop, &tabstop] = [a:tab_len, a:tab_len, a:tab_len]
else
let l:tab_len = input('input shiftwidth: ')
if !empty(l:tab_len)
let [&shiftwidth, &softtabstop, &tabstop] = [l:tab_len, l:tab_len, l:tab_len]
endif
endif
redraw!
echo printf('shiftwidth: %d', &shiftwidth)
endf
]])

-- 折叠
G.map({
{ 'n', '-', "za", { noremap = true, silent = true } },
{ 'v', '-', 'zf', { noremap = true, silent = true } },
})
-- 折叠
{ 'n', '-', "za", { noremap = true, silent = true } },
{ 'v', '-', 'zf', { noremap = true, silent = true } },

-- space 行首行尾跳转
G.map({
{ 'n', '<space>', ':call MagicMove()<cr>', { noremap = true, silent = true } },
{ 'n', '0', '%', { noremap = true } },
{ 'v', '0', '%', { noremap = true } },
-- space 行首行尾跳转
{ 'n', '<space>', ':call v:lua.MagicMove()<cr>', { noremap = true, silent = true } },
{ 'n', '0', '%', { noremap = true } },
{ 'v', '0', '%', { noremap = true } },

-- 驼峰转换
{ 'v', 'T', ':call v:lua.MagicToggleHump(v:true)<CR>', { noremap = true, silent = true } },
{ 'v', 't', ':call v:lua.MagicToggleHump(v:false)<CR>', { noremap = true, silent = true } },
})
G.cmd([[
fun! MagicMove()
let [l:first, l:head] = [1, len(getline('.')) - len(substitute(getline('.'), '^\s*', '', 'G')) + 1]
let l:before = col('.')
exe l:before == l:first && l:first != l:head ? 'norm! ^' : 'norm! $'
let l:after = col('.')
if l:before == l:after
exe 'norm! 0'
endif
endf
]])

-- 驼峰转换
G.map({ { 'v', 't', ':call ToggleHump()<CR>', { noremap = true, silent = true } }, })
G.cmd([[
fun! ToggleHump()
let [l, c1, c2] = [line('.'), col("'<"), col("'>")]
let line = getline(l)
let w = line[c1 - 1 : c2 - 2]
let w = w =~ '_' ? substitute(w, '\v_(.)', '\u\1', 'g') : substitute(substitute(w, '\v^(\u)', '\l\1', 'g'), '\v(\u)', '_\l\1', 'g')
call setbufline('%', l, printf('%s%s%s', c1 == 1 ? '' : line[:c1-2], w, c2 == 1 ? '' : line[c2-1:]))
call cursor(l, c1)
endf
]])

-- 光标在$ 0 ^依次跳转
function MagicMove()
local first = 1
local head = #G.fn.getline('.') - #G.fn.substitute(G.fn.getline('.'), '^\\s*', '', 'G') + 1
local before = G.fn.col('.')
G.fn.execute(before == first and first ~= head and 'norm! ^' or 'norm! $')
local after = G.fn.col('.')
if before == after then
G.fn.execute('norm! 0')
end
end

-- 1 当目录不存在时 先创建目录, 2 当前文件是acwrite时, 用sudo保存
function MagicSave()
if G.fn.empty(G.fn.glob(G.fn.expand('%:p:h'))) then G.fn.system('mkdir -p ' .. G.fn.expand('%:p:h')) end
if G.o.buftype == 'acwrite' then
G.fn.execute('w !sudo tee > /dev/null %')
else
G.fn.execute('w')
end
end

-- 驼峰转换 MagicToggleHump(true) 首字母大写 MagicToggleHump(false) 首字母小写
function MagicToggleHump(upperCase)
G.fn.execute('normal! gv"tx')
local w = G.fn.getreg('t')
local toHump = w:find('_') ~= nil
if toHump then
w = w:gsub('_(%w)', function(c) return c:upper() end)
else
w = w:gsub('(%u)', function(c) return '_' .. c:lower() end)
end
if w:sub(1, 1) == '_' then w = w:sub(2) end
if upperCase then w = w:sub(1,1):upper() .. w:sub(2) end
G.fn.setreg('t', w)
G.fn.execute('normal! "tP')
end
2 changes: 1 addition & 1 deletion lua/profile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ G.opt.viminfo = "!,'10000,<50,s10,h"
-- 折叠
G.opt.foldenable = true
G.opt.foldmethod = 'manual'
G.opt.viewdir = os.getenv('HOME') .. '/.config/nvim/cache//viewdir'
G.opt.viewdir = os.getenv('HOME') .. '/.config/nvim/cache/viewdir'
G.opt.foldtext = 'v:lua.MagicFoldText()'

function MagicFoldText()
Expand Down

0 comments on commit 9044ae3

Please sign in to comment.