Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

:InitCsv overrides locally set b:delimiter #43

Closed
jeetsukumaran opened this issue Feb 8, 2015 · 5 comments
Closed

:InitCsv overrides locally set b:delimiter #43

jeetsukumaran opened this issue Feb 8, 2015 · 5 comments

Comments

@jeetsukumaran
Copy link
Contributor

If I set b:delimiter for a file (and update b:col accordingly) to handle a custom delimiter for a file, and at any time that call :InitCsv, the b:delimiter value gets set again to the default or g:csv_delim.

There are situations when :InitCsv might need to be called to, e.g., update/change highlighting, but we want the b:delimiter preserved if it was manually changed.

Since b:delimiter is set automatically, and there is (I think) no way to tell if it was manually changed, I suggest a non-userspace variable (e.g, b:_delimiter) that is set to g:csv_delim automatically, but is only used if b:delimiter is not set. But maybe some other solution would be better.

@chrisbra
Copy link
Owner

chrisbra commented Feb 8, 2015

Does the latest commit help you?

@jeetsukumaran
Copy link
Contributor Author

Afraid not.

Given the following file (which is named "f.csv", so that filetype is csv):

a#b#c
1#2,2,2#3,4,5,6
1#2,2,2#3,4,5,6

Opening it, by default, the CSV plugin sees and highlights six columns instead of three, because the default , is the delimiter instead of '#'.

So I set b:delimiter and b:col:

let old_delim = get(b:, "delimiter", ",")
let b:delimiter = '#'
let b:col=substitute(b:col, old_delim, b:delimiter, 'g')

This works, and correctly recognizes the columns (though the highlighting is still wrong).

But for whatever reason (e.g., to update the column highlighting), if I then run

:InitCSV

It goes back to using "," (or whatever "g:csv_delim" is) for the delimiter, as verified by:

:echo b:delimiter

Here is what I use to make ad-hoc changing of the delimiter on a file-by-file basis more convenient:

function s:_reset_csv_delimiter(new_delimiter)
    let old_delim = get(b:, "delimiter", ",")
    let b:delimiter = a:new_delimiter
    let b:col=substitute(b:col, old_delim, b:delimiter, 'g')
    " echomsg "Resetting delimiter from '" . old_delim . "' to '" . b:delimiter . "'"
    " :InitCSV
endfunction
command -nargs=1 CsvDelimiterReset :call <SID>_reset_csv_delimiter(<q-args>)

Incidentally, the following works fine, with the highlighting correct:

if has("autocmd")
     augroup resetcsvdelimiter
         autocmd!
         autocmd BufNewFile,BufFilePre,BufRead *.tsv :call <SID>_reset_csv_delimiter('\t')
    augroup END
endif

@chrisbra
Copy link
Owner

chrisbra commented Feb 8, 2015

Hi Jeet!

On So, 08 Feb 2015, Jeet Sukumaran wrote:

Afraid not.

Given the following file (which is named "f.csv", so that filetype is csv):

a#b#c
1#2,2,2#3,4,5,6
1#2,2,2#3,4,5,6

Opening it, by default, the CSV plugin sees and highlights six columns instead of three, because the default , is the delimiter instead of '#'.

So I set b:delimiter and b:col:

let old_delim = get(b:, "delimiter", ",")
let b:delimiter = '#'
let b:col=substitute(b:col, old_delim, b:delimiter, 'g')

This works, and correctly recognizes the columns (though the highlighting is still wrong).

But for whatever reason (e.g., to update the column highlighting), if I then run

:InitCSV

Try using :InitCSV!

Best,

Christian

Alles Lebendige bildet eine Atmosphäre um sich her.
-- Goethe, Maximen und Reflektionen, Nr. 819

@jeetsukumaran
Copy link
Contributor Author

Ahhh! This works perfectly! Thank you for a great plugin!

@jeetsukumaran
Copy link
Contributor Author

BTW, I have updated the on-the-fly delimiter changing script:

function s:_reset_csv_delimiter(new_delimiter)
    :%UnArrangeColumn
    let old_delim = get(b:, "delimiter", ",")
    let b:delimiter = a:new_delimiter
    let b:col=substitute(b:col, old_delim, b:delimiter, 'g')
    " echomsg "Resetting delimiter from '" . old_delim . "' to '" . b:delimiter . "'"
    :InitCSV!
    :%ArrangeColumn
endfunction
command -nargs=1 CsvDelimiterReset :call <SID>_reset_csv_delimiter(<q-args>)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants