-
Notifications
You must be signed in to change notification settings - Fork 0
/
matlab.el
5793 lines (5415 loc) · 216 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 dot-m files
;;
;; Author: Matt Wette <[email protected]>,
;; Eric M. Ludlam <[email protected]>
;; Maintainer: Eric M. Ludlam <[email protected]>
;; Created: 04 Jan 91
;; Version: 2.3.1
;; Keywords: MATLAB
;;
;; Copyright (C) 1997-2003 Eric M. Ludlam
;; 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 dot-m
;; files. It automatically indents for block structures, 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 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.
;;; Finding Updates:
;;
;; The latest stable version of matlab.el can be found on MATLAB Central:
;;
;; Catagory:
;; http://www.mathworks.com/matlabcentral/fileexchange/loadCategory.do?objectId=19&objectType=Category
;; This File
;; http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=104&objectType=file
;;
;; Older versions of matlab.el can be found in as matlab.el.1.10.1
;; for emacsen that do not have the latest additional utilities such
;; as tempo and derived.
;;; Installation:
;;
;; Put the this file as "matlab.el" somewhere on your load path, then
;; add this to your .emacs or site-init.el file:
;;
;; (autoload 'matlab-mode "matlab" "Enter MATLAB mode." t)
;; (setq auto-mode-alist (cons '("\\.m\\'" . matlab-mode) auto-mode-alist))
;; (autoload 'matlab-shell "matlab" "Interactive MATLAB mode." t)
;;
;; User Level customizations (You need not use them all):
;; (setq matlab-indent-function t) ; if you want function bodies indented
;; (setq matlab-verify-on-save-flag nil) ; turn off auto-verify on save
;; (defun my-matlab-mode-hook ()
;; (setq fill-column 76)) ; where auto-fill should wrap
;; (add-hook 'matlab-mode-hook 'my-matlab-mode-hook)
;; (defun my-matlab-shell-mode-hook ()
;; '())
;; (add-hook 'matlab-shell-mode-hook 'my-matlab-shell-mode-hook)
;;
;; If you are using a version of MATLAB with the Desktop enabled,
;; you may need to add this:
;;
;; (setq matlab-shell-command-swithes '("-nojvm"))
;;
;; Please read the mode help for matlab-mode for additional
;; configuration options.
;;
;; Syntax highlighting:
;; To get font-lock try adding this for older emacsen:
;; (font-lock-mode 1)
;; Or for newer versions of Emacs:
;; (global-font-lock-mode t)
;; To get hilit19 support try adding:
;; (matlab-mode-hilit)
;;
;; This package requires easymenu, tempo, and derived.
;; This package will optionally use custom, shell, and gud.
;; This package supports language specific extensions in imenu, func-menu,
;; speedbar, font-lock, and hilit19.
;;; Mailing List:
;;
;; A mailing list has been set up where beta versions of matlab.el are
;; posted, and where comments, questions, bug reports, and answers to
;; questions can be sent.
;;
;; To subscribe send email to "[email protected]" with a body of:
;; "subscribe matlab-emacs"
;; to unsubscribe, send email with the body of: "unsubscribe matlab-emacs"
;;; Code:
(require 'easymenu)
(require 'tempo)
(require 'derived)
(defconst matlab-mode-version "2.3"
"Current version of MATLAB mode.")
;; From custom web page for compatibility between versions of custom:
(eval-and-compile
(condition-case ()
(require 'custom)
(error nil))
(if (and (featurep 'custom) (fboundp 'custom-declare-variable))
nil ;; We've got what we needed
;; We have the old custom-library, hack around it!
(defmacro defgroup (&rest args)
nil)
(defmacro custom-add-option (&rest args)
nil)
(defmacro defface (&rest args) nil)
(defmacro defcustom (var value doc &rest args)
(` (defvar (, var) (, value) (, doc))))))
;; 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-delete-overlay 'delete-extent)
(defalias 'matlab-overlay-start 'extent-start)
(defalias 'matlab-overlay-end 'extent-end)
(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 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-delete-overlay 'delete-overlay)
(defalias 'matlab-overlay-start 'overlay-start)
(defalias 'matlab-overlay-end 'overlay-end)
(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
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 (featurep 'matlab-eei)
matlab-eei-process))
;;; User-changeable variables =================================================
;; Variables which the user can change
(defgroup matlab nil
"MATLAB mode."
:prefix "matlab-"
:group 'languages)
(defcustom matlab-indent-level 2
"*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 '(1 . 1)
"*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."
:group 'matlab
:type 'sexp)
(defcustom matlab-indent-function nil
"*If non-nil, indent body of function."
:group 'matlab
:type 'boolean)
(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)
"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-block-matches-forward
matlab-mode-vf-block-matches-backward
matlab-mode-vf-quietafy-buffer
))))
(defcustom matlab-block-verify-max-buffer-size 50000
"*Largest buffer size allowed for block verification during save."
:group 'matlab
:type 'integer)
(defcustom matlab-vers-on-startup t
"*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-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)))
;; 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.")
(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.")
)
;; 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)
)
(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 "\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 [(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 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), and mark it as a comment.
The if(0) and else/end construct sould be highlighted differently.
Argument LIMIT is the maximum distance to search."
(if (and (< (point) limit)
(re-search-forward
"\\<\\(if\\>\\s-*(?\\s-*0\\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)))))
(defcustom matlab-keyword-list '("global" "persistent" "for" "while"
"if" "elseif" "else"
"endfunction" "return" "break" "continue"
"switch" "case" "otherwise" "try"
"catch" "tic" "toc")
"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\\)\
\\|for\\|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.
)
;; 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.")
(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.]*"
"\\(([^)]*)\\)?\\s-*\\([,;\n%]\\|$\\)")
'(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.]*"
"\\(([^)]*)\\)?\\s-*\\([,;\n%]\\|$\\)")
'(1 font-lock-keyword-face append)
'(2 font-lock-function-name-face append))
;; Anchor on the function keyword, highlight params
(list (concat "^\\s-*function\\>[ \t\n.]*"
"\\(\\(\\[[^]]*\\]\\|\\sw+\\)[ \t\n.]*=[ \t\n.]*\\)?"
"\\sw+\\s-*(")
'("\\s-*\\(\\sw+\\)\\s-*[,)]" nil nil
(1 font-lock-variable-name-face)))
;; I like variables for FOR loops
'("\\<\\(for\\)\\s-+\\(\\sw+\\)\\s-*=\\s-*\
\\(\\([^\n,;%(]+\\|([^\n%)]+)\\)+\\)"
(1 font-lock-keyword-face)
(2 font-lock-variable-name-face append)
(3 font-lock-reference-face append))
;; Items after a switch statements are cool
'("\\<\\(case\\|switch\\)\\s-+\\({[^}\n]+}\\|[^,%\n]+\\)"
(1 font-lock-keyword-face) (2 font-lock-reference-face))
;; How about a few matlab constants such as pi, infinity, and sqrt(-1)?
;; The ^>> is in case I use this in an interactive mode someday
'("\\<\\(eps\\|pi\\|inf\\|Inf\\|NaN\\|nan\\|ans\\|i\\|j\\|^>>\\)\\>"
1 font-lock-reference-face)
'("\\<[0-9]\\.?\\(i\\|j\\)\\>" 1 font-lock-reference-face)
;; Define these as variables since this is about as close
;; as matlab gets to variables
(list (concat "\\<" matlab-indent-past-arg1-functions "\\s-*")
'("(\\s-*\\(\\w+\\)\\s-*\\(,\\|)\\)" nil nil
(1 font-lock-variable-name-face)))
))
"Expressions to highlight in MATLAB mode.")
(defvar matlab-really-gaudy-font-lock-keywords
(append
matlab-gaudy-font-lock-keywords
(list
;; Since it's a math language, how bout dem symbols?
'("\\([<>~]=?\\|\\.[/*^']\\|==\\|\\<xor\\>\\|[-!^&|*+\\/~:]\\)"
1 font-lock-type-face)
;; How about references in the HELP text.
(list (concat "^" matlab-comment-line-s "\\s-*"
"\\(\\([A-Z]+\\s-*=\\s-+\\|\\[[^]]+]\\s-*=\\s-+\\|\\)"
"\\([A-Z][0-9A-Z]+\\)\\(([^)\n]+)\\| \\)\\)")
'(1 font-lock-reference-face prepend))
(list (concat "^" matlab-comment-line-s "\\s-*"
"See also\\s-+")
'("\\([A-Z][A-Z0-9]+\\)\\([,.]\\| and\\|$\\) *" nil nil
(1 font-lock-reference-face prepend)))
(list (concat "^" matlab-comment-line-s "\\s-*"
"\\(\\$" "Revision" "[^\n$]+\\$\\)")
'(1 font-lock-reference-face prepend))
;; continuation ellipsis.
'("[^.]\\(\\.\\.\\.+\\)\\([^\n]*\\)" (1 'underline)
(2 font-lock-comment-face))
;; How about debugging statements?
;;'("\\<\\(db\\sw+\\)\\>" 1 'bold)
(list
(if (fboundp 'regexp-opt)
(concat "\\<\\(" (regexp-opt matlab-debug-list) "\\)\\>")
;; pre-regexp-opt days.
"\\<\\(db\\(c\\(lear\\|ont\\)\\|down\\|mex\\|quit\\|\
st\\(a\\(ck\\|tus\\)\\|ep\\|op\\)\\|type\\|up\\)\\)\\>")
'(0 'bold)))
(if matlab-handle-simulink
;; Simulink functions, but only if the user wants it.
(list (list (concat "\\<\\(\\([sg]et_param\\|sim\\([gs]et\\)?\\|"
"\\(mld\\|ss\\)[A-Z]\\w+\\)\\|"
"\\(new\\|open\\|close\\|save\\|find\\)_system\\|"
"\\(add\\|delete\\|replace\\)_\\(block\\|line\\)\\|"
"simulink\\|bd\\(root\\|close\\)"
"\\)\\>")
1 matlab-simulink-keyword-face))
nil))
"Expressions to highlight in MATLAB mode.")
(defvar matlab-shell-font-lock-keywords
(list
;; How about Errors?
'("^\\(Error in\\|Syntax error in\\)\\s-+==>\\s-+\\(.+\\)$"
(1 font-lock-comment-face) (2 font-lock-string-face))
;; and line numbers
'("^\\(On line [0-9]+\\)" 1 font-lock-comment-face)
;; User beep things
'("\\(\\?\\?\\?[^\n]+\\)" 1 font-lock-comment-face)
;; Useful user commands, but not useful programming constructs
'("\\<\\(demo\\|whatsnew\\|info\\|subscribe\\|help\\|doc\\|lookfor\\|what\
\\|whos?\\|cd\\|clear\\|load\\|save\\|helpdesk\\|helpwin\\)\\>"
1 font-lock-keyword-face)
;; Various notices
'(" M A T L A B " 0 'underline)
'("All Rights Reserved" 0 'italic)
'("\\((c)\\s-+Copyright[^\n]+\\)" 1 font-lock-comment-face)
'("\\(Version\\)\\s-+\\([^\n]+\\)"
(1 font-lock-function-name-face) (2 font-lock-variable-name-face))
)
"Additional keywords used by MATLAB when reporting errors in interactive\
mode.")
;; hilit19 patterns
(defvar matlab-hilit19-patterns
'(
("\\(^\\|[^%]\\)\\(%[ \t].*\\|%\\)$" 2 comment)
("\\(^\\|[;,]\\)[ \t]*\\(\
function\\|global\\|for\\|while\\|if\\|elseif\\|else\\|end\\(function\\)?\
\\|return\\|switch\\|case\\|otherwise\\|try\\|catch\\)\\b" 2 keyword)))
;; func-menu support for matlab
(defvar fume-function-name-regexp-matlab
"^\\s-*function\\>[ \t\n.]*\\(\\(\\[[^]]*\\]\\|\\sw+\\)[ \t\n.]*\
=\[ \t\n.]*\\)?\\([a-zA-Z0-9_]+\\)"
"Expression to get matlab function/procedure names.")
;; "The connection between a mode and the regexp that matches function
(if (boundp 'fume-function-name-regexp-alist)
(add-to-list 'fume-function-name-regexp-alist
'(matlab-mode . fume-function-name-regexp-matlab)))
;;; Specialised routine to find the next MATLAB function or subroutine.
(defun fume-find-next-matlab-function-name (buffer)
"Searches for the next matlab function in BUFFER."
(fume-find-next-sexp buffer))
(if (boundp 'fume-function-name-regexp-alist)
(add-to-list 'fume-find-function-name-method-alist
'(matlab-mode . fume-find-next-matlab-function-name)))
;; Imenu support. Recycle the expression for fume since they are the
;; same, and this reduced code duplication.
(defvar matlab-imenu-generic-expression
`((nil ,fume-function-name-regexp-matlab 3))
"Expressions which find function headings in MATLAB M files.")
;;; MATLAB mode entry point ==================================================
;;;###autoload
(defun matlab-mode ()
"MATLAB-mode is a major mode for editing MATLAB dot-m files.
\\<matlab-mode-map>
Convenient editing commands are:
\\[matlab-comment-region] - Comment/Uncomment out a region of code.
\\[matlab-fill-comment-line] - Fill the current comment line.
\\[matlab-fill-region] - Fill code and comments in region.
\\[matlab-fill-paragraph] - Refill the current command or comment.
\\[matlab-complete-symbol] - Symbol completion of matlab symbols\
based on the local syntax.
Convenient navigation commands are:
\\[matlab-beginning-of-command] - Move to the beginning of a command.
\\[matlab-end-of-command] - Move to the end of a command.
\\[matlab-beginning-of-defun] - Move to the beginning of a function.
\\[matlab-end-of-defun] - Move do the end of a function.
\\[matlab-forward-sexp] - Move forward over a syntactic block of code.
\\[matlab-backward-sexp] - Move backwards over a syntactic block of code.
Convenient template insertion commands:
\\[tempo-template-matlab-function] - Insert a function definition.
\\[tempo-template-matlab-if] - Insert an IF END block.
\\[tempo-template-matlab-for] - Insert a FOR END block.
\\[tempo-template-matlab-switch] - Insert a SWITCH END statement.
\\[matlab-insert-next-case] - Insert the next CASE condition in a SWITCH.
\\[matlab-insert-end-block] - Insert a matched END statement. With \
optional ARG, reindent.
\\[matlab-stringify-region] - Convert plaintext in region to a string \
with correctly quoted chars.
Variables:
`matlab-indent-level' Level to indent blocks.
`matlab-cont-level' Level to indent continuation lines.
`matlab-cont-requires-ellipsis' Does your MATLAB support implied elipsis.
`matlab-case-level' Level to unindent case statements.
`matlab-indent-past-arg1-functions'
Regexp of functions to indent past the first
argument on continuation lines.
`matlab-maximum-indents' List of maximum indents during lineups.
`matlab-comment-column' Goal column for on-line comments.
`fill-column' Column used in auto-fill.
`matlab-indent-function' If non-nil, indents body of MATLAB functions.
`matlab-return-function' Customize RET handling with this function
`matlab-auto-fill' Non-nil, do auto-fill at startup
`matlab-fill-code' Non-nil, auto-fill code.
`matlab-fill-strings' Non-nil, auto-fill strings.
`matlab-verify-on-save-flag' Non-nil, enable code checks on save
`matlab-highlight-block-match-flag'
Enable matching block begin/end keywords
`matlab-vers-on-startup' If t, show version on start-up.
`matlab-handle-simulink' If t, enable simulink keyword highlighting.
All Key Bindings:
\\{matlab-mode-map}"
(interactive)
(kill-all-local-variables)
(use-local-map matlab-mode-map)
(setq major-mode 'matlab-mode)
(setq mode-name "MATLAB")
(if (boundp 'whitespace-modes)
(add-to-list 'whitespace-modes 'matlab-mode))
(setq local-abbrev-table matlab-mode-abbrev-table)
(set-syntax-table matlab-mode-syntax-table)
(setq indent-tabs-mode nil)
(make-local-variable 'indent-line-function)
(setq indent-line-function 'matlab-indent-line)
(make-local-variable 'paragraph-start)
(setq paragraph-start (concat "^$\\|" page-delimiter))
(make-local-variable 'paragraph-separate)
(setq paragraph-separate paragraph-start)
(make-local-variable 'paragraph-ignore-fill-prefix)
(setq paragraph-ignore-fill-prefix t)