├── debian ├── compat ├── docs ├── source │ ├── format │ └── options ├── changelog ├── rules ├── control └── copyright ├── .gitignore ├── screenshot.png ├── thisvimrc ├── runtime ├── plugin │ └── toansicolorcodes.vim ├── doc │ └── syntax.txt ├── autoload │ └── toansicolorcodes.vim └── syntax │ └── 2ansicolorcodes.vim ├── LICENSE ├── make-rgb-index.vim ├── README.md ├── Makefile └── vimcat /debian/compat: -------------------------------------------------------------------------------- 1 | 7 2 | -------------------------------------------------------------------------------- /debian/docs: -------------------------------------------------------------------------------- 1 | README.textile 2 | -------------------------------------------------------------------------------- /debian/source/format: -------------------------------------------------------------------------------- 1 | 3.0 (native) 2 | -------------------------------------------------------------------------------- /debian/source/options: -------------------------------------------------------------------------------- 1 | tar-ignore = .git 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /runtime/syntax/2ansicolorcodes_colors.vim 2 | -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ofavre/vimcat/HEAD/screenshot.png -------------------------------------------------------------------------------- /debian/changelog: -------------------------------------------------------------------------------- 1 | vimcat (1.0.0-1) unstable; urgency=low 2 | 3 | * Initial release 4 | 5 | -- Olivier Favre Tue, 03 Sep 2013 00:23:43 +0200 6 | -------------------------------------------------------------------------------- /thisvimrc: -------------------------------------------------------------------------------- 1 | let root = expand(':h') 2 | execute 'set runtimepath^=' . fnameescape(root . '/runtime') 3 | " Manually load our plugin(s) 4 | execute 'source ' . root . '/runtime/plugin/*.vim' 5 | " Under debian, /etc/vim/vimrc sources debian.vim, which resets the runtimepath 6 | " option, preventing Vim from autoloading the plugins 7 | 8 | let g:ansicolorcodes_expand_tabs=1 9 | -------------------------------------------------------------------------------- /debian/rules: -------------------------------------------------------------------------------- 1 | #!/usr/bin/make -f 2 | # -*- makefile -*- 3 | 4 | # Uncomment this to turn on verbose mode. 5 | #DH_VERBOSE=1 # local variable, will only affect dh* commands. 6 | #export DH_VERBOSE=1 # environment variable, will recursively affect all dh* subcommands. 7 | #export DH_OPTIONS=-v 8 | 9 | # Delegate to debhelper 10 | %: 11 | dh $@ 12 | 13 | .PHONY: 14 | -------------------------------------------------------------------------------- /debian/control: -------------------------------------------------------------------------------- 1 | Source: vimcat 2 | Section: universe/utils 3 | Priority: optional 4 | Maintainer: Olivier Favre 5 | Build-Depends: debhelper (>= 7.0.50) 6 | Standards-Version: 3.8.4 7 | Vcs-Git: https://github.com/ofavre/vimcat.git 8 | Vcs-browser: https://github.com/ofavre/vimcat/tree/master 9 | Homepage: https://github.com/ofavre/vimcat 10 | 11 | Package: vimcat 12 | Section: universe/utils 13 | Architecture: all 14 | Depends: vim 15 | Description: A supercat with Vim powers 16 | Uses the famous vim text editor syntax highlighting capabilities 17 | to colorize files you cat. 18 | -------------------------------------------------------------------------------- /runtime/plugin/toansicolorcodes.vim: -------------------------------------------------------------------------------- 1 | " Vim plugin for converting a syntax highlighted file to ANSI color codes. 2 | " Maintainer: Olivier Favre 3 | " Last Change: 2012 Aug 22 4 | " 5 | " The core of the code is in $VIMRUNTIME/autoload/toansicolorcodes.vim and 6 | " $VIMRUNTIME/syntax/2ansicolorcodes.vim 7 | " 8 | 9 | if exists('g:loaded_2ansicolorcodes_plugin') 10 | finish 11 | endif 12 | let g:loaded_2ansicolorcodes_plugin = 'vim7.3_v10' 13 | 14 | " 15 | " Changelog: 16 | " 7.3_v10 (this version): First version 17 | 18 | " Define the :TOansicolorcodes command when: 19 | " - 'compatible' is not set 20 | " - this plugin was not already loaded 21 | " - user commands are available. 22 | if !&cp && !exists(":TOansicolorcodes") && has("user_commands") 23 | command -range=% TOansicolorcodes :call toansicolorcodes#Convert2ANSIColorCodes(, ) 24 | endif 25 | 26 | " Make sure any patches will probably use consistent indent 27 | " vim: ts=2 sw=2 sts=2 et 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012, Olivier Favre 2 | All rights reserved. 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are met: 5 | 6 | * Redistributions of source code must retain the above copyright 7 | notice, this list of conditions and the following disclaimer. 8 | * Redistributions in binary form must reproduce the above copyright 9 | notice, this list of conditions and the following disclaimer in the 10 | documentation and/or other materials provided with the distribution. 11 | 12 | THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY 13 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 14 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 15 | DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY 16 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 17 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 18 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 19 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 20 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 21 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22 | -------------------------------------------------------------------------------- /make-rgb-index.vim: -------------------------------------------------------------------------------- 1 | " Reads $VIMRUNTIME/rgb.txt and transforms it into a dictionary inside a VimScript 2 | 3 | " Check if we're running on Vim v8.2.3562 or ulterior. 4 | " If v:colornames is available, still create the output file but leave it 5 | " free of side effect. 6 | if exists('v:colornames') 7 | 0 8 | norm! O 9 | call setline(1, '" Your Vim version ships with the v:colornames variable, this file is not used.') 10 | call setline(2, '" vim: ts=8 sw=2 sts=2 et') 11 | " Write output file 12 | w! 2ansicolorcodes_colors.vim 13 | " And quit 14 | qa! 15 | endif 16 | 17 | " Open the file to transform 18 | e $VIMRUNTIME/rgb.txt 19 | 20 | " Set up working environment 21 | set notitle noicon 22 | set nomore nowarn 23 | set report=1000000 24 | set noro 25 | set modifiable 26 | 27 | " Keep comments 28 | %s/^!/"/ 29 | " Transform color lines into dictionary assignment body 30 | %s/^\s*\(\d\+\)\s\+\(\d\+\)\s\+\(\d\+\)\s\+\([a-zA-Z0-9 ]\+\)\s*$/ \\ '\L\4\E' : '\1;\2;\3',/ 31 | " Prepend dictionary assignment begin 32 | 0 33 | norm! O 34 | call setline(1, 'let g:ansicolorcodes_colors = {') 35 | " Put all comments in the top 36 | g/^"/m0 37 | " (they were reversed in the process, reverse them back) 38 | g/^"/m0 39 | " Append dictionary assignment end 40 | $ 41 | norm o 42 | call setline(line('$'), ' \}') 43 | 44 | " Write output file 45 | w! 2ansicolorcodes_colors.vim 46 | " And quit 47 | qa! 48 | 49 | " vim: ts=8 sw=2 sts=2 et 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vimcat 2 | 3 | A supercat with Vim powers! 4 | 5 | ![Screenshot of a side by side comparison of vimcat on the left and cat on the right](screenshot.png) 6 | 7 | ## Installing 8 | 9 | Simply issue the following: 10 | 11 | ``` 12 | sudo make install 13 | ``` 14 | 15 | To uninstall, issue the following: 16 | 17 | ``` 18 | sudo make uninstall 19 | ``` 20 | 21 | Alternatively you can use the Debian packages published in the [releases section](https://github.com/ofavre/vimcat/releases). 22 | 23 | ## How does it work? 24 | 25 | Uses a headless Vim and runs a convertion VimScript to translate the syntax highlighting into ANSI color codes, and print the file to the console to get colorized output. 26 | This software consists solely of VimScript and bash. 27 | 28 | The translation program is a Vim Script inspired by the `2html` plugin (see `:help :TOhtml`). 29 | 30 | ## Options 31 | 32 | This program has many options, but you would normally use a few, if any. Use `--help` for more information. 33 | A few options permits you to issue arbitrary Vim commands. 34 | 35 | ## Performance 36 | 37 | Inspecting the syntax highlighting with `synID()` does not offer very good performance. 38 | Moreover the script needs to iterates over each and every character of the file, to finds any change in coloring, and produces the corresponding color codes if any change is detected. 39 | In 256 color modes, the parameterized strings used to output the correct color escape codes are a bit time consuming too. 40 | 41 | On a Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz, capped at that frequency, highlighting a simple C file takes 1.73 second per 1,000 lines, or 578 lines/s. 42 | -------------------------------------------------------------------------------- /debian/copyright: -------------------------------------------------------------------------------- 1 | Format-Specification: http://svn.debian.org/wsvn/dep/web/deps/dep5.mdwn?rev=135 2 | Upstream-Name: vimcat 3 | Upstream-Maintainer: Olivier Favre 4 | Upstream-Author: Olivier Favre 5 | 6 | Files: * 7 | Copyright: 2012-2013, Olivier Favre 8 | License: BSD-2-Clause 9 | 10 | Files: debian/* 11 | Copyright: 2013, Olivier Favre 12 | License: BSD-2-Clause 13 | 14 | License: BSD-2-Clause 15 | Redistribution and use in source and binary forms, with or without 16 | modification, are permitted provided that the following conditions 17 | are met: 18 | 1. Redistributions of source code must retain the above copyright 19 | notice, this list of conditions and the following disclaimer. 20 | 2. Redistributions in binary form must reproduce the above copyright 21 | notice, this list of conditions and the following disclaimer in the 22 | documentation and/or other materials provided with the distribution. 23 | . 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 25 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 26 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 27 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 28 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 29 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 30 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 31 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 32 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 34 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | PREFIX ?= /usr 2 | 3 | all: build 4 | 5 | clean: 6 | rm -f runtime/syntax/2ansicolorcodes_colors.vim 7 | 8 | build: runtime/syntax/2ansicolorcodes_colors.vim 9 | 10 | runtime/syntax/2ansicolorcodes_colors.vim: 11 | cd runtime/syntax/ && vim -u NONE -X -S ../../make-rgb-index.vim -E -n >/dev/null 12 | 13 | install: build 14 | install -d $(DESTDIR)$(PREFIX)/bin/ 15 | install -d $(DESTDIR)$(PREFIX)/share/ 16 | install -m0755 vimcat $(DESTDIR)$(PREFIX)/bin/ 17 | install -d $(DESTDIR)$(PREFIX)/share/vimcat/ 18 | install -m0644 thisvimrc $(DESTDIR)$(PREFIX)/share/vimcat/ 19 | install -m0644 make-rgb-index.vim $(DESTDIR)$(PREFIX)/share/vimcat/ 20 | install -d $(DESTDIR)$(PREFIX)/share/vimcat/runtime/ 21 | install -d $(DESTDIR)$(PREFIX)/share/vimcat/runtime/autoload/ 22 | install -m0644 runtime/autoload/toansicolorcodes.vim $(DESTDIR)$(PREFIX)/share/vimcat/runtime/autoload/ 23 | install -d $(DESTDIR)$(PREFIX)/share/vimcat/runtime/doc/ 24 | install -m0644 runtime/doc/syntax.txt $(DESTDIR)$(PREFIX)/share/vimcat/runtime/doc/ 25 | install -d $(DESTDIR)$(PREFIX)/share/vimcat/runtime/plugin/ 26 | install -m0644 runtime/plugin/toansicolorcodes.vim $(DESTDIR)$(PREFIX)/share/vimcat/runtime/plugin/ 27 | install -d $(DESTDIR)$(PREFIX)/share/vimcat/runtime/syntax/ 28 | install -m0644 runtime/syntax/2ansicolorcodes.vim $(DESTDIR)$(PREFIX)/share/vimcat/runtime/syntax/ 29 | install -m0644 runtime/syntax/2ansicolorcodes_colors.vim $(DESTDIR)$(PREFIX)/share/vimcat/runtime/syntax/ 30 | install -d $(DESTDIR)/etc/vimcat.conf.d/ 31 | 32 | uninstall: 33 | rm -f \ 34 | $(DESTDIR)$(PREFIX)/bin/vimcat \ 35 | $(DESTDIR)$(PREFIX)/share/vimcat/thisvimrc \ 36 | $(DESTDIR)$(PREFIX)/share/vimcat/make-rgb-index.vim \ 37 | $(DESTDIR)$(PREFIX)/share/vimcat/runtime/autoload/toansicolorcodes.vim \ 38 | $(DESTDIR)$(PREFIX)/share/vimcat/runtime/doc/syntax.txt \ 39 | $(DESTDIR)$(PREFIX)/share/vimcat/runtime/plugin/toansicolorcodes.vim \ 40 | $(DESTDIR)$(PREFIX)/share/vimcat/runtime/syntax/2ansicolorcodes.vim \ 41 | $(DESTDIR)$(PREFIX)/share/vimcat/runtime/syntax/2ansicolorcodes_colors.vim \ 42 | $(DESTDIR)/etc/vimcat.conf 43 | rmdir --ignore-fail-on-non-empty \ 44 | $(DESTDIR)$(PREFIX)/share/vimcat/runtime/autoload/ \ 45 | $(DESTDIR)$(PREFIX)/share/vimcat/runtime/doc/ \ 46 | $(DESTDIR)$(PREFIX)/share/vimcat/runtime/plugin/ \ 47 | $(DESTDIR)$(PREFIX)/share/vimcat/runtime/syntax/ \ 48 | $(DESTDIR)$(PREFIX)/share/vimcat/runtime/ \ 49 | $(DESTDIR)$(PREFIX)/share/vimcat/ \ 50 | $(DESTDIR)/etc/vimcat.conf.d/ 51 | 52 | .PHONY: all help clean build install uninstall 53 | -------------------------------------------------------------------------------- /runtime/doc/syntax.txt: -------------------------------------------------------------------------------- 1 | *syntax.txt* For Vim version 7.3. Last change: 2011 May 06 2 | 3 | 4 | VIM REFERENCE MANUAL by Bram Moolenaar 5 | 6 | 7 | Syntax highlighting *syntax* *syntax-highlighting* *coloring* 8 | 9 | Syntax highlighting enables Vim to show parts of the text in another font or 10 | color. Those parts can be specific keywords or text matching a pattern. Vim 11 | doesn't parse the whole file (to keep it fast), so the highlighting has its 12 | limitations. Lexical highlighting might be a better name, but since everybody 13 | calls it syntax highlighting we'll stick with that. 14 | 15 | Vim supports syntax highlighting on all terminals. But since most ordinary 16 | terminals have very limited highlighting possibilities, it works best in the 17 | GUI version, gvim. 18 | 19 | Note: This is a stripped version of the regular |syntax.txt| file, it only 20 | contains the documentation of the *2ansicolorcodes.vim* plugin. 21 | 22 | 4. Syntax file remarks |:syn-file-remarks| 23 | 24 | Syntax highlighting is not available when the |+syntax| feature has been 25 | disabled at compile time. 26 | 27 | ============================================================================== 28 | 4. Syntax file remarks *:syn-file-remarks* 29 | 30 | 2HTML *2ansicolorcodes.vim* *convert-to-HTML* 31 | 32 | This is not a syntax file itself, but a script that converts the current 33 | window into HTML. Vim opens a new window in which it builds the HTML file. 34 | 35 | You are not supposed to set the 'filetype' or 'syntax' option to "2ansicolorcodes"! 36 | Source the script to convert the current file: > 37 | 38 | :runtime! syntax/2ansicolorcodes.vim 39 | < 40 | *:TOansicolorcodes* 41 | Or use the ":TOansicolorcodes" user command. It is defined in a standard plugin. 42 | ":TOansicolorcodes" also works with a range and in a Visual area: > 43 | 44 | :10,40TOansicolorcodes 45 | 46 | Warning: This can be slow! The script must process every character of every 47 | line. Because it can take a long time, by default a progress bar is displayed 48 | in the statusline for each major step in the conversion process. If you don't 49 | like seeing this progress bar, you can disable it and get a very minor speed 50 | improvement with: > 51 | 52 | let g:ansicolorcodes_no_progress = 1 53 | 54 | ":TOansicolorcodes" has another special feature: if the window is in diff mode, it will 55 | generate HTML that shows all the related windows. This can be disabled by 56 | setting the g:ansicolorcodes_diff_one_file variable: > 57 | 58 | let g:ansicolorcodes_diff_one_file = 1 59 | 60 | After you save the resulting file, you can view it with any browser. The 61 | colors should be exactly the same as you see them in Vim. 62 | 63 | To restrict the conversion to a range of lines, use a range with the |:TOansicolorcodes| 64 | command, or set "g:ansicolorcodes_start_line" and "g:ansicolorcodes_end_line" to the first and 65 | last line to be converted. Example, using the last set Visual area: > 66 | 67 | :let g:ansicolorcodes_start_line = line("'<") 68 | :let g:ansicolorcodes_end_line = line("'>") 69 | 70 | The lines are numbered according to 'number' option and the Number 71 | highlighting. You can force lines to be numbered in the HTML output by 72 | setting "html_number_lines" to non-zero value: > 73 | :let g:ansicolorcodes_number_lines = 1 74 | Force to omit the line numbers by using a zero value: > 75 | :let g:ansicolorcodes_number_lines = 0 76 | Go back to the default to use 'number' by deleting the variable: > 77 | :unlet g:ansicolorcodes_number_lines 78 | 79 | By default, valid HTML 4.01 using cascading style sheets (CSS1) is generated. 80 | If you need to generate markup for really old browsers or some other user 81 | agent that lacks basic CSS support, use: > 82 | :let g:ansicolorcodes_use_css = 0 83 | 84 | Concealed text is removed from the HTML and replaced with the appropriate 85 | character from |:syn-cchar| or 'listchars' depending on the current value of 86 | 'conceallevel'. If you always want to display all text in your document, 87 | either set 'conceallevel' to zero before invoking 2ansicolorcodes, or use: > 88 | :let g:ansicolorcodes_ignore_conceal = 1 89 | 90 | Similarly, closed folds are put in the HTML as they are displayed. If you 91 | don't want this, use the |zR| command before invoking 2ansicolorcodes, or use: > 92 | :let g:ansicolorcodes_ignore_folding = 1 93 | 94 | You may want to generate HTML that includes all the data within the folds, and 95 | allow the user to view the folded data similar to how they would in Vim. To 96 | generate this dynamic fold information, use: > 97 | :let g:ansicolorcodes_dynamic_folds = 1 98 | 99 | Using html_dynamic_folds will imply html_use_css, because it would be far too 100 | difficult to do it for old browsers. However, html_ignore_folding overrides 101 | html_dynamic_folds. 102 | 103 | Using html_dynamic_folds will default to generating a foldcolumn in the html 104 | similar to Vim's foldcolumn, that will use javascript to open and close the 105 | folds in the HTML document. The width of this foldcolumn starts at the current 106 | setting of |'foldcolumn'| but grows to fit the greatest foldlevel in your 107 | document. If you do not want to show a foldcolumn at all, use: > 108 | :let g:ansicolorcodes_no_foldcolumn = 1 109 | 110 | Using this option, there will be no foldcolumn available to open the folds in 111 | the HTML. For this reason, another option is provided: html_hover_unfold. 112 | Enabling this option will use CSS 2.0 to allow a user to open a fold by 113 | hovering the mouse pointer over it. Note that old browsers (notably Internet 114 | Explorer 6) will not support this feature. Browser-specific markup for IE6 is 115 | included to fall back to the normal CSS1 code so that the folds show up 116 | correctly for this browser, but they will not be openable without a 117 | foldcolumn. Note that using html_hover_unfold will allow modern browsers with 118 | disabled javascript to view closed folds. To use this option, use: > 119 | :let g:ansicolorcodes_hover_unfold = 1 120 | 121 | Setting html_no_foldcolumn with html_dynamic_folds will automatically set 122 | html_hover_unfold, because otherwise the folds wouldn't be dynamic. 123 | 124 | By default "
" and "
" are used around the text. When 'wrap' is set 125 | in the window being converted, the CSS 2.0 "white-space:pre-wrap" value is 126 | used to wrap the text. You can explicitly enable the wrapping with: > 127 | :let g:ansicolorcodes_pre_wrap = 1 128 | or disable with > 129 | :let g:ansicolorcodes_pre_wrap = 0 130 | This generates HTML that looks very close to the Vim window, but unfortunately 131 | there can be minor differences such as the lack of a 'showbreak' option in in 132 | the HTML, or where line breaks can occur. 133 | 134 | Another way to obtain text wrapping in the HTML, at the risk of making some 135 | things look even more different, is to use: > 136 | :let g:ansicolorcodes_no_pre = 1 137 | This will use
at the end of each line and use " " for repeated 138 | spaces. Doing it this way is more compatible with old browsers, but modern 139 | browsers support the "white-space" method. 140 | 141 | If you do stick with the default "
" tags,  characters in the text
142 | are included in the generated output if they will have no effect on the
143 | appearance of the text and it looks like they are in the document
144 | intentionally. This allows for the HTML output to be copied and pasted from a
145 | browser without losing the actual whitespace used in the document.
146 | 
147 | Specifically,  characters will be included if the 'tabstop' option is set
148 | to the default of 8, 'expandtab' is not set, and if neither the foldcolumn nor
149 | the line numbers are included in the HTML output (see options above). When any
150 | of these conditions are not met, any  characters in the text are expanded
151 | to the appropriate number of spaces in the HTML output.
152 | 
153 | When "
" is included, you can force |:TOansicolorcodes| to keep the tabs even if the
154 | other conditions are not met with: >
155 |    :let g:ansicolorcodes_expand_tabs = 0
156 | Note that this can easily break text alignment and indentation in the HTML.
157 | 
158 | Force tabs to be expanded even when they would be kept using: >
159 |    :let g:ansicolorcodes_expand_tabs = 1
160 | 
161 | For diff mode on a single file (with g:ansicolorcodes_diff_one_file) a sequence of more
162 | than 3 filler lines is displayed as three lines with the middle line
163 | mentioning the total number of inserted lines.  If you prefer to see all the
164 | inserted lines as with the side-by-side diff, use: >
165 |     :let g:ansicolorcodes_whole_filler = 1
166 | And to go back to displaying up to three lines again: >
167 |     :unlet g:ansicolorcodes_whole_filler
168 | 
169 | For most buffers, TOansicolorcodes uses the current value of 'fileencoding' if set, or
170 | 'encoding' if not, to determine the charset and 'fileencoding' of the HTML
171 | file. 'encoding' is always used for certain 'buftype' values. In general, this
172 | works for the encodings mentioned specifically by name in |encoding-names|,
173 | but TOansicolorcodes will only automatically use those encodings which are widely
174 | supported. However, you can override this to support specific encodings that
175 | may not be automatically detected by default.
176 | 
177 | To overrule all automatic charset detection, set g:ansicolorcodes_use_encoding to the
178 | name of the charset to be used. TOansicolorcodes will try to determine the appropriate
179 | 'fileencoding' setting from the charset, but you may need to set it manually
180 | if TOansicolorcodes cannot determine the encoding. It is recommended to set this
181 | variable to something widely supported, like UTF-8, for anything you will be
182 | hosting on a webserver: >
183 |    :let g:ansicolorcodes_use_encoding = "UTF-8"
184 | You can also use this option to omit the line that specifies the charset
185 | entirely, by setting g:ansicolorcodes_use_encoding to an empty string: >
186 |    :let g:ansicolorcodes_use_encoding = ""
187 | To go back to the automatic mechanism, delete the g:ansicolorcodes_use_encoding
188 | variable: >
189 |    :unlet g:ansicolorcodes_use_encoding
190 | 
191 | If you specify a charset with g:ansicolorcodes_use_encoding for which TOansicolorcodes cannot
192 | automatically detect the corresponding 'fileencoding' setting, you can use
193 | g:ansicolorcodes_encoding_override to allow TOansicolorcodes to detect the correct encoding.
194 | This is a dictionary of charset-encoding pairs that will replace existing
195 | pairs automatically detected by TOansicolorcodes, or supplement with new pairs. For
196 | example, to allow TOansicolorcodes to detect the HTML charset "windows-1252" properly as
197 | the encoding "8bit-cp1252", use: >
198 |    :let g:ansicolorcodes_encoding_override = {'windows-1252': '8bit-cp1252'}
199 | <
200 | The g:ansicolorcodes_charset_override is similar, it allows TOansicolorcodes to detect the HTML
201 | charset for any 'fileencoding' or 'encoding' which is not detected
202 | automatically. You can also use it to override specific existing
203 | encoding-charset pairs. For example, TOansicolorcodes will by default use UTF-8 for all
204 | Unicode/UCS encodings. To use UTF-16 and UTF-32 instead, use: >
205 |    :let g:ansicolorcodes_charset_override = {'ucs-4': 'UTF-32', 'utf-16': 'UTF-16'}
206 | 
207 | Note that documents encoded in either UTF-32 or UTF-16 have known
208 | compatibility problems with at least one major browser.
209 | 
210 | 					    *convert-to-XML* *convert-to-XHTML*
211 | If you do not like plain HTML, an alternative is to have the script generate
212 | XHTML (XML compliant HTML). To do this set the "html_use_xhtml" variable: >
213 |     :let g:ansicolorcodes_use_xhtml = 1
214 | 
215 | Any of the on/off options listed above can be enabled or disabled by setting
216 | them explicitly to the desired value, or restored to their default by removing
217 | the variable using |:unlet|.
218 | 
219 | Remarks:
220 | - Some truly ancient browsers may not show the background colors.
221 | - From most browsers you can also print the file (in color)!
222 | - This version of TOansicolorcodes may work with older versions of Vim, but some
223 |   features such as conceal support will not function, and the colors may be
224 |   incorrect for an old Vim without GUI support compiled in.
225 | 
226 | Here is an example how to run the script over all .c and .h files from a
227 | Unix shell: >
228 |    for f in *.[ch]; do gvim -f +"syn on" +"run! syntax/2ansicolorcodes.vim" +"wq" +"q" $f; done
229 | <
230 | 
231 |  vim:tw=78:sw=4:ts=8:ft=help:norl:
232 | 


