-
Notifications
You must be signed in to change notification settings - Fork 1
/
teco.el
2412 lines (2190 loc) · 77.5 KB
/
teco.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
;;; teco.el --- Teco interpreter
;; todo
;; - maybe implement :l as end-of-line since 'l -c' doesn't always
;; work (e.g. not terminating newline)
;; This program is free software: you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of
;; the License, or (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public
;; License along with this program. If not, see
;; <https://www.gnu.org/licenses/>.
;; WARRANTY DISCLAIMER
;; This software was created by Dale R. Worley and is
;; distributed free of charge. It is placed in the public domain and
;; permission is granted to anyone to use, duplicate, modify and redistribute
;; it provided that this notice is attached.
;; Dale R. Worley provides absolutely NO WARRANTY OF ANY KIND
;; with respect to this software. The entire risk as to the quality and
;; performance of this software is with the user. IN NO EVENT WILL DALE R.
;; WORLEY BE LIABLE TO ANYONE FOR ANY DAMAGES ARISING OUT THE
;; USE OF THIS SOFTWARE, INCLUDING, WITHOUT LIMITATION, DAMAGES RESULTING FROM
;; LOST DATA OR LOST PROFITS, OR FOR ANY SPECIAL, INCIDENTAL OR CONSEQUENTIAL
;; DAMAGES.
;; Since much of this code is translated from the C version by
;; Matt Fichtenbaum, I include his copyright notice:
;; TECO for Ultrix. Copyright 1986 Matt Fichtenbaum.
;; This program and its components belong to GenRad Inc, Concord MA 01742.
;; They may be copied if this copyright notice is included.
;; Author: Dale R. Worley <worley@alum.mit.edu>
;; Maintainer: Mark T. Kennedy <mtk@acm.org>
;; Created: 09-02-1996
;; Version: 9
;; Keywords: convenience emulations files
;; URL: https://github.com/mtk/teco.git
;;; Commentary:
;; This code has been tested some, but no doubt contains a zillion bugs.
;; You have been warned.
;;; Philosophy:
;; Real programmers don't want "what you see is what you get", they want
;; "you asked for it, you got it". They want editors that are terse,
;; powerful, cryptic, and unforgiving. In a word, Teco.
;; after Ed Post, "Real Programmers Don't Use Pascal", in Datamation,
;; July 1983, p. 264
;;; Change Log:
;; Normally, best practice says not to include a change log since it
;; replicates info from underlying source code management systems.
;; But this package is so old, it predates git! So I'm leaving this
;; static text here as developer documentation. - mtk@acm.org
;; Version 1
;; Original implementation
;; Version 2
;; Fix bugs found by Alan Katz in S.
;; Version 3
;; Fix bugs found by Lum Johnson in key-binding code.
;; Fix handling of ^C, ^G, and ^L in command input.
;; Fix <...> so it iterates indefinitely. (Found by Mark Henderson.)
;; Fix ; so it exits from iterations correctly.
;; Add FR and FS commands.
;; Make commands that are supposed to set ^S do so.
;; Make flow-control commands clear the @-flag, so the @-flag can be
;; statically predicted during skipping of code.
;; Set up immediate-action commands ?, /, and *q.
;; Version 4
;; The T, D, and K commands weren't clearing their arguments.
;; Since ! is a flow-control command (O can go to it), it must clear the
;; @-flag.
;; Version 5
;; Fix bug found by karl@kelp.boston.ma.us in teco:output, causing trace output
;; to generate errors. It also caused =, ==, and === operators to generate
;; errors.
;; Put in improved disclaimer of copyright and warranty.
;; Added teco:version variable and function.
;; Version 6
;; Add attribution to "Philosophy".
;; Fix handling of negative arguments to S. (Fix due to Bill Freeman.)
;; Fix order of arguments produced by ^Y. (Fix due to Bill Freeman.)
;; Add ES flag to control placement of cursor after reverse searches.
;; (because different Tecos handle cursor placement differently)
;; Altered expression computation so that "-" alone yields -1 as a value.
;; (useful for -Sabc$)
;; Add n,mL command, mostly for use with FW.
;; Add FL and FW commands.
;; Add FR and FS to list of commands.
;; Add FE to execute Emacs lisp code.
;; Add teco:copy-to-q-reg to make loading q-regs easier.
;; Function 'teco' added as alias for 'teco:command', to make invocation with
;; M-x easier.
;; Fix an assortment of bugs and bad coding found by karl@kelp.boston.ma.us.
;; Version 7
;; Changed construction of teco:command-keymap to work in Emacs 19.30.
;; (This may be non-compatible with Emacs 18.)
;; Fixed minor bugs revealed by byte-compile.
;; Version 8
;; Change `last-command-char' to new `last-command-event'.
;; Fixed some bugs revealed by byte-compile.
;; Added support for evil, try to add the ability to remap
;; teco:command-escape, which is one of the original feature
;; of TECO.
;; Version 9
;; Someone fixed a problem with the macro syntax used to define teco
;; operators.
;; Someone simplified the code used to display Escape's as $'s in the
;; minibuffer.
;; Someone fixed the code that returns the minibuffer contents to the
;; TECO interpreter to strip out the prompt and text properties in
;; order to return a plain string.
;; It wasn't Dale Worley (I asked). So this now works with at least
;; EMACS version 28.
;;; Code:
;; The version number
(defvar teco-version "7.9"
"The version of Teco.")
(defun teco-version ()
"Return string describing the version of Teco.
When called interactively, displays the version."
(interactive)
(if (called-interactively-p 'interactive)
(message "Teco version %s" (teco-version))
teco-version))
;; set a range of elements of an array to a value
(defun teco:set-elements (array start end value)
(let ((i start))
(while (<= i end)
(aset array i value)
(setq i (1+ i)))))
;; set a range of elements of an array to their indexes plus an offset
(defun teco:set-elements-index (array start end offset)
(let ((i start))
(while (<= i end)
(aset array i (+ i offset))
(setq i (1+ i)))))
(defvar teco:command-string ""
"The current command string being executed.")
(defvar teco:command-pointer nil
"Pointer into teco:command-string showing next character to be executed.")
(defvar teco:ctrl-r 10
"Current number radix.")
(defvar teco:et-flag 0
"ET flags:
8 do not echo ^T input
32 do not wait for ^T input if no character is available.")
(defvar teco:es-flag 0
"ES flags:
1 leave pointer at end of found string after reverse search,
rather than at beginning.")
(defvar teco:ctrl-s 0
"The negative of the length of the last string inserted or searched for.
Set by the S, I, TAB, G, FR, FS, and \\ commands.")
(defvar teco:digit-switch nil
"Set if we have just executed a digit.")
(defvar teco:exp-exp nil
"Expression value preceeding operator.")
(defvar teco:exp-val1 nil
"Current argument value.")
(defvar teco:exp-val2 nil
"Argument before comma.")
(defvar teco:exp-flag1 nil
"t if argument is present.")
(defvar teco:exp-flag2 nil
"t if argument before comma is present.")
(defvar teco:exp-op nil
"Pending arithmetic operation on argument.")
(defvar teco:exp-stack nil
"Stack for parenthesized expressions.")
(defvar teco:macro-stack nil
"Stack for macro invocations.")
(defvar teco:mapch-l nil
"Translation table to lower-case letters.")
(setq teco:mapch-l (make-vector 256 0))
(teco:set-elements-index teco:mapch-l 0 255 0)
(teco:set-elements-index teco:mapch-l ?A ?Z (- ?a ?A))
(defvar teco:trace nil
"t if tracing is on.")
(defvar teco:at-flag nil
"t if an @ flag is pending.")
(defvar teco:colon-flag nil
"1 if a : flag is pending, 2 if a :: flag is pending.")
(defvar teco:qspec-valid nil
"Flags describing whether a character is a vaid q-register name.
3 means yes, 2 means yes but only for file and search operations.")
(setq teco:qspec-valid (make-vector 256 0))
(teco:set-elements teco:qspec-valid ?a ?z 3)
(teco:set-elements teco:qspec-valid ?0 ?9 3)
(aset teco:qspec-valid ?_ 2)
(aset teco:qspec-valid ?# 2)
(aset teco:qspec-valid ?* 2)
(aset teco:qspec-valid ?% 2)
(defvar teco:iteration-stack nil
"Iteration list.")
(defvar teco:cond-stack nil
"Conditional stack.")
(defvar teco:qreg-text (make-vector 256 "")
"The text contents of the q-registers.")
(defvar teco:qreg-number (make-vector 256 0)
"The number contents of the q-registers.")
(defvar teco:qreg-stack nil
"The stack of saved q-registers.")
(defconst teco:prompt "*"
"*Prompt to be used when inputting Teco command.")
(defconst teco:exec-1 (make-vector 256 nil)
"Names of routines handling type 1 characters (characters that are
part of expression processing).")
(defconst teco:exec-2 (make-vector 256 nil)
"Names of routines handling type 2 characters (characters that are
not part of expression processing).")
(defvar teco:last-search-regexp ""
"Regexp version of last search string (q-reg '_').")
(defvar teco:search-result 0
"Result of the last search.")
(defmacro teco:define-type-1 (char &rest body)
"Define the code to process a type 1 character.
Transforms
(teco:define-type-1 ?x
code ...)
into
(defun teco:type-1-x ()
code ...)
and does
(aset teco:exec-1 ?x 'teco:type-1-x)"
(let ((s (intern (concat "teco:type-1-" (char-to-string char)))))
`(progn
(defun ,s ()
,@body)
(aset teco:exec-1 ,char (quote ,s)))))
(defmacro teco:define-type-2 (char &rest body)
"Define the code to process a type 2 character.
Transforms
(teco:define-type-2 ?x
code ...)
into
(defun teco:type-2-x ()
code ...)
and does
(aset teco:exec-2 ?x 'teco:type-2-x)"
(let ((s (intern (concat "teco:type-2-" (char-to-string char)))))
`(progn
(defun ,s ()
,@body)
(aset teco:exec-2 ,char (quote ,s)))))
(defconst teco:char-types (make-vector 256 0)
"Define the characteristics of characters, as tested by \":
1 alphabetic
2 alphabetic, $, or .
4 digit
8 alphabetic or digit
16 lower-case alphabetic
32 upper-case alphabetic")
(teco:set-elements teco:char-types ?0 ?9 (+ 4 8))
(teco:set-elements teco:char-types ?A ?Z (+ 1 2 8 32))
(teco:set-elements teco:char-types ?a ?z (+ 1 2 8 16))
(aset teco:char-types ?$ 2)
(aset teco:char-types ?. 2)
(defconst teco:error-texts '(("BNI" . "> not in iteration")
("CPQ" . "Can't pop Q register")
("COF" . "Can't open output file ")
("FNF" . "File not found ")
("IEC" . "Invalid E character")
("IFC" . "Invalid F character")
("IIA" . "Invalid insert arg")
("ILL" . "Invalid command")
("ILN" . "Invalid number")
("IPA" . "Invalid P arg")
("IQC" . "Invalid \" character")
("IQN" . "Invalid Q-reg name")
("IRA" . "Invalid radix arg")
("ISA" . "Invalid search arg")
("ISS" . "Invalid search or text string")
("IUC" . "Invalid ^ character")
("LNF" . "Label not found")
("MEM" . "Insufficient memory available")
("MRP" . "Missing )")
("NAB" . "No arg before ^_")
("NAC" . "No arg before ,")
("NAE" . "No arg before =")
("NAP" . "No arg before )")
("NAQ" . "No arg before \"")
("NAS" . "No arg before ;")
("NAU" . "No arg before U")
("NFI" . "No file for input")
("NFO" . "No file for output")
("NYA" . "Numeric arg with Y")
("OFO" . "Output file already open")
("PDO" . "Pushdown list overflow")
("POP" . "Pointer off page")
("SNI" . "; not in iteration")
("SRH" . "Search failure ")
("STL" . "String too long")
("UTC" . "Unterminated command")
("UTM" . "Unterminated macro")
("XAB" . "Execution interrupted")
("YCA" . "Y command suppressed")
("IWA" . "Invalid W arg")
("NFR" . "Numeric arg with FR")
("INT" . "Internal error")
("EFI" . "EOF read from std input")
("IAA" . "Invalid A arg")))
(defconst teco:spec-chars
[0 1 0 0 ; ^@ ^A ^B ^C
0 64 0 0 ; ^D ^E ^F ^G
0 2 128 128 ; ^H ^I ^J ^K
128 0 64 0 ; ^L ^M ^N ^O
0 64 64 64 ; ^P ^Q ^R ^S
0 34 0 0 ; ^T ^U ^V ^W
64 0 0 0 ; ^X ^Y ^Z ^\[
0 0 1 0 ; ^\ ^\] ^^ ^_
0 1025 1040 0 ; ! \" #
0 0 0 1040; $ % & '
0 0 0 0 ; \( \) * +
0 0 0 0 ; , - . /
0 0 0 0 ; 0 1 2 3
0 0 0 0 ; 4 5 6 7
0 0 0 0 ; 8 9 : ;
1040 0 1040 0 ; < = > ?
1 0 12 0 ; @ A B C
0 9 1 32 ; D E F G
0 2 0 0 ; H I J K
0 32 10 1026; L M N O
0 32 8 514 ; P Q R S
0 32 0 4 ; T U V W
32 0 0 32 ; X Y Z \[
0 32 1 2 ; \ \] ^ _
0 0 12 0 ; ` a b c
0 9 1 32 ; d e f g
0 2 0 0 ; h i j k
0 32 10 1026; l m n o
0 32 8 514 ; p q r s
0 32 0 4 ; t u v w
32 0 0 0 ; x y z {
16 0 0 0 ; | } ~ DEL
]
"The special properties of characters:
1 skipto() special character
2 command with std text argument
4 E<char> takes a text argument
8 F<char> takes a text argument
16 char causes skipto() to exit
32 command with q-register argument
64 special char in search string
128 character is a line separator
256 command with a double text argument
512 F<char> takes a double text argument
1024 transfer of control command")
(defun teco:execute-command (string)
"Execute teco command string."
;; Initialize everything
(let ((teco:command-string string)
(teco:command-pointer 0)
(teco:digit-switch nil)
(teco:exp-exp nil)
(teco:exp-val1 nil)
(teco:exp-val2 nil)
(teco:exp-flag1 nil)
(teco:exp-flag2 nil)
(teco:exp-op 'start)
(teco:trace nil)
(teco:at-flag nil)
(teco:colon-flag nil)
(teco:iteration-stack nil)
(teco:cond-stack nil)
(teco:exp-stack nil)
(teco:macro-stack nil)
(teco:qreg-stack nil)
(teco:search-result 0))
;; save command string
(aset teco:qreg-text ?* (aref teco:qreg-text ?#))
(aset teco:qreg-text ?# string)
;; initialize output
(teco:out-init)
;; execute commands
(catch 'teco:exit
(while t
;; get next command character
(let ((cmdc (teco:get-command0 teco:trace)))
;; if it's ^, interpret the next character as a control character
(if (eq cmdc ?^)
(setq cmdc (logand (teco:get-command teco:trace) 31)))
(if (and (<= ?0 cmdc) (<= cmdc ?9))
;; process a number
(progn
(setq cmdc (- cmdc ?0))
;; check for invalid digit
(if (>= cmdc teco:ctrl-r)
(teco:error "ILN"))
(if teco:digit-switch
;; later digits
(setq teco:exp-val1
(+ (* teco:exp-val1 teco:ctrl-r) cmdc))
;; first digit
(setq teco:exp-val1 cmdc)
(setq teco:digit-switch t))
;; indicate a value was read in
(setq teco:exp-flag1 t))
;; not a digit
(setq teco:digit-switch nil)
;; cannonicalize the case
(setq cmdc (aref teco:mapch-l cmdc))
;; dispatch on the character, if it is a type 1 character
(let ((r (aref teco:exec-1 cmdc)))
(if r
(funcall r)
;; if a value has been entered, process any pending operation
(if teco:exp-flag1
(cond ((eq teco:exp-op 'start)
nil)
((eq teco:exp-op 'add)
(setq teco:exp-val1 (+ teco:exp-exp teco:exp-val1))
(setq teco:exp-op 'start))
((eq teco:exp-op 'sub)
(setq teco:exp-val1 (- teco:exp-exp teco:exp-val1))
(setq teco:exp-op 'start))
((eq teco:exp-op 'mult)
(setq teco:exp-val1 (* teco:exp-exp teco:exp-val1))
(setq teco:exp-op 'start))
((eq teco:exp-op 'div)
(setq teco:exp-val1
(if (/= teco:exp-val1 0)
(/ teco:exp-exp teco:exp-val1)
0))
(setq teco:exp-op 'start))
((eq teco:exp-op 'and)
(setq teco:exp-val1
(logand teco:exp-exp teco:exp-val1))
(setq teco:exp-op 'start))
((eq teco:exp-op 'or)
(setq teco:exp-val1
(logior teco:exp-exp teco:exp-val1))
(setq teco:exp-op 'start)))
;; a solitary '-' yields -1
(if (eq teco:exp-op 'sub)
(setq teco:exp-val1 -1
teco:exp-op 'start
teco:exp-flag1 t)))
;; dispatch on a type 2 character
(let ((r (aref teco:exec-2 cmdc)))
(if r
(funcall r)
(teco:error "ILL")))))))))))
;; Type 1 commands
(teco:define-type-1
?\^m ; CR
nil)
(teco:define-type-1
?\n ; LF
nil)
(teco:define-type-1
?\^k ; VT
nil)
(teco:define-type-1
?\^l ; FF
nil)
(teco:define-type-1
32 ; SPC
nil)
(teco:define-type-1
?\e ; ESC
(if (teco:peek-command ?\e)
;; ESC ESC terminates macro or command
(teco:pop-macro-stack)
;; otherwise, consume arguments
(setq teco:exp-flag1 nil
teco:exp-flag2 nil
teco:exp-op 'start)))
(teco:define-type-1
?! ; !
(while (/= (teco:get-command teco:trace) ?!)
nil)
(setq teco:at-flag nil))
(teco:define-type-1
?@ ; @
;; set at-flag
(setq teco:at-flag t))
(teco:define-type-1
?: ; :
;; is it '::'?
(if (teco:peek-command ?:)
(progn
;; skip second colon
(teco:get-command teco:trace)
;; set flag to show two colons
(setq teco:colon-flag 2))
;; set flag to show one colon
(setq teco:colon-flag 1)))
(teco:define-type-1
?? ; ?
;; toggle trace
(setq teco:trace (not teco:trace)))
(teco:define-type-1
?. ; .
;; value is point
(setq teco:exp-val1 (point)
teco:exp-flag1 t))
(teco:define-type-1
?z ; z
;; value is point-max
(setq teco:exp-val1 (point-max)
teco:exp-flag1 t))
(teco:define-type-1
?b ; b
;; value is point-min
(setq teco:exp-val1 (point-min)
teco:exp-flag1 t))
(teco:define-type-1
?h ; h
;; value is b,z
(setq teco:exp-val1 (point-max)
teco:exp-val2 (point-min)
teco:exp-flag1 t
teco:exp-flag2 t
teco:exp-op 'start))
(teco:define-type-1
?\^s ; ^s
;; value is - length of last insert, etc.
(setq teco:exp-val1 teco:ctrl-s
teco:exp-flag1 t))
(teco:define-type-1
?\^y ; ^y
;; value is .+^S,.
(setq teco:exp-val1 (point)
teco:exp-val2 (+ (point) teco:ctrl-s)
teco:exp-flag1 t
teco:exp-flag2 t
teco:exp-op 'start))
(teco:define-type-1
?\( ; \(
;; push expression stack
(teco:push-exp-stack)
(setq teco:exp-flag1 nil
teco:exp-flag2 nil
teco:exp-op 'start))
(teco:define-type-1
?\C-^ ; ^^
;; get next command character
(setq teco:exp-val1 (teco:get-command teco:trace)
teco:exp-flag1 t))
;; Type 2 commands
(teco:define-type-2
?+ ; +
(setq teco:exp-exp (if teco:exp-flag1 teco:exp-val1 0)
teco:exp-flag1 nil
teco:exp-op 'add))
(teco:define-type-2
?- ; -
(setq teco:exp-exp (if teco:exp-flag1 teco:exp-val1 0)
teco:exp-flag1 nil
teco:exp-op 'sub))
(teco:define-type-2
?* ; *
(setq teco:exp-exp (if teco:exp-flag1 teco:exp-val1 0)
teco:exp-flag1 nil
teco:exp-op 'mult))
(teco:define-type-2
?/ ; /
(setq teco:exp-exp (if teco:exp-flag1 teco:exp-val1 0)
teco:exp-flag1 nil
teco:exp-op 'div))
(teco:define-type-2
?& ; &
(setq teco:exp-exp (if teco:exp-flag1 teco:exp-val1 0)
teco:exp-flag1 nil
teco:exp-op 'and))
(teco:define-type-2
?# ; #
(setq teco:exp-exp (if teco:exp-flag1 teco:exp-val1 0)
teco:exp-flag1 nil
teco:exp-op 'or))
(teco:define-type-2
?\) ; \)
(if (or (not teco:exp-flag1) (not teco:exp-stack))
(teco:error "NAP"))
(let ((v teco:exp-val1))
(teco:pop-exp-stack)
(setq teco:exp-val1 v
teco:exp-flag1 t)))
(teco:define-type-2
?, ; ,
(if (not teco:exp-flag1)
(teco:error "NAC"))
(setq teco:exp-val2 teco:exp-val1
teco:exp-flag2 t
teco:exp-flag1 nil))
(teco:define-type-2
?\^_ ; ^_
(if (not teco:exp-flag1)
(teco:error "NAB")
(setq teco:exp-val1 (lognot teco:exp-val1))))
(teco:define-type-2
?\^d ; ^d
(setq teco:ctrl-r 10
teco:exp-flag1 nil
teco:exp-op 'start))
(teco:define-type-2
?\^o ; ^o
(setq teco:ctrl-r 8
teco:exp-flag1 nil
teco:exp-op 'start))
(teco:define-type-2
?\^r ; ^r
(if teco:colon-flag
(progn
(recursive-edit)
(setq teco:colon-flag nil))
(if teco:exp-flag1
;; set radix
(progn
(if (and (/= teco:exp-val1 8)
(/= teco:exp-val1 10)
(/= teco:exp-val1 16))
(teco:error "IRA"))
(setq teco:ctrl-r teco:exp-val1
teco:exp-flag1 nil
teco:exp-op 'start))
;; get radix
(setq teco:exp-val1 teco:ctrl-r
teco:exp-flag1 t))))
(teco:define-type-2
?\^c ; ^c
(if (teco:peek-command ?\^c)
;; ^C^C stops execution
(throw 'teco:exit nil)
(if teco:macro-stack
;; ^C inside macro exits macro
(teco:pop-macro-stack)
;; ^C in command stops execution
(throw 'teco:exit nil))))
(teco:define-type-2
?\^x ; ^x
;; set/get search mode flag, which is case-fold-search
(let ((x-flag (if case-fold-search 0 -1)))
(teco:set-var 'x-flag)
(setq case-fold-search (= x-flag 0))))
(teco:define-type-2
?m ; m
(let ((macro-name (teco:get-qspec nil
(teco:get-command teco:trace))))
(teco:push-macro-stack)
(setq teco:command-string (aref teco:qreg-text macro-name)
teco:command-pointer 0)))
(teco:define-type-2
?< ; <
;; begin iteration
(if (and teco:exp-flag1 (<= teco:exp-val1 0))
;; if this is not to be executed, just skip the
;; intervening stuff
(teco:find-enditer)
;; push iteration stack
(teco:push-iter-stack teco:command-pointer
teco:exp-flag1 teco:exp-val1)
;; consume the argument
(setq teco:exp-flag1 nil
teco:at-flag nil)))
(teco:define-type-2
?> ; >
;; end iteration
(if (not teco:iteration-stack)
(teco:error "BNI"))
;; decrement count and pop conditionally
(teco:pop-iter-stack nil)
;; consume arguments
(setq teco:exp-flag1 nil
teco:exp-flag2 nil
teco:exp-op 'start
teco:at-flag nil))
(teco:define-type-2
59 ; ;
;; semicolon iteration exit
(if (not teco:iteration-stack)
(teco:error "SNI"))
;; if exit
(if (if (>= (if teco:exp-flag1
teco:exp-val1
teco:search-result) 0)
(not teco:colon-flag)
teco:colon-flag)
(progn
(teco:find-enditer)
(teco:pop-iter-stack t)))
;; consume argument and colon
(setq teco:exp-flag1 nil
teco:colon-flag nil
teco:exp-op 'start))
(teco:define-type-2
?\" ; \"
;; must be an argument
(if (not teco:exp-flag1)
(teco:error "NAQ"))
;; consume argument
(setq teco:exp-flag1 nil
teco:exp-op 'start)
(let* (;; get the test specification
(c (aref teco:mapch-l (teco:get-command teco:trace)))
;; determine whether the test is true
(test (cond ((eq c ?a)
(/= (logand (aref teco:char-types teco:exp-val1)
1) 0))
((eq c ?c)
(/= (logand (aref teco:char-types teco:exp-val1)
2) 0))
((eq c ?d)
(/= (logand (aref teco:char-types teco:exp-val1)
4) 0))
((or (eq c ?e) (eq c ?f) (eq c ?u) (eq c ?=))
(= teco:exp-val1 0))
((or (eq c ?g) (eq c ?>))
(> teco:exp-val1 0))
((or (eq c ?l) (eq c ?s) (eq c ?t) (eq c ?<))
(< teco:exp-val1 0))
((eq c ?n)
(/= teco:exp-val1 0))
((eq c ?r)
(/= (logand (aref teco:char-types teco:exp-val1)
8) 0))
((eq c ?v)
(/= (logand (aref teco:char-types teco:exp-val1)
16) 0))
((eq c ?w)
(/= (logand (aref teco:char-types teco:exp-val1)
32) 0))
(t
(teco:error "IQC")))))
(if (not test)
;; if the conditional isn't satisfied, read
;; to matching | or '
;; ll counts the number of conditionals we are inside of
(let ((ll 1)
c)
(while (> ll 0)
;; skip to the next significant character
(while (progn (setq c (teco:skipto))
(and (/= c ?\")
(/= c ?|)
(/= c ?\')))
nil)
(if (= c ?\")
(setq ll (1+ ll))
(if (= c ?\')
(setq ll (1- ll))
(if (= ll 1)
(setq ll 0) ; for immediate exit if | at ll=1
))))))
;; clear at-flag
(setq teco:at-flag nil)))
(teco:define-type-2
?' ; '
;; no effect if executing
;; clear at-flag
(setq teco:at-flag nil))
(teco:define-type-2
?| ; |
(let ((ll 1)
c)
(while (> ll 0)
(while (progn (setq c (teco:skipto))
(and (/= c ?\")
(/= c ?\')))
nil)
(if (= c ?\")
(setq ll (1+ ll))
(setq ll (1- ll))))))
(teco:define-type-2
?u ; u
(if (not teco:exp-flag1)
(teco:error "NAU"))
(aset teco:qreg-number
(teco:get-qspec 0 (teco:get-command teco:trace))
teco:exp-val1)
(setq teco:exp-flag1 teco:exp-flag2 ; command's value is second arg
teco:exp-val1 teco:exp-val2
teco:exp-flag2 nil
teco:exp-op 'start))
(teco:define-type-2
?q ; q
;; Qn is numeric val, :Qn is # of chars, mQn is mth char
(let ((mm (teco:get-qspec (or teco:colon-flag teco:exp-flag1)
(teco:get-command teco:trace))))
(if (not teco:exp-flag1)
(setq teco:exp-val1 (if teco:colon-flag
;; :Qn
(length (aref teco:qreg-text mm))
;; Qn
(aref teco:qreg-number mm))
teco:exp-flag1 t)
;; mQn
(let ((v (aref teco:qreg-text mm)))
(setq teco:exp-val1 (condition-case nil
(aref v teco:exp-val1)
(error -1))
teco:exp-op 'start)))
(setq teco:colon-flag nil)))
(teco:define-type-2
?% ; %
(let* ((mm (teco:get-qspec nil (teco:get-command teco:trace)))
(v (+ (aref teco:qreg-number mm) (teco:get-value 1))))
(aset teco:qreg-number mm v)
(setq teco:exp-val1 v
teco:exp-flag1 t)))
(teco:define-type-2
?c ; c
(let ((p (+ (point) (teco:get-value 1))))
(if (or (< p (point-min)) (> p (point-max)))
(teco:error "POP")
(goto-char p)
(setq teco:exp-flag2 nil))))
(teco:define-type-2
?r ; r
(let ((p (- (point) (teco:get-value 1))))
(if (or (< p (point-min)) (> p (point-max)))
(teco:error "POP")
(goto-char p)
(setq teco:exp-flag2 nil))))
(teco:define-type-2
?j ; j
(let ((p (teco:get-value (point-min))))
(if (or (< p (point-min)) (> p (point-max)))
(teco:error "POP")
(goto-char p)
(setq teco:exp-flag1 nil
teco:exp-flag2 nil))))
(teco:define-type-2
?l ; l
;; move forward by lines
(let* ((ll (teco:line-args))
(p (+ (car ll) (cdr ll) (- (point)))))
(if (or (< p (point-min)) (> p (point-max)))
(teco:error "POP")
(goto-char p))))
(teco:define-type-2
?\C-q ; ^q
;; number of characters until the nth line feed
(setq teco:exp-val1 (teco:lines (teco:get-value 1))
teco:exp-flag1 t))
(teco:define-type-2
?= ; =
;; print numeric value
(if (not teco:exp-flag1)
(teco:error "NAE"))
(teco:output (format
(if (teco:peek-command ?=)
;; at least one more =
(progn
;; read past it
(teco:get-command teco:trace)
(if (teco:peek-command ?=)
;; another?
(progn
;; read it too
(teco:get-command teco:trace)
;; print in hex
"%x")
;; print in octal
"%o"))
;; print in decimal
"%d")
teco:exp-val1))
;; add newline if no colon
(if (not teco:colon-flag)
(teco:output ?\n))
;; absorb argument, etc.
(setq teco:exp-flag1 nil
teco:exp-flag2 nil
teco:colon-flag nil
teco:exp-op 'start))
(teco:define-type-2
?\t ; TAB
(if teco:exp-flag1
(teco:error "IIA"))
(let ((text (teco:get-text-arg)))
(insert ?\t text)
(setq teco:ctrl-s (- (1+ (length text)))))
;; clear arguments
(setq teco:colon-flag nil
teco:exp-flag1 nil
teco:exp-flag2 nil))
(teco:define-type-2
?i ; i