forked from byplayer/egg
/
egg.el
1027 lines (897 loc) · 33.8 KB
/
egg.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
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
;;; egg -- a magit fork
;; Copyright (C) 2008 Marius Vollmer
;; Copyright (C) 2008 Linh Dang
;;
;; Egg 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, or (at your option)
;; any later version.
;;
;; Egg 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 GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary
;;; This is my fork of Marius's excellent magit. his work is at:
;;; http://zagadka.vm.bytemark.co.uk/magit
;;;
(require 'parse-time)
(require 'cl)
(defgroup egg nil
"Controlling Git from Emacs."
:prefix "egg-"
:group 'tools)
(defface egg-header
'((t :weight bold :inherit variable-pitch :height 1.3))
"Face for generic headers.
Many Egg faces inherit from this one by default."
:group 'egg)
(defface egg-section-title
'((((class color) (background light))
:foreground "DarkGoldenrod" :inherit egg-header :height 1.1)
(((class color) (background dark))
:foreground "PaleGreen" :inherit egg-header :height 1.1)
(t :weight bold))
"Face for generic header lines.
Many Egg faces inherit from this one by default."
:group 'egg)
(defface egg-branch
'((((class color) (background light))
:foreground "SkyBlue" :inherit egg-header :height 1.4)
(((class color) (background dark))
:foreground "Yellow" :inherit egg-header :height 1.4)
(t :weight bold))
"Face for the current branch."
:group 'egg)
(defface egg-diff-file-header
'((((class color) (background light))
:foreground "SlateBlue" :inherit egg-header)
(((class color) (background dark))
:foreground "LightSlateBlue" :inherit egg-header)
(t :weight bold))
"Face for diff file headers."
:group 'egg)
(defface egg-diff-hunk-header
'((((class color) (background light))
:background "grey85")
(((class color) (background dark))
:background "grey45"))
"Face for diff hunk headers."
:group 'egg)
(defface egg-diff-add
'((((class color) (background light))
:foreground "blue1")
(((class color) (background dark))
:foreground "white"))
"Face for lines in a diff that have been added."
:group 'egg)
(defface egg-diff-none
'((((class color) (background light))
:foreground "grey50")
(((class color) (background dark))
:foreground "grey70"))
"Face for lines in a diff that are unchanged."
:group 'egg)
(defface egg-diff-del
'((((class color) (background light))
:foreground "red")
(((class color) (background dark))
:foreground "OrangeRed"))
"Face for lines in a diff that have been deleted."
:group 'egg)
(defface egg-item-highlight
'((((class color) (background light))
:foreground "gray95")
(((class color) (background dark))
:foreground "gray30"))
"Face for highlighting the current item."
:group 'egg)
(defcustom egg-diff-init-hiding-mode nil
"Initial hiding mode for diff results."
:group 'egg
:type '(choice :tag "Initial Hiding Mode"
(const :tag "Hide Nothing" nil)
(const :tag "Hide Everything" t)))
;;;========================================================
;;; simple routines
;;;========================================================
(defsubst egg-prepend (str prefix &rest other-properties)
(propertize str 'display
(apply 'propertize (concat prefix str) other-properties)))
(defun egg-cmd-to-string-1 (program args)
"Execute PROGRAM and return its output as a string.
ARGS is a list of arguments to pass to PROGRAM."
(let (str code)
(setq str
(with-output-to-string
(with-current-buffer
standard-output
(setq code (apply 'call-process program nil t nil args)))))
(if (= code 0)
str
nil)))
(defun egg-cmd-1 (program args)
"Execute PROGRAM with ARGS.
The return code and the output are return as a cons."
(let (str code)
(setq str
(with-output-to-string
(with-current-buffer
standard-output
(setq code (apply 'call-process program nil t nil args)))))
(cons code str)))
(defsubst egg-cmd-to-string (program &rest args)
"Execute PROGRAM and return its output as a string.
ARGS is a list of arguments to pass to PROGRAM."
(egg-cmd-to-string-1 program args))
(defun egg-git-to-string (&rest args)
(let* ((str (egg-cmd-to-string-1 "git" args))
(len (length str)))
(when (> len 0)
(if (eq (aref str (1- len)) ?\n)
(substring str 0 -1)
str))))
;; ;; (defun egg-git (exit-codes &rest args)
;; ;; (let ((ret (egg-cmd-1 "git" args))
;; ;; code str len)
;; ;; (unless (consp exit-codes) (setq exit-codes (list exit-codes)))
;; ;; (setq code (car ret)
;; ;; str (cdr ret))
;; ;; (setq len (length str))
;; ;; (when (> len 0)
;; ;; (if (eq (aref str (1- len)) ?\n)
;; ;; (setq str (substring str 0 -1))))
;; ;; (unless (memq code (cons 0 exit-codes))
;; ;; str)))
(defsubst egg-git-to-lines (&rest args)
(split-string (substring (egg-cmd-to-string-1 "git" args) 0 -1)
"\n"))
(defsubst egg-local-branches ()
"Get a list of local branches. E.g. (\"master\", \"wip1\")."
(egg-git-to-lines "rev-parse" "--symbolic" "--branches"))
(defun egg-remote-branches (&optional raw-p)
"Get a list of local branches. E.g. (\"origin/master\", \"joe/fork1\")."
(let ((lst (egg-git-to-lines "rev-parse" "--symbolic" "--remotes")))
(if raw-p lst
(mapcar (lambda (full-name)
(let ((tmp (split-string full-name "/")))
(cons (cadr tmp) (car tmp))))
lst))))
(defsubst egg-rbranch-to-remote (rbranch)
(and (stringp rbranch)
(> (length rbranch) 0)
(car (split-string rbranch "/"))))
(defsubst egg-rbranch-name (rbranch)
(and (stringp rbranch)
(> (length rbranch) 0)
(cadr (split-string rbranch "/"))))
(defsubst egg-push-refspec (lbranch rbranch)
(setq rbranch (egg-rbranch-name rbranch))
(if (or lbranch rbranch)
(format "%s%s%s" (or lbranch "") (if rbranch ":" "") (or rbranch ""))))
(defun egg-file-as-string-raw (file-name)
(with-temp-buffer
(insert-file-contents-literally file-name)
(buffer-string)))
(defun egg-pick-file-contents (file-name regexp &rest indices)
(with-temp-buffer
(insert-file-contents-literally file-name)
(goto-char (point-min))
(when (re-search-forward regexp nil t)
(if (null indices)
(match-string-no-properties 0)
(dolist (idx indices)
(if (match-beginning idx)
(return (match-string-no-properties idx))))))))
(defun egg-file-as-string (file-name)
(let ((str (egg-file-as-string-raw file-name)))
(if (> (length str) 0)
(substring str 0 -1)
str)))
(defsubst egg-is-in-git ()
(= (call-process "git" nil nil nil "rev-parse" "--git-dir") 0))
(defsubst egg-is-dir-in-git (dir)
(let ((default-directory dir)) (egg-is-in-git)))
(defsubst egg-name-rev (rev)
(egg-git-to-string "name-rev" "--always" "--name-only" rev))
(defun egg-read-git-dir ()
(let ((dir (egg-git-to-string "rev-parse" "--git-dir")))
(if (stringp dir)
(expand-file-name dir))))
(defsubst egg-read-dir-git-dir (dir)
(let ((default-directory dir)) (egg-read-git-dir)))
(defvar egg-git-dir nil)
(defsubst egg-git-dir ()
(if (local-variable-p 'egg-git-dir)
egg-git-dir
(set (make-local-variable 'egg-git-dir) (egg-read-git-dir))))
(defsubst egg-buf-git-dir (buffer)
(with-current-buffer buffer
(egg-git-dir)))
(defun egg-HEAD ()
(let* ((git-dir (egg-git-dir)))
(if git-dir
(egg-pick-file-contents (concat git-dir "/HEAD")
"^ref: refs/heads/\\(.+\\)\\|^\\([0-9a-z]+\\)" 1 2))))
(defsubst egg-current-branch ()
(let* ((git-dir (egg-git-dir)))
(if (stringp git-dir)
(egg-pick-file-contents (concat git-dir "/HEAD")
"^ref: refs/heads/\\(.+\\)" 1))))
(defsubst egg-current-sha1 ()
(egg-git-to-string "rev-parse" "--verify" "-q" "HEAD"))
(defsubst egg-head ()
(if (egg-git-dir)
(cons (egg-current-sha1) (egg-current-branch))))
(defun egg-config-section-raw (type &optional name)
(egg-pick-file-contents (concat (egg-git-dir) "/config")
(concat "^"
(if name (format "\\[%s \"%s\"\\]" type name)
(format "\\[%s\\]" type))
"\n"
"\\(\\(:?\t.+\n\\)+\\)")
1))
(defsubst egg-config-section (type &optional name)
(mapcar
(lambda (line) (split-string line "[ =]+" t))
(split-string (egg-config-section-raw type name) "[\t\n]+" t)))
(defsubst egg-config-get (type attr &optional name)
(and (egg-git-dir)
(cadr (assoc attr (egg-config-section type name)))))
(defun egg-bool-config (type attr &optional name)
(let ((flag (egg-config-get type attr name)))
(cond ((equal flag "true")
t)
((equal flag "false")
nil)
(t (error "Unexpected contents of boolean config %s of %s.%s"
attr type name)))))
;;;========================================================
;;; hooks
;;;========================================================
(add-hook 'find-file-hook 'egg-git-dir)
;;;========================================================
;;; Async Git process
;;;========================================================
(defun egg-async-do (exit-code func-args args)
"Run GIT asynchronously with ARGS.
if EXIT code is an exit-code from GIT other than zero but considered
success."
(let ((dir (file-name-directory (egg-git-dir)))
(buf (get-buffer-create "*egg-process*"))
(inhibit-read-only inhibit-read-only)
(accepted-msg (and (integerp exit-code)
(format "exited abnormally with code %d"
exit-code)))
proc)
(setq proc (get-buffer-process buf))
(when (and (processp proc) ;; is a process
(not (eq (process-status proc) 'exit)) ;; not finised
(= (process-exit-status proc) 0)) ;; still running
(error "EGG: %s is already running!" (process-command proc)))
(with-current-buffer buf
(setq inhibit-read-only t)
(setq default-directory dir)
;;(erase-buffer)
(widen)
(goto-char (point-max))
(insert "EGG-GIT-CMD:\n")
(insert (format "%S\n" args))
(insert "EGG-GIT-OUTPUT:\n")
(setq proc (apply 'start-process "egg-git" buf "git" args))
(setq mode-line-process " git")
(when (and (consp func-args) (functionp (car func-args)))
(process-put proc :callback-func (car func-args))
(process-put proc :callback-args (cdr func-args)))
(when (stringp accepted-msg)
(process-put proc :accepted-msg accepted-msg)
(process-put proc :accepted-code exit-code))
(process-put proc :cmds (cons "git" args))
(set-process-sentinel proc #'egg-process-sentinel))))
(defun egg-process-sentinel (proc msg)
(let ((exit-code (process-get proc :accepted-code))
(accepted-msg (process-get proc :accepted-msg))
(callback-func (process-get proc :callback-func))
(callback-args (process-get proc :callback-args))
(cmds (process-get proc :cmds)))
(cond ((string= msg "finished\n")
(message "EGG: git finished."))
((string= msg "killed\n")
(message "EGG: git was killed."))
((string-match accepted-msg msg)
(message "EGG: git exited with code: %d." exit-code))
((string-match "exited abnormally" msg)
(message "EGG: git failed."))
(t (message "EGG: git is weird!")))
(with-current-buffer (process-buffer proc)
(setq mode-line-process nil)
(widen)
(goto-char (point-max))
(re-search-backward "^EGG-GIT-CMD:" nil t)
(narrow-to-region (point) (point-max))
(if (functionp callback-func)
(apply callback-func proc cmds callback-args)))))
;;;========================================================
;;; Diff/Hunk
;;;========================================================
(defconst egg-section-map
(let ((map (make-sparse-keymap "Egg:Section")))
(define-key map (kbd "h") 'egg-section-cmd-toggle-hide-show)
(define-key map (kbd "H") 'egg-section-cmd-toggle-hide-show-children)
map))
(defconst egg-diff-section-map
(let ((map (make-sparse-keymap "Egg:Diff")))
(set-keymap-parent map egg-section-map)
(define-key map (kbd "RET") 'egg-diff-section-cmd-visit-file)
map))
(defconst egg-staged-diff-section-map
(let ((map (make-sparse-keymap "Egg:Diff")))
(set-keymap-parent map egg-diff-section-map)
(define-key map (kbd "s") 'egg-diff-section-cmd-unstage)
map))
(defconst egg-unstaged-diff-section-map
(let ((map (make-sparse-keymap "Egg:Diff")))
(set-keymap-parent map egg-diff-section-map)
(define-key map (kbd "s") 'egg-diff-section-cmd-stage)
(define-key map (kbd "u") 'egg-diff-section-cmd-undo)
map))
(defconst egg-hunk-section-map
(let ((map (make-sparse-keymap "Egg:Hunk")))
(set-keymap-parent map egg-section-map)
(define-key map (kbd "RET") 'egg-hunk-section-cmd-visit-file)
map))
(defconst egg-staged-hunk-section-map
(let ((map (make-sparse-keymap "Egg:Hunk")))
(set-keymap-parent map egg-hunk-section-map)
(define-key map (kbd "s") 'egg-hunk-section-cmd-unstage)
map))
(defconst egg-unstaged-hunk-section-map
(let ((map (make-sparse-keymap "Egg:Hunk")))
(set-keymap-parent map egg-hunk-section-map)
(define-key map (kbd "s") 'egg-hunk-section-cmd-stage)
(define-key map (kbd "u") 'egg-hunk-section-cmd-undo)
map))
(defun list-tp ()
(interactive)
(message "tp: %S" (text-properties-at (point))))
(defun egg-safe-search (re limit &optional no)
(save-excursion
(save-match-data
(and (re-search-forward re limit t)
(match-beginning (or no 0))))))
(defun egg-decorate-diff-header (no)
(put-text-property (match-beginning 0)
(match-end 0)
'display
(propertize (concat "\n" (match-string-no-properties no))
'face
'egg-diff-file-header)))
(defun egg-decorate-diff-index-line (no)
(put-text-property (1- (match-beginning 0))
(match-end 0)
'display
(propertize
(concat "\t-- "
(match-string-no-properties no))
'face 'egg-diff-none)))
(defun egg-decorate-hunk-header (no)
(put-text-property (match-beginning no)
(match-end no)
'face
'egg-diff-hunk-header)
(put-text-property (match-end no)
(match-end 0)
'face
'egg-diff-none))
(defvar egg-invisibility-positions nil)
(defsubst egg-delimit-section (sect-type section beg end
&optional inv-beg
keymap)
(put-text-property beg end :sect-type sect-type)
(put-text-property beg end sect-type section)
(put-text-property beg end :navigation beg)
(when (keymapp keymap)
(put-text-property beg end 'keymap keymap))
(when (integer-or-marker-p inv-beg)
(let ((current-inv (get-text-property inv-beg 'invisible)))
(add-to-list 'current-inv beg t)
(put-text-property inv-beg (1- end) 'invisible current-inv)
(add-to-list 'egg-invisibility-positions beg))))
(defun egg-decorate-diff-sequence (beg end diff-map hunk-map regexp
diff-re-no
hunk-re-no
index-re-no
del-re-no
add-re-no
none-re-no)
(save-match-data
(save-excursion
(let (sub-beg sub-end head-end)
(goto-char beg)
(while (re-search-forward regexp end t)
(setq sub-beg (match-beginning 0))
(cond ((match-beginning del-re-no) ;; del
(put-text-property (match-beginning 0) (match-end 0)
'face 'egg-diff-del))
((match-beginning add-re-no) ;; add
(put-text-property (match-beginning 0) (match-end 0)
'face 'egg-diff-add))
((match-beginning none-re-no) ;; unchanged
(put-text-property (match-beginning 0) (match-end 0)
'face 'egg-diff-none))
((match-beginning hunk-re-no) ;; hunk
(setq sub-end (or (egg-safe-search "^\\(:?diff\\|@@\\)" end)
end))
(egg-decorate-hunk-header hunk-re-no)
(egg-delimit-section
:hunk (list (match-string-no-properties hunk-re-no)
sub-beg sub-end)
sub-beg sub-end (match-end 0) hunk-map))
((match-beginning diff-re-no) ;; diff
(setq sub-end (or (egg-safe-search "^diff " end) end))
(setq head-end (or (egg-safe-search "^@@" end) end))
(egg-decorate-diff-header diff-re-no)
(egg-delimit-section
:diff (list (match-string-no-properties diff-re-no)
sub-beg sub-end head-end)
sub-beg sub-end (match-end 0) diff-map))
((match-beginning index-re-no) ;; index
(egg-decorate-diff-index-line index-re-no))
) ;; cond
) ;; while
nil))))
(defun egg-decorate-diff-section (beg end &optional diff-src-prefix
diff-dst-prefix
diff-map hunk-map)
(let ((a (or diff-src-prefix "a/"))
(b (or diff-dst-prefix "b/"))
regexp)
(setq regexp
(concat "^\\(?:"
"diff --git " a "\\(.+\\) " b ".+\\|" ;1 file
"\\(@@ .+@@\\).*\\|" ;2 hunk
"index \\(.+\\)\\|" ;3 index
"\\(-.*\\)\\|" ;4 del
"\\(\\+.*\\)\\|" ;5 add
"\\( .*\\)" ;6 none
"\\)$"))
(egg-decorate-diff-sequence beg end diff-map hunk-map
regexp 1 2 3 4 5 6)))
(defun egg-diff-section-cmd-visit-file (file)
(interactive (list (car (get-text-property (point) :diff))))
(find-file-other-window file))
(defun egg-hunk-section-cmd-visit-file (file hunk-header hunk-beg hunk-end)
(interactive (cons (car (get-text-property (point) :diff))
(get-text-property (point) :hunk)))
(let ((limit (line-end-position))
(line (string-to-number (nth 2 (split-string hunk-header "[ @,\+,-]+" t))))
(adjust 0))
(save-excursion
(goto-char hunk-beg)
(forward-line 1)
(end-of-line)
(while (re-search-forward "^\\(:?\\+\\| \\).*" limit t)
(setq adjust (1+ adjust))))
(find-file-other-window file)
(goto-line (+ line adjust))))
(defun egg-section-cmd-toggle-hide-show (pos)
(interactive (list (get-text-property (point) :navigation)))
(if (assq pos buffer-invisibility-spec)
(remove-from-invisibility-spec (cons pos t))
(add-to-invisibility-spec (cons pos t)))
(force-window-update (current-buffer)))
(defun egg-section-cmd-toggle-hide-show-children (pos sect-type)
(interactive (list (get-text-property (point) :navigation)
(get-text-property (point) :sect-type)))
(let ((end (next-single-property-change pos sect-type))
child
currently-hidden)
(setq child (next-single-property-change pos :navigation nil end))
(setq currently-hidden (and child
(assq child buffer-invisibility-spec)))
(setq child pos)
(while (< (setq child (next-single-property-change child :navigation nil end)) end)
(if currently-hidden
(remove-from-invisibility-spec (cons child t))
(add-to-invisibility-spec (cons child t))))
(force-window-update (current-buffer))))
(defun egg-diff-section-patch-string (&optional pos)
(let ((diff-info (get-text-property (or pos (point)) :diff)))
(buffer-substring-no-properties (nth 1 diff-info)
(nth 2 diff-info))))
(defun egg-hunk-section-patch-string (&optional pos)
(let ((diff-info (get-text-property (or pos (point)) :diff))
(hunk-info (get-text-property (or pos (point)) :hunk)))
(concat (buffer-substring-no-properties (nth 1 diff-info)
(nth 3 diff-info))
(buffer-substring-no-properties (nth 1 hunk-info)
(nth 2 hunk-info)))))
;;;========================================================
;;; Status Buffer
;;;========================================================
(defun egg-sb-insert-repo-section ()
(let ((head-info (egg-head))
(beg (point)))
(insert (propertize (or (cdr head-info)
(format "Detached HEAD: %s"
(egg-name-rev (car head-info))))
'face 'egg-branch)
"\n"
(propertize (car head-info) 'face 'font-lock-string-face)
"\n"
(propertize (egg-git-dir) 'face 'font-lock-constant-face)
"\n")
(call-process "git" nil t nil
"log" "--max-count=5"
"--abbrev-commit" "--pretty=oneline")
(egg-delimit-section :section 'repo beg (point))))
(defun egg-sb-insert-untracked-section ()
(let ((beg (point)) inv-beg)
(insert (egg-prepend "Untracked Files:" "\n\n"
'face 'egg-section-title)
(progn (setq inv-beg (point))
"\n"))
(call-process "git" nil t nil "ls-files" "--others"
"--exclude-standard")
(egg-delimit-section :section 'untracked beg (point)
inv-beg egg-section-map)))
(defun egg-sb-insert-unstaged-section (title &rest extra-diff-options)
(let ((beg (point)) inv-beg)
(insert (egg-prepend title "\n\n" 'face 'egg-section-title)
(progn (setq inv-beg (point))
"\n"))
(apply 'call-process "git" nil t nil "diff" "--no-color" "-p"
"--src-prefix=INDEX/" "--dst-prefix=WORKDIR/"
extra-diff-options)
(egg-delimit-section :section 'unstaged beg (point)
inv-beg egg-section-map)
(egg-decorate-diff-section beg (point) "INDEX/" "WORKDIR/"
egg-unstaged-diff-section-map
egg-unstaged-hunk-section-map)))
(defun egg-sb-insert-staged-section (title &rest extra-diff-options)
(let ((beg (point)) inv-beg)
(insert (egg-prepend title "\n\n"
'face 'egg-section-title)
(progn (setq inv-beg (point))
"\n"))
(apply 'call-process "git" nil t nil "diff" "--no-color" "--cached" "-p"
"--src-prefix=HEAD/" "--dst-prefix=INDEX/"
extra-diff-options)
(egg-delimit-section :section 'staged beg (point)
inv-beg egg-section-map)
(egg-decorate-diff-section beg (point) "HEAD/" "INDEX/"
egg-staged-diff-section-map
egg-staged-hunk-section-map)))
(defun egg-update-status-buffer (buffer &optional update-display-p)
(with-current-buffer buffer
(let ((inhibit-read-only t))
(erase-buffer)
(set (make-local-variable 'egg-invisibility-positions) nil)
(setq buffer-invisibility-spec nil)
(egg-sb-insert-repo-section)
(egg-sb-insert-unstaged-section "Unstaged Changes:")
(egg-sb-insert-staged-section "Staged Changes:")
(egg-sb-insert-untracked-section)
(when update-display-p
(force-window-update (current-buffer)))
(goto-char (point-min))
(current-buffer))))
(defun egg-update-status-buffer-disp (buf)
(egg-update-status-buffer buf t)
(goto-char (point-min)))
(defun egg-status-buffer-cmd-refresh ()
(interactive)
(egg-update-status-buffer-disp (current-buffer)))
(defun egg-status-buffer-cmd-navigate-next ()
(interactive)
(goto-char (or (next-single-property-change (point) :navigation)
(point))))
(defun egg-status-buffer-cmd-navigate-prev ()
(interactive)
(goto-char (previous-single-property-change (point) :navigation
nil (point-min))))
(defconst egg-status-buffer-mode-map
(let ((map (make-sparse-keymap "Egg:StatusBuffer")))
(define-key map (kbd "q") 'quit-window)
(define-key map (kbd "g") 'egg-status-buffer-cmd-refresh)
(define-key map (kbd "n") 'egg-status-buffer-cmd-navigate-next)
(define-key map (kbd "p") 'egg-status-buffer-cmd-navigate-prev)
map))
(defun egg-status-buffer-mode ()
"Major mode to display the status buffer."
(kill-all-local-variables)
(setq buffer-read-only t)
(setq major-mode 'egg-status-buffer-mode
mode-name "Egg:Status"
mode-line-process ""
truncate-lines t)
(use-local-map egg-status-buffer-mode-map)
(setq buffer-invisibility-spec nil)
(run-mode-hooks 'egg-status-buffer-mode-hook))
(defvar egg-buffer-update-func nil)
(defun egg-get-status-buffer-create (&optional init-p)
(let* ((git-dir (egg-git-dir))
(dir (file-name-directory git-dir))
(dir-name (file-name-nondirectory (directory-file-name dir)))
(buf-name (format "*%s@egg:%s*" dir-name dir))
(default-directory dir)
(buf (get-buffer buf-name)))
(when (or (null (prog1 buf (setq buf (get-buffer-create buf-name))))
init-p)
(with-current-buffer buf
(egg-status-buffer-mode)
(setq egg-buffer-update-func 'egg-update-status-buffer-disp)))
buf))
(defun egg-status (&optional no-update-p)
(interactive "P")
(let ((buf (egg-get-status-buffer-create)))
(unless no-update-p
(egg-update-status-buffer buf t))
(display-buffer buf t)))
;;;========================================================
;;; action
;;;========================================================
(defun egg-revert-visited-files (file-or-files)
(let* ((git-dir (egg-git-dir))
(default-directory (file-name-directory git-dir))
(files (if (consp file-or-files)
file-or-files
(list file-or-files))))
(mapcar (lambda (file)
(let ((buf (get-file-buffer file)))
(when (bufferp buf)
(with-current-buffer buf
(when (equal (egg-git-dir) git-dir)
(revert-buffer t t t))))))
files)))
(defun egg-revert-all-visited-files ()
(let* ((git-dir (egg-git-dir))
(default-directory (file-name-directory git-dir))
bufs files)
(setq files
(delq nil (mapcar (lambda (buf)
(with-current-buffer buf
(when (and (buffer-file-name buf)
(equal (egg-git-dir) git-dir))
(buffer-file-name buf))))
(buffer-list))))
(when (consp files)
(setq files (mapcar 'expand-file-name
(apply 'egg-git-to-lines "ls-files" files)))
(when (consp files)
(egg-revert-visited-files files)))))
(defun egg-log-buffer ()
(or (get-buffer (concat " *egg-logs@" (egg-git-dir) "*"))
(let ((git-dir (egg-git-dir))
(default-directory default-directory)
dir)
(unless git-dir
(error "Can't find git dir in %s" default-directory))
(setq dir (file-name-nondirectory git-dir))
(setq default-directory dir)
(get-buffer-create (concat " *egg-logs@" git-dir "*")))))
(defsubst egg-log (&rest strings)
(with-current-buffer (egg-log-buffer)
(goto-char (point-max))
(cons (current-buffer)
(prog1 (point)
(apply 'insert (current-time-string) "\n" strings)))))
(defun egg-sync-handle-exit-code (ret accepted-codes output)
(egg-log (format "RET:%d\n" ret))
(if (listp accepted-codes)
(setq accepted-codes (cons 0 accepted-codes))
(setq accepted-codes (list 0 accepted-codes)))
(if (null (memq ret accepted-codes))
(with-current-buffer (car output)
(widen)
(narrow-to-region (cdr output) (point-max))
(display-buffer (current-buffer) t)
nil)
(if (functionp egg-buffer-update-func)
(funcall egg-buffer-update-func (current-buffer)))
ret))
(defun egg-sync-do (program stdin accepted-codes args)
(let (output ret)
(setq output (egg-log program " " (mapconcat 'identity args " ")
(if (stringp stdin) " <REGION\n" "\n")))
(setq ret
(cond ((stringp stdin)
(with-temp-buffer
(insert stdin)
(apply 'call-process-region (point-min) (point-max)
program nil (car output) nil args)))
((consp stdin)
(apply 'call-process-region (car stdin) (cdr stdin)
program nil (car output) nil args))
((null stdin)
(apply 'call-process program nil (car output) nil args))))
(egg-sync-handle-exit-code ret accepted-codes output)))
(defsubst egg-sync-do-region-0 (program beg end args)
(egg-sync-do program (cons beg end) nil args))
(defsubst egg-sync-do-region (program beg end &rest args)
(egg-sync-do program (cons beg end) nil args))
(defsubst egg-sync-git-region (beg end &rest args)
(egg-sync-do "git" (cons beg end) nil args))
(defun egg-sync-do-file (file program stdin accepted-codes args)
(let ((default-directory (file-name-directory (egg-git-dir))))
(setq file (expand-file-name file))
(setq args (mapcar (lambda (word)
(if (string= word file) file word))
args))
(if (egg-sync-do program stdin accepted-codes args)
file)))
(defun egg-hunk-section-patch-cmd (pos program &rest args)
(let ((patch (egg-hunk-section-patch-string pos))
(file (car (get-text-property pos :diff))))
(unless (stringp file)
(error "No diff with file-name here!"))
(egg-sync-do-file file program patch nil args)))
(defun egg-hunk-section-cmd-stage (pos)
(interactive (list (point)))
(egg-hunk-section-patch-cmd pos "git" "apply" "--cached"))
(defun egg-hunk-section-cmd-unstage (pos)
(interactive (list (point)))
(egg-hunk-section-patch-cmd pos "git" "apply" "--cached" "--reverse"))
(defun egg-hunk-section-cmd-undo (pos)
(interactive (list (point)))
(let ((file (egg-hunk-section-patch-cmd pos "patch"
"-p1" "--quiet" "--reverse")))
(when (stringp file)
(egg-revert-visited-files file))))
(defun egg-diff-section-patch-cmd (pos accepted-codes &rest args)
(let ((file (car (get-text-property pos :diff))))
(unless (stringp file)
(error "No diff with file-name here!"))
(egg-sync-do-file file "git" nil accepted-codes
(append args (list file)))))
(defun egg-diff-section-cmd-stage (pos)
(interactive (list (point)))
(egg-diff-section-patch-cmd pos nil "add"))
(defun egg-diff-section-cmd-unstage (pos)
(interactive (list (point)))
(egg-diff-section-patch-cmd pos 1 "reset" "HEAD" "--"))
(defun egg-diff-section-cmd-undo (pos)
(interactive (list (point)))
(let ((file (egg-diff-section-patch-cmd pos nil "reset" "HEAD" "--")))
(when (stringp file)
(egg-revert-visited-files file))))
;;;========================================================
;;; log message
;;;========================================================
(require 'derived)
(require 'ring)
(defvar egg-log-msg-ring (make-ring 32))
(defvar egg-log-msg-ring-idx nil)
(defvar egg-log-msg-action nil)
(defvar egg-log-msg-text-beg nil)
(defvar egg-log-msg-text-end nil)
(defvar egg-log-msg-diff-beg nil)
(define-derived-mode egg-log-msg-mode text-mode "Egg:LogMsg"
"Major mode for editing Git log message.\n\n
\{egg-log-msg-mode-map}."
(setq default-directory (file-name-directory (egg-git-dir)))
(make-local-variable 'egg-log-msg-action)
(set (make-local-variable 'egg-log-msg-ring-idx) nil)
(set (make-local-variable 'egg-log-msg-text-beg) nil)
(set (make-local-variable 'egg-log-msg-text-end) nil)
(set (make-local-variable 'egg-log-msg-diff-beg) nil))
(define-key egg-log-msg-mode-map (kbd "C-c C-c") 'egg-log-msg-done)
(define-key egg-log-msg-mode-map (kbd "M-p") 'egg-log-msg-older-text)
(define-key egg-log-msg-mode-map (kbd "M-n") 'egg-log-msg-newer-text)
(define-key egg-log-msg-mode-map (kbd "C-l") 'egg-commit-log-cmd-update)
(defun egg-commit ()
(when (egg-sync-git-region egg-log-msg-text-beg egg-log-msg-text-end
"commit" "-F" "-")
(egg-update-status-buffer (egg-get-status-buffer-create) t)))
(defun egg-log-msg-done ()
(interactive)
(widen)
(goto-char egg-log-msg-text-beg)
(if (save-excursion (re-search-forward "\\sw\\|\\-"
egg-log-msg-text-end t))
(when (functionp egg-log-msg-action)
(ring-insert egg-log-msg-ring
(buffer-substring-no-properties egg-log-msg-text-beg
egg-log-msg-text-end))
(funcall egg-log-msg-action))
(message "Please enter a log message!")
(ding)))
(defun egg-log-msg-hist-cycle (&optional forward-p)
"Cycle through message log history."
(let ((len (ring-length egg-log-msg-ring)))
(cond ((<= len 0)
;; no history
(message "No previous log message.")
(ding))
;; don't accidentally throw away unsaved text
((and (null egg-log-msg-ring-idx)
(> egg-log-msg-text-end egg-log-msg-text-beg)
(not (y-or-n-p "throw away current text? "))))
;; do it
(t (delete-region egg-log-msg-text-beg egg-log-msg-text-end)
(setq egg-log-msg-ring-idx
(if (null egg-log-msg-ring-idx)
(if forward-p
;; 1st-time + fwd = oldest
(ring-minus1 0 len)
;; 1st-time + bwd = newest
0)
(if forward-p
;; newer
(ring-minus1 egg-log-msg-ring-idx len)
;; older
(ring-plus1 egg-log-msg-ring-idx len))))
(goto-char egg-log-msg-text-beg)
(insert (ring-ref egg-log-msg-ring egg-log-msg-ring-idx))))))
(defun egg-log-msg-older-text ()
"Cycle backward through comment history."
(interactive)
(egg-log-msg-hist-cycle))
(defun egg-log-msg-newer-text ()
"Cycle forward through comment history."
(interactive)
(egg-log-msg-hist-cycle t))
(defun egg-commit-log-insert-diff (buf)
(with-current-buffer buf
(let ((inhibit-read-only t)
beg)
(goto-char egg-log-msg-diff-beg)
(delete-region (point) (point-max))
(setq beg (point))
(egg-sb-insert-staged-section "Changes to Commit:" "--stat")
(egg-sb-insert-unstaged-section "Deferred Changes:")
(egg-sb-insert-untracked-section)
(put-text-property beg (point) 'read-only t)
(put-text-property beg (point) 'front-sticky nil)
(force-window-update buf))))
(defun egg-commit-log-update (buf)
(egg-commit-log-insert-diff buf)
(egg-update-status-buffer (egg-get-status-buffer-create)))
(defun egg-commit-log-cmd-update ()
(interactive)
(save-excursion
(egg-commit-log-update (current-buffer)))
(recenter))
(defun egg-commit-log-edit ()
(interactive)
(let* ((git-dir (egg-git-dir))
(default-directory (file-name-directory git-dir))
(buf (get-buffer-create (concat "*commit@egg:" git-dir "*")))
(head-info (egg-head))
(head (or (cdr head-info)
(format "Detached HEAD! (%s)" (car head-info))))
(inhibit-read-only inhibit-read-only)
tmp)
(pop-to-buffer buf)
(setq inhibit-read-only t)