much love
[mu.git] / vimrc.vim
blob9d13957904733948cea4084b88129c71cf4c69fd
1 " Highlighting literate directives in C++ sources.
2 function! HighlightTangledFile()
3   " Tangled comments only make sense in the sources and are stripped out of
4   " the generated .cc file. They're highlighted same as regular comments.
5   syntax match tangledComment /\/\/:.*/ | highlight link tangledComment Comment
6   syntax match tangledSalientComment /\/\/::.*/ | highlight link tangledSalientComment SalientComment
7   set comments-=://
8   set comments-=n://
9   set comments+=n://:,n://
11   " Inside tangle scenarios.
12   syntax region tangleDirective start=+:(+ skip=+".*"+ end=+)+
13   highlight link tangleDirective Delimiter
14   syntax match traceContains /^+.*/
15   highlight traceContains ctermfg=22
16   syntax match traceAbsent /^-.*/
17   highlight traceAbsent ctermfg=darkred
18   syntax match tangleScenarioSetup /^\s*% .*/ | highlight link tangleScenarioSetup SpecialChar
19   highlight Special ctermfg=160
21   syntax match subxString %"[^"]*"% | highlight link subxString Constant
22   " match globals but not registers like 'EAX'
23   syntax match subxGlobal %\<[A-Z][a-z0-9_-]*\>% | highlight link subxGlobal SpecialChar
24 endfunction
25 augroup LocalVimrc
26   autocmd BufRead,BufNewFile *.cc call HighlightTangledFile()
27   autocmd BufRead,BufNewFile *.subx set ft=subx
28 augroup END
30 " we often want to crib lines of machine code from other files
31 function! GrepSubX(regex)
32   " https://github.com/mtth/scratch.vim
33   Scratch!
34   silent exec "r !grep -h '".a:regex."' *.subx */*.subx"
35 endfunction
36 command! -nargs=1 G call GrepSubX(<q-args>)
38 if exists("&splitvertical")
39   command! -nargs=0 P hor split subx_opcodes
40 else
41   command! -nargs=0 P split subx_opcodes
42 endif
44 " useful for inspecting just the control flow in a trace
45 " see https://github.com/akkartik/mu/blob/main/linux/subx_debugging.md
46 command! -nargs=0 L exec "%!grep label |grep -v clear-stream:loop"
48 " show the call stack for the current line in the trace (by temporarily blowing away all earlier lines)
49 "? command! -nargs=0 C 1,.!awk '$4 == "label"{x[$1] = $0; for(i in x){if(i >= $1){delete x[i]}}} END{for (i in x) {if (i < $1) {print x[i]}}}'
50 "? command! -nargs=0 C 1,.!awk '$4 == "label"{x[$1] = $0} END{for (i in x) {if (i < $1) {print x[i]}}}'
51 command! -nargs=0 C 1,.!awk '{x[$1] = $0} END{for (i in x) {if (int(i) < int($1)) {print x[i]}}}'
53 " run test around cursor
54 if empty($TMUX) || (system("tmux display-message -p '#{client_control_mode}'") =~ "^1")
55   " hack: need to move cursor outside function at start (`{`), but inside function at end (`<C-o>`)
56   " this solution is unfortunate, but seems forced:
57   "   can't put initial cursor movement inside function because we rely on <C-r><C-w> to grab word at cursor
58   "   can't put final cursor movement out of function because that disables the wait for <CR> prompt; function must be final operation of map
59   "   can't avoid the function because that disables the wait for <CR> prompt
60   noremap <Leader>t {:keeppatterns /^[^ #]<CR>:call RunTestMoveCursor("<C-r><C-w>")<CR>
61   function! RunTestMoveCursor(arg)
62     exec "!./run_one_test ".expand("%")." '".a:arg."'"
63     exec "normal \<C-o>"
64   endfunction
65 else
66   " we have tmux and are not in control mode; we don't need to show any output in the Vim pane so life is simpler
67   " assume the left-most window is for the shell
68   noremap <Leader>t {:keeppatterns /^[^ #]<CR>:silent! call RunTestInFirstPane("<C-r><C-w>")<CR><C-o>
69   function! RunTestInFirstPane(arg)
70     call RunInFirstPane("./run_one_test ".expand("%")." ".a:arg)
71   endfunction
72   function! RunInFirstPane(arg)
73     exec "!tmux select-pane -t :0.0"
74     exec "!tmux send-keys '".a:arg."' C-m"
75     exec "!tmux last-pane"
76     " for some reason my screen gets messed up, so force a redraw
77     exec "!tmux send-keys 'C-l'"
78   endfunction
79 endif
81 if exists("&splitvertical")
82   command! -nargs=0 T badd last_run | sbuffer last_run
83 else
84   command! -nargs=0 T badd last_run | vert sbuffer last_run
85 endif
87 inoremap <Leader>a <Esc>F<Space>a(addr <Esc>A)
88 inoremap <Leader>h <Esc>F<Space>a(handle <Esc>A)
89 inoremap <Leader>ah <Esc>F<Space>a(addr handle <Esc>A)
90 inoremap <Leader>aa <Esc>F<Space>a(addr array <Esc>A)
91 inoremap <Leader>ha <Esc>F<Space>a(handle array <Esc>A)
92 inoremap <Leader>aha <Esc>F<Space>a(addr handle array <Esc>A)
93 inoremap <Leader>as <Esc>F<Space>a(addr stream <Esc>A)
94 inoremap <Leader>hs <Esc>F<Space>a(handle stream <Esc>A)
95 inoremap <Leader>ahs <Esc>F<Space>a(addr handle stream <Esc>A)
96 inoremap <Leader>as <Esc>F<Space>a(addr stream <Esc>A)
97 inoremap <Leader>o <Esc>F<Space>a(offset <Esc>A)
98 inoremap ,- <-
99 inoremap -. ->