mirror of
https://github.com/junegunn/vim-easy-align.git
synced 2025-11-10 10:53:49 -05:00
Improve message output
- Error message highlighting - Wipe previous message if needed - No line wrap on narrow terminal
This commit is contained in:
@@ -88,14 +88,33 @@ function! s:ignored_syntax()
|
|||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
let s:prev_echon_len = 0
|
||||||
|
function! s:echon_(msg)
|
||||||
|
" http://vim.wikia.com/wiki/How_to_print_full_screen_width_messages
|
||||||
|
let xy = [&ruler, &showcmd]
|
||||||
|
set noruler noshowcmd
|
||||||
|
let winlen = winwidth(winnr()) - 2
|
||||||
|
let msg = len(a:msg) > winlen ? (a:msg[0 : winlen - 3] . '..') : a:msg
|
||||||
|
let len = len(msg)
|
||||||
|
if len < s:prev_echon_len
|
||||||
|
echon "\r". repeat(' ', min([winlen, s:prev_echon_len]))
|
||||||
|
endif
|
||||||
|
echon "\r". msg
|
||||||
|
let s:prev_echon_len = len
|
||||||
|
let [&ruler, &showcmd] = xy
|
||||||
|
endfunction
|
||||||
|
|
||||||
function! s:echon(l, n, d, o)
|
function! s:echon(l, n, d, o)
|
||||||
echon "\r"
|
let msg = "EasyAlign". s:mode_labels[a:l] ." (" .a:n.a:d. ")"
|
||||||
echon "\rEasyAlign". s:mode_labels[a:l] ." (" .a:n.a:d. ")"
|
\ . (empty(a:o) ? '' : ' '.string(a:o))
|
||||||
\ . (empty(a:o) ? '' : ' '.string(a:o))
|
call s:echon_(msg)
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:exit(msg)
|
function! s:exit(msg)
|
||||||
echon "\r". a:msg
|
call s:echon_('')
|
||||||
|
echohl ErrorMsg
|
||||||
|
call s:echon_(a:msg)
|
||||||
|
echohl None
|
||||||
throw 'exit'
|
throw 'exit'
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
@@ -570,9 +589,11 @@ function! s:interactive(modes, vis)
|
|||||||
silent! call remove(opts, 'm')
|
silent! call remove(opts, 'm')
|
||||||
endif
|
endif
|
||||||
elseif ch == "\<C-_>" || ch == "\<C-X>"
|
elseif ch == "\<C-_>" || ch == "\<C-X>"
|
||||||
let ch = s:input('Regular expression: ', '', a:vis)
|
let prompt = 'Regular expression: '
|
||||||
|
let ch = s:input(prompt, '', a:vis)
|
||||||
if !empty(ch)
|
if !empty(ch)
|
||||||
let regx = 1
|
let regx = 1
|
||||||
|
let s:prev_echon_len = len(prompt . ch)
|
||||||
break
|
break
|
||||||
endif
|
endif
|
||||||
else
|
else
|
||||||
@@ -582,6 +603,12 @@ function! s:interactive(modes, vis)
|
|||||||
return [mode, n, ch, opts, s:normalize_options(opts), regx]
|
return [mode, n, ch, opts, s:normalize_options(opts), regx]
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
function! s:test_regexp(regexp)
|
||||||
|
try | call matchlist('', a:regexp)
|
||||||
|
catch | call s:exit("Invalid regular expression: ". a:regexp)
|
||||||
|
endtry
|
||||||
|
endfunction
|
||||||
|
|
||||||
function! s:parse_args(args)
|
function! s:parse_args(args)
|
||||||
let n = ''
|
let n = ''
|
||||||
let ch = ''
|
let ch = ''
|
||||||
@@ -626,12 +653,7 @@ function! s:parse_args(args)
|
|||||||
|
|
||||||
" Found regexp
|
" Found regexp
|
||||||
if !empty(matches)
|
if !empty(matches)
|
||||||
let regexp = matches[2]
|
return [matches[1], matches[2], opts, 1]
|
||||||
" Test regexp
|
|
||||||
try | call matchlist('', regexp)
|
|
||||||
catch | call s:exit("Invalid regular expression: ". regexp)
|
|
||||||
endtry
|
|
||||||
return [matches[1], regexp, opts, 1]
|
|
||||||
else
|
else
|
||||||
let tokens = matchlist(args, '^\([1-9][0-9]*\|-[0-9]*\|\*\*\?\)\?\s*\(.\{-}\)\?$')
|
let tokens = matchlist(args, '^\([1-9][0-9]*\|-[0-9]*\|\*\*\?\)\?\s*\(.\{-}\)\?$')
|
||||||
return [tokens[1], tokens[2], opts, 0]
|
return [tokens[1], tokens[2], opts, 0]
|
||||||
@@ -639,10 +661,17 @@ function! s:parse_args(args)
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! easy_align#align(bang, expr) range
|
function! easy_align#align(bang, expr) range
|
||||||
let modes = get(g:,
|
try
|
||||||
\ (a:bang ? 'easy_align_bang_interactive_modes' : 'easy_align_interactive_modes'),
|
let modes = get(g:,
|
||||||
\ (a:bang ? ['r', 'l', 'c'] : ['l', 'r', 'c']))
|
\ (a:bang ? 'easy_align_bang_interactive_modes' : 'easy_align_interactive_modes'),
|
||||||
let mode = modes[0]
|
\ (a:bang ? ['r', 'l', 'c'] : ['l', 'r', 'c']))
|
||||||
|
call s:align(modes, a:firstline, a:lastline, a:expr)
|
||||||
|
catch 'exit'
|
||||||
|
endtry
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:align(modes, first_line, last_line, expr)
|
||||||
|
let mode = a:modes[0]
|
||||||
let recur = 0
|
let recur = 0
|
||||||
let n = ''
|
let n = ''
|
||||||
let ch = ''
|
let ch = ''
|
||||||
@@ -651,31 +680,26 @@ function! easy_align#align(bang, expr) range
|
|||||||
let iopts = {}
|
let iopts = {}
|
||||||
let regexp = 0
|
let regexp = 0
|
||||||
" Heuristically determine if the user was in visual mode
|
" Heuristically determine if the user was in visual mode
|
||||||
let vis = a:firstline == line("'<") && a:lastline == line("'>")
|
let vis = a:first_line == line("'<") && a:last_line == line("'>")
|
||||||
|
|
||||||
try
|
if empty(a:expr)
|
||||||
if empty(a:expr)
|
let [mode, n, ch, ioptsr, iopts, regexp] = s:interactive(copy(a:modes), vis)
|
||||||
let [mode, n, ch, ioptsr, iopts, regexp] = s:interactive(copy(modes), vis)
|
else
|
||||||
else
|
let [n, ch, opts, regexp] = s:parse_args(a:expr)
|
||||||
let [n, ch, opts, regexp] = s:parse_args(a:expr)
|
if empty(n) && empty(ch)
|
||||||
if empty(n) && empty(ch)
|
let [mode, n, ch, ioptsr, iopts, regexp] = s:interactive(copy(a:modes), vis)
|
||||||
let [mode, n, ch, ioptsr, iopts, regexp] = s:interactive(copy(modes), vis)
|
elseif empty(ch)
|
||||||
elseif empty(ch)
|
" Try swapping n and ch
|
||||||
" Try swapping n and ch
|
let [n, ch] = ['', n]
|
||||||
let [n, ch] = ['', n]
|
|
||||||
endif
|
|
||||||
endif
|
endif
|
||||||
catch 'exit'
|
endif
|
||||||
return
|
|
||||||
endtry
|
|
||||||
|
|
||||||
if n == '*' | let [nth, recur] = [1, 1]
|
if n == '*' | let [nth, recur] = [1, 1]
|
||||||
elseif n == '**' | let [nth, recur] = [1, 2]
|
elseif n == '**' | let [nth, recur] = [1, 2]
|
||||||
elseif n == '-' | let nth = -1
|
elseif n == '-' | let nth = -1
|
||||||
elseif empty(n) | let nth = 1
|
elseif empty(n) | let nth = 1
|
||||||
elseif n == '0' || ( n != '-0' && n != string(str2nr(n)) )
|
elseif n == '0' || ( n != '-0' && n != string(str2nr(n)) )
|
||||||
echon "\rInvalid field number: ". n
|
call s:exit('Invalid field number: '. n)
|
||||||
return
|
|
||||||
else
|
else
|
||||||
let nth = n
|
let nth = n
|
||||||
endif
|
endif
|
||||||
@@ -686,6 +710,7 @@ function! easy_align#align(bang, expr) range
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
if regexp
|
if regexp
|
||||||
|
call s:test_regexp(ch)
|
||||||
let dict = { 'pattern': ch }
|
let dict = { 'pattern': ch }
|
||||||
else
|
else
|
||||||
" Resolving command-line ambiguity
|
" Resolving command-line ambiguity
|
||||||
@@ -699,8 +724,7 @@ function! easy_align#align(bang, expr) range
|
|||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
if !has_key(delimiters, ch)
|
if !has_key(delimiters, ch)
|
||||||
echon "\rUnknown delimiter key: ". ch
|
call s:exit('Unknown delimiter key: '. ch)
|
||||||
return
|
|
||||||
endif
|
endif
|
||||||
let dict = delimiters[ch]
|
let dict = delimiters[ch]
|
||||||
endif
|
endif
|
||||||
@@ -719,8 +743,7 @@ function! easy_align#align(bang, expr) range
|
|||||||
let bvisual = vis && char2nr(visualmode()) == 22 " ^V
|
let bvisual = vis && char2nr(visualmode()) == 22 " ^V
|
||||||
|
|
||||||
if recur && bvisual
|
if recur && bvisual
|
||||||
echon "\rRecursive alignment is currently not supported in blockwise-visual mode"
|
call s:exit('Recursive alignment is not supported in blockwise-visual mode')
|
||||||
return
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
let aseq = get(dict, 'mode_sequence',
|
let aseq = get(dict, 'mode_sequence',
|
||||||
@@ -735,10 +758,9 @@ function! easy_align#align(bang, expr) range
|
|||||||
endif
|
endif
|
||||||
let aseq_list = type(aseq) == 1 ? split(tolower(aseq), '\s*') : map(copy(aseq), 'tolower(v:val)')
|
let aseq_list = type(aseq) == 1 ? split(tolower(aseq), '\s*') : map(copy(aseq), 'tolower(v:val)')
|
||||||
|
|
||||||
try
|
call s:do_align(
|
||||||
call s:do_align(
|
|
||||||
\ aseq_list,
|
\ aseq_list,
|
||||||
\ {}, {}, a:firstline, a:lastline,
|
\ {}, {}, a:first_line, a:last_line,
|
||||||
\ bvisual ? min([col("'<"), col("'>")]) : 1,
|
\ bvisual ? min([col("'<"), col("'>")]) : 1,
|
||||||
\ bvisual ? max([col("'<"), col("'>")]) : 0,
|
\ bvisual ? max([col("'<"), col("'>")]) : 0,
|
||||||
\ get(dict, 'pattern', ch),
|
\ get(dict, 'pattern', ch),
|
||||||
@@ -751,8 +773,6 @@ function! easy_align#align(bang, expr) range
|
|||||||
\ get(dict, 'ignore_unmatched', get(g:, 'easy_align_ignore_unmatched', 1)),
|
\ get(dict, 'ignore_unmatched', get(g:, 'easy_align_ignore_unmatched', 1)),
|
||||||
\ get(dict, 'ignore_groups', get(dict, 'ignores', s:ignored_syntax())),
|
\ get(dict, 'ignore_groups', get(dict, 'ignores', s:ignored_syntax())),
|
||||||
\ recur)
|
\ recur)
|
||||||
call s:echon(mode, n, regexp ? '/'.ch.'/' : ch, ioptsr)
|
call s:echon(mode, n, regexp ? '/'.ch.'/' : ch, ioptsr)
|
||||||
catch 'exit'
|
|
||||||
endtry
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user