Sunday, January 22, 2017

Re: Problem setting 'fileformat'

From a3fb2ab27e796650e5bbfb54d484fa502a848419 Mon Sep 17 00:00:00 2001
From: Christian Brabandt <cb@256bit.org>
Date: Sun, 22 Jan 2017 20:40:15 +0100
Subject: [PATCH] Make readfile() aware of autocommands

Changing &ffs inside an BufReadPre autocommand
might have an unexpected result in readfile() since it would not
consider to have the 'ffs' option being altered later on.

So initialize those variables later, after those autocommands have been
called.
---
src/fileio.c | 15 ++++++++++++---
src/testdir/test_fileformat.vim | 14 ++++++++++++++
2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/src/fileio.c b/src/fileio.c
index aeb53b593..39e356f88 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -274,9 +274,9 @@ readfile(
int msg_save = msg_scroll;
linenr_T read_no_eol_lnum = 0; /* non-zero lnum when last line of
* last read was missing the eol */
- int try_mac = (vim_strchr(p_ffs, 'm') != NULL);
- int try_dos = (vim_strchr(p_ffs, 'd') != NULL);
- int try_unix = (vim_strchr(p_ffs, 'x') != NULL);
+ int try_mac;
+ int try_dos;
+ int try_unix;
int file_rewind = FALSE;
#ifdef FEAT_MBYTE
int can_retry;
@@ -738,6 +738,10 @@ readfile(
curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
curbuf->b_op_start.col = 0;

+ try_mac = (vim_strchr(p_ffs, 'm') != NULL);
+ try_dos = (vim_strchr(p_ffs, 'd') != NULL);
+ try_unix = (vim_strchr(p_ffs, 'x') != NULL);
+
#ifdef FEAT_AUTOCMD
if (!read_buffer)
{
@@ -769,6 +773,11 @@ readfile(
else
apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
FALSE, NULL, eap);
+ /* autocommands may have changed it */
+ try_mac = (vim_strchr(p_ffs, 'm') != NULL);
+ try_dos = (vim_strchr(p_ffs, 'd') != NULL);
+ try_unix = (vim_strchr(p_ffs, 'x') != NULL);
+
if (msg_scrolled == n)
msg_scroll = m;

diff --git a/src/testdir/test_fileformat.vim b/src/testdir/test_fileformat.vim
index 584f20cdf..f6fcdb281 100644
--- a/src/testdir/test_fileformat.vim
+++ b/src/testdir/test_fileformat.vim
@@ -15,3 +15,17 @@ func Test_fileformat_after_bw()
call assert_equal(test_fileformats, &fileformat)
set fileformats&
endfunc
+
+func Test_fileformat_autocommand()
+ let filecnt=[' ', 'foobar ', 'eins ', ' ', 'zwei ', 'drei', 'vier', 'fünf', '']
+ let ffs=&ffs
+ call writefile(filecnt, 'Xfile', 'b')
+ au BufReadPre Xfile set ffs=dos ff=dos
+ new Xfile
+ call assert_equal('dos', &l:ff)
+ call assert_equal('dos', &ffs)
+ " cleanup
+ let &ffs=ffs
+ au! BufReadPre Xfile
+ bw!
+endfunc
--
2.11.0

Hi Gary!

On So, 22 Jan 2017, Gary Johnson wrote:

> Some of the log files I collect contain both CR and CR-LF line
> endings. Normally on a Linux system, Vim opens those files as
> 'fileformat' "unix", which puts ^M at the end of lines ending with
> CR-LF. I would like to hide those ^M by forcing 'fileformat' to
> "dos" for those files. I think the following should work, but it
> doesn't.
>
> au BufReadPre mary* set ffs=dos ff=dos
>
> (I'm using a test file named mary.txt which contains a mixture of CR
> and CR-LF line endings.)
>
> Instead, whether I start vim with the file name on the command line,
> e.g., "vim mary.txt", or start vim with no argument and open the
> file later with ":e mary.txt", the results are the same:
> 'fileformat' is "unix" and ^M appear at the end of the CR-LF lines.
>
> It can be seen from this command,
>
> :verbose set ffs? ff?
> fileformats=dos
> Last set from ~/.vimrc
> fileformat=unix
>
> that the autocommand is being triggered, but that Vim is setting
> 'fileformat' to "unix" internally. If Vim is setting 'fileformat'
> _before_ the autocommand is triggered, it is ignoring the 'ff' in
> the autocommand. If Vim is setting 'fileformat' _after_ the
> autocommand is triggered, it is ignoring the 'ffs' in the
> autocommand. Is this a bug or my lack of understanding?
>
> I'm running version 8.0.187 on Linux. I used a ~/.vimrc containing
> only that autocommand and I moved aside my ~/.vim directory. I even
> ran vim as "vim -N --noplugin", all with the same results.

Yeah, readfile() does not expect autocommands to change those values.

Attached is a patch that fixes it.

Best,
Christian
--
Auch der reiche Autor stiehlt oft, weil er denkt, er hätt es ebensogut
erfinden können, und der andere denk auch das.
-- Jean Paul

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: