-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspecman-mode.el
5590 lines (4813 loc) · 191 KB
/
specman-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
;; @(#) specman-mode.el -- Mode for editing specman files
;; @(#) $Id: specman-mode.el,v 1.23 2013/05/23 07:03:00 pouyet Exp $
;; @(#) $Keywords: tools $
;; $KnownCompatibility: 23.1+, XEmacs21.4.15+ $
;; This file is not part of Emacs
;; Copyright (C) 2013 Cadence Design Systems, Inc.
;; Authors: Uri Maoz <[email protected]>
;; Michael McNamara <[email protected]>
;; Yaron Peri <[email protected]>
;; Maintainer:
;; Created: May 25 2013
;; LCD Archive Entry:
;; specman-mode|Michael McNamara|[email protected]|
;; Specman Major mode. auto indents, colorizes, code in the 'e language|
;; 25-May-2013|$Revision: 1.23 $|~/misc/specman-mode.el.Z|
;; COPYRIGHT NOTICE
;;
;; 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 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 GNU Emacs. If you did not, write to the Free Software Foundation,
;; Inc., 675 Mass Ave., Cambridge, MA 02139, USA.
;;
;; =============================================================================
;; SPECMAN MODE - A Major mode for writing in 'e'
;; =============================================================================
;;
;; ....................................................... &t-install ...
;; Put this file on your Emacs-Lisp load path, add following into your
;; ~/.emacs startup file
;;
;; (require 'specman-mode)
;;
;; Or add the following code to your .emacs:
;;
;; (autoload 'specman-mode "specman-mode" "Specman code editing mode" t)
;;
;; (setq auto-mode-alist
;; (remove-duplicates
;; (append (list
;; (cons "\\.e\\'" 'specman-mode)
;; (cons "\\.ecom\\'" 'specman-mode)
;; (cons "\\.erld\\'" 'specman-mode))
;; auto-mode-alist)))
;;
;;
;;; Commetary:
;;
;; specman-mode is a major mode for editing code written in the 'e' language
;;
;; Branch 'shr' created by Scott Roland <[email protected]>
;; GitHub home of 'shr' branch: https://github.com/hackonteur/specman-mode
(defconst specman-mode-version "Revision: 1.23 (shr 2)"
"Version of this Specman mode.")
(defun specman-version ()
"Inform caller of the version of this file"
(interactive)
(message (concat "Using specman-mode version "
(substring specman-mode-version 12 -3 )) )
)
(autoload 'specman-mode "specman-mode" "Specman code editing mode" t)
;; remove-duplicates below requires cl
(require 'cl)
(setq auto-mode-alist
(remove-duplicates
(append (list
(cons "\\.e\\'" 'specman-mode)
(cons "\\.ecom\\'" 'specman-mode)
(cons "\\.erld\\'" 'specman-mode))
auto-mode-alist)))
(add-hook 'specman-mode-hook
(lambda ()
(turn-on-font-lock)
(setq indent-tabs-mode nil)
(setq write-file-hooks nil)))
(add-hook 'speedbar-load-hook
(lambda ()
(speedbar-add-supported-extension ".e")))
(defmacro specman-safe (&rest body)
"Safely execute BODY, return nil if an error occurred."
`(condition-case nil
(progn ,@body)
(error nil)))
(if (fboundp 'eval-when-compile)
(eval-when-compile
(condition-case nil
(require 'cl) ;; FSF emacs's imenu needs cl, but doesn't (require 'cl)
(error nil))
(condition-case nil
(require 'imenu)
(error nil))
(condition-case nil
(unless (fboundp 'imenu-add-to-menubar)
(defun imenu-add-to-menubar (a) ))
(error nil))
(condition-case nil
(require 'reporter)
(error nil))
(condition-case nil
(if (boundp 'current-menubar)
nil ;; great
(defmacro set-buffer-menubar (&rest args) nil)
(defmacro add-submenu (&rest args) nil))
(error nil))
(condition-case nil
(require 'func-menu)
(error nil))
(condition-case nil
(unless (fboundp 'char=)
(defun char= (a b)
(= a b)))
(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 customize (&rest args)
(message "Sorry, Customize is not available with this version of emacs"))
(defmacro defcustom (var value doc &rest args)
`(defvar ,var ,value , doc))
)
(if (fboundp 'defface)
nil ;; great!
(defmacro defface (var value doc &rest args)
`(make-face ,var))
)
(if (and (featurep 'custom) (fboundp 'customize-group))
nil ;; We've got what we needed
;; We have an intermediate custom-library, hack around it!
(defmacro customize-group (var &rest args)
`(customize ,var) )
)
(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 customize (&rest args)
(message "Sorry, Customize is not available with this version of emacs"))
(defmacro defcustom (var value doc &rest args)
`(defvar ,var ,value , doc))
)
(if (and (featurep 'custom) (fboundp 'customize-group))
nil ;; We've got what we needed
;; We have an intermediate custom-library, hack around it!
(defmacro customize-group (var &rest args)
`(customize ,var) )
)
(condition-case nil
(require 'easymenu)
(error nil))))
;; If you install xemacs-devel, you will get a 10-20% speedup.
;; if not, you get this:
(unless (fboundp 'regexp-opt)
(defun regexp-opt (strings &optional paren)
(let ((open (if paren "\\(" ""))
(close (if paren "\\)" ""))
)
(concat open (mapconcat 'regexp-quote strings "\\|") close))))
;; TODO: this is also defined above, not sure why
(unless (fboundp 'char=)
(defun char= (a b)
(= a b)))
(if (not (boundp 'imenu-generic-expression))
(defvar imenu-generic-expression))
(unless (boundp 'font-lock-constant-face)
(make-face 'font-lock-constant-face)
(set-face-foreground 'font-lock-constant-face "CadetBlue"))
(if (< max-specpdl-size 3000)
(setq max-specpdl-size 3000))
;; =================================================
;; SPECMAN CUSTOMIZABLE VARS
;; =================================================
;;; - Customizable variables
(defgroup specman-mode nil
"Facilitates easy editing of Specman source text"
:group 'languages
)
(defcustom specman-basic-offset 4
"*Indentation of Specman statements with respect to containing block."
:group 'specman-mode
:type 'integer
)
(put 'specman-basic-offset 'safe-local-variable #'integerp)
(defconst specman-tab-width specman-basic-offset
"*Tab stop width"
)
(defcustom specman-continued-line-offset 2
"*Indentation of continued Specman statements with respect to first line of statement."
:group 'specman-mode
:type 'integer
)
(defcustom specman-auto-newline nil
"*ON (or non nil) means to automatically newline after inserting a semicolon."
:group 'specman-mode
:type 'boolean
)
(defcustom specman-auto-endcomments nil
"*ON (or non nil) means to automatically add a comment when closing a code block."
:group 'specman-mode
:type 'boolean
)
(defcustom specman-auto-endcomments-for-major-scopes-only t
"*ON (or non nil) means that end-comments are only applied to struct and method scopes."
:group 'specman-mode
:type 'boolean
)
(defcustom specman-auto-endcomments-kill-existing-comment t
"*ON (or non nil) means that redoing an end-comment replaces an existing end-comment."
:group 'specman-mode
:type 'boolean
)
(defcustom specman-max-line-length 80
"*The maximum number of characters that should be in a line."
:group 'specman-mode
:type 'integer
)
(defcustom specman-highlight-beyond-max-line-length t
"*ON (or non nil) means to highlight text beyond specman-max-line-length."
:group 'specman-mode
:type 'boolean
)
(defface specman-highlight-beyond-max-line-length-face
'((t (:underline t)))
"Face used to highlight text beyond the defined maximum line length."
:group 'specman-mode
)
(defcustom specman-highlight-punctuation t
"*ON (or non nil) means to highlight punctuation symbols."
:group 'specman-mode
:type 'boolean
)
(defface specman-punctuation-face
'((t (:foreground "SteelBlue")))
"Face used to highlight punctuation symbols - significant because they are mostly a single char."
:group 'specman-mode
)
(defcustom specman-compile-command "${SPECMAN_HOME}/bin/sn_compile.sh "
"String used to compile e files."
:group 'specman-mode
)
(defcustom specman-make-command "/usr/bin/make "
"String used to invoke make"
:group 'specman-mode
)
(defcustom specman-index-menu-active t
"*ON (or non nil) means that the 'Specman-Index' menu is active."
:group 'specman-mode
:type 'boolean
)
(defcustom specman-date-scientific-format nil
"*If non-nil, dates are written in scientific format (e.g. 1997/09/17),
in european format otherwise (e.g. 17.09.1997). The braindead american
format (e.g. 09/17/1997) is not supported."
:group 'specman-mode
:type 'boolean
)
(defcustom specman-company "ACME Company"
"*Default name of Company for specman header. If set will become buffer local. "
:group 'specman-mode
:type 'string
)
(defcustom specman-project "Roadrunner"
"*Default name of Project for specman header. If set will become buffer local."
:group 'specman-mode
:type 'string
)
;; =================================================
;; SPECMAN REGULAR EXPRESSIONS
;; =================================================
(defconst specman-ex-code-regexp "^\\(?:\\('>\\)\\|\\(<'\\)\\)"
"Regular Expression that matches ex-code tokens")
;; Matthew Lovell <[email protected]>
;; Up to one level of parentheses is allowed in the parameter
;; list to a method. The regex pattern for that portion if
;; from J. Friedl's "Mastering Regular Expressions"
(defconst specman-legal-name-regexp "\\(?:[a-zA-Z][a-zA-Z0-9_]*\\)"
"e legal name")
(defconst specman-method-definition-regexp
"\\(?:\\(?:private\\|package\\|protected\\)[ \t\n]+\\)?\\([A-Za-z0-9_]+\\)[ \t\n]*([^()]*\\(?:([^()]*)[^()]*\\)*)[^-/;]*?[ \t\n]+is[a-z \t\n]*{"
"Regexp that identifies methods (arg 1)")
(defconst specman-on-event-method-definition-regexp
"\\(on[ \t\n]+[A-Za-z0-9_]+\\)[ \t\n]*{"
"Regexp that identifies on-event methods (arg 1)")
(defconst specman-class-definition-regexp
(concat
"\\("
"\\(?:\\(?:package[ \t\n]*\\)?struct[ \t\n]+[A-Za-z0-9_]+\\)"
"\\|"
"\\(?:extend[ \t\n]+[^{]+\\)"
"\\|"
"\\(?:\\(?:package[ \t\n]*\\)?unit[ \t\n]+[A-Za-z0-9_]+\\)"
"\\)")
"Regexp that identifies major class scopes (arg 1)")
(defconst specman-when-subtype-definition-regexp
"\\(when[ \t\n]+[^{]+\\)"
"Regexp that identifies when subtypes (arg 1)")
(defconst specman-define-regexp
"\\(define[ \t]*<[^>]+>\\)"
"Regexp that identifies define scopes (arg 1)")
(defconst specman-macro-and-define-regexp
"\\(\\(?:#?define\\|#ifn?def\\|#else\\)[ \t\n]+[^ \t\n]+\\)"
"Regexp that identifies macro and define scopes (arg 1)")
(defconst specman-top-level-container-definition-regexp
(concat
"\\("
"\\(?:\\(?:package[ \t\n]*\\)?struct[ \t\n]+[A-Za-z0-9_]+\\)"
"\\|"
"\\(?:extend[ \t\n]+[^{]+\\)"
"\\|"
"\\(?:\\(?:package[ \t\n]*\\)?unit[ \t\n]+[A-Za-z0-9_]+\\)"
"\\|"
"\\(?:\\(?:#?define\\|#ifn?def\\|#else\\)[ \t\n]+[^ \t\n]+\\)"
"\\)")
"Regexp that identifies all specman container scopes (arg 1)")
(defconst specman-container-scope-definition-regexp
(concat
"\\("
"\\(?:\\(?:package[ \t\n]*\\)?struct[ \t\n]+[A-Za-z0-9_]+\\)"
"\\|"
"\\(?:extend[ \t\n]+[^{]+\\)"
"\\|"
"\\(?:\\(?:package[ \t\n]*\\)?unit[ \t\n]+[A-Za-z0-9_]+\\)"
"\\|"
"\\(?:\\(?:#?define\\|#ifn?def\\|#else\\)[ \t\n]+[^ \t\n]+\\)"
"\\|"
"\\(?:when[ \t\n]+[^{]+\\)"
"\\)")
"Regexp that identifies all specman container scopes (arg 1)")
(defconst specman-field-definition-regexp
"\\(?:\\(?:private\\|package\\|protected\\)[ \t\n]+\\)?\\([A-Za-z0-9_]+\\)[ \t\n]*:[ \t\n]*\\(?:\\*?[A-Za-z0-9_]+\\|\\[[^\\[]*\\]\\)"
"Regexp that identifies field definitions (arg 1)")
(defconst specman-variable-definition-regexp
"var[ \t\n]+\\([A-Za-z0-9_]+\\)[ \t\n]*:=?[ \t\n]*[{*+-]?[ \t\n]*\\([A-Za-z0-9_]+\\)"
"Regexp that identifies variable definitions (arg 1)")
(defconst specman-event-definition-regexp
"\\(?:\\(?:private\\|package\\|protected\\)[ \t\n]+\\)?event[ \t\n]+\\([A-Za-z0-9_]+\\)"
"Regexp that identifies event definitions (arg 1)")
(defconst specman-type-definition-regexp
"\\(?:package[ \t\n]*\\)?\\(?:method_\\)?type[ \t\n]+\\([A-Za-z0-9_]+\\)[ \t\n]*:[ \t\n]*\\["
"Regexp that identifies type definitions (arg 1)")
(defconst specman-cover-definition-regexp
"cover[ \t\n]+\\([A-Za-z0-9_]+\\)[ \t\n]+is"
"Regexp that identifies cover definitions (arg 1)")
(eval-and-compile
(defconst specman-symbol-begin-regexp
"\\<"
"Regexp that identifies the beginning of a symbol")
(defconst specman-symbol-end-regexp
"\\>"
"Regexp that identifies the end of a symbol"))
(defconst specman-number-regexp
(concat
"\\("
;; this is ugly, but the only way I could find to count + and - as
;; number symbols but maintaining that numbers have to come after
;; a symbol beginning
"\\(?:"
"[+-]"
"\\|"
specman-symbol-begin-regexp
"\\)"
"\\(?:"
"[0-9]+"
"\\|"
"0b[01]+"
"\\|"
"0o[0-7]+"
"\\|"
"0[xh][0-9a-fA-F]+"
"\\)"
"[kKmM]?"
"\\)"
specman-symbol-end-regexp)
"Regexp that identifies numbers (arg1)")
(defconst specman-method-definition-regexp-full
(concat "^[ \t]*" specman-method-definition-regexp))
(defconst specman-on-event-method-definition-regexp-full
(concat "^[ \t]*" specman-on-event-method-definition-regexp))
(defconst specman-class-definition-regexp-full
(concat "^[ \t]*" specman-class-definition-regexp))
(defconst specman-when-subtype-definition-regexp-full
(concat "^[ \t]*" specman-when-subtype-definition-regexp))
(defconst specman-define-regexp-full
(concat "^[ \t]*" specman-define-regexp))
(defconst specman-macro-and-define-regexp-full
(concat "^[ \t]*" specman-macro-and-define-regexp))
(defconst specman-container-scope-definition-regexp-full
(concat "^[ \t]*" specman-container-scope-definition-regexp))
(defconst specman-field-definition-regexp-full
(concat "^[ \t]*" specman-field-definition-regexp))
(defconst specman-variable-definition-regexp-full
(concat "^[ \t]*" specman-variable-definition-regexp))
(defconst specman-event-definition-regexp-full
(concat "^[ \t]*" specman-event-definition-regexp))
(defconst specman-type-definition-regexp-full
(concat "^[ \t]*" specman-type-definition-regexp))
(defconst specman-cover-definition-regexp-full
(concat "^[ \t]*" specman-cover-definition-regexp))
;; =================================================
;; SPECMAN SEARCHES AND QUERIES
;; =================================================
(defsubst specman-re-search-forward (REGEXP BOUND NOERROR &optional within-code-region)
"Like re-search-forward, but skips over matches in comments or strings"
(let ((start-pos
(point))
(full-regexp
(concat
"\\(//\\|--\\)\\|\\(^'>\\)\\|\\(\"\\)\\|\\(/\\*\\)\\|\\("
REGEXP
"\\)"))
)
(unless within-code-region
(specman-skip-forward-comment-or-string))
(store-match-data '(nil nil))
(while (and (if BOUND
(<= (point) BOUND)
t)
(re-search-forward full-regexp BOUND NOERROR)
(cond
(;; e line comment
(match-beginning 1)
(progn
(forward-line 1)
t)
)
(;; ex-code region
(match-beginning 2)
(progn
(re-search-forward "^<'" BOUND 'move)
t)
)
(;; string
(match-beginning 3)
(progn
;; to also consider the next character, for ""
(goto-char (match-beginning 3))
(re-search-forward "\\(?:[^\\]\\|[\\][\\]\\)\"" BOUND 'move)
t)
)
(;; c multi-line comment
(match-beginning 4)
(progn
(re-search-forward "\\*/" BOUND 'move)
t)
)
(;; the regexp
(match-beginning 5)
(progn
(goto-char (match-beginning 5))
(looking-at REGEXP) ;; to reset match-data
nil)
)
)
(progn
(store-match-data '(nil nil))
(if BOUND
(< (point) BOUND)
t))
))
(goto-char (if (match-end 0)
(match-end 0)
start-pos))
(match-end 0)))
(defsubst specman-re-search-backward (REGEXP BOUND NOERROR &optional within-code-region)
"Like re-search-backward, but skips over matches in comments or strings"
(let ((start-pos
(point))
(full-regexp
(concat
"\\(^<'\\)\\|\\(\"\\)\\|\\(\\*/\\)\\|\\("
REGEXP
"\\)"))
)
(unless within-code-region
(specman-skip-backward-comment-or-string))
(store-match-data '(nil nil))
(while (and (if BOUND
(>= (point) BOUND)
t)
(re-search-backward full-regexp BOUND NOERROR)
(cond
(;; ex-code region
(match-beginning 1)
(progn
(re-search-backward "^'>" BOUND 'move)
t)
)
(;; string
(match-beginning 2)
(progn
;; NOTE: this approach is more elegant, but slower than the code below
;;(if (specman-within-string-p)
;; (re-search-backward "[^\\]\"" BOUND 'move)
;; (re-search-backward "//\\|--" (specman-beg-of-line-pos) 'move))
(when (save-match-data
(back-to-indentation)
(re-search-forward "\\(//\\|--\\)\\|\\([^\\]\"\\)" (match-beginning 2) 'move)
(if (match-beginning 1) ;; a comment and not a string
(progn
(goto-char (match-beginning 1)) ;; go to the comment beginning
nil) ;; and fail, staying there
t))
(goto-char (match-beginning 2)) ;; return to the string
(when ;; and find the string opener
(re-search-backward "\\(?:[^\\]\\|[\\][\\]\\)\\(\"\\)" BOUND 'move)
(goto-char (match-beginning 1))))
t)
)
(;; c multi-line comment
(match-beginning 3)
(progn
(re-search-backward "/\\*" BOUND 'move)
t)
)
(;; the regexp
(match-beginning 4)
(let ((failed-match nil)
)
(goto-char (match-beginning 4))
(looking-at REGEXP) ;; to reset match-data
(save-match-data
;; if a comment is not found then point is left on the
;; beginning of the line but the goto-char below fixes this.
;; note: the while checks that the comment prefix is not in a string
(while (and (re-search-backward "//\\|--" (specman-beg-of-line-pos) 'move)
(if (specman-within-string-p)
t
(progn
(setq failed-match t)
nil)))))
failed-match
)
)
)
(progn
(store-match-data '(nil nil))
(if BOUND
(> (point) BOUND)
t))
))
(goto-char (if (match-beginning 0)
(match-beginning 0)
start-pos))
(match-beginning 0)))
(defsubst specman-beg-of-line-pos ()
(save-excursion
(beginning-of-line)
(point)))
(defsubst specman-end-of-line-pos ()
(save-excursion
(end-of-line)
(point)))
(defsubst specman-within-comment-p ()
(or (specman-within-line-comment-p)
(specman-within-region-comment-p))
)
(defsubst specman-empty-line-p ()
(looking-at "^[ \t]*$"))
(defun specman-within-string-p ()
(save-excursion
(nth 3 (parse-partial-sexp (point-min) (point)))))
(defsubst specman-line-within-string-p ()
(save-excursion
(beginning-of-line)
(specman-within-string-p)))
(defsubst specman-prepared-buffer-substring (beg end)
"Remove extra spaces and new-lines from strings."
(save-match-data
(if (eq specman-emacs-kind 'emacs)
(replace-regexp-in-string "[ \t\n]+"
" "
(replace-regexp-in-string "^[ \t\n]+\\|[ \t\n]+$"
""
(buffer-substring beg
end)))
(replace-in-string (replace-in-string (buffer-substring beg
end)
"^[ \t\n]+\\|[ \t\n]+$"
"")
"[ \t\n]+"
" "))))
;; =================================================
;; SPECMAN SCOPE QUERIES
;; =================================================
(defvar global-scope-index nil
"Used as a global reference for cached refs to scopes for performance.
Initialized for performace intensive operations without user interaction
(e.g. region indentation) and must be cleared afterwards.")
(make-variable-buffer-local 'global-scope-index)
(defstruct scope-descriptor paren match parent paren-parent)
(defun specman-create-scope-index ()
"Create a list of scope descriptors, each of which is a list of 3 members:
scope, matching-scope and parent-scope-opener"
(save-excursion
(save-match-data
(let ((scope-index
nil
)
(scope-stack
nil
)
(paren-stack
nil
)
(buffer-end
(point-max)
)
(top-scope-opener
(point-min-marker)
)
(end-scope-closer
(point-max-marker)
)
(search-regexp
(concat "\\("
"{\\|(" "\\)\\|\\("
"}\\|)" "\\)\\|\\("
"//\\|--" "\\)\\|\\("
"^'>" "\\)\\|\\("
"\"" "\\)\\|\\("
"/\\*" "\\)")
)
)
(goto-char (point-min))
;; for completeness - have a top pseudo-scope for the whole file
(set-marker-insertion-type top-scope-opener t)
(set-marker-insertion-type end-scope-closer t)
(push (cons top-scope-opener
(make-scope-descriptor
:paren top-scope-opener
:match end-scope-closer
:parent top-scope-opener
:paren-parent nil))
scope-index)
(when (re-search-forward "^<'" buffer-end t)
(while (and (< (point) buffer-end)
(re-search-forward search-regexp buffer-end 'move))
(cond
;; new scope
((match-beginning 1)
(save-excursion
(goto-char (match-beginning 1))
(let* ((point-marker
(point-marker)
)
(is-scope
(looking-at "{")
)
(parent
(if scope-stack
(scope-descriptor-paren (car scope-stack))
top-scope-opener)
)
(paren-parent
(if paren-stack
(scope-descriptor-paren (car paren-stack))
nil)
)
(point-descriptor
(make-scope-descriptor
:paren point-marker
:match end-scope-closer ;; place holder until the closer is found
:parent parent
:paren-parent paren-parent)
)
)
(set-marker-insertion-type point-marker t)
(push (cons point-marker
point-descriptor)
scope-index)
(if is-scope
(push point-descriptor scope-stack)
(push point-descriptor paren-stack))))
)
;; close scope
((match-beginning 2)
(save-excursion
(goto-char (match-beginning 2))
(let* ((point-marker
(point-marker)
)
(is-scope
(looking-at "}")
)
(has-match
(if is-scope
scope-stack
paren-stack)
)
(match
(when has-match
(if is-scope
(pop scope-stack)
(pop paren-stack)))
)
(parent
(if scope-stack
(scope-descriptor-paren (car scope-stack))
top-scope-opener)
)
(paren-parent
(if paren-stack
(scope-descriptor-paren (car paren-stack))
nil)
)
)
(set-marker-insertion-type point-marker t)
(if has-match
(let ((point-descriptor
(make-scope-descriptor
:paren point-marker
:match (scope-descriptor-paren match)
:parent parent
:paren-parent paren-parent)
)
)
(setf (scope-descriptor-match match) point-marker)
(push (cons point-marker
point-descriptor)
scope-index)
)
;; currently no error checking for unmatched scopes - here for closing scope
(progn
(push (cons point-marker
(make-scope-descriptor
:paren point-marker
:match top-scope-opener
:parent parent
:paren-parent paren-parent))
scope-index))
)
;; TODO: clear any dangling open parens which aren't closed within the
;; scope being closed now. nice idea, maybe should be explored more,
;; but doesn't work for now.
;; (when match
;; (let ((close-to-point
;; (scope-descriptor-paren match)
;; )
;; (stack-to-clear
;; (if is-scope
;; paren-stack
;; scope-stack)
;; )
;; (continue
;; t
;; )
;; )
;; (while (and continue
;; stack-to-clear)
;; (if (< close-to-point
;; (scope-descriptor-paren (car stack-to-clear)))
;; (pop stack-to-clear)
;; (setq continue nil)))
;; ))
))
)
;; comment
((match-beginning 3)
(forward-line 1)
)
;; end e-code region
((match-beginning 4)
(re-search-forward "^<'" buffer-end 'move)
)
;; string
((match-beginning 5)
;; to also consider the next character, for ""
(goto-char (match-beginning 0))
(re-search-forward "\\(?:[^\\]\\|[\\][\\]\\)\"" buffer-end 'move)
)
;; c multi-line comment
((match-beginning 6)
(re-search-forward "\\*/" buffer-end 'move)
)
)
)
;; currently no error checking for unmatched scopes - here for opening scope
)
;; for aesthetic reasons - the list is arranged as a stack, so it is backward
(reverse scope-index)
))))
(defun specman-clear-scope-descriptor-and-key (key value)
"Clear a scope index entry. Intended for use with maphash."
(let ((opener
(scope-descriptor-paren value))
(closer
(scope-descriptor-match value))
(parent
(scope-descriptor-parent value))
)
;; some scope markers can be nil
(when key
(set-marker key nil))
(when opener
(set-marker opener nil))
(when closer
(set-marker closer nil))
(when parent
(set-marker parent nil))))
(defun specman-clear-scope-index (scope-index)
"Clear a scope index by setting all markers to nil, so they no longer
have an updating cost and the index itself to nil."
(while scope-index
(specman-clear-scope-descriptor-and-key (caar scope-index)
(cdar scope-index))
(setq scope-index
(cdr scope-index))))
(defun specman-format-scope-descriptor-and-key (key value)
"format a scope index entry. Intended for use with maphash."
(format "%d => <%d %d> %d^\n"
(marker-position key)
(marker-position (scope-descriptor-paren value))
(marker-position (scope-descriptor-match value))
(marker-position (scope-descriptor-parent value))))
(defun specman-print-scope-index (scope-index)
"Print a scope index."
(if scope-index
(while scope-index
(specman-format-scope-descriptor-and-key (caar scope-index)
(cdar scope-index))
(setq scope-index
(cdr scope-index)))
(message "Trying to print an empty scope-index.")))
(defun specman-scope-index-up-list (scope-index &optional within-code-region)
(let ((result
nil)
)
(when (specman-re-search-backward "\\((\\)\\|\\()\\|{\\|}\\)"
nil
t
within-code-region)
;; at match-beginning 1 we're done, otherwise:
(if (match-beginning 2)
(let* ((point-marker
(point-marker))
(scope-descriptor
(assoc point-marker
scope-index))
(parent-opener
(when scope-descriptor
(scope-descriptor-paren-parent (cdr scope-descriptor))))
)
(set-marker point-marker nil)
(when parent-opener
(setq result (marker-position parent-opener))))
(setq result (point))
))
(when result
(goto-char result))
result))
(defun specman-scope-index-up-scope (scope-index &optional within-code-region)
(if (specman-re-search-backward "\\({\\)\\|\\(}\\|(\\|)\\)"
nil