Function: prolog-ensure-process

prolog-ensure-process is a byte-compiled function defined in prolog.el.gz.

Signature

(prolog-ensure-process &optional WAIT)

Documentation

If Prolog process is not running, run it.

If the optional argument WAIT is non-nil, wait for Prolog prompt specified by the variable prolog-prompt-regexp(var)/prolog-prompt-regexp(fun).

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/prolog.el.gz
(defun prolog-ensure-process (&optional wait)
  "If Prolog process is not running, run it.
If the optional argument WAIT is non-nil, wait for Prolog prompt specified by
the variable `prolog-prompt-regexp'."
  (let ((pname (prolog-program-name))
        (pswitches (prolog-program-switches)))
    (if (null pname)
        (error "This Prolog system has defined no interpreter"))
    (unless (comint-check-proc "*prolog*")
      (with-current-buffer (get-buffer-create "*prolog*")
        ;; The "INFERIOR=yes" hack is for SWI-Prolog 7.2.3 and earlier,
        ;; which assumes it is running under Emacs if either INFERIOR=yes or
        ;; if EMACS is set to a nonempty value.  The EMACS setting is
        ;; obsolescent, so set INFERIOR.  Newer versions of SWI-Prolog should
        ;; know about INSIDE_EMACS (which replaced EMACS) and should not need
        ;; this hack.
        (let ((process-environment
	       (if (getenv "INFERIOR")
		   process-environment
                 (cons "INFERIOR=yes" process-environment))))
	  (apply 'make-comint-in-buffer "prolog" (current-buffer)
                 pname nil pswitches))
        (prolog-inferior-mode)

        (unless prolog-system
          ;; Setup auto-detection.
          (setq-local
           prolog-system
           ;; Force re-detection.
           (let* ((proc (get-buffer-process (current-buffer)))
                  (pmark (and proc (marker-position (process-mark proc)))))
             (cond
              ((null pmark) (1- (point-min)))
              ;; The use of insert-before-markers in comint.el together with
              ;; the potential use of comint-truncate-buffer in the output
              ;; filter, means that it's difficult to reliably keep track of
              ;; the buffer position where the process's output started.
              ;; If possible we use a marker at "start - 1", so that
              ;; insert-before-marker at `start' won't shift it.  And if not,
              ;; we fall back on using a plain integer.
              ((> pmark (point-min)) (copy-marker (1- pmark)))
              (t (1- pmark)))))
          (add-hook 'comint-output-filter-functions
                    'prolog-inferior-guess-flavor nil t))
        (if wait
            (progn
              (goto-char (point-max))
              (while
                  (save-excursion
                    (not
                     (re-search-backward
                      (concat "\\(" (prolog-prompt-regexp) "\\)" "\\=")
                      nil t)))
                (sit-for 0.1))))))))