kakoune

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

pascal.kak (10648B)


      1 # https://www.freepascal.org/docs-html/ref/ref.html
      2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
      3 
      4 # Detection, see https://wiki.freepascal.org/file_types
      5 hook global BufCreate .*\.(p|pp|pas|pascal)$ %{
      6     set-option buffer filetype pascal
      7 }
      8 hook global BufCreate .*\.(dpr|dpk|dfm)$ %{
      9     set-option buffer filetype delphi
     10 }
     11 hook global BufCreate .*\.(lpr|lfm)$ %{
     12     set-option buffer filetype freepascal
     13 }
     14 
     15 hook global WinSetOption filetype=((free|object)?pascal|delphi) %[
     16     require-module pascal
     17     hook window ModeChange pop:insert:.* -group pascal-trim-indent pascal-trim-indent
     18     hook window InsertChar \n -group pascal-indent pascal-indent-on-new-line
     19     hook -once -always window WinSetOption filetype=.* %{ remove-hooks window pascal-.+ }
     20     set-option window static_words %opt{static_words}
     21 ]
     22 
     23 hook -group pascal-highlight global WinSetOption filetype=((free|object)?pascal|delphi) %[
     24     add-highlighter window/pascal ref pascal
     25     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/pascal }
     26 ]
     27 
     28 provide-module pascal %§
     29 
     30 add-highlighter shared/pascal regions
     31 add-highlighter shared/pascal/code default-region group
     32 
     33 evaluate-commands %sh¶
     34     # This might seem like a lot of effort to highlight routines and
     35     # properties correctly but it is worth it
     36 
     37     id='([_a-zA-Z][\w]{,126})\s*' # identifier for variables etc.
     38     id2="(?:$id\.)?$id(?:<.*?>)?" # (module|type).id
     39     id4="(?:$id\.)?(?:$id\.)?(?:$id\.)?$id" # type.type.type.id
     40     type=":\s*(specialize\s+)?(?:(array\s+of\s+)?$id2)" # 1:attribute 2:keyword 3:module 4:type
     41 
     42     cat <<EOF
     43         # routine without parameters
     44         add-highlighter shared/pascal/code/simple_routine regex \
     45             "\b(?i)(function|procedure|constructor|destructor|operator)\s+$id4(?:$type)?" \
     46             2:type 3:type 4:type 5:function 6:attribute 7:keyword 8:module 9:type
     47         add-highlighter shared/pascal/simple_property region \
     48             "\b(?i)property\s+$id2:"  ";" group
     49 
     50         # routine with parameters
     51         add-highlighter shared/pascal/routine region \
     52             "\b(?i)(function|procedure|constructor|destructor|operator)(\s+$id4)?\s*(<.*?>)?\s*\("  "\).*?;" regions
     53         add-highlighter shared/pascal/property region \
     54             "\b(?i)property\s+$id4\["  "\].*?;" regions
     55 
     56         add-highlighter shared/pascal/routine/parameters  region -recurse \( \( \) regions
     57         add-highlighter shared/pascal/property/parameters region -recurse \[ \[ \] regions
     58 EOF
     59 
     60     # Used to highlight "var1, var2, var3, var4 : type" declarations
     61     x="(?:$id,\s*)?"
     62 
     63     for r in property routine; do
     64         cat <<EOF
     65             add-highlighter shared/pascal/$r/parameters/default default-region group
     66             add-highlighter shared/pascal/$r/parameters/default/ regex \
     67                 "(?i)(?:(constref|const|var|out|univ)\s+)?$x$x$x$x$x$id(?:$type)?" \
     68                 1:attribute 2:variable 3:variable 4:variable 5:variable 6:variable 7:variable 8:attribute 9:keyword 10:module 11:type
     69             add-highlighter shared/pascal/$r/default default-region group
     70 EOF
     71     done
     72 
     73     cat <<EOF
     74         add-highlighter shared/pascal/routine/default/ regex \
     75             "\b(?i)(function|procedure|constructor|destructor|operator)(?:\s+$id4)?" \
     76             1:keyword 2:type 3:type 4:type 5:function
     77         add-highlighter shared/pascal/routine/default/return_type regex \
     78             "(?i)$type" 1:attribute 2:keyword 3:module 4:type
     79         add-highlighter shared/pascal/routine/default/ regex \
     80             "(?i)(of\s+object|is\s+nested)" 1:keyword
     81 EOF
     82 
     83     for r in property/default simple_property; do
     84         cat <<EOF
     85             add-highlighter shared/pascal/$r/ regex "\b(?i)(property)" 1:keyword
     86             add-highlighter shared/pascal/$r/type regex ":\s*$id" 1:type
     87 
     88             # https://www.freepascal.org/docs-html/ref/refsu33.html
     89             add-highlighter shared/pascal/$r/specifier regex \
     90                 "\b(?i)(index|read|write|implements|(no)?default|stored)\b" 0:attribute
     91 EOF
     92     done
     93 
     94     for r in pascal pascal/routine pascal/routine/parameters pascal/property; do
     95         cat <<EOF
     96             # Example string: 'You''re welcome!'
     97             add-highlighter shared/$r/string region \
     98                 -recurse %{(?<!')('')+(?!')} %{'} %{'(?!')|\$} group
     99             add-highlighter shared/$r/string/ fill string
    100             add-highlighter shared/$r/string/escape regex "''" 0:+b
    101 
    102             add-highlighter shared/$r/directive region \{\\$[a-zA-Z] \} fill meta
    103 
    104             # comments (https://www.freepascal.org/docs-html/ref/refse2.html)
    105             add-highlighter shared/$r/comment_old region -recurse \(\* \(\* \*\) fill comment
    106             add-highlighter shared/$r/comment_new region -recurse \{ \{  \} fill comment
    107             add-highlighter shared/$r/comment_oneline region // $ fill comment
    108 EOF
    109     done
    110 
    111 
    112     # The types "string" and "file", the value "nil", and the modifiers
    113     # "bitpacked" and "packed" are not included.
    114     reserved='and array as asm begin case class const constructor cppclass
    115               destructor dispinterface div do downto else end except exports
    116               finalization finally for function goto if implementation in
    117               inherited initialization interface is label library mod not
    118               object of operator or otherwise procedure program property raise
    119               record repeat resourcestring set shl shr then threadvar to try
    120               type unit until uses var while with xor'
    121 
    122     # These are not reserved words in Free Pascal, but for consistency
    123     # with other programming languages are highlighted as reserved words:
    124     keywords='Break Continue Exit at on package'
    125     # "at" is used in Delphi, e.g. 'raise object at address'
    126 
    127     # https://www.freepascal.org/docs-html/ref/refsu3.html and
    128     # https://wiki.freepascal.org/Reserved_words
    129     # "name" and "alias" are not included beacuse they produce too many
    130     # false positives. Some modifiers like "index" are only highlighted in
    131     # certain places.
    132     modifiers='absolute abstract assembler automated bitpacked cdecl contains
    133                cppdecl cvar default deprecated dispid dynamic enumerator
    134                experimental export external far far16 final forward generic
    135                helper inline interrupt iocheck local near noreturn
    136                nostackframe oldfpccall overload override packed pascal
    137                platform private protected public published readonly register
    138                reintroduce requires safecall saveregisters softfloat specialize
    139                static stdcall strict syscall unaligned unimplemented varargs
    140                virtual winapi writeonly'
    141 
    142     # https://wiki.freepascal.org/Category:Data_types
    143     # not highlighted for consistency with not built-in types
    144     types='AnsiChar AnsiString Boolean Boolean16 Boolean32 Boolean64 Buffer
    145            Byte ByteBool Cardinal Char Comp Currency Double DWord Extended
    146            Int16 Int32 Int64 Int8 Integer IUnknown LongBool LongInt Longword
    147            NativeInt NativeUInt OleVariant PAnsiChar PAnsiString PBoolean PByte
    148            PByteArray PCardinal PChar PComp PCurrency PDate PDateTime PDouble
    149            PDWord PExtended PHandle PInt64 PInteger PLongInt PLongWord Pointer
    150            PPointer PQWord PShortInt PShortString PSingle PSmallInt PString
    151            PUnicodeChar PUnicodeString PVariant PWideChar PWideString PWord
    152            PWordArray PWordBool QWord QWordBool RawBytestring Real Real48
    153            ShortInt ShortString Single SmallInt TClass TDate TDateTime Text
    154            TextFile THandle TObject TTime UInt16 UInt32 UInt8 UnicodeChar
    155            UnicodeString UTF16String UTF8String Variant WideChar WideString
    156            Word WordBool file string'
    157 
    158     constants='False True nil MaxInt Self'
    159 
    160     # Add the language's grammar to the static completion list
    161     echo declare-option str-list static_words $reserved $keywords $modifiers \
    162         $types $constants name index result constref out read write implements \
    163         nodefault stored message
    164 
    165     # Replace spaces with a pipe
    166     join() { eval set -- $1; IFS='|'; echo "$*"; }
    167 
    168     cat <<EOF
    169         add-highlighter shared/pascal/code/keywords  regex \
    170             (?i)(?<!&)\b($(join "$reserved $keywords")|file\s+of)\b 0:keyword
    171         add-highlighter shared/pascal/code/modifiers regex \
    172             (?i)(?<!\.)\b($(join "$modifiers"))\b(?!\()|message\s+(?!:) 0:attribute
    173         add-highlighter shared/pascal/code/index regex \
    174             '\b(?i)(index)\s+\w+\s*;' 1:attribute
    175 EOF
    176 
    177     for r in code routine/parameters/default routine/default property/default simple_property; do
    178         cat <<EOF
    179             add-highlighter shared/pascal/$r/ regex '[.:=<>@^*/+-]' 0:operator
    180             add-highlighter shared/pascal/$r/constants regex \
    181                 \b(?i)($(join "$constants"))\b 0:value
    182 
    183             # numbers (https://www.freepascal.org/docs-html/ref/refse6.html)
    184             add-highlighter shared/pascal/$r/decimal regex \b\d+([eE][+-]?\d+)?\b 0:value
    185             add-highlighter shared/pascal/$r/hex     regex \\\$[\da-fA-F]+\b 0:value
    186             add-highlighter shared/pascal/$r/octal   regex &[0-7]+\b 0:value
    187             add-highlighter shared/pascal/$r/binary  regex \%[01]+\b 0:value
    188             add-highlighter shared/pascal/$r/char    regex '#\d+\b' 0:value
    189 EOF
    190     done
    191    192 
    193 define-command -hidden pascal-trim-indent %{
    194     evaluate-commands -no-hooks -draft -itersel %{
    195         execute-keys x
    196         # remove trailing white spaces
    197         try %{ execute-keys -draft s \h + $ <ret> d }
    198     }
    199 }
    200 
    201 define-command -hidden pascal-indent-on-new-line %{
    202     evaluate-commands -no-hooks -draft -itersel %{
    203         # preserve previous line indent
    204         try %{ execute-keys -draft <semicolon> K <a-&> }
    205         # cleanup trailing whitespaces from previous line
    206         try %{ execute-keys -draft k x s \h+$ <ret> d }
    207         # indent after certain keywords
    208         try %{ execute-keys -draft kx<a-k>(?i)(asm|begin|const|else|except|exports|finalization|finally|label|of|otherwise|private|property|public|protected|published|record|repeat|resourcestring|threadvar|try|type|uses|var|:)\h*$<ret>j<a-gt> }
    209     }
    210 }
    211 §
    212 
    213 # Other syntax highlighters for reference:
    214 # https://github.com/pygments/pygments/blob/master/pygments/lexers/pascal.py
    215 # https://github.com/codemirror/CodeMirror/blob/master/mode/pascal/pascal.js
    216 # https://github.com/vim/vim/blob/master/runtime/syntax/pascal.vim
    217 # https://github.com/highlightjs/highlight.js/blob/master/src/languages/delphi.js