-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmajutsu-log.el
More file actions
2088 lines (1852 loc) · 83.7 KB
/
majutsu-log.el
File metadata and controls
2088 lines (1852 loc) · 83.7 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
;;; majutsu-log.el --- Log view for majutsu -*- lexical-binding: t; -*-
;; Copyright (C) 2025 Brandon Olivier
;; Copyright (C) 2025-2026 0WD0
;; Author: Brandon Olivier
;; 0WD0 <wd.1105848296@gmail.com>
;; Maintainer: 0WD0 <wd.1105848296@gmail.com>
;; Keywords: tools, vc
;; URL: https://github.com/0WD0/majutsu
;; SPDX-License-Identifier: GPL-3.0-or-later
;;; Commentary:
;; This library builds the Majutsu log buffer: compiles jj templates,
;; parses log output, renders sections, and handles navigation.
;;; Code:
(require 'majutsu)
;;; Section Keymaps
(defvar-keymap majutsu-commit-section-map
:doc "Keymap for `jj-commit' sections."
"<remap> <majutsu-visit-thing>" #'majutsu-edit-changeset)
;;; Log State
(defun majutsu-log--get-value (mode &optional use-buffer-args)
"Get log arguments for MODE.
Returns (args revsets filesets) triple. USE-BUFFER-ARGS follows
`majutsu-prefix-use-buffer-arguments' or
`majutsu-direct-use-buffer-arguments'."
(setq use-buffer-args
(pcase-exhaustive use-buffer-args
('prefix majutsu-prefix-use-buffer-arguments)
('direct majutsu-direct-use-buffer-arguments)
('nil majutsu-direct-use-buffer-arguments)
((or 'always 'selected 'current 'never)
use-buffer-args)))
(cond
((and (memq use-buffer-args '(always selected current))
(eq major-mode mode))
(list majutsu-buffer-log-args
majutsu-buffer-log-revsets
majutsu-buffer-log-filesets))
((and (memq use-buffer-args '(always selected))
(when-let* ((buf (majutsu--get-mode-buffer mode (eq use-buffer-args 'selected))))
(list (buffer-local-value 'majutsu-buffer-log-args buf)
(buffer-local-value 'majutsu-buffer-log-revsets buf)
(buffer-local-value 'majutsu-buffer-log-filesets buf)))))
((plist-member (symbol-plist mode) 'majutsu-log-current-arguments)
(list (get mode 'majutsu-log-current-arguments)
(get mode 'majutsu-log-current-revsets)
(get mode 'majutsu-log-current-filesets)))
((when-let* ((elt (assq (intern (format "majutsu-log:%s" mode))
transient-values)))
(list (cdr elt)
(get mode 'majutsu-log-current-revsets)
(get mode 'majutsu-log-current-filesets))))
(t
(list (get mode 'majutsu-log-default-arguments)
(get mode 'majutsu-log-default-revsets)
(get mode 'majutsu-log-default-filesets)))))
(defun majutsu-log--set-value (mode args revsets filesets &optional save)
"Set current log values for MODE.
When SAVE is non-nil, also persist ARGS using `transient-values'."
(setq args (seq-remove #'null (flatten-tree args)))
(setq filesets
(and filesets
(seq-remove (lambda (s)
(or (null s)
(and (stringp s) (string-empty-p s))))
(flatten-tree filesets))))
(put mode 'majutsu-log-current-arguments args)
(put mode 'majutsu-log-current-revsets revsets)
(put mode 'majutsu-log-current-filesets filesets)
(when save
(setf (alist-get (intern (format "majutsu-log:%s" mode)) transient-values) args)
(transient-save-values))
(when (eq major-mode mode)
(setq-local majutsu-buffer-log-args args)
(setq-local majutsu-buffer-log-revsets revsets)
(setq-local majutsu-buffer-log-filesets filesets))
nil)
(defvar-local majutsu-log--this-error nil
"Last jj side-effect error summary for this log buffer.
This is set by process runners (see `majutsu-process-buffer') and
rendered by `majutsu-log-insert-error-header' on the next refresh.")
(defcustom majutsu-log-sections-hook
(list #'majutsu-log-insert-error-header
#'majutsu-log-insert-logs
#'majutsu-log-insert-status
#'majutsu-insert-workspaces)
"Hook run to insert sections in the log buffer."
:type 'hook
:group 'majutsu)
(defun majutsu-log--args-member-p (args flag)
(and args (member flag args)))
(defun majutsu-log--args-get-option (args opt)
"Return OPT's value from ARGS, or nil.
Only supports simple OPT VALUE pairs."
(let ((pos (seq-position args opt #'equal)))
(and pos
(nth (1+ pos) args))))
(defun majutsu-log--args-remove-option (args opt &optional takes-value)
"Return ARGS with OPT removed.
When TAKES-VALUE is non-nil, also remove the following element."
(let ((out nil))
(while args
(let ((a (pop args)))
(if (equal a opt)
(when takes-value
(pop args))
(push a out))))
(nreverse out)))
(defun majutsu-log--args-toggle-flag (args flag)
(if (member flag args)
(remove flag args)
(append args (list flag))))
(defun majutsu-log--args-set-option (args opt value)
"Set OPT to VALUE inside ARGS (removing existing OPT)."
(setq args (majutsu-log--args-remove-option args opt t))
(if value
(append args (list opt value))
args))
(defun majutsu-log--summary-parts ()
"Return a list of human-readable fragments describing current log buffer."
(pcase-let* ((`(,args ,revsets ,filesets)
(majutsu-log--get-value 'majutsu-log-mode 'current))
(parts '()))
(when revsets
(push (format "rev=%s" revsets) parts))
(when-let* ((limit (majutsu-log--args-get-option args "-n")))
(push (format "limit=%s" limit) parts))
(when (majutsu-log--args-member-p args "--reversed")
(push "reversed" parts))
(when (majutsu-log--args-member-p args "--no-graph")
(push "no-graph" parts))
(when filesets
(push (if (= (length filesets) 1)
(format "path=%s" (car filesets))
(format "paths=%d" (length filesets)))
parts))
(nreverse parts)))
(defun majutsu-log--format-summary (prefix)
"Return PREFIX annotated with active log state summary."
(let ((parts (majutsu-log--summary-parts)))
(if parts
(format "%s (%s)" prefix (string-join parts ", "))
prefix)))
(defun majutsu-log--heading-string ()
"Return heading string for the log section."
(majutsu-log--format-summary "Log Graph"))
(defun majutsu-log--transient-description ()
"Return description string for the log transient."
(majutsu-log--format-summary "JJ Log"))
;;; Log Template
(defconst majutsu-log--field-separator "\x1e"
"Separator character inserted between fields inside each module payload.")
(defconst majutsu-log--field-list-separator "\x1c"
"Separator character inserted between list items inside a single field value.")
(defconst majutsu-log--field-line-separator "\x1f"
"Encoded newline separator used inside template field payloads.
Log records are transported as single lines, then this separator is
decoded back to literal newlines after field splitting.")
(defconst majutsu-log--record-marker "\x1d"
"Control marker prefix for module boundaries inside log output.")
(defconst majutsu-log--entry-start-token (concat majutsu-log--record-marker "S")
"Marker that starts a commit entry and heading payload.")
(defconst majutsu-log--entry-tail-token (concat majutsu-log--record-marker "T")
"Marker that starts the tail payload.")
(defconst majutsu-log--entry-body-token (concat majutsu-log--record-marker "B")
"Marker that starts the body payload.")
(defconst majutsu-log--entry-meta-token (concat majutsu-log--record-marker "M")
"Marker that starts the metadata payload.")
(defconst majutsu-log--entry-end-token (concat majutsu-log--record-marker "E")
"Marker that terminates a commit entry.")
(defconst majutsu-log--module-order '(heading tail body metadata)
"Module parse/render order for sequential log payloads.")
(defconst majutsu-log--field-default-modules
'((id . metadata)
(change-id . heading)
(commit-id . metadata)
(parent-ids . metadata)
(bookmarks . heading)
(tags . heading)
(working-copies . heading)
(flags . metadata)
(git-head . heading)
(signature . heading)
(empty . heading)
(description . heading)
(author . tail)
(timestamp . tail)
(long-desc . body))
"Default module placement for known log fields.")
(defconst majutsu-log--required-columns '(id commit-id parent-ids)
"Fields that must exist in `majutsu-log-commit-columns'.
These fields are transported even when users omit them from visible layout,
so log semantics such as stable identity, commit-hash copying, and relation
navigation remain available.")
(defconst majutsu-log--default-column-postprocessors nil
"Default postprocessors appended to every column instance.")
(defconst majutsu-log--field-default-postprocessors
'((parent-ids . (majutsu-log-post-split-list-separator))
(timestamp . (majutsu-log-post-remove-ago-suffix)))
"Field-specific default postprocessors appended after global defaults.")
(defcustom majutsu-log-commit-columns
'((:field change-id :module heading :face t)
(:field bookmarks :module heading :face magit-branch-local)
(:field tags :module heading :face magit-tag)
(:field working-copies :module heading :face magit-branch-remote)
(:field empty :module heading :face t)
(:field git-head :module heading :face t)
(:field description :module heading :face t)
(:field author :module tail :face magit-log-author)
(:field timestamp :module tail :face magit-log-date)
(:field long-desc :module body :face t)
(:field id :module metadata :face nil)
(:field commit-id :module metadata :face nil)
(:field flags :module metadata :face nil))
"Field specification controlling log template and rendering.
Each element is a plist with at least `:field'. Supported keys:
- :field - symbol identifying a known field.
- :module - one of `heading', `tail', `body', or `metadata'.
- :face - t (preserve jj highlighting), nil (strip), or FACE (override).
- :post - postprocessor function or function list for field value transforms.
When `:face' is omitted, it defaults to t.
`heading' is the only module that may emit physical newlines. Other modules
remain in the sequential payload tail and should encode logical newlines as
`majutsu-log--field-line-separator' (\\x1f)."
:type '(repeat (plist :options (:field :module :face :post)))
:group 'majutsu)
(defmacro majutsu-log-define-column (name template doc)
"Define a log column template variable for NAME with default TEMPLATE and DOC.
The variable name will be `majutsu-log-template-NAME'.
Also registers a variable watcher to invalidate the template cache."
(declare (indent 1) (debug t) (doc-string 3))
(let ((var-name (intern (format "majutsu-log-template-%s" name))))
`(progn
(defcustom ,var-name ,template
,doc
:type 'sexp
:group 'majutsu)
(when (fboundp 'add-variable-watcher)
(add-variable-watcher ',var-name #'majutsu-log--invalidate-template-cache)))))
(majutsu-template-defkeyword git_head Commit
(:returns Boolean :doc "Deprecated alias for .contained_in('first_parent(@)')")
[:method [:self] :contained_in "first_parent(@)"])
(majutsu-template-defkeyword short-change-id Commit
(:returns Template :doc "Shortest unique change id.")
[:change_id :shortest 8])
(majutsu-template-defkeyword short-change-id-with-offset Commit
(:returns Template :doc "Shortest unique change id with offset.")
[[:short-change-id]
[:label "change_offset" "/"]
[:change_offset]])
(majutsu-template-defkeyword canonical-log-id Commit
(:returns Template :doc "Canonical log id.")
[:if [:or [:hidden]
[:divergent]]
[:commit_id :shortest 8]
[:change_id :shortest 8]])
(majutsu-log-define-column id
[:canonical-log-id]
"Template for the commit-id column.")
(majutsu-log-define-column change-id
[:label
[:separate " "
[:if [:current_working_copy] "working_copy"]
[:if [:immutable] "immutable" "mutable"]
[:if [:conflict] "conflicted"]]
[:coalesce
[:if [:hidden]
[:label "hidden" [:short-change-id-with-offset]]]
[:if [:divergent]
[:label "divergent" [:short-change-id-with-offset]]]
[:short-change-id]]]
"Template for the change-id column.")
(majutsu-log-define-column commit-id
[:commit_id :shortest 8]
"Template for the commit-id column.")
(majutsu-log-define-column parent-ids
`[:method
[:map [:parents] p [:canonical-log-id]]
:join ,majutsu-log--field-list-separator]
"Template for the parent-ids metadata column.")
(majutsu-log-define-column bookmarks
[:bookmarks]
"Template for the bookmarks column.")
(majutsu-log-define-column tags
[:tags]
"Template for the tags column.")
(majutsu-log-define-column working-copies
[:working_copies]
"Template for the working-copies column.")
(majutsu-log-define-column flags
[:separate " "
[:if [:current_working_copy] "@"]
[:if [:immutable] "immutable" "mutable"]
[:if [:conflict] [:label "conflict" "conflict"]]
[:if [:git_head] "git_head"]
[:if [:root] "root"]
[:if [:empty] "(empty)"]]
"Template for the flags column.")
(majutsu-log-define-column git-head
[:if [:git_head] [:label "git_head" "(git_head)"]]
"Template for the git-head column.")
(majutsu-log-define-column signature
[:if [:method [:call 'config "ui.show-cryptographic-signatures"] :as_boolean]
[:if [:signature]
[:label "signature status"
["["
[:label [:signature :status]
[:coalesce
[:if [:== [:signature :status] "good"] "✓︎"]
[:if [:== [:signature :status] "unknown"] "?"]
"x"]]
"]"]]]]
"Template for the signature column.")
(majutsu-log-define-column empty
[:if [:empty]
[:label "empty" "(empty)"]]
"Template for the empty column.")
(majutsu-log-define-column description
[:if [:description]
[:method [:description] :first_line]
[:label
[:if [:empty] "empty"]
[:label
"description placeholder"
"(no desc)"]]]
"Template for the description column.")
(majutsu-log-define-column author
[:author :name]
"Template for the author column.")
(majutsu-log-define-column timestamp
[:committer :timestamp :ago]
"Template for the timestamp column.")
(majutsu-log-define-column long-desc
[:description :lines :skip 1 :join "\x1f"]
"Template for the long-desc column.
Newlines are encoded as an internal separator so ANSI/label styling
can survive transport through the single-line log format.")
(defvar majutsu-log--compiled-template-cache nil
"Cached structure holding the compiled log template and column metadata.")
(defvar-local majutsu-log--cached-entries nil
"Cached log entries for the current buffer.")
(defvar-local majutsu-log--entry-by-id nil
"Hash table mapping visible log entry ids to parsed entry plists.")
(defvar-local majutsu-log--children-by-id nil
"Hash table mapping visible parent ids to visible child id lists.")
(defvar-local majutsu-log--buffer-compiled nil
"Compiled column/layout metadata used to render the current buffer.")
(defun majutsu-log--invalidate-template-cache (&rest _)
"Reset cached compiled template when layout changes."
(setq majutsu-log--compiled-template-cache nil)
(setq majutsu-log--cached-entries nil)
(setq majutsu-log--entry-by-id nil)
(setq majutsu-log--children-by-id nil)
(setq majutsu-log--buffer-compiled nil))
(defun majutsu-log-post-decode-line-separator (value &optional _ctx)
"Decode `majutsu-log--field-line-separator' inside VALUE.
This is the default postprocessor for log fields so all modules can
transport logical newlines safely through single-line payload segments."
(if (stringp value)
(subst-char-in-string
(aref majutsu-log--field-line-separator 0)
?\n
value t)
value))
(defun majutsu-log-post-split-list-separator (value &optional _ctx)
"Split VALUE by `majutsu-log--field-list-separator'."
(when (and (stringp value)
(not (string-empty-p value)))
(mapcar #'substring-no-properties
(majutsu-log--split-by-separator value majutsu-log--field-list-separator))))
(defun majutsu-log-post-remove-ago-suffix (value &optional _ctx)
"Trim a trailing \\=' ago\\=' suffix from VALUE."
(if (stringp value)
(string-remove-suffix " ago" value)
value))
(defun majutsu-log--default-module-for-field (field)
"Return default module symbol for FIELD."
(or (alist-get field majutsu-log--field-default-modules nil nil #'eq)
(user-error "Field %S requires explicit :module" field)))
(defun majutsu-log--default-postprocessors-for-field (field)
"Return default postprocessors for FIELD."
(append majutsu-log--default-column-postprocessors
(alist-get field majutsu-log--field-default-postprocessors nil nil #'eq)))
(defun majutsu-log--normalize-postprocessors (post field)
"Normalize POST value for FIELD into a function list.
Omitted or `:default' values use the field defaults. Explicit functions are
appended after those defaults, while nil disables column postprocessing."
(let* ((defaults (majutsu-log--default-postprocessors-for-field field))
(fns (cond
((eq post :default) defaults)
((null post) nil)
((functionp post) (append defaults (list post)))
((and (listp post) (seq-every-p #'functionp post))
(append defaults post))
(t (user-error "Column %S has invalid :post %S" field post)))))
(dolist (fn fns)
(unless (functionp fn)
(user-error "Column %S has non-callable postprocessor %S" field fn)))
fns))
(defun majutsu-log--normalize-column-spec (spec)
"Normalize a single column SPEC into a plist with defaults."
(let* ((col (cond
((and (plistp spec) (plist-get spec :field)) spec)
((symbolp spec) (list :field spec))
(t (user-error "Invalid column spec: %S" spec))))
(field (plist-get col :field))
(module (if (plist-member col :module)
(plist-get col :module)
(majutsu-log--default-module-for-field field)))
(face (if (plist-member col :face)
(plist-get col :face)
t))
(post (if (plist-member col :post)
(plist-get col :post)
:default)))
(setq module (if (keywordp module)
(intern (substring (symbol-name module) 1))
module))
(unless (memq module majutsu-log--module-order)
(user-error "Column %S has invalid :module %S" field module))
(unless (or (eq face t) (null face) (symbolp face))
(user-error "Column %S has invalid :face %S" field face))
(list :field field
:module module
:face face
:post (majutsu-log--normalize-postprocessors post field))))
(defun majutsu-log--ensure-required-columns (columns)
"Ensure required columns are present in COLUMNS list.
Missing required fields are appended with defaults."
(let ((present (mapcar (lambda (c) (plist-get c :field)) columns)))
(dolist (req majutsu-log--required-columns)
(unless (memq req present)
(setq columns (append columns (list (majutsu-log--normalize-column-spec req))))))
columns))
(defun majutsu-log--module-columns (compiled module)
"Return compiled column specs for MODULE from COMPILED metadata."
(alist-get module (plist-get compiled :module-columns) nil nil #'eq))
(defun majutsu-log--assign-column-instances (columns)
"Return COLUMNS with stable per-instance ids assigned."
(cl-loop for column in columns
for idx from 0
collect (plist-put (copy-sequence column) :instance idx)))
(defun majutsu-log--column-template (field)
"Return majutsu-template form for FIELD.
Looks up `majutsu-log-template-FIELD'."
(let ((var (intern-soft (format "majutsu-log-template-%s" field))))
(if (and var (boundp var))
(symbol-value var)
(user-error "Unknown column field %S" field))))
(defun majutsu-log--build-module-template-form (templates)
"Return a template form joining TEMPLATES with field separators."
(cond
((null templates) "")
((null (cdr templates)) (car templates))
(t
(let ((forms nil)
(first t))
(dolist (template templates)
(unless first
(setq forms (append forms (list majutsu-log--field-separator))))
(setq first nil)
(setq forms (append forms (list template))))
(cons :concat forms)))))
(defun majutsu-log--compile-columns (&optional columns)
"Compile COLUMNS (or `majutsu-log-commit-columns') into a jj template string.
Returns a plist with :template, :columns, and :module-columns."
(let* ((normalized (mapcar #'majutsu-log--normalize-column-spec
(or columns majutsu-log-commit-columns)))
(complete (majutsu-log--assign-column-instances
(majutsu-log--ensure-required-columns normalized)))
(module-columns
(mapcar (lambda (module)
(cons module
(seq-filter (lambda (c)
(eq (plist-get c :module) module))
complete)))
majutsu-log--module-order))
(heading-form
(majutsu-log--build-module-template-form
(mapcar (lambda (c)
(majutsu-log--column-template (plist-get c :field)))
(alist-get 'heading module-columns nil nil #'eq))))
(tail-form
(majutsu-log--build-module-template-form
(mapcar (lambda (c)
(majutsu-log--column-template (plist-get c :field)))
(alist-get 'tail module-columns nil nil #'eq))))
(body-form
(majutsu-log--build-module-template-form
(mapcar (lambda (c)
(majutsu-log--column-template (plist-get c :field)))
(alist-get 'body module-columns nil nil #'eq))))
(meta-form
(majutsu-log--build-module-template-form
(mapcar (lambda (c)
(majutsu-log--column-template (plist-get c :field)))
(alist-get 'metadata module-columns nil nil #'eq))))
(compiled
(majutsu-tpl
`[:concat
,majutsu-log--entry-start-token
,heading-form
,majutsu-log--entry-tail-token
,tail-form
,majutsu-log--entry-body-token
,body-form
,majutsu-log--entry-meta-token
,meta-form
,majutsu-log--entry-end-token
"\n"])))
(list :template compiled
:columns complete
:module-columns module-columns)))
(defun majutsu-log--ensure-template ()
"Return cached compiled template structure, recomputing if necessary."
(or majutsu-log--compiled-template-cache
(setq majutsu-log--compiled-template-cache
(majutsu-log--compile-columns majutsu-log-commit-columns))))
(defun majutsu-log--build-args ()
"Build argument list for `jj log' using current log variables."
(pcase-let ((`(,args ,revsets ,filesets)
(majutsu-log--get-value 'majutsu-log-mode 'current)))
(let ((cmd '("log")))
(setq cmd (append cmd args))
(when revsets
(setq cmd (append cmd (list "-r" revsets))))
(setq cmd (append cmd (list "-T" (plist-get (majutsu-log--ensure-template) :template))))
(setq cmd (append cmd filesets))
cmd)))
;;; Log Parsing
(defun majutsu-log--split-by-separator (value separator)
"Split VALUE by one-char string SEPARATOR, preserving empty fields."
(if (not (stringp value))
nil
(let ((start 0)
(len (length value))
(sep (aref separator 0))
out)
(dotimes (idx len)
(when (eq (aref value idx) sep)
(push (substring value start idx) out)
(setq start (1+ idx))))
(push (substring value start len) out)
(nreverse out))))
(defun majutsu-log--join-lines (lines)
"Join LINES with literal newlines, preserving string properties."
(if (null lines)
""
(let ((out (car lines)))
(dolist (line (cdr lines) out)
(setq out (concat out "\n" line))))))
(defun majutsu-log--line-token-position (token bol eol &optional start)
"Return start position of TOKEN between BOL and EOL, or nil."
(save-excursion
(goto-char (or start bol))
(when (search-forward token eol t)
(- (point) (length token)))))
(defun majutsu-log--parse-trailing-payloads (payload)
"Parse trailing payload segments from PAYLOAD string.
PAYLOAD is expected to start with either `majutsu-log--entry-tail-token'
(new format) or `majutsu-log--entry-body-token' (legacy format)."
(cond
((string-prefix-p majutsu-log--entry-tail-token payload)
(let* ((tail-start (length majutsu-log--entry-tail-token))
(body-pos (string-match (regexp-quote majutsu-log--entry-body-token)
payload tail-start))
(meta-pos (and body-pos
(string-match (regexp-quote majutsu-log--entry-meta-token)
payload (+ body-pos (length majutsu-log--entry-body-token)))))
(end-pos (and meta-pos
(string-match (regexp-quote majutsu-log--entry-end-token)
payload (+ meta-pos (length majutsu-log--entry-meta-token))))))
(when (and body-pos meta-pos end-pos)
(let ((trailing (substring payload (+ end-pos (length majutsu-log--entry-end-token)))))
(when (string-empty-p trailing)
(list :tail (substring payload tail-start body-pos)
:body (substring payload
(+ body-pos (length majutsu-log--entry-body-token))
meta-pos)
:metadata (substring payload
(+ meta-pos (length majutsu-log--entry-meta-token))
end-pos)))))))
((string-prefix-p majutsu-log--entry-body-token payload)
(let* ((body-start (length majutsu-log--entry-body-token))
(meta-pos (string-match (regexp-quote majutsu-log--entry-meta-token)
payload body-start))
(end-pos (and meta-pos
(string-match (regexp-quote majutsu-log--entry-end-token)
payload (+ meta-pos (length majutsu-log--entry-meta-token))))))
(when (and meta-pos end-pos)
(let ((trailing (substring payload (+ end-pos (length majutsu-log--entry-end-token)))))
(when (string-empty-p trailing)
(list :tail ""
:body (substring payload body-start meta-pos)
:metadata (substring payload
(+ meta-pos (length majutsu-log--entry-meta-token))
end-pos)))))))))
(defun majutsu-log--split-module-values (payload count)
"Split PAYLOAD into COUNT field values using `majutsu-log--field-separator'."
(if (<= count 0)
nil
(let ((values (majutsu-log--split-by-separator (or payload "") majutsu-log--field-separator)))
(cond
((< (length values) count)
(append values (make-list (- count (length values)) "")))
((> (length values) count)
(seq-take values count))
(t values)))))
(defun majutsu-log--decode-transport-value (value)
"Decode transport-level escapes inside VALUE before column postprocessing."
(majutsu-log-post-decode-line-separator value))
(defun majutsu-log--apply-postprocessor (fn value ctx)
"Apply postprocessor FN to VALUE with context CTX.
FN may accept either (VALUE) or (VALUE CTX). Errors return VALUE unchanged."
(condition-case err
(condition-case _
(funcall fn value ctx)
(wrong-number-of-arguments
(funcall fn value)))
(error
(majutsu--debug "majutsu-log postprocessor failed (%S on %S): %s"
fn (plist-get ctx :field) (error-message-string err))
value)))
(defun majutsu-log--apply-postprocessors (value postprocessors ctx)
"Apply POSTPROCESSORS to VALUE with CTX sequentially."
(let ((out value))
(dolist (fn postprocessors out)
(setq out (majutsu-log--apply-postprocessor fn out ctx)))))
(defun majutsu-log--apply-flags (entry value)
"Set flag fields on ENTRY based on VALUE string."
(dolist (flag (split-string (or value "") " " t))
(pcase flag
("immutable" (setq entry (plist-put entry :immutable t)))
("mutable" (setq entry (plist-put entry :immutable nil)))
("conflict" (setq entry (plist-put entry :conflict t)))
("git_head" (setq entry (plist-put entry :git-head t)))
("root" (setq entry (plist-put entry :root t)))
("@" (setq entry (plist-put entry :current_working_copy t)))))
entry)
(defun majutsu-log--canonical-field-value (field value)
"Return canonical semantic value for FIELD based on VALUE."
(majutsu-log--apply-postprocessors
value
(majutsu-log--default-postprocessors-for-field field)
(list :field field :module 'canonical :raw-value value :canonical t)))
(defun majutsu-log--record-field (entry field value)
"Record canonical FIELD VALUE onto ENTRY plist and field map."
(pcase field
('id
(setq entry (plist-put entry :id value)))
('change-id
(setq entry (plist-put entry :change-id value)))
('commit-id
(setq entry (plist-put entry :commit-id value)))
('parent-ids
(setq entry (plist-put entry :parent-ids value)))
('bookmarks
(setq entry (plist-put entry :bookmarks value)))
('tags
(setq entry (plist-put entry :tags value)))
('working-copies
(setq entry (plist-put entry :working-copies value)))
('description
(setq entry (plist-put entry :short-desc value)))
('author
(setq entry (plist-put entry :author value)))
('timestamp
(setq entry (plist-put entry :timestamp value)))
('long-desc
(setq entry (plist-put entry :long-desc value)))
('flags
(setq entry (majutsu-log--apply-flags entry value)))
('git-head
(when (and value (not (string-empty-p value)))
(setq entry (plist-put entry :git-head t))))
('signature
(setq entry (plist-put entry :signature value)))
('empty
(setq entry (plist-put entry :empty (not (string-empty-p value))))))
(let ((columns (plist-get entry :columns)))
(setf (alist-get field columns nil nil #'eq) value)
(setq entry (plist-put entry :columns columns)))
entry)
(defun majutsu-log--record-column-value (entry column value)
"Record per-instance VALUE for COLUMN onto ENTRY."
(let* ((instance (plist-get column :instance))
(column-values (plist-get entry :column-values)))
(when instance
(setf (alist-get instance column-values nil nil #'eql) value)
(setq entry (plist-put entry :column-values column-values)))
entry))
(defun majutsu-log--record-module-fields (entry module payload compiled)
"Record MODULE PAYLOAD values into ENTRY using COMPILED module layout."
(let* ((columns (majutsu-log--module-columns compiled module))
(values (majutsu-log--split-module-values payload (length columns)))
(stored nil))
(cl-loop for column in columns
for raw-value in values
do (let* ((field (plist-get column :field))
(decoded (majutsu-log--decode-transport-value raw-value))
(canonical (majutsu-log--canonical-field-value field decoded)))
(setq entry (majutsu-log--record-field entry field canonical))
(let* ((ctx (list :field field
:module module
:column column
:entry entry
:raw-value decoded
:canonical-value canonical))
(out (majutsu-log--apply-postprocessors decoded
(plist-get column :post)
ctx)))
(setq entry (majutsu-log--record-column-value entry column out))
(push out stored))))
(let ((modules (plist-get entry :modules)))
(setf (alist-get module modules nil nil #'eq) (nreverse stored))
(setq entry (plist-put entry :modules modules)))
entry))
(defun majutsu-log--parse-entry-at-point (compiled)
"Parse one sequentially-encoded log entry at point using COMPILED.
Point must be at a line potentially containing `majutsu-log--entry-start-token'.
Returns entry plist and moves point past the consumed entry, or nil."
(let* ((entry-beg (line-beginning-position))
(bol entry-beg)
(eol (line-end-position))
(start-pos (majutsu-log--line-token-position majutsu-log--entry-start-token bol eol)))
(when start-pos
(let* ((indent (- start-pos bol))
(heading-prefixes nil)
(heading-segments nil)
(trailing-payload nil)
(done nil)
(first-line t))
(while (and (not done) (not (eobp)))
(setq bol (line-beginning-position)
eol (line-end-position))
(let* ((prefix-end (min (+ bol indent) eol))
(prefix (buffer-substring bol prefix-end))
(content-start (if first-line
(+ start-pos (length majutsu-log--entry-start-token))
prefix-end))
(tail-pos (majutsu-log--line-token-position
majutsu-log--entry-tail-token bol eol content-start))
(body-pos (and (null tail-pos)
(majutsu-log--line-token-position
majutsu-log--entry-body-token bol eol content-start)))
(segment-pos (or tail-pos body-pos)))
(if segment-pos
(progn
(push prefix heading-prefixes)
(push (buffer-substring content-start segment-pos) heading-segments)
(setq trailing-payload (buffer-substring segment-pos eol))
(setq done t)
(forward-line 1))
(push prefix heading-prefixes)
(push (buffer-substring content-start eol) heading-segments)
(forward-line 1)
(when (eobp)
(setq done :incomplete))))
(setq first-line nil))
(when (eq done t)
(when-let* ((payloads (majutsu-log--parse-trailing-payloads trailing-payload)))
(let* ((entry (list :beg entry-beg
:indent indent
:columns nil
:column-values nil
:modules nil
:heading-prefixes (nreverse heading-prefixes)))
(heading-payload (majutsu-log--join-lines (nreverse heading-segments))))
(setq entry (majutsu-log--record-module-fields entry 'heading heading-payload compiled))
(setq entry (majutsu-log--record-module-fields
entry 'tail (plist-get payloads :tail) compiled))
(setq entry (majutsu-log--record-module-fields
entry 'body (plist-get payloads :body) compiled))
(setq entry (majutsu-log--record-module-fields
entry 'metadata (plist-get payloads :metadata) compiled))
(let ((suffix-lines nil))
;; Preserve graph continuation lines between the current entry's
;; end marker and the next entry start marker. These lines stay
;; visible as part of the current section heading area.
(while (and (not (eobp))
(let ((next-bol (line-beginning-position))
(next-eol (line-end-position)))
(not (majutsu-log--line-token-position
majutsu-log--entry-start-token next-bol next-eol))))
(push (buffer-substring (line-beginning-position) (line-end-position))
suffix-lines)
(forward-line 1))
(setq entry (plist-put entry :suffix-lines (nreverse suffix-lines)))
(setq entry (plist-put entry :end (point))))
entry)))))))
(defun majutsu-log--parse-entries-in-buffer (compiled)
"Parse all sequentially-encoded log entries in current buffer using COMPILED."
(let (entries)
(save-excursion
(goto-char (point-min))
(while (not (eobp))
(let ((entry (majutsu-log--parse-entry-at-point compiled)))
(if entry
(push entry entries)
(forward-line 1)))))
(nreverse entries)))
(defun majutsu-log--apply-line-prefix-span (start end line-prefix-str &optional wrap-prefix-str)
"Apply display-only line/wrap prefix strings to START..END."
(when (< start end)
(add-text-properties
start end
(list 'line-prefix (or line-prefix-str "")
'wrap-prefix (or wrap-prefix-str line-prefix-str "")))))
(defun majutsu-log--insert-prefixed-line (content prefix)
"Insert CONTENT as one line with display-only PREFIX."
(let ((start (point)))
(insert (or content "") "\n")
(majutsu-log--apply-line-prefix-span start (point) prefix)))
(defun majutsu-log--split-prefix-line (line prefix-width)
"Split LINE into (PREFIX . CONTENT) using PREFIX-WIDTH characters."
(let* ((width (max 0 (min (or prefix-width 0) (length (or line "")))))
(text (or line "")))
(cons (substring text 0 width)
(substring text width))))
(defun majutsu-log--entry-column (entry field)
"Return canonical value for FIELD stored on ENTRY."
(alist-get field (plist-get entry :columns) nil nil #'eq))
(defun majutsu-log--entry-column-value (entry column)
"Return per-instance value for COLUMN stored on ENTRY.
Fallback to the canonical field value when ENTRY predates per-instance storage."
(let* ((instance (plist-get column :instance))
(column-values (plist-get entry :column-values))
(missing (make-symbol "majutsu-log-missing-instance"))
(value (if instance
(alist-get instance column-values missing nil #'eql)
missing)))
(if (eq value missing)
(majutsu-log--entry-column entry (plist-get column :field))
value)))
(defun majutsu-log--display-string (value)
"Return VALUE converted to a display string."
(cond
((null value) "")
((stringp value) value)
((listp value)
(mapconcat #'majutsu-log--display-string value " "))
(t (format "%s" value))))
(defun majutsu-log--apply-face-policy (value face)
"Apply FACE policy to VALUE and return display string."
(let ((v (or value "")))
(cond
((eq face t) v)
((null face) (substring-no-properties v))
(t (propertize (substring-no-properties v) 'font-lock-face face)))))
(defun majutsu-log--content-properties (entry-id module &optional column)
"Return content text properties for ENTRY-ID in MODULE.
When COLUMN is non-nil, also include field and column-instance identity."
(append `(majutsu-log-module ,module
majutsu-log-entry-id ,entry-id)
(when column
`(majutsu-log-field ,(plist-get column :field)
majutsu-log-column ,(plist-get column :instance)))))
(defun majutsu-log--decoration-properties (entry-id module decoration)
"Return decoration text properties for ENTRY-ID in MODULE."
`(majutsu-log-module ,module
majutsu-log-entry-id ,entry-id
majutsu-log-decoration ,decoration))
(defun majutsu-log--propertize-content (text entry-id module &optional column)
"Return TEXT tagged as MODULE content for ENTRY-ID and COLUMN."
(if (stringp text)
(apply #'propertize text
(majutsu-log--content-properties entry-id module column))
text))
(defun majutsu-log--propertize-decoration (text entry-id module decoration)
"Return TEXT tagged as MODULE DECORATION for ENTRY-ID."
(if (stringp text)
(apply #'propertize text
(majutsu-log--decoration-properties entry-id module decoration))
text))
(defun majutsu-log--concat-heading-parts (parts)
"Concatenate heading PARTS without adding spaces after newlines."
(let ((out ""))
(dolist (part parts out)
(unless (string-empty-p part)
(let ((need-space
(and (> (length out) 0)