Better handling of w32-shell-execute.
[ess.git] / lisp / essl-bug.el
blob5215fd126c2dc5d1f374006fc537f1d6c96fda47
1 ;;; essl-bug.el -- ESS BUGS customization
3 ;; Copyright (C) 2001 Rodney Sparapani
4 ;; Copyright (C) 2002--2004 Free Software Foundation, Inc.
5 ;; Copyright (C) 2002--2004 A.J. Rossini, Rich M. Heiberger, Martin
6 ;; Maechler, Kurt Hornik, Rodney Sparapani, and Stephen Eglen.
8 ;; Original Author: Rodney Sparapani <rsparapa@mcw.edu>
9 ;; Created: 27 February 2001
10 ;; Maintainers: ESS-core <ESS-core@stat.math.ethz.ch>
12 ;; Keywords: BUGS, bugs, BACKBUGS, backbugs.
14 ;; This file is part of ESS
16 ;; This file is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; any later version.
21 ;; This file is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING. If not, write to
28 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
30 ;; In short: you may use this code any way you like, as long as you
31 ;; don't charge money for it, remove this notice, or hold anyone liable
32 ;; for its results.
34 ;; Code:
36 (require 'font-lock)
37 (require 'comint)
38 (require 'ess-emcs)
40 (if (assoc "\\.[bB][uUoO][gG]\\'" auto-mode-alist) nil
41 (setq auto-mode-alist
42 (append
43 '(("\\.[bB][uUoO][gG]\\'" . ess-bugs-mode)
44 ("\\.[bB][mM][dD]\\'" . ess-bugs-mode))
45 auto-mode-alist)
49 (defgroup ess-bugs nil
50 "ESS: BUGS."
51 :group 'ess
52 :prefix "ess-")
54 (defcustom ess-bugs-batch-method
55 (if ess-microsoft-p
56 (if (w32-shell-dos-semantics) 'ms-dos 'sh)
57 (if (equal system-type 'Apple-Macintosh) 'apple-script 'sh))
58 "Method used by `ess-bugs-batch'.
59 The default is based on the value of the emacs variable `system-type'
60 and, on Windows machines, the function `w32-shell-dos-semantics'.
61 'ms-dos if *shell* follows MS-DOS semantics
62 'sh if *shell* runs sh, ksh, csh, tcsh or bash
63 'apple-script *shell* unavailable, use AppleScript
65 Windows users running MS-DOS in *shell* will get 'ms-dos by default.
67 Windows users running bash in *shell* will get 'sh by default.
69 Unix users will get 'sh by default.
71 Users whose default is not 'sh, but are accessing a remote machine with
72 `telnet', `rlogin', or `ssh', should have the following in ~/.emacs
73 (setq-default ess-bugs-batch-method 'sh)"
74 :group 'ess-bugs
77 (defcustom ess-bugs-batch-version "0.6"
78 "*ESS[BUGS]: The batch BUGS version to use."
79 :group 'ess-bugs
80 :type 'string
83 ;; SJE Thu 13 May 2004
84 ;; The "backbugs" scripts can be found in ess-etc-directory, so maybe we
85 ;; can use ess-etc-directory here too.
86 (defcustom ess-bugs-batch-command
87 (if (equal ess-bugs-batch-version "0.6") "backbugs" "backbug5")
88 "*ESS[BUGS]: The name of the command to run BUGS in batch mode.
90 Set to the name of the batch BUGS script that comes with ESS or
91 to the name of BUGS command. Make sure it is in your PATH or
92 add path to the command name."
93 :group 'ess-bugs
94 :type 'string
97 (defcustom ess-bugs-batch-post-command
98 (if (equal ess-bugs-batch-method 'sh) "&" " ")
99 "*ESS[BUGS]: Modifiers at the end of the batch BUGS command line."
100 :group 'ess-bugs
101 :type 'string
104 (defcustom ess-bugs-batch-pre-command
105 (if (equal ess-bugs-batch-method 'sh) "test -f nohup.out && rm -f nohup.out || true; nohup"
106 (if ess-microsoft-p "start"))
107 "*ESS[BUGS]: Modifiers at the beginning of the batch BUGS command line."
108 :group 'ess-bugs
109 :type 'string
112 (defcustom ess-bugs-default-bins "32"
113 "ESS[BUGS]: Number of bins to use in the Griddy algorithm (Metropolis sampling)."
114 :group 'ess-bugs
115 :type 'string
118 (defcustom ess-bugs-default-burn-in "500"
119 "ESS[BUGS]: Burn-in iterations to discard."
120 :group 'ess-bugs
121 :type 'string
124 (defcustom ess-bugs-default-update "1000"
125 "ESS[BUGS]: Iterations to store."
126 :group 'ess-bugs
127 :type 'string
130 (defcustom ess-bugs-default-checkpoint "100"
131 "ESS[BUGS]: Make a snapshot every this many iterations."
132 :group 'ess-bugs
133 :type 'string
136 (defvar ess-bugs-file "."
137 "ESS[BUGS]: BUGS file with PATH.")
139 (defvar ess-bugs-file-root "."
140 "ESS[BUGS]: Root of BUGS file.")
142 (defvar ess-bugs-file-suffix "."
143 "ESS[BUGS]: Suffix of BUGS file.")
145 (defvar ess-bugs-file-dir "."
146 "ESS[BUGS]: Directory of BUGS file.")
148 (defvar ess-bugs-file-data "..."
149 "ESS[BUGS]: BUGS data file.")
151 (defcustom ess-bugs-inits-suffix ".in"
152 "ESS[BUGS]: BUGS init file suffix."
153 :group 'ess-bugs
154 :type 'string
157 (defcustom ess-bugs-data-suffix ".dat"
158 "ESS[BUGS]: BUGS data file suffix."
159 :group 'ess-bugs
160 :type 'string
163 (defcustom ess-bugs-suffix-regexp
164 (concat "[.]\\([bB][oOuU][gG]\\|[bB][mM][dD]\\|"
165 (if ess-bugs-inits-suffix (concat
166 "\\|" (downcase ess-bugs-inits-suffix) "\\|" (upcase ess-bugs-inits-suffix)))
167 (if ess-bugs-data-suffix (concat
168 "\\|" (downcase ess-bugs-data-suffix) "\\|" (upcase ess-bugs-data-suffix)))
169 "\\)")
170 "*ESS[BUGS]: Regular expression for BUGS suffixes."
171 :group 'ess-bugs
172 :type 'string
175 (defcustom ess-bugs-mode-hook nil
176 "*ESS[BUGS]: List of functions to call upon entering mode."
177 :group 'ess-bugs
178 :type 'hook)
180 (defvar ess-bugs-monitor-vars " "
181 "ESS[BUGS]: List of BUGS variables to be written out to a file.")
183 (defvar ess-bugs-stats-vars " "
184 "ESS[BUGS]: List of BUGS variables to be summarized with statistics.")
186 (defvar ess-bugs-mode-map nil
187 "ESS[BUGS]: Keymap for mode.")
189 (if ess-bugs-mode-map nil
190 (setq ess-bugs-mode-map (make-keymap))
191 (define-key ess-bugs-mode-map (quote [f2]) 'ess-revert-wisely)
192 (define-key ess-bugs-mode-map (quote [f12]) 'ess-bugs-next-action)
196 (defvar ess-bugs-syntax-table nil
197 "ESS[BUGS]: Syntax table for mode.")
199 (if ess-bugs-syntax-table nil
200 (setq ess-bugs-syntax-table (make-syntax-table))
201 (modify-syntax-entry ?\\ "." ess-bugs-syntax-table)
202 (modify-syntax-entry ?# "<" ess-bugs-syntax-table)
203 (modify-syntax-entry ?\n ">" ess-bugs-syntax-table)
204 (modify-syntax-entry ?( "()" ess-bugs-syntax-table)
205 (modify-syntax-entry ?) ")(" ess-bugs-syntax-table)
206 (modify-syntax-entry ?. "w" ess-bugs-syntax-table)
210 (defvar ess-bugs-font-lock-keywords
211 (list
212 ;; .bug files
213 (cons "#.*\n" font-lock-comment-face)
215 (cons "^[ \t]*\\(model\\|const\\|data\\|inits\\|var\\)\\>"
216 font-lock-keyword-face)
218 (cons "\\<in[ \t]+[1-9]\\>" font-lock-keyword-face)
220 (cons (concat "\\<d\\(bern\\|beta\\|bin\\|cat\\|chisq\\|"
221 "dexp\\|dirch\\|exp\\|gamma\\|lnorm\\|logis\\|"
222 "mnorm\\|multi\\|negbin\\|norm\\|par\\|pois\\|"
223 "t\\|unif\\|weib\\|wish\\)[ \t\n]*(")
224 font-lock-reference-face)
226 (cons (concat "\\<\\(for\\|cloglog\\|equals\\|exp\\|inprod\\|"
227 "inverse\\|log\\(det\\|fact\\|gam\\|it\\)?\\|max\\|"
228 "mean\\|min\\|phi\\|pow\\|probit\\|sd\\|sqrt\\|"
229 "step\\|sum\\|I\\)[ \t\n]*(")
230 font-lock-function-name-face)
232 ;; .bmd files
233 (cons (concat "\\<\\(clear\\|checkpoint\\|compile\\|data\\|"
234 "diag\\|help\\|inits\\|iter\\|model\\|monitor\\|"
235 "out\\|q\\|save\\|stats\\|update\\)[ \t\n]*(")
236 font-lock-function-name-face)
238 ;; .dat files
239 ;;(cons (concat "\\<\\(c\\|list\\)[ \t\n]*(")
240 ;; font-lock-function-name-face)
242 "ESS[BUGS]: Font lock keywords."
246 (defun ess-bugs-file ()
247 "ESS[BUGS]: Set internal variables dealing with BUGS files.
248 Set `ess-bugs-file', `ess-bugs-file-root', `ess-bugs-file-suffix'
249 and `ess-bugs-file-dir'."
250 (let ((ess-bugs-temp-string (buffer-name)))
251 (setq ess-bugs-file (expand-file-name ess-bugs-temp-string))
252 (setq ess-bugs-file-dir
253 (convert-standard-filename (file-name-directory ess-bugs-file)))
254 (setq ess-bugs-file-root
255 (file-name-nondirectory (file-name-sans-extension ess-bugs-file)))
257 (if (fboundp 'file-name-extension)
258 (setq ess-bugs-file-suffix (file-name-extension ess-bugs-temp-string))
259 ;;else
260 (setq ess-bugs-file-suffix (car (last (split-string ess-bugs-temp-string "[.]")))))
262 (setq ess-bugs-file-suffix
263 (downcase (car (split-string (concat "." ess-bugs-file-suffix) "[<]"))))
265 (setq ess-bugs-file (concat ess-bugs-file-dir ess-bugs-file-root ess-bugs-file-suffix))
270 (defun ess-switch-to-suffix (suffix)
271 "ESS: Switch to file with suffix."
272 (find-file (concat ess-bugs-file-dir ess-bugs-file-root suffix))
274 (if (equal 0 (buffer-size)) (progn
275 (if (equal ".bug" suffix) (progn
276 (insert (concat "model %MODEL;\n"))
277 (insert (concat "const N = 0;#%N\n"))
278 (insert "var ;\n")
279 (insert "#%MONITOR;\n")
280 (insert "#%STATS;\n")
281 (insert (concat "data in \"%DATA\";\n"))
282 (insert (concat "inits in \"%INITS\";\n"))
283 (insert "{\n")
284 (insert " for (i in 1:N) {\n \n")
285 (insert " }\n")
286 (insert "}\n")
289 (if (equal ".bmd" suffix) (let
290 ((tmp-bugs-file-dir (if (equal ess-bugs-batch-version "0.6") ess-bugs-file-dir)))
291 (insert (concat "compile(\"" tmp-bugs-file-dir ess-bugs-file-root ".bug\")\n"))
292 (insert (concat "save(\"" tmp-bugs-file-dir ess-bugs-file-root ".in0\")\n"))
293 (insert (concat "update(" ess-bugs-default-burn-in ")\n"))
294 (insert (concat "save(\"" tmp-bugs-file-dir ess-bugs-file-root ".in1\")\n"))
295 (insert "#%MONITOR\n\n#%MONITOR\n")
296 (if (equal ess-bugs-batch-version "0.6")
297 (insert (concat "checkpoint(" ess-bugs-default-checkpoint ")\n")))
298 (insert (concat "update(" ess-bugs-default-update ")\n"))
299 (insert (concat "save(\"" tmp-bugs-file-dir ess-bugs-file-root ".in2\")\n"))
300 (insert "#%STATS\n\n#%STATS\n")
301 (insert "q()\n")
302 ;;(insert "q(\"" ess-bugs-file-dir ess-bugs-file-root ".bog\")\n")
307 (defun ess-exit-notify-sh (string)
308 "Detect completion or failure of submitted job and notify the user."
309 (let* ((exit-done "\\[[0-9]+\\]\\ *\\+*\\ *\\(Exit\\|Done\\).*$")
310 (beg (string-match exit-done string)))
311 (if beg (message (substring string beg (match-end 0))))))
313 (defun ess-bugs-next-action ()
314 "ESS[BUGS]: Perform the appropriate next action."
315 (interactive)
316 (ess-bugs-file)
318 (if (equal ".bug" ess-bugs-file-suffix) (ess-bugs-na-bug))
319 ;;else
320 (if (equal ".bmd" ess-bugs-file-suffix) (ess-bugs-na-bmd))
323 (defun ess-bugs-na-bmd ()
324 "ESS[BUGS]: Perform the Next-Action for .bmd."
326 (save-buffer)
327 (shell)
329 (if (w32-shell-dos-semantics)
330 (if (string-equal ":" (substring ess-bugs-file 1 2))
331 (progn
332 (insert (substring ess-bugs-file 0 2))
333 (comint-send-input)
338 (insert (concat "cd \"" ess-bugs-file-dir "\""))
339 (comint-send-input)
341 (insert (concat ess-bugs-batch-pre-command " " ess-bugs-batch-command " "
342 (if (equal ess-bugs-batch-version "0.6") ess-bugs-default-bins)
343 " " ess-bugs-file-root " "
344 (if (equal ess-bugs-batch-version "0.6")
345 ess-bugs-file (concat ess-bugs-file-root ".bmd"))
346 " " ess-bugs-batch-post-command))
348 (comint-send-input)
352 (defun ess-bugs-na-bug ()
353 "ESS[BUGS]: Perform Next-Action for .bug"
355 (if (equal 0 (buffer-size)) (ess-switch-to-suffix ".bug")
356 (save-excursion (let
357 ((tmp-bugs-file-dir (if (equal ess-bugs-batch-version "0.6") ess-bugs-file-dir)))
358 (goto-char (point-min))
360 (if (search-forward "%MODEL" nil t)
361 (replace-match ess-bugs-file-root t t))
363 (if (search-forward "%DATA" nil t) (progn
364 (setq ess-bugs-file-data
365 (concat tmp-bugs-file-dir ess-bugs-file-root ess-bugs-data-suffix))
366 (replace-match ess-bugs-file-data t t))
367 ;;else
368 (if (search-forward-regexp "data.+in[ \t\n]+\"\\(.*\\)\"" nil t)
369 (setq ess-bugs-file-data (match-string 1))
370 ;;else
371 (setq ess-bugs-file-data "...")
374 (if (search-forward "%INITS" nil t)
375 (replace-match
376 (concat tmp-bugs-file-dir ess-bugs-file-root ess-bugs-inits-suffix) t t))
378 (let ((ess-bugs-temp-string " ")
379 (ess-bugs-buffer-ptr nil))
380 (goto-char (point-min))
382 (if (search-forward-regexp
383 "N[ \t]*=[ \t]*[0-9]+[ \t]*;[ \t]*#[ \t]*%N" nil t) (progn
385 (save-excursion (save-match-data
386 (setq ess-bugs-buffer-ptr (find-buffer-visiting ess-bugs-file-data))
388 (if ess-bugs-buffer-ptr (set-buffer ess-bugs-buffer-ptr)
389 (set-buffer (create-file-buffer ess-bugs-file-data))
390 (insert-file-contents ess-bugs-file-data t))
392 (setq ess-bugs-temp-string
393 (concat "N = "
394 (int-to-string (count-lines (point-min) (point-max))) ";#%N"))
397 (replace-match ess-bugs-temp-string t t)
401 (let (
402 (ess-bugs-search-min nil)
403 (ess-bugs-search-max nil)
404 (ess-bugs-search-vars
405 "\\([a-zA-Z0-9.]+\\)\\(\\(\\[\\)[a-zA-Z0-9]*\\(,\\)?[a-zA-Z0-9]*\\(\\]\\)\\)?[ \t]*[,]?[ \t]*\\(#.*\\)?[\n]?"
408 (goto-char (point-min))
410 (if (search-forward-regexp "%MONITOR[ \t]+" nil t)
411 (setq ess-bugs-search-min (point))
412 ;;else
413 (setq ess-bugs-search-min (search-forward "var"))
416 (setq ess-bugs-search-max (search-forward-regexp ";"))
418 (goto-char ess-bugs-search-min)
419 (setq ess-bugs-monitor-vars "")
421 (while (search-forward-regexp ess-bugs-search-vars ess-bugs-search-max t)
423 (setq ess-bugs-monitor-vars
424 (concat ess-bugs-monitor-vars "monitor("
425 (match-string 1) (match-string 3) (match-string 4) (match-string 5) ")\n"))
428 (setq ess-bugs-monitor-vars
429 (concat "#%MONITOR\n" ess-bugs-monitor-vars "#%MONITOR\n"))
431 (goto-char (point-min))
433 (if (search-forward-regexp "%STATS[ \t]+" nil t) (progn
434 (setq ess-bugs-search-min (point))
435 (setq ess-bugs-search-max (search-forward-regexp ";"))
437 (goto-char ess-bugs-search-min)
438 (setq ess-bugs-stats-vars "")
440 (while (search-forward-regexp ess-bugs-search-vars ess-bugs-search-max t)
442 (setq ess-bugs-stats-vars
443 (concat ess-bugs-stats-vars "stats("
444 (match-string 1) (match-string 3) (match-string 4) (match-string 5) ")\n"))
447 (setq ess-bugs-stats-vars (concat "#%STATS\n" ess-bugs-stats-vars "#%STATS\n"))
450 ;; replace-in-string may not be available, work-around necessary; see below
451 ;; (setq ess-bugs-stats-vars
452 ;; (replace-in-string ess-bugs-monitor-vars "#%MONITOR" "#%STATS"))
453 ;; (setq ess-bugs-stats-vars
454 ;; (replace-in-string ess-bugs-stats-vars "monitor" "stats" t))
456 ;;else
457 (setq ess-bugs-stats-vars ess-bugs-monitor-vars)
459 (while (string-match "#%MONITOR" ess-bugs-stats-vars)
460 (setq ess-bugs-stats-vars
461 (replace-match "#%STATS" t t ess-bugs-stats-vars)))
463 (while (string-match "monitor" ess-bugs-stats-vars)
464 (setq ess-bugs-stats-vars
465 (replace-match "stats" t t ess-bugs-stats-vars)))
472 (save-buffer)
473 (ess-switch-to-suffix ".bmd")
475 (save-excursion
476 (goto-char (point-min))
478 (if (search-forward-regexp "#%MONITOR\\(.\\|\n\\)*#%MONITOR\n" nil t)
479 (replace-match ess-bugs-monitor-vars t))
481 (if (search-forward-regexp "#%STATS\\(.\\|\n\\)*#%STATS\n" nil t)
482 (replace-match ess-bugs-stats-vars t))
489 (defun ess-bugs-mode ()
490 "ESS[BUGS]: Major mode for Classic BUGS."
491 (interactive)
492 (kill-all-local-variables)
493 (setq major-mode 'ess-bugs-mode)
494 (setq mode-name "ESS[BUGS]")
495 (use-local-map ess-bugs-mode-map)
496 (setq font-lock-auto-fontify t)
497 (make-local-variable 'font-lock-defaults)
498 (setq font-lock-defaults '(ess-bugs-font-lock-keywords nil t))
499 (run-hooks 'ess-bugs-mode-hook)
501 (if (not (w32-shell-dos-semantics))
502 (add-hook 'comint-output-filter-functions 'ess-exit-notify-sh))
507 ;;; ESS[BUGS-Shell] for running BUGS interactively
508 (defgroup ess-bugs-shell nil
509 "ESS: BUGS-Shell."
510 :group 'ess-bugs
511 :prefix "ess-")
513 (defcustom ess-bugs-shell-buffer-name "BUGS"
514 "*ESS[BUGS-Shell]: The name of the BUGS-Shell buffer."
515 :group 'ess-bugs-shell
516 :type 'string)
518 (defcustom ess-bugs-shell-command "bugs"
519 "*ESS[BUGS-Shell]: The name of the command to run BUGS interactively.
521 Set to the name of the batch BUGS script that comes with ESS or
522 to the name of BUGS command. Make sure it is in your PATH or
523 add path to the command name."
524 :group 'ess-bugs-shell
525 :type 'string)
527 (defcustom ess-bugs-shell-default-output-file-root "bugs"
528 "*ESS[BUGS-Shell]: Default value for the root of output files."
529 :group 'ess-bugs-shell
530 :type 'string)
532 (defcustom ess-bugs-shell-mode-hook nil
533 "*ESS[BUGS-Shell]: List of functions to call upon entering mode."
534 :group 'ess-bugs-shell
535 :type 'hook)
537 (defun ess-bugs-shell ()
538 "Create a buffer with BUGS running as a subprocess."
539 (interactive)
540 (require 'shell)
541 (switch-to-buffer (concat "*" ess-bugs-shell-buffer-name "*"))
542 (make-comint ess-bugs-shell-buffer-name ess-bugs-shell-command nil
543 ess-bugs-default-bins ess-bugs-shell-default-output-file-root)
544 (comint-mode)
545 (setq shell-dirtrackp t
546 major-mode 'bugs-shell-mode
547 mode-name "ESS[BUGS-Shell]"
548 comint-prompt-regexp "^Bugs> *")
549 (make-local-variable 'font-lock-defaults)
550 (setq font-lock-defaults '(ess-bugs-font-lock-keywords nil t))
551 (run-hooks 'ess-bugs-shell-mode-hook)
554 (provide 'essl-bug)