-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtutorial_macros.html
991 lines (931 loc) · 78.2 KB
/
tutorial_macros.html
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
<!doctype html>
<html class="no-js" lang="en" data-content_root="./">
<head><meta charset="utf-8"/>
<meta name="viewport" content="width=device-width,initial-scale=1"/>
<meta name="color-scheme" content="light dark"><meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="index" title="Index" href="genindex.html" /><link rel="search" title="Search" href="search.html" /><link rel="next" title="Tutorial: Extensions (Xontribs)" href="tutorial_xontrib.html" /><link rel="prev" title="Tutorial: History" href="tutorial_hist.html" />
<link rel="shortcut icon" href="_static/magic_conch.ico"/><!-- Generated with Sphinx 8.1.3 and Furo 2024.08.06 -->
<title>Tutorial: Macros - xonsh 0.19.0 documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=fa44fd50" />
<link rel="stylesheet" type="text/css" href="_static/styles/furo.css?v=354aac6f" />
<link rel="stylesheet" type="text/css" href="_static/graphviz.css?v=4ae1632d" />
<link rel="stylesheet" type="text/css" href="_static/custom.css?v=c8b63bf6" />
<link rel="stylesheet" type="text/css" href="_static/styles/furo-extensions.css?v=302659d7" />
<style>
body {
--color-code-background: #eeffcc;
--color-code-foreground: black;
}
@media not print {
body[data-theme="dark"] {
--color-code-background: #202020;
--color-code-foreground: #d0d0d0;
}
@media (prefers-color-scheme: dark) {
body:not([data-theme="light"]) {
--color-code-background: #202020;
--color-code-foreground: #d0d0d0;
}
}
}
</style></head>
<body>
<script>
document.body.dataset.theme = localStorage.getItem("theme") || "auto";
</script>
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
<symbol id="svg-toc" viewBox="0 0 24 24">
<title>Contents</title>
<svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 1024 1024">
<path d="M408 442h480c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8H408c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8zm-8 204c0 4.4 3.6 8 8 8h480c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8H408c-4.4 0-8 3.6-8 8v56zm504-486H120c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0 632H120c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zM115.4 518.9L271.7 642c5.8 4.6 14.4.5 14.4-6.9V388.9c0-7.4-8.5-11.5-14.4-6.9L115.4 505.1a8.74 8.74 0 0 0 0 13.8z"/>
</svg>
</symbol>
<symbol id="svg-menu" viewBox="0 0 24 24">
<title>Menu</title>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather-menu">
<line x1="3" y1="12" x2="21" y2="12"></line>
<line x1="3" y1="6" x2="21" y2="6"></line>
<line x1="3" y1="18" x2="21" y2="18"></line>
</svg>
</symbol>
<symbol id="svg-arrow-right" viewBox="0 0 24 24">
<title>Expand</title>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather-chevron-right">
<polyline points="9 18 15 12 9 6"></polyline>
</svg>
</symbol>
<symbol id="svg-sun" viewBox="0 0 24 24">
<title>Light mode</title>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="1" stroke-linecap="round" stroke-linejoin="round" class="feather-sun">
<circle cx="12" cy="12" r="5"></circle>
<line x1="12" y1="1" x2="12" y2="3"></line>
<line x1="12" y1="21" x2="12" y2="23"></line>
<line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line>
<line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line>
<line x1="1" y1="12" x2="3" y2="12"></line>
<line x1="21" y1="12" x2="23" y2="12"></line>
<line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line>
<line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line>
</svg>
</symbol>
<symbol id="svg-moon" viewBox="0 0 24 24">
<title>Dark mode</title>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="1" stroke-linecap="round" stroke-linejoin="round" class="icon-tabler-moon">
<path stroke="none" d="M0 0h24v24H0z" fill="none" />
<path d="M12 3c.132 0 .263 0 .393 0a7.5 7.5 0 0 0 7.92 12.446a9 9 0 1 1 -8.313 -12.454z" />
</svg>
</symbol>
<symbol id="svg-sun-with-moon" viewBox="0 0 24 24">
<title>Auto light/dark, in light mode</title>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="1" stroke-linecap="round" stroke-linejoin="round"
class="icon-custom-derived-from-feather-sun-and-tabler-moon">
<path style="opacity: 50%" d="M 5.411 14.504 C 5.471 14.504 5.532 14.504 5.591 14.504 C 3.639 16.319 4.383 19.569 6.931 20.352 C 7.693 20.586 8.512 20.551 9.25 20.252 C 8.023 23.207 4.056 23.725 2.11 21.184 C 0.166 18.642 1.702 14.949 4.874 14.536 C 5.051 14.512 5.231 14.5 5.411 14.5 L 5.411 14.504 Z"/>
<line x1="14.5" y1="3.25" x2="14.5" y2="1.25"/>
<line x1="14.5" y1="15.85" x2="14.5" y2="17.85"/>
<line x1="10.044" y1="5.094" x2="8.63" y2="3.68"/>
<line x1="19" y1="14.05" x2="20.414" y2="15.464"/>
<line x1="8.2" y1="9.55" x2="6.2" y2="9.55"/>
<line x1="20.8" y1="9.55" x2="22.8" y2="9.55"/>
<line x1="10.044" y1="14.006" x2="8.63" y2="15.42"/>
<line x1="19" y1="5.05" x2="20.414" y2="3.636"/>
<circle cx="14.5" cy="9.55" r="3.6"/>
</svg>
</symbol>
<symbol id="svg-moon-with-sun" viewBox="0 0 24 24">
<title>Auto light/dark, in dark mode</title>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="1" stroke-linecap="round" stroke-linejoin="round"
class="icon-custom-derived-from-feather-sun-and-tabler-moon">
<path d="M 8.282 7.007 C 8.385 7.007 8.494 7.007 8.595 7.007 C 5.18 10.184 6.481 15.869 10.942 17.24 C 12.275 17.648 13.706 17.589 15 17.066 C 12.851 22.236 5.91 23.143 2.505 18.696 C -0.897 14.249 1.791 7.786 7.342 7.063 C 7.652 7.021 7.965 7 8.282 7 L 8.282 7.007 Z"/>
<line style="opacity: 50%" x1="18" y1="3.705" x2="18" y2="2.5"/>
<line style="opacity: 50%" x1="18" y1="11.295" x2="18" y2="12.5"/>
<line style="opacity: 50%" x1="15.316" y1="4.816" x2="14.464" y2="3.964"/>
<line style="opacity: 50%" x1="20.711" y1="10.212" x2="21.563" y2="11.063"/>
<line style="opacity: 50%" x1="14.205" y1="7.5" x2="13.001" y2="7.5"/>
<line style="opacity: 50%" x1="21.795" y1="7.5" x2="23" y2="7.5"/>
<line style="opacity: 50%" x1="15.316" y1="10.184" x2="14.464" y2="11.036"/>
<line style="opacity: 50%" x1="20.711" y1="4.789" x2="21.563" y2="3.937"/>
<circle style="opacity: 50%" cx="18" cy="7.5" r="2.169"/>
</svg>
</symbol>
<symbol id="svg-pencil" viewBox="0 0 24 24">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="1" stroke-linecap="round" stroke-linejoin="round" class="icon-tabler-pencil-code">
<path d="M4 20h4l10.5 -10.5a2.828 2.828 0 1 0 -4 -4l-10.5 10.5v4" />
<path d="M13.5 6.5l4 4" />
<path d="M20 21l2 -2l-2 -2" />
<path d="M17 17l-2 2l2 2" />
</svg>
</symbol>
<symbol id="svg-eye" viewBox="0 0 24 24">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="1" stroke-linecap="round" stroke-linejoin="round" class="icon-tabler-eye-code">
<path stroke="none" d="M0 0h24v24H0z" fill="none" />
<path d="M10 12a2 2 0 1 0 4 0a2 2 0 0 0 -4 0" />
<path
d="M11.11 17.958c-3.209 -.307 -5.91 -2.293 -8.11 -5.958c2.4 -4 5.4 -6 9 -6c3.6 0 6.6 2 9 6c-.21 .352 -.427 .688 -.647 1.008" />
<path d="M20 21l2 -2l-2 -2" />
<path d="M17 17l-2 2l2 2" />
</svg>
</symbol>
</svg>
<input type="checkbox" class="sidebar-toggle" name="__navigation" id="__navigation">
<input type="checkbox" class="sidebar-toggle" name="__toc" id="__toc">
<label class="overlay sidebar-overlay" for="__navigation">
<div class="visually-hidden">Hide navigation sidebar</div>
</label>
<label class="overlay toc-overlay" for="__toc">
<div class="visually-hidden">Hide table of contents sidebar</div>
</label>
<a class="skip-to-content muted-link" href="#furo-main-content">Skip to content</a>
<div class="page">
<header class="mobile-header">
<div class="header-left">
<label class="nav-overlay-icon" for="__navigation">
<div class="visually-hidden">Toggle site navigation sidebar</div>
<i class="icon"><svg><use href="#svg-menu"></use></svg></i>
</label>
</div>
<div class="header-center">
<a href="contents.html"><div class="brand">xonsh 0.19.0 documentation</div></a>
</div>
<div class="header-right">
<div class="theme-toggle-container theme-toggle-header">
<button class="theme-toggle">
<div class="visually-hidden">Toggle Light / Dark / Auto color theme</div>
<svg class="theme-icon-when-auto-light"><use href="#svg-sun-with-moon"></use></svg>
<svg class="theme-icon-when-auto-dark"><use href="#svg-moon-with-sun"></use></svg>
<svg class="theme-icon-when-dark"><use href="#svg-moon"></use></svg>
<svg class="theme-icon-when-light"><use href="#svg-sun"></use></svg>
</button>
</div>
<label class="toc-overlay-icon toc-header-icon" for="__toc">
<div class="visually-hidden">Toggle table of contents sidebar</div>
<i class="icon"><svg><use href="#svg-toc"></use></svg></i>
</label>
</div>
</header>
<aside class="sidebar-drawer">
<div class="sidebar-container">
<div class="sidebar-sticky"><a class="sidebar-brand" href="contents.html">
<div class="sidebar-logo-container">
<img class="sidebar-logo" src="_static/ascii_conch_part_transparent_tight.png" alt="Logo"/>
</div>
<span class="sidebar-brand-text">xonsh 0.19.0 documentation</span>
</a><form class="sidebar-search-container" method="get" action="search.html" role="search">
<input class="sidebar-search" placeholder="Search" name="q" aria-label="Search">
<input type="hidden" name="check_keywords" value="yes">
<input type="hidden" name="area" value="default">
</form>
<div id="searchbox"></div><div class="sidebar-scroll"><div class="sidebar-tree">
<ul>
<li class="toctree-l1"><a class="reference internal" href="packages.html">Package Manager</a></li>
<li class="toctree-l1"><a class="reference internal" href="appimage.html">AppImage</a></li>
<li class="toctree-l1"><a class="reference internal" href="containers.html">Docker container</a></li>
</ul>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="tutorial.html">Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial_hist.html">Tutorial: History</a></li>
<li class="toctree-l1 current current-page"><a class="current reference internal" href="#">Tutorial: Macros</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial_xontrib.html">Tutorial: Extensions (Xontribs)</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial_xonsh_projects.html">Tutorial: Xonsh Projects</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial_events.html">Tutorial: Events</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial_completers.html">Tutorial: Programmable Tab-Completion</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial_history_backend.html">Tutorial: Write Your Own History Backend</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial_subproc_strings.html">Tutorial: Subprocess Strings</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial_ptk.html">Tutorial: <code class="docutils literal notranslate"><span class="pre">prompt_toolkit</span></code> custom keybindings</a></li>
<li class="toctree-l1"><a class="reference internal" href="bash_to_xsh.html">Bash to Xonsh Translation Guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="editors.html">Editor and IDE Support</a></li>
<li class="toctree-l1"><a class="reference internal" href="subproc_types.html">Subprocess Types Table</a></li>
<li class="toctree-l1"><a class="reference internal" href="python_virtual_environments.html">Python Virtual Environments</a></li>
<li class="toctree-l1"><a class="reference internal" href="keyboard_shortcuts.html">Keyboard Shortcuts</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="xonshrc.html">Run Control File</a></li>
<li class="toctree-l1"><a class="reference internal" href="customization.html">Updating and customizing xonsh</a></li>
<li class="toctree-l1"><a class="reference internal" href="envvars.html">Environment Variables</a></li>
<li class="toctree-l1"><a class="reference internal" href="aliases.html">Built-in Aliases</a></li>
<li class="toctree-l1"><a class="reference internal" href="events.html">Core Events</a></li>
<li class="toctree-l1"><a class="reference internal" href="platform-issues.html">Platform-specific tips and tricks</a></li>
</ul>
<ul>
<li class="toctree-l1 has-children"><a class="reference internal" href="api/index.html">Xonsh API</a><input class="toctree-checkbox" id="toctree-checkbox-1" name="toctree-checkbox-1" role="switch" type="checkbox"/><label for="toctree-checkbox-1"><div class="visually-hidden">Toggle navigation of Xonsh API</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/lang/xonsh.parsers.lexer.html">xonsh.parsers.lexer</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/lang/xonsh.parser.html">xonsh.parser</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/lang/xonsh.parsers.ast.html">xonsh.parsers.ast</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/lang/xonsh.execer.html">xonsh.execer</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/lang/xonsh.imphooks.html">xonsh.imphooks</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/cmd/xonsh.built_ins.html">xonsh.built_ins</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/cmd/xonsh.environ.html">xonsh.environ</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/cmd/xonsh.aliases.html">xonsh.aliases</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/cmd/xonsh.dirstack.html">xonsh.dirstack</a></li>
<li class="toctree-l2 has-children"><a class="reference internal" href="api/_autosummary/cmd/xonsh.procs.html">xonsh.procs</a><input class="toctree-checkbox" id="toctree-checkbox-2" name="toctree-checkbox-2" role="switch" type="checkbox"/><label for="toctree-checkbox-2"><div class="visually-hidden">Toggle navigation of xonsh.procs</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.procs.executables.html">xonsh.procs.executables</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.procs.jobs.html">xonsh.procs.jobs</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.procs.pipelines.html">xonsh.procs.pipelines</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.procs.posix.html">xonsh.procs.posix</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.procs.proxies.html">xonsh.procs.proxies</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.procs.readers.html">xonsh.procs.readers</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.procs.specs.html">xonsh.procs.specs</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/cmd/xonsh.lib.inspectors.html">xonsh.lib.inspectors</a></li>
<li class="toctree-l2 has-children"><a class="reference internal" href="api/_autosummary/cmd/xonsh.history.html">xonsh.history</a><input class="toctree-checkbox" id="toctree-checkbox-3" name="toctree-checkbox-3" role="switch" type="checkbox"/><label for="toctree-checkbox-3"><div class="visually-hidden">Toggle navigation of xonsh.history</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.history.base.html">xonsh.history.base</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.history.diff_history.html">xonsh.history.diff_history</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.history.dummy.html">xonsh.history.dummy</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.history.json.html">xonsh.history.json</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.history.main.html">xonsh.history.main</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.history.sqlite.html">xonsh.history.sqlite</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/cmd/xonsh.completer.html">xonsh.completer</a></li>
<li class="toctree-l2 has-children"><a class="reference internal" href="api/_autosummary/cmd/xonsh.completers.html">xonsh.completers</a><input class="toctree-checkbox" id="toctree-checkbox-4" name="toctree-checkbox-4" role="switch" type="checkbox"/><label for="toctree-checkbox-4"><div class="visually-hidden">Toggle navigation of xonsh.completers</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.completers.base.html">xonsh.completers.base</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.completers.bash.html">xonsh.completers.bash</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.completers.bash_completion.html">xonsh.completers.bash_completion</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.completers.commands.html">xonsh.completers.commands</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.completers.completer.html">xonsh.completers.completer</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.completers.dirs.html">xonsh.completers.dirs</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.completers.environment.html">xonsh.completers.environment</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.completers.imports.html">xonsh.completers.imports</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.completers.init.html">xonsh.completers.init</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.completers.man.html">xonsh.completers.man</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.completers.path.html">xonsh.completers.path</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.completers.python.html">xonsh.completers.python</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.completers.tools.html">xonsh.completers.tools</a></li>
</ul>
</li>
<li class="toctree-l2 has-children"><a class="reference internal" href="api/_autosummary/cmd/xonsh.prompt.html">xonsh.prompt</a><input class="toctree-checkbox" id="toctree-checkbox-5" name="toctree-checkbox-5" role="switch" type="checkbox"/><label for="toctree-checkbox-5"><div class="visually-hidden">Toggle navigation of xonsh.prompt</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.prompt.base.html">xonsh.prompt.base</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.prompt.cwd.html">xonsh.prompt.cwd</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.prompt.env.html">xonsh.prompt.env</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.prompt.gitstatus.html">xonsh.prompt.gitstatus</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.prompt.job.html">xonsh.prompt.job</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.prompt.times.html">xonsh.prompt.times</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.prompt.vc.html">xonsh.prompt.vc</a></li>
</ul>
</li>
<li class="toctree-l2 has-children"><a class="reference internal" href="api/_autosummary/cmd/xonsh.shells.html">xonsh.shells</a><input class="toctree-checkbox" id="toctree-checkbox-6" name="toctree-checkbox-6" role="switch" type="checkbox"/><label for="toctree-checkbox-6"><div class="visually-hidden">Toggle navigation of xonsh.shells</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.shells.base_shell.html">xonsh.shells.base_shell</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.shells.dumb_shell.html">xonsh.shells.dumb_shell</a></li>
<li class="toctree-l3 has-children"><a class="reference internal" href="api/_autosummary/cmd/xonsh.shells.ptk_shell.html">xonsh.shells.ptk_shell</a><input class="toctree-checkbox" id="toctree-checkbox-7" name="toctree-checkbox-7" role="switch" type="checkbox"/><label for="toctree-checkbox-7"><div class="visually-hidden">Toggle navigation of xonsh.shells.ptk_shell</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l4"><a class="reference internal" href="api/_autosummary/cmd/xonsh.shells.ptk_shell.completer.html">xonsh.shells.ptk_shell.completer</a></li>
<li class="toctree-l4"><a class="reference internal" href="api/_autosummary/cmd/xonsh.shells.ptk_shell.formatter.html">xonsh.shells.ptk_shell.formatter</a></li>
<li class="toctree-l4"><a class="reference internal" href="api/_autosummary/cmd/xonsh.shells.ptk_shell.history.html">xonsh.shells.ptk_shell.history</a></li>
<li class="toctree-l4"><a class="reference internal" href="api/_autosummary/cmd/xonsh.shells.ptk_shell.key_bindings.html">xonsh.shells.ptk_shell.key_bindings</a></li>
<li class="toctree-l4"><a class="reference internal" href="api/_autosummary/cmd/xonsh.shells.ptk_shell.updator.html">xonsh.shells.ptk_shell.updator</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.shells.readline_shell.html">xonsh.shells.readline_shell</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/cmd/xonsh.shells.base_shell.html">xonsh.shells.base_shell</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/cmd/xonsh.shells.readline_shell.html">xonsh.shells.readline_shell</a></li>
<li class="toctree-l2 has-children"><a class="reference internal" href="api/_autosummary/cmd/xonsh.shells.ptk_shell.html">xonsh.shells.ptk_shell</a><input class="toctree-checkbox" id="toctree-checkbox-8" name="toctree-checkbox-8" role="switch" type="checkbox"/><label for="toctree-checkbox-8"><div class="visually-hidden">Toggle navigation of xonsh.shells.ptk_shell</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.shells.ptk_shell.completer.html">xonsh.shells.ptk_shell.completer</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.shells.ptk_shell.formatter.html">xonsh.shells.ptk_shell.formatter</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.shells.ptk_shell.history.html">xonsh.shells.ptk_shell.history</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.shells.ptk_shell.key_bindings.html">xonsh.shells.ptk_shell.key_bindings</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.shells.ptk_shell.updator.html">xonsh.shells.ptk_shell.updator</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/cmd/xonsh.lib.pretty.html">xonsh.lib.pretty</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/cmd/xonsh.history.diff_history.html">xonsh.history.diff_history</a></li>
<li class="toctree-l2 has-children"><a class="reference internal" href="api/_autosummary/cmd/xonsh.xoreutils.html">xonsh.xoreutils</a><input class="toctree-checkbox" id="toctree-checkbox-9" name="toctree-checkbox-9" role="switch" type="checkbox"/><label for="toctree-checkbox-9"><div class="visually-hidden">Toggle navigation of xonsh.xoreutils</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.xoreutils.cat.html">xonsh.xoreutils.cat</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.xoreutils.echo.html">xonsh.xoreutils.echo</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.xoreutils.pwd.html">xonsh.xoreutils.pwd</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.xoreutils.tee.html">xonsh.xoreutils.tee</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.xoreutils.tty.html">xonsh.xoreutils.tty</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.xoreutils.ulimit.html">xonsh.xoreutils.ulimit</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.xoreutils.umask.html">xonsh.xoreutils.umask</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.xoreutils.uname.html">xonsh.xoreutils.uname</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.xoreutils.uptime.html">xonsh.xoreutils.uptime</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.xoreutils.util.html">xonsh.xoreutils.util</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.xoreutils.which.html">xonsh.xoreutils.which</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/cmd/xonsh.xoreutils.yes.html">xonsh.xoreutils.yes</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/helpers/xonsh.events.html">xonsh.events</a></li>
<li class="toctree-l2 has-children"><a class="reference internal" href="api/_autosummary/helpers/xonsh.lib.html">xonsh.lib</a><input class="toctree-checkbox" id="toctree-checkbox-10" name="toctree-checkbox-10" role="switch" type="checkbox"/><label for="toctree-checkbox-10"><div class="visually-hidden">Toggle navigation of xonsh.lib</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/helpers/xonsh.lib.collections.html">xonsh.lib.collections</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/helpers/xonsh.lib.inspectors.html">xonsh.lib.inspectors</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/helpers/xonsh.lib.itertools.html">xonsh.lib.itertools</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/helpers/xonsh.lib.jsonutils.html">xonsh.lib.jsonutils</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/helpers/xonsh.lib.lazyasd.html">xonsh.lib.lazyasd</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/helpers/xonsh.lib.lazyimps.html">xonsh.lib.lazyimps</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/helpers/xonsh.lib.lazyjson.html">xonsh.lib.lazyjson</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/helpers/xonsh.lib.modules.html">xonsh.lib.modules</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/helpers/xonsh.lib.openpy.html">xonsh.lib.openpy</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/helpers/xonsh.lib.os.html">xonsh.lib.os</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/helpers/xonsh.lib.pretty.html">xonsh.lib.pretty</a></li>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/helpers/xonsh.lib.subprocess.html">xonsh.lib.subprocess</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/helpers/xonsh.tools.html">xonsh.tools</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/helpers/xonsh.platform.html">xonsh.platform</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/helpers/xonsh.lazyjson.html">xonsh.lazyjson</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/helpers/xonsh.lazyasd.html">xonsh.lazyasd</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/helpers/xonsh.lib.openpy.html">xonsh.lib.openpy</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/helpers/xonsh.foreign_shells.html">xonsh.foreign_shells</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/helpers/xonsh.commands_cache.html">xonsh.commands_cache</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/helpers/xonsh.tracer.html">xonsh.tracer</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/helpers/xonsh.main.html">xonsh.main</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/helpers/xonsh.color_tools.html">xonsh.color_tools</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/helpers/xonsh.pyghooks.html">xonsh.pyghooks</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/helpers/xonsh.shells.dumb_shell.html">xonsh.shells.dumb_shell</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/helpers/xonsh.wizard.html">xonsh.wizard</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/helpers/xonsh.xonfig.html">xonsh.xonfig</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/helpers/xonsh.xontribs.html">xonsh.xontribs</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/helpers/xonsh.codecache.html">xonsh.codecache</a></li>
<li class="toctree-l2"><a class="reference internal" href="api/_autosummary/helpers/xonsh.contexts.html">xonsh.contexts</a></li>
<li class="toctree-l2 has-children"><a class="reference internal" href="api/_autosummary/xontribs/xontrib.html">xontrib</a><input class="toctree-checkbox" id="toctree-checkbox-11" name="toctree-checkbox-11" role="switch" type="checkbox"/><label for="toctree-checkbox-11"><div class="visually-hidden">Toggle navigation of xontrib</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="api/_autosummary/xontribs/xontrib.coreutils.html">xontrib.coreutils</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="advanced_events.html">Advanced Events</a></li>
<li class="toctree-l1"><a class="reference internal" href="devguide.html">Developer’s Guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="changelog.html">Xonsh Change Log</a></li>
<li class="toctree-l1"><a class="reference internal" href="faq.html">Frequently Asked Questions</a></li>
<li class="toctree-l1"><a class="reference internal" href="todo.html">Wishlist & To-Dos</a></li>
</ul>
</div>
</div>
</div>
</div>
</aside>
<div class="main">
<div class="content">
<div class="article-container">
<a href="#" class="back-to-top muted-link">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8v12z"></path>
</svg>
<span>Back to top</span>
</a>
<div class="content-icon-container">
<div class="view-this-page">
<a class="muted-link" href="https://github.com/xonsh/xonsh/blob/main/docs/tutorial_macros.rst?plain=true" title="View this page">
<svg><use href="#svg-eye"></use></svg>
<span class="visually-hidden">View this page</span>
</a>
</div><div class="edit-this-page">
<a class="muted-link" href="https://github.com/xonsh/xonsh/edit/main/docs/tutorial_macros.rst" title="Edit this page">
<svg><use href="#svg-pencil"></use></svg>
<span class="visually-hidden">Edit this page</span>
</a>
</div><div class="theme-toggle-container theme-toggle-content">
<button class="theme-toggle">
<div class="visually-hidden">Toggle Light / Dark / Auto color theme</div>
<svg class="theme-icon-when-auto-light"><use href="#svg-sun-with-moon"></use></svg>
<svg class="theme-icon-when-auto-dark"><use href="#svg-moon-with-sun"></use></svg>
<svg class="theme-icon-when-dark"><use href="#svg-moon"></use></svg>
<svg class="theme-icon-when-light"><use href="#svg-sun"></use></svg>
</button>
</div>
<label class="toc-overlay-icon toc-content-icon" for="__toc">
<div class="visually-hidden">Toggle table of contents sidebar</div>
<i class="icon"><svg><use href="#svg-toc"></use></svg></i>
</label>
</div>
<article role="main" id="furo-main-content">
<section id="tutorial-macros">
<span id="id1"></span><h1>Tutorial: Macros<a class="headerlink" href="#tutorial-macros" title="Link to this heading">¶</a></h1>
<p>Bust out your DSLRs, people. It is time to closely examine macros!</p>
<section id="what-are-macro-instructions">
<h2>What are macro instructions?<a class="headerlink" href="#what-are-macro-instructions" title="Link to this heading">¶</a></h2>
<p>In generic terms, a programming macro is a special kind of syntax that
replaces a smaller amount of code with a larger expression, syntax tree,
code object, etc after the macro has been evaluated.
In practice, macros pause the normal parsing and evaluation of the code
that they contain. This is so that they can perform their expansion with
a complete inputs. Roughly, the algorithm executing a macro follows is:</p>
<ol class="arabic simple">
<li><p>Macro start, pause or skip normal parsing</p></li>
<li><p>Gather macro inputs as strings</p></li>
<li><p>Evaluate macro with inputs</p></li>
<li><p>Resume normal parsing and execution.</p></li>
</ol>
<p>Is this meta-programming? You betcha!</p>
</section>
<section id="when-and-where-are-macros-used">
<h2>When and where are macros used?<a class="headerlink" href="#when-and-where-are-macros-used" title="Link to this heading">¶</a></h2>
<p>Macros are a practicality-beats-purity feature of many programming
languages. Because they allow you break out of the normal parsing
cycle, depending on the language, you can do some truly wild things with
them. However, macros are really there to reduce the amount of boiler plate
code that users and developers have to write.</p>
<p>In C and C++ (and Fortran), the C Preprocessor <code class="docutils literal notranslate"><span class="pre">cpp</span></code> is a macro evaluation
engine. For example, every time you see an <code class="docutils literal notranslate"><span class="pre">#include</span></code> or <code class="docutils literal notranslate"><span class="pre">#ifdef</span></code>, this is
the <code class="docutils literal notranslate"><span class="pre">cpp</span></code> macro system in action.
In these languages, the macros are technically outside of the definition
of the language at hand. Furthermore, because <code class="docutils literal notranslate"><span class="pre">cpp</span></code> must function with only
a single pass through the code, the sorts of macros that can be written with
<code class="docutils literal notranslate"><span class="pre">cpp</span></code> are relatively simple.</p>
<p>Rust, on the other hand, has a first-class notion of macros that look and
feel a lot like normal functions. Macros in Rust are capable of pulling off
type information from their arguments and preventing their return values
from being consumed.</p>
<p>Other languages like Lisp, Forth, and Julia also provide their macro systems.
Even restructured text (rST) directives could be considered macros.</p>
<p>If these seem unfamiliar to the Python world, note that Jupyter and IPython
magics <code class="docutils literal notranslate"><span class="pre">%</span></code> and <code class="docutils literal notranslate"><span class="pre">%%</span></code> are macros!</p>
</section>
<section id="function-macros">
<h2>Function Macros<a class="headerlink" href="#function-macros" title="Link to this heading">¶</a></h2>
<p>Xonsh supports Rust-like macros that are based on normal Python callables.
Macros do not require a special definition in xonsh. However, like in Rust,
they must be called with an exclamation point <code class="docutils literal notranslate"><span class="pre">!</span></code> between the callable
and the opening parentheses <code class="docutils literal notranslate"><span class="pre">(</span></code>. Macro arguments are split on the top-level
commas <code class="docutils literal notranslate"><span class="pre">,</span></code>, like normal Python functions. For example, say we have the
functions <code class="docutils literal notranslate"><span class="pre">f</span></code> and <code class="docutils literal notranslate"><span class="pre">g</span></code>. We could perform a macro call on these functions
with the following:</p>
<div class="highlight-xonsh notranslate"><div class="highlight"><pre><span></span><span class="c1"># No macro args</span>
<span class="n">f</span><span class="k">!</span><span class="p">()</span>
<span class="c1"># Single arg</span>
<span class="n">f</span><span class="k">!</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">g</span><span class="k">!</span><span class="p">([</span><span class="n">y</span><span class="p">,</span> <span class="mi">43</span><span class="p">,</span> <span class="mi">44</span><span class="p">])</span>
<span class="c1"># Two args</span>
<span class="n">f</span><span class="k">!</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">42</span><span class="p">)</span>
<span class="n">g</span><span class="k">!</span><span class="p">([</span><span class="n">y</span><span class="p">,</span> <span class="mi">43</span><span class="p">,</span> <span class="mi">44</span><span class="p">],</span> <span class="n">f</span><span class="k">!</span><span class="p">(</span><span class="n">z</span><span class="p">))</span>
</pre></div>
</div>
<p>Not so bad, right? So what actually happens to the arguments when used
in a macro call? Well, that depends on the definition of the function. In
particular, each argument in the macro call is matched up with the corresponding
parameter annotation in the callable’s signature. For example, say we have
an <code class="docutils literal notranslate"><span class="pre">identity()</span></code> function that annotates its sole argument as a string:</p>
<div class="highlight-xonsh notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">identity</span><span class="p">(</span><span class="n">x</span> <span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
<span class="k">return</span> <span class="n">x</span>
</pre></div>
</div>
<p>If we call this normally, we’ll just get whatever object we put in back out,
even if that object is not a string:</p>
<div class="highlight-xonshcon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">identity</span><span class="p">(</span><span class="s1">'me'</span><span class="p">)</span>
<span class="go">'me'</span>
<span class="gp">>>></span> <span class="n">identity</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="go">42</span>
<span class="gp">>>></span> <span class="n">identity</span><span class="p">(</span><span class="n">identity</span><span class="p">)</span>
<span class="go"><function __main__.identity></span>
</pre></div>
</div>
<p>However, if we perform macro calls instead we are now guaranteed to get
the string of the source code that is in the macro call:</p>
<div class="highlight-xonshcon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">identity</span><span class="k">!</span><span class="p">(</span><span class="s1">'me'</span><span class="p">)</span>
<span class="go">"'me'"</span>
<span class="gp">>>></span> <span class="n">identity</span><span class="k">!</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="go">'42'</span>
<span class="gp">>>></span> <span class="n">identity</span><span class="k">!</span><span class="p">(</span><span class="n">identity</span><span class="p">)</span>
<span class="go">'identity'</span>
</pre></div>
</div>
<p>Also note that each macro argument is stripped prior to passing it to the
macro itself. This is done for consistency.</p>
<div class="highlight-xonshcon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">identity</span><span class="k">!</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="go">'42'</span>
<span class="gp">>>></span> <span class="n">identity</span><span class="k">!</span><span class="p">(</span> <span class="mi">42</span> <span class="p">)</span>
<span class="go">'42'</span>
</pre></div>
</div>
<p>Importantly, because we are capturing and not evaluating the source code,
a macro call can contain input that is beyond the usual syntax. In fact, that
is sort of the whole point. Here are some cases to start your gears turning:</p>
<div class="highlight-xonshcon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">identity</span><span class="k">!</span><span class="p">(</span><span class="kn">import</span> <span class="nn">os</span><span class="p">)</span>
<span class="go">'import os'</span>
<span class="gp">>>></span> <span class="n">identity</span><span class="k">!</span><span class="p">(</span><span class="k">if</span> <span class="kc">True</span><span class="p">:</span>
<span class="gp">>>></span> <span class="k">pass</span><span class="p">)</span>
<span class="go">'if True:\n pass'</span>
<span class="gp">>>></span> <span class="n">identity</span><span class="k">!</span><span class="p">(</span><span class="n">std</span><span class="p">::</span><span class="n">vector</span><span class="o"><</span><span class="n">std</span><span class="p">::</span><span class="n">string</span><span class="o">></span> <span class="n">x</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"yoo"</span><span class="p">,</span> <span class="s2">"hoo"</span><span class="p">})</span>
<span class="go">'std::vector<std::string> x = {"yoo", "hoo"}'</span>
</pre></div>
</div>
<p>You do you, <code class="docutils literal notranslate"><span class="pre">identity()</span></code>.</p>
</section>
<section id="calling-function-macros">
<h2>Calling Function Macros<a class="headerlink" href="#calling-function-macros" title="Link to this heading">¶</a></h2>
<p>There are a couple of points to consider when calling macros. The first is
that passing in arguments by name will not behave as expected. This is because
the <code class="docutils literal notranslate"><span class="pre"><name>=</span></code> is captured by the macro itself. Using the <code class="docutils literal notranslate"><span class="pre">identity()</span></code>
function from above:</p>
<div class="highlight-xonshcon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">identity</span><span class="k">!</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">42</span><span class="p">)</span>
<span class="go">'x=42'</span>
</pre></div>
</div>
<p>Performing a macro call uses only argument order to pass in values.</p>
<p>Additionally, macro calls split arguments only on the top-level commas.
The top-level commas are not included in any argument.
This behaves analogously to normal Python function calls. For instance,
say we have the following <code class="docutils literal notranslate"><span class="pre">g()</span></code> function that accepts two arguments:</p>
<div class="highlight-xonsh notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">g</span><span class="p">(</span><span class="n">x</span> <span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">y</span> <span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'x = '</span> <span class="o">+</span> <span class="nb">repr</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'y = '</span> <span class="o">+</span> <span class="nb">repr</span><span class="p">(</span><span class="n">y</span><span class="p">))</span>
</pre></div>
</div>
<p>Then you can see the splitting and stripping behavior on each macro
argument:</p>
<div class="highlight-xonshcon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">g</span><span class="k">!</span><span class="p">(</span><span class="mi">42</span><span class="p">,</span> <span class="mi">65</span><span class="p">)</span>
<span class="go">x = '42'</span>
<span class="go">y = '65'</span>
<span class="gp">>>></span> <span class="n">g</span><span class="k">!</span><span class="p">(</span><span class="mi">42</span><span class="p">,</span> <span class="mi">65</span><span class="p">,)</span>
<span class="go">x = '42'</span>
<span class="go">y = '65'</span>
<span class="gp">>>></span> <span class="n">g</span><span class="k">!</span><span class="p">(</span> <span class="mi">42</span><span class="p">,</span> <span class="mi">65</span><span class="p">,</span> <span class="p">)</span>
<span class="go">x = '42'</span>
<span class="go">y = '65'</span>
<span class="gp">>>></span> <span class="n">g</span><span class="k">!</span><span class="p">([</span><span class="s1">'x'</span><span class="p">,</span> <span class="s1">'y'</span><span class="p">],</span> <span class="p">{</span><span class="mi">1</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">:</span> <span class="mi">3</span><span class="p">})</span>
<span class="go">x = "['x', 'y']"</span>
<span class="go">y = '{1: 1, 2: 3}'</span>
</pre></div>
</div>
<p>Sometimes you may only want to pass in the first few arguments as macro
arguments and you want the rest to be treated as normal Python arguments.
By convention, xonsh’s macro caller will look for a lone <code class="docutils literal notranslate"><span class="pre">*</span></code> argument
in order to split the macro arguments and the regular arguments. So for
example:</p>
<div class="highlight-xonshcon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">g</span><span class="k">!</span><span class="p">(</span><span class="mi">42</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="mi">65</span><span class="p">)</span>
<span class="go">x = '42'</span>
<span class="go">y = 65</span>
<span class="gp">>>></span> <span class="n">g</span><span class="k">!</span><span class="p">(</span><span class="mi">42</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">65</span><span class="p">)</span>
<span class="go">x = '42'</span>
<span class="go">y = 65</span>
</pre></div>
</div>
<p>In the above, note that <code class="docutils literal notranslate"><span class="pre">x</span></code> is still captured as a macro argument. However,
everything after the <code class="docutils literal notranslate"><span class="pre">*</span></code>, namely <code class="docutils literal notranslate"><span class="pre">y</span></code>, is evaluated is if it were passed
in to a normal function call. This can be useful for large interfaces where
only a handful of args are expected as macro arguments.</p>
<p>Hopefully, now you see the big picture.</p>
</section>
<section id="writing-function-macros">
<h2>Writing Function Macros<a class="headerlink" href="#writing-function-macros" title="Link to this heading">¶</a></h2>
<p>Though any function (or callable) can be used as a macro, this functionality
is probably most useful if the function was <em>designed</em> as a macro. There
are two main aspects of macro design to consider: argument annotations and
call site execution context.</p>
<section id="macro-annotations">
<h3>Macro Annotations<a class="headerlink" href="#macro-annotations" title="Link to this heading">¶</a></h3>
<p>There are six kinds of annotations that macros are able to interpret:</p>
<div class="table-wrapper docutils container" id="id2">
<table class="docutils align-default" id="id2">
<caption><span class="caption-text">Kinds of Annotation</span><a class="headerlink" href="#id2" title="Link to this table">¶</a></caption>
<thead>
<tr class="row-odd"><th class="head"><p>Category</p></th>
<th class="head"><p>Object</p></th>
<th class="head"><p>Flags</p></th>
<th class="head"><p>Modes</p></th>
<th class="head"><p>Returns</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>String</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">str</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'s'</span></code>, <code class="docutils literal notranslate"><span class="pre">'str'</span></code>, or <code class="docutils literal notranslate"><span class="pre">'string'</span></code></p></td>
<td></td>
<td><p>Source code of argument as string, <em>default</em>.</p></td>
</tr>
<tr class="row-odd"><td><p>AST</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">ast.AST</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'a'</span></code> or <code class="docutils literal notranslate"><span class="pre">'ast'</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'eval'</span></code> (default), <code class="docutils literal notranslate"><span class="pre">'exec'</span></code>, or <code class="docutils literal notranslate"><span class="pre">'single'</span></code></p></td>
<td><p>Abstract syntax tree of argument.</p></td>
</tr>
<tr class="row-even"><td><p>Code</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">types.CodeType</span></code> or <code class="docutils literal notranslate"><span class="pre">compile</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'c'</span></code>, <code class="docutils literal notranslate"><span class="pre">'code'</span></code>, or <code class="docutils literal notranslate"><span class="pre">'compile'</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'eval'</span></code> (default), <code class="docutils literal notranslate"><span class="pre">'exec'</span></code>, or <code class="docutils literal notranslate"><span class="pre">'single'</span></code></p></td>
<td><p>Compiled code object of argument.</p></td>
</tr>
<tr class="row-odd"><td><p>Eval</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">eval</span></code> or <code class="docutils literal notranslate"><span class="pre">None</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'v'</span></code> or <code class="docutils literal notranslate"><span class="pre">'eval'</span></code></p></td>
<td></td>
<td><p>Evaluation of the argument.</p></td>
</tr>
<tr class="row-even"><td><p>Exec</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">exec</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'x'</span></code> or <code class="docutils literal notranslate"><span class="pre">'exec'</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'exec'</span></code> (default) or <code class="docutils literal notranslate"><span class="pre">'single'</span></code></p></td>
<td><p>Execs the argument and returns None.</p></td>
</tr>
<tr class="row-odd"><td><p>Type</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">type</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'t'</span></code> or <code class="docutils literal notranslate"><span class="pre">'type'</span></code></p></td>
<td></td>
<td><p>The type of the argument after it has been evaluated.</p></td>
</tr>
</tbody>
</table>
</div>
<p>These annotations allow you to hook into whichever stage of the compilation
that you desire. It is important to note that the string form of the arguments
is split and stripped (as described above) prior to conversion to the
annotation type.</p>
<p>Each argument may be annotated with its own individual type. Annotations
may be provided as either objects or as the string flags seen in the above
table. String flags are case-insensitive.
If an argument does not have an annotation, <code class="docutils literal notranslate"><span class="pre">str</span></code> is selected.
This makes the macro function call behave like the subprocess macros and
context manager macros below. For example,</p>
<div class="highlight-xonsh notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="p">:</span> <span class="s1">'AST'</span><span class="p">,</span> <span class="n">c</span> <span class="p">:</span> <span class="nb">compile</span><span class="p">):</span>
<span class="k">pass</span>
</pre></div>
</div>
<p>In a macro call of <code class="docutils literal notranslate"><span class="pre">func!()</span></code>,</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">a</span></code> will be evaluated with <code class="docutils literal notranslate"><span class="pre">str</span></code> since no annotation was provided,</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">b</span></code> will be parsed into a syntax tree node, and</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">c</span></code> will be compiled into code object since the builtin <code class="docutils literal notranslate"><span class="pre">compile()</span></code>
function was used as the annotation.</p></li>
</ul>
<p>Additionally, certain kinds of annotations have different modes that
affect the parsing, compilation, and execution of its argument. While a
sensible default is provided, you may also supply your own. This is
done by annotating with a (kind, mode) tuple. The first element can
be any valid object or flag. The second element must be a corresponding
mode as a string. For instance,</p>
<div class="highlight-xonsh notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">gunc</span><span class="p">(</span><span class="n">d</span> <span class="p">:</span> <span class="p">(</span><span class="n">exec</span><span class="p">,</span> <span class="s1">'single'</span><span class="p">),</span> <span class="n">e</span> <span class="p">:</span> <span class="p">(</span><span class="s1">'c'</span><span class="p">,</span> <span class="s1">'exec'</span><span class="p">)):</span>
<span class="k">pass</span>
</pre></div>
</div>
<p>Thus in a macro call of <code class="docutils literal notranslate"><span class="pre">gunc!()</span></code>,</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">d</span></code> will be exec’d in single-mode (rather than exec-mode), and</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">e</span></code> will be compiled in exec-mode (rather than eval-mode).</p></li>
</ul>
<p>For more information on the differences between the exec, eval, and single
modes please see the Python documentation.</p>
</section>
<section id="macro-function-execution-context">
<h3>Macro Function Execution Context<a class="headerlink" href="#macro-function-execution-context" title="Link to this heading">¶</a></h3>
<p>Equally important as having the macro arguments is knowing the execution
context of the macro call itself. Rather than mucking around with frames,
macros provide both the globals and locals of the call site. These are
accessible as the <code class="docutils literal notranslate"><span class="pre">macro_globals</span></code> and <code class="docutils literal notranslate"><span class="pre">macro_locals</span></code> attributes of
the macro function itself while the macro is being executed.</p>
<p>For example, consider a macro which replaces all literal <code class="docutils literal notranslate"><span class="pre">1</span></code> digits
with the literal <code class="docutils literal notranslate"><span class="pre">2</span></code>, evaluates the modification, and returns the results.
To eval, the macro will need to pull off its globals and locals:</p>
<div class="highlight-xonsh notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">one_to_two</span><span class="p">(</span><span class="n">x</span> <span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
<span class="n">s</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">'1'</span><span class="p">,</span> <span class="s1">'2'</span><span class="p">)</span>
<span class="n">glbs</span> <span class="o">=</span> <span class="n">one_to_two</span><span class="o">.</span><span class="n">macro_globals</span>
<span class="n">locs</span> <span class="o">=</span> <span class="n">one_to_two</span><span class="o">.</span><span class="n">macro_locals</span>
<span class="k">return</span> <span class="nb">eval</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">glbs</span><span class="p">,</span> <span class="n">locs</span><span class="p">)</span>
</pre></div>
</div>
<p>Running this with a few of different inputs, we see:</p>
<div class="highlight-xonshcon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">one_to_two</span><span class="k">!</span><span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">4</span>
<span class="gp">>>></span> <span class="n">one_to_two</span><span class="k">!</span><span class="p">(</span><span class="mi">11</span><span class="p">)</span>
<span class="go">22</span>
<span class="gp">>>></span> <span class="n">x</span> <span class="o">=</span> <span class="mi">1</span>
<span class="gp">>>></span> <span class="n">one_to_two</span><span class="k">!</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">3</span>
</pre></div>
</div>
<p>Of course, many other more sophisticated options are available depending on the
use case.</p>
</section>
</section>
<section id="subprocess-macros">
<h2>Subprocess Macros<a class="headerlink" href="#subprocess-macros" title="Link to this heading">¶</a></h2>
<p>Like with function macros above, subprocess macros allow you to pause the parser
for until you are ready to exit subprocess mode. Unlike function macros, there
is only a single macro argument and its macro type is always a string. This
is because it (usually) doesn’t make sense to pass non-string arguments to a
command. And when it does, there is the <code class="docutils literal notranslate"><span class="pre">@()</span></code> syntax!</p>
<p>In the simplest case, subprocess macros look like the equivalent of their
function macro counterparts:</p>
<div class="highlight-xonshcon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">echo</span><span class="k">!</span> <span class="n">I</span><span class="s1">'m Mr. Meeseeks.</span>
<span class="n">I</span><span class="s1">'m Mr. Meeseeks.</span>
</pre></div>
</div>
<p>Again, note that everything to the right of the <code class="docutils literal notranslate"><span class="pre">!</span></code> is passed down to the
<code class="docutils literal notranslate"><span class="pre">echo</span></code> command as the final, single argument. This is space preserving,
like wrapping with quotes:</p>
<div class="highlight-xonshcon notranslate"><div class="highlight"><pre><span></span><span class="c1"># normally, xonsh will split on whitespace,</span>
<span class="go"># so each argument is passed in separately</span>
<span class="gp">>>></span> <span class="n">echo</span> <span class="n">x</span> <span class="n">y</span> <span class="n">z</span>
<span class="go">x y z</span>
<span class="go"># usually space can be preserved with quotes</span>
<span class="gp">>>></span> <span class="n">echo</span> <span class="s2">"x y z"</span>
<span class="go">x y z</span>
<span class="go"># however, subprocess macros will pause and then strip</span>
<span class="go"># all input after the exclamation point</span>
<span class="gp">>>></span> <span class="n">echo</span><span class="k">!</span> <span class="n">x</span> <span class="n">y</span> <span class="n">z</span>
<span class="go">x y z</span>
</pre></div>
</div>
<p>However, the macro will pause everything, including path and environment variable
expansion, that might be present even with quotes. For example:</p>
<div class="highlight-xonshcon notranslate"><div class="highlight"><pre><span></span><span class="c1"># without macros, environment variable are expanded</span>
<span class="gp">>>></span> <span class="n">echo</span> <span class="nv">$USER</span>
<span class="go">lou</span>
<span class="go"># inside of a macro, all additional munging is turned off.</span>
<span class="gp">>>></span> <span class="n">echo</span><span class="k">!</span> <span class="nv">$USER</span>
<span class="go">$USER</span>
</pre></div>
</div>
<p>Everything to the right of the exclamation point, except the leading and trailing
whitespace, is passed into the command directly as written. This allows certain
commands to function in cases where quoting or piping might be more burdensome.
The <code class="docutils literal notranslate"><span class="pre">timeit</span></code> command is a great example where simple syntax will often fail,
but will be easily executable as a macro:</p>
<div class="highlight-xonshcon notranslate"><div class="highlight"><pre><span></span><span class="c1"># fails normally</span>
<span class="gp">>>></span> <span class="n">timeit</span> <span class="s2">"hello mom "</span> <span class="o">+</span> <span class="s2">"and dad"</span>
<span class="go">xonsh: subprocess mode: command not found: hello</span>
<span class="go"># macro success!</span>
<span class="gp">>>></span> <span class="n">timeit</span><span class="k">!</span> <span class="s2">"hello mom "</span> <span class="o">+</span> <span class="s2">"and dad"</span>
<span class="go">100000000 loops, best of 3: 8.24 ns per loop</span>
</pre></div>
</div>
<p>All expressions to the left of the exclamation point are passed in normally and
are not treated as the special macro argument. This allows the mixing of
simple and complex command line arguments. For example, sometimes you might
really want to write some code in another language:</p>
<div class="highlight-xonshcon notranslate"><div class="highlight"><pre><span></span><span class="c1"># don't worry, it is temporary!</span>
<span class="gp">>>></span> <span class="n">bash</span> <span class="o">-</span><span class="n">c</span> <span class="err">!</span> <span class="n">export</span> <span class="n">var</span><span class="o">=</span><span class="mi">42</span><span class="p">;</span> <span class="n">echo</span> <span class="nv">$var</span>
<span class="go">42</span>
<span class="go"># that's better!</span>
<span class="gp">>>></span> <span class="n">python</span> <span class="o">-</span><span class="n">c</span> <span class="err">!</span> <span class="kn">import</span> <span class="nn">os</span><span class="p">;</span> <span class="nb">print</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="s2">"/"</span><span class="p">))</span>
<span class="go">/</span>
</pre></div>
</div>
<p>Compared to function macros, subprocess macros are relatively simple.
However, they can still be very expressive!</p>
</section>
<section id="context-manager-macros">
<h2>Context Manager Macros<a class="headerlink" href="#context-manager-macros" title="Link to this heading">¶</a></h2>
<p>Now that we have seen what life can be like with macro expressions, it is time
to introduce the macro statement: <code class="docutils literal notranslate"><span class="pre">with!</span></code>. With-bang provides macros
on top of existing Python context managers. This provides both anonymous
and onymous blocks in xonsh.</p>
<p>The syntax for context manager macros is the same as the usual with-statement
in Python, but with an additional exclamation point between the <code class="docutils literal notranslate"><span class="pre">with</span></code> word
and the first context manager expression. As a simple example,</p>
<div class="highlight-xonsh notranslate"><div class="highlight"><pre><span></span><span class="k">with!</span> <span class="n">x</span><span class="p">:</span>
<span class="n">y</span> <span class="o">=</span> <span class="mi">10</span>
<span class="nb">print</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
</pre></div>
</div>
<p>In the above, everything to the left of the colon (<code class="docutils literal notranslate"><span class="pre">x</span></code>) will be evaluated
normally. However, the body will not be executed and <code class="docutils literal notranslate"><span class="pre">y</span></code> will not be defined
or printed. In this case, the body will be attached to x as a string, along with
globals and locals, prior to the body even being entered. The body is then
replaced with a <code class="docutils literal notranslate"><span class="pre">pass</span></code> statement. You can think of the above as being
transformed into the following:</p>
<div class="highlight-xonsh notranslate"><div class="highlight"><pre><span></span><span class="n">x</span><span class="o">.</span><span class="n">macro_block</span> <span class="o">=</span> <span class="s1">'y = 10</span><span class="se">\n</span><span class="s1">print(y)</span><span class="se">\n</span><span class="s1">'</span>
<span class="n">x</span><span class="o">.</span><span class="n">macro_globals</span> <span class="o">=</span> <span class="nb">globals</span><span class="p">()</span>
<span class="n">x</span><span class="o">.</span><span class="n">macro_locals</span> <span class="o">=</span> <span class="nb">locals</span><span class="p">()</span>
<span class="k">with!</span> <span class="n">x</span><span class="p">:</span>
<span class="k">pass</span>
</pre></div>
</div>
<p>There are a few important things about this to notice:</p>
<ol class="arabic simple">
<li><p>The <code class="docutils literal notranslate"><span class="pre">macro_block</span></code> string is dedented,</p></li>
<li><p>The <code class="docutils literal notranslate"><span class="pre">macro_*</span></code> attributes are set <em>before</em> the context manager is entered so
the <code class="docutils literal notranslate"><span class="pre">__enter__()</span></code> method may use them, and</p></li>
<li><p>The <code class="docutils literal notranslate"><span class="pre">macro_*</span></code> attributes are not cleaned up automatically so that the
context manager may use them even after the object is exited. The
<code class="docutils literal notranslate"><span class="pre">__exit__()</span></code> method may clean up these attributes, if desired.</p></li>
</ol>
<p>By default, macro blocks are returned as a string. However, like with function
macro arguments, the kind of <code class="docutils literal notranslate"><span class="pre">macro_block</span></code> is determined by a special
annotation. This annotation is given via the <code class="docutils literal notranslate"><span class="pre">__xonsh_block__</span></code> attribute
on the context manager itself. This allows the block to be interpreted as
an AST, byte compiled, etc.</p>
<p>The convenient part about this syntax is that the macro block is only
exited once it sees a dedent back to the level of the <code class="docutils literal notranslate"><span class="pre">with!</span></code>. All other
code is indiscriminately skipped! This allows you to write blocks of code in
languages other than xonsh without pause.</p>
<p>For example, consider a simple
XML macro context manager. This will return the parsed XML tree from a
macro block. The context manager itself can be written as:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">xml.etree.ElementTree</span> <span class="k">as</span> <span class="nn">ET</span>
<span class="k">class</span> <span class="nc">XmlBlock</span><span class="p">:</span>
<span class="c1"># make sure the macro_block comes back as a string</span>
<span class="n">__xonsh_block__</span> <span class="o">=</span> <span class="nb">str</span>
<span class="k">def</span> <span class="fm">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="c1"># parse and return the block on entry</span>
<span class="n">root</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">macro_block</span><span class="p">)</span>
<span class="k">return</span> <span class="n">root</span>
<span class="k">def</span> <span class="fm">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">exc</span><span class="p">):</span>
<span class="c1"># no reason to keep these attributes around.</span>
<span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">macro_block</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">macro_globals</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">macro_locals</span>
</pre></div>
</div>
<p>The above class may then be used in a with-bang as follows:</p>
<div class="highlight-xonsh notranslate"><div class="highlight"><pre><span></span><span class="k">with!</span> <span class="n">XmlBlock</span><span class="p">()</span> <span class="k">as</span> <span class="n">tree</span><span class="p">:</span>
<span class="o"><</span><span class="n">note</span><span class="o">></span>
<span class="o"><</span><span class="n">to</span><span class="o">></span><span class="n">You</span><span class="o"></</span><span class="n">to</span><span class="o">></span>
<span class="o"><</span><span class="n">from</span><span class="o">></span><span class="n">Xonsh</span><span class="o"></</span><span class="n">from</span><span class="o">></span>
<span class="o"><</span><span class="n">heading</span><span class="o">></span><span class="n">Don</span><span class="s1">'t You Want Me, Baby</heading></span>
<span class="o"><</span><span class="n">body</span><span class="o">></span>
<span class="n">You</span> <span class="n">know</span> <span class="n">I</span> <span class="n">don</span><span class="s1">'t believe you when you say that you don'</span><span class="n">t</span> <span class="n">need</span> <span class="n">me</span><span class="o">.</span>
<span class="o"></</span><span class="n">body</span><span class="o">></span>
<span class="o"></</span><span class="n">note</span><span class="o">></span>
</pre></div>
</div>
<p>And if you run this, you’ll see that the <code class="docutils literal notranslate"><span class="pre">tree</span></code> object really is a parsed
XML object.</p>
<div class="highlight-xonshcon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">tag</span><span class="p">)</span>
<span class="go">note</span>
</pre></div>
</div>
<p>So in roughly eight lines of xonsh code, you can seamlessly interface
with another, vastly different language.</p>
<p>The possibilities for this are not limited to just markup languages or other
party tricks. You could be a remote execution interface via SSH, RPC,
dask / distributed, etc. The real benefit of context manager macros is
that they allow you to select when, where, and what code is executed as a
part of the xonsh language itself.</p>
<p>The power is there; use it without reservation!</p>
</section>
<section id="take-away">
<h2>Take Away<a class="headerlink" href="#take-away" title="Link to this heading">¶</a></h2>
<p>Hopefully, at this point, you see that a few well placed macros can be extremely
convenient and valuable to any project.</p>
</section>
</section>
</article>
</div>
<footer>
<div class="related-pages">
<a class="next-page" href="tutorial_xontrib.html">
<div class="page-info">
<div class="context">
<span>Next</span>
</div>
<div class="title">Tutorial: Extensions (Xontribs)</div>
</div>
<svg class="furo-related-icon"><use href="#svg-arrow-right"></use></svg>
</a>
<a class="prev-page" href="tutorial_hist.html">
<svg class="furo-related-icon"><use href="#svg-arrow-right"></use></svg>
<div class="page-info">
<div class="context">
<span>Previous</span>
</div>
<div class="title">Tutorial: History</div>
</div>
</a>
</div>
<div class="bottom-of-page">
<div class="left-details">
<div class="copyright">
Copyright © 2015, Anthony Scopatz
</div>
Made with <a href="https://www.sphinx-doc.org/">Sphinx</a> and <a class="muted-link" href="https://pradyunsg.me">@pradyunsg</a>'s
<a href="https://github.com/pradyunsg/furo">Furo</a>
</div>
<div class="right-details">
</div>
</div>
</footer>
</div>
<aside class="toc-drawer">
<div class="toc-sticky toc-scroll">
<div class="toc-title-container">
<span class="toc-title">
On this page
</span>
</div>
<div class="toc-tree-container">
<div class="toc-tree">
<ul>
<li><a class="reference internal" href="#">Tutorial: Macros</a><ul>
<li><a class="reference internal" href="#what-are-macro-instructions">What are macro instructions?</a></li>
<li><a class="reference internal" href="#when-and-where-are-macros-used">When and where are macros used?</a></li>
<li><a class="reference internal" href="#function-macros">Function Macros</a></li>
<li><a class="reference internal" href="#calling-function-macros">Calling Function Macros</a></li>
<li><a class="reference internal" href="#writing-function-macros">Writing Function Macros</a><ul>
<li><a class="reference internal" href="#macro-annotations">Macro Annotations</a></li>
<li><a class="reference internal" href="#macro-function-execution-context">Macro Function Execution Context</a></li>
</ul>
</li>
<li><a class="reference internal" href="#subprocess-macros">Subprocess Macros</a></li>
<li><a class="reference internal" href="#context-manager-macros">Context Manager Macros</a></li>
<li><a class="reference internal" href="#take-away">Take Away</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</aside>
</div>
</div><script src="_static/documentation_options.js?v=cbfb3150"></script>
<script src="_static/doctools.js?v=9bcbadda"></script>
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="_static/scripts/furo.js?v=5fa4622c"></script>
<script src="_static/runthis-client.min.js?v=ad2a40d9"></script>
</body>
</html>