forked from psaris/q-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
q-mode.el
2016 lines (1803 loc) · 68 KB
/
q-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
;;; q-mode.el --- modes for editing and running Q scripts -*- Emacs-Lisp -*-
;; Copyright (C) 1997-2002 Free Software Foundation, Inc.
;; Copyright (C) 1999-2002 Albert Graef
;; Author/Maintainer: Albert Graef
;; This file is part of the Q programming system.
;; The Q programming system is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; The Q programming system 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, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;; Commentary:
;; This version is for recent releases of the Q programming system (V3.0 or
;; later).
;; See "The Q Programming Language", Section "Running Scripts in GNU Emacs"
;; for a brief description.
;; To a large extent, this code has been pilfered from other language modes,
;; most notably elisp and octave mode (therefore the FSF copyright at the
;; beginning of this file). Preliminary support for autoindent and comment
;; filling is now also included (taken from Prolog and C++ modes).
;; KNOWN BUGS (Q 7.7 and later): Autoindent of equations doesn't work very
;; well with left-hand qualifiers right now; it easily gets confused by
;; 'where' clauses on the left-hand side of an equation. There's no easy way
;; to get this right without doing a full parse of the equation to be
;; indented. Thus, for the time being, the code immediately following a
;; left-hand qualifier will often have to be indented manually.
;; INSTALLATION: If necessary, edit the values of the `q-prog', `q-data-dir'
;; and `q-lib-dir' variables below.
(defvar q-prog (expand-file-name "$QHOME/l64/q"))
(defvar q-data-dir (expand-file-name "$QHOME/l64/q"))
(defvar q-lib-dir (expand-file-name "$QHOME/l64/q"))
;; Then copy this file to your site-lisp directory. The easiest way to make Q
;; mode available in emacs is to add the following to your emacs startup file:
;; (require 'q-mode)
;; Alternatively, you may specify the following autoloads:
;; (autoload 'q-mode "q-mode")
;; (autoload 'run-q "q-mode")
;; To enable Q mode for *.q files, add the following to your emacs startup
;; file:
;; (setq auto-mode-alist (cons '("\\.q$" . q-mode) auto-mode-alist))
;; Furthermore, you can enable font lock (syntax highlighting) as follows:
;; (add-hook 'q-mode-hook 'turn-on-font-lock)
;; (add-hook 'q-eval-mode-hook 'turn-on-font-lock)
;; Well, that's the way it works with XEmacs and newer GNU Emacs versions. For
;; older versions of GNU Emacs you might have to try something like:
;; (global-font-lock-mode t)
;; (add-hook 'q-mode-hook (lambda () (font-lock-mode 1)))
;; (add-hook 'q-eval-mode-hook (lambda () (font-lock-mode 1)))
;; Using the Q-Eval hook you can also rebind the cursor up and down keys to
;; the history cycling commands:
;; (add-hook 'q-eval-mode-hook
;; (lambda ()
;; (define-key q-eval-mode-map [up] 'comint-previous-input)
;; (define-key q-eval-mode-map [down] 'comint-next-input)))
;; Finally, you might wish to add some global key bindings, e.g.:
;; (global-set-key "\C-c\C-q" 'run-q)
(require 'comint)
;; customizable variables
(defgroup q nil "Major modes for editing and running Q scripts."
:group 'languages)
(defgroup q-options nil
"Q interpreter options. Note that changing any of these options does
not affect a running instance of the interpreter; rather they denote
the settings to be used when a new interpreter is started."
:group 'q)
(defcustom q-default-rhs-indent 32
"*Default indentation of the right-hand side of a rule."
:type 'integer
:group 'q )
(defcustom q-extra-decl-indent 2
"*Extra indentation of continuation lines in declarations."
:type 'integer
:group 'q )
(defcustom q-extra-qual-indent 2
"*Extra indentation of qualifiers in rules."
:type 'integer
:group 'q )
(defcustom q-hanging-comment-ender-p t
"*Controls what \\[fill-paragraph] does to Q block comment enders.
When set to nil, Q block comment enders are left on their own line.
When set to t, block comment enders will be placed at the end of the
previous line (i.e. they `hang' on that line)."
:type 'boolean
:group 'q)
(defcustom q-hanging-comment-starter-p t
"*Controls what \\[fill-paragraph] does to Q block comment starters.
When set to nil, Q block comment starters are left on their own line.
When set to t, text that follows a block comment starter will be
placed on the same line as the block comment starter (i.e. the text
`hangs' on that line)."
:type 'boolean
:group 'q)
(defcustom q-prog-name q-prog
"*Name of the interpreter executable."
:type 'string
:group 'q)
(defcustom q-prog-opts nil
"*List of extra command line options the interpreter is invoked with.
These options will be prepended to the options set in the Options group below.
See the Q online documentation for a list of available options."
:type '(repeat string)
:group 'q)
(defcustom q-prog-args nil
"*List of extra arguments the interpreter is invoked with."
:type '(repeat string)
:group 'q)
(defcustom q-path-separator path-separator
"*Separator to be used to delimit directories in `q-path' when passing the
path to the interpreter. Usually this is just identical to `path-separator'.
You only have to set this value if your Emacs assumes another path separator
than the interpreter."
:type 'string
:group 'q)
(defcustom q-path
(let ((qpath (getenv "QPATH")))
(if qpath (split-string qpath q-path-separator)
(if (string= q-data-dir q-lib-dir)
(list "." q-lib-dir)
(list "." q-data-dir q-lib-dir))))
"*A list of directories to be searched by the interpreter for scripts and
code files. Initialized from the QPATH environment variable if set."
:type '(repeat string)
:group 'q-options)
(defcustom q-histfile "~/.q_history"
"*Name of the command history file."
:type 'string
:group 'q-options)
(defcustom q-histsize 500
"*Size of the command history."
:type 'integer
:group 'q-options)
(defcustom q-int-format "dec"
"*Integer output format."
:type '(choice (const :tag "Decimal" "dec")
(const :tag "Hexadecimal" "hex")
(const :tag "Octal" "oct"))
:group 'q-options)
(defcustom q-float-format "std"
"*Floating point output format."
:type '(choice (const :tag "Standard" "std")
(const :tag "Scientific" "sci")
(const :tag "Fixed" "fix"))
:group 'q-options)
(defcustom q-float-prec 15
"*Floating point output precision."
:type 'integer
:group 'q-options)
(defcustom q-cstacksize 256
"*Maximum C stack size of the interpreter (KB)."
:type 'integer
:group 'q-options)
(defcustom q-stacksize 1024000
"*Maximum stack size of the interpreter."
:type 'integer
:group 'q-options)
(defcustom q-memsize 4096000
"*Maximum memory size of the interpreter."
:type 'integer
:group 'q-options)
(defcustom q-debug nil
"*Enable debugging."
:type 'boolean
:group 'q-options)
(defcustom q-debug-options ""
"*Debugger options."
:type 'string
:group 'q-options)
(defcustom q-break nil
"*Enable debugging after break."
:type 'boolean
:group 'q-options)
(defcustom q-echo nil
"*Enable command echoing in batch mode."
:type 'boolean
:group 'q-options)
(defcustom q-quiet t
"*Disable sign-on at startup."
:type 'boolean
:group 'q-options)
(defcustom q-query-before-kill nil
"*Indicates that the user should be prompted before zapping an existing
interpreter process when starting a new one."
:type 'boolean
:group 'q)
;; I disabled this stuff because it does not work reliably with the new \\x
;; escape sequences. Thus if you set a customized prompt, e.g., in the qinitrc
;; file, you probably have to change the q-prompt-regexp customization
;; variable accordingly. - AG
;;;; default prompts of the interpreter; make sure that the command prompt is
;;;; the first in this list, and that not all these strings are empty
;;(defvar q-prompts '("\n==> " ": " "> "))
;;(defun lastline (x)
;; (let ((l (split-string x "\n")))
;; (nth (1- (length l)) l)))
;;(defun q-make-prompt-regexp ()
;; (mapconcat
;; 'identity
;; (let ((l (mapcar (lambda (x) (concat "^" (regexp-quote x)))
;; (cdr q-prompts)))
;; (s (lastline (car q-prompts))))
;; (if (string= s "")
;; l
;; (cons (concat "^" (regexp-quote s)) l)))
;; "\\|"))
;;
;;(defcustom q-prompt-regexp (q-make-prompt-regexp)
(defcustom q-prompt-regexp "^==> \\|^[A-Za-z_0-9-]*>> \\|^: \\|^> "
"*Regexp to match prompts in the Q interpreter. If you customize the
interpreter's default prompt, you will have to change this value accordingly."
:type 'regexp
:group 'q)
(defcustom q-msg-regexp
"^[ \t]*\\([0-9]+>\\|\\sw+\\)?[ \t]+\\(\\([^ \t\n]+\\), line \\([0-9]+\\)\\):"
"*Regexp to match compiler and debugger messages with source line references
in the Q eval buffer. Expression 1 denotes the optional message prefix
(usually either \"Error\", \"Warning\" or a stack index), expression 2 the
whole source line info, expression 3 the file name and expression 4 the
corresponding line number."
:type 'regexp
:group 'q)
(defcustom q-mode-hook nil
"*Hook for customising Q mode.
For instance, add `turn-on-font-lock' to enable syntax highlighting."
:type 'hook
:group 'q)
(defcustom q-eval-mode-hook nil
"*Hook for customising Q eval mode.
For instance, add `turn-on-font-lock' to enable syntax highlighting."
:type 'hook
:group 'q)
;; the following are used internally
(defvar q-output-list nil)
(defvar q-output-string nil)
(defvar q-receive-in-progress nil)
(defvar q-last-dir nil)
(defvar q-last-script nil)
(defvar q-other-script nil)
(defvar q-last-path nil)
;; this must not be local to a buffer it is shared between the source file-
;; and the comint- buffer.
(defvar q-ldr-file nil)
;; font-lock support
(defvar q-eval-font-lock-keywords
(list
;; make sure the prompt is the first pattern, since it is updated by
;; q_prompt_cmd
(list q-prompt-regexp 0 'font-lock-type-face t)
(list "^!.*" 0 'font-lock-warning-face t)
(list "^Warning:" 0 'font-lock-warning-face t)
;; (list "\\(//\\|^#!\\).*" 0 'font-lock-comment-face t)
(list q-msg-regexp 0 'font-lock-warning-face t)
(list "\\<\\(if\\|else\\|then\\|where\\)\\>\\| ==> " 0 'font-lock-keyword-face))
"Rules for fontifying in Q-Eval mode.")
;; FIXME: need support for unicode identifiers here
(defvar q-font-lock-keywords kdbp-font-lock-keywords)
;; keymaps
(defvar q-mode-map nil)
(cond ((not q-mode-map)
(setq q-mode-map (make-sparse-keymap))
(define-key q-mode-map "\C-c\C-l" 'q-run-script)
(define-key q-mode-map "\C-c\C-x" 'q-quit-eval)
(define-key q-mode-map "\C-c\C-m" 'q-run-main)
(define-key q-mode-map "\C-c\C-u" 'q-current-msg)
(define-key q-mode-map "\C-c\C-n" 'q-next-msg)
(define-key q-mode-map "\C-c\C-p" 'q-prev-msg)
(define-key q-mode-map "\C-c\C-z" 'q-last-msg)
(define-key q-mode-map "\C-c\C-c" 'q-do-cmd)
(define-key q-mode-map "\C-c\C-e" 'q-do-cmdf)
(define-key q-mode-map "\C-c\C-b" 'q-do-buf)
(define-key q-mode-map "\C-c\C-a" 'q-first-msg)
(define-key q-mode-map "\C-c\C-f" 'q-find-script)
(define-key q-mode-map "\C-c\C-v" 'q-goto-input-line)
(define-key q-mode-map "\t" 'q-indent-line)
(define-key q-mode-map "(" 'q-electric-delim)
(define-key q-mode-map ")" 'q-electric-delim)
(define-key q-mode-map "[" 'q-electric-delim)
(define-key q-mode-map "]" 'q-electric-delim)
(define-key q-mode-map ";" 'q-electric-delim)
(define-key q-mode-map "=" 'q-electric-delim)
(define-key q-mode-map "|" 'q-electric-delim)
(define-key q-mode-map "\e\C-i" 'q-move-to-indent-column)
(define-key q-mode-map "\e\C-q" 'q-indent-current-rule)))
(defvar q-eval-mode-map nil)
(cond ((not q-eval-mode-map)
(setq q-eval-mode-map (copy-keymap comint-mode-map))
(define-key q-eval-mode-map "\t" 'comint-dynamic-complete)
(define-key q-eval-mode-map "\C-a" 'comint-bol)
(define-key q-eval-mode-map [home] 'comint-bol)
;; (define-key q-eval-mode-map [up] 'comint-previous-input)
;; (define-key q-eval-mode-map [down] 'comint-next-input)
(define-key q-eval-mode-map [return] 'q-current-msg-or-send)
(if (string-match "XEmacs\\|Lucid" emacs-version)
(define-key q-eval-mode-map [button2] 'q-mouse-msg)
(define-key q-eval-mode-map [mouse-2] 'q-mouse-msg))
(define-key q-eval-mode-map "\C-c\C-u" 'q-current-msg)
(define-key q-eval-mode-map "\C-c\C-n" 'q-next-msg)
(define-key q-eval-mode-map "\C-c\C-p" 'q-prev-msg)
(define-key q-eval-mode-map "\C-c\C-e" 'q-last-msg)
(define-key q-eval-mode-map "\C-c\C-a" 'q-first-msg)
(define-key q-eval-mode-map "\C-c\C-f" 'q-find-script)
(define-key q-eval-mode-map "\C-c\C-v" 'q-goto-input-line)))
;; menus
(defsubst q-region-is-active-p ()
;; Return t when the region is active. The determination of region
;; activeness is different in both Emacs and XEmacs.
(cond
;; XEmacs
((and (fboundp 'region-active-p)
zmacs-regions)
(region-active-p))
;; Emacs
((boundp 'mark-active) mark-active)
;; fallback; shouldn't get here
(t (mark t))))
(defvar q-mode-menu
'("Q"
["Describe Q Mode" describe-mode t]
["Customize" (customize-group 'q) t]
"-"
["Move to `=' Column" q-move-to-indent-column t]
["Indent Current Rule" q-indent-current-rule t]
["Indent Line or Region" q-indent-line-or-region t]
["Comment Out Region" comment-region (q-region-is-active-p)]
["Uncomment Region" uncomment-region (q-region-is-active-p)]
["Fill Comment Paragraph" q-fill-paragraph t]
"-"
["Start Q" run-q]
["Run Script" q-run-script t]
["Run Buffer" q-do-buf t]
["Run Group" q-do-cmd t]
["Run Function (\\l)" q-do-cmdf t]
["Find Main Script" q-find-script q-last-script]
["Find Last Script" q-find-script q-other-script]
["Run Main Script" q-run-main t]
["Goto Input Line" q-goto-input-line
(get-process "q-eval")]
"-"
["Quit Q" q-quit-eval t]
["Current Message" q-current-msg
(get-buffer "*q-eval*")]
["First Message" q-first-msg
(get-buffer "*q-eval*")]
["Next Message" q-next-msg
(get-buffer "*q-eval*")]
["Previous Message" q-prev-msg
(get-buffer "*q-eval*")]
["Last Message" q-last-msg
(get-buffer "*q-eval*")]))
(defvar q-eval-mode-menu
'("Q-Eval"
["Describe Q-Eval Mode" describe-mode t]
["Customize" (customize-group 'q) t]
"-"
["Find Main Script" q-find-script q-last-script]
["Goto Input Line" q-goto-input-line
(get-process "q-eval")]
"-"
["Current Message" q-current-msg
(get-buffer "*q-eval*")]
["First Message" q-first-msg
(get-buffer "*q-eval*")]
["Next Message" q-next-msg
(get-buffer "*q-eval*")]
["Previous Message" q-prev-msg
(get-buffer "*q-eval*")]
["Last Message" q-last-msg
(get-buffer "*q-eval*")]
"-"
["Complete Symbol" comint-dynamic-complete
(q-at-command-prompt-p)]))
;; some helper functions for q/q-eval-mode: check that we're on the command
;; resp. debugger prompt
(defun q-at-pmark-p ()
(and (get-buffer "*q-eval*")
(get-process "q-eval")
(progn (set-buffer "*q-eval*") (comint-after-pmark-p))))
(defun q-at-command-prompt-p ()
(and
(q-at-pmark-p)
(save-excursion
(forward-line 0)
(looking-at q-prompt-regexp))))
(defun q-at-debug-prompt-p ()
(and
(q-at-pmark-p)
(save-excursion
(forward-line 0)
(looking-at ":"))))
;; Q mode
;;;###autoload
(define-derived-mode q-mode kdbp-mode "Q" ()
"Major mode for editing Q scripts.
Provides the `q-run-script' (\\[q-run-script]) command to run the interpreter
on the script in the current buffer. It will be verified that the buffer has a
file associated with it, and you will be prompted to save edited buffers when
invoking this command. Special commands to quickly locate the main script and
the input line of the Q eval buffer, and to visit the source lines shown in
compiler/debugger messages are provided as well (see `q-eval-mode').
These operations can be selected from the Q mode menu (accessible from
the menu bar), which also provides commands for reading the online
help and customizing the Q/Q-Eval mode setup.
If you use \"Start Q\", you may find q complains about your .git or other . hidden
directories. Use \"Run Function\" for function definitions across multiple lines.
Command list:
\\{q-mode-map}
Entry to this mode calls the value of q-mode-hook if that value is
non-nil."
(interactive)
(kill-all-local-variables)
(set-syntax-table (make-syntax-table kdbp-mode-syntax-table))
(modify-syntax-entry ?_ "_")
(modify-syntax-entry ?\. "_")
(modify-syntax-entry ?\+ ".")
(modify-syntax-entry ?\- ".")
(modify-syntax-entry ?\= ".")
(modify-syntax-entry ?\< ".")
(modify-syntax-entry ?\> ".")
(modify-syntax-entry ?\$ ".")
(modify-syntax-entry ?\| ".")
(cond
((string-match "XEmacs\\|Lucid" emacs-version)
(modify-syntax-entry ?/ ". 1456")
(modify-syntax-entry ?* ". 23"))
(t
(modify-syntax-entry ?/ ". 124b")
(modify-syntax-entry ?* ". 23")))
(modify-syntax-entry ?\n "> b")
(modify-syntax-entry ?\^m "> b")
(setq major-mode 'q-mode)
(setq mode-name "Q")
(use-local-map q-mode-map)
;; (use-local-map q-mode-map)
;; (derived-mode-set-keymap 'q-mode)
(setq-local font-lock-multiline t)
(setq-local font-lock-defaults
'(kdbp-font-lock-keywords nil nil ((?_ . "w"))))
;; (setq paragraph-start (concat "^$\\|" page-delimiter))
;; (setq paragraph-start (concat "^//\\|^$\\|" page-delimiter))
(setq-local paragraph-start (concat page-delimiter "\\|$"))
(setq-local paragraph-separate paragraph-start)
(setq-local paragraph-ignore-fill-prefix t)
(if (boundp 'fill-paragraph-function)
(progn
(setq-local fill-paragraph-function 'q-fill-paragraph)))
(setq-local indent-line-function 'q-indent-line)
(setq-local indent-region-function 'q-indent-region)
(setq q-ldr-file (make-temp-file "q-" nil ".q"))
(make-local-variable 'comment-start)
(make-local-variable 'comment-end)
(make-local-variable 'comment-column)
(make-local-variable 'comment-start-skip)
(make-local-variable 'comment-multi-line)
(make-local-variable 'q-prog-args)
;; "^\\s-*\\([[:alpha:]_][[:alnum:]_]*\\)\\s-*:"
;; this is for func-menu
(setq-local imenu-generic-expression
'((nil
"^\\s-*\\([[:alpha:]_][[:alnum:]_]*\\)\\s-*:\\s-*{" 1)
("V" "^\\s-*\\([[:alpha:]_.][[:alnum:]_.]*\\)\\s-*:\\s-*[^{]+$" 1)
("G" "^\\s-*\\([[:alpha:]_.][[:alnum:]_.]*\\)\\s-*:\\s-*{" 1)))
(require 'easymenu)
(easy-menu-define q-mode-menu-map q-mode-map
"Menu keymap for Q mode." q-mode-menu)
(easy-menu-add q-mode-menu-map q-mode-map)
(run-hooks 'q-mode-hook))
;; Not used.
(defun q-minor-mode ()
"Minor mode for editing Q scripts.
Provides the `q-run-script' (\\[q-run-script]) command to run the interpreter
on the script in the current buffer. It will be verified that the buffer has a
file associated with it, and you will be prompted to save edited buffers when
invoking this command. Special commands to quickly locate the main script and
the input line of the Q eval buffer, and to visit the source lines shown in
compiler/debugger messages are provided as well (see `q-eval-mode').
These operations can be selected from the Q mode menu (accessible from
the menu bar), which also provides commands for reading the online
help and customizing the Q/Q-Eval mode setup.
Command list:
\\{q-mode-map}
Entry to this mode calls the value of q-mode-hook if that value is
non-nil."
(setq-local paragraph-start (concat page-delimiter "\\|$"))
(setq-local paragraph-separate paragraph-start)
(setq-local paragraph-ignore-fill-prefix t)
(if (boundp 'fill-paragraph-function)
(progn
(setq-local fill-paragraph-function 'q-fill-paragraph)))
(setq-local indent-line-function 'q-indent-line)
(setq-local indent-region-function 'q-indent-region)
(setq-local comment-column 48
comment-start "// "
comment-end ""
comment-start-skip "/\\*+ *\\|// *\\|^#! *"
comment-multi-line nil
)
(make-local-variable 'q-prog-args)
(setq-local comment-indent-function 'q-comment-indent)
;; (make-local-variable 'font-lock-defaults)
;; (setq font-lock-defaults '(q-font-lock-keywords nil nil ((?_ . "w"))))
(require 'easymenu)
(easy-menu-define q-mode-menu-map q-mode-map
"Menu keymap for Q mode." q-mode-menu)
(easy-menu-add q-mode-menu-map q-mode-map)
(run-hooks 'q-minor-mode-hook))
;; Q eval mode
(defun q-eval-mode ()
"Major mode for interacting with the Q interpreter, based on comint-mode.
Provides the `q-current-msg-or-send' (\\[q-current-msg-or-send]) command,
which, when point is at a compiler or debugger message describing a source
reference, visits the given line in the corresponding source file in another
window. Otherwise it runs the `comint-send-input' command, which usually
submits a command line to the interpreter, or copies it to the command prompt
when point is not at the current command line.
Compiler and debugger messages are indicated with a special font, and in
XEmacs they will also be highlighted when the mouse passes over
them. Moreover, pressing the middle mouse button (button2) over such a message
visits the corresponding source line in another window (`q-mouse-msg'
command); anywhere else, the middle mouse button invokes the usual
`mouse-yank' command, so that you can also use the mouse to perform xterm-like
cut and paste in the Q-Eval buffer.
You can also use the `q-first-msg' (\\[q-first-msg]), `q-next-msg'
(\\[q-next-msg]), `q-prev-msg' (\\[q-prev-msg]) and `q-last-msg'
(\\[q-last-msg]) commands to scan through the compiler and debugger messages
found in the buffer. The `q-find-script' (\\[q-find-script]) command lets you
visit the script that is currently running, and `q-goto-input-line'
(\\[q-goto-input-line]) quickly takes you to the prompt at the current input
line in the Q eval buffer. (These commands are also provided in Q mode. If you
like, you can bind them globally, so that you can invoke them from other kinds
of buffers as well.)
Besides this, you can use the usual comint commands, see the description of
`comint-mode' for details. Some important commands are listed below:
\\[comint-previous-input] and \\[comint-next-input] cycle through the command history.
\\[comint-previous-matching-input] and \\[comint-next-matching-input] search the command history.
\\[comint-interrupt-subjob] sends a Ctl-C to the interpreter.
\\[comint-send-eof] sends a Ctl-D to the interpreter.
\\[comint-dynamic-list-input-ring] lists the command history.
\\[comint-dynamic-complete] performs symbol and filename completion.
Note that in difference to standard comint mode, the C-a/home keys are rebound
to `comint-bol', to mimic the behaviour of the default binding of these keys
in the interpreter.
Most of these operations can also be selected from the Comint and Q-Eval mode
menus accessible from the menu bar. The Q-Eval menu also provides operations
for reading the online help and customizing Q/Q-Eval mode setup. Moreover, a
History menu is provided from which the most recent commands can be selected.
The interpreter's prompt and lines containing compiler and debugger messages
are described by the variables `q-prompt-regexp' and `q-msg-regexp'. The
history file and size is given by the `q-histfile' and `q-histsize'
variables.
A complete command list is given below:
\\{q-eval-mode-map}
Entry to this mode runs the hooks on `comint-mode-hook' and `q-eval-mode-hook'
(in that order)."
(interactive)
(kill-all-local-variables)
(comint-mode)
(set-syntax-table (make-syntax-table kdbp-mode-syntax-table))
(modify-syntax-entry ?_ "_")
(modify-syntax-entry ?\. "_")
(modify-syntax-entry ?\+ ".")
(modify-syntax-entry ?\- ".")
(modify-syntax-entry ?\= ".")
(modify-syntax-entry ?\< ".")
(modify-syntax-entry ?\> ".")
(modify-syntax-entry ?\| ".")
(modify-syntax-entry ?\$ ".")
(modify-syntax-entry ?\/ ". 12")
(modify-syntax-entry ?\* ".")
(modify-syntax-entry ?\n ">")
(modify-syntax-entry ?\^m ">")
(setq major-mode 'q-eval-mode)
(setq mode-name "Q-Eval")
(use-local-map q-eval-mode-map)
(setq comint-prompt-regexp q-prompt-regexp)
(setq-local paragraph-start comint-prompt-regexp)
(make-local-variable 'comment-start)
(make-local-variable 'comment-end)
(setq-local comment-column 48
comment-start-skip "// *\\|^#! *"
comment-multi-line nil)
(setq-local font-lock-defaults
'(q-eval-font-lock-keywords nil nil ((?_ . "w"))))
(setq comint-input-ring-file-name q-histfile
comint-input-ring-size q-histsize
comint-dynamic-complete-functions
'(q-complete comint-dynamic-complete-filename))
;; mouse-sensitive messages (requires XEmacs)
(cond
((string-match "XEmacs\\|Lucid" emacs-version)
(require 'mode-motion)
(setq mode-motion-hook 'q-motion-hook)))
(comint-read-input-ring t)
(require 'easymenu)
(easy-menu-define q-eval-mode-menu-map q-eval-mode-map
"Menu keymap for Q mode." q-eval-mode-menu)
(easy-menu-add q-eval-mode-menu-map q-eval-mode-map)
(run-hooks 'q-eval-mode-hook))
;; make sure win32 XEmacs quotes arguments containing whitespace
(if (string-match "XEmacs.*-win32" (emacs-version))
(defun q-quote-arg (x)
(if (string-match "[ \t]" x) (concat "\"" x "\"") x))
(defun q-quote-arg (x) x))
;;;###autoload
(defun run-q (&rest args)
"Run the interpreter with given arguments, in buffer *q-eval*.
The interpreter is invoked in the directory of the current buffer (current
default directory if no file is associated with the current buffer).
If buffer exists but process is not running, make new process.
If buffer exists and process is running, kill it and start a new one.
Program used comes from variable `q-prog-name'. Extra options and arguments
can be specified in the variables `q-prog-opts' and `q-prog-args'. The buffer
is put in Q eval mode, giving commands for visiting source files, sending
input, manipulating the command history, etc. See `q-eval-mode'.
\(Type \\[describe-mode] in the Q eval buffer for a list of commands.)"
(interactive)
(let* ((dir (if buffer-file-name
(file-name-directory (buffer-file-name))
default-directory))
(q-eval-active (not (null (get-buffer "*q-eval*"))))
(q-eval-running (comint-check-proc "*q-eval*"))
(q-eval-buffer (get-buffer-create "*q-eval*")))
(if (and q-eval-running
q-query-before-kill
(not
(y-or-n-p
"An interpreter process is still running. Start a new one? ")))
(message "Aborted")
(set-buffer q-eval-buffer)
;; give process some time to terminate, then blast it away
(if q-eval-running
(progn
(comint-send-eof)
(sleep-for .5)))
(if (comint-check-proc "*q-eval*")
(progn
(comint-kill-subjob)
(sleep-for .1)))
(cd dir)
(if (not q-eval-active)
(q-eval-mode)
(if (and q-eval-running
(or (not (string-equal
comint-input-ring-file-name q-histfile))
(not (= comint-input-ring-size q-histsize))))
;; reset history in case any of the options have changed
(progn
(comint-write-input-ring)
(setq comint-input-ring-file-name q-histfile
comint-input-ring-size q-histsize)
(comint-read-input-ring t))))
;; show the directory and other parameters with which we start the
;; new process
(goto-char (point-max))
;;(if q-eval-active (insert "\n"))
(setq default-args (mapconcat 'identity (default-value 'q-prog-args) " "))
(setq input-args (mapconcat 'identity args " "))
;; (insert "\n// [" (directory-file-name dir) "] "
;; (mapconcat 'identity (list input-args default-args) ":") "\n")
(setq largs (if (car (cdr args)) (split-string (car (cdr args))) (list "")))
(setq dntarg (directory-file-name dir))
(setq fntarg (list (car args)))
(setq targ (list (mapconcat 'identity (append (list dntarg) fntarg ) "/")))
;; (insert "\n// [" (directory-file-name dir) "] "
;; (mapconcat 'identity (append targ largs) ":") "\n")
;; we pass q-path in an environment variable since it can be long;
;; hopefully, this prevents probs on systems with small maximum command
;; line length
(setenv "QPATH" (mapconcat 'identity q-path q-path-separator))
;; invoke the interpreter
(let ((qargs
(append q-prog-opts
targ largs)))
(comint-exec q-eval-buffer "q-eval" q-prog-name nil qargs))
;; set up process parameters
(setq q-output-list nil
q-output-string nil
q-receive-in-progress nil
q-last-script nil
q-last-dir dir
q-last-path nil)
(set-process-sentinel (get-process "q-eval") 'q-eval-sentinel)
;; switch to and go to the end of the eval buffer
(pop-to-buffer "*q-eval*")
(goto-char (point-max))))
)
(defun q-quit-eval ()
"Depending on whether point is at a compiler/debugger message, either
execute a `q-current-msg' or a `comint-send-input' command. This must be
invoked from the Q-Eval buffer."
(interactive)
(if (save-excursion (forward-line 0) (looking-at q-msg-regexp))
(q-current-msg)
(comint-simple-send "*q-eval*" "\\\\")))
(defun q-run-script ()
"Run the interpreter with the script in the current buffer, in buffer
*q-eval*. See `run-q' for details."
(interactive)
(let ((script-file
(if (buffer-file-name)
(file-name-nondirectory (buffer-file-name))
(error "Buffer is not associated with any file"))))
(save-some-buffers 1)
(revert-buffer t t) ; forces a re-read of the local variables, no prompt
(run-q script-file q-prog-args)
(setq q-last-script script-file)))
(defun q-run-main ()
"Run the interpreter with the script in the current buffer, in buffer
*q-eval*. See `run-q' for details."
(interactive)
(let ((script-file
(if (buffer-file-name)
(file-name-nondirectory (buffer-file-name))
(error "Buffer is not associated with any file"))))
(q-find-script)
(q-run-script)
(switch-to-buffer-other-window script-file)
(setq q-other-script script-file)))
;; find a script on the Q library path
(defun q-locate-script (file)
(let ((script (locate-library file t q-path)))
(if script
script
(error (concat "File " file " not found on search path")))))
;; visit source lines of error and debugging messages
(defun q-current-msg ()
"Show the source line referenced by a compiler/debugger message on the
current line in the Q eval buffer."
(interactive)
(let ((actwindow (selected-window)))
(if (get-buffer "*q-eval*")
(pop-to-buffer "*q-eval*")
(error "No script is running"))
(cond
((save-excursion (forward-line 0) (looking-at q-msg-regexp))
(forward-line 0) (recenter 0)
(let (visit-buffer
visit-line
(file (match-string 3)) (line (match-string 4)))
(setq visit-buffer (find-file-noselect (q-locate-script file)))
(setq visit-line (string-to-number line))
(message "%s, line %s" file line)
(switch-to-buffer-other-window visit-buffer)
(goto-line visit-line)))
(t
(select-window actwindow)
(error "No message found")))))
(defun q-current-msg-or-send ()
"Depending on whether point is at a compiler/debugger message, either
execute a `q-current-msg' or a `comint-send-input' command. This must be
invoked from the Q-Eval buffer."
(interactive)
(if (save-excursion (forward-line 0) (looking-at q-msg-regexp))
(q-current-msg)
(comint-send-input)))
(defun q-next-msg (&optional count)
"Advance to the next Q compiler/debugger message below the current line in
the Q eval buffer, and show the referenced source line in another
window. When used with a numeric argument n, advance to the nth message below
the current line (move backwards if numeric argument is negative).
Note that this command can easily be fooled if the running script produces
some output, or you insert some text, which looks like a compiler or debugger
message, so you should take care what you're doing."
(interactive "P")
(if (and (numberp count) (< count 0))
(q-prev-msg (- count))
(if (null count) (setq count 1))
(let ((actwindow (selected-window)))
(if (get-buffer "*q-eval*")
(pop-to-buffer "*q-eval*")
(error "No script is running"))
(forward-line 0)
(if (looking-at q-msg-regexp)
(if (save-excursion (end-of-line) (not (eobp)))
(forward-line 1)
(error "No more messages")))
(let ((pos (re-search-forward q-msg-regexp nil t count)))
(if pos
(let ((file (match-string 3)) (line (match-string 4)))
(goto-char pos)
(recenter 0)
(find-file-other-window (q-locate-script file))
(goto-line (string-to-number line))
(message "%s, line %s" file line))
(select-window actwindow)
(error "No more messages"))))))
(defun q-prev-msg (&optional count)
"Advance to previous Q compiler/debugger messages above the current line in
the Q eval buffer, and show the referenced source line in another
window. Like `q-next-msg', but moves backward."
(interactive "P")
(if (and (numberp count) (< count 0))
(q-next-msg (- count))
(if (null count) (setq count 1))
(let ((actwindow (selected-window)))
(if (get-buffer "*q-eval*")
(pop-to-buffer "*q-eval*")
(error "No script is running"))
(forward-line 0)
(let ((pos (re-search-backward q-msg-regexp nil t count)))
(if pos
(let ((file (match-string 3)) (line (match-string 4)))
(goto-char pos)
(recenter 0)
(find-file-other-window (q-locate-script file))
(goto-line (string-to-number line))
(message "%s, line %s" file line))
(select-window actwindow)
(error "No more messages"))))))
(defun q-last-msg ()
"Advance to the last message in a contiguous sequence of compiler/debugger
messages at or below the current line, and show the referenced source line
in another window."
(interactive)
(let ((actwindow (selected-window)))
(if (get-buffer "*q-eval*")
(pop-to-buffer "*q-eval*")
(error "No script is running"))
(forward-line 0)
(let ((pos
(if (looking-at q-msg-regexp)
(point)
(re-search-forward q-msg-regexp nil t))))
(if pos
(progn
(goto-char pos)
(while (and (save-excursion (end-of-line) (not (eobp)))
(save-excursion (forward-line 1)
(looking-at q-msg-regexp)))
(forward-line 1))
(let ((file (match-string 3)) (line (match-string 4)))
(recenter 0)
(find-file-other-window (q-locate-script file))
(goto-line (string-to-number line))
(message "%s, line %s" file line)))
(select-window actwindow)
(error "No more messages")))))
(defun q-first-msg ()
"Advance to the first message in a contiguous sequence of compiler/debugger
messages at or above the current line, and show the referenced source line
in another window."
(interactive)
(let ((actwindow (selected-window)))
(if (get-buffer "*q-eval*")
(pop-to-buffer "*q-eval*")
(error "No script is running"))
(forward-line 0)
(let ((pos
(if (looking-at q-msg-regexp)