-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathemacs-diff
More file actions
executable file
·162 lines (144 loc) · 3.57 KB
/
emacs-diff
File metadata and controls
executable file
·162 lines (144 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env bash
#
# Requirements:
# - emacsclient
#
# Author: James Cherti
# URL: https://github.com/jamescherti/bash-stdops
#
# Description:
# ------------
# Compare files in Emacs using emacsclient and ediff (by default).
#
# Usage:
# emacs-diff [options] <file1> <file2>
#
# Options:
# -v, --verbose Print the emacsclient commands being executed
# -t, --tool NAME Select the diff tool: "ediff" (default) or "vdiff"
#
# Example:
# emacs-diff file1.txt file2.txt
#
# (This will compare file1.txt with file2.txt)
#
# Notes:
# - Requires Emacs to be running as a server via the (server-start) function or
# as an Emacs daemon.
# - Paths are automatically converted to absolute paths before comparison.
#
# License:
# --------
# Copyright (C) 2012-2026 James Cherti
#
# Distributed under terms of the GNU General Public License version 3.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
set -euo pipefail
VERBOSE=0
DIFF_TOOL="ediff" # ediff or vdiff
FILES=()
NO_OPTIONS=0
while [[ $# -gt 0 ]]; do
if [[ $NO_OPTIONS -eq 0 ]]; then
case "$1" in
--)
NO_OPTIONS=1
shift
continue
;;
-v | --verbose)
VERBOSE=1
shift
continue
;;
-t | --tool)
if [[ $# -lt 2 ]]; then
echo "Error: --tool requires an argument" >&2
exit 1
fi
DIFF_TOOL="$2"
shift 2
continue
;;
-*)
echo "Unknown option: $1" >&2
exit 1
;;
esac
fi
# All remaining arguments (or after --) are treated as files
FILES+=("$1")
shift
done
escape_quote_elisp_string() {
local input="$1"
local escaped="${input//\\/\\\\}" # Escape backslashes
escaped="${escaped//\"/\\\"}" # Escape double quotes
escaped="${escaped//$'\n'/\\n}" # Escape newlines
escaped="${escaped//$'\t'/\\t}" # Escape tabs
escaped="${escaped//$'\r'/\\r}" # Escape carriage returns
printf '"%s"' "$escaped"
}
emacsclient_eval() {
local args=()
args+=("emacsclient")
for expr in "$@"; do
args+=("-e" "$expr")
done
if [[ $VERBOSE -ne 0 ]]; then
printf '[CMD] %s\n' "${args[*]}"
fi
"${args[@]}"
}
main() {
if [[ ${#FILES[@]} -lt 2 ]]; then
echo "Error: At least two files must be provided." >&2
echo "Usage: $0 [options] <file1> <file2> [file3 ...]"
exit 1
fi
local func=""
case "$DIFF_TOOL" in
ediff)
func="ediff"
;;
vdiff)
func="vdiff"
;;
*)
echo "Error: Unsupported diff tool: $DIFF_TOOL" >&2
exit 1
;;
esac
local file1="${FILES[0]}"
local file2="${FILES[1]}"
[[ ! -e "$file1" ]] && {
echo "Error: no such file: $file1" >&2
exit 1
}
[[ ! -e "$file2" ]] && {
echo "Error: no such file: $file2" >&2
exit 1
}
local abs_file1
local abs_file2
abs_file1="$(realpath "$file1")"
abs_file2="$(realpath "$file2")"
local cmd
cmd="(progn (${func} $(escape_quote_elisp_string "$abs_file1")
$(escape_quote_elisp_string "$abs_file2")))"
emacsclient_eval --suppress-output --no-wait "$cmd"
}
main "$@"