Wikia

Vim Tips Wiki

Script:231

Talk0
1,610pages on
this wiki
Revision as of 07:38, November 15, 2011 by JohnBot (Talk | contribs)

Use this page to discuss script 231 Smart Tabs: use tabs for indent, spaces for alignment

  • Add constructive comments, bug reports, or discuss improvements (see the guideline).
  • Do not document the script here (the author should do that on vim.org).
  • This page may be out of date: check the script's vim.org page above, and its release notes.

Bug report

There is a bug on line 284 of ctab.vim, version 2.3 which prevents :RetabIndent from working. Below patch fixes this:

diff -- a/plugin/ctab.vim b/plugin/ctab.vim
index 98d305b..9e873fe 100644
--- a/plugin/ctab.vim
+++ b/plugin/ctab.vim
@@ -281,7 +281,7 @@ fun! s:RetabIndent( bang, firstl, lastl, tab )
   let checkspace=((!&expandtab)? "^\<tab>* ": "^ *\<tab>")
   let l = a:firstl
   let force= a:tab != '' && a:tab != 0 && (a:tab != &tabstop)
-  let checkalign = &expandtab || !(&autoindent || &indentexpr || &cindent) ! exists('g:ctab_disable_checkalign') || g:ctab_disable_checkalign==0
+  let checkalign = &expandtab || !(&autoindent || &indentexpr || &cindent) || ! exists('g:ctab_disable_checkalign') || g:ctab_disable_checkalign==0
   let newtabstop = (force?(a:tab):(&tabstop))
   while l <= a:lastl
     let txt=getline(l)

--Themiwi 14:51, August 21, 2010 (UTC)

Bug with empty lines

I found problem in ctab.vim script.

How to reproduce:

Ctab problem
:e test.c
:set cindent
i
test() {<CR>
<CR>
<CR>
test();<CR>
<CR>
}<CR>

I fixed the problem and add some minor fixes (correct undo for some <CR>, handling <Esc>):

--- ctab.vim.orig	2011-02-07 11:46:44.000000000 +0300
+++ ctab.vim	2011-02-07 13:35:50.000000000 +0300
@@ -199,6 +199,9 @@
   " Check the alignment of line.
   " Used in the case where some alignment whitespace is required .. like for unmatched brackets.
   fun! s:CheckAlign(line)
+    if getline(line('.') - 1) =~ '^\s*$'
+      call setline(line('.') - 1, '')
+    endif
     if &expandtab || !(&autoindent || &indentexpr || &cindent)
       return ''
     endif
@@ -246,13 +249,27 @@
   fun! s:CheckCR()
     echo 'SID:'.s:SID()
     if getline('.') =~ '^\s*$'
-      return "\<CR>"
+      return "\<c-r>=<SNR>".s:SID().'_RemoveSpaces(line(''.''))'."\<CR>\<CR>\<END>"
     else
       return "\<CR>\<c-r>=<SNR>".s:SID().'_CheckAlign(line(''.''))'."\<CR>\<END>"
     endif
   endfun
 
+  fun! s:RemoveSpaces(lnum)
+    undojoin
+    call setline(a:lnum, '')
+    return ''
+  endfun
+
+  fun! s:CheckEsc()
+    if getline('.') =~ '^\s*$'
+      return "\<c-r>=<SNR>".s:SID().'_RemoveSpaces(line(''.''))'."\<CR>\<Esc>\<END>"
+    endif
+    return "\<Esc>"
+  endfun
+
   "exe 'inoremap '.s:buff_map.'<silent> <CR> <CR><c-r>=<SID>CheckAlign(line(''.''))."\<lt>END>"<CR>'
+  exe 'inoremap '.s:buff_map.'<silent> <expr> <Esc> <SID>CheckEsc()'
   exe 'inoremap '.s:buff_map.'<silent> <expr> <CR> <SID>CheckCR()'
   exe 'nnoremap '.s:buff_map.'<silent> o o<c-r>=<SID>CheckAlign(line(''.''))."\<lt>END>"<CR>'
   exe 'nnoremap '.s:buff_map.'<silent> O O<c-r>=<SID>CheckAlign(line(''.''))."\<lt>END>"<CR>'

--Konstantin Lepa 11:05, February 7, 2011 (UTC)

Comments

Some information on this plugin is at Indent with tabs, align with spaces. JohnBeckett 09:45, August 22, 2010 (UTC)

Around Wikia's network

Random Wiki