summaryrefslogtreecommitdiffstats
path: root/2026/captions/emacsconf-2026-align--columnaligning-text-in-emacs--jeff-trull--main.vtt
blob: a001f0799d63ec98e0a76bc4ca06a46521b57f3b (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
WEBVTT captioned by sachac

NOTE Lining things up: Introduction

00:00:00.617 --> 00:00:03.138
Let's talk about aligning things vertically

00:00:03.139 --> 00:00:04.739
in Emacs.

00:00:04.740 --> 00:00:06.820
It's often the case we want to line up text

00:00:06.821 --> 00:00:08.381
vertically in Emacs,

00:00:08.382 --> 00:00:10.883
typically for aesthetic purposes

00:00:10.884 --> 00:00:12.884
to make paragraphs or tables

00:00:12.885 --> 00:00:15.339
or code easier to read.

00:00:15.340 --> 00:00:17.267
I'm embarrassed to say that I used to do

00:00:17.268 --> 00:00:19.909
these kinds of things in a painfully manual way

00:00:19.910 --> 00:00:23.071
because I hadn't read the documentation enough

00:00:23.072 --> 00:00:25.439
and I didn't realize there were features for it.

00:00:25.440 --> 00:00:28.695
I want to introduce you to these features today

00:00:28.696 --> 00:00:31.488
and then use them as a launching point

00:00:31.489 --> 00:00:34.390
for a discussion of customizations and extensions

00:00:34.391 --> 00:00:36.051
using Emacs Lisp.

00:00:36.052 --> 00:00:37.092
We're specifically going to

00:00:37.093 --> 00:00:39.993
talk about aligning the sides of paragraphs

00:00:39.994 --> 00:00:44.296
or comments, aligning columns of text for tables

00:00:44.297 --> 00:00:48.459
or in code, how to recognize columns

00:00:48.460 --> 00:00:52.081
using regular expressions, and then finally,

00:00:52.082 --> 00:00:53.402
doing the same thing

00:00:53.403 --> 00:00:55.563
but recognizing columns using parsers,

00:00:55.564 --> 00:01:00.347
which are more powerful than regular expressions.

NOTE Terminology: justification, indentation, column alignment

00:01:00.348 --> 00:01:03.890
But first, some terminology.

00:01:03.891 --> 00:01:07.472
Left justification is just putting whitespace on

00:01:07.473 --> 00:01:10.994
the left hand side the same amount on every line.

00:01:10.995 --> 00:01:16.937
This is different, in my view, from indentation,

00:01:16.938 --> 00:01:20.238
which is using the amount of whitespace on the

00:01:20.239 --> 00:01:22.900
left hand side to indicate something semantic

00:01:22.901 --> 00:01:23.900
about what you're looking at

00:01:23.901 --> 00:01:25.841
to give a cue to the reader.

00:01:25.842 --> 00:01:30.824
For example, in the paragraph that we have here,

00:01:30.825 --> 00:01:32.425
the first line is indented

00:01:32.426 --> 00:01:34.365
because it begins a paragraph.

00:01:34.366 --> 00:01:35.226
So that gives you a cue

00:01:35.227 --> 00:01:37.186
that a paragraph has begun.

00:01:37.187 --> 00:01:40.187
You might also be using indentation in code

00:01:40.188 --> 00:01:43.408
to indicate the level of nesting

00:01:43.409 --> 00:01:46.149
or which pieces of code are considered

00:01:46.150 --> 00:01:47.750
parents of other pieces of code,

00:01:47.751 --> 00:01:48.910
which is inside a block,

00:01:48.911 --> 00:01:51.351
which is outside and so forth.

00:01:51.352 --> 00:01:52.591
Now the behavior of indentation

00:01:52.592 --> 00:01:54.936
is highly mode-dependent

00:01:54.937 --> 00:01:58.118
and is really complex enough that it deserves

00:01:58.119 --> 00:01:59.759
a presentation of its own,

00:01:59.760 --> 00:02:01.301
so I won't do that today.

00:02:01.302 --> 00:02:05.964
We'll move on to right justification,

00:02:05.965 --> 00:02:08.626
the mirror of left justification.

00:02:08.627 --> 00:02:10.847
But it's a little tricky in that to get the

00:02:10.848 --> 00:02:13.589
non-whitespace text on the right-hand side

00:02:13.590 --> 00:02:16.491
lined up usually means inserting some space

00:02:16.492 --> 00:02:19.592
at some point in the middle.

00:02:19.593 --> 00:02:22.954
The manual usually calls this just "justification."

00:02:22.955 --> 00:02:25.175
We will be spending some time on it.

00:02:25.176 --> 00:02:28.696
And then finally, column alignment.

00:02:28.697 --> 00:02:31.077
To me, this is the most interesting one.

00:02:31.078 --> 00:02:35.119
This is recognizing portions of lines using

00:02:35.120 --> 00:02:38.281
regular expressions or eventually parsers and

00:02:38.282 --> 00:02:40.542
lining them up either on the left-hand side or

00:02:40.543 --> 00:02:43.964
the right-hand side in what looks like a table or

00:02:43.965 --> 00:02:44.624
something like that.

00:02:44.625 --> 00:02:49.782
And we're going to spend a lot of time on this one.

00:02:49.783 --> 00:02:55.325
But first, we'll talk about alignment in text modes.

NOTE Alignment in text modes: Fill (and justification)

00:02:55.326 --> 00:02:58.267
There is a feature called fill in Emacs, and what

00:02:58.268 --> 00:03:02.929
it is is a way of moving words between lines with

00:03:02.930 --> 00:03:07.092
a goal called the `fill-column` and trying to get

00:03:07.093 --> 00:03:09.133
the lines as long as possible

00:03:09.134 --> 00:03:12.315
while still being below the fill column

00:03:12.316 --> 00:03:15.417
by moving words between them.

00:03:15.418 --> 00:03:18.408
The command `fill-paragraph` is bound to `M-q`.

00:03:18.409 --> 00:03:20.090
If you add a numeric prefix

00:03:20.091 --> 00:03:25.557
like `C-u M-q`, it will right-justify

00:03:25.558 --> 00:03:30.380
to the fill-column if you've set it.

00:03:30.381 --> 00:03:33.261
Fill can also infer an initial prefix

00:03:33.262 --> 00:03:36.082
for each line by looking at the existing text,

00:03:36.083 --> 00:03:37.483
so that filling takes place

00:03:37.484 --> 00:03:41.104
only with respect to the remaining words.

00:03:41.105 --> 00:03:44.305
If you didn't set a fill prefix manually, fill is

00:03:44.306 --> 00:03:46.986
going to try to infer one for each line from the

00:03:46.987 --> 00:03:48.967
leading characters of the existing lines,

00:03:48.968 --> 00:03:51.428
which are typically whitespace, but not always.

00:03:51.429 --> 00:03:55.689
This is known as adaptive filling.

00:03:55.690 --> 00:03:59.567
There is a `left-margin` variable in each buffer

00:03:59.568 --> 00:04:02.867
that you can set, and every time you run `fill-paragraph`,

00:04:02.868 --> 00:04:06.600
the left-hand side will be justified at least to that point.

00:04:06.601 --> 00:04:09.200
And then in addition, you can use commands

00:04:09.201 --> 00:04:11.533
`set-left-margin` and `set-right-margin`

00:04:11.534 --> 00:04:13.700
to control the padding at different words,

00:04:13.701 --> 00:04:16.133
so that you can vary it a little bit

00:04:16.134 --> 00:04:19.080
more granularly than the full buffer.

00:04:19.081 --> 00:04:26.419
Let's look at some examples.

00:04:26.420 --> 00:04:29.988
First note that this very long run-on line here

00:04:29.989 --> 00:04:31.969
begins with two spaces.

00:04:31.970 --> 00:04:35.231
When we hit `M-q`, like that, and line

00:04:35.232 --> 00:04:38.634
everything up, now these lines are filled.

00:04:38.635 --> 00:04:41.836
Instead of one line, there's five, and each line

00:04:41.837 --> 00:04:44.738
begins with that same pair of spaces.

00:04:44.739 --> 00:04:47.580
This is adaptive fill at work.

00:04:47.581 --> 00:04:52.196
Let's try right-justifying with `C-u M-q`.

00:04:52.197 --> 00:04:54.077
Now you can see that all lines except for the

00:04:54.078 --> 00:04:57.759
last one are lined up on the right-hand side.

00:04:57.760 --> 00:05:00.842
Let's try setting the global left margin

00:05:00.843 --> 00:05:03.404
in this buffer.

00:05:03.405 --> 00:05:08.908
`(setq left-margin 5)`.

00:05:08.909 --> 00:05:14.813
And if I fill again, now there's at least five

00:05:14.814 --> 00:05:16.959
spaces on the left side.

00:05:16.960 --> 00:05:20.156
Maybe we want a couple of lines to have extra

00:05:20.157 --> 00:05:21.576
left margin.

00:05:21.577 --> 00:05:25.460
Then we can select them, pick a region, say this

00:05:25.461 --> 00:05:30.905
part, and now if we use the command

00:05:30.906 --> 00:05:38.952
`set-left-margin` and we set it to, say, 7,

00:05:38.953 --> 00:05:41.214
now we have seven spaces on the left-hand side

00:05:41.215 --> 00:05:45.724
but just for these and if I run `M-q` again,

00:05:45.725 --> 00:05:51.188
we get actually the global 5 left margin

00:05:51.189 --> 00:05:55.330
plus that additional 7 that we asked for.

00:05:55.331 --> 00:05:57.632
We can do the same thing with the right margin.

00:05:57.633 --> 00:06:00.434
Let's just quickly try that.

00:06:00.435 --> 00:06:06.137
If I set that right margin here, and I set it to

00:06:06.138 --> 00:06:10.380
say, I don't know, another 7 maybe, and then I

00:06:10.381 --> 00:06:14.062
fill again, then these last two lines

00:06:14.063 --> 00:06:16.467
have extra margin,

00:06:16.468 --> 00:06:19.549
just as the first two had extra margin on the

00:06:19.550 --> 00:06:21.110
left-hand side.

00:06:21.111 --> 00:06:26.133
So that's the fill feature.

NOTE Column alignment

00:06:26.134 --> 00:06:28.595
We can also do column alignment in text.

00:06:28.596 --> 00:06:30.035
Let's have a look.

00:06:30.036 --> 00:06:31.957
There are two kind of ways that you can do column

00:06:31.958 --> 00:06:33.417
alignment easily.

00:06:33.418 --> 00:06:36.920
One of them is this interesting built-in feature

00:06:36.921 --> 00:06:41.362
where in text mode, if you use the tab key, it's

00:06:41.363 --> 00:06:43.264
going to bring you to the beginning of

00:06:43.265 --> 00:06:45.905
every word in the previous line.

00:06:45.905 --> 00:06:50.705
So we tab here, we can say some text here.

00:06:50.705 --> 00:06:53.265
And every... I just tabbed in between those words.

00:06:53.265 --> 00:06:55.905
So it makes it really easy to line things up.

00:06:58.380 --> 00:07:01.116
There's also the align command, and there's a

00:07:01.117 --> 00:07:05.097
whole align subsystem which we'll be getting into here.

00:07:05.098 --> 00:07:08.319
By default, it will line up words into columns

00:07:08.320 --> 00:07:12.940
with the align command, the `M-x align`, if you

00:07:12.941 --> 00:07:14.767
select a region first.

00:07:14.768 --> 00:07:17.648
There are different default rules for each mode,

00:07:17.649 --> 00:07:21.249
so I'm just going to show you the text mode one.

00:07:21.250 --> 00:07:24.690
Usually, in most modes, there are some special

00:07:24.691 --> 00:07:27.050
alignment commands or special alignment rules,

00:07:27.051 --> 00:07:29.491
I should say, that are more advanced,

00:07:29.492 --> 00:07:33.212
and that you can get by using a numeric prefix.

00:07:33.213 --> 00:07:35.112
So let's try this out.

00:07:35.113 --> 00:07:37.113
We go here and edit this thing.

00:07:37.114 --> 00:07:39.253
If I select this whole thing

00:07:39.254 --> 00:07:42.779
and I say `M-x align`,

00:07:42.780 --> 00:07:45.261
then it aligns everything on spaces.

00:07:45.262 --> 00:07:47.843
Now the interesting extra rule that you can use

00:07:47.844 --> 00:07:49.624
aligns things on decimal points.

00:07:49.625 --> 00:07:51.826
It's actually designed for currencies.

00:07:51.827 --> 00:07:54.668
So if I say `C--` [control dash],

00:07:54.669 --> 00:07:57.270
which is a negative prefix argument,

00:07:57.271 --> 00:08:00.966
and I run `M-x align` again,

00:08:00.967 --> 00:08:02.567
now these numbers on the right hand side are

00:08:02.568 --> 00:08:05.088
actually aligned on their decimal points.

00:08:05.089 --> 00:08:06.588
For more information, you can check out

00:08:06.589 --> 00:08:09.769
`align-rules-list`, a variable that lists

00:08:09.770 --> 00:08:11.090
all of the alignment rules

00:08:11.091 --> 00:08:15.732
that are in Emacs by default for each mode.

NOTE Alignment in code: Fill in comments

00:08:15.733 --> 00:08:17.532
Now, alignment in code.

00:08:17.533 --> 00:08:19.473
This may be a little bit more interesting.

00:08:19.474 --> 00:08:24.775
There's still fill in code, mostly in comments,

00:08:24.776 --> 00:08:27.096
but column alignment is huge.

00:08:27.097 --> 00:08:29.296
So let's look at fill in code.

00:08:29.297 --> 00:08:31.237
Basically this is in comments,

00:08:31.238 --> 00:08:32.577
but let's check this out.

00:08:32.578 --> 00:08:34.437
Let's see what this comment looks like.

00:08:34.438 --> 00:08:37.618
This time the lines don't begin with spaces.

00:08:37.619 --> 00:08:39.819
They begin with semicolons.

00:08:39.820 --> 00:08:43.459
And if I do `M-q` now and call

00:08:43.460 --> 00:08:46.060
`fill-paragraph`, it still works,

00:08:46.061 --> 00:08:46.860
even though the lines

00:08:46.861 --> 00:08:48.900
all began with semicolons and it doesn't try to

00:08:48.901 --> 00:08:50.341
insert the semicolons in the middle.

00:08:50.342 --> 00:08:52.898
It understands that these are special.

00:08:52.899 --> 00:08:55.160
Let's try setting the fill column here.

00:08:55.161 --> 00:08:56.801
Let's move back to about right here.

00:08:56.802 --> 00:08:59.063
`C-x f` sets the fill column.

00:08:59.064 --> 00:09:00.024
By default, it's 70.

00:09:00.025 --> 00:09:02.006
We're sending it to 58 now.

00:09:02.007 --> 00:09:05.769
Now if I do `M-q` again, it squeezed the

00:09:05.770 --> 00:09:07.691
paragraph down, but the other interesting thing

00:09:07.692 --> 00:09:09.833
that it did is it added two semicolons at the

00:09:09.834 --> 00:09:11.394
beginning of the last line.

00:09:11.395 --> 00:09:17.019
That's adaptive fill at work.

NOTE Column alignment for code elements

00:09:17.020 --> 00:09:21.062
Now let's talk about column alignment in code.

00:09:21.063 --> 00:09:23.184
I'm going to use a specific example drawn from

00:09:23.185 --> 00:09:28.808
real C++ code defining the bits of a register

00:09:28.809 --> 00:09:32.711
using a bitmask.

00:09:32.712 --> 00:09:35.633
The C++ code on the right hand side is a

00:09:35.634 --> 00:09:40.216
definition of the bit in a hardware register.

00:09:40.217 --> 00:09:42.897
You have names on one side, the name of the bit,

00:09:42.898 --> 00:09:46.078
and on the right-hand side is a number that's a

00:09:46.079 --> 00:09:49.859
power of two, which indicates you can use it as a

00:09:49.860 --> 00:09:52.900
mask if you do bitwise arithmetic, and it can

00:09:52.901 --> 00:09:56.981
help you change or access the values of bits in a

00:09:56.982 --> 00:10:01.182
register in a useful way inside your code.

00:10:01.183 --> 00:10:04.083
We can align the two columns, that is the names

00:10:04.084 --> 00:10:06.804
and the values, in two steps using `align-regexp`,

00:10:06.805 --> 00:10:11.579
which is a command.

00:10:11.580 --> 00:10:12.447
Let's give this a try,

00:10:12.448 --> 00:10:13.908
but we're going to do the first one first.

00:10:13.909 --> 00:10:16.319
This one will align everything

00:10:16.320 --> 00:10:18.610
before the equal sign.

00:10:18.611 --> 00:10:19.311
This one here.

00:10:19.312 --> 00:10:22.132
So let's try just running it.

00:10:22.133 --> 00:10:24.173
Now you can see on the right-hand side that

00:10:24.174 --> 00:10:28.855
everything before the equal sign has been aligned.

00:10:28.856 --> 00:10:31.977
But we have one thing down at the bottom that

00:10:31.978 --> 00:10:34.538
doesn't look very nice, because we also have to

00:10:34.539 --> 00:10:36.619
align the numbers on the right-hand side of the

00:10:36.620 --> 00:10:37.939
equal sign.

00:10:37.940 --> 00:10:39.787
And so to do that,

00:10:39.788 --> 00:10:43.128
I'll uncomment this and we run it again.

00:10:43.129 --> 00:10:46.289
And now MPIE is lined up as well.

00:10:46.290 --> 00:10:47.229
The only difference between

00:10:47.230 --> 00:10:49.310
these two commands here

00:10:49.311 --> 00:10:51.830
is just this first thing.

00:10:51.831 --> 00:10:54.991
This argument here, it comes after that gnarly

00:10:54.992 --> 00:10:58.032
regular expression, is the group number,

00:10:58.033 --> 00:11:00.113
so it refers to something called a capture

00:11:00.114 --> 00:11:02.173
group inside the regular expression.

00:11:02.174 --> 00:11:06.134
It tells us that we want to first do the

00:11:06.135 --> 00:11:07.835
first capture group and then do the second

00:11:07.836 --> 00:11:09.916
capture group.

00:11:09.917 --> 00:11:11.957
So the rest of the arguments tell you how many

00:11:11.958 --> 00:11:17.819
spaces to use and whether to repeat.

00:11:17.820 --> 00:11:19.600
Let's take a moment to analyze the regular

00:11:19.601 --> 00:11:22.381
expression we just used a little bit.

00:11:22.382 --> 00:11:26.563
So they're kind of gnarly-looking and they're

00:11:26.564 --> 00:11:29.304
very similar to regular expressions used in other

00:11:29.305 --> 00:11:31.665
languages, but not exactly.

00:11:31.666 --> 00:11:34.166
So let's look at this one.

00:11:34.167 --> 00:11:37.146
This one we use...

00:11:37.147 --> 00:11:42.028
We use these things called character classes,

00:11:42.029 --> 00:11:43.809
which is the colon,

00:11:43.810 --> 00:11:45.569
the things in the bracket here.

00:11:45.570 --> 00:11:47.170
So instead of listing out all the valid

00:11:47.171 --> 00:11:48.651
characters here,

00:11:48.652 --> 00:11:50.911
we put it using this special syntax.

00:11:50.912 --> 00:11:54.513
So this says any space character is here,

00:11:54.514 --> 00:11:57.914
and this one means any hexadecimal digits,

00:11:57.915 --> 00:12:00.936
so 0 through 9, A through F.

00:12:00.937 --> 00:12:03.037
We also have these capture group things in here

00:12:03.038 --> 00:12:04.117
which are these parentheses.

00:12:04.118 --> 00:12:06.178
Now, normally, parentheses in regular expressions

00:12:06.179 --> 00:12:08.159
in Emacs are not special,

00:12:08.160 --> 00:12:11.340
so you have to have a backslash in front of them.

00:12:11.341 --> 00:12:15.181
And also because we're writing strings here,

00:12:15.182 --> 00:12:18.702
backslashes are significant to the string reader.

00:12:18.703 --> 00:12:22.024
So you actually have to have two backslashes

00:12:22.025 --> 00:12:24.985
to get the capture groups right.

00:12:24.986 --> 00:12:26.885
Here you can see a little diagram I

00:12:26.886 --> 00:12:29.759
made of how the capture groups work.

00:12:29.760 --> 00:12:32.781
If you read all of this together, it's zero or

00:12:32.782 --> 00:12:35.742
more white space characters in a group,

00:12:35.743 --> 00:12:38.084
an equal sign, another zero or more

00:12:38.085 --> 00:12:40.385
whitespace characters in a group,

00:12:40.386 --> 00:12:44.427
and then a hexadecimal number.

00:12:44.428 --> 00:12:46.708
So let's try right justifying now.

00:12:46.709 --> 00:12:48.789
We did left justification before.

00:12:48.790 --> 00:12:52.331
We're going to change our `align-regexp` calls so

00:12:52.332 --> 00:12:56.594
that they are slightly different in two ways.

00:12:56.595 --> 00:12:58.809
First of all,

00:12:58.810 --> 00:13:00.590
in the one where we want to right-justify,

00:13:00.591 --> 00:13:02.751
we've made the group number negative.

00:13:02.752 --> 00:13:05.212
The other thing that we've done, we've gone down

00:13:05.213 --> 00:13:07.532
here and this capture group

00:13:07.533 --> 00:13:10.113
inside the parentheses now contains more stuff.

00:13:10.114 --> 00:13:12.834
It contains the non-whitespace characters

00:13:12.835 --> 00:13:13.835
that we want to capture.

00:13:13.836 --> 00:13:15.575
That's because you have to include the

00:13:15.576 --> 00:13:18.497
non-whitespace for the Emacs alignment code to

00:13:18.498 --> 00:13:22.338
figure out how far over the whole thing goes,

00:13:22.339 --> 00:13:24.479
so that it can line everything up.

00:13:24.480 --> 00:13:26.239
We'll make these two changes and let's see

00:13:26.240 --> 00:13:28.548
what happens when we run this.

00:13:28.549 --> 00:13:28.928
Bam!

00:13:28.929 --> 00:13:31.229
Now they're all lined up on the right-hand side

00:13:31.230 --> 00:13:35.211
as well with just these two changes to the calls.

00:13:35.212 --> 00:13:36.611
We can use a different function called

00:13:36.612 --> 00:13:38.952
`align-region` that allows us to combine those two

00:13:38.953 --> 00:13:40.913
calls to `align-regexp`.

00:13:40.914 --> 00:13:44.354
This allows us to do multiple columns at once.

00:13:44.355 --> 00:13:47.655
It uses rules instead of regular expressions.

00:13:47.656 --> 00:13:49.096
So the rules also specify

00:13:49.097 --> 00:13:51.097
the regular expression as before.

00:13:51.098 --> 00:13:53.738
They list the groups that you're interested in.

00:13:53.739 --> 00:13:57.879
Here we've noted that we need to justify, also,

00:13:57.880 --> 00:14:02.100
instead of using a negative group number.

00:14:02.101 --> 00:14:03.941
Let's see if I can undo any of this.

00:14:03.942 --> 00:14:04.441
Oh, there we go.

00:14:04.442 --> 00:14:07.342
Okay, now let's try... Now let's run this one.

00:14:07.343 --> 00:14:09.062
So this is a different way of achieving the same

00:14:09.063 --> 00:14:13.264
goal, but with a rule instead of simply a regexp.

00:14:13.265 --> 00:14:15.064
So this leaves us with a left-justified first

00:14:15.065 --> 00:14:17.745
column and a right-justified second one using a

00:14:17.746 --> 00:14:22.456
line region instead of a line regex called twice.

00:14:22.457 --> 00:14:25.478
And as an aside, when you're debugging these

00:14:25.479 --> 00:14:26.978
regular expressions, which can be a little

00:14:26.979 --> 00:14:29.160
challenging to get right, there's a function

00:14:29.161 --> 00:14:31.141
called `align-highlight-rule`.

00:14:31.142 --> 00:14:33.422
And if you're using `align-region`, it's really easy

00:14:33.423 --> 00:14:36.844
to use because it's basically the same syntax.

00:14:36.845 --> 00:14:38.645
It produces an image that looks like the one

00:14:38.646 --> 00:14:42.527
on the right with little highlights everywhere

00:14:42.528 --> 00:14:46.269
the capture group would be.

NOTE Structured regular expressions with rx

00:14:46.270 --> 00:14:48.450
Now, the regular expression construction, as we

00:14:48.451 --> 00:14:51.026
discussed, can be a little cryptic.

00:14:51.027 --> 00:14:53.708
You want a feature that allows you to write

00:14:53.709 --> 00:14:56.129
things in human language.

00:14:56.130 --> 00:14:58.630
And rx is exactly that feature.

00:14:58.631 --> 00:15:00.391
So this is the English language description

00:15:00.392 --> 00:15:03.133
basically in comments that I showed before.

00:15:03.134 --> 00:15:07.816
Let's see what happens when we run this.

00:15:07.817 --> 00:15:10.397
That rx expression resulted in a string, and the

00:15:10.398 --> 00:15:12.398
string is just exactly the thing that we wrote

00:15:12.399 --> 00:15:13.659
manually earlier.

00:15:13.660 --> 00:15:16.961
So rx can really help.

NOTE Replacing regexps with PEG parsers

00:15:16.962 --> 00:15:19.462
Let's replace our regular expression

00:15:19.463 --> 00:15:20.854
with a parser.

00:15:20.855 --> 00:15:22.315
Why would we want to do this?

00:15:22.316 --> 00:15:24.696
Well, sometimes you want to align things that

00:15:24.697 --> 00:15:28.038
require a parser to recognize the thing that

00:15:28.039 --> 00:15:31.399
you're trying to align, and a regular expression

00:15:31.400 --> 00:15:32.300
might not be enough.

00:15:32.301 --> 00:15:35.661
Parenthesized expressions are a classic example,

00:15:35.662 --> 00:15:37.943
as is HTML.

00:15:37.944 --> 00:15:40.504
But also for the inputs that they can handle,

00:15:40.505 --> 00:15:42.005
sometimes a parser is more readable

00:15:42.006 --> 00:15:43.305
because you can write rules

00:15:43.306 --> 00:15:45.889
and rules can be repeated.

00:15:45.890 --> 00:15:48.411
Finally, parsers can return more than pass, fail,

00:15:48.412 --> 00:15:50.172
and match data.

00:15:50.173 --> 00:15:51.854
In the parsing framework we're going to use,

00:15:51.855 --> 00:15:55.696
we have parser actions that allow us to extract more

00:15:55.697 --> 00:15:58.178
information from the parser run.

00:15:58.179 --> 00:16:00.600
Let's write a small example parser using the

00:16:00.601 --> 00:16:03.462
peg.el framework that's built into Emacs.

00:16:03.463 --> 00:16:06.485
Our parser will recognize arbitrarily-nested

00:16:06.486 --> 00:16:09.287
bitwise OR operations on hexadecimal numbers,

00:16:09.288 --> 00:16:11.708
which is useful for bitmasks.

00:16:11.709 --> 00:16:13.630
There are two rules in our grammar

00:16:13.631 --> 00:16:15.729
and one parser action.

00:16:15.730 --> 00:16:18.230
The first rule describes a hexadecimal number in

00:16:18.231 --> 00:16:21.392
very similar terms to those we used for rx.

00:16:21.393 --> 00:16:24.333
It also uses a parser action called `substring` to

00:16:24.334 --> 00:16:28.255
ensure we collect the hex numbers during parsing.

00:16:28.256 --> 00:16:30.496
The second rule is recursively defined, which

00:16:30.497 --> 00:16:33.497
allows it to handle expressions of any depth.

00:16:33.498 --> 00:16:37.439
Let's try the whole thing out.

00:16:37.440 --> 00:16:39.560
We successfully collected the numbers that were

00:16:39.561 --> 00:16:43.674
in the test data.

00:16:43.675 --> 00:16:45.996
Let's make a more complex parser to handle our

00:16:45.997 --> 00:16:49.018
bitmasks formatting application.

00:16:49.019 --> 00:16:52.360
We have one rule, spaces, that we reuse in

00:16:52.361 --> 00:16:55.883
several places.

00:16:55.884 --> 00:16:58.785
Our final rule looks very much like the Rx

00:16:58.786 --> 00:17:01.907
definition of the enum line matcher.

00:17:01.908 --> 00:17:05.410
Let's see what the whole thing does.

00:17:05.411 --> 00:17:08.472
It just returned true, and the reason is that we

00:17:08.473 --> 00:17:12.452
don't have any parser actions that do anything special.

00:17:12.453 --> 00:17:14.914
And true just means that we successfully parsed

00:17:14.915 --> 00:17:17.216
the input.

00:17:17.217 --> 00:17:20.578
Let's add some parser actions.

00:17:20.579 --> 00:17:23.320
A regex capture group remembers the range of

00:17:23.321 --> 00:17:25.222
locations it matched.

00:17:25.223 --> 00:17:27.585
It would be helpful to have the same thing with

00:17:27.586 --> 00:17:28.906
our parser.

00:17:28.907 --> 00:17:32.269
That's what the region parser action is for.

00:17:32.270 --> 00:17:34.050
Here we've wrapped parts of the grammar that

00:17:34.051 --> 00:17:36.052
correspond to capture groups

00:17:36.053 --> 00:17:42.250
with the region action.

00:17:42.251 --> 00:17:46.631
Let's see what we get.

00:17:46.632 --> 00:17:48.772
We have four numbers that correspond

00:17:48.773 --> 00:17:50.773
to the starts and ends

00:17:50.774 --> 00:17:55.354
of what we used to call capture groups.

00:17:55.355 --> 00:17:58.415
True match data must add one more pair of numbers

00:17:58.416 --> 00:18:00.696
at the beginning for the region matched by the

00:18:00.697 --> 00:18:04.677
parser overall, giving us a total of six numbers.

00:18:04.678 --> 00:18:09.837
We'll do that in some extra code.

00:18:09.838 --> 00:18:12.098
We now have all the information the regex would

00:18:12.099 --> 00:18:13.479
have produced.

00:18:13.480 --> 00:18:16.241
How do we substitute our parser in?

00:18:16.242 --> 00:18:19.883
Well, it turns out that `align-region` accepts any

00:18:19.884 --> 00:18:22.585
function meeting certain requirements as a

00:18:22.586 --> 00:18:25.987
substitute for a string-based regular expression.

00:18:25.988 --> 00:18:27.888
It just has to emulate a function called

00:18:27.889 --> 00:18:30.990
`re-search-forward`, which performs a regex search

00:18:30.991 --> 00:18:32.871
within the buffer.

00:18:32.872 --> 00:18:38.896
Let's see how we can do that.

00:18:38.897 --> 00:18:39.896
This is a function

00:18:39.897 --> 00:18:42.877
`parse-enum-assignment-set-matchdata`.

00:18:42.878 --> 00:18:45.797
It takes the required two arguments,

00:18:45.798 --> 00:18:47.558
which it doesn't use,

00:18:47.559 --> 00:18:49.858
sets the global match data when it's done,

00:18:49.859 --> 00:18:52.019
and returns true.

00:18:52.020 --> 00:18:58.640
Let's try it out and print the match data.

00:18:58.641 --> 00:18:59.901
That looks right.

00:18:59.902 --> 00:19:02.821
We now have the required six numbers.

00:19:02.822 --> 00:19:05.682
Let's try substituting this function

00:19:05.683 --> 00:19:07.681
we just defined

00:19:07.682 --> 00:19:10.244
in our call to `align-region`.

00:19:10.245 --> 00:19:14.448
Let's try it.

00:19:14.449 --> 00:19:15.489
Awesome.

00:19:15.490 --> 00:19:21.090
Everything looks good now.

00:19:21.091 --> 00:19:24.073
So we covered vertical alignment commands for

00:19:24.074 --> 00:19:27.356
both text and code, custom column alignment with

00:19:27.357 --> 00:19:30.339
regular expressions, using rx to make the

00:19:30.340 --> 00:19:32.802
expression more readable, extending column

00:19:32.803 --> 00:19:36.005
alignment with Emacs Lisp, and replacing regexp

00:19:36.006 --> 00:19:38.067
with peg.el parsers.

00:19:38.068 --> 00:19:38.829
Thanks for watching.