Skip to content
Browse files

New Command: AddColumn

  • Loading branch information...
1 parent bca09f7 commit b74f8998562edcc7e9198129b1027fdc5d4eb4c5 @chrisbra committed Mar 20, 2013
Showing with 83 additions and 11 deletions.
  1. +20 −0 doc/ft-csv.txt
  2. +63 −11 ftplugin/csv.vim
View
20 doc/ft-csv.txt
@@ -34,6 +34,7 @@ NO WARRANTY, EXPRESS OR IMPLIED. USE AT-YOUR-OWN-RISK.
3.21 Vertical Folding.......................|csv-vertfold|
3.22 Transposing columns....................|csv-transpose|
3.23 Transforming into a table..............|csv-tabularize|
+ 3.24 Add new empty columns..................|AddColumn_CSV|
4. CSV Filetype configuration...................|csv-configuration|
4.1 Delimiter...............................|csv-delimiter|
4.2 Column..................................|csv-column|
@@ -733,6 +734,24 @@ available for non-CSV files as :Table
Note: Each row must contain exactly as many fields as columns.
+3.24 Add new empty columns *AddColumn_CSV*
+--------------------------
+
+If you want to add new empty columns to your file you can use the
+:AddColumn command: >
+
+ :[range]AddColumn [column] [count]
+
+By default, this works for the whole file, but you can give a different range
+to which the AddColumn command applies. If no arguments are given, the new
+empty column will be added after the column on which the cursor is. You can
+however add as first argument the column number after which the new column
+needs to be added.
+
+Additionally, you can also add a count number to add several columns at once
+after the specified column number. You 0 for the column number, if you want to
+add several columns after the current column.
+
==============================================================================
4. CSV Configuration *csv-configuration*
@@ -1318,6 +1337,7 @@ search pattern, if none is given.
0.29 (unreleased) {{{1
- setup |QuitPre| autocommand to quit cleanly in newer vims when using :Header
and :VHeader
+- new |AddColumn_CSV| command
0.28 Dec 14, 2012 {{{1
- new command :Table to create ascii tables for non-csv files
View
74 ftplugin/csv.vim
@@ -160,7 +160,7 @@ fu! <sid>Init(startline, endline) "{{{3
" \ delf <sid>NewDelimiter | delf <sid>DuplicateRows | delf <sid>IN |
" \ delf <sid>SaveOptions | delf <sid>CheckDuplicates |
" \ delf <sid>CompleteColumnNr | delf <sid>CSVPat | delf <sid>Transpose |
- " \ delf <sid>LocalSettings()
+ " \ delf <sid>LocalSettings() | delf <sid>AddColumn
endfu
fu! <sid>LocalSettings(type) "{{{3
@@ -1069,23 +1069,23 @@ fu! <sid>MoveColumn(start, stop, ...) range "{{{3
endif
if !exists("b:csv_fixed_width_cols")
let fields=split(content, b:col . '\zs')
+ " Add delimiter to destination column, in case there was none,
+ " remove delimiter from source, in case destination did not have one
+ if matchstr(fields[dest], '.$') !~? b:delimiter
+ let fields[dest] = fields[dest] . b:delimiter
+ if matchstr(fields[source], '.$') =~? b:delimiter
+ let fields[source] = substitute(fields[source],
+ \ '^\(.*\).$', '\1', '')
+ endif
+ endif
else
let fields=[]
+ " this is very inefficient!
for j in range(1, max, 1)
call add(fields, matchstr(content, <sid>GetColPat(j,0)))
endfor
endif
- " Add delimiter to destination column, in case there was none,
- " remove delimiter from source, in case destination did not have one
- if matchstr(fields[dest], '.$') !~? b:delimiter
- let fields[dest] = fields[dest] . b:delimiter
- if matchstr(fields[source], '.$') =~? b:delimiter
- let fields[source] = substitute(fields[source],
- \ '^\(.*\).$', '\1', '')
- endif
- endif
-
let fields= (source == 0 ? [] : fields[0 : (source-1)])
\ + fields[ (source+1) : dest ]
\ + [ fields[source] ] + fields[(dest+1):]
@@ -1097,6 +1097,54 @@ fu! <sid>MoveColumn(start, stop, ...) range "{{{3
endfu
+fu! <sid>AddColumn(start, stop, ...) range "{{{3
+ " Add new empty column
+ " Explicitly give the range as argument,
+ " cause otherwise, Vim would move the cursor
+ if exists("b:csv_fixed_width_cols")
+ call <sid>Warn("Adding Columns only works for delimited files")
+ return
+ endif
+
+ let wsv = winsaveview()
+
+ let col = <sid>WColumn()
+ let max = <sid>MaxColumns()
+
+ " If no argument is given, move current column after last column
+ let pos=(exists("a:1") && a:1 >= 0 && a:1 <= max ? a:1 : col)
+ let cnt=(exists("a:2") && a:2 > 0 ? a:2 : 1)
+
+ if pos == 0
+ let pos = col
+ endif
+
+ " translate 1 based columns into zero based list index
+ let pos -= 1
+ let col -= 1
+
+ if pos == 0
+ let pat = '^'
+ elseif pos == max-1
+ let pat = '$'
+ else
+ let pat = <sid>GetColPat(pos,1)
+ endif
+
+ " instead of reading the whole range into memory
+
+ for i in range(a:start, a:stop)
+ let content = getline(i)
+ if content =~ '^\s*\V'. escape(b:csv_cmt[0], '\\')
+ " skip comments
+ continue
+ endif
+ exe printf("%ds/%s/%s/e", i, pat, repeat(' '.b:delimiter, cnt))
+ endfor
+
+ call winrestview(wsv)
+endfu
+
fu! <sid>SumColumn(list) "{{{3
" Sum a list of values, but only consider the digits within each value
" parses the digits according to the given format (if none has been
@@ -1760,6 +1808,9 @@ fu! <sid>CommandDefinitions() "{{{3
" Alias for :Tabularize, might be taken by Tabular plugin
call <sid>LocalCmd('CSVTabularize', ':call <sid>Tabularize(<bang>0,<line1>,<line2>)',
\ '-bang -range=%')
+ call <sid>LocalCmd("AddColumn",
+ \ ':call <sid>AddColumn(<line1>,<line2>,<f-args>)',
+ \ '-range=% -nargs=* -complete=custom,<sid>SortComplete')
endfu
fu! <sid>Map(map, name, definition) "{{{3
@@ -1803,6 +1854,7 @@ fu! <sid>Menu(enable) "{{{3
amenu CSV.Column.Analy&ze :Analyze<cr>
amenu CSV.Column.&Arrange :%ArrangeCol<cr>
amenu CSV.Column.&UnArrange :%UnArrangeCol<cr>
+ amenu CSV.Column.&Add :%AddColumn<cr>
amenu CSV.-sep2- <nul>
amenu CSV.&Toggle\ Header :HeaderToggle<cr>
amenu CSV.&ConvertData :ConvertData<cr>

0 comments on commit b74f899

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