2

I am trying to make Windows open text files in Emacs, which I have installed under Cygwin. I have followed these instructions: and created a bat file like this:

@echo off
chdir C:\LocalApp\cygwin\bin
start mintty.exe /usr/bin/emacs-X11.exe %1

It does launch Emacs when I double-click on the file name in Windows Explorer, but I have two problems:

  1. Emacs runs in console mode not in window mode (and I have XWin running).

  2. It shows an empty buffer instead of the file content (I suspect this is a path issue, but I could not find a way to insert cygpath in the .bat script and make it work).

Any idea? Thanks.

9
  • Why not just install NT Emacs? NT Emacs Link
    – Techie Joe
    Oct 8, 2013 at 23:40
  • because I don't have admin rights and the administrator won't let me; but I managed to convince him to let me use cygwin. silly I know, but that's the way it is.
    – point618
    Nov 1, 2013 at 11:09
  • You really don't need admin rights to use NT Emacs. They're just files with no install program at all. The only thing you may need admin rights for is a quick registry hack but it's not mandidtory. The readme/setup text files have all the information.
    – Techie Joe
    Nov 1, 2013 at 15:47
  • all right, then I can work around the problem. still I would be curious to know why my solution does not work. thanks.
    – point618
    Nov 5, 2013 at 17:56
  • Can you open the files within Emacs via the manual directory search? If not there's a problem with the program in general or a bug. I haven't used Emacs via Cygwin. All I've done outside of the general usage is rip the unix commands and DLLs out of the install folder and threw them all into the %systemroot%\system32 directory so I can have the commands available via the native Windows command prompt. You might want to ping a Cygwin board or the developers themselves to see how you can get what you want done accomplished.
    – Techie Joe
    Nov 5, 2013 at 18:11

1 Answer 1

1

1) Emacs most likely starts in console mode because no DISPLAY variable is set; set that environment variable, with a value which points to an X server able to accept clients, and you should find better results. You can also pass a display identifier via the --display or -d command line option to Emacs, which I'll do in the following example because I don't know how to set env vars in Windows batch files:

@echo off
chdir c:\LocalApp\cygwin\bin
start mintty.exe /usr/bin/emacs-X11.exe --display 127.0.0.1:0 %1

If necessary, which it probably isn't, replace the --display value given here with something more suited for your X server configuration.

This will probably still display a console window, since you're using the Windows start command to spawn a mintty process which you then ask to launch Emacs. What you can do instead is use the Cygwin run command, which launches a given binary without a console window, and eliminate the redundant mintty process:

@echo off
chdir c:\LocalApp\cygwin\bin
run /usr/bin/emacs-X11.exe --display 127.0.0.1:0 %1

2) Finally, you need to find a way to pass the file path to Emacs in a form it can understand. Unfortunately, I'm pretty sure command interpolation is impossible in Windows batch language, so you can't do the equivalent of e.g. Bash $(cygpath -au %1). Perhaps your best option might be to have the Windows batch file run Cygwin Bash, passing the filename argument to a script which translates it and launches Emacs. For example, your batch file might be

@echo off
chdir C:\LocalApp\cygwin\bin
run sh /path/to/launch-emacs.sh %1

And then, in launch-emacs.sh, you might have something like:

#!/bin/sh
cd /cygdrive/c/LocalApp/cygwin/bin
/usr/bin/emacs-X11 --display 127.0.0.1:0 `cygpath -au $1`

which translates the path via cygpath, then hands it off to Emacs, along with a display identifier as described above.

My only Windows box is at home, so I haven't had opportunity to test these exact scripts, but I do some pretty similar things with Emacs on that machine; assuming your X server is properly configured, the stuff in 1) will almost certainly work, and the rest should be OK modulo a superfluous console window about which you may or may not care. Let me know how it goes, and I'll see what further help I can offer.

6
  • first of all apologies to everybody for the late reply and thanks for all the discussion.
    – point618
    Dec 5, 2013 at 16:22
  • I have tried the above solution: part #1 works as you said. part #2 sort of works but not quite. the launch-emacs.sh script does not seem to need the cd /cygdrive/c/LocalApp/cygwin/bin instruction (it messes up with the file path). if that line is taken out, launch-emacs.sh can open a file in emacs if I run it manually from mintty but not if it is called from the bat file. it seems info about the file is not passed correctly from one script to the next for some reason. they seem right to me. cheers.
    – point618
    Dec 5, 2013 at 17:01
  • @rsom If you're using these scripts with files whose paths contain spaces, you'll probably need to add quotes around the positional parameter in both the batch file and the shell script, e.g. "%1" in the former and "$1" in the latter. Sorry, I should've thought of that in the first place. Dec 5, 2013 at 17:48
  • no I think it is something else. the bat file is:@echo off chdir C:\LocalApp\RS445\cygwin\bin run sh /cygdrive/c/LocalApp/RS445/cygwin/launch-emacs.sh "%1" and the launch-emacs.sh file is basically only: /usr/bin/emacs-X11 --display 127.0.0.1:0 `cygpath -au "$1"` I understand the first script opens a shell (sh), and if I could output to it from the second script maybe I could understand if the file name and path are passed on correctly. but the windows disappears too fast!
    – point618
    Dec 5, 2013 at 18:02
  • @rsom You could add a line to the shell script, before it starts Emacs, e.g. echo `cygpath -au "$1"` >> /tmp/launch-emacs.log to see what path's actually getting passed to the Emacs binary. The same technique generalizes to get pretty much whatever information you need out of the shell script, and you can tail -F /tmp/launch-emacs.log in another shell window to see what's coming out of the script on each run. Dec 5, 2013 at 19:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .