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 files placed in special directories (like "Program Files").

To allow saving of modification you should grand Administrator privileges to Emacs process.

The usual way to run program under Administrator privileges is via menu Run as Administrator.

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 a subject of localization so it depends on Windows 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
  set MAKECYGEMACSADMIN=yes
)

%EMACSCLIENT% %RUNEMACSARG% %args% .

and e has:

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

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

Feeds

all / emacs / java

Tags

adb(1), admin(1), android(1), anki(1), ansible(2), aop(1), blog(2), bytecode(1), c(1), css(2), cygwin(2), driver(1), emacs(3), fs(1), git(3), google(1), gradle(1), hardware(1), hg(2), html(1), interview(13), java(4), js(3), lang(2), lighttpd(1), markdown(1), mobile(1), naming(1), oracle(1), print(1), problem(5), python(1), quiz(6), rst(2), security(3), spring(2), sql(2), srs(1), style(1), tls(2), txt(1), unit(1), utils(1), vcs(3), web(2), win(2), windows(1)

Archive