Oleksandr Gavenko's blog
2017-02-17 23:40 Cygwin emacs-w32 with Administrator privileges

With modern Windows privileges schema (unlike in Windows XP) Emacs can't save edits to many of files placed in special directories (like "Program Files"). To allow saving of modification you should grand Administrator privileges to Emacs process.

Usual way to run program under Administrator privileges via menu Run as Administrator in Explorer or on desktop shortcut.

In order to run program as Administrator from batch file you have an option to use runas Windows utility. Unfortunately its option /user:... takes account name and Administrator account name is subject of localization so would be different depending on Windows local edition...

Cygwin comes with cygstart utility and with --action=runas option you can start program with administrator privileges.

I maintain special script e that is used to invoke emacsclient with convenient behavior. For example for Cygwin emacs-w32 it makes special steps to hide console window.

I made decision to improve e script so it would ask for Administrator privileges.

Because granting administrator privileges under Windows UAC mean that you will have a dialog with conformation in order to see this dialog only once I need to start only main Emacs server under this condition.

Because core part of e script is just invocation of emacsclient with -a ... option to start Emacs server if it absent the only way to start Emacs by cygstart is by supplying wrapper script to -a option. Because I don't want to maintain and install additional script I told emacsclient to invoke e itself and guard from infinite invocation loop by setting environment variable.

I start Emacs by Win+R then e and Enter. Windows doesn't understand sh-scripts, so it starts e.bat. And if emacsclient-w32 can't find running Emacs server it calls my e sh-script which start emacs-w32 by cygstart --action=runas.

So e.bat has:

for %%X in (emacsclient-w32.exe) do (set FOUND=%%~$PATH:X)
if defined FOUND (
  rem Cygwin Emacs-w32.
  set RUNEMACSARG=-a e
  set EMACSCLIENT=run --quote emacsclient-w32


and e has:

if [[ "$MAKECYGEMACSADMIN" = yes ]]; then
    exec cygstart --action=runas run emacs-w32

Complete solution available at: http://hg.defun.work/utils/file/tip/emacs

Other scenarios of usage e script as documented in man page aren't changed.

emacs, win, cygwin


all / emacs / java / python


admin(1), anki(1), blog(1), css(2), cygwin(2), emacs(3), fs(1), git(1), hg(2), html(1), interview(11), java(1), js(3), lighttpd(1), mobile(1), naming(1), printer(1), problem(5), quiz(6), rst(1), security(1), sql(1), srs(1), unit(1), utils(1), vcs(1), web(2), win(2)