~ubuntu-branches/ubuntu/lucid/ess/lucid

« back to all changes in this revision

Viewing changes to lisp/ess-inf.el

  • Committer: Bazaar Package Importer
  • Author(s): Dirk Eddelbuettel
  • Date: 2008-10-04 18:41:09 UTC
  • mfrom: (1.2.6 upstream) (3.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20081004184109-scx543vld6rcbl9r
Tags: 5.3.9~svn3950-1
New packages based on svn revision 3950 approaching release 5.3.9.

Show diffs side-by-side

added added

removed removed

Lines of Context:
481
481
 
482
482
;;*;; General process handling code
483
483
 
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
 
487
there is one."
 
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)
487
 
  (if (null name)
488
 
      (error "No ESS process is associated with this buffer now.")
489
 
    (if (assoc name ess-process-name-list)
490
 
        (get-process name)
491
 
      (error "Process %s is not running." name))))
 
491
  (if (assoc name ess-process-name-list)
 
492
      (get-process name)
 
493
    ;; else :
 
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))
 
498
        (save-current-buffer
 
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 ..."
 
503
                   name ess-language)
 
504
          (ess-start-process-specific ess-language ess-dialect)
 
505
          ;; and return the process: "call me again"
 
506
          (get-ess-process name))
 
507
 
 
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))
 
514
        ;; else
 
515
        (ding)
 
516
        (if (yes-or-no-p
 
517
             (format "Process %s is not running, but others are. Switch? " name))
 
518
            (progn
 
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))))))
 
523
 
492
524
 
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)))
550
582
    (set var val)))
551
583
 
 
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")
 
593
               (R)
 
594
             ;; else S, but not R
 
595
             (message
 
596
              "ESS process not running, trying to start R, since language = 'S")
 
597
             (R))
 
598
           ;; (save-excursion <the above>) fails, but this "works":
 
599
           (switch-to-buffer cur-buf)
 
600
           )
 
601
          (t
 
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)))))
 
606
 
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.
558
613
  (interactive
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
565
621
        (progn
566
622
          (ess-write-to-dribble-buffer
567
 
           (concat "ess-request-a-process:\n  "
 
623
           (concat " ... request-a-process:\n  "
568
624
                   (format
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")
574
 
                  (R)
575
 
                ;; else S, but not R
576
 
                (message
577
 
                 "No ESS process running, trying to start R, since ess-language = 'S")
578
 
                (R))
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.")
582
 
            )
 
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)))
584
 
 
585
 
    ;; else : num-processes >= 1 :
 
632
    ;; now num-processes >= 1 :
586
633
    (let ((proc
587
634
           (if (and (not ask-if-1) (= 1 num-processes))
588
635
               (let ((rr (car (car ess-process-name-list))))
608
655
      proc)))
609
656
 
610
657
 
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.
620
667
      nil ; do nothing
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
625
672
      (save-excursion
626
673
        (let ((proc (ess-request-a-process prompt 'no-switch))
843
890
           do-sleep end-of-output
844
891
           oldpb oldpf oldpm
845
892
           )
846
 
      (if sprocess nil
 
893
      (if (null sprocess)
 
894
        ;; should hardly happen, since (get-ess-process *)  already checked:
847
895
        (error "Process %s is not running!" ess-current-process-name))
848
896
      (setq sbuffer (process-buffer sprocess))
849
897
      (save-excursion
1095
1143
            (process-send-string sprocess "\n"))))))
1096
1144
 
1097
1145
  (message "Finished evaluation")
 
1146
  (if ess-eval-deactivate-mark
 
1147
      (deactivate-mark))
1098
1148
  ;; return value
1099
1149
  (list start end))
1100
1150
 
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: ")
1211
1261
    (end-of-line)
1212
1262
    (let ((end (point)))
1213
1263
      (beginning-of-line)
1581
1631
 
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))
1881
1931
        (ess-cleanup)
1882
1932
        (goto-char (marker-position (process-mark sprocess)))
1895
1945
  (let (cmd
1896
1946
;;Q     response
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))
1901
1951
;;Q                                 nil t))