Skip to content
This repository
Browse code

:Tabularize with no args reuses the last pattern

The docs are updated to document this feature.  Calling :Tabularize with
arguments and no previous pattern prints an error message and does
nothing.  This works with both patterns and named commands.
Suggested by nocash - http://github.com/godlygeek/tabular/pull/2
  • Loading branch information...
commit b7b4d8791a50013b56c2d68b6c5708c808203b54 1 parent 4b543cd
Matt Wozniski authored March 31, 2011
4  doc/Tabular.txt
@@ -72,7 +72,9 @@ to be acted upon.  You can always override this by specifying a range, though.
72 72
 The second thing you should notice is that you'll almost certainly be able to
73 73
 abbreviate :Tabularize to :Tab - using this form in mappings and scripts is
74 74
 discouraged as it will make conflicts with other scripts more likely, but for
75  
-interactive use it's a nice timesaver.
  75
+interactive use it's a nice timesaver.  Another convenience feature is that
  76
+running :Tabularize without providing a new pattern will cause it to reuse the
  77
+last pattern it was called with.
76 78
 
77 79
 So, anyway, now the commas line up.  Splitting the lines on commas, Tabular
78 80
 realized that 'Some short phrase' would need to be padded with spaces to match
27  plugin/Tabular.vim
@@ -235,14 +235,27 @@ endfunction
235 235
 "
236 236
 " Align text, either using the given pattern, or the command associated with
237 237
 " the given name.
238  
-com! -nargs=+ -range -complete=customlist,<SID>CompleteTabularizeCommand
  238
+com! -nargs=* -range -complete=customlist,<SID>CompleteTabularizeCommand
239 239
    \ Tabularize <line1>,<line2>call Tabularize(<q-args>)
240 240
 
241 241
 function! Tabularize(command) range
  242
+  if empty(a:command)
  243
+    if !exists("s:last_tabularize_command")
  244
+      echohl ErrorMsg
  245
+      echomsg "Tabularize hasn't been called yet; no pattern/command to reuse!"
  246
+      echohl None
  247
+      return
  248
+    endif
  249
+  else
  250
+    let s:last_tabularize_command = a:command
  251
+  endif
  252
+
  253
+  let command = s:last_tabularize_command
  254
+
242 255
   let range = a:firstline . ',' . a:lastline
243 256
 
244 257
   try
245  
-    let [ pattern, format ] = s:ParsePattern(a:command)
  258
+    let [ pattern, format ] = s:ParsePattern(command)
246 259
 
247 260
     if !empty(pattern)
248 261
       let cmd  = "tabular#TabularizeStrings(a:lines, " . string(pattern)
@@ -255,12 +268,12 @@ function! Tabularize(command) range
255 268
 
256 269
       exe range . 'call tabular#PipeRange(pattern, cmd)'
257 270
     else
258  
-      if exists('b:TabularCommands') && has_key(b:TabularCommands, a:command)
259  
-        let command = b:TabularCommands[a:command]
260  
-      elseif has_key(s:TabularCommands, a:command)
261  
-        let command = s:TabularCommands[a:command]
  271
+      if exists('b:TabularCommands') && has_key(b:TabularCommands, command)
  272
+        let command = b:TabularCommands[command]
  273
+      elseif has_key(s:TabularCommands, command)
  274
+        let command = s:TabularCommands[command]
262 275
       else
263  
-        throw "Unrecognized command " . string(a:command)
  276
+        throw "Unrecognized command " . string(command)
264 277
       endif
265 278
 
266 279
       exe range . command

0 notes on commit b7b4d87

Please sign in to comment.
Something went wrong with that request. Please try again.