forked from emacs-ess/ESS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathess-sas-a.el
1482 lines (1265 loc) · 65.1 KB
/
ess-sas-a.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
;;; ess-sas-a.el --- clean-room implementation of many SAS-mode features
;; Copyright (C) 1997--2009 A.J. Rossini, Richard M. Heiberger, Martin
;; Maechler, Kurt Hornik, Rodney Sparapani, and Stephen Eglen.
;; Author: Rodney A. Sparapani
;; Maintainer: [email protected]
;; Created: 17 November 1999
;; Keywords: languages
;; This file is part of ESS
;; This file 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.
;;
;; This file 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.
;;
;; A copy of the GNU General Public License is available at
;; http://www.r-project.org/Licenses/
;;; Code:
;;; Table of Contents
;;; Section 1: Variable Definitions
;;; Section 2: Function Definitions
;;; Section 3: Key Definitions
;;; Section 1: Variable Definitions
(defvar ess-sas-file-path "."
"Full path-name of the sas file to perform operations on.")
(defcustom ess-sas-data-view-libname " "
"*SAS code to define a library for `ess-sas-data-view-fsview'
or `ess-sas-data-view-insight'."
:group 'ess-sas
:type 'string)
(defcustom ess-sas-data-view-submit-options
(if ess-microsoft-p "-noenhancededitor -nosysin -log NUL:"
"-nodms -nosysin -log /dev/null -terminal")
"*The command-line options necessary for your OS with respect to
`ess-sas-data-view-fsview' and `ess-sas-data-view-insight'."
:group 'ess-sas
:type 'string)
(defcustom ess-sas-data-view-fsview-command "; proc fsview data="
"*SAS code to open a SAS dataset with `ess-sas-data-view-fsview'."
:group 'ess-sas
:type 'string)
(defcustom ess-sas-data-view-fsview-statement " "
"*SAS code to perform a PROC FSVIEW statement with `ess-sas-data-view-fsview'."
:group 'ess-sas
:type 'string)
(make-variable-buffer-local 'ess-sas-data-view-fsview-statement)
(defcustom ess-sas-data-view-insight-command "; proc insight data="
"*SAS code to open a SAS dataset with `ess-sas-data-view-insight'."
:group 'ess-sas
:type 'string)
(defcustom ess-sas-data-view-insight-statement " "
"*SAS code to perform a PROC FSVIEW statement with `ess-sas-data-view-insight'."
:group 'ess-sas
:type 'string)
(make-variable-buffer-local 'ess-sas-data-view-insight-statement)
(defcustom ess-sas-graph-view-suffix-regexp
"[.]\\([eE]?[pP][sS]\\|[pP][dD][fF]\\|[gG][iI][fF]\\|[jJ][pP][eE]?[gG]\\|[tT][iI][fF][fF]?\\)"
"*GSASFILE suffix regexp."
:group 'ess-sas
:type 'string)
(defcustom ess-sas-graph-view-viewer-alist
;;creates something like
;;'(("[pP][dD][fF]" . "/usr/local/bin/acroread") ("[eE]?[pP][sS]" . "/usr/local/bin/gv")))
(let ((ess-tmp-alist nil)
(ess-tmp-ps nil) (ess-tmp-pdf nil))
(setq ess-tmp-ps (executable-find (if ess-microsoft-p "gsview32" "gsview")))
(if (not ess-tmp-ps) (setq ess-tmp-ps (executable-find "gv")))
(if (not ess-tmp-ps) (setq ess-tmp-ps (executable-find "ghostview")))
(setq ess-tmp-pdf (executable-find "evince"))
(if (not ess-tmp-pdf) (setq ess-tmp-pdf (executable-find "xpdf")))
(if (not ess-tmp-pdf) (setq ess-tmp-pdf (if ess-microsoft-p "acrord32" "acroread")))
(if (and ess-tmp-ps ess-tmp-pdf)
(setq ess-tmp-alist (list (cons "[eE]?[pP][sS]" ess-tmp-ps)
(cons "[pP][dD][fF]" ess-tmp-pdf)))
(if ess-tmp-ps
(setq ess-tmp-alist (list (cons "[eE]?[pP][sS]" ess-tmp-ps)
(cons "[pP][dD][fF]" ess-tmp-ps))))))
"*Associate file name extensions with graphics image file viewers."
:group 'ess-sas
:type 'string)
;;(defcustom ess-sas-smart-back-tab nil
;; "*Set to t to make C-TAB insert an end/%end; statement to close a block."
;; :group 'ess-sas
;;)
(defcustom ess-sas-log-max 0
"*If >0 and .log file exceeds this many bytes, just \"refresh\" this many bytes."
:group 'ess-sas
:type 'integer)
(defcustom ess-sas-rtf-font-name "Bitstream Vera Sans Mono"
; (if (featurep 'xemacs) "Lucida Sans Typewriter" "Bitstream Vera Sans Mono")
"*Name of font to create MS RTF with"
:group 'ess-sas
:type 'string)
(defcustom ess-sas-shell-buffer "*shell*"
"*Name that you want to use for the shell buffer; buffer-local."
:group 'ess-sas
:type 'string)
(make-variable-buffer-local 'ess-sas-shell-buffer)
(defcustom ess-sas-shell-buffer-remote-host nil
"*Remote host that you want to open a shell on."
:group 'ess-sas
:type '(choice (const nil) string))
(make-variable-buffer-local 'ess-sas-shell-buffer-remote-host)
(defcustom ess-sas-shell-buffer-remote-init "ssh"
"*Command to open a shell on a remote host."
:group 'ess-sas
:type 'string)
(make-variable-buffer-local 'ess-sas-shell-buffer-remote-init)
(defcustom ess-sas-submit-mac-virtual-pc nil
"*Non-nil means that you want to run Windows SAS in a
Virtual PC emulator on your Mac; buffer-local."
:group 'ess-sas
:type 'boolean)
(make-variable-buffer-local 'ess-sas-submit-mac-virtual-pc)
(defcustom ess-sas-submit-command sas-program
"*Command to invoke SAS in batch; buffer-local."
:group 'ess-sas
:type 'string)
(make-variable-buffer-local 'ess-sas-submit-command)
(defcustom ess-sas-submit-command-options "-rsasuser"
"*Options to pass to SAS in batch; buffer-local."
:group 'ess-sas
:type 'string)
(make-variable-buffer-local 'ess-sas-submit-command-options)
(defvar ess-sas-submit-method
(if ess-microsoft-p
(if (w32-shell-dos-semantics) 'ms-dos 'sh)
(if (or (equal system-type 'Apple-Macintosh)
(and ess-sas-submit-mac-virtual-pc (equal system-type 'darwin)))
'apple-script 'sh))
"Method used by `ess-sas-submit'.
The default is based on the value of the emacs variable `system-type'
and, on Windows, the function `w32-shell-dos-semantics'.
'sh if *shell* runs sh, ksh, csh, tcsh or bash
'ms-dos if *shell* follows MS-DOS semantics
'apple-script *shell* unavailable in Mac Classic, use AppleScript,
also for Windows SAS in Virtual PC on Mac OS X
Unix users will get 'sh by default.
Windows users running bash in *shell* will get 'sh by default.
Windows users running MS-DOS in *shell* will get 'ms-dos by default.
Users accessing a remote machine with `telnet', `rlogin', `ssh', etc.,
should set this variable to 'sh regardless of their local shell
(since their remote shell is 'sh).")
(make-variable-buffer-local 'ess-sas-submit-method)
(defcustom ess-sas-graph-view-viewer-default
(if ess-microsoft-p "explorer"
(if (equal ess-sas-submit-method 'sh) "sdtimage"))
"*Default graphics image file viewer."
:group 'ess-sas
:type 'string)
(defcustom ess-sas-submit-post-command
(if (equal ess-sas-submit-method 'sh) "&"
(if ess-microsoft-p "-icon"))
"*Command-line statement to post-modify SAS invocation"
:group 'ess-sas
:type 'string)
(defcustom ess-sas-submit-pre-command ;;"nohup"
(if (equal ess-sas-submit-method 'sh)
;; nice is tricky, higher numbers give you lower priorities
;; if you are using csh/tcsh, the default priority is 4
;; if you are using most other shells, the default priority is 10,
;; and some implementations are higher, i.e. zsh unless you
;; specify "setopt no_bg_nice" in your ~/.zshrc
;; therefore, on the same machine, you can run at a higher or
;; lower priority by changing shells, although, the command
;; line is the same!
;; the following code should give you a priority of 10 regardless
;; of which shell is in use, but it will default to the old
;; behavior if csh or variant is not recognized
;; this should avoid the necessity of each user needing to set this
;; variable correctly based on the shell that they use and provide
;; an environment where all shells are treated equally
(let* ((temp-shell (getenv "SHELL"))
;; AJR: old CYGWIN versions return nil for (getenv
;; "SHELL"), so we need to deal with it 'cause I have to
(temp-char (if temp-shell
(string-match "/" temp-shell)
nil)))
(while temp-char
(setq temp-shell (substring temp-shell (+ 1 temp-char)))
(setq temp-char (string-match "/" temp-shell)))
(cond ((or (equal temp-shell "csh") (equal temp-shell "tcsh"))
"nohup nice +6")
(t "nohup nice")))
(if ess-microsoft-p "start"))
"*Command-line statement to precede SAS invocation, e.g. start or nohup"
:group 'ess-sas
:type 'string)
(defcustom ess-sas-suffix-1 "txt"
"*The first suffix to associate with SAS."
:group 'ess-sas
:type 'string)
(defcustom ess-sas-suffix-2 "csv"
"*The second suffix to associate with SAS."
:group 'ess-sas
:type 'string)
(defcustom ess-sas-suffix-regexp
(concat "[.]\\([sS][aA][sS]\\|[lL][oO][gG]\\|[lL][sS][tT]"
(if ess-sas-suffix-1 (concat
"\\|" (downcase ess-sas-suffix-1) "\\|" (upcase ess-sas-suffix-1)))
(if ess-sas-suffix-2 (concat
"\\|" (downcase ess-sas-suffix-2) "\\|" (upcase ess-sas-suffix-2)))
"\\)")
"*Regular expression for SAS suffixes."
:group 'ess-sas
:type 'string)
(defcustom ess-sas-tab-stop-list
'(4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100 104 108 112 116 120)
"List of tab stop positions used by `tab-to-tab-stop' in ESS[SAS]."
:group 'ess-sas)
(defcustom ess-sas-temp-root "-temp"
"*Appended to root name of the temporary .sas file for `ess-sas-submit-region'."
:group 'ess-sas
:type 'string)
(defvar ess-sas-versions '("sas")
"List of partial strings for versions of SAS to access within ESS.
Each string specifies the start of a filename. If a filename
beginning with one of these strings is found on `exec-path', a M-x
command for that version of SAS is made available. For example, if the
file \"sas8\" is found and this variable includes the string
\"sas\", a function called `M-x SAS8' will be available to run that
version of SAS.
If duplicate versions of the same program are found (which happens if
the same path is listed on `exec-path' more than once), they are
ignored by calling `ess-uniq-list'.
If you set this variable, you need to restart Emacs (and set this variable
before ess-site is loaded) for it to take effect.")
;;; Section 2: Function Definitions
(defun ess-ebcdic-to-ascii-search-and-replace ()
"*Search and replace EBCDIC text with ASCII equivalents."
(interactive)
(let ((ess-tmp-dd (executable-find "dd")) (ess-tmp-recode (executable-find "recode"))
(ess-tmp-util nil) (ess-tmp-util-args nil))
(if ess-tmp-dd (progn
(setq ess-tmp-util ess-tmp-dd)
(setq ess-tmp-util-args "conv=ascii"))
(setq ess-tmp-util ess-tmp-recode)
(setq ess-tmp-util-args "EBCDIC..ISO-8859-1"))
(if ess-tmp-util
(while (search-forward-regexp "[^\f\t\n -~][^\f\t\n -?A-JQ-Yb-jp-y]*[^\f\t\n -~]?" nil t)
(call-process-region (match-beginning 0) (match-end 0)
ess-tmp-util t (list t nil) t ess-tmp-util-args)))))
(defun ess-exit-notify-sh (string)
"Detect completion or failure of submitted job and notify the user."
;(let* ((exit-done "\\[[0-9]+\\]\\ *\\+*\\ *\\(Exit\\|Done\\).*$")
(let* ((exit-done "\\[[0-9]+\\]\\ *\\+*\\ *\\(Exit\\|Done\\)[^\r\n]*") ; GNU Emacs needs this
(beg (string-match exit-done string)))
(if beg
(message (substring string beg (match-end 0))))))
(defun ess-sas-append-log ()
"Append ess-temp.log to the current .log file."
(interactive)
(ess-sas-goto "log" 'revert)
(goto-char (point-max))
(insert-file-contents (concat (ess-sas-temp-root) ".log"))
(save-buffer))
(defun ess-sas-append-lst ()
"Append ess-temp.lst to the current .lst file."
(interactive)
(ess-sas-goto "lst" 'revert)
(goto-char (point-max))
(insert-file-contents (concat (ess-sas-temp-root) ".lst"))
(save-buffer))
(defun ess-sas-backward-delete-tab ()
"Moves the cursor to the previous tab-stop, deleting any characters
on the way."
(interactive)
(let* (;; point of search
;;(ess-sas-search-point nil)
;; column of search
;;(ess-sas-search-column nil)
;; limit of search
;;(ess-sas-search-limit nil)
;; text to be inserted after a back-tab, if any
;;(ess-sas-end-text "end;")
;; current-column
(ess-sas-column (current-column))
;; remainder of current-column and sas-indent-width
(ess-sas-remainder (% ess-sas-column sas-indent-width)))
(if (not (= ess-sas-column 0))
(progn
(if (= ess-sas-remainder 0)
(setq ess-sas-remainder sas-indent-width))
(let ((backward-delete-char-untabify-method 'nil))
(backward-delete-char-untabify ess-sas-remainder t)
(setq ess-sas-column (- ess-sas-column ess-sas-remainder))
(move-to-column ess-sas-column)
(setq left-margin ess-sas-column))
))
))
;; this feature was far too complicated to perfect
;; (if ess-sas-smart-back-tab (progn
;; (save-excursion
;; (setq ess-sas-search-point
;; (search-backward-regexp "end" nil t))
;; (if (and ess-sas-search-point
;; (search-backward-regexp "%" (+ ess-sas-search-point -1) t))
;; (setq ess-sas-search-point (+ ess-sas-search-point -1))
;; )
;; (if (and ess-sas-search-point
;; (not (equal ess-sas-column (current-column))))
;; (setq ess-sas-search-point nil))
;; )
;; (save-excursion
;; (setq ess-sas-search-point
;; (search-backward-regexp "do\\|select"
;; ess-sas-search-point t))
;; (setq ess-sas-search-column (current-column))
;; (if ess-sas-search-point (progn
;; (save-excursion
;; (search-backward-regexp "^" nil t)
;; (setq ess-sas-search-limit (point))
;; )
;; (if (search-backward-regexp "if.*then\\|else" ess-sas-search-limit t)
;; (setq ess-sas-search-point (point)))
;; (if (search-backward-regexp "%" ess-sas-search-limit t) (progn
;; (setq ess-sas-end-text "%end;")
;; (setq ess-sas-search-point (point))
;; ))
;; (setq ess-sas-search-column (current-column))
;; (if (not (equal ess-sas-column ess-sas-search-column))
;; (setq ess-sas-search-point nil))
;; )))
;; (if ess-sas-search-point (insert ess-sas-end-text))
;; ))
(defun ess-sas-cd ()
"Change directory, taking into account various issues with respect to
`ess-sas-file-path'."
;(interactive)
(ess-sas-file-path)
(ess-sas-goto-shell t)
(comint-send-input)
(if (equal ess-sas-submit-method 'sh)
(insert "cd " (car (last (split-string (file-name-directory ess-sas-file-path)
"\\([a-zA-Z][a-zA-Z]:\\|]\\)"))))
(if (equal ess-sas-submit-method 'ms-dos) (progn
(if (string-equal ":" (substring ess-sas-file-path 1 2)) (progn
(insert (substring ess-sas-file-path 0 2))
(comint-send-input)))
(insert "cd \"" (convert-standard-filename
(file-name-directory ess-sas-file-path)) "\""))))
(comint-send-input))
(defun ess-sas-create-local-variables-alist (&optional file-or-buffer)
"Create an alist of local variables from file-or-buffer, use the
current buffer if nil."
(if file-or-buffer (set-buffer (ess-get-file-or-buffer file-or-buffer)))
(ess-change-alist 'ess-kermit-remote-directory ess-kermit-remote-directory nil))
(defun ess-sas-data-view-fsview (&optional ess-sas-data)
"Open a dataset for viewing with PROC FSVIEW."
(interactive)
(ess-save-and-set-local-variables)
(save-excursion (let ((ess-tmp-sas-data nil)
(ess-tmp-sas-data-view-fsview-statement ess-sas-data-view-fsview-statement)
(ess-search-regexp
"[ \t=]\\([a-zA-Z_][a-zA-Z_0-9]*[.][a-zA-Z_][a-zA-Z_0-9]*\\)\\(&.*\\)?[. ,()\t;/]")
(ess-search-except
"^\\([wW][oO][rR][kK]\\|[fF][iI][rR][sS][tT]\\|[lL][aA][sS][tT]\\)[.]"))
(if ess-sas-data nil (save-match-data
(search-backward-regexp "[ \t=]" nil t)
(save-excursion
(setq ess-tmp-sas-data
(ess-search-except ess-search-regexp ess-search-except)))
(if (not ess-tmp-sas-data)
(setq ess-tmp-sas-data
(ess-search-except ess-search-regexp ess-search-except t)))
(setq ess-sas-data (read-string "Permanent SAS Dataset: " ess-tmp-sas-data))
(ess-sas-goto-shell t)
(ess-sas-cd)
(insert (concat ess-sas-submit-pre-command " " ess-sas-submit-command
" -initstmt \"" ess-sas-data-view-libname ess-sas-data-view-fsview-command
ess-sas-data ";" ess-tmp-sas-data-view-fsview-statement "; run;\" "
ess-sas-submit-command-options " "
ess-sas-data-view-submit-options " " ess-sas-submit-post-command))
(comint-send-input)
)))))
(defun ess-sas-data-view-insight (&optional ess-sas-data)
"Open a dataset for viewing with PROC INSIGHT."
(interactive)
(ess-save-and-set-local-variables)
(save-excursion (let ((ess-tmp-sas-data nil)
(ess-tmp-sas-data-view-insight-statement ess-sas-data-view-insight-statement)
(ess-search-regexp
"[ \t=]\\([a-zA-Z_][a-zA-Z_0-9]*[.][a-zA-Z_][a-zA-Z_0-9]*\\)\\(&.*\\)?[. ,()\t;]")
(ess-search-except
"^\\([wW][oO][rR][kK]\\|[fF][iI][rR][sS][tT]\\|[lL][aA][sS][tT]\\)[.]"))
(if ess-sas-data nil (save-match-data
(search-backward-regexp "[ \t=]" nil t)
(save-excursion
(setq ess-tmp-sas-data
(ess-search-except ess-search-regexp ess-search-except)))
(if (not ess-tmp-sas-data)
(setq ess-tmp-sas-data
(ess-search-except ess-search-regexp ess-search-except t)))
(setq ess-sas-data (read-string "Permanent SAS Dataset: " ess-tmp-sas-data))
(ess-sas-goto-shell t)
(ess-sas-cd)
(insert (concat ess-sas-submit-pre-command " " ess-sas-submit-command
" -initstmt \"" ess-sas-data-view-libname ess-sas-data-view-insight-command
ess-sas-data ";" ess-tmp-sas-data-view-insight-statement "; run;\" "
ess-sas-data-view-submit-options " " ess-sas-submit-post-command))
(comint-send-input)
)))))
(defun ess-sas-graph-view ()
"Open a GSASFILE for viewing."
(interactive)
; (ess-sas-file-path)
(ess-sas-goto-log 'no-error-check)
(save-excursion (let (
(ess-tmp-length (length ess-sas-graph-view-viewer-alist))
(ess-tmp-counter 0)
(ess-tmp-graph nil)
(ess-tmp-graph-alist nil)
(ess-tmp-glyph nil)
(ess-tmp-graph-regexp
; (concat "[ ]RECORDS[ ]WRITTEN[ ]+TO[ ]\n?[ ]*\\(\\(\n\\|[^.]\\)*"
(concat "[ ][rR][eE][cC][oO][rR][dD][sS][ ][wW][rR][iI][tT][tT][eE][nN][ ]+[tT][oO][ ]\n?[ ]*\\(.*"
ess-sas-graph-view-suffix-regexp "\\)")))
; (concat "['\"]\\(.*" ess-sas-graph-suffix-regexp "\\)['\"]")))
(save-match-data
(search-backward-regexp "[ \t=]" nil t)
(save-excursion
(setq ess-tmp-graph (ess-search-except ess-tmp-graph-regexp)))
(if (not ess-tmp-graph)
(setq ess-tmp-graph (ess-search-except ess-tmp-graph-regexp nil t)))
(setq ess-tmp-graph (read-string "GSASFILE: "
(or ess-tmp-graph ess-sas-file-path)))
(if (fboundp 'ess-xemacs-insert-glyph) (progn
(if (string-match "[.][gG][iI][fF]" ess-tmp-graph)
(setq ess-tmp-glyph 'gif)
;;else
(if (string-match "[.][jJ][pP][eE]?[gG]" ess-tmp-graph)
(setq ess-tmp-glyph 'jpeg)))))
;;GNU Emacs graphics file image viewing mode loaded?
(if (and (boundp 'auto-image-file-mode) auto-image-file-mode
(string-match "[.][jJ][pP][eE]?[gG]" ess-tmp-graph))
(find-file ess-tmp-graph)
;;else XEmacs graphics file image viewing mode loaded?
(if (and (fboundp 'image-mode)
(string-match "[.]\\([jJ][pP][eE]?[gG]\\|[gG][iI][fF]\\)"
ess-tmp-graph))
(find-file ess-tmp-graph)
;;else XEmacs graphics file image viewing primitives loaded?
(if ess-tmp-glyph (progn
(switch-to-buffer (file-name-nondirectory ess-tmp-graph))
(ess-xemacs-insert-glyph
(make-glyph (vector ess-tmp-glyph :file ess-tmp-graph))))
;;else use the appropriate graphics file image viewer
(while (< ess-tmp-counter ess-tmp-length)
(setq ess-tmp-graph-alist
(nth ess-tmp-counter ess-sas-graph-view-viewer-alist))
(setq ess-tmp-graph-regexp (car ess-tmp-graph-alist))
(if (string-match
(concat "[.]" ess-tmp-graph-regexp) ess-tmp-graph)
(progn
(ess-sas-goto-shell t)
(insert ess-sas-submit-pre-command " "
(cdr ess-tmp-graph-alist) " " ess-tmp-graph
(if (equal ess-sas-submit-method 'sh) " &"))
(setq ess-tmp-glyph 'alist)
(setq ess-tmp-counter ess-tmp-length))
;;else
(setq ess-tmp-counter (+ ess-tmp-counter 1))))
(if (not ess-tmp-glyph) (progn
(ess-sas-goto-shell t)
(insert ess-sas-submit-pre-command " "
ess-sas-graph-view-viewer-default " " ess-tmp-graph
(if (equal ess-sas-submit-method 'sh) " &"))))
(comint-send-input))))))))
(defun ess-sas-file-path (&optional force)
"Define `ess-sas-file-path' to be the current buffer depending on suffix."
(interactive)
(save-match-data (let ((ess-sas-temp-file (expand-file-name (buffer-name))))
(if (or force (string-match ess-sas-suffix-regexp ess-sas-temp-file)) ;;(progn
(setq ess-sas-file-path
(nth 0 (split-string ess-sas-temp-file "[<]")))))))
;; (setq ess-directory (file-name-directory ess-sas-file-path)))))))
(defun ess-sas-file-path-remote-host ()
"Return the remote host, if any, associated with `ess-sas-file-path'."
(interactive)
(let* ((temp-colon-pos (string-match ":" ess-sas-file-path))
(temp-list
(if (or (not temp-colon-pos) (> temp-colon-pos 2))
(if (equal ess-sas-file-path ".") nil
(split-string (file-name-directory ess-sas-file-path)
"\\(@\\|:\\|]\\)"))
(list ess-sas-file-path)))
(temp-list-length (length temp-list)))
(if (= temp-list-length 1) (setq temp-list nil)
(if (= temp-list-length 2) (setq temp-list (car temp-list))
(setq temp-list (nth 1 temp-list))))
(if temp-list (setq temp-list
(car (last (split-string temp-list "/")))))
temp-list))
(defun ess-sas-goto (suffix &optional revert no-create)
"Find a file associated with a SAS file by suffix and revert if necessary."
; (interactive)
; (let ((ess-temp-regexp (concat ess-sas-suffix-regexp "[.]?[1-9]?\\'")))
; can we identify common nonsense extensions like .log.1 or .sas.2?
(let ((ess-temp-regexp (concat ess-sas-suffix-regexp "\\(@.+\\)?\\'")))
(save-match-data
(if (or (string-match ess-temp-regexp (expand-file-name (buffer-name)))
(string-match ess-temp-regexp ess-sas-file-path))
(progn
(ess-sas-file-path)
(let* (
(ess-sas-temp-file (replace-match (concat "." suffix) t t
ess-sas-file-path))
(ess-sas-temp-buff (find-buffer-visiting ess-sas-temp-file))
(ess-temp-kermit-remote-directory ess-kermit-remote-directory))
(if ess-sas-temp-buff (switch-to-buffer ess-sas-temp-buff)
;; else
(if no-create (setq revert nil)
(if (file-exists-p ess-sas-temp-file)
(find-file ess-sas-temp-file))))
;; else
;; (let* ((ess-sas-buffer-list (buffer-list))
;; (ess-sas-buffer-list-index 0)
;; (ess-sas-buffer-list-file nil)
;; (ess-sas-buffer-list-length (length ess-sas-buffer-list)))
;; (while (< ess-sas-buffer-list-index ess-sas-buffer-list-length)
;; (setq ess-sas-buffer-list-file
;; (buffer-file-name (nth ess-sas-buffer-list-index ess-sas-buffer-list)))
;; (if (and ess-sas-buffer-list-file
;; (string-match (concat "." suffix) ess-sas-buffer-list-file))
;; (switch-to-buffer (nth ess-sas-buffer-list-index ess-sas-buffer-list))
;; (setq ess-sas-buffer-list-index ess-sas-buffer-list-length)
;; )
;; (setq ess-sas-buffer-list-index (+ 1 ess-sas-buffer-list-index))
;; )))
(if (and (not no-create)
(or (string-equal suffix "log")
(string-equal suffix "lst")))
(ess-kermit-get (file-name-nondirectory ess-sas-temp-file)
ess-temp-kermit-remote-directory))
(if revert
(if (and (> ess-sas-log-max 0) (string-equal suffix "log")
(> (ess-num-or-zero (nth 7 (file-attributes ess-sas-temp-file)))
ess-sas-log-max))
(progn
(insert-file-contents ess-sas-temp-file nil 0
ess-sas-log-max t)
t)
(ess-revert-wisely)) nil)))))))
;;(defun ess-sas-file (suffix &optional revert)
;; "Please use `ess-sas-goto' instead."
;; (let* ((tail (downcase (car (split-string
;; (car (last (split-string (buffer-name) "[.]"))) "[<]"))))
;;(if (fboundp 'file-name-extension) (file-name-extension (buffer-name))
;; (substring (buffer-name) -3)))
;; (tail-in-tail-list (member tail (list "sas" "log" "lst"
;; ess-sas-suffix-1 ess-sas-suffix-2)))
;; (root (if tail-in-tail-list (expand-file-name (buffer-name))
;; ess-sas-file-path))
;; (ess-sas-arg (concat (file-name-sans-extension root) "." suffix))
;; (ess-sas-buf (find-buffer-visiting ess-sas-arg)))
;; (if (equal tail suffix) (if revert (ess-revert-wisely))
;; (if (not ess-sas-buf) (find-file ess-sas-arg)
;; (switch-to-buffer ess-sas-buf)
;; (if revert (ess-revert-wisely))))))
(defun ess-sas-goto-file-1 ()
"Switch to ess-sas-file-1 and revert from disk."
(interactive)
(ess-sas-goto ess-sas-suffix-1 'revert))
(defun ess-sas-goto-file-2 ()
"Switch to ess-sas-file-2 and revert from disk."
(interactive)
(ess-sas-goto ess-sas-suffix-2 'revert))
(defun ess-sas-goto-log (&optional ess-tmp-no-error-check)
"Switch to the .log file, revert from disk and search for error messages."
(interactive)
(let ((ess-sas-error (concat
"^ERROR [0-9]+-[0-9]+:\\|^ERROR:\\|_ERROR_=1 _N_=\\|_ERROR_=1[ ]?$"
"\\|NOTE: MERGE statement has more than one data set with repeats"
"\\|NOTE: Variable .* is uninitialized."
"\\|NOTE: SAS went to a new line when INPUT statement reached past"
"\\|NOTE 485-185: Informat .* was not found"
"\\|NOTE: Estimated G matrix is not positive definite."
"\\|NOTE: Compressing data set .* increased size by"
"\\|NOTE: ERROR DETECTED IN ANNOTATE="
"\\|WARNING: Apparent symbolic reference .* not resolved."
"\\|WARNING: Length of character variable has already been set."
"\\|WARNING: Not all variables in the list "
"\\|WARNING: RUN statement ignored due to previous errors."
"\\|WARNING: Values exist outside the axis range"
"\\|Bus Error In Task\\|Segmentation Violation In Task"))
(ess-sas-save-point nil)); (ess-sas-pop-mark nil))
(if (ess-sas-goto "log" 'revert) (progn
(setq ess-sas-save-point (point))
(goto-char (point-min)))
(setq ess-sas-save-point (point)))
;(if (number-char-or-marker-p ess-sas-save-point) (progn
(if ess-tmp-no-error-check (goto-char ess-sas-save-point)
(if (or (search-forward-regexp ess-sas-error nil t)
(and (goto-char (point-min))
(search-forward-regexp ess-sas-error nil t)))
t
; this feature never worked quite right (and was XEmacs only to boot)
; after highlighting an error message, moving point would cause an unwanted
; highlighting between point and mark; why god, why?!?
;
; (if (and (boundp 'zmacs-regions) zmacs-regions)
; (progn
; (if ess-sas-pop-mark (pop-mark)
; (setq ess-sas-pop-mark t))
; (push-mark (match-beginning 0) t)
; (zmacs-activate-region)))
(goto-char ess-sas-save-point)))))
(defun ess-sas-goto-lst ()
"Switch to the .lst file and revert from disk."
(interactive)
(ess-sas-goto "lst" 'revert))
(defun ess-sas-goto-sas (&optional revert)
"Switch to the .sas file."
(interactive)
(ess-sas-goto "sas" revert))
(defun ess-sas-goto-shell (&optional set-buffer)
"Set `ess-sas-file-path' and goto `ess-sas-shell-buffer'. If
optional argument is non-nil, then set-buffer rather than switch."
(interactive)
(ess-sas-file-path)
; The following let* block is an attempt to deal with remote directories.
(let* ((temp-shell-buffer-remote-host
(or ess-sas-shell-buffer-remote-host (ess-sas-file-path-remote-host)))
(temp-shell-buffer-remote-init ess-sas-shell-buffer-remote-init)
(temp-shell-buffer
(if temp-shell-buffer-remote-host
(concat "*" temp-shell-buffer-remote-host "*")
ess-sas-shell-buffer))
)
(if (get-buffer temp-shell-buffer)
(if set-buffer (set-buffer temp-shell-buffer)
(switch-to-buffer temp-shell-buffer))
(shell)
(rename-buffer temp-shell-buffer)
(ess-sleep) ; GNU Emacs needs this
(if temp-shell-buffer-remote-host (progn
(insert (concat
temp-shell-buffer-remote-init " " temp-shell-buffer-remote-host))
(comint-send-input))
)
(if (eq ess-sas-submit-method 'sh)
(add-hook 'comint-output-filter-functions 'ess-exit-notify-sh)) ;; 19.28
;; nil t) works for newer emacsen
)
)
(goto-char (point-max))
; (insert "cd " ess-temp-directory)
; (comint-send-input))
)
(defun ess-sas-interactive ()
"And now for something completely different."
(interactive)
(ess-sas-file-path)
(setq ess-customize-alist SAS-customize-alist)
(let ((ess-temp-sas-file
(nth 0 (split-string
(car (last (split-string ess-sas-file-path "\\([a-zA-Z][a-zA-Z]:\\|]\\)"))) "[.]"))))
;; (message "%s" ess-temp-sas-file)
(setq ess-sas-shell-buffer "*iESS[SAS]*")
(ess-sas-goto-shell)
(ess-add-ess-process)
(ess-setq-vars-local ess-customize-alist)
(inferior-ess-mode)
(ess-eval-linewise (concat ess-sas-submit-command " " ess-sas-submit-command-options
" -altlog " ess-temp-sas-file ".log -altprint "
ess-temp-sas-file ".lst -stdio"))
;;ess-temp-sas-file ".lst -stdio < /dev/tty"))
(ess-sas-goto-sas)
(setq ess-sas-submit-method 'iESS)
(setq ess-eval-visibly-p nil)
))
;;(defun ess-sas-interactive ()
;; (interactive)
;; (ess-sas-file-path)
;; (setq ess-sas-submit-method 'iESS)
;;
;; (let ((ess-temp-stderr " ") (ess-temp-stdout " ") (ess-temp-stdin " "))
;; (setq ess-sas-shell-buffer "*LOG*")
;; (ess-sas-goto-shell)
;; (insert "tty")
;; (comint-send-input)
;; (sleep-for ess-sleep-for)
;; (save-excursion (setq ess-temp-stderr (ess-search-except "\\(/dev/[a-z0-9/]+\\)" nil t)))
;; (setq ess-sas-shell-buffer "*OUTPUT*")
;; (ess-sas-goto-shell)
;; (insert "tty")
;; (comint-send-input)
;; (sleep-for ess-sleep-for)
;; (save-excursion (setq ess-temp-stdout (ess-search-except "\\(/dev/[a-z0-9/]+\\)" nil t)))
;; (setq ess-sas-shell-buffer "*PROGRAM*")
;; (ess-sas-goto-shell)
;;;; (insert "tty")
;; (comint-send-input)
;; (sleep-for ess-sleep-for)
;; (insert "sh")
;; (comint-send-input)
;; (sleep-for ess-sleep-for)
;; (save-excursion (setq ess-temp-stdin (ess-search-except "\\(/dev/[a-z0-9/]+\\)" nil t)))
;; (insert (concat ess-sas-submit-command " " ess-sas-submit-command-options " -stdio <"
;; ess-temp-stdin " >1 " ess-temp-stdout " >2 " ess-temp-stderr))
;; (comint-send-input)
;; (ess-add-ess-process)
;; (ess-sas-goto-sas)
;;))
(defun ess-sas-kill-buffers ()
"Kill all buffers related to a .sas file."
(interactive)
(ess-sas-file-path)
(ess-sas-goto "log" nil t)
(kill-buffer nil)
(ess-sas-goto "lst" nil t)
(kill-buffer nil)
(ess-sas-goto ess-sas-suffix-1 nil t)
(kill-buffer nil)
(ess-sas-goto ess-sas-suffix-2 nil t)
(kill-buffer nil)
(ess-sas-goto "sas" nil t)
(kill-buffer nil)
)
(if (featurep 'xemacs) (condition-case nil
(progn
(require 'rtf-support)
(when (featurep 'rtf-support)
(defun ess-sas-rtf-portrait (&optional ess-tmp-font-size)
"Creates an MS RTF portrait file from the current buffer."
(interactive)
(ess-sas-file-path t)
(ess-revert-wisely)
(if (equal ess-tmp-font-size nil)
(setq ess-tmp-font-size "21"))
(let
((ess-temp-rtf-file (replace-in-string ess-sas-file-path "[.][^.]*$" ".rtf")))
;(expand-file-name (buffer-name)) "[.][^.]*$" ".rtf")))
(rtf-export ess-temp-rtf-file)
(ess-sas-goto "rtf" t)
(goto-char (point-min))
(replace-regexp "\\\\fmodern .*;" (concat "\\\\fmodern " ess-sas-rtf-font-name ";"))
(goto-line 2)
(if (string-match ess-sas-suffix-regexp ess-sas-file-path)
(insert "\\margl720\\margr720\\margt720\\margb720\n"))
(goto-char (point-min))
(while (replace-regexp "\\\\fs[0-9]+" (concat "\\\\fs" ess-tmp-font-size)) nil)
(save-buffer)
(kill-buffer (current-buffer))))
))
(error nil))
; else
(defun ess-sas-rtf-portrait (&optional ess-tmp-font-size)
"Creates an MS RTF portrait file from the current buffer."
(interactive)
; (ess-sas-file-path t)
(ess-revert-wisely)
(set-visited-file-name (concat (buffer-name) ".rtf"))
; (ess-sas-goto "rtf" t)
(if 'buffer-read-only (setq buffer-read-only nil))
(ess-rtf-replace-chars)
(goto-char (point-min))
(insert (concat
"{\\rtf1\\ansi{\\fonttbl\\f1\\fmodern " ess-sas-rtf-font-name ";}\n"
"\\margl720\\margr720\\margt720\\margb720\n"
"{\\colortbl;\\red0\\green0\\blue0;\\red0\\green0\\blue255;\\red0\\green255\\blue255;\\red0\\green255\\blue0;\\red255\\green0\\blue255;\\red255\\green0\\blue0;\\red255\\green255\\blue0;\\red255\\green255\\blue255;\\red0\\green0\\blue128;\\red0\\green128\\blue128;\\red0\\green128\\blue0;\\red128\\green0\\blue128;\\red128\\green0\\blue0;\\red128\\green128\\blue0;\\red128\\green128\\blue128;\\red192\\green192\\blue192;}\n"
"{\\stylesheet{\\s15\\plain\\f1\\fs16\\cf1\\cb8\\lang1024 Emacs Text;}{\\*\\cs16 \\additive\\f1\\fs16\\cf1\\cb8\\lang1024 Emacs Base Style;}}\n"
"{\\plain\\s15{\\cs16\\cs16\\f1\\fs16\\cf1\\cb8\\lang1024{\\cs16\\f1\\fs16\\cf1\\cb8\\lang1024\n"))
(goto-char (point-max))
(insert "}}}}\n")
(save-buffer)
(kill-buffer (current-buffer)))
)
(defun ess-rtf-replace-chars ()
"Convert a text file to an MS RTF file."
(interactive)
(goto-char (point-min))
(while (re-search-forward "\n" nil t) (replace-match "\\par\n" nil t))
(goto-char (point-min))
(while (re-search-forward "\f" nil t) (replace-match "\\page\n" nil t))
(goto-char (point-min))
(while (re-search-forward "\t" nil t) (replace-match "\\tab" nil t)))
(defun ess-sas-rtf-landscape (&optional ess-tmp-font-size)
"Creates an MS RTF landscape file from the current buffer."
(interactive)
(ess-revert-wisely)
(set-visited-file-name (concat (buffer-name) ".rtf"))
(if 'buffer-read-only (setq buffer-read-only nil))
(ess-rtf-replace-chars)
(goto-char (point-min))
(insert (concat
"{\\rtf1\\ansi{\\fonttbl\\f1\\fmodern " ess-sas-rtf-font-name ";}\n"
"\\margl720\\margr720\\margt720\\margb720\n"
"{\\*\\pgdsctbl\n"
"{\\pgdsc0\\pgdscuse195\\lndscpsxn\\pgwsxn15840\\pghsxn12240\\marglsxn1800\\margrsxn1800\\margtsxn1440\\margbsxn1440\\pgdscnxt0 Default;}}\n"
"\\landscape\\paperh12240\\paperw15840\\margl1800\\margr1800\\margt1440\\margb1440\\sectd\\sbknone\\lndscpsxn\\pgwsxn15840\\pghsxn12240\\marglsxn1800\\margrsxn1800\\margtsxn1440\\margbsxn1440\\ftnbj\\ftnstart1\\ftnrstcont\\ftnnar\\aenddoc\\aftnrstcont\\aftnstart1\\aftnnrlc\n"
"{\\colortbl;\\red0\\green0\\blue0;\\red0\\green0\\blue255;\\red0\\green255\\blue255;\\red0\\green255\\blue0;\\red255\\green0\\blue255;\\red255\\green0\\blue0;\\red255\\green255\\blue0;\\red255\\green255\\blue255;\\red0\\green0\\blue128;\\red0\\green128\\blue128;\\red0\\green128\\blue0;\\red128\\green0\\blue128;\\red128\\green0\\blue0;\\red128\\green128\\blue0;\\red128\\green128\\blue128;\\red192\\green192\\blue192;}\n"
"{\\stylesheet{\\s15\\plain\\f1\\fs16\\cf1\\cb8\\lang1024 Emacs Text;}{\\*\\cs16 \\additive\\f1\\fs16\\cf1\\cb8\\lang1024 Emacs Base Style;}}\n"
"{\\plain\\s15{\\cs16\\cs16\\f1\\fs16\\cf1\\cb8\\lang1024{\\cs16\\f1\\fs16\\cf1\\cb8\\lang1024\n"))
(goto-char (point-max))
(insert "}}}}\n")
(save-buffer)
(kill-buffer (current-buffer)))
(defun ess-sas-rtf-us-landscape ()
"Creates an MS RTF US landscape file from the current buffer."
(interactive)
(ess-sas-rtf-portrait "16")
(ess-sas-goto "rtf" t)
(goto-char (point-min))
(forward-line 3)
(insert (concat "{\\*\\pgdsctbl\n"
"{\\pgdsc0\\pgdscuse195\\lndscpsxn\\pgwsxn15840\\pghsxn12240\\marglsxn1800\\margrsxn1800\\margtsxn1440\\margbsxn1440\\pgdscnxt0 Default;}}\n"
"\\landscape\\paperh12240\\paperw15840\\margl1800\\margr1800\\margt1440\\margb1440\\sectd\\sbknone\\lndscpsxn\\pgwsxn15840\\pghsxn12240\\marglsxn1800\\margrsxn1800\\margtsxn1440\\margbsxn1440\\ftnbj\\ftnstart1\\ftnrstcont\\ftnnar\\aenddoc\\aftnrstcont\\aftnstart1\\aftnnrlc\n"))
(save-buffer)
(kill-buffer (current-buffer)))
(defun ess-sas-rtf-a4-landscape ()
"Creates an MS RTF A4 landscape file from the current buffer."
(interactive)
(ess-sas-rtf-portrait "16")
(ess-sas-goto "rtf" t)
(goto-char (point-min))
(forward-line 3)
(insert (concat "{\\*\\pgdsctbl\n"
"{\\pgdsc0\\pgdscuse195\\lndscpsxn\\pgwsxn16837\\pghsxn11905\\marglsxn1800\\margrsxn1800\\margtsxn1440\\margbsxn1440\\pgdscnxt0 Default;}}\n"
"\\landscape\\paperh11905\\paperw16837\\margl1800\\margr1800\\margt1440\\margb1440\\sectd\\sbknone\\lndscpsxn\\pgwsxn16837\\pghsxn11905\\marglsxn1800\\margrsxn1800\\margtsxn1440\\margbsxn1440\\ftnbj\\ftnstart1\\ftnrstcont\\ftnnar\\aenddoc\\aftnrstcont\\aftnstart1\\aftnnrlc\n"))
(save-buffer)
(kill-buffer (current-buffer)))
(defun ess-sas-submit ()
"Save the .sas file and submit to shell using a function that
depends on the value of `ess-sas-submit-method'"
(interactive)
(ess-sas-file-path)
(ess-sas-goto-sas)
(save-buffer)
(hack-local-variables)