Advertisement
Guest User

vimcat

a guest
Jan 18th, 2017
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 5.38 KB | None | 0 0
  1. #!/bin/sh
  2. #!/usr/bin/env vim
  3. #! This is a shell script that executes itself as a vimscript to do its work
  4.  
  5. : if 0
  6.     : "Bourne shell code goes here, but you cannot use if-statements"
  7.     case "$0" in
  8.     /*) script="$0" ;;
  9.      *) script="$PWD/$0"
  10.     esac
  11.     export line_offset=0
  12.     [ $# -lt 1 -o "$1" = "-" ] && {
  13.         tmpfile=$( mktemp -t "${0##*/}" )
  14.         trap 'rm -f "$tmpfile"' EXIT
  15.         eval 'cat > "$tmpfile" || exit'
  16.         shift 1 # -
  17.         set -- "$tmpfile" "$@"
  18.         case "${UNAME_s:=$( uname -s )}" in
  19.         Darwin) line_offset=1 ;;
  20.         esac
  21.     }
  22.     vim -e -X -R "$@" -c "source $script" -c "visual" \
  23.         -c "bufdo call AnsiHighlight()" -c qa < /dev/tty | sed
  24.     exit
  25. : endif
  26.  
  27. """
  28. """ The remainder of this file is vimscript
  29. """
  30.  
  31. " AnsiHighlight:  Allows for marking up a file, using ANSI color escapes when
  32. "                 the syntax changes colors, for easy, faithful reproduction.
  33. " Author:         Matthew Wozniski (mjw@drexel.edu)
  34. " Date:           Wed, 18 Jan 2017 17:14:29 -0800
  35. " Version:        1.1
  36. " History:        FIXME see :help marklines-history
  37. " License:        BSD 3-Clause. Below copyright/license must be replicated.
  38.  
  39. " Copyright (c) 2008, Matthew J. Wozniski                                {{{1
  40. " Copyright (c) 2017, Devin E. Teske <dteske@FreeBSD.org>
  41. " All rights reserved.
  42. "
  43. " Redistribution and use in source and binary forms, with or without
  44. " modification, are permitted provided that the following conditions are met:
  45. "     * Redistributions of source code must retain the above copyright
  46. "       notice, this list of conditions and the following disclaimer.
  47. "     * Redistributions in binary form must reproduce the above copyright
  48. "       notice, this list of conditions and the following disclaimer in the
  49. "       documentation and/or other materials provided with the distribution.
  50. "     * The names of the contributors may not be used to endorse or promote
  51. "       products derived from this software without specific prior written
  52. "       permission.
  53. "
  54. " THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY
  55. " EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  56. " WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  57. " DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  58. " DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  59. " (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  60. " LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  61. " ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  62. " (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  63. " SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  64.  
  65. " Turn off vi-compatible mode, unless it's already off                   {{{1
  66. if &cp
  67.   set nocp
  68. endif
  69.  
  70. let s:type = 'cterm'
  71. if &t_Co == 0
  72.   let s:type = 'term'
  73. endif
  74.  
  75. " Converts info for a highlight group to a string of ANSI color escapes  {{{1
  76. function! s:GroupToAnsi(groupnum)
  77.   if ! exists("s:ansicache")
  78.     let s:ansicache = {}
  79.   endif
  80.  
  81.   let groupnum = a:groupnum
  82.  
  83.   if groupnum == 0
  84.     let groupnum = hlID('Normal')
  85.   endif
  86.  
  87.   if has_key(s:ansicache, groupnum)
  88.     return s:ansicache[groupnum]
  89.   endif
  90.  
  91.   let fg = synIDattr(groupnum, 'fg', s:type)
  92.   let bg = synIDattr(groupnum, 'bg', s:type)
  93.   let rv = synIDattr(groupnum, 'reverse', s:type)
  94.   let bd = synIDattr(groupnum, 'bold', s:type)
  95.  
  96.   " FIXME other attributes?
  97.  
  98.   if rv == "" || rv == -1
  99.     let rv = 0
  100.   endif
  101.  
  102.   if bd == "" || bd == -1
  103.     let bd = 0
  104.   endif
  105.  
  106.   if rv
  107.     let temp = bg
  108.     let bg = fg
  109.     let fg = temp
  110.   endif
  111.  
  112.   if fg >= 8 && fg < 16
  113.     let fg -= 8
  114.     let bd = 1
  115.   endif
  116.  
  117.   if fg == "" || fg == -1
  118.     unlet fg
  119.   endif
  120.  
  121.   if !exists('fg') && !groupnum == hlID('Normal')
  122.     let fg = synIDattr(hlID('Normal'), 'fg', s:type)
  123.     if fg == "" || fg == -1
  124.       unlet fg
  125.     endif
  126.   endif
  127.  
  128.   if bg == "" || bg == -1
  129.     unlet bg
  130.   endif
  131.  
  132.   if !exists('bg') && !groupnum == hlID('Normal')
  133.     let bg = synIDattr(hlID('Normal'), 'bg', s:type)
  134.     if bg == "" || bg == -1
  135.       unlet bg
  136.     endif
  137.   endif
  138.  
  139.   let retv = "\<Esc>[0"
  140.  
  141.   if bd
  142.     let retv .= ";1"
  143.   endif
  144.  
  145.   if exists('fg') && fg < 8
  146.     let retv .= ";3" . fg
  147.   elseif exists('fg')
  148.     let retv .= ";38;5;" . fg
  149.   endif
  150.  
  151.   if exists('bg') && bg < 8
  152.     let retv .= ";4" . bg
  153.   elseif exists('bg')
  154.     let retv .= ";48;5;" . bg
  155.   endif
  156.  
  157.   let retv .= "m"
  158.  
  159.   let s:ansicache[groupnum] = retv
  160.  
  161.   return retv
  162. endfunction
  163.  
  164. function! AnsiHighlight()
  165.   let retv = []
  166.  
  167.   for lnum in range(1, line('$') + $line_offset)
  168.     let last = hlID('Normal')
  169.     let output = s:GroupToAnsi(last) . "\<Esc>[K" " Clear to right
  170.  
  171.     " Hopefully fix highlighting sync issues
  172.     exe "norm! " . lnum . "G$"
  173.  
  174.     let line = getline(lnum)
  175.  
  176.     for cnum in range(1, col('.'))
  177.       if synIDtrans(synID(lnum, cnum, 1)) != last
  178.         let last = synIDtrans(synID(lnum, cnum, 1))
  179.         let output .= s:GroupToAnsi(last)
  180.       endif
  181.  
  182.       let output .= matchstr(line, '\%(\zs.\)\{'.cnum.'}')
  183.       "let line = substitute(line, '.', '', '')
  184.       "let line = matchstr(line, '^\@<!.*')
  185.     endfor
  186.  
  187.     let retv += ["\r" . output]
  188.   endfor
  189.  
  190.   " Reset the colors to default after displaying the file
  191.   let retv[-1] .= "\<Esc>[0m"
  192.  
  193.   echo ""
  194.   return writefile([""] + retv, '/dev/stdout', 'b')
  195. endfunction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement