Creating a Cygwin emacs macro - windows

I have been researching how to get Cygwin to work under emacs. I have it working, but now I want to write a macro that will do the following:
Launch by typing M-x cygwin
Have the script stored in some obvious place (probably my .emacs file)
M-x shell
Rename the buffer to cygwin (or cygwin1, cygwin2, cygwin3, ... if cygwin exists) probably using M-x rename-buffer
M-x ansi-color-for-comint-mode-on
M-x set-buffer-process-coding-system 'undecided-unix 'undecided-unix
Open and run cygwin
It will basically do all of the above steps. I think most of the organization for this little project is done. How do I tie it all together so I can just type M-x cygwin and see a happy new cygwin buffer? What exactly needs to be added to .emacs? Also, where exactly is .emacs in Windows?
I still want the ability to run M-x shell for the windows shell for now. I may also make similar macros for MSYS and ssh'ing to my Linux boxes. How do I get started?

Many questions, here are some answers:
Your .emacs can exist many places, it depends, read here. In short try C-x C-f .emacs, or check the value of the variable 'user-init-file (C-h v user-init-file).
I think the command you want is something along the lines of this:
(require 'comint) ; this does require comint
(defun cygwin ()
"do what i want for cygwin"
(interactive)
(let ((buffer (get-buffer-create (generate-new-buffer-name "cygwin"))))
(pop-to-buffer buffer)
(unless (comint-check-proc buffer)
(apply 'make-comint-in-buffer (buffer-name buffer) buffer "c:/cygwin/Cygwin.bat"
nil
nil)
(ansi-color-for-comint-mode-on)
(set-buffer-process-coding-system 'undecided-unix 'undecided-unix))))
Note: I directly ran the process Cygwin.bat instead of running shell and then starting that batch program. I believe the effect is the same, and more straight forward. I did choose the easy way out for naming the buffer (using 'generate-new-buffer-name) - you'll want to customize to what you want.
You can dump the above command in your .emacs easiest by doing the C-x C-f .emacs and pasting it in the buffer that gets opened up. Save it and restart (or do M-x eval-defun when your cursor is in the body of that command. Then M-x cygwin will run the command.

Related

How to start a Stata process in Emacs (with the ESS package) in Windows

In the ESS (Emacs speaks Statistics) manual, it says if I use Cygwin bash shell, I should run M-x S RET.
and if MSDOS prompt shell, run something like M-x S+6-msdos RET.
so what's the proper syntax for starting a Stata process? I tried M-x stata-msdos RET, M-x sta-msdos, and M-x Stata-msdos and failed.
EDIT 1: I'm using Windows 8.1 and MSDOS prompt shell (this is the shell pops up when I use M-x shell)
It seems there is some problem when I use M-x stata RET (might not be the right command for msdos shell), instead of opening a new buffer in Stata mode, Emacs starts Stata in its own window and leaves the old Emacs window irresponsive, which I have to force kill in the end.
EDIT 2: I asked roughly the same question in the ess mailing list and someone mentioned that it seems in Windows Stata Can only be used with its own GUI, not the command line, and that explains why when I M-x stata RET it always opens its own window ( I thought it was because I was using the wrong command). I asked the question because I didn't see the question being asked, now I guess I understand (sadly) why: because it can not be solved..

How to quickly create a IPython shell in Emacs?

When I do python development in emacs, I want an IPython shell available. Here is my current way of accomplishing this:
M-x ansi-term
Which prompts me with Run program: /bin/bash, which I replace with /usr/bin/ipython
It's often the case that I want another terminal in emacs, perhaps to run the app, so I do:
M-x rename-buffer
and change the name of the buffer to ipython so that I can change buffers easily, with having to remember which terminal was the IPython shell.
It seems that there would be a way to create an emacs command that would accomplish all of this with a few keystrokes, but I don't know the right way to begin, being fairly inexperienced with Emacs.
Here's the code:
(defun ipython ()
(interactive)
(ansi-term "/usr/bin/ipython" "ipython"))
Also, smex makes it even faster to start.
Suggest to run IPython from a python-mode.
M-x ipython RET would be sufficient if python-mode.el is loaded.
python.el comes with comments in head how to configure, so M-x run-python RET will open an properly named IPython-shell buffer.

Garbage characters in my Windows Emacs shell. . . not sure what's wrong

I'm running GNU Emacs (23.1.1) on Windows and when I run a cygwin shell inside emacs I get garbage like the following, after each command:
^[]0;/cygdrive/c/emacs-23.1/bin
^[[32user#HOST [33m/cygdrive/c/emacs-23.1/bin[0m
Note: I've replaced my real user and host name with user/HOST.
I've found a few links that describe how to set up emacs with cygwin on Windows including this one on SO:
How can I run Cygwin Bash Shell from within Emacs?
That hasn't helped. As of right now, I don't have anything referencing shells in my .emacs file except for this:
;; For the interactive shell
(setq explicit-shell-file-name "bash")
So I'm not even sure how emacs is finding my cygwin shell. Any help is appreciated. Thanks.
Try adding these lines to your .emacs file
(autoload 'ansi-color-for-comint-mode-on "ansi-color" nil t)
(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
I'm not sure if these are the correct ones but let me know (as my .emacs file has gotten quite large over the years)

Getting Windows 7 SUA's bash shell working with emacs (EmacsW32)?

Having recently purchased Windows 7 Ultimate in order to gain access to the SUA - http://www.suacommunity.com - subsystem, I have been struggling to get SUA's bash utility (/usr/local/bin/bash) working with EmacsW32. SUA comes with ksh and csh by default, so I installed a community bundle to obtain the bash process.
M-x shell normally invokes a shell process and pipes stdio through an Emacs buffer. This works well with Cygwin. I have tried adjusting emacs variables like w32-shell-* to point it at the SUA bash executable, and also tried invoking bash via the posix.exe tool provided with SUA. I often see that the file descriptors associated with the bash process are deleted as soon as the process is created by EmacsW32.
Cygwin is very slow compared to SUA, so I am very keen to get this facility working with the EmacsW32 + SUA combo. Any tips, experience, solutions would be appreciated.
I don't know the w32-shell-* variables. Maybe you could show some code to illustrate what you mean.
I also don't know SUA.
I use GNU emacs v22 on Windows, and I run powershell as an inferior shell. I had some difficulties initially, similar to yours, and solved them with a better understanding of how to start the shell. Maybe this will help you.
I use these variables:
(setq explicit-shell-file-name
"c:\\windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe")
(setq explicit-powershell.exe-args
'("-Command" "-" )) ;; interactive, but no command prompt
I had difficulty until I realized two things:
if you don't specify command-line arguments for the shell process, emacs silently defaults to using -i. In the case of powershell, that argument was either not supported or it did something other than what I wanted (I forget). So I had to explicitly set the arguments to invoke the shell.
the name of the variable for specifying arguments to the shell, depends on the name of the program used to start the shell. This isn't documented as far as I know (unless you consider the source code to be documentation!). If you're on Linux and running sh, then the variable is explicit-sh-args. If bsh, then explicit-bsh-args. If you're on Windows, you need to use the proper name of the exe, including the .exe suffix. It makes for a strange looking variable name, but it works.
The full code to start powershell as an inferior emacs shell is like this:
;; get a name for the buffer
(setq buffer (get-buffer-create "*PowerShell*"))
(let ((tmp-shellfile explicit-shell-file-name))
(setq explicit-shell-file-name
"c:\\windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe")
(setq explicit-powershell.exe-args
'("-Command" "-" )) ; interactive, but no command prompt
;; launch the shell
(shell buffer)
; restore the original shell
(if tmp-shellfile
(setq explicit-shell-file-name tmp-shellfile)))
Maybe something like that would work for you.

Emacs remote shell

I tend to run my shell in emacs, but when I use it to ssh into another computer this breaks tab-completion. Is there a way to fix this?
Try:
M-x cd /hostname:/current/path/in/the/shell
That should set up ange-ftp (or tramp), and then TAB completion for paths should work properly for that shell - until you log into a different machine.
You could set up a comint process filter to recognize when you type ssh to do that for you automatically, but that's difficult to get right as it should revert when you exit the ssh session, but not be tricked by other uses of exit.
For an automated solution, I'd suggest augmenting the approach I personally use to keep Emacs synchronized with the current working directory of the shell buffer. Just add an an extra bit of information with the hostname, and use that to set the hostname and path like shown above.
in another thread, someone mentioned eshell which I've never used but I tried it with SSH and all sorts of nice integration is happening. Time to learn eshell.
You could try M-x ansi-term to host your shell if your getting unexpected behavior with key mappings. Having said that I couldn't re-produce the problem your describing on your set-up.
I had a similar problem I think and solved it by editing my ~/.bash_login on the remove machine and append
export TERM=xterm
I use OS X and had problems when connecting to a Linux (Debian Lenny)
I just wrote a little function to open a shell on a remote host. The cd call before shell gets the tab completion working.
This may be different than you want, since it opens a new shell instead of ssh'ing in a local shell. Beyond that, you could look into hacking emacs Directory Tracking (or see who else has).
(defun remote-shell (&optional host)
"Open a remote shell to a host."
(interactive)
(with-temp-buffer
(let ((host (if host host (read-string "Host: "))))
(cd (concat "/scp:" host ":"))
(shell (concat "*" host "*")))))
(defun myserver-shell () (interactive) (remote-shell "myserver"))
M-x shell invoked in buffer A will switch to a shell buffer B; usually shell. Unsurprisingly it creates B if necessary. A prefix arg will cause it to ask for the name of B. If A is viewing something on a remote host then the shell will be run on the remote host. But only if it can't find an existing B. At that point file name completion will work.
There are some notes about how to tinker with this in the emacs wiki. See for example the function my-shell in this section, which will extend the default name for B so remote files get remote shells. I do that same thing by advising the shell function.
The filename auto completion will work fine. Command autocomplete? Less so. For me it blocks emacs and then doesn't actually work.
I use dired to access the remote machine and open a shell there.
Here is the function I use, taken and modified from Tikhon Jelviss' emacs configuration:
(defun anr-shell (buffer)
"Opens a new shell buffer where the given buffer is located."
(interactive "sBuffer: ")
(pop-to-buffer (concat "*" buffer "*"))
(unless (eq major-mode 'shell-mode)
(dired buffer)
(shell buffer)
(sleep-for 0 200)
(delete-region (point-min) (point-max))
(comint-simple-send (get-buffer-process (current-buffer))
(concat "export PS1=\"\033[33m" buffer "\033[0m:\033[35m\\W\033[0m>\""))))
Example:
(anr-shell "/vagrant#localhost#2222:/vagrant/")

Resources