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 13ec0f448e3edc781a2e39cef15590ee8ae882bb
parent f9ccea5af3c9e74d63b85ef3460b478774e62b5c
Author: mcol <mcol@posteo.net>
Date:   Sun,  2 Dec 2018 00:19:20 +0000

fixed some things

Diffstat:
Mafter/matlab.vim | 2+-
Aftplugin/matlab.vim | 272+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dftplugin/matlab/matlab.vim | 269-------------------------------------------------------------------------------
Mop.m | 4++--
4 files changed, 275 insertions(+), 272 deletions(-)

diff --git a/after/matlab.vim b/after/matlab.vim @@ -90,7 +90,7 @@ au BufEnter <buffer> call s:RunLint() au InsertLeave <buffer> call s:RunLint() au BufUnload <buffer> call s:Cleanup(expand("<afile>:t"), getbufvar(expand("<afile>"), "mlintTempDir")) -if !exists("mlint_hover") +if !exists("g:mlint_hover") au CursorHold <buffer> if s:BufChanged() | call s:RunLint() | endif au CursorHold <buffer> call s:GetLintMessage() au CursorHoldI <buffer> if s:BufChanged() | call s:RunLint() | endif diff --git a/ftplugin/matlab.vim b/ftplugin/matlab.vim @@ -0,0 +1,272 @@ +"------------------------------------------------------------------" +" _ _ _ " +" (_) | | | | " +" _ _ _ ____ | | _____ | |__ " +" | | | || || \ | | (____ || _ \ " +" \ V / | || | | || | / ___ || |_) ) " +" \_/ |_||_|_|_| \_)\_____||____/ " +" " +"------------------------------------------------------------------" + +" Vimlab: easy interaction between vim and matlab. +" Copyright (C) 2018 Matt Colligan +" +" This program is free software: you can redistribute it and/or modify +" it under the terms of the GNU General Public License as published by +" the Free Software Foundation, either version 3 of the License, or +" (at your option) any later version. +" +" This program is distributed in the hope that it will be useful, +" but WITHOUT ANY WARRANTY; without even the implied warranty of +" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +" GNU General Public License for more details. +" +" You should have received a copy of the GNU General Public License +" along with this program. If not, see <https://www.gnu.org/licenses/>. +" +" +" +" Much of this requires vim-slime to be installed and configured to send to +" matlab. Vim-slime -> github.com/jpalardy/vim-slime.git +" + +if exists("b:did_ftplugin_vimlab") + finish +endif +let b:did_ftplugin_vimlab = 1 + + +" map hash sign to percentage sign for easier commenting +inoremap <buffer> # % + +setlocal fo+=croql + +" comment format +setlocal comments=:%>,:% + + +"------------------------------------------------------------------ +" Auto closing of for, if, switch statements +"------------------------------------------------------------------ +" This can be disabled by setting the variable g:no_vimlab_autoend +if !exists("g:no_vimlab_autoend") + + function! VimlabAutoEnd() + if match(synIDattr(synIDtrans(synID(line("."), col("."), 0)), "name"), "Comment") == -1 + let l:endline = search("^$\\|\\%$", 'nWc') + if l:endline < line("$") + let l:endline = l:endline - 1 + endif + "exec l:endline . "s/$/\rend/" + call append(l:endline, "end") + norm mm=ip`m + call feedkeys("A") + endif + endfunction + + inoremap <buffer> <silent> if<space> if<space><Esc>:call VimlabAutoEnd()<CR> + inoremap <buffer> <silent> for<space> for<space><Esc>:call VimlabAutoEnd()<CR> + inoremap <buffer> <silent> switch<space> switch<space><Esc>:call VimlabAutoEnd()<CR> +endif + + + +"------------------------------------------------------------------ +" Session saving and restoration +"------------------------------------------------------------------ +" This can be enabled by setting the g:vimlab_session to the path of session file +" e.g. let g:vimlab_session = "~/.cache/vim/sessions/matlab-session.vim" + +if exists("g:vimlab_session") + " This could be mapped separately to save the session without exiting + function! VimlabSaveSession() + wall + exec "mksession!" . g:vimlab_session + echo "Session saved" + endfunction + + function! VimlabEndSession() + call VimlabSaveSession() + confirm qall + endfunction + + if !hasmapto('<Plug>VimlabEndSession') + nnoremap <buffer> XA :call VimlabEndSession()<CR> + endif +endif + + +"------------------------------------------------------------------ +" Matchit definitions (from Fabrice Guy) +"------------------------------------------------------------------ +let s:save_cpo = &cpo +set cpo-=C + +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 +endif + +let &cpo = s:save_cpo + + +"------------------------------------------------------------------ +" Open word under cursor +"------------------------------------------------------------------ + +" The default vim server name is vimlab, but can be changed by setting this variable +if !exists("g:vimlab_server") + let g:vimlab_server = "vimlab" +endif + +" The terminal is used if you want to open the word in a new window, rather than vim server +if !exists("g:vimlab_terminal") + " this defaults to urxvtc + let g:vimlab_terminal = "urxvtc" +endif + +function! VimlabOpenServer(server, terminal) + SlimeSend1 op expand('<cword>') a:server a:terminal; +endfunction +nnoremap <expr> <Plug>VimlabOpenServer :call VimlabOpenServer()<CR> +if !hasmapto("<Plug>VimlabOpenServer") + nnoremap <buffer> <silent> <CR> <Plug>VimlabOpenServer +endif + +function! VimlabOpenNewWindow(terminal) + SlimeSend1 op expand('<cword>') expand('""') a:terminal; +endfunction +nnoremap <expr> <Plug>VimlabOpenNewWindow :call VimlabOpenNewWindow()<CR> +if !hasmapto("<Plug>VimlabOpenNewWindow") + nnoremap <buffer> <silent> <Leader><CR> <Plug>VimlabOpenNewWindow +endif + + +"------------------------------------------------------------------ +" Running code +"------------------------------------------------------------------ +" This is mostly vim-slime stuff, but I've tried to use matlab's original key bindings + +" F9 will send paragraph or visual selection +xmap <buffer> <silent> <F9> <Plug>SlimeRegionSend +nmap <buffer> <silent> <F9> <Plug>SlimeParagraphSend + +" F8 will run current line +nnoremap <buffer> <silent> <F8> :SlimeSend<CR> + +" F7 will run the current word +nnoremap <buffer> <silent> <F7> :SlimeSend0(expand('<cword>'))<CR> + +" F4 will run the current section +function! VimlabRunSection() + try + foldopen! + catch + endtry + let l:top = search('%%\|\%^?', 'cbnW') + let l:bottom = search('%%\|\%$', 'nW') + + " this is required when running section from the last line + if l:bottom < l:top + let l:bottom = line('$') + endif + + execute l:top . "," . l:bottom . "SlimeSend" +endfunction +nnoremap <buffer> <silent> <F4> :call VimlabRunSection()<CR> + +" F2 will call script by title, so must be on matlab path +nnoremap <buffer> <silent> <F2> :SlimeSend0(expand("%:r"))<CR> + +" Control-w will list variables in the current workspace +nnoremap <buffer> <silent> <C-w> :SlimeSend1 who<CR> + + +"------------------------------------------------------------------ +" Debugging code +"------------------------------------------------------------------ + +" set debug marker highlight style +hi matlabDebug term=bold,reverse cterm=bold ctermfg=0 ctermbg=10 gui=bold guifg=bg guibg=LightGreen + +" toggle debug marker (work in progress) +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 <buffer> <silent> <Leader>t :call Matlab_debug_toggle_marker(line("."))<CR> + +" (a)dd - this will save the buffer, as matlab needs the current version +nnoremap <buffer> <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 <buffer> <silent> <Leader>c :let @" = "dbclear in " . expand("%") . " at " . line(".")<CR>:SlimeSend1 <C-r>"<CR>:call matchdelete(line('.'))<CR> + +" (s)tep +nnoremap <buffer> <silent> <Leader>s :SlimeSend1 dbstep<CR> +nnoremap <buffer> <silent> <F10> :SlimeSend1 dbstep<CR> + +" (g)o onward +nnoremap <buffer> <silent> <Leader>g :SlimeSend1 dbcont<CR> +nnoremap <buffer> <silent> <F5> :SlimeSend1 dbcont<CR> + +" stop on (e)rror +nnoremap <buffer> <silent> <Leader>e :SlimeSend1 dbstop if error<CR> + +" e(x)it debugging +nnoremap <buffer> <silent> <Leader>x :SlimeSend1 dbquit all<CR> + +" (f)lush all markers +nnoremap <buffer> <silent> <Leader>f :call clearmatches()<CR>:SlimeSend1 dbclear all<CR> + +" dbstack, dbup and dbdown +nnoremap <buffer> <silent> <Leader>ds :SlimeSend1 dbstack<CR> +nnoremap <buffer> <silent> <Leader>du :SlimeSend1 dbup<CR> +nnoremap <buffer> <silent> <Leader>dd :SlimeSend1 dbdown<CR> + + + +"------------------------------------------------------------------ +" Matlab section folding +"------------------------------------------------------------------ + +" remember folding layout +autocmd BufWinLeave *.m mkview +autocmd BufWinEnter *.m silent loadview + +" format folded section header text +function! MyFoldText() + let line = getline(v:foldstart) + + let nucolwidth = &fdc + &number * &numberwidth + let windowwidth = winwidth(0) - nucolwidth - 3 + let foldedlinecount = v:foldend - v:foldstart + + let onetab = strpart(' ', 0, &tabstop) + let line = substitute(line, '\t', onetab, 'g') + + let line = strpart(line, 0, windowwidth - len(foldedlinecount)) + let fillcharcount = windowwidth - len(line) - len(foldedlinecount) + return line . repeat(" ",fillcharcount) . ' ' . foldedlinecount +endfunction +set foldtext=MyFoldText() + + +" This is from ebranlard/vim-matlab-behave +" enable matlab section folding +function! MatlabFolds() + let thisline = getline(v:lnum) + if match(thisline,'^[\ ]*%%') >=0 + return ">1" + else + return "=" + endif +endfunction +setlocal foldmethod=expr +setlocal foldexpr=MatlabFolds() diff --git a/ftplugin/matlab/matlab.vim b/ftplugin/matlab/matlab.vim @@ -1,269 +0,0 @@ -"------------------------------------------------------------------" -" _ _ _ " -" (_) | | | | " -" _ _ _ ____ | | _____ | |__ " -" | | | || || \ | | (____ || _ \ " -" \ V / | || | | || | / ___ || |_) ) " -" \_/ |_||_|_|_| \_)\_____||____/ " -" " -"------------------------------------------------------------------" - -" Vimlab: easy interaction between vim and matlab. -" Copyright (C) 2018 Matt Colligan -" -" This program is free software: you can redistribute it and/or modify -" it under the terms of the GNU General Public License as published by -" the Free Software Foundation, either version 3 of the License, or -" (at your option) any later version. -" -" This program is distributed in the hope that it will be useful, -" but WITHOUT ANY WARRANTY; without even the implied warranty of -" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -" GNU General Public License for more details. -" -" You should have received a copy of the GNU General Public License -" along with this program. If not, see <https://www.gnu.org/licenses/>. -" -" -" -" Much of this requires vim-slime to be installed and configured to send to -" matlab. Vim-slime -> github.com/jpalardy/vim-slime.git -" - -if exists("b:did_ftplugin_vimlab") - finish -endif -let b:did_ftplugin_vimlab = 1 - - -" map hash sign to percentage sign for easier commenting -inoremap <buffer> # % - -setlocal fo+=croql - -" comment format -setlocal comments=:%>,:% - - -"------------------------------------------------------------------ -" Auto closing of for, if, switch statements -"------------------------------------------------------------------ -" This can be disabled by setting the variable g:no_vimlab_autoend -if !exists("g:no_vimlab_autoend") - function! VimlabAutoEnd() - 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 <buffer> <silent> if<space> if<space><Esc><Plug>MatlabAutoEnd - inoremap <buffer> <silent> for<space> for<space><Esc><Plug> MatlabAutoEnd - inoremap <buffer> <silent> switch<space> switch<space><Esc><Plug> MatlabAutoEnd -endif - - -"------------------------------------------------------------------ -" Session saving and restoration -"------------------------------------------------------------------ -" This can be enabled by setting the g:vimlab_session to the path of session file -" e.g. let g:vimlab_session = "~/.cache/vim/sessions/matlab-session.vim" - -if exists("g:vimlab_session") - " This could be mapped separately to save the session without exiting - function! VimlabSaveSession() - wall - exec "mksession!" . g:vimlab_session - echo "Session saved" - endfunction - - function! VimlabEndSession() - call VimlabSaveSession() - confirm qall - endfunction - - if !hasmapto('<Plug>VimlabEndSession') - "nnoremap <buffer> XA <Plug>VimlabEndSession - nnoremap <buffer> XA :call VimlabEndSession()<CR> - endif -endif - - -"------------------------------------------------------------------ -" Matchit definitions (from Fabrice Guy) -"------------------------------------------------------------------ -let s:save_cpo = &cpo -set cpo-=C - -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 -endif - -let &cpo = s:save_cpo - - -"------------------------------------------------------------------ -" Open word under cursor -"------------------------------------------------------------------ - -" The default vim server name is vimlab, but can be changed by setting this variable -if !exists("g:vimlab_server") - let g:vimlab_server = "vimlab" -endif - -" The terminal is used if you want to open the word in a new window, rather than vim server -if !exists("g:vimlab_terminal") - let g:vimlab_terminal = "urxvtc" -endif - -function! VimlabOpenWordServer(server, terminal) - SlimeSend1 op expand('<cword>') a:server a:terminal; -endfunction - -function! VimlabOpenWordNewWindow(terminal) - SlimeSend1 op expand('<cword>') expand('""') a:terminal; -endfunction - -if !hasmapto("<Plug>VimlabOpenWordServer") - nnoremap <buffer> <silent> <CR> <Plug>VimlabOpenWordServer -endif - -if !hasmapto("<Plug>VimlabOpenWordNewWindow") - nnoremap <buffer> <silent> <Leader><CR> <Plug>VimlabOpenWordNewWindow -endif - - -"------------------------------------------------------------------ -" Running code -"------------------------------------------------------------------ -" This is mostly vim-slime stuff, but I've tried to use matlab's original key bindings - -" F9 will send paragraph or visual selection -xnoremap <buffer> <silent> <F9> <Plug>SlimeRegionSend -nnoremap <buffer> <silent> <F9> <Plug>SlimeParagraphSend - -" F8 will run current line -nnoremap <buffer> <silent> <F8> :SlimeSend0(getline('.'))<CR> - -" F7 will run the current word -nnoremap <buffer> <silent> <F7> :SlimeSend0(expand('<cword>'))<CR> - -" F4 will run the current section -function! VimlabRunSection() - mkview - try - foldopen! - catch - endtry - normal ms$?%%\\|\%^?<CR>v$/%%\\|\%$<CR>?^.*\S<CR>$<F9>`s - let @/="" - loadview -endfunction -"nnoremap <buffer> <F4> <Plug>VimlabRunSection() - -map <buffer> <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 matlab path -nnoremap <buffer> <silent> <F2> :SlimeSend0(expand("%:r"))<CR> - -" Control-w will list variables in the current workspace -nnoremap <buffer> <silent> <C-w> :SlimeSend1 who<CR> - - -"------------------------------------------------------------------ -" Debugging code -"------------------------------------------------------------------ - -" set debug marker highlight style -hi matlabDebug term=bold,reverse cterm=bold ctermfg=0 ctermbg=10 gui=bold guifg=bg guibg=LightGreen - -" toggle debug marker (work in progress) -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 <buffer> <silent> <Leader>t :call Matlab_debug_toggle_marker(line("."))<CR> - -" (a)dd - this will save the buffer, as matlab needs the current version -nnoremap <buffer> <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 <buffer> <silent> <Leader>c :let @" = "dbclear in " . expand("%") . " at " . line(".")<CR>:SlimeSend1 <C-r>"<CR>:call matchdelete(line('.'))<CR> - -" (s)tep -nnoremap <buffer> <silent> <Leader>s :SlimeSend1 dbstep<CR> -nnoremap <buffer> <silent> <F10> :SlimeSend1 dbstep<CR> - -" (g)o onward -nnoremap <buffer> <silent> <Leader>g :SlimeSend1 dbcont<CR> -nnoremap <buffer> <silent> <F5> :SlimeSend1 dbcont<CR> - -" stop on (e)rror -nnoremap <buffer> <silent> <Leader>e :SlimeSend1 dbstop if error<CR> - -" e(x)it debugging -nnoremap <buffer> <silent> <Leader>x :SlimeSend1 dbquit all<CR> - -" (f)lush all markers -nnoremap <buffer> <silent> <Leader>f :call clearmatches()<CR>:SlimeSend1 dbclear all<CR> - -" dbstack, dbup and dbdown -nnoremap <buffer> <silent> <Leader>ds :SlimeSend1 dbstack<CR> -nnoremap <buffer> <silent> <Leader>du :SlimeSend1 dbup<CR> -nnoremap <buffer> <silent> <Leader>dd :SlimeSend1 dbdown<CR> - - - -"------------------------------------------------------------------ -" Matlab section folding -"------------------------------------------------------------------ - -" remember folding layout -autocmd BufWinLeave *.m mkview -autocmd BufWinEnter *.m silent loadview - -" format folded section header text -function! MyFoldText() - let line = getline(v:foldstart) - - let nucolwidth = &fdc + &number * &numberwidth - let windowwidth = winwidth(0) - nucolwidth - 3 - let foldedlinecount = v:foldend - v:foldstart - - let onetab = strpart(' ', 0, &tabstop) - let line = substitute(line, '\t', onetab, 'g') - - let line = strpart(line, 0, windowwidth - len(foldedlinecount)) - let fillcharcount = windowwidth - len(line) - len(foldedlinecount) - return line . repeat(" ",fillcharcount) . ' ' . foldedlinecount -endfunction -set foldtext=MyFoldText() - - -" This is from ebranlard/vim-matlab-behave -" enable matlab section folding -function! MatlabFolds() - let thisline = getline(v:lnum) - if match(thisline,'^[\ ]*%%') >=0 - return ">1" - else - return "=" - endif -endfunction -setlocal foldmethod=expr -setlocal foldexpr=MatlabFolds() diff --git a/op.m b/op.m @@ -90,11 +90,11 @@ end % open file in external editor if isempty(server) % a new terminal window will open - command = sprintf('%s -e bash -c "vim %s"', term, file); + command = sprintf('%s -e bash -c "vim \"%s\""', term, file); else % a vim server is running - command = sprintf('vim --servername %s --remote %s', server, file); + command = sprintf('vim --servername %s --remote "%s"', server, file); end