| 1 |
% \iffalse meta-comment |
| 2 |
% arara: clean: { files: [ termmenu.aux, termmenu.glo, termmenu.hd, termmenu.idx, termmenu.ilg, termmenu.ind, termmenu.out ] } |
| 3 |
% arara: pdflatex |
| 4 |
% !arara: makeindex |
| 5 |
% !arara: pdflatex |
| 6 |
% arara: pdflatex |
| 7 |
% |
| 8 |
%% |
| 9 |
%% File: termmenu.dtx Copyright(C) 2015 Sean Allred |
| 10 |
%% |
| 11 |
%% termmenu.dtx may be distributed and/or modified under the |
| 12 |
%% conditions of the LaTeX Project Public License (LPPL), |
| 13 |
%% either version 1.3c of this license or (at your option) |
| 14 |
%% any later version. The latest version of this license is |
| 15 |
%% in the file |
| 16 |
%% |
| 17 |
%% http://www.latex-project.org/lppl.txt |
| 18 |
%% |
| 19 |
%% The released version is available from CTAN. |
| 20 |
%% |
| 21 |
%% ----------------------------------------------------------------------- |
| 22 |
%% |
| 23 |
%% The development version can be found at |
| 24 |
%% |
| 25 |
%% http://www.github.com/vermiculus/tex-termmenu |
| 26 |
%% |
| 27 |
%% for those people who are interested. |
| 28 |
%% |
| 29 |
%% ----------------------------------------------------------------------- |
| 30 |
%% |
| 31 |
% |
| 32 |
%<*driver> |
| 33 |
\documentclass[full]{l3doc} |
| 34 |
%</driver> |
| 35 |
%<*driver|package> |
| 36 |
\def\ExplFileName{termmenu} |
| 37 |
\def\ExplFileVersion{0.2} |
| 38 |
\def\ExplFileDate{2015-05-25} |
| 39 |
\def\ExplFileDescription{Terminal-driven menu support} |
| 40 |
\def\ExplFileExtension{dtx} |
| 41 |
%</driver|package> |
| 42 |
%<*driver> |
| 43 |
\begin{document} |
| 44 |
\renewcommand\partname{Part} |
| 45 |
\DocInput{\jobname.dtx} |
| 46 |
\end{document} |
| 47 |
%</driver> |
| 48 |
% \fi |
| 49 |
% |
| 50 |
% \title{^^A |
| 51 |
% The \textsf{termmenu} package\\ Terminal-driven menu support^^A |
| 52 |
% \thanks{This file describes v\ExplFileVersion, |
| 53 |
% last revised \ExplFileDate.}^^A |
| 54 |
% } |
| 55 |
% |
| 56 |
% \author{^^A |
| 57 |
% Sean Allred\thanks |
| 58 |
% {^^A |
| 59 |
% E-mail: |
| 60 |
% \href{mailto:[email protected]} |
| 61 |
% {[email protected]}^^A |
| 62 |
% }^^A |
| 63 |
% } |
| 64 |
% |
| 65 |
% \date{Released \ExplFileDate} |
| 66 |
% |
| 67 |
% \maketitle |
| 68 |
% |
| 69 |
% \begin{documentation} |
| 70 |
% |
| 71 |
% This module provides simple support for terminal-driven menus in |
| 72 |
% \pkg{expl3}. |
| 73 |
% |
| 74 |
% \paragraph{Example of use} |
| 75 |
% \begin{verbatim} |
| 76 |
% \termmenu_new:N \g_demo_termmenu |
| 77 |
% \termmenu_set_name:Nn \g_demo_termmenu { Demo } |
| 78 |
% |
| 79 |
% \termmenu_add:Nnnn \g_demo_termmenu { d, duck } |
| 80 |
% { Oh,~you~know...~:) } |
| 81 |
% { \msg_term:n { Quack! } } |
| 82 |
% |
| 83 |
% \termmenu_do:N \g_demo_termmenu |
| 84 |
% |
| 85 |
% \bye |
| 86 |
% \end{verbatim} |
| 87 |
% \begin{verbatim} |
| 88 |
% $ pdftex demo.tex |
| 89 |
% ************************************************* |
| 90 |
% * Demo |
| 91 |
% ************************************************* |
| 92 |
% |
| 93 |
% The following commands are available: |
| 94 |
% |
| 95 |
% > d, duck |
| 96 |
% Oh, you know... :) |
| 97 |
% |
| 98 |
% \choice=d |
| 99 |
% ************************************************* |
| 100 |
% * Quack! |
| 101 |
% ************************************************* |
| 102 |
% \end{verbatim} |
| 103 |
% |
| 104 |
% \newpage |
| 105 |
% \tableofcontents |
| 106 |
% \section*{Introduction} |
| 107 |
% |
| 108 |
% Menus are effectively documented property lists with a fancy, |
| 109 |
% user-friendly version of \cs{prop_show:N}. |
| 110 |
% |
| 111 |
% \newpage |
| 112 |
% |
| 113 |
% \part{Interface Documentation} |
| 114 |
% |
| 115 |
% \section{Creating and initializing menus} |
| 116 |
% |
| 117 |
% \begin{function}[added = 2015-05-23]{\termmenu_new:N} |
| 118 |
% \begin{syntax} |
| 119 |
% \cs{termmenu_new:N} \meta{menu} |
| 120 |
% \end{syntax} |
| 121 |
% Creates a new \meta{menu} or raises an error if the name is |
| 122 |
% already taken. The declaration is global. Initially, the menu |
| 123 |
% will be empty. |
| 124 |
% \end{function} |
| 125 |
% |
| 126 |
% \begin{function}[added = 2015-05-23]{\termmenu_set_name:Nn} |
| 127 |
% \begin{syntax} |
| 128 |
% \cs{termmenu_set_name:Nn} \meta{menu} \Arg{name} |
| 129 |
% \end{syntax} |
| 130 |
% Give \meta{menu} a human-friendly name. When a menu is being |
| 131 |
% presented, \meta{name} will appear as a title. |
| 132 |
% \end{function} |
| 133 |
% |
| 134 |
% \begin{function}[added = 2015-05-23]{\termmenu_add:Nnnn} |
| 135 |
% \begin{syntax} |
| 136 |
% \cs{termmenu_add:Nnnn} \meta{menu} \Arg{entry} \Arg{help text} \Arg{value} |
| 137 |
% \end{syntax} |
| 138 |
% Insert \meta{entry} into a \meta{menu} and provide \meta{help |
| 139 |
% text}. When a menu is being presented, both \meta{entry} and |
| 140 |
% \meta{help text} will be shown. When \meta{entry} is being used, |
| 141 |
% \meta{value} will be associated with it. |
| 142 |
% |
| 143 |
% To simplify the user experience, \meta{entry} can be a |
| 144 |
% comma-separated list of synonymous options. This can be used to |
| 145 |
% create shortcuts to functionality: instead of always typing out |
| 146 |
% |entry|, the end-user can simply say |o| if something like the |
| 147 |
% following is used: |
| 148 |
% \begin{verbatim} |
| 149 |
% \termmenu_add:Nnnn \g_tmpa_termmenu { o, option } { ... } { ... } |
| 150 |
% \end{verbatim} |
| 151 |
% \end{function} |
| 152 |
% |
| 153 |
% \section{Using menus} |
| 154 |
% |
| 155 |
% To allow for ad-hoc processing, menus are shown and acted upon in |
| 156 |
% four separate phases: display, input, retrieval, insertion. |
| 157 |
% |
| 158 |
% \begin{enumerate} |
| 159 |
% \item The display phase displays the menu to the end-user. |
| 160 |
% \item Then, an input routine is called upon to store information |
| 161 |
% into a specific variable while prompting for a different one. |
| 162 |
% (This is useful to keep the interface clean. The internal |
| 163 |
% variable can be very strange-looking, but the user can see |
| 164 |
% whatever you which them to see.) |
| 165 |
% \item The retrieval phase receives the user's input and looks it up |
| 166 |
% in the list of option synonyms, returning the matching menu entry. |
| 167 |
% \item When the entry has been found, the appropriate value is either |
| 168 |
% returned to the programmer or inserted into the input stream. |
| 169 |
% \end{enumerate} |
| 170 |
% |
| 171 |
% \begin{function}[added = 2015-05-24]{\termmenu_do:NNNNN, |
| 172 |
% \termmenu_do:NNNN, |
| 173 |
% \termmenu_do:NNN, |
| 174 |
% \termmenu_do:NN, |
| 175 |
% \termmenu_do:N} |
| 176 |
% \begin{syntax} |
| 177 |
% \cs{termmenu_do:NNNNN} \meta{menu} \meta{prompt token} \meta{input tl} \meta{entry tl} \meta{value tl} |
| 178 |
% \cs{termmenu_do:NNNN} \meta{menu} \meta{prompt token} \meta{input tl} \meta{entry tl} |
| 179 |
% \cs{termmenu_do:NNN} \meta{menu} \meta{input tl} \meta{value tl} |
| 180 |
% \cs{termmenu_do:NN} \meta{menu} \meta{prompt token} |
| 181 |
% \cs{termmenu_do:N} \meta{menu} |
| 182 |
% \end{syntax} |
| 183 |
% The \cs{termmenu_do:} family of functions are convenience wrappers |
| 184 |
% around the basic display, prompt, and lookup functions. |
| 185 |
% \begin{description} |
| 186 |
% \item[|do:NNNNN|] uses \meta{menu} to prompt the user for \meta{prompt |
| 187 |
% token}, setting their input to \meta{input tl}, setting the |
| 188 |
% matched entry to \meta{entry tl}, and storing the associated value |
| 189 |
% in \meta{value tl}. |
| 190 |
% |
| 191 |
% \item[|do:NNNN|] is the same as |do:NNNNN|, but instead of storing the |
| 192 |
% output in a token list, it is inserted into the input stread. |
| 193 |
% |
| 194 |
% \item[|do:NNN|] is similar to |do:NNNNN|, but does not allow the |
| 195 |
% programmer to set what the user sees as a prompt. Instead, the |
| 196 |
% generic \cs{choice} name is used for \meta{input tl}. |
| 197 |
% Additionally, the associated menu entry is not stored. |
| 198 |
% |
| 199 |
% \item[|do:NN|] is like |do:NNNN|, but does not store either the user's |
| 200 |
% input or its associated menu entry. The only parameter this takes |
| 201 |
% is the \meta{prompt token}. |
| 202 |
% |
| 203 |
% \item[|do:N|] is the simplest way to use a menu. It uses \cs{choice} for |
| 204 |
% the \meta{prompt token} and places the associated value in the |
| 205 |
% input stream. |
| 206 |
% \end{description} |
| 207 |
% \end{function} |
| 208 |
% |
| 209 |
% \section{Input and output} |
| 210 |
% |
| 211 |
% \begin{function}[added = 2015-05-23, updated = 2015-05-24]{\termmenu_prompt:NN, \termmenu_prompt:N} |
| 212 |
% \begin{syntax} |
| 213 |
% \cs{termmenu_prompt:NN} \meta{input tl} \meta{prompt token} |
| 214 |
% \cs{termmenu_prompt:N} \meta{input tl} |
| 215 |
% \end{syntax} |
| 216 |
% Read a value for \meta{input tl} showing \meta{prompt token} to |
| 217 |
% the user. When \meta{prompt token} isn't provided, it defaults to |
| 218 |
% \cs{choice}. |
| 219 |
% \end{function} |
| 220 |
% |
| 221 |
% \begin{variable}[added = 2015-05-23]{\l_termmenu_prompt_tl} |
| 222 |
% This is the message displayed to the user when a menu is |
| 223 |
% presented. The default value is |
| 224 |
% |The~following~commands~are~available:|. |
| 225 |
% \end{variable} |
| 226 |
% |
| 227 |
% \section{Inspection} |
| 228 |
% |
| 229 |
% \begin{function}[added = 2015-05-23]{\termmenu_show:N} |
| 230 |
% \begin{syntax} |
| 231 |
% \cs{termmenu_show:N} \meta{menu} |
| 232 |
% \end{syntax} |
| 233 |
% Show the contents of \meta{menu}. Right now, this is the same as |
| 234 |
% \cs{prop_show:N}. |
| 235 |
% \end{function} |
| 236 |
% |
| 237 |
% \begin{function}[added = 2015-05-23, updated = 2015-05-24]{\termmenu_get_name:NN} |
| 238 |
% \begin{syntax} |
| 239 |
% \cs{termmenu_get_name:NN} \meta{menu} \meta{name tl} |
| 240 |
% \end{syntax} |
| 241 |
% Retrieve the name of \meta{menu} and place it in \meta{name tl}. |
| 242 |
% \end{function} |
| 243 |
% |
| 244 |
% \begin{function}[added = 2015-05-25]{\termmenu_entry:NnN, \termmenu_entry:NVN} |
| 245 |
% \begin{syntax} |
| 246 |
% \cs{termmenu_entry:NnN} \meta{menu} \Arg{option} \meta{entry tl} |
| 247 |
% \end{syntax} |
| 248 |
% Finds the entry that \meta{option} would refer to in \meta{menu} |
| 249 |
% and places it in \meta{entry tl}. |
| 250 |
% \end{function} |
| 251 |
% |
| 252 |
% \begin{function}[added = 2015-05-25]{\termmenu_doc:NnN} |
| 253 |
% \begin{syntax} |
| 254 |
% \cs{termmenu_doc:NnN} \meta{menu} \Arg{entry} \meta{doc tl} |
| 255 |
% \end{syntax} |
| 256 |
% Finds the documentation for \meta{entry} in \meta{menu} and places |
| 257 |
% it in \meta{doc tl}. |
| 258 |
% \end{function} |
| 259 |
% |
| 260 |
% \begin{function}[added = 2015-05-25]{\termmenu_value:NnN, \termmenu_value:NVN} |
| 261 |
% \begin{syntax} |
| 262 |
% \cs{termmenu_value:NnN} \meta{menu} \Arg{entry} \meta{value tl} |
| 263 |
% \end{syntax} |
| 264 |
% Places \meta{menu}'s value for \meta{entry} in \meta{value tl}. |
| 265 |
% \end{function} |
| 266 |
% |
| 267 |
% \section{Internal variables and functions} |
| 268 |
% |
| 269 |
% \begin{variable}[added = 2015-05-23]{\g__termmenu_names_prop} |
| 270 |
% This property list stores the names of each menu. The keys of the |
| 271 |
% property lists are menus (e.g., \cs{g_demo_termmenu}) and the |
| 272 |
% values are their names. |
| 273 |
% \end{variable} |
| 274 |
% |
| 275 |
% \begin{variable}[added = 2015-05-23]{\l__termmenu_spec_tl} |
| 276 |
% Generally speaking, this scratch variable stores lower-level |
| 277 |
% information about a menu (or one of its entries). |
| 278 |
% \end{variable} |
| 279 |
% |
| 280 |
% \begin{variable}[added = 2015-05-23]{\g__termmenu_doc_tl} |
| 281 |
% This scratch variable stores the documentation for an entry. It |
| 282 |
% is necessary because none of |f|, |x|, |o|-type expansions seem to |
| 283 |
% work where they need to. Patches\slash pull requests welcome. |
| 284 |
% ^^A For reference, this place is \cs{termmenu_display:N}. |
| 285 |
% \end{variable} |
| 286 |
% |
| 287 |
% \begin{variable}[added = 2015-05-24]{\l__termmenu_value_tl} |
| 288 |
% This scratch variable stores the value of an entry. It is |
| 289 |
% immediately inserted back into the input stream. |
| 290 |
% \end{variable} |
| 291 |
% |
| 292 |
% \begin{variable}[added = 2015-05-24]{\l__termmenu_tmp_tl} |
| 293 |
% This scratch variable holds values returned by the various |do:| |
| 294 |
% convenience macros. This value serves as a sort of |
| 295 |
% \enquote*{trash bin} to throw away some unwanted values. |
| 296 |
% \end{variable} |
| 297 |
% |
| 298 |
% \begin{variable}[added = 2015-05-24]{\g__termmenu_tmp_tl} |
| 299 |
% This scratch variable is used to help set \meta{tl var} from |
| 300 |
% \cs{termmenu_find:NnN}. |
| 301 |
% \end{variable} |
| 302 |
% |
| 303 |
% \begin{variable}[added = 2015-05-24]{\g__termmenu_opt_bool} |
| 304 |
% This variable is used to signal if the user's input was able to |
| 305 |
% match against a known option. |
| 306 |
% \end{variable} |
| 307 |
% |
| 308 |
% \begin{function}[added = 2015-05-24]{\termmenu_display:N} |
| 309 |
% \begin{syntax} |
| 310 |
% \cs{termmenu_display:N} \meta{menu} |
| 311 |
% \end{syntax} |
| 312 |
% Writes out \meta{menu} to the terminal. No associated actions |
| 313 |
% are performed. If \meta{menu} has a name (i.e., an entry in |
| 314 |
% \cs{__termmenu_names_prop}), print it as well. |
| 315 |
% \end{function} |
| 316 |
% |
| 317 |
% \end{documentation} |
| 318 |
% |
| 319 |
% \newpage |
| 320 |
% |
| 321 |
% \part{Implementation} |
| 322 |
% |
| 323 |
% Before I begin, I want to establish a few definitions: |
| 324 |
% \begin{description} |
| 325 |
% \item[menu] The over-arching data structure that holds (nearly) all |
| 326 |
% related information for a given menu. |
| 327 |
% \item[entry] A full, comma-separated specification of valid |
| 328 |
% inputs for a given menu item. |
| 329 |
% \item[option] A valid input for an entry. |
| 330 |
% \item[value] The associated action for an entry. |
| 331 |
% \item[prompt token] The token read in by \TeX{} on a low level; this |
| 332 |
% token's name is presented to the user in output. |
| 333 |
% \end{description} |
| 334 |
% |
| 335 |
% \begin{implementation} |
| 336 |
% |
| 337 |
% \section{Creation and initialization} |
| 338 |
% |
| 339 |
% \begin{macrocode} |
| 340 |
%<*package> |
| 341 |
% \end{macrocode} |
| 342 |
% \begin{macrocode} |
| 343 |
%<@@=termmenu> |
| 344 |
% \end{macrocode} |
| 345 |
% |
| 346 |
% \begin{macro}{\termmenu_new:N, \termmenu_show:N, \termmenu_add:Nnnn} |
| 347 |
% Each menu is implemented as a property list of options mapped |
| 348 |
% to documentation and actions. Each option is a property list |
| 349 |
% key. Since the there is no good way to distinguish between |
| 350 |
% when documentation ends and an associated action begins, the |
| 351 |
% documentation is placed in a group at the head of the key's |
| 352 |
% value.\footnote{This could also be done with sequences, but I |
| 353 |
% consider that unnecessary overhead and complication.} |
| 354 |
% \begin{macrocode} |
| 355 |
\cs_set_eq:NN \termmenu_new:N \prop_new:N |
| 356 |
\cs_set_eq:NN \termmenu_show:N \prop_show:N |
| 357 |
\cs_new_nopar:Nn \termmenu_add:Nnnn |
| 358 |
{ \prop_put:Nnn #1 {#2} { {#3} #4 } } |
| 359 |
% \end{macrocode} |
| 360 |
% \end{macro} |
| 361 |
% |
| 362 |
% \section{Convenience functions} |
| 363 |
% |
| 364 |
% \begin{variable}{\l_@@_tmp_tl, \l_@@_value_tl} |
| 365 |
% These scratch variables are used in the following code. You may |
| 366 |
% be reviewing the code and thing to yourself, \enquote{Wait, |
| 367 |
% couldn't you use just one variable?} Rest assured that you |
| 368 |
% cannot: \cs{termmenu_do:NNNN} uses \cs{l_@@_value_tl} to insert |
| 369 |
% the value code into the input stream. Using the same variable for |
| 370 |
% this would be at best confusing\slash unmaintainble. Besides, |
| 371 |
% \cs{l_@@_tmp_tl} is supposed to be a variable for unwanted values. |
| 372 |
% \begin{macrocode} |
| 373 |
\tl_new:N \l_@@_tmp_tl |
| 374 |
\tl_new:N \l_@@_value_tl |
| 375 |
% \end{macrocode} |
| 376 |
% \end{variable} |
| 377 |
% |
| 378 |
% \begin{macro}{\termmenu_do:NNNNN, |
| 379 |
% \termmenu_do:NNNN, |
| 380 |
% \termmenu_do:NNN, |
| 381 |
% \termmenu_do:NN, |
| 382 |
% \termmenu_do:N} |
| 383 |
% These functions make menus easier to use by choosing some sane |
| 384 |
% defaults and running through the entire flow. Each of these |
| 385 |
% macros is ultimately based on the most general one, |
| 386 |
% \cs{termmenu_do:NNNNN}. This results in some waste of work, but |
| 387 |
% those extra gets\slash sets pale in comparison to wrapping the |
| 388 |
% menu output, executing whatever the menu entry stands for, |
| 389 |
% \emph{actually waiting for the user to make a choice}, etc. |
| 390 |
% ^^A NNNNN <in menu> <out input> <in prompt> <out entry> <out value> |
| 391 |
% ^^A NNNN <in menu> <out input> <in prompt> <out entry> >>value |
| 392 |
% ^^A NNN <in menu> <out input> <out value> |
| 393 |
% ^^A NN <in menu> <in prompt> |
| 394 |
% ^^A N <in menu> |
| 395 |
% \begin{macrocode} |
| 396 |
\cs_new_nopar:Nn \termmenu_do:NNNNN |
| 397 |
{ |
| 398 |
\termmenu_display:N #1 |
| 399 |
\termmenu_prompt:NN #2 #3 |
| 400 |
\termmenu_entry:NVN #1 #2 #4 |
| 401 |
\termmenu_value:NVN #1 #4 #5 |
| 402 |
} |
| 403 |
\cs_new_nopar:Nn \termmenu_do:NNNN |
| 404 |
{ |
| 405 |
\tl_clear:N \l_@@_value_tl |
| 406 |
\termmenu_do:NNNNN #1 #2 #3 #4 \l_@@_value_tl |
| 407 |
\tl_use:N \l_@@_value_tl |
| 408 |
} |
| 409 |
\cs_new_nopar:Nn \termmenu_do:NNN |
| 410 |
{ \termmenu_do:NNNNN #1 #2 \choice \l_@@_tmp_tl #3 } |
| 411 |
\cs_new_nopar:Nn \termmenu_do:NN |
| 412 |
{ \termmenu_do:NNNN #1 \l_@@_tmp_tl #2 \l_@@_tmp_tl } |
| 413 |
\cs_new_nopar:Nn \termmenu_do:N |
| 414 |
{ \termmenu_do:NN #1 \choice } |
| 415 |
% \end{macrocode} |
| 416 |
% \end{macro} |
| 417 |
% |
| 418 |
% |
| 419 |
% \section{Retrieving values} |
| 420 |
% |
| 421 |
% \begin{variable}{\l_@@_spec_tl} |
| 422 |
% \cs{l_@@_spec_tl} is used when retrieving data from property |
| 423 |
% lists. It's used quite a bit in the following functions. |
| 424 |
% \begin{macrocode} |
| 425 |
\tl_new:N \l_@@_tmp_tl |
| 426 |
\tl_new:N \l_@@_value_tl |
| 427 |
\tl_new:N \l_@@_spec_tl |
| 428 |
% \end{macrocode} |
| 429 |
% \end{variable} |
| 430 |
% |
| 431 |
% \begin{macro}{\termmenu_value:NnN, \termmenu_value:NVN} |
| 432 |
% This function retrieves the value for entry |#2| in menu |#1| and |
| 433 |
% places it in |#3|. Remember that the documentation and value are |
| 434 |
% stored together in the property list. Since the documentation is |
| 435 |
% kept in a group, we can use \cs{tl_tail:N} to grab just the |
| 436 |
% desired value. |
| 437 |
% \begin{macrocode} |
| 438 |
\cs_new_nopar:Nn \termmenu_value:NnN |
| 439 |
{ |
| 440 |
\prop_get:NnN #1 {#2} \l_@@_spec_tl |
| 441 |
\tl_set:Nf #3 { \tl_tail:N \l_@@_spec_tl } %@todo test expansion |
| 442 |
} |
| 443 |
\cs_generate_variant:Nn \termmenu_value:NnN { NVN } |
| 444 |
% \end{macrocode} |
| 445 |
% \end{macro} |
| 446 |
% |
| 447 |
% |
| 448 |
% \section{Entry documentation} |
| 449 |
% |
| 450 |
% |
| 451 |
% \begin{variable}{\g_@@_doc_tl} |
| 452 |
% This variable stores the help text for an option. |
| 453 |
% \begin{macrocode} |
| 454 |
\tl_new:N \l__termmenu_doc_tl |
| 455 |
% \end{macrocode} |
| 456 |
% \end{variable} |
| 457 |
% |
| 458 |
% \begin{macro}{\termmenu_doc:NnN} |
| 459 |
% These functions retrieve the help text for entry |#2| in |#1| and |
| 460 |
% place it in |#3|. Keep in mind the structure of |
| 461 |
% \cs{l_@@_names_prop}. |
| 462 |
% \begin{macrocode} |
| 463 |
\cs_new_nopar:Nn \termmenu_doc:NnN |
| 464 |
{ |
| 465 |
\prop_get:NnN #1 {#2} \l_@@_spec_tl |
| 466 |
\tl_set:Nf #3 { \tl_head:N \l_@@_spec_tl } %@todo test expansion |
| 467 |
} |
| 468 |
% \end{macrocode} |
| 469 |
% \end{macro} |
| 470 |
% |
| 471 |
% |
| 472 |
% \section{Entry lookup} |
| 473 |
% |
| 474 |
% |
| 475 |
% \begin{variable}{\g_@@_tmp_tl} |
| 476 |
% This scratch variable is used to escape the groups of mapping |
| 477 |
% constructs like \cs{prop_map_inline:Nn}. |
| 478 |
% \begin{macrocode} |
| 479 |
\tl_new:N \g_@@_tmp_tl |
| 480 |
% \end{macrocode} |
| 481 |
% \end{variable} |
| 482 |
% |
| 483 |
% \begin{variable}{\g_@@_opt_bool} |
| 484 |
% This scratch variable is used to signal if a suitable option was |
| 485 |
% found when searching for a match based on user input. Since it is |
| 486 |
% used inside two inlined mappings, it is most straightforward for |
| 487 |
% all assignments to be global. |
| 488 |
% \begin{macrocode} |
| 489 |
\bool_new:N \g_@@_opt_bool |
| 490 |
% \end{macrocode} |
| 491 |
% \end{variable} |
| 492 |
% |
| 493 |
% \begin{macro}{\termmenu_entry:NnN, \termmenu_entry:NVN} |
| 494 |
% Using the menu in |#1|, find a match for |#2| and stick that match |
| 495 |
% in |#3|. |
| 496 |
% \begin{macrocode} |
| 497 |
\cs_new_nopar:Nn \termmenu_entry:NnN |
| 498 |
{ |
| 499 |
\prop_map_inline:Nn #1 |
| 500 |
{ |
| 501 |
% \end{macrocode} |
| 502 |
% If the input value is in the list of synonyms, set our output value |
| 503 |
% to the full list, indicate that we've found a match, and break out |
| 504 |
% of the loop. Note that we use global variables to free ourselves |
| 505 |
% from the confines of the group. |
| 506 |
% \begin{macrocode} |
| 507 |
\clist_if_in:nnT {##1} {#2} |
| 508 |
{ |
| 509 |
\tl_gset:Nn \g_@@_tmp_tl {##1} |
| 510 |
\bool_gset_true:N \g_@@_opt_bool |
| 511 |
\prop_map_break: |
| 512 |
} |
| 513 |
} |
| 514 |
% \end{macrocode} |
| 515 |
% If we haven't set \cs{g_@@_opt_bool} by the time we've finished |
| 516 |
% looking, we never found anything. Set |#3| to \cs{q_no_value} to |
| 517 |
% indicate the lack of a match and reset the value of |
| 518 |
% \cs{g_@@_opt_bool}. |
| 519 |
% \begin{macrocode} |
| 520 |
\bool_if:NTF \g_@@_opt_bool |
| 521 |
{ \tl_set_eq:NN #3 \g_@@_tmp_tl } |
| 522 |
{ \tl_set:Nn #3 { \q_no_value } } |
| 523 |
\bool_gset_false:N \g_@@_opt_bool |
| 524 |
} |
| 525 |
\cs_generate_variant:Nn \termmenu_entry:NnN { NVN } |
| 526 |
% \end{macrocode} |
| 527 |
% \end{macro} |
| 528 |
% |
| 529 |
% |
| 530 |
% \section{Retrieving input} |
| 531 |
% |
| 532 |
% |
| 533 |
% \begin{variable}{\l_termmenu_prompt_tl} |
| 534 |
% This public variable contains the text to be used as a prompt when |
| 535 |
% displaying a menu. It is given a reasonable default. |
| 536 |
% \begin{macrocode} |
| 537 |
\tl_new:N \l_termmenu_prompt_tl |
| 538 |
\tl_set:Nn \l_termmenu_prompt_tl |
| 539 |
{ The~following~commands~are~available: } |
| 540 |
% \end{macrocode} |
| 541 |
% \end{variable} |
| 542 |
% |
| 543 |
% \begin{macro}{\termmenu_prompt:NN, \termmenu_prompt:N} |
| 544 |
% This function is a little interesting. In order to keep the |
| 545 |
% end-user's interface clean, we can't simply prompt for the |
| 546 |
% destination variable. Say, if the destination variable were |
| 547 |
% something like \cs{l__some_confusing_variable_name}, this would |
| 548 |
% cause \TeX{} to display that confusing variable name to the |
| 549 |
% end-user as part of the prompt. Instead, we ask for |#2| |
| 550 |
% (starting a new group to avoid clobbering any existing definition) |
| 551 |
% and \TeX{} puts the user's input in |#2|. Now we have to get this |
| 552 |
% value outside the group and into |#1|. The |
| 553 |
% |\expandafter\endgroup| trick works nicely here (and is the |
| 554 |
% official\footnote{\url{http://tex.stackexchange.com/a/246542}} way |
| 555 |
% to do this). |
| 556 |
% \begin{macrocode} |
| 557 |
\cs_new_nopar:Nn \termmenu_prompt:NN |
| 558 |
{ |
| 559 |
\group_begin: |
| 560 |
\termmenu_prompt:N #2 |
| 561 |
\exp_args:NNNV \group_end: |
| 562 |
\tl_set:Nn #1 #2 |
| 563 |
} |
| 564 |
\cs_new_nopar:Nn \termmenu_prompt:N |
| 565 |
{ \ior_get_str:NN \c_term_ior #1 } |
| 566 |
% \end{macrocode} |
| 567 |
% \end{macro} |
| 568 |
% |
| 569 |
% |
| 570 |
% \section{Names} |
| 571 |
% |
| 572 |
% |
| 573 |
% \begin{variable}{\g_@@_names_prop} |
| 574 |
% Globally define the property list to store menu names. |
| 575 |
% \begin{macrocode} |
| 576 |
\prop_new:N \g_@@_names_prop |
| 577 |
% \end{macrocode} |
| 578 |
% \end{variable} |
| 579 |
% |
| 580 |
% \begin{macro}{\termmenu_get_name:NN} |
| 581 |
% \begin{macrocode} |
| 582 |
\cs_new_nopar:Nn \termmenu_get_name:NN |
| 583 |
{ \prop_get:NnN \g__termmenu_names_prop {#1} #2 } |
| 584 |
% \end{macrocode} |
| 585 |
% \end{macro} |
| 586 |
% |
| 587 |
% \begin{macro}{\termmenu_set_name:Nn} |
| 588 |
% This function names a menu. An entry is placed in |
| 589 |
% \cs{g_@@_names_prop} with the menu |#1| as a key and its name |#2| |
| 590 |
% as the value. |
| 591 |
% \begin{macrocode} |
| 592 |
\cs_new_nopar:Nn \termmenu_set_name:Nn |
| 593 |
{ \prop_put:Nnn \g__termmenu_names_prop {#1} {#2} } |
| 594 |
% \end{macrocode} |
| 595 |
% \end{macro} |
| 596 |
% |
| 597 |
% |
| 598 |
% |
| 599 |
% \section{Display} |
| 600 |
% |
| 601 |
% |
| 602 |
% |
| 603 |
% \begin{macro}{\termmenu_display:N} |
| 604 |
% Perhaps the only truly complicated part of this package is this |
| 605 |
% function. Let's generate some new terminal output variants to |
| 606 |
% make our lives easier. |
| 607 |
% \begin{macrocode} |
| 608 |
\cs_generate_variant:Nn \iow_term:n { V } |
| 609 |
\cs_generate_variant:Nn \msg_term:n { V } |
| 610 |
\cs_new_nopar:Nn \termmenu_display:N |
| 611 |
{ |
| 612 |
% \end{macrocode} |
| 613 |
% First, we retrieve the name of the menu. If it does not |
| 614 |
% exist, print a generic \enquote{Menu} header. If it does |
| 615 |
% exist, use the menu's title as the header. |
| 616 |
% \begin{macrocode} |
| 617 |
\termmenu_get_name:NN #1 \l_@@_spec_tl |
| 618 |
\quark_if_no_value:NTF \l_@@_spec_tl |
| 619 |
{ \msg_term:n { Menu } } |
| 620 |
{ \msg_term:V \l_@@_spec_tl } |
| 621 |
% \end{macrocode} |
| 622 |
% Display the prompt. Note that \cs{iow_term:n} without an argument |
| 623 |
% will simply output one blank line. |
| 624 |
% \begin{macrocode} |
| 625 |
\iow_term:n { } |
| 626 |
\iow_term:V \l_termmenu_prompt_tl |
| 627 |
\iow_term:n { } |
| 628 |
% \end{macrocode} |
| 629 |
% We want to display each option with its help text in a way that is |
| 630 |
% easy to read. |
| 631 |
% \begin{macrocode} |
| 632 |
\prop_map_inline:Nn #1 |
| 633 |
{ |
| 634 |
% \end{macrocode} |
| 635 |
% For each entry |##1| in the menu |#1|, send wrapped output to the |
| 636 |
% terminal that contains the entry \cs{l_@@_tmp_clist} indented by |
| 637 |
% four spaces, a new line, and the documentation \cs{l_@@_tmp_tl}, all |
| 638 |
% wrapped with a running indent of eight spaces. Note that since we |
| 639 |
% introduce a new line with the |\\| macro in \cs{iow_wrap:nnnN}, we |
| 640 |
% get the first indentation of the documentation for free. |
| 641 |
% \begin{macrocode} |
| 642 |
\clist_set:Nn \l_@@_tmp_clist {##1} |
| 643 |
\termmenu_doc:NnN #1 {##1} \l_@@_tmp_tl |
| 644 |
\iow_wrap:nnnN |
| 645 |
{ |
| 646 |
\prg_replicate:nn {4} { \iow_char:N \ } |
| 647 |
> ~ \clist_use:Nn \l_@@_tmp_clist { ,~ } \\ |
| 648 |
\tl_use:N \l_@@_tmp_tl |
| 649 |
} { \prg_replicate:nn {8} { \ } } { } \iow_term:n |
| 650 |
} |
| 651 |
} |
| 652 |
% \end{macrocode} |
| 653 |
% \end{macro} |
| 654 |
% |
| 655 |
% \begin{macrocode} |
| 656 |
%</package> |
| 657 |
% \end{macrocode} |
| 658 |
% |
| 659 |
% \end{implementation} |
| 660 |
% |
| 661 |
% \PrintIndex |