Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions autoload/vimpager_utils.vim
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,34 @@ function! vimpager_utils#DoAnsiEsc()
endif
endfunction

function! vimpager_utils#StripAnsiEsc()
let pos = getpos('.')
%substitute/\v\e\[[;?]*[0-9.;]*[a-z]//egi
set nomodified
call cursor(pos)
endfunction

function! vimpager_utils#StripOverstrike()
let pos = getpos('.')
%substitute/\v.\b//egi
set nomodified
call cursor(pos)
endfunction

function! vimpager_utils#SripBlankLines()
let pos = getpos('.')
1,/[^ ]/ global /^[ ]*$/ delete
set nomodified
call cursor(pos)
endfunction

function! vimpager_utils#SqueezeBlankLines()
let pos = getpos('.')
%substitute/\v^\_s*$//
set nomodified
call cursor(pos)
endfunction

function! vimpager_utils#IsDiff()
let lnum = 1
while getline(lnum) =~ '^\s*$'
Expand Down
33 changes: 6 additions & 27 deletions vimpager
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,7 @@ main() {
# check for ANSI codes and strip if not using ansiesc
if head -100 "${tempfile:-$file}" | grep -Eq "$ANSI_RE"; then
if [ -z "$ansiesc_available" -o -n "$force_strip_ansi" ]; then
ansi_filter "${tempfile:-$file}" > "$tmp/$filename.work"
tempfile=$tmp/$filename
mv -f -- "$tempfile.work" "$tempfile"
echo 'call vimpager_utils#StripAnsiEsc()' >> "$tmp/$file_idx.vim"
else
echo 'call vimpager_utils#DoAnsiEsc()' >> "$tmp/$file_idx.vim"
set_key ansi_files "$file_idx" yes
Expand All @@ -219,12 +217,7 @@ main() {

# squeeze blank lines if option was specified, Ubuntu man with /usr/bin/pager does this
if [ "${squeeze_blank_lines:-0}" -eq 1 ]; then
sed -e '/^[ ]*$/{
N
/^[ ]*\n[ ]*$/D
}' < "${tempfile:-$file}" > "$tmp/$filename.work"
tempfile=$tmp/$filename
mv -f -- "$tempfile.work" "$tempfile"
echo 'call vimpager_utils#SqueezeBlankLines()' >> "$tmp/$file_idx.vim"
fi

# dumb man detection when the pstree heuristic fails
Expand All @@ -236,13 +229,9 @@ main() {
# if it's a man page, remove starting blank lines, or the C syntax highlighting fails
# and write out ft command for vim
if [ -n "$is_doc" ]; then
ansi_filter "${tempfile:-$file}" | overstrike_filter | awk '
BEGIN { skipblank=1 }
/^[ ]*$/ { if (!skipblank) print }
/[^ ]/ { skipblank=0; print }
' > "$tmp/$filename.work"
tempfile=$tmp/$filename
mv -f -- "$tempfile.work" "$tempfile"
echo 'call vimpager_utils#StripAnsiEsc()' >> "$tmp/$file_idx.vim"
echo 'call vimpager_utils#StripOverstrike()' >> "$tmp/$file_idx.vim"
echo 'call vimpager_utils#StripBlankLines()' >> "$tmp/$file_idx.vim"

if [ -n "$is_man" ]; then
echo 'set ft=man' >> "$tmp/$file_idx.vim"
Expand Down Expand Up @@ -886,21 +875,11 @@ win32_native() {
# this is compatible with osed
ANSI_RE='\[[;?]*[0-9.;]*[A-Za-z]'

ansi_filter() {
sed -e 's/'"$ANSI_RE"'//g' "$@"
}

# Even /bin/sed on Solaris handles UTF-8 characters correctly, so we can safely
# use sed for this.
overstrike_filter() {
sed 's/.//g' "$@"
}

fits_on_screen() {
[ $# -eq 0 ] && set -- -

# First remove overstrikes and ANSI codes with sed
ansi_filter "$@" | overstrike_filter | \
sed -e 's/'"$ANSI_RE"'//g' -e 's/.//g' "$@" | \
awk '
{
if (NR == 1) {
Expand Down