Advertisement
devinteske

vimcatn

Nov 7th, 2018
909
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.58 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##*/}.XXXXXXXX" )
  14.         trap 'rm -f "$tmpfile"' EXIT
  15.         eval 'cat > "$tmpfile" || exit'
  16.         [ $# -gt 0 ] && 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 -e ''
  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:           Tue, 6 Nov 2018 20:21:25 -0800
  35. " Version:        1.0
  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-2018, 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. let v:statusmsg = 'xxx'
  70.  
  71. let s:type = 'cterm'
  72. if &t_Co == 0
  73.  let s:type = 'term'
  74. endif
  75.  
  76. " Converts info for a highlight group to a string of ANSI color escapes  {{{1
  77. function! s:GroupToAnsi(groupnum)
  78.  if ! exists("s:ansicache")
  79.    let s:ansicache = {}
  80.  endif
  81.  
  82.  let groupnum = a:groupnum
  83.  
  84.  if groupnum == 0
  85.    let groupnum = hlID('Normal')
  86.  endif
  87.  
  88.  if has_key(s:ansicache, groupnum)
  89.    return s:ansicache[groupnum]
  90.  endif
  91.  
  92.  let fg = synIDattr(groupnum, 'fg', s:type)
  93.  let bg = synIDattr(groupnum, 'bg', s:type)
  94.  let rv = synIDattr(groupnum, 'reverse', s:type)
  95.  let bd = synIDattr(groupnum, 'bold', s:type)
  96.  
  97.  " FIXME other attributes?
  98.  
  99.  if rv == "" || rv == -1
  100.    let rv = 0
  101.  endif
  102.  
  103.  if bd == "" || bd == -1
  104.    let bd = 0
  105.  endif
  106.  
  107.  if rv
  108.    let temp = bg
  109.    let bg = fg
  110.    let fg = temp
  111.  endif
  112.  
  113.  if fg >= 8 && fg < 16
  114.    let fg -= 8
  115.    let bd = 1
  116.  endif
  117.  
  118.  if fg == "" || fg == -1
  119.    unlet fg
  120.  endif
  121.  
  122.  if !exists('fg') && !groupnum == hlID('Normal')
  123.    let fg = synIDattr(hlID('Normal'), 'fg', s:type)
  124.    if fg == "" || fg == -1
  125.      unlet fg
  126.    endif
  127.  endif
  128.  
  129.  if bg == "" || bg == -1
  130.    unlet bg
  131.  endif
  132.  
  133.  if !exists('bg') && !groupnum == hlID('Normal')
  134.    let bg = synIDattr(hlID('Normal'), 'bg', s:type)
  135.    if bg == "" || bg == -1
  136.      unlet bg
  137.    endif
  138.  endif
  139.  
  140.  let retv = "\<Esc>[0"
  141.  
  142.  if bd
  143.    let retv .= ";1"
  144.  endif
  145.  
  146.  if exists('fg') && fg < 8
  147.    let retv .= ";3" . fg
  148.  elseif exists('fg')
  149.    let retv .= ";38;5;" . fg
  150.  endif
  151.  
  152.  if exists('bg') && bg < 8
  153.    let retv .= ";4" . bg
  154.  elseif exists('bg')
  155.    let retv .= ";48;5;" . bg
  156.  endif
  157.  
  158.  let retv .= "m"
  159.  
  160.  let s:ansicache[groupnum] = retv
  161.  
  162.  return retv
  163. endfunction
  164.  
  165. function! AnsiHighlight()
  166.  let retv = []
  167.  
  168.  let LineNr = hlID('LineNr')
  169.  let llen = strlen(line('$'))
  170.  for lnum in range(1, line('$') + $line_offset)
  171.    let last = hlID('Normal')
  172.    let output = "\<Esc>[K" " Clear to right
  173.  
  174.    " Hopefully fix highlighting sync issues
  175.    exe "norm! " . lnum . "G$"
  176.  
  177.    let line = getline(lnum)
  178.    let output .= s:GroupToAnsi(LineNr) . printf("%*u ", llen, lnum)
  179.    let output .= s:GroupToAnsi(last)
  180.  
  181.    for cnum in range(1, col('.'))
  182.      if synIDtrans(synID(lnum, cnum, 1)) != last
  183.        let last = synIDtrans(synID(lnum, cnum, 1))
  184.        let output .= s:GroupToAnsi(last)
  185.      endif
  186.  
  187.      let output .= matchstr(line, '\%(\zs.\)\{'.cnum.'}')
  188.      "let line = substitute(line, '.', '', '')
  189.      "let line = matchstr(line, '^\@<!.*')
  190.    endfor
  191.  
  192.    let retv += ["\r" . output]
  193.  endfor
  194.  
  195.  " Reset the colors to default after displaying the file
  196.  let retv[-1] .= "\<Esc>[0m"
  197.  
  198.  echo ""
  199.  return writefile([""] + retv, '/dev/stdout', 'b')
  200. endfunction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement