-
Notifications
You must be signed in to change notification settings - Fork 0
/
matlab.el
5912 lines (5455 loc) · 215 KB
/
matlab.el
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
;;; matlab.el --- major mode for MATLAB(R) dot-m files
;; Author: Matt Wette <[email protected]>,
;; Eric M. Ludlam <[email protected]>
;; Maintainer: Eric M. Ludlam <[email protected]>
;; Created: 04 Jan 91
;; Keywords: MATLAB(R)
;; Version:
(defconst matlab-mode-version "3.3.6"
"Current version of MATLAB(R) mode.")
;;
;; Copyright (C) 2004-2010, 2014 The Mathworks, Inc
;; Copyright (C) 1997-2004 Eric M. Ludlam: The MathWorks, Inc
;; Copyright (C) 1991-1997 Matthew R. Wette
;;
;; 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 2, 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 GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
;;
;;; Commentary:
;;
;; This major mode for GNU Emacs provides support for editing MATLAB(R) dot-m
;; files. It automatically indents for block structures (including nested
;; functions), line continuations (e.g., ...), and comments.
;;
;; Additional features include auto-fill including auto-additions of
;; ellipsis for commands, and even strings. Block/end construct
;; highlighting as you edit. Primitive code-verification and
;; identification. Templates and other code editing functions.
;; Advanced symbol completion. Code highlighting via font-lock.
;; There are many navigation commands that let you move across blocks
;; of code at different levels.
;;
;; Lastly, there is support for running MATLAB(R) in an Emacs buffer,
;; with full shell history and debugger support (when used with the db
;; commands.) The shell can be used as an online help while editing
;; code, providing help on functions, variables, or running arbitrary
;; blocks of code from the buffer you are editing.
;;; Code:
(require 'easymenu)
(require 'tempo)
(require 'derived)
;; compatibility
(if (string-match "X[Ee]macs" emacs-version)
(progn
(defalias 'matlab-make-overlay 'make-extent)
(defalias 'matlab-overlay-put 'set-extent-property)
(defalias 'matlab-overlay-get 'extent-property)
(defalias 'matlab-delete-overlay 'delete-extent)
(defalias 'matlab-overlay-start 'extent-start-position)
(defalias 'matlab-overlay-end 'extent-end-position)
(defalias 'matlab-previous-overlay-change 'previous-extent-change)
(defalias 'matlab-next-overlay-change 'next-extent-change)
(defalias 'matlab-overlays-at
(lambda (pos) (extent-list nil pos pos)))
(defalias 'matlab-cancel-timer 'delete-itimer)
(defun matlab-run-with-idle-timer (secs repeat function &rest args)
(condition-case nil
(apply 'start-itimer
"matlab" function secs
(if repeat secs nil) t
t (car args))
(error
;; If the above doesn't work, then try this old version of
;; start itimer.
(start-itimer "matlab" function secs (if repeat secs nil)))))
)
(defalias 'matlab-make-overlay 'make-overlay)
(defalias 'matlab-overlay-put 'overlay-put)
(defalias 'matlab-overlay-get 'overlay-get)
(defalias 'matlab-delete-overlay 'delete-overlay)
(defalias 'matlab-overlay-start 'overlay-start)
(defalias 'matlab-overlay-end 'overlay-end)
(defalias 'matlab-previous-overlay-change 'previous-overlay-change)
(defalias 'matlab-next-overlay-change 'next-overlay-change)
(defalias 'matlab-overlays-at 'overlays-at)
(defalias 'matlab-cancel-timer 'cancel-timer)
(defalias 'matlab-run-with-idle-timer 'run-with-idle-timer)
)
(cond ((fboundp 'point-at-bol)
(defalias 'matlab-point-at-bol 'point-at-bol)
(defalias 'matlab-point-at-eol 'point-at-eol))
;; Emacs 20.4
((fboundp 'line-beginning-position)
(defalias 'matlab-point-at-bol 'line-beginning-position)
(defalias 'matlab-point-at-eol 'line-end-position))
(t
(defmacro matlab-point-at-bol ()
(save-excursion (beginning-of-line) (point)))
(defmacro matlab-point-at-eol ()
(save-excursion (end-of-line) (point)))))
(defmacro matlab-run-in-matlab-mode-only (&rest body)
"Execute BODY only if the active buffer is a MATLAB(R) M-file buffer."
`(if (eq major-mode 'matlab-mode)
(progn
,@body)
(error "This command works only in a MATLAB M-file buffer")))
(defun matlab-with-emacs-link ()
"Return non-nil if Emacs Link is running and user wants to use it."
(and (featurep 'matlab-eei)
matlab-use-eei
matlab-eei-process))
;;; User-changeable variables =================================================
;; Variables which the user can change
(defgroup matlab nil
"MATLAB(R) mode."
:prefix "matlab-"
:group 'languages)
(defcustom matlab-indent-level 4
"*The basic indentation amount in `matlab-mode'."
:group 'matlab
:type 'integer)
(defcustom matlab-cont-level 4
"*Basic indentation after continuation if no other methods are found."
:group 'matlab
:type 'integer)
(defcustom matlab-cont-requires-ellipsis t
"*Specify if ellipses are required at the end of a line for continuation.
Future versions of Matlab may not require ellipses ... , so a heuristic
determining if there is to be continuation is used instead."
:group 'matlab
:type 'integer)
(defcustom matlab-case-level '(2 . 2)
"*How far to indent case/otherwise statements in a switch.
This can be an integer, which is the distance to indent the CASE and
OTHERWISE commands, and how far to indent commands appearing in CASE
and OTHERWISE blocks. It can also be a cons cell which is of form
(CASEINDENT . COMMANDINDENT)
where CASEINDENT is the indentation of the CASE and OTHERWISE
statements, and COMMANDINDENT is the indentation of commands appearing
after the CASE or OTHERWISE command.
Note: Currently a bug exists if:
CASEINDENT+COMMANDINDENT != `matlab-indent-level'
so if you customize these variables, follow the above rule, and you
should be ok."
:group 'matlab
:type 'sexp)
(defcustom matlab-align-to-paren t
"*Whether continuation lines should be aligned to the opening parenthesis.
When non-nil, continuation lines are aligned to the opening parenthesis if the
opening is not followed by only spaces and ellipses. When nil, continued lines
are simply indented by `matlab-cont-level'."
:group 'matlab
:type 'boolean
)
(defcustom matlab-indent-function-body 'guess
"*If non-nil, indent body of function.
If the global value is nil, do not indent function bodies.
If the global value is t, always indent function bodies.
If the global value is 'guess, then the local value will be set to
either nil or t when the MATLAB mode is started in a buffer based on the
file's current indentation.
If the global value is 'MathWorks-Standard, then the local value is not
changed, and functions are indented based on `matlab-functions-have-end'."
:group 'matlab
:type '(choice (const :tag "Always" t)
(const :tag "Never" nil)
(const :tag "Guess" 'guess)
(const :tag "MathWorks Standard"
'MathWorks-Standard))
)
(make-variable-buffer-local 'matlab-indent-function-body)
(defcustom matlab-functions-have-end nil
"*If non-nil, functions-have-end minor mode is on by default."
:group 'matlab
:type 'boolean)
(make-variable-buffer-local 'matlab-functions-have-end)
(defun matlab-toggle-functions-have-end ()
(interactive)
(matlab-toggle-functions-have-end-minor-mode))
;; The following minor mode is on if and only if the above variable is true;
(easy-mmode-define-minor-mode matlab-functions-have-end-minor-mode
"Toggle functions-have-end minor mode, indicating function/end pairing."
nil
(condition-case nil ;; avoid parse error on xemacs
(eval (read "#(\" function...end\" 0 15 (face (font-lock-keyword-face) fontified t))"))
(error " function...end"))
nil ; empty mode-map
;; body of matlab-functions-have-end-minor-mode
(if matlab-functions-have-end-minor-mode
(setq matlab-functions-have-end t)
(setq matlab-functions-have-end nil)
)
)
(defun matlab-do-functions-have-end-p ()
"Non-nil if the first function in the current buffer terminates with end."
(save-excursion
(goto-char (point-min))
(if (re-search-forward matlab-defun-regex nil t)
(let ((matlab-functions-have-end t))
(beginning-of-line)
(condition-case nil
(progn (matlab-forward-sexp) t)
(error nil))
)
nil
)
))
(defun matlab-toggle-functions-have-end-minor-mode ()
(matlab-functions-have-end-minor-mode)
(if (and matlab-functions-have-end-minor-mode (not (eq major-mode 'matlab-mode)))
(progn
(matlab-functions-have-end-minor-mode -1)
(error "functions-have-end minor mode is only for MATLAB Major mode")))
(setq matlab-functions-have-end matlab-functions-have-end-minor-mode))
(defun matlab-indent-function-body-p ()
"Non-nil if functions bodies are indented.
See `matlab-indent-function-body' variable."
(if (eq matlab-indent-function-body 'MathWorks-Standard)
;; Dec '09
;; The MathWorks standard is the same as if functions have end.
matlab-functions-have-end
;; Else, just return the variable.
matlab-indent-function-body))
(defcustom matlab-indent-past-arg1-functions
"[sg]et\\(_param\\)?\\|waitfor"
"*Regex describing functions whose first arg is special.
This specialness means that all following parameters which appear on
continued lines should appear indented to line up with the second
argument, not the first argument."
:group 'matlab
:type 'string)
(defcustom matlab-arg1-max-indent-length 15
"*The maximum length to indent when indenting past arg1.
If arg1 is exceptionally long, then only this number of characters
will be indented beyond the open paren starting the parameter list.")
(defcustom matlab-maximum-indents '(;; = is a convenience. Don't go too far
(?= . (10 . 4))
;; Fns should provide hard limits
(?\( . 50)
;; Matrix/Cell arrays
(?\[ . 20)
(?\{ . 20))
"Alist of maximum indentations when lining up code.
Each element is of the form (CHAR . INDENT) where char is a character
the indent engine is using, and INDENT is the maximum indentation
allowed. Indent could be of the form (MAXIMUM . INDENT), where
MAXIMUM is the maximum allowed calculated indent, and INDENT is the
amount to use if MAXIMUM is reached."
:group 'matlab
:type '(repeat (cons (character :tag "Open List Character")
(sexp :tag "Number (max) or cons (max indent)"))))
(defcustom matlab-handle-simulink t
"*If true, add in a few simulink customizations.
This variable's state is mostly useful when set at load time when
simulink font lock keywords can be removed. This will handle
additional cases as the need arrises."
:group 'matlab
:type 'boolean)
(defcustom matlab-auto-fill t
"*If true, set variable `auto-fill-function' to our function at startup."
:group 'matlab
:type 'boolean)
(defcustom matlab-fill-fudge 10
"Number of characters around `fill-column' we can fudge filling.
Basically, there are places that are very convenient to fill at, but
might not be the closest fill spot, or occur after `fill-column'.
If they occur within this fudge factor, we will use them.
Also, if none of the above occur, and we find a symbol to break at,
but an open paren (group) starts or ends within this fudge factor,
move there to boost the amount of fill leverage we can get."
:group 'matlab
:type 'integer)
(defcustom matlab-fill-fudge-hard-maximum 79
"The longest line allowed when auto-filling code.
This overcomes situations where the `fill-column' plus the
`matlab-fill-fudge' is greater than some hard desired limit."
:group 'matlab
:type 'integer)
(defcustom matlab-elipsis-string "..."
"Text used to perform continuation on code lines.
This is used to generate and identify continuation lines.")
(defcustom matlab-fill-code t
"*If true, `auto-fill-mode' causes code lines to be automatically continued."
:group 'matlab
:type 'boolean)
(defcustom matlab-fill-count-ellipsis-flag t
"*Non-nil means to count the ellipsis when auto filling.
This effectively shortens the `fill-column' by the length of
`matlab-elipsis-string'.")
(defcustom matlab-fill-strings-flag t
"*Non-nil means that when auto-fill is on, strings are broken across lines.
If `matlab-fill-count-ellipsis-flag' is non nil, this shortens the
`fill-column' by the length of `matlab-elipsis-string'.")
(defcustom matlab-comment-column 40
"*The goal comment column in `matlab-mode' buffers."
:group 'matlab
:type 'integer)
(defcustom matlab-comment-anti-indent 0
"*Amount of anti-indentation to use for comments in relation to code."
:group 'matlab
:type 'integer)
(defcustom matlab-comment-line-s "% "
"*String to start comment on line by itself."
:group 'matlab
:type 'string)
(defcustom matlab-comment-on-line-s "% "
"*String to start comment on line with code."
:group 'matlab
:type 'string)
(defcustom matlab-comment-region-s "% $$$ "
"*String inserted by \\[matlab-comment-region] at start of each line in \
region."
:group 'matlab
:type 'string)
(defcustom matlab-verify-on-save-flag t
"*Non-nil means to verify M whenever we save a file."
:group 'matlab
:type 'boolean)
(defcustom matlab-mode-verify-fix-functions
'(matlab-mode-vf-functionname matlab-mode-vf-classname)
"List of function symbols which perform a verification and fix to M code.
Each function gets no arguments, and returns nothing. They can move
point, but it will be restored for them."
:group 'matlab
:type '(repeat (choice :tag "Function: "
'(matlab-mode-vf-functionname
matlab-mode-vf-classname
matlab-mode-vf-block-matches-forward
matlab-mode-vf-block-matches-backward
matlab-mode-vf-quiesce-buffer
))))
(defcustom matlab-block-verify-max-buffer-size 50000
"*Largest buffer size allowed for block verification during save."
:group 'matlab
:type 'integer)
;; It is time to disable this.
(defcustom matlab-vers-on-startup nil
"*If non-nil, show the version number on startup."
:group 'matlab
:type 'boolean)
(defcustom matlab-highlight-block-match-flag t
"*Non-nil means to highlight the matching if/end/whatever.
The highlighting only occurs when the cursor is on a block start or end
keyword."
:group 'matlab
:type 'boolean)
(defcustom matlab-show-periodic-code-details-flag nil
"*Non-nil means to show code details in the minibuffer.
This will only work if `matlab-highlight-block-match-flag' is non-nil."
:group 'matlab
:type 'boolean)
(defcustom matlab-use-eei t
"*Use Emacs Link for save-and-go and run-region."
:group 'matlab
:type 'boolean)
(defcustom matlab-mode-hook nil
"*List of functions to call on entry to MATLAB mode."
:group 'matlab
:type 'hook)
(defcustom matlab-completion-technique 'complete
"*How the `matlab-complete-symbol' interfaces with the user.
Valid values are:
'increment - which means that new strings are tried with each
successive call until all methods are exhausted.
(Similar to `hippie-expand'.)
'complete - Which means that if there is no single completion, then
all possibilities are displayed in a completion buffer."
:group 'matlab
:type '(radio (const :tag "Incremental completion (hippie-expand)."
increment)
(const :tag "Show completion buffer."
complete)))
(defcustom matlab-show-mlint-warnings nil
"*If non-nil, show mlint warnings."
:group 'matlab
:type 'boolean)
(make-variable-buffer-local 'matlab-show-mlint-warnings)
(defcustom matlab-highlight-cross-function-variables nil
"*If non-nil, highlight cross-function variables."
:group 'matlab
:type 'boolean)
(make-variable-buffer-local 'matlab-highlight-cross-function-variables)
(defcustom matlab-return-add-semicolon nil
"*If non nil, check to see a semicolon is needed when RET is pressed."
:group 'matlab
:type 'boolean)
(make-variable-buffer-local 'matlab-return-add-semicolon)
(defcustom matlab-change-current-directory nil
"*If non nil, make file's directory the current directory when
evaluating it."
:group 'matlab
:type 'boolean)
(make-variable-buffer-local 'matlab-change-current-directory)
;; Load in the region we use for highlighting stuff.
(if (and (featurep 'custom) (fboundp 'custom-declare-variable))
(let ((l-region-face (if (facep 'region) 'region 'zmacs-region)))
;; If we have custom, we can make our own special face like this
(defface matlab-region-face
(list
(list t
(list :background (face-background l-region-face)
:foreground (face-foreground l-region-face))))
"*Face used to highlight a matlab region."
:group 'matlab))
;; If we do not, then we can fake it by copying 'region.
(cond ((facep 'region)
(copy-face 'region 'matlab-region-face))
(t
(copy-face 'zmacs-region 'matlab-region-face))))
(defvar matlab-unterminated-string-face 'matlab-unterminated-string-face
"Self reference for unterminated string face.")
(defvar matlab-simulink-keyword-face 'matlab-simulink-keyword-face
"Self reference for simulink keywords.")
(defvar matlab-nested-function-keyword-face 'matlab-nested-function-keyword-face
"Self reference for nested function/end keywords.")
(defvar matlab-cross-function-variable-face 'matlab-cross-function-variable-face
"Self reference for cross-function variables.")
(defvar matlab-cellbreak-face 'matlab-cellbreak-face
"Self reference for cellbreaks.")
(defun matlab-font-lock-adjustments ()
"Make adjustments for font lock.
If font lock is not loaded, lay in wait."
(if (and (featurep 'custom) (fboundp 'custom-declare-variable))
(progn
(defface matlab-unterminated-string-face
(list
(list t
(list :background (face-background font-lock-string-face)
:foreground (face-foreground font-lock-string-face)
:underline t)))
"*Face used to highlight unterminated strings."
:group 'matlab)
(defface matlab-simulink-keyword-face
(list
(list t
(list :background (face-background font-lock-type-face)
:foreground (face-foreground font-lock-type-face)
:underline t)))
"*Face used to highlight simulink specific functions."
:group 'matlab)
(defface matlab-nested-function-keyword-face
(list
(list t
(list :slant 'italic)))
"*Face to use for cross-function variables.")
(defface matlab-cross-function-variable-face
(list
(list t
(list :weight 'bold
:slant 'italic)))
"*Face to use for cross-function variables."
:group 'matlab)
(defface matlab-cellbreak-face
(list
(list t
(list :background (face-background font-lock-comment-face)
:foreground (face-foreground font-lock-comment-face)
:overline t
:bold t)))
"*Face to use for cellbreak %% lines.")
)
;; Now, lets make the unterminated string face
(cond ((facep 'font-lock-string-face)
(copy-face 'font-lock-string-face
'matlab-unterminated-string-face))
(t
(make-face 'matlab-unterminated-string-face)))
(set-face-underline-p 'matlab-unterminated-string-face t)
;; Now make some simulink faces
(cond ((facep 'font-lock-type-face)
(copy-face 'font-lock-type-face 'matlab-simulink-keyword-face))
(t
(make-face 'matlab-simulink-keyword-face)))
(set-face-underline-p 'matlab-simulink-keyword-face t)
;; Now make some nested function/end keyword faces
(cond ((facep 'font-lock-type-face)
(copy-face 'font-lock-type-face 'matlab-nested-function-keyword-face))
(t
(make-face 'matlab-nested-function-keyword-face)))
;; Now make some cross-function variable faces
(cond ((facep 'font-lock-type-face)
(copy-face 'font-lock-type-face 'matlab-cross-function-variable-face))
(t
(make-face 'matlab-cross-function-variable-face)))
(set-face-bold-p 'matlab-cross-function-variable-face t)
;; Now make some cellbreak variable faces
(cond ((facep 'font-comment-face)
(copy-face 'font-lock-comment-face 'matlab-cellbreak-face))
(t
(make-face 'matlab-cellbreak-face)))
(set-face-bold-p 'matlab-cellbreak-face t)
(condition-case nil
(set-face-attribute 'matlab-cellbreak-face nil :overline t)
(error nil))
)
(remove-hook 'font-lock-mode-hook 'matlab-font-lock-adjustments))
;; Make the adjustments for font lock after it's loaded.
;; I found that eval-after-load was unreliable.
(if (featurep 'font-lock)
(matlab-font-lock-adjustments)
(add-hook 'font-lock-mode-hook 'matlab-font-lock-adjustments))
;;; MATLAB mode variables =====================================================
(defvar matlab-tempo-tags nil
"List of templates used in MATLAB mode.")
;; syntax table
(defvar matlab-mode-syntax-table
(let ((st (make-syntax-table (standard-syntax-table))))
(modify-syntax-entry ?_ "_" st)
(modify-syntax-entry ?% "<" st)
(modify-syntax-entry ?\n ">" st)
(modify-syntax-entry ?\\ "." st)
(modify-syntax-entry ?\t " " st)
(modify-syntax-entry ?+ "." st)
(modify-syntax-entry ?- "." st)
(modify-syntax-entry ?* "." st)
(modify-syntax-entry ?' "." st)
(modify-syntax-entry ?/ "." st)
(modify-syntax-entry ?= "." st)
(modify-syntax-entry ?< "." st)
(modify-syntax-entry ?> "." st)
(modify-syntax-entry ?& "." st)
(modify-syntax-entry ?| "." st)
st)
"The syntax table used in `matlab-mode' buffers.")
(defvar matlab-mode-special-syntax-table
(let ((st (copy-syntax-table matlab-mode-syntax-table)))
;; Make _ a part of words so we can skip them better
(modify-syntax-entry ?_ "w" st)
st)
"The syntax table used when navigating blocks.")
;; abbrev table
(defvar matlab-mode-abbrev-table nil
"The abbrev table used in `matlab-mode' buffers.")
(define-abbrev-table 'matlab-mode-abbrev-table ())
;;; Keybindings ===============================================================
(defvar matlab-help-map
(let ((km (make-sparse-keymap)))
(define-key km "r" 'matlab-shell-run-command)
(define-key km "f" 'matlab-shell-describe-command)
(define-key km "a" 'matlab-shell-apropos)
(define-key km "v" 'matlab-shell-describe-variable)
(define-key km "t" 'matlab-shell-topic-browser)
km)
"The help key map for `matlab-mode' and `matlab-shell-mode'.")
(defvar matlab-insert-map
(let ((km (make-sparse-keymap)))
(define-key km "c" 'matlab-insert-next-case)
(define-key km "e" 'matlab-insert-end-block)
(define-key km "i" 'tempo-template-matlab-if)
(define-key km "I" 'tempo-template-matlab-if-else)
(define-key km "f" 'tempo-template-matlab-for)
(define-key km "s" 'tempo-template-matlab-switch)
(define-key km "t" 'tempo-template-matlab-try)
(define-key km "w" 'tempo-template-matlab-while)
(define-key km "F" 'tempo-template-matlab-function)
(define-key km "'" 'matlab-stringify-region)
;; Not really inserts, but auto coding stuff
(define-key km "\C-s" 'matlab-ispell-strings)
(define-key km "\C-c" 'matlab-ispell-comments)
km)
"Keymap used for inserting simple texts based on context.")
;; mode map
(defvar matlab-mode-map
(let ((km (make-sparse-keymap)))
(define-key km [return] 'matlab-return)
(define-key km "%" 'matlab-electric-comment)
(define-key km "\C-c;" 'matlab-comment-region)
(define-key km "\C-c:" 'matlab-uncomment-region)
(define-key km [(control c) return] 'matlab-comment-return)
(define-key km [(control c) (control c)] matlab-insert-map)
(define-key km [(control c) (control f)] 'matlab-fill-comment-line)
(define-key km [(control c) (control j)] 'matlab-justify-line)
(define-key km [(control c) (control q)] 'matlab-fill-region)
(define-key km [(control c) (control s)] 'matlab-shell-save-and-go)
(define-key km [(control c) (control r)] 'matlab-shell-run-region)
(define-key km [(meta control return)] 'matlab-shell-run-cell)
(define-key km [(control c) (control t)] 'matlab-show-line-info)
(define-key km [(control c) ?. ] 'matlab-find-file-on-path)
(define-key km [(control h) (control m)] matlab-help-map)
(define-key km [(control j)] 'matlab-linefeed)
(define-key km "\M-\r" 'newline)
(define-key km [(meta \;)] 'matlab-comment)
(define-key km [(meta q)] 'matlab-fill-paragraph)
(define-key km [(meta a)] 'matlab-beginning-of-command)
(define-key km [(meta e)] 'matlab-end-of-command)
(define-key km [(meta j)] 'matlab-comment-line-break-function)
(define-key km [(meta s)] 'matlab-show-matlab-shell-buffer)
(define-key km "\M-\t" 'matlab-complete-symbol)
(define-key km [(meta control f)] 'matlab-forward-sexp)
(define-key km [(meta control b)] 'matlab-backward-sexp)
(define-key km [(meta control q)] 'matlab-indent-sexp)
(define-key km [(meta control a)] 'matlab-beginning-of-defun)
(define-key km [(meta control e)] 'matlab-end-of-defun)
(if (string-match "XEmacs" emacs-version)
(define-key km [(control meta button1)] 'matlab-find-file-click)
(define-key km [(control meta mouse-2)] 'matlab-find-file-click))
(substitute-key-definition 'comment-region 'matlab-comment-region
km) ; global-map ;torkel
km)
"The keymap used in `matlab-mode'.")
;;; Font locking keywords =====================================================
(defvar matlab-string-start-regexp "\\(^\\|[^]})a-zA-Z0-9_.']\\)"
"Regexp used to represent the character before the string char '.
The ' character has restrictions on what starts a string which is needed
when attempting to understand the current context.")
;; To quote a quote, put two in a row, thus we need an anchored
;; first quote. In addition, we don't want to color strings in comments.
(defvar matlab-string-end-regexp "[^'\n]*\\(''[^'\n]*\\)*'"
"Regexp used to represent the character pattern for ending a string.
The ' character can be used as a transpose, and can transpose transposes.
Therefore, to end, we must check all that goop.")
(defun matlab-font-lock-string-match-normal (limit)
"When font locking strings, call this function for normal strings.
Argument LIMIT is the maximum distance to scan."
(matlab-font-lock-string-match-here
(concat matlab-string-start-regexp
"\\('" matlab-string-end-regexp "\\)"
"\\([^']\\|$\\)")
limit))
(defun matlab-font-lock-string-match-unterminated (limit)
"When font locking strings, call this function for normal strings.
Argument LIMIT is the maximum distance to scan."
(matlab-font-lock-string-match-here
(concat matlab-string-start-regexp "\\('[^'\n]*\\(''[^'\n]*\\)*\\)$")
limit))
(defun matlab-font-lock-string-match-here (regex limit)
"When font-locking strings, call this function to determine a match.
Argument REGEX is the expression to scan for. Match 2 must be the string.
Argument LIMIT is the maximum distance to scan."
(let (e)
(while (and (re-search-forward regex limit t)
(progn
;; This gets us out of a comment after the string.
(setq e (match-end 2))
(goto-char (match-beginning 2))
(prog1
(or (matlab-cursor-in-comment)
(if (bolp) nil
(save-excursion
(forward-char -1)
(matlab-cursor-in-string))))
(goto-char e))))
(setq e nil))
(if (not e)
nil
(goto-char e)
t)))
(defun matlab-font-lock-comment-match (limit)
"When font-locking comments, call this function to determine a match.
Argument LIMIT is the maximum distance to scan."
(let (e)
(while (and (re-search-forward "\\(%[^%\n]*\\)" limit t)
(progn
(setq e (match-end 1))
(member (get-text-property (match-beginning 0) 'face)
'(font-lock-string-face
matlab-unterminated-string-face))))
(setq e nil))
(if (not e)
nil
(goto-char e)
t)))
(defun matlab-find-unreachable-code (limit)
"Find code that is if'd out with if(0) or if(false), and mark it as a comment.
The if(0) and else/end construct should be highlighted differently.
Argument LIMIT is the maximum distance to search."
(if (and (< (point) limit)
(re-search-forward
"\\<\\(if\\>\\s-*(?\\s-*\\(0\\|false\\)\\s-*)?$\\)"
limit t))
(let ((b1 (match-beginning 1))
(e1 (match-end 1))
(b2 nil) (e2 nil)
(b3 nil) (e3 nil))
(goto-char b1)
(condition-case nil
(progn
;; Go forward over the matlab sexp. Include scanning
;; for ELSE since parts of the ELSE block are not
;; `commented out'.
(matlab-forward-sexp t)
(forward-word -1)
;; Is there an ELSE in this block?
(if (looking-at (matlab-block-mid-re))
(progn
(setq b3 (match-beginning 0)
e3 (match-end 0))
;; Now find the REAL end.
(matlab-forward-sexp)
(forward-word -1)))
;; End of block stuff
(if (looking-at (matlab-block-end-re))
(progn
(setq b2 (match-beginning 0)
e2 (match-end 0))
;; make sure something exists...
(if (not b3) (setq b3 b2 e3 e2)))
(error "Eh?"))
;; Ok, build up some match data.
(set-match-data
(list b1 e2 ;the real deal.
b1 e1 ;if (0)
b2 e2 ;end
b3 e3 ;else (if applicable.)
b1 e3)) ;body commented out.
t)
(error nil)))))
(defun matlab-font-lock-nested-function-keyword-match (limit)
"Find next nested function/end keyword for font-lock.
Argument LIMIT is the maximum distance to search."
; Because of the way overlays are setup, the cursor will be sitting
; on either a "function" or "end" keyword.
(catch 'result
(let ((pos (point))
overlays)
(while (< pos limit)
(setq overlays (matlab-overlays-at pos))
(while overlays
(let ((overlay (car overlays)))
(when (matlab-overlay-get overlay 'nested-function)
(when (= pos (matlab-overlay-start overlay))
(goto-char pos)
;; The following line presumably returns true.
(throw 'result (re-search-forward "function" (+ pos 8) t)))
(let ((end-of-overlay (- (matlab-overlay-end overlay) 3)))
(when (<= pos end-of-overlay)
(goto-char end-of-overlay)
(throw 'result
(re-search-forward "end" (+ end-of-overlay 3) t))))))
(setq overlays (cdr overlays)))
(setq pos (matlab-next-overlay-change pos)))
nil ;; no matches, stop
)))
(defun matlab-font-lock-cross-function-variables-match (limit)
"Find next cross-function variable for font-lock.
Argument LIMIT is the maximum distance to search."
(catch 'result
(let ((pos (point))
overlays variables)
(while (< pos limit)
(let ((overlays (matlab-overlays-at pos)))
(while overlays
(let ((overlay (car overlays)))
(setq variables (matlab-overlay-get
overlay 'cross-function-variables))
(if variables
(progn
(goto-char pos)
(setq pos (min limit (matlab-overlay-end overlay)))
(if (re-search-forward variables pos t)
(progn
(throw 'result t))))))
(setq overlays (cdr overlays))))
(setq pos (matlab-next-overlay-change pos)))
nil ;; no matches, stop
)))
(defun matlab-find-block-comments (limit)
"Find code that is commented out with %{ until %}.
Argument LIMIT is the maximum distance to search."
(if (and (< (point) limit)
(re-search-forward "%{" limit t))
(let ((b1 (match-beginning 0))
(e1 (match-end 0))
(b2 nil) (e2 nil)
(b3 nil) (e3 nil))
(goto-char b1)
(forward-char -1)
(when (not (matlab-cursor-in-comment))
(setq b2 (re-search-forward "%}" limit t))
(when b2
(setq b2 (match-beginning 0)
e2 (match-end 0))
(set-match-data
(list b1 e2 ; full match
b1 e2 ; the full comment
b1 e1 ; the block start
b2 e2 ; the block end
))
t
)))))
(defcustom matlab-keyword-list '("global" "persistent" "for" "parfor" "while"
"spmd" "if" "elseif" "else"
"endfunction" "return" "break" "continue"
"switch" "case" "otherwise" "try"
"catch" "tic" "toc"
;; MCOS keywords
"classdef" "properties" "methods" "enumeration"
)
"List of keywords for MATLAB used in highlighting.
Customizing this variable is only useful if `regexp-opt' is available."
:group 'matlab
:type '(repeat (string :tag "Keyword: ")))
(defcustom matlab-handle-graphics-list '("figure" "axes" "axis" "line"
"surface" "patch" "text" "light"
"image" "set" "get" "uicontrol"
"uimenu" "uitoolbar"
"uitoggletool" "uipushtool"
"uicontext" "uicontextmenu"
"setfont" "setcolor")
"List of handle graphics functions used in highlighting.
Customizing this variable is only useful if `regexp-opt' is available."
:group 'matlab
:type '(repeat (string :tag "HG Keyword: ")))
(defcustom matlab-debug-list '("dbstop" "dbclear" "dbcont" "dbdown" "dbmex"
"dbstack" "dbstatus" "dbstep" "dbtype" "dbup"
"dbquit")
"List of debug commands used in highlighting.
Customizing this variable is only useful if `regexp-opt' is available."
:group 'matlab
:type '(repeat (string :tag "Debug Keyword: ")))
;; font-lock keywords
(defvar matlab-font-lock-keywords
(list
;; String quote chars are also used as transpose, but only if directly
;; after characters, numbers, underscores, or closing delimiters.
'(matlab-font-lock-string-match-normal 2 font-lock-string-face)
;; A string with no termination is not currently highlighted.
;; This will show that the string needs some attention.
'(matlab-font-lock-string-match-unterminated
2 matlab-unterminated-string-face)
;; Comments must occur after the string, that way we can check to see
;; if the comment start char has occurred inside our string. (EL)
'(matlab-font-lock-comment-match 1 font-lock-comment-face)
;; Various pragmas should be in different colors.
;; I think pragmas are always lower case?
'("%#\\([a-z]+\\)" (1 'bold prepend))
;; General keywords
(list
(if (fboundp 'regexp-opt)
(concat "\\<\\(" (regexp-opt matlab-keyword-list) "\\)\\>")
;; Original hard-coded value for pre Emacs 20.1
"\\<\\(break\\|ca\\(se\\|tch\\)\\|e\\(lse\\(\\|if\\)\\|ndfunction\\)\
\\|\\(par\\)?for\\|spmd\\|global\\|if\\|otherwise\\|return\\|switch\\|try\\|while\\|tic\\|toc\\)\\>")
'(0 font-lock-keyword-face))
;; The end keyword is only a keyword when not used as an array
;; dereferencing part.
'("\\(^\\|[;,]\\)[ \t]*\\(end\\)\\b"
2 (if (matlab-valid-end-construct-p) font-lock-keyword-face nil))
;; How about unreachable code? MUsT BE AFTER KEYWORDS in order to
;; get double-highlighting.
'(matlab-find-unreachable-code
(1 'underline prepend) ;if part
(2 'underline prepend) ;end part
(3 'underline prepend) ;else part (if applicable)
(4 font-lock-comment-face prepend) ;commented out part.
)
;; block comments need to be commented out too!
'(matlab-find-block-comments
(1 font-lock-comment-face prepend) ; commented out
(2 'underline prepend)
(3 'underline prepend) ;the comment parts
)
;; Cell mode breaks get special treatment
'("^\\s-*\\(%%[^\n]*\n\\)" (1 matlab-cellbreak-face append))
;; Highlight cross function variables
'(matlab-font-lock-cross-function-variables-match
(1 matlab-cross-function-variable-face prepend))
;; Highlight nested function/end keywords
'(matlab-font-lock-nested-function-keyword-match
(0 matlab-nested-function-keyword-face prepend))
;; The global keyword defines some variables. Mark them.
'("^\\s-*global\\s-+"
("\\(\\w+\\)\\(\\s-*=[^,; \t\n]+\\|[, \t;]+\\|$\\)"
nil nil (1 font-lock-variable-name-face)))
;; Handle graphics stuff
(list
(if (fboundp 'regexp-opt)
(concat "\\<\\(" (regexp-opt matlab-handle-graphics-list) "\\)\\>")
;; The original regular expression for pre Emacs 20.1
"\\<\\(ax\\(es\\|is\\)\\|figure\\|get\\|image\\|li\\(ght\\|ne\\)\\|\
patch\\|s\\(et\\(\\|color\\|font\\)\\|urface\\)\\|text\\|\
ui\\(cont\\(ext\\(\\|menu\\)\\|rol\\)\\|menu\\|\
\\(toggle\\|push\\)tool\\|toolbar\\)\\)\\>")
'(0 font-lock-type-face))
)
"Expressions to highlight in MATLAB mode.")
(defconst matlab-function-arguments
"\\(([^)]*)\\)?\\s-*\\([,;\n%]\\|$\\)")
(defvar matlab-gaudy-font-lock-keywords
(append
matlab-font-lock-keywords
(list
;; defining a function, a (possibly empty) list of assigned variables,
;; function name, and an optional (possibly empty) list of input variables
(list (concat "^\\s-*\\(function\\)\\>[ \t\n.]*"
"\\(\\[[^]]*\\]\\|\\sw+\\)[ \t\n.]*"
"=[ \t\n.]*\\(\\sw+\\)[ \t\n.]*"
matlab-function-arguments)
'(1 font-lock-keyword-face append)
'(2 font-lock-variable-name-face append)
'(3 font-lock-function-name-face append))
;; defining a function, a function name, and an optional (possibly
;; empty) list of input variables
(list (concat "^\\s-*\\(function\\)[ \t\n.]+"
"\\(\\sw+\\)[ \t\n.]*"
matlab-function-arguments)