482
482
;;*;; General process handling code
484
(defun get-ess-process (name)
485
"Return the ESS process named by NAME."
484
(defun get-ess-process (name &optional try-another)
485
"Return the ESS process named by NAME. If TRY-ANOTHER is non-nil,
486
and the process NAME is not running (anymore), try to connect to another if
488
(if (null name) ; should almost never happen at this point
489
(error "No ESS process is associated with this buffer now"))
486
490
(update-ess-process-name-list)
488
(error "No ESS process is associated with this buffer now.")
489
(if (assoc name ess-process-name-list)
491
(error "Process %s is not running." name))))
491
(if (assoc name ess-process-name-list)
494
;; was (error "Process %s is not running" name)
495
(ess-write-to-dribble-buffer
496
(format "get-ess-process: process '%s' not running" name))
497
(if (= 0 (length ess-process-name-list))
499
(ess-write-to-dribble-buffer
500
(format " .. restart proc %s for language %s (buf %s)\n"
501
name ess-language (current-buffer)))
502
(message "trying to (re)start process %s for language %s ..."
504
(ess-start-process-specific ess-language ess-dialect)
505
;; and return the process: "call me again"
506
(get-ess-process name))
508
;; else: there are other running processes
509
(if try-another ; connect to another running process : the first one
510
(let ((other-name (car (elt ess-process-name-list 0))))
511
;; "FIXME": try to find the process name that matches *closest*
512
(message "associating with *other* process '%s'" other-name)
513
(get-ess-process other-name))
517
(format "Process %s is not running, but others are. Switch? " name))
519
(ess-force-buffer-current
520
(concat ess-dialect " process to use: ") t)
521
(get-ess-process ess-current-process-name))
522
(error "Process %s is not running" name))))))
493
525
(defun inferior-ess-wait-for-prompt ()
494
526
"Wait until the ESS process is ready for input."
549
581
(set-buffer (process-buffer (get-ess-process name)))
584
(defun ess-start-process-specific (language dialect)
585
"Start an ESS process typically from a language-specific buffer, using
586
LANGUAGE (and DIALECT)."
587
(let ((cur-buf (current-buffer)))
588
(ess-write-to-dribble-buffer
589
(format " ..start-process-specific: lang:dialect= %s:%s, current-buf=%s\n"
590
language dialect cur-buf))
591
(cond ((string= language "S")
592
(if (string= dialect "R")
596
"ESS process not running, trying to start R, since language = 'S")
598
;; (save-excursion <the above>) fails, but this "works":
599
(switch-to-buffer cur-buf)
602
;; else: ess-language is not S
603
;; FIXME find a better solution than this, at least in some cases:
604
(error "No ESS processes running; not yet implemented to start (%s,%s)"
605
language dialect)))))
552
607
(defun ess-request-a-process (message &optional noswitch ask-if-1)
553
608
"Ask for a process, and make it the current ESS process.
554
609
If there is exactly one process, only ask if ASK-IF-1 is non-nil.
559
614
(list "Switch to which ESS process? " current-prefix-arg))
560
615
; prefix sets 'noswitch
616
(ess-write-to-dribble-buffer "ess-request-a-process: {beginning}\n")
561
617
(update-ess-process-name-list)
562
618
(let ((num-processes (length ess-process-name-list)))
563
619
(if (= 0 num-processes)
564
;; try to start "the appropriate" process or bail out
620
;; try to start "the appropriate" process
566
622
(ess-write-to-dribble-buffer
567
(concat "ess-request-a-process:\n "
623
(concat " ... request-a-process:\n "
569
"major mode is %s; ess-language: %s, ess-dialect: %s"
625
"major mode is %s; ess-language: %s, ess-dialect: %s\n"
570
626
major-mode ; 'ess-mode; how can we guess R?
571
627
ess-language ess-dialect)))
572
(if (string= ess-language "S")
573
(if (string= ess-dialect "R")
577
"No ESS process running, trying to start R, since ess-language = 'S")
579
;; else: ess-language is not S
580
;; FIXME find a better solution than this, at least in some cases:
581
(error "No ESS processes running.")
628
(ess-start-process-specific ess-language ess-dialect)
629
(ess-write-to-dribble-buffer
630
(format " ... request-a-process: buf=%s\n" (current-buffer)))
583
631
(setq num-processes 1)))
585
;; else : num-processes >= 1 :
632
;; now num-processes >= 1 :
587
634
(if (and (not ask-if-1) (= 1 num-processes))
588
635
(let ((rr (car (car ess-process-name-list))))
611
(defun ess-force-buffer-current (prompt &optional force)
658
(defun ess-force-buffer-current (&optional prompt force)
612
659
"Make sure the current buffer is attached to an ESS process.
613
660
If not, or FORCE (prefix argument) is non-nil,
614
661
prompt for a process name with PROMPT.
621
668
;; Make sure the source buffer is attached to a process
622
669
(if (and ess-local-process-name (not force))
623
(error "Process %s has died." ess-local-process-name)
670
(error "Process %s has died" ess-local-process-name)
624
671
;; ess-local-process-name is nil -- which process to attach to
626
673
(let ((proc (ess-request-a-process prompt 'no-switch))
1206
1256
both SIMPLE-NEXT and EVEN-EMPTY are interpreted as true."
1207
1257
;; From an idea by Rod Ball (rod@marcam.dsir.govt.nz)
1208
1258
(interactive "P\nP"); prefix sets BOTH !
1209
(ess-force-buffer-current "Process to load into: ")
1210
1259
(save-excursion
1260
(ess-force-buffer-current "Process to load into: ")
1212
1262
(let ((end (point)))
1213
1263
(beginning-of-line)
1582
1632
;; SJE 2007-06-28: Emacs 22.1 has a bug in that comint-mode will set
1583
1633
;; this variable to t, when we need it to be nil. The Emacs 22
1584
;; solution to this bug is to use define-dervied-mode to derive
1634
;; solution to this bug is to use define-derived-mode to derive
1585
1635
;; inferior-ess-mode from comint-mode. Not sure if we can go down
1586
1636
;; that route yet. I've used the when condition so that if the var
1587
1637
;; is nil, don't bother setting it -- as setting it will make a new
1876
1926
(ess-force-buffer-current "Process to quit: ")
1877
1927
(ess-make-buffer-current)
1878
1928
(let ((sprocess (get-ess-process ess-current-process-name)))
1879
(if (not sprocess) (error "No ESS process running."))
1929
(if (not sprocess) (error "No ESS process running"))
1880
1930
(when (yes-or-no-p (format "Really quit ESS process %s? " sprocess))
1882
1932
(goto-char (marker-position (process-mark sprocess)))
1897
1947
(sprocess (get-ess-process ess-current-process-name)))
1898
(if (not sprocess) (error "No ESS process running."))
1948
(if (not sprocess) (error "No ESS process running"))
1899
1949
;;Q (setq response (completing-read "Save workspace image? "
1900
1950
;;Q '( ( "yes".1) ("no" . 1) ("cancel" . 1))