Skip to content

Commit

Permalink
Version 2.0: Drastic simplification.
Browse files Browse the repository at this point in the history
We now use the local textwidth to determine line length. (Use autocmds
instead of LongLine settings to set the textwidth as appropriate).

We no longer force line wrapping. We actually never did: I didn't used
to understand how format-option 't' works. Now, at least, we don't
advertize as such.

Line highlighting actually works now.
  • Loading branch information
nate committed Mar 1, 2014
1 parent 9181db4 commit 73b96c0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 149 deletions.
12 changes: 5 additions & 7 deletions README.md
@@ -1,11 +1,9 @@
# Longline
# Avoid long lines
# Longline - Avoid long lines

Longline helps you with long lines in vim.
LongLine detects lines that are too long. It helps you:

* Highlight lines that are too long.
* Jump to and fro between long lines.
* Vary the definition of "too long" by file type.
* Provide easy hooks for your `statusline` to know if long lines exist.
* Jump between overlong lines
* Highlight the overflowing parts of long lines
* Add a flag to your statusline when a line is too long

See doc/longline.txt for details.
48 changes: 7 additions & 41 deletions autoload/longline.vim
Expand Up @@ -7,45 +7,11 @@ function! s:regex_col(num)
endfunction


" The regex for the nth column of a line and everything thereafter.
" @param {number} num The column to search for.
" @return {string} The regex.
" @private
function! s:regex_all(num)
return s:regex_col(num) . '.+'
endfunction


" Find the maximum allowable line length.
" @param {string?} Filetype to check the max length of. Default &ft.
" @return {number} The maximum allowable line length.
function! longline#maxlength(...)
let l:ft = a:0 > 0 ? a:1 : &ft
let l:num = get(g:longline_exceptions, l:ft, -1)
if l:num == -1
let l:num = g:longline_maxlength
endif
return l:num <= 0 ? 0 : l:num
endfunction


" Determines whether or not text should be wrapped.
" @param {string?} Filetype to check. Default &ft.
" @return {boolean} Whether or not text should be wrapped.
function! longline#wraptext(...)
let l:ft = a:0 > 0 ? a:1 : &ft
if index(g:longline_noautotw, l:ft) > -1
return
endif
return longline#maxlength(l:ft) > 0
endfunction


" Finds out whether a long line exists in the file.
" @param {number?} Max line width. Detected from filetype if ommited.
" @return {boolean} Whether or not a too-line long exists in the file.
function! longline#exists(...)
let l:num = a:0 > 0 ? a:1 : longline#maxlength()
let l:num = get(a:000, 0, &l:textwidth)
if l:num <= 0 | return 0 | endif
return search(s:regex_col(l:num+1), 'nw') != 0
endfunction
Expand All @@ -54,23 +20,23 @@ endfunction
" Jumps the cursor to the next long line.
" @param {number?} Max line width. Detected from filetype if ommitted.
function! longline#next(...)
let l:num = a:0 > 1 ? a:1 : longline#maxlength()
let l:num = get(a:000, 0, &l:textwidth)
call search(s:regex_col(l:num).'.+$', 'w')
endfunction


" Jumps the cursor to the previous long line.
" @param {number?} Max line width. Detected from filetype if ommitted.
function! longline#prev(...)
let l:num = a:0 > 1 ? a:1 : longline#maxlength()
let l:num = get(a:000, 0, &l:textwidth)
call search(s:regex_col(l:num).'.+$', 'wb')
endfunction


" Removes the highlighting on long lines.
function! longline#hide()
if exists('b:longline_match')
call deletematch(b:longline_match)
call matchdelete(b:longline_match)
unlet b:longline_match
endif
endfunction
Expand All @@ -80,10 +46,10 @@ endfunction
" @param {number?} Max line width. Detected from filetype if ommitted.
function! longline#show(...)
call longline#hide()
let l:num = a:0 > 0 ? a:1 : longline#maxlength()
let l:num = get(a:000, 0, &l:textwidth)
if l:num > 0
let l:regex = s:regex_all(l:num)
let b:longline_match = addmatch(g:longline_matchgroup, l:regex)
let l:regex = s:regex_col(l:num).'.+'
let b:longline_match = matchadd(g:longline_matchgroup, l:regex)
endif
endfunction

Expand Down
66 changes: 25 additions & 41 deletions doc/longline.txt
Expand Up @@ -16,54 +16,43 @@ CONTENTS *longline-contents*
=============================================================================
INTRODUCTION *longline-intro*

Longline allows you to:
LongLine detects lines that are too long. It helps you:

* Specify by filetype how long is too long
* Automatically set |textwidth| correctly
* Highlight lines that are too long
* Place a long line warning in your statusline
* Jump between overlong lines
* Highlight the overflowing parts of long lines
* Add a flag to your statusline when a line is too long

The length of lines allowed is controlled by the 'textwidth' setting, which is
buffer-local. Remember that you can control line wrapping using the t format
option. See |fo-table|.

=============================================================================
CONFIGURATION *longline-config*

*g:loaded_longline*
use this to disable the plugin completely: >
use this to disable the plugin completely:
>
let g:loaded_longline = 1
<


*g:longline_autohl*
Whether or not to automatically highlight the ends of lines that are too long.
Disabled by default.

*g:longline_autotw*
Whether or not to automatically set |textwrap|. Enabled by default.

*g:longline_maxlength*
The maximum line length. Default: 80. '0' denotes no limit.

