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
  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 / python

Tags

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

Archive