Wrapping isatty(3)
in a dynamic module seems to work:
$ emacs --batch -Q --load isatty.so --eval '(princ (isatty))'
t
$ emacs --batch -Q --load isatty.so --eval '(princ (isatty))' | cat
nil
Build the module
$ EMACS_ROOT=/home/xcy/src/emacs/ # /path/to/root/of/emacs/source/code
$ gcc -I$EMACS_ROOT/src -fPIC -c isatty.c
$ gcc -shared isatty.o -o isatty.so
isatty.c
's contents:
#include <emacs-module.h>
#include <stdio.h>
#include <unistd.h>
int plugin_is_GPL_compatible;
static emacs_value
Fisatty (emacs_env *env, int argc, emacs_env argv[], void *data)
{
if (isatty (fileno (stdout)))
return env->intern (env, "t");
else
return env->intern (env, "nil");
}
static void
bind_function (emacs_env *env, const char *name, emacs_value Sfun)
{
emacs_value Qfset = env->intern (env, "fset");
emacs_value Qsym = env->intern (env, name);
emacs_value args[] = {Qsym, Sfun};
env->funcall (env, Qfset, 2, args);
}
int emacs_module_init (struct emacs_runtime *ert)
{
emacs_env *env = ert->get_environment (ert);
emacs_value fun = env->make_function (env,
0,
0,
Fisatty,
"Return t if stdout is tty, nil otherwise.",
NULL);
bind_function (env, "isatty", fun);
return 0;
}