(?`): define the backquote (`) character to be in the quote character
[ess.git] / lisp / ess-inf.el
blob8929e2bad152deaad4b31dc18e7d1d36af91d03c
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)) ;prefix sets 'noswitch
557 (update-ess-process-name-list)
558 (let ((num-processes (length ess-process-name-list)))
559 (if (= 0 num-processes)
560 (error "No ESS processes running."))
561 ;; else
562 (let ((proc
563 (if (and (not ask-if-1) (= 1 num-processes))
564 (car (car ess-process-name-list))
565 ;; else
566 (completing-read message
567 ess-process-name-list
568 nil ; predicate
569 'require-match
570 ;; If in S buffer, don't offer current process
571 (if (eq major-mode 'inferior-ess-mode)
572 ess-language
573 ess-current-process-name
574 ;; maybe ess-local-process-name IF exists?
575 )))))
576 (save-excursion
577 (set-buffer (process-buffer (get-process proc)))
578 (ess-make-buffer-current))
579 (if noswitch
581 (ess-show-buffer (buffer-name (process-buffer (get-process proc))) t))
582 proc)))
585 (defun ess-force-buffer-current (prompt &optional force)
586 "Make sure the current buffer is attached to an ESS process.
587 If not, or FORCE (prefix argument) is non-nil,
588 prompt for a process name with PROMPT.
589 `ess-local-process-name' is set to the name of the process selected.
590 `ess-dialect' is set to the dialect associated with the process selected."
591 (interactive
592 (list (concat ess-dialect " process to use: ") current-prefix-arg))
593 (if (and (not force) (ess-make-buffer-current))
594 nil ; do nothing
595 ;; Make sure the source buffer is attached to a process
596 (if (and ess-local-process-name (not force))
597 (error "Process %s has died." ess-local-process-name)
598 ;; ess-local-process-name is nil -- which process to attach to
599 (save-excursion
600 (let ((proc (ess-request-a-process prompt 'no-switch))
601 temp-ess-help-filetype
602 dialect)
603 (save-excursion
604 (set-buffer (process-buffer (get-process proc)))
605 (setq temp-ess-help-filetype inferior-ess-help-filetype)
606 (setq dialect ess-dialect))
607 (setq ess-local-process-name proc)
608 (setq inferior-ess-help-filetype temp-ess-help-filetype)
609 (setq ess-dialect dialect))))))
611 (defun ess-switch-process ()
612 "Force a switch to a new underlying process."
613 (interactive)
614 (ess-force-buffer-current "Process to use: " t))
616 ;;*;;; Commands for switching to the process buffer
618 (defun ess-switch-to-ESS (eob-p)
619 "Switch to the current inferior ESS process buffer.
620 With (prefix) EOB-P non-nil, positions cursor at end of buffer.
621 This function should follow the description in `ess-show-buffer'
622 for showing the iESS buffer, except that the iESS buffer is also
623 made current."
624 (interactive "P")
625 (ess-make-buffer-current)
626 (if (and ess-current-process-name (get-process ess-current-process-name))
627 (progn
628 ;; Display the buffer, but don't select it yet.
629 (ess-show-buffer
630 (buffer-name (process-buffer (get-process ess-current-process-name)))
632 (if eob-p (goto-char (point-max))))
633 (message "No inferior ESS process")
634 (ding)))
636 (defun ess-switch-to-end-of-ESS ()
637 "Switch to the end of the inferior ESS process buffer."
638 (interactive)
639 (ess-switch-to-ESS t))
641 (defun get-ess-buffer (name)
642 "Return the buffer associated with the ESS process named by NAME."
643 (process-buffer (get-ess-process name)))
645 (defun update-ess-process-name-list ()
646 "Remove names with no process."
647 (let (defunct)
648 (mapcar
649 '(lambda (conselt)
650 (let ((proc (get-process (car conselt))))
651 (if (and proc (eq (process-status proc) 'run)) nil
652 (setq defunct (cons conselt defunct)))))
653 ess-process-name-list)
654 (mapcar
655 '(lambda (pointer)
656 (setq ess-process-name-list (delq pointer ess-process-name-list)))
657 defunct))
658 (if (eq (length ess-process-name-list) 0)
659 (setq ess-current-process-name nil)))
661 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
662 ;; ess-show-buffer
663 ;; Something like this almost works, but problems with XEmacs and Emacs
664 ;; differing implementations of the args to display-buffer make this
665 ;; too tough to pursue. The longer version below works.
666 ;; (defun ess-show-buffer (buf)
667 ;; "Display the buffer BUF, a string, but do not select it.
668 ;; Returns the window corresponding to the buffer."
669 ;; ;; On XEmacs, I get an error if third arg to display-buffer is t and
670 ;; ;; the BUF is in another frame. Emacs does not have this problem.
671 ;; (if (featurep 'xemacs)
672 ;; (display-buffer buf nil (get-frame-for-buffer buf))
673 ;; (display-buffer buf nil t)))
674 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
675 (defun ess-show-buffer (buf &optional visit)
676 "Ensure the ESS buffer BUF is visible.
677 The buffer, specified as a string, is typically an iESS (e.g. *R*) buffer.
679 This handles several cases:
681 1. If BUF is visible in the current frame, nothing is done.
682 2. If BUF is visible in another frame, then we ensure that frame is
683 visible (it may have been iconified).
684 3. If buffer is not visible in any frame, simply show it in another window
685 in the current frame.
687 Iff VISIT is non-nil, as well as making BUF visible, we also select it
688 as the current buffer."
689 (let ( (frame))
690 (if (ess-buffer-visible-this-frame buf)
691 ;;1. Nothing to do, BUF visible in this frame; just return window
692 ;; where this buffer is.
695 ;; 2. Maybe BUF visible in another frame.
696 (setq frame (ess-buffer-visible-other-frame buf))
697 (if frame
698 ;; BUF is visible in frame, so just check frame is raised.
699 (if (not (eq (frame-visible-p frame) t))
700 ;; frame is not yet visible, so raise it.
701 (raise-frame frame))
702 ;; 3. else BUF not visible in any frame, so show it (but do
703 ;; not select it) in another window in current frame.
704 (display-buffer buf)))
705 ;; At this stage, the buffer should now be visible on screen,
706 ;; although it won't have been made current.
707 (when visit
708 ;; Need to select the buffer.
710 ;; First of all, check case 2 if buffer is in another frame
711 ;; but that frame may not be selected.
712 (if frame
713 (ess-select-frame-set-input-focus frame))
714 (select-window (get-buffer-window buf 0)))))
717 (defvar ess-bufs-in-frame nil) ;silence the compiler.
718 ;; The next few functions are copied from my (SJE) iswitchb library.
719 (defun ess-get-bufname (win)
720 "Used by `ess-get-buffers-in-frames' to walk through all windows."
721 (let ((buf (buffer-name (window-buffer win))))
722 (if (not (member buf ess-bufs-in-frame))
723 ;; Only add buf if it is not already in list.
724 ;; This prevents same buf in two different windows being
725 ;; put into the list twice.
726 (setq ess-bufs-in-frame
727 (cons buf ess-bufs-in-frame)))))
729 (defun ess-get-buffers-in-frames (&optional current)
730 "Return the list of buffers that are visible in the current frame.
731 If optional argument CURRENT is given, restrict searching to the
732 current frame, rather than all frames."
733 (let ((ess-bufs-in-frame nil))
734 (walk-windows 'ess-get-bufname nil (if current nil 0))
735 ess-bufs-in-frame))
737 (defun ess-buffer-visible-this-frame (buf)
738 "Return t if BUF is visible in current frame."
739 (member buf (ess-get-buffers-in-frames t)))
741 (defun ess-buffer-visible-other-frame (buf)
742 "Return t if BUF is visible in another frame.
743 Assumes that buffer has not already been in found in current frame."
744 (if (member buf (ess-get-buffers-in-frames))
745 (window-frame (get-buffer-window buf 0))
746 nil))
749 \f ; Functions for evaluating code
751 (defun ess-ddeclient-p ()
752 "Returns t iff `ess-local-process-name' is associated with an
753 inferior-ess-ddeclient, and nil if the ess-process is running as an
754 ordinary inferior process. Alway nil on Unix machines."
755 (interactive)
756 (if ess-microsoft-p
757 (not (equal (ess-get-process-variable
758 ess-local-process-name 'inferior-ess-ddeclient)
759 (default-value 'inferior-ess-ddeclient)))))
761 (defun ess-prompt-wait (proc &optional start-of-output sleep)
762 "Wait for a prompt to appear at BOL of current buffer.
763 PROC is the ESS process. Does not change point"
764 (if sleep (sleep-for sleep)); we sleep here, *and* wait below
765 (if start-of-output nil (setq start-of-output (point-min)))
766 (save-excursion
767 (while (progn
768 ;; get output if there is some ready
770 ;; (if (and ess-microsoft-p ess-ms-slow)
771 ;; (accept-process-output proc 0 1500) ; Microsoft is slow
772 (accept-process-output proc 0 500)
773 ;; )
774 (goto-char (marker-position (process-mark proc)))
775 (beginning-of-line)
776 (if (< (point) start-of-output) (goto-char start-of-output))
777 (not (looking-at inferior-ess-primary-prompt))))))
779 (defun ordinary-insertion-filter (proc string)
780 (let ((old-buffer (current-buffer)))
781 (unwind-protect
782 (let (moving)
783 (set-buffer (process-buffer proc))
784 (setq moving (= (point) (process-mark proc)))
785 (save-excursion
786 ;; Insert the text, moving the process-marker.
787 (goto-char (process-mark proc))
788 (insert string)
789 (set-marker (process-mark proc) (point)))
790 (if moving (goto-char (process-mark proc))))
791 (set-buffer old-buffer))))
793 (defun ess-command (com &optional buf sleep)
794 "Send the ESS process command COM and delete the output
795 from the ESS process buffer. If an optional second argument BUF exists
796 save the output in that buffer. BUF is erased before use.
797 COM should have a terminating newline.
798 Guarantees that the value of .Last.value will be preserved.
799 When optional third arg SLEEP is non-nil, `(sleep-for (* a SLEEP))'
800 will be used in a few places where `a' is proportional to `ess-cmd-delay'."
801 ;; Use this function when you need to evaluate some S code, and the
802 ;; result is needed immediately. Waits until the output is ready
804 ;; the ddeclient-p checks needs to use the local-process-name
805 (if buf nil (setq buf (get-buffer-create " *ess-command-output*")))
806 (save-excursion
807 (set-buffer buf)
808 (if ess-local-process-name
810 (setq ess-local-process-name ess-current-process-name)))
811 (if (ess-ddeclient-p)
812 (ess-command-ddeclient com buf sleep)
814 ;; else: "normal", non-DDE behavior:
816 (let* ((sprocess (get-ess-process ess-current-process-name))
817 sbuffer
818 do-sleep end-of-output
819 oldpb oldpf oldpm
821 (if sprocess nil
822 (error "Process %s is not running!" ess-current-process-name))
823 (setq sbuffer (process-buffer sprocess))
824 (save-excursion
825 (set-buffer sbuffer)
826 (setq do-sleep ; only now when in sprocess buffer
827 (progn
828 (if sleep (if (numberp sleep) nil (setq sleep 1))) ; t means 1
829 (and ess-cmd-delay sleep)))
830 (if do-sleep (setq sleep (* sleep ess-cmd-delay)))
831 (save-excursion
832 (goto-char (marker-position (process-mark sprocess)))
833 (beginning-of-line)
834 (if (looking-at inferior-ess-primary-prompt) nil
835 (ess-error
836 "ESS process not ready. Finish your command before trying again.")))
837 (setq oldpf (process-filter sprocess))
838 (setq oldpb (process-buffer sprocess))
839 (setq oldpm (marker-position (process-mark sprocess)))
840 ;; need the buffer-local values in result buffer "buf":
841 (unwind-protect
842 (progn
843 (set-process-buffer sprocess buf)
844 (set-process-filter sprocess 'ordinary-insertion-filter)
845 ;; Output is now going to BUF:
846 (save-excursion
847 (set-buffer buf)
848 (erase-buffer)
849 (set-marker (process-mark sprocess) (point-min))
850 (process-send-string sprocess com)
851 ;; need time for ess-create-object-name-db on PC
852 (ess-prompt-wait sprocess nil (and do-sleep (* 0.4 sleep))) ;MS: 4
853 ;;(if do-sleep (sleep-for (* 0.0 sleep))); microsoft: 0.5
855 ;; goto end, making sure final prompt is deleted
856 ;; FIXME? do this with much less code
857 (goto-char (point-max))
858 (save-excursion
859 (beginning-of-line)
860 (setq end-of-output (point)))
861 (delete-region end-of-output (point-max))
863 ;; Restore old values for process filter
864 (set-process-buffer sprocess oldpb)
865 (set-process-filter sprocess oldpf)
866 (set-marker (process-mark sprocess) oldpm))))))
868 (defun ess-replace-in-string (str regexp newtext &optional literal)
869 "Replace all matches in STR for REGEXP with NEWTEXT string.
870 Optional LITERAL non-nil means do a literal replacement.
871 Otherwise treat \\ in NEWTEXT string as special:
872 \\& means substitute original matched text,
873 \\N means substitute match for \(...\) number N,
874 \\\\ means insert one \\."
875 (if (not (stringp str))
876 (error "(replace-in-string): First argument must be a string: %s" str))
877 (if (stringp newtext)
879 (error "(replace-in-string): 3rd arg must be a string: %s"
880 newtext))
881 (let ((rtn-str "")
882 (start 0)
883 (special)
884 match prev-start)
885 (while (setq match (string-match regexp str start))
886 (setq prev-start start
887 start (match-end 0)
888 rtn-str
889 (concat
890 rtn-str
891 (substring str prev-start match)
892 (cond (literal newtext)
893 (t (mapconcat
894 (function
895 (lambda (c)
896 (if special
897 (progn
898 (setq special nil)
899 (cond ((eq c ?\\) "\\")
900 ((eq c ?&)
901 (substring str
902 (match-beginning 0)
903 (match-end 0)))
904 ((and (>= c ?0) (<= c ?9))
905 (if (> c (+ ?0 (length
906 (match-data))))
907 ;; Invalid match num
908 (error "(replace-in-string) Invalid match num: %c" c)
909 (setq c (- c ?0))
910 (substring str
911 (match-beginning c)
912 (match-end c))))
913 (t (char-to-string c))))
914 (if (eq c ?\\) (progn (setq special t) nil)
915 (char-to-string c)))))
916 newtext ""))))))
917 (concat rtn-str (substring str start))))
921 ;;*;; Evaluating lines, paragraphs, regions, and buffers.
923 ;;--- The two basic building blocks [called by all other ess-eval..] are
924 ;; (ess-eval-linewise ....)
925 ;; and
926 ;; (ess-eval-region ....)
928 (defun ess-eval-linewise (text-withtabs &optional
929 invisibly eob even-empty
930 wait-last-prompt sleep-sec timeout-ms)
931 ;; RDB 28/8/92 added optional arg eob
932 ;; AJR 971022: text-withtabs was text.
933 ;; MM 2006-08-23: added 'timeout-ms' -- but the effect seems "nil"
934 ;; MM 2007-01-05: added 'sleep-sec'
935 "Evaluate TEXT-WITHTABS in the ESS process buffer as if typed in w/o tabs.
936 Waits for prompt after each line of input, so won't break on large texts.
938 If optional second arg INVISIBLY is non-nil, don't echo commands. If it
939 is a string, just include that string. If optional third arg
940 EOB is non-nil go to end of ESS process buffer after evaluation. If
941 optional 4th arg EVEN-EMPTY is non-nil, also send empty text (e.g. an
942 empty line). If 5th arg WAIT-LAST-PROMPT is non-nil, also wait for
943 the prompt after the last line; if 6th arg SLEEP-SEC is a number, ESS
944 will call '(\\[sleep-for] SLEEP-SEC) at the end of this function. If the
945 7th arg TIMEOUT-MS is set to number, it will be used instead of the
946 default 100 ms and be passed to \\[accept-process-output]."
947 ;; but the effect is unclear
948 (if (ess-ddeclient-p)
949 (ess-eval-linewise-ddeclient text-withtabs
950 invisibly eob even-empty
951 (if wait-last-prompt
952 ess-eval-ddeclient-sleep))
954 ;; else: "normal", non-DDE behavior:
956 ;; Use this to evaluate some code, but don't wait for output.
957 (let* ((cbuffer (current-buffer))
958 (sprocess (get-ess-process ess-current-process-name))
959 (sbuffer (process-buffer sprocess))
960 (text (ess-replace-in-string text-withtabs "\t" " "))
961 start-of-output
962 com pos txt-gt-0)
964 (unless (numberp timeout-ms)
965 (setq timeout-ms 100));; << make '100' into a custom-variable
967 ;;(message "'ess-eval-linewise: sbuffer = %s" sbuffer)
968 (set-buffer sbuffer)
970 ;; the following is required to make sure things work!
971 (if (string= ess-language "STA")
972 (setq invisibly t))
973 ;; dbg:
974 ;; dbg(ess-write-to-dribble-buffer
975 ;; dbg (format "(eval-visibly 1): lang %s (invis=%s, eob=%s, even-empty=%s)\n"
976 ;; dbg ess-language invisibly eob even-empty))
978 (goto-char (marker-position (process-mark sprocess)))
979 (if (stringp invisibly)
980 (insert-before-markers (concat "*** " invisibly " ***\n")))
981 ;; dbg:
982 ;; dbg (ess-write-to-dribble-buffer
983 ;; dbg (format "(eval-visibly 2): text[%d]= '%s'\n" (length text) text))
984 (while (or (setq txt-gt-0 (> (length text) 0))
985 even-empty)
986 (if even-empty (setq even-empty nil))
987 (if txt-gt-0
988 (progn
989 (setq pos (string-match "\n\\|$" text))
990 (setq com (concat (substring text 0 pos) "\n"))
991 (setq text (substring text (min (length text) (1+ pos)))))
992 ;; else 0-length text
993 (setq com "\n")
995 (goto-char (marker-position (process-mark sprocess)))
996 (if (not invisibly)
997 ;; Terrible kludge -- need to insert after all markers *except*`
998 ;; the process mark
999 (let ((dokludge (eq (point)
1000 (marker-position (process-mark sprocess)))))
1001 (insert com)
1002 (if dokludge (set-marker (process-mark sprocess) (point)))))
1003 (setq start-of-output (marker-position (process-mark sprocess)))
1004 ;; A kludge to prevent the delay between insert and process output
1005 ;; affecting the display. A case for a comint-send-input-hook?
1006 ;; (save-excursion
1007 ;; ;; comint-postoutput-scroll-to-bottom can change
1008 ;; ;; current-buffer. Argh.
1009 ;; (let ((functions comint-output-filter-functions))
1010 ;; (while functions
1011 ;; (funcall (car functions) com)
1012 ;; (setq functions (cdr functions)))))
1013 (process-send-string sprocess com)
1014 ;; wait for the prompt - after the last line of input only if wait-last:
1015 (if (or wait-last-prompt
1016 (> (length text) 0))
1017 (while (progn
1018 (accept-process-output nil 0 timeout-ms)
1019 (goto-char (marker-position (process-mark sprocess)))
1020 (beginning-of-line)
1021 (if (< (point) start-of-output)
1022 (goto-char start-of-output))
1023 (not (looking-at inferior-ess-prompt))))))
1025 (goto-char (marker-position (process-mark sprocess)))
1026 (if eob
1027 (progn
1028 (ess-show-buffer (buffer-name sbuffer) nil)
1029 ;; Once SBUFFER is visible, we can then move the point in that
1030 ;; window to the end of the buffer.
1031 (set-window-point (get-buffer-window sbuffer t)
1032 (with-current-buffer sbuffer (point-max))))
1033 (set-buffer cbuffer))
1034 (if (numberp sleep-sec)
1035 (sleep-for sleep-sec))))); in addition to timeout-ms
1038 ;;;*;;; Evaluate only
1040 (defun ess-eval-region (start end toggle &optional message)
1041 "Send the current region to the inferior ESS process.
1042 With prefix argument toggle the meaning of `ess-eval-visibly-p';
1043 this does not apply when using the S-plus GUI, see `ess-eval-region-ddeclient'."
1044 (interactive "r\nP")
1045 ;;(untabify (point-min) (point-max))
1046 ;;(untabify start end); do we really need to save-excursion?
1047 (ess-force-buffer-current "Process to load into: ")
1048 (message "Starting evaluation...")
1050 (if (ess-ddeclient-p)
1051 (ess-eval-region-ddeclient start end 'even-empty)
1052 ;; else: "normal", non-DDE behavior:
1053 (let ((visibly (if toggle (not ess-eval-visibly-p) ess-eval-visibly-p)))
1054 (if visibly
1055 (ess-eval-linewise (buffer-substring start end))
1056 (if ess-synchronize-evals
1057 (ess-eval-linewise (buffer-substring start end)
1058 (or message "Eval region"))
1059 ;; else [almost always!]
1060 (let ((sprocess (get-ess-process ess-current-process-name)))
1061 (process-send-region sprocess start end)
1062 (process-send-string sprocess "\n"))))))
1064 (message "Finished evaluation")
1065 ;; return value
1066 (list start end))
1068 (defun ess-eval-buffer (vis)
1069 "Send the current buffer to the inferior ESS process.
1070 Arg has same meaning as for `ess-eval-region'."
1071 (interactive "P")
1072 (ess-eval-region (point-min) (point-max) vis "Eval buffer"))
1074 (defun ess-eval-function (vis)
1075 "Send the current function to the inferior ESS process.
1076 Arg has same meaning as for `ess-eval-region'."
1077 (interactive "P")
1078 (save-excursion
1079 (let* ((beg-end (ess-end-of-function))
1080 (beg (nth 0 beg-end))
1081 (end (nth 1 beg-end))
1082 name)
1083 (goto-char beg)
1084 (setq name (ess-read-object-name-default))
1085 (princ (concat "Loading: " name) t)
1086 (ess-eval-region beg end vis
1087 (concat "Eval function " name)))))
1089 ;; This is from Mary Lindstrom <lindstro@Biostat.Wisc.Edu>
1090 ;; 31 Aug 1995 14:11:43 To: S-mode@stat.math.ethz.ch
1091 (defun ess-eval-paragraph (vis)
1092 "Send the current paragraph to the inferior ESS process.
1093 Prefix arg VIS toggles visibility of ess-code as for `ess-eval-region'."
1094 (interactive "P")
1095 (save-excursion
1096 (forward-paragraph)
1097 (let ((end (point)))
1098 (backward-paragraph)
1099 (ess-eval-region (point) end vis "Eval paragraph"))))
1101 (defun ess-eval-function-or-paragraph-and-step (vis)
1102 "Send the current function if \\[point] is inside one, otherwise the current
1103 paragraph other to the inferior ESS process.
1104 Prefix arg VIS toggles visibility of ess-code as for `ess-eval-region'."
1105 (interactive "P")
1106 (let ((beg (ess-beginning-of-function 'no-error)))
1107 (if beg ;; inside a function
1108 (let ((end-fun (cadr (ess-end-of-function beg)))
1109 name)
1110 (goto-char beg)
1111 (setq name (ess-read-object-name-default))
1112 (princ (concat "Loading: " name) t)
1113 (ess-eval-region beg end-fun vis
1114 (concat "Eval function " name))
1115 (goto-char (1+ end-fun)))
1116 ;; else: not in a function
1117 (ess-eval-paragraph-and-step vis)
1121 (defun ess-eval-line (vis)
1122 "Send the current line to the inferior ESS process.
1123 Arg has same meaning as for `ess-eval-region'."
1124 (interactive "P")
1125 (save-excursion
1126 (end-of-line)
1127 (let ((end (point)))
1128 (beginning-of-line)
1129 (princ (concat "Loading line: " (ess-extract-word-name) " ...") t)
1130 (ess-eval-region (point) end vis "Eval line"))))
1134 ;; Contributed by Stephen Eglen <stephen@anc.ed.ac.uk> {idea from octave int.}
1135 (defun ess-next-code-line (&optional arg)
1136 "Move ARG lines of code forward (backward if ARG is negative).
1137 Skips past all empty and comment lines. Default for ARG is 1.
1139 On success, return 0. Otherwise, go as far as possible and return -1."
1140 (interactive "p")
1141 (or arg (setq arg 1))
1142 (beginning-of-line)
1143 (let ((n 0)
1144 (inc (if (> arg 0) 1 -1)))
1145 (while (and (/= arg 0) (= n 0))
1146 (setq n (forward-line inc)); n=0 is success
1147 (while (and (= n 0)
1148 (looking-at "\\s-*\\($\\|\\s<\\)"))
1149 (setq n (forward-line inc)))
1150 (setq arg (- arg inc)))
1153 (defun ess-eval-line-and-step (&optional simple-next even-empty invisibly)
1154 "Evaluate the current line visibly and step to the \"next\" line.
1155 \"next\" = the next line with non-comment code _unless_ SIMPLE-NEXT is non-nil,
1156 possibly via prefix arg. If 2nd arg EVEN-EMPTY [prefix as well],
1157 also send empty lines. When the variable `ess-eval-empty' is non-nil
1158 both SIMPLE-NEXT and EVEN-EMPTY are interpreted as true."
1159 ;; From an idea by Rod Ball (rod@marcam.dsir.govt.nz)
1160 (interactive "P\nP"); prefix sets BOTH !
1161 (ess-force-buffer-current "Process to load into: ")
1162 (save-excursion
1163 (end-of-line)
1164 (let ((end (point)))
1165 (beginning-of-line)
1166 ;; go to end of process buffer so user can see result
1167 (ess-eval-linewise (buffer-substring (point) end)
1168 invisibly 'eob (or even-empty ess-eval-empty))))
1169 (if (or simple-next ess-eval-empty)
1170 (forward-line 1)
1171 (ess-next-code-line 1)))
1173 (defun ess-eval-line-and-step-invisibly ()
1174 "Evaluate the current line invisibly and step to the next line.
1175 Evaluate all comments and empty lines."
1176 (interactive)
1177 (ess-eval-line-and-step t t t))
1179 ;; goes to the real front, in case you do double function definition
1180 ;; 29-Jul-92 -FER
1181 ;; don't know why David changed it.
1183 ;; FER's versions don't work properly with nested functions. Replaced
1184 ;; mine. DMS 16 Nov 92
1186 ;;;*;;; Evaluate and switch to S
1188 (defun ess-eval-region-and-go (start end vis)
1189 "Send the current region to the inferior S and switch to the process buffer.
1190 Arg has same meaning as for `ess-eval-region'."
1191 (interactive "r\nP")
1192 (ess-eval-region start end vis)
1193 (ess-switch-to-ESS t))
1195 (defun ess-eval-buffer-and-go (vis)
1196 "Send the current buffer to the inferior S and switch to the process buffer.
1197 Arg has same meaning as for `ess-eval-region'."
1198 (interactive "P")
1199 (ess-eval-buffer vis)
1200 (ess-switch-to-ESS t))
1202 (defun ess-eval-function-and-go (vis)
1203 "Send the current function to the inferior ESS process and switch to
1204 the process buffer. Arg has same meaning as for `ess-eval-region'."
1205 (interactive "P")
1206 (ess-eval-function vis)
1207 (ess-switch-to-ESS t))
1209 (defun ess-eval-line-and-go (vis)
1210 "Send the current line to the inferior ESS process and switch to the
1211 process buffer. Arg has same meaning as for `ess-eval-region'."
1212 (interactive "P")
1213 (ess-eval-line vis)
1214 (ess-switch-to-ESS t))
1216 (defun ess-eval-paragraph-and-go (vis)
1217 "Send the current paragraph to the inferior ESS process and switch to the
1218 process buffer. Arg has same meaning as for `ess-eval-region'."
1219 (interactive "P")
1220 (ess-eval-paragraph vis)
1221 (ess-switch-to-ESS t))
1223 (defun ess-eval-paragraph-and-step (vis)
1224 "Send the current paragraph to the inferior ESS process and move forward to
1225 the next paragraph. Arg has same meaning as for `ess-eval-region'."
1226 (interactive "P")
1227 (let ((beg-end (ess-eval-paragraph vis)))
1228 (goto-char (1+ (cadr beg-end))))
1231 ;;; Related to the ess-eval-* commands, there are the ess-load
1232 ;;; commands. Need to add appropriate stuff...
1235 (defun ess-load-file (filename)
1236 "Load an S source file into an inferior ESS process."
1237 (interactive (list
1239 (and (eq major-mode 'ess-mode)
1240 (buffer-file-name))
1241 (expand-file-name
1242 (read-file-name "Load S file: " nil nil t)))))
1243 (ess-make-buffer-current)
1244 (if (ess-ddeclient-p);; << FIXME: rather ess-microsoft-p
1245 (setq filename (ess-replace-in-string filename "[\\]" "/")))
1246 (let ((source-buffer (get-file-buffer filename)))
1247 (if (ess-check-source filename)
1248 (error "Buffer %s has not been saved" (buffer-name source-buffer)))
1249 ;; else
1250 (if (ess-ddeclient-p)
1251 (ess-load-file-ddeclient filename)
1253 ;; else: "normal", non-DDE behavior:
1255 ;; Find the process to load into
1256 (if source-buffer
1257 (save-excursion
1258 (set-buffer source-buffer)
1259 (ess-force-buffer-current "Process to load into: ")
1260 (ess-check-modifications)))
1261 (let ((errbuffer (ess-create-temp-buffer ess-error-buffer-name))
1262 error-occurred nomessage)
1263 (ess-command (format inferior-ess-load-command filename) errbuffer) ;sleep ?
1264 (save-excursion
1265 (set-buffer errbuffer)
1266 (goto-char (point-max))
1267 (setq error-occurred (re-search-backward ess-dump-error-re nil t))
1268 (setq nomessage (= (buffer-size) 0)))
1269 (if error-occurred
1270 (message "Errors: Use %s to find error."
1271 (substitute-command-keys
1272 "\\<inferior-ess-mode-map>\\[ess-parse-errors]"))
1273 ;; Load did not cause an error
1274 (if nomessage (message "Load successful.")
1275 ;; There was a warning message from S
1276 (ess-display-temp-buffer errbuffer))
1277 ;; Consider deleting the file
1278 (let ((skdf (if source-buffer
1279 (save-excursion
1280 (set-buffer source-buffer)
1281 ess-keep-dump-files)
1282 ess-keep-dump-files))) ;; global value
1283 (cond
1284 ((null skdf)
1285 (delete-file filename))
1286 ((memq skdf '(check ask))
1287 (let ((doit (y-or-n-p (format "Delete %s " filename))))
1288 (if doit (delete-file filename))
1289 (and source-buffer
1290 (local-variable-p 'ess-keep-dump-files source-buffer)
1291 (save-excursion
1292 (set-buffer source-buffer)
1293 (setq ess-keep-dump-files doit)))))))
1294 (ess-switch-to-ESS t))))))
1296 \f ; Inferior S mode
1297 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1298 ;;;; In this section:
1299 ;;;;
1300 ;;;; * The major mode inferior-ess-mode
1301 ;;;; * Process handling code
1302 ;;;; * Completion code
1303 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1305 ;;*;; Major mode definition
1307 (if inferior-ess-mode-map
1310 (cond ((featurep 'xemacs)
1311 ;; Code for XEmacs
1312 (setq inferior-ess-mode-map (make-keymap))
1313 (set-keymap-parent inferior-ess-mode-map comint-mode-map))
1314 ((not (featurep 'xemacs))
1315 ;; Code for GNU Emacs
1316 (setq inferior-ess-mode-map (cons 'keymap comint-mode-map))))
1318 ;; Use syntax valid *both* for GNU emacs and XEmacs :
1319 (define-key inferior-ess-mode-map "\r" 'inferior-ess-send-input)
1320 (define-key inferior-ess-mode-map "\C-a" 'comint-bol)
1321 (define-key inferior-ess-mode-map "\M-\r" 'ess-transcript-send-command-and-move)
1322 (define-key inferior-ess-mode-map "\C-c\C-l" 'ess-load-file)
1323 ;; the above OVERRIDES comint-dynamic-list-input-ring --> re-assign:
1324 (define-key inferior-ess-mode-map "\C-c\M-l" 'comint-dynamic-list-input-ring)
1325 (define-key inferior-ess-mode-map "\C-c`" 'ess-parse-errors)
1326 (define-key inferior-ess-mode-map "\C-c\C-d" 'ess-dump-object-into-edit-buffer)
1327 (define-key inferior-ess-mode-map "\C-c\C-v" 'ess-display-help-on-object)
1328 (define-key inferior-ess-mode-map "\C-c\C-q" 'ess-quit)
1329 (define-key inferior-ess-mode-map "\C-c\C-t" 'ess-execute)
1330 (define-key inferior-ess-mode-map "\C-c\C-s" 'ess-execute-search)
1331 (define-key inferior-ess-mode-map "\C-c\C-x" 'ess-execute-objects)
1332 ;;(define-key inferior-ess-mode-map "\C-c\C-a" 'ess-execute-attach)
1333 (define-key inferior-ess-mode-map "\C-c\034" 'ess-abort) ; \C-c\C-backslash
1334 (define-key inferior-ess-mode-map "\C-c\C-z" 'ess-abort) ; mask comint map
1335 (define-key inferior-ess-mode-map "\C-d" 'delete-char) ; EOF no good in S
1336 (define-key inferior-ess-mode-map "\t" 'comint-dynamic-complete)
1337 (define-key inferior-ess-mode-map "\C-c\t" 'ess-complete-object-name)
1338 (define-key inferior-ess-mode-map "\M-\t" 'comint-replace-by-expanded-filename)
1339 (define-key inferior-ess-mode-map "\M-?" 'ess-list-object-completions)
1340 (define-key inferior-ess-mode-map "\C-c\C-k" 'ess-request-a-process))
1342 (easy-menu-define
1343 inferior-ess-mode-menu inferior-ess-mode-map
1344 "Menu for use in Inferior S mode"
1345 '("iESS"
1346 ["What is this? (beta)" ess-mouse-me t]
1347 ["Resynch S completions" ess-resynch t]
1348 ["Quit S" ess-quit t]
1349 ["Display search list" ess-execute-search t]
1350 ["Display object list" ess-execute-objects t]
1351 ["Get help on S object" ess-display-help-on-object t]
1352 ["Enter S command" ess-execute t]
1353 ["Attach directory" ess-execute-attach t]
1354 "------"
1355 ["Send and move" ess-transcript-send-command-and-move t]
1356 ["Copy command" comint-copy-old-input t]
1357 ["Send command" inferior-ess-send-input t]
1358 "------"
1359 ["Jump to Error" ess-parse-errors t]
1360 ;; need a toggle switch for above, AJR.
1361 ["Load source file" ess-load-file t]
1362 ["Edit S Object" ess-dump-object-into-edit-buffer t]
1363 "------"
1364 ["Describe" describe-mode t]
1365 ["About" (ess-goto-info "Entering Commands") t]
1366 ["Send bug report" ess-submit-bug-report t]
1370 (defun inferior-ess-mode-xemacs-menu ()
1371 "Hook to install `ess-mode' menu for XEmacs (w/ easymenu)."
1372 (if 'inferior-ess-mode
1373 (easy-menu-add inferior-ess-mode-menu)
1374 (easy-menu-remove inferior-ess-mode-menu)))
1376 (if (string-match "XEmacs" emacs-version)
1377 (add-hook 'inferior-ess-mode-hook 'inferior-ess-mode-xemacs-menu))
1379 (if ess-mode-minibuffer-map nil
1381 (cond ((featurep 'xemacs)
1382 ;; Code for XEmacs
1383 (setq ess-mode-minibuffer-map (make-keymap))
1384 (set-keymap-parent ess-mode-minibuffer-map minibuffer-local-map)))
1386 (cond ((not (featurep 'xemacs))
1387 ;; Code for Emacs
1388 (setq ess-mode-minibuffer-map (cons 'keymap minibuffer-local-map))))
1390 (define-key ess-mode-minibuffer-map "\t" 'ess-complete-object-name)
1391 (define-key ess-mode-minibuffer-map "\C-c\C-s" 'ess-execute-search)
1392 (define-key ess-mode-minibuffer-map "\C-c\C-x" 'ess-execute-objects))
1394 (defun inferior-ess-mode ()
1395 "Major mode for interacting with an inferior ESS process.
1396 Runs an S interactive job as a subprocess of Emacs, with I/O through an
1397 Emacs buffer. Variable `inferior-ess-program' controls which S
1398 is run.
1400 Commands are sent to the ESS process by typing them, and pressing
1401 \\[inferior-ess-send-input]. Pressing \\[complete-dynamic-complete]
1402 completes known object names or filenames, as appropriate. Other
1403 keybindings for this mode are:
1405 \\{inferior-ess-mode-map}
1407 When editing S objects, the use of \\[ess-load-file] is advocated.
1408 `ess-load-file' keeps source files (if `ess-keep-dump-files' is non-nil) in
1409 the directory specified by `ess-source-directory', with the
1410 filename chosen according to `ess-dump-filename-template'. When a file is
1411 loaded, `ess-mode' parses error messages and jumps to the appropriate file
1412 if errors occur. The ess-eval- commands do not do this.
1414 Customization: Entry to this mode runs the hooks on `comint-mode-hook' and
1415 `inferior-ess-mode-hook' (in that order).
1417 You can send text to the inferior ESS process from other buffers containing
1418 S source. The key bindings of these commands can be found by typing
1419 C-h m (help for mode) in the other buffers.
1420 `ess-eval-region' sends the current region to the ESS process.
1421 `ess-eval-buffer' sends the current buffer to the ESS process.
1422 `ess-eval-function' sends the current function to the ESS process.
1423 `ess-eval-line' sends the current line to the ESS process.
1424 `ess-beginning-of-function' and `ess-end-of-function' move the point to
1425 the beginning and end of the current S function.
1426 `ess-switch-to-ESS' switches the current buffer to the ESS process buffer.
1427 `ess-switch-to-end-of-ESS' switches the current buffer to the ESS process
1428 buffer and puts point at the end of it.
1430 `ess-eval-region-and-go', `ess-eval-buffer-and-go',
1431 `ess-eval-function-and-go', and `ess-eval-line-and-go' switch to the S
1432 process buffer after sending their text.
1433 `ess-dump-object-into-edit-buffer' moves an S object into a temporary file
1434 and buffer for editing
1435 `ess-load-file' sources a file of commands to the ESS process.
1437 Commands:
1438 Return after the end of the process' output sends the text from the
1439 end of process to point.
1440 Return before the end of the process' output copies the sexp ending at point
1441 to the end of the process' output, and sends it.
1442 Delete converts tabs to spaces as it moves back.
1443 C-M-q does Tab on each line starting within following expression.
1444 Paragraphs are separated only by blank lines. Crosshatches start comments.
1445 If you accidentally suspend your process, use \\[comint-continue-subjob]
1446 to continue it."
1447 (interactive)
1448 ;;(message " at very beg. of (inferior-ess-mode): inf.-ess-prompt= %s"
1449 ;; inferior-ess-prompt)
1450 (comint-mode)
1453 ;; SJE: is this the proper place for setting inferior-ess-prompt,
1454 ;; rather than within ess-multi? Tony - have you remembered yet
1456 ;; about the setq-default, as I changed it back to setq.
1457 (setq inferior-ess-prompt
1458 ;; shouldn't be setq-default! And I've
1459 ;; forgotten why! (AJR)
1460 ;; Do not anchor to bol with `^'
1461 (concat "\\("
1462 inferior-ess-primary-prompt
1463 "\\|"
1464 inferior-ess-secondary-prompt
1465 "\\)"))
1466 (setq comint-prompt-regexp (concat "^" inferior-ess-prompt))
1467 (setq major-mode 'inferior-ess-mode)
1468 (setq mode-name "iESS") ;(concat "iESS:" ess-dialect))
1469 (setq mode-line-process
1470 '(" [" ess-local-process-name "]: %s"))
1471 (use-local-map inferior-ess-mode-map)
1472 (if ess-mode-syntax-table
1473 (set-syntax-table ess-mode-syntax-table)
1474 ;; FIXME: need to do something if not set! Get from the proper place!
1476 (add-hook 'comint-input-filter-functions 'ess-search-path-tracker)
1477 (setq comint-get-old-input 'inferior-ess-get-old-input)
1479 (ess-write-to-dribble-buffer
1480 (format "(i-ess 1): buf=%s, lang=%s, comint..echo=%s, comint..sender=%s,\n"
1481 (current-buffer) ess-language
1482 comint-process-echoes comint-input-sender))
1484 (make-local-variable 'comint-process-echoes)
1485 (make-local-variable 'comint-input-sender)
1486 (make-local-variable 'process-connection-type)
1487 (setq process-connection-type t)
1489 ;; Configuration for SAS/XLispStat input handling
1490 ;; We set comint-process-echoes to t because inferior-ess-input-sender
1491 ;; recopies the input. If comint-process-echoes was *meant* to be t ...
1493 ;; except that XLS doesn't like it. This is an ugly hack that ought
1494 ;; to go into the dialect configuration...
1495 (if (or (string= ess-language "SAS")
1496 (string= ess-language "XLS"))
1497 (setq comint-process-echoes nil)
1498 (setq comint-process-echoes t))
1500 ;; Configuration for S/R input handling
1501 ;; AJR: add KH's fix. This is ugly, change to do it right.
1502 ;; i.e. put it as a buffer local var, in S or R defuns...
1503 (if (or (string= ess-language "S"))
1504 (cond
1505 ((string= ess-dialect "R")
1506 (setq comint-input-sender 'inferior-R-input-sender))
1507 ((or (string= ess-dialect "S3") ;; S dialects
1508 (string= ess-dialect "S4")
1509 (string= ess-dialect "S+3")
1510 (string= ess-dialect "S+4")
1511 (string= ess-dialect "S+5")
1512 (string= ess-dialect "S+6")
1513 (string= ess-dialect "S"))
1514 (setq comint-input-sender 'inferior-ess-input-sender))))
1516 ;; Configuration for Stata input handling
1517 ;; AJR: Stata is hell. This is the primary configuration point.
1518 (if (string= ess-language "STA")
1519 (progn
1520 (setq comint-input-sender 'inferior-ess-input-sender) ; was STA
1521 (setq comint-process-echoes t)))
1523 ;; Configuration for Omegahat input handling
1524 (if (string= ess-language "OMG")
1525 (progn
1526 ;; the following doesn't exist (until needed?)
1527 ;;(setq comint-input-sender 'inferior-OMG-input-sender)
1528 (setq comint-process-echoes nil)))
1530 (ess-write-to-dribble-buffer
1531 (format "(i-ess 2): buf=%s, lang=%s, comint..echo=%s, comint..sender=%s,\n"
1532 (current-buffer) ess-language
1533 comint-process-echoes comint-input-sender))
1534 ;; Font-lock support
1535 ;; AJR: This (the following local-var is already the case!
1536 ;; KH sez: only in XEmacs :-(.
1537 (make-local-variable 'font-lock-defaults)
1538 (setq font-lock-defaults
1539 '(inferior-ess-font-lock-keywords nil nil ((?' . "."))))
1541 (ess-setq-vars-local ess-customize-alist) ; (current-buffer))
1543 (ess-write-to-dribble-buffer
1544 (format "(i-ess 3): curr-buf=%s, comint..echo=%s, comint..sender=%s,\n"
1545 (current-buffer) comint-process-echoes comint-input-sender))
1547 ;;; Completion support ----------------
1549 ;; SJE: comint-dynamic-complete-functions is regarded as a hook, rather
1550 ;; than a regular variable. Note order of completion (thanks David Brahm):
1552 (add-hook 'comint-dynamic-complete-functions
1553 'ess-complete-filename 'append 'local)
1554 (add-hook 'comint-dynamic-complete-functions
1555 'ess-complete-object-name 'append 'local)
1556 (add-hook 'comint-dynamic-complete-functions
1557 'comint-replace-by-expanded-history 'append 'local)
1559 ;; When a hook is buffer-local, the dummy function `t' is added to
1560 ;; indicate that the functions in the global value of the hook
1561 ;; should also be run. SJE: I have removed this, as I think it
1562 ;; interferes with our normal completion.
1563 (remove-hook 'comint-dynamic-complete-functions 't 'local)
1565 ;; MM: in *R* in GNU emacs and in Xemacs, the c*-dyn*-compl*-fun* are now
1566 ;; (ess-complete-filename
1567 ;; ess-complete-object-name
1568 ;; comint-replace-by-expanded-history)
1570 ;; However this fails in Xemacs 21.4.17 where the value in *shell* is
1571 ;; -- the same as in GNU emacs *shell* :
1572 ;; (comint-replace-by-expanded-history shell-dynamic-complete-environment-variable shell-dynamic-complete-command shell-replace-by-expanded-directory comint-dynamic-complete-filename)
1574 ;; and the (Xemacs) global 'Default-value' is
1575 ;; (comint-replace-by-expanded-history comint-dynamic-complete-filename)
1577 ;; (setq comint-completion-addsuffix nil) ; To avoid spaces after filenames
1578 ;; KH: next 2 lines solve.
1579 (make-local-variable 'comint-completion-addsuffix)
1580 (setq comint-completion-addsuffix (cons "/" ""))
1582 (setq comint-input-autoexpand t) ; Only for completion, not on input.
1584 ;;; Keep <tabs> out of the code.
1585 (make-local-variable 'indent-tabs-mode)
1586 (setq indent-tabs-mode nil)
1588 (make-local-variable 'paragraph-start)
1589 (setq paragraph-start (concat inferior-ess-primary-prompt "\\|\^L"))
1590 (make-local-variable 'paragraph-separate)
1591 (setq paragraph-separate "\^L")
1593 ;; SJE Tue 28 Dec 2004: do not attempt to load object name db.
1594 ;; (ess-load-object-name-db-file)
1595 ;; (sleep-for 0.5)
1596 (make-local-variable 'kill-buffer-hook)
1597 (add-hook 'kill-buffer-hook 'ess-kill-buffer-function)
1598 (run-hooks 'inferior-ess-mode-hook)
1599 (message
1600 (concat (substitute-command-keys
1601 "Type \\[describe-mode] for help on ESS version ")
1602 ess-version)))
1604 ;;*;; Commands used exclusively in inferior-ess-mode
1606 ;;;*;;; Main user commands
1608 (defun inferior-ess-input-sender (proc string)
1609 (ess-eval-linewise (concat string "\n") nil nil ess-eval-empty))
1611 (defun inferior-STA-input-sender (proc string)
1612 (ess-eval-linewise (concat string "\n") t t))
1614 ;;> <PD writes>:
1615 ;;> Also, invoking help() from the command line may lead to confusing
1616 ;;> output, somewhat worse with R than with S. You're not really supposed
1617 ;;> to do that (use C-c C-v to invoke ess-display-help-on-object), but it's
1618 ;;> an obvious newcomer's mistake.
1620 ;;> (I wonder: could the elisp-code not quite easily recognize help
1621 ;;> calls (at least in the ?xxx form) and do the right thing automagically?)
1623 ;;> As promised, here is a quick hack:
1624 ;; ___hack much improved by MM___ , both help(.) and ?... now work
1625 ;; FIXME: Note that '??' nicely works in *R*, but
1626 ;; 'type ? topic' doesn't use ess-help {but display in *R*}
1627 (defconst inferior-R-1-input-help (format "^ *help *(%s)" ess-help-arg-regexp))
1628 (defconst inferior-R-2-input-help (format "^ *\\? *%s" ess-help-arg-regexp))
1629 (defconst inferior-R-page (format "^ *page *(%s)" ess-help-arg-regexp))
1631 (defun inferior-R-input-sender (proc string)
1632 ;; REALLY only for debugging: this S_L_O_W_S D_O_W_N [here AND below]
1633 ;;(ess-write-to-dribble-buffer (format "(inf..-R-..): string='%s'; " string))
1634 ;; rmh: 2002-01-12 catch page() in R
1635 (let ((help-string (or (string-match inferior-R-1-input-help string)
1636 (string-match inferior-R-2-input-help string)))
1637 (page-string (string-match inferior-R-page string)))
1638 (if (or help-string page-string)
1639 (let* ((string2 (match-string 2 string))
1640 (string2-rt (concat string2 ".rt")))
1641 ;; (ess-write-to-dribble-buffer (format " new string='%s'\n" string2))
1642 (beginning-of-line)
1643 (if (looking-at inferior-ess-primary-prompt)
1644 (progn
1645 (end-of-line)
1646 (insert-before-markers string)) ;; emacs 21.0.105 and older
1647 (delete-backward-char 1)) ;; emacs 21.0.106 and newer
1648 (if page-string
1649 (progn
1650 (ess-command (concat string2 "\n")
1651 (get-buffer-create (concat string2 ".rt")))
1652 (ess-eval-linewise "\n")
1653 (switch-to-buffer-other-window string2-rt)
1654 (R-transcript-mode))
1655 (ess-display-help-on-object
1656 (if (string= string2 "") "help" string2))
1657 (ess-eval-linewise "\n")))
1658 ;; else: normal command
1659 (inferior-ess-input-sender proc string))))
1662 (defun inferior-ess-send-input ()
1663 "Sends the command on the current line to the ESS process."
1664 (interactive)
1665 (ess-make-buffer-current)
1666 (run-hooks 'ess-send-input-hook)
1667 (comint-send-input)
1668 (setq ess-object-list nil)) ;; Will be reconstructed from cache if needs be
1670 (defun inferior-ess-get-old-input ()
1671 "Return the ESS command surrounding point."
1672 (save-excursion
1673 (beginning-of-line)
1674 (if (not (looking-at inferior-ess-prompt))
1675 (ess-error "No command on this line."))
1676 (if (looking-at inferior-ess-primary-prompt) nil
1677 (re-search-backward (concat "^" inferior-ess-primary-prompt)))
1678 (comint-skip-prompt)
1679 (let (command
1680 (beg (point)))
1681 (end-of-line)
1682 (setq command (buffer-substring beg (point)))
1683 (forward-line 1)
1684 (while (looking-at inferior-ess-secondary-prompt)
1685 (comint-skip-prompt)
1686 (setq beg (point))
1687 (end-of-line)
1688 (setq command (concat command "\n" (buffer-substring beg (point))))
1689 (forward-line 1))
1690 (forward-line -1)
1691 (setq ess-temp-point (point))
1692 command)))
1694 ;;;*;;; Hot key commands
1696 (defun ess-execute-objects (posn)
1697 "Send the objects() command to the ESS process.
1698 By default, gives the objects at position 1.
1699 A prefix argument toggles the meaning of `ess-execute-in-process-buffer'.
1700 A prefix argument of 2 or more means get objects for that position.
1701 A negative prefix argument gets the objects for that position
1702 and toggles `ess-execute-in-process-buffer' as well."
1703 (interactive "P")
1704 (ess-make-buffer-current)
1705 (let* ((num-arg (if (listp posn)
1706 (if posn -1 1)
1707 (prefix-numeric-value posn)))
1708 (the-posn (if (< num-arg 0) (- num-arg) num-arg))
1709 (invert (< num-arg 0))
1710 (the-command (format inferior-ess-objects-command the-posn ".*"))
1711 (the-message (concat ">>> Position "
1712 (number-to-string the-posn)
1713 " ("
1714 (nth (1- the-posn) (ess-search-list))
1715 ")\n")))
1716 (ess-execute the-command invert "S objects" the-message)))
1718 ;;;; S4 Version
1719 ;;;; soup-up the interactive usage: allow modifications to a default pattern
1720 ;;(defun ess-execute-objects (posn)
1721 ;; "Send the `inferior-ess-objects-command' to the ESS process.
1722 ;;No prefix argument uses position 1 and pattern inferior-ess-objects-pattern.
1723 ;;A nonnegative prefix gets objects for that position and prompts for
1724 ;; the pattern.
1725 ;;A negative prefix also toggles ess-execute-in-process-buffer."
1726 ;; (interactive "P")
1727 ;; (ess-make-buffer-current)
1728 ;; (let* ((num-arg (if (listp posn) 1
1729 ;; (prefix-numeric-value posn)))
1730 ;; (the-posn (if (< num-arg 0) (- num-arg) num-arg))
1731 ;; (invert (< num-arg 0))
1732 ;; (pattern (if current-prefix-arg (read-string "Pattern (.*): ")
1733 ;; inferior-ess-objects-pattern))
1734 ;; (pattern (if (string= pattern "") ".*" pattern))
1735 ;; (the-command (format inferior-ess-objects-command the-posn pattern))
1736 ;; (the-message (concat ">>> Position "
1737 ;; the-posn
1738 ;; " ("
1739 ;; (nth (1- the-posn) (ess-search-list))
1740 ;; ") (pattern = "
1741 ;; pattern
1742 ;; ")\n")))
1743 ;; (ess-execute the-command invert "S objects" the-message)))
1745 (defun ess-execute-search (invert)
1746 "Send the `inferior-ess-search-list-command' command to the `ess-language' process.
1747 [search(..) in S]"
1748 (interactive "P")
1749 (ess-execute inferior-ess-search-list-command invert "S search list"))
1751 (defun ess-execute-attach (dir &optional posn)
1752 "Attach a directory in the `ess-language' process with the attach() command.
1753 When used interactively, user is prompted for DIR to attach and
1754 prefix argument is used for POSN (or 2, if absent.)
1755 Doesn't work for data frames."
1756 (interactive "DAttach directory: \nP")
1757 (ess-execute (concat "attach(\""
1758 (directory-file-name (expand-file-name dir))
1759 "\""
1760 (if posn (concat "," (number-to-string
1761 (prefix-numeric-value posn))))
1762 ")") 'buffer)
1763 (setq ess-sp-change t))
1765 (defun ess-execute-screen-options ()
1766 "Cause S to set the \"width\" option to 1 less than the frame width.
1767 Also sets the \"length\" option to 99999.
1768 This is a good thing to put in `ess-post-run-hook' --- for the S dialects."
1769 (interactive)
1770 (if (string= ess-language "S")
1771 (let ((ess-current-process-name)); local, used as S-process buffer below
1772 ;; when run inside an ESS process buffer, use that one
1773 (if (and (comint-check-proc (current-buffer)); has running proc
1774 (memq major-mode '(inferior-ess-mode)))
1775 (setq ess-current-process-name ess-local-process-name))
1777 (ess-eval-linewise (format "options(width=%d,length=99999)"
1778 (1- (window-width)))
1779 nil nil nil 'wait-prompt))))
1781 (defun ess-execute (command &optional invert buff message)
1782 "Send a command to the ESS process.
1783 A newline is automatically added to COMMAND. Prefix arg (or second arg
1784 INVERT) means invert the meaning of
1785 `ess-execute-in-process-buffer'. If INVERT is 'buffer, output is
1786 forced to go to the process buffer. If the output is going to a
1787 buffer, name it *BUFF*. This buffer is erased before use. Optional
1788 fourth arg MESSAGE is text to print at the top of the buffer (defaults
1789 to the command if BUFF is not given.)"
1790 (interactive (list
1791 (read-from-minibuffer "Execute> "
1793 ess-mode-minibuffer-map)
1794 current-prefix-arg))
1795 (ess-make-buffer-current)
1796 (let ((the-command (concat command "\n"))
1797 (buff-name (concat "*" (or buff "ess-output") "*"))
1798 (in-pbuff (if invert (or (eq invert 'buffer)
1799 (not ess-execute-in-process-buffer))
1800 ess-execute-in-process-buffer)))
1801 (if in-pbuff
1802 (ess-eval-linewise the-command)
1803 (let ((buff (ess-create-temp-buffer buff-name)))
1804 (save-excursion
1805 (set-buffer buff)
1806 (ess-command the-command (get-buffer buff-name));; sleep?
1807 (goto-char (point-min))
1808 (if message (insert message)
1809 (if buff nil
1810 ;; Print the command in the buffer if it has not been
1811 ;; given a special name
1812 (insert "> " the-command)))
1813 ;;(make-local-variable 'ess-local-process-name)
1814 (setq ess-local-process-name ess-current-process-name))
1815 (ess-display-temp-buffer buff)))))
1817 ;;;*;;; Quitting
1819 (defun ess-quit ()
1820 "Issue an exiting command to the inferior process, additionally
1821 also running \\[ess-cleanup]. For R, runs \\[ess-quit-r], see there."
1822 (interactive)
1823 (if (string-equal ess-dialect "R")
1824 (ess-quit-r)
1825 (ess-force-buffer-current "Process to quit: ")
1826 (ess-make-buffer-current)
1827 (let ((sprocess (get-ess-process ess-current-process-name)))
1828 (if (not sprocess) (error "No ESS process running."))
1829 (when (yes-or-no-p (format "Really quit ESS process %s? " sprocess))
1830 (ess-cleanup)
1831 (goto-char (marker-position (process-mark sprocess)))
1832 (insert inferior-ess-exit-command)
1833 (process-send-string sprocess inferior-ess-exit-command)
1834 ;;SJE - suggest no need to rename buffer upon exit.
1835 ;;(rename-buffer (concat (buffer-name) "-exited") t)
1836 ))))
1838 (defun ess-quit-r ()
1839 "Issue an exiting command to an inferior R process, and optionally clean up.
1840 This version is for killing *R* processes; it asks the extra question
1841 regarding whether the workspace image should be saved."
1842 (ess-force-buffer-current "Process to quit: ")
1843 (ess-make-buffer-current)
1844 (let (cmd
1845 ;;Q response
1846 (sprocess (get-ess-process ess-current-process-name)))
1847 (if (not sprocess) (error "No ESS process running."))
1848 ;;Q (setq response (completing-read "Save workspace image? "
1849 ;;Q '( ( "yes".1) ("no" . 1) ("cancel" . 1))
1850 ;;Q nil t))
1851 ;;Q (if (string-equal response "")
1852 ;;Q (setq response "default")); which will ask again (in most situations)
1853 ;;Q (unless (string-equal response "cancel")
1854 (ess-cleanup)
1855 ;;Q (setq cmd (format "q(\"%s\")\n" response))
1856 (setq cmd "q()\n")
1857 (goto-char (marker-position (process-mark sprocess)))
1858 (process-send-string sprocess cmd)
1859 ;;(rename-buffer (concat (buffer-name) "-exited") t)
1860 ;;Q )
1863 (defun ess-abort ()
1864 "Kill the ESS process, without executing .Last or terminating devices.
1865 If you want to finish your session, use \\[ess-quit] instead."
1866 ;;; Provided as a safety measure over the default binding of C-c C-z in
1867 ;;; comint-mode-map.
1868 (interactive)
1869 (ding)
1870 (message "WARNING: \\[inferior-ess-exit-command] will not be executed and graphics devices won't finish properly!")
1871 (sit-for 2)
1872 (if (yes-or-no-p "Still abort? ")
1873 (comint-quit-subjob)
1874 (message "Good move.")))
1876 (defun ess-cleanup ()
1877 "Possibly kill or offer to kill, depending on the value of
1878 `ess-S-quit-kill-buffers-p', all buffers associated with this ESS process.
1879 Leaves you in the ESS process buffer. It's a good idea to run this
1880 before you quit. It is run automatically by \\[ess-quit]."
1881 (interactive)
1882 (let ((the-procname (or (ess-make-buffer-current) ess-local-process-name)))
1883 (if the-procname nil
1884 (error "I don't know which ESS process to clean up after!"))
1886 (or (eq ess-S-quit-kill-buffers-p t)
1887 (and
1888 (eq ess-S-quit-kill-buffers-p 'ask)
1889 (y-or-n-p
1890 (format
1891 "Delete all buffers associated with process %s? " the-procname))))
1892 (save-excursion
1893 (mapcar '(lambda (buf)
1894 (set-buffer buf)
1895 ;; Consider buffers for which
1896 ;; ess-local-process-name is the same as
1897 ;; the-procname
1898 (if (and (not (get-buffer-process buf))
1899 ess-local-process-name
1900 (equal ess-local-process-name the-procname))
1901 (kill-buffer buf)))
1902 (buffer-list))))
1903 (ess-switch-to-ESS nil)))
1905 (defun ess-kill-buffer-function nil
1906 "Function run just before an ESS process buffer is killed."
1907 ;; This simply deletes the buffers process to avoid an Emacs bug
1908 ;; where the sentinel is run *after* the buffer is deleted
1909 (let ((proc (get-buffer-process (current-buffer))))
1910 (if proc (delete-process proc))))
1912 ;;*;; Object name completion
1914 ;;;*;;; The user completion command
1915 (defun ess-complete-object-name (&optional listcomp)
1916 "Perform completion on `ess-language' object preceding point.
1917 The object is compared against those objects known by
1918 `ess-get-object-list' and any additional characters up to ambiguity are
1919 inserted. Completion only works on globally-known objects (including
1920 elements of attached data frames), and thus is most suitable for
1921 interactive command-line entry, and not so much for function editing
1922 since local objects (e.g. argument names) aren't known.
1924 Use \\[ess-resynch] to re-read the names of the attached directories.
1925 This is done automatically (and transparently) if a directory is
1926 modified (S only!), so the most up-to-date list of object names is always
1927 available. However attached dataframes are *not* updated, so this
1928 command may be necessary if you modify an attached dataframe.
1930 If ARG is non-nil, no completion is attempted, but the available
1931 completions are listed [__UNIMPLEMENTED__]."
1932 (interactive "P");; FIXME : the `listcomp' argument is NOT used
1933 (ess-make-buffer-current)
1934 (if (memq (char-syntax (preceding-char)) '(?w ?_))
1935 (let* ((comint-completion-addsuffix nil)
1936 (end (point))
1937 (buffer-syntax (syntax-table))
1938 (beg (unwind-protect
1939 (save-excursion
1940 (set-syntax-table ess-mode-syntax-table)
1941 (backward-sexp 1)
1942 (point))
1943 (set-syntax-table buffer-syntax)))
1944 (full-prefix (buffer-substring beg end))
1945 (pattern full-prefix)
1946 ;; See if we're indexing a list with `$'
1947 (listname (if (string-match "\\(.+\\)\\$\\(\\(\\sw\\|\\s_\\)*\\)$"
1948 full-prefix)
1949 (progn
1950 (setq pattern
1951 (if (not (match-beginning 2)) ""
1952 (substring full-prefix
1953 (match-beginning 2)
1954 (match-end 2))))
1955 (substring full-prefix (match-beginning 1)
1956 (match-end 1)))))
1957 ;; are we trying to get a slot via `@' ?
1958 (classname (if (string-match "\\(.+\\)@\\(\\(\\sw\\|\\s_\\)*\\)$"
1959 full-prefix)
1960 (progn
1961 (setq pattern
1962 (if (not (match-beginning 2)) ""
1963 (substring full-prefix
1964 (match-beginning 2)
1965 (match-end 2))))
1966 (ess-write-to-dribble-buffer
1967 (format "(ess-C-O-Name : slots..) : patt=%s"
1968 pattern))
1969 (substring full-prefix (match-beginning 1)
1970 (match-end 1)))))
1971 (components (if listname
1972 (ess-object-names listname)
1973 (if classname
1974 (ess-slot-names classname)
1975 ;; Default case: It hangs here when
1976 ;; options(error=recoves) :
1977 (ess-get-object-list ess-current-process-name)))))
1978 ;; always return a non-nil value to prevent history expansions
1979 (or (comint-dynamic-simple-complete pattern components) 'none))))
1981 (defun ess-list-object-completions nil
1982 "List all possible completions of the object name at point."
1983 (interactive)
1984 (ess-complete-object-name t));; FIXME: NOT WORKING since argument is unused!
1986 ;;;*;;; Support functions
1987 (defun ess-extract-onames-from-alist (alist posn &optional force)
1988 "Return the object names in position POSN of ALIST.
1989 ALIST is an alist like `ess-sl-modtime-alist'. POSN should be in 1 .. (length
1990 ALIST). If optional third arg FORCE is t, the corresponding element
1991 of the search list is re-read. Otherwise it is only re-read if it's a
1992 directory and has been modified since it was last read."
1993 (let* ((entry (nth (1- posn) alist))
1994 (dir (car entry))
1995 (timestamp (car (cdr entry)))
1996 (new-modtime (ess-dir-modtime dir)))
1997 ;; Refresh the object listing if necessary
1998 (if (or force (not (equal new-modtime timestamp)))
1999 (setcdr (cdr entry) (ess-object-names dir posn)))
2000 (cdr (cdr entry))))
2002 (defun ess-dir-modtime (dir)
2003 "Return the last modtime if DIR is a directory, and nil otherwise."
2004 (and ess-filenames-map
2005 (file-directory-p dir)
2006 (nth 5 (file-attributes dir))))
2008 (defun ess-object-modtime (object)
2009 "Return the modtime of the S object OBJECT (a string).
2010 Searches along the search list for a file named OBJECT and returns its modtime
2011 Returns nil if that file cannot be found, i.e., for R or any non-S language!"
2012 (let ((path (ess-search-list))
2013 result)
2014 (while (and (not result) path)
2015 (setq result (file-attributes
2016 (concat (file-name-as-directory (car path))
2017 object)))
2018 (setq path (cdr path)))
2019 (nth 5 result)))
2021 (defun ess-modtime-gt (mod1 mod2)
2022 "Return t if MOD1 is later than MOD2."
2023 (and mod1
2024 (or (> (car mod1) (car mod2))
2025 (and (= (car mod1) (car mod2))
2026 (> (car (cdr mod1)) (car (cdr mod2)))))))
2028 (defun ess-get-object-list (name)
2029 "Return a list of current S object names associated with process NAME,
2030 using `ess-object-list' if that is non-nil."
2031 (or ess-object-list ;; <<- MM: I think this is now always nil
2032 (save-excursion
2033 (set-buffer (process-buffer (get-ess-process name)))
2034 (ess-make-buffer-current)
2035 (ess-write-to-dribble-buffer (format "(get-object-list %s) .." name))
2036 (if (or (not ess-sl-modtime-alist) ess-sp-change)
2037 (progn (ess-write-to-dribble-buffer "--> (ess-get-modtime-list)\n")
2038 (ess-get-modtime-list))
2039 ;;else
2040 (ess-write-to-dribble-buffer " using existing ess-*-alist\n")
2042 (let* ((alist ess-sl-modtime-alist)
2043 (i 2)
2044 (n (length alist))
2045 result)
2046 (ess-write-to-dribble-buffer (format " (length alist) : %d\n" n))
2047 ;; Always force a re-read of position 1 :
2048 (setq result (ess-extract-onames-from-alist alist 1 'force))
2049 (ess-write-to-dribble-buffer
2050 (format " have re-read pos=1: -> length %d\n" (length result)))
2051 ;; Re-read remaining directories if necessary.
2052 (while (<= i n)
2053 (setq result
2054 (append result
2055 (ess-extract-onames-from-alist alist i)))
2056 (setq i (1+ i)))
2057 (setq ess-object-list (ess-uniq-list result))))))
2059 (defun ess-get-words-from-vector (command)
2060 "Evaluate the S command COMMAND, which returns a character vector.
2061 Return the elements of the result of COMMAND as an alist of strings.
2062 COMMAND should have a terminating newline."
2063 (let ((tbuffer (get-buffer-create
2064 " *ess-names-list*")); initial space: disable-undo
2065 names)
2066 (save-excursion
2067 (set-buffer tbuffer)
2068 (ess-command command tbuffer 'sleep)
2069 (goto-char (point-min))
2070 (if ess-verbose (ess-write-to-dribble-buffer "ess-get-words-.. "))
2071 (if (not (looking-at "\\s-*\\[1\\]"))
2072 (progn (if ess-verbose
2073 (ess-write-to-dribble-buffer "not seeing \"[1]\".. "))
2074 (setq names nil)
2076 (goto-char (point-max))
2077 (while (re-search-backward "\"\\([^\"]*\\)\"" nil t)
2078 (setq names (cons (buffer-substring (match-beginning 1)
2079 (match-end 1)) names))))
2080 ;;DBG, do *not* (i.e., comment):
2081 (kill-buffer tbuffer)
2083 (if ess-verbose
2084 (ess-write-to-dribble-buffer
2085 (if (> (length names) 5)
2086 (format " |-> (length names)= %d\n" (length names))
2087 (format " |-> names= '%s'\n" names))))
2088 names))
2090 (defun ess-compiled-dir (dir)
2091 "Return non-nil if DIR is an S object directory with special files.
2092 I.e. if the filenames in DIR are not representative of the objects in DIR."
2093 (or (file-exists-p (concat (file-name-as-directory dir) "___nonfile"))
2094 (file-exists-p (concat (file-name-as-directory dir) "__BIGIN"))
2095 (file-exists-p (concat (file-name-as-directory dir) "___NONFI"))))
2097 (defun ess-object-names (obj &optional pos)
2098 "Return alist of S object names in directory (or object) OBJ.
2099 If OBJ is a directory name (begins with `/') returns a listing of that dir.
2100 This may use the search list position POS if necessary.
2101 If OBJ is an object name, returns result of S command names(OBJ).
2102 If OBJ is nil or not a directory, POS must be supplied, and objects(POS) is returned.
2103 In all cases, the value is an list of object names."
2105 ;; FIXME: in both cases below, use the same fallback "objects(POS)" -- merge!
2106 (if (and obj (file-accessible-directory-p obj))
2107 ;; Check the pre-compiled object list in ess-object-name-db first
2109 ;; FIXME: If used at all, ess-object-name-db should not only
2110 ;; ----- be used in the directory case !!
2111 (or (cdr-safe (assoc obj ess-object-name-db))
2112 ;; Take a directory listing
2113 (and ess-filenames-map
2114 ;; first try .Data subdirectory:
2115 ;;FIXME: move ".Data" or ``this function'' to essd-sp6.el etc:
2116 (let ((dir (concat (file-name-as-directory obj) ".Data")))
2117 (if (not (file-accessible-directory-p dir))
2118 (setq dir obj))
2119 (and (not (ess-compiled-dir dir))
2120 (directory-files dir))))
2121 ;; Get objects(pos) instead
2122 (and (or (ess-write-to-dribble-buffer
2123 (format "(ess-object-names ..): directory %s not used\n" obj))
2126 (ess-get-words-from-vector
2127 (format inferior-ess-objects-command pos))))
2128 ;; "else" should really give an error!
2129 ;; would need pos = which(obj = search())
2131 ;; else
2132 (or (and obj ;; want names(obj)
2133 (or (ess-write-to-dribble-buffer
2134 (format "(ess-object-names obj=%s): no directory - trying names\n"
2135 obj))
2137 (ess-get-words-from-vector
2138 (format inferior-ess-safe-names-command obj)))
2139 (and nil; must return nil
2140 (ess-write-to-dribble-buffer
2141 (format "(ess-object-names obj=%s): no dir.; -> objects()\n" obj)))
2142 ;; get objects(pos)
2143 (ess-get-words-from-vector
2144 (format inferior-ess-objects-command pos))))) ; had 2nd arg ".*"
2145 ; s4 needs 2
2146 ; args, rest only need 1 ?
2147 ; changes needed to allow for
2148 ; pattern argument to
2149 ; .SmodeObs
2151 (defun ess-slot-names (obj)
2152 "Return alist of S4 slot names of S4 object OBJ."
2153 (ess-get-words-from-vector (format "slotNames(%s)\n" obj)))
2156 ;;; SJE: Wed 29 Dec 2004 --- remove this function.
2157 ;;; rmh: Wed 5 Jan 2005 --- bring it back for use on Windows
2158 (defun ess-create-object-name-db ()
2159 "Create a database of object names in standard S directories. This
2160 database is saved in the file specified by `ess-object-name-db-file',
2161 and is loaded when `ess-mode' is loaded. It defines the variable
2162 `ess-object-name-db', which is used for completions.
2164 Before you call this function, modify the S search list so that it contains
2165 all the non-changing (i.e. system) S directories. All positions of the search
2166 list except for position 1 are searched and stored in the database.
2168 After running this command, you should move ess-namedb.el to a directory in
2169 the `load-path'."
2170 (interactive)
2171 (setq ess-object-name-db nil)
2172 (let ((search-list (cdr (ess-search-list)))
2173 (pos 2)
2174 name
2175 (buffer (get-buffer-create " *ess-db*"))
2176 (temp-object-name-db nil)
2177 (temp-object-name-db-file ess-object-name-db-file))
2179 (ess-write-to-dribble-buffer
2180 (format "(object db): search-list=%s \n " search-list))
2181 (while search-list
2182 (message "Searching %s" (car search-list))
2183 (setq temp-object-name-db (cons (cons (car search-list)
2184 (ess-object-names nil pos))
2185 temp-object-name-db))
2186 (setq search-list (cdr search-list))
2187 (ess-write-to-dribble-buffer
2188 (format "(object db): temp-obj-name-db=%s \n pos=%s"
2189 temp-object-name-db pos))
2190 (setq pos (1+ pos)))
2192 (save-excursion
2193 (set-buffer buffer)
2194 (erase-buffer)
2195 (insert "(setq ess-object-name-db '")
2196 (prin1 temp-object-name-db (current-buffer))
2197 (insert ")\n")
2198 (setq name (expand-file-name ess-object-name-db-file))
2199 (write-region (point-min) (point-max) name)
2200 (message "Wrote %s" name))
2201 (kill-buffer buffer)
2202 (setq ess-object-name-db temp-object-name-db)))
2204 (defun ess-resynch nil
2205 "Reread all directories/objects in variable `ess-search-list' to
2206 form completions."
2207 (interactive)
2208 (if (ess-make-buffer-current) nil
2209 (error "Not an ESS process buffer"))
2210 (setq ess-sl-modtime-alist nil)
2211 (setq ess-object-list nil)
2212 (setq ess-object-name-db nil) ; perhaps it would be better to reload?
2213 (setq ess-sp-change t)
2214 (ess-get-modtime-list))
2217 (defun ess-complete-filename ()
2218 "Do file completion only within strings, or when ! call is being used."
2219 (if (comint-within-quotes
2220 (1- (process-mark (get-buffer-process (current-buffer)))) (point))
2221 ;; (- comint-last-input-start 1) (point)) <- from S4 modeadds.
2222 ;; changed on 4/12/96 (dxsun)
2223 ;; This is sensible, but makes it hard to use history refs
2224 ;; (or
2225 ;; (save-excursion
2226 ;; (goto-char comint-last-input-start)
2227 ;; (looking-at "\\s-*!"))
2228 ;; (comint-within-quotes comint-last-input-start (point)))
2229 (progn
2230 ;;DBG (ess-write-to-dribble-buffer "ess-complete-f.name: within-quotes")
2231 (if (featurep 'xemacs) ;; work around Xemacs bug
2232 (comint-dynamic-complete-filename)
2233 ;; GNU emacs and correctly working Xemacs:
2234 (comint-replace-by-expanded-filename))
2235 ;; always return t if in a string
2236 t)))
2238 (defun ess-after-pathname-p nil
2239 ;; Heuristic: after partial pathname if it looks like we're in a
2240 ;; string, and that string looks like a pathname. Not the best for
2241 ;; use with unix() (or it's alias, !). Oh well.
2242 (save-excursion
2243 (save-match-data
2244 (let ((opoint (point)))
2245 (and (re-search-backward "\\(\"\\|'\\)[~/#$.a-zA-Z0-9][^ \t\n\"']*"
2246 nil t)
2247 (eq opoint (match-end 0)))))))
2249 ;;*;; Functions handling the search list
2251 (defun ess-search-list ()
2252 "Return the current search list as a list of strings.
2253 Elements which are apparently directories are expanded to full dirnames.
2254 Is *NOT* used by \\[ess-execute-search],
2255 but by \\[ess-resynch], \\[ess-get-object-list], \\[ess-get-modtime-list],
2256 \\[ess-execute-objects], \\[ess-object-modtime], \\[ess-create-object-name-db],
2257 and (indirectly) by \\[ess-get-help-files-list]."
2258 (save-excursion
2259 (let ((result nil))
2260 (set-buffer (get-ess-buffer ess-current-process-name));to get *its* local vars
2261 (if (and ess-search-list (not ess-sp-change))
2262 ;; use cache:
2263 ess-search-list
2264 ;; else, re-compute:
2265 (ess-write-to-dribble-buffer " (ess-search-list: re-computing..) ")
2266 (let ((tbuffer (get-buffer-create " *search-list*"))
2267 (homedir ess-directory)
2268 (my-search-cmd inferior-ess-search-list-command); from ess-buffer
2269 elt)
2270 (save-excursion
2271 (set-buffer tbuffer)
2272 ;; guaranteed by the initial space in its name: (buffer-disable-undo)
2273 (ess-command my-search-cmd tbuffer 0.2); <- sleep; does (erase-buffer)
2274 (goto-char (point-min))
2275 (ess-write-to-dribble-buffer
2276 (format "after '%s', point-max=%d\n" my-search-cmd (point-max)))
2277 (while (re-search-forward "\"\\([^\"]*\\)\"" nil t)
2278 (setq elt (buffer-substring (match-beginning 1) (match-end 1)))
2279 ;;Dbg: (ess-write-to-dribble-buffer (format " .. elt= %s \t" elt))
2280 (if (and (string-match "^[^/]" elt)
2281 (file-directory-p (concat ess-directory elt)))
2282 (progn
2283 ;;Dbg: (ess-write-to-dribble-buffer "*IS* directory\n")
2284 (setq elt (concat homedir elt)))
2285 ;;else
2286 ;;dbg
2287 ;;- (ess-write-to-dribble-buffer "not dir.\n")
2289 (setq result (append result (list elt))))
2290 (kill-buffer tbuffer)))
2291 (setq ess-search-list result)
2292 (setq ess-sp-change nil)
2293 result))))
2295 ;;; ess-sl-modtime-alist is a list with elements as follows:
2296 ;;; * key (directory or object name)
2297 ;;; * modtime (list of 2 integers)
2298 ;;; * name, name ... (accessible objects in search list posn labeled by key)
2299 ;;; It is a buffer-local variable (belonging to e.g. *R*, *S+6*, .. etc)
2300 ;;; and has the same number of elements and is in the same order as the
2301 ;;; S search list
2303 (defun ess-get-modtime-list ()
2304 "Record the modification times of the directories in the search list,
2305 and the objects in those directories.
2306 The result is stored in `ess-sl-modtime-alist'."
2307 ;; Operation applies to process of current buffer
2308 (let* ((searchlist (ess-search-list))
2309 (index 1)
2310 posn
2311 newalist)
2312 (while searchlist
2313 (setq posn (car searchlist))
2314 (setq newalist
2315 (append
2316 newalist
2317 (list (or (assoc posn ess-sl-modtime-alist)
2318 (append
2319 (list posn (ess-dir-modtime posn))
2320 (prog2
2321 (message "Forming completions for %s..." posn)
2322 (ess-object-names posn index)
2323 (message "Forming completions for %s...done" posn)
2324 ))))))
2325 (setq index (1+ index))
2326 (setq searchlist (cdr searchlist)))
2327 ;;DBG:
2328 (ess-write-to-dribble-buffer
2329 (format "ess-get-modtime-list: new alist of length %d\n"
2330 (length newalist)));; todo : also give length of components!
2332 (setq ess-sl-modtime-alist newalist)))
2335 (defun ess-search-path-tracker (str)
2336 "Check if input STR changed the search path.
2337 This function monitors user input to the inferior ESS process so that
2338 Emacs can keep the variable `ess-search-list' up to date. `completing-read' in
2339 \\[ess-read-object-name] uses this list indirectly when it prompts for help or
2340 for an object to dump."
2341 (if (string-match ess-change-sp-regexp str)
2342 (setq ess-sp-change t)))
2344 \f ; Miscellaneous routines
2346 ;;;*;;; Routines for reading object names
2348 (defun ess-read-object-name (p-string)
2349 "Read an S object name from the minibuffer with completion, and return it.
2350 P-STRING is the prompt string."
2351 (let* ((default (ess-read-object-name-dump))
2352 (prompt-string (if default
2353 (format "%s(default %s) " p-string default)
2354 p-string))
2355 (object-list (mapcar 'list (ess-get-object-list ess-local-process-name)))
2356 (spec (completing-read prompt-string object-list)))
2357 (list (cond
2358 ((string= spec "") default)
2359 (t spec)))))
2361 (defun ess-read-object-name-default ()
2362 "Return the object name at point, or nil if none."
2363 (condition-case ()
2364 (save-excursion
2365 ;; The following line circumvents an 18.57 bug in following-char
2366 (if (eobp) (backward-char 1)) ; Hopefully buffer is not empty!
2367 ;; Get onto a symbol
2368 (catch 'nosym ; bail out if there's no symbol at all before point
2369 (while (/= (char-syntax (following-char)) ?w)
2370 (if (bobp) (throw 'nosym nil) (backward-char 1)))
2371 (let*
2372 ((end (progn (forward-sexp 1) (point)))
2373 (beg (progn (backward-sexp 1) (point))))
2374 (buffer-substring beg end))))
2375 (error nil)))
2377 (defun ess-read-object-name-dump ()
2378 "Return the object name at point, or \"Temporary\" if none."
2379 (condition-case ()
2380 (save-excursion
2381 ;; The following line circumvents an 18.57 bug in following-char
2382 (if (eobp) (backward-char 1)) ; Hopefully buffer is not empty!
2383 ;; Get onto a symbol
2384 (catch 'nosym ; bail out if there's no symbol at all before point
2385 (while (/= (char-syntax (following-char)) ?w)
2386 (if (bobp) (throw 'nosym nil) (backward-char 1)))
2387 (let*
2388 ((end (progn (forward-sexp 1) (point)))
2389 (beg (progn (backward-sexp 1) (point)))
2390 (object-name (buffer-substring beg end)))
2391 (or object-name "Temporary"))))
2392 (error nil)))
2395 ;;*;; Temporary buffer handling
2397 ;(defun ess-create-temp-buffer (name)
2398 ; "Create an empty buffer called NAME, but doesn't display it."
2399 ; (let ((buff (get-buffer-create name)))
2400 ; (save-excursion
2401 ; (set-buffer buff)
2402 ; (erase-buffer))
2403 ; buff))
2406 ;; Ed Kademan's version:
2407 ;From: Ed Kademan <kademan@phz.com>
2408 ;Subject: Re: ess-mode 5.1.16; search list
2409 ;To: rossini@biostat.washington.edu (A.J. Rossini)
2410 ;Cc: Martin Maechler <maechler@stat.math.ethz.ch>, ess-bugs@stat.math.ethz.ch
2411 ;Date: 26 Jul 2000 16:12:12 -0400
2413 ;Dear Tony Rossini,
2415 ;I was having trouble looking at the search list under ess. When I
2416 ;started up multiple inferior processes---each for a different
2417 ;dialect---ess-mode would issue the wrong variant of the "search"
2418 ;command when I typed C-c C-s. In case it is useful let me tell you
2419 ;what I did to get it to work for me.
2421 ;I added the component:
2422 ; (inferior-ess-search-list-command . "search()\n")
2423 ;to S+3-customize-alist and R-customize-alist, and then I redefined the
2424 ;ess-create-temp-buffer function as follows:
2425 (defun ess-create-temp-buffer (name)
2426 "Create an empty buffer called NAME."
2427 (let ((buff (get-buffer-create name))
2428 (elca (eval ess-local-customize-alist)))
2429 (save-excursion
2430 (set-buffer buff)
2431 (erase-buffer)
2432 (ess-setq-vars-local elca buff))
2433 buff))
2434 ;;These two steps seem to insure that the temporary buffer in which the
2435 ;;search results appear has the correct version of the local variables.
2436 ;;I am not that well acquainted with the ess code and don't know whether
2437 ;;this is a good fundamental way of fixing the problem, or even whether
2438 ;;or not this breaks some other feature of ess-mode that I never use.
2439 ;;Thanks for listening.
2440 ;;Ed K.
2441 ;;--
2442 ;;Ed Kademan 508.651.3700
2443 ;;PHZ Capital Partners 508.653.1745 (fax)
2444 ;;321 Commonwealth Road <kademan@phz.com>
2445 ;;Wayland, MA 01778
2449 (defun ess-display-temp-buffer (buff)
2450 "Display the buffer BUFF using `temp-buffer-show-function' and respecting
2451 `ess-display-buffer-reuse-frames'."
2452 (let ((display-buffer-reuse-frames ess-display-buffer-reuse-frames))
2453 (funcall (or temp-buffer-show-function 'display-buffer) buff)))
2455 ;;*;; Error messages
2457 (defun ess-error (msg)
2458 "Something bad has happened.
2459 Display the S buffer, and cause an error displaying MSG."
2460 (display-buffer (process-buffer (get-ess-process ess-current-process-name)))
2461 (error msg))
2463 \f ; Provide package
2465 (provide 'ess-inf)
2467 \f ; Local variables section
2469 ;;; This file is automatically placed in Outline minor mode.
2470 ;;; The file is structured as follows:
2471 ;;; Chapters: ^L ;
2472 ;;; Sections: ;;*;;
2473 ;;; Subsections: ;;;*;;;
2474 ;;; Components: defuns, defvars, defconsts
2475 ;;; Random code beginning with a ;;;;* comment
2477 ;;; Local variables:
2478 ;;; mode: emacs-lisp
2479 ;;; outline-minor-mode: nil
2480 ;;; mode: outline-minor
2481 ;;; outline-regexp: "\^L\\|\\`;\\|;;\\*\\|;;;\\*\\|(def[cvu]\\|(setq\\|;;;;\\*"
2482 ;;; End:
2484 ;;; ess-inf.el ends here