-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpcom.vim
149 lines (143 loc) · 4.99 KB
/
pcom.vim
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
vim9script
import autoload 'popup.vim'
import autoload 'text.vim'
# Example of multi level popup
# export def QfLoc()
# var qf_commands = [
# {text: "Quickfix"},
# {text: "Next", key: "j", cmd: "cnext"},
# {text: "Prev", key: "k", cmd: "cprev"},
# {text: "Last", key: "J", cmd: "redraw|clast"},
# {text: "First", key: "K", cmd: "redraw|cfirst"},
# ]
# var loc_commands = [
# {text: "Locations"},
# {text: "Next", key: ".", cmd: "lnext"},
# {text: "Prev", key: ",", cmd: "lprev"},
# {text: "Last", key: ">", cmd: "redraw|llast"},
# {text: "First", key: "<", cmd: "redraw|lfirst"},
# ]
# var commands = [
# {text: "Quickfix", key: "q", close: true, cmd: () => {
# popup.Commands(qf_commands)
# }},
# {text: "Location", key: "l", close: true, cmd: () => {
# popup.Commands(loc_commands)
# }},
# ]
# popup.Commands(commands)
# enddef
# Navigate quickfix/location lists.
# Usage:
# import autoload 'pcom.vim'
# nnoremap <space>q <scriptcmd>pcom.Qf()<CR>
export def Qf()
var commands = []
if len(getqflist()) > 0
commands->extend([
{text: "Quickfix"},
{text: "Next", key: "j", cmd: "redraw|cnext"},
{text: "Prev", key: "k", cmd: "redraw|cprev"},
{text: "Last", key: "J", cmd: "redraw|clast"},
{text: "First", key: "K", cmd: "redraw|cfirst"},
])
endif
if len(getloclist(winnr())) > 0
commands->extend([
{text: "Locations"},
{text: "Next", key: ".", cmd: "redraw|lnext"},
{text: "Prev", key: ",", cmd: "redraw|lprev"},
{text: "Last", key: ">", cmd: "redraw|llast"},
{text: "First", key: "<", cmd: "redraw|lfirst"},
])
endif
popup.Commands(commands)
enddef
# horiontal scroll
# Usage:
# import autoload 'pcom.vim'
# nnoremap zl <scriptcmd>pcom.HScroll($'normal! {v:count1}zl')<CR>
# nnoremap zh <scriptcmd>pcom.HScroll($'normal! {v:count1}zh')<CR>
# nnoremap zs <scriptcmd>pcom.HScroll($'normal! zs')<CR>
# nnoremap ze <scriptcmd>pcom.HScroll($'normal! ze')<CR>
export def HScroll(initial: string)
exe initial
var commands = [
{text: "Horizontal scroll"},
{text: "Left", key: "h", cmd: "normal! zh"},
{text: "Right", key: "l", cmd: "normal! zl"},
{text: "10 x Left", key: "H", cmd: "normal! 10zh"},
{text: "10 x Right", key: "L", cmd: "normal! 10zl"},
{text: "Start", key: "s", cmd: "normal! zs"},
{text: "End", key: "e", cmd: "normal! ze"},
]
popup.Commands(commands)
enddef
# Navigate windows
# Usage:
# import autoload 'pcom.vim'
# nnoremap gt <scriptcmd>pcom.Windows("gt")<cr>
# nnoremap gT <scriptcmd>pcom.Windows("gT")<cr>
# nnoremap <C-w>h <scriptcmd>pcom.Windows("h")<cr>
# nmap <C-w><C-h> <C-w>h
# nnoremap <C-w>j <scriptcmd>pcom.Windows("j")<cr>
# nmap <C-w><C-j> <C-w>j
# nnoremap <C-w>k <scriptcmd>pcom.Windows("k")<cr>
# nmap <C-w><C-k> <C-w>k
# nnoremap <C-w>l <scriptcmd>pcom.Windows("l")<cr>
# nmap <C-w><C-l> <C-w>l
export def Windows(initial: string)
exe "wincmd" initial
var commands = [
{text: "Windows"},
{text: "Left", key: "h", cmd: "wincmd h"},
{text: "Down", key: "j", cmd: "wincmd j"},
{text: "Up", key: "k", cmd: "wincmd k"},
{text: "Right", key: "l", cmd: "wincmd l"},
{text: "Tabpages"},
{text: "Next", key: "t", cmd: "wincmd gt"},
{text: "Prev", key: "T", cmd: "wincmd gT"},
]
popup.Commands(commands)
enddef
# Various text transformations
# Usage:
# import autoload 'pcom.vim'
# xnoremap <space>t <scriptcmd>pcom.TextTr()<cr>
# nnoremap <space>t <scriptcmd>pcom.TextTr()<cr>
export def TextTr()
if mode() == 'n'
normal! g_v^
endif
var region = getregion(getpos('v'), getpos('.'), {type: mode()})
var base64_commands = [
{text: "Base64"},
{text: "Encode", key: "e", close: true, cmd: () => {
setreg("", region->str2blob()->base64_encode())
normal! ""p
}},
{text: "Decode", key: "d", close: true, cmd: () => {
# TODO: go line by line?
setreg("", region->join('')->base64_decode()->blob2str()->join("\n"))
normal! ""p
}}
]
var commands = [
{text: "Text transform"},
{text: "Fix spaces", key: "s", close: true, cmd: () => {
text.FixSpaces(line('v'), line('.'))
exe "normal!" mode()
}},
{text: "Base64", key: "b", close: true, cmd: () => {
popup.Commands(base64_commands)
}},
{text: "Calc", key: "c", close: true, cmd: () => {
var result = system($'python -c "from math import *; print({region->join(" ")})"')->trim()
if v:shell_error == 0
setreg("", result)
normal! ""p
endif
}},
]
popup.Commands(commands)
enddef