1# http://dlang.org/
2#
3
4# Detection
5# ‾‾‾‾‾‾‾‾‾
6
7hook global BufCreate .*\.di? %{
8    set-option buffer filetype d
9}
10
11# Initialization
12# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
13
14hook global WinSetOption filetype=d %{
15    require-module d
16
17    set-option window static_words %opt{d_static_words}
18
19    # cleanup trailing whitespaces when exiting insert mode
20    hook window ModeChange pop:insert:.* -group d-trim-indent %{ try %{ execute-keys -draft <a-x>s^\h+$<ret>d } }
21    hook window InsertChar \n -group d-insert d-insert-on-new-line
22    hook window InsertChar \n -group d-indent d-indent-on-new-line
23    hook window InsertChar \{ -group d-indent d-indent-on-opening-curly-brace
24    hook window InsertChar \} -group d-indent d-indent-on-closing-curly-brace
25
26    hook -once -always window WinSetOption filetype=.* %{ remove-hooks window d-.+ }
27}
28
29hook -group d-highlight global WinSetOption filetype=d %{
30    add-highlighter window/d ref d
31    hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/d }
32}
33
34provide-module d %§
35
36# Highlighters
37# ‾‾‾‾‾‾‾‾‾‾‾‾
38
39add-highlighter shared/d regions
40add-highlighter shared/d/code default-region group
41add-highlighter shared/d/string region %{(?<!')(?<!'\\)"} %{(?<!\\)(?:\\\\)*"} group
42add-highlighter shared/d/verbatim_string region %{(?<!')(?<!'\\)`} %{(?<!\\)(?:\\\\)*`} fill meta
43add-highlighter shared/d/verbatim_string_prefixed region %{r`([^(]*)\(} %{\)([^)]*)`} fill meta
44add-highlighter shared/d/docstring1 region -recurse '/\+' '/\+\+' '\+/' fill documentation
45add-highlighter shared/d/docstring2 region '/\*\*' '\*/' fill documentation
46add-highlighter shared/d/docstring3 region /// $ fill documentation
47add-highlighter shared/d/disabled region -recurse '/\+' '/\+[^+]?' '\+/' fill comment
48add-highlighter shared/d/comment1 region '/\*[^*]?' '\*/' fill comment
49add-highlighter shared/d/comment2 region '//[^/]?' $ fill comment
50
51add-highlighter shared/d/string/ fill string
52add-highlighter shared/d/string/ regex %{\\(x[0-9a-fA-F]{2}|[0-7]{1,3}|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8})\b} 0:value
53add-highlighter shared/d/code/ regex %{'((\\.)?|[^'\\])'} 0:value
54add-highlighter shared/d/code/ regex "-?([0-9_]*\.(?!0[xXbB]))?\b([0-9_]+|0[xX][0-9a-fA-F_]*\.?[0-9a-fA-F_]+|0[bb][01_]+)([ep]-?[0-9_]+)?[fFlLuUi]*\b" 0:value
55add-highlighter shared/d/code/ regex "\b(this)\b\s*[^(]" 1:value
56add-highlighter shared/d/code/ regex "((?:~|\b)this)\b\s*\(" 1:function
57add-highlighter shared/d/code/ regex '(#line)\h+(\d+)(\h+"[^"\n]*")?' 1:meta 2:value 3:string
58
59evaluate-commands %sh{
60    # Grammar
61
62    keywords="abstract|alias|align|asm|assert|auto|body|break|case|cast"
63    keywords="${keywords}|catch|cent|class|const|continue|debug"
64    keywords="${keywords}|default|delegate|delete|deprecated|do|else|enum|export|extern"
65    keywords="${keywords}|final|finally|for|foreach|foreach_reverse|function|goto"
66    keywords="${keywords}|if|immutable|import|in|inout|interface|invariant"
67    keywords="${keywords}|is|lazy|macro|mixin|module|new|nothrow|out|override"
68    keywords="${keywords}|package|pragma|private|protected|public|pure|ref|return|scope"
69    keywords="${keywords}|shared|static|struct|super|switch|synchronized|template"
70    keywords="${keywords}|throw|try|typedef|typeid|typeof|union"
71    keywords="${keywords}|unittest|version|volatile|while|with"
72    attributes="abstract|align|auto|const|debug|deprecated|export|extern|final"
73    attributes="${attributes}|immutable|inout|nothrow|package|private|protected"
74    attributes="${attributes}|public|pure|ref|override|scope|shared|static|synchronized|version"
75    attributes="${attributes}|__gshared|__traits|__vector|__parameters"
76    types="bool|byte|cdouble|cent|cfloat|char|creal|dchar|double|dstring|float"
77    types="${types}|idouble|ifloat|int|ireal|long|ptrdiff_t|real|size_t|short"
78    types="${types}|string|ubyte|ucent|uint|ulong|ushort|void|wchar|wstring"
79    values="true|false|null"
80    tokens="__FILE__|__MODULE__|__LINE__|__FUNCTION__"
81    tokens="${tokens}|__PRETTY_FUNCTION__|__DATE__|__EOF__|__TIME__"
82    tokens="${tokens}|__TIMESTAMP__|__VENDOR__|__VERSION__|#line"
83    properties="this|init|sizeof|alignof|mangleof|stringof|infinity|nan|dig|epsilon|mant_dig"
84    properties="${properties}|max_10_exp|min_exp|max|min_normal|re|im|classinfo"
85    properties="${properties}|length|dup|keys|values|rehash|clear"
86    decorators="disable|property|nogc|safe|trusted|system"
87
88    # Add the language's grammar to the static completion list
89    printf %s\\n "declare-option str-list d_static_words ${keywords} ${attributes} ${types} ${values} ${decorators} ${properties}" | tr '|' ' '
90
91    # Highlight keywords
92    printf %s "
93        add-highlighter shared/d/code/ regex \b(${keywords})\b 0:keyword
94        add-highlighter shared/d/code/ regex \b(${attributes})\b 0:attribute
95        add-highlighter shared/d/code/ regex \b(${types})\b 0:type
96        add-highlighter shared/d/code/ regex \b(${values})\b 0:value
97        add-highlighter shared/d/code/ regex @(${decorators})\b 0:attribute
98        add-highlighter shared/d/code/ regex \b(${tokens})\b 0:builtin
99        add-highlighter shared/d/code/ regex \.(${properties})\b 1:builtin
100    "
101}
102
103add-highlighter shared/d/code/ regex "\bimport\s+([\w._-]+)(?:\s*=\s*([\w._-]+))?" 1:module 2:module
104add-highlighter shared/d/code/ regex "\bmodule\s+([\w_-]+)\b" 1:module
105
106# Commands
107# ‾‾‾‾‾‾‾‾
108
109define-command -hidden d-insert-on-new-line %~
110    evaluate-commands -draft -itersel %=
111        # copy // comments prefix and following white spaces
112        try %{ execute-keys -draft <semicolon><c-s>k<a-x> s ^\h*\K/{2,}\h* <ret> y<c-o>P<esc> }
113    =
114~
115
116define-command -hidden d-indent-on-new-line %~
117    evaluate-commands -draft -itersel %=
118        # preserve previous line indent
119        try %{ execute-keys -draft <semicolon>K<a-&> }
120        # indent after lines ending with { or (
121        try %[ execute-keys -draft k<a-x> <a-k> [{(]\h*$ <ret> j<a-gt> ]
122        # cleanup trailing white spaces on the previous line
123        try %{ execute-keys -draft k<a-x> s \h+$ <ret>d }
124        # align to opening paren of previous line
125        try %{ execute-keys -draft [( <a-k> \A\([^\n]+\n[^\n]*\n?\z <ret> s \A\(\h*.|.\z <ret> '<a-;>' & }
126        # indent after a switch's case/default statements
127        try %[ execute-keys -draft k<a-x> <a-k> ^\h*(case|default).*:$ <ret> j<a-gt> ]
128        # indent after if|else|while|for
129        try %[ execute-keys -draft <semicolon><a-F>)MB <a-k> \A(if|else|while|for)\h*\(.*\)\h*\n\h*\n?\z <ret> s \A|.\z <ret> 1<a-&>1<a-space><a-gt> ]
130        # deindent closing brace(s) when after cursor
131        try %[ execute-keys -draft <a-x> <a-k> ^\h*[})] <ret> gh / [})] <ret> m <a-S> 1<a-&> ]
132    =
133~
134
135define-command -hidden d-indent-on-opening-curly-brace %[
136    # align indent with opening paren when { is entered on a new line after the closing paren
137    try %[ execute-keys -draft -itersel h<a-F>)M <a-k> \A\(.*\)\h*\n\h*\{\z <ret> s \A|.\z <ret> 1<a-&> ]
138]
139
140define-command -hidden d-indent-on-closing-curly-brace %[
141    # align to opening curly brace when alone on a line
142    try %[ execute-keys -itersel -draft <a-h><a-k>^\h+\}$<ret>hms\A|.\z<ret>1<a-&> ]
143]
144
145§
146