0

I'm putting together a presentation that involves some live demos in the terminal. In one of the demos, I run a shell script that takes an input file, runs it through an Awk script, and generates a transformed output file. After doing so, I'd like to display the diff between the input and output files.

Ediff is perfect for this, since it colors the diff and I can navigate easily with n and p. However, I've only been using a gui Emacs. If I start ediff in terminal emacs, the coloring seems to be absent, and other things are just wonky, since I've been gui-centric in my configuration.

I'd like to have the shell script focus a running gui Emacs and run ediff on the two files. I'm on macOS, and I have a shell script that uses applescript to focus Emacs, but I don't know of a way it could tell Emacs to evaluate an expression.

My current workaround is to add a keybinding in my init.el that will launch the desired ediff session, and have my script simply launch/focus Emacs. Then it's up to me to hit the keybinding.

The demo shell script looks like:

#!/usr/bin/env bash

input=/path/to/input
output=/path/to/output

awk_script=/path/to/awk_script

awk -f "$awk_script" "$input" > "$output"

e

The e script launches/focuses emacs. It can be seen here, but the gist of it is:

osascript -e "tell application \"$emacs_app\" to activate"

And in my init.el, I've added:

(defun ivan-demo-ediff ()
  (interactive)
  (ediff-files "/path/to/input"
               "/path/to/output"))

(global-set-key [f2] #'ivan-demo-ediff)

Is there a better way to do this?

ivan
  • 1,928
  • 10
  • 20

1 Answers1

2

I use this to diff from bash. --eval is what I think you are looking for. If you are not using emacsclient you can adjust the first portion accordingly.

ediff () 
{ 
    if [ -d $1 ]; then
        emacsclient -c -a emacs -q --eval "(ediff-directories \"$1\" \"$2\" \"\")";
    else
        emacsclient -c -a emacs -q --eval "(ediff-files \"$1\" \"$2\")";
    fi
}


ediff3 () 
{ 
    if [ $# -ne 3 ]; then
        echo Usage: $0 local base other;
        exit 1;
    fi;
    emacsclient -c -a emacs -q --eval '(ediff-merge-with-ancestor "'$1'" "'$2'" "'$3'")'
}
eflanigan00
  • 785
  • 4
  • 11
  • Does this open in a gui or in the terminal? I tried it and for me it opens in terminal, but that may be because of the [emacs build](https://github.com/railwaycat/homebrew-emacsmacport) I'm using. – ivan Aug 18 '17 at 11:40
  • Oh, I was mistaken. I must not have had a gui server running the first time I tried this. It works! – ivan Aug 18 '17 at 11:43
  • 1
    The one thing I don't know is how to restore emacs frame properly after quitting the ediff. Magit does a really good job of cleaning up afterwards. But magit is invoked from within emacs itself. – eflanigan00 Aug 18 '17 at 17:58