forked from magit/magit
/
magit-section.el
2476 lines (2191 loc) · 101 KB
/
magit-section.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
;;; magit-section.el --- Sections for read-only buffers -*- lexical-binding:t; coding:utf-8 -*-
;; Copyright (C) 2008-2024 The Magit Project Contributors
;; Author: Jonas Bernoulli <jonas@bernoul.li>
;; Maintainer: Jonas Bernoulli <jonas@bernoul.li>
;; Homepage: https://github.com/magit/magit
;; Keywords: tools
;; Package-Version: 3.3.0.50-git
;; Package-Requires: (
;; (emacs "25.1")
;; (compat "29.1.4.4")
;; (dash "2.19.1")
;; (seq "2.24"))
;; SPDX-License-Identifier: GPL-3.0-or-later
;; Magit 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.
;;
;; Magit 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 Magit. If not, see <https://www.gnu.org/licenses/>.
;; You should have received a copy of the AUTHORS.md file, which
;; lists all contributors. If not, see https://magit.vc/authors.
;;; Commentary:
;; This package implements the main user interface of Magit — the
;; collapsible sections that make up its buffers. This package used
;; to be distributed as part of Magit but now it can also be used by
;; other packages that have nothing to do with Magit or Git.
;;; Code:
(require 'cl-lib)
(require 'compat)
(require 'dash)
(require 'eieio)
(require 'subr-x)
;; For older Emacs releases we depend on an updated `seq' release from GNU
;; ELPA, for `seq-keep'. Unfortunately something else may require `seq'
;; before `package' had a chance to put this version on the `load-path'.
(when (and (featurep' seq)
(not (fboundp 'seq-keep)))
(unload-feature 'seq 'force))
(require 'seq)
;; Furthermore, by default `package' just silently refuses to upgrade.
(defconst magit--core-upgrade-instructions "\
Magit requires `%s' >= %s,
but due to bad defaults, Emacs' package manager, refuses to
upgrade this and other built-in packages to higher releases
from GNU Elpa.
To fix this, you have to add this to your init file:
(setq package-install-upgrade-built-in t)
Then evaluate that expression by placing the cursor after it
and typing \\[eval-last-sexp].
Once you have done that, you have to explicitly upgrade `%s':
\\[package-install] %s \\`RET'
Then you also must make sure the updated version is loaded,
by evaluating this form:
(progn (unload-feature \\='%s t) (require \\='%s))
If this does not work, then try uninstalling Magit and all of its
dependencies. After that exit and restart Emacs, and only then
reinstalling Magit.
If you don't use the `package' package manager but still get
this warning, then your chosen package manager likely has a
similar defect.")
(unless (fboundp 'seq-keep)
(display-warning 'magit (substitute-command-keys
(format magit--core-upgrade-instructions
'seq "2.24" 'seq 'seq 'seq 'seq))
:emergency))
(require 'cursor-sensor)
(require 'format-spec)
(eval-when-compile (require 'benchmark))
;; For `magit-section-get-relative-position'
(declare-function magit-hunk-section-p "magit-diff" (section) t)
;;; Hooks
(defvar magit-section-movement-hook nil
"Hook run by `magit-section-goto'.
That function in turn is used by all section movement commands.")
(defvar magit-section-highlight-hook
'(magit-section-highlight
magit-section-highlight-selection)
"Functions used to highlight the current section.
Each function is run with the current section as only argument
until one of them returns non-nil.")
(defvar magit-section-unhighlight-hook nil
"Functions used to unhighlight the previously current section.
Each function is run with the current section as only argument
until one of them returns non-nil. Most sections are properly
unhighlighted without requiring a specialized unhighlighter,
diff-related sections being the only exception.")
(defvar magit-section-set-visibility-hook
'(magit-section-cached-visibility)
"Hook used to set the initial visibility of a section.
Stop at the first function that returns non-nil. The returned
value should be `show', `hide' or nil. If no function returns
non-nil, determine the visibility as usual, i.e., use the
hardcoded section specific default (see `magit-insert-section').")
;;; Options
(defgroup magit-section nil
"Expandable sections."
:link '(info-link "(magit)Sections")
:group 'extensions)
(defcustom magit-section-show-child-count t
"Whether to append the number of children to section headings.
This only applies to sections for which doing so makes sense."
:package-version '(magit-section . "2.1.0")
:group 'magit-section
:type 'boolean)
(defcustom magit-section-cache-visibility t
"Whether to cache visibility of sections.
Sections always retain their visibility state when they are being
recreated during a refresh. But if a section disappears and then
later reappears again, then this option controls whether this is
the case.
If t, then cache the visibility of all sections. If a list of
section types, then only do so for matching sections. If nil,
then don't do so for any sections."
:package-version '(magit-section . "2.12.0")
:group 'magit-section
:type '(choice (const :tag "Don't cache visibility" nil)
(const :tag "Cache visibility of all sections" t)
(repeat :tag "Cache visibility for section types" symbol)))
(defcustom magit-section-initial-visibility-alist
'((stashes . hide))
"Alist controlling the initial visibility of sections.
Each element maps a section type or lineage to the initial
visibility state for such sections. The state has to be one of
`show' or `hide', or a function that returns one of these symbols.
A function is called with the section as the only argument.
Use the command `magit-describe-section' to determine a section's
lineage or type. The vector in the output is the section lineage
and the type is the first element of that vector. Wildcards can
be used, see `magit-section-match'.
Currently this option is only used to override hardcoded defaults,
but in the future it will also be used set the defaults.
An entry whose key is `magit-status-initial-section' specifies
the visibility of the section `magit-status-goto-initial-section'
jumps to. This does not only override defaults, but also other
entries of this alist."
:package-version '(magit-section . "2.12.0")
:group 'magit-section
:type '(alist :key-type (sexp :tag "Section type/lineage")
:value-type (choice (const hide)
(const show)
function)))
(defcustom magit-section-visibility-indicator
(if (window-system)
'(magit-fringe-bitmap> . magit-fringe-bitmapv)
(cons (if (char-displayable-p ?…) "…" "...")
t))
"Whether and how to indicate that a section can be expanded/collapsed.
If nil, then don't show any indicators.
Otherwise the value has to have one of these two forms:
\(EXPANDABLE-BITMAP . COLLAPSIBLE-BITMAP)
Both values have to be variables whose values are fringe
bitmaps. In this case every section that can be expanded or
collapsed gets an indicator in the left fringe.
To provide extra padding around the indicator, set
`left-fringe-width' in `magit-mode-hook'.
\(STRING . BOOLEAN)
In this case STRING (usually an ellipsis) is shown at the end
of the heading of every collapsed section. Expanded sections
get no indicator. The cdr controls whether the appearance of
these ellipsis take section highlighting into account. Doing
so might potentially have an impact on performance, while not
doing so is kinda ugly."
:package-version '(magit-section . "3.0.0")
:group 'magit-section
:type '(choice (const :tag "No indicators" nil)
(cons :tag "Use +- fringe indicators"
(const magit-fringe-bitmap+)
(const magit-fringe-bitmap-))
(cons :tag "Use >v fringe indicators"
(const magit-fringe-bitmap>)
(const magit-fringe-bitmapv))
(cons :tag "Use bold >v fringe indicators)"
(const magit-fringe-bitmap-bold>)
(const magit-fringe-bitmap-boldv))
(cons :tag "Use custom fringe indicators"
(variable :tag "Expandable bitmap variable")
(variable :tag "Collapsible bitmap variable"))
(cons :tag "Use ellipses at end of headings"
(string :tag "Ellipsis" "…")
(choice :tag "Use face kludge"
(const :tag "Yes (potentially slow)" t)
(const :tag "No (kinda ugly)" nil)))))
(define-obsolete-variable-alias 'magit-keep-region-overlay
'magit-section-keep-region-overlay "Magit-Section 4.0.0")
(defcustom magit-section-keep-region-overlay nil
"Whether to keep the region overlay when there is a valid selection.
By default Magit removes the regular region overlay if, and only
if, that region constitutes a valid selection as understood by
Magit commands. Otherwise it does not remove that overlay, and
the region looks like it would in other buffers.
There are two types of such valid selections: hunk-internal
regions and regions that select two or more sibling sections.
In such cases Magit removes the region overlay and instead
highlights a slightly larger range. All text (for hunk-internal
regions) or the headings of all sections (for sibling selections)
that are inside that range (not just inside the region) are acted
on by commands such as the staging command. This buffer range
begins at the beginning of the line on which the region begins
and ends at the end of the line on which the region ends.
Because Magit acts on this larger range and not the region, it is
actually quite important to visualize that larger range. If we
don't do that, then one might think that these commands act on
the region instead. If you want to *also* visualize the region,
then set this option to t. But please note that when the region
does *not* constitute a valid selection, then the region is
*always* visualized as usual, and that it is usually under such
circumstances that you want to use a non-magit command to act on
the region.
Besides keeping the region overlay, setting this option to t also
causes all face properties, except for `:foreground', to be
ignored for the faces used to highlight headings of selected
sections. This avoids the worst conflicts that result from
displaying the region and the selection overlays at the same
time. We are not interested in dealing with other conflicts.
In fact we *already* provide a way to avoid all of these
conflicts: *not* changing the value of this option.
It should be clear by now that we consider it a mistake to set
this to display the region when the Magit selection is also
visualized, but since it has been requested a few times and
because it doesn't cost much to offer this option we do so.
However that might change. If the existence of this option
starts complicating other things, then it will be removed."
:package-version '(magit-section . "2.3.0")
:group 'magit-section
:type 'boolean)
(defcustom magit-section-disable-line-numbers t
"In Magit buffers, whether to disable modes that display line numbers.
Some users who turn on `global-display-line-numbers-mode' (or
`global-nlinum-mode' or `global-linum-mode') expect line numbers
to be displayed everywhere except in Magit buffers. Other users
do not expect Magit buffers to be treated differently. At least
in theory users in the first group should not use the global mode,
but that ship has sailed, thus this option."
:package-version '(magit-section . "3.0.0")
:group 'magit-section
:type 'boolean)
(defcustom magit-section-show-context-menu-for-emacs<28 nil
"Whether `mouse-3' shows a context menu for Emacs < 28.
This has to be set before loading `magit-section' or it has
no effect. This also has no effect for Emacs >= 28, where
`context-menu-mode' should be enabled instead."
:package-version '(magit-section . "4.0.0")
:group 'magit-section
:type 'boolean)
;;; Variables
(defvar-local magit-section-preserve-visibility t)
(defvar-local magit-section-pre-command-region-p nil)
(defvar-local magit-section-pre-command-section nil)
(defvar-local magit-section-highlight-force-update nil)
(defvar-local magit-section-highlight-overlays nil)
(defvar-local magit-section-highlighted-sections nil)
(defvar-local magit-section-unhighlight-sections nil)
(defvar-local magit-section-inhibit-markers nil)
(defvar-local magit-section-insert-in-reverse nil)
;;; Faces
(defgroup magit-section-faces nil
"Faces used by Magit-Section."
:group 'magit-section
:group 'faces)
(defface magit-section-highlight
`((((class color) (background light))
,@(and (>= emacs-major-version 27) '(:extend t))
:background "grey95")
(((class color) (background dark))
,@(and (>= emacs-major-version 27) '(:extend t))
:background "grey20"))
"Face for highlighting the current section."
:group 'magit-section-faces)
(defface magit-section-heading
`((((class color) (background light))
,@(and (>= emacs-major-version 27) '(:extend t))
:foreground "DarkGoldenrod4"
:weight bold)
(((class color) (background dark))
,@(and (>= emacs-major-version 27) '(:extend t))
:foreground "LightGoldenrod2"
:weight bold))
"Face for section headings."
:group 'magit-section-faces)
(defface magit-section-secondary-heading
`((t ,@(and (>= emacs-major-version 27) '(:extend t))
:weight bold))
"Face for section headings of some secondary headings."
:group 'magit-section-faces)
(defface magit-section-heading-selection
`((((class color) (background light))
,@(and (>= emacs-major-version 27) '(:extend t))
:foreground "salmon4")
(((class color) (background dark))
,@(and (>= emacs-major-version 27) '(:extend t))
:foreground "LightSalmon3"))
"Face for selected section headings."
:group 'magit-section-faces)
(defface magit-section-child-count '((t nil))
"Face used for child counts at the end of some section headings."
:group 'magit-section-faces)
;;; Classes
(defvar magit--current-section-hook nil
"Internal variable used for `magit-describe-section'.")
(defvar magit--section-type-alist nil)
(defclass magit-section ()
((keymap :initform nil)
(type :initform nil :initarg :type)
(value :initform nil :initarg :value)
(start :initform nil :initarg :start)
(content :initform nil)
(end :initform nil)
(hidden :initform nil)
(washer :initform nil)
(process :initform nil)
(heading-highlight-face :initform nil)
(inserter :initform (symbol-value 'magit--current-section-hook))
(parent :initform nil :initarg :parent)
(children :initform nil)))
;;; Mode
(defvar symbol-overlay-inhibit-map)
(defvar-keymap magit-section-heading-map
:doc "Keymap used in the heading line of all expandable sections.
This keymap is used in addition to the section-specific keymap,
if any."
"<double-down-mouse-1>" #'ignore
"<double-mouse-1>" #'magit-mouse-toggle-section
"<double-mouse-2>" #'magit-mouse-toggle-section)
(defvar magit-section-mode-map
(let ((map (make-keymap)))
(suppress-keymap map t)
(when (and magit-section-show-context-menu-for-emacs<28
(< emacs-major-version 28))
(keymap-set map "<mouse-3>" nil)
(keymap-set
map "<down-mouse-3>"
`( menu-item "" ,(make-sparse-keymap)
:filter ,(lambda (_)
(let ((menu (make-sparse-keymap)))
(if (fboundp 'context-menu-local)
(context-menu-local menu last-input-event)
(magit--context-menu-local menu last-input-event))
(magit-section-context-menu menu last-input-event)
menu)))))
(keymap-set map "<left-fringe> <mouse-1>" #'magit-mouse-toggle-section)
(keymap-set map "<left-fringe> <mouse-2>" #'magit-mouse-toggle-section)
(keymap-set map "TAB" #'magit-section-toggle)
(keymap-set map "C-c TAB" #'magit-section-cycle)
(keymap-set map "C-<tab>" #'magit-section-cycle)
(keymap-set map "M-<tab>" #'magit-section-cycle)
;; <backtab> is the most portable binding for Shift+Tab.
(keymap-set map "<backtab>" #'magit-section-cycle-global)
(keymap-set map "^" #'magit-section-up)
(keymap-set map "p" #'magit-section-backward)
(keymap-set map "n" #'magit-section-forward)
(keymap-set map "M-p" #'magit-section-backward-sibling)
(keymap-set map "M-n" #'magit-section-forward-sibling)
(keymap-set map "1" #'magit-section-show-level-1)
(keymap-set map "2" #'magit-section-show-level-2)
(keymap-set map "3" #'magit-section-show-level-3)
(keymap-set map "4" #'magit-section-show-level-4)
(keymap-set map "M-1" #'magit-section-show-level-1-all)
(keymap-set map "M-2" #'magit-section-show-level-2-all)
(keymap-set map "M-3" #'magit-section-show-level-3-all)
(keymap-set map "M-4" #'magit-section-show-level-4-all)
map)
"Parent keymap for all keymaps of modes derived from `magit-section-mode'.")
(define-derived-mode magit-section-mode special-mode "Magit-Sections"
"Parent major mode from which major modes with Magit-like sections inherit.
Magit-Section is documented in info node `(magit-section)'."
:group 'magit-section
(buffer-disable-undo)
(setq truncate-lines t)
(setq buffer-read-only t)
(setq-local line-move-visual t) ; see #1771
;; Turn off syntactic font locking, but not by setting
;; `font-lock-defaults' because that would enable font locking, and
;; not all magit plugins may be ready for that (see #3950).
(setq-local font-lock-syntactic-face-function #'ignore)
(setq show-trailing-whitespace nil)
(setq-local symbol-overlay-inhibit-map t)
(setq list-buffers-directory (abbreviate-file-name default-directory))
;; (hack-dir-local-variables-non-file-buffer)
(make-local-variable 'text-property-default-nonsticky)
(push (cons 'keymap t) text-property-default-nonsticky)
(add-hook 'pre-command-hook #'magit-section-pre-command-hook nil t)
(add-hook 'post-command-hook #'magit-section-post-command-hook t t)
(add-hook 'deactivate-mark-hook #'magit-section-deactivate-mark t t)
(setq-local redisplay-highlight-region-function
#'magit-section--highlight-region)
(setq-local redisplay-unhighlight-region-function
#'magit-section--unhighlight-region)
(add-function :filter-return (local 'filter-buffer-substring-function)
#'magit-section--remove-text-properties)
(when (fboundp 'magit-section-context-menu)
(add-hook 'context-menu-functions #'magit-section-context-menu 10 t))
(when magit-section-disable-line-numbers
(when (and (fboundp 'linum-mode)
(bound-and-true-p global-linum-mode))
(linum-mode -1))
(when (and (fboundp 'nlinum-mode)
(bound-and-true-p global-nlinum-mode))
(nlinum-mode -1))
(when (and (fboundp 'display-line-numbers-mode)
(bound-and-true-p global-display-line-numbers-mode))
(display-line-numbers-mode -1)))
(when (fboundp 'magit-preserve-section-visibility-cache)
(add-hook 'kill-buffer-hook #'magit-preserve-section-visibility-cache)))
(defun magit-section--remove-text-properties (string)
"Remove all text-properties from STRING.
Most importantly `magit-section'."
(set-text-properties 0 (length string) nil string)
string)
;;; Core
(defvar-local magit-root-section nil
"The root section in the current buffer.
All other sections are descendants of this section. The value
of this variable is set by `magit-insert-section' and you should
never modify it.")
(put 'magit-root-section 'permanent-local t)
(defvar-local magit--context-menu-section nil "For internal use only.")
(defvar magit--context-menu-buffer nil "For internal use only.")
(defun magit-point ()
"Return point or the position where the context menu was invoked.
When using the context menu, return the position the user clicked
on, provided the current buffer is the buffer in which the click
occurred. Otherwise return the same value as `point'."
(if magit--context-menu-section
(magit-menu-position)
(point)))
(defun magit-thing-at-point (thing &optional no-properties)
"Return the THING at point or where the context menu was invoked.
When using the context menu, return the thing the user clicked
on, provided the current buffer is the buffer in which the click
occurred. Otherwise return the same value as `thing-at-point'.
For the meaning of THING and NO-PROPERTIES see that function."
(if-let ((pos (magit-menu-position)))
(save-excursion
(goto-char pos)
(thing-at-point thing no-properties))
(thing-at-point thing no-properties)))
(defun magit-current-section ()
"Return the section at point or where the context menu was invoked.
When using the context menu, return the section that the user
clicked on, provided the current buffer is the buffer in which
the click occurred. Otherwise return the section at point."
(or magit--context-menu-section
(magit-section-at)
magit-root-section))
(defun magit-section-at (&optional position)
"Return the section at POSITION, defaulting to point."
(get-text-property (or position (point)) 'magit-section))
(defun magit-section-ident (section)
"Return an unique identifier for SECTION.
The return value has the form ((TYPE . VALUE)...)."
(cons (cons (oref section type)
(magit-section-ident-value section))
(and-let* ((parent (oref section parent)))
(magit-section-ident parent))))
(cl-defgeneric magit-section-ident-value (object)
"Return OBJECT's value, making it constant and unique if necessary.
This is used to correlate different incarnations of the same
section, see `magit-section-ident' and `magit-get-section'.
Sections whose values that are not constant and/or unique should
implement a method that return a value that can be used for this
purpose.")
(cl-defmethod magit-section-ident-value ((section magit-section))
"Return the value unless it is an object.
Different object incarnations representing the same value then to
not be equal, so call this generic function on the object itself
to determine a constant value."
(let ((value (oref section value)))
(if (eieio-object-p value)
(magit-section-ident-value value)
value)))
(cl-defmethod magit-section-ident-value ((object eieio-default-superclass))
"Simply return the object itself. That likely isn't
good enough, so you need to implement your own method."
object)
(defun magit-get-section (ident &optional root)
"Return the section identified by IDENT.
IDENT has to be a list as returned by `magit-section-ident'.
If optional ROOT is non-nil, then search in that section tree
instead of in the one whose root `magit-root-section' is."
(setq ident (reverse ident))
(let ((section (or root magit-root-section)))
(when (eq (car (pop ident))
(oref section type))
(while (and ident
(pcase-let ((`(,type . ,value) (car ident)))
(setq section
(cl-find-if
(lambda (section)
(and (eq (oref section type) type)
(equal (magit-section-ident-value section)
value)))
(oref section children)))))
(pop ident))
section)))
(defun magit-section-lineage (section &optional raw)
"Return the lineage of SECTION.
If optional RAW is non-nil, return a list of section object
beginning with SECTION, otherwise return a list of section
types."
(cons (if raw section (oref section type))
(and-let* ((parent (oref section parent)))
(magit-section-lineage parent raw))))
(defvar magit-insert-section--current nil "For internal use only.")
(defvar magit-insert-section--parent nil "For internal use only.")
(defvar magit-insert-section--oldroot nil "For internal use only.")
;;; Menu
(defvar magit-menu-common-value nil "See function `magit-menu-common-value'.")
(defvar magit-menu--desc-values nil "For internal use only.")
(defun magit-section-context-menu (menu click)
"Populate MENU with Magit-Section commands at CLICK."
(when-let ((section (save-excursion
(unless (region-active-p)
(mouse-set-point click))
(magit-section-at))))
(unless (region-active-p)
(setq magit--context-menu-buffer (current-buffer))
(if-let ((alt (save-excursion
(mouse-set-point click)
(run-hook-with-args-until-success
'magit-menu-alternative-section-hook section))))
(setq magit--context-menu-section (setq section alt))
(setq magit--context-menu-section section)
(magit-section-update-highlight t)))
(when (magit-section-content-p section)
(keymap-set-after menu "<magit-section-toggle>"
`(menu-item
,(if (oref section hidden) "Expand section" "Collapse section")
magit-section-toggle))
(unless (oref section hidden)
(when-let ((children (oref section children)))
(when (seq-some #'magit-section-content-p children)
(when (seq-some (lambda (c) (oref c hidden)) children)
(keymap-set-after menu "<magit-section-show-children>"
`(menu-item "Expand children"
magit-section-show-children)))
(when (seq-some (lambda (c) (not (oref c hidden))) children)
(keymap-set-after menu "<magit-section-hide-children>"
`(menu-item "Collapse children"
magit-section-hide-children))))))
(keymap-set-after menu "<separator-magit-1>" menu-bar-separator))
(keymap-set-after menu "<magit-describe-section>"
`(menu-item "Describe section" magit-describe-section))
(when-let ((map (oref section keymap)))
(keymap-set-after menu "<separator-magit-2>" menu-bar-separator)
(when (symbolp map)
(setq map (symbol-value map)))
(setq magit-menu-common-value (magit-menu-common-value section))
(setq magit-menu--desc-values (magit-menu--desc-values section))
(map-keymap (lambda (key binding)
(when (consp binding)
(define-key-after menu (vector key)
(copy-sequence binding))))
(if (fboundp 'menu-bar-keymap)
(menu-bar-keymap map)
(magit--menu-bar-keymap map)))))
menu)
(defun magit-menu-item (desc def &optional props)
"Return a menu item named DESC binding DEF and using PROPS.
If DESC contains a supported %-spec, substitute the
expression (magit-menu-format-desc DESC) for that.
See `magit-menu-format-desc'."
`(menu-item
,(if (and (stringp desc) (string-match-p "%[tTvsmMx]" desc))
(list 'magit-menu-format-desc desc)
desc)
,def
;; Without this, the keys for point would be shown instead
;; of the relevant ones from where the click occurred.
:keys ,(apply-partially #'magit--menu-position-keys def)
,@props))
(defun magit--menu-position-keys (def)
(or (ignore-errors
(save-excursion
(goto-char (magit-menu-position))
(and-let* ((key (cl-find-if-not
(lambda (key)
(string-match-p "\\`<[0-9]+>\\'"
(key-description key)))
(where-is-internal def))))
(key-description key))))
""))
(defun magit-menu-position ()
"Return the position where the context-menu was invoked.
If the current command wasn't invoked using the context-menu,
then return nil."
(and magit--context-menu-section
(ignore-errors
(posn-point (event-start (aref (this-command-keys-vector) 0))))))
(defun magit-menu-highlight-point-section ()
(setq magit-section-highlight-force-update t)
(if (eq (current-buffer) magit--context-menu-buffer)
(setq magit--context-menu-section nil)
(if-let ((window (get-buffer-window magit--context-menu-buffer)))
(with-selected-window window
(setq magit--context-menu-section nil)
(magit-section-update-highlight))
(with-current-buffer magit--context-menu-buffer
(setq magit--context-menu-section nil))))
(setq magit--context-menu-buffer nil))
(defvar magit--plural-append-es '(branch))
(cl-defgeneric magit-menu-common-value (_section)
"Return some value to be used by multiple menu items.
This function is called by `magit-section-context-menu', which
stores the value in `magit-menu-common-value'. Individual menu
items can use it, e.g., in the expression used to set their
description."
nil)
(defun magit-menu--desc-values (section)
(let ((type (oref section type))
(value (oref section value))
(multiple (magit-region-sections nil t)))
(list type
value
(format "%s %s" type value)
(and multiple (length multiple))
(if (memq type magit--plural-append-es) "es" "s"))))
(defun magit-menu-format-desc (format)
"Format a string based on FORMAT and menu section or selection.
The following %-specs are allowed:
%t means \"TYPE\".
%T means \"TYPE\", or \"TYPEs\" if multiple sections are selected.
%v means \"VALUE\".
%s means \"TYPE VALUE\".
%m means \"TYPE VALUE\", or \"COUNT TYPEs\" if multiple sections
are selected.
%M means \"VALUE\", or \"COUNT TYPEs\" if multiple sections are
selected.
%x means the value of `magit-menu-common-value'."
(pcase-let* ((`(,type ,value ,single ,count ,suffix) magit-menu--desc-values)
(multiple (and count (format "%s %s%s" count type suffix))))
(format-spec format
`((?t . ,type)
(?T . ,(format "%s%s" type (if count suffix "")))
(?v . ,value)
(?s . ,single)
(?m . ,(or multiple single))
(?M . ,(or multiple value))
(?x . ,(format "%s" magit-menu-common-value))))))
(defun magit--menu-bar-keymap (keymap)
"Backport of `menu-bar-keymap' for Emacs < 28.
Slight trimmed down."
(let ((menu-bar nil))
(map-keymap (lambda (key binding)
(push (cons key binding) menu-bar))
keymap)
(cons 'keymap (nreverse menu-bar))))
(defun magit--context-menu-local (menu _click)
"Backport of `context-menu-local' for Emacs < 28."
(run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
(keymap-set-after menu "<separator-local>" menu-bar-separator)
(let ((keymap (local-key-binding [menu-bar])))
(when keymap
(map-keymap (lambda (key binding)
(when (consp binding)
(define-key-after menu (vector key)
(copy-sequence binding))))
(magit--menu-bar-keymap keymap))))
menu)
(advice-add 'context-menu-region :around
(lambda (fn menu click)
"Disable in `magit-section-mode' buffers."
(if (derived-mode-p 'magit-section-mode)
menu
(funcall fn menu click))))
;;; Commands
;;;; Movement
(defun magit-section-forward ()
"Move to the beginning of the next visible section."
(interactive)
(if (eobp)
(user-error "No next section")
(let ((section (magit-current-section)))
(if (oref section parent)
(let ((next (and (not (oref section hidden))
(not (= (oref section end)
(1+ (point))))
(car (oref section children)))))
(while (and section (not next))
(unless (setq next (car (magit-section-siblings section 'next)))
(setq section (oref section parent))))
(if next
(magit-section-goto next)
(user-error "No next section")))
(magit-section-goto 1)))))
(defun magit-section-backward ()
"Move to the beginning of the current or the previous visible section.
When point is at the beginning of a section then move to the
beginning of the previous visible section. Otherwise move to
the beginning of the current section."
(interactive)
(if (bobp)
(user-error "No previous section")
(let ((section (magit-current-section)) children)
(cond
((and (= (point)
(1- (oref section end)))
(setq children (oref section children)))
(magit-section-goto (car (last children))))
((and (oref section parent)
(not (= (point)
(oref section start))))
(magit-section-goto section))
(t
(let ((prev (car (magit-section-siblings section 'prev))))
(if prev
(while (and (not (oref prev hidden))
(setq children (oref prev children)))
(setq prev (car (last children))))
(setq prev (oref section parent)))
(cond (prev
(magit-section-goto prev))
((oref section parent)
(user-error "No previous section"))
;; Eob special cases.
((not (get-text-property (1- (point)) 'invisible))
(magit-section-goto -1))
(t
(goto-char (previous-single-property-change
(1- (point)) 'invisible))
(forward-line -1)
(magit-section-goto (magit-current-section))))))))))
(defun magit-section-up ()
"Move to the beginning of the parent section."
(interactive)
(if-let ((parent (oref (magit-current-section) parent)))
(magit-section-goto parent)
(user-error "No parent section")))
(defun magit-section-forward-sibling ()
"Move to the beginning of the next sibling section.
If there is no next sibling section, then move to the parent."
(interactive)
(let ((current (magit-current-section)))
(if (oref current parent)
(if-let ((next (car (magit-section-siblings current 'next))))
(magit-section-goto next)
(magit-section-forward))
(magit-section-goto 1))))
(defun magit-section-backward-sibling ()
"Move to the beginning of the previous sibling section.
If there is no previous sibling section, then move to the parent."
(interactive)
(let ((current (magit-current-section)))
(if (oref current parent)
(if-let ((previous (car (magit-section-siblings current 'prev))))
(magit-section-goto previous)
(magit-section-backward))
(magit-section-goto -1))))
(defun magit-section-goto (arg)
(if (integerp arg)
(progn (forward-line arg)
(setq arg (magit-current-section)))
(goto-char (oref arg start)))
(run-hook-with-args 'magit-section-movement-hook arg))
(defun magit-section-set-window-start (section)
"Ensure the beginning of SECTION is visible."
(unless (pos-visible-in-window-p (oref section end))
(set-window-start (selected-window) (oref section start))))
(defmacro magit-define-section-jumper (name heading type &optional value)
"Define an interactive function to go some section.
Together TYPE and VALUE identify the section.
HEADING is the displayed heading of the section."
(declare (indent defun))
`(defun ,name (&optional expand)
,(format "Jump to the section \"%s\".
With a prefix argument also expand it." heading)
(interactive "P")
(if-let ((section (magit-get-section
(cons (cons ',type ,value)
(magit-section-ident magit-root-section)))))
(progn (goto-char (oref section start))
(when expand
(with-local-quit (magit-section-show section))
(recenter 0)))
(message ,(format "Section \"%s\" wasn't found" heading)))))
;;;; Visibility
(defun magit-section-show (section)
"Show the body of the current section."
(interactive (list (magit-current-section)))
(oset section hidden nil)
(magit-section--maybe-wash section)
(when-let ((beg (oref section content)))
(remove-overlays beg (oref section end) 'invisible t))
(magit-section-maybe-update-visibility-indicator section)
(magit-section-maybe-cache-visibility section)
(dolist (child (oref section children))
(if (oref child hidden)
(magit-section-hide child)
(magit-section-show child))))
(defun magit-section--maybe-wash (section)
(when-let ((washer (oref section washer)))
(oset section washer nil)
(let ((inhibit-read-only t)
(magit-insert-section--parent section)
(content (oref section content)))
(save-excursion
(if (and content (< content (oref section end)))
(funcall washer section) ; already partially washed (hunk)
(goto-char (oref section end))
(oset section content (point-marker))
(funcall washer)
(oset section end (point-marker)))))
(setq magit-section-highlight-force-update t)))
(defun magit-section-hide (section)
"Hide the body of the current section."
(interactive (list (magit-current-section)))
(if (eq section magit-root-section)
(user-error "Cannot hide root section")
(oset section hidden t)
(when-let ((beg (oref section content)))
(let ((end (oref section end)))
(when (< beg (point) end)
(goto-char (oref section start)))
(remove-overlays beg end 'invisible t)
(let ((o (make-overlay beg end)))
(overlay-put o 'evaporate t)
(overlay-put o 'invisible t)
(overlay-put o 'cursor-intangible t))))
(magit-section-maybe-update-visibility-indicator section)
(magit-section-maybe-cache-visibility section)))
(defun magit-section-toggle (section)
"Toggle visibility of the body of the current section."
(interactive (list (magit-current-section)))
(cond ((eq section magit-root-section)
(user-error "Cannot hide root section"))
((oref section hidden)
(magit-section-show section))
(t (magit-section-hide section))))
(defun magit-section-toggle-children (section)
"Toggle visibility of bodies of children of the current section."
(interactive (list (magit-current-section)))
(let* ((children (oref section children))
(show (--any-p (oref it hidden) children)))
(dolist (c children)
(oset c hidden show)))
(magit-section-show section))
(defun magit-section-show-children (section &optional depth)
"Recursively show the bodies of children of the current section.
With a prefix argument show children that deep and hide deeper
children."
(interactive (list (magit-current-section)))
(magit-section-show-children-1 section depth)
(magit-section-show section))
(defun magit-section-show-children-1 (section &optional depth)
(dolist (child (oref section children))
(oset child hidden nil)
(if depth
(if (> depth 0)
(magit-section-show-children-1 child (1- depth))
(magit-section-hide child))
(magit-section-show-children-1 child))))
(defun magit-section-hide-children (section)
"Recursively hide the bodies of children of the current section."
(interactive (list (magit-current-section)))
(mapc #'magit-section-hide (oref section children)))
(defun magit-section-show-headings (section)
"Recursively show headings of children of the current section.
Only show the headings, previously shown text-only bodies are
hidden."
(interactive (list (magit-current-section)))
(magit-section-show-headings-1 section)
(magit-section-show section))