-
Notifications
You must be signed in to change notification settings - Fork 59
/
cm-mods.el
412 lines (343 loc) · 11.4 KB
/
cm-mods.el
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
;;; cm-mods.el --- some additions to cm-mode -*- lexical-binding: t; -*-
;; Copyright (C) 2016 John Kitchin
;; Author: John Kitchin <[email protected]>
;; Keywords:
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Builds on https://github.com/joostkremers/criticmarkup-emacs
;;; Code:
(defcustom cm-wdiff-cmd
"wdiff -w {-- -x --} -y {++ -z ++} "
"Command to run wdiff with.")
(defvar *cm-wdiff-git-source* nil
"Global var to hold filename for cm-wdiff-git.")
(require 'cm-mode)
(require 'vc-git)
;; I like more obvious colors
(set-face-foreground cm-deletion-face "red")
(set-face-bold cm-deletion-face t)
(set-face-foreground cm-addition-face "blue")
(set-face-bold cm-addition-face t)
(set-face-foreground cm-comment-face "orange")
(set-face-bold cm-comment-face t)
;;* Parser approach to fontification
;; These functions set match data for fontification.
;; This is how to add them
;; (font-lock-add-keywords
;; nil
;; '((cm-next-deletion (0 cm-deletion-face))
;; (cm-next-addition (0 cm-addition-face))))
;; the original cm-mode regular expressions are used. Here I use a search based,
;; parsing approach to be more robust over multiple lines.
(defun cm-next-deletion (limit)
"Find the next deletion up to LIMIT.
{--content--}.
Sets `match-data'.
group 0 the whole match
group 1 opening marker
group 2 closing marker
group 3 the content.
See `cm-forward-deletion' for an alternative."
(let (start end s1 s2 e1 e2 contents-start contents-end)
(when (re-search-forward
(regexp-quote
(nth 1 (assoc 'cm-deletion cm-delimiters)))
limit t)
(setq start (match-beginning 0)
s1 (match-beginning 0)
s2 (match-end 0)
contents-start (match-end 0)))
(when (re-search-forward
(regexp-quote
(nth 2 (assoc 'cm-deletion cm-delimiters)))
limit t)
(setq end (match-end 0)
e1 (match-beginning 0)
e2 (match-end 0)
contents-end (match-beginning 0)))
(if (and start end)
(progn
(let ((inhibit-read-only t))
(set-text-properties start end '(font-lock-multiline t)))
(set-match-data (list
start end
s1 s2
e1 e2
contents-start contents-end))
;; return t for font-lock to work
t)
nil)))
(defun cm-next-addition (limit)
"Find the next addition up to LIMIT.
{++content++}.
Sets `match-data'.
group 0 the whole match
group 1 opening marker
group 2 closing marker
group 3 the content.
See `cm-forward-addition' for an alternative."
(let (start end s1 s2 e1 e2 contents-start contents-end)
(when (re-search-forward
(regexp-quote
(nth 1 (assoc 'cm-addition cm-delimiters)))
limit t)
(setq start (match-beginning 0)
s1 (match-beginning 0)
s2 (match-end 0)
contents-start (match-end 0)))
(when (re-search-forward
(regexp-quote
(nth 2 (assoc 'cm-addition cm-delimiters)))
limit t)
(setq end (match-end 0)
e1 (match-beginning 0)
e2 (match-end 0)
contents-end (match-beginning 0)))
(if (and start end)
(progn
(let ((inhibit-read-only t))
(set-text-properties start end '(font-lock-multiline t)))
(set-match-data (list
start end
s1 s2
e1 e2
contents-start contents-end))
;; return t for font-lock to work
t)
nil)))
(defun cm-next-comment (limit)
"Find the next addition up to LIMIT.
{>>content<<}.
Sets `match-data'.
group 0 the whole match
group 1 opening marker
group 2 closing marker
group 3 the content.
See `cm-forward-comment' for an alternative."
(let (start end s1 s2 e1 e2 contents-start contents-end)
(when (re-search-forward
(regexp-quote
(nth 1 (assoc 'cm-comment cm-delimiters)))
limit t)
(setq start (match-beginning 0)
s1 (match-beginning 0)
s2 (match-end 0)
contents-start (match-end 0)))
(when (re-search-forward
(regexp-quote
(nth 2 (assoc 'cm-comment cm-delimiters)))
limit t)
(setq end (match-end 0)
e1 (match-beginning 0)
e2 (match-end 0)
contents-end (match-beginning 0)))
(if (and start end)
(progn
(let ((inhibit-read-only t))
(set-text-properties start end '(font-lock-multiline t)))
(set-match-data (list
start end
s1 s2
e1 e2
contents-start contents-end))
;; return t for font-lock to work
t)
nil)))
;;* Convenience functions
(defun cm-accept-all-changes ()
"Accept all changes in the document."
(interactive)
(goto-char (point-min))
(while (cm-forward-change)
(let ((change (cm-expand-change (cm-markup-at-point)))
(inhibit-read-only t))
(cm-without-following-changes
(delete-region (third change) (fourth change))
(insert (cm-substitution-string change ?a))))))
(defun cm-reject-all-changes ()
"Reject all changes in the document."
(interactive)
(goto-char (point-min))
(while (cm-forward-change)
(let ((change (cm-expand-change (cm-markup-at-point)))
(inhibit-read-only t))
(cm-without-following-changes
(delete-region (third change) (fourth change))
(insert (cm-substitution-string change ?r))))))
;;* Convert cm markup to LaTeX
(defun multiline-p (content)
(save-match-data
(string-match "\n" content)))
(defun cm-markup-to-org-latex ()
"Convert cm markup in an org-file to LaTeX."
(interactive)
(goto-char (point-min))
(insert "
#+latex_header: \\usepackage[normalem]{ulem}
#+latex_header: \\usepackage{todonotes}
#+latex_header: \\usepackage[usenames, dvipsnames]{color}
#+latex_header: \\newcommand\\cmred{\\bgroup\\markoverwith{\\textcolor{red}{\\rule[0.5ex]{4pt}{1.4pt}}}\\ULon}
#+latex_header: \\newcommand\\cmblue{\\bgroup\\markoverwith{\\textcolor{blue}{\\rule[-0.5ex]{4pt}{1.4pt}}}\\ULon}
")
;; comments should only be one line so we wrap them in a snippet.
(goto-char (point-min))
(while (cm-next-comment nil)
(replace-match "@@latex:\\\\todo{\\3}@@"))
;; Deletions
(goto-char (point-min))
(while (cm-next-deletion nil)
(replace-match "@@latex:\\\\protect\\\\cmred{\\3}@@"))
;; Additions
(goto-char (point-min))
(while (cm-next-addition nil)
(replace-match "@@latex:\\\\protect\\\\cmblue{\\3}@@")))
(defun cm-wdiff-to-pdf (commits)
"On current buffer, select a commit(s) and convert the wdiff to
a PDF."
(interactive (list (cm-git-commit-selector)))
(with-current-buffer
(cm-wdiff-git (list (if (= (length commits) 2)
(nth 1 commits)
"HEAD")
(nth 0 commits)))
(cm-markup-to-org-latex)
(let ((revised-org (replace-regexp-in-string
".org" "-revised.org"
*cm-wdiff-git-source*)))
(write-file revised-org)
(ox-manuscript-export-and-build-and-open)
(kill-buffer revised-org))))
;;* Get cm markup with wdiff and git
(defun cm-git-commit-selector ()
"Return list of commits."
(helm :sources `((name . "commits")
(candidates . ,(mapcar (lambda (s)
(let ((commit
(nth
0
(split-string s))))
(cons s
commit)))
(split-string
(shell-command-to-string
"git log --pretty=format:\"%h %ad | %s%d [%an]\" --date=relative") "\n")))
(action . (lambda (candidate)
(helm-marked-candidates))))))
(defun cm-wdiff-git (commits)
"Perform a wdiff between git commits.
a helm selection buffer is used to choose commits.
If you choose one commit, the wdiff is between that commit and
the current version.
If you choose two commits, the wdiff is between those two
commits. Returns the buffer."
(interactive
(list (cm-git-commit-selector)))
(let ((buf (get-buffer-create
"*org-wdiff-git*"))
(mmode major-mode)
(git-root (vc-git-root
(buffer-file-name)))
(fname
(file-relative-name
(buffer-file-name)
(vc-git-root (buffer-file-name))))
cmd)
(cond
;; current version vs commit
((= 1 (length commits))
(setq cmd (format "%s <(git show %s:%s) %s"
cm-wdiff-cmd
(car commits) fname
fname)))
;; more than 1 commit, we just take first two
((> (length commits) 1)
(setq cmd (format "%s <(git show %s:%s) <(git show %s:%s)"
cm-wdiff-cmd
(nth 0 commits) fname
(nth 1 commits) fname))))
;; Save fname in global var for convenience to save buffer later
(setq *cm-wdiff-git-source* fname)
(switch-to-buffer-other-window buf)
(let ((inhibit-read-only t))
(erase-buffer))
;; Try to keep same major mode
(funcall mmode)
;; get the wdiff. we do this in git-root so the paths are all correct.
(let ((default-directory git-root))
(insert (shell-command-to-string cmd)))
;; Turn on cm-mode
(cm-mode)
(goto-char (point-min))
buf))
(defun cm-wdiff-save ()
"Save changes.
IF there is an *org-wdiff-git* buffer, then we copy that content
to the buffer visiting `*cm-wdiff-git-source*'. You may use
*org-wdiff-git* to accept/reject changes, and then put it back to
where it came from. Otherwise we just save the buffer."
(interactive)
(if (get-buffer "*org-wdiff-git*")
(progn
(switch-to-buffer (find-buffer-visiting *cm-wdiff-git-source*))
(erase-buffer)
(insert-buffer-substring "*org-wdiff-git*")
(kill-buffer "*org-wdiff-git*"))
(save-buffer)))
(defun cm-wdiff-buffer-with-file ()
"Do a wdiff of the buffer with the last saved version.
For line-based diff use `diff-buffer-with-file'."
(interactive)
(let ((contents (buffer-string))
(tempf (make-temp-file "wdiff-"))
(fname (buffer-file-name)))
(with-temp-file tempf
(insert contents))
(switch-to-buffer "*wdiff-buffer*")
(insert
(shell-command-to-string
(format "%s %s %s"
cm-wdiff-cmd
fname
tempf)))
(delete-file tempf)
(goto-char (point-min))
(cm-mode)))
;;* A Hydra menu
(defhydra cm (:color blue :hint nil)
"
Track changes:
_i_: insert text _d_: delete text _c_: comment
_n_: next change _p_: previous change _e_: accept/reject this change
_a_: acc/rej all ^ ^ _t_: toggle track changes
_A_: accept all _R_: reject all _s_: save changes
_b_: buffer wdiff _g_: git wdiff
"
("i" cm-addition)
("d" cm-deletion)
("c" cm-comment)
("t" (lambda ()
(interactive)
(unless cm-mode
(cm-mode))
(cm-follow-changes 'toggle)))
("n" cm-forward-change :color red)
("p" cm-backward-change :color red)
("e" cm-accept/reject-change-at-point :color red)
("a" cm-accept/reject-all-changes)
("A" cm-accept-all-changes)
("R" cm-reject-all-changes)
("g" cm-wdiff-git)
("b" cm-wdiff-buffer-with-file)
("s" cm-wdiff-save))
(global-set-key (kbd "s-x") 'cm/body)
(provide 'cm-mods)
;;; cm-mods.el ends here