home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / gnu / emacs / sources / 859 < prev    next >
Encoding:
Text File  |  1992-12-14  |  20.3 KB  |  530 lines

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