Initial Commit
[packages] / xemacs-packages / ess / lisp / essa-sas.el
1 ;;; essa-sas.el -- clean-room implementation of many SAS-mode features
2
3 ;; Copyright (C) 1997--2005 A.J. Rossini, Rich M. Heiberger, Martin
4 ;;      Maechler, Kurt Hornik, Rodney Sparapani, and Stephen Eglen.
5
6 ;; Original Author: Rodney A. Sparapani <rsparapa@mcw.edu>
7 ;; Maintainer: ESS-core@stat.math.ethz.ch
8 ;; Created: 17 November 1999
9 ;; Keywords: SAS 
10
11 ;; This file is part of ESS
12
13 ;; This file is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17 ;;
18 ;; This file is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ;; GNU General Public License for more details.
22 ;;
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING.  If not, write to
25 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;;
27 ;; In short: you may use this code any way you like, as long as you
28 ;; don't charge money for it, remove this notice, or hold anyone liable
29 ;; for its results.
30
31 ;; Code:
32
33 ;;; Table of Contents
34 ;;; Section 1:  Variable Definitions
35 ;;; Section 2:  Function Definitions
36 ;;; Section 3:  Key Definitions
37
38 ;;; Section 1:  Variable Definitions
39
40 (defvar ess-sas-file-path "."
41   "Full path-name of the sas file to perform operations on.")
42
43 (defcustom ess-sas-data-view-libname " "
44   "*SAS code to define a library for `ess-sas-data-view-fsview'
45 or `ess-sas-data-view-insight'."
46   :group 'ess-sas  
47   :type  'string)
48
49 (defcustom ess-sas-data-view-submit-options 
50   (if ess-microsoft-p "-noenhancededitor -nosysin -log NUL:"
51     "-nodms -nosysin -log /dev/null")
52   "*The command-line options necessary for your OS with respect to
53 `ess-sas-data-view-fsview' and `ess-sas-data-view-insight'."
54     :group 'ess-sas
55   :type  'string)
56
57 (defcustom ess-sas-data-view-fsview-command "; proc fsview data=" 
58   "*SAS code to open a SAS dataset with `ess-sas-data-view-fsview'."
59     :group 'ess-sas
60   :type  'string)
61
62 (defcustom ess-sas-data-view-fsview-statement " "
63   "*SAS code to perform a PROC FSVIEW statement with `ess-sas-data-view-fsview'."
64     :group 'ess-sas
65   :type  'string)
66
67 (make-variable-buffer-local 'ess-sas-data-view-fsview-statement)
68
69 (defcustom ess-sas-data-view-insight-command "; proc insight data=" 
70   "*SAS code to open a SAS dataset with `ess-sas-data-view-insight'."
71   :group 'ess-sas
72   :type  'string)
73
74 (defcustom ess-sas-data-view-insight-statement " "
75   "*SAS code to perform a PROC FSVIEW statement with `ess-sas-data-view-insight'."
76     :group 'ess-sas
77   :type  'string)
78
79 (make-variable-buffer-local 'ess-sas-data-view-insight-statement)
80
81 (defcustom ess-sas-graph-view-suffix-regexp 
82   "[.]\\([eE]?[pP][sS]\\|[pP][dD][fF]\\|[gG][iI][fF]\\|[jJ][pP][eE]?[gG]\\|[tT][iI][fF][fF]?\\)"
83     "*GSASFILE suffix regexp."
84     :group 'ess-sas
85   :type  'string)
86
87 (defcustom ess-sas-graph-view-viewer-alist
88 ;;creates something like 
89 ;;'(("[pP][dD][fF]" . "/usr/local/bin/acroread") ("[eE]?[pP][sS]" . "/usr/local/bin/gv")))
90     (let ((ess-tmp-alist nil)
91         (ess-tmp-file nil))
92
93     (setq ess-tmp-file (executable-find "gv"))
94
95     (if (not ess-tmp-file) (setq ess-tmp-file (executable-find "ghostview")))
96
97     (if (not ess-tmp-file) (setq ess-tmp-file (executable-find 
98         (if ess-microsoft-p "gsview32" "gsview"))))
99
100     (if ess-tmp-file 
101         (setq ess-tmp-alist (list (cons "[eE]?[pP][sS]" ess-tmp-file)
102             (cons "[pP][dD][fF]" ess-tmp-file)))
103
104         (setq ess-tmp-file (executable-find (if ess-microsoft-p "acrord32" "acroread")))
105
106         (if ess-tmp-file
107             (setq ess-tmp-alist (cons "[pP][dD][fF]" ess-tmp-file)))))
108
109   "*Associate file name extensions with graphics image file viewers."
110   :group 'ess-sas
111   :type  'string)
112
113 ;;(defcustom ess-sas-smart-back-tab nil
114 ;;    "*Set to t to make C-TAB insert an end/%end; statement to close a block."
115 ;;    :group 'ess-sas
116 ;;)
117
118 (defcustom ess-sas-log-max 0
119   "*If >0 and .log file exceeds this many bytes, just \"refresh\" this many bytes."
120   :group 'ess-sas
121   :type  'integer)
122
123 (defcustom ess-sas-shell-buffer "*shell*"
124   "*Name that you want to use for the shell buffer; buffer-local."
125   :group 'ess-sas
126   :type  'string)
127
128 (make-variable-buffer-local 'ess-sas-shell-buffer)
129
130 (defcustom ess-sas-shell-buffer-remote-host nil 
131   "*Remote host that you want to open a shell on."
132   :group 'ess-sas
133   :type '(choice (const nil) string))
134
135 (make-variable-buffer-local 'ess-sas-shell-buffer-remote-host)
136
137 (defcustom ess-sas-shell-buffer-remote-init "ssh" 
138   "*Command to open a shell on a remote host."
139   :group 'ess-sas
140   :type  'string)
141
142 (make-variable-buffer-local 'ess-sas-shell-buffer-remote-init)
143
144 (defcustom ess-sas-submit-mac-virtual-pc nil
145   "*Non-nil means that you want to run Windows SAS in a
146 Virtual PC emulator on your Mac; buffer-local."
147   :group 'ess-sas
148   :type 'boolean)
149
150 (make-variable-buffer-local 'ess-sas-submit-mac-virtual-pc)
151
152 (defcustom ess-sas-submit-command sas-program
153     "*Command to invoke SAS in batch; buffer-local."
154     :group 'ess-sas
155   :type  'string)
156
157 (make-variable-buffer-local 'ess-sas-submit-command)
158
159 (defcustom ess-sas-submit-command-options " "
160     "*Options to pass to SAS in batch; buffer-local."
161     :group 'ess-sas
162   :type  'string)
163
164 (make-variable-buffer-local 'ess-sas-submit-command-options)
165
166 (defvar ess-sas-submit-method 
167   (if ess-microsoft-p 
168     (if (w32-shell-dos-semantics) 'ms-dos 'sh)
169     (if (or (equal system-type 'Apple-Macintosh) 
170             (and ess-sas-submit-mac-virtual-pc (equal system-type 'darwin)))
171         'apple-script 'sh))
172 "Method used by `ess-sas-submit'.
173 The default is based on the value of the emacs variable `system-type'
174 and, on Windows, the function `w32-shell-dos-semantics'.
175 'sh               if *shell* runs sh, ksh, csh, tcsh or bash
176 'ms-dos           if *shell* follows MS-DOS semantics
177 'apple-script     *shell* unavailable in Mac Classic, use AppleScript,
178                   also for Windows SAS in Virtual PC on Mac OS X 
179
180 Unix users will get 'sh by default.  
181
182 Windows users running bash in *shell* will get 'sh by default.
183
184 Windows users running MS-DOS in *shell* will get 'ms-dos by default.
185
186 Users accessing a remote machine with `telnet', `rlogin', `ssh', etc.,
187 should set this variable to 'sh regardless of their local shell 
188 (since their remote shell is 'sh).")
189
190 (make-variable-buffer-local 'ess-sas-submit-method)
191
192 (defcustom ess-sas-graph-view-viewer-default
193     (if ess-microsoft-p "kodakimg" 
194         (if (equal ess-sas-submit-method 'sh) "sdtimage"))
195   "*Default graphics image file viewer."
196     :group 'ess-sas
197   :type  'string)
198
199 (defcustom ess-sas-submit-post-command 
200     (if (equal ess-sas-submit-method 'sh) "-rsasuser &" 
201         (if ess-microsoft-p "-rsasuser -icon"))    
202     "*Command-line statement to post-modify SAS invocation, e.g. -rsasuser"
203     :group 'ess-sas
204   :type  'string)
205
206 (defcustom ess-sas-submit-pre-command 
207   (if (equal ess-sas-submit-method 'sh) 
208       ;; nice is tricky, higher numbers give you lower priorities
209       ;; if you are using csh/tcsh, the default priority is 4
210       ;; if you are using most other shells, the default priority is 10,
211       ;; and some implementations are higher, i.e. zsh unless you 
212       ;; specify "setopt no_bg_nice" in your ~/.zshrc
213       ;; therefore, on the same machine, you can run at a higher or
214       ;; lower priority by changing shells, although, the command
215       ;; line is the same!
216       ;; the following code should give you a priority of 10 regardless
217       ;; of which shell is in use, but it will default to the old
218       ;; behavior if csh or variant is not recognized
219       ;; this should avoid the necessity of each user needing to set this
220       ;; variable correctly based on the shell that they use and provide
221       ;; an environment where all shells are treated equally
222       (let* ((temp-shell (getenv "SHELL"))
223              (temp-char (string-match "/" temp-shell)))
224             
225         (while temp-char 
226           (setq temp-shell (substring temp-shell (+ 1 temp-char)))
227           (setq temp-char (string-match "/" temp-shell)))
228
229         (cond ((or (equal temp-shell "csh") (equal temp-shell "tcsh")) 
230                "nohup nice +6")
231               (t "nohup nice")))
232         (if ess-microsoft-p "start"))
233   "*Command-line statement to precede SAS invocation, e.g. start or nohup"
234     :group 'ess-sas
235   :type  'string)
236
237 (defcustom ess-sas-suffix-1 "txt"
238     "*The first suffix to associate with SAS."
239     :group 'ess-sas
240   :type  'string)
241
242 (defcustom ess-sas-suffix-2 "csv"
243     "*The second suffix to associate with SAS."
244     :group 'ess-sas
245   :type  'string)
246
247 (defcustom ess-sas-suffix-regexp 
248     (concat "[.]\\([sS][aA][sS]\\|[lL][oO][gG]\\|[lL][sS][tT]"
249         (if ess-sas-suffix-1 (concat 
250             "\\|" (downcase ess-sas-suffix-1) "\\|" (upcase ess-sas-suffix-1)))
251         (if ess-sas-suffix-2 (concat 
252             "\\|" (downcase ess-sas-suffix-2) "\\|" (upcase ess-sas-suffix-2)))
253         "\\)")
254     "*Regular expression for SAS suffixes."
255     :group 'ess-sas
256   :type  'string)
257
258 (defcustom ess-sleep-for (if ess-microsoft-p 5 0)
259   "*`ess-sas-submit-sh' may need to pause before sending output 
260 to the shell on Windows when `ess-sas-submit-method' is 'sh."
261     :group 'ess-sas
262   :type  'number)
263
264 (defvar ess-sas-tab-stop-alist
265  '(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)
266   "List of tab stop positions used by `tab-to-tab-stop' in ESS[SAS].")
267
268 (defcustom ess-sas-temp-root "ess-temp"
269     "*The root of the temporary .sas file for `ess-sas-submit-region'."
270     :group 'ess-sas
271   :type  'string)
272
273 ;;; Section 2:  Function Definitions
274
275
276 (defun ess-ebcdic-to-ascii-search-and-replace ()
277     "*Search and replace EBCDIC text with ASCII equivalents."
278     (interactive)
279     (let ((ess-tmp-dd (executable-find "dd")) (ess-tmp-recode (executable-find "recode"))
280           (ess-tmp-util nil) (ess-tmp-util-args nil))
281
282     (if ess-tmp-dd (progn
283         (setq ess-tmp-util ess-tmp-dd)
284         (setq ess-tmp-util-args "conv=ascii"))
285
286         (setq ess-tmp-util ess-tmp-recode)
287         (setq ess-tmp-util-args "EBCDIC..ISO-8859-1"))
288
289     (if ess-tmp-util
290         (while (search-forward-regexp "[^\f\t\n -~][^\f\t\n -?A-JQ-Yb-jp-y]*[^\f\t\n -~]?" nil t)
291             (call-process-region (match-beginning 0) (match-end 0)
292                     ess-tmp-util t (list t nil) t ess-tmp-util-args)))))
293
294 (defun ess-exit-notify-sh (string)
295 "Detect completion or failure of submitted job and notify the user."
296   (let* ((exit-done "\\[[0-9]+\\]\\ *\\+*\\ *\\(Exit\\|Done\\).*$")
297          (beg (string-match exit-done string)))
298     (if beg
299         (message (substring string beg (match-end 0))))))
300
301
302
303 (defun ess-sas-append-log ()
304     "Append ess-temp.log to the current .log file."
305     (interactive)
306     (ess-sas-goto "log" 'revert)
307     (goto-char (point-max))
308     (insert-file-contents (concat ess-sas-temp-root ".log"))
309     (save-buffer))
310
311 (defun ess-sas-append-lst ()
312     "Append ess-temp.lst to the current .lst file."
313     (interactive)
314     (ess-sas-goto "lst" 'revert)
315     (goto-char (point-max))
316     (insert-file-contents (concat ess-sas-temp-root ".lst"))
317     (save-buffer))
318
319 (defun ess-sas-backward-delete-tab ()
320   "Moves the cursor to the previous tab-stop, deleting any characters
321 on the way."
322   (interactive)
323   
324   (let* (;; point of search 
325          ;;(ess-sas-search-point nil)
326          ;; column of search 
327          ;;(ess-sas-search-column nil)
328          ;; limit of search 
329          ;;(ess-sas-search-limit nil)
330          ;; text to be inserted after a back-tab, if any
331          ;;(ess-sas-end-text "end;")
332          ;; current-column
333          (ess-sas-column (current-column))
334          ;; remainder of current-column and sas-indent-width
335          (ess-sas-remainder (% ess-sas-column sas-indent-width)))
336
337     (if (not (= ess-sas-column 0)) 
338         (progn
339           (if (= ess-sas-remainder 0) 
340               (setq ess-sas-remainder sas-indent-width))
341           
342          (let ((backward-delete-char-untabify-method 'nil))
343           (backward-delete-char-untabify ess-sas-remainder t)
344           (setq ess-sas-column (- ess-sas-column ess-sas-remainder))
345           (move-to-column ess-sas-column)
346              (setq left-margin ess-sas-column))
347     ))
348 ))
349
350 ;; this feature was far too complicated to perfect
351 ;;      (if ess-sas-smart-back-tab (progn
352 ;;        (save-excursion
353 ;;          (setq ess-sas-search-point      
354 ;;              (search-backward-regexp "end" nil t))
355
356 ;;          (if (and ess-sas-search-point
357 ;;              (search-backward-regexp "%" (+ ess-sas-search-point -1) t))
358 ;;              (setq ess-sas-search-point (+ ess-sas-search-point -1))
359 ;;          )
360                 
361 ;;          (if (and ess-sas-search-point
362 ;;              (not (equal ess-sas-column (current-column))))
363 ;;              (setq ess-sas-search-point nil))
364 ;;          )
365
366 ;;        (save-excursion
367 ;;          (setq ess-sas-search-point      
368 ;;              (search-backward-regexp "do\\|select" 
369 ;;                  ess-sas-search-point t))
370
371 ;;          (setq ess-sas-search-column (current-column))
372
373 ;;          (if ess-sas-search-point (progn
374 ;;              (save-excursion
375 ;;               (search-backward-regexp "^" nil t)
376 ;;               (setq ess-sas-search-limit (point))
377 ;;              )
378
379 ;;              (if (search-backward-regexp "if.*then\\|else" ess-sas-search-limit t)
380 ;;                  (setq ess-sas-search-point (point)))
381
382 ;;              (if (search-backward-regexp "%" ess-sas-search-limit t) (progn
383 ;;                  (setq ess-sas-end-text "%end;")
384 ;;                  (setq ess-sas-search-point (point))
385 ;;              ))
386
387 ;;              (setq ess-sas-search-column (current-column))
388
389 ;;              (if (not (equal ess-sas-column ess-sas-search-column))
390 ;;                 (setq ess-sas-search-point nil))
391 ;;        )))
392
393 ;;        (if ess-sas-search-point (insert ess-sas-end-text))
394 ;;         ))
395
396 (defun ess-sas-cd ()
397 "Change directory, taking into account various issues with respect to
398 `ess-sas-file-path'."
399     ;(interactive)
400     (ess-sas-file-path)
401     (ess-sas-goto-shell t)
402     (if (equal ess-sas-submit-method 'sh)
403       (insert "cd " (car (last (split-string (file-name-directory ess-sas-file-path) 
404         "\\([a-zA-Z][a-zA-Z]:\\|]\\)"))))
405         (if (equal ess-sas-submit-method 'ms-dos) (progn
406             (if (string-equal ":" (substring ess-sas-file-path 1 2)) (progn
407                 (insert (substring ess-sas-file-path 0 2))
408                 (comint-send-input)))
409             (insert "cd \"" (convert-standard-filename 
410                 (file-name-directory ess-sas-file-path)) "\""))))
411     (comint-send-input))
412
413 (defun ess-sas-create-local-variables-alist (&optional file-or-buffer)
414 "Create an alist of local variables from file-or-buffer, use the 
415 current buffer if nil."
416
417 (if file-or-buffer (set-buffer (ess-get-file-or-buffer file-or-buffer)))
418
419 (ess-change-alist 'ess-kermit-remote-directory ess-kermit-remote-directory nil))
420
421 (defun ess-sas-data-view-fsview (&optional ess-sas-data)
422   "Open a dataset for viewing with PROC FSVIEW."
423     (interactive)
424     (ess-save-and-set-local-variables)
425
426  (save-excursion (let ((ess-tmp-sas-data nil) 
427     (ess-tmp-sas-data-view-fsview-statement ess-sas-data-view-fsview-statement)
428     (ess-search-regexp 
429     "[ \t=]\\([a-zA-Z_][a-zA-Z_0-9]*[.][a-zA-Z_][a-zA-Z_0-9]*\\)\\(&.*\\)?[. ,()\t;/]")
430     (ess-search-except 
431     "^\\([wW][oO][rR][kK]\\|[fF][iI][rR][sS][tT]\\|[lL][aA][sS][tT]\\)[.]"))
432
433     (if ess-sas-data nil (save-match-data 
434        (search-backward-regexp "[ \t=]" nil t)
435
436         (save-excursion 
437             (setq ess-tmp-sas-data 
438                 (ess-search-except ess-search-regexp ess-search-except)))
439
440         (if (not ess-tmp-sas-data) 
441             (setq ess-tmp-sas-data 
442                 (ess-search-except ess-search-regexp ess-search-except t)))
443
444         (setq ess-sas-data (read-string "Permanent SAS Dataset: " ess-tmp-sas-data))
445
446         (ess-sas-goto-shell t)
447         (ess-sas-cd)
448
449         (insert (concat ess-sas-submit-pre-command " " ess-sas-submit-command 
450             " -initstmt \"" ess-sas-data-view-libname ess-sas-data-view-fsview-command 
451             ess-sas-data ";" ess-tmp-sas-data-view-fsview-statement "; run;\" " 
452             ess-sas-data-view-submit-options " " ess-sas-submit-post-command))
453     (comint-send-input)
454 )))))
455
456 (defun ess-sas-data-view-insight (&optional ess-sas-data)
457   "Open a dataset for viewing with PROC INSIGHT."
458     (interactive)
459     (ess-save-and-set-local-variables)
460
461  (save-excursion (let ((ess-tmp-sas-data nil) 
462     (ess-tmp-sas-data-view-insight-statement ess-sas-data-view-insight-statement)
463     (ess-search-regexp 
464     "[ \t=]\\([a-zA-Z_][a-zA-Z_0-9]*[.][a-zA-Z_][a-zA-Z_0-9]*\\)\\(&.*\\)?[. ,()\t;]")
465     (ess-search-except 
466     "^\\([wW][oO][rR][kK]\\|[fF][iI][rR][sS][tT]\\|[lL][aA][sS][tT]\\)[.]"))
467
468     (if ess-sas-data nil (save-match-data 
469         (search-backward-regexp "[ \t=]" nil t)
470
471         (save-excursion 
472             (setq ess-tmp-sas-data 
473                 (ess-search-except ess-search-regexp ess-search-except)))
474
475         (if (not ess-tmp-sas-data) 
476             (setq ess-tmp-sas-data 
477                 (ess-search-except ess-search-regexp ess-search-except t)))
478
479         (setq ess-sas-data (read-string "Permanent SAS Dataset: " ess-tmp-sas-data))
480
481         (ess-sas-goto-shell t)
482         (ess-sas-cd)
483
484         (insert (concat ess-sas-submit-pre-command " " ess-sas-submit-command 
485             " -initstmt \"" ess-sas-data-view-libname ess-sas-data-view-insight-command 
486             ess-sas-data ";" ess-tmp-sas-data-view-insight-statement "; run;\" " 
487             ess-sas-data-view-submit-options " " ess-sas-submit-post-command))
488     (comint-send-input)
489 )))))
490
491 (defun ess-sas-graph-view ()
492   "Open a GSASFILE for viewing."
493   (interactive)
494 ;  (ess-sas-file-path)
495   (ess-sas-goto-log 'no-error-check)
496
497   (save-excursion (let (
498         (ess-tmp-length (length ess-sas-graph-view-viewer-alist))
499         (ess-tmp-counter 0)
500         (ess-tmp-graph nil)
501         (ess-tmp-graph-alist nil)
502         (ess-tmp-glyph nil)
503         (ess-tmp-graph-regexp 
504             (concat "[ ]RECORDS[ ]WRITTEN[ ]TO[ ]\n?[ ]*\\(\\(\n\\|[^.]\\)*" 
505                 ess-sas-graph-view-suffix-regexp "\\)")))
506 ;           (concat "['\"]\\(.*" ess-sas-graph-suffix-regexp "\\)['\"]")))
507
508     (save-match-data 
509        (search-backward-regexp "[ \t=]" nil t)
510
511        (save-excursion 
512             (setq ess-tmp-graph (ess-search-except ess-tmp-graph-regexp)))
513
514         (if (not ess-tmp-graph) 
515             (setq ess-tmp-graph (ess-search-except ess-tmp-graph-regexp nil t)))
516
517         (setq ess-tmp-graph (read-string "GSASFILE: " 
518             (or ess-tmp-graph ess-sas-file-path)))
519
520           (if (fboundp 'ess-xemacs-insert-glyph) (progn
521               (if (string-match "[.][gG][iI][fF]" ess-tmp-graph)
522                  (setq ess-tmp-glyph 'gif)
523               ;;else
524               (if (string-match "[.][jJ][pP][eE]?[gG]" ess-tmp-graph)
525                  (setq ess-tmp-glyph 'jpeg)))))
526
527           ;;GNU Emacs graphics file image viewing mode loaded?
528           (if (and (boundp 'auto-image-file-mode) auto-image-file-mode
529               (string-match "[.][jJ][pP][eE]?[gG]" ess-tmp-graph))
530               (find-file ess-tmp-graph)
531           ;;else XEmacs graphics file image viewing mode loaded?
532           (if (and (fboundp 'image-mode)
533                 (string-match "[.]\\([jJ][pP][eE]?[gG]\\|[gG][iI][fF]\\)" 
534                     ess-tmp-graph))
535               (find-file ess-tmp-graph)
536           ;;else XEmacs graphics file image viewing primitives loaded?
537           (if ess-tmp-glyph (progn
538                 (switch-to-buffer (file-name-nondirectory ess-tmp-graph))
539                 (ess-xemacs-insert-glyph 
540                     (make-glyph (vector ess-tmp-glyph :file ess-tmp-graph))))
541
542           ;;else use the appropriate graphics file image viewer
543             (while (< ess-tmp-counter ess-tmp-length)
544                 (setq ess-tmp-graph-alist 
545                     (nth ess-tmp-counter ess-sas-graph-view-viewer-alist))
546                 (setq ess-tmp-graph-regexp (car ess-tmp-graph-alist))
547
548                 (if (string-match 
549                         (concat "[.]" ess-tmp-graph-regexp) ess-tmp-graph)
550                     (progn
551                         (ess-sas-goto-shell t)
552                         (insert ess-sas-submit-pre-command " " 
553                             (cdr ess-tmp-graph-alist) " " ess-tmp-graph 
554               (if (equal ess-sas-submit-method 'sh) " &"))
555                         (setq ess-tmp-glyph 'alist)
556                         (setq ess-tmp-counter ess-tmp-length))
557                     ;;else
558                     (setq ess-tmp-counter (+ ess-tmp-counter 1))))
559
560             (if (not ess-tmp-glyph) (progn
561                 (ess-sas-goto-shell t)
562                 (insert ess-sas-submit-pre-command " " 
563                     ess-sas-graph-view-viewer-default " " ess-tmp-graph 
564                     (if (equal ess-sas-submit-method 'sh) " &"))))
565
566             (comint-send-input))))))))
567
568 (defun ess-sas-file-path ()
569  "Define `ess-sas-file-path' to be the current buffer depending on suffix."
570   (interactive)
571
572   (save-match-data (let ((ess-sas-temp-file (expand-file-name (buffer-name))))
573     (if (string-match ess-sas-suffix-regexp ess-sas-temp-file) ;;(progn
574         (setq ess-sas-file-path 
575            (nth 0 (split-string ess-sas-temp-file "[<]")))))))
576         ;; (setq ess-directory (file-name-directory ess-sas-file-path)))))))
577
578 (defun ess-sas-file-path-remote-host ()
579 "Return the remote host, if any, associated with `ess-sas-file-path'."
580 (interactive)
581
582 (let* ((temp-colon-pos (string-match ":" ess-sas-file-path))
583        (temp-list 
584         (if (or (not temp-colon-pos) (> temp-colon-pos 2))
585                 (if (equal ess-sas-file-path ".") nil
586                     (split-string (file-name-directory ess-sas-file-path) 
587                         "\\(@\\|:\\|]\\)"))
588         (list ess-sas-file-path)))
589        (temp-list-length (length temp-list)))   
590     (if (= temp-list-length 1) (setq temp-list nil)
591         (if (= temp-list-length 2) (setq temp-list (car temp-list))
592             (setq temp-list (nth 1 temp-list))))
593
594     (if temp-list (setq temp-list 
595                 (car (last (split-string temp-list "/"))))) 
596     temp-list)) 
597
598 (defun ess-sas-goto (suffix &optional revert no-create)
599   "Find a file associated with a SAS file by suffix and revert if necessary."
600     (let ((ess-temp-regexp (concat ess-sas-suffix-regexp "\\(@.+\\)?")))
601         (save-match-data 
602         (if (or (string-match ess-temp-regexp (expand-file-name (buffer-name)))
603             (string-match ess-temp-regexp ess-sas-file-path))
604         (progn
605             (ess-sas-file-path)
606             (let* (
607                 (ess-sas-temp-file (replace-match (concat "." suffix) t t 
608                     ess-sas-file-path))
609                 (ess-sas-temp-buff (find-buffer-visiting ess-sas-temp-file))
610                 (ess-temp-kermit-remote-directory ess-kermit-remote-directory))
611
612         (if ess-sas-temp-buff (switch-to-buffer ess-sas-temp-buff)
613                 ;; else
614                 (if no-create (setq revert nil) (find-file ess-sas-temp-file)))
615         
616             (if (and (not no-create) 
617                        (or (string-equal suffix "log")
618                            (string-equal suffix "lst")))
619                 (ess-kermit-get (file-name-nondirectory ess-sas-temp-file) 
620                     ess-temp-kermit-remote-directory))
621
622               (if revert
623                   (if (and (> ess-sas-log-max 0) (string-equal suffix "log")
624                            (> (nth 7 (file-attributes ess-sas-temp-file))
625                               ess-sas-log-max))
626                       (progn
627                         (insert-file-contents ess-sas-temp-file nil 0
628                                               ess-sas-log-max t)
629                         t)
630
631                     (ess-revert-wisely)) nil)))))))
632
633 ;;(defun ess-sas-file (suffix &optional revert)
634 ;;  "Please use `ess-sas-goto' instead."
635 ;;  (let* ((tail (downcase (car (split-string 
636 ;;          (car (last (split-string (buffer-name) "[.]"))) "[<]"))))
637         ;;(if (fboundp 'file-name-extension) (file-name-extension (buffer-name))
638         ;;               (substring (buffer-name) -3)))
639 ;;       (tail-in-tail-list (member tail (list "sas" "log" "lst"
640 ;;                           ess-sas-suffix-1 ess-sas-suffix-2)))
641 ;;       (root (if tail-in-tail-list (expand-file-name (buffer-name))
642 ;;               ess-sas-file-path))
643 ;;       (ess-sas-arg (concat (file-name-sans-extension root) "." suffix))
644 ;;       (ess-sas-buf (find-buffer-visiting ess-sas-arg)))
645 ;;    (if (equal tail suffix) (if revert (ess-revert-wisely))
646 ;;      (if (not ess-sas-buf) (find-file ess-sas-arg)
647 ;;          (switch-to-buffer ess-sas-buf)
648 ;;          (if revert (ess-revert-wisely))))))
649
650
651 (defun ess-sas-goto-file-1 ()
652   "Switch to ess-sas-file-1 and revert from disk."
653   (interactive)
654   (ess-sas-goto ess-sas-suffix-1 'revert))
655
656 (defun ess-sas-goto-file-2 ()
657   "Switch to ess-sas-file-2 and revert from disk."
658   (interactive)
659   (ess-sas-goto ess-sas-suffix-2 'revert))
660
661 (defun ess-sas-goto-log (&optional ess-tmp-no-error-check)
662   "Switch to the .log file, revert from disk and search for error messages."
663   (interactive)
664
665   (let ((ess-sas-error (concat
666     "^ERROR [0-9]+-[0-9]+:\\|^ERROR:\\|_ERROR_=1 _N_=\\|_ERROR_=1[ ]?$"
667     "\\|NOTE: MERGE statement has more than one data set with repeats of BY values."
668     "\\|NOTE: Variable .* is uninitialized."
669     "\\|WARNING: Apparent symbolic reference .* not resolved."
670     "\\|NOTE 485-185: Informat .* was not found or could not be loaded."
671     "\\|WARNING: Length of character variable has already been set."
672     "\\|Bus Error In Task\\|Segmentation Violation In Task"
673     "\\|NOTE: Estimated G matrix is not positive definite."))
674         (ess-sas-save-point nil))
675
676   (if (ess-sas-goto "log" 'revert) (progn
677         (setq ess-sas-save-point (point))
678         (goto-char (point-min)))
679     (setq ess-sas-save-point (point)))
680
681 (if ess-tmp-no-error-check (goto-char ess-sas-save-point)
682   (if (or (search-forward-regexp ess-sas-error nil t)
683         (and (goto-char (point-min))
684             (search-forward-regexp ess-sas-error nil t)))
685         (if (and (boundp 'zmacs-regions) zmacs-regions)
686             (progn
687                 (if ess-sas-pop-mark (pop-mark)
688                     (setq ess-sas-pop-mark t))
689                 (push-mark (match-beginning 0) t)
690                 (zmacs-activate-region)))
691         (goto-char ess-sas-save-point)))))
692
693 (defun ess-sas-goto-lst ()
694   "Switch to the .lst file and revert from disk."
695   (interactive)
696   (ess-sas-goto "lst" 'revert))
697
698 (defun ess-sas-goto-sas (&optional revert)
699   "Switch to the .sas file."
700   (interactive)
701   (ess-sas-goto "sas" revert))
702
703 (defun ess-sas-goto-shell (&optional set-buffer)
704 "Set `ess-sas-file-path' and goto `ess-sas-shell-buffer'.  If
705 optional argument is non-nil, then set-buffer rather than switch."
706   (interactive)
707   (ess-sas-file-path)
708
709 ; The following let* block is an attempt to deal with remote directories.
710     (let* ((temp-shell-buffer-remote-host 
711             (or ess-sas-shell-buffer-remote-host (ess-sas-file-path-remote-host)))
712         (temp-shell-buffer-remote-init ess-sas-shell-buffer-remote-init)
713         (temp-shell-buffer 
714             (if temp-shell-buffer-remote-host 
715                 (concat "*" temp-shell-buffer-remote-host "*")
716                 ess-sas-shell-buffer))
717 )
718
719   (if (get-buffer temp-shell-buffer) 
720     (if set-buffer (set-buffer temp-shell-buffer) 
721                    (switch-to-buffer temp-shell-buffer))
722     (shell)
723     (rename-buffer temp-shell-buffer)
724     
725     (if temp-shell-buffer-remote-host (progn
726         (insert (concat 
727             temp-shell-buffer-remote-init " " temp-shell-buffer-remote-host))
728         (comint-send-input))
729     )
730
731     (if (eq ess-sas-submit-method 'sh)
732         (add-hook 'comint-output-filter-functions 'ess-exit-notify-sh)) ;; 19.28
733                                           ;; nil t) works for newer emacsen
734     )
735   )
736
737   (goto-char (point-max))
738 ; (insert "cd " ess-temp-directory)
739 ; (comint-send-input))
740 )
741
742 (defun ess-sas-interactive ()
743 "And now for something completely different."
744     (interactive)
745     (ess-sas-file-path)
746
747     (let ((ess-temp-sas-file 
748 (nth 0 (split-string 
749 (car (last (split-string ess-sas-file-path "\\([a-zA-Z][a-zA-Z]:\\|]\\)"))) "[.]"))))
750 ;;    (message "%s" ess-temp-sas-file)
751     (setq ess-sas-shell-buffer "*iESS[SAS]*") 
752     (ess-sas-goto-shell)
753     (insert (concat ess-sas-submit-command " " ess-sas-submit-command-options 
754         " -altlog " ess-temp-sas-file ".log -altprint "
755             ess-temp-sas-file ".lst -stdio < /dev/tty"))
756     (comint-send-input)
757     (ess-add-ess-process)
758     (ess-sas-goto-sas)
759     (setq ess-sas-submit-method 'iESS)
760     (setq ess-eval-visibly-p nil)
761 ))
762 ;;(defun ess-sas-interactive ()
763 ;;  (interactive)
764 ;;  (ess-sas-file-path)
765 ;;    (setq ess-sas-submit-method 'iESS)
766 ;;
767 ;;    (let ((ess-temp-stderr " ") (ess-temp-stdout " ") (ess-temp-stdin " "))
768 ;;    (setq ess-sas-shell-buffer "*LOG*")
769 ;;    (ess-sas-goto-shell)
770 ;;    (insert "tty")
771 ;;    (comint-send-input)
772 ;;    (ess-sleep)
773 ;;    (save-excursion (setq ess-temp-stderr (ess-search-except "\\(/dev/[a-z0-9/]+\\)" nil t)))
774 ;;    (setq ess-sas-shell-buffer "*OUTPUT*") 
775 ;;    (ess-sas-goto-shell) 
776 ;;    (insert "tty") 
777 ;;    (comint-send-input) 
778 ;;    (ess-sleep) 
779 ;;    (save-excursion (setq ess-temp-stdout (ess-search-except "\\(/dev/[a-z0-9/]+\\)" nil t)))
780 ;;    (setq ess-sas-shell-buffer "*PROGRAM*") 
781 ;;    (ess-sas-goto-shell)
782 ;;;;    (insert "tty")
783 ;;    (comint-send-input)
784 ;;    (ess-sleep)
785 ;;    (insert "sh")
786 ;;    (comint-send-input)
787 ;;    (ess-sleep)
788 ;;    (save-excursion (setq ess-temp-stdin (ess-search-except "\\(/dev/[a-z0-9/]+\\)" nil t)))
789 ;;    (insert (concat ess-sas-submit-command " " ess-sas-submit-command-options " -stdio <"
790 ;;      ess-temp-stdin " >1 " ess-temp-stdout " >2 " ess-temp-stderr))
791 ;;    (comint-send-input)
792 ;;    (ess-add-ess-process)
793 ;;    (ess-sas-goto-sas)
794 ;;))
795
796 (defun ess-sas-kill-buffers ()
797 "Kill all buffers related to a .sas file."
798   (interactive)
799   (ess-sas-file-path)
800   (ess-sas-goto "log" nil t)
801   (kill-buffer nil)  
802   (ess-sas-goto "lst" nil t)
803   (kill-buffer nil)  
804   (ess-sas-goto ess-sas-suffix-1 nil t)
805   (kill-buffer nil)  
806   (ess-sas-goto ess-sas-suffix-2 nil t)
807   (kill-buffer nil)  
808   (ess-sas-goto "sas" nil t)
809   (kill-buffer nil)  
810 )
811
812 (eval-when-compile
813   (condition-case nil
814       (progn
815         (require 'rtf-support)
816         (when (featurep 'rtf-support)
817
818 (defun ess-sas-rtf-portrait ()
819 "Creates an MS RTF portrait file from the current buffer."
820     (interactive)
821     (ess-sas-file-path)
822     
823     (let
824         ((ess-temp-rtf-file (replace-in-string ess-sas-file-path "[.][^.]*$" ".rtf")))
825             ;(expand-file-name (buffer-name)) "[.][^.]*$" ".rtf")))
826         (rtf-export ess-temp-rtf-file)
827         (ess-sas-goto "rtf" t)
828         (goto-char (point-min))
829         (replace-regexp "\\\\fmodern .*;" "\\\\fmodern courier;" )
830         (goto-char (point-min))
831             
832         (while (replace-regexp "\\\\fs[0-9]+" "\\\\fs18" ) nil)
833             
834         (save-buffer)))
835
836 (defun ess-sas-rtf-us-landscape ()
837 "Creates an MS RTF US landscape file from the current buffer."
838     (interactive)
839     (ess-sas-rtf-portrait)
840     (ess-sas-goto "rtf" t)
841     (goto-char (point-min))
842     (forward-line 3)
843     (insert (concat "{\\*\\pgdsctbl\n"
844 "{\\pgdsc0\\pgdscuse195\\lndscpsxn\\pgwsxn15840\\pghsxn12240\\marglsxn1800\\margrsxn1800\\margtsxn1440\\margbsxn1440\\pgdscnxt0 Default;}}\n"
845 "\\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")) 
846     (save-buffer))
847
848 (defun ess-sas-rtf-a4-landscape ()
849 "Creates an MS RTF A4 landscape file from the current buffer."
850     (interactive)
851     (ess-sas-rtf-portrait)
852     (ess-sas-goto "rtf" t)
853     (goto-char (point-min))
854     (forward-line 3)
855     (insert (concat "{\\*\\pgdsctbl\n"
856 "{\\pgdsc0\\pgdscuse195\\lndscpsxn\\pgwsxn16837\\pghsxn11905\\marglsxn1800\\margrsxn1800\\margtsxn1440\\margbsxn1440\\pgdscnxt0 Default;}}\n"
857 "\\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"))
858     (save-buffer))
859 ))
860     (error nil)))
861
862 (defun ess-sas-submit ()
863   "Save the .sas file and submit to shell using a function that
864 depends on the value of  `ess-sas-submit-method'"
865   (interactive)
866   (ess-sas-file-path)
867   (ess-sas-goto-sas)
868   (ess-save-and-set-local-variables)
869
870   (cond
871    ((eq ess-sas-submit-method 'apple-script) 
872         (ess-sas-submit-mac ess-sas-submit-command 
873             ess-sas-submit-command-options))
874    ((eq ess-sas-submit-method 'ms-dos) 
875         (ess-sas-submit-windows ess-sas-submit-command 
876             ess-sas-submit-command-options))
877    ((eq ess-sas-submit-method 'iESS) 
878         (ess-sas-submit-iESS ess-sas-submit-command 
879             ess-sas-submit-command-options))
880    ((eq ess-sas-submit-method 'sh) 
881         (ess-sas-submit-sh ess-sas-submit-command 
882             ess-sas-submit-command-options)) 
883    (t (ess-sas-submit-sh ess-sas-submit-command 
884         ess-sas-submit-command-options)))
885 ;  (ess-sas-goto-sas)
886 )
887
888 (defun ess-sas-submit-iESS (arg1 arg2)
889   "iESS
890 Submit a batch job in an inferior-ESS buffer.  The buffer should
891 (1) have telnet access and be running a shell on a remote machine
892 or
893 (2) be running a shell on the local machine.
894
895 The user can telnet to the remote computer and then declare the
896 *telnet-buffer* to be an inferior ESS buffer with the `ess-add-ess-process'
897 command.  When using a remote computer, the .sas file must live on the
898 remote computer and be accessed through `ange-ftp'.  When
899 `ess-sas-submit' saves a file, it is therefore saved on the remote
900 computer.  The various functions such as `ess-sas-goto-lst' retrieve
901 their files from the remote computer.  Local copies of the .sas .lst
902 .log and others may be made manually with `write-buffer'."
903   ;;  (ess-eval-linewise (concat "cd  default-directory))
904   (ess-force-buffer-current "Process to load into: ")
905   (ess-eval-linewise 
906     (concat "cd " (car (last 
907         (split-string (file-name-directory ess-sas-file-path) "\\(:\\|]\\)")))))
908   (ess-eval-linewise (concat arg1 " " arg2 " " (buffer-name) " &")))
909
910 (defun ess-sas-submit-mac (arg1 arg2)
911 "If you are using Mac SAS, then arg1, `ess-sas-submit-command', should be 
912 the AppleScript command \"invoke SAS using program file\", and, if necessary,
913 arg2, `ess-sas-submit-command-options', is a string of the form 
914 \"with options { \\\"option-1\\\", \\\"option-2\\\", etc.}\".  If you are
915 using Windows SAS with the PC emulator Virtual PC, then `ess-sas-submit-command'
916 should be ..."
917   ;(ess-save-and-set-local-variables)
918
919   (do-applescript (concat arg1 " \""
920      (if (not ess-sas-submit-mac-virtual-pc) 
921             (unix-filename-to-mac default-directory))
922                           (buffer-name) "\"" arg2)))
923
924 (defun ess-sas-submit-region ()
925     "Write region to temporary file, and submit to SAS."
926     (interactive)
927     (ess-sas-file-path)
928     (write-region (region-beginning) (region-end) 
929         (concat ess-sas-temp-root ".sas"))
930
931     (save-excursion 
932       (ess-sas-goto-shell t)
933
934     (if (and (w32-shell-dos-semantics)
935         (string-equal ":" (substring ess-sas-file-path 1 2)))
936         (progn
937                 (insert (substring ess-sas-file-path 0 2))
938                 (comint-send-input)
939     ))
940
941     (insert "cd \"" (convert-standard-filename 
942         (file-name-directory ess-sas-file-path)) "\"")
943     (comint-send-input)
944
945     (insert (concat ess-sas-submit-pre-command " " ess-sas-submit-command 
946           " " ess-sas-temp-root " " ess-sas-submit-post-command))
947     (comint-send-input)
948     )
949 )
950
951 (defun ess-sas-submit-sh (arg1 arg2)
952   "Unix or bash in the *shell* buffer.
953 Multiple processing is supported on this platform.
954 SAS may not be found in your PATH.  You can alter your PATH to include
955 SAS or you can specify the PATHNAME (PATHNAME can NOT contain spaces),
956 i.e. let arg1 be your local equivalent of
957 \"/usr/local/sas612/sas\"."
958     (if (string-equal (substring 
959             (file-name-nondirectory ess-sas-file-path) 0 1) ess-kermit-prefix)
960       (progn
961        (ess-kermit-send)
962        (ess-sas-goto-shell t)
963         (insert ess-sas-submit-pre-command " " arg1 " "  
964          (substring (file-name-sans-extension 
965             (file-name-nondirectory ess-sas-file-path)) 1)
966          " " arg2 " " ess-sas-submit-post-command))
967     ;;else
968       (ess-sas-goto-shell t)
969 ;      (if ess-microsoft-p
970 ;         (insert "cd "  (file-name-directory ess-sas-file-path))
971 ;       (insert "cd " (car (last (split-string 
972 ;           (file-name-directory ess-sas-file-path) "\\(:\\|]\\)")))))
973       (insert "cd " (car (last (split-string (file-name-directory ess-sas-file-path) 
974 "\\([a-zA-Z][a-zA-Z]:\\|]\\)"))))
975       (comint-send-input)
976       (insert ess-sas-submit-pre-command " " arg1 " "  
977         (file-name-sans-extension (file-name-nondirectory ess-sas-file-path)) 
978         " " arg2 " " ess-sas-submit-post-command))
979     (ess-sleep)
980     (comint-send-input))
981
982 (defun ess-sas-submit-windows (arg1 arg2)
983   "Windows using MS-DOS prompt in the *shell* buffer.
984 Multiple processing is supported on this platform.
985 On most Windows installations, SAS will not be found in your
986 PATH.  You can set `ess-sas-submit-command' to 
987 \"sas -icon -rsasuser\" and alter your PATH to include SAS, i.e.
988
989 SET PATH=%PATH%;C:\\Program Files\\SAS
990
991 Or you can specify the PATHNAME directly (you must escape 
992 spaces by enclosing the string in \\\"'s), i.e. let 
993 `ess-sas-submit-command' be \"\\\"C:\\Program Files\\SAS\\sas.exe\\\"\".
994 Keep in mind that the maximum command line length in MS-DOS is
995 127 characters so altering your PATH is preferable."
996     ;(ess-save-and-set-local-variables)
997     (ess-sas-goto-shell t)
998     (if (string-equal ":" (substring ess-sas-file-path 1 2)) 
999         (progn
1000                 (insert (substring ess-sas-file-path 0 2))
1001                 (comint-send-input)
1002         )
1003     )
1004     (insert "cd \"" (convert-standard-filename 
1005         (file-name-directory ess-sas-file-path)) "\"")
1006     (comint-send-input)
1007     (insert ess-sas-submit-pre-command " " arg1 " -sysin \"" 
1008         (file-name-sans-extension (file-name-nondirectory ess-sas-file-path)) "\" "
1009         arg2 " " ess-sas-submit-post-command)
1010     (comint-send-input))
1011
1012 (defun ess-sas-tab-to-tab-stop ()
1013   "Tab to next tab-stop and set left margin."
1014   (interactive)
1015   (tab-to-tab-stop)
1016   (setq left-margin (current-column))
1017 )
1018   
1019 (defun ess-sas-transcript (&optional strip)
1020 "Comment .log messages to create a .sas program; use C-u to strip."
1021 (interactive "P")
1022 (save-excursion
1023     (goto-char (point-min))
1024
1025     (while (search-forward-regexp (concat
1026            "^\\(\\(1[ \t]+The SAS System\\|\f\\|NOTE\\|WARNING\\|ERROR\\|"
1027            "[ \t]+\\(\\(real\\|cpu\\) time\\|Licensed to\\|Engine:\\|"
1028            "Physical Name:\\|File Name=\\|Owner Name=\\|Group Name=\\|"
1029            "Access Permission=\\|File Size (bytes)=\\|Pipe command=\\|"
1030            "RECFM=[DFNPV],LRECL=\\|[0-9]+:[0-9]+[ /t]+[0-9]+:[0-9]+\\|"
1031            "[1-9][0-9]* at [0-9]+:[0-9]+[ /t]+[1-9][0-9]* at [0-9]+:[0-9]+\\)\\).*$"
1032            "\\|[0-9]+\\([ \t]+!\\)?\\|MPRINT([_A-Z]+):\\|"
1033            "[ \t]+\\(values at the places given by: (Line):(Column).\\|"
1034            "The m\\(in\\|ax\\)imum record length was [1-9][0-9]*.\\|"
1035            "One or more lines were truncated.\\|"
1036            "Each place is given by: (Number of times) at (Line):(Column).\\|"
1037            "[0-9][0-9]:[0-9][0-9] [MTWFS][aeioudhnrst]+day, [JFMASOND]"
1038            "[aeiouybcghlmnprstv]+ [1-9][0-9]?, 20[0-9][0-9]\\)\\)") 
1039            nil t) (replace-match (if strip " " "/*\\&*/") t))
1040 ))
1041
1042 (defun ess-sas-toggle-sas-listing-mode (&optional force)
1043   "Toggle SAS-listing-mode for .lst files."
1044   (interactive)
1045   (ess-sas-goto-lst)
1046
1047 (if (equal (cdr (assoc "\\.[lL][sS][tT]\\'" auto-mode-alist)) 'SAS-listing-mode) (progn
1048       (setq auto-mode-alist (delete '("\\.[lL][sS][tT]\\'" . SAS-listing-mode) auto-mode-alist))
1049       (setq buffer-read-only nil)
1050       (ess-listing-minor-mode 0))
1051       (setq auto-mode-alist (append '(("\\.[lL][sS][tT]\\'" . SAS-listing-mode)) auto-mode-alist))
1052       (setq buffer-read-only t)
1053       (ess-listing-minor-mode 1)))
1054
1055 (defun ess-sas-toggle-sas-log-mode ()
1056   "Toggle SAS-log-mode for .log files."
1057   (interactive)
1058   (ess-sas-goto-log t)
1059
1060   (if (equal (cdr (assoc "\\.[lL][oO][gG]\\'" auto-mode-alist)) 'SAS-log-mode) (progn
1061       (setq auto-mode-alist (delete '("\\.[lL][oO][gG]\\'" . SAS-log-mode) auto-mode-alist))
1062       (setq buffer-read-only nil)
1063       (ess-transcript-minor-mode 0)
1064       (font-lock-mode 0))
1065       (setq auto-mode-alist (append '(("\\.[lL][oO][gG]\\'" . SAS-log-mode)) auto-mode-alist))
1066       (setq buffer-read-only t)
1067       (ess-transcript-minor-mode 1)
1068       (font-lock-mode 1)
1069       (font-lock-fontify-buffer)))
1070   
1071 (defun ess-sleep ()
1072   "Put emacs to sleep for `ess-sleep-for' seconds.
1073 Sometimes its necessary to wait for a shell prompt."
1074   (if (featurep 'xemacs) (sleep-for ess-sleep-for)
1075        (sleep-for 0 (truncate (* ess-sleep-for 1000)))
1076     )
1077   )
1078
1079 ;;; Section 3:  Key Definitions
1080
1081
1082 (defun ess-sas-edit-keys-set (&optional arg)
1083 "Set TAB/RET key in `SAS-mode'.
1084 If arg is nil
1085     TAB is `sas-indent-line' and
1086     RET is `newline-and-indent'.
1087 Else
1088     TAB is `ess-sas-tab-to-tab-stop', 
1089     C-TAB is `ess-sas-backward-delete-tab' and
1090     RET is `newline'."
1091   (interactive)
1092
1093   (if arg
1094       (progn
1095         (if (and (equal emacs-major-version 19) (equal emacs-minor-version 28))
1096                (define-key sas-mode-local-map [C-tab] 'ess-sas-backward-delete-tab)
1097           ;;else
1098                (define-key sas-mode-local-map [(control tab)] 'ess-sas-backward-delete-tab))
1099         (define-key sas-mode-local-map [return] 'newline)
1100         (define-key sas-mode-local-map "\t" 'ess-sas-tab-to-tab-stop))
1101     ;;else
1102       (define-key sas-mode-local-map [return] 'newline-and-indent)
1103     (define-key sas-mode-local-map "\t" 'sas-indent-line)))
1104
1105 (defvar ess-sas-edit-keys-toggle nil
1106 "Toggle TAB/RET key in `SAS-mode'.
1107 nil binds TAB to `sas-indent-line' and RET to `newline-and-indent'.
1108 Non-nil binds TAB to `ess-sas-tab-to-tab-stop', 
1109 C-TAB to `ess-sas-backward-delete-tab', and RET to `newline'.")
1110
1111 (defun ess-sas-edit-keys-toggle (&optional arg)
1112 "Toggle `ess-sas-edit-keys-toggle'.  Optional arg is still
1113 accepted for backward compatibility, however, arg is ignored."
1114   (interactive)
1115
1116   (setq ess-sas-edit-keys-toggle (not ess-sas-edit-keys-toggle))
1117   (ess-sas-edit-keys-set ess-sas-edit-keys-toggle)
1118 )
1119
1120 (defvar ess-sas-global-pc-keys nil
1121   "Non-nil if function keys use PC-like SAS key definitions in all modes.")
1122
1123 (defun ess-sas-global-pc-keys ()
1124   "PC-like SAS key definitions"
1125   (interactive)
1126   (when (featurep 'rtf-support)
1127   (global-set-key [(control f1)] 'ess-sas-rtf-portrait)
1128     (global-set-key [(control f2)] 'ess-sas-rtf-us-landscape))
1129   (global-set-key (quote [f2]) 'ess-revert-wisely)
1130   (global-set-key (quote [f3]) 'ess-sas-goto-shell)
1131   (global-set-key (quote [f4]) 'ess-sas-goto-file-1)
1132   (global-set-key (quote [f5]) 'ess-sas-goto-sas)
1133   (global-set-key (quote [f6]) 'ess-sas-goto-log)
1134   (global-set-key [(control f6)] 'ess-sas-append-log)
1135   (global-set-key (quote [f7]) 'ess-sas-goto-lst)
1136   (global-set-key [(control f7)] 'ess-sas-append-lst)
1137   (global-set-key (quote [f8]) 'ess-sas-submit)
1138   (global-set-key [(control f8)] 'ess-sas-submit-region)
1139   (global-set-key (quote [f9]) 'ess-sas-data-view-fsview)
1140   (global-set-key [(control f9)] 'ess-sas-data-view-insight)
1141   (global-set-key (quote [f10]) 'ess-sas-toggle-sas-log-mode)
1142   (global-set-key [(control f10)] 'ess-sas-toggle-sas-listing-mode)
1143   (global-set-key (quote [f11]) 'ess-sas-goto-file-2)
1144   (global-set-key [(control f11)] 'ess-ebcdic-to-ascii-search-and-replace)
1145   (global-set-key (quote [f12]) 'ess-sas-graph-view)
1146   (if (and ess-sas-edit-keys-toggle
1147            (equal emacs-major-version 19) (equal emacs-minor-version 28))
1148       (global-set-key [C-tab] 'ess-sas-backward-delete-tab)
1149                                         ;else
1150     (global-set-key [(control tab)] 'ess-sas-backward-delete-tab))
1151   ;(define-key sas-mode-local-map "\C-c\C-p" 'ess-sas-file-path)
1152   (setq ess-sas-global-pc-keys t)
1153   (setq ess-sas-global-unix-keys nil)
1154   (setq ess-sas-local-pc-keys nil)
1155   (setq ess-sas-local-unix-keys nil)
1156 )
1157
1158 (defvar ess-sas-global-unix-keys nil
1159   "Non-nil if function keys use Unix-like SAS key definitions in all modes.")
1160
1161 (defun ess-sas-global-unix-keys ()
1162   "Unix/Mainframe-like SAS key definitions"
1163   (interactive)
1164   (when (featurep 'rtf-support)
1165   (global-set-key [(control f1)] 'ess-sas-rtf-portrait)
1166     (global-set-key [(control f2)] 'ess-sas-rtf-us-landscape))
1167   (global-set-key (quote [f2]) 'ess-revert-wisely)
1168   (global-set-key (quote [f3]) 'ess-sas-submit)
1169   (global-set-key [(control f3)] 'ess-sas-submit-region)
1170   (global-set-key (quote [f4]) 'ess-sas-goto-sas)
1171   (global-set-key (quote [f5]) 'ess-sas-goto-log)
1172   (global-set-key [(control f5)] 'ess-sas-append-log)
1173   (global-set-key (quote [f6]) 'ess-sas-goto-lst)
1174   (global-set-key [(control f6)] 'ess-sas-append-lst)
1175   (global-set-key (quote [f7]) 'ess-sas-goto-file-1)
1176   (global-set-key (quote [f8]) 'ess-sas-goto-shell)
1177   (global-set-key (quote [f9]) 'ess-sas-data-view-fsview)
1178   (global-set-key [(control f9)] 'ess-sas-data-view-insight)
1179   (global-set-key (quote [f10]) 'ess-sas-toggle-sas-log-mode)
1180   (global-set-key [(control f10)] 'ess-sas-toggle-sas-listing-mode)
1181   (global-set-key (quote [f11]) 'ess-sas-goto-file-2)
1182   (global-set-key [(control f11)] 'ess-ebcdic-to-ascii-search-and-replace)
1183   (global-set-key (quote [f12]) 'ess-sas-graph-view)
1184         (if (and ess-sas-edit-keys-toggle
1185             (equal emacs-major-version 19) (equal emacs-minor-version 28))
1186             (global-set-key [C-tab] 'ess-sas-backward-delete-tab)
1187             ;else
1188             (global-set-key [(control tab)] 'ess-sas-backward-delete-tab))
1189   ;(define-key sas-mode-local-map "\C-c\C-p" 'ess-sas-file-path) 
1190   (setq ess-sas-global-pc-keys nil)
1191   (setq ess-sas-global-unix-keys t)
1192   (setq ess-sas-local-pc-keys nil)
1193   (setq ess-sas-local-unix-keys nil)
1194 )
1195
1196 (defvar ess-sas-local-pc-keys nil
1197   "Non-nil if function keys use PC-like SAS key definitions
1198 in SAS-mode and related modes.")
1199
1200 (defun ess-sas-local-pc-keys ()
1201   "PC-like SAS key definitions."
1202   (interactive)
1203   (when (featurep 'rtf-support)
1204   (define-key sas-mode-local-map [(control f1)] 'ess-sas-rtf-portrait)
1205     (define-key sas-mode-local-map [(control f2)] 'ess-sas-rtf-us-landscape))
1206   (define-key sas-mode-local-map (quote [f2]) 'ess-revert-wisely)
1207   (define-key sas-mode-local-map (quote [f3]) 'ess-sas-goto-shell)
1208   (define-key sas-mode-local-map (quote [f4]) 'ess-sas-goto-file-1)
1209   (define-key sas-mode-local-map (quote [f5]) 'ess-sas-goto-sas)
1210   (define-key sas-mode-local-map (quote [f6]) 'ess-sas-goto-log)
1211   (define-key sas-mode-local-map [(control f6)] 'ess-sas-append-log)
1212   (define-key sas-mode-local-map (quote [f7]) 'ess-sas-goto-lst)
1213   (define-key sas-mode-local-map [(control f7)] 'ess-sas-append-lst)
1214   (define-key sas-mode-local-map (quote [f8]) 'ess-sas-submit)
1215   (define-key sas-mode-local-map [(control f8)] 'ess-sas-submit-region)
1216   (define-key sas-mode-local-map (quote [f9]) 'ess-sas-data-view-fsview)
1217   (define-key sas-mode-local-map [(control f9)] 'ess-sas-data-view-insight)
1218   (define-key sas-mode-local-map (quote [f10]) 'ess-sas-toggle-sas-log-mode)
1219   (define-key sas-mode-local-map [(control f10)] 'ess-sas-toggle-sas-listing-mode)
1220   (define-key sas-mode-local-map (quote [f11]) 'ess-sas-goto-file-2)
1221   (define-key sas-mode-local-map [(control f11)] 'ess-ebcdic-to-ascii-search-and-replace)
1222   (define-key sas-mode-local-map (quote [f12]) 'ess-sas-graph-view)
1223   ;(define-key sas-mode-local-map "\C-c\C-p" 'ess-sas-file-path) 
1224   (setq ess-sas-global-pc-keys nil)
1225   (setq ess-sas-global-unix-keys nil)
1226   (setq ess-sas-local-pc-keys t)
1227   (setq ess-sas-local-unix-keys nil)
1228 )
1229
1230 (defvar ess-sas-local-unix-keys nil
1231   "Non-nil if function keys use Unix-like SAS key definitions
1232 in SAS-mode and related modes.")
1233
1234 (defun ess-sas-local-unix-keys ()
1235   "Unix/Mainframe-like SAS key definitions"
1236   (interactive)
1237   (when (featurep 'rtf-support)
1238   (define-key sas-mode-local-map [(control f1)] 'ess-sas-rtf-portrait)
1239     (define-key sas-mode-local-map [(control f2)] 'ess-sas-rtf-us-landscape))
1240   (define-key sas-mode-local-map (quote [f2]) 'ess-revert-wisely)
1241   (define-key sas-mode-local-map (quote [f3]) 'ess-sas-submit)
1242   (define-key sas-mode-local-map [(control f3)] 'ess-sas-submit-region)
1243   (define-key sas-mode-local-map (quote [f4]) 'ess-sas-goto-sas)
1244   (define-key sas-mode-local-map (quote [f5]) 'ess-sas-goto-log)
1245   (define-key sas-mode-local-map [(control f5)] 'ess-sas-append-log)
1246   (define-key sas-mode-local-map (quote [f6]) 'ess-sas-goto-lst)
1247   (define-key sas-mode-local-map [(control f6)] 'ess-sas-append-lst)
1248   (define-key sas-mode-local-map (quote [f7]) 'ess-sas-goto-file-1)
1249   (define-key sas-mode-local-map (quote [f8]) 'ess-sas-goto-shell)
1250   (define-key sas-mode-local-map (quote [f9]) 'ess-sas-data-view-fsview)
1251   (define-key sas-mode-local-map [(control f9)] 'ess-sas-data-view-insight)
1252   (define-key sas-mode-local-map (quote [f10]) 'ess-sas-toggle-sas-log-mode)
1253   (define-key sas-mode-local-map [(control f10)] 'ess-sas-toggle-sas-listing-mode)
1254   (define-key sas-mode-local-map (quote [f11]) 'ess-sas-goto-file-2)
1255   (define-key sas-mode-local-map [(control f11)] 'ess-ebcdic-to-ascii-search-and-replace)
1256   (define-key sas-mode-local-map (quote [f12]) 'ess-sas-graph-view)
1257   ;(define-key sas-mode-local-map "\C-c\C-p" 'ess-sas-file-path) 
1258   (setq ess-sas-global-pc-keys nil)
1259   (setq ess-sas-global-unix-keys nil)
1260   (setq ess-sas-local-pc-keys nil)
1261   (setq ess-sas-local-unix-keys t)
1262 )
1263
1264 (provide 'essa-sas)
1265
1266 \f ; Local variables section
1267
1268 ;;; This file is automatically placed in Outline minor mode.
1269 ;;; The file is structured as follows:
1270 ;;; Chapters:     ^L ;
1271 ;;; Sections:    ;;*;;
1272 ;;; Subsections: ;;;*;;;
1273 ;;; Components:  defuns, defvars, defconsts
1274 ;;;              Random code beginning with a ;;;;* comment
1275
1276 ;;; Local variables:
1277 ;;; mode: emacs-lisp
1278 ;;; outline-minor-mode: nil
1279 ;;; mode: outline-minor
1280 ;;; outline-regexp: "\^L\\|\\`;\\|;;\\*\\|;;;\\*\\|(def[cvu]\\|(setq\\|;;;;\\*"
1281 ;;; End:
1282
1283 ;;; essa-sas.el ends here