kakoune

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

php.kak (4969B)


      1 # Detection
      2 # ‾‾‾‾‾‾‾‾‾
      3 
      4 hook global BufCreate .*[.](phpt?) %{
      5     set-option buffer filetype php
      6 }
      7 
      8 # Initialization
      9 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     10 
     11 hook global WinSetOption filetype=php %{
     12     require-module php
     13 
     14     hook window ModeChange pop:insert:.* -group php-trim-indent php-trim-indent
     15     hook window InsertChar .* -group php-indent php-indent-on-char
     16     hook window InsertChar \n -group php-insert php-insert-on-new-line
     17     hook window InsertChar \n -group php-indent php-indent-on-new-line
     18 
     19     hook -once -always window WinSetOption filetype=.* %{ remove-hooks window php-.+ }
     20 }
     21 
     22 hook -group php-highlight global WinSetOption filetype=php %{
     23     add-highlighter window/php-file ref php-file
     24     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/php-file }
     25 }
     26 
     27 provide-module php %§
     28 require-module html
     29 
     30 # Highlighters
     31 # ‾‾‾‾‾‾‾‾‾‾‾‾
     32 
     33 add-highlighter shared/php regions
     34 add-highlighter shared/php/code  default-region group
     35 add-highlighter shared/php/double_string region '"'    (?<!\\)(\\\\)*" group
     36 add-highlighter shared/php/single_string region "'"    (?<!\\)(\\\\)*' fill string
     37 add-highlighter shared/php/doc_comment   region ///    '$'             group
     38 add-highlighter shared/php/doc_comment2  region /\*\*  \*/             ref php/doc_comment
     39 add-highlighter shared/php/comment1      region //     '$'             fill comment
     40 add-highlighter shared/php/comment2      region /\*    \*/             fill comment
     41 add-highlighter shared/php/comment3      region '#'    '$'             fill comment
     42 add-highlighter shared/php/heredoc       region -match-capture '<<<''?(.*?)''?$' '^\h*(.*?);?$' fill string
     43 
     44 
     45 add-highlighter shared/php/code/ regex &?\$\w* 0:variable
     46 add-highlighter shared/php/code/ regex \b(false|null|parent|self|this|true)\b 0:value
     47 add-highlighter shared/php/code/ regex "(\b|-)[0-9]*\.?[0-9]+\b" 0:value
     48 add-highlighter shared/php/code/ regex \b((string|int|bool)|[A-Z][a-z].*?)\b 0:type
     49 add-highlighter shared/php/code/ regex \B/[^\n/]+/[gimy]* 0:meta
     50 add-highlighter shared/php/code/ regex '<\?(php)?|\?>' 0:meta
     51 
     52 add-highlighter shared/php/double_string/ fill string
     53 add-highlighter shared/php/double_string/ regex (?<!\\)(\\\\)*(\$\w+)(->\w+)* 0:variable
     54 add-highlighter shared/php/double_string/ regex \{(?<!\\)(\\\\)*(\$\w+)(->\w+)*\} 0:variable
     55 
     56 # Highlight doc comments
     57 add-highlighter shared/php/doc_comment/ fill string
     58 add-highlighter shared/php/doc_comment/ regex '`.*`' 0:module
     59 add-highlighter shared/php/doc_comment/ regex '@\w+' 0:meta
     60 
     61 # Keywords are collected at
     62 # http://php.net/manual/en/reserved.keywords.php
     63 add-highlighter shared/php/code/ regex \b(__halt_compiler|abstract|and|array|as|break|callable|case|catch|class|clone|const|continue|declare|default|die|do|echo|else|elseif|empty|enddeclare|endfor|endforeach|endif|endswitch|endwhile|eval|exit|extends|final|finally|for|foreach|function|global|goto|if|implements|include|include_once|instanceof|insteadof|interface|isset|list|namespace|new|or|print|private|protected|public|require|require_once|return|static|switch|throw|trait|try|unset|use|var|while|xor|yield|__CLASS__|__DIR__|__FILE__|__FUNCTION__|__LINE__|__METHOD__|__NAMESPACE__|__TRAIT__)\b 0:keyword
     64 
     65 # Highlighter for html with php tags in it, i.e. the structure of conventional php files
     66 add-highlighter shared/php-file regions
     67 add-highlighter shared/php-file/html default-region ref html
     68 add-highlighter shared/php-file/php  region '<\?(php)?'     '\?>'      ref php
     69 
     70 # Commands
     71 # ‾‾‾‾‾‾‾‾
     72 
     73 define-command -hidden php-trim-indent %{
     74     # remove trailing white spaces
     75     try %{ execute-keys -draft -itersel x s \h+$ <ret> d }
     76 }
     77 
     78 define-command -hidden php-indent-on-char %<
     79     evaluate-commands -draft -itersel %<
     80         # align closer token to its opener when alone on a line
     81         try %/ execute-keys -draft <a-h> <a-k> ^\h+[\]}]$ <ret> m s \A|.\z <ret> 1<a-&> /
     82     >
     83 >
     84 
     85 define-command -hidden php-insert-on-new-line %<
     86     evaluate-commands -draft -itersel %<
     87         # copy // comments or docblock * prefix and following white spaces
     88         try %{ execute-keys -draft s [^/] <ret> k x s ^\h*\K(?://|[*][^/])\h* <ret> y gh j P }
     89         # append " * " on lines starting a multiline /** or /* comment
     90         try %{ execute-keys -draft k x s ^\h*/[*][* ]? <ret> j gi i <space>*<space> }
     91     >
     92 >
     93 
     94 define-command -hidden php-indent-on-new-line %<
     95     evaluate-commands -draft -itersel %<
     96         # preserve previous line indent
     97         try %{ execute-keys -draft <semicolon> K <a-&> }
     98         # filter previous line
     99         try %{ execute-keys -draft k : php-trim-indent <ret> }
    100         # indent after lines beginning / ending with opener token
    101         try %_ execute-keys -draft k x <a-k> ^\h*[[{]|[[{]$ <ret> j <a-gt> _
    102         # deindent closer token(s) when after cursor
    103         try %_ execute-keys -draft x <a-k> ^\h*[})] <ret> gh / [})] <ret> m <a-S> 1<a-&> _
    104     >
    105 >
    106 
    107 §