#!/bin/sh #set -x # Using emacsclient and emacs server for editing files is very # fast and since all files are edited in the same emacs instance # you can e.g. kill-and-yank between different buffers. # This script transparently edits files with emacsclient from the # console, from xterm or from within a screen session running on # the console or on a xterm. # As visible difference to running one emacs instance for every # file you will see in every window the very same buffer because # all files are edited in the same emacs instance which runs in a # screen session. # You may want to set your EDITOR and VISUAL environment # variables to this script. E. g.: # EDITOR=/usr/local/bin/ec # VISUAL=/usr/local/bin/ec # For emacsclient to work, we need an already running Emacs with # a server. If the emacs server is not already running then # start it in an detached screen session and wait until startup # finished. if ! screen -ls|grep emacsserver >/dev/null ; then # screen command line options: # -a: all terminal capabilities # -fn: no flow control # -e °°: use ° as command character since the default is unusable with emacs # -d -m: start in detached mode # -S emmacsserver: call this session 'emacsserver' # -t emacs: window title is 'emacs' # emacs command line options: # -nw: do not use graphical interface # -f server-start: Execute the lisp function 'server-start' # --unibyte: Do almost everything with single-byte buffers # and strings; this you may change screen -a -fn -e °° -d -m -S emacsserver -t emacs emacs -nw --unibyte -f server-start echo "please wait patiently" until ps xu | grep '/[e]macsserver' > /dev/null ; do sleep 1 done # Do not show hard status line since we use this screen # session as transparent replacement for an emacs invokation screen -x emacsserver -X hardstatus ignore fi # As a background process connect to the emacs server via # emacsclient. When the user finished editing the buffer, s/he # types `C-x #' (`server-edit'). This sends a message back to # the `emacsclient' program telling it to exit. Then we close # the screen the session connected to the emacs server. # Use 'emacs -w' as fallback if connection to the emacs server # fails. It's probably a safer to avoid emacs as fallback. { emacsclient --alternate-editor "emacs -nw" "$@" screen -x emacsserver -X detach ; } 2>&1 | egrep -v "Waiting for Emacs|No screen session found|screen is terminating" & # Wait until this script connets to screen session 'emacsserver' # in multi-display mode. The fit this screen session to the # terminal. { until ps t | grep 'screen -x emacsserver' > /dev/null ; do sleep 0.01 done screen -x emacsserver -X fit ; } & # Connect to screen session 'emacsserver' in multi-display mode. screen -x emacsserver