--------------------------------------------------------------------------------
/vimcat:
--------------------------------------------------------------------------------
  1 | #!/bin/bash
  2 | 
  3 | #
  4 | # Default values
  5 | #
  6 | 
  7 | default_number_lines=""
  8 | default_colors=""
  9 | auto_colors="$(tput colors)"
 10 | default_foldmethod=""
 11 | default_foldlevel=""
 12 | default_foldminlines=""
 13 | default_tabstop=""
 14 | default_shiftwidth=""
 15 | default_softtabstop=""
 16 | default_foldmarker=""
 17 | 
 18 | 
 19 | 
 20 | #
 21 | # Load configuration files
 22 | #
 23 | 
 24 | if [ -r /etc/vimcat.conf ]; then
 25 |   . /etc/vimcat.conf
 26 | fi
 27 | if [ -d /etc/vimcat.conf.d/ ]; then
 28 |   OLDIFS="$IFS"
 29 |   IFS=
 30 |   for f in `find /etc/vimcat.conf.d/ -type f | sort`; do
 31 |     . "$f"
 32 |   done
 33 |   IFS="$OLDIFS"
 34 | fi
 35 | if [ -r ~/.vimcatrc ]; then
 36 |   . ~/.vimcatrc
 37 | fi
 38 | if [ -r ./.vimcatrc ]; then
 39 |   . ./.vimcatrc
 40 | fi
 41 | 
 42 | 
 43 | 
 44 | #
 45 | # Script functions
 46 | #
 47 | 
 48 | print_usage() {
 49 |   cat < "$tmpfile"
292 |     process_file "$tmpfile"
293 |     return "$?"
294 |   fi
295 | 
296 |   if ! [ -r "$1" ]; then
297 |     echo "Cannot read file \"$1\"!" >&2
298 |     return 1
299 |   fi
300 | 
301 |   init_tempfile
302 |   prepare_opt_cmd
303 | 
304 |   # Use vim-fu to colorize the file:
305 |   # * -X: Do not connect to the X server.
306 |   #       Speeds up launch, disables terminal tab name change.
307 |   #       -> All good, we don't need that stuff!
308 |   # * -S sourced-file:
309 |   #       A script file to be sourced.
310 |   #       This is different from -u vimrc, in that we do not replace the
311 |   #       default vimrcs.
312 |   #       Note: It is very important to keep this option *before* -E,
313 |   #             as -s after it doesn't have the same meaning and doesn't take
314 |   #             any argument!
315 |   # * -E: Starts Vim in Ex mode. It will await command, but won't display the
316 |   #       files on screen like the visual mode does.
317 |   #       This permits to bypass the "Vim: Warning: Output is not to a
318 |   #       terminal" message and its associated pause.
319 |   # * -n: Do not use swapfiles.
320 |   #       We won't modify the loaded file, so that doesn't hurt.
321 |   #       Moreover, it won't display a message to the user in case we are
322 |   #       processing a file already opened in another vim.
323 |   #       A -R trick would display a "W10: Warning: Changing a readonly file"
324 |   #       warning, and add a little 1s pause. Adding -c "set noreadonly", helps
325 |   #       a bit in such case.
326 |   #       The tempfile we are writing to doesn't care about recovery too, so
327 |   #       we can disable swapfiles completely.
328 |   # * -c cmd:
329 |   #       Executes the command as if written by the user.
330 |   #       Note: Vim permits only 10 of such arguments!
331 |   # * --: Cleanly separate the arguments from the files to be processed.
332 |   #       We can't make any assumption on the file names. They can even start
333 |   #       with a dash, in which case they must not be mistaken for arguments.
334 |   # * >/dev/null:
335 |   #       Along with the Ex mode, this prevents the screen from being cleared
336 |   #       and restored a short while after. None of Vim usage will be visible
337 |   #       to the user, which is good.
338 |   vim -X -S /usr/share/vimcat/thisvimrc -E -n "${opt_number_lines[@]}" "${opt_colors[@]}" "${opt_folding[@]}" "${opt_cmd[@]}" -c "TOansicolorcodes" -c "w! $tmpfile" -c "qa!" -- "$1" >/dev/null
339 | 
340 |   cat "$tmpfile"
341 | }
342 | 
343 | #
344 | # Entry point
345 | #
346 | 
347 | # Process each file from the command line
348 | processed_files=0
349 | while [ "$#" -gt 0 ]; do
350 |   if [ "$no_more_args" == "1" ]; then
351 |     processed_files=1
352 |     process_file "$1"
353 |   else
354 |     case "$1" in
355 |       -h|--help)
356 |         print_usage
357 |         exit 0
358 |         ;;
359 |       --)
360 |         no_more_args=1
361 |         ;;
362 |       -n|--line-number|--nl)
363 |         set_opt_number_lines 1
364 |         ;;
365 |       +n|--no-line-number|--no-nl)
366 |         set_opt_number_lines 0
367 |         ;;
368 |       -n*)
369 |         set_opt_number_lines "${1#-n}"
370 |         ;;
371 |       --nl=*|--line-number=*)
372 |         set_opt_number_lines "${1#*=}"
373 |         ;;
374 |       -c)
375 |         set_opt_colors "$auto_colors"
376 |         ;;
377 |       -c*)
378 |         set_opt_colors "${1#-c}"
379 |         ;;
380 |       --colors=*)
381 |         set_opt_colors "${1#*=}"
382 |         ;;
383 |       +c|--no-colors)
384 |         set_opt_colors 0
385 |         ;;
386 |       --set-*)
387 |         set_opt_cmd "set ${1#--set-}"
388 |         ;;
389 |       --cmd=*)
390 |         set_opt_cmd "${1#*=}"
391 |         ;;
392 |       --cmd-reset)
393 |         set_opt_cmd_reset
394 |         ;;
395 |       --fdm=*|--foldmethod=*)
396 |         set_opt_foldmethod "${1#*=}"
397 |         ;;
398 |       --fdl=*|--foldlevel=*)
399 |         set_opt_foldlevel "${1#*=}"
400 |         ;;
401 |       --fml=*|--foldminlines=*)
402 |         set_opt_foldminlines "${1#*=}"
403 |         ;;
404 |       --ts=*|--tabstop=*)
405 |         set_opt_tabstop "${1#*=}"
406 |         ;;
407 |       --sw=*|--shiftwidth=*)
408 |         set_opt_shiftwidth "${1#*=}"
409 |         ;;
410 |       --sts=*|--softtabstop=*)
411 |         set_opt_softtabstop "${1#*=}"
412 |         ;;
413 |       --fmr=*|--foldmarker=*)
414 |         set_opt_foldmarker "${1#*=}"
415 |         ;;
416 |       -f=*)
417 |         processed_files=1
418 |         process_file "${1#*=}"
419 | 	;;
420 |       *)
421 |         processed_files=1
422 |         process_file "$1"
423 |     esac
424 |   fi
425 | 
426 |   shift
427 | done
428 | 
429 | if [ "$processed_files" -eq 0 ]; then
430 |   process_file -
431 | fi
432 | 
433 | teardown
434 | 


