ESS[SAS]: somebody forgot about the SUM statement (probably me)
[ess.git] / lisp / essd-sas.el
blobfb55b93c04880f94f32e7b333fac02e3fe661b48
1 ;;; essd-sas.el --- SAS customization
3 ;; Copyright (C) 1997--2001 Richard M. Heiberger and A. J. Rossini
4 ;; Copyright (C) 2002--2004 A.J. Rossini, Rich M. Heiberger, Martin
5 ;; Maechler, Kurt Hornik, Rodney Sparapani, and Stephen Eglen.
7 ;; Original Author: Richard M. Heiberger <rmh@astro.ocis.temple.edu>
8 ;; Created: 20 Aug 1997
9 ;; Maintainers: ESS-core <ESS-core@stat.math.ethz.ch>
11 ;; Keywords: start up, configuration.
13 ;; This file is part of ESS.
15 ;; This file is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
20 ;; This file is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to
27 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
29 ;;; Commentary:
30 ;; This file defines all the SAS customizations for ESS behaviors. See
31 ;; essl-sas and essa-sas for the underlying general modifications.
34 ;;; Autoloads:
36 (ess-message "[essd-sas:] require 'comint & 'shell ...")
37 (require 'comint)
38 (require 'shell)
39 (require 'executable)
41 ;;(ess-message "[essd-sas:] require 'essa-sas ...")
42 ;;(require 'essa-sas)
43 (ess-message "[essd-sas:] require 'essl-sas ...")
44 (require 'essl-sas)
45 (ess-message "[essd-sas:] (autoload ..) (def** ..) ...")
48 (autoload 'inferior-ess "ess-inf" no-doc t)
49 (autoload 'ess-mode "ess-mode" no-doc t)
50 (autoload 'ess-proc-name "ess-inf" no-doc nil)
52 (defvar inferior-SAS-args "-stdio -linesize 80 -noovp -nosyntaxcheck"
53 "*Arguments to use for starting SAS.")
55 (defvar inferior-SAS-args-temp nil
56 "Hack variable, needed for args preprocessing.
57 Better logic needed! (see 2 uses, in this file).")
59 ;;; Code:
61 (defun ess-SAS-pre-run-hook (temp-ess-dialect)
62 "Set up log and list files for interactive SAS."
64 (let* ((ess-shell-buffer-name-flag (get-buffer "*shell*"))
65 ess-shell-buffer-name
66 ;; isn't pretty yet.
67 ;; ess-local-process-name is defined after this function.
68 ;; it needs to be defined prior to this function.
69 (tmp-procname (let ((ntry 0)
70 (done nil))
71 ;; find a non-existent process
72 (while (not done)
73 (setq ntry (1+ ntry)
74 done (not
75 (get-process (ess-proc-name
76 ntry
77 temp-ess-dialect)))))
78 (ess-proc-name ntry temp-ess-dialect)))
79 ;; Following was tmp-local-process-name. Stolen from inferior-ess
80 (ess-sas-lst-bufname (concat "*" tmp-procname ".lst*"))
81 (ess-sas-log-bufname (concat "*" tmp-procname ".log*"))
82 (explicit-shell-file-name "/bin/sh")
83 inferior-SAS-redirect-args
84 ess-sas-lst
85 ess-sas-log)
87 (ess-write-to-dribble-buffer
88 (format "(ess-SAS-pre-run-hook 1): ess-lang=%s, ess-dialect=%s, temp-dialect=%s, buf=%s \n"
89 ess-language
90 ess-dialect
91 temp-ess-dialect
92 (current-buffer)))
93 ;; If someone is running a *shell* buffer, rename it to avoid
94 ;; inadvertent nuking.
95 (if ess-shell-buffer-name-flag
96 (save-excursion
97 (set-buffer "*shell*")
98 (setq ess-shell-buffer-name
99 (rename-buffer "*ess-shell-regular*" t))))
101 ;; Construct the LST buffer for output
102 (if (get-buffer ess-sas-lst-bufname)
104 (shell)
105 (accept-process-output (get-buffer-process (current-buffer)))
106 (sleep-for 2) ; need to wait, else working too fast!
107 (setq ess-sas-lst (ess-insert-accept "tty"))
108 (SAS-listing-mode)
109 (shell-mode)
110 (ess-listing-minor-mode t)
111 (rename-buffer ess-sas-lst-bufname t))
113 ;; Construct the LOG buffer for output
114 (if (get-buffer ess-sas-log-bufname)
116 (shell)
117 (accept-process-output (get-buffer-process (current-buffer)))
118 (sleep-for 2) ; need to wait, else working too fast!
119 (setq ess-sas-log (ess-insert-accept "tty"))
120 ;(SAS-log-mode)
121 (shell-mode)
122 (ess-transcript-minor-mode t)
123 (rename-buffer ess-sas-log-bufname t))
125 (setq inferior-SAS-redirect-args (concat " "
126 ess-sas-lst
128 ess-sas-log
129 " ")
130 inferior-SAS-args-temp (concat inferior-SAS-redirect-args
131 inferior-SAS-args))
133 ;; Restore the *shell* buffer
134 (if ess-shell-buffer-name-flag
135 (save-excursion
136 (set-buffer ess-shell-buffer-name)
137 (rename-buffer "*shell*")))
139 (delete-other-windows)
140 (split-window-vertically)
141 (split-window-vertically)
142 (switch-to-buffer (nth 2 (buffer-list)))
143 (other-window 2)
144 (switch-to-buffer ess-sas-log-bufname)
145 (split-window-vertically)
146 (other-window 1)
147 (switch-to-buffer ess-sas-lst-bufname)
148 (other-window 2)
150 ;;workaround
151 (setq inferior-SAS-program-name
152 (concat ess-etc-directory "ess-sas-sh-command"))
153 (setq inferior-ess-program inferior-SAS-program-name)))
155 (defun ess-insert-accept (command)
156 "Submit command to process, get next line."
157 (interactive)
158 (goto-char (point-max))
159 (insert command)
160 (comint-send-input)
161 (accept-process-output (get-buffer-process (current-buffer)))
162 (forward-line -1)
163 (let* ((beg (point))
164 (ess-tty-name (progn (end-of-line) (buffer-substring beg (point)))))
165 (goto-char (point-max))
166 ess-tty-name))
169 (defvar SAS-customize-alist
170 '((ess-local-customize-alist . 'SAS-customize-alist)
171 (ess-language . "SAS")
172 (ess-dialect . "SAS")
173 (ess-mode-editing-alist . SAS-editing-alist) ; from essl-sas.el
174 (ess-mode-syntax-table . SAS-syntax-table)
175 (inferior-ess-program . inferior-SAS-program-name)
176 (ess-help-sec-regex . "^[A-Z. ---]+:$")
177 (ess-help-sec-keys-alist . " ")
178 (ess-object-name-db-file . "ess-sas-namedb.el")
179 (inferior-ess-objects-command . "objects(%d)");;FIXME
180 (inferior-ess-help-command . "help(\"%s\",pager=\"cat\",window=F)\n");;FIXME
181 (inferior-ess-exit-command . "endsas;\n")
182 (ess-loop-timeout . 500000 )
183 (inferior-ess-primary-prompt . "^")
184 (inferior-ess-secondary-prompt . "^")
185 (comint-use-prompt-regexp-instead-of-fields . t) ;; emacs 21 and up
186 (inferior-ess-start-file . nil) ;"~/.ess-SAS")
187 (inferior-ess-start-args . inferior-SAS-args-temp)
188 (inferior-ess-font-lock-keywords . SAS-mode-font-lock-keywords)
189 ;; (ess-pre-run-hook . 'ess-SAS-pre-run-hook)
190 (ess-local-process-name . nil))
191 "Variables to customize for SAS")
193 ;;; The functions of interest (mode, inferior mode)
195 (defvar sas-mode-local-map nil "contains modified local keymap for SAS")
197 (defun SAS-mode (&optional proc-name)
198 "Major mode for editing SAS source. See ess-mode for more help."
199 (interactive)
200 (setq ess-customize-alist SAS-customize-alist)
201 (ess-mode SAS-customize-alist proc-name)
203 ;; Local map settings, AFTER initialization (only if not yet defined)
204 (if sas-mode-local-map
206 (setq sas-mode-local-map (copy-keymap (current-local-map)))
207 (ess-sas-edit-keys-set ess-sas-edit-keys-toggle)
208 (if ess-sas-local-unix-keys (ess-sas-local-unix-keys))
209 (if ess-sas-local-pc-keys (ess-sas-local-pc-keys))
210 (if ess-sas-global-unix-keys (ess-sas-global-unix-keys))
211 (if ess-sas-global-pc-keys (ess-sas-global-pc-keys)))
212 (define-key sas-mode-local-map "\C-ci" 'ess-eval-line-and-step-invisibly)
213 (define-key sas-mode-local-map ";" 'ess-electric-run-semicolon)
215 ; this is a mess
216 ; interactive and batch commands share sas-mode-local-map,
217 ; but the associated commands are very different
218 ; what would be better is two maps like
219 ; sas-batch-mode-local-map and sas-interactive-mode-local-map
220 ; or smart function definitions that would do the appropriate
221 ; thing for either batch or interactive sessions
222 ; however, neither of these solutions are planned
223 ; therefore, no key definitions can be shared between
224 ; batch and interactive at this time, hence the lines that
225 ; are commented below: uncomment at your own risk
226 ; (define-key sas-mode-local-map "\C-c\C-p" 'ess-sas-file-path)
227 ; (define-key sas-mode-local-map "\C-c\C-b" 'ess-sas-submit)
228 ; (define-key sas-mode-local-map "\C-c\C-r" 'ess-sas-submit-region)
229 ; (define-key sas-mode-local-map "\C-c\C-x" 'ess-sas-goto-log)
230 ; (define-key sas-mode-local-map "\C-c\C-y" 'ess-sas-goto-lst)
232 (use-local-map sas-mode-local-map))
234 ;; rmh Jul 10 2003
235 (defun ess-electric-run-semicolon (arg)
236 "Insert character. If the line contains \"run;\" and nothing else then indent line."
237 (interactive "P")
238 (if ess-sas-edit-keys-toggle (insert ";") (let (insertpos)
239 (if (and (not arg)
240 (eolp)
241 (save-excursion
242 (skip-chars-backward " \t")
243 (backward-word 1)
244 (and (looking-at "run")
245 (progn
246 (skip-chars-backward " \t")
247 (bolp)))))
248 (progn
249 (insert last-command-char)
250 (ess-indent-line)
251 (save-excursion
252 (if insertpos (goto-char (1+ insertpos)))
253 (delete-char -1))))
254 (if insertpos
255 (save-excursion
256 (goto-char insertpos)
257 (self-insert-command (prefix-numeric-value arg)))
258 (self-insert-command (prefix-numeric-value arg))))))
260 (defun SAS ()
261 "Call 'SAS', from SAS Institute."
262 (interactive)
263 (setq-default ess-customize-alist SAS-customize-alist)
264 (let* ((temp-dialect "SAS")) ;(cdr (rassoc ess-dialect SAS-customize-alist))))
265 (ess-write-to-dribble-buffer
266 (format "(SAS): ess-dial=%s, temp-dial=%s\n"
267 ess-dialect
268 temp-dialect))
269 (ess-SAS-pre-run-hook temp-dialect)
270 (inferior-ess)
271 (save-excursion
272 (set-buffer "*SAS*")
273 (use-local-map sas-mode-local-map))))
276 (defun ess-multi-frame-SAS ()
277 "Put running SAS buffers into separate frames.
278 Load this function M-x load-file essx-sas.el RET.
279 Then find-file myfile.sas. If myfile.sas is already in a buffer, kill-buffer
280 it and then find-file it again.
281 Place the cursor in a myfile.sas buffer. Run SAS with M-x SAS,
282 Return the cursor to the myfile.sas buffer,
283 then enter C-c C-w to put *SAS* *SAS.log* *SAS.lst* buffers into
284 their own frames."
285 (interactive)
286 (delete-other-windows)
287 (save-excursion
288 (set-buffer "*SAS*")
289 (make-frame)
290 (set-buffer "*SAS.log*")
291 (make-frame)
292 (set-buffer "*SAS.lst*")
293 (make-frame)))
296 (define-key ess-mode-map "\C-c\C-w" 'ess-multi-frame-SAS)
298 \f ; Provide package
300 (provide 'essd-sas)
302 \f ; Local variables section
304 ;;; This file is automatically placed in Outline minor mode.
305 ;;; The file is structured as follows:
306 ;;; Chapters: ^L ;
307 ;;; Sections: ;;*;;
308 ;;; Subsections: ;;;*;;;
309 ;;; Components: defuns, defvars, defconsts
310 ;;; Random code beginning with a ;;;;* comment
312 ;;; Local variables:
313 ;;; mode: emacs-lisp
314 ;;; outline-minor-mode: nil
315 ;;; mode: outline-minor
316 ;;; outline-regexp: "\^L\\|\\`;\\|;;\\*\\|;;;\\*\\|(def[cvu]\\|(setq\\|;;;;\\*"
317 ;;; End:
319 ;;; essd-sas.el ends here