*** empty log message ***
[ess.git] / lisp / ess-inf.el
blobb7c0e1f98a1ad0e5cc84e2e83243ebdc5abab537
1 ;;; ess-inf.el --- Support for running S as an inferior Emacs process
3 ;; Copyright (C) 1989-1994 Bates, Kademan, Ritter and Smith
4 ;; Copyright (C) 1997-1999 A.J. Rossini <rossini@u.washington.edu>,
5 ;; Martin Maechler <maechler@stat.math.ethz.ch>.
6 ;; Copyright (C) 2000--2006 A.J. Rossini, Rich M. Heiberger, Martin
7 ;; Maechler, Kurt Hornik, Rodney Sparapani, and Stephen Eglen.
9 ;; Original Author: David Smith <dsmith@stats.adelaide.edu.au>
10 ;; Created: 7 Jan 1994
11 ;; Maintainers: ESS-core <ESS-core@stat.math.ethz.ch>
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:
31 ;; Code for handling running ESS processes.
33 ;;; Code:
35 \f ; Requires and autoloads
37 ;;*;; Requires
38 (require 'ess-site)
40 ;; Byte-compiler, SHUT-UP!
41 (eval-and-compile
42 (require 'comint)
43 (require 'ess-utils))
45 ;;*;; Autoloads
46 (autoload 'ess-parse-errors "ess-mode" "(autoload)." t)
47 (autoload 'ess-dump-object-into-edit-buffer "ess-mode" "(autoload)." t)
48 (autoload 'ess-beginning-of-function "ess-mode" "(autoload)." t)
49 (autoload 'ess-end-of-function "ess-mode" "(autoload)." t)
50 (autoload 'ess-display-help-on-object "ess-help" "(autoload)." t)
52 (autoload 'ess-extract-word-name "ess-utils" "(autoload)." t)
53 (autoload 'ess-uniq-list "ess-utils" "(autoload)." t)
55 (autoload 'ess-transcript-send-command-and-move "ess-trns" "(autoload)." t)
57 (autoload 'ess-eval-region-ddeclient "ess-dde" "(autoload)." nil)
58 (autoload 'ess-eval-linewise-ddeclient "ess-dde" "(autoload)." nil)
59 (autoload 'ess-load-file-ddeclient "ess-dde" "(autoload)." nil)
60 (autoload 'ess-command-ddeclient "ess-dde" "(autoload)." nil)
62 \f ;;*;; Process handling
64 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
65 ;;; In this section:
66 ;;;
67 ;;; * User commands for starting an ESS process
68 ;;; * Functions called at startup
69 ;;; * Process handling code
70 ;;; * Multiple process implementation
71 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
73 ;;*;; Starting a process
75 (defun ess-proc-name (n name)
76 "Return name of process N, as a string, with NAME prepended.
77 If ess-plain-first-buffername, then initial process is number-free."
78 (concat name
79 (if (not (and ess-plain-first-buffername
80 (= n 1))) ; if not both first and plain-first add number
81 (concat ":" (number-to-string n)))))
83 (defun inferior-ess (&optional ess-start-args)
84 "Start inferior ESS process.
86 Without a prefix argument, starts a new ESS process, or switches
87 to the ESS process associated with the current buffer.
88 With a prefix, starts the process with those args.
89 The current buffer is used if it is an `inferior-ess-mode'
90 or `ess-transcript-mode' buffer.
92 If `ess-ask-about-transfile' is non-nil, you will be asked for a
93 transcript file to use. If there is no transcript file, the buffer
94 name will be like *S* or *S2*.
96 Takes the program name from the variable `inferior-ess-program'.
97 An initialization file (dumped into the process) is specified by
98 `inferior-ess-start-file', and `inferior-ess-start-args' is used to
99 accompany the call for `inferior-ess-program'.
101 When creating a new process, the process buffer replaces the
102 current window if `inferior-ess-same-window' is non-nil.
103 Alternatively, it can appear in its own frame if
104 `inferior-ess-own-frame' is non-nil.
106 \(Type \\[describe-mode] in the process buffer for a list of commands.)"
108 ;; Use the current buffer if it is in inferior-ess-mode or ess-trans-mode
109 ;; If not, maybe ask about starting directory and/or transcript file.
110 ;; If no transfile, use buffer *S*
112 ;; This function is primarily used to figure out the Process and
113 ;; buffer names to use for inferior-ess.
115 ;; Once, long ago, it was used for switching buffers, but we don't
116 ;; do that any more (at least not from here).
118 (interactive)
120 (ess-write-to-dribble-buffer
121 (format "(inferior-ess 0): ess-start-args=%s \n" ess-start-args))
123 ;; set up for current language (need here, to get ess-language, etc).
125 ;; Couldn't we rather set all the default values or Local values now ?
126 ;;>>> (ess-setq-vars-default ess-customize-alist (current-buffer))
127 ;;>>> (ess-setq-vars-local ess-customize-alist (current-buffer))
128 ;;; AJR sez: I think we should set them later; don't want to nuke if
129 ;;; I don't have to.
130 ;;- MM: We shouldn't have to use ess-setq-vars-default _at all_ ;
131 ;; only do the buffer local ...-vars-local ones
132 (let ((temp-ess-dialect (eval (cdr (assoc 'ess-dialect
133 ess-customize-alist))))
134 (temp-ess-lang (eval (cdr (assoc 'ess-language
135 ess-customize-alist)))))
136 (save-excursion
137 ;;- Is this needed? (no, but it's useful to see them there [MM])
138 (set-buffer ess-dribble-buffer)
139 ;; Hack to work around the following "default" (global) setting of vars:
140 ;; make sure our comint-... hack doesn't affect anything else
141 (make-variable-buffer-local 'comint-use-prompt-regexp-instead-of-fields)
142 ;; now the abomination:
143 (ess-setq-vars-default ess-customize-alist)
145 ;; Emacs 22.0.50: ".. obsolete since 22.1;
146 ;; use `comint-use-prompt-regexp' instead
147 (setq-default comint-use-prompt-regexp-instead-of-fields nil) ; re set HACK!
148 ;;>> Doesn't set ess-language,
149 ;;>> => comint-input-sender is not set to 'ess-input- ==> no input echo!
150 ;;>> => that's why things fail:
151 ;;>> (ess-setq-vars-local ess-customize-alist (current-buffer))
152 ;; ======
155 ;; run hooks now, to overwrite the above!
156 (run-hooks 'ess-pre-run-hook)
157 (ess-write-to-dribble-buffer
158 (format "(inf-ess 1): lang=%s, dialect=%s, tmp-dialect=%s, buf=%s\n"
159 ess-language ess-dialect temp-ess-dialect (current-buffer)))
160 (let* ((process-environment process-environment)
161 (defdir (or (and ess-directory-function (funcall ess-directory-function))
162 ess-directory default-directory))
163 (temp-dialect (if ess-use-inferior-program-name-in-buffer-name
164 (if (string-equal temp-ess-dialect "R")
165 inferior-R-program-name
166 temp-ess-dialect) ; use temp-ess-dialect
167 ; if not R, R program name
168 ; otherwise.
169 temp-ess-dialect))
170 (temp-lang temp-ess-lang)
171 (procname (let ((ntry 0) ;; find a non-existent process
172 (done nil))
173 (while (not done)
174 (setq ntry (1+ ntry)
175 done (not
176 (get-process (ess-proc-name
177 ntry
178 temp-dialect)))))
179 (ess-proc-name ntry temp-dialect)))
180 (startdir nil)
181 (buf nil)
182 (buf-name-str (concat "*" procname "*")))
184 (ess-write-to-dribble-buffer
185 (format "(inf-ess 1.1): procname=%s temp-dialect=%s, buf-name=%s \n"
186 procname
187 temp-dialect
188 buf-name-str))
189 (cond
190 ;; Since it's a new or terminated process, try to use current buffer
191 ((and (not buf)
192 (not (comint-check-proc (current-buffer)))
193 (memq major-mode '(inferior-ess-mode)))
194 (setq startdir
195 (if ess-ask-for-ess-directory
196 (ess-get-directory (directory-file-name defdir))
197 defdir))
198 (setq buf (current-buffer))
199 (ess-write-to-dribble-buffer
200 (format "(inferior-ess) Method #1 start=%s buf=%s\n" startdir buf)))
202 ;; Not an ESS buffer yet
203 ((and (not buf)
204 (get-buffer buf-name-str))
205 (setq buf (get-buffer buf-name-str))
206 (ess-write-to-dribble-buffer
207 (format "(inferior-ess) Method #2 buf=%s\n" buf)))
209 ;; Ask for transcript file and startdir
210 ;; FIXME -- this should be in ess-get-transfile
211 ;; AJR: Why? I'm not clear about the logic, i.e. when would it
212 ;; be used again, to justify?
213 ((not buf)
214 (setq startdir
215 (if ess-ask-for-ess-directory
216 (ess-get-directory (directory-file-name defdir))
217 defdir))
218 (if ess-ask-about-transfile
219 (let ((transfilename (read-file-name
220 "Use transcript file (default none):"
221 startdir "")))
222 (setq buf (if (string= transfilename "")
223 (get-buffer-create buf-name-str)
224 (find-file-noselect (expand-file-name
225 transfilename)))))
226 (setq buf (get-buffer-create buf-name-str)))
227 (ess-write-to-dribble-buffer
228 (format "(inferior-ess) Method #3 start=%s buf=%s\n" startdir buf))))
230 (set-buffer buf)
231 ;; Now that we have the buffer, set buffer-local variables.
232 (ess-setq-vars-local ess-customize-alist) ; buf)
233 (if ess-start-args (setq inferior-ess-start-args ess-start-args))
234 ;; Was: if not, set to null.
235 ;;(setq inferior-ess-start-args "")) ;; AJR: Errors with XLS?
236 ;; I think I might have solved this?
238 ;; Write out debug info
239 (ess-write-to-dribble-buffer
240 (format "(inf-ess 2.1): ess-language=%s, ess-dialect=%s buf=%s \n"
241 ess-language
242 ess-dialect
243 (current-buffer)))
244 (ess-write-to-dribble-buffer
245 (format "(inf-ess 2.2): start args = %s, inf-ess-start-args=%s \n"
246 ess-start-args
247 inferior-ess-start-args))
248 (ess-write-to-dribble-buffer
249 (format "(inf-ess finish [%s(%s), %s(%s,%s)]\n"
250 ess-language
251 ess-dialect
252 inferior-ess-program
253 ess-current-process-name
254 ess-local-process-name))
256 ;; Start from the "right" directory
257 (if startdir (setq default-directory startdir))
258 ;; Set up history
259 (setq-default ess-history-file
260 (concat "." ess-dialect "history"))
261 ;; initialize.
262 (let ((ess-directory (if startdir default-directory ess-directory)))
263 (ess-multi procname buf inferior-ess-start-args)))))
266 (defvar inferior-ess-objects-command nil
267 "The language/dialect specific command for listing objects.
268 It is initialized from the corresponding inferior-<lang>-objects-command
269 and then made buffer local."); and the *-<lang>-* ones are customized!
270 (make-variable-buffer-local 'inferior-ess-objects-command)
272 (defvar ess-save-lastvalue-command nil
273 "The command to save the last value. See S section for more details.
274 Default depends on the ESS language/dialect and hence made buffer local")
275 (make-variable-buffer-local 'ess-save-lastvalue-command)
277 (defvar ess-retr-lastvalue-command nil
278 "The command to retrieve the last value. See S section for more details.
279 Default depends on the ESS language/dialect and hence made buffer local")
280 (make-variable-buffer-local 'ess-retr-lastvalue-command)
282 ;;; A note on multiple processes: the following variables
283 ;;; ess-local-process-name
284 ;;; ess-search-list
285 ;;; ess-sl-modtime-alist
286 ;;; ess-sp-change
287 ;;; ess-prev-load-dir/file
288 ;;; ess-directory
289 ;;; ess-object-list
290 ;;; are specific to each ess-process and are buffer-local variables
291 ;;; local to the ESS process buffer. If required, these variables should
292 ;;; be accessed with the function ess-get-process-variable
294 (defun ess-multi (name &optional buffer inf-ess-start-args)
295 "Start or switch to ESS process named NAME in the buffer BUFFER.
296 BUFFER is only needed if process NAME is not running. BUFFER must
297 exist. Default-directory is the ESS starting directory. BUFFER may be
298 visiting a file.
300 If ess-process NAME is running, switch to it. If not, use COMINT to
301 start up a new process, using NAME and BUFFER (which is needed if
302 there is no process NAME)."
304 (let* ((proc-name name)
305 (special-display-regexps nil)
306 (special-display-frame-alist inferior-ess-frame-alist)
307 (proc (get-process proc-name)))
308 (if inferior-ess-own-frame
309 (setq special-display-regexps '(".")))
310 ;; If ESS process NAME is running, switch to it
311 (if (and proc (comint-check-proc (process-buffer proc)))
312 (pop-to-buffer (process-buffer proc))
313 ;; Otherwise, crank up a new process
314 (let* ((symbol-string
315 (concat "inferior-" inferior-ess-program "-args"))
316 (switches-symbol (intern-soft symbol-string))
317 (switches
318 (if (and switches-symbol (boundp switches-symbol))
319 (symbol-value switches-symbol)))
320 (buf-name-str (buffer-name buffer)))
321 (ess-write-to-dribble-buffer
322 (format "(ess-multi 0): inf-ess-start-args=%s, comint-..echoes=%s\n"
323 inf-ess-start-args comint-process-echoes))
324 (set-buffer buffer)
325 (inferior-ess-mode)
326 (ess-write-to-dribble-buffer
327 (format "(ess-multi post inf-ess: start-args=%s, comint-echoes=%s\n"
328 inf-ess-start-args comint-process-echoes))
329 (setq ess-local-process-name proc-name)
330 (goto-char (point-max))
331 ;; load past history
332 (setq comint-input-ring-file-name
333 (expand-file-name ess-history-file ess-directory))
334 (comint-read-input-ring)
335 ;; create and run process.
336 (ess-write-to-dribble-buffer
337 (format "(ess-multi 1): start-args=%s \n"
338 inf-ess-start-args))
339 (set-buffer
340 (if switches
341 (inferior-ess-make-comint buf-name-str
342 proc-name
343 inf-ess-start-args
344 switches)
345 (inferior-ess-make-comint buf-name-str
346 proc-name
347 inf-ess-start-args)))
348 ;; Set the process sentinel to save the history
349 (set-process-sentinel (get-process proc-name) 'ess-process-sentinel)
350 ;; Add this process to ess-process-name-list, if needed
351 (let ((conselt (assoc proc-name ess-process-name-list)))
352 (if conselt nil
353 (setq ess-process-name-list
354 (cons (cons proc-name nil) ess-process-name-list))))
355 (ess-make-buffer-current)
356 (inferior-ess-wait-for-prompt)
357 (goto-char (point-max))
358 (setq ess-sl-modtime-alist nil)
359 ;; Get search list when needed
360 (setq ess-sp-change t)
362 ;; Add the process filter to catch certain output.
363 (set-process-filter (get-process proc-name)
364 'inferior-ess-output-filter)
366 (run-hooks 'ess-post-run-hook))
367 (if (and inferior-ess-same-window (not inferior-ess-own-frame))
368 (switch-to-buffer (process-buffer (get-process proc-name)))
369 (pop-to-buffer (process-buffer (get-process proc-name)))))))
371 (defun inferior-ess-output-filter (proc string)
372 "Standard output filter for the inferior ESS process.
373 Ring Emacs bell if process output starts with an ASCII bell, and pass
374 the rest to `comint-output-filter'.
375 Taken from octave-mod.el."
376 (comint-output-filter proc (inferior-ess-strip-ctrl-g string)))
378 (defun inferior-ess-strip-ctrl-g (string)
379 "Strip leading `^G' character.
380 If STRING starts with a `^G', ring the Emacs bell and strip it.
381 Depending on the value of `visible-bell', either the frame will
382 flash or you'll hear a beep. Taken from octave-mod.el."
383 (if (string-match "^\a" string)
384 (progn
385 (ding)
386 (setq string (substring string 1))))
387 string)
390 (defun ess-process-sentinel (proc message)
391 "Sentinel for use with ESS processes.
392 This marks the process with a message, at a particular time point."
393 (save-excursion
394 (setq message (substring message 0 -1)) ; strip newline
395 (set-buffer (process-buffer proc))
396 (comint-write-input-ring)
397 (goto-char (point-max))
398 (insert-before-markers
399 (format "\nProcess %s %s at %s\n"
400 (process-name proc) message (current-time-string)))))
402 (defun inferior-ess-make-comint (bufname
403 procname
404 inferior-ess-start-args
405 &rest switches)
406 "Make an S comint process in buffer BUFNAME with process PROCNAME.
407 This was rewritten by KH in April 1996."
408 ;;; This function is a modification of make-comint from the comint.el
409 ;;; code of Olin Shivers.
410 (let* ((buffer (get-buffer-create bufname))
411 (proc (get-process procname)))
412 ;; If no process, or nuked process, crank up a new one and put buffer in
413 ;; comint mode. Otherwise, leave buffer and existing process alone.
414 (cond ((or (not proc) (not (memq (process-status proc) '(run stop))))
415 (save-excursion
416 (set-buffer buffer)
417 (if ess-directory (setq default-directory ess-directory))
418 (if (eq (buffer-size) 0) nil
419 (goto-char (point-max))
420 (insert "\^L\n"))) ; page boundaries = Interactive sessions
421 (let ((process-environment
422 (nconc
423 (list "STATATERM=emacs"
424 (format "PAGER=%s" inferior-ess-pager))
425 process-environment)))
426 (ess-write-to-dribble-buffer "Making Process...")
427 (ess-write-to-dribble-buffer
428 (format "Buf %s, Proc %s, Prog %s\n Start File=%s, Args= %s.\n"
429 buffer
430 procname
431 inferior-ess-program
432 inferior-ess-start-file
433 inferior-ess-start-args))
434 (comint-exec buffer
435 procname
436 inferior-ess-program
437 inferior-ess-start-file
438 (ess-line-to-list-of-words
439 inferior-ess-start-args)))))
440 buffer))
443 ;;*;; Requester functions called at startup
445 ;(defun ess-get-directory-ORIG (default)
446 ; "Request and return S starting directory."
447 ; (let ((the-dir
448 ; (expand-file-name
449 ; (file-name-as-directory
450 ; (read-file-name
451 ; (format "ESS [%s(%s): %s] starting data directory? "
452 ; ess-language ess-dialect inferior-ess-program)
453 ; (file-name-as-directory default)
454 ; (file-name-as-directory default) t nil)))))
455 ; (if (file-directory-p the-dir) nil
456 ; (error "%s is not a valid directory" the-dir))
457 ; the-dir))
459 (defun ess-get-directory (default)
460 (ess-prompt-for-directory
461 default
462 (format "ESS [%s(%s): %s] starting data directory? "
463 ess-language ess-dialect inferior-ess-program)))
465 (defun ess-prompt-for-directory (default prompt)
466 "`prompt' for a directory, using `default' as the usual."
467 (let* ((def-dir (file-name-as-directory default))
468 (the-dir (expand-file-name
469 (file-name-as-directory
470 (if (fboundp 'read-directory-name)
471 ;; use XEmacs' read-directory-name if exists.
472 (read-directory-name prompt def-dir def-dir t nil)
473 (read-file-name prompt def-dir def-dir t nil))))))
474 (if (file-directory-p the-dir) nil
475 (error "%s is not a valid directory" the-dir))
476 the-dir))
479 ;;*;; General process handling code
481 (defun get-ess-process (name)
482 "Return the ESS process named by NAME."
483 (update-ess-process-name-list)
484 (if (null name)
485 (error "No ESS process is associated with this buffer now.")
486 (if (assoc name ess-process-name-list)
487 (get-process name)
488 (error "Process %s is not running." name))))
490 (defun inferior-ess-wait-for-prompt ()
491 "Wait until the ESS process is ready for input."
492 (let* ((cbuffer (current-buffer))
493 (sprocess (get-ess-process ess-current-process-name))
494 (sbuffer (process-buffer sprocess))
495 (r nil)
496 (timeout 0))
497 (set-buffer sbuffer)
498 (while (progn
499 (if (not (eq (process-status sprocess) 'run))
500 (ess-error "ESS process has died unexpectedly.")
501 (if (> (setq timeout (1+ timeout)) ess-loop-timeout)
502 (ess-error "Timeout waiting for prompt. Check inferior-ess-prompt or ess-loop-timeout."))
503 (accept-process-output)
504 (goto-char (point-max))
505 (beginning-of-line); bol ==> no need for "^" in *-prompt! (MM?)
506 ;; above, except for Stata, which has "broken" i/o,
507 ;; sigh... (AJR)
508 (setq r (looking-at inferior-ess-prompt))
509 (not (or r (looking-at ".*\\?\\s *"))))))
510 (goto-char (point-max))
511 (set-buffer cbuffer)
512 (symbol-value r)))
514 ;;--- Unfinished idea (ESS-help / R-help ) -- probably not worth it...
515 ;;- (defun ess-set-inferior-program-name (filename)
516 ;;- "Allows to set or change `inferior-ess-program', the program (file)name."
517 ;;- (interactive "fR executable (script) file: ")
518 ;;- ;; "f" : existing file {file name completion} !
519 ;;- (setq inferior-ess-program filename))
520 ;; the inferior-ess-program is initialized in the customize..alist,
521 ;; e.g. from inferior-R-program-name ... --> should change rather these.
522 ;; However these really depend on the current ess-language!
523 ;; Plan: 1) must know and use ess-language
524 ;; 2) change the appropriate inferior-<ESSlang>-program-name
525 ;; (how?) in R/S : assign(paste("inferior-",ESSlang,"-p...."), filename))
527 ;;*;; Multiple process handling code
529 (defun ess-make-buffer-current nil
530 "Make the process associated with the current buffer the current ESS process.
531 Returns the name of the process, or nil if the current buffer has none."
532 (update-ess-process-name-list)
533 (if ess-local-process-name
534 (setq ess-current-process-name ess-local-process-name))
535 ess-local-process-name)
537 (defun ess-get-process-variable (name var)
538 "Return the variable VAR (symbol) local to ESS process called NAME (string)."
539 (save-excursion
540 (set-buffer (process-buffer (get-ess-process name)))
541 (symbol-value var)))
543 (defun ess-set-process-variable (name var val)
544 "Set variable VAR (symbol) local to ESS process called NAME (string) to VAL."
545 (save-excursion
546 (set-buffer (process-buffer (get-ess-process name)))
547 (set var val)))
549 (defun ess-request-a-process (message &optional noswitch ask-if-1)
550 "Ask for a process, and make it the current ESS process.
551 If there is exactly one process, only ask if ASK-IF-1 is non-nil.
552 Also switches to the process buffer unless NOSWITCH is non-nil. Interactively,
553 NOSWITCH can be set by giving a prefix argument.
554 Returns the name of the selected process."
555 (interactive
556 (list "Switch to which ESS process? " current-prefix-arg))
557 ; prefix sets 'noswitch
558 (update-ess-process-name-list)
559 (let ((num-processes (length ess-process-name-list)))
560 (if (= 0 num-processes)
561 (error "No ESS processes running."))
562 ;; else
563 (let ((proc
564 (if (and (not ask-if-1) (= 1 num-processes))
565 (let ((rr (car (car ess-process-name-list))))
566 (message "using process '%s'" rr)
568 ;; else
569 (completing-read message
570 ess-process-name-list
571 nil ; predicate
572 'require-match
573 ;; If in S buffer, don't offer current process
574 (if (eq major-mode 'inferior-ess-mode)
575 ess-language
576 ess-current-process-name
577 ;; maybe ess-local-process-name IF exists?
578 )))))
579 (save-excursion
580 (set-buffer (process-buffer (get-process proc)))
581 (ess-make-buffer-current))
582 (if noswitch
584 (ess-show-buffer (buffer-name (process-buffer (get-process proc))) t))
585 proc)))
588 (defun ess-force-buffer-current (prompt &optional force)
589 "Make sure the current buffer is attached to an ESS process.
590 If not, or FORCE (prefix argument) is non-nil,
591 prompt for a process name with PROMPT.
592 `ess-local-process-name' is set to the name of the process selected.
593 `ess-dialect' is set to the dialect associated with the process selected."
594 (interactive
595 (list (concat ess-dialect " process to use: ") current-prefix-arg))
596 (if (and (not force) (ess-make-buffer-current))
597 nil ; do nothing
598 ;; Make sure the source buffer is attached to a process
599 (if (and ess-local-process-name (not force))
600 (error "Process %s has died." ess-local-process-name)
601 ;; ess-local-process-name is nil -- which process to attach to
602 (save-excursion
603 (let ((proc (ess-request-a-process prompt 'no-switch))
604 temp-ess-help-filetype
605 dialect)
606 (save-excursion
607 (set-buffer (process-buffer (get-process proc)))
608 (setq temp-ess-help-filetype inferior-ess-help-filetype)
609 (setq dialect ess-dialect))
610 (setq ess-local-process-name proc)
611 (setq inferior-ess-help-filetype temp-ess-help-filetype)
612 (setq ess-dialect dialect))))))
614 (defun ess-switch-process ()
615 "Force a switch to a new underlying process."
616 (interactive)
617 (ess-force-buffer-current "Process to use: " t))
619 ;;*;;; Commands for switching to the process buffer
621 (defun ess-switch-to-ESS (eob-p)
622 "Switch to the current inferior ESS process buffer.
623 With (prefix) EOB-P non-nil, positions cursor at end of buffer.
624 This function should follow the description in `ess-show-buffer'
625 for showing the iESS buffer, except that the iESS buffer is also
626 made current."
627 (interactive "P")
628 (ess-make-buffer-current)
629 (if (and ess-current-process-name (get-process ess-current-process-name))
630 (progn
631 ;; Display the buffer, but don't select it yet.
632 (ess-show-buffer
633 (buffer-name (process-buffer (get-process ess-current-process-name)))
635 (if eob-p (goto-char (point-max))))
636 (message "No inferior ESS process")
637 (ding)))
639 (defun ess-switch-to-end-of-ESS ()
640 "Switch to the end of the inferior ESS process buffer."
641 (interactive)
642 (ess-switch-to-ESS t))
644 (defun get-ess-buffer (name)
645 "Return the buffer associated with the ESS process named by NAME."
646 (process-buffer (get-ess-process name)))
648 (defun update-ess-process-name-list ()
649 "Remove names with no process."
650 (let (defunct)
651 (mapcar
652 '(lambda (conselt)
653 (let ((proc (get-process (car conselt))))
654 (if (and proc (eq (process-status proc) 'run)) nil
655 (setq defunct (cons conselt defunct)))))
656 ess-process-name-list)
657 (mapcar
658 '(lambda (pointer)
659 (setq ess-process-name-list (delq pointer ess-process-name-list)))
660 defunct))
661 (if (eq (length ess-process-name-list) 0)
662 (setq ess-current-process-name nil)))
664 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
665 ;; ess-show-buffer
666 ;; Something like this almost works, but problems with XEmacs and Emacs
667 ;; differing implementations of the args to display-buffer make this
668 ;; too tough to pursue. The longer version below works.
669 ;; (defun ess-show-buffer (buf)
670 ;; "Display the buffer BUF, a string, but do not select it.
671 ;; Returns the window corresponding to the buffer."
672 ;; ;; On XEmacs, I get an error if third arg to display-buffer is t and
673 ;; ;; the BUF is in another frame. Emacs does not have this problem.
674 ;; (if (featurep 'xemacs)
675 ;; (display-buffer buf nil (get-frame-for-buffer buf))
676 ;; (display-buffer buf nil t)))
677 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
678 (defun ess-show-buffer (buf &optional visit)
679 "Ensure the ESS buffer BUF is visible.
680 The buffer, specified as a string, is typically an iESS (e.g. *R*) buffer.
682 This handles several cases:
684 1. If BUF is visible in the current frame, nothing is done.
685 2. If BUF is visible in another frame, then we ensure that frame is
686 visible (it may have been iconified).
687 3. If buffer is not visible in any frame, simply show it in another window
688 in the current frame.
690 Iff VISIT is non-nil, as well as making BUF visible, we also select it
691 as the current buffer."
692 (let ( (frame))
693 (if (ess-buffer-visible-this-frame buf)
694 ;;1. Nothing to do, BUF visible in this frame; just return window
695 ;; where this buffer is.
698 ;; 2. Maybe BUF visible in another frame.
699 (setq frame (ess-buffer-visible-other-frame buf))
700 (if frame
701 ;; BUF is visible in frame, so just check frame is raised.
702 (if (not (eq (frame-visible-p frame) t))
703 ;; frame is not yet visible, so raise it.
704 (raise-frame frame))
705 ;; 3. else BUF not visible in any frame, so show it (but do
706 ;; not select it) in another window in current frame.
707 (display-buffer buf)))
708 ;; At this stage, the buffer should now be visible on screen,
709 ;; although it won't have been made current.
710 (when visit
711 ;; Need to select the buffer.
713 ;; First of all, check case 2 if buffer is in another frame
714 ;; but that frame may not be selected.
715 (if frame
716 (ess-select-frame-set-input-focus frame))
717 (select-window (get-buffer-window buf 0)))))
720 (defvar ess-bufs-in-frame nil) ;silence the compiler.
721 ;; The next few functions are copied from my (SJE) iswitchb library.
722 (defun ess-get-bufname (win)
723 "Used by `ess-get-buffers-in-frames' to walk through all windows."
724 (let ((buf (buffer-name (window-buffer win))))
725 (if (not (member buf ess-bufs-in-frame))
726 ;; Only add buf if it is not already in list.
727 ;; This prevents same buf in two different windows being
728 ;; put into the list twice.
729 (setq ess-bufs-in-frame
730 (cons buf ess-bufs-in-frame)))))
732 (defun ess-get-buffers-in-frames (&optional current)
733 "Return the list of buffers that are visible in the current frame.
734 If optional argument CURRENT is given, restrict searching to the
735 current frame, rather than all frames."
736 (let ((ess-bufs-in-frame nil))
737 (walk-windows 'ess-get-bufname nil (if current nil 0))
738 ess-bufs-in-frame))
740 (defun ess-buffer-visible-this-frame (buf)
741 "Return t if BUF is visible in current frame."
742 (member buf (ess-get-buffers-in-frames t)))
744 (defun ess-buffer-visible-other-frame (buf)
745 "Return t if BUF is visible in another frame.
746 Assumes that buffer has not already been in found in current frame."
747 (if (member buf (ess-get-buffers-in-frames))
748 (window-frame (get-buffer-window buf 0))
749 nil))
752 \f ; Functions for evaluating code
754 (defun ess-ddeclient-p ()
755 "Returns t iff `ess-local-process-name' is associated with an
756 inferior-ess-ddeclient, and nil if the ess-process is running as an
757 ordinary inferior process. Alway nil on Unix machines."
758 (interactive)
759 (if ess-microsoft-p
760 (not (equal (ess-get-process-variable
761 ess-local-process-name 'inferior-ess-ddeclient)
762 (default-value 'inferior-ess-ddeclient)))))
764 (defun ess-prompt-wait (proc &optional start-of-output sleep)
765 "Wait for a prompt to appear at BOL of current buffer.
766 PROC is the ESS process. Does not change point"
767 (if sleep (sleep-for sleep)); we sleep here, *and* wait below
768 (if start-of-output nil (setq start-of-output (point-min)))
769 (save-excursion
770 (while (progn
771 ;; get output if there is some ready
773 ;; (if (and ess-microsoft-p ess-ms-slow)
774 ;; (accept-process-output proc 0 1500) ; Microsoft is slow
775 (accept-process-output proc 0 500)
776 ;; )
777 (goto-char (marker-position (process-mark proc)))
778 (beginning-of-line)
779 (if (< (point) start-of-output) (goto-char start-of-output))
780 (not (looking-at inferior-ess-primary-prompt))))))
782 (defun ordinary-insertion-filter (proc string)
783 (let ((old-buffer (current-buffer)))
784 (unwind-protect
785 (let (moving)
786 (set-buffer (process-buffer proc))
787 (setq moving (= (point) (process-mark proc)))
788 (save-excursion
789 ;; Insert the text, moving the process-marker.
790 (goto-char (process-mark proc))
791 (insert string)
792 (set-marker (process-mark proc) (point)))
793 (if moving (goto-char (process-mark proc))))
794 (set-buffer old-buffer))))
796 (defun ess-command (com &optional buf sleep)
797 "Send the ESS process command COM and delete the output
798 from the ESS process buffer. If an optional second argument BUF exists
799 save the output in that buffer. BUF is erased before use.
800 COM should have a terminating newline.
801 Guarantees that the value of .Last.value will be preserved.
802 When optional third arg SLEEP is non-nil, `(sleep-for (* a SLEEP))'
803 will be used in a few places where `a' is proportional to `ess-cmd-delay'."
804 ;; Use this function when you need to evaluate some S code, and the
805 ;; result is needed immediately. Waits until the output is ready
807 ;; the ddeclient-p checks needs to use the local-process-name
808 (if buf nil (setq buf (get-buffer-create " *ess-command-output*")))
809 (save-excursion
810 (set-buffer buf)
811 (if ess-local-process-name
813 (setq ess-local-process-name ess-current-process-name)))
814 (if (ess-ddeclient-p)
815 (ess-command-ddeclient com buf sleep)
817 ;; else: "normal", non-DDE behavior:
819 (let* ((sprocess (get-ess-process ess-current-process-name))
820 sbuffer
821 do-sleep end-of-output
822 oldpb oldpf oldpm
824 (if sprocess nil
825 (error "Process %s is not running!" ess-current-process-name))
826 (setq sbuffer (process-buffer sprocess))
827 (save-excursion
828 (set-buffer sbuffer)
829 (setq do-sleep ; only now when in sprocess buffer
830 (progn
831 (if sleep (if (numberp sleep) nil (setq sleep 1))) ; t means 1
832 (and ess-cmd-delay sleep)))
833 (if do-sleep (setq sleep (* sleep ess-cmd-delay)))
834 (save-excursion
835 (goto-char (marker-position (process-mark sprocess)))
836 (beginning-of-line)
837 (if (looking-at inferior-ess-primary-prompt) nil
838 (ess-error
839 "ESS process not ready. Finish your command before trying again.")))
840 (setq oldpf (process-filter sprocess))
841 (setq oldpb (process-buffer sprocess))
842 (setq oldpm (marker-position (process-mark sprocess)))
843 ;; need the buffer-local values in result buffer "buf":
844 (unwind-protect
845 (progn
846 (set-process-buffer sprocess buf)
847 (set-process-filter sprocess 'ordinary-insertion-filter)
848 ;; Output is now going to BUF:
849 (save-excursion
850 (set-buffer buf)
851 (erase-buffer)
852 (set-marker (process-mark sprocess) (point-min))
853 (process-send-string sprocess com)
854 ;; need time for ess-create-object-name-db on PC
855 (ess-prompt-wait sprocess nil (and do-sleep (* 0.4 sleep))) ;MS: 4
856 ;;(if do-sleep (sleep-for (* 0.0 sleep))); microsoft: 0.5
858 ;; goto end, making sure final prompt is deleted
859 ;; FIXME? do this with much less code
860 (goto-char (point-max))
861 (save-excursion
862 (beginning-of-line)
863 (setq end-of-output (point)))
864 (delete-region end-of-output (point-max))
866 ;; Restore old values for process filter
867 (set-process-buffer sprocess oldpb)
868 (set-process-filter sprocess oldpf)
869 (set-marker (process-mark sprocess) oldpm))))))
871 (defun ess-replace-in-string (str regexp newtext &optional literal)
872 "Replace all matches in STR for REGEXP with NEWTEXT string.
873 Optional LITERAL non-nil means do a literal replacement.
874 Otherwise treat \\ in NEWTEXT string as special:
875 \\& means substitute original matched text,
876 \\N means substitute match for \(...\) number N,
877 \\\\ means insert one \\."
878 (if (not (stringp str))
879 (error "(replace-in-string): First argument must be a string: %s" str))
880 (if (stringp newtext)
882 (error "(replace-in-string): 3rd arg must be a string: %s"
883 newtext))
884 (let ((rtn-str "")
885 (start 0)
886 (special)
887 match prev-start)
888 (while (setq match (string-match regexp str start))
889 (setq prev-start start
890 start (match-end 0)
891 rtn-str
892 (concat
893 rtn-str
894 (substring str prev-start match)
895 (cond (literal newtext)
896 (t (mapconcat
897 (function
898 (lambda (c)
899 (if special
900 (progn
901 (setq special nil)
902 (cond ((eq c ?\\) "\\")
903 ((eq c ?&)
904 (substring str
905 (match-beginning 0)
906 (match-end 0)))
907 ((and (>= c ?0) (<= c ?9))
908 (if (> c (+ ?0 (length
909 (match-data))))
910 ;; Invalid match num
911 (error "(replace-in-string) Invalid match num: %c" c)
912 (setq c (- c ?0))
913 (substring str
914 (match-beginning c)
915 (match-end c))))
916 (t (char-to-string c))))
917 (if (eq c ?\\) (progn (setq special t) nil)
918 (char-to-string c)))))
919 newtext ""))))))
920 (concat rtn-str (substring str start))))
924 ;;*;; Evaluating lines, paragraphs, regions, and buffers.
926 ;;--- The two basic building blocks [called by all other ess-eval..] are
927 ;; (ess-eval-linewise ....)
928 ;; and
929 ;; (ess-eval-region ....)
931 (defun ess-eval-linewise (text-withtabs &optional
932 invisibly eob even-empty
933 wait-last-prompt sleep-sec timeout-ms)
934 ;; RDB 28/8/92 added optional arg eob
935 ;; AJR 971022: text-withtabs was text.
936 ;; MM 2006-08-23: added 'timeout-ms' -- but the effect seems "nil"
937 ;; MM 2007-01-05: added 'sleep-sec'
938 "Evaluate TEXT-WITHTABS in the ESS process buffer as if typed in w/o tabs.
939 Waits for prompt after each line of input, so won't break on large texts.
941 If optional second arg INVISIBLY is non-nil, don't echo commands. If it
942 is a string, just include that string. If optional third arg
943 EOB is non-nil go to end of ESS process buffer after evaluation. If
944 optional 4th arg EVEN-EMPTY is non-nil, also send empty text (e.g. an
945 empty line). If 5th arg WAIT-LAST-PROMPT is non-nil, also wait for
946 the prompt after the last line; if 6th arg SLEEP-SEC is a number, ESS
947 will call '(\\[sleep-for] SLEEP-SEC) at the end of this function. If the
948 7th arg TIMEOUT-MS is set to number, it will be used instead of the
949 default 100 ms and be passed to \\[accept-process-output]."
950 ;; but the effect is unclear
951 (if (ess-ddeclient-p)
952 (ess-eval-linewise-ddeclient text-withtabs
953 invisibly eob even-empty
954 (if wait-last-prompt
955 ess-eval-ddeclient-sleep))
957 ;; else: "normal", non-DDE behavior:
959 ;; Use this to evaluate some code, but don't wait for output.
960 (let* ((cbuffer (current-buffer))
961 (sprocess (get-ess-process ess-current-process-name))
962 (sbuffer (process-buffer sprocess))
963 (text (ess-replace-in-string text-withtabs "\t" " "))
964 start-of-output
965 com pos txt-gt-0)
967 (unless (numberp timeout-ms)
968 (setq timeout-ms 100));; << make '100' into a custom-variable
970 ;;(message "'ess-eval-linewise: sbuffer = %s" sbuffer)
971 (set-buffer sbuffer)
973 ;; the following is required to make sure things work!
974 (if (string= ess-language "STA")
975 (setq invisibly t))
976 ;; dbg:
977 ;; dbg(ess-write-to-dribble-buffer
978 ;; dbg (format "(eval-visibly 1): lang %s (invis=%s, eob=%s, even-empty=%s)\n"
979 ;; dbg ess-language invisibly eob even-empty))
981 (goto-char (marker-position (process-mark sprocess)))
982 (if (stringp invisibly)
983 (insert-before-markers (concat "*** " invisibly " ***\n")))
984 ;; dbg:
985 ;; dbg (ess-write-to-dribble-buffer
986 ;; dbg (format "(eval-visibly 2): text[%d]= '%s'\n" (length text) text))
987 (while (or (setq txt-gt-0 (> (length text) 0))
988 even-empty)
989 (if even-empty (setq even-empty nil))
990 (if txt-gt-0
991 (progn
992 (setq pos (string-match "\n\\|$" text))
993 (setq com (concat (substring text 0 pos) "\n"))
994 (setq text (substring text (min (length text) (1+ pos)))))
995 ;; else 0-length text
996 (setq com "\n")
998 (goto-char (marker-position (process-mark sprocess)))
999 (if (not invisibly)
1000 ;; Terrible kludge -- need to insert after all markers *except*`
1001 ;; the process mark
1002 (let ((dokludge (eq (point)
1003 (marker-position (process-mark sprocess)))))
1004 (insert com)
1005 (if dokludge (set-marker (process-mark sprocess) (point)))))
1006 (setq start-of-output (marker-position (process-mark sprocess)))
1007 ;; A kludge to prevent the delay between insert and process output
1008 ;; affecting the display. A case for a comint-send-input-hook?
1009 ;; (save-excursion
1010 ;; ;; comint-postoutput-scroll-to-bottom can change
1011 ;; ;; current-buffer. Argh.
1012 ;; (let ((functions comint-output-filter-functions))
1013 ;; (while functions
1014 ;; (funcall (car functions) com)
1015 ;; (setq functions (cdr functions)))))
1016 (process-send-string sprocess com)
1017 ;; wait for the prompt - after the last line of input only if wait-last:
1018 (if (or wait-last-prompt
1019 (> (length text) 0))
1020 (while (progn
1021 (accept-process-output nil 0 timeout-ms)
1022 (goto-char (marker-position (process-mark sprocess)))
1023 (beginning-of-line)
1024 (if (< (point) start-of-output)
1025 (goto-char start-of-output))
1026 (not (looking-at inferior-ess-prompt))))))
1028 (goto-char (marker-position (process-mark sprocess)))
1029 (if eob
1030 (progn
1031 (ess-show-buffer (buffer-name sbuffer) nil)
1032 ;; Once SBUFFER is visible, we can then move the point in that
1033 ;; window to the end of the buffer.
1034 (set-window-point (get-buffer-window sbuffer t)
1035 (with-current-buffer sbuffer (point-max))))
1036 (set-buffer cbuffer))
1037 (if (numberp sleep-sec)
1038 (sleep-for sleep-sec))))); in addition to timeout-ms
1041 ;;;*;;; Evaluate only
1043 (defun ess-eval-region (start end toggle &optional message)
1044 "Send the current region to the inferior ESS process.
1045 With prefix argument toggle the meaning of `ess-eval-visibly-p';
1046 this does not apply when using the S-plus GUI, see `ess-eval-region-ddeclient'."
1047 (interactive "r\nP")
1048 ;;(untabify (point-min) (point-max))
1049 ;;(untabify start end); do we really need to save-excursion?
1050 (ess-force-buffer-current "Process to load into: ")
1051 (message "Starting evaluation...")
1053 (if (ess-ddeclient-p)
1054 (ess-eval-region-ddeclient start end 'even-empty)
1055 ;; else: "normal", non-DDE behavior:
1056 (let ((visibly (if toggle (not ess-eval-visibly-p) ess-eval-visibly-p)))
1057 (if visibly
1058 (ess-eval-linewise (buffer-substring start end))
1059 (if ess-synchronize-evals
1060 (ess-eval-linewise (buffer-substring start end)
1061 (or message "Eval region"))
1062 ;; else [almost always!]
1063 (let ((sprocess (get-ess-process ess-current-process-name)))
1064 (process-send-region sprocess start end)
1065 (process-send-string sprocess "\n"))))))
1067 (message "Finished evaluation")
1068 ;; return value
1069 (list start end))
1071 (defun ess-eval-buffer (vis)
1072 "Send the current buffer to the inferior ESS process.
1073 Arg has same meaning as for `ess-eval-region'."
1074 (interactive "P")
1075 (ess-eval-region (point-min) (point-max) vis "Eval buffer"))
1077 (defun ess-eval-function (vis)
1078 "Send the current function to the inferior ESS process.
1079 Arg has same meaning as for `ess-eval-region'."
1080 (interactive "P")
1081 (save-excursion
1082 (let* ((beg-end (ess-end-of-function))
1083 (beg (nth 0 beg-end))
1084 (end (nth 1 beg-end))
1085 name)
1086 (goto-char beg)
1087 (setq name (ess-read-object-name-default))
1088 (princ (concat "Loading: " name) t)
1089 (ess-eval-region beg end vis
1090 (concat "Eval function " name)))))
1092 ;; This is from Mary Lindstrom <lindstro@Biostat.Wisc.Edu>
1093 ;; 31 Aug 1995 14:11:43 To: S-mode@stat.math.ethz.ch
1094 (defun ess-eval-paragraph (vis)
1095 "Send the current paragraph to the inferior ESS process.
1096 Prefix arg VIS toggles visibility of ess-code as for `ess-eval-region'."
1097 (interactive "P")
1098 (save-excursion
1099 (forward-paragraph)
1100 (let ((end (point)))
1101 (backward-paragraph)
1102 (ess-eval-region (point) end vis "Eval paragraph"))))
1104 (defun ess-eval-function-or-paragraph-and-step (vis)
1105 "Send the current function if \\[point] is inside one, otherwise the current
1106 paragraph other to the inferior ESS process.
1107 Prefix arg VIS toggles visibility of ess-code as for `ess-eval-region'."
1108 (interactive "P")
1109 (let ((beg (ess-beginning-of-function 'no-error)))
1110 (if beg ;; inside a function
1111 (let ((end-fun (cadr (ess-end-of-function beg)))
1112 name)
1113 (goto-char beg)
1114 (setq name (ess-read-object-name-default))
1115 (princ (concat "Loading: " name) t)
1116 (ess-eval-region beg end-fun vis
1117 (concat "Eval function " name))
1118 (goto-char (1+ end-fun)))
1119 ;; else: not in a function
1120 (ess-eval-paragraph-and-step vis)
1124 (defun ess-eval-line (vis)
1125 "Send the current line to the inferior ESS process.
1126 Arg has same meaning as for `ess-eval-region'."
1127 (interactive "P")
1128 (save-excursion
1129 (end-of-line)
1130 (let ((end (point)))
1131 (beginning-of-line)
1132 (princ (concat "Loading line: " (ess-extract-word-name) " ...") t)
1133 (ess-eval-region (point) end vis "Eval line"))))
1137 ;; Contributed by Stephen Eglen <stephen@anc.ed.ac.uk> {idea from octave int.}
1138 (defun ess-next-code-line (&optional arg)
1139 "Move ARG lines of code forward (backward if ARG is negative).
1140 Skips past all empty and comment lines. Default for ARG is 1.
1142 On success, return 0. Otherwise, go as far as possible and return -1."
1143 (interactive "p")
1144 (or arg (setq arg 1))
1145 (beginning-of-line)
1146 (let ((n 0)
1147 (inc (if (> arg 0) 1 -1)))
1148 (while (and (/= arg 0) (= n 0))
1149 (setq n (forward-line inc)); n=0 is success
1150 (while (and (= n 0)
1151 (looking-at "\\s-*\\($\\|\\s<\\)"))
1152 (setq n (forward-line inc)))
1153 (setq arg (- arg inc)))
1156 (defun ess-eval-line-and-step (&optional simple-next even-empty invisibly)
1157 "Evaluate the current line visibly and step to the \"next\" line.
1158 \"next\" = the next line with non-comment code _unless_ SIMPLE-NEXT is non-nil,
1159 possibly via prefix arg. If 2nd arg EVEN-EMPTY [prefix as well],
1160 also send empty lines. When the variable `ess-eval-empty' is non-nil
1161 both SIMPLE-NEXT and EVEN-EMPTY are interpreted as true."
1162 ;; From an idea by Rod Ball (rod@marcam.dsir.govt.nz)
1163 (interactive "P\nP"); prefix sets BOTH !
1164 (ess-force-buffer-current "Process to load into: ")
1165 (save-excursion
1166 (end-of-line)
1167 (let ((end (point)))
1168 (beginning-of-line)
1169 ;; go to end of process buffer so user can see result
1170 (ess-eval-linewise (buffer-substring (point) end)
1171 invisibly 'eob (or even-empty ess-eval-empty))))
1172 (if (or simple-next ess-eval-empty)
1173 (forward-line 1)
1174 (ess-next-code-line 1)))
1176 (defun ess-eval-line-and-step-invisibly ()
1177 "Evaluate the current line invisibly and step to the next line.
1178 Evaluate all comments and empty lines."
1179 (interactive)
1180 (ess-eval-line-and-step t t t))
1182 ;; goes to the real front, in case you do double function definition
1183 ;; 29-Jul-92 -FER
1184 ;; don't know why David changed it.
1186 ;; FER's versions don't work properly with nested functions. Replaced
1187 ;; mine. DMS 16 Nov 92
1189 ;;;*;;; Evaluate and switch to S
1191 (defun ess-eval-region-and-go (start end vis)
1192 "Send the current region to the inferior S and switch to the process buffer.
1193 Arg has same meaning as for `ess-eval-region'."
1194 (interactive "r\nP")
1195 (ess-eval-region start end vis)
1196 (ess-switch-to-ESS t))
1198 (defun ess-eval-buffer-and-go (vis)
1199 "Send the current buffer to the inferior S and switch to the process buffer.
1200 Arg has same meaning as for `ess-eval-region'."
1201 (interactive "P")
1202 (ess-eval-buffer vis)
1203 (ess-switch-to-ESS t))
1205 (defun ess-eval-function-and-go (vis)
1206 "Send the current function to the inferior ESS process and switch to
1207 the process buffer. Arg has same meaning as for `ess-eval-region'."
1208 (interactive "P")
1209 (ess-eval-function vis)
1210 (ess-switch-to-ESS t))
1212 (defun ess-eval-line-and-go (vis)
1213 "Send the current line to the inferior ESS process and switch to the
1214 process buffer. Arg has same meaning as for `ess-eval-region'."
1215 (interactive "P")
1216 (ess-eval-line vis)
1217 (ess-switch-to-ESS t))
1219 (defun ess-eval-paragraph-and-go (vis)
1220 "Send the current paragraph to the inferior ESS process and switch to the
1221 process buffer. Arg has same meaning as for `ess-eval-region'."
1222 (interactive "P")
1223 (ess-eval-paragraph vis)
1224 (ess-switch-to-ESS t))
1226 (defun ess-eval-paragraph-and-step (vis)
1227 "Send the current paragraph to the inferior ESS process and move forward to
1228 the next paragraph. Arg has same meaning as for `ess-eval-region'."
1229 (interactive "P")
1230 (let ((beg-end (ess-eval-paragraph vis)))
1231 (goto-char (1+ (cadr beg-end))))
1234 ;;; Related to the ess-eval-* commands, there are the ess-load
1235 ;;; commands. Need to add appropriate stuff...
1238 (defun ess-load-file (filename)
1239 "Load an S source file into an inferior ESS process."
1240 (interactive (list
1242 (and (eq major-mode 'ess-mode)
1243 (buffer-file-name))
1244 (expand-file-name
1245 (read-file-name "Load S file: " nil nil t)))))
1246 (ess-make-buffer-current)
1247 (if (ess-ddeclient-p);; << FIXME: rather ess-microsoft-p
1248 (setq filename (ess-replace-in-string filename "[\\]" "/")))
1249 (let ((source-buffer (get-file-buffer filename)))
1250 (if (ess-check-source filename)
1251 (error "Buffer %s has not been saved" (buffer-name source-buffer)))
1252 ;; else
1253 (if (ess-ddeclient-p)
1254 (ess-load-file-ddeclient filename)
1256 ;; else: "normal", non-DDE behavior:
1258 ;; Find the process to load into
1259 (if source-buffer
1260 (save-excursion
1261 (set-buffer source-buffer)
1262 (ess-force-buffer-current "Process to load into: ")
1263 (ess-check-modifications)))
1264 (let ((errbuffer (ess-create-temp-buffer ess-error-buffer-name))
1265 error-occurred nomessage)
1266 (ess-command (format inferior-ess-load-command filename) errbuffer) ;sleep ?
1267 (save-excursion
1268 (set-buffer errbuffer)
1269 (goto-char (point-max))
1270 (setq error-occurred (re-search-backward ess-dump-error-re nil t))
1271 (setq nomessage (= (buffer-size) 0)))
1272 (if error-occurred
1273 (message "Errors: Use %s to find error."
1274 (substitute-command-keys
1275 "\\<inferior-ess-mode-map>\\[ess-parse-errors]"))
1276 ;; Load did not cause an error
1277 (if nomessage (message "Load successful.")
1278 ;; There was a warning message from S
1279 (ess-display-temp-buffer errbuffer))
1280 ;; Consider deleting the file
1281 (let ((skdf (if source-buffer
1282 (save-excursion
1283 (set-buffer source-buffer)
1284 ess-keep-dump-files)
1285 ess-keep-dump-files))) ;; global value
1286 (cond
1287 ((null skdf)
1288 (delete-file filename))
1289 ((memq skdf '(check ask))
1290 (let ((doit (y-or-n-p (format "Delete %s " filename))))
1291 (if doit (delete-file filename))
1292 (and source-buffer
1293 (local-variable-p 'ess-keep-dump-files source-buffer)
1294 (save-excursion
1295 (set-buffer source-buffer)
1296 (setq ess-keep-dump-files doit)))))))
1297 (ess-switch-to-ESS t))))))
1299 \f ; Inferior S mode
1300 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1301 ;;;; In this section:
1302 ;;;;
1303 ;;;; * The major mode inferior-ess-mode
1304 ;;;; * Process handling code
1305 ;;;; * Completion code
1306 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1308 ;;*;; Major mode definition
1310 (if inferior-ess-mode-map
1313 (cond ((featurep 'xemacs)
1314 ;; Code for XEmacs
1315 (setq inferior-ess-mode-map (make-keymap))
1316 (set-keymap-parent inferior-ess-mode-map comint-mode-map))
1317 ((not (featurep 'xemacs))
1318 ;; Code for GNU Emacs
1319 (setq inferior-ess-mode-map (cons 'keymap comint-mode-map))))
1321 ;; Use syntax valid *both* for GNU emacs and XEmacs :
1322 (define-key inferior-ess-mode-map "\r" 'inferior-ess-send-input)
1323 (define-key inferior-ess-mode-map "\C-a" 'comint-bol)
1324 (define-key inferior-ess-mode-map "\M-\r" 'ess-transcript-send-command-and-move)
1325 (define-key inferior-ess-mode-map "\C-c\C-l" 'ess-load-file)
1326 ;; the above OVERRIDES comint-dynamic-list-input-ring --> re-assign:
1327 (define-key inferior-ess-mode-map "\C-c\M-l" 'comint-dynamic-list-input-ring)
1328 (define-key inferior-ess-mode-map "\C-c`" 'ess-parse-errors)
1329 (define-key inferior-ess-mode-map "\C-c\C-d" 'ess-dump-object-into-edit-buffer)
1330 (define-key inferior-ess-mode-map "\C-c\C-v" 'ess-display-help-on-object)
1331 (define-key inferior-ess-mode-map "\C-c\C-q" 'ess-quit)
1332 (define-key inferior-ess-mode-map "\C-c\C-t" 'ess-execute)
1333 (define-key inferior-ess-mode-map "\C-c\C-s" 'ess-execute-search)
1334 (define-key inferior-ess-mode-map "\C-c\C-x" 'ess-execute-objects)
1335 ;;(define-key inferior-ess-mode-map "\C-c\C-a" 'ess-execute-attach)
1336 (define-key inferior-ess-mode-map "\C-c\034" 'ess-abort) ; \C-c\C-backslash
1337 (define-key inferior-ess-mode-map "\C-c\C-z" 'ess-abort) ; mask comint map
1338 (define-key inferior-ess-mode-map "\C-d" 'delete-char) ; EOF no good in S
1339 (define-key inferior-ess-mode-map "\t" 'comint-dynamic-complete)
1340 (define-key inferior-ess-mode-map "\C-c\t" 'ess-complete-object-name)
1341 (define-key inferior-ess-mode-map "\M-\t" 'comint-replace-by-expanded-filename)
1342 (define-key inferior-ess-mode-map "\M-?" 'ess-list-object-completions)
1343 (define-key inferior-ess-mode-map "\C-c\C-k" 'ess-request-a-process))
1345 (easy-menu-define
1346 inferior-ess-mode-menu inferior-ess-mode-map
1347 "Menu for use in Inferior S mode"
1348 '("iESS"
1349 ["What is this? (beta)" ess-mouse-me t]
1350 ["Resynch S completions" ess-resynch t]
1351 ["Quit S" ess-quit t]
1352 ["Display search list" ess-execute-search t]
1353 ["Display object list" ess-execute-objects t]
1354 ["Get help on S object" ess-display-help-on-object t]
1355 ["Enter S command" ess-execute t]
1356 ["Attach directory" ess-execute-attach t]
1357 "------"
1358 ["Send and move" ess-transcript-send-command-and-move t]
1359 ["Copy command" comint-copy-old-input t]
1360 ["Send command" inferior-ess-send-input t]
1361 "------"
1362 ["Jump to Error" ess-parse-errors t]
1363 ;; need a toggle switch for above, AJR.
1364 ["Load source file" ess-load-file t]
1365 ["Edit S Object" ess-dump-object-into-edit-buffer t]
1366 "------"
1367 ["Describe" describe-mode t]
1368 ["About" (ess-goto-info "Entering Commands") t]
1369 ["Send bug report" ess-submit-bug-report t]
1373 (defun inferior-ess-mode-xemacs-menu ()
1374 "Hook to install `ess-mode' menu for XEmacs (w/ easymenu)."
1375 (if 'inferior-ess-mode
1376 (easy-menu-add inferior-ess-mode-menu)
1377 (easy-menu-remove inferior-ess-mode-menu)))
1379 (if (string-match "XEmacs" emacs-version)
1380 (add-hook 'inferior-ess-mode-hook 'inferior-ess-mode-xemacs-menu))
1382 (if ess-mode-minibuffer-map nil
1384 (cond ((featurep 'xemacs)
1385 ;; Code for XEmacs
1386 (setq ess-mode-minibuffer-map (make-keymap))
1387 (set-keymap-parent ess-mode-minibuffer-map minibuffer-local-map)))
1389 (cond ((not (featurep 'xemacs))
1390 ;; Code for Emacs
1391 (setq ess-mode-minibuffer-map (cons 'keymap minibuffer-local-map))))
1393 (define-key ess-mode-minibuffer-map "\t" 'ess-complete-object-name)
1394 (define-key ess-mode-minibuffer-map "\C-c\C-s" 'ess-execute-search)
1395 (define-key ess-mode-minibuffer-map "\C-c\C-x" 'ess-execute-objects))
1397 (defun inferior-ess-mode ()
1398 "Major mode for interacting with an inferior ESS process.
1399 Runs an S interactive job as a subprocess of Emacs, with I/O through an
1400 Emacs buffer. Variable `inferior-ess-program' controls which S
1401 is run.
1403 Commands are sent to the ESS process by typing them, and pressing
1404 \\[inferior-ess-send-input]. Pressing \\[complete-dynamic-complete]
1405 completes known object names or filenames, as appropriate. Other
1406 keybindings for this mode are:
1408 \\{inferior-ess-mode-map}
1410 When editing S objects, the use of \\[ess-load-file] is advocated.
1411 `ess-load-file' keeps source files (if `ess-keep-dump-files' is non-nil) in
1412 the directory specified by `ess-source-directory', with the
1413 filename chosen according to `ess-dump-filename-template'. When a file is
1414 loaded, `ess-mode' parses error messages and jumps to the appropriate file
1415 if errors occur. The ess-eval- commands do not do this.
1417 Customization: Entry to this mode runs the hooks on `comint-mode-hook' and
1418 `inferior-ess-mode-hook' (in that order).
1420 You can send text to the inferior ESS process from other buffers containing
1421 S source. The key bindings of these commands can be found by typing
1422 C-h m (help for mode) in the other buffers.
1423 `ess-eval-region' sends the current region to the ESS process.
1424 `ess-eval-buffer' sends the current buffer to the ESS process.
1425 `ess-eval-function' sends the current function to the ESS process.
1426 `ess-eval-line' sends the current line to the ESS process.
1427 `ess-beginning-of-function' and `ess-end-of-function' move the point to
1428 the beginning and end of the current S function.
1429 `ess-switch-to-ESS' switches the current buffer to the ESS process buffer.
1430 `ess-switch-to-end-of-ESS' switches the current buffer to the ESS process
1431 buffer and puts point at the end of it.
1433 `ess-eval-region-and-go', `ess-eval-buffer-and-go',
1434 `ess-eval-function-and-go', and `ess-eval-line-and-go' switch to the S
1435 process buffer after sending their text.
1436 `ess-dump-object-into-edit-buffer' moves an S object into a temporary file
1437 and buffer for editing
1438 `ess-load-file' sources a file of commands to the ESS process.
1440 Commands:
1441 Return after the end of the process' output sends the text from the
1442 end of process to point.
1443 Return before the end of the process' output copies the sexp ending at point
1444 to the end of the process' output, and sends it.
1445 Delete converts tabs to spaces as it moves back.
1446 C-M-q does Tab on each line starting within following expression.
1447 Paragraphs are separated only by blank lines. Crosshatches start comments.
1448 If you accidentally suspend your process, use \\[comint-continue-subjob]
1449 to continue it."
1450 (interactive)
1451 ;;(message " at very beg. of (inferior-ess-mode): inf.-ess-prompt= %s"
1452 ;; inferior-ess-prompt)
1453 (comint-mode)
1456 ;; SJE: is this the proper place for setting inferior-ess-prompt,
1457 ;; rather than within ess-multi? Tony - have you remembered yet
1458 ;; about the setq-default, as I changed it back to setq.
1459 (setq inferior-ess-prompt
1460 ;; shouldn't be setq-default! And I've
1461 ;; forgotten why! (AJR)
1462 ;; Do not anchor to bol with `^'
1463 (concat "\\("
1464 inferior-ess-primary-prompt
1465 "\\|"
1466 inferior-ess-secondary-prompt
1467 "\\)"))
1468 (setq comint-prompt-regexp (concat "^" inferior-ess-prompt))
1469 (setq major-mode 'inferior-ess-mode)
1470 (setq mode-name "iESS") ;(concat "iESS:" ess-dialect))
1471 (setq mode-line-process
1472 '(" [" ess-local-process-name "]: %s"))
1473 (use-local-map inferior-ess-mode-map)
1474 (if ess-mode-syntax-table
1475 (set-syntax-table ess-mode-syntax-table)
1476 ;; FIXME: need to do something if not set! Get from the proper place!
1478 (add-hook 'comint-input-filter-functions 'ess-search-path-tracker)
1479 (setq comint-get-old-input 'inferior-ess-get-old-input)
1481 (ess-write-to-dribble-buffer
1482 (format "(i-ess 1): buf=%s, lang=%s, comint..echo=%s, comint..sender=%s,\n"
1483 (current-buffer) ess-language
1484 comint-process-echoes comint-input-sender))
1486 (make-local-variable 'comint-process-echoes)
1487 (make-local-variable 'comint-input-sender)
1488 (set (make-local-variable 'process-connection-type) t)
1490 ;; Configuration for SAS/XLispStat input handling
1491 ;; We set comint-process-echoes to t because inferior-ess-input-sender
1492 ;; recopies the input. If comint-process-echoes was *meant* to be t ...
1494 ;; except that XLS doesn't like it. This is an ugly hack that ought
1495 ;; to go into the dialect configuration...
1496 (setq comint-process-echoes (not (member ess-language '("SAS" "XLS"))))
1498 ;; Configuration for S/R input handling
1499 ;; AJR: add KH's fix. This is ugly, change to do it right.
1500 ;; i.e. put it as a buffer local var, in S or R defuns...
1502 ;; SJE: Do you mean that we should put this code into (R) and the S
1503 ;; dialects? I agree that would be cleaner. e.g. in essd-r.el, for
1504 ;; the R defun we could have:
1505 ;; (inferior-ess r-start-args) ;; (R)
1506 ;; (setq comint-input-sender 'inferior-R-input-sender) ;; <<- add this.
1507 (if (or (string= ess-language "S"))
1508 (cond
1509 ((string= ess-dialect "R")
1510 (setq comint-input-sender 'inferior-R-input-sender))
1511 ( (member ess-dialect '("S3" "S4" "S+3" "S+4" "S+5" "S+6" "S"))
1512 (setq comint-input-sender 'inferior-ess-input-sender))))
1514 ;; Configuration for Stata input handling
1515 ;; AJR: Stata is hell. This is the primary configuration point.
1516 (when (string= ess-language "STA")
1517 (setq comint-input-sender 'inferior-ess-input-sender) ; was STA
1518 (setq comint-process-echoes t))
1520 ;; Configuration for Omegahat input handling
1521 ;; SJE: cleanup
1522 (when (string= ess-language "OMG")
1523 ;; the following doesn't exist (until needed?)
1524 ;;(setq comint-input-sender 'inferior-OMG-input-sender)
1525 (setq comint-process-echoes nil))
1527 (ess-write-to-dribble-buffer
1528 (format "(i-ess 2): buf=%s, lang=%s, comint..echo=%s, comint..sender=%s,\n"
1529 (current-buffer) ess-language
1530 comint-process-echoes comint-input-sender))
1531 ;; Font-lock support
1532 ;; AJR: This (the following local-var is already the case!
1533 ;; KH sez: only in XEmacs :-(. (& Emacs 22.1, SJE).
1534 (set (make-local-variable 'font-lock-defaults)
1535 '(inferior-ess-font-lock-keywords nil nil ((?' . "."))))
1537 ;; SJE 2007-06-28: Emacs 22.1 has a bug in that comint-mode will set
1538 ;; this variable to t, when we need it to be nil. The Emacs 22
1539 ;; solution to this bug is to use define-dervied-mode to derive
1540 ;; inferior-ess-mode from comint-mode. Not sure if we can go down
1541 ;; that route yet. I've used the when condition so that if the var
1542 ;; is nil, don't bother setting it -- as setting it will make a new
1543 ;; local var.
1544 (when font-lock-keywords-only
1545 (setq font-lock-keywords-only nil))
1547 (ess-setq-vars-local ess-customize-alist) ; (current-buffer))
1549 (ess-write-to-dribble-buffer
1550 (format "(i-ess 3): curr-buf=%s, comint..echo=%s, comint..sender=%s,\n"
1551 (current-buffer) comint-process-echoes comint-input-sender))
1553 ;;; Completion support ----------------
1555 ;; SJE: comint-dynamic-complete-functions is regarded as a hook, rather
1556 ;; than a regular variable. Note order of completion (thanks David Brahm):
1558 (add-hook 'comint-dynamic-complete-functions
1559 'ess-complete-filename 'append 'local)
1560 (add-hook 'comint-dynamic-complete-functions
1561 'ess-complete-object-name 'append 'local)
1562 (add-hook 'comint-dynamic-complete-functions
1563 'comint-replace-by-expanded-history 'append 'local)
1565 ;; When a hook is buffer-local, the dummy function `t' is added to
1566 ;; indicate that the functions in the global value of the hook
1567 ;; should also be run. SJE: I have removed this, as I think it
1568 ;; interferes with our normal completion.
1569 (remove-hook 'comint-dynamic-complete-functions 't 'local)
1571 ;; MM: in *R* in GNU emacs and in Xemacs, the c*-dyn*-compl*-fun* are now
1572 ;; (ess-complete-filename
1573 ;; ess-complete-object-name
1574 ;; comint-replace-by-expanded-history)
1576 ;; However this fails in Xemacs 21.4.17 where the value in *shell* is
1577 ;; -- the same as in GNU emacs *shell* :
1578 ;; (comint-replace-by-expanded-history shell-dynamic-complete-environment-variable shell-dynamic-complete-command shell-replace-by-expanded-directory comint-dynamic-complete-filename)
1580 ;; and the (Xemacs) global 'Default-value' is
1581 ;; (comint-replace-by-expanded-history comint-dynamic-complete-filename)
1583 ;; (setq comint-completion-addsuffix nil) ; To avoid spaces after filenames
1584 ;; KH: next 2 lines solve.
1585 (set (make-local-variable 'comint-completion-addsuffix)
1586 (cons "/" ""))
1588 (setq comint-input-autoexpand t) ; Only for completion, not on input.
1590 ;;; Keep <tabs> out of the code.
1591 (set (make-local-variable 'indent-tabs-mode) nil)
1593 (set (make-local-variable 'paragraph-start)
1594 (concat inferior-ess-primary-prompt "\\|\^L"))
1595 (set (make-local-variable 'paragraph-separate) "\^L")
1597 ;; SJE Tue 28 Dec 2004: do not attempt to load object name db.
1598 ;; (ess-load-object-name-db-file)
1599 ;; (sleep-for 0.5)
1600 (make-local-variable 'kill-buffer-hook)
1601 (add-hook 'kill-buffer-hook 'ess-kill-buffer-function)
1602 (run-hooks 'inferior-ess-mode-hook)
1603 (message
1604 (concat (substitute-command-keys
1605 "Type \\[describe-mode] for help on ESS version ")
1606 ess-version)))
1608 ;;*;; Commands used exclusively in inferior-ess-mode
1610 ;;;*;;; Main user commands
1612 (defun inferior-ess-input-sender (proc string)
1613 (ess-eval-linewise (concat string "\n") nil nil ess-eval-empty))
1615 (defun inferior-STA-input-sender (proc string)
1616 (ess-eval-linewise (concat string "\n") t t))
1618 ;;> <PD writes>:
1619 ;;> Also, invoking help() from the command line may lead to confusing
1620 ;;> output, somewhat worse with R than with S. You're not really supposed
1621 ;;> to do that (use C-c C-v to invoke ess-display-help-on-object), but it's
1622 ;;> an obvious newcomer's mistake.
1624 ;;> (I wonder: could the elisp-code not quite easily recognize help
1625 ;;> calls (at least in the ?xxx form) and do the right thing automagically?)
1627 ;;> As promised, here is a quick hack:
1628 ;; ___hack much improved by MM___ , both help(.) and ?... now work
1629 ;; FIXME: Note that '??' nicely works in *R*, but
1630 ;; 'type ? topic' doesn't use ess-help {but display in *R*}
1631 (defconst inferior-R-1-input-help (format "^ *help *(%s)" ess-help-arg-regexp))
1632 (defconst inferior-R-2-input-help (format "^ *\\? *%s" ess-help-arg-regexp))
1633 (defconst inferior-R-page (format "^ *page *(%s)" ess-help-arg-regexp))
1635 (defun inferior-R-input-sender (proc string)
1636 ;; REALLY only for debugging: this S_L_O_W_S D_O_W_N [here AND below]
1637 ;;(ess-write-to-dribble-buffer (format "(inf..-R-..): string='%s'; " string))
1638 ;; rmh: 2002-01-12 catch page() in R
1639 (let ((help-string (or (string-match inferior-R-1-input-help string)
1640 (string-match inferior-R-2-input-help string)))
1641 (page-string (string-match inferior-R-page string)))
1642 (if (or help-string page-string)
1643 (let* ((string2 (match-string 2 string))
1644 (string2-rt (concat string2 ".rt")))
1645 ;; (ess-write-to-dribble-buffer (format " new string='%s'\n" string2))
1646 (beginning-of-line)
1647 (if (looking-at inferior-ess-primary-prompt)
1648 (progn
1649 (end-of-line)
1650 (insert-before-markers string)) ;; emacs 21.0.105 and older
1651 (delete-backward-char 1)) ;; emacs 21.0.106 and newer
1652 (if page-string
1653 (progn
1654 (ess-command (concat string2 "\n")
1655 (get-buffer-create (concat string2 ".rt")))
1656 (ess-eval-linewise "\n")
1657 (switch-to-buffer-other-window string2-rt)
1658 (R-transcript-mode))
1659 (ess-display-help-on-object
1660 (if (string= string2 "") "help" string2))
1661 (ess-eval-linewise "\n")))
1662 ;; else: normal command
1663 (inferior-ess-input-sender proc string))))
1666 (defun inferior-ess-send-input ()
1667 "Sends the command on the current line to the ESS process."
1668 (interactive)
1669 (ess-make-buffer-current)
1670 (run-hooks 'ess-send-input-hook)
1671 (comint-send-input)
1672 (setq ess-object-list nil)) ;; Will be reconstructed from cache if needs be
1674 (defun inferior-ess-get-old-input ()
1675 "Return the ESS command surrounding point."
1676 (save-excursion
1677 (beginning-of-line)
1678 (if (not (looking-at inferior-ess-prompt))
1679 (ess-error "No command on this line."))
1680 (if (looking-at inferior-ess-primary-prompt) nil
1681 (re-search-backward (concat "^" inferior-ess-primary-prompt)))
1682 (comint-skip-prompt)
1683 (let (command
1684 (beg (point)))
1685 (end-of-line)
1686 (setq command (buffer-substring beg (point)))
1687 (forward-line 1)
1688 (while (looking-at inferior-ess-secondary-prompt)
1689 (comint-skip-prompt)
1690 (setq beg (point))
1691 (end-of-line)
1692 (setq command (concat command "\n" (buffer-substring beg (point))))
1693 (forward-line 1))
1694 (forward-line -1)
1695 (setq ess-temp-point (point))
1696 command)))
1698 ;;;*;;; Hot key commands
1700 (defun ess-execute-objects (posn)
1701 "Send the objects() command to the ESS process.
1702 By default, gives the objects at position 1.
1703 A prefix argument toggles the meaning of `ess-execute-in-process-buffer'.
1704 A prefix argument of 2 or more means get objects for that position.
1705 A negative prefix argument gets the objects for that position
1706 and toggles `ess-execute-in-process-buffer' as well."
1707 (interactive "P")
1708 (ess-make-buffer-current)
1709 (let* ((num-arg (if (listp posn)
1710 (if posn -1 1)
1711 (prefix-numeric-value posn)))
1712 (the-posn (if (< num-arg 0) (- num-arg) num-arg))
1713 (invert (< num-arg 0))
1714 (the-command (format inferior-ess-objects-command the-posn ".*"))
1715 (the-message (concat ">>> Position "
1716 (number-to-string the-posn)
1717 " ("
1718 (nth (1- the-posn) (ess-search-list))
1719 ")\n")))
1720 (ess-execute the-command invert "S objects" the-message)))
1722 ;;;; S4 Version
1723 ;;;; soup-up the interactive usage: allow modifications to a default pattern
1724 ;;(defun ess-execute-objects (posn)
1725 ;; "Send the `inferior-ess-objects-command' to the ESS process.
1726 ;;No prefix argument uses position 1 and pattern inferior-ess-objects-pattern.
1727 ;;A nonnegative prefix gets objects for that position and prompts for
1728 ;; the pattern.
1729 ;;A negative prefix also toggles ess-execute-in-process-buffer."
1730 ;; (interactive "P")
1731 ;; (ess-make-buffer-current)
1732 ;; (let* ((num-arg (if (listp posn) 1
1733 ;; (prefix-numeric-value posn)))
1734 ;; (the-posn (if (< num-arg 0) (- num-arg) num-arg))
1735 ;; (invert (< num-arg 0))
1736 ;; (pattern (if current-prefix-arg (read-string "Pattern (.*): ")
1737 ;; inferior-ess-objects-pattern))
1738 ;; (pattern (if (string= pattern "") ".*" pattern))
1739 ;; (the-command (format inferior-ess-objects-command the-posn pattern))
1740 ;; (the-message (concat ">>> Position "
1741 ;; the-posn
1742 ;; " ("
1743 ;; (nth (1- the-posn) (ess-search-list))
1744 ;; ") (pattern = "
1745 ;; pattern
1746 ;; ")\n")))
1747 ;; (ess-execute the-command invert "S objects" the-message)))
1749 (defun ess-execute-search (invert)
1750 "Send the `inferior-ess-search-list-command' command to the `ess-language' process.
1751 [search(..) in S]"
1752 (interactive "P")
1753 (ess-execute inferior-ess-search-list-command invert "S search list"))
1755 (defun ess-execute-attach (dir &optional posn)
1756 "Attach a directory in the `ess-language' process with the attach() command.
1757 When used interactively, user is prompted for DIR to attach and
1758 prefix argument is used for POSN (or 2, if absent.)
1759 Doesn't work for data frames."
1760 (interactive "DAttach directory: \nP")
1761 (ess-execute (concat "attach(\""
1762 (directory-file-name (expand-file-name dir))
1763 "\""
1764 (if posn (concat "," (number-to-string
1765 (prefix-numeric-value posn))))
1766 ")") 'buffer)
1767 (setq ess-sp-change t))
1769 (defun ess-execute-screen-options ()
1770 "Cause S to set the \"width\" option to 1 less than the frame width.
1771 Also sets the \"length\" option to 99999.
1772 This is a good thing to put in `ess-post-run-hook' --- for the S dialects."
1773 (interactive)
1774 (if (string= ess-language "S")
1775 (let ((ess-current-process-name)); local, used as S-process buffer below
1776 ;; when run inside an ESS process buffer, use that one
1777 (if (and (comint-check-proc (current-buffer)); has running proc
1778 (memq major-mode '(inferior-ess-mode)))
1779 (setq ess-current-process-name ess-local-process-name))
1781 (ess-eval-linewise (format "options(width=%d,length=99999)"
1782 (1- (window-width)))
1783 nil nil nil 'wait-prompt))))
1785 (defun ess-execute (command &optional invert buff message)
1786 "Send a command to the ESS process.
1787 A newline is automatically added to COMMAND. Prefix arg (or second arg
1788 INVERT) means invert the meaning of
1789 `ess-execute-in-process-buffer'. If INVERT is 'buffer, output is
1790 forced to go to the process buffer. If the output is going to a
1791 buffer, name it *BUFF*. This buffer is erased before use. Optional
1792 fourth arg MESSAGE is text to print at the top of the buffer (defaults
1793 to the command if BUFF is not given.)"
1794 (interactive (list
1795 (read-from-minibuffer "Execute> "
1797 ess-mode-minibuffer-map)
1798 current-prefix-arg))
1799 (ess-make-buffer-current)
1800 (let ((the-command (concat command "\n"))
1801 (buff-name (concat "*" (or buff "ess-output") "*"))
1802 (in-pbuff (if invert (or (eq invert 'buffer)
1803 (not ess-execute-in-process-buffer))
1804 ess-execute-in-process-buffer)))
1805 (if in-pbuff
1806 (ess-eval-linewise the-command)
1807 (let ((buff (ess-create-temp-buffer buff-name)))
1808 (save-excursion
1809 (set-buffer buff)
1810 (ess-command the-command (get-buffer buff-name));; sleep?
1811 (goto-char (point-min))
1812 (if message (insert message)
1813 (if buff nil
1814 ;; Print the command in the buffer if it has not been
1815 ;; given a special name
1816 (insert "> " the-command)))
1817 ;;(make-local-variable 'ess-local-process-name)
1818 (setq ess-local-process-name ess-current-process-name))
1819 (ess-display-temp-buffer buff)))))
1821 ;;;*;;; Quitting
1823 (defun ess-quit ()
1824 "Issue an exiting command to the inferior process, additionally
1825 also running \\[ess-cleanup]. For R, runs \\[ess-quit-r], see there."
1826 (interactive)
1827 (if (string-equal ess-dialect "R")
1828 (ess-quit-r)
1829 (ess-force-buffer-current "Process to quit: ")
1830 (ess-make-buffer-current)
1831 (let ((sprocess (get-ess-process ess-current-process-name)))
1832 (if (not sprocess) (error "No ESS process running."))
1833 (when (yes-or-no-p (format "Really quit ESS process %s? " sprocess))
1834 (ess-cleanup)
1835 (goto-char (marker-position (process-mark sprocess)))
1836 (insert inferior-ess-exit-command)
1837 (process-send-string sprocess inferior-ess-exit-command)
1838 ;;SJE - suggest no need to rename buffer upon exit.
1839 ;;(rename-buffer (concat (buffer-name) "-exited") t)
1840 ))))
1842 (defun ess-quit-r ()
1843 "Issue an exiting command to an inferior R process, and optionally clean up.
1844 This version is for killing *R* processes; it asks the extra question
1845 regarding whether the workspace image should be saved."
1846 (ess-force-buffer-current "Process to quit: ")
1847 (ess-make-buffer-current)
1848 (let (cmd
1849 ;;Q response
1850 (sprocess (get-ess-process ess-current-process-name)))
1851 (if (not sprocess) (error "No ESS process running."))
1852 ;;Q (setq response (completing-read "Save workspace image? "
1853 ;;Q '( ( "yes".1) ("no" . 1) ("cancel" . 1))
1854 ;;Q nil t))
1855 ;;Q (if (string-equal response "")
1856 ;;Q (setq response "default")); which will ask again (in most situations)
1857 ;;Q (unless (string-equal response "cancel")
1858 (ess-cleanup)
1859 ;;Q (setq cmd (format "q(\"%s\")\n" response))
1860 (setq cmd "q()\n")
1861 (goto-char (marker-position (process-mark sprocess)))
1862 (process-send-string sprocess cmd)
1863 ;;(rename-buffer (concat (buffer-name) "-exited") t)
1864 ;;Q )
1867 (defun ess-abort ()
1868 "Kill the ESS process, without executing .Last or terminating devices.
1869 If you want to finish your session, use \\[ess-quit] instead."
1870 ;;; Provided as a safety measure over the default binding of C-c C-z in
1871 ;;; comint-mode-map.
1872 (interactive)
1873 (ding)
1874 (message "WARNING: \\[inferior-ess-exit-command] will not be executed and graphics devices won't finish properly!")
1875 (sit-for 2)
1876 (if (yes-or-no-p "Still abort? ")
1877 (comint-quit-subjob)
1878 (message "Good move.")))
1880 (defun ess-cleanup ()
1881 "Possibly kill or offer to kill, depending on the value of
1882 `ess-S-quit-kill-buffers-p', all buffers associated with this ESS process.
1883 Leaves you in the ESS process buffer. It's a good idea to run this
1884 before you quit. It is run automatically by \\[ess-quit]."
1885 (interactive)
1886 (let ((the-procname (or (ess-make-buffer-current) ess-local-process-name)))
1887 (if the-procname nil
1888 (error "I don't know which ESS process to clean up after!"))
1890 (or (eq ess-S-quit-kill-buffers-p t)
1891 (and
1892 (eq ess-S-quit-kill-buffers-p 'ask)
1893 (y-or-n-p
1894 (format
1895 "Delete all buffers associated with process %s? " the-procname))))
1896 (save-excursion
1897 (mapcar '(lambda (buf)
1898 (set-buffer buf)
1899 ;; Consider buffers for which
1900 ;; ess-local-process-name is the same as
1901 ;; the-procname
1902 (if (and (not (get-buffer-process buf))
1903 ess-local-process-name
1904 (equal ess-local-process-name the-procname))
1905 (kill-buffer buf)))
1906 (buffer-list))))
1907 (ess-switch-to-ESS nil)))
1909 (defun ess-kill-buffer-function nil
1910 "Function run just before an ESS process buffer is killed."
1911 ;; This simply deletes the buffers process to avoid an Emacs bug
1912 ;; where the sentinel is run *after* the buffer is deleted
1913 (let ((proc (get-buffer-process (current-buffer))))
1914 (if proc (delete-process proc))))
1916 ;;*;; Object name completion
1918 ;;;*;;; The user completion command
1919 (defun ess-complete-object-name (&optional listcomp)
1920 "Perform completion on `ess-language' object preceding point.
1921 The object is compared against those objects known by
1922 `ess-get-object-list' and any additional characters up to ambiguity are
1923 inserted. Completion only works on globally-known objects (including
1924 elements of attached data frames), and thus is most suitable for
1925 interactive command-line entry, and not so much for function editing
1926 since local objects (e.g. argument names) aren't known.
1928 Use \\[ess-resynch] to re-read the names of the attached directories.
1929 This is done automatically (and transparently) if a directory is
1930 modified (S only!), so the most up-to-date list of object names is always
1931 available. However attached dataframes are *not* updated, so this
1932 command may be necessary if you modify an attached dataframe.
1934 If ARG is non-nil, no completion is attempted, but the available
1935 completions are listed [__UNIMPLEMENTED__]."
1936 (interactive "P");; FIXME : the `listcomp' argument is NOT used
1937 (ess-make-buffer-current)
1938 (if (memq (char-syntax (preceding-char)) '(?w ?_))
1939 (let* ((comint-completion-addsuffix nil)
1940 (end (point))
1941 (buffer-syntax (syntax-table))
1942 (beg (unwind-protect
1943 (save-excursion
1944 (set-syntax-table ess-mode-syntax-table)
1945 (backward-sexp 1)
1946 (point))
1947 (set-syntax-table buffer-syntax)))
1948 (full-prefix (buffer-substring beg end))
1949 (pattern full-prefix)
1950 ;; See if we're indexing a list with `$'
1951 (listname (if (string-match "\\(.+\\)\\$\\(\\(\\sw\\|\\s_\\)*\\)$"
1952 full-prefix)
1953 (progn
1954 (setq pattern
1955 (if (not (match-beginning 2)) ""
1956 (substring full-prefix
1957 (match-beginning 2)
1958 (match-end 2))))
1959 (substring full-prefix (match-beginning 1)
1960 (match-end 1)))))
1961 ;; are we trying to get a slot via `@' ?
1962 (classname (if (string-match "\\(.+\\)@\\(\\(\\sw\\|\\s_\\)*\\)$"
1963 full-prefix)
1964 (progn
1965 (setq pattern
1966 (if (not (match-beginning 2)) ""
1967 (substring full-prefix
1968 (match-beginning 2)
1969 (match-end 2))))
1970 (ess-write-to-dribble-buffer
1971 (format "(ess-C-O-Name : slots..) : patt=%s"
1972 pattern))
1973 (substring full-prefix (match-beginning 1)
1974 (match-end 1)))))
1975 (components (if listname
1976 (ess-object-names listname)
1977 (if classname
1978 (ess-slot-names classname)
1979 ;; Default case: It hangs here when
1980 ;; options(error=recoves) :
1981 (ess-get-object-list ess-current-process-name)))))
1982 ;; always return a non-nil value to prevent history expansions
1983 (or (comint-dynamic-simple-complete pattern components) 'none))))
1985 (defun ess-list-object-completions nil
1986 "List all possible completions of the object name at point."
1987 (interactive)
1988 (ess-complete-object-name t));; FIXME: NOT WORKING since argument is unused!
1990 ;;;*;;; Support functions
1991 (defun ess-extract-onames-from-alist (alist posn &optional force)
1992 "Return the object names in position POSN of ALIST.
1993 ALIST is an alist like `ess-sl-modtime-alist'. POSN should be in 1 .. (length
1994 ALIST). If optional third arg FORCE is t, the corresponding element
1995 of the search list is re-read. Otherwise it is only re-read if it's a
1996 directory and has been modified since it was last read."
1997 (let* ((entry (nth (1- posn) alist))
1998 (dir (car entry))
1999 (timestamp (car (cdr entry)))
2000 (new-modtime (ess-dir-modtime dir)))
2001 ;; Refresh the object listing if necessary
2002 (if (or force (not (equal new-modtime timestamp)))
2003 (setcdr (cdr entry) (ess-object-names dir posn)))
2004 (cdr (cdr entry))))
2006 (defun ess-dir-modtime (dir)
2007 "Return the last modtime if DIR is a directory, and nil otherwise."
2008 (and ess-filenames-map
2009 (file-directory-p dir)
2010 (nth 5 (file-attributes dir))))
2012 (defun ess-object-modtime (object)
2013 "Return the modtime of the S object OBJECT (a string).
2014 Searches along the search list for a file named OBJECT and returns its modtime
2015 Returns nil if that file cannot be found, i.e., for R or any non-S language!"
2016 (let ((path (ess-search-list))
2017 result)
2018 (while (and (not result) path)
2019 (setq result (file-attributes
2020 (concat (file-name-as-directory (car path))
2021 object)))
2022 (setq path (cdr path)))
2023 (nth 5 result)))
2025 (defun ess-modtime-gt (mod1 mod2)
2026 "Return t if MOD1 is later than MOD2."
2027 (and mod1
2028 (or (> (car mod1) (car mod2))
2029 (and (= (car mod1) (car mod2))
2030 (> (car (cdr mod1)) (car (cdr mod2)))))))
2032 (defun ess-get-object-list (name)
2033 "Return a list of current S object names associated with process NAME,
2034 using `ess-object-list' if that is non-nil."
2035 (or ess-object-list ;; <<- MM: I think this is now always nil
2036 (save-excursion
2037 (set-buffer (process-buffer (get-ess-process name)))
2038 (ess-make-buffer-current)
2039 (ess-write-to-dribble-buffer (format "(get-object-list %s) .." name))
2040 (if (or (not ess-sl-modtime-alist) ess-sp-change)
2041 (progn (ess-write-to-dribble-buffer "--> (ess-get-modtime-list)\n")
2042 (ess-get-modtime-list))
2043 ;;else
2044 (ess-write-to-dribble-buffer " using existing ess-*-alist\n")
2046 (let* ((alist ess-sl-modtime-alist)
2047 (i 2)
2048 (n (length alist))
2049 result)
2050 (ess-write-to-dribble-buffer (format " (length alist) : %d\n" n))
2051 ;; Always force a re-read of position 1 :
2052 (setq result (ess-extract-onames-from-alist alist 1 'force))
2053 (ess-write-to-dribble-buffer
2054 (format " have re-read pos=1: -> length %d\n" (length result)))
2055 ;; Re-read remaining directories if necessary.
2056 (while (<= i n)
2057 (setq result
2058 (append result
2059 (ess-extract-onames-from-alist alist i)))
2060 (setq i (1+ i)))
2061 (setq ess-object-list (ess-uniq-list result))))))
2063 (defun ess-get-words-from-vector (command)
2064 "Evaluate the S command COMMAND, which returns a character vector.
2065 Return the elements of the result of COMMAND as an alist of strings.
2066 COMMAND should have a terminating newline."
2067 (let ((tbuffer (get-buffer-create
2068 " *ess-names-list*")); initial space: disable-undo
2069 names)
2070 (save-excursion
2071 (set-buffer tbuffer)
2072 (ess-command command tbuffer 'sleep)
2073 (goto-char (point-min))
2074 (if ess-verbose (ess-write-to-dribble-buffer "ess-get-words-.. "))
2075 (if (not (looking-at "\\s-*\\[1\\]"))
2076 (progn (if ess-verbose
2077 (ess-write-to-dribble-buffer "not seeing \"[1]\".. "))
2078 (setq names nil)
2080 (goto-char (point-max))
2081 (while (re-search-backward "\"\\([^\"]*\\)\"" nil t)
2082 (setq names (cons (buffer-substring (match-beginning 1)
2083 (match-end 1)) names))))
2084 ;;DBG, do *not* (i.e., comment):
2085 (kill-buffer tbuffer)
2087 (if ess-verbose
2088 (ess-write-to-dribble-buffer
2089 (if (> (length names) 5)
2090 (format " |-> (length names)= %d\n" (length names))
2091 (format " |-> names= '%s'\n" names))))
2092 names))
2094 (defun ess-compiled-dir (dir)
2095 "Return non-nil if DIR is an S object directory with special files.
2096 I.e. if the filenames in DIR are not representative of the objects in DIR."
2097 (or (file-exists-p (concat (file-name-as-directory dir) "___nonfile"))
2098 (file-exists-p (concat (file-name-as-directory dir) "__BIGIN"))
2099 (file-exists-p (concat (file-name-as-directory dir) "___NONFI"))))
2101 (defun ess-object-names (obj &optional pos)
2102 "Return alist of S object names in directory (or object) OBJ.
2103 If OBJ is a directory name (begins with `/') returns a listing of that dir.
2104 This may use the search list position POS if necessary.
2105 If OBJ is an object name, returns result of S command names(OBJ).
2106 If OBJ is nil or not a directory, POS must be supplied, and objects(POS) is returned.
2107 In all cases, the value is an list of object names."
2109 ;; FIXME: in both cases below, use the same fallback "objects(POS)" -- merge!
2110 (if (and obj (file-accessible-directory-p obj))
2111 ;; Check the pre-compiled object list in ess-object-name-db first
2113 ;; FIXME: If used at all, ess-object-name-db should not only
2114 ;; ----- be used in the directory case !!
2115 (or (cdr-safe (assoc obj ess-object-name-db))
2116 ;; Take a directory listing
2117 (and ess-filenames-map
2118 ;; first try .Data subdirectory:
2119 ;;FIXME: move ".Data" or ``this function'' to essd-sp6.el etc:
2120 (let ((dir (concat (file-name-as-directory obj) ".Data")))
2121 (if (not (file-accessible-directory-p dir))
2122 (setq dir obj))
2123 (and (not (ess-compiled-dir dir))
2124 (directory-files dir))))
2125 ;; Get objects(pos) instead
2126 (and (or (ess-write-to-dribble-buffer
2127 (format "(ess-object-names ..): directory %s not used\n" obj))
2130 (ess-get-words-from-vector
2131 (format inferior-ess-objects-command pos))))
2132 ;; "else" should really give an error!
2133 ;; would need pos = which(obj = search())
2135 ;; else
2136 (or (and obj ;; want names(obj)
2137 (or (ess-write-to-dribble-buffer
2138 (format "(ess-object-names obj=%s): no directory - trying names\n"
2139 obj))
2141 (ess-get-words-from-vector
2142 (format inferior-ess-safe-names-command obj)))
2143 (and nil; must return nil
2144 (ess-write-to-dribble-buffer
2145 (format "(ess-object-names obj=%s): no dir.; -> objects()\n" obj)))
2146 ;; get objects(pos)
2147 (ess-get-words-from-vector
2148 (format inferior-ess-objects-command pos))))) ; had 2nd arg ".*"
2149 ; s4 needs 2
2150 ; args, rest only need 1 ?
2151 ; changes needed to allow for
2152 ; pattern argument to
2153 ; .SmodeObs
2155 (defun ess-slot-names (obj)
2156 "Return alist of S4 slot names of S4 object OBJ."
2157 (ess-get-words-from-vector (format "slotNames(%s)\n" obj)))
2160 ;;; SJE: Wed 29 Dec 2004 --- remove this function.
2161 ;;; rmh: Wed 5 Jan 2005 --- bring it back for use on Windows
2162 (defun ess-create-object-name-db ()
2163 "Create a database of object names in standard S directories. This
2164 database is saved in the file specified by `ess-object-name-db-file',
2165 and is loaded when `ess-mode' is loaded. It defines the variable
2166 `ess-object-name-db', which is used for completions.
2168 Before you call this function, modify the S search list so that it contains
2169 all the non-changing (i.e. system) S directories. All positions of the search
2170 list except for position 1 are searched and stored in the database.
2172 After running this command, you should move ess-namedb.el to a directory in
2173 the `load-path'."
2174 (interactive)
2175 (setq ess-object-name-db nil)
2176 (let ((search-list (cdr (ess-search-list)))
2177 (pos 2)
2178 name
2179 (buffer (get-buffer-create " *ess-db*"))
2180 (temp-object-name-db nil)
2181 (temp-object-name-db-file ess-object-name-db-file))
2183 (ess-write-to-dribble-buffer
2184 (format "(object db): search-list=%s \n " search-list))
2185 (while search-list
2186 (message "Searching %s" (car search-list))
2187 (setq temp-object-name-db (cons (cons (car search-list)
2188 (ess-object-names nil pos))
2189 temp-object-name-db))
2190 (setq search-list (cdr search-list))
2191 (ess-write-to-dribble-buffer
2192 (format "(object db): temp-obj-name-db=%s \n pos=%s"
2193 temp-object-name-db pos))
2194 (setq pos (1+ pos)))
2196 (save-excursion
2197 (set-buffer buffer)
2198 (erase-buffer)
2199 (insert "(setq ess-object-name-db '")
2200 (prin1 temp-object-name-db (current-buffer))
2201 (insert ")\n")
2202 (setq name (expand-file-name ess-object-name-db-file))
2203 (write-region (point-min) (point-max) name)
2204 (message "Wrote %s" name))
2205 (kill-buffer buffer)
2206 (setq ess-object-name-db temp-object-name-db)))
2208 (defun ess-resynch nil
2209 "Reread all directories/objects in variable `ess-search-list' to
2210 form completions."
2211 (interactive)
2212 (if (ess-make-buffer-current) nil
2213 (error "Not an ESS process buffer"))
2214 (setq ess-sl-modtime-alist nil)
2215 (setq ess-object-list nil)
2216 (setq ess-object-name-db nil) ; perhaps it would be better to reload?
2217 (setq ess-sp-change t)
2218 (ess-get-modtime-list))
2221 (defun ess-complete-filename ()
2222 "Do file completion only within strings, or when ! call is being used."
2223 (if (comint-within-quotes
2224 (1- (process-mark (get-buffer-process (current-buffer)))) (point))
2225 ;; (- comint-last-input-start 1) (point)) <- from S4 modeadds.
2226 ;; changed on 4/12/96 (dxsun)
2227 ;; This is sensible, but makes it hard to use history refs
2228 ;; (or
2229 ;; (save-excursion
2230 ;; (goto-char comint-last-input-start)
2231 ;; (looking-at "\\s-*!"))
2232 ;; (comint-within-quotes comint-last-input-start (point)))
2233 (progn
2234 ;;DBG (ess-write-to-dribble-buffer "ess-complete-f.name: within-quotes")
2235 (if (featurep 'xemacs) ;; work around Xemacs bug
2236 (comint-dynamic-complete-filename)
2237 ;; GNU emacs and correctly working Xemacs:
2238 (comint-replace-by-expanded-filename))
2239 ;; always return t if in a string
2240 t)))
2242 (defun ess-after-pathname-p nil
2243 ;; Heuristic: after partial pathname if it looks like we're in a
2244 ;; string, and that string looks like a pathname. Not the best for
2245 ;; use with unix() (or it's alias, !). Oh well.
2246 (save-excursion
2247 (save-match-data
2248 (let ((opoint (point)))
2249 (and (re-search-backward "\\(\"\\|'\\)[~/#$.a-zA-Z0-9][^ \t\n\"']*"
2250 nil t)
2251 (eq opoint (match-end 0)))))))
2253 ;;*;; Functions handling the search list
2255 (defun ess-search-list ()
2256 "Return the current search list as a list of strings.
2257 Elements which are apparently directories are expanded to full dirnames.
2258 Is *NOT* used by \\[ess-execute-search],
2259 but by \\[ess-resynch], \\[ess-get-object-list], \\[ess-get-modtime-list],
2260 \\[ess-execute-objects], \\[ess-object-modtime], \\[ess-create-object-name-db],
2261 and (indirectly) by \\[ess-get-help-files-list]."
2262 (save-excursion
2263 (let ((result nil))
2264 (set-buffer (get-ess-buffer ess-current-process-name));to get *its* local vars
2265 (if (and ess-search-list (not ess-sp-change))
2266 ;; use cache:
2267 ess-search-list
2268 ;; else, re-compute:
2269 (ess-write-to-dribble-buffer " (ess-search-list: re-computing..) ")
2270 (let ((tbuffer (get-buffer-create " *search-list*"))
2271 (homedir ess-directory)
2272 (my-search-cmd inferior-ess-search-list-command); from ess-buffer
2273 elt)
2274 (save-excursion
2275 (set-buffer tbuffer)
2276 ;; guaranteed by the initial space in its name: (buffer-disable-undo)
2277 (ess-command my-search-cmd tbuffer 0.2); <- sleep; does (erase-buffer)
2278 (goto-char (point-min))
2279 (ess-write-to-dribble-buffer
2280 (format "after '%s', point-max=%d\n" my-search-cmd (point-max)))
2281 (while (re-search-forward "\"\\([^\"]*\\)\"" nil t)
2282 (setq elt (buffer-substring (match-beginning 1) (match-end 1)))
2283 ;;Dbg: (ess-write-to-dribble-buffer (format " .. elt= %s \t" elt))
2284 (if (and (string-match "^[^/]" elt)
2285 (file-directory-p (concat ess-directory elt)))
2286 (progn
2287 ;;Dbg: (ess-write-to-dribble-buffer "*IS* directory\n")
2288 (setq elt (concat homedir elt)))
2289 ;;else
2290 ;;dbg
2291 ;;- (ess-write-to-dribble-buffer "not dir.\n")
2293 (setq result (append result (list elt))))
2294 (kill-buffer tbuffer)))
2295 (setq ess-search-list result)
2296 (setq ess-sp-change nil)
2297 result))))
2299 ;;; ess-sl-modtime-alist is a list with elements as follows:
2300 ;;; * key (directory or object name)
2301 ;;; * modtime (list of 2 integers)
2302 ;;; * name, name ... (accessible objects in search list posn labeled by key)
2303 ;;; It is a buffer-local variable (belonging to e.g. *R*, *S+6*, .. etc)
2304 ;;; and has the same number of elements and is in the same order as the
2305 ;;; S search list
2307 (defun ess-get-modtime-list ()
2308 "Record the modification times of the directories in the search list,
2309 and the objects in those directories.
2310 The result is stored in `ess-sl-modtime-alist'."
2311 ;; Operation applies to process of current buffer
2312 (let* ((searchlist (ess-search-list))
2313 (index 1)
2314 posn
2315 newalist)
2316 (while searchlist
2317 (setq posn (car searchlist))
2318 (setq newalist
2319 (append
2320 newalist
2321 (list (or (assoc posn ess-sl-modtime-alist)
2322 (append
2323 (list posn (ess-dir-modtime posn))
2324 (prog2
2325 (message "Forming completions for %s..." posn)
2326 (ess-object-names posn index)
2327 (message "Forming completions for %s...done" posn)
2328 ))))))
2329 (setq index (1+ index))
2330 (setq searchlist (cdr searchlist)))
2331 ;;DBG:
2332 (ess-write-to-dribble-buffer
2333 (format "ess-get-modtime-list: new alist of length %d\n"
2334 (length newalist)));; todo : also give length of components!
2336 (setq ess-sl-modtime-alist newalist)))
2339 (defun ess-search-path-tracker (str)
2340 "Check if input STR changed the search path.
2341 This function monitors user input to the inferior ESS process so that
2342 Emacs can keep the variable `ess-search-list' up to date. `completing-read' in
2343 \\[ess-read-object-name] uses this list indirectly when it prompts for help or
2344 for an object to dump."
2345 (if (string-match ess-change-sp-regexp str)
2346 (setq ess-sp-change t)))
2348 \f ; Miscellaneous routines
2350 ;;;*;;; Routines for reading object names
2352 (defun ess-read-object-name (p-string)
2353 "Read an S object name from the minibuffer with completion, and return it.
2354 P-STRING is the prompt string."
2355 (let* ((default (ess-read-object-name-dump))
2356 (prompt-string (if default
2357 (format "%s(default %s) " p-string default)
2358 p-string))
2359 (object-list (mapcar 'list (ess-get-object-list ess-local-process-name)))
2360 (spec (completing-read prompt-string object-list)))
2361 (list (cond
2362 ((string= spec "") default)
2363 (t spec)))))
2365 (defun ess-read-object-name-default ()
2366 "Return the object name at point, or nil if none."
2367 (condition-case ()
2368 (save-excursion
2369 ;; The following line circumvents an 18.57 bug in following-char
2370 (if (eobp) (backward-char 1)) ; Hopefully buffer is not empty!
2371 ;; Get onto a symbol
2372 (catch 'nosym ; bail out if there's no symbol at all before point
2373 (while (/= (char-syntax (following-char)) ?w)
2374 (if (bobp) (throw 'nosym nil) (backward-char 1)))
2375 (let*
2376 ((end (progn (forward-sexp 1) (point)))
2377 (beg (progn (backward-sexp 1) (point))))
2378 (buffer-substring beg end))))
2379 (error nil)))
2381 (defun ess-read-object-name-dump ()
2382 "Return the object name at point, or \"Temporary\" if none."
2383 (condition-case ()
2384 (save-excursion
2385 ;; The following line circumvents an 18.57 bug in following-char
2386 (if (eobp) (backward-char 1)) ; Hopefully buffer is not empty!
2387 ;; Get onto a symbol
2388 (catch 'nosym ; bail out if there's no symbol at all before point
2389 (while (/= (char-syntax (following-char)) ?w)
2390 (if (bobp) (throw 'nosym nil) (backward-char 1)))
2391 (let*
2392 ((end (progn (forward-sexp 1) (point)))
2393 (beg (progn (backward-sexp 1) (point)))
2394 (object-name (buffer-substring beg end)))
2395 (or object-name "Temporary"))))
2396 (error nil)))
2399 ;;*;; Temporary buffer handling
2401 ;(defun ess-create-temp-buffer (name)
2402 ; "Create an empty buffer called NAME, but doesn't display it."
2403 ; (let ((buff (get-buffer-create name)))
2404 ; (save-excursion
2405 ; (set-buffer buff)
2406 ; (erase-buffer))
2407 ; buff))
2410 ;; Ed Kademan's version:
2411 ;From: Ed Kademan <kademan@phz.com>
2412 ;Subject: Re: ess-mode 5.1.16; search list
2413 ;To: rossini@biostat.washington.edu (A.J. Rossini)
2414 ;Cc: Martin Maechler <maechler@stat.math.ethz.ch>, ess-bugs@stat.math.ethz.ch
2415 ;Date: 26 Jul 2000 16:12:12 -0400
2417 ;Dear Tony Rossini,
2419 ;I was having trouble looking at the search list under ess. When I
2420 ;started up multiple inferior processes---each for a different
2421 ;dialect---ess-mode would issue the wrong variant of the "search"
2422 ;command when I typed C-c C-s. In case it is useful let me tell you
2423 ;what I did to get it to work for me.
2425 ;I added the component:
2426 ; (inferior-ess-search-list-command . "search()\n")
2427 ;to S+3-customize-alist and R-customize-alist, and then I redefined the
2428 ;ess-create-temp-buffer function as follows:
2429 (defun ess-create-temp-buffer (name)
2430 "Create an empty buffer called NAME."
2431 (let ((buff (get-buffer-create name))
2432 (elca (eval ess-local-customize-alist)))
2433 (save-excursion
2434 (set-buffer buff)
2435 (erase-buffer)
2436 (ess-setq-vars-local elca buff))
2437 buff))
2438 ;;These two steps seem to insure that the temporary buffer in which the
2439 ;;search results appear has the correct version of the local variables.
2440 ;;I am not that well acquainted with the ess code and don't know whether
2441 ;;this is a good fundamental way of fixing the problem, or even whether
2442 ;;or not this breaks some other feature of ess-mode that I never use.
2443 ;;Thanks for listening.
2444 ;;Ed K.
2445 ;;--
2446 ;;Ed Kademan 508.651.3700
2447 ;;PHZ Capital Partners 508.653.1745 (fax)
2448 ;;321 Commonwealth Road <kademan@phz.com>
2449 ;;Wayland, MA 01778
2453 (defun ess-display-temp-buffer (buff)
2454 "Display the buffer BUFF using `temp-buffer-show-function' and respecting
2455 `ess-display-buffer-reuse-frames'."
2456 (let ((display-buffer-reuse-frames ess-display-buffer-reuse-frames))
2457 (funcall (or temp-buffer-show-function 'display-buffer) buff)))
2459 ;;*;; Error messages
2461 (defun ess-error (msg)
2462 "Something bad has happened.
2463 Display the S buffer, and cause an error displaying MSG."
2464 (display-buffer (process-buffer (get-ess-process ess-current-process-name)))
2465 (error msg))
2467 \f ; Provide package
2469 (provide 'ess-inf)
2471 \f ; Local variables section
2473 ;;; This file is automatically placed in Outline minor mode.
2474 ;;; The file is structured as follows:
2475 ;;; Chapters: ^L ;
2476 ;;; Sections: ;;*;;
2477 ;;; Subsections: ;;;*;;;
2478 ;;; Components: defuns, defvars, defconsts
2479 ;;; Random code beginning with a ;;;;* comment
2481 ;;; Local variables:
2482 ;;; mode: emacs-lisp
2483 ;;; outline-minor-mode: nil
2484 ;;; mode: outline-minor
2485 ;;; outline-regexp: "\^L\\|\\`;\\|;;\\*\\|;;;\\*\\|(def[cvu]\\|(setq\\|;;;;\\*"
2486 ;;; End:
2488 ;;; ess-inf.el ends here