--------------------------------------------------------------------------------
/runtime/autoload/toansicolorcodes.vim:
--------------------------------------------------------------------------------
  1 | " Vim autoload file for the toansicolorcodes plugin.
  2 | " Maintainer: Olivier Favre 
  3 | " Last Change: 2012 Aug 22
  4 | 
  5 | " this file uses line continuations
  6 | let s:cpo_sav = &cpo
  7 | set cpo-=C
  8 | 
  9 | " Automatically find charsets from all encodings supported natively by Vim. With
 10 | " the 8bit- and 2byte- prefixes, Vim can actually support more encodings than
 11 | " this. Let the user specify these however since they won't be supported on
 12 | " every system.
 13 | "
 14 | " Note, not all of Vim's supported encodings have a charset to use.
 15 | "
 16 | " Names in this list are from:
 17 | "   http://www.iana.org/assignments/character-sets
 18 | " g:toansicolorcodes#encoding_to_charset: {{{
 19 | let g:toansicolorcodes#encoding_to_charset = {
 20 |       \ 'latin1' : 'ISO-8859-1',
 21 |       \ 'iso-8859-2' : 'ISO-8859-2',
 22 |       \ 'iso-8859-3' : 'ISO-8859-3',
 23 |       \ 'iso-8859-4' : 'ISO-8859-4',
 24 |       \ 'iso-8859-5' : 'ISO-8859-5',
 25 |       \ 'iso-8859-6' : 'ISO-8859-6',
 26 |       \ 'iso-8859-7' : 'ISO-8859-7',
 27 |       \ 'iso-8859-8' : 'ISO-8859-8',
 28 |       \ 'iso-8859-9' : 'ISO-8859-9',
 29 |       \ 'iso-8859-10' : '',
 30 |       \ 'iso-8859-13' : 'ISO-8859-13',
 31 |       \ 'iso-8859-14' : '',
 32 |       \ 'iso-8859-15' : 'ISO-8859-15',
 33 |       \ 'koi8-r' : 'KOI8-R',
 34 |       \ 'koi8-u' : 'KOI8-U',
 35 |       \ 'macroman' : 'macintosh',
 36 |       \ 'cp437' : '',
 37 |       \ 'cp775' : '',
 38 |       \ 'cp850' : '',
 39 |       \ 'cp852' : '',
 40 |       \ 'cp855' : '',
 41 |       \ 'cp857' : '',
 42 |       \ 'cp860' : '',
 43 |       \ 'cp861' : '',
 44 |       \ 'cp862' : '',
 45 |       \ 'cp863' : '',
 46 |       \ 'cp865' : '',
 47 |       \ 'cp866' : 'IBM866',
 48 |       \ 'cp869' : '',
 49 |       \ 'cp874' : '',
 50 |       \ 'cp1250' : 'windows-1250',
 51 |       \ 'cp1251' : 'windows-1251',
 52 |       \ 'cp1253' : 'windows-1253',
 53 |       \ 'cp1254' : 'windows-1254',
 54 |       \ 'cp1255' : 'windows-1255',
 55 |       \ 'cp1256' : 'windows-1256',
 56 |       \ 'cp1257' : 'windows-1257',
 57 |       \ 'cp1258' : 'windows-1258',
 58 |       \ 'euc-jp' : 'EUC-JP',
 59 |       \ 'sjis' : 'Shift_JIS',
 60 |       \ 'cp932' : 'Shift_JIS',
 61 |       \ 'cp949' : '',
 62 |       \ 'euc-kr' : 'EUC-KR',
 63 |       \ 'cp936' : 'GBK',
 64 |       \ 'euc-cn' : 'GB2312',
 65 |       \ 'big5' : 'Big5',
 66 |       \ 'cp950' : 'Big5',
 67 |       \ 'utf-8' : 'UTF-8',
 68 |       \ 'ucs-2' : 'UTF-8',
 69 |       \ 'ucs-2le' : 'UTF-8',
 70 |       \ 'utf-16' : 'UTF-8',
 71 |       \ 'utf-16le' : 'UTF-8',
 72 |       \ 'ucs-4' : 'UTF-8',
 73 |       \ 'ucs-4le' : 'UTF-8',
 74 |       \ }
 75 | lockvar g:toansicolorcodes#encoding_to_charset
 76 | " Notes:
 77 | "   1. All UCS/UTF are converted to UTF-8 because it is much better supported
 78 | "   2. Any blank spaces are there because Vim supports it but at least one major
 79 | "      web browser does not according to http://wiki.whatwg.org/wiki/Web_Encodings.
 80 | " }}}
 81 | 
 82 | " Only automatically find encodings supported natively by Vim, let the user
 83 | " specify the encoding if it's not natively supported. This function is only
 84 | " used when the user specifies the charset, they better know what they are
 85 | " doing!
 86 | "
 87 | " Names in this list are from:
 88 | "   http://www.iana.org/assignments/character-sets
 89 | " g:toansicolorcodes#charset_to_encoding: {{{
 90 | let g:toansicolorcodes#charset_to_encoding = {
 91 |       \ 'iso_8859-1:1987' : 'latin1',
 92 |       \ 'iso-ir-100' : 'latin1',
 93 |       \ 'iso_8859-1' : 'latin1',
 94 |       \ 'iso-8859-1' : 'latin1',
 95 |       \ 'latin1' : 'latin1',
 96 |       \ 'l1' : 'latin1',
 97 |       \ 'ibm819' : 'latin1',
 98 |       \ 'cp819' : 'latin1',
 99 |       \ 'csisolatin1' : 'latin1',
