diff --git a/PHP/Repl.php b/PHP/Repl.php index fe5faaf..9af9f62 100755 --- a/PHP/Repl.php +++ b/PHP/Repl.php @@ -124,13 +124,13 @@ public function run(array $scope = array()) while (true) { // inner loop is to escape from stacked output buffers while ($__ob__ = ob_get_clean() ) { - echo $__ob__; + echo $this->ob_cleanup($__ob__); unset($__ob__); } try { if (((boolean) $__code__ = $this->read()) === false) { - break; + continue; } ob_start(array($this, 'ob_cleanup')); ob_implicit_flush(true); @@ -157,6 +157,7 @@ private function read() $code = ''; $done = false; $lines = 0; + $stack = array(); static $shifted; if (!$shifted) { // throw away argv[0] @@ -179,13 +180,45 @@ private function read() return false; } + $done = true; $line = trim($line); // If the last char is a backslash, remove it and // accumulate more lines. if (substr($line, -1) == '\\') { - $line = substr($line, 0, strlen($line) - 1); - } else { - $done = true; + $line = trim(substr($line, 0, strlen($line) - 1)); + $done = false; + } + + // check for curleys and parens, keep accumulating lines. + $tokens = token_get_all(" 0) { + $last_t = array_pop($tokens); + if (is_array($last_t) && $last_t[0] == T_OPEN_TAG) { + // if the last token was an open tag, this is nothing. + } else if ($stack[count($stack) - 1] === '{' && !in_array($last_t, array('{', '}', ';'))) { + // allow implied semicolons inside curlies + $line .= ';'; + } + $done = false; } $code .= $line; $lines++; diff --git a/data/php-repl.el b/data/php-repl.el index 08d6ad4..3939d2f 100644 --- a/data/php-repl.el +++ b/data/php-repl.el @@ -43,7 +43,7 @@ (defgroup php-repl nil "Major mode for interacting with an inferior PHP interpreter." - :prefix "ph-repl-" + :prefix "php-repl-" :group 'php) (defcustom php-repl-program @@ -70,19 +70,27 @@ (interactive "p") (let ((buf (cond ((buffer-live-p inferior-php-buffer) inferior-php-buffer) (t (generate-new-buffer "*inferior-php*"))))) - (make-comint-in-buffer - "PHP" buf php-repl-program nil - (mapconcat 'identity php-repl-program-arguments " ")) + (apply 'make-comint-in-buffer "PHP" buf php-repl-program nil php-repl-program-arguments) (setq inferior-php-buffer buf) - (display-buffer buf t) - ;; (pop-to-buffer buf t) + (set-process-sentinel + (get-buffer-process inferior-php-buffer) 'run-php-process-sentinel) + (unless (eq (current-buffer) inferior-php-buffer) + (display-buffer buf t) + (pop-to-buffer buf t)) (inferior-php-mode))) +(defun run-php-process-sentinel (process event) + "*Restart PHP process after abnormal exit." + (when (string-match-p "^exited abnormally with code" event) + (message "Restarting PHP process") + (run-php))) + (define-derived-mode inferior-php-mode comint-mode "Inferior PHP") (defvar inferior-php-mode-abbrev-table (make-abbrev-table)) -(derived-mode-merge-abbrev-tables php-mode-abbrev-table - inferior-php-mode-abbrev-table) +(if (boundp 'php-mode-abbrev-table) + (derived-mode-merge-abbrev-tables php-mode-abbrev-table + inferior-php-mode-abbrev-table)) (derived-mode-set-abbrev-table 'inferior-php-mode) (defvar eval-php-mode-map @@ -112,7 +120,7 @@ (save-excursion (comint-send-region inferior-php-buffer start end) (if (not (string-match "\n$" (buffer-substring start end))) - (comint-send-string sql-buffer "\n")) + (comint-send-string inferior-php-buffer "\n")) (display-buffer inferior-php-buffer)))) (defun php-eval-sexp ()) diff --git a/scripts/php-repl b/scripts/php-repl index 909effc..8ef4d6a 100755 --- a/scripts/php-repl +++ b/scripts/php-repl @@ -16,6 +16,12 @@ * @filesource */ +// Add environment variable PHP_INCLUDE_PATH to include_path if it exists. +$includePath = getenv('PHP_INCLUDE_PATH'); +if ($includePath !== false) { + set_include_path(get_include_path().':'.getenv('PHP_INCLUDE_PATH')); +} + require_once 'PHP/Repl.php'; $__repl__ = new PHP_Repl(); $__repl__->run();