#!/usr/bin/perl -w # closetex [-v] [-a] - print the next closing LaTeX tag (or all) use v5.16; use Getopt::Std; our ($opt_a, $opt_v); getopts('av'); my (@tags, @ind); while (<<>>) { print if $opt_v; while (/(^\s*)\\(begin|end)\{(.*?)\}/g) { if ($2 eq "begin") { push @tags, $3; push @ind, $1; } elsif ($2 eq "end") { die "error: line $.: wanted $_, got $3\n" if $3 ne ($_ = pop @tags); pop @ind; } } } for (reverse @tags) { say pop @ind, "\\end{$_}"; last unless $opt_a; } __END__ # kakoune define-command lc %{ exec 'ZGGclosetex 2>&1zdgl' } # vim command! Lc k` | exe "1,.!closetex -v" | norm ``