On Fri, Nov 16, 2012 at 11:00 AM, Ben Fritz <[email protected]> wrote:

> On Friday, November 16, 2012 9:55:10 AM UTC-6, Marco wrote:
> >
> > Thanks a lot, that works. Can I automate this somehow, so that vim
> > opens <CR> (mac) files automatically with the ff=mac setting?
> >
>
> :help 'fileformats' (note the s at the end).
>
>
>
To elaborate on what Ben said, you could just make sure your 'ffs' also has
"mac" in there and this problem should go away. I have this function that I
fire off every time I edit a file. (The autocommand is right after the
endfunction.) Occasionally, I will get a file that was edited by both
developers on DOS and Unix systems and their editors will quietly leave
mixed newlines in there. I believe that Vim will open a file with mixed
newlines as Unix (at least, this is what I see).

Basically, this counts the number of ^M characters and prints a message
reporting the number of ^M characters (if any) and, if more than half the
file contains them, suggests that the file might be DOS--otherwise, Unix.
It also returns Vim script commands to fix the file format, but the
autocommand doesn't use that. I put an arbitrary line limit of 10,000 lines
so that the file opening process isn't slowed down too much by this check.

Admittedly, it's going to be have to refactored for Mac; also, that
'silent! execute' line contains a ^M (literal control-M) where there's a
newline in the paste below.

function! NewFileReport()
  if ( &binary )
    return
  endif

  let result    = ''
  let newFormat = ''

  if ( line( '$' ) > 10000 )
    let result = printf( 'File too large to check for newlines (%d
lines).', line( '$' ) )
  else
    let num         = 0
    let savedSearch = @/
    let pos         = winsaveview()

    silent! execute "g/
$/let num+= 1"

    let @/ = savedSearch
    call winrestview( pos )

    if ( num > 0 )
      let result = printf( 'Number of DOS newlines: %d (out of %d total).',
num, line( '$' ) )

      if ( &fileformat != 'unix' )
        let result .= ' File is ' . toupper( &fileformat ) . '.'
      else
        let newFormat = num > ( line( '$' ) / 2 ) ? 'DOS' : 'Unix'

        let result .= ' File is probably ' . newFormat . '.'
      endif
    endif
  endif

  let result .= &eol ? '' : ' No end-of-line.'

  if ( result != '' )
    redraw
    echo result
  endif

  let result = ''

  if ( newFormat != '' )
    let result = 'set ff=' . tolower( newFormat )
  endif

  if ( !&eol )
    let result .= "\<nl>set eol"
  endif

  return result
endfunction
au BufReadPost * call NewFileReport()

-- 
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

Reply via email to