-
Notifications
You must be signed in to change notification settings - Fork 0
/
capslock_vimshell-compatible.vim
184 lines (163 loc) · 5.5 KB
/
capslock_vimshell-compatible.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
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
" capslock.vim - Software Caps Lock
" Maintainer: Tim Pope
" $Id: capslock.vim,v 1.1 2006/12/06 17:45:20 tpope Exp $
" This plugin enables a software caps lock. This is advantageous over a
" regular caps lock in that normal mode commands, other buffers, and other
" applications are unaffected.
"
" The default insert mode mapping is <C-G>c, and there is no default normal
" mode mapping.. If you make frequent use of this feature, you will probably
" want to change this. Try something like
"
" imap <C-L> <Plug>CapsLockToggle
" nmap <Leader>l <Plug>CapsLockToggle
"
" Alternatively, enable the shorter insert mode mapping on a per-filetype
" basis:
"
" autocmd FileType sql,cobol imap <buffer> <C-L> <Plug>CapsLockToggle
"
" <Plug>CapsLockEnable and <Plug>CapsLockDisable are also provided.
"
" One may prefer a normal mode mapping that enters insert mode and activates
" caps lock:
"
" nmap <Leader>i i<Plug>CapsLockToggle
"
" By default, caps lock is automatically disabled after leaving insert mode
" for the insert mode mappings, but must be explicitly disabled for the normal
" mode mappings. If you always want to use the latter method, make your
" insert mode mapping call the normal mode one.
"
" imap <C-L> <C-O><Plug>CapsLockToggle
"
" Two functions, CapsLockStatusline() and CapsLockSTATUSLINE(), are provided
" for use inside %{} in your statusline. These respectively return "[caps]"
" and ",CAPS" if the software caps lock is enabled. Here's an example usage
" that won't cause problems if capslock.vim is missing:
"
" set statusline=...%{exists('*CapsLockStatusline')?CapsLockStatusline():''}
" ============================================================================
" Exit quickly when:
" - this plugin was already loaded (or disabled)
" - when 'compatible' is set
if (exists("g:loaded_capslock") && g:loaded_capslock) || &cp
finish
endif
let g:loaded_capslock = 1
let s:cpo_save = &cpo
set cpo&vim
" Code {{{1
" Uses for this should be rare, but if you :let the following variable, caps
" lock state should be tracked globally. Largely untested, let me know if you
" have problems.
if exists('g:capslock_global')
let s:buffer = ''
else
let s:buffer = '<buffer>'
endif
function! s:enable(mode,...)
let i = char2nr('A')
while i <= char2nr('Z')
exe a:mode."noremap" s:buffer nr2char(i) nr2char(i+32)
exe a:mode."noremap" s:buffer nr2char(i+32) nr2char(i)
let i = i + 1
endwhile
if a:0 && a:1
if exists('g:capslock_global')
let g:capslock_persist = 1
else
let b:capslock_persist = 1
endif
endif
return ""
endfunction
function! s:disable(mode)
let i = char2nr('A')
while i <= char2nr('Z')
silent! exe a:mode."unmap" s:buffer nr2char(i)
silent! exe a:mode."unmap" s:buffer nr2char(i+32)
let i = i + 1
endwhile
unlet! b:capslock_persist
if exists('g:capslock_global')
unlet! g:capslock_persist
endif
return ""
endfunction
function! s:toggle(mode,...)
if s:enabled(a:mode)
call s:disable(a:mode)
else
if a:0
call s:enable(a:mode,a:1)
else
call s:enable(a:mode)
endif
endif
return ""
endfunction
function! s:enabled(mode)
return maparg('a',a:mode) == 'A'
endfunction
function! s:exitcallback()
if !exists('g:capslock_persist') && !exists('b:capslock_persist') && s:enabled('i')
call s:disable('i')
endif
endfunction
function! CapsLockStatusline()
if mode() == 'c' && s:enabled('c')
" This won't actually fire because the statusline is apparently not
" updated in command mode
return '[(caps)]'
elseif s:enabled('i')
return '[caps]'
else
return ''
endif
endfunction
function! CapsLockSTATUSLINE()
if mode() == 'c' && s:enabled('c')
return ',(CAPS)'
elseif s:enabled('i')
return ',CAPS'
else
return ''
endif
endfunction
"augroup capslock
" if v:version >= 700
" autocmd InsertLeave * call s:exitcallback()
" endif
" autocmd CursorHold * call s:exitcallback()
"augroup END
" }}}1
" Maps {{{1
noremap <silent> <Plug>CapsLockToggle :<C-U>call <SID>toggle('i',1)<CR>
noremap <silent> <Plug>CapsLockEnable :<C-U>call <SID>enable('i',1)<CR>
noremap <silent> <Plug>CapsLockDisable :<C-U>call <SID>disable('i')<CR>
inoremap <silent> <Plug>CapsLockToggle <C-R>=<SID>toggle('i')<CR>
inoremap <silent> <Plug>CapsLockEnable <C-R>=<SID>enable('i')<CR>
inoremap <silent> <Plug>CapsLockDisable <C-R>=<SID>disable('i')<CR>
cnoremap <silent> <Plug>CapsLockToggle <C-R>=<SID>toggle('c')<CR>
cnoremap <silent> <Plug>CapsLockEnable <C-R>=<SID>enable('c')<CR>
cnoremap <silent> <Plug>CapsLockDisable <C-R>=<SID>disable('c')<CR>
cnoremap <silent> <SID>CapsLockDisable <C-R>=<SID>disable('c')<CR>
if !hasmapto("<Plug>CapsLockToggle")
imap <C-G>c <Plug>CapsLockToggle
endif
" Enable g:capslock_command_mode if you want capslock.vim to attempt to
" disable command mode caps lock after each :command. This is hard to trap
" elegantly so it is disabled by default. If you use this, you still must
" provide your own command mode mapping.
if exists('g:capslock_command_mode')
map <script> : :<SID>CapsLockDisable
map <script> / /<SID>CapsLockDisable
map <script> ? ?<SID>CapsLockDisable
cmap <script> <CR> <SID>CapsLockDisable<CR>
" Breaks arrow keys
"cmap <script> <Esc> <SID>CapsLockDisable<Esc>
endif
" }}}1
let &cpo = s:cpo_save
" vim:set ft=vim ff=unix ts=8 sw=4 sts=4: