From d364dee6ba29f72f1ed1e715043e865af5fc98ef Mon Sep 17 00:00:00 2001 From: Stefan Monnier Date: Wed, 6 Sep 2006 17:34:12 +0000 Subject: Remove * in docstrings. (prolog-program-name): Add SWI prolog. (prolog-mode-menu): New menu. (prolog-mode): Set comment-add. (prolog-indent-line): Simplify. Use indent-line-to. (inferior-prolog-buffer): New var. (inferior-prolog-run, inferior-prolog-process): New funs. (run-prolog, switch-to-prolog): Rewrite, using them. (prolog-consult-region): Use inferior-prolog-buffer. (inferior-prolog-load-file): New function. (prolog-mode-map): Add bindings for load-file and switch-to-prolog. --- lisp/progmodes/prolog.el | 125 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 95 insertions(+), 30 deletions(-) (limited to 'lisp/progmodes/prolog.el') diff --git a/lisp/progmodes/prolog.el b/lisp/progmodes/prolog.el index 14b47475eb1..7bb8c3a3f40 100644 --- a/lisp/progmodes/prolog.el +++ b/lisp/progmodes/prolog.el @@ -41,27 +41,27 @@ (defcustom prolog-program-name - (let ((names '("prolog" "gprolog"))) + (let ((names '("prolog" "gprolog" "swipl"))) (while (and names (not (executable-find (car names)))) (setq names (cdr names))) (or (car names) "prolog")) - "*Program name for invoking an inferior Prolog with `run-prolog'." + "Program name for invoking an inferior Prolog with `run-prolog'." :type 'string :group 'prolog) (defcustom prolog-consult-string "reconsult(user).\n" - "*(Re)Consult mode (for C-Prolog and Quintus Prolog). " + "(Re)Consult mode (for C-Prolog and Quintus Prolog). " :type 'string :group 'prolog) (defcustom prolog-compile-string "compile(user).\n" - "*Compile mode (for Quintus Prolog)." + "Compile mode (for Quintus Prolog)." :type 'string :group 'prolog) (defcustom prolog-eof-string "end_of_file.\n" - "*String that represents end of file for Prolog. + "String that represents end of file for Prolog. When nil, send actual operating system end of file." :type 'string :group 'prolog) @@ -121,7 +121,21 @@ When nil, send actual operating system end of file." (defvar prolog-mode-map (let ((map (make-sparse-keymap))) (define-key map "\e\C-x" 'prolog-consult-region) + (define-key map "\C-c\C-l" 'inferior-prolog-load-file) + (define-key map "\C-c\C-z" 'switch-to-prolog) map)) + +(easy-menu-define prolog-mode-menu prolog-mode-map "Menu for Prolog mode." + ;; Mostly copied from scheme-mode's menu. + ;; Not tremendously useful, but it's a start. + '("Prolog" + ["Indent line" indent-according-to-mode t] + ["Indent region" indent-region t] + ["Comment region" comment-region t] + ["Uncomment region" uncomment-region t] + "--" + ["Run interactive Prolog session" run-prolog t] + )) ;;;###autoload (defun prolog-mode () @@ -138,29 +152,24 @@ if that value is non-nil." (setq major-mode 'prolog-mode) (setq mode-name "Prolog") (prolog-mode-variables) + (set (make-local-variable 'comment-add) 1) ;; font lock (setq font-lock-defaults '(prolog-font-lock-keywords nil nil nil beginning-of-line)) (run-mode-hooks 'prolog-mode-hook)) -(defun prolog-indent-line (&optional whole-exp) +(defun prolog-indent-line () "Indent current line as Prolog code. With argument, indent any additional lines of the same clause rigidly along with this one (not yet)." (interactive "p") (let ((indent (prolog-indent-level)) - (pos (- (point-max) (point))) beg) + (pos (- (point-max) (point)))) (beginning-of-line) - (setq beg (point)) - (skip-chars-forward " \t") - (if (zerop (- indent (current-column))) - nil - (delete-region beg (point)) - (indent-to indent)) + (indent-line-to indent) (if (> (- (point-max) pos) (point)) - (goto-char (- (point-max) pos))) - )) + (goto-char (- (point-max) pos))))) (defun prolog-indent-level () "Compute Prolog indentation level." @@ -256,27 +265,73 @@ Return not at end copies rest of line to end and sends it. (setq comint-prompt-regexp "^| [ ?][- ] *") (prolog-mode-variables)) +(defvar inferior-prolog-buffer nil) + +(defun inferior-prolog-run (&optional name) + (with-current-buffer (make-comint "prolog" (or name prolog-program-name)) + (inferior-prolog-mode) + (setq-default inferior-prolog-buffer (current-buffer)) + (make-local-variable 'inferior-prolog-buffer) + (when (and name (not (equal name prolog-program-name))) + (set (make-local-variable 'prolog-program-name) name)) + (set (make-local-variable 'inferior-prolog-flavor) + ;; 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 + 'inferior-prolog-guess-flavor nil t))) + +(defun inferior-prolog-process (&optional dontstart) + (or (and (buffer-live-p inferior-prolog-buffer) + (get-buffer-process inferior-prolog-buffer)) + (unless dontstart + (inferior-prolog-run) + ;; Try again. + (inferior-prolog-process)))) + ;;;###autoload -(defun run-prolog () - "Run an inferior Prolog process, input and output via buffer *prolog*." - (interactive) - (require 'comint) - (pop-to-buffer (make-comint "prolog" prolog-program-name)) - (inferior-prolog-mode)) +(defalias 'run-prolog 'switch-to-prolog) +;;;###autoload +(defun switch-to-prolog (&optional name) + "Run an inferior Prolog process, input and output via buffer *prolog*. +With prefix argument \\[universal-prefix], prompt for the program to use." + (interactive + (list (when current-prefix-arg + (let ((proc (inferior-prolog-process 'dontstart))) + (if proc + (if (yes-or-no-p "Kill current process before starting new one? ") + (kill-process proc) + (error "Abort"))) + (read-string "Run Prolog: " prolog-program-name))))) + (unless (inferior-prolog-process 'dontstart) + (inferior-prolog-run name)) + (pop-to-buffer inferior-prolog-buffer)) (defun prolog-consult-region (compile beg end) "Send the region to the Prolog process made by \"M-x run-prolog\". If COMPILE (prefix arg) is not nil, use compile mode rather than consult mode." (interactive "P\nr") - (save-excursion - (if compile - (process-send-string "prolog" prolog-compile-string) - (process-send-string "prolog" prolog-consult-string)) - (process-send-region "prolog" beg end) - (process-send-string "prolog" "\n") ;May be unnecessary + (let ((proc (inferior-prolog-process))) + (comint-send-string proc + (if compile prolog-compile-string + prolog-consult-string)) + (comint-send-region proc beg end) + (comint-send-string proc "\n") ;May be unnecessary (if prolog-eof-string - (process-send-string "prolog" prolog-eof-string) - (process-send-eof "prolog")))) ;Send eof to prolog process. + (comint-send-string proc prolog-eof-string) + (with-current-buffer (process-buffer proc) + (comint-send-eof))))) ;Send eof to prolog process. (defun prolog-consult-region-and-go (compile beg end) "Send the region to the inferior Prolog, and switch to *prolog* buffer. @@ -285,7 +340,17 @@ If COMPILE (prefix arg) is not nil, use compile mode rather than consult mode." (prolog-consult-region compile beg end) (switch-to-buffer "*prolog*")) +(defun inferior-prolog-load-file () + "Pass the current buffer's file to the inferior prolog process." + (interactive) + (save-buffer) + (let ((file buffer-file-name) + (proc (inferior-prolog-process))) + (with-current-buffer (process-buffer proc) + (comint-send-string proc (concat "['" (file-relative-name file) "'].\n")) + (pop-to-buffer (current-buffer))))) + (provide 'prolog) -;;; arch-tag: f3ec6748-1272-4ab6-8826-c50cb1607636 +;; arch-tag: f3ec6748-1272-4ab6-8826-c50cb1607636 ;;; prolog.el ends here -- cgit v1.2.3 From 23f2d048b5df9fe9bfd39e8e1cd606e3bcd42c7c Mon Sep 17 00:00:00 2001 From: Stefan Monnier Date: Thu, 7 Sep 2006 16:18:06 +0000 Subject: (inferior-prolog-flavor): New var left out of previous commit. (inferior-prolog-guess-flavor): New fun left out of previous commit. (prolog-consult-region-and-go): Don't hard code "*prolog*" and don't burp in dedicated windows. (inferior-prolog-self-insert-command): New command. (inferior-prolog-mode-map): Use it. --- lisp/ChangeLog | 10 ++++++++++ lisp/progmodes/prolog.el | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) (limited to 'lisp/progmodes/prolog.el') diff --git a/lisp/ChangeLog b/lisp/ChangeLog index fde31fe5b21..7e50d886d56 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,13 @@ +2006-09-07 Stefan Monnier + + * progmodes/prolog.el (inferior-prolog-flavor): New var left out of + previous commit. + (inferior-prolog-guess-flavor): New fun left out of previous commit. + (prolog-consult-region-and-go): Don't hard code "*prolog*" and don't + burp in dedicated windows. + (inferior-prolog-self-insert-command): New command. + (inferior-prolog-mode-map): Use it. + 2006-09-07 Reiner Steib * international/latexenc.el (latex-inputenc-coding-alist): Add cp858. diff --git a/lisp/progmodes/prolog.el b/lisp/progmodes/prolog.el index 7bb8c3a3f40..c29a259c3a6 100644 --- a/lisp/progmodes/prolog.el +++ b/lisp/progmodes/prolog.el @@ -233,6 +233,8 @@ rigidly along with this one (not yet)." (let ((map (make-sparse-keymap))) ;; This map will inherit from `comint-mode-map' when entering ;; inferior-prolog-mode. + (define-key map [remap self-insert-command] + 'inferior-prolog-self-insert-command) map)) (defvar inferior-prolog-mode-syntax-table prolog-mode-syntax-table) @@ -300,6 +302,27 @@ Return not at end copies rest of line to end and sends it. ;; Try again. (inferior-prolog-process)))) +(defvar inferior-prolog-flavor 'unknown + "Either a symbol or a buffer position offset by one. +If a buffer position, the flavor has not been determined yet and +it is expected that the process's output has been or will +be inserted at that position plus one.") + +(defun inferior-prolog-guess-flavor (&optional ignored) + (save-excursion + (goto-char (1+ inferior-prolog-flavor)) + (setq inferior-prolog-flavor + (cond + ((looking-at "GNU Prolog") 'gnu) + ((looking-at "Welcome to SWI-Prolog") 'swi) + ((looking-at ".*\n") 'unknown) ;There's at least one line. + (t inferior-prolog-flavor)))) + (when (symbolp inferior-prolog-flavor) + (remove-hook 'comint-output-filter-functions + 'inferior-prolog-guess-flavor t) + (if (eq inferior-prolog-flavor 'gnu) + (set (make-local-variable 'comint-process-echoes) t)))) + ;;;###autoload (defalias 'run-prolog 'switch-to-prolog) ;;;###autoload @@ -318,6 +341,22 @@ With prefix argument \\[universal-prefix], prompt for the program to use." (inferior-prolog-run name)) (pop-to-buffer inferior-prolog-buffer)) +(defun inferior-prolog-self-insert-command () + "Insert the char in the buffer or pass it directly to the process." + (interactive) + (let* ((proc (get-buffer-process (current-buffer))) + (pmark (and proc (marker-position (process-mark proc))))) + (if (and (eq inferior-prolog-flavor 'gnu) + pmark + (null current-prefix-arg) + (eobp) + (eq (point) pmark) + (save-excursion + (goto-char (- pmark 3)) + (looking-at " \\? "))) + (comint-send-string proc (string last-command-char)) + (call-interactively 'self-insert-command)))) + (defun prolog-consult-region (compile beg end) "Send the region to the Prolog process made by \"M-x run-prolog\". If COMPILE (prefix arg) is not nil, use compile mode rather than consult mode." @@ -338,7 +377,7 @@ If COMPILE (prefix arg) is not nil, use compile mode rather than consult mode." If COMPILE (prefix arg) is not nil, use compile mode rather than consult mode." (interactive "P\nr") (prolog-consult-region compile beg end) - (switch-to-buffer "*prolog*")) + (pop-to-buffer inferior-prolog-buffer)) (defun inferior-prolog-load-file () "Pass the current buffer's file to the inferior prolog process." -- cgit v1.2.3