-
Notifications
You must be signed in to change notification settings - Fork 0
/
nsi-mode.el
2892 lines (2543 loc) · 107 KB
/
nsi-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
;;; nsi-mode.el --- Major mode for editing and compiling Nsi programs (nsis scripts)
;; This is just a cannibalization of python mode because I wanted to have
;; something to edit and compile nsis scripts in emacs (Hans van Dam)
;; Compilation of the current buffer is performed using Ctrl-f9 (just like in the nsis-editor).
;; Copyright (C) 2005 Tim Peters
;; Author: 1995-1998 Barry A. Warsaw
;; 1992-1994 Tim Peters
;; Maintainer: [email protected]
;; Created: Feb 1992
;; Keywords: nsi languages oop
(defconst nsi-version "3.105"
"`nsi-mode' version number.")
;; This software is provided as-is, without express or implied
;; warranty. Permission to use, copy, modify, distribute or sell this
;; software, without fee, for any purpose and by any individual or
;; organization, is hereby granted, provided that the above copyright
;; notice and this paragraph appear in all copies.
;;; Commentary:
;;
;;
;; This is a major mode for editing Nsi programs. It was developed
;; by Tim Peters after an original idea by Michael A. Guravage. Tim
;; subsequently left the net; in 1995, Barry Warsaw inherited the mode
;; and is the current maintainer. Tim's now back but disavows all
;; responsibility for the mode. Smart Tim :-)
;; This version of nsi-mode.el is no longer compatible with Emacs
;; 18. I am striving to maintain compatibility with the X/Emacs 19
;; lineage but as time goes on that becomes more and more difficult.
;; I current recommend that you upgrade to the latest stable released
;; version of your favorite branch: Emacs 20.3 or better, or XEmacs
;; 20.4 or better (XEmacs 21.0 is in beta testing as of this writing
;; 27-Oct-1998 appears to work fine with this version of
;; nsi-mode.el). Even Windows users should be using at least
;; NTEmacs 20.3, and XEmacs 21.0 will work very nicely on Windows when
;; it is released.
;; FOR MORE INFORMATION:
;; For more information on installing nsi-mode.el, especially with
;; respect to compatibility information, please see
;;
;; http://www.nsi.org/emacs/nsi-mode/
;;
;; This site also contains links to other packages that you might find
;; useful, such as pdb interfaces, OO-Browser links, etc.
;; BUG REPORTING:
;; To submit bug reports, use C-c C-b. Please include a complete, but
;; concise code sample and a recipe for reproducing the bug. Send
;; suggestions and other comments to [email protected].
;; When in a Nsi mode buffer, do a C-h m for more help. It's
;; doubtful that a texinfo manual would be very useful, but if you
;; want to contribute one, I'll certainly accept it!
;; TO DO LIST:
;; - Better integration with pdb.py and gud-mode for debugging.
;; - Rewrite according to GNU Emacs Lisp standards.
;; - have nsi-execute-region on indented code act as if the region is
;; left justified. Avoids syntax errors.
;; - add a nsi-goto-block-down, bound to C-c C-d
;;; Code:
(require 'comint)
(require 'custom)
(eval-when-compile
(require 'cl)
(if (not (and (condition-case nil
(require 'custom)
(error nil))
;; Stock Emacs 19.34 has a broken/old Custom library
;; that does more harm than good. Fortunately, it is
;; missing defcustom
(fboundp 'defcustom)))
(error "STOP! STOP! STOP! STOP!
The Custom library was not found or is out of date. A more current
version is required. Please download and install the latest version
of the Custom library from:
<http://www.dina.kvl.dk/~abraham/custom/>
See the Nsi Mode home page for details:
<http://www.nsi.org/emacs/nsi-mode>
")))
;; user definable variables
;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
(defgroup nsi nil
"Support for the Nsi programming language, <http://www.nsi.org/>"
:group 'languages
:prefix "nsi-")
(defcustom nsi-nsi-command "nsi"
"*Shell command used to start nsi compiler."
:type 'string
:group 'nsi)
(defcustom nsi-default-interpreter 'cnsi
"*Which Nsi interpreter is used by default.
The value for this variable can be either `cnsi' or `jnsi'.
ote that this variable is consulted only the first time that a Nsi
mode buffer is visited during an Emacs session. After that, use
\\[nsi-toggle-shells] to change the interpreter shell."
:type '(choice (const :tag "Nsi (a.k.a. CNsi)" cnsi)
(const :tag "JNsi" jnsi))
:group 'nsi)
(defcustom nsi-indent-offset 4
"*Amount of offset per level of indentation.
`\\[nsi-guess-indent-offset]' can usually guess a good value when
you're editing someone else's Nsi code."
:type 'integer
:group 'nsi)
(defcustom nsi-smart-indentation t
"*Should `nsi-mode' try to automagically set some indentation variables?
When this variable is non-nil, two things happen when a buffer is set
to `nsi-mode':
1. `nsi-indent-offset' is guessed from existing code in the buffer.
Only guessed values between 2 and 8 are considered. If a valid
guess can't be made (perhaps because you are visiting a new
file), then the value in `nsi-indent-offset' is used.
2. `indent-tabs-mode' is turned off if `nsi-indent-offset' does not
equal `tab-width' (`indent-tabs-mode' is never turned on by
Nsi mode). This means that for newly written code, tabs are
only inserted in indentation if one tab is one indentation
level, otherwise only spaces are used.
Note that both these settings occur *after* `nsi-mode-hook' is run,
so if you want to defeat the automagic configuration, you must also
set `nsi-smart-indentation' to nil in your `nsi-mode-hook'."
:type 'boolean
:group 'nsi)
(defcustom nsi-align-multiline-strings-p t
"*Flag describing how multi-line triple quoted strings are aligned.
When this flag is non-nil, continuation lines are lined up under the
preceding line's indentation. When this flag is nil, continuation
lines are aligned to column zero."
:type '(choice (const :tag "Align under preceding line" t)
(const :tag "Align to column zero" nil))
:group 'nsi)
(defcustom nsi-block-comment-prefix ";;"
"*String used by \\[comment-region] to comment out a block of code.
This should follow the convention for non-indenting comment lines so
that the indentation commands won't get confused (i.e., the string
should be of the form `#x...' where `x' is not a blank or a tab, and
`...' is arbitrary). However, this string should not end in whitespace."
:type 'string
:group 'nsi)
(defcustom nsi-honor-comment-indentation t
"*Controls how comment lines influence subsequent indentation.
When nil, all comment lines are skipped for indentation purposes, and
if possible, a faster algorithm is used (i.e. X/Emacs 19 and beyond).
When t, lines that begin with a single `;' are a hint to subsequent
line indentation. If the previous line is such a comment line (as
opposed to one that starts with `nsi-block-comment-prefix'), then its
indentation is used as a hint for this line's indentation. Lines that
begin with `nsi-block-comment-prefix' are ignored for indentation
purposes.
When not nil or t, comment lines that begin with a `#' are used as
indentation hints, unless the comment character is in column zero."
:type '(choice
(const :tag "Skip all comment lines (fast)" nil)
(const :tag "Single ; `sets' indentation for next line" t)
(const :tag "Single ; `sets' indentation except at column zero"
other)
)
:group 'nsi)
(defcustom nsi-temp-directory
(let ((ok '(lambda (x)
(and x
(setq x (expand-file-name x)) ; always true
(file-directory-p x)
(file-writable-p x)
x))))
(or (funcall ok (getenv "TMPDIR"))
(funcall ok "/usr/tmp")
(funcall ok "/tmp")
(funcall ok ".")
(error
"Couldn't find a usable temp directory -- set `nsi-temp-directory'")))
"*Directory used for temp files created by a *Nsi* process.
By default, the first directory from this list that exists and that you
can write into: the value (if any) of the environment variable TMPDIR,
/usr/tmp, /tmp, or the current directory."
:type 'string
:group 'nsi)
(defcustom nsi-beep-if-tab-change t
"*Ring the bell if `tab-width' is changed.
If a comment of the form
\t# vi:set tabsize=<number>:
is found before the first code line when the file is entered, and the
current value of (the general Emacs variable) `tab-width' does not
equal <number>, `tab-width' is set to <number>, a message saying so is
displayed in the echo area, and if `nsi-beep-if-tab-change' is non-nil
the Emacs bell is also rung as a warning."
:type 'boolean
:group 'nsi)
(defcustom nsi-ask-about-save t
"If not nil, ask about which buffers to save before executing some code.
Otherwise, all modified buffers are saved without asking."
:type 'boolean
:group 'nsi)
(defcustom nsi-backspace-function 'backward-delete-char-untabify
"*Function called by `nsi-electric-backspace' when deleting backwards."
:type 'function
:group 'nsi)
(defcustom nsi-delete-function 'delete-char
"*Function called by `nsi-electric-delete' when deleting forwards."
:type 'function
:group 'nsi)
(defcustom nsi-imenu-show-method-args-p nil
"*Controls echoing of arguments of functions & methods in the Imenu buffer.
When non-nil, arguments are printed."
:type 'boolean
:group 'nsi)
(make-variable-buffer-local 'nsi-indent-offset)
;; Not customizable
(defvar nsi-master-file nil
"If non-nil, execute the named file instead of the buffer's file.
The intent is to allow you to set this variable in the file's local
variable section, e.g.:
# Local Variables:
# nsi-master-file: \"master.py\"
# End:
so that typing \\[nsi-execute-buffer] in that buffer executes the named
master file instead of the buffer's file. If the file name has a
relative path, the value of variable `default-directory' for the
buffer is prepended to come up with a file name.")
(make-variable-buffer-local 'nsi-master-file)
;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;; NO USER DEFINABLE VARIABLES BEYOND THIS POINT
(defconst nsi-emacs-features
(let (features)
;; NTEmacs 19.34.6 has a broken make-temp-name; it always returns
;; the same string.
(let ((tmp1 (make-temp-name ""))
(tmp2 (make-temp-name "")))
(if (string-equal tmp1 tmp2)
(push 'broken-temp-names features)))
;; return the features
features)
"A list of features extant in the Emacs you are using.
There are many flavors of Emacs out there, with different levels of
support for features needed by `nsi-mode'.")
(defvar nsi-font-lock-keywords
(let ((kw1 (mapconcat 'identity
'("Section" "Function" "FunctionEnd" "SectionEnd"
)
"\\|"))
)
(list
;; keywords
(cons (concat "\\b\\(" kw1 "\\)\\b[ \n\t(]") 1)
;; block introducing keywords with immediately following colons.
;; Yes "except" is in both lists.
;; classes
'("\\bclass[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
1 font-lock-type-face)
;; functions
'("\\bdef[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
1 font-lock-function-name-face)
))
"Additional expressions to highlight in Nsi mode.")
(put 'nsi-mode 'font-lock-defaults '(nsi-font-lock-keywords))
;; have to bind nsi-file-queue before installing the kill-emacs-hook
;; Constants
(defconst nsi-stringlit-re
(concat
;; These fail if backslash-quote ends the string (not worth
;; fixing?). They precede the short versions so that the first two
;; quotes don't look like an empty short string.
;;
;; (maybe raw), long single quoted triple quoted strings (SQTQ),
;; with potential embedded single quotes
"[rR]?'''[^']*\\(\\('[^']\\|''[^']\\)[^']*\\)*'''"
"\\|"
;; (maybe raw), long double quoted triple quoted strings (DQTQ),
;; with potential embedded double quotes
"[rR]?\"\"\"[^\"]*\\(\\(\"[^\"]\\|\"\"[^\"]\\)[^\"]*\\)*\"\"\""
"\\|"
"[rR]?'\\([^'\n\\]\\|\\\\.\\)*'" ; single-quoted
"\\|" ; or
"[rR]?\"\\([^\"\n\\]\\|\\\\.\\)*\"" ; double-quoted
)
"Regular expression matching a Nsi string literal.")
(defconst nsi-continued-re
;; This is tricky because a trailing backslash does not mean
;; continuation if it's in a comment
(concat
"\\(" "[^;'\"\n\\]" "\\|" nsi-stringlit-re "\\)*"
"\\\\$")
"Regular expression matching Nsi backslash continuation lines.")
(defconst nsi-blank-or-comment-re "[ \t]*\\($\\|;\\)"
"Regular expression matching a blank or comment line.")
(defconst nsi-outdent-re
(concat "\\(" (mapconcat 'identity
'("else:"
"except\\(\\s +.*\\)?:"
"finally:"
"elif\\s +.*:")
"\\|")
"\\)")
"Regular expression matching statements to be dedented one level.")
(defconst nsi-block-closing-keywords-re
"\\(return\\|raise\\|break\\|continue\\|pass\\)"
"Regular expression matching keywords which typically close a block.")
(defconst nsi-no-outdent-re
(concat
"\\("
(mapconcat 'identity
(list "try:"
"except\\(\\s +.*\\)?:"
"while\\s +.*:"
"for\\s +.*:"
"if\\s +.*:"
"elif\\s +.*:"
(concat nsi-block-closing-keywords-re "[ \t\n]")
)
"\\|")
"\\)")
"Regular expression matching lines not to dedent after.")
(defconst nsi-defun-start-re
"^\\([ \t]*\\)def[ \t]+\\([a-zA-Z_0-9]+\\)\\|\\(^[a-zA-Z_0-9]+\\)[ \t]*="
;; If you change this, you probably have to change nsi-current-defun
;; as well. This is only used by nsi-current-defun to find the name
;; for add-log.el.
"Regular expression matching a function, method, or variable assignment.")
(defconst nsi-class-start-re "^class[ \t]*\\([a-zA-Z_0-9]+\\)"
;; If you change this, you probably have to change nsi-current-defun
;; as well. This is only used by nsi-current-defun to find the name
;; for add-log.el.
"Regular expression for finding a class name.")
(defconst nsi-traceback-line-re
"[ \t]+File \"\\([^\"]+\\)\", line \\([0-9]+\\)"
"Regular expression that describes tracebacks.")
;; Major mode boilerplate
;; define a mode-specific abbrev table for those who use such things
(defvar nsi-mode-abbrev-table nil
"Abbrev table in use in `nsi-mode' buffers.")
(define-abbrev-table 'nsi-mode-abbrev-table nil)
(defvar nsi-mode-hook nil
"*Hook called by `nsi-mode'.")
;; In previous version of nsi-mode.el, the hook was incorrectly
;; called nsi-mode-hook, and was not defvar'd. Deprecate its use.
(and (fboundp 'make-obsolete-variable)
(make-obsolete-variable 'nsi-mode-hook 'nsi-mode-hook))
(defvar nsi-mode-map ()
"Keymap used in `nsi-mode' buffers.")
(if nsi-mode-map
nil
(setq nsi-mode-map (make-sparse-keymap))
;; electric keys
;; indentation level modifiers
;; subprocess commands
(define-key nsi-mode-map [C-f9] 'nsi-execute-buffer)
;; Caution! Enter here at your own risk. We are trying to support
;; several behaviors and it gets disgusting. :-( This logic ripped
;; largely from CC Mode.
;;
;; In XEmacs 19, Emacs 19, and Emacs 20, we use this to bind
;; backwards deletion behavior to DEL, which both Delete and
;; Backspace get translated to. There's no way to separate this
;; behavior in a clean way, so deal with it! Besides, it's been
;; this way since the dawn of time.
(if (not (boundp 'delete-key-deletes-forward))
(define-key nsi-mode-map "\177" 'nsi-electric-backspace)
;; However, XEmacs 20 actually achieved enlightenment. It is
;; possible to sanely define both backward and forward deletion
;; behavior under X separately (TTYs are forever beyond hope, but
;; who cares? XEmacs 20 does the right thing with these too).
(define-key nsi-mode-map [delete] 'nsi-electric-delete)
(define-key nsi-mode-map [backspace] 'nsi-electric-backspace))
;; Separate M-BS from C-M-h. The former should remain
;; backward-kill-word.
;; stuff that is `standard' but doesn't interface well with
;; nsi-mode, which forces us to rebind to special commands
(define-key nsi-mode-map "\C-xnd" 'nsi-narrow-to-defun)
;; information
(define-key nsi-mode-map "\C-c\C-b" 'nsi-submit-bug-report)
(define-key nsi-mode-map "\C-c\C-v" 'nsi-version)
;; shadow global bindings for newline-and-indent w/ the nsi- version.
;; BAW - this is extremely bad form, but I'm not going to change it
;; for now.
(mapcar #'(lambda (key)
(define-key nsi-mode-map key 'nsi-newline-and-indent))
(where-is-internal 'newline-and-indent))
;; Force RET to be nsi-newline-and-indent even if it didn't get
;; mapped by the above code. motivation: Emacs' default binding for
;; RET is `newline' and C-j is `newline-and-indent'. Most Nsieers
;; expect RET to do a `nsi-newline-and-indent' and any Emacsers who
;; dislike this are probably knowledgeable enough to do a rebind.
;; However, we do *not* change C-j since many Emacsers have already
;; swapped RET and C-j and they don't want C-j bound to `newline' to
;; change.
(define-key nsi-mode-map "\C-m" 'nsi-newline-and-indent)
)
(defvar nsi-mode-output-map nil
"Keymap used in *Nsi Output* buffers.")
(if nsi-mode-output-map
nil
(setq nsi-mode-output-map (make-sparse-keymap))
(define-key nsi-mode-output-map [button2] 'nsi-mouseto-exception)
(define-key nsi-mode-output-map "\C-c\C-c" 'nsi-goto-exception)
;; TBD: Disable all self-inserting keys. This is bogus, we should
;; really implement this as *Nsi Output* buffer being read-only
(mapcar #' (lambda (key)
(define-key nsi-mode-output-map key
#'(lambda () (interactive) (beep))))
(where-is-internal 'self-insert-command))
)
(defvar nsi-mode-syntax-table nil
"Syntax table used in `nsi-mode' buffers.")
(if nsi-mode-syntax-table
nil
(setq nsi-mode-syntax-table (make-syntax-table))
(modify-syntax-entry ?\( "()" nsi-mode-syntax-table)
(modify-syntax-entry ?\) ")(" nsi-mode-syntax-table)
(modify-syntax-entry ?\[ "(]" nsi-mode-syntax-table)
(modify-syntax-entry ?\] ")[" nsi-mode-syntax-table)
(modify-syntax-entry ?\{ "(}" nsi-mode-syntax-table)
(modify-syntax-entry ?\} "){" nsi-mode-syntax-table)
;; Add operator symbols misassigned in the std table
(modify-syntax-entry ?\$ "." nsi-mode-syntax-table)
(modify-syntax-entry ?\% "." nsi-mode-syntax-table)
(modify-syntax-entry ?\& "." nsi-mode-syntax-table)
(modify-syntax-entry ?\* "." nsi-mode-syntax-table)
(modify-syntax-entry ?\+ "." nsi-mode-syntax-table)
(modify-syntax-entry ?\- "." nsi-mode-syntax-table)
(modify-syntax-entry ?\/ "." nsi-mode-syntax-table)
(modify-syntax-entry ?\< "." nsi-mode-syntax-table)
(modify-syntax-entry ?\= "." nsi-mode-syntax-table)
(modify-syntax-entry ?\> "." nsi-mode-syntax-table)
(modify-syntax-entry ?\| "." nsi-mode-syntax-table)
;; For historical reasons, underscore is word class instead of
;; symbol class. GNU conventions say it should be symbol class, but
;; there's a natural conflict between what major mode authors want
;; and what users expect from `forward-word' and `backward-word'.
;; Guido and I have hashed this out and have decided to keep
;; underscore in word class. If you're tempted to change it, try
;; binding M-f and M-b to nsi-forward-into-nomenclature and
;; nsi-backward-into-nomenclature instead. This doesn't help in all
;; situations where you'd want the different behavior
;; (e.g. backward-kill-word).
(modify-syntax-entry ?\_ "w" nsi-mode-syntax-table)
;; Both single quote and double quote are string delimiters
(modify-syntax-entry ?\' "\"" nsi-mode-syntax-table)
(modify-syntax-entry ?\" "\"" nsi-mode-syntax-table)
;; backquote is open and close paren
(modify-syntax-entry ?\` "$" nsi-mode-syntax-table)
;; comment delimiters
(modify-syntax-entry ?\# "<" nsi-mode-syntax-table)
(modify-syntax-entry ?\n ">" nsi-mode-syntax-table)
)
;; Utilities
(defmacro nsi-safe (&rest body)
"Safely execute BODY, return nil if an error occurred."
(` (condition-case nil
(progn (,@ body))
(error nil))))
(defsubst nsi-keep-region-active ()
"Keep the region active in XEmacs."
;; Ignore byte-compiler warnings you might see. Also note that
;; FSF's Emacs 19 does it differently; its policy doesn't require us
;; to take explicit action.
(and (boundp 'zmacs-region-stays)
(setq zmacs-region-stays t)))
(defsubst nsi-highlight-line (from to file line)
(cond
((fboundp 'make-extent)
;; XEmacs
(let ((e (make-extent from to)))
(set-extent-property e 'mouse-face 'highlight)
(set-extent-property e 'nsi-exc-info (cons file line))
(set-extent-property e 'keymap nsi-mode-output-map)))
(t
;; Emacs -- Please port this!
)
))
;; Menu definitions, only relevent if you have the easymenu.el package
;; (standard in the latest Emacs 19 and XEmacs 19 distributions).
(defvar nsi-menu nil
"Menu for Nsi Mode.
This menu will get created automatically if you have the `easymenu'
package. Note that the latest X/Emacs releases contain this package.")
(and (nsi-safe (require 'easymenu) t)
(easy-menu-define
nsi-menu nsi-mode-map "Nsi Mode menu"
'("Nsi"
["Execute buffer" nsi-execute-buffer t]
["Describe mode" nsi-describe-mode t]
)))
;; Imenu definitions
(defvar nsi-imenu-class-regexp
(concat ; <<classes>>
"\\(" ;
"^[ \t]*" ; newline and maybe whitespace
"\\(class[ \t]+[a-zA-Z0-9_]+\\)" ; class name
; possibly multiple superclasses
"\\([ \t]*\\((\\([a-zA-Z0-9_,. \t\n]\\)*)\\)?\\)"
"[ \t]*:" ; and the final :
"\\)" ; >>classes<<
)
"Regexp for Nsi classes for use with the Imenu package."
)
(defvar nsi-imenu-method-regexp
(concat ; <<methods and functions>>
"\\(" ;
"^[ \t]*" ; new line and maybe whitespace
"\\(def[ \t]+" ; function definitions start with def
"\\([a-zA-Z0-9_]+\\)" ; name is here
; function arguments...
;; "[ \t]*(\\([-+/a-zA-Z0-9_=,\* \t\n.()\"'#]*\\))"
"[ \t]*(\\([^:#]*\\))"
"\\)" ; end of def
"[ \t]*:" ; and then the :
"\\)" ; >>methods and functions<<
)
"Regexp for Nsi methods/functions for use with the Imenu package."
)
(defvar nsi-imenu-method-no-arg-parens '(2 8)
"Indices into groups of the Nsi regexp for use with Imenu.
Using these values will result in smaller Imenu lists, as arguments to
functions are not listed.
See the variable `nsi-imenu-show-method-args-p' for more
information.")
(defvar nsi-imenu-method-arg-parens '(2 7)
"Indices into groups of the Nsi regexp for use with imenu.
Using these values will result in large Imenu lists, as arguments to
functions are listed.
See the variable `nsi-imenu-show-method-args-p' for more
information.")
;; Note that in this format, this variable can still be used with the
;; imenu--generic-function. Otherwise, there is no real reason to have
;; it.
(defvar nsi-imenu-generic-expression
(cons
(concat
nsi-imenu-class-regexp
"\\|" ; or...
nsi-imenu-method-regexp
)
nsi-imenu-method-no-arg-parens)
"Generic Nsi expression which may be used directly with Imenu.
Used by setting the variable `imenu-generic-expression' to this value.
Also, see the function \\[nsi-imenu-create-index] for a better
alternative for finding the index.")
;; These next two variables are used when searching for the Nsi
;; class/definitions. Just saving some time in accessing the
;; generic-nsi-expression, really.
(defvar nsi-imenu-generic-regexp nil)
(defvar nsi-imenu-generic-parens nil)
(defun nsi-imenu-create-index-function ()
"Nsi interface function for the Imenu package.
Finds all Nsi classes and functions/methods. Calls function
\\[nsi-imenu-create-index-engine]. See that function for the details
of how this works."
(setq nsi-imenu-generic-regexp (car nsi-imenu-generic-expression)
nsi-imenu-generic-parens (if nsi-imenu-show-method-args-p
nsi-imenu-method-arg-parens
nsi-imenu-method-no-arg-parens))
(goto-char (point-min))
;; Warning: When the buffer has no classes or functions, this will
;; return nil, which seems proper according to the Imenu API, but
;; causes an error in the XEmacs port of Imenu. Sigh.
(nsi-imenu-create-index-engine nil))
(defun nsi-imenu-create-index-engine (&optional start-indent)
"Function for finding Imenu definitions in Nsi.
Finds all definitions (classes, methods, or functions) in a Nsi
file for the Imenu package.
Returns a possibly nested alist of the form
(INDEX-NAME . INDEX-POSITION)
The second element of the alist may be an alist, producing a nested
list as in
(INDEX-NAME . INDEX-ALIST)
This function should not be called directly, as it calls itself
recursively and requires some setup. Rather this is the engine for
the function \\[nsi-imenu-create-index-function].
It works recursively by looking for all definitions at the current
indention level. When it finds one, it adds it to the alist. If it
finds a definition at a greater indentation level, it removes the
previous definition from the alist. In its place it adds all
definitions found at the next indentation level. When it finds a
definition that is less indented then the current level, it returns
the alist it has created thus far.
The optional argument START-INDENT indicates the starting indentation
at which to continue looking for Nsi classes, methods, or
functions. If this is not supplied, the function uses the indentation
of the first definition found."
(let (index-alist
sub-method-alist
looking-p
def-name prev-name
cur-indent def-pos
(class-paren (first nsi-imenu-generic-parens))
(def-paren (second nsi-imenu-generic-parens)))
(setq looking-p
(re-search-forward nsi-imenu-generic-regexp (point-max) t))
(while looking-p
(save-excursion
;; used to set def-name to this value but generic-extract-name
;; is new to imenu-1.14. this way it still works with
;; imenu-1.11
;;(imenu--generic-extract-name nsi-imenu-generic-parens))
(let ((cur-paren (if (match-beginning class-paren)
class-paren def-paren)))
(setq def-name
(buffer-substring-no-properties (match-beginning cur-paren)
(match-end cur-paren))))
(save-match-data
(nsi-beginning-of-def-or-class 'either))
(beginning-of-line)
(setq cur-indent (current-indentation)))
;; HACK: want to go to the next correct definition location. We
;; explicitly list them here but it would be better to have them
;; in a list.
(setq def-pos
(or (match-beginning class-paren)
(match-beginning def-paren)))
;; if we don't have a starting indent level, take this one
(or start-indent
(setq start-indent cur-indent))
;; if we don't have class name yet, take this one
(or prev-name
(setq prev-name def-name))
;; what level is the next definition on? must be same, deeper
;; or shallower indentation
(cond
;; at the same indent level, add it to the list...
((= start-indent cur-indent)
(push (cons def-name def-pos) index-alist))
;; deeper indented expression, recurse
((< start-indent cur-indent)
;; the point is currently on the expression we're supposed to
;; start on, so go back to the last expression. The recursive
;; call will find this place again and add it to the correct
;; list
(re-search-backward nsi-imenu-generic-regexp (point-min) 'move)
(setq sub-method-alist (nsi-imenu-create-index-engine cur-indent))
(if sub-method-alist
;; we put the last element on the index-alist on the start
;; of the submethod alist so the user can still get to it.
(let ((save-elmt (pop index-alist)))
(push (cons prev-name
(cons save-elmt sub-method-alist))
index-alist))))
;; found less indented expression, we're done.
(t
(setq looking-p nil)
(re-search-backward nsi-imenu-generic-regexp (point-min) t)))
;; end-cond
(setq prev-name def-name)
(and looking-p
(setq looking-p
(re-search-forward nsi-imenu-generic-regexp
(point-max) 'move))))
(nreverse index-alist)))
;;;###autoload
(defun nsi-mode ()
"Major mode for editing Nsi files.
To submit a problem report, enter `\\[nsi-submit-bug-report]' from a
`nsi-mode' buffer. Do `\\[nsi-describe-mode]' for detailed
documentation. To see what version of `nsi-mode' you are running,
enter `\\[nsi-version]'.
This mode knows about Nsi indentation, tokens, comments and
continuation lines. Paragraphs are separated by blank lines only.
COMMANDS
\\{nsi-mode-map}
VARIABLES
nsi-indent-offset\t\tindentation increment
nsi-block-comment-prefix\t\tcomment string used by `comment-region'
nsi-nsi-command\t\tshell command to invoke Nsi interpreter
nsi-temp-directory\t\tdirectory used for temp files (if needed)
nsi-beep-if-tab-change\t\tring the bell if `tab-width' is changed"
(interactive)
;; set up local variables
(kill-all-local-variables)
(make-local-variable 'font-lock-defaults)
(make-local-variable 'paragraph-separate)
(make-local-variable 'paragraph-start)
(make-local-variable 'require-final-newline)
(make-local-variable 'comment-start)
(make-local-variable 'comment-end)
(make-local-variable 'comment-start-skip)
(make-local-variable 'comment-column)
(make-local-variable 'comment-indent-function)
(make-local-variable 'indent-region-function)
(make-local-variable 'indent-line-function)
(make-local-variable 'add-log-current-defun-function)
;;
(set-syntax-table nsi-mode-syntax-table)
(setq major-mode 'nsi-mode
mode-name "Nsi"
local-abbrev-table nsi-mode-abbrev-table
font-lock-defaults '(nsi-font-lock-keywords)
paragraph-separate "^[ \t]*$"
paragraph-start "^[ \t]*$"
require-final-newline t
comment-start "; "
comment-end ""
comment-start-skip "; *"
comment-column 40
comment-indent-function 'nsi-comment-indent-function
indent-region-function 'nsi-indent-region
indent-line-function 'nsi-indent-line
;; tell add-log.el how to find the current function/method/variable
add-log-current-defun-function 'nsi-current-defun
)
(use-local-map nsi-mode-map)
;; add the menu
(if nsi-menu
(easy-menu-add nsi-menu))
;; Emacs 19 requires this
(if (boundp 'comment-multi-line)
(setq comment-multi-line nil))
;; Install Imenu if available
(when (nsi-safe (require 'imenu))
(setq imenu-create-index-function #'nsi-imenu-create-index-function)
(setq imenu-generic-expression nsi-imenu-generic-expression)
(if (fboundp 'imenu-add-to-menubar)
(imenu-add-to-menubar (format "%s-%s" "IM" mode-name)))
)
;; Run the mode hook. Note that nsi-mode-hook is deprecated.
(if nsi-mode-hook
(run-hooks 'nsi-mode-hook)
(run-hooks 'nsi-mode-hook))
;; Now do the automagical guessing
(if nsi-smart-indentation
(let ((offset nsi-indent-offset))
;; It's okay if this fails to guess a good value
(if (and (nsi-safe (nsi-guess-indent-offset))
(<= nsi-indent-offset 8)
(>= nsi-indent-offset 2))
(setq offset nsi-indent-offset))
(setq nsi-indent-offset offset)
;; Only turn indent-tabs-mode off if tab-width !=
;; nsi-indent-offset. Never turn it on, because the user must
;; have explicitly turned it off.
(if (/= tab-width nsi-indent-offset)
(setq indent-tabs-mode nil))
))
;; Set the default shell if not already set
(when (null nsi-which-shell)
(nsi-toggle-shells nsi-default-interpreter))
)
;; electric characters
(defun nsi-outdent-p ()
"Returns non-nil if the current line should dedent one level."
(save-excursion
(and (progn (back-to-indentation)
(looking-at nsi-outdent-re))
;; short circuit infloop on illegal construct
(not (bobp))
(progn (forward-line -1)
(nsi-goto-initial-line)
(back-to-indentation)
(while (or (looking-at nsi-blank-or-comment-re)
(bobp))
(backward-to-indentation 1))
(not (looking-at nsi-no-outdent-re)))
)))
(defun nsi-electric-colon (arg)
"Insert a colon.
In certain cases the line is dedented appropriately. If a numeric
argument ARG is provided, that many colons are inserted
non-electrically. Electric behavior is inhibited inside a string or
comment."
(interactive "P")
(self-insert-command (prefix-numeric-value arg))
;; are we in a string or comment?
(if (save-excursion
(let ((pps (parse-partial-sexp (save-excursion
(nsi-beginning-of-def-or-class)
(point))
(point))))
(not (or (nth 3 pps) (nth 4 pps)))))
(save-excursion
(let ((here (point))
(outdent 0)
(indent (nsi-compute-indentation t)))
(if (and (not arg)
(nsi-outdent-p)
(= indent (save-excursion
(nsi-next-statement -1)
(nsi-compute-indentation t)))
)
(setq outdent nsi-indent-offset))
;; Don't indent, only dedent. This assumes that any lines
;; that are already dedented relative to
;; nsi-compute-indentation were put there on purpose. It's
;; highly annoying to have `:' indent for you. Use TAB, C-c
;; C-l or C-c C-r to adjust. TBD: Is there a better way to
;; determine this???
(if (< (current-indentation) indent) nil
(goto-char here)
(beginning-of-line)
(delete-horizontal-space)
(indent-to (- indent outdent))
)))))
;; Nsi subprocess utilities and filters
(defun nsi-execute-file (proc filename)
"Send to Nsi interpreter process PROC \"execfile('FILENAME')\".
Make that process's buffer visible and force display. Also make
comint believe the user typed this string so that
`kill-output-from-shell' does The Right Thing."
(let ((curbuf (current-buffer))
(procbuf (process-buffer proc))
; (comint-scroll-to-bottom-on-output t)
(msg (format "## working on region in file %s...\n" filename))
(cmd (format "execfile(r'%s')\n" filename)))
(unwind-protect
(save-excursion
(set-buffer procbuf)
(goto-char (point-max))
(move-marker (process-mark proc) (point))
(funcall (process-filter proc) proc msg))
(set-buffer curbuf))
(process-send-string proc cmd)))
;; (defun nsi-comint-output-filter-function (string)
;; "Watch output for Nsi prompt and exec next file waiting in queue.
;; This function is appropriate for `comint-output-filter-functions'."
;; ;; TBD: this should probably use split-string
;; (when (and (or (string-equal string ">>> ")
;; (and (>= (length string) 5)
;; (string-equal (substring string -5) "\n>>> ")))
;; nsi-file-queue)
;; (nsi-safe (delete-file (car nsi-file-queue)))
;; (setq nsi-file-queue (cdr nsi-file-queue))
;; (if nsi-file-queue
;; (let ((pyproc (get-buffer-process (current-buffer))))
;; (nsi-execute-file pyproc (car nsi-file-queue))))
;; ))
(defun nsi-postprocess-output-buffer (buf)
"Highlight exceptions found in BUF.
If an exception occurred return t, otherwise return nil. BUF must exist."
(let (line file bol err-p)
(save-excursion
(set-buffer buf)
(beginning-of-buffer)
(while (re-search-forward nsi-traceback-line-re nil t)
(setq file (match-string 1)
line (string-to-int (match-string 2))
bol (nsi-point 'bol))
(nsi-highlight-line bol (nsi-point 'eol) file line)))
(when (and nsi-jump-on-exception line)
(beep)
(nsi-jump-to-exception file line)
(setq err-p t))
err-p))
;;; Subprocess commands
;; only used when (memq 'broken-temp-names nsi-emacs-features)
(defvar nsi-serial-number 0)
(defvar nsi-exception-buffer nil)
(defconst nsi-output-buffer "*Nsi Output*")
(make-variable-buffer-local 'nsi-output-buffer)
;; for toggling between CNsi and JNsi
(defvar nsi-which-shell nil)
(defvar nsi-which-bufname "Nsi")
(make-variable-buffer-local 'nsi-which-shell)
(make-variable-buffer-local 'nsi-which-args)
(make-variable-buffer-local 'nsi-which-bufname)
(defun nsi-toggle-shells (arg)
"Toggles between the CNsi and JNsi shells.
With positive argument ARG (interactively \\[universal-argument]),
uses the CNsi shell, with negative ARG uses the JNsi shell, and
with a zero argument, toggles the shell.
Programmatically, ARG can also be one of the symbols `cnsi' or
`jnsi', equivalent to positive arg and negative arg respectively."
(interactive "P")
;; default is to toggle
(if (null arg)
(setq arg 0))
;; preprocess arg
(cond
((equal arg 0)
;; toggle
(if (string-equal nsi-which-bufname "Nsi")
(setq arg -1)
(setq arg 1)))
((equal arg 'cnsi) (setq arg 1))
((equal arg 'jnsi) (setq arg -1)))
(let (msg)
(cond
((< 0 arg)
;; set to CNsi
(setq nsi-which-shell nsi-nsi-command
nsi-which-bufname "Nsi"
msg "CNsi"