Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Vim syntax file |
| 2 | " Language: X Pixmap v2 |
| 3 | " Maintainer: Steve Wall (hitched97@velnet.com) |
Bram Moolenaar | 3df0173 | 2017-02-17 22:47:16 +0100 | [diff] [blame] | 4 | " Last Change: 2017 Feb 01 |
Bram Moolenaar | c873442 | 2012-06-01 22:38:45 +0200 | [diff] [blame] | 5 | " (Dominique Pelle added @Spell) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6 | " Version: 5.8 |
Bram Moolenaar | 3df0173 | 2017-02-17 22:47:16 +0100 | [diff] [blame] | 7 | " Jemma Nelson added termguicolors support |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8 | " |
| 9 | " Made from xpm.vim by Ronald Schild <rs@scutum.de> |
| 10 | |
Bram Moolenaar | 89bcfda | 2016-08-30 23:26:57 +0200 | [diff] [blame] | 11 | " quit when a syntax file was already loaded |
| 12 | if exists("b:current_syntax") |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 13 | finish |
| 14 | endif |
| 15 | |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 16 | let s:cpo_save = &cpo |
| 17 | set cpo&vim |
| 18 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 19 | syn region xpm2PixelString start="^" end="$" contains=@xpm2Colors |
| 20 | syn keyword xpm2Todo TODO FIXME XXX contained |
Bram Moolenaar | c873442 | 2012-06-01 22:38:45 +0200 | [diff] [blame] | 21 | syn match xpm2Comment "\!.*$" contains=@Spell,xpm2Todo |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 22 | |
| 23 | |
Bram Moolenaar | 89bcfda | 2016-08-30 23:26:57 +0200 | [diff] [blame] | 24 | command -nargs=+ Hi hi def <args> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 25 | |
Bram Moolenaar | 3df0173 | 2017-02-17 22:47:16 +0100 | [diff] [blame] | 26 | if has("gui_running") || has("termguicolors") && &termguicolors |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 27 | |
| 28 | let color = "" |
| 29 | let chars = "" |
| 30 | let colors = 0 |
| 31 | let cpp = 0 |
| 32 | let n = 0 |
| 33 | let i = 1 |
| 34 | |
| 35 | while i <= line("$") " scanning all lines |
| 36 | |
| 37 | let s = getline(i) |
| 38 | if match(s,"\!.*$") != -1 |
| 39 | let s = matchstr(s, "^[^\!]*") |
| 40 | endif |
| 41 | if s != "" " does line contain a string? |
| 42 | |
| 43 | if n == 0 " first string is the Values string |
| 44 | |
| 45 | " get the 3rd value: colors = number of colors |
| 46 | let colors = substitute(s, '\s*\d\+\s\+\d\+\s\+\(\d\+\).*', '\1', '') |
| 47 | " get the 4th value: cpp = number of character per pixel |
| 48 | let cpp = substitute(s, '\s*\d\+\s\+\d\+\s\+\d\+\s\+\(\d\+\).*', '\1', '') |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 49 | if cpp =~ '[^0-9]' |
| 50 | break " if cpp is not made of digits there must be something wrong |
| 51 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 52 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 53 | " Highlight the Values string as normal string (no pixel string). |
| 54 | " Only when there is no slash, it would terminate the pattern. |
| 55 | if s !~ '/' |
| 56 | exe 'syn match xpm2Values /' . s . '/' |
| 57 | endif |
Bram Moolenaar | f37506f | 2016-08-31 22:22:10 +0200 | [diff] [blame] | 58 | hi def link xpm2Values Statement |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 59 | |
| 60 | let n = 1 " n = color index |
| 61 | |
| 62 | elseif n <= colors " string is a color specification |
| 63 | |
| 64 | " get chars = <cpp> length string representing the pixels |
| 65 | " (first incl. the following whitespace) |
| 66 | let chars = substitute(s, '\(.\{'.cpp.'}\s\+\).*', '\1', '') |
| 67 | |
| 68 | " now get color, first try 'c' key if any (color visual) |
| 69 | let color = substitute(s, '.*\sc\s\+\(.\{-}\)\s*\(\(g4\=\|[ms]\)\s.*\)*\s*', '\1', '') |
| 70 | if color == s |
| 71 | " no 'c' key, try 'g' key (grayscale with more than 4 levels) |
| 72 | let color = substitute(s, '.*\sg\s\+\(.\{-}\)\s*\(\(g4\|[ms]\)\s.*\)*\s*', '\1', '') |
| 73 | if color == s |
| 74 | " next try: 'g4' key (4-level grayscale) |
| 75 | let color = substitute(s, '.*\sg4\s\+\(.\{-}\)\s*\([ms]\s.*\)*\s*', '\1', '') |
| 76 | if color == s |
| 77 | " finally try 'm' key (mono visual) |
| 78 | let color = substitute(s, '.*\sm\s\+\(.\{-}\)\s*\(s\s.*\)*\s*', '\1', '') |
| 79 | if color == s |
| 80 | let color = "" |
| 81 | endif |
| 82 | endif |
| 83 | endif |
| 84 | endif |
| 85 | |
| 86 | " Vim cannot handle RGB codes with more than 6 hex digits |
| 87 | if color =~ '#\x\{10,}$' |
| 88 | let color = substitute(color, '\(\x\x\)\x\x', '\1', 'g') |
| 89 | elseif color =~ '#\x\{7,}$' |
| 90 | let color = substitute(color, '\(\x\x\)\x', '\1', 'g') |
| 91 | " nor with 3 digits |
| 92 | elseif color =~ '#\x\{3}$' |
| 93 | let color = substitute(color, '\(\x\)\(\x\)\(\x\)', '0\10\20\3', '') |
| 94 | endif |
| 95 | |
| 96 | " escape meta characters in patterns |
| 97 | let s = escape(s, '/\*^$.~[]') |
| 98 | let chars = escape(chars, '/\*^$.~[]') |
| 99 | |
| 100 | " change whitespace to "\s\+" |
| 101 | let s = substitute(s, "[ \t][ \t]*", "\\\\s\\\\+", "g") |
| 102 | let chars = substitute(chars, "[ \t][ \t]*", "\\\\s\\\\+", "g") |
| 103 | |
| 104 | " now create syntax items |
| 105 | " highlight the color string as normal string (no pixel string) |
| 106 | exe 'syn match xpm2Col'.n.'Def /'.s.'/ contains=xpm2Col'.n.'inDef' |
Bram Moolenaar | f37506f | 2016-08-31 22:22:10 +0200 | [diff] [blame] | 107 | exe 'hi def link xpm2Col'.n.'Def Constant' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 108 | |
| 109 | " but highlight the first whitespace after chars in its color |
| 110 | exe 'syn match xpm2Col'.n.'inDef /^'.chars.'/hs=s+'.(cpp).' contained' |
Bram Moolenaar | f37506f | 2016-08-31 22:22:10 +0200 | [diff] [blame] | 111 | exe 'hi def link xpm2Col'.n.'inDef xpm2Color'.n |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 112 | |
| 113 | " remove the following whitespace from chars |
| 114 | let chars = substitute(chars, '\\s\\+$', '', '') |
| 115 | |
| 116 | " and create the syntax item contained in the pixel strings |
| 117 | exe 'syn match xpm2Color'.n.' /'.chars.'/ contained' |
| 118 | exe 'syn cluster xpm2Colors add=xpm2Color'.n |
| 119 | |
| 120 | " if no color or color = "None" show background |
| 121 | if color == "" || substitute(color, '.*', '\L&', '') == 'none' |
| 122 | exe 'Hi xpm2Color'.n.' guifg=bg guibg=NONE' |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 123 | elseif color !~ "'" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 124 | exe 'Hi xpm2Color'.n." guifg='".color."' guibg='".color."'" |
| 125 | endif |
| 126 | let n = n + 1 |
| 127 | else |
| 128 | break " no more color string |
| 129 | endif |
| 130 | endif |
| 131 | let i = i + 1 |
| 132 | endwhile |
| 133 | |
| 134 | unlet color chars colors cpp n i s |
| 135 | |
Bram Moolenaar | 3df0173 | 2017-02-17 22:47:16 +0100 | [diff] [blame] | 136 | endif " has("gui_running") || has("termguicolors") && &termguicolors |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 137 | |
| 138 | " Define the default highlighting. |
Bram Moolenaar | 89bcfda | 2016-08-30 23:26:57 +0200 | [diff] [blame] | 139 | " Only when an item doesn't have highlighting yet |
| 140 | " The default highlighting. |
Bram Moolenaar | f37506f | 2016-08-31 22:22:10 +0200 | [diff] [blame] | 141 | hi def link xpm2Type Type |
| 142 | hi def link xpm2StorageClass StorageClass |
| 143 | hi def link xpm2Todo Todo |
| 144 | hi def link xpm2Comment Comment |
| 145 | hi def link xpm2PixelString String |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 146 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 147 | delcommand Hi |
| 148 | |
| 149 | let b:current_syntax = "xpm2" |
| 150 | |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 151 | let &cpo = s:cpo_save |
| 152 | unlet s:cpo_save |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 153 | " vim: ts=8:sw=2:noet: |