forked from WolfgangMehner/vim-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatlab-support.vim
More file actions
1238 lines (1238 loc) · 47.9 KB
/
matlab-support.vim
File metadata and controls
1238 lines (1238 loc) · 47.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"===============================================================================
"
" File: matlab-support.vim
"
" Description: Matlab IDE for Vim/gVim.
"
" See help file matlabsupport.txt .
"
" VIM Version: 7.0+
" Author: Wolfgang Mehner, [email protected]
" Organization:
" Version: see variable g:Matlab_Version below
" Created: 11.04.2010
" Revision: 24.11.2013
" License: Copyright (c) 2012-2014, Wolfgang Mehner
" 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, version 2 of the
" License.
" 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 version 2 for more details.
"===============================================================================
"
"-------------------------------------------------------------------------------
" Basic checks. {{{1
"-------------------------------------------------------------------------------
"
" need at least 7.0
if v:version < 700
echohl WarningMsg
echo 'The plugin matlab-support.vim needs Vim version >= 7.'
echohl None
finish
endif
"
" prevent duplicate loading
" need compatible
if &cp || ( exists('g:Matlab_Version') && ! exists('g:Matlab_DevelopmentOverwrite') )
finish
endif
let g:Matlab_Version= '0.8rc2' " version number of this script; do not change
"
"-------------------------------------------------------------------------------
" Auxiliary functions. {{{1
"-------------------------------------------------------------------------------
"
"-------------------------------------------------------------------------------
" s:ApplyDefaultSetting : Write default setting to a global variable. {{{2
"
" Parameters:
" varname - name of the variable (string)
" value - default value (string)
" Returns:
" -
"
" If g:<varname> does not exists, assign:
" g:<varname> = value
"-------------------------------------------------------------------------------
"
function! s:ApplyDefaultSetting ( varname, value )
if ! exists ( 'g:'.a:varname )
exe 'let g:'.a:varname.' = '.string( a:value )
endif
endfunction " ---------- end of function s:ApplyDefaultSetting ----------
"
"-------------------------------------------------------------------------------
" s:ErrorMsg : Print an error message. {{{2
"
" Parameters:
" line1 - a line (string)
" line2 - a line (string)
" ... - ...
" Returns:
" -
"-------------------------------------------------------------------------------
"
function! s:ErrorMsg ( ... )
echohl WarningMsg
for line in a:000
echomsg line
endfor
echohl None
endfunction " ---------- end of function s:ErrorMsg ----------
"
"-------------------------------------------------------------------------------
" s:GetGlobalSetting : Get a setting from a global variable. {{{2
"
" Parameters:
" varname - name of the variable (string)
" Returns:
" -
"
" If g:<varname> exists, assign:
" s:<varname> = g:<varname>
"-------------------------------------------------------------------------------
"
function! s:GetGlobalSetting ( varname )
if exists ( 'g:'.a:varname )
exe 'let s:'.a:varname.' = g:'.a:varname
endif
endfunction " ---------- end of function s:GetGlobalSetting ----------
"
"-------------------------------------------------------------------------------
" s:ImportantMsg : Print an important message. {{{2
"
" Parameters:
" line1 - a line (string)
" line2 - a line (string)
" ... - ...
" Returns:
" -
"-------------------------------------------------------------------------------
"
function! s:ImportantMsg ( ... )
echohl Search
echo join ( a:000, "\n" )
echohl None
endfunction " ---------- end of function s:ImportantMsg ----------
" }}}2
"-------------------------------------------------------------------------------
"
"-------------------------------------------------------------------------------
" Modul setup. {{{1
"-------------------------------------------------------------------------------
"
"-------------------------------------------------------------------------------
" Installation. {{{2
"-------------------------------------------------------------------------------
"
let s:MSWIN = has("win16") || has("win32") || has("win64") || has("win95")
let s:UNIX = has("unix") || has("macunix") || has("win32unix")
"
let s:installation = '*undefined*' " 'local' or 'system'
let s:plugin_dir = '' " the directory hosting ftplugin/ plugin/ matlab-support/ ...
let s:Matlab_GlbTemplateFile = '' " the global templates, undefined for s:installation == 'local'
let s:Matlab_LclTemplateFile = '' " the local templates
"
if s:MSWIN
"
"-------------------------------------------------------------------------------
" MS Windows
"-------------------------------------------------------------------------------
"
let s:plugin_dir = substitute( expand('<sfile>:p:h:h'), '\\', '/', 'g' )
"
if match( substitute( expand('<sfile>'), '\\', '/', 'g' ),
\ '\V'.substitute( expand('$HOME'), '\\', '/', 'g' ) ) == 0
"
" user installation assumed
let s:installation = 'local'
let s:Matlab_LclTemplateFile = s:plugin_dir.'/matlab-support/templates/Templates'
else
"
" system wide installation
let s:installation = 'system'
let s:Matlab_GlbTemplateFile = s:plugin_dir.'/matlab-support/templates/Templates'
let s:Matlab_LclTemplateFile = $HOME.'/vimfiles/matlab-support/templates/Templates'
endif
"
else
"
"-------------------------------------------------------------------------------
" Linux/Unix
"-------------------------------------------------------------------------------
"
let s:plugin_dir = expand('<sfile>:p:h:h')
"
if match( expand('<sfile>'), '\V'.resolve(expand('$HOME')) ) == 0
"
" user installation assumed
let s:installation = 'local'
let s:Matlab_LclTemplateFile = s:plugin_dir.'/matlab-support/templates/Templates'
else
"
" system wide installation
let s:installation = 'system'
let s:Matlab_GlbTemplateFile = s:plugin_dir.'/matlab-support/templates/Templates'
let s:Matlab_LclTemplateFile = $HOME.'/.vim/matlab-support/templates/Templates'
endif
"
endif
"
"-------------------------------------------------------------------------------
" Various setting. {{{2
"-------------------------------------------------------------------------------
"
let s:CmdLineEscChar = ' |"\'
"
let s:Matlab_LoadMenus = 'auto' " load the menus?
let s:Matlab_RootMenu = '&Matlab' " name of the root menu
"
let s:Matlab_MapLeader = '' " default: do not overwrite 'maplocalleader'
"
let s:Matlab_MlintExecutable = 'mlint' " default: mlint on system path
"
let s:Matlab_LineEndCommColDefault = 49
let s:Matlab_SnippetDir = s:plugin_dir.'/matlab-support/codesnippets/'
let s:Matlab_SnippetBrowser = 'gui'
"
if ! exists ( 's:MenuVisible' )
let s:MenuVisible = 0 " menus are not visible at the moment
endif
"
call s:GetGlobalSetting ( 'Matlab_GlbTemplateFile' )
call s:GetGlobalSetting ( 'Matlab_LclTemplateFile' )
call s:GetGlobalSetting ( 'Matlab_LoadMenus' )
call s:GetGlobalSetting ( 'Matlab_RootMenu' )
call s:GetGlobalSetting ( 'Matlab_MlintExecutable' )
call s:GetGlobalSetting ( 'Matlab_LineEndCommColDefault' )
call s:GetGlobalSetting ( 'Matlab_SnippetDir' )
call s:GetGlobalSetting ( 'Matlab_SnippetBrowser' )
"
call s:ApplyDefaultSetting ( 'Matlab_MapLeader', '' )
"
let s:Matlab_GlbTemplateFile = expand ( s:Matlab_GlbTemplateFile )
let s:Matlab_LclTemplateFile = expand ( s:Matlab_LclTemplateFile )
let s:Matlab_SnippetDir = expand ( s:Matlab_SnippetDir )
"
" }}}2
"-------------------------------------------------------------------------------
"
"-------------------------------------------------------------------------------
" Matlab_EndOfLineComment : Append end-of-line comment. {{{1
"-------------------------------------------------------------------------------
"
function! Matlab_EndOfLineComment () range
"
" local position
if !exists( 'b:Matlab_LineEndCommentColumn' )
let b:Matlab_LineEndCommentColumn = s:Matlab_LineEndCommColDefault
endif
"
" ----- trim whitespaces -----
exe a:firstline.','.a:lastline.'s/\s*$//'
"
for line in range( a:lastline, a:firstline, -1 )
silent exe ':'.line
if getline(line) !~ '^\s*$'
let linelength = virtcol( [line,'$'] ) - 1
let diff = 1
if linelength < b:Matlab_LineEndCommentColumn
let diff = b:Matlab_LineEndCommentColumn - 1 - linelength
endif
exe 'normal! '.diff.'A '
call mmtemplates#core#InsertTemplate (g:Matlab_Templates, 'Comments.end-of-line comment')
endif
endfor
"
endfunction " ---------- end of function Matlab_EndOfLineComment ----------
"
"-------------------------------------------------------------------------------
" Matlab_AdjustEndOfLineComm : Adjust end-of-line comment. {{{1
"-------------------------------------------------------------------------------
"
function! Matlab_AdjustEndOfLineComm () range
"
" comment character (for use in regular expression)
let cc = '%'
"
" patterns to ignore when adjusting line-end comments (maybe incomplete):
" - single-quoted strings, includes '' \n \\ ...
" :TODO:01.12.2013 14:26:WM: does Matlab support escape sequences like that?
let align_regex = "'\\%(''\\|\\\\.\\|[^']\\)*'"
"
" local position
if !exists( 'b:Matlab_LineEndCommentColumn' )
let b:Matlab_LineEndCommentColumn = s:Matlab_LineEndCommColDefault
endif
let correct_idx = b:Matlab_LineEndCommentColumn
"
" === plug-in specific code ends here ===
" === the behavior is governed by the variables above ===
"
" save the cursor position
let save_cursor = getpos('.')
"
for line in range( a:firstline, a:lastline )
silent exe ':'.line
"
let linetxt = getline('.')
"
" "pure" comment line left unchanged
if match ( linetxt, '^\s*'.cc ) == 0
"echo 'line '.line.': "pure" comment'
continue
endif
"
let b_idx1 = 1 + match ( linetxt, '\s*'.cc.'.*$', 0 )
let b_idx2 = 1 + match ( linetxt, cc.'.*$', 0 )
"
" not found?
if b_idx1 == 0
"echo 'line '.line.': no end-of-line comment'
continue
endif
"
" walk through ignored patterns
let idx_start = 0
"
while 1
let this_start = match ( linetxt, align_regex, idx_start )
"
if this_start == -1
break
else
let idx_start = matchend ( linetxt, align_regex, idx_start )
"echo 'line '.line.': ignoring >>>'.strpart(linetxt,this_start,idx_start-this_start).'<<<'
endif
endwhile
"
let b_idx1 = 1 + match ( linetxt, '\s*'.cc.'.*$', idx_start )
let b_idx2 = 1 + match ( linetxt, cc.'.*$', idx_start )
"
" not found?
if b_idx1 == 0
"echo 'line '.line.': no end-of-line comment'
continue
endif
"
call cursor ( line, b_idx2 )
let v_idx2 = virtcol('.')
"
" do b_idx1 last, so the cursor is in the right position for substitute below
call cursor ( line, b_idx1 )
let v_idx1 = virtcol('.')
"
" already at right position?
if ( v_idx2 == correct_idx )
"echo 'line '.line.': already at right position'
continue
endif
" ... or line too long?
if ( v_idx1 > correct_idx )
"echo 'line '.line.': line too long'
continue
endif
"
" substitute all whitespaces behind the cursor (regex '\%#') and the next character,
" to ensure the match is at least one character long
silent exe 'substitute/\%#\s*\(\S\)/'.repeat( ' ', correct_idx - v_idx1 ).'\1/'
"echo 'line '.line.': adjusted'
"
endfor
"
" restore the cursor position
call setpos ( '.', save_cursor )
"
endfunction " ---------- end of function Matlab_AdjustEndOfLineComm ----------
"
"-------------------------------------------------------------------------------
" Matlab_SetEndOfLineCommPos : Set end-of-line comment position. {{{1
"-------------------------------------------------------------------------------
"
function! Matlab_SetEndOfLineCommPos () range
"
let b:Matlab_LineEndCommentColumn = virtcol('.')
call s:ImportantMsg ( 'line end comments will start at column '.b:Matlab_LineEndCommentColumn )
"
endfunction " ---------- end of function Matlab_SetEndOfLineCommPos ----------
"
"-------------------------------------------------------------------------------
" Matlab_CodeComment : Code -> Comment {{{1
"-------------------------------------------------------------------------------
"
function! Matlab_CodeComment() range
"
" add '%' at the beginning of the lines
silent exe ":".a:firstline.",".a:lastline."s/^/%/"
"
endfunction " ---------- end of function Matlab_CodeComment ----------
"
"-------------------------------------------------------------------------------
" Matlab_CommentCode : Comment -> Code {{{1
"-------------------------------------------------------------------------------
"
function! Matlab_CommentCode( toggle ) range
"
" remove comments:
" - remove '%' from the beginning of the line
" and, in toggling mode:
" - if the line is not a comment, comment it
for i in range( a:firstline, a:lastline )
if getline( i ) =~ '^%'
silent exe i."s/^%//"
elseif a:toggle
silent exe i."s/^/%/"
endif
endfor
"
endfunction " ---------- end of function Matlab_CommentCode ----------
"
"-------------------------------------------------------------------------------
" s:GetFunctionParameters : Get the name, parameters, ... of a function. {{{1
"
" Parameters:
" fun_line - the function definition (string)
" Returns:
" [ <fun_name>, <returns>, <params> ] - data (list: string, list, list)
"
" The entries are as follows:
" file name - name of the function (string)
" returns - the name of the return arguments (list of strings)
" params - the names of the parameters (list of strings)
"
" In case of an error, an empty list is returned.
"-------------------------------------------------------------------------------
function! s:GetFunctionParameters( fun_line )
"
" 1st expression: the syntax category
" 2nd expression: as before, but with brackets to catch the match
"
let identifier = '[a-zA-Z][a-zA-Z0-9_]*'
let identifier_c = '\('.identifier.'\)'
let in_bracket = '[^)\]]*'
let in_bracket_c = '\('.in_bracket.'\)'
let spaces = '\s*'
let spaces_c = '\('.spaces.'\)'
let tail_c = '\(.*\)$'
"
let mlist = matchlist ( a:fun_line, '^'.spaces_c.'function\s*'.tail_c )
"
" no function?
if empty( mlist )
return []
endif
"
" found a function!
let tail = mlist[2]
let fun_name = ''
let return_str = ''
let param_str = ''
"
" no return
let mlist = matchlist( tail, '^'.identifier_c.'\s*(\s*'.in_bracket_c.')' )
if ! empty( mlist )
let fun_name = mlist[1]
let return_str = ''
let param_str = mlist[2]
endif
"
" single return
let mlist = matchlist( tail, '^'.identifier_c.'\s*=\s*'.identifier_c.'\s*(\s*'.in_bracket_c.')' )
if ! empty( mlist )
let fun_name = mlist[2]
let return_str = mlist[1]
let param_str = mlist[3]
endif
"
" multiple returns
let mlist = matchlist( tail, '^\[\s*'.in_bracket_c.'\]\s*=\s*'.identifier_c.'\s*(\s*'.in_bracket_c.')' )
if ! empty( mlist )
let fun_name = mlist[2]
let return_str = mlist[1]
let param_str = mlist[3]
endif
"
let param_str = substitute ( param_str, '\s*$', '', '' )
let param_list = split ( param_str, '\s*,\s*' )
"
let return_str = substitute ( return_str, '\s*$', '', '' )
let return_list = split ( return_str, '\s*,\s*' )
"
if empty ( fun_name )
return []
else
return [ fun_name, return_list, param_list ]
endif
"
endfunction " ---------- end of function s:GetFunctionParameters ----------
"
"-------------------------------------------------------------------------------
" Matlab_FunctionComment : Automatically comment a function. {{{1
"-------------------------------------------------------------------------------
"
function! Matlab_FunctionComment() range
"
" TODO: varargin, varargout
" TODO: multiple lines possible?
" TODO: remove '...' operator
let linestring = getline(a:firstline)
for i in range(a:firstline+1,a:lastline)
let linestring .= ' '.getline(i)
endfor
"
let res_list = s:GetFunctionParameters( linestring )
"
if empty( res_list )
return s:ImportantMsg ( 'No function found.' )
endif
"
" get all the parts
let [ fun_name, return_list, param_list ] = res_list
let base_name = mmtemplates#core#ExpandText ( g:Matlab_Templates, '|BASENAME|' )
"
" description of the file or another function?
if fun_name == base_name
call mmtemplates#core#InsertTemplate ( g:Matlab_Templates, 'Comments.file description',
\ '|PARAMETERS|', param_list, '|RETURNS|', return_list )
else
call mmtemplates#core#InsertTemplate ( g:Matlab_Templates, 'Comments.function description',
\ '|FUNCTION_NAME|', fun_name, '|PARAMETERS|', param_list, '|RETURNS|', return_list )
endif
"
endfunction " ---------- end of function Matlab_FunctionComment ----------
"
"-------------------------------------------------------------------------------
" Matlab_CodeSnippet : Code snippets. {{{1
"
" Parameters:
" action - "insert", "create", "vcreate", "view" or "edit" (string)
"-------------------------------------------------------------------------------
"
function! Matlab_CodeSnippet ( action )
"
"-------------------------------------------------------------------------------
" setup
"-------------------------------------------------------------------------------
"
" check directory
if ! isdirectory( s:Matlab_SnippetDir )
return s:ErrorMsg (
\ 'Code snippet directory '.s:Matlab_SnippetDir.' does not exist.',
\ '(Please create it.)' )
endif
"
" save option 'browsefilter'
if has( 'browsefilter' ) && exists( 'b:browsefilter' )
let browsefilter_save = b:browsefilter
let b:browsefilter = '*'
endif
"
"-------------------------------------------------------------------------------
" do action
"-------------------------------------------------------------------------------
"
if a:action == 'insert'
"
"-------------------------------------------------------------------------------
" action "insert"
"-------------------------------------------------------------------------------
"
" select file
if has('browse') && s:Matlab_SnippetBrowser == 'gui'
let snippetfile = browse ( 0, 'insert a code snippet', s:Matlab_SnippetDir, '' )
else
let snippetfile = input ( 'insert snippet ', s:Matlab_SnippetDir, 'file' )
endif
"
" insert snippet
if filereadable(snippetfile)
let linesread = line('$')
"
let old_cpoptions = &cpoptions " prevent the alternate buffer from being set to this files
setlocal cpoptions-=a
"
exe 'read '.snippetfile
"
let &cpoptions = old_cpoptions " restore previous options
"
let linesread = line('$') - linesread - 1 " number of lines inserted
"
" :TODO:03.12.2013 14:29:WM: indent here?
" indent lines
if linesread >= 0 && match( snippetfile, '\.\(ni\|noindent\)$' ) < 0
silent exe 'normal! ='.linesread.'+'
endif
endif
"
" delete first line if empty
if line('.') == 2 && getline(1) =~ '^$'
silent exe ':1,1d'
endif
"
elseif a:action == 'create' || a:action == 'vcreate'
"
"-------------------------------------------------------------------------------
" action "create" or "vcreate"
"-------------------------------------------------------------------------------
"
" select file
if has('browse') && s:Matlab_SnippetBrowser == 'gui'
let snippetfile = browse ( 1, 'create a code snippet', s:Matlab_SnippetDir, '' )
else
let snippetfile = input ( 'create snippet ', s:Matlab_SnippetDir, 'file' )
endif
"
" create snippet
if ! empty( snippetfile )
" new file or overwrite?
if ! filereadable( snippetfile ) || confirm( 'File '.snippetfile.' exists! Overwrite? ', "&Cancel\n&No\n&Yes" ) == 3
if a:action == 'create' && confirm( 'Write whole file as a snippet? ', "&Cancel\n&No\n&Yes" ) == 3
exe 'write! '.fnameescape( snippetfile )
elseif a:action == 'vcreate'
exe '*write! '.fnameescape( snippetfile )
endif
endif
endif
"
elseif a:action == 'view' || a:action == 'edit'
"
"-------------------------------------------------------------------------------
" action "view" or "edit"
"-------------------------------------------------------------------------------
if a:action == 'view' | let saving = 0
else | let saving = 1 | endif
"
" select file
if has('browse') && s:Matlab_SnippetBrowser == 'gui'
let snippetfile = browse ( saving, a:action.' a code snippet', s:Matlab_SnippetDir, '' )
else
let snippetfile = input ( a:action.' snippet ', s:Matlab_SnippetDir, 'file' )
endif
"
" open file
if ! empty( snippetfile )
exe 'split | '.a:action.' '.fnameescape( snippetfile )
endif
else
call s:ErrorMsg ( 'Unknown action "'.a:action.'".' )
endif
"
"-------------------------------------------------------------------------------
" wrap up
"-------------------------------------------------------------------------------
"
" restore option 'browsefilter'
if has( 'browsefilter' ) && exists( 'b:browsefilter' )
let b:browsefilter = browsefilter_save
endif
"
endfunction " ---------- end of function Matlab_CodeSnippet ----------
"
"-------------------------------------------------------------------------------
" Matlab_CheckCode : Use mlint to check the code. {{{1
"-------------------------------------------------------------------------------
"
function! Matlab_CheckCode() range
"
silent exe 'update' | " write source file if necessary
cclose
"
" assemble all the information
let currentdir = getcwd()
let currentbuffer = bufname('%')
let fullname = currentdir.'/'.currentbuffer
let fullname = shellescape( fullname, 1 )
"
" prepare and check the executable
if ! executable( s:Matlab_MlintExecutable )
return s:ErrorMsg (
\ 'Command "'.s:Matlab_MlintExecutable.'" not found. Not configured correctly?',
\ 'Further information: :help matlabsupport-config-mlint' )
endif
"
" call 'mlint' and process the output
let errors_mlint = system ( shellescape( s:Matlab_MlintExecutable ).' -id '.fullname )
"
if empty( errors_mlint )
let errors = ''
else
let errors = 'FILE '.currentbuffer."\n".errors_mlint.'FILEEND'
endif
"
let errorf_saved = &g:errorformat
"
let &g:errorformat =
\ '%-PFILE %f,%-QFILEEND,'
\ .'L %l (C %c): %m,L %l (C %c-%*\d): %m'
silent exe 'cexpr errors'
"
let &g:errorformat = errorf_saved
"
if empty ( errors_mlint )
redraw " redraw after cclose, before echoing
call s:ImportantMsg ( bufname('%').': No warnings.' )
else
botright cwindow
cc
endif
"
endfunction " ---------- end of function Matlab_CheckCode ----------
"
"-------------------------------------------------------------------------------
" Matlab_IgnoreWarning : Ignore the current mlint warning. {{{1
"-------------------------------------------------------------------------------
"
function! Matlab_IgnoreWarning() range
"
" the list of errors
let qf_list = getqflist ()
"
if empty ( qf_list )
return s:ImportantMsg ( 'No warnings.' )
endif
"
" assemble all the information
let my_buf = bufnr ( '%' )
let my_line = line ( '.' )
let my_col = col ( '.' )
let text = ''
let type = ''
"
" look for the right error
" number of errors on the line:
" - none : abort
" - one : continue with this error
" - two or more : the column must match as well
let err_on_line = 0
"
for error in qf_list
" ignore invalid errors, errors on other lines
if ! error.valid || error.bufnr != my_buf || error.lnum != my_line
continue
endif
"
" matches the location precisely
if error.col == my_col
let text = error.text
let err_on_line = 1
break
endif
"
let text = error.text
let err_on_line += 1
continue
endfor
"
if empty ( text )
return s:ImportantMsg ( 'No warning for this line.' )
elseif err_on_line > 1
return s:ImportantMsg ( 'There is more than one warning for this line. Go to the correct location.' )
endif
"
let type = matchstr ( text, '^\w\+' )
"
" append or add to the special comment
let line = getline ( '.' )
let mlist = matchlist ( line, '%#ok\%(:\|<\)\([a-zA-Z,]\+\)>\?\s*$' )
if ! empty ( mlist )
" line contains %#ok, check if the error-code is contained as well
if -1 == match ( mlist[1], type )
exe ':.s/\(>\?\)\s*$/,'.type.'\1'
else
call s:ImportantMsg ( 'Error is already being ignored.' )
endif
else
" append %#ok:...
exe ':.s/\s*$/ %#ok<'.type.'>'
endif
"
" reposition the cursor
call cursor ( my_line, my_col )
"
endfunction " ---------- end of function Matlab_IgnoreWarning ----------
"
"------------------------------------------------------------------------------
" === Templates API === {{{1
"------------------------------------------------------------------------------
"
"------------------------------------------------------------------------------
" Matlab_SetMapLeader {{{2
"------------------------------------------------------------------------------
function! Matlab_SetMapLeader ()
if exists ( 'g:Matlab_MapLeader' )
call mmtemplates#core#SetMapleader ( g:Matlab_MapLeader )
endif
endfunction " ---------- end of function Matlab_SetMapLeader ----------
"
"------------------------------------------------------------------------------
" Matlab_ResetMapLeader {{{2
"------------------------------------------------------------------------------
function! Matlab_ResetMapLeader ()
if exists ( 'g:Matlab_MapLeader' )
call mmtemplates#core#ResetMapleader ()
endif
endfunction " ---------- end of function Matlab_ResetMapLeader ----------
" }}}2
"
"-------------------------------------------------------------------------------
" s:SetupTemplates : Initial loading of the templates. {{{1
"-------------------------------------------------------------------------------
"
function! s:SetupTemplates()
"
"-------------------------------------------------------------------------------
" setup template library
"-------------------------------------------------------------------------------
let g:Matlab_Templates = mmtemplates#core#NewLibrary ()
"
" mapleader
if empty ( g:Matlab_MapLeader )
call mmtemplates#core#Resource ( g:Matlab_Templates, 'set', 'property', 'Templates::Mapleader', '\' )
else
call mmtemplates#core#Resource ( g:Matlab_Templates, 'set', 'property', 'Templates::Mapleader', g:Matlab_MapLeader )
endif
"
" map: choose style
call mmtemplates#core#Resource ( g:Matlab_Templates, 'set', 'property', 'Templates::ChooseStyle::Map', 'nts' )
"
" syntax: comments
call mmtemplates#core#ChangeSyntax ( g:Matlab_Templates, 'comment', '§' )
"
"-------------------------------------------------------------------------------
" load template library
"-------------------------------------------------------------------------------
if s:installation == 'system'
"-------------------------------------------------------------------------------
" system installation
"-------------------------------------------------------------------------------
"
" global templates
if filereadable( s:Matlab_GlbTemplateFile )
call mmtemplates#core#ReadTemplates ( g:Matlab_Templates, 'load', s:Matlab_GlbTemplateFile )
else
return s:ErrorMsg ( 'Global template file "'.s:Matlab_GlbTemplateFile.'" not readable.' )
endif
"
let local_dir = fnamemodify ( s:Matlab_LclTemplateFile, ':p:h' )
"
if ! isdirectory( local_dir ) && exists('*mkdir')
try
call mkdir ( local_dir, 'p' )
catch /.*/
endtry
endif
"
if isdirectory( local_dir ) && ! filereadable( s:Matlab_LclTemplateFile )
let sample_template_file = s:plugin_dir.'/matlab-support/rc/sample_template_file'
if filereadable( sample_template_file )
call writefile ( readfile ( sample_template_file ), s:Matlab_LclTemplateFile )
endif
endif
"
" local templates
if filereadable( s:Matlab_LclTemplateFile )
call mmtemplates#core#ReadTemplates ( g:Matlab_Templates, 'load', s:Matlab_LclTemplateFile )
if mmtemplates#core#ExpandText ( g:Matlab_Templates, '|AUTHOR|' ) == 'YOUR NAME'
call s:ErrorMsg ( 'Please set your personal details in the file "'.s:Matlab_LclTemplateFile.'".' )
endif
endif
"
elseif s:installation == 'local'
"-------------------------------------------------------------------------------
" local installation
"-------------------------------------------------------------------------------
"
" local templates
if filereadable ( s:Matlab_LclTemplateFile )
call mmtemplates#core#ReadTemplates ( g:Matlab_Templates, 'load', s:Matlab_LclTemplateFile )
else
return s:ErrorMsg ( 'Local template file "'.s:Matlab_LclTemplateFile.'" not readable.' )
endif
endif
endfunction " ---------- end of function s:SetupTemplates ----------
"
"-------------------------------------------------------------------------------
" Matlab_HelpPlugin : Plug-in help. {{{1
"-------------------------------------------------------------------------------
"
function! Matlab_HelpPlugin ()
try
help matlab-support
catch
exe 'helptags '.s:plugin_dir.'/doc'
help matlab-support
endtry
endfunction " ---------- end of function Matlab_HelpPlugin ----------
"
"-------------------------------------------------------------------------------
" s:CreateMaps : Create additional maps. {{{1
"-------------------------------------------------------------------------------
"
function! s:CreateMaps ()
"
"-------------------------------------------------------------------------------
" settings - local leader
"-------------------------------------------------------------------------------
if ! empty ( g:Matlab_MapLeader )
if exists ( 'g:maplocalleader' )
let ll_save = g:maplocalleader
endif
let g:maplocalleader = g:Matlab_MapLeader
endif
"
"-------------------------------------------------------------------------------
" comments
"-------------------------------------------------------------------------------
noremap <buffer> <silent> <LocalLeader>cl :call Matlab_EndOfLineComment()<CR>
inoremap <buffer> <silent> <LocalLeader>cl <Esc>:call Matlab_EndOfLineComment()<CR>
noremap <buffer> <silent> <LocalLeader>cj :call Matlab_AdjustEndOfLineComm()<CR>
inoremap <buffer> <silent> <LocalLeader>cj <Esc>:call Matlab_AdjustEndOfLineComm()<CR>
noremap <buffer> <silent> <LocalLeader>cs :call Matlab_SetEndOfLineCommPos()<CR>
inoremap <buffer> <silent> <LocalLeader>cs <Esc>:call Matlab_SetEndOfLineCommPos()<CR>
"
noremap <buffer> <silent> <LocalLeader>cc :call Matlab_CodeComment()<CR>
inoremap <buffer> <silent> <LocalLeader>cc <Esc>:call Matlab_CodeComment()<CR>
noremap <buffer> <silent> <LocalLeader>co :call Matlab_CommentCode(0)<CR>
inoremap <buffer> <silent> <LocalLeader>co <Esc>:call Matlab_CommentCode(0)<CR>
noremap <buffer> <silent> <LocalLeader>ct :call Matlab_CommentCode(1)<CR>
inoremap <buffer> <silent> <LocalLeader>ct <Esc>:call Matlab_CommentCode(1)<CR>
"
noremap <buffer> <silent> <LocalLeader>ca :call Matlab_FunctionComment()<CR>
inoremap <buffer> <silent> <LocalLeader>ca <Esc>:call Matlab_FunctionComment()<CR>
"
"-------------------------------------------------------------------------------
" snippets
"-------------------------------------------------------------------------------
nnoremap <buffer> <silent> <LocalLeader>ni :call Matlab_CodeSnippet('insert')<CR>
inoremap <buffer> <silent> <LocalLeader>ni <C-C>:call Matlab_CodeSnippet('insert')<CR>
vnoremap <buffer> <silent> <LocalLeader>ni <C-C>:call Matlab_CodeSnippet('insert')<CR>
nnoremap <buffer> <silent> <LocalLeader>nc :call Matlab_CodeSnippet('create')<CR>
inoremap <buffer> <silent> <LocalLeader>nc <C-C>:call Matlab_CodeSnippet('create')<CR>
vnoremap <buffer> <silent> <LocalLeader>nc <C-C>:call Matlab_CodeSnippet('vcreate')<CR>
nnoremap <buffer> <silent> <LocalLeader>nv :call Matlab_CodeSnippet('view')<CR>
inoremap <buffer> <silent> <LocalLeader>nv <C-C>:call Matlab_CodeSnippet('view')<CR>
vnoremap <buffer> <silent> <LocalLeader>nv <C-C>:call Matlab_CodeSnippet('view')<CR>
nnoremap <buffer> <silent> <LocalLeader>ne :call Matlab_CodeSnippet('edit')<CR>
inoremap <buffer> <silent> <LocalLeader>ne <C-C>:call Matlab_CodeSnippet('edit')<CR>
vnoremap <buffer> <silent> <LocalLeader>ne <C-C>:call Matlab_CodeSnippet('edit')<CR>
"
"-------------------------------------------------------------------------------
" templates - specials
"-------------------------------------------------------------------------------
nnoremap <buffer> <silent> <LocalLeader>ntl :call mmtemplates#core#EditTemplateFiles(g:Matlab_Templates,-1)<CR>
inoremap <buffer> <silent> <LocalLeader>ntl <C-C>:call mmtemplates#core#EditTemplateFiles(g:Matlab_Templates,-1)<CR>
vnoremap <buffer> <silent> <LocalLeader>ntl <C-C>:call mmtemplates#core#EditTemplateFiles(g:Matlab_Templates,-1)<CR>
if s:installation == 'system'
nnoremap <buffer> <silent> <LocalLeader>ntg :call mmtemplates#core#EditTemplateFiles(g:Matlab_Templates,0)<CR>
inoremap <buffer> <silent> <LocalLeader>ntg <C-C>:call mmtemplates#core#EditTemplateFiles(g:Matlab_Templates,0)<CR>
vnoremap <buffer> <silent> <LocalLeader>ntg <C-C>:call mmtemplates#core#EditTemplateFiles(g:Matlab_Templates,0)<CR>
endif
nnoremap <buffer> <silent> <LocalLeader>ntr :call mmtemplates#core#ReadTemplates(g:Matlab_Templates,"reload","all")<CR>
inoremap <buffer> <silent> <LocalLeader>ntr <C-C>:call mmtemplates#core#ReadTemplates(g:Matlab_Templates,"reload","all")<CR>
vnoremap <buffer> <silent> <LocalLeader>ntr <C-C>:call mmtemplates#core#ReadTemplates(g:Matlab_Templates,"reload","all")<CR>
nnoremap <buffer> <silent> <LocalLeader>nts :call mmtemplates#core#ChooseStyle(g:Matlab_Templates,"!pick")<CR>
inoremap <buffer> <silent> <LocalLeader>nts <C-C>:call mmtemplates#core#ChooseStyle(g:Matlab_Templates,"!pick")<CR>
vnoremap <buffer> <silent> <LocalLeader>nts <C-C>:call mmtemplates#core#ChooseStyle(g:Matlab_Templates,"!pick")<CR>
"
"-------------------------------------------------------------------------------
" code checker
"-------------------------------------------------------------------------------
nnoremap <buffer> <silent> <LocalLeader>rc :call Matlab_CheckCode()<CR>
inoremap <buffer> <silent> <LocalLeader>rc <Esc>:call Matlab_CheckCode()<CR>
vnoremap <buffer> <silent> <LocalLeader>rc <Esc>:call Matlab_CheckCode()<CR>
nnoremap <buffer> <silent> <LocalLeader>ri :call Matlab_IgnoreWarning()<CR>
inoremap <buffer> <silent> <LocalLeader>ri <Esc>:call Matlab_IgnoreWarning()<CR>
vnoremap <buffer> <silent> <LocalLeader>ri <Esc>:call Matlab_IgnoreWarning()<CR>
"
"-------------------------------------------------------------------------------
" settings
"-------------------------------------------------------------------------------
nnoremap <buffer> <silent> <LocalLeader>rs :call Matlab_Settings(0)<CR>
inoremap <buffer> <silent> <LocalLeader>rs <Esc>:call Matlab_Settings(0)<CR>
vnoremap <buffer> <silent> <LocalLeader>rs <Esc>:call Matlab_Settings(0)<CR>
"
"-------------------------------------------------------------------------------
" help
"-------------------------------------------------------------------------------
nnoremap <buffer> <silent> <LocalLeader>hs :call Matlab_HelpPlugin()<CR>
inoremap <buffer> <silent> <LocalLeader>hs <Esc>:call Matlab_HelpPlugin()<CR>
vnoremap <buffer> <silent> <LocalLeader>hs <Esc>:call Matlab_HelpPlugin()<CR>
"
"-------------------------------------------------------------------------------
" settings - reset local leader
"-------------------------------------------------------------------------------
if ! empty ( g:Matlab_MapLeader )
if exists ( 'll_save' )
let g:maplocalleader = ll_save
else
unlet g:maplocalleader
endif
endif
"
"-------------------------------------------------------------------------------
" templates
"-------------------------------------------------------------------------------
call mmtemplates#core#CreateMaps ( 'g:Matlab_Templates', g:Matlab_MapLeader, 'do_jump_map' )
"
endfunction " ---------- end of function s:CreateMaps ----------
"
"-------------------------------------------------------------------------------
" s:InitMenus : Initialize menus. {{{1
"-------------------------------------------------------------------------------
"
function! s:InitMenus()
"
if ! has ( 'menu' )
return
endif
"
" Preparation
call mmtemplates#core#CreateMenus ( 'g:Matlab_Templates', s:Matlab_RootMenu, 'do_reset' )
"
" get the mapleader (correctly escaped)
let [ esc_mapl, err ] = mmtemplates#core#Resource ( g:Matlab_Templates, 'escaped_mapleader' )
"
exe 'anoremenu '.s:Matlab_RootMenu.'.Matlab <Nop>'