home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / unix / emacs / lisp / shell.el < prev    next >
Encoding:
Text File  |  1992-07-22  |  17.1 KB  |  467 lines

  1. ;; changed for OS/2 by Eberhard Mattes -- May 1992
  2. ;; Run subshell under Emacs
  3. ;; Copyright (C) 1985, 1986, 1987, 1988 Free Software Foundation, Inc.
  4.  
  5. ;; This file is part of GNU Emacs.
  6.  
  7. ;; GNU Emacs is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation; either version 1, or (at your option)
  10. ;; any later version.
  11.  
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ;; GNU General Public License for more details.
  16.  
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  19. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.  
  22. (provide 'shell)
  23.  
  24. (defvar last-input-start nil
  25.   "In a shell-mode buffer, marker for start of last unit of input.")
  26. (defvar last-input-end nil
  27.   "In a shell-mode buffer, marker for end of last unit of input.")
  28.  
  29. (defvar shell-mode-map nil)
  30.  
  31. (defvar shell-directory-stack nil
  32.   "List of directories saved by pushd in this buffer's shell.")
  33.  
  34. (defvar shell-popd-regexp "popd"
  35.   "*Regexp to match subshell commands equivalent to popd.")
  36.  
  37. (defvar shell-pushd-regexp "pushd"
  38.   "*Regexp to match subshell commands equivalent to pushd.")
  39.  
  40. (defvar shell-cd-regexp "[Cc][Dd]\\|[Cc][Hh][Dd][Ii][Rr]"
  41.   "*Regexp to match subshell commands equivalent to cd.")
  42.  
  43. (defvar explicit-shell-file-name nil
  44.   "*If non-nil, is file name to use for explicitly requested inferior shell.")
  45.  
  46. ;In loaddefs.el now.
  47. ;(defconst shell-prompt-pattern
  48. ;  "^[^#$%>]*[#$%>] *"
  49. ;  "*Regexp used by Newline command to match subshell prompts.
  50. ;Anything from beginning of line up to the end of what this pattern matches
  51. ;is deemed to be prompt, and is not reexecuted.")
  52.  
  53. (defun shell-mode ()
  54.   "Major mode for interacting with an inferior shell.
  55. Shell name is same as buffer name, sans the asterisks.
  56. Return at end of buffer sends line as input.
  57. Return not at end copies rest of line to end and sends it.
  58.  
  59. The following commands imitate the usual Unix interrupt and
  60. editing control characters:
  61. \\{shell-mode-map}
  62.  
  63. Entry to this mode calls the value of shell-mode-hook with no args,
  64. if that value is non-nil.
  65.  
  66. cd, pushd and popd commands given to the shell are watched
  67. by Emacs to keep this buffer's default directory
  68. the same as the shell's working directory.
  69. Variables shell-cd-regexp, shell-pushd-regexp and shell-popd-regexp
  70. are used to match these command names.
  71.  
  72. You can send text to the shell (or its subjobs) from other buffers
  73. using the commands process-send-region, process-send-string
  74. and lisp-send-defun."
  75.   (interactive)
  76.   (kill-all-local-variables)
  77.   (setq major-mode 'shell-mode)
  78.   (setq mode-name "Shell")
  79.   (setq mode-line-process '(": %s"))
  80.   (use-local-map shell-mode-map)
  81.   (make-local-variable 'shell-directory-stack)
  82.   (setq shell-directory-stack nil)
  83.   (make-local-variable 'last-input-start)
  84.   (setq last-input-start (make-marker))
  85.   (make-local-variable 'last-input-end)
  86.   (setq last-input-end (make-marker))
  87.   (run-hooks 'shell-mode-hook))
  88.  
  89. (if shell-mode-map
  90.     nil
  91.   (setq shell-mode-map (make-sparse-keymap))
  92.   (define-key shell-mode-map "\C-m" 'shell-send-input)
  93.   (define-key shell-mode-map "\C-c\C-d" 'shell-send-eof)
  94.   (define-key shell-mode-map "\C-c\C-u" 'kill-shell-input)
  95.   (define-key shell-mode-map "\C-c\C-w" 'backward-kill-word)
  96.   (define-key shell-mode-map "\C-c\C-c" 'interrupt-shell-subjob)
  97.   (define-key shell-mode-map "\C-c\C-z" 'stop-shell-subjob)
  98.   (define-key shell-mode-map "\C-c\C-\\" 'quit-shell-subjob)
  99.   (define-key shell-mode-map "\C-c\C-o" 'kill-output-from-shell)
  100.   (define-key shell-mode-map "\C-c\C-r" 'show-output-from-shell)
  101.   (define-key shell-mode-map "\C-c\C-y" 'copy-last-shell-input))
  102.  
  103. (defvar explicit-csh-args
  104.   (if (eq system-type 'hpux)
  105.       ;; -T persuades HP's csh not to think it is smarter
  106.       ;; than us about what terminal modes to use.
  107.       '("-i" "-T")
  108.     '("-i"))
  109.   "Args passed to inferior shell by M-x shell, if the shell is csh.
  110. Value is a list of strings, which may be nil.")
  111.  
  112. (defun shell ()
  113.   "Run an inferior shell, with I/O through buffer *shell*.
  114. If buffer exists but shell process is not running, make new shell.
  115. Program used comes from variable explicit-shell-file-name,
  116.  or (if that is nil) from the ESHELL environment variable,
  117.  or else from SHELL if there is no ESHELL.
  118. If a file ~/.emacs_SHELLNAME exists, it is given as initial input
  119.  (Note that this may lose due to a timing error if the shell
  120.   discards input when it starts up.)
  121. The buffer is put in shell-mode, giving commands for sending input
  122. and controlling the subjobs of the shell.  See shell-mode.
  123. See also variable shell-prompt-pattern.
  124.  
  125. The shell file name (sans directories) is used to make a symbol name
  126. such as `explicit-csh-arguments'.  If that symbol is a variable,
  127. its value is used as a list of arguments when invoking the shell.
  128. Otherwise, one argument `-i' is passed to the shell.
  129.  
  130. Note that many people's .cshrc files unconditionally clear the prompt.
  131. If yours does, you will probably want to change it."
  132.   (interactive)
  133.   (let* ((prog (or explicit-shell-file-name
  134.            (getenv "ESHELL")
  135.            (getenv "SHELL")
  136.            "cmd"))             
  137.      (name (file-name-nondirectory prog)))
  138.     (switch-to-buffer
  139.      (apply 'make-shell "shell" prog
  140.         (if (file-exists-p (concat "~/.emacs_" name))
  141.         (concat "~/.emacs_" name))
  142.         (let ((symbol (intern-soft (concat "explicit-" name "-args"))))
  143.           (if (and symbol (boundp symbol))
  144.           (symbol-value symbol)
  145.                 '("/q" "/s")))))))
  146.  
  147. (defun make-shell (name program &optional startfile &rest switches)
  148.   (let ((buffer (get-buffer-create (concat "*" name "*")))
  149.     proc status size)
  150.     (setq proc (get-buffer-process buffer))
  151.     (if proc (setq status (process-status proc)))
  152.     (save-excursion
  153.       (set-buffer buffer)
  154.       ;;    (setq size (buffer-size))
  155.       (if (memq status '(run stop))
  156.       nil
  157.     (if proc (delete-process proc))
  158.     (setq proc (apply 'start-process name buffer
  159.               (concat exec-directory "env")
  160.             ;;(format "TERMCAP=emacs:co#%d:tc=unknown:"
  161.             ;;      (screen-width))
  162.             ;;"TERM=emacs"
  163.             ;;"EMACS=t"
  164.               (format "TERMCAP=/emx/etc/termcap.dat"
  165.                   (screen-width))
  166.               "TERM=mono"
  167.               "EMACS=t"
  168.                           "PROMPT=[$p]"
  169.                           "-"
  170.               (or program explicit-shell-file-name
  171.                   (getenv "ESHELL")
  172.                   (getenv "SHELL")
  173.                   "cmd")
  174.               switches))
  175.     (cond (startfile
  176.            ;;This is guaranteed to wait long enough
  177.            ;;but has bad results if the shell does not prompt at all
  178.            ;;         (while (= size (buffer-size))
  179.            ;;           (sleep-for 1))
  180.            ;;I hope 1 second is enough!
  181.            (sleep-for 1)
  182.            (goto-char (point-max))
  183.            (insert-file-contents startfile)
  184.            (setq startfile (buffer-substring (point) (point-max)))
  185.            (delete-region (point) (point-max))
  186.            (process-send-string proc startfile)))
  187.     (setq name (process-name proc)))
  188.       (goto-char (point-max))
  189.       (set-marker (process-mark proc) (point))
  190.       (shell-mode))
  191.     buffer))
  192.  
  193. (defvar shell-set-directory-error-hook 'ignore
  194.   "Function called with no arguments when shell-send-input
  195. recognizes a change-directory command but gets an error
  196. trying to change Emacs's default directory.")
  197.  
  198. (defun shell-send-input ()
  199.   "Send input to subshell.
  200. At end of buffer, sends all text after last output
  201.  as input to the subshell, including a newline inserted at the end.
  202. When not at end, copies current line to the end of the buffer and sends it,
  203. after first attempting to discard any prompt at the beginning of the line
  204. by matching the regexp that is the value of shell-prompt-pattern if possible.
  205. This regexp should start with \"^\"."
  206.   (interactive)
  207.   (or (get-buffer-process (current-buffer))
  208.       (error "Current buffer has no process"))
  209.   (end-of-line)
  210.   (if (eobp)
  211.       (progn
  212.     (move-marker last-input-start
  213.              (process-mark (get-buffer-process (current-buffer))))
  214.     (insert ?\n)
  215.     (move-marker last-input-end (point)))
  216.     (beginning-of-line)
  217.     ;; Exclude the shell prompt, if any.
  218.     (re-search-forward shell-prompt-pattern
  219.                (save-excursion (end-of-line) (point))
  220.                t)
  221.     (let ((copy (buffer-substring (point)
  222.                   (progn (forward-line 1) (point)))))
  223.       (goto-char (point-max))
  224.       (move-marker last-input-start (point))
  225.       (insert copy)
  226.       (move-marker last-input-end (point))))
  227.   ;; Even if we get an error trying to hack the working directory,
  228.   ;; still send the input to the subshell.
  229.   (condition-case ()
  230.       (save-excursion
  231.     (goto-char last-input-start)
  232.     (shell-set-directory))
  233.     (error (funcall shell-set-directory-error-hook)))
  234.   (let ((process (get-buffer-process (current-buffer))))
  235.     (process-send-region process last-input-start last-input-end)
  236.     (set-marker (process-mark process) (point))))
  237.  
  238. ;;;  If this code changes (shell-send-input and shell-set-directory),
  239. ;;;  the customization tutorial in
  240. ;;;  info/customizing-tutorial must also change, since it explains this
  241. ;;;  code.  Please let marick@gswd-vms.arpa know of any changes you
  242. ;;;  make. 
  243.  
  244. (defun shell-set-directory ()
  245.   (cond ((and (looking-at shell-popd-regexp)
  246.           (memq (char-after (match-end 0)) '(?\& ?\n)))
  247.      (if shell-directory-stack
  248.          (progn
  249.            (cd (car shell-directory-stack))
  250.            (setq shell-directory-stack (cdr shell-directory-stack)))))
  251.     ((looking-at shell-pushd-regexp)
  252.      (cond ((memq (char-after (match-end 0)) '(?\& ?\n))
  253.         (if shell-directory-stack
  254.             (let ((old default-directory))
  255.               (cd (car shell-directory-stack))
  256.               (setq shell-directory-stack
  257.                 (cons old (cdr shell-directory-stack))))))
  258.            ((memq (char-after (match-end 0)) '(?\  ?\t))
  259.         (let (dir)
  260.           (skip-chars-forward "^ ")
  261.           (skip-chars-forward " \t")
  262.           (if (file-directory-p
  263.             (setq dir
  264.                   (expand-file-name
  265.                 (substitute-in-file-name
  266.                   (buffer-substring
  267.                     (point)
  268.                     (progn
  269.                       (skip-chars-forward "^\n \t&")
  270.                       (point)))))))
  271.               (progn
  272.             (setq shell-directory-stack
  273.                   (cons default-directory shell-directory-stack))
  274.             (cd dir)))))))
  275.     ((looking-at shell-cd-regexp)
  276.      (cond ((memq (char-after (match-end 0)) '(?\  ?\t ?\\ ?/))
  277.         (let (dir)
  278.                   (goto-char (match-end 0))
  279.           (skip-chars-forward " \t")
  280.           (if (and
  281.                        (file-directory-p
  282.             (setq dir
  283.                   (expand-file-name
  284.                                (substitute-in-file-name
  285.                                 (buffer-substring
  286.                                  (point)
  287.                                  (progn
  288.                                    (skip-chars-forward "^\n \t&")
  289.                                    (point)))))))
  290.                        (string= (substring dir 0 2)
  291.                                 (substring default-directory 0 2)))
  292.               (cd dir))))))
  293.     ((looking-at "[A-Za-z]:[\n &]")
  294.          (progn
  295.            (goto-char (match-beginning 0))
  296.            (if (file-directory-p
  297.                 (setq dir
  298.                       (expand-file-name
  299.                        (buffer-substring
  300.                         (point) (+ (point) 2)))))
  301.                (cd dir))))))
  302.   
  303. (defun shell-send-eof ()
  304.   "Send eof to subshell (or to the program running under it)."
  305.   (interactive)
  306.   (process-send-eof))
  307.  
  308. (defun kill-output-from-shell ()
  309.   "Kill all output from shell since last input."
  310.   (interactive)
  311.   (goto-char (point-max))
  312.   (beginning-of-line)
  313.   (kill-region last-input-end (point))
  314.   (insert "*** output flushed ***\n")
  315.   (goto-char (point-max)))
  316.  
  317. (defun show-output-from-shell ()
  318.   "Display start of this batch of shell output at top of window.
  319. Also put cursor there."
  320.   (interactive)
  321.   (set-window-start (selected-window) last-input-end)
  322.   (goto-char last-input-end))
  323.  
  324. (defun copy-last-shell-input ()
  325.   "Copy previous shell input, sans newline, and insert before point."
  326.   (interactive)
  327.   (insert (buffer-substring last-input-end last-input-start))
  328.   (delete-char -1))
  329.  
  330. (defun interrupt-shell-subjob ()
  331.   "Interrupt this shell's current subjob."
  332.   (interactive)
  333.   (interrupt-process nil t))
  334.  
  335. (defun kill-shell-subjob ()
  336.   "Send kill signal to this shell's current subjob."
  337.   (interactive)
  338.   (kill-process nil t))
  339.  
  340. (defun quit-shell-subjob ()
  341.   "Send quit signal to this shell's current subjob."
  342.   (interactive)
  343.   (quit-process nil t))
  344.  
  345. (defun stop-shell-subjob ()
  346.   "Stop this shell's current subjob."
  347.   (interactive)
  348.   (stop-process nil t))
  349.  
  350. (defun kill-shell-input ()
  351.   "Kill all text since last stuff output by the shell or its subjobs."
  352.   (interactive)
  353.   (kill-region (process-mark (get-buffer-process (current-buffer)))
  354.            (point)))
  355.  
  356. (defvar inferior-lisp-mode-map nil)
  357. (if inferior-lisp-mode-map
  358.     nil
  359.   (setq inferior-lisp-mode-map (copy-alist shell-mode-map))
  360.   (lisp-mode-commands inferior-lisp-mode-map)
  361.   (define-key inferior-lisp-mode-map "\e\C-x" 'lisp-send-defun))
  362.  
  363. (defvar inferior-lisp-program "lisp"
  364.   "*Program name for invoking an inferior Lisp with `run-lisp'.")
  365.  
  366. (defvar inferior-lisp-load-command "(load \"%s\")\n"
  367.   "*Format-string for building a Lisp expression to load a file.
  368. This format string should use %s to substitute a file name
  369. and should result in a Lisp expression that will command the inferior Lisp
  370. to load that file.  The default works acceptably on most Lisps.
  371. The string \"(progn (load \\\"%s\\\" :verbose nil :print t) (values))\\\n\"
  372. produces cosmetically superior output for this application,
  373. but it works only in Common Lisp.")
  374.  
  375. (defvar inferior-lisp-prompt "^.*>:? *$"
  376.   "*Regexp to recognize prompts from the inferior Lisp.
  377. Default is right for Franz Lisp and kcl.")
  378.  
  379. (defun inferior-lisp-mode ()
  380.   "Major mode for interacting with an inferior Lisp process.
  381. Runs a Lisp interpreter as a subprocess of Emacs, with Lisp I/O
  382. through an Emacs buffer.  Variable inferior-lisp-program controls
  383. which Lisp interpreter is run.  Variables inferior-lisp-prompt
  384. and inferior-lisp-load-command can customize this mode for different
  385. Lisp interpreters.
  386.  
  387. Commands:
  388. DELETE converts tabs to spaces as it moves back.
  389. TAB indents for Lisp; with argument, shifts rest
  390.  of expression rigidly with the current line.
  391. Meta-Control-Q does TAB on each line starting within following expression.
  392. Paragraphs are separated only by blank lines.  Semicolons start comments.
  393.  
  394. Return at end of buffer sends line as input.
  395. Return not at end copies rest of line to end and sends it.
  396.  
  397. The following commands imitate the usual Unix interrupt and
  398. editing control characters:
  399. \\{shell-mode-map}
  400.  
  401. Entry to this mode calls the value of lisp-mode-hook with no arguments,
  402. if that value is non-nil.  Likewise with the value of shell-mode-hook.
  403. lisp-mode-hook is called after shell-mode-hook.
  404.  
  405. You can send text to the inferior Lisp from other buffers
  406. using the commands process-send-region, process-send-string
  407. and \\[lisp-send-defun]."
  408.   (interactive)
  409.   (kill-all-local-variables)
  410.   (setq major-mode 'inferior-lisp-mode)
  411.   (setq mode-name "Inferior Lisp")
  412.   (setq mode-line-process '(": %s"))
  413.   (lisp-mode-variables t)
  414.   (use-local-map inferior-lisp-mode-map)
  415.   (make-local-variable 'last-input-start)
  416.   (setq last-input-start (make-marker))
  417.   (make-local-variable 'last-input-end)
  418.   (setq last-input-end (make-marker))
  419.   (run-hooks 'shell-mode-hook 'lisp-mode-hook))
  420.  
  421. (defun run-lisp ()
  422.   "Run an inferior Lisp process, input and output via buffer *lisp*."
  423.   (interactive)
  424.   (switch-to-buffer (make-shell "lisp" inferior-lisp-program))
  425.   (inferior-lisp-mode))
  426.  
  427. (defun lisp-send-defun (display-flag)
  428.   "Send the current defun to the Lisp process made by M-x run-lisp.
  429. With argument, force redisplay and scrolling of the *lisp* buffer.
  430. Variable `inferior-lisp-load-command' controls formatting of
  431. the `load' form that is set to the Lisp process."
  432.   (interactive "P")
  433.   (or (get-process "lisp")
  434.       (error "No current lisp process"))
  435.   (save-excursion
  436.    (end-of-defun)
  437.    (let ((end (point))
  438.      (filename (format "/tmp/emlisp%d" (process-id (get-process "lisp")))))
  439.      (beginning-of-defun)
  440.      (write-region (point) end filename nil 'nomessage)
  441.      (process-send-string "lisp" (format inferior-lisp-load-command filename)))
  442.    (if display-flag
  443.        (let* ((process (get-process "lisp"))
  444.           (buffer (process-buffer process))
  445.           (w (or (get-buffer-window buffer) (display-buffer buffer)))
  446.           (height (window-height w))
  447.           (end))
  448.      (save-excursion
  449.        (set-buffer buffer)
  450.        (setq end (point-max))
  451.        (while (progn
  452.             (accept-process-output process)
  453.             (goto-char (point-max))
  454.             (beginning-of-line)
  455.             (or (= (point-max) end)
  456.             (not (looking-at inferior-lisp-prompt)))))
  457.        (setq end (point-max))
  458.        (vertical-motion (- 4 height))
  459.        (set-window-start w (point)))
  460.      (set-window-point w end)))))
  461.  
  462. (defun lisp-send-defun-and-go ()
  463.   "Send the current defun to the inferior Lisp, and switch to *lisp* buffer."
  464.   (interactive)
  465.   (lisp-send-defun nil)
  466.   (switch-to-buffer "*lisp*"))
  467.