Skip to content

Instantly share code, notes, and snippets.

@seylerius
Created November 9, 2015 01:57
Show Gist options
  • Save seylerius/ede1a26a2a5aa8693ab9 to your computer and use it in GitHub Desktop.
Save seylerius/ede1a26a2a5aa8693ab9 to your computer and use it in GitHub Desktop.
Smart Emacsclient Wrapper
#!/usr/bin/env python
"""e -- a smart emacsclient wrapper"""
import subprocess
import argparse
import locale
import os
import stat
def effectively_writable(path):
"""Determine whether the specified path is writable as the current"""
uid = os.getuid()
euid = os.geteuid()
gid = os.getgid()
egid = os.getegid()
# Is the user's identity fully consistent, or is the user effectively
# someone else?
if uid == euid and gid == egid:
return(os.access(path, os.W_OK))
st = os.stat(path)
# This may be wrong depending on the semantics of your OS.
# i.e. if the file is -------r--, does the owner have access or not?
if st.st_uid == euid:
return(st.st_mode & stat.S_IWUSR != 0)
# See comment for UID check above.
groups = os.getgroups()
if st.st_gid == egid or st.st_gid in groups:
return(st.st_mode & stat.S_IWGRP != 0)
return(st.st_mode & stat.S_IROTH != 0)
def trampify(path):
path = os.path.abspath(os.path.expandvars(os.path.expanduser(path)))
check_path = path
if path.startswith("/ssh:"):
return(path)
if not os.path.isfile(path):
if not os.path.isdir(path):
check_path = os.path.dirname(path)
if not effectively_writable(check_path):
path = "/sudo::" + path
return(path)
def graphical():
if "DISPLAY" in os.environ:
## print("Graphical: True")
return(True)
else:
## print("Graphical: False")
return(False)
def desktop():
## print(os.environ["DISPLAY"])
if graphical() and os.environ["DISPLAY"] in (":0", ":0.0"):
## print("Desktop: True")
return(True)
else:
## print("Desktop: False")
return(False)
#locale.setlocale(locale.LC_ALL, 'en_US.utf-8')
parser = argparse.ArgumentParser(description="Smartly call emacsclient")
parser.add_argument('-w', '--wait',
action='store_true', default=False, dest='wait')
parser.add_argument('args', nargs=argparse.REMAINDER)
args = parser.parse_args()
## print(args.args)
if not args.args:
empty = True
else:
empty = False
args.args = [trampify(path) for path in args.args]
args.args = ['-a', ''] + args.args
if graphical():
if not args.wait:
args.args = ['-n'] + args.args
if desktop():
try:
output = subprocess.check_output(['wmctrl', '-l'])
output = output.decode('utf-8').split('\n')
output = [line.split(' ') for line in output]
output = [line for line in output if len(line) >= 5]
emacs_windows = [win[4] for win in output if 'emacs' in win[4]]
except CalledProcessError:
emacs_windows = None
if not emacs_windows:
## print("No windows")
args.args = ['-c'] + args.args
else:
args.args = ['-d', os.environ["DISPLAY"]] + args.args
else:
## print("Graphical-else")
args.args = ['-c'] + args.args
if empty and desktop() and emacs_windows:
subprocess.call(['wmctrl', '-xa', 'emacs'])
if graphical and args.wait:
old_window = subprocess.check_output(['current-winid'])
subprocess.call(['wmctrl', '-xa', 'emacs'])
if not (empty and emacs_windows):
## print(args.args)
subprocess.call(['emacsclient'] + args.args)
if graphical and args.wait:
subprocess.call(['wmctrl', '-ia', old_window])
#!/bin/zsh
e -w "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment