diff options
Diffstat (limited to '2026/captions')
4 files changed, 2171 insertions, 0 deletions
diff --git a/2026/captions/emacsconf-2026-align--columnaligning-text-in-emacs--jeff-trull--main--chapters.vtt b/2026/captions/emacsconf-2026-align--columnaligning-text-in-emacs--jeff-trull--main--chapters.vtt new file mode 100644 index 00000000..1c5c3e0c --- /dev/null +++ b/2026/captions/emacsconf-2026-align--columnaligning-text-in-emacs--jeff-trull--main--chapters.vtt @@ -0,0 +1,26 @@ +WEBVTT + + +00:00:00.617 --> 00:01:00.347 +Lining things up: Introduction + +00:01:00.348 --> 00:02:55.325 +Terminology: justification, indentation, column alignment + +00:02:55.326 --> 00:06:26.133 +Alignment in text modes: Fill (and justification) + +00:06:26.134 --> 00:08:15.732 +Column alignment + +00:08:15.733 --> 00:09:17.019 +Alignment in code: Fill in comments + +00:09:17.020 --> 00:14:46.269 +Column alignment for code elements + +00:14:46.270 --> 00:15:16.961 +Structured regular expressions with rx + +00:15:16.962 --> 00:19:38.829 +Replacing regexps with PEG parsers diff --git a/2026/captions/emacsconf-2026-align--columnaligning-text-in-emacs--jeff-trull--main.vtt b/2026/captions/emacsconf-2026-align--columnaligning-text-in-emacs--jeff-trull--main.vtt new file mode 100644 index 00000000..a001f079 --- /dev/null +++ b/2026/captions/emacsconf-2026-align--columnaligning-text-in-emacs--jeff-trull--main.vtt @@ -0,0 +1,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. diff --git a/2026/captions/emacsconf-2026-babel--org-babel-everywhere--george-huebner--main--chapters.vtt b/2026/captions/emacsconf-2026-babel--org-babel-everywhere--george-huebner--main--chapters.vtt new file mode 100644 index 00000000..22a27cb2 --- /dev/null +++ b/2026/captions/emacsconf-2026-babel--org-babel-everywhere--george-huebner--main--chapters.vtt @@ -0,0 +1,35 @@ +WEBVTT + + +00:00:00.480 --> 00:00:37.640 +Introduction + +00:00:37.640 --> 00:01:10.280 +What happens when we want to use Org babel with a language that's not supported by default? + +00:01:10.280 --> 00:01:29.520 +Version information + +00:01:29.520 --> 00:02:34.880 +The technique + +00:02:34.880 --> 00:03:27.333 +Budget ob-LANG + +00:03:27.334 --> 00:04:40.120 +Library of Babel + +00:04:40.120 --> 00:07:20.800 +Examples + +00:07:20.800 --> 00:09:11.480 +Serial + +00:09:11.480 --> 00:09:25.600 +Parallel map + +00:09:25.600 --> 00:10:24.640 +Tangle map + +00:10:24.640 --> 00:10:42.720 +Conclusion diff --git a/2026/captions/emacsconf-2026-babel--org-babel-everywhere--george-huebner--main.vtt b/2026/captions/emacsconf-2026-babel--org-babel-everywhere--george-huebner--main.vtt new file mode 100644 index 00000000..61b31e1c --- /dev/null +++ b/2026/captions/emacsconf-2026-babel--org-babel-everywhere--george-huebner--main.vtt @@ -0,0 +1,839 @@ +WEBVTT captioned by George Huebner + +NOTE Introduction + +00:00:00.480 --> 00:00:02.439 +Alright, we have a lot to get through and not a + +00:00:02.440 --> 00:00:03.899 +whole lot of time, so I'm just going to preface + +00:00:03.900 --> 00:00:05.399 +the talk by saying all the materials I use will + +00:00:05.400 --> 00:00:07.740 +be available on the talk page at emacsconf.org. + +00:00:07.940 --> 00:00:09.999 +I also encourage you to stop and pause the talk + +00:00:10.000 --> 00:00:11.959 +so you can try the demos at home, and of course + +00:00:11.960 --> 00:00:13.220 +you can ask questions after the talk. + +00:00:14.320 --> 00:00:15.820 +Alright, let's get into it! + +00:00:17.420 --> 00:00:19.839 +Alright, so Org babel is a very powerful system for + +00:00:19.840 --> 00:00:20.580 +literate programming. + +00:00:21.280 --> 00:00:22.780 +There is extensive language support, + +00:00:23.980 --> 00:00:25.439 +we can make our code more flexible through the + +00:00:25.440 --> 00:00:27.180 +usage of header arguments and variables, + +00:00:27.780 --> 00:00:30.279 +we can use noweb syntax to dovetail functions, + +00:00:30.280 --> 00:00:31.960 +even those written in different languages, + +00:00:32.860 --> 00:00:35.079 +and we can also use TRAMP to execute source + +00:00:35.080 --> 00:00:36.240 +blocks on remote machines. + +NOTE What happens when we want to use Org babel with a language that's not supported by default? + +00:00:37.540 --> 00:00:39.159 +What happens when we want to use Org babel with a + +00:00:39.160 --> 00:00:40.480 +language that's not supported by default? + +00:00:41.380 --> 00:00:43.780 +Hmm, no <i>org-babel-execute</i> function for Zig. + +00:00:44.380 --> 00:00:46.599 +Well, we could write our own ob-zig.el, but that + +00:00:46.600 --> 00:00:48.319 +would bring our .emacs.d 100 lines closer to + +00:00:48.320 --> 00:00:49.540 +"Emacs bankruptcy", + +00:00:49.840 --> 00:00:51.919 +and it would also add a dependency to this + +00:00:51.920 --> 00:00:53.640 +Org mode document: if you don't have ob-zig, + +00:00:53.740 --> 00:00:55.260 +you can't execute the source block. + +00:00:55.560 --> 00:00:57.319 +So, in today's talk, I'm going to show you how to + +00:00:57.320 --> 00:00:58.920 +define an arbitrary "runnable" + +00:00:59.020 --> 00:01:01.279 +that not only allows you to recover the + +00:01:01.280 --> 00:01:03.440 +functionality of <i>org-babel-execute-src-block</i>, + +00:01:03.540 --> 00:01:05.679 +but do so much more by having an arbitrary + +00:01:05.680 --> 00:01:06.959 +function on some source code— + +00:01:06.960 --> 00:01:08.359 +that could be compiling and running, but it could + +00:01:08.360 --> 00:01:09.580 +also be other stuff. + +NOTE Version information + +00:01:10.280 --> 00:01:12.479 +So, before we get into things, I do just want to + +00:01:12.480 --> 00:01:13.919 +clarify that I'm using a pretty tricked out + +00:01:13.920 --> 00:01:16.599 +personal build of Emacs and also tecosaur's fork of + +00:01:16.600 --> 00:01:17.319 +Org mode. + +00:01:17.320 --> 00:01:19.359 +But everything I talk about in today's talk you + +00:01:19.360 --> 00:01:21.919 +can use in a version of Emacs from probably the + +00:01:21.920 --> 00:01:23.240 +past 5 years or so. + +00:01:23.440 --> 00:01:25.639 +So not to worry, you can run everything at home. + +00:01:25.640 --> 00:01:28.180 +(You will need some external packages like, for example, Zig.) + +NOTE The technique + +00:01:29.420 --> 00:01:32.199 +Ok, so here's the technique: it's literally just + +00:01:32.200 --> 00:01:33.759 +one line of ELisp. + +00:01:33.760 --> 00:01:35.199 +I like to call this "block-code". + +00:01:35.200 --> 00:01:36.599 +So, what's going on here? + +00:01:36.600 --> 00:01:39.159 +<i>org-babel-expand-noweb-references</i>, and + +00:01:39.160 --> 00:01:40.600 +then <i>org-babel-lob--src-info</i>... + +00:01:40.700 --> 00:01:43.119 +all this is doing is taking the name of an + +00:01:43.120 --> 00:01:46.119 +Org babel block, and it's returning the content of + +00:01:46.120 --> 00:01:48.239 +the source code of that block. + +00:01:48.240 --> 00:01:50.780 +We will use this using Org's CALL keyword: + +00:01:51.080 --> 00:01:53.199 +we have some runnable code block, we use + +00:01:53.200 --> 00:01:54.679 +"block-code", and then we pass in the name + +00:01:54.680 --> 00:01:56.600 +of the code block we want to execute. + +00:01:57.100 --> 00:01:58.879 +That's all a little bit abstract, so I think it's + +00:01:58.880 --> 00:02:00.080 +easier to show than tell. + +00:02:00.480 --> 00:02:02.919 +Here I have some ELisp test block, just counts + +00:02:02.920 --> 00:02:05.280 +the number of lines in my narrowed buffer. + +00:02:06.280 --> 00:02:08.479 +Obviously I can press <b>C-c C-c</b> like I just did, + +00:02:08.480 --> 00:02:10.079 +but for the sake of demonstration, let's use + +00:02:10.080 --> 00:02:10.679 +a runnable. + +00:02:10.680 --> 00:02:13.239 +So here I have "eval", that just evaluates a string + +00:02:13.240 --> 00:02:16.119 +as an S-expression, and here I have "eval", + +00:02:16.120 --> 00:02:17.759 +"block-code", "elisp-test". + +00:02:17.760 --> 00:02:20.900 +So I put my point over this line, I press <b>C-c C-c</b>, + +00:02:21.200 --> 00:02:22.820 +and as you can see I get the same thing. + +00:02:23.220 --> 00:02:25.039 +So that might be a little underwhelming, we just + +00:02:25.040 --> 00:02:27.400 +accomplished the same thing as ob-elisp.el + +00:02:27.500 --> 00:02:28.640 +but with extra steps. + +00:02:28.740 --> 00:02:30.799 +But in the coming examples I'm going to show, I think + +00:02:30.800 --> 00:02:32.919 +you will find that "block-code" is small + +00:02:32.920 --> 00:02:34.080 +but mighty. + +NOTE Budget ob-LANG + +00:02:34.780 --> 00:02:37.200 +Ok, so let's return to the example of Zig from + +00:02:37.220 --> 00:02:37.719 +the beginning. + +00:02:37.720 --> 00:02:39.439 +As a reminder, we cannot use + +00:02:39.440 --> 00:02:40.640 +<i>org-babel-execute-src-block</i> on this. + +00:02:41.440 --> 00:02:44.040 +Down here I define a runnable which is only + +00:02:44.140 --> 00:02:45.040 +4 lines of code + +00:02:45.240 --> 00:02:47.340 +(in fairness, Zig is pulling a lot of weight for us here), + +00:02:47.440 --> 00:02:48.740 +and I'm defining this in Bash. + +00:02:48.940 --> 00:02:52.039 +You can use things other than Bash and ELisp, + +00:02:52.040 --> 00:02:53.960 +anything supported by Org babel by default, but I + +00:02:54.060 --> 00:02:56.300 +would encourage you to use ELisp or Bash + +00:02:56.400 --> 00:02:58.039 +because it's the most portable. + +00:02:58.040 --> 00:03:00.530 +So we call this down here, and... + +00:03:00.730 --> 00:03:02.000 +here we go, we get "Hello, world!" + +00:03:02.400 --> 00:03:03.799 +One thing I want to mention is that we're + +00:03:03.800 --> 00:03:06.020 +actually redirecting stderr to stdout, and that's + +00:03:06.320 --> 00:03:08.519 +not a generally desirable thing to do, that might + +00:03:08.520 --> 00:03:10.399 +actually be kind of annoying to do with a + +00:03:10.400 --> 00:03:11.980 +normal Org babel flow, + +00:03:12.280 --> 00:03:14.959 +but because this is an ad-hoc runnable— + +00:03:14.960 --> 00:03:16.479 +it doesn't need to be the most general, it just + +00:03:16.480 --> 00:03:18.050 +needs to fit our use case— + +00:03:18.150 --> 00:03:19.080 +we can just add this in. + +00:03:19.180 --> 00:03:20.999 +Again, that's kind of a recurring theme for this + +00:03:21.000 --> 00:03:24.519 +talk, molding Org babel to fit your use case, + +00:03:24.520 --> 00:03:26.330 +even if it's not the most general thing in the world. + +NOTE Library of Babel + +00:03:27.330 --> 00:03:28.410 +Alright, so the next thing I want to talk about + +00:03:28.420 --> 00:03:30.999 +is Library of Babel, and to do that I have some + +00:03:31.000 --> 00:03:32.719 +code blocks, these are actually from the Org + +00:03:32.720 --> 00:03:34.779 +document I use for job applications/cover letters + +00:03:34.780 --> 00:03:36.039 +and stuff like that, + +00:03:36.040 --> 00:03:38.039 +and this is a little more involved than + +00:03:38.040 --> 00:03:40.100 +"block-code", but it's doing the same thing in spirit— + +00:03:40.200 --> 00:03:42.900 +you can see right here I've got the same thing in spirit. + +00:03:44.100 --> 00:03:46.239 +But I'm doing some additional introspection using + +00:03:46.240 --> 00:03:50.360 +ELisp to pass variables to this inner code block, + +00:03:51.060 --> 00:03:52.960 +and what I want to show here is + +00:03:52.980 --> 00:03:54.580 +—let me tangle this real quick— + +00:03:55.380 --> 00:03:56.839 +I have some file example.org, + +00:03:56.840 --> 00:03:59.839 +which has a block named "lorem-letter", and here + +00:03:59.840 --> 00:04:02.879 +in my other file, org_babel_everywhere.org, I can + +00:04:02.880 --> 00:04:05.920 +actually use "lorem-letter", I can reference it + +00:04:06.020 --> 00:04:07.199 +by prepending the file name to it, + +00:04:07.200 --> 00:04:09.220 +"lorem-letter:example.org". + +00:04:09.420 --> 00:04:13.860 +So I press <b>C-c C-c</b> here... and I open this up, and + +00:04:13.960 --> 00:04:16.620 +we have some lorem ipsum stuff in the file. + +00:04:17.120 --> 00:04:19.079 +So this is a pretty cool technique, that I can + +00:04:19.080 --> 00:04:21.720 +use Org babel's Library of Babel functionality. + +00:04:21.820 --> 00:04:24.399 +I will say, there is a feature where you can add + +00:04:24.400 --> 00:04:25.959 +Org files to your personal "Library of Babel" + +00:04:25.960 --> 00:04:28.320 +and then reference them without prepending the filename. + +00:04:28.520 --> 00:04:30.639 +I'm personally not such a big fan of this because + +00:04:30.640 --> 00:04:32.199 +it makes it harder for someone else to run the + +00:04:32.200 --> 00:04:34.320 +same Org document on their machine. + +00:04:34.420 --> 00:04:36.599 +But maybe it could be useful if you have some + +00:04:36.600 --> 00:04:39.439 +company-wide Emacs config, that everyone has the + +00:04:39.440 --> 00:04:40.620 +same setup everywhere. + +NOTE Examples + +00:04:42.120 --> 00:04:43.399 +We'll get onto the examples. + +00:04:43.400 --> 00:04:45.039 +I just want to emphasize that all of these examples + +00:04:45.040 --> 00:04:47.119 +are real use cases I've actually used, + +00:04:47.120 --> 00:04:48.680 +and none of them are contrived. + +00:04:49.280 --> 00:04:51.860 +The first one is micro VMs, this is an excerpt + +00:04:51.960 --> 00:04:54.560 +from a kernel pwn writeup I made about a year ago. + +00:04:54.860 --> 00:04:57.360 +(I'm just switching the version of Zig that's in my <b>$PATH</b>.) + +00:04:57.960 --> 00:04:59.940 +So, this is some kernel exploit + +00:05:00.640 --> 00:05:02.680 +(the code is not important, just the context is), + +00:05:02.780 --> 00:05:06.660 +it's a vulnerable x86 Linux kernel module. + +00:05:07.160 --> 00:05:09.260 +So I'm going to call this runnable + +00:05:09.360 --> 00:05:10.260 +"pawny-run-exploit". + +00:05:10.360 --> 00:05:13.560 +Ok, we have uid=1337, some stuff, + +00:05:13.660 --> 00:05:14.919 +then we get root, that's generally a good + +00:05:14.920 --> 00:05:15.980 +sign for kernel pwn. + +00:05:16.980 --> 00:05:19.759 +Just to prove there's no movie magic, let's add a + +00:05:19.760 --> 00:05:20.720 +compile error here. + +00:05:21.520 --> 00:05:23.600 +Ok, so we get the error in our *Org-Babel Error Output* + +00:05:23.700 --> 00:05:25.300 +buffer, just as we'd expect. + +00:05:25.700 --> 00:05:27.610 +And also, we can also add + +00:05:27.710 --> 00:05:31.420 +"kernel base are belong to us". + +00:05:32.020 --> 00:05:33.330 +Run this again— + +00:05:33.830 --> 00:05:35.380 +sorry, still have "foobar" here— + +00:05:38.280 --> 00:05:41.740 +and we will see output reflected here. + +00:05:41.940 --> 00:05:43.060 +So what's actually happening? + +00:05:43.160 --> 00:05:44.980 +For the sake of time, I'm going to give a very + +00:05:45.080 --> 00:05:47.020 +high level play-by-play. + +00:05:47.320 --> 00:05:50.599 +So "pawny-compile-exploit", it's cross-compiling + +00:05:50.600 --> 00:05:53.650 +our block of code to x86_64-linux + +00:05:53.750 --> 00:05:55.560 +(I'm currently on aarch64-darwin), + +00:05:56.360 --> 00:05:57.719 +then "pawny-run-command": + +00:05:57.720 --> 00:06:00.960 +ok, so this is going to regenerate an initramfs, + +00:06:01.160 --> 00:06:03.980 +it's going to include the exploit we just compiled, + +00:06:04.080 --> 00:06:06.959 +it also, based on some Org babel variable, + +00:06:06.960 --> 00:06:09.839 +it will change the init shell to be run + +00:06:09.840 --> 00:06:11.599 +as root or an unprivileged user, which is really + +00:06:11.600 --> 00:06:13.140 +useful for debugging a kernel exploit. + +00:06:13.740 --> 00:06:17.200 +Then we're going to run a QEMU micro VM over here, + +00:06:17.900 --> 00:06:20.759 +and we're actually communicating bidirectionally with + +00:06:20.760 --> 00:06:23.039 +the shell in the VM and the Bash process running + +00:06:23.040 --> 00:06:25.220 +this Org babel code block with the power of + +00:06:25.320 --> 00:06:26.719 +Unix sockets and socat. + +00:06:26.720 --> 00:06:28.580 +And then we're sending some <b>$shellcmd</b>. + +00:06:29.480 --> 00:06:31.719 +And then down here, this is an ELisp block I'm using to + +00:06:31.720 --> 00:06:33.520 +clean up the output using Org babel's + +00:06:33.620 --> 00:06:34.620 +:post header argument. + +00:06:34.820 --> 00:06:36.799 +That's just to get rid of ANSI control codes and + +00:06:36.800 --> 00:06:37.780 +boot logs and stuff like that. + +00:06:38.380 --> 00:06:40.860 +So, this is a pretty nice workflow! + +00:06:40.960 --> 00:06:43.700 +I actually used this to solve this challenge, and + +00:06:43.800 --> 00:06:45.620 +we also get all the trimmings of Org mode: + +00:06:45.820 --> 00:06:49.659 +I can use noweb syntax, I can reverse tangle and then I + +00:06:49.660 --> 00:06:52.180 +can use LSP in a proper zig-mode buffer, + +00:06:52.380 --> 00:06:56.100 +I can also use Org export and I can export this code + +00:06:56.200 --> 00:07:00.380 +and the results to a PDF or a website. + +00:07:00.580 --> 00:07:02.940 +I used ox-hugo to export this exact writeup + +00:07:03.040 --> 00:07:03.960 +to my website. + +00:07:04.560 --> 00:07:06.359 +Overall, just a really cool technique. + +00:07:06.360 --> 00:07:08.999 +And again, to remind you: I can send this Org + +00:07:09.000 --> 00:07:10.520 +document to you, and you can run the exact same + +00:07:10.620 --> 00:07:12.180 +thing on base Emacs + +00:07:12.380 --> 00:07:13.839 +(with no extra ELisp packages). + +00:07:13.840 --> 00:07:15.999 +You will obviously need QEMU and Zig to run this + +00:07:16.000 --> 00:07:19.000 +stuff, but yeah, that's pretty cool. + +00:07:20.800 --> 00:07:22.959 +For the next example, I thought if I can talk to + +00:07:22.960 --> 00:07:25.839 +a VM over socat, why not do the same thing using + +00:07:25.840 --> 00:07:27.460 +the rich functionality of Emacs core? + +NOTE Serial + +00:07:27.560 --> 00:07:30.180 +So I made this little interactive serial REPL, + +00:07:30.380 --> 00:07:32.339 +which again, doesn't require any external functionality, + +00:07:32.340 --> 00:07:36.100 +it's all in ELisp—30-odd lines of ELisp— + +00:07:36.150 --> 00:07:37.940 +which is pretty impressive for something like this. + +00:07:39.060 --> 00:07:40.679 +I actually have a board on my desk, I'm doing + +00:07:40.680 --> 00:07:41.999 +bring-up on this RISC-V board. + +00:07:42.000 --> 00:07:46.359 +So I'm going to CALL this runnable, and it will + +00:07:46.360 --> 00:07:48.720 +wait until it hits a U-Boot prompt... + +00:07:49.420 --> 00:07:50.800 +Let me turn on the board... + +00:07:52.500 --> 00:07:53.239 +and there we go, we + +00:07:53.240 --> 00:07:55.560 +have in our buffer "Pretty cool, huh?". + +00:07:56.260 --> 00:07:58.260 +I also added a feature + +00:07:58.860 --> 00:08:00.900 +to give me an interactive terminal— + +00:08:01.200 --> 00:08:03.800 +it will open the serial device in a term-mode buffer + +00:08:05.000 --> 00:08:08.160 +So, that's pretty neat. + +00:08:08.860 --> 00:08:12.360 +(I'll turn this off so the fan doesn't annoy me.) + +00:08:14.060 --> 00:08:16.799 +Again, I just want to emphasize, this doesn't + +00:08:16.800 --> 00:08:18.479 +have to be the most general runnable of all time, + +00:08:18.480 --> 00:08:19.759 +it really just has to fit my use case. + +00:08:19.760 --> 00:08:22.039 +I actually did use this to bring up this RISC-V + +00:08:22.040 --> 00:08:23.480 +board, it was really useful because + +00:08:23.780 --> 00:08:25.439 +when you're debugging a kernel panic, you don't have a + +00:08:25.440 --> 00:08:26.800 +userspace and therefore no TRAMP. + +00:08:27.800 --> 00:08:29.559 +Ok, the last example I want to talk about is + +00:08:29.560 --> 00:08:30.599 +about higher order functions. + +00:08:30.600 --> 00:08:32.479 +If we're passing around code blocks as values, + +00:08:32.480 --> 00:08:34.719 +naturally you might think of something like map + +00:08:34.720 --> 00:08:35.540 +as a higher order function. + +00:08:36.440 --> 00:08:40.279 +The details I'm going to gloss over because we're + +00:08:40.280 --> 00:08:43.679 +short on time, but down here I have this "map". + +00:08:43.680 --> 00:08:45.679 +I'm mapping the above code block over my 3 + +00:08:45.680 --> 00:08:48.780 +most recent TRAMP machines. + +00:08:49.280 --> 00:08:51.420 +So it's going to open this stuff... + +00:08:51.520 --> 00:08:53.320 +it's taking a little bit to think... + +00:08:53.920 --> 00:08:56.479 +I will emphasize, the things you pass in, it's + +00:08:56.480 --> 00:08:58.439 +just a list-of-lists where each child list is the + +00:08:58.440 --> 00:08:59.719 +list of header arguments you pass to the + +00:08:59.720 --> 00:09:00.340 +source block. + +00:09:00.740 --> 00:09:03.280 +So here I'm mapping over :dir, but you could just as + +00:09:03.380 --> 00:09:07.960 +easily map over :results, :var, whatever. + +00:09:08.060 --> 00:09:10.539 +And then I have some :post header argument to + +00:09:10.540 --> 00:09:11.480 +make the table look nice. + +NOTE Parallel map + +00:09:12.880 --> 00:09:14.519 +I want to briefly talk about "parallel map", + +00:09:14.520 --> 00:09:16.519 +a little experiment I did to try and use Emacs's + +00:09:16.520 --> 00:09:18.280 +multithreading feature to speed up map. + +00:09:18.980 --> 00:09:20.520 +Unfortunately, it only works with ELisp, + +00:09:20.620 --> 00:09:22.200 +it doesn't work with any external processes. + +00:09:22.400 --> 00:09:24.279 +Probably not too useful, but maybe someone out + +00:09:24.280 --> 00:09:25.700 +there will find it interesting. + +NOTE Tagle map + +00:09:26.700 --> 00:09:28.900 +The last thing I want to talk about is "tangle map". + +00:09:29.720 --> 00:09:33.239 +This is essentially mapping a cat runnable over a + +00:09:33.240 --> 00:09:35.739 +file, but you can use this in conjunction with + +00:09:35.740 --> 00:09:39.060 +"map" to get a kind of devops workflow. + +00:09:42.360 --> 00:09:45.760 +(I have to type in my auth-source password for my password-store password.) + +00:09:47.060 --> 00:09:49.260 +I'm tangling this to /var/run, so that requires root, + +00:09:49.360 --> 00:09:50.660 +which is why I need to add my password. + +00:09:51.060 --> 00:09:54.120 +Just to prove that's actually there... + +00:10:04.420 --> 00:10:06.079 +So yeah, that's another pretty interesting + +00:10:06.080 --> 00:10:06.640 +thing you can do. + +00:10:07.340 --> 00:10:09.920 +And then if we're talking about map, of course we have fold. + +00:10:13.140 --> 00:10:15.999 +I guess I did not load ob-python, but that's not + +00:10:16.000 --> 00:10:16.479 +too important. + +00:10:16.480 --> 00:10:20.400 +But fold works the same way as map, pretty much. + +00:10:20.500 --> 00:10:23.239 +Maybe you could fold a code block multiple times + +00:10:23.240 --> 00:10:24.740 +to take some benchmarking statistics. + +NOTE Conclusion + +00:10:26.040 --> 00:10:27.780 +So that brings me to the end of my talk. + +00:10:27.880 --> 00:10:29.440 +If you take away one thing, I just want to + +00:10:29.540 --> 00:10:31.240 +emphasize that you can use a tiny bit of ELisp + +00:10:31.540 --> 00:10:33.959 +to augment the already very powerful capabilities of + +00:10:33.960 --> 00:10:34.560 +Org babel. + +00:10:34.760 --> 00:10:36.940 +Whether your document is a research paper, blog post, + +00:10:37.040 --> 00:10:38.879 +homework assignment, whatever: I encourage + +00:10:38.880 --> 00:10:40.880 +you to get creative and bend Emacs to your will. + +00:10:41.880 --> 00:10:42.720 +Happy hacking! |
