Skip to content

Instantly share code, notes, and snippets.

@salewski
Last active September 21, 2020 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salewski/b33ce69a7d8c74587545d5bb65cfd4a4 to your computer and use it in GitHub Desktop.
Save salewski/b33ce69a7d8c74587545d5bb65cfd4a4 to your computer and use it in GitHub Desktop.
Half-unfuck Debian's 'crontab -e' when $EDITOR is emacsclient
#!/bin/bash -
# SPDX-FileCopyrightText: <text> © 2020 Alan D. Salewski <ads@salewski.email> </text>
#
# SPDX-License-Identifier: GPL-2.0-or-later
# This file has been saved as a GitHub Gist here:
#
# https://gist.github.com/salewski/b33ce69a7d8c74587545d5bb65cfd4a4
# crontab: Wraps Debian's busted crontab(1) command to half-unfuck
# 'crontab -e' invocation when $EDITOR is set to some variation of
# 'emacsclient'.
#
# See:
# • "Debian Bug #19237: crontab should support TMPDIR env variable"
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=19237
#
# For now, our workaround is to first try to guess the path to the "Unix
# domain socket" that the 'crontab' command is effectively hiding from
# 'emacsclient'. If that doesn't work, then we just intercept the
# 'crontab' invocation and manually set EDITOR to 'vim' :-(
#
# Whether you are running Emacs in "daemon mode" (that is, you started
# emacs with some variation of 'emacs --daemon') or you just ran
# (server-start) in your running emacs, generally speaking you /ought
# to/ be able (without this workaround) to invoke 'emacsclient' and have
# it connect to your existing session. Alas...
#
#
# Here's what the bug looks like in action:
#
# $ EDITOR='emacsclient -t' crontab -e
# emacsclient: can't find socket; have you started the server?
# To start the server in Emacs, type "M-x server-start".
# emacsclient: No socket or alternate editor. Please use:
#
# --socket-name
# --server-file (or environment variable EMACS_SERVER_FILE)
# --alternate-editor (or environment variable ALTERNATE_EDITOR)
# crontab: "emacsclient -t" exited with status 1
set -e
declare -r PROG='crontab (wrapper)'
declare -r CRONTAB_PROG='/usr/bin/crontab'
printf "${PROG} (warn): wrapped crontab command (%s) to workaround Debian bug #19237 (\"crontab should support TMPDIR env variable\")\$
' See: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=19237\n\n' \
"$(readlink -f "$0")" 1>&2
set -x
if test "${EDITOR:+is_set}" = 'is_set'; then :; else
"${CRONTAB_PROG}" "$@"
exit $?
fi
: EDITOR: $EDITOR
# We (effectively) only need to wrap crontab(1) when $EDITOR is set to some
# flavor of emacsclient(1).
#
case $EDITOR in
*/ec-c* | */ec-t* | */emacsclient* )
# Note that our 'emacsclient' invocations both specify the '-t' opt ("Open a
# new Emacs frame on the current terminal"). This is both to avoid the
# guesswork that could result in a bogus "*ERROR*: Unknown terminal type"
# error (explanation here: https://unix.stackexchange.com/a/119891) and
# because we are trying to edit the crontab, damnit, and don't need some
# stupid graphical popup editor action.
#
t_guess_spath_001="/tmp/user/${EUID}/emacs${EUID}/server"
t_guess_spath_002="/tmp/emacs${EUID}/server"
if test -S "${t_guess_spath_001}"; then
EDITOR="emacsclient -t -s '${t_guess_spath_001}'" "${CRONTAB_PROG}" "$@"
elif test -S "${t_guess_spath_002}"; then
EDITOR="emacsclient -t -s '${t_guess_spath_002}'" "${CRONTAB_PROG}" "$@"
else
EDITOR='vim' "${CRONTAB_PROG}" "$@"
fi
exit $?
;;
*)
"${CRONTAB_PROG}" "$@"
exit $?
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment