From b300c2b94904776326041e1395dc3f8e1dbaaf6c Mon Sep 17 00:00:00 2001 From: Scott Mcdermott Date: Thu, 23 May 2024 07:38:41 -0700 Subject: [PATCH 1/2] Make the maps install/uninstall driven by callable functions this allows to "disable" and "enable" smart tabs, by putting all the maps in functions. we also have an autocommand we need to install/uninstall during enable/disable --- plugin/stabs.vim | 107 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 77 insertions(+), 30 deletions(-) diff --git a/plugin/stabs.vim b/plugin/stabs.vim index 0391aea..7a6dce5 100644 --- a/plugin/stabs.vim +++ b/plugin/stabs.vim @@ -17,14 +17,6 @@ if !exists('g:stabs_insert_leave') endif -if g:stabs_maps =~ 't' - inoremap StabsTab() -endif - -if g:stabs_maps =~ 'b' - inoremap StabsBS() -endif - " TODO: Properly add CTRL-d and CTRL-t mappings "imap :call SmartDeleteTab() "imap SmartInsertTab() @@ -83,12 +75,6 @@ if g:stabs_insert_leave exe 's/'.s:GetIndentRegex().' *$//e' endif endfun - - " Remove indentation tabs when leaving insert mode - augroup Stabs - autocmd! - autocmd InsertLeave * call CheckLeaveLine(line('.')) - augroup END endif @@ -225,22 +211,6 @@ fun! StabsCR() endif endfun -if g:stabs_maps =~ 'c' - inoremap StabsCR() -endif -" Notice \END> results in \, which is the end key -if g:stabs_maps =~ 'o' - nnoremap o o=StabsFixAlign(line('.'))."\END>" -endif -if g:stabs_maps =~ 'O' - nnoremap O O=StabsFixAlign(line('.'))."\END>" -endif - -" The = is implemented by remapping it so that it calls the original = and -" then checks all indents using StabsFixAlign. -if g:stabs_maps =~ '=' - nnoremap = StabsEqual() -endif fun! StabsEqual() set operatorfunc=StabsRedoIndent " Call the operator func so we get the range @@ -297,6 +267,83 @@ fun! s:RetabIndent( bang, firstl, lastl, tab ) if newtabstop != &tabstop | let &tabstop = newtabstop | endif endfun +"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" + +function StabsInstall() + + if g:stabs_active | return | endif + + " imaps + if g:stabs_maps =~ 't' + inoremap StabsTab() + endif + if g:stabs_maps =~ 'b' + inoremap StabsBS() + endif + if g:stabs_maps =~ 'c' + inoremap StabsCR() + endif + + " nmaps + if g:stabs_maps =~ 'o' + " Notice \END> results in \, which is the end key + nnoremap o + \ o=StabsFixAlign(line('.'))."\END>" + endif + if g:stabs_maps =~ 'O' + nnoremap O + \ O=StabsFixAlign(line('.'))."\END>" + endif + if g:stabs_maps =~ '=' + " The = is implemented by remapping it so that it calls the + " original = and then checks all indents using StabsFixAlign. + nnoremap = + \ StabsEqual() + endif + + " autocmds + if g:stabs_insert_leave + augroup Stabs + au! + autocmd InsertLeave * call CheckLeaveLine(line('.')) + augroup END + endif + + let g:stabs_active = 1 +endfunc + +function StabsUninstall() + + if !g:stabs_active | return | endif + + if g:stabs_maps =~ 't' + iunmap + endif + if g:stabs_maps =~ 'b' + iunmap + endif + if g:stabs_maps =~ 'c' + iunmap + endif + if g:stabs_maps =~ 'o' + nunmap o + endif + if g:stabs_maps =~ 'O' + nunmap O + endif + if g:stabs_maps =~ '=' + nunmap = + endif + if g:stabs_insert_leave + autocmd! Stabs + endif + + let g:stabs_active = 0 +endfunc + +"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" + +call StabsInstall() " Retab the indent of a file - ie only the first nonspace. " Optional argument specified the value of the new tabstops From 28f71bca65fe60ceafc51cc9ab7535af171ae630 Mon Sep 17 00:00:00 2001 From: Scott Mcdermott Date: Thu, 23 May 2024 07:42:29 -0700 Subject: [PATCH 2/2] Add a stabs mode toggle function This allows only a single map to be used, driven by state retained in a single global variable --- plugin/stabs.vim | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/plugin/stabs.vim b/plugin/stabs.vim index 7a6dce5..4e153ba 100644 --- a/plugin/stabs.vim +++ b/plugin/stabs.vim @@ -3,7 +3,6 @@ if exists('g:loaded_stabs') && g:loaded_stabs endif let g:loaded_stabs = 1 - if !exists('g:stabs_indent_regex') let g:stabs_indent_regex = '^\t*' endif @@ -341,9 +340,14 @@ function StabsUninstall() let g:stabs_active = 0 endfunc +function StabsToggle() + if !exists('g:stabs_active') | let g:stabs_active = 0 | endif + call function(g:stabs_active ? 'StabsUninstall' : 'StabsInstall')() +endfunc + """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" -call StabsInstall() +call StabsToggle() " Retab the indent of a file - ie only the first nonspace. " Optional argument specified the value of the new tabstops