" 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, 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 CapsLockToggle " nmap l CapsLockToggle " " Alternatively, enable the shorter insert mode mapping on a per-filetype " basis: " " autocmd FileType sql,cobol imap CapsLockToggle " " CapsLockEnable and CapsLockDisable are also provided. " " One may prefer a normal mode mapping that enters insert mode and activates " caps lock: " " nmap i iCapsLockToggle " " 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 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 = '' 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 CapsLockToggle :call toggle('i',1) noremap CapsLockEnable :call enable('i',1) noremap CapsLockDisable :call disable('i') inoremap CapsLockToggle =toggle('i') inoremap CapsLockEnable =enable('i') inoremap CapsLockDisable =disable('i') cnoremap CapsLockToggle =toggle('c') cnoremap CapsLockEnable =enable('c') cnoremap CapsLockDisable =disable('c') cnoremap CapsLockDisable =disable('c') if !hasmapto("CapsLockToggle") imap c 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