Real Hackers use M-x butterfly!

I trust vi to simply mangle my text on a wrong keystroke, not wipe my
filesystem, refinance my house, make large political contributions on behalf
of dead relatives, drive while intoxicated or consort with demons, all of
which (I suspect) emacs could perform with an accidental Meta-Meta-Keystroke.
 -- Bob Apthorpe in c.l.p.m

the brown-dragon blog

Run! EMACS Run!

2009-10-01

Old jokes aside, I do use Emacs on WinXP as a near complete replacement for explorer and as a very advanced replacement for the command line utilty SlickRun.

Overall I "live" in Emacs although I use Vim as my main text editor.

One problem I ran into was launching a cygwin terminal from Emacs. The terminal would not function properly so, for instance, mutt would not run correctly.

The problem was that Emacs would set the TERM and SHELL variables before launching a program (using w32-shell-execute) so many terminal applications would run crippled.

This then, is a small wrapper utility that un-sets the TERM and SHELL variables before launching a terminal or program so allowing me to launch everything without ever leaving Emacs or touching the mouse.

Nirvanaaaa!

emacs-run.c

/**
 * emacs-run.c
 *  Launch a WinXP executable from within EMACS using (w32-shell-execute ...)
 *  (after unsetting the SHELL and TERM variables).
 *
 *  http://the-brown-dragon.com/
 */
#define _WIN32_WINNT 0x0501     /* To use XP functions. */
#include <windows.h>

/**
 * Initializes the size of the launched window
 * to a reasonble value that I like.
 */
void
_set_fullscreensize (LPSTARTUPINFO si)
{
        HWND        dw;
        RECT        dr;

    dw  =   GetDesktopWindow ();
    GetClientRect (dw, &dr);
    si->dwFlags |= STARTF_USESIZE;
    si->dwXSize  = 640;
    si->dwYSize  = dr.bottom - dr.top;
}

/**
 * Main entry point.
 */
int WINAPI
WinMain (HINSTANCE hInstance,
         HINSTANCE hPrevInstance,
         LPSTR lpCmdLine,
         int nCmdShow)
{
        STARTUPINFO          si;
        PROCESS_INFORMATION  pi;
        char                 cmd[1024];

    ZeroMemory (&si, sizeof (si));
    si.cb      = sizeof (si);
    _set_fullscreensize (&si);
    ZeroMemory (&pi, sizeof (pi));

    strncpy (cmd, lpCmdLine, sizeof (cmd) - 1);

    SetEnvironmentVariable ("SHELL", NULL);
    SetEnvironmentVariable ("TERM", NULL);
    if (!CreateProcess (NULL,
                   cmd,
                   NULL,
                   NULL,
                   FALSE,
                   CREATE_NEW_CONSOLE,
                   NULL,
                   NULL,
                   &si,
                   &pi)) {
            char    e[1024];

        sprintf (e, "emacs-run.exe: CreateProcess Failed! (%d)", GetLastError ());
        MessageBox (NULL, e, "emacs-run", MB_ICONERROR);
        return EXIT_FAILURE;
    }

    WaitForSingleObject (pi.hProcess, INFINITE);
    CloseHandle (pi.hProcess);
    CloseHandle (pi.hThread);

    return EXIT_SUCCESS;
}

/**
 * NB
 *   - Will not handle very long command lines (should not be needed
 *     for a simple launcher).
 *   - Compiles with: cl /DWIN32 emacs-run.c user32.lib
 */

Other Posts

(ordered by Tags then Date)