forked from emacsorphanage/git-gutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-gutter.el
More file actions
1703 lines (1509 loc) · 69.8 KB
/
Copy pathgit-gutter.el
File metadata and controls
1703 lines (1509 loc) · 69.8 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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; git-gutter.el --- Port of Sublime Text plugin GitGutter -*- lexical-binding: t; -*-
;; Copyright (C) 2016-2020 Syohei YOSHIDA <syohex@gmail.com>
;; Copyright (C) 2020-2022 Neil Okamoto <neil.okamoto+melpa@gmail.com>
;; Copyright (C) 2020-2024 Shen, Jen-Chieh <jcs090218@gmail.com>
;; Copyright (C) 2024-2025 Seven Beep <ebn@entreparentheses.xyz>
;; Author: Syohei YOSHIDA <syohex@gmail.com>
;; Maintainer: Neil Okamoto <neil.okamoto+melpa@gmail.com>
;; Shen, Jen-Chieh <jcs090218@gmail.com>
;; URL: https://github.com/emacsorphanage/git-gutter
;; Version: 0.94.4
;; Package-Requires: ((emacs "27.1"))
;; 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:
;;
;; Port of GitGutter which is a plugin of Sublime Text
;;; Code:
(require 'cl-lib)
(defgroup git-gutter nil
"Port GitGutter"
:prefix "git-gutter:"
:group 'vc)
;; Defined by `define-minor-mode' below; declared here for the byte compiler.
(defvar git-gutter-mode)
(defcustom git-gutter:window-width nil
"Character width of gutter window. Emacs mistakes width of some characters.
It is better to explicitly assign width to this variable, if you use full-width
character for signs of changes"
:type 'integer
:group 'git-gutter)
(defcustom git-gutter:diff-option ""
"Option of `git diff\'."
:type 'string
:group 'git-gutter)
(defcustom git-gutter:subversion-diff-option ""
"Option of `svn diff\'."
:type 'string
:group 'git-gutter)
(defcustom git-gutter:mercurial-diff-option ""
"Option of `hg diff\'."
:type 'string
:group 'git-gutter)
(defcustom git-gutter:bazaar-diff-option ""
"Option of `bzr diff\'."
:type 'string
:group 'git-gutter)
(defcustom git-gutter:jj-diff-option ""
"Option of `jj diff\'."
:type 'string
:group 'git-gutter)
(defcustom git-gutter:update-hooks
'(after-save-hook
after-revert-hook
find-file-hook
after-change-major-mode-hook
text-scale-mode-hook)
"Hook points of updating gutter."
:type '(list (hook :tag "HookPoint")
(repeat :inline t (hook :tag "HookPoint")))
:group 'git-gutter)
(defcustom git-gutter:always-show-separator nil
"Show separator even if there are no changes."
:type 'boolean
:group 'git-gutter)
(defcustom git-gutter:separator-sign nil
"Separator sign."
:type 'string
:group 'git-gutter)
(defcustom git-gutter:modified-sign "="
"Modified sign."
:type 'string
:group 'git-gutter)
(defcustom git-gutter:added-sign "+"
"Added sign."
:type 'string
:group 'git-gutter)
(defcustom git-gutter:deleted-sign "-"
"Deleted sign."
:type 'string
:group 'git-gutter)
(defcustom git-gutter:staged-sign nil
"Sign for lines whose changes are staged, or nil to show no staged signs.
Staged signs are shown only for git, and only when
`git-gutter:start-revision' is not set."
:type '(choice (const :tag "No staged signs" nil) string)
:group 'git-gutter)
(defcustom git-gutter:unchanged-sign nil
"Unchanged sign."
:type 'string
:group 'git-gutter)
(defcustom git-gutter:hide-gutter nil
"Hide gutter if there are no changes."
:type 'boolean
:group 'git-gutter)
(defcustom git-gutter:lighter " GitGutter"
"Minor mode lighter in mode-line."
:type 'string
:group 'git-gutter)
(defcustom git-gutter:verbosity 0
"Log/message level. 4 means all, 0 nothing."
:type 'integer
:group 'git-gutter)
(defcustom git-gutter:visual-line nil
"Show sign at gutter by visual line."
:type 'boolean
:group 'git-gutter)
(defface git-gutter:separator
'((t (:foreground "cyan" :weight bold :inherit default)))
"Face of separator")
(defface git-gutter:modified
'((t (:foreground "magenta" :weight bold :inherit default)))
"Face of modified")
(defface git-gutter:added
'((t (:foreground "green" :weight bold :inherit default)))
"Face of added")
(defface git-gutter:deleted
'((t (:foreground "red" :weight bold :inherit default)))
"Face of deleted")
(defface git-gutter:unchanged
'((t (:background "yellow" :inherit default)))
"Face of unchanged")
(defface git-gutter:staged
'((t (:foreground "cyan" :weight bold :inherit default)))
"Face of staged")
(defcustom git-gutter:disabled-modes nil
"A list of modes which `global-git-gutter-mode' should be disabled."
:type '(repeat symbol)
:group 'git-gutter)
(defcustom git-gutter:handled-backends '(git)
"List of version control backends for which `git-gutter.el` will be used.
`git', `svn', `hg', `bzr' and `jj' are supported."
:type '(repeat symbol)
:group 'git-gutter)
(defvar git-gutter:view-diff-function #'git-gutter:view-diff-infos
"Function of viewing changes.")
(defvar git-gutter:clear-function #'git-gutter:clear-diff-infos
"Function of clear changes.")
(defvar git-gutter:init-function 'nil
"Function of initialize.")
(defcustom git-gutter-mode-on-hook nil
"Hook run when git-gutter mode enable."
:type 'hook
:group 'git-gutter)
(defcustom git-gutter-mode-off-hook nil
"Hook run when git-gutter mode disable."
:type 'hook
:group 'git-gutter)
(defcustom git-gutter:update-interval nil
"Idle time in seconds before a live update, or nil for no live updates.
A live update compares the unsaved buffer with the original version
and runs each time Emacs has been idle for this many seconds, for
example after you stop typing. A value such as 0.1 shows changes while
you edit. A new value takes effect at once, also when set with
`setq'. The value 0 also means no live updates."
:type '(choice (const :tag "No live updates" nil)
(number :tag "Idle seconds"))
:group 'git-gutter)
(defun git-gutter:live-update-interval ()
"Return `git-gutter:update-interval' if live updates are on, else nil."
(and (numberp git-gutter:update-interval)
(> git-gutter:update-interval 0)
git-gutter:update-interval))
(defcustom git-gutter:ask-p t
"Ask whether commit/revert or not."
:type 'boolean
:group 'git-gutter)
(defcustom git-gutter:display-p t
"Display diff information or not."
:type 'boolean
:group 'git-gutter)
(defvar git-gutter:start-revision nil
"Starting revision for vc diffs.
Can be a directory-local variable in your project.")
(make-variable-buffer-local 'git-gutter:start-revision)
(put 'git-gutter:start-revision 'safe-local-variable
(lambda (x) (or (booleanp x) (stringp x))))
(cl-defstruct git-gutter-hunk
type content start-line end-line)
(defvar-local git-gutter:enabled nil)
(defvar git-gutter:diffinfos nil)
(defvar git-gutter:has-indirect-buffers nil)
(defun git-gutter--has-indirect-buffers-p ()
"Non-nil if an indirect buffer of the current buffer exists.
`git-gutter:has-indirect-buffers' counts them through advice on
`make-indirect-buffer', which does not run when natively compiled code
calls it, so also look for them."
(or git-gutter:has-indirect-buffers
(let ((base (current-buffer)))
(cl-some (lambda (buf) (eq (buffer-base-buffer buf) base))
(buffer-list)))))
(defvar git-gutter:vcs-type nil)
(defvar git-gutter:revision-history nil)
(defvar git-gutter:update-timer nil)
(defvar-local git-gutter--installed-update-hooks nil
"The hooks in which `git-gutter-mode' added `git-gutter' in this buffer.")
(defvar-local git-gutter:last-chars-modified-tick nil)
(defvar-local git-gutter--live-update-file nil
"Temporary file that live update writes the buffer to, or nil.
The first live update in the buffer creates it; later ones overwrite it.")
(defvar-local git-gutter--temp-files-owner nil
"The buffer whose live update made the temporary files named here.
A clone copies the local variables of its base buffer, and with them the
names of the base buffer's files; see `git-gutter--own-temp-files'.")
(defvar-local git-gutter--live-update-process nil
"The diff process of the live update running in the current buffer, or nil.")
(defvar-local git-gutter--live-update-pending nil
"Non-nil if a live update was due while the previous one's diff ran.")
(defvar-local git-gutter--last-update 0
"Number of the last update started in the current buffer.
Each full or live update takes the next number when it starts, and shows
its result only if no other update has started since.")
(defvar-local git-gutter:live-update-cache nil
"Cons (ROOT . ORIGINAL) that live update reuses, or nil.
ROOT is the true name of the repository root. ORIGINAL is a temporary
file with the version live update compares the buffer with, or nil if
the file has no such version. A full update clears the cache.")
(defvar git-gutter:popup-buffer "*git-gutter:diff*")
(defmacro git-gutter:awhen (test &rest body)
"Anaphoric when.
Argument TEST is the case before BODY execution."
(declare (indent 1))
`(let ((it ,test))
(when it ,@body)))
(defsubst git-gutter:execute-command (cmd output &rest args)
(apply #'process-file cmd nil output nil args))
(defun git-gutter:in-git-repository-p ()
(when (executable-find "git" t)
(with-temp-buffer
(when-let* ((exec-result (git-gutter:execute-command
"git" t "rev-parse" "--is-inside-work-tree")))
(when (zerop exec-result)
(goto-char (point-min))
(looking-at-p "true"))))))
(defun git-gutter:in-repository-common-p (cmd check-subcmd repodir)
(and (executable-find cmd t)
(locate-dominating-file default-directory repodir)
(zerop (apply #'git-gutter:execute-command cmd nil check-subcmd))
(not (string-match-p (regexp-quote (concat "/" repodir "/"))
default-directory))))
(defun git-gutter:vcs-check-function (vcs)
(cl-case vcs
(git (git-gutter:in-git-repository-p))
(svn (git-gutter:in-repository-common-p "svn" '("info") ".svn"))
(hg (git-gutter:in-repository-common-p "hg" '("root") ".hg"))
(bzr (git-gutter:in-repository-common-p "bzr" '("root") ".bzr"))
(jj (git-gutter:in-repository-common-p "jj" '("root") ".jj"))))
(defun git-gutter:in-repository-p ()
(cl-loop for vcs in git-gutter:handled-backends
when (git-gutter:vcs-check-function vcs)
return (setq-local git-gutter:vcs-type vcs)))
(defsubst git-gutter:changes-to-number (str)
(if (string= str "")
1
(string-to-number str)))
(defsubst git-gutter:base-file ()
(buffer-file-name (buffer-base-buffer)))
(defun git-gutter:diff-content ()
(save-excursion
(goto-char (line-beginning-position))
(let ((curpoint (point)))
(forward-line 1)
(if (re-search-forward "^@@" nil t)
(backward-char 3) ;; for '@@'
(goto-char (point-max)))
(buffer-substring curpoint (point)))))
(defvar git-gutter:diff-output-regexp
"^@@ -\\(?:[0-9]+\\),?\\([0-9]*\\) \\+\\([0-9]+\\),?\\([0-9]*\\) @@"
"Parse diff output.")
(defun git-gutter:process-diff-output (buf)
(when (buffer-live-p buf)
(with-current-buffer buf
(goto-char (point-min))
(cl-loop while (re-search-forward git-gutter:diff-output-regexp nil t)
for new-line = (string-to-number (match-string 2))
for orig-changes = (git-gutter:changes-to-number (match-string 1))
for new-changes = (git-gutter:changes-to-number (match-string 3))
for type = (cond ((zerop orig-changes) 'added)
((zerop new-changes) 'deleted)
(t 'modified))
for end-line = (if (eq type 'deleted)
new-line
(1- (+ new-line new-changes)))
for content = (git-gutter:diff-content)
collect
(let ((start (if (zerop new-line) 1 new-line))
(end (if (zerop end-line) 1 end-line)))
(make-git-gutter-hunk
:type type :content content :start-line start :end-line end))))))
(defsubst git-gutter:window-margin ()
(or git-gutter:window-width (git-gutter:longest-sign-width)))
(defun git-gutter:set-window-margin (width)
(when (>= width 0)
(let ((curwin (get-buffer-window)))
(set-window-margins curwin width (cdr (window-margins curwin))))))
(defsubst git-gutter:revision-set-p ()
(and git-gutter:start-revision (not (string= git-gutter:start-revision ""))))
(defun git-gutter:git-diff-arguments (file)
(let (args)
(unless (string= git-gutter:diff-option "")
(setq args (nreverse (split-string git-gutter:diff-option))))
(when (git-gutter:revision-set-p)
(push git-gutter:start-revision args))
(push "--" args)
(nreverse (cons file args))))
(defun git-gutter:start-git-diff-process (file proc-buf)
(let ((arg (git-gutter:git-diff-arguments file)))
(apply #'start-file-process "git-gutter" proc-buf
"git" "--no-pager" "-c" "diff.autorefreshindex=0"
"diff" "--no-color" "--no-ext-diff" "--relative" "-U0"
arg)))
(defun git-gutter:start-git-head-diff-process (file proc-buf)
"Diff FILE in the working tree against HEAD, with output to PROC-BUF."
(let ((git-gutter:start-revision "HEAD"))
(git-gutter:start-git-diff-process file proc-buf)))
(defun git-gutter:svn-diff-arguments (file)
(let (args)
(unless (string= git-gutter:subversion-diff-option "")
(setq args (nreverse (split-string git-gutter:subversion-diff-option))))
(when (git-gutter:revision-set-p)
(push "-r" args)
(push git-gutter:start-revision args))
(nreverse (cons file args))))
(defsubst git-gutter:start-svn-diff-process (file proc-buf)
(let ((args (git-gutter:svn-diff-arguments file)))
(apply #'start-file-process "git-gutter" proc-buf "svn" "diff" "--diff-cmd"
"diff" "-x" "-U0" args)))
(defun git-gutter:hg-diff-arguments (file)
(let (args)
(unless (string= git-gutter:mercurial-diff-option "")
(setq args (nreverse (split-string git-gutter:mercurial-diff-option))))
(when (git-gutter:revision-set-p)
(push "-r" args)
(push git-gutter:start-revision args))
(nreverse (cons file args))))
(defsubst git-gutter:start-hg-diff-process (file proc-buf)
(let ((args (git-gutter:hg-diff-arguments file))
(process-environment (cons "HGPLAIN=1" process-environment)))
(apply #'start-file-process "git-gutter" proc-buf "hg" "diff" "-U0" args)))
(defun git-gutter:bzr-diff-arguments (file)
(let (args)
(unless (string= git-gutter:bazaar-diff-option "")
(setq args (nreverse (split-string git-gutter:bazaar-diff-option))))
(when (git-gutter:revision-set-p)
(push "-r" args)
(push git-gutter:start-revision args))
(nreverse (cons file args))))
(defsubst git-gutter:start-bzr-diff-process (file proc-buf)
(let ((args (git-gutter:bzr-diff-arguments file)))
(apply #'start-file-process "git-gutter" proc-buf
"bzr" "diff" "--context=0" args)))
(defun git-gutter:jj-diff-arguments (file)
(let (args)
(unless (string= git-gutter:jj-diff-option "")
(setq args (nreverse (split-string git-gutter:jj-diff-option))))
(when (git-gutter:revision-set-p)
(push "--from" args)
(push git-gutter:start-revision args))
;; jj reads a path as a fileset expression; quote it, so that names
;; with spaces or parentheses are not parsed as fileset syntax.
(nreverse (cons (format "file:%S" file) args))))
(defsubst git-gutter:start-jj-diff-process (file proc-buf)
(let ((args (git-gutter:jj-diff-arguments file)))
(apply #'start-file-process "git-gutter" proc-buf
"jj" "--config=ui.diff-formatter=:git"
"--no-pager" "--quiet" "--color" "never" "diff" "--context" "0"
args)))
(defun git-gutter:start-diff-process1 (file proc-buf)
(let ((coding-system-for-read buffer-file-coding-system))
(cl-case git-gutter:vcs-type
(git (git-gutter:start-git-diff-process file proc-buf))
(svn (git-gutter:start-svn-diff-process file proc-buf))
(hg (git-gutter:start-hg-diff-process file proc-buf))
(bzr (git-gutter:start-bzr-diff-process file proc-buf))
(jj (git-gutter:start-jj-diff-process file proc-buf)))))
(defun git-gutter:show-staged-p ()
"Non-nil when staged signs are shown in the current buffer."
(and git-gutter:staged-sign
(eq git-gutter:vcs-type 'git)
(not (git-gutter:revision-set-p))))
(defun git-gutter:start-diff-process (curfile proc-buf)
(if (git-gutter:show-staged-p)
(git-gutter:start-combined-git-diff-process curfile proc-buf)
(let* ((file (git-gutter:base-file))
(curbuf (current-buffer))
(update (cl-incf git-gutter--last-update))
(process (git-gutter:start-diff-process1 curfile proc-buf)))
(set-process-query-on-exit-flag process nil)
(set-process-sentinel
process
(lambda (proc _event)
(when (eq (process-status proc) 'exit)
(let ((diffinfos (git-gutter:process-diff-output (process-buffer proc))))
(when (and (buffer-live-p curbuf)
(= update (buffer-local-value 'git-gutter--last-update curbuf)))
(with-current-buffer curbuf
(setq git-gutter:enabled nil)
(git-gutter:update-diffinfo diffinfos)
(when (git-gutter--has-indirect-buffers-p)
(git-gutter:update-indirect-buffers file))
(setq git-gutter:enabled t)))
(kill-buffer proc-buf))))))))
(defun git-gutter:staged-hunks (head-hunks unstaged-hunks)
"Return the parts of HEAD-HUNKS that are not in UNSTAGED-HUNKS.
HEAD-HUNKS are the changes since HEAD and UNSTAGED-HUNKS the changes not
in the index. Both come from diffs against the working tree, so their
line numbers are the buffer's. A line changed since HEAD but not
unstaged is staged. The result has type `staged'."
(let ((unstaged-lines (make-hash-table))
staged)
(dolist (hunk unstaged-hunks)
(cl-loop for line from (git-gutter-hunk-start-line hunk)
to (git-gutter-hunk-end-line hunk)
do (puthash line t unstaged-lines)))
(dolist (hunk head-hunks)
(let (run-start)
(cl-loop for line from (git-gutter-hunk-start-line hunk)
to (1+ (git-gutter-hunk-end-line hunk))
do (if (and (<= line (git-gutter-hunk-end-line hunk))
(not (gethash line unstaged-lines)))
(unless run-start (setq run-start line))
(when run-start
(push (make-git-gutter-hunk
:type 'staged
:content (git-gutter-hunk-content hunk)
:start-line run-start :end-line (1- line))
staged)
(setq run-start nil))))))
(nreverse staged)))
(defun git-gutter:start-combined-git-diff-process (curfile proc-buf)
"Diff CURFILE against the index and against HEAD, and show both.
PROC-BUF stays alive until both processes finish, so that `git-gutter'
does not start another pair of processes meanwhile."
(let* ((file (git-gutter:base-file))
(curbuf (current-buffer))
(update (cl-incf git-gutter--last-update))
(unstaged-buf (get-buffer-create (concat (buffer-name proc-buf) "-unstaged")))
(head-buf (get-buffer-create (concat (buffer-name proc-buf) "-head")))
(unstaged :pending)
(head :pending)
(finish
(lambda ()
(unless (or (eq unstaged :pending) (eq head :pending))
(when (and (buffer-live-p curbuf)
(= update (buffer-local-value 'git-gutter--last-update curbuf)))
(with-current-buffer curbuf
(setq git-gutter:enabled nil)
(git-gutter:update-diffinfo
(sort (append unstaged (git-gutter:staged-hunks head unstaged))
(lambda (a b)
(< (git-gutter-hunk-start-line a)
(git-gutter-hunk-start-line b)))))
(when (git-gutter--has-indirect-buffers-p)
(git-gutter:update-indirect-buffers file))
(setq git-gutter:enabled t)))
(when (buffer-live-p proc-buf)
(kill-buffer proc-buf))))))
(dolist (spec (list (list unstaged-buf #'git-gutter:start-git-diff-process
(lambda (hunks) (setq unstaged hunks)))
(list head-buf #'git-gutter:start-git-head-diff-process
(lambda (hunks) (setq head hunks)))))
(let ((process (funcall (nth 1 spec) curfile (nth 0 spec)))
(store (nth 2 spec)))
(set-process-query-on-exit-flag process nil)
(set-process-sentinel
process
(lambda (proc _event)
(when (memq (process-status proc) '(exit signal))
(funcall store (git-gutter:process-diff-output (process-buffer proc)))
(kill-buffer (process-buffer proc))
(funcall finish))))))))
(defsubst git-gutter:gutter-seperator ()
(when git-gutter:separator-sign
(propertize git-gutter:separator-sign 'face 'git-gutter:separator)))
(defun git-gutter:before-string (sign)
(let ((gutter-sep (concat sign (git-gutter:gutter-seperator))))
(propertize " " 'display `((margin left-margin) ,gutter-sep))))
(defun git-gutter:propertized-sign (type)
(let (sign face)
(cl-case type
(added (setq sign git-gutter:added-sign
face 'git-gutter:added))
(modified (setq sign git-gutter:modified-sign
face 'git-gutter:modified))
(deleted (setq sign git-gutter:deleted-sign
face 'git-gutter:deleted))
(staged (setq sign git-gutter:staged-sign
face 'git-gutter:staged)))
(when (get-text-property 0 'face sign)
(setq face (append
(get-text-property 0 'face sign)
`(:inherit ,face))))
(propertize sign 'face face)))
(defun git-gutter:wrap-prefix-for-sign (sign pos)
"Return a `wrap-prefix' string that renders SIGN in the left margin.
Prepends the gutter sign to any existing `wrap-prefix' text property at POS
so that continuation indentation (e.g. from `visual-wrap-prefix-mode') is
preserved on wrapped rows."
(let ((existing (get-text-property pos 'wrap-prefix)))
(concat (git-gutter:before-string sign) (if (stringp existing) existing ""))))
(cl-defstruct (git-gutter--group
(:constructor git-gutter--make-group (key lines overlays)))
"The sign overlays drawn for one hunk or one range of unchanged lines.
KEY identifies the signs, LINES is the number of lines, and OVERLAYS
are the overlays in buffer order."
key lines overlays)
(defvar-local git-gutter--groups nil
"The `git-gutter--group's of the sign overlays in the current buffer.")
(defvar-local git-gutter--edited t
"The text edited since `git-gutter:view-diff-infos' last drew the signs.
nil if no text was edited, t for the whole buffer, or a cons of two
markers around the edited text.")
(defvar git-gutter--old-groups nil
"Hash table from positions to the old `git-gutter--group' that starts there.
`git-gutter:view-diff-infos' binds it while it draws the signs.")
(defvar git-gutter--old-overlays nil
"Hash table from positions to the old sign overlays that start there.
These overlays belong to no group any more; `git-gutter:put-signs'
reuses them. `git-gutter:view-diff-infos' binds it while it draws the
signs.")
(defvar git-gutter--new-groups nil
"The groups drawn so far by `git-gutter:view-diff-infos'.")
(defun git-gutter--after-change (beg end _len)
"Add the text from BEG to END to `git-gutter--edited'.
Delete the sign overlays there that no longer start a line."
(cond ((eq git-gutter--edited t))
((null git-gutter--edited)
(setq git-gutter--edited (cons (copy-marker beg) (copy-marker end t))))
(t
(when (< beg (car git-gutter--edited))
(set-marker (car git-gutter--edited) beg))
(when (> end (cdr git-gutter--edited))
(set-marker (cdr git-gutter--edited) end))))
;; When an edit joins two lines, the second line's overlay moves to the
;; join and would show a second sign on that screen row until the next
;; update.
(save-excursion
(dolist (ov (overlays-in beg (min (1+ end) (point-max))))
(when (and (overlay-get ov 'git-gutter)
(progn (goto-char (overlay-start ov)) (not (bolp))))
(delete-overlay ov)))))
(defun git-gutter--set-edited (value)
"Set `git-gutter--edited' to VALUE, and release its old markers."
(when (consp git-gutter--edited)
(set-marker (car git-gutter--edited) nil)
(set-marker (cdr git-gutter--edited) nil))
(setq git-gutter--edited value))
(defun git-gutter--sign-key (sign)
(list (substring-no-properties sign) (get-text-property 0 'face sign)
git-gutter:separator-sign))
(defun git-gutter--release-group (group)
"Move the overlays of GROUP to `git-gutter--old-overlays'."
(dolist (ov (git-gutter--group-overlays group))
(when (overlay-buffer ov)
(push ov (gethash (overlay-start ov) git-gutter--old-overlays)))))
(defun git-gutter--take-group (pos)
"Remove from `git-gutter--old-groups' the group at POS and return it."
(when git-gutter--old-groups
(let ((group (gethash pos git-gutter--old-groups)))
(when group
(remhash pos git-gutter--old-groups))
group)))
(defun git-gutter--old-overlay (pos)
"Remove from `git-gutter--old-overlays' an overlay at POS and return it.
If a group starts at POS, release its overlays first."
(when git-gutter--old-overlays
(git-gutter:awhen (git-gutter--take-group pos)
(git-gutter--release-group it))
(let ((ovs (gethash pos git-gutter--old-overlays)))
(when ovs
(puthash pos (cdr ovs) git-gutter--old-overlays)
(car ovs)))))
(defvar git-gutter--edited-lines nil
"The lines of `git-gutter--edited': t, nil, or (BEG . END).
BEG is the start of the first edited line, END the end of the last.
`git-gutter:view-diff-infos' binds it while it draws the signs.")
(defun git-gutter--edited-lines ()
(if (consp git-gutter--edited)
(save-excursion
(cons (progn (goto-char (car git-gutter--edited)) (line-beginning-position))
(progn (goto-char (cdr git-gutter--edited)) (line-end-position))))
git-gutter--edited))
(defun git-gutter--group-edited-p (group)
"Non-nil if text in the lines of GROUP was edited.
A group whose first or last overlay `git-gutter--after-change' deleted
counts as edited."
(or (eq git-gutter--edited-lines t)
(and git-gutter--edited-lines
(let* ((ovs (git-gutter--group-overlays group))
(first (car ovs))
(last (car (last ovs))))
(or (not (overlay-buffer first))
(not (overlay-buffer last))
;; The overlays start at the beginning of their lines,
;; so comparing the starts is enough.
(and (<= (overlay-start first) (cdr git-gutter--edited-lines))
(>= (overlay-start last) (car git-gutter--edited-lines))))))))
(defun git-gutter:put-signs (sign points &optional wrap-sign)
"Put SIGN at each position in POINTS, and return the overlays.
When `git-gutter:visual-line' is non-nil, continuation rows show WRAP-SIGN,
or SIGN if WRAP-SIGN is nil. An overlay from `git-gutter--old-overlays'
that already shows the same sign at the same position is kept unchanged."
;; SIGN is the same for all POINTS: build its display string once, and
;; let every overlay share it.
(let ((gutter-sign (git-gutter:before-string sign))
;; Ensure changed signs win over separator/unchanged overlays.
(priority (when (string-match-p "\\S-" (substring-no-properties sign))
10))
(wrap-sign (or wrap-sign sign))
(sign-key (git-gutter--sign-key sign))
overlays)
(dolist (pos points)
(let* ((visual git-gutter:visual-line)
;; Span the line and its newline: an edit that deletes the
;; line deletes the overlay (`evaporate'), instead of moving
;; it onto the next line. With `git-gutter:visual-line',
;; `wrap-prefix' then applies to every continuation row.
;; `line-end-position' respects fields, which on Emacs 27
;; takes time proportional to the number of overlays.
(end (save-excursion (goto-char pos) (forward-line 1) (point)))
(key (vector sign-key
(when visual (git-gutter--sign-key wrap-sign))
(when visual (get-text-property pos 'wrap-prefix))))
(ov (git-gutter--old-overlay pos)))
(if (not ov)
(progn
(setq ov (make-overlay pos end))
(when (< pos end)
(overlay-put ov 'evaporate t)))
(unless (= (overlay-end ov) end)
;; `move-overlay' deletes an empty overlay that evaporates.
(overlay-put ov 'evaporate (< pos end))
(move-overlay ov pos end)))
(unless (equal-including-properties (overlay-get ov 'git-gutter-key) key)
(overlay-put ov 'before-string gutter-sign)
(overlay-put ov 'priority priority)
(overlay-put ov 'wrap-prefix
(when visual (git-gutter:wrap-prefix-for-sign wrap-sign pos)))
(overlay-put ov 'git-gutter-key key))
(overlay-put ov 'git-gutter t)
(push ov overlays)))
(nreverse overlays)))
(defun git-gutter--signs-key (sign &optional wrap-sign)
(list (git-gutter--sign-key sign)
(when wrap-sign (git-gutter--sign-key wrap-sign))
git-gutter:visual-line))
(defun git-gutter--put-group (sign lines &optional wrap-sign key)
"Put SIGN on LINES lines from point, and move to the line after them.
Keep the old group that starts at point if it shows the same signs on
the same number of lines and no text in them was edited. A non-nil
WRAP-SIGN marks a deleted hunk: one sign, see `git-gutter:put-signs'.
KEY is `git-gutter--signs-key' of SIGN and WRAP-SIGN, or nil to compute it."
(let ((key (or key (git-gutter--signs-key sign wrap-sign)))
(old (git-gutter--take-group (point))))
(if (and old
(equal (git-gutter--group-key old) key)
(= (git-gutter--group-lines old) lines)
(not (git-gutter--group-edited-p old)))
(progn
(push old git-gutter--new-groups)
(forward-line lines))
(when old
(git-gutter--release-group old))
(let (points)
(if wrap-sign
(progn (push (point) points) (forward-line 1))
(dotimes (_ lines)
(unless (eobp)
(push (point) points)
(forward-line 1))))
(when points
(let ((overlays (git-gutter:put-signs sign (nreverse points) wrap-sign)))
(when git-gutter--old-groups
(push (git-gutter--make-group key lines overlays)
git-gutter--new-groups))))))))
(defsubst git-gutter:sign-width (sign)
(cl-loop for s across sign
sum (char-width s)))
(defun git-gutter:longest-sign-width ()
(let ((signs (list git-gutter:modified-sign
git-gutter:added-sign
git-gutter:deleted-sign)))
(when git-gutter:unchanged-sign
(push git-gutter:unchanged-sign signs))
(when git-gutter:staged-sign
(push git-gutter:staged-sign signs))
(+ (apply #'max (mapcar 'git-gutter:sign-width signs))
(git-gutter:sign-width git-gutter:separator-sign))))
(defun git-gutter:propertized-unchanged-sign ()
(if git-gutter:unchanged-sign
(propertize git-gutter:unchanged-sign 'face 'git-gutter:unchanged)
" "))
(defun git-gutter:build-unchanged-ranges (diffinfos max-line)
"Build list of unchanged line ranges [start, end] from diff hunks.
Returns list of (start-line . end-line) pairs for unchanged regions."
(if (null diffinfos)
(list (cons 1 max-line))
(let ((ranges '())
(last-end 0))
;; Process gaps between hunks
(dolist (hunk diffinfos)
(let ((hunk-start (git-gutter-hunk-start-line hunk))
(hunk-end (git-gutter-hunk-end-line hunk)))
;; Add unchanged range before this hunk
(when (> hunk-start (1+ last-end))
(push (cons (1+ last-end) (1- hunk-start)) ranges))
(setq last-end hunk-end)))
;; Add final unchanged range after last hunk
(when (< last-end max-line)
(push (cons (1+ last-end) max-line) ranges))
(nreverse ranges))))
(defun git-gutter:view-for-unchanged (diffinfos)
"Put the unchanged sign on every line outside the hunks in DIFFINFOS.
Without `git-gutter:unchanged-sign', put a blank, which is followed by
`git-gutter:separator-sign'."
(save-excursion
(let* ((sign (git-gutter:propertized-unchanged-sign))
(key (git-gutter--signs-key sign))
(max-line (line-number-at-pos (point-max)))
(line 1))
(goto-char (point-min))
(dolist (range (git-gutter:build-unchanged-ranges diffinfos max-line))
;; Move from the end of the previous range, not from `point-min'.
(forward-line (- (car range) line))
(git-gutter--put-group sign (1+ (- (cdr range) (car range))) nil key)
(setq line (1+ (cdr range)))))))
(defsubst git-gutter:check-file-and-directory ()
(and (git-gutter:base-file)
default-directory (file-directory-p default-directory)))
(defun git-gutter:window-buffer-change-function (window)
"Function to hook into `window-buffer-change-functions' to update `git-gutter'."
(with-selected-window (window-normalize-window window)
(when git-gutter-mode
(git-gutter))))
(defun git-gutter:window-selection-change-function (window)
"Update the signs when WINDOW becomes the selected window.
Emacs calls this from `window-selection-change-functions' also when
WINDOW is deselected; then it does nothing."
(when (eq window (selected-window))
(with-selected-window window
(when git-gutter-mode
(git-gutter)))))
(defsubst git-gutter:diff-process-buffer (curfile)
(concat " *git-gutter-" curfile "-*"))
(defun git-gutter:kill-buffer-hook ()
(git-gutter--delete-buffer-temp-files)
;; In an indirect buffer, the process buffer is the base buffer's.
(unless (buffer-base-buffer)
(let ((buf (git-gutter:diff-process-buffer (git-gutter:base-file))))
(git-gutter:awhen (get-buffer buf)
(kill-buffer it)))))
;;;###autoload
(defun git-gutter:linum-setup ()
"Do nothing; support for `linum-mode' has been removed.
Use `display-line-numbers-mode' instead."
(display-warning 'git-gutter
"`git-gutter:linum-setup' does nothing; use `display-line-numbers-mode'"))
(make-obsolete 'git-gutter:linum-setup 'display-line-numbers-mode "0.95")
(defun git-gutter:show-backends ()
(mapconcat (lambda (backend)
(capitalize (symbol-name backend)))
git-gutter:handled-backends "/"))
(defun git-gutter--install-update-hooks (hooks)
"Run `git-gutter' from HOOKS in this buffer, and from no other hook."
(dolist (hook git-gutter--installed-update-hooks)
(remove-hook hook 'git-gutter t))
(dolist (hook hooks)
(add-hook hook 'git-gutter nil t))
(setq git-gutter--installed-update-hooks (copy-sequence hooks)))
(defun git-gutter--update-hooks-watcher (_symbol newval operation where)
"Install NEWVAL, the new `git-gutter:update-hooks', where the mode is on.
A buffer-local value (WHERE) applies to its buffer only. Ignore `let'
bindings (OPERATION)."
(when (eq operation 'set)
(dolist (buf (if where (list where) (buffer-list)))
(with-current-buffer buf
(when (and git-gutter-mode
(or where (not (local-variable-p 'git-gutter:update-hooks))))
(git-gutter--install-update-hooks newval))))))
(add-variable-watcher 'git-gutter:update-hooks
#'git-gutter--update-hooks-watcher)
;;;###autoload
(define-minor-mode git-gutter-mode
"Git-Gutter mode"
:init-value nil
:global nil
:lighter git-gutter:lighter
(if git-gutter-mode
(if (and (git-gutter:check-file-and-directory)
(git-gutter:in-repository-p))
(progn
(when git-gutter:init-function
(funcall git-gutter:init-function))
(make-local-variable 'git-gutter:diffinfos)
;;(setq-local git-gutter:start-revision nil)
(add-hook 'kill-buffer-hook 'git-gutter:kill-buffer-hook nil t)
;; A new major mode kills all local variables, the cache too.
(add-hook 'change-major-mode-hook
#'git-gutter--delete-buffer-temp-files nil t)
(add-hook 'kill-emacs-hook #'git-gutter--delete-temp-files)
(add-hook 'after-change-functions #'git-gutter--after-change nil t)
(add-hook 'window-buffer-change-functions
#'git-gutter:window-buffer-change-function nil t)
(add-hook 'window-selection-change-functions
#'git-gutter:window-selection-change-function nil t)
(git-gutter--install-update-hooks git-gutter:update-hooks)
(git-gutter)
(unless git-gutter:update-timer
(git-gutter--start-update-timer git-gutter:update-interval)))
(when (> git-gutter:verbosity 2)
(message "Here is not %s work tree" (git-gutter:show-backends)))
(git-gutter-mode -1))
(git-gutter--delete-buffer-temp-files)
(remove-hook 'kill-buffer-hook 'git-gutter:kill-buffer-hook t)
(remove-hook 'change-major-mode-hook #'git-gutter--delete-buffer-temp-files t)
(remove-hook 'after-change-functions #'git-gutter--after-change t)
(git-gutter--set-edited t)
(git-gutter--install-update-hooks nil)
(remove-hook 'window-buffer-change-functions
#'git-gutter:window-buffer-change-function t)
(remove-hook 'window-selection-change-functions
#'git-gutter:window-selection-change-function t)
(git-gutter:clear-gutter)))
(defun git-gutter--turn-on ()
(when (and (buffer-file-name)
(not (memq major-mode git-gutter:disabled-modes)))
(git-gutter-mode +1)))
;;;###autoload
(define-globalized-minor-mode global-git-gutter-mode git-gutter-mode git-gutter--turn-on)
(defsubst git-gutter:show-gutter-p (diffinfos)
(if git-gutter:hide-gutter
(or diffinfos git-gutter:unchanged-sign)
(or global-git-gutter-mode git-gutter:unchanged-sign diffinfos)))
(defun git-gutter:show-gutter (diffinfos)
(when (git-gutter:show-gutter-p diffinfos)
(git-gutter:set-window-margin (git-gutter:window-margin))))
(defun git-gutter:view-set-overlays (diffinfos)
(when (or git-gutter:unchanged-sign git-gutter:separator-sign)
(git-gutter:view-for-unchanged diffinfos))
(save-excursion
(goto-char (point-min))
(cl-loop with curline = 1
;; (TYPE SIGN WRAP-SIGN KEY) for each type met so far.
with signs = nil
for info in diffinfos
for start-line = (git-gutter-hunk-start-line info)
for end-line = (git-gutter-hunk-end-line info)
for type = (git-gutter-hunk-type info)
for (sign wrap-sign key)
= (or (cdr (assq type signs))
(let* ((sign (git-gutter:propertized-sign type))
;; The line of a deleted hunk is unchanged; mark
;; only its first row.
(wrap-sign (when (eq type 'deleted)
(git-gutter:propertized-unchanged-sign)))
(entry (list sign wrap-sign
(git-gutter--signs-key sign wrap-sign))))
(push (cons type entry) signs)
entry))
do
(forward-line (- start-line curline))
(git-gutter--put-group sign (if wrap-sign 1 (1+ (- end-line start-line)))
wrap-sign key)
(setq curline (1+ end-line)))))
(defsubst git-gutter:reset-window-margin-p ()
(or git-gutter:hide-gutter (not global-git-gutter-mode)))