Merge from emacs-23
[bpt/emacs.git] / lisp / progmodes / ps-mode.el
index cb75142..b3fe1c9 100644 (file)
@@ -1,12 +1,12 @@
 ;;; ps-mode.el --- PostScript mode for GNU Emacs
 
-;; Copyright (C) 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
+;; Copyright (C) 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
 ;; Free Software Foundation, Inc.
 
 ;; Author:     Peter Kleiweg <p.c.j.kleiweg@rug.nl>
 ;; Maintainer: Peter Kleiweg <p.c.j.kleiweg@rug.nl>
 ;; Created:    20 Aug 1997
-;; Version:    1.1h, 16 Jun 2005
+;; Version:    1.1h
 ;; Keywords:   PostScript, languages
 
 ;; Yoni Rabkin <yoni@rabkins.net> contacted the maintainer of this
@@ -39,6 +39,7 @@
 (defconst ps-mode-version "1.1h, 16 Jun 2005")
 (defconst ps-mode-maintainer-address "Peter Kleiweg <p.c.j.kleiweg@rug.nl>")
 
+(require 'comint)
 (require 'easymenu)
 
 ;; Define core `PostScript' group.
@@ -181,12 +182,7 @@ You won't need to set this option for Ghostscript."
 
 (defcustom ps-run-tmp-dir nil
   "*Name of directory to place temporary file.
-
-If nil, the following are tried in turn, until success:
-  1. \"$TEMP\"
-  2. \"$TMP\"
-  3. \"$HOME/tmp\"
-  4. \"/tmp\""
+If nil, use `temporary-file-directory'."
   :group 'PostScript-interaction
   :type '(choice (const nil) directory))
 
@@ -436,12 +432,11 @@ If nil, the following are tried in turn, until success:
 
 (unless ps-run-mode-map
   (setq ps-run-mode-map (make-sparse-keymap))
+  (set-keymap-parent ps-run-mode-map comint-mode-map)
   (define-key ps-run-mode-map "\C-c\C-q" 'ps-run-quit)
   (define-key ps-run-mode-map "\C-c\C-k" 'ps-run-kill)
   (define-key ps-run-mode-map "\C-c\C-e" 'ps-run-goto-error)
-  (define-key ps-run-mode-map [mouse-2] 'ps-run-mouse-goto-error)
-  (define-key ps-run-mode-map "\r" 'ps-run-newline)
-  (define-key ps-run-mode-map [return] 'ps-run-newline))
+  (define-key ps-run-mode-map [mouse-2] 'ps-run-mouse-goto-error))
 
 \f
 ;; Syntax table.
@@ -686,7 +681,7 @@ defines the beginning of a group. These tokens are:  {  [  <<"
     (if (or (not ps-mode-auto-indent)
            (< ps-mode-tab 1)
            (not (re-search-backward "^[ \t]+\\=" nil t)))
-       (delete-backward-char 1)
+       (call-interactively 'delete-backward-char)
       (setq target (ps-mode-target-column))
       (while (> column target)
        (setq target (+ target ps-mode-tab)))
@@ -723,12 +718,9 @@ defines the beginning of a group. These tokens are:  {  [  <<"
   (blink-matching-open))
 
 (defun ps-mode-other-newline ()
-  "Perform newline in `*ps run*' buffer."
+  "Perform newline in `*ps-run*' buffer."
   (interactive)
-  (let ((buf (current-buffer)))
-    (set-buffer "*ps run*")
-    (ps-run-newline)
-    (set-buffer buf)))
+  (ps-run-send-string ""))
 
 \f
 ;; Print PostScript.
@@ -985,11 +977,9 @@ plus the usually uncoded characters inserted on positions 1 through 28."
 \f
 ;; Interactive PostScript interpreter.
 
-(define-derived-mode ps-run-mode fundamental-mode "Interactive PS"
+(define-derived-mode ps-run-mode comint-mode "Interactive PS"
   "Major mode in interactive PostScript window.
-This mode is invoked from `ps-mode' and should not be called directly.
-
-\\{ps-run-mode-map}"
+This mode is invoked from `ps-mode' and should not be called directly."
   (set (make-local-variable 'font-lock-defaults)
        '((ps-run-font-lock-keywords
          ps-run-font-lock-keywords-1
@@ -999,7 +989,7 @@ This mode is invoked from `ps-mode' and should not be called directly.
 
 (defun ps-run-running ()
   "Error if not in `ps-mode' or not running PostScript."
-  (unless (equal major-mode 'ps-mode)
+  (unless (derived-mode-p 'ps-mode)
     (error "This function can only be called from PostScript mode"))
   (unless (equal (process-status "ps-run") 'run)
     (error "No PostScript process running")))
@@ -1019,20 +1009,23 @@ This mode is invoked from `ps-mode' and should not be called directly.
       (setq init-file (ps-run-make-tmp-filename))
       (write-region (concat ps-run-init "\n") 0 init-file)
       (setq init-file (list init-file)))
-    (pop-to-buffer "*ps run*")
+    (pop-to-buffer "*ps-run*")
     (ps-run-mode)
     (when (process-status "ps-run")
       (delete-process "ps-run"))
     (erase-buffer)
     (setq command (append command init-file))
     (insert (mapconcat 'identity command " ") "\n")
-    (apply 'start-process "ps-run" "*ps run*" command)
+    (apply 'make-comint "ps-run" (car command) nil (cdr command))
+    (with-current-buffer "*ps-run*"
+      (use-local-map ps-run-mode-map)
+      (setq comint-prompt-regexp ps-run-prompt))
     (select-window oldwin)))
 
 (defun ps-run-quit ()
   "Quit interactive PostScript."
   (interactive)
-  (ps-run-send-string "quit" t)
+  (ps-run-send-string "quit")
   (ps-run-cleanup))
 
 (defun ps-run-kill ()
@@ -1044,9 +1037,9 @@ This mode is invoked from `ps-mode' and should not be called directly.
 (defun ps-run-clear ()
   "Clear/reset PostScript graphics."
   (interactive)
-  (ps-run-send-string "showpage" t)
+  (ps-run-send-string "showpage")
   (sit-for 1)
-  (ps-run-send-string "" t))
+  (ps-run-send-string ""))
 
 (defun ps-run-buffer ()
   "Send buffer to PostScript interpreter."
@@ -1061,7 +1054,7 @@ This mode is invoked from `ps-mode' and should not be called directly.
   (let ((f (ps-run-make-tmp-filename)))
     (set-marker ps-run-mark begin)
     (write-region begin end f)
-    (ps-run-send-string (format "(%s) run" f) t)))
+    (ps-run-send-string (format "(%s) run" f))))
 
 (defun ps-run-boundingbox ()
   "View BoundingBox."
@@ -1109,39 +1102,23 @@ grestore
 " x1 y1 x2 y1 x2 y2 x1 y2)
      0
      f)
-    (ps-run-send-string (format "(%s) run" f) t)
+    (ps-run-send-string (format "(%s) run" f))
     (set-buffer buf)))
 
-(defun ps-run-send-string (string &optional echo)
+(defun ps-run-send-string (string)
   (let ((oldwin (selected-window)))
-    (pop-to-buffer "*ps run*")
-    (goto-char (point-max))
-    (when echo
-      (insert string "\n"))
-    (set-marker (process-mark (get-process "ps-run")) (point))
-    (process-send-string "ps-run" (concat string "\n"))
+    (pop-to-buffer "*ps-run*")
+    (comint-goto-process-mark)
+    (insert string)
+    (comint-send-input)
     (select-window oldwin)))
 
 (defun ps-run-make-tmp-filename ()
   (unless ps-mode-tmp-file
-    (cond (ps-run-tmp-dir)
-         ((setq ps-run-tmp-dir (getenv "TEMP")))
-         ((setq ps-run-tmp-dir (getenv "TMP")))
-         ((setq ps-run-tmp-dir (getenv "HOME"))
-          (setq
-           ps-run-tmp-dir
-           (concat (file-name-as-directory ps-run-tmp-dir) "tmp"))
-          (unless (file-directory-p ps-run-tmp-dir)
-            (setq ps-run-tmp-dir nil))))
-    (unless ps-run-tmp-dir
-      (setq ps-run-tmp-dir "/tmp"))
     (setq ps-mode-tmp-file
-         (make-temp-file
-          (concat
-           (if ps-run-tmp-dir
-               (file-name-as-directory ps-run-tmp-dir)
-             "")
-           "ps-run-"))))
+         (let ((temporary-file-directory (or ps-run-tmp-dir
+                                             temporary-file-directory)))
+           (make-temp-file "ps-run-"))))
   ps-mode-tmp-file)
 
 ;; Remove temporary file
@@ -1159,18 +1136,6 @@ grestore
   (mouse-set-point event)
   (ps-run-goto-error))
 
-(defun ps-run-newline ()
-  "Process newline in PostScript interpreter window."
-  (interactive)
-  (end-of-line)
-  (insert "\n")
-  (forward-line -1)
-  (when (looking-at ps-run-prompt)
-    (goto-char (match-end 0)))
-  (looking-at ".*")
-  (goto-char (1+ (match-end 0)))
-  (ps-run-send-string (buffer-substring (match-beginning 0) (match-end 0))))
-
 (defun ps-run-goto-error ()
   "Jump to buffer position read as integer at point.
 Use line numbers if `ps-run-error-line-numbers' is not nil"