Initial Commit
[packages] / xemacs-packages / ess / lisp / msdos.el
1 ;;; msdos.el --- Run an MS-DOS shell in an NTemacs buffer with bash as the shell
2
3 ;; Copyright (C) 1999 Richard M. Heiberger <rmh@fisher.stat.temple.edu>
4 ;; Copyright (C) 2000--2004 A.J. Rossini, Rich M. Heiberger, Martin
5 ;;      Maechler, Kurt Hornik, Rodney Sparapani, and Stephen Eglen.
6
7 ;; Original Author: Richard M. Heiberger <rmh@fisher.stat.temple.edu>
8 ;; Created: February 1999
9 ;; Maintainers: ESS-core <ESS-core@stat.math.ethz.ch>
10
11 ;; Keywords: processes
12
13 ;; This file is part of ESS.
14
15 ;; This file is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
19
20 ;; This file is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING.  If not, write to
27 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28
29 ;;; Comments on file:
30
31 ;; The file msdos.el in the next mail message opens an *msdos* buffer
32 ;; in shell-mode and msdos-minor-mode.  When cmdproxy.exe/command.com
33 ;; is the emacs shell, then this gets various setting right that M-x
34 ;; shell currently misses.  The function M-x msdos-minor-mode could be
35 ;; automatically run by emacs in shell-mode in that case.
36
37 ;; When bash is the emacs shell, msdos.el still opens a
38 ;; cmdproxy.exe/command.com shell in the buffer *msdos*.  There are
39 ;; occasions when it is necessary to run DOS character-based programs
40 ;; in an emacs window.
41
42 ;; I followed the suggestion by AndrewI to look at M-x shell and modify
43 ;; it.  It turns out not to have been trivial.
44
45 ;; I've listed it as part of ESS (emacs speaks statistics) for now.  I
46 ;; will be happy to sign it over to FSF and have it become part of the
47 ;; standard distribution for windows machines.
48
49 ;;; Code:
50
51 (require 'shell)
52
53 ;;; Customization and Buffer Variables
54
55 (defcustom explicit-msdos-shell-file-name "cmdproxy.exe"
56   "*If non-nil, is file name to use for explicitly requested msdos
57 inferior shell."
58   :type '(choice (const :tag "None" nil) file)
59   :group 'shell)
60
61 (defcustom explicit-msdos-comspec-file-name
62   (if (w32-using-nt)
63       "cmd.exe"
64     "command.com")
65   "*If non-nil, is file name to use for explicitly requested COMSPEC
66 environment variable."
67   :type '(choice (const :tag "None" nil) file)
68   :group 'shell)
69
70 (defun msdos ()
71   "Run an inferior msdos shell, with I/O through buffer *msdos*.
72 This function is intended to be used in an Ntemacs session in which
73 bash is the primary shell.  But sometimes an MSDOS window, within emacs,
74 is also needed.
75
76 If buffer exists but shell process is not running, make new shell.
77 If buffer exists and shell process is running, just switch to buffer `*msdos*'.
78 Program used comes from variable `explicit-msdos-shell-file-name'.
79 If a file `~/.emacs_SHELLNAME' exists, it is given as initial input
80  (Note that this may lose due to a timing error if the shell
81   discards input when it starts up.)
82 The buffer is put in Shell mode, giving commands for sending input
83 and controlling the subjobs of the shell.  See `shell-mode'.
84 See also the variable `shell-prompt-pattern'.
85
86 The buffer is put into \\[msdos-minor-mode].  See `msdos-minor-mode'.
87
88 The COMSPEC environment variable in the inferior shell, but not in the emacs
89 process, is set to `explicit-msdos-comspec-file-name'.
90 The SHELL environment variable in the inferior shell, but not in the emacs
91 process, is set to `explicit-msdos-shell-file-name'.
92
93 The shell file name (sans directories) is used to make a symbol name
94 such as `explicit-csh-args'.  If that symbol is a variable,
95 its value is used as a list of arguments when invoking the shell.
96
97 \(Type \\[describe-mode] in the shell buffer for a list of commands.)"
98   (interactive)
99   (if (not (comint-check-proc "*msdos*"))
100       (let* ((prog explicit-msdos-shell-file-name)
101              (name (file-name-nondirectory prog))
102              (startfile (concat "~/.emacs_" name))
103              (xargs-name (intern-soft (concat "explicit-" name "-args")))
104              shell-buffer
105              (comspec (getenv "COMSPEC"))
106              (shell (getenv "SHELL"))
107              )
108         (save-excursion
109           (setenv "COMSPEC" explicit-msdos-comspec-file-name)
110           (setenv "SHELL" explicit-msdos-shell-file-name)
111           (set-buffer (apply 'make-comint "msdos" prog
112                              (if (and xargs-name (boundp xargs-name))
113                                  (symbol-value xargs-name))
114                              (if (file-exists-p startfile)
115                                  (concat "/k " startfile))))
116           (setenv "COMSPEC" comspec)
117           (setenv "SHELL" shell)
118           (setq shell-buffer (current-buffer))
119           (shell-mode)
120           (msdos-minor-mode)
121           (sleep-for 4) ; need to wait, else working too fast!
122                       ;;; The `exit' warning should precede the "c:\" prompt.
123                       ;;; If not, then increase the sleep-for time!
124           (goto-char (point-min))
125           (insert
126            "Remember to exit this buffer with `exit'.  If you kill the
127 buffer without exiting, you may not be able to shut down Windows cleanly.")
128           (goto-char (point-max)))
129         (pop-to-buffer shell-buffer))
130     (pop-to-buffer "*msdos*")))
131
132
133 (defun msdos-minor-mode ()
134   "Minor mode for running msdos in a shell-mode buffer:
135 a. Uses \\[set-buffer-process-coding-system] to set the coding system
136 to `'raw-text-dos'.  The DOS C-m C-l end-of-line is critical.  The
137 shell freezes without it.
138
139 b. The variable `comint-completion-addsuffix' is set to `\\' for directories.
140
141 c. Prevents echoing of commands.
142
143 d. strips ctrl-m from output.
144 "
145   (interactive)
146   (setq msdos-minor-mode t)
147   (make-variable-buffer-local 'comint-completion-addsuffix)
148   (setq comint-completion-addsuffix '("\\" . " "))
149   (setq comint-process-echoes t)
150   (add-hook 'comint-output-filter-functions 'shell-strip-ctrl-m nil t)
151   (set-buffer-process-coding-system 'raw-text-dos 'raw-text-dos)
152   ;; buffer-process-coding-system is critical.
153   )
154
155 ;; Install ourselves:
156
157 (make-variable-buffer-local 'msdos-minor-mode)
158 (defvar msdos-minor-mode nil
159   "Non-nil if using msdos-minor mode as a minor mode of some other mode.")
160
161 (put 'msdos-minor-mode 'permanent-local t)
162 (or (assq 'msdos-minor-mode minor-mode-alist)
163     (setq minor-mode-alist
164           (append minor-mode-alist
165                   (list '(msdos-minor-mode " msdos")))))
166
167 ;; Provide ourselves:
168
169 (provide 'msdos)
170
171 ;;; msdos.el ends here