*g:longline_exceptions*
Set exceptions for filetypes that don't conform to |g:longline_maxlength|. For
example, the following allows java to be 100 characters long and turns
warnings off for markdown files: >
let g:longline_exceptions={'java': 100, 'markdown': 0}
<
A zero value means that no line is too long. A negative value means that the
default should be used.

This dict will be merged with |g:longline_default_exceptions|.

*g:longline_default_exceptions*
The default longline exceptions: >
{'text': 0, 'html': 0, 'help': 78}
< You should generally change |g:longline_exceptions| instead.

*g:longline_noautotw*
A list of filetypes which should never be affected by longline's
auto-textwrapping.

*g:longline_automap*
Makes the default key bindings. See |longline-mappings|. Disabled by default.

*g:longline_defcmds*
Whether or not to define the longline commands. Enabled by default. Use this
to prevent the commands from being defined if (for example) you want to give
the commands different names.

*g:longline_matchgroup*
The matchgroup which matches the overflowing parts of long lines. It is set to
'Error' by default. See |group-name|. For angrier (red background) long
lines, try setting this parameter to 'ErrorMsg'.


=============================================================================
COMMANDS *longline-commands*
Expand Down Expand Up @@ -93,9 +82,8 @@ You are encouraged to write your own statusline function using
|longline#exists()| if you prefer different formatting.

longline#status#flag() *longline#status#flag()*
An alias for |longline#status#warning()|. If any other statusline
states are added, longline#status#flag() will return the first
non-empty flag in decreasing order of severity.
Returns the most severe active flag. Currently, this is a future-proof
alias for |longline#status#warning()|.

longline#status#warning() *longline#status#warning()*
[…] if the file has lines that are too long.
Expand All @@ -112,17 +100,13 @@ FUNCTIONS *longline-extras*
*longline#exists()*
This function returns true if a long line exists in the file. It can be used
in various scripts or to make your own flavor of statusline flag. For example,
to emulate |longline#statusline()|, do >
emulate |longline#status#warning()|, do
>
set statusline+=%#WarningMsg#
set statusline+=%{longline#exists()?'[…]':''}
set statusline+=%*
<

*longline#maxlength*
Your scripts may call this function to determine the longest allowable line
for a filetype. You may pass a filetype as an argument. If you don't, the
current filetype will be used.

=============================================================================
MAPPINGS *longline-mappings*

Expand Down
77 changes: 17 additions & 60 deletions plugin/longline.vim
@@ -1,6 +1,6 @@
" longline.vim - Avoid long lines.
" Author: Nate Soares <http://so8r.es>
" Version: 1.1.1
" Version: 2.0.0
" License: The same as vim itself. (See |license|)
" GetLatestVimScripts: 4246 1 :AutoInstall: terminus.zip

Expand All @@ -10,46 +10,21 @@ endif
let g:loaded_longline = 1


" Whether or not to automatically adjust the shiftwidth.
if !exists('g:longline_autotw')
let g:longline_autotw = 1
endif


" Whether or not to automatically highlight long lines.
if !exists('g:longline_autohl')
let g:longline_autohl = 0
endif


" What to match long lines with.
" Which highlight group to match long lines with.
if !exists('g:longline_matchgroup')
let g:longline_matchgroup = 'ErrorMsg'
endif


" How long is 'too long'.
if !exists('g:longline_maxlength')
let g:longline_maxlength = 80
endif


" Filetypes that don't conform to the default 'too long' rule.
" Will be extended with defaults when the script is autoloaded.
if !exists('g:longline_default_exceptions')
let g:longline_default_exceptions = {'text': 0, 'html': 0, 'help': 78}
endif


" Filetypes that don't conform to the default 'too long' rule.
if !exists('g:longline_exceptions')
let g:longline_exceptions = {}
" Whether or not to define the LongLine commands.
if !exists('g:longline_defcmds')
let g:longline_defcmds = 1
endif


" A list of filetypes that should never have 'textwrap' set by longline.
if !exists('g:longline_noautotw')
let g:longline_noautotw = []
" Whether or not to automatically highlight long lines.
if !exists('g:longline_autohl')
let g:longline_autohl = 0
endif


Expand All @@ -59,39 +34,21 @@ if !exists('g:longline_automap')
endif


" Extend the exceptions with the default exceptions.
call extend(g:longline_exceptions, g:longline_default_exceptions, 'keep')


" Commands:
command! LongLineHide call longline#hide()
command! LongLineShow call longline#show()
command! LongLineToggle call longline#toggle()
command! LongLineNext call longline#next()
command! LongLinePrev call longline#prev()


" Clear existing longline autocmds
augroup longline
autocmd!
augroup end


" Automatically wrap text.
if g:longline_autotw
augroup longline
autocmd FileType *
\ if longline#wraptext()
\| let &l:tw=longline#maxlength()
\| endif
augroup end
if g:longline_defcmds
command! LongLineHide call longline#hide()
command! LongLineShow call longline#show()
command! LongLineToggle call longline#toggle()
command! LongLineNext call longline#next()
command! LongLinePrev call longline#prev()
endif


" Automatically highlight long lines.
" Clear existing longline autocmds
if g:longline_autohl
augroup longline
autocmd BufEnter * LongLineShow
autocmd!
autocmd BufEnter * call longline#show()
augroup end
endif

Expand Down

0 comments on commit 73b96c0

Please sign in to comment.