100 |       \ 'iso_8859-2:1987' : 'iso-8859-2',
101 |       \ 'iso-ir-101' : 'iso-8859-2',
102 |       \ 'iso_8859-2' : 'iso-8859-2',
103 |       \ 'iso-8859-2' : 'iso-8859-2',
104 |       \ 'latin2' : 'iso-8859-2',
105 |       \ 'l2' : 'iso-8859-2',
106 |       \ 'csisolatin2' : 'iso-8859-2',
107 |       \ 'iso_8859-3:1988' : 'iso-8859-3',
108 |       \ 'iso-ir-109' : 'iso-8859-3',
109 |       \ 'iso_8859-3' : 'iso-8859-3',
110 |       \ 'iso-8859-3' : 'iso-8859-3',
111 |       \ 'latin3' : 'iso-8859-3',
112 |       \ 'l3' : 'iso-8859-3',
113 |       \ 'csisolatin3' : 'iso-8859-3',
114 |       \ 'iso_8859-4:1988' : 'iso-8859-4',
115 |       \ 'iso-ir-110' : 'iso-8859-4',
116 |       \ 'iso_8859-4' : 'iso-8859-4',
117 |       \ 'iso-8859-4' : 'iso-8859-4',
118 |       \ 'latin4' : 'iso-8859-4',
119 |       \ 'l4' : 'iso-8859-4',
120 |       \ 'csisolatin4' : 'iso-8859-4',
121 |       \ 'iso_8859-5:1988' : 'iso-8859-5',
122 |       \ 'iso-ir-144' : 'iso-8859-5',
123 |       \ 'iso_8859-5' : 'iso-8859-5',
124 |       \ 'iso-8859-5' : 'iso-8859-5',
125 |       \ 'cyrillic' : 'iso-8859-5',
126 |       \ 'csisolatincyrillic' : 'iso-8859-5',
127 |       \ 'iso_8859-6:1987' : 'iso-8859-6',
128 |       \ 'iso-ir-127' : 'iso-8859-6',
129 |       \ 'iso_8859-6' : 'iso-8859-6',
130 |       \ 'iso-8859-6' : 'iso-8859-6',
131 |       \ 'ecma-114' : 'iso-8859-6',
132 |       \ 'asmo-708' : 'iso-8859-6',
133 |       \ 'arabic' : 'iso-8859-6',
134 |       \ 'csisolatinarabic' : 'iso-8859-6',
135 |       \ 'iso_8859-7:1987' : 'iso-8859-7',
136 |       \ 'iso-ir-126' : 'iso-8859-7',
137 |       \ 'iso_8859-7' : 'iso-8859-7',
138 |       \ 'iso-8859-7' : 'iso-8859-7',
139 |       \ 'elot_928' : 'iso-8859-7',
140 |       \ 'ecma-118' : 'iso-8859-7',
141 |       \ 'greek' : 'iso-8859-7',
142 |       \ 'greek8' : 'iso-8859-7',
143 |       \ 'csisolatingreek' : 'iso-8859-7',
144 |       \ 'iso_8859-8:1988' : 'iso-8859-8',
145 |       \ 'iso-ir-138' : 'iso-8859-8',
146 |       \ 'iso_8859-8' : 'iso-8859-8',
147 |       \ 'iso-8859-8' : 'iso-8859-8',
148 |       \ 'hebrew' : 'iso-8859-8',
149 |       \ 'csisolatinhebrew' : 'iso-8859-8',
150 |       \ 'iso_8859-9:1989' : 'iso-8859-9',
151 |       \ 'iso-ir-148' : 'iso-8859-9',
152 |       \ 'iso_8859-9' : 'iso-8859-9',
153 |       \ 'iso-8859-9' : 'iso-8859-9',
154 |       \ 'latin5' : 'iso-8859-9',
155 |       \ 'l5' : 'iso-8859-9',
156 |       \ 'csisolatin5' : 'iso-8859-9',
157 |       \ 'iso-8859-10' : 'iso-8859-10',
158 |       \ 'iso-ir-157' : 'iso-8859-10',
159 |       \ 'l6' : 'iso-8859-10',
160 |       \ 'iso_8859-10:1992' : 'iso-8859-10',
161 |       \ 'csisolatin6' : 'iso-8859-10',
162 |       \ 'latin6' : 'iso-8859-10',
163 |       \ 'iso-8859-13' : 'iso-8859-13',
164 |       \ 'iso-8859-14' : 'iso-8859-14',
165 |       \ 'iso-ir-199' : 'iso-8859-14',
166 |       \ 'iso_8859-14:1998' : 'iso-8859-14',
167 |       \ 'iso_8859-14' : 'iso-8859-14',
168 |       \ 'latin8' : 'iso-8859-14',
169 |       \ 'iso-celtic' : 'iso-8859-14',
170 |       \ 'l8' : 'iso-8859-14',
171 |       \ 'iso-8859-15' : 'iso-8859-15',
172 |       \ 'iso_8859-15' : 'iso-8859-15',
173 |       \ 'latin-9' : 'iso-8859-15',
174 |       \ 'koi8-r' : 'koi8-r',
175 |       \ 'cskoi8r' : 'koi8-r',
176 |       \ 'koi8-u' : 'koi8-u',
177 |       \ 'macintosh' : 'macroman',
178 |       \ 'mac' : 'macroman',
179 |       \ 'csmacintosh' : 'macroman',
180 |       \ 'ibm437' : 'cp437',
181 |       \ 'cp437' : 'cp437',
182 |       \ '437' : 'cp437',
183 |       \ 'cspc8codepage437' : 'cp437',
184 |       \ 'ibm775' : 'cp775',
185 |       \ 'cp775' : 'cp775',
186 |       \ 'cspc775baltic' : 'cp775',
187 |       \ 'ibm850' : 'cp850',
188 |       \ 'cp850' : 'cp850',
189 |       \ '850' : 'cp850',
190 |       \ 'cspc850multilingual' : 'cp850',
191 |       \ 'ibm852' : 'cp852',
192 |       \ 'cp852' : 'cp852',
193 |       \ '852' : 'cp852',
194 |       \ 'cspcp852' : 'cp852',
195 |       \ 'ibm855' : 'cp855',
196 |       \ 'cp855' : 'cp855',
197 |       \ '855' : 'cp855',
198 |       \ 'csibm855' : 'cp855',
199 |       \ 'ibm857' : 'cp857',
200 |       \ 'cp857' : 'cp857',
201 |       \ '857' : 'cp857',
202 |       \ 'csibm857' : 'cp857',
203 |       \ 'ibm860' : 'cp860',
204 |       \ 'cp860' : 'cp860',
205 |       \ '860' : 'cp860',
206 |       \ 'csibm860' : 'cp860',
207 |       \ 'ibm861' : 'cp861',
208 |       \ 'cp861' : 'cp861',
209 |       \ '861' : 'cp861',
210 |       \ 'cp-is' : 'cp861',
211 |       \ 'csibm861' : 'cp861',
212 |       \ 'ibm862' : 'cp862',
213 |       \ 'cp862' : 'cp862',
214 |       \ '862' : 'cp862',
215 |       \ 'cspc862latinhebrew' : 'cp862',
216 |       \ 'ibm863' : 'cp863',
217 |       \ 'cp863' : 'cp863',
218 |       \ '863' : 'cp863',
219 |       \ 'csibm863' : 'cp863',
220 |       \ 'ibm865' : 'cp865',
221 |       \ 'cp865' : 'cp865',
222 |       \ '865' : 'cp865',
223 |       \ 'csibm865' : 'cp865',
224 |       \ 'ibm866' : 'cp866',
225 |       \ 'cp866' : 'cp866',
226 |       \ '866' : 'cp866',
227 |       \ 'csibm866' : 'cp866',
228 |       \ 'ibm869' : 'cp869',
229 |       \ 'cp869' : 'cp869',
230 |       \ '869' : 'cp869',
231 |       \ 'cp-gr' : 'cp869',
232 |       \ 'csibm869' : 'cp869',
233 |       \ 'windows-1250' : 'cp1250',
234 |       \ 'windows-1251' : 'cp1251',
235 |       \ 'windows-1253' : 'cp1253',
236 |       \ 'windows-1254' : 'cp1254',
237 |       \ 'windows-1255' : 'cp1255',
238 |       \ 'windows-1256' : 'cp1256',
239 |       \ 'windows-1257' : 'cp1257',
240 |       \ 'windows-1258' : 'cp1258',
241 |       \ 'extended_unix_code_packed_format_for_japanese' : 'euc-jp',
242 |       \ 'cseucpkdfmtjapanese' : 'euc-jp',
243 |       \ 'euc-jp' : 'euc-jp',
244 |       \ 'shift_jis' : 'sjis',
245 |       \ 'ms_kanji' : 'sjis',
246 |       \ 'sjis' : 'sjis',
247 |       \ 'csshiftjis' : 'sjis',
248 |       \ 'ibm-thai' : 'cp874',
249 |       \ 'csibmthai' : 'cp874',
250 |       \ 'ks_c_5601-1987' : 'cp949',
251 |       \ 'iso-ir-149' : 'cp949',
252 |       \ 'ks_c_5601-1989' : 'cp949',
253 |       \ 'ksc_5601' : 'cp949',
254 |       \ 'korean' : 'cp949',
255 |       \ 'csksc56011987' : 'cp949',
256 |       \ 'euc-kr' : 'euc-kr',
257 |       \ 'cseuckr' : 'euc-kr',
258 |       \ 'gbk' : 'cp936',
259 |       \ 'cp936' : 'cp936',
260 |       \ 'ms936' : 'cp936',
261 |       \ 'windows-936' : 'cp936',
262 |       \ 'gb_2312-80' : 'euc-cn',
263 |       \ 'iso-ir-58' : 'euc-cn',
264 |       \ 'chinese' : 'euc-cn',
265 |       \ 'csiso58gb231280' : 'euc-cn',
266 |       \ 'big5' : 'big5',
267 |       \ 'csbig5' : 'big5',
268 |       \ 'utf-8' : 'utf-8',
269 |       \ 'iso-10646-ucs-2' : 'ucs-2',
270 |       \ 'csunicode' : 'ucs-2',
271 |       \ 'utf-16' : 'utf-16',
272 |       \ 'utf-16be' : 'utf-16',
273 |       \ 'utf-16le' : 'utf-16le',
274 |       \ 'utf-32' : 'ucs-4',
275 |       \ 'utf-32be' : 'ucs-4',
276 |       \ 'utf-32le' : 'ucs-4le',
277 |       \ 'iso-10646-ucs-4' : 'ucs-4',
278 |       \ 'csucs4' : 'ucs-4'
279 |       \ }
280 | lockvar g:toansicolorcodes#charset_to_encoding
281 | "}}}
282 | 
283 | func! toansicolorcodes#Convert2ANSIColorCodes(line1, line2) "{{{
284 |   let s:settings = toansicolorcodes#GetUserSettings()
285 | 
286 |   if a:line2 >= a:line1 "{{{
287 |     let g:ansicolorcodes_start_line = a:line1
288 |     let g:ansicolorcodes_end_line = a:line2
289 |   else
290 |     let g:ansicolorcodes_start_line = a:line2
291 |     let g:ansicolorcodes_end_line = a:line1
292 |   endif
293 |   runtime syntax/2ansicolorcodes.vim "}}}
294 | 
295 |   unlet g:ansicolorcodes_start_line
296 |   unlet g:ansicolorcodes_end_line
297 |   unlet s:settings
298 | endfunc "}}}
299 | 
300 | " Gets a single user option and sets it in the passed-in Dict, or gives it the
301 | " default value if the option doesn't actually exist.
302 | func! toansicolorcodes#GetOption(settings, option, default) "{{{
303 |   if exists('g:ansicolorcodes_'.a:option)
304 |     let a:settings[a:option] = g:ansicolorcodes_{a:option}
305 |   else
306 |     let a:settings[a:option] = a:default
307 |   endif
308 | endfunc "}}}
309 | 
310 | " returns a Dict containing the values of all user options for 2ansicolorcodes, including
311 | " default values for those not given an explicit value by the user. Discards the
312 | " html_ prefix of the option for nicer looking code.
313 | func! toansicolorcodes#GetUserSettings() "{{{
314 |   if exists('s:settings')
315 |     " just restore the known options if we've already retrieved them
316 |     return s:settings
317 |   else
318 |     " otherwise figure out which options are set
319 |     let user_settings = {}
320 | 
321 |     " get current option settings with appropriate defaults {{{
322 |     call toansicolorcodes#GetOption(user_settings,    'no_progress', !has("statusline") )
323 |     call toansicolorcodes#GetOption(user_settings,   'number_lines', &number )
324 |     call toansicolorcodes#GetOption(user_settings, 'ignore_conceal', 0 )
325 |     call toansicolorcodes#GetOption(user_settings, 'ignore_folding', 0 )
326 |     call toansicolorcodes#GetOption(user_settings,   'whole_filler', 0 )
327 |     " }}}
328 |     
329 |     " set up expand_tabs option after all the overrides so we know the
330 |     " appropriate defaults {{{
331 |     call toansicolorcodes#GetOption(user_settings,
332 |           \ 'expand_tabs',
333 |           \ &expandtab || &ts != 8 || user_settings.number_lines)
334 |     " }}}
335 | 
336 |     if exists("g:ansicolorcodes_use_encoding") "{{{
337 |       " user specified the desired MIME charset, figure out proper
338 |       " 'fileencoding' from it or warn the user if we cannot
339 |       let user_settings.encoding = g:ansicolorcodes_use_encoding
340 |       let user_settings.vim_encoding = toansicolorcodes#EncodingFromCharset(g:ansicolorcodes_use_encoding)
341 |       if user_settings.vim_encoding == ''
342 |         echohl WarningMsg
343 |         echomsg "TOansicolorcodes: file encoding for"
344 |               \ g:ansicolorcodes_use_encoding
345 |               \ "unknown, please set 'fileencoding'"
346 |         echohl None
347 |       endif
348 |     else
349 |       " Figure out proper MIME charset from 'fileencoding' if possible
350 |       if &l:fileencoding != '' 
351 |         " If the buffer is not a "normal" type, the 'fileencoding' value may not
352 |         " be trusted; since the buffer should not be written the fileencoding is
353 |         " not intended to be used.
354 |         if &l:buftype=='' || &l:buftype==?'help'
355 |           let user_settings.vim_encoding = &l:fileencoding
356 |           call toansicolorcodes#CharsetFromEncoding(user_settings)
357 |         else
358 |           let user_settings.encoding = '' " trigger detection using &encoding
359 |         endif
360 |       endif
361 | 
362 |       " else from 'encoding' if possible
363 |       if &l:fileencoding == '' || user_settings.encoding == ''
364 |         let user_settings.vim_encoding = &encoding
365 |         call toansicolorcodes#CharsetFromEncoding(user_settings)
366 |       endif
367 | 
368 |       " else default to UTF-8 and warn user
369 |       if user_settings.encoding == ''
370 |         let user_settings.vim_encoding = 'utf-8'
371 |         let user_settings.encoding = 'UTF-8'
372 |         echohl WarningMsg
373 |         echomsg "TOansicolorcodes: couldn't determine MIME charset, using UTF-8"
374 |         echohl None
375 |       endif
376 |     endif "}}}
377 | 
378 |     " TODO: font
379 | 
380 |     return user_settings
381 |   endif
382 | endfunc "}}}
383 | 
384 | " get the proper HTML charset name from a Vim encoding option.
385 | function! toansicolorcodes#CharsetFromEncoding(settings) "{{{
386 |   let l:vim_encoding = a:settings.vim_encoding
387 |   if exists('g:ansicolorcodes_charset_override') && has_key(g:ansicolorcodes_charset_override, l:vim_encoding)
388 |     let a:settings.encoding = g:ansicolorcodes_charset_override[l:vim_encoding]
389 |   else
390 |     if l:vim_encoding =~ '^8bit\|^2byte'
391 |       " 8bit- and 2byte- prefixes are to indicate encodings available on the
392 |       " system that Vim will convert with iconv(), look up just the encoding name,
393 |       " not Vim's prefix.
394 |       let l:vim_encoding = substitute(l:vim_encoding, '^8bit-\|^2byte-', '', '')
395 |     endif
396 |     if has_key(g:toansicolorcodes#encoding_to_charset, l:vim_encoding)
397 |       let a:settings.encoding = g:toansicolorcodes#encoding_to_charset[l:vim_encoding]
398 |     else
399 |       let a:settings.encoding = ""
400 |     endif
401 |   endif
402 |   if a:settings.encoding != ""
403 |     let l:vim_encoding = toansicolorcodes#EncodingFromCharset(a:settings.encoding)
404 |     if l:vim_encoding != ""
405 |       " if the Vim encoding to HTML encoding conversion is set up (by default or
406 |       " by the user) to convert to a different encoding, we need to also change
407 |       " the Vim encoding of the new buffer
408 |       let a:settings.vim_encoding = l:vim_encoding
409 |     endif
410 |   endif
411 | endfun "}}}
412 | 
413 | " Get the proper Vim encoding option setting from an HTML charset name.
414 | function! toansicolorcodes#EncodingFromCharset(encoding) "{{{
415 |   if exists('g:ansicolorcodes_encoding_override') && has_key(g:ansicolorcodes_encoding_override, a:encoding)
416 |     return g:ansicolorcodes_encoding_override[a:encoding]
417 |   elseif has_key(g:toansicolorcodes#charset_to_encoding, tolower(a:encoding))
418 |     return g:toansicolorcodes#charset_to_encoding[tolower(a:encoding)]
419 |   else
420 |     return ""
421 |   endif
422 | endfun "}}}
423 | 
424 | let &cpo = s:cpo_sav
425 | unlet s:cpo_sav
426 | 
427 | " Make sure any patches will probably use consistent indent
428 | "   vim: ts=2 sw=2 sts=2 et fdm=marker
429 | 


--------------------------------------------------------------------------------
/runtime/syntax/2ansicolorcodes.vim:
--------------------------------------------------------------------------------
  1 | " Vim syntax support file
  2 | " Maintainer: Olivier Favre 
  3 | " Last Change: 2023 May 29
  4 | 
  5 | " Transform a file into ANSI color codes, using the current syntax highlighting.
  6 | 
  7 | " This file uses line continuations
  8 | let s:cpo_sav = &cpo
  9 | let s:ls  = &ls
 10 | set cpo-=C
 11 | 
 12 | let s:end=line('$')
 13 | 
 14 | let s:settings = toansicolorcodes#GetUserSettings()
 15 | 
 16 | if exists('g:ansicolorcodes_truecolors') && g:ansicolorcodes_truecolors != '0'
 17 | 
 18 |   " Load GVim related configuration files
 19 |   " This was not done automatically because we need to run `vim`, not `gvim`
 20 | 
 21 |   " Load system-wide gvimrc
 22 |   runtime gvimrc
 23 |   " Load user-wide gvimrc
 24 |   if filereadable(expand('~/.gvimrc'))
 25 |     source ~/.gvimrc
 26 |   endif
 27 | 
 28 |   if !exists('v:colornames')
 29 |     " Load colorname to RGB translation table
 30 |     runtime syntax/2ansicolorcodes_colors.vim
 31 |   endif
 32 | 
 33 |   let s:whatterm = 'gui'
 34 | 
 35 |   " Hard-set Konsole 24 bits color codes
 36 |   let s:fg_code = "\x1B[38;2;%p1%sm"
 37 |   let s:bg_code = "\x1B[48;2;%p1%sm"
 38 | 
 39 | else
 40 | 
 41 |   if &t_Co > 1
 42 |     let s:whatterm = 'cterm'
 43 |   else
 44 |     let s:whatterm = 'term'
 45 |   endif
 46 | 
 47 |   if &t_AF != ''
 48 |     let s:fg_code = &t_AF
 49 |   else
 50 |     let s:fg_code = &t_Sf
 51 |   endif
 52 | 
 53 |   if &t_AB != ''
 54 |     let s:bg_code = &t_AB
 55 |   else
 56 |     let s:bg_code = &t_Sb
 57 |   endif
 58 | 
 59 | endif
 60 | 
 61 | let s:normal_id = synIDtrans(hlID('normal'))
 62 | let s:normal_fg = synIDattr(s:normal_id, 'fg#', s:whatterm)
 63 | let s:normal_bg = synIDattr(s:normal_id, 'bg#', s:whatterm)
 64 | let s:normal_fg_bold = synIDattr(s:normal_id, 'bold', s:whatterm)
 65 | " store last highlight attribute
 66 | let s:last_fg = -1
 67 | let s:last_bg = -1
 68 | let s:last_bold = 0
 69 | let s:last_inverse = 0
 70 | let s:last_standout = 0
 71 | let s:last_undercurl = 0
 72 | let s:last_italic = 0
 73 | let s:last_underline = 0
 74 | " memoization
 75 | let s:term_color_cache = {}
 76 | 
 77 | " set termcap
 78 | "     t_Co=8          : number of colors
 79 | "     t_AB=\e[4%p1%dm : ANSI background
 80 | "     t_AF=\e[3%p1%dm : ANSI foreground
 81 | "     t_mb=\e[5m      : blink
 82 | "     t_md=\e[1m      : bold
 83 | "     t_me=\e[m       : normal (no invert, no blink, no bold, default color, also no underline)
 84 | "     t_mr=\e[7m      : invert
 85 | "     t_op=\e[39;49m  : reset foreground and background
 86 | "     t_se=\e[27m     : standout end
 87 | "     t_so=\e[7m      : standout mode (sometimes invert)
 88 | "     t_ue=\e[m       : underline end
 89 | "     t_us=\e[4m      : underline mode
 90 | "     t_Ce=(N/A)      : undercurl end (gui mode only?)
 91 | "     t_Cs=(N/A)      : undercurl mode (gui mode only?)
 92 | "     t_ZH=\e[6m      : italic mode
 93 | "     t_ZR=\e[m       : italic end
 94 | "     t_Sb=\e[4%?%p1%{1}%=%t4%e%p1%{3}%=%t6%e%p1%{4}%=%t1%e%p1%{6}%=%t3%e%p1%d%;m
 95 | "                     : background
 96 | "     t_Sf=\e[3%?%p1%{1}%=%t4%e%p1%{3}%=%t6%e%p1%{4}%=%t1%e%p1%{6}%=%t3%e%p1%d%;m
 97 | "                     : foreground
 98 | " If t_Co is non zero:
 99 | "   Use t_AB and t_AF if available, t_Sb and t_Sf otherwise.
100 | "   Use t_me to reset
101 | 
102 | " See term.c:void term_color(uchar*,int)
103 | function! s:term_color(code, color)
104 |   let code = a:code
105 |   let color = a:color
106 |   " Already memoized?
107 |   let cache_key = color."#".code
108 |   if has_key(s:term_color_cache, cache_key)
109 |     return s:term_color_cache[cache_key]
110 |   endif
111 |   if color >= 8 && &t_Co >= 16
112 |     let i = 0
113 |     if code[0] == "\x9B"
114 |       let i = 1
115 |     elseif (code[0] == "\e" || code[0] == "\033" || code[0] == "\x27") && code[1] == '['
116 |       let i = 2
117 |     endif
118 |     if i != 0 && code[i] != "\0"
119 |           \ && (code[i] == '3' || code[i] == '4')
120 |           \ && (code[(i+1):(i+1+6)] == '%p1%dm' || code[(i+1):(i+1+3)] == '%dm')
121 |       if color >= 16
122 |         let code = code[0 : (i)] . '8;5;' . code[(i+1) : ]
123 |       else
124 |         if code[i] == 3 "fg
125 |           let code = code[0 : (i-1)] . '9' . code[(i+1) : ]
126 |         else
127 |           let code = code[0 : (i-1)] . '10' . code[(i+1) : ]
128 |         endif
129 |         let color = color - 8
130 |       endif
131 |     endif
132 |   endif
133 |   let rtn = s:tgoto(code, color)
134 |   " Memoize result
135 |   let s:term_color_cache[cache_key] = rtn
136 |   return rtn
137 | endfun
138 | " See termlib.c:char* tgoto(char*,int,int)
139 | " See man terminfo(5)#Parameterized-Strings
140 | function! s:tgoto(code, color)
141 |   let acolor = a:color   " as a variable, because it could be modified
142 |   let stack = []         " value stack
143 |   let e = strlen(a:code) " end offset of a:code
144 |   let i = 0              " current offset inside a:code
145 |   let ifmatched = 0      " whether a %t (then) matched
146 |   let seekto = ''        " request to seek to a particular string sequence
147 |   let code = ''          " string output
148 |   while i < e
149 |     let c = a:code[i]
150 |     if c == '%' && i + 1 < e
151 |       let i = i + 1
152 |       let c = a:code[i]
153 |       if c == '%'
154 |         let code = code . '%'
155 |       elseif c == 'p' && i + 1 < e && a:code[i+1] == '1' " push parameter 1 onto the stack
156 |         " Only %p1 is defined, it is the color
157 |         let i = i + 1
158 |         let stack += [acolor]
159 |       elseif c == 'd' " format number (simplified)
160 |         let code = code . printf('%d', remove(stack, -1))
161 |       elseif c == 's' " format string (simplified)
162 |         let code = code . printf('%s', remove(stack, -1))
163 |       elseif c == "'" && i + 2 < e && a:code[i+2] == "'" " push a character onto the stack
164 |         let i = i + 2
165 |         let stack += [a:code[i-1]]
166 |       elseif c == '{' && i + 2 < e && a:code[i+2] == '}' " push a 1-digit number onto the stack
167 |         let i = i + 2
168 |         let stack += [a:code[i-1]]
169 |       elseif c == '{' && i + 3 < e && a:code[i+3] == '}' " push a 2-digit number onto the stack
170 |         let i = i + 3
171 |         let stack += [a:code[i-2:i-1]]
172 |       elseif c == '{' && i + 4 < e && a:code[i+4] == '}' " push a 3-digit number onto the stack (no need for more apparently)
173 |         let i = i + 4
174 |         let stack += [a:code[i-3:i-1]]
175 |       elseif c == 'l' " replace topmost value by its length
176 |         let stack += [strlen(remove(stack, -1))]
177 |       elseif c == '+' " add the two topmost values
178 |         let b = remove(stack, -1) + 0
179 |         let a = remove(stack, -1) + 0
180 |         let stack += [a + b]
181 |       elseif c == '-' " subtract the two topmost values
182 |         let b = remove(stack, -1) + 0
183 |         let a = remove(stack, -1) + 0
184 |         let stack += [a - b]
185 |       elseif c == '*' " multiply the two topmost values
186 |         let b = remove(stack, -1) + 0
187 |         let a = remove(stack, -1) + 0
188 |         let stack += [a * b]
189 |       elseif c == '/' " divide the two topmost values
190 |         let b = remove(stack, -1) + 0
191 |         let a = remove(stack, -1) + 0
192 |         let stack += [a / b]
193 |       elseif c == 'm' " calculate modulus of the two topmost values
194 |         let b = remove(stack, -1) + 0
195 |         let a = remove(stack, -1) + 0
196 |         let stack += [a % b]
197 |       elseif c == '&' || c == '|' || c == '^' || c == '!' || c == '~'
198 |         " %& %| %^ %! %~ bitwise operations AND OR XOR NOT are not supported by vim
199 |       elseif c == '<' " less-than comparison of the two topmost values
200 |         let b = remove(stack, -1) + 0
201 |         let a = remove(stack, -1) + 0
202 |         let stack += [a < b]
203 |       elseif c == '=' " equality comparison of the two topmost values
204 |         let b = remove(stack, -1) + 0
205 |         let a = remove(stack, -1) + 0
206 |         let stack += [a == b]
207 |       elseif c == '>' " greater-than comparison of the two topmost values
208 |         let b = remove(stack, -1) + 0
209 |         let a = remove(stack, -1) + 0
210 |         let stack += [a > b]
211 |       elseif c == 'i' " increment %p1
212 |         let acolor = acolor + 1
213 |       elseif c == '?' " if start
214 |         " Ignore if nesting for simplicity
215 |       elseif c == 't' " then
216 |         if ifmatched == 1 " (should not happen)
217 |           let seekto = '%;'
218 |         elseif remove(stack, -1) == 0 " test topmost value
219 |           let seekto = '%e' " jump to next else
220 |         else
221 |           let ifmatched = 1 " note we are inside an if clause body to permit jumping to if end afterwards
222 |         endif
223 |       elseif c == 'e' " else
224 |         if ifmatched == 1 " the previous then matched, jump to if end
225 |           let seekto = '%;'
226 |         endif
227 |       elseif c == ';' " if end
228 |         let ifmatched = 0
229 |       else " unknown escape
230 |         let code = code . '%' . c
231 |       endif
232 |     else " regular character
233 |       let code = code . c
234 |     endif
235 |     let i = i + 1
236 |     " Eventual seek
237 |     if seekto != ''
238 |       let pos = stridx(a:code, seekto, i)
239 |       if pos == -1 " rescue string not found by ignoring seek
240 |         let pos = i
241 |       endif
242 |       if seekto != '%;'
243 |         " Seeks are used inside ifs, don't permit crossing the if end
244 |         let ifend = stridx(a:code, '%;', i)
245 |         if ifend < pos " cap to if end if sought string is further (no final %e before %;)
246 |           let pos = ifend
247 |         endif
248 |       endif
249 |       let i = pos
250 |       let seekto = ''
251 |     endif
252 |   endwhile
253 | 
254 |   return code
255 | endfun
256 | function! s:color_is_set(color)
257 |   return a:color != '' && a:color >= 0
258 | endfun
259 | function! s:translate_color(color)
260 |   if s:color_is_set(a:color)
261 |     let l:color = a:color
262 |     if l:color[0] !~ '^\v[#0-9]'
263 |       if exists('v:colornames')
264 |         let l:color = v:colornames[tolower(l:color)]
265 |       else
266 |         let l:color = g:ansicolorcodes_colors[tolower(l:color)]
267 |       endif
268 |     endif
269 |     if l:color[0] == '#'
270 |       let l:r = ('0x' . l:color[1:2]) + 0
271 |       let l:g = ('0x' . l:color[3:4]) + 0
272 |       let l:b = ('0x' . l:color[5:6]) + 0
273 |       let l:color = l:r . ';' . l:g . ';' . l:b
274 |     endif
275 |   endif
276 |   return l:color
277 | endfun
278 | if s:whatterm == 'gui'
279 |   function! s:term_fg_color(color)
280 |     if !s:color_is_set(a:color)
281 |       return ''
282 |     else
283 |       return s:term_color(s:fg_code, s:translate_color(a:color))
284 |     endif
285 |   endfun
286 |   " See term.c:void term_bg_color(int)
287 |   function! s:term_bg_color(color)
288 |     if !s:color_is_set(a:color)
289 |       return ''
290 |     else
291 |       return s:term_color(s:bg_code, s:translate_color(a:color))
292 |     endif
293 |   endfun
294 | else
295 |   " See term.c:void term_fg_color(int)
296 |   function! s:term_fg_color(color)
297 |     if !s:color_is_set(a:color)
298 |       return ''
299 |     else
300 |       return s:term_color(s:fg_code, a:color)
301 |     endif
302 |   endfun
303 |   " See term.c:void term_bg_color(int)
304 |   function! s:term_bg_color(color)
305 |     if !s:color_is_set(a:color)
306 |       return ''
307 |     else
308 |       return s:term_color(s:bg_code, a:color)
309 |     endif
310 |   endfun
311 | endif
312 | " See screen.c:void screen_start_highlight(int)
313 | function! s:screen_start_highlight(id)
314 |   let rtn = ''
315 |   if &t_Co > 1 && s:normal_fg_bold && s:color_is_set(synIDattr(a:id, 'fg#', s:whatterm))
316 |     let rtn = rtn . &t_me
317 |   endif
318 |   if exists('&t_md') && synIDattr(a:id, 'bold', s:whatterm)
319 |     let rtn = rtn . &t_md
320 |   endif
321 |   if exists('&t_so') && synIDattr(a:id, 'standout', s:whatterm)
322 |     let rtn = rtn . &t_so
323 |   endif
324 |   if exists('&t_us') && (synIDattr(a:id, 'underline', s:whatterm) || synIDattr(a:id, 'undercurl', s:whatterm))
325 |     let rtn = rtn . &t_us
326 |   endif
327 |   if exists('&t_ZH') && synIDattr(a:id, 'italic', s:whatterm)
328 |     let rtn = rtn . &t_ZH
329 |   endif
330 |   if exists('&t_mr') && synIDattr(a:id, 'inverse', s:whatterm)
331 |     let rtn = rtn . &t_mr
332 |   endif
333 |   if &t_Co > 1
334 |     if s:last_fg != synIDattr(a:id, 'fg#', s:whatterm) || s:last_bg != synIDattr(a:id, 'bg#', s:whatterm)
335 |       if (s:color_is_set(s:last_fg) && !s:color_is_set(synIDattr(a:id, 'fg#', s:whatterm))) || (s:color_is_set(s:last_bg) && !s:color_is_set(synIDattr(a:id, 'bg#', s:whatterm)))
336 |         let rtn = rtn . &t_op
337 |       endif
338 |       let s:last_fg = synIDattr(a:id, 'fg#', s:whatterm)
339 |       let s:last_bg = synIDattr(a:id, 'bg#', s:whatterm)
340 |       let rtn = rtn . s:term_fg_color(synIDattr(a:id, 'fg#', s:whatterm))
341 |             \ . s:term_bg_color(synIDattr(a:id, 'bg#', s:whatterm))
342 |     endif
343 |   else
344 |     " The following cannot be implemented due to lack of Vim script getters
345 |     "let start = synIDattr(s:last_id, 'start', s:whatterm)
346 |     "if start
347 |     "  let rtn = rtn . start
348 |     "endif
349 |   endif
350 |   let s:last_id = a:id
351 |   let s:last_bold = synIDattr(a:id, 'bold', s:whatterm)
352 |   let s:last_standout = synIDattr(a:id, 'standout', s:whatterm)
353 |   let s:last_undercurl = synIDattr(a:id, 'undercurl', s:whatterm)
354 |   let s:last_underline = synIDattr(a:id, 'underline', s:whatterm)
355 |   let s:last_italic = synIDattr(a:id, 'italic', s:whatterm)
356 |   let s:last_inverse = synIDattr(a:id, 'inverse', s:whatterm)
357 |   let s:last_fg = synIDattr(a:id, 'fg#', s:whatterm)
358 |   let s:last_bg = synIDattr(a:id, 'bg#', s:whatterm)
359 |   return rtn
360 | endfun
361 | " See screen.c:void screen_stop_highlight()
362 | function! s:screen_stop_highlight()
363 |   let rtn = ''
364 |   let me = 0
365 |   if &t_Co > 1
366 |     if s:color_is_set(s:last_fg) || s:color_is_set(s:last_bg)
367 |       let me = 1 " assume &t_me restores the original colors
368 |     else
369 |       " The following cannot be implemented due to lack of Vim script getters
370 |       "let stop = synIDattr(s:last_id, 'stop', s:whatterm)
371 |       "if stop
372 |       "  if stop == &t_me | let me = 1
373 |       "  else | let rtn = rtn . stop | endif
374 |       "endif
375 |     endif
376 |   endif
377 |   if s:last_standout
378 |     if &t_se == &t_me | let me = 1
379 |     else | let rtn = rtn . &t_se | endif
380 |   endif
381 |   if s:last_underline || s:last_undercurl
382 |     if &t_ue == &t_me | let me = 1
383 |     else | let rtn = rtn . &t_ue | endif
384 |   endif
385 |   if s:last_italic
386 |     if &t_ZR == &t_me | let me = 1
387 |     else | let rtn = rtn . &t_ZR | endif
388 |   endif
389 |   if me == 1 || s:last_bold || s:last_inverse
390 |     let rtn = rtn . &t_me
391 |   endif
392 |   if &t_Co > 1
393 |     if s:color_is_set(s:normal_fg) | let rtn = rtn . s:term_fg_color(s:normal_fg) | endif
394 |     if s:color_is_set(s:normal_bg) | let rtn = rtn . s:term_bg_color(s:normal_bg) | endif
395 |     if s:normal_fg_bold | let rtn = rtn . &t_md | endif
396 |   endif
397 |   let s:last_id = -1
398 |   let s:last_bold = 0
399 |   let s:last_standout = 0
400 |   let s:last_undercurl = 0
401 |   let s:last_underline = 0
402 |   let s:last_italic = 0
403 |   let s:last_inverse = 0
404 |   let s:last_fg = -1
405 |   let s:last_bg = -1
406 |   return rtn
407 | endfun
408 | " See screen.c:void reset_cterm_colors()
409 | function! s:reset_cterm_colors()
410 |   let rtn = ''
411 |   if &t_Co > 1
412 |     if s:color_is_set(s:normal_fg) || s:color_is_set(s:normal_bg)
413 |       let rtn = rtn . &t_op
414 |     endif
415 |     if s:normal_fg_bold
416 |       let rtn = rtn . &t_me
417 |     endif
418 |   endif
419 |   let s:last_id = -1
420 |   let s:last_bold = 0
421 |   let s:last_standout = 0
422 |   let s:last_undercurl = 0
423 |   let s:last_underline = 0
424 |   let s:last_italic = 0
425 |   let s:last_inverse = 0
426 |   let s:last_fg = -1
427 |   let s:last_bg = -1
428 |   return rtn
429 | endfun
430 | 
431 | function! s:Format(text, name)
432 |   let text = strtrans(a:text)
433 |   let s:id = synIDtrans(hlID(a:name))
434 |   return s:screen_start_highlight(s:id) . text . s:screen_stop_highlight()
435 | endfun
436 | 
437 | 
438 | 
439 | " Set some options to make it work faster.
440 | " Don't report changes for :substitute, there will be many of them.
441 | " Don't change other windows; turn off scroll bind temporarily
442 | let s:old_title = &title
443 | let s:old_icon = &icon
444 | let s:old_et = &l:et
445 | let s:old_bind = &l:scrollbind
446 | let s:old_report = &report
447 | let s:old_search = @/
448 | let s:old_more = &more
449 | set notitle noicon
450 | setlocal et
451 | set nomore
452 | set report=1000000
453 | setlocal noscrollbind
454 | 
455 | if exists(':ownsyntax') && exists('w:current_syntax')
456 |   let s:current_syntax = w:current_syntax
457 | elseif exists('b:current_syntax')
458 |   let s:current_syntax = b:current_syntax
459 | else
460 |   let s:current_syntax = 'none'
461 | endif
462 | 
463 | if s:current_syntax == ''
464 |   let s:current_syntax = 'none'
465 | endif
466 | 
467 | " Split window to create a buffer with the ANSI color coded file.
468 | let s:orgbufnr = winbufnr(0)
469 | let s:origwin_stl = &l:stl
470 | if expand('%') == ''
471 |   exec 'new Untitled.cat'
472 | else
473 |   exec 'new %.cat'
474 | endif
475 | 
476 | " Resize the new window to very small in order to make it draw faster
477 | let s:old_winheight = winheight(0)
478 | let s:old_winfixheight = &l:winfixheight
479 | if s:old_winheight > 2
480 |   resize 1 " leave enough room to view one line at a time
481 |   norm! G
482 |   norm! zt
483 | endif
484 | setlocal winfixheight
485 | 
486 | let s:newwin_stl = &l:stl
487 | 
488 | " on the new window, set the least time-consuming fold method
489 | let s:old_fdm = &foldmethod
490 | let s:old_fen = &foldenable
491 | setlocal foldmethod=manual
492 | setlocal nofoldenable
493 | 
494 | let s:newwin = winnr()
495 | let s:orgwin = bufwinnr(s:orgbufnr)
496 | 
497 | setlocal modifiable
498 | %d
499 | let s:old_paste = &paste
500 | set paste
501 | let s:old_magic = &magic
502 | set magic
503 | 
504 | " set the fileencoding to match the charset we'll be using
505 | let &l:fileencoding=s:settings.vim_encoding
506 | 
507 | exe s:orgwin . 'wincmd w'
508 | 
509 | 
510 | 
511 | " Now loop over all lines in the original text to convert to ANSI color codes.
512 | " Use ansicolorcodes_start_line and ansicolorcodes_end_line if they are set.
513 | if exists('g:ansicolorcodes_start_line')
514 |   let s:lnum = ansicolorcodes_start_line
515 |   if s:lnum < 1 || s:lnum > line('$')
516 |     let s:lnum = 1
517 |   endif
518 | else
519 |   let s:lnum = 1
520 | endif
521 | if exists('g:ansicolorcodes_end_line')
522 |   let s:end = ansicolorcodes_end_line
523 |   if s:end < s:lnum || s:end > line('$')
524 |     let s:end = line('$')
525 |   endif
526 | else
527 |   let s:end = line('$')
528 | endif
529 | 
530 | 
531 | 
532 | if s:settings.number_lines
533 |   let s:margin = max([3, strlen(s:end)]) + 1
534 | else
535 |   let s:margin = 0
536 | endif
537 | 
538 | if has('folding') && !s:settings.ignore_folding
539 |   let s:foldfillchar = &fillchars[matchend(&fillchars, 'fold:')]
540 |   if s:foldfillchar == ''
541 |     let s:foldfillchar = '-'
542 |   endif
543 | endif
544 | 
545 | if !s:settings.expand_tabs
546 |   " If keeping tabs, add them to printable characters so we keep them when
547 |   " formatting text (strtrans() doesn't replace printable chars)
548 |   let s:old_isprint = &isprint
549 |   setlocal isprint+=9
550 | endif
551 | 
552 | 
553 | 
554 | let s:lines = []
555 | 
556 | while s:lnum <= s:end
557 |   " Start the line with the line number.
558 |   if s:settings.number_lines
559 |     let s:numcol = repeat(' ', s:margin - 1 - strlen(s:lnum)) . s:lnum . ' '
560 |   else
561 |     let s:numcol = ''
562 |   endif
563 | 
564 |   if s:color_is_set(s:normal_bg)
565 |     let s:new = &t_ce " clear to end of line to apply the normal background color
566 |   else
567 |     let s:new = ''
568 |   endif
569 | 
570 |   if has('folding') && !s:settings.ignore_folding && foldclosed(s:lnum) > -1
571 |     "
572 |     " This is the beginning of a folded block
573 |     "
574 |     let s:new = s:numcol . foldtextresult(s:lnum)
575 |     " Go ahead and fill to the margin
576 |     let s:new = s:new . repeat(s:foldfillchar, &columns - strlen(s:new))
577 | 
578 |     let s:new = s:Format(s:new, 'Folded')
579 | 
580 |     " Skip to the end of the fold
581 |     let s:new_lnum = foldclosedend(s:lnum)
582 | 
583 |     let s:lnum = s:new_lnum
584 | 
585 |   else
586 |     "
587 |     " A line that is not folded, or doing dynamic folding.
588 |     "
589 |     let s:line = getline(s:lnum)
590 |     let s:len = strlen(s:line)
591 | 
592 |     " Now continue with the unfolded line text
593 |     if s:settings.number_lines
594 |       let s:new = s:new . s:Format(s:numcol, 'LineNr')
595 |     endif
596 | 
597 |     " initialize conceal info to act like not concealed, just in case
598 |     let s:concealinfo = [0, '']
599 | 
600 |     " Loop over each character in the line
601 |     let s:col = 1
602 | 
603 |     while s:col <= s:len
604 |       let s:startcol = s:col " The start column for processing text
605 |       if !s:settings.ignore_conceal && has('conceal')
606 |         let s:concealinfo = synconcealed(s:lnum, s:col)
607 |       endif
608 |       if !s:settings.ignore_conceal && s:concealinfo[0]
609 |         let s:col = s:col + 1
610 |         " Speed loop (it's small - that's the trick)
611 |         " Go along till we find a change in the match sequence number (ending
612 |         " the specific concealed region) or until there are no more concealed
613 |         " characters.
614 |         while s:col <= s:len && s:concealinfo == synconcealed(s:lnum, s:col) | let s:col = s:col + 1 | endwhile
615 |       else
616 |         let s:id = synID(s:lnum, s:col, 1)
617 |         let s:col = s:col + 1
618 |         " Speed loop (it's small - that's the trick)
619 |         " Go along till we find a change in synID
620 |         while s:col <= s:len && s:id == synID(s:lnum, s:col, 1) | let s:col = s:col + 1 | endwhile
621 |       endif
622 | 
623 |       if s:settings.ignore_conceal || !s:concealinfo[0]
624 |         " Expand tabs if needed
625 |         let s:expandedtab = strpart(s:line, s:startcol - 1, s:col - s:startcol)
626 |         if s:settings.expand_tabs
627 |           let s:offset = 0
628 |           let s:idx = stridx(s:expandedtab, "\t")
629 |           while s:idx >= 0
630 |             if has('multi_byte_encoding')
631 |               if s:startcol + s:idx == 1
632 |                 let s:i = &ts
633 |               else
634 |                 if s:idx == 0
635 |                   let s:prevc = matchstr(s:line, '.\%' . (s:startcol + s:idx + s:offset) . 'c')
636 |                 else
637 |                   let s:prevc = matchstr(s:expandedtab, '.\%' . (s:idx + 1) . 'c')
638 |                 endif
639 |                 let s:vcol = virtcol([s:lnum, s:startcol + s:idx + s:offset - len(s:prevc)])
640 |                 let s:i = &ts - (s:vcol % &ts)
641 |               endif
642 |               let s:offset -= s:i - 1
643 |             else
644 |               let s:i = &ts - ((s:idx + s:startcol - 1) % &ts)
645 |             endif
646 |             let s:expandedtab = substitute(s:expandedtab, '\t', repeat(' ', s:i), '')
647 |             let s:idx = stridx(s:expandedtab, "\t")
648 |           endwhile
649 |         end
650 | 
651 |         " get the highlight group name to use
652 |         let s:id = synIDtrans(s:id)
653 |         let s:id_name = synIDattr(s:id, 'name', s:whatterm)
654 |       else
655 |         " use Conceal highlighting for concealed text
656 |         let s:id_name = 'Conceal'
657 |         let s:expandedtab = s:concealinfo[1]
658 |       endif
659 | 
660 |       " Output the text with the same synID, with class set to {s:id_name},
661 |       " unless it has been concealed completely.
662 |       if strlen(s:expandedtab) > 0
663 |         let s:new = s:new . s:Format(s:expandedtab,  s:id_name)
664 |       endif
665 |     endwhile
666 |   endif
667 | 
668 |   call extend(s:lines, split(s:new, '\n', 1))
669 |   let s:lnum = s:lnum + 1
670 | endwhile
671 | 
672 | let s:lines[-1] = s:lines[-1] . s:reset_cterm_colors()
673 | let s:lines[0] = s:reset_cterm_colors() . s:screen_stop_highlight() . s:lines[0]
674 | 
675 | exe s:newwin . 'wincmd w'
676 | call setline(1, s:lines)
677 | unlet s:lines
678 | 
679 | " Restore old settings (new window first)
680 | let &l:foldenable = s:old_fen
681 | let &l:foldmethod = s:old_fdm
682 | let &report = s:old_report
683 | let &title = s:old_title
684 | let &icon = s:old_icon
685 | let &paste = s:old_paste
686 | let &magic = s:old_magic
687 | let @/ = s:old_search
688 | let &more = s:old_more
689 | 
690 | " switch to original window to restore those settings
691 | exe s:orgwin . "wincmd w"
692 | 
693 | if !s:settings.expand_tabs
694 |   let &l:isprint = s:old_isprint
695 | endif
696 | let &l:stl = s:origwin_stl
697 | let &l:et = s:old_et
698 | let &l:scrollbind = s:old_bind
699 | 
700 | " and back to the new window again to end there
701 | exe s:newwin . "wincmd w"
702 | 
703 | let &l:stl = s:newwin_stl
704 | exec 'resize' s:old_winheight
705 | let &l:winfixheight = s:old_winfixheight
706 | 
707 | let &ls=s:ls
708 | 
709 | " Save a little bit of memory (worth doing?)
710 | unlet s:old_et s:old_paste s:old_icon s:old_report s:old_title s:old_search
711 | unlet s:old_magic s:old_more s:old_fdm s:old_fen s:old_winheight
712 | unlet! s:old_isprint
713 | unlet s:whatterm s:lnum s:end s:margin s:old_winfixheight
714 | unlet! s:col s:id s:len s:line s:new s:expandedtab s:concealinfo
715 | unlet! s:orgwin s:newwin s:orgbufnr s:idx s:i s:offset s:ls s:origwin_stl
716 | unlet! s:newwin_stl s:current_syntax
717 | if !v:profiling
718 |   delfunc s:Format
719 |   delfunc s:screen_start_highlight
720 |   delfunc s:screen_stop_highlight
721 |   delfunc s:term_fg_color
722 |   delfunc s:term_bg_color
723 |   delfunc s:term_color
724 |   delfunc s:tgoto
725 | endif
726 | 
727 | unlet! s:new_lnum s:numcol s:settings
728 | 
729 | let &cpo = s:cpo_sav
730 | unlet! s:cpo_sav
731 | 
732 | unlet! s:bg_code s:cpo_sav s:fg_code s:id_name s:lines s:old_bind s:prevc s:startcol s:vcol
733 | unlet! s:normal_id s:normal_fg s:normal_bg s:normal_fg_bold s:last_fg s:last_bg s:last_bold s:last_inverse s:last_standout s:last_undercurl s:last_italic s:last_underline
734 | " Forget memoization to avoid leaking too much memory in adverse situations
735 | unlet! s:term_color_cache
736 | 
737 | " vim: ts=8 sw=2 sts=2 et
738 | 


--------------------------------------------------------------------------------