-
Notifications
You must be signed in to change notification settings - Fork 8
/
modelica-mode.el
executable file
·1223 lines (1145 loc) · 40.6 KB
/
modelica-mode.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
;;; modelica-mode.el --- Major mode for editing Modelica files
;; Copyright (C) 2022- Rudolf Schlatte
;; Copyright (C) 2010- Dietmar Winkler
;; Copyright (C) 1997--2001 Ruediger Franke
;; Copyright (C) 1997--2001 Free Software Foundation, Inc.
;; Original author: Ruediger Franke <[email protected]>
;; URL: https://github.com/modelica-tools/modelica-mode
;; Version: 2.0.0
;; Package-Requires: ((emacs "27.1"))
;; Keywords: languages, continuous system modeling
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This package provides a fundamental Modelica mode.
;; It covers:
;; - show / hide of annotations
;; C-c C-s show annotation of current statement
;; C-c C-h hide annotation of current statement
;; C-c M-s show all annotations
;; C-c M-h hide all annotations
;;
;; - indentation of lines, e.g.
;; TAB indent current line
;; C-M-\ indent current region
;; C-j indent current line, create a new line, indent it
;; (like TAB ENTER TAB)
;;
;; - automatic insertion of end statements
;; C-c C-e search backwards for the last unended begin of a code block,
;; insert the according end-statement
;;
;; - move commands which know about statements and statement blocks
;; M-e move to next beginning of a statement
;; M-a move to previous beginning of a statement
;; M-n move to next beginning of a statement block
;; M-p move to previous beginning of a statement block
;; C-M-a move to beginning of current statement block
;; C-M-e move to end of current statement block
;;
;; - commands for writing comments treat documentation strings as well
;; M-; insert a comment for current statement (standard Emacs)
;; M-" insert a documentation string for current statement
;; M-j continue comment or documentation string on next line
;;
;; - syntax highlighting using font-lock-mode
;;
;; Current limitations:
;; - conditional expessions are only supported on right hand sides of
;; equations; otherwise simple expressions are assumed
;;
;; Installation:
;; (1) Put the file
;; modelica-mode.el
;; to an Emacs Lisp directory, e.g. ~/elisp
;;
;; (2) Add the following lines to your ~/.emacs file
;;
;; (add-to-list 'load-path "~/elisp")
;; (autoload 'modelica-mode "modelica-mode" "Modelica Editing Mode" t)
;; (add-to-list 'auto-mode-alist '("\\.mo\\'" . modelica-mode))
;;
;; (3) Activate the mode by loading a file with the extension ".mo"
;; or by invoking
;; M-x modelica-mode
;;
;; (4) Optionally byte-compile the Lisp code
;;
;; (5) Please send comments and suggestions to
;; Ruediger Franke <[email protected]>
(require 'newcomment)
(require 'easymenu)
;;; Code:
(defconst modelica-mode-version "2.0.0")
;;; customization
(defgroup modelica nil
"Major mode for editing Modelica code."
:group 'languages)
(defcustom modelica-mode-hook nil
"Hook run after entering `modelica-mode'."
:type 'hook
:options (list 'hs-minor-mode)
:group 'modelica)
(defcustom modelica-use-emacs-keybindings t
"Choose whether to use Emacs standard or original keybindings.
If true, use keybindings similar to other programming modes. If
false, use original `modelica-mode' keybindings; those override
some standard Emacs keybindings."
:type 'boolean
:safe t
:group 'modelica)
(defcustom modelica-basic-offset 2
"Basic offset for indentation in Modelica Mode."
:type 'integer
:safe 'integerp
:group 'modelica)
(defcustom modelica-comment-offset 3
"Offset for indentation in comments in Modelica Mode."
:type 'integer
:safe 'integerp
:group 'modelica)
(defcustom modelica-statement-offset 2
"Offset for indentation in statements in Modelica Mode."
:type 'integer
:safe 'integerp
:group 'modelica)
;;; constants
(defconst modelica-class-modifier-keyword
"\\(encapsulated\\|final\\|inner\\|outer\\|partial\\|re\\(declare\\|placeable\\)\\)[ \t\n\r]+"
"*Keyword regexp optionally found before a class keyword.")
(defconst modelica-class-keyword
"\\(block\\|c\\(lass\\|onnector\\)\\|function\\|model\\|package\\|record\\|type\\)[ \t\n\r]+"
"*Keyword regexp preceding a Modelica class declaration or definition.")
;;; Interface to font-lock
(defvar modelica-font-lock-keywords nil
"Keywords to highlight for Modelica. See variable `font-lock-keywords'.")
(if modelica-font-lock-keywords
()
(setq modelica-font-lock-keywords
(list
(list (concat "\\<"
"\\(do\\|"
"\\(end[ \t\n]+\\(if\\|for\\|wh\\(en\\|ile\\)\\)\\)\\|"
; (regexp-opt
; '("import" "within" "extends"
; "for" "while" "in" "loop" "when"
; "if" "then" "else" "elseif" "elsewhen"
; "and" "not" "or"))
"and\\|e\\(lse\\(if\\|when\\)?\\|xtends\\)\\|for\\|"
"i\\(mport\\|[fn]\\)\\|loop\\|not\\|or\\|then\\|"
"w\\(h\\(en\\|ile\\)\\|ithin\\)"
"\\)\\>")
0 'font-lock-keyword-face)
(list (concat "\\<"
;(regexp-opt
; '("algorithm" "equation" "public" "protected") t)
"\\(algorithm\\|equation\\|p\\(rotected\\|ublic\\)\\)"
"\\>")
0 'font-lock-keyword-face)
(list (concat "\\<"
;(regexp-opt
; '("redeclare" "final" "partial" "replaceable"
; "inner" "outer" "encapsulated"
; "discrete" "parameter" "constant"
; "flow" "input" "output" "external"
; "block" "class" "connector" "function" "model"
; "package" "record" "type"
; "end") t)
"\\(block\\|c\\(lass\\|on\\(nector\\|stant\\)\\)\\|"
"discrete\\|e\\(n\\(capsulated\\|d\\)\\|xternal\\)\\|"
"f\\(inal\\|low\\|unction\\)\\|in\\(ner\\|put\\)\\|"
"model\\|out\\(er\\|put\\)\\|pa\\(ckage\\|r\\(ameter\\|"
"tial\\(\\)?\\)\\)\\|re\\(cord\\|declare\\|"
"placeable\\)\\|type\\)"
"\\>")
0 'font-lock-type-face)
(list (concat "\\<"
;(regexp-opt
; '("der" "analysisType" "initial" "terminal"
; "noEvent" "samle" "pre" "edge" "change"
; "reinit" "abs" "sign" "sqrt" "div" "mod"
; "rem" "ceil" "floor" "integer" "delay"
; "cardinality"
; "promote" "ndims" "size" "scalar" "vector" "matrix"
; "transpose" "outerProduct" "identity" "diagonal"
; "zeros" "ones" "fill" "linspace" "min" "max" "sum"
; "product" "symmetric" "cross" "skew"
;) t)
"\\(a\\(bs\\|nalysisType\\)\\|c\\(ardinality\\|eil\\|"
"hange\\|ross\\)\\|d\\(e\\(lay\\|r\\)\\|i\\(agonal\\|"
"v\\)\\)\\|edge\\|f\\(ill\\|loor\\)\\|i\\(dentity\\|"
"n\\(itial\\|teger\\)\\)\\|linspace\\|m\\(a\\(trix\\|"
"x\\)\\|in\\|od\\)\\|n\\(dims\\|oEvent\\)\\|o\\(nes\\|"
"uterProduct\\)\\|pr\\(e\\|o\\(duct\\|mote\\)\\)\\|"
"re\\(init\\|m\\)\\|s\\(amle\\|calar\\|i\\(gn\\|"
"ze\\)\\|kew\\|qrt\\|um\\|ymmetric\\)\\|t\\(erminal\\|"
"ranspose\\)\\|vector\\|zeros\\)"
"\\>")
0 'font-lock-function-name-face)
(list (concat "\\<"
;(regexp-opt
; '("assert" "terminate") t)
"\\(assert\\|terminate\\)"
"\\>")
0 'font-lock-warning-face)
(list (concat "\\<"
;(regexp-opt
; '("annotation" "connect") t)
"\\(annotation\\|connect\\)"
"\\>")
0 (identity 'font-lock-builtin-face))
(list (concat "\\<"
;(regexp-opt
; '("false" "true") t)
"\\(false\\|true\\)"
"\\>")
0 (identity 'font-lock-constant-face))
(list (concat "\\<"
;(regexp-opt
; '("time") t)
"\\(time\\)"
"\\>")
0 'font-lock-variable-name-face))))
;;; The mode
(defvar modelica-mode-syntax-table nil
"Syntax table used while in Modelica mode.")
(defvar modelica-mode-abbrev-table nil
"Abbrev table used while in Modelica mode.")
(define-abbrev-table 'modelica-mode-abbrev-table ())
(if modelica-mode-syntax-table
() ; Do not change the table if it is already set up.
(setq modelica-mode-syntax-table (make-syntax-table))
(modify-syntax-entry ?_ "w" modelica-mode-syntax-table)
(modify-syntax-entry ?. "w" modelica-mode-syntax-table)
(modify-syntax-entry ?/ ". 124b" modelica-mode-syntax-table)
(modify-syntax-entry ?* ". 23" modelica-mode-syntax-table)
(modify-syntax-entry ?\n "> b" modelica-mode-syntax-table)
(modify-syntax-entry ?\' "\"" modelica-mode-syntax-table))
(defvar modelica-original-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "\C-j" 'modelica-newline-and-indent)
(define-key map "\C-c\C-e" 'modelica-insert-end)
(define-key map "\C-c\C-s" 'modelica-show-annotation)
(define-key map "\C-c\C-a" 'modelica-hide-annotation)
(define-key map "\es" 'modelica-show-all-annotations)
(define-key map "\ea" 'modelica-hide-all-annotations)
(define-key map "\C-c\C-c" 'comment-region)
(define-key map "\e\"" 'modelica-indent-for-docstring)
(define-key map "\e;" 'modelica-indent-for-comment)
(define-key map "\ej" 'modelica-indent-new-comment-line)
(define-key map "\ef" 'modelica-forward-statement)
(define-key map "\eb" 'modelica-backward-statement)
(define-key map "\en" 'modelica-forward-block)
(define-key map "\ep" 'modelica-backward-block)
(define-key map "\ea" 'modelica-to-block-begin)
(define-key map "\ee" 'modelica-to-block-end)
map)
"Original keymap for `modelica-mode'.
This keymap overrides some standard Emacs keybindings.")
(defvar modelica-new-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-j") 'modelica-newline-and-indent)
(define-key map (kbd "C-c C-e") 'modelica-insert-end)
(define-key map (kbd "C-c C-s") 'modelica-show-annotation)
(define-key map (kbd "C-c C-a") 'modelica-hide-annotation)
(define-key map (kbd "C-c M-s") 'modelica-show-all-annotations)
(define-key map (kbd "C-c M-a") 'modelica-hide-all-annotations)
(define-key map (kbd "C-c C-c") 'comment-region)
(define-key map (kbd "M-\"") 'modelica-indent-for-docstring)
(define-key map (kbd "M-;") 'modelica-indent-for-comment)
(define-key map (kbd "M-j") 'modelica-indent-new-comment-line)
(define-key map (kbd "M-a") 'modelica-backward-statement)
(define-key map (kbd "M-e") 'modelica-forward-statement)
(define-key map (kbd "M-n") 'modelica-forward-block)
(define-key map (kbd "M-p") 'modelica-backward-block)
(define-key map (kbd "C-M-a") 'modelica-to-block-begin)
(define-key map (kbd "C-M-e") 'modelica-to-block-end)
map)
"New-style keymap for `modelica-mode'.
This keymap tries to adhere to Emacs keybindings conventions.")
(defvar modelica-mode-map
(if modelica-use-emacs-keybindings
modelica-new-mode-map
modelica-original-mode-map)
"Keymap for `modelica-mode'.")
(defvar modelica-mode-menu
'("Modelica"
("Move to"
[" - next statement" modelica-forward-statement t]
[" - previous statement" modelica-backward-statement t]
[" - start of code block" modelica-to-block-begin t]
[" - end of code block" modelica-to-block-end t])
[" - next code block" modelica-forward-block t]
[" - previous code block" modelica-backward-block t]
"-"
("Annotation"
[" - show all" modelica-show-all-annotations t]
[" - hide all" modelica-hide-all-annotations t])
[" - show current" modelica-show-annotation t]
[" - hide current" modelica-hide-annotation
:keys "C-c C-h" :active t]
"-"
("Indent"
[" - for comment" modelica-indent-for-comment t]
[" - for docstring" modelica-indent-for-docstring t]
["Newline and indent" modelica-newline-and-indent
:keys "C-j" :active t]
["New comment line" modelica-indent-new-comment-line t])
[" - line" indent-for-tab-command t]
[" - region" indent-region (mark)]
"-"
["Comment out region" comment-region (mark)]
["Uncomment region" (comment-region (point) (mark) '(4))
:keys "C-u C-c C-c" :active (mark)]
"-"
["End code block" modelica-insert-end t])
"Menu for Modelica mode.")
(when (featurep 'hideshow)
(unless (assoc 'modelica-mode hs-special-modes-alist) ;; one could also use `cl-pushnew'
(push
(list
'modelica-mode
(list
(concat "\\(?:" modelica-class-modifier-keyword "\\)?\\(?1:" modelica-class-keyword "\\)")
1)
"\\_<end\\_>[[:blank:]][^[:blank:]]+[[:blank:]]*;"
nil
#'modelica-to-block-end)
hs-special-modes-alist)))
;;;###autoload
(define-derived-mode modelica-mode prog-mode "Modelica"
"Major mode for editing Modelica files."
:group 'modelica
:syntax-table modelica-mode-syntax-table
:abbrev-table modelica-mode-abbrev-table
;; Allow switching between original and new keybindings just by setting
;; `modelica-use-emacs-keybindings' and reverting a modelica buffer
(setq modelica-mode-map
(if modelica-use-emacs-keybindings
modelica-new-mode-map
modelica-original-mode-map))
(use-local-map modelica-mode-map)
(setq-local indent-line-function #'modelica-indent-line)
;; comment syntax
(setq-local comment-column 32
comment-start "// "
comment-start-skip "/\\*+ *\\|// *"
comment-end ""
comment-multi-line nil)
;; settings for font-lock-mode
(setq-local font-lock-keywords modelica-font-lock-keywords)
;; font-lock-mode for newer GNU Emacs versions
(setq-local font-lock-defaults '(modelica-font-lock-keywords nil nil))
;; hide/show annotations
(setq-local line-move-ignore-invisible t)
(add-to-invisibility-spec '(modelica-annotation . t))
(modelica-hide-all-annotations)
(easy-menu-define modelica-mode-menu-symbol
modelica-mode-map
"Menu for Modelica mode"
modelica-mode-menu))
(defun modelica-indent-for-comment ()
"Indent this line's comment to `comment-column', or insert an empty comment."
(interactive)
(indent-for-comment)
(modelica-indent-line))
(defun modelica-indent-for-docstring ()
"Indent this statement's documentation string to `comment-column'.
Insert an empty documentation string if necessary."
(interactive)
(let ((deleted "") save-point)
;; move behind current statement
(skip-chars-forward " \t")
(condition-case nil
(progn
(modelica-forward-statement)
(forward-comment (- (point-max))))
(error
(progn
(end-of-line)
(skip-chars-backward " \t"))))
;; remove ending ";", if any, and store it in "deleted"
(if (or (looking-at "[ \t\n]")
(eobp))
(forward-char -1))
(if (looking-at ";")
(progn
(delete-char 1)
(setq deleted ";"))
(forward-char 1))
;; move backwards to last non-blank
(skip-chars-backward " \t")
(if (or (looking-at "[ \t\n]")
(eobp))
(forward-char -1))
(if (looking-at "\"")
;; indent docstring
(progn
(forward-char 1)
(insert deleted)
(forward-char (- (1+ (length deleted))))
(modelica-within-string t)
(while (modelica-behind-string t))
(setq save-point (point))
(skip-chars-backward " \t")
(delete-region (point) save-point)
(indent-to (max comment-column (1+ (current-column))))
(forward-char 1))
;; insert new docstring
(forward-char 1)
(indent-to (max comment-column (1+ (current-column))))
(insert "\"\"" deleted)
(forward-char (- (1+ (length deleted))))))
(modelica-indent-line))
(defun modelica-indent-new-comment-line ()
"Indent new comment line for Modelica mode.
Same behavior as `indent-new-comment-line', but additionally
considers documentation strings."
(interactive)
(modelica-indent-line)
(let (starter)
(cond
;; treat documentation string
((modelica-within-string)
(insert "\"\n\""))
;; adapt comment-multi-line and
;; call default indent-new-comment-line
(t
(setq starter (modelica-within-comment))
(if (equal starter "/*")
(setq comment-multi-line t)
(setq comment-multi-line nil))
(indent-new-comment-line))))
(modelica-indent-line))
(defun modelica-indent-line ()
"Indentation for Modelica."
(let ((pos (- (point-max) (point))) beg beg-anno end-anno)
(beginning-of-line)
(setq beg (point))
;; no indentation of invisible text (hidden annotations)
(if (modelica-within-overlay 'invisible)
()
;; no indentation if preceeding newline is quoted
(if (and (> (point) 2)
(progn
(forward-char -2)
(looking-at "[\\]\n")))
(forward-char 2)
;; else indent line
(goto-char beg)
(skip-chars-forward " \t")
(let ((indent (modelica-calculate-indent)))
(if (= indent (current-column))
;; nothing to be done
()
(delete-region beg (point))
(indent-to indent)))))
;; return to the old position inside the line
(if (> (- (point-max) pos) (point))
(goto-char (- (point-max) pos)))))
(defun modelica-calculate-indent ()
"Calculate indentation for current line.
Assumes point to be over the first non-blank of the line."
(save-excursion
(let ((case-fold-search nil)
offset (last-open nil) (save-point (point))
ref-point ref-column)
(cond
;; multi-line comment has fixed indentation, relative to its start
((modelica-within-comment t)
(setq ref-column (current-column))
(goto-char save-point)
(if (looking-at "\\*/")
ref-column
(+ ref-column modelica-comment-offset)))
;; concatenation of strings
((and (looking-at "\"")
(modelica-behind-string t))
;; move point to the very first string constant
;; in order to consider concatenation on the same line
(while (modelica-behind-string t))
(current-column))
;; continued single-line comment
((and (looking-at "//")
(forward-comment -1)
(looking-at "//"))
(current-column))
;; default looks for last unended begin-like statement
(t
(goto-char save-point) ; needed after check for singele-line comments
(setq offset modelica-basic-offset)
;; goto left for labels, end's etc.
(if (looking-at
(concat
; ("algorithm" "elseif" "elsewhen" "end" "equation" "external"
; "in" "loop" "protected" "public")
"\\(algorithm\\|e\\(lse\\(if\\|when\\)\\|nd\\|quation\\|"
"xternal\\)\\|in\\|loop\\|p\\(rotected\\|ublic\\)\\)"
"\\>"))
(setq offset (- offset modelica-basic-offset)))
(if (and
(looking-at
(concat
; ("else" "then")
"\\(else\\|then\\)"
"\\>"))
(not (modelica-within-equation)))
(setq offset (- offset modelica-basic-offset)))
(condition-case nil
(progn
(modelica-last-unended-begin t)
;; correct offset
(if (looking-at "end\\>")
;; found an 'end', means no basic offset
(setq offset (- offset modelica-basic-offset)))
;; check indentation in statements
(setq ref-column (current-column))
(setq ref-point (point))
(goto-char save-point)
(modelica-statement-start ref-point)
(if (>= (point) save-point)
;; indent relative to ref-point as new statement starts
(max 0 (+ ref-column offset))
;; else add modelica-statement-offset
;; provided that point is behind ref-point
;; and point is not within a begin-like statement
(if (and (> (point) ref-point)
(not (and (modelica-forward-begin)
(> (point) save-point))))
(setq offset (+ offset modelica-statement-offset)))
(setq last-open
(car (cdr (parse-partial-sexp (point) save-point))))
(if (not last-open)
(max 0 (+ ref-column offset))
(goto-char last-open)
(+ 1 (current-column)))))
(error 0)))))))
(defun modelica-empty-line ()
"Return t if current line is empty, else return nil."
(save-excursion
(beginning-of-line)
(skip-chars-forward " \t")
(eolp)))
(defun modelica-within-comment (&optional move-point)
"Return comment starter if point is within a comment, nil otherwise.
Optionally move point to the beginning of the comment if
MOVE-POINT is true."
(when-let* ((syn (syntax-ppss))
((nth 4 syn))
(start (nth 8 syn)))
(when move-point
(goto-char start))
(buffer-substring-no-properties start (+ start 2))))
(defun modelica-within-single-line-comment (&optional move-point)
"Return comment starter if point is within a single-line comment.
Return nil otherwise. Optionally move point to the beginning of
the comment if MOVE-POINT is true."
(let ((starter nil) (save-point (point)))
;; check single-line comment
(condition-case nil
(if (and (re-search-backward "//\\|\n")
(looking-at "//"))
(setq starter "//"))
(error nil))
(if (and starter move-point)
(setq save-point (point)))
(goto-char save-point)
starter))
(defun modelica-within-string (&optional move-point)
"Return t if point is within a string constant, nil otherwise.
Optionally move point to the starting double quote of the string
if MOVE-POINT is true."
(if (and (boundp 'font-lock-mode) font-lock-mode)
;; use result of font-lock-mode to also cover multi-line strings
(let (within-string)
(setq within-string
(text-property-any (point) (min (+ (point) 1) (point-max))
'face 'font-lock-string-face))
(if (and within-string move-point)
(let ((point-next
(text-property-not-all (point) (point-max)
'face 'font-lock-string-face)))
(goto-char point-next)
(backward-sexp)))
within-string)
(let ((within-string nil) (save-point (point)) (start-point nil))
(condition-case nil
(while
(progn
(re-search-backward "\\(^\\|[^\\]\\)[\"\n]")
(looking-at "\\(^\\|.\\)\""))
(if (not (looking-at "^"))
(forward-char 1))
(if within-string
(setq within-string nil)
(setq within-string t)
(or start-point (setq start-point (point)))))
(error nil))
(if (and within-string move-point start-point)
(goto-char start-point)
(goto-char save-point))
within-string)))
(defun modelica-behind-string (&optional move-point)
"Check for string concatenation.
Return t if only blanks are between point and the preceeding
string constant, nil otherwise. Optionally move point to the
starting double quote of the preceeding string if MOVE-POINT is
true."
(let ((behind-string nil)
(save-point (point)))
(if (and (> (point) 1)
(progn
(skip-chars-backward " \t\n")
(if (> (point) 1)
(forward-char -1))
(looking-at "\""))
(modelica-within-string move-point))
(setq behind-string t))
(if (or (not move-point)
(not behind-string))
(goto-char save-point))
behind-string))
(defun modelica-within-matrix-expression (&optional move-point)
"Return t if an opening bracket is found backwards from point.
Return nil otherwise; optionally move point to the bracket if
MOVE-POINT is true."
(let ((save-point (point)) (matrix-expression nil))
(condition-case nil
(progn
(while (progn
(re-search-backward "[\]\[]")
(modelica-within-comment t)))
(if (looking-at "[\[]")
(progn
(setq matrix-expression t)
(if move-point
(setq save-point (point))))))
(error nil))
(goto-char save-point)
matrix-expression))
(defun modelica-within-equation (&optional move-point)
"Return t if point is within right hand side of an equation, nil otherwise.
Optionally move point to the identifying '=' or ':=' if
MOVE-POINT is true."
(let ((equation nil) (save-point (point)))
(condition-case nil
(progn
(while (progn
(re-search-backward
(concat "\\([^=]:?=[^=]\\)\\|;"))
(modelica-within-comment)))
(if (looking-at ";")
(setq equation nil)
(setq equation t)
(if (looking-at "[^:]=")
(forward-char 1))
(if move-point
(setq save-point (point)))))
(error nil))
(goto-char save-point)
equation))
(defun modelica-statement-start (&optional ref-point)
"Move point to the first character of the current statement.
The optional argument REF-POINT points to the last end or unended
begin."
(let ((save-point (point)))
(if ref-point
()
(condition-case nil
(modelica-last-unended-begin t)
(error (goto-char (point-min))))
(setq ref-point (point))
(goto-char save-point))
(while (progn
(re-search-backward
;; ("]" ")" ";"
;; "algorithm" "equation" "external"
;; "else" "elseif" "elsewhen"
;; "loop" "protected" "public" "then")
(concat
"[\]\);]\\|"
"\\<"
"\\(algorithm\\|e\\(lse\\(if\\|when\\)?\\|quation\\|"
"xternal\\)\\|loop\\|p\\(rotected\\|ublic\\)\\|then\\)"
"\\>")
ref-point 'no-error)
(and
(> (point) ref-point)
(or (modelica-within-comment t)
(modelica-within-string)
(if (looking-at "[\]\)]")
(progn
(forward-char 1)
(forward-sexp -1)
t))
(if (looking-at ";")
(modelica-within-matrix-expression t)
(modelica-within-equation t))))))
(cond
((= (point) ref-point)
;; we arrived at last unended begin,
;; but might be looking for first statement of block
(modelica-forward-begin)
(forward-comment (- (buffer-size)))
(if (> (point) save-point)
(goto-char ref-point)))
((looking-at ";")
(forward-char 1))
(t
(forward-word 1)))
(forward-comment (buffer-size))))
(defun modelica-short-class-definition ()
"Return t if point is over a short class definition."
(looking-at (concat
"\\(" modelica-class-modifier-keyword "\\)*"
modelica-class-keyword
"[A-Za-z_][0-9A-Za-z_]*[ \t\n\r]+=")))
(defun modelica-end-ident ()
"Return t if last word is an 'end'."
(save-excursion
(forward-word -1)
(looking-at "end\\>")))
(defun modelica-last-unended-begin (&optional indentation-only)
"Position point at last unended begin.
Raise an error if nothing found. If INDENTATION-ONLY is true,
position point at last begin or end."
;; find last unended begin-like keyword
(let ((depth 1))
(while (>= depth 1)
(while (progn
(re-search-backward
(concat
"\\<"
; ("block" "class" "connector" "end"
; "for" "function" "if" "model" "package"
; "record" "type" "when" "while")
"\\(block\\|c\\(lass\\|onnector\\)\\|end\\|"
"f\\(or\\|unction\\)\\|if\\|model\\|package\\|"
"record\\|type\\|wh\\(en\\|ile\\)\\)"
"\\>"))
(or (modelica-within-comment t)
(modelica-short-class-definition)
(modelica-within-string)
(modelica-end-ident)
(and (looking-at "if") (modelica-within-equation t)))))
(if (looking-at "end\\>")
(if indentation-only
(setq depth -1)
(setq depth (+ depth 1)))
(setq depth (- depth 1))))
;; step backwards over class prefixes
(if (>= depth 0)
(let ((save-point (point)))
(while (progn
(forward-word -1)
(and
(looking-at modelica-class-modifier-keyword)
(not (modelica-within-comment))))
(setq save-point (point)))
(goto-char save-point)))))
(defun modelica-forward-begin ()
"Move point forward over a begin-like statement.
Return block ident (string) or nil if not found.
Point is assumed over the start of the begin-like statement upon call."
(let ((ident nil) (save-point (point)) start-point)
(cond
((looking-at "\\(for\\|while\\)\\>")
(setq ident (buffer-substring (match-beginning 0) (match-end 0)))
(while (progn
(re-search-forward "\\<loop\\>")
(modelica-within-comment))))
;;(regexp-opt '("if" "elseif" "when" "elsewhen"))
((looking-at "\\(else\\(if\\|when\\)\\|if\\|when\\)\\>")
(setq ident (buffer-substring (match-beginning 0) (match-end 0)))
(while (progn
(re-search-forward "\\<then\\>")
(modelica-within-comment))))
((looking-at
(concat "\\(" modelica-class-modifier-keyword "\\)\\|"
"\\(" modelica-class-keyword "\\)"))
;; move over class modifiers
(while (looking-at modelica-class-modifier-keyword)
(forward-word 1)
(forward-comment (buffer-size)))
;; check and move over class specifier
(if (not (looking-at modelica-class-keyword))
(goto-char save-point)
(forward-word 1)
(forward-comment (buffer-size))
;; move over class name and look it up
(setq start-point (point))
(re-search-forward "\\>")
(setq ident (buffer-substring start-point (point)))
(forward-comment (buffer-size))
;; check short class definition
(if (looking-at "=")
(progn
(setq ident nil)
(goto-char save-point))
;; else move over documentation strings
(while (looking-at "\"")
(forward-char 1)
(if (looking-at "\"")
(forward-char 1)
(re-search-forward "[^\\]\""))
(forward-comment (buffer-size)))))))
(forward-comment (buffer-size))
ident))
(defun modelica-newline-and-indent ()
"Indent current line before calling `newline-and-indent'."
(interactive)
(modelica-indent-line)
(newline-and-indent))
(defun modelica-insert-end ()
"Insert end statement for current block."
(interactive)
(let ((case-fold-search nil)
indentation (save-point (point)) (block-start nil) (end-ident ""))
(save-excursion
(condition-case nil
(modelica-last-unended-begin)
(error (error "Couldn't find unended begin")))
(setq indentation (current-column))
(setq end-ident (modelica-forward-begin))
(if (<= save-point (point))
(setq block-start t)))
;; insert newline or clear up an empty line
(if (not (modelica-empty-line))
(insert "\n")
(setq save-point (point))
(beginning-of-line)
(delete-region (point) save-point))
;; insert proper end
(indent-to indentation)
(insert "end " end-ident ";")
;; step back if block just starts
(if (not block-start)
()
(forward-line -1)
(end-of-line))
;; insert newline
(insert "\n")
(modelica-indent-line)))
;; active regions, and auto-newline/hungry delete key
;; (copied from cc-mode.el)
(defun modelica-keep-region-active ()
"Do whatever is necessary to keep the region active in XEmacs 19.
Ignore byte-compiler warnings you might see."
(and (boundp 'zmacs-region-stays)
(setq zmacs-region-stays t)))
(defun modelica-forward-statement ()
"Move point to next beginning of a statement."
(interactive)
(modelica-keep-region-active)
(let ((case-fold-search nil)
(save-point (point))
pos)
(modelica-statement-start)
(if (> (point) save-point)
;; ready after skipping leading comment or blanks
()
;; else move forward
(setq save-point (point))
(if (modelica-forward-begin)
;; ready after moving over begin-like statement
()
;; move forward behind next ";" ending a statement
(while
(progn
(re-search-forward ";" (point-max) t)
(or (modelica-within-comment)
(modelica-within-string)
(modelica-within-matrix-expression))))
(forward-comment (point-max))
(if (eobp)
(progn
(goto-char save-point)
(error "No next statement"))
;; move backwards over possibly skipped statements
;; without ending ";"
(setq pos (point))
(while (> (point) save-point)
(setq pos (point))
(forward-comment (- (point-max)))
(if (not (bobp))
(forward-char -1))
(modelica-statement-start))
(goto-char pos))))))
(defun modelica-backward-statement ()
"Move point to previous beginning of a statement."
(interactive)
(modelica-keep-region-active)
(let ((case-fold-search nil) (save-point (point)))
(modelica-statement-start)
(if (< (point) save-point)
;; ready after having moved to start of current statement
()
;; else move backward
(setq save-point (point))
(forward-comment (- (point-max)))
(if (not (bobp))
(forward-char -1))
(modelica-statement-start)
(if (= (point) save-point)
(error "No previous statement")))))
(defun modelica-forward-block (&optional arg)
"Move point to next beginning of a block at the same nesting level.
If no next block found on the same level move a level higher.
If ARG is a positive integer move that many times.
If ARG is a negative integer move backwards instead.
In interactive calls, ARG is the numeric prefix argument."
(interactive "p")
(cond
((or (null (integerp arg)) (eq arg 1))
(let ((save-point (point)))
(condition-case nil
(progn
(modelica-to-block-begin)
(if (> (point) save-point)
;; we moved already forward to a block begin
()
(modelica-to-block-end)
(modelica-forward-statement)
(modelica-to-block-begin)
(if (< (point) save-point)
(modelica-forward-block))))
;; in case of error and if we did move yet,
;; move forward one statement
(error (if (= (point) save-point)
(modelica-forward-statement))))))
((> arg 1)
(dotimes (_i arg)
(modelica-forward-block 1)))
((<= arg 0)
(modelica-backward-block)
(modelica-forward-block (1+ arg)))))
(defun modelica-backward-block ()
"Move point to previous beginning of a block at the same nesting level.
If point is at the first block of the current level, move it a level higher."
(interactive)
(let ((save-point (point)))
(condition-case nil
(progn
(modelica-to-block-begin)
(if (< (point) save-point)
;; we moved already backward to the beginning of a block
()
(modelica-backward-statement)
(modelica-to-block-begin)))
;; in case of error and if we did not move yet,
;; move backward one statement
;; and move to beginning of that block if one ends there