kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

css.kak (7852B)


      1 # http://w3.org/Style/CSS
      2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
      3 
      4 # Detection
      5 # ‾‾‾‾‾‾‾‾‾
      6 
      7 hook global BufCreate .*[.](css) %{
      8     set-option buffer filetype css
      9 }
     10 
     11 # Initialization
     12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     13 
     14 hook global WinSetOption filetype=css %[
     15     require-module css
     16 
     17     hook window ModeChange pop:insert:.* -group css-trim-indent css-trim-indent
     18     hook window InsertChar \n -group css-insert css-insert-on-new-line
     19     hook window InsertChar \n -group css-indent css-indent-on-new-line
     20     hook window InsertChar \} -group css-indent css-indent-on-closing-curly-brace
     21     set-option buffer extra_word_chars '_' '-'
     22     set-face global cssLogicalOperator +i@keyword
     23 
     24     hook -once -always window WinSetOption filetype=.* %{ remove-hooks window css-.+ }
     25 ]
     26 
     27 hook -group css-highlight global WinSetOption filetype=css %{
     28     add-highlighter window/css ref css
     29     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/css }
     30 }
     31 
     32 
     33 provide-module css %[
     34 
     35 # Highlighters
     36 # ‾‾‾‾‾‾‾‾‾‾‾‾
     37 add-highlighter shared/css regions
     38 add-highlighter shared/css/code default-region group
     39 add-highlighter shared/css/attr_selector region \[ \] regions
     40 add-highlighter shared/css/double_string region '"'  (?<!\\)(\\\\)*" fill string
     41 add-highlighter shared/css/single_string region "'"  (?<!\\)(\\\\)*' fill string
     42 add-highlighter shared/css/comment    region /\* \*/ fill comment
     43 
     44 evaluate-commands %sh{
     45   # html tags
     46   # generated from the URL & <code> below
     47   # includes elements that cannot be styled, which is fine.
     48   # 
     49   # https://developer.mozilla.org/en-US/docs/Web/HTML/Element
     50   html_tags='html body address article aside footer header h1 h2 h3 h4 h5 h6 main nav section blockquote dd div dl dt figcaption figure hr li ol p pre ul a abbr b bdi bdo br cite code data dfn em i kbd mark q rp rt ruby s samp small span strong sub sup time u var wbr area audio img map track video embed iframe object param picture portal source canvas noscript script del ins caption col colgroup table tbody td tfoot th thead tr button datalist fieldset form input label legend meter optgroup option output progress select textarea details dialog menu summary slot template acronym applet basefont bgsound big blink center content dir font frame frameset hgroup image keygen marquee menuitem nobr noembed noframes plaintext rb rtc shadow spacer strike tt xmp'
     51 
     52   # Units
     53   # ‾‾‾‾‾
     54   # generated from the URL & <code> below
     55   # includes #rgb, #rrggbb, #rrggbbaa as color values {3,8}
     56   # 
     57   # https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Values_and_Units
     58   units='% cap ch cm deg dpcm dpi dppx em ex grad Hz ic in kHz lh mm ms pc pt px Q rad rem rlh s turn vb vh vi vmax vmin vw x'
     59 
     60   logical_ops='and not only from to'
     61   keywords='!important auto inherit initial unset none'
     62   media_types='all print screen speech'
     63 
     64   # easing_re='linear|ease(-(in-out|in|out))?|step-(start|end)'
     65 
     66   # combinators='+ > ~ ||'
     67   # attribute_op='= ~= |= ^= $= *='
     68 
     69   join() { eval set -- $1; IFS="|"; echo "$*"; }
     70 
     71   # Selectors
     72   # ‾‾‾‾‾‾‾‾‾
     73   # universal: *, ns|*, *|*, |*
     74   # class/id: .class, #id
     75   # type: element
     76   # attr: [attr=val]
     77 
     78   # order below matters
     79   printf %s "
     80   add-highlighter shared/css/code/tag_selectors regex \b($(join "${html_tags}"))((:[a-z:])|[\h.#,]) 1:keyword
     81 
     82   add-highlighter shared/css/code/functional_notation regex ([a-zA-Z0-9-_]+[a-zA-Z0-9])\( 1:keyword
     83 
     84   add-highlighter shared/css/code/logical_operators regex (\b($(join "${logical_ops}"))\b|$(join "${keywords}")) 1:cssLogicalOperator
     85 
     86   add-highlighter shared/css/code/media_types regex \b($(join "${media_types}"))\b 1:+i
     87 
     88   # (after functional notation as they may contain paranthesis)
     89   add-highlighter shared/css/code/pseudo regex (:{1,2})([a-z-]+) 0:attribute
     90 
     91   add-highlighter shared/css/code/at_rules regex @[a-z-]+ 0:function 
     92 
     93   add-highlighter shared/css/code/css_property regex ([A-Za-z][A-Za-z0-9_-]*)\h*:\h 1:operator 1:+a
     94 
     95   add-highlighter shared/css/code/selectors regex (\*|[*]?[.][A-Za-z][A-Za-z0-9_-]+) 0:type
     96   add-highlighter shared/css/code/selectors_id regex (\*|[*]?[#][A-Za-z][A-Za-z0-9_-]+) 0:type 0:+i
     97 
     98   add-highlighter shared/css/code/hex_values regex (#[0-9A-Fa-f]{3,8})\b 0:value 0:+a
     99   add-highlighter shared/css/code/units regex \b(\d*\.)?\d+($(join "${units}"))?\b 0:value 0:+a
    100   "
    101 }
    102 
    103 # Attribute Selectors
    104 add-highlighter shared/css/attr_selector/base default-region group
    105 add-highlighter shared/css/attr_selector/base/ regex ([a-zA-Z0-9-]+) 1:attribute
    106 add-highlighter shared/css/attr_selector/base/ regex \h(i|s) 1:type
    107 add-highlighter shared/css/attr_selector/double_string region '"'  (?<!\\)(\\\\)*" fill string
    108 add-highlighter shared/css/attr_selector/single_string region "'"  (?<!\\)(\\\\)*' fill string
    109 
    110 # Commands
    111 # ‾‾‾‾‾‾‾‾
    112 
    113 define-command -hidden css-trim-indent %{
    114     # remove trailing white spaces
    115     try %{ execute-keys -draft -itersel x s \h+$ <ret> d }
    116 }
    117 
    118 define-command -hidden css-indent-on-new-line %[
    119     evaluate-commands -draft -itersel %[
    120         execute-keys <semicolon>
    121         try %<
    122             # if previous line is part of a comment, do nothing
    123             execute-keys -draft <a-?>/\*<ret> <a-K>^\h*[^/*\h]<ret>
    124         > catch %<
    125             # else if previous line closed a paren (possibly followed by words and a comment),
    126             # copy indent of the opening paren line
    127             execute-keys -draft kx 1s(\))(\h+\w+)*\h*(\;\h*)?(?://\N+)?\n\z<ret> m<a-semicolon>J <a-S> 1<a-&>
    128         > catch %<
    129             # else indent new lines with the same level as the previous one
    130             execute-keys -draft K <a-&>
    131         >
    132         # filter previous line
    133         try %< execute-keys -draft k x <a-k>^\h+$<ret> Hd >
    134         # indent after lines ending with with {
    135         try %[ execute-keys -draft k x <a-k> \{$ <ret> j <a-gt> ]
    136         # deindent closing brace when after cursor
    137         try %[ execute-keys -draft x <a-k> ^\h*\} <ret> gh / \} <ret> m <a-S> 1<a-&> ]
    138     ]
    139 ]
    140 
    141 define-command -hidden css-insert-on-new-line %[
    142     evaluate-commands -draft -itersel %<
    143     execute-keys <semicolon>
    144     try %[
    145         # if the previous line isn't within a comment scope, break
    146         execute-keys -draft kx <a-k>^(\h*/\*|\h+\*(?!/))<ret>
    147 
    148         # find comment opening, validate it was not closed, and check its using star prefixes
    149         execute-keys -draft <a-?>/\*<ret><a-H> <a-K>\*/<ret> <a-k>\A\h*/\*(\N*\n\h*\*)*\N*\n\h*.\z<ret>
    150 
    151         try %[
    152             # if the previous line is opening the comment, insert star preceeded by space
    153             execute-keys -draft kx<a-k>^\h*/\*<ret>
    154             execute-keys -draft i*<space><esc>
    155         ] catch %[
    156            try %[
    157                 # if the next line is a comment line insert a star
    158                 execute-keys -draft jx<a-k>^\h+\*<ret>
    159                 execute-keys -draft i*<space><esc>
    160             ] catch %[
    161                 try %[
    162                     # if the previous line is an empty comment line, close the comment scope
    163                     execute-keys -draft kx<a-k>^\h+\*\h+$<ret> x1s\*(\h*)<ret>c/<esc>
    164                 ] catch %[
    165                     # if the previous line is a non-empty comment line, add a star
    166                     execute-keys -draft i*<space><esc>
    167                 ]
    168             ]
    169         ]
    170 
    171         # trim trailing whitespace on the previous line
    172         try %[ execute-keys -draft s\h+$<ret> d ]
    173         # align the new star with the previous one
    174         execute-keys Kx1s^[^*]*(\*)<ret>&
    175     ]
    176     >
    177 ]
    178 
    179 define-command -hidden css-indent-on-closing-curly-brace %[
    180     evaluate-commands -draft -itersel %[
    181         # align to opening curly brace when alone on a line
    182         try %[ execute-keys -draft <a-h> <a-k> ^\h+\}$ <ret> m s \A|.\z <ret> 1<a-&> ]
    183     ]
    184 ]
    185 
    186 ]