A vim plugin that emulates all of the powerful IDE features of MATLAB in vim so you don't need to run the full GUI.
git clone https://mcol.xyz/code/vimlab
Log | Files | Refs | README | LICENSE

commit e392969732c4e7b79c07f5346df657ee7ef8d4ae
parent 495e93f4fefdf8d8cedd0fedb2d5be066599e9cf
Author: mcol <mcol@posteo.net>
Date:   Sat,  3 Nov 2018 17:31:36 +0000

updates to syntax highlighting

Diffstat:
Mftplugin/matlab.vim | 98+++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------
Msyntax/matlab.vim | 43+++++++++++++++++++++++--------------------
2 files changed, 89 insertions(+), 52 deletions(-)

diff --git a/ftplugin/matlab.vim b/ftplugin/matlab.vim @@ -4,7 +4,7 @@ " Credit to this section goes to Fabrice Guy if exists("b:did_ftplugin") - finish + finish endif let b:did_ftplugin = 1 @@ -15,21 +15,21 @@ setlocal fo+=croql setlocal comments=:%>,:% if exists("loaded_matchit") - let s:conditionalEnd = '\([-+{\*\:(\/]\s*\)\@<!\<end\>\(\s*[-+}\:\*\/)]\)\@!' - let b:match_words = '\<classdef\>\|\<methods\>\|\<events\>\|\<properties\>\|\<if\>\|\<while\>\|\<for\>\|\<switch\>\|\<try\>\|\<function\>:' . s:conditionalEnd + let s:conditionalEnd = '\([-+{\*\:(\/]\s*\)\@<!\<end\>\(\s*[-+}\:\*\/)]\)\@!' + let b:match_words = '\<classdef\>\|\<methods\>\|\<events\>\|\<properties\>\|\<if\>\|\<while\>\|\<for\>\|\<switch\>\|\<try\>\|\<function\>:' . s:conditionalEnd endif setlocal suffixesadd=.m setlocal suffixes+=.asv " Change the :browse e filter to primarily show M-files if has("gui_win32") && !exists("b:browsefilter") - let b:browsefilter="M-files (*.m)\t*.m\n" . - \ "All files (*.*)\t*.*\n" + let b:browsefilter="M-files (*.m)\t*.m\n" . + \ "All files (*.*)\t*.*\n" endif let b:undo_ftplugin = "setlocal suffixesadd< suffixes< " - \ . "| unlet! b:browsefilter" - \ . "| unlet! b:match_words" + \ . "| unlet! b:browsefilter" + \ . "| unlet! b:match_words" let &cpo = s:save_cpo @@ -61,7 +61,7 @@ autocmd BufWinLeave *.m mkview autocmd BufWinEnter *.m silent loadview " format folded section header text -function! MyFoldText() " {{{ +function! MyFoldText() let line = getline(v:foldstart) let nucolwidth = &fdc + &number * &numberwidth @@ -72,25 +72,21 @@ function! MyFoldText() " {{{ let onetab = strpart(' ', 0, &tabstop) let line = substitute(line, '\t', onetab, 'g') - let line = strpart(line, 0, windowwidth - 2 -len(foldedlinecount)) + let line = strpart(line, 0, windowwidth - len(foldedlinecount)) let fillcharcount = windowwidth - len(line) - len(foldedlinecount) - return line . '…' . repeat(" ",fillcharcount) . foldedlinecount . '…' . ' ' -endfunction " }}} + return line . repeat(" ",fillcharcount) . ' ' . foldedlinecount +endfunction set foldtext=MyFoldText() -" These next two pieces are from ebranlard/vim-matlab-behave -" make section title bold -highlight MATCELL cterm=bold term=bold gui=bold -match MATCELL /^%%[^%]*$/ - +" This is from ebranlard/vim-matlab-behave " enable matlab section folding function! MatlabFolds() let thisline = getline(v:lnum) if match(thisline,'^[\ ]*%%') >=0 - return ">1" + return ">1" else - return "=" + return "=" endif endfunction setlocal foldmethod=expr @@ -121,19 +117,20 @@ endif """ Running code " F9 will send paragraph or visual selection -map <F9> <C-c><C-c> +xmap <F9> <Plug>SlimeRegionSend +nmap <F9> <Plug>SlimeParagraphSend " F8 will run current line map <F8> <Esc>ms0v$<F9>`s " F7 will run the current small word -map <F7> <Esc>msviw<C-c><C-c>`s +map <F7> <Esc>msviw<F9>`s " F4 will run the current section map <silent> <F4> ms:mkview<CR>:execute 'try \| foldopen! \| catch \| \| endtry'<CR>$?%%\\|\%^?<CR>v$/%%\\|\%$<CR>?^.*\S<CR>$<F9>`s:let @/=""<CR>:loadview<CR> -" F2 will call script by title, so must be on path -map <silent> <F2> <Esc>:let @" = expand("%")<CR>:SlimeSend1 <C-r>"<CR> +" F2 will call script by title, so must be on matlab path +map <silent> <F2> <Esc>:let @" = expand("%")<CR>:SlimeSend1 <C-r>";<CR> " list variables nnoremap <silent> <C-w> :SlimeSend1 who<CR> @@ -141,8 +138,23 @@ nnoremap <silent> <C-w> :SlimeSend1 who<CR> """ Debugging -" (a)dd -nnoremap <silent> <Leader>a :let @" = "dbstop in " . expand("%") . " at " . line(".")<CR>:SlimeSend1 <C-r>"<CR>:call matchaddpos('WildMenu', [line('.')], 10, line('.'))<CR> +hi matlabDebug ctermfg=2 ctermbg=1 cterm=bold + +" toggle debug marker +function! Matlab_debug_toggle_marker(line) + if (matchdelete(line(".")) == 0) + " remove marker + call SlimeSend1 "dbclear in" . expand("%") . " at " . line(".") + else + " add marker + call matchaddpos('matlabDebug', [line('.')], 10, line('.')) + call SlimeSend("dbstop in " . expand("%") . " at " . line(".")) + endif +endfunction +nnoremap <silent> <Leader>t :call Matlab_debug_toggle_marker(line("."))<CR> + +" (a)dd - this will save the buffer, as matlab needs the current draft +nnoremap <silent> <Leader>a :w<CR>:let @" = "dbstop in " . expand("%") . " at " . line(".")<CR>:SlimeSend1 <C-r>"<CR>:call matchaddpos('matlabDebug', [line('.')], 10, line('.'))<CR> " (c)lear nnoremap <silent> <Leader>c :let @" = "dbclear in " . expand("%") . " at " . line(".")<CR>:SlimeSend1 <C-r>"<CR>:call matchdelete(line('.'))<CR> @@ -151,7 +163,7 @@ nnoremap <silent> <Leader>c :let @" = "dbclear in " . expand("%") . " at " . lin nnoremap <silent> <Leader>s :SlimeSend1 dbstep<CR> nnoremap <silent> <F10> :SlimeSend1 dbstep<CR> -" continue (g for go) +" (g)o onward nnoremap <silent> <Leader>g :SlimeSend1 dbcont<CR> nnoremap <silent> <F5> :SlimeSend1 dbcont<CR> @@ -161,7 +173,7 @@ nnoremap <silent> <Leader>e :SlimeSend1 dbstop if error<CR> " e(x)it debugging nnoremap <silent> <Leader>x :SlimeSend1 dbquit all<CR> -" (f)lush markers +" (f)lush all markers nnoremap <silent> <Leader>f :call clearmatches()<CR>:SlimeSend1 dbclear all<CR> " dbstack, dbup and dbdown @@ -176,22 +188,44 @@ nnoremap <silent> <Leader>dd :SlimeSend1 dbdown<CR> " map hash sign to percentage sign for commenting inoremap # % -" add shortcuts to write chunks of code -"inoremap for<space> for<space><Esc>ms/^$<CR>iend<Esc>`s<CR>=ipA -"inoremap if<space> if<space><CR>end<Esc>kA -"inoremap switch<space> switch<space><CR>case<CR>end<Esc>kkA -" echo synIDattr(synIDtrans(synID(line("."), col("."), 0)), "name") -" the line above will tell you if the cursor is in a comment + +" colors for comments and section headers +hi HComment ctermfg=3 cterm=bold +hi MComment ctermfg=10 + + +" automatically add 'end' to if, for and switch statements +function! Matlab_auto_end() + if match(synIDattr(synIDtrans(synID(line("."), col("."), 0)), "name"), "Comment") == -1 + let l:win_view = winsaveview() + call search("^$\\|\\%$", 'W') + if line(".") == line("$") + :normal! oend + else + :normal! Oend + endif + :normal! =ip + call winrestview(l:win_view) + endif + call feedkeys('A') +endfunction +inoremap <silent> if<space> if<space><Esc>:call Matlab_auto_end()<CR> +inoremap <silent> for<space> for<space><Esc>:call Matlab_auto_end()<CR> +inoremap <silent> switch<space> switch<space><Esc>:call Matlab_auto_end()<CR> + " Save matlab session manually map <Leader>ss :w<CR>:mksession! ~/.cache/vim/sessions/matlab-session.vim<Cr> " and automatically on exit nnoremap XA :mksession! ~/.cache/vim/sessions/matlab-session.vim<CR>:wqa<CR> + " ctrl-i to indent the whole file function! IndentFile() + mkview let l:win_view = winsaveview() :normal! gg=G + silent loadview call winrestview(l:win_view) endfunction nnoremap <C-i> :call IndentFile()<CR> diff --git a/syntax/matlab.vim b/syntax/matlab.vim @@ -1,11 +1,6 @@ " Vim syntax file " Language: Matlab -" Maintainer: Fabrice Guy <fabrice.guy at gmail dot com> -" Original authors: Mario Eusebio and Preben Guldberg -" Last Change: 2008 Oct 16 : added try/catch/rethrow and class statements -" 2008 Oct 28 : added highlighting for most of Matlab functions -" 2009 Nov 23 : added 'todo' keyword in the matlabTodo keywords -" (for doxygen support) +" credit to Mario Eusebio, Preben Guldberg, Fabrice Guy " For version 5.x: Clear all syntax items " For version 6.x: Quit when a syntax file was already loaded @@ -23,6 +18,7 @@ syn keyword matlabExceptions try catch rethrow throw syn keyword matlabTodo contained TODO NOTE FIXME XXX syn keyword matlabImport import + " If you do not want these operators lit, uncommment them and the "hi link" below syn match matlabRelationalOperator "\(==\|\~=\|>=\|<=\|=\~\|>\|<\|=\)" syn match matlabArithmeticOperator "[-+]" @@ -34,9 +30,10 @@ syn match matlabLineContinuation "\.\{3}" " String syn region matlabString start=+'+ end=+'+ oneline +syn region matlabString start=+"+ end=+"+ oneline " If you don't like tabs -syn match matlabTab "\t" +"syn match matlabTab "\t" " Standard numbers syn match matlabNumber "\<\d\+[ij]\=\>" @@ -49,12 +46,16 @@ syn keyword matlabConstant eps Inf NaN pi " Transpose character and delimiters: Either use just [...] or (...) aswell syn match matlabDelimiter "[][]" -"syn match matlabDelimiter "[][()]" +"syn match matlabDelimiter "[()[]]" syn match matlabTransposeOperator "[])a-zA-Z0-9.]'"lc=1 syn match matlabSemicolon ";" syn match matlabComment "%.*$" contains=matlabTodo,matlabTab +"header is a comment starting with two %s. The Header sign line will colour the %%s differently + +syn match matlabHeaderSign "^[ \s]*%%\|%%[ \s]*$" +syn match matlabHeader "^[ \s]*%%.*$" contains=matlabTodo,matlabTab,matlabHeaderSign syn region matlabBlockComment start=+%{+ end=+%}+ contains=matlabBlockComment @@ -83,7 +84,7 @@ syn keyword matlabFunc cell cell2mat celldisp cellfun cellplot iscell iscells "Function Handles syn keyword matlabFunc feval func2str functions str2func "Java Classes and Objects -syn keyword matlabFunc clear depfun exist im2java inmem javaaddpath javaArray javachk Generate javaclasspath javaMethod javaObject javarmpath methodsview usejava which +syn keyword matlabFunc clearvars clear depfun exist im2java inmem javaaddpath javaArray javachk Generate javaclasspath javaMethod javaObject javarmpath methodsview usejava which "Data Type Identification syn keyword matlabFunc ischar isfloat isinteger isjava islogical isobject validateattributes who whos "Data type conversion @@ -155,7 +156,7 @@ syn keyword matlabFunc frame2im image imagesc imfinfo imformats imread imwrite i "Printing syn keyword matlabFunc frameedit hgexport orient print printopt saveas "Handle Graphics -syn keyword matlabFunc allchild ancestor copyobj delete gca gco get ishandle propedit set colormap +syn keyword matlabFunc allchild ancestor copyobj delete gca gco get ishandle propedit set colormap colorbar "Object syn keyword matlabFunc axes figure hggroup hgtransform light patch "root object @@ -320,36 +321,38 @@ if version >= 508 || !exists("did_matlab_syntax_inits") command -nargs=+ HiLink hi def link <args> endif - HiLink matlabTransposeOperator matlabOperator + HiLink matlabTransposeOperator matlabOperator HiLink matlabLineContinuation Special HiLink matlabLabel Label HiLink matlabConditional Conditional HiLink matlabRepeat Repeat HiLink matlabTodo Todo HiLink matlabString String - HiLink matlabDelimiter Identifier + HiLink matlabDelimiter Question HiLink matlabTransposeOther Identifier HiLink matlabNumber Number HiLink matlabFloat Float HiLink matlabConstant Constant HiLink matlabImplicit matlabStatement - HiLink matlabStatement Statement - HiLink matlabSemicolon SpecialChar - HiLink matlabComment Comment - HiLink matlabBlockComment Comment + HiLink matlabStatement Statement + HiLink matlabSemicolon SpecialChar + HiLink matlabComment MComment + HiLink matlabHeader HComment + HiLink matlabHeaderSign Question + HiLink matlabBlockComment MComment HiLink matlabImport Include HiLink matlabBoolean Boolean HiLink matlabStorageClass StorageClass - HiLink matlabArithmeticOperator matlabOperator - HiLink matlabRelationalOperator matlabOperator + HiLink matlabArithmeticOperator matlabOperator + HiLink matlabRelationalOperator matlabOperator HiLink matlabLogicalOperator matlabOperator HiLink matlabOperator Operator - HiLink matlabExceptions Exception + HiLink matlabExceptions Exception HiLink matlabFunc Function "optional highlighting - "HiLink matlabIdentifier Identifier + "HiLink matlabIdentifier Identifier "HiLink matlabTab Error delcommand HiLink endif