Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

declare function: Strip spaces around each function arg #1124

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

muzimuzhi
Copy link
Member

Motivation for this change

To reduce the use of \expandafter and make the code more readable, some \pgfexpl@xxx macros are introduced.

Fixes #1123

Checklist

Please signoff your commits to explicitly state your agreement to the Developer Certificate of Origin. If that is not possible you may check the boxes below instead:

@@ -184,7 +184,7 @@
\expandafter\pgfmath@toks\expandafter=\expandafter{\pgfmath@local@body}%
\else%
\pgfmath@toks={}%
\expandafter\pgfmath@local@function@@body\pgfmath@local@args,,%
\expandafter\pgfmath@local@function@@body@trimspaces\pgfmath@local@args,,%
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\exp_last_unbraced:No fits this case, but it won't obviously increase the readability.

% original text unexpanded in the input stream. Same as \trim@spaces@noexp
% in trimspaces.sty
\def\pgfutil@trimspaces@noexp#1{%
\unexpanded\expandafter\expandafter\expandafter{\pgfutil@trimspaces{#1}}%
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a pity that there's no expl3 function to handle this two-step expansion case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\exp_args:Nf should do that. It basically equals \expandafter#1\expandafter{\romannumeral-`0#2}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We used to have them (d-type), but there are very few places we found we needed them - as we've required e-TeX for a long time, we've been able to rely on most functions including \unexpanded to protect their return values. Also, now we have \expanded, we can use that ... sorry it doesn't work quite so well for you.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I have in my mind is sth more general (and/but less efficient, of course), like \exp_by_steps:nn {<num of steps>} {<tokens>}

\documentclass{article}
\usepackage{pgfmath}

\ExplSyntaxOn\makeatletter
% Expand #2 by exactly #1 steps and leaves the result in input stream. 
% The result is returned within \unexpanded.
\cs_new:Npn \exp_by_steps:nn #1#2
  {
    \int_compare:nNnTF {#1} > 0
      {
        \exp_by_steps:eo { \int_eval:n {#1-1} } {#2}
      }
      {
        \exp_not:n {#2}
      }
  }
\cs_generate_variant:Nn \exp_by_steps:nn {eo}

\exp_args:Ne \tl_show:n { \exp_by_steps:nn {0} {\undefined} }
\exp_args:Ne \tl_show:n { \exp_by_steps:nn {1} {\tl_if_eq:ccTF} }
\exp_args:Ne \tl_show:n { \exp_by_steps:nn {2} {\pgfutil@trimspaces{ \x}} }
\makeatother\ExplSyntaxOff

\begin{document}
\end{document}
% > \undefined .
% > \exp_args:Ncc \tl_if_eq:NNTF .
% > \x .

Signed-off-by: muzimuzhi <muzimuzhi@gmail.com>
@@ -910,5 +919,6 @@
\endgroup
}%
\input pgfutil-common-lists.tex
\input pgfutil-common-expl.tex
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be inlined into pgfutil-common.tex directly. Loading files in TeX is slow. @josephwright promised to develop some kind of luadocstrip for l3build to merge files during build. Once that arrives we can split it again.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made inlined in commit 190342a

Copy link
Member Author

@muzimuzhi muzimuzhi Jan 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since pgfutil-<format>.def is loaded after pgfutil-common.tex, all wrapped primitives [1] are undefined in pgfutil-common.tex. We have to pay attention to not actually expand any of them in defining pgfexpl macros. Not sure if this is achievable in the long term.

[1] Currently there're \pgfutil@protected and \pgfutil@unexpanded. \pgfutil@expanded should be added (see also #1028).

@muzimuzhi
Copy link
Member Author

force pushed to add a changelog entry

% for expl3 macros.
%
% Naming conventions:
% pgf_cs = "pgfexpl@" + expl3_cs.replace("_", "@").replace(":", "@@")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think with a little bit of work we could make _ a letter. There are very few uses of _ outside of \write and comments.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

: is a bit more difficult, because it's used in \pgfutil@ifnextchar (and some other stuff that needs to skip spaces).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doable. Do you expect it happens in current PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say yes, because we should address this immediately as pgfexpl is introduced. However, if you can't do it in a timely manner, we can go ahead without it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about

% Naming conventions:
%   pgf_cs = "__pgfexpl_" + expl3_cs.replace(":", "@")
% For example,
%   expl3_cs: \exp_args:Ne,          \tl_put_right:Nn
%   pgf_cs:   \__pgfexpl_exp_args@Ne, \__pgfexpl_tl_put_right@Nn

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

: is a bit more difficult, because it's used in \pgfutil@ifnextchar (and some other stuff that needs to skip spaces).

Since the ultimate goal is to depend on expl3-code.tex, now I'm kind of leaning more towards making both _ and : letters at once. A constant token list \c__pgfexpl_semicolon_tl and some careful expansion works will overcome the : issue.

which are drop-in replacements for expl3 functions

Signed-off-by: muzimuzhi <muzimuzhi@gmail.com>
Signed-off-by: muzimuzhi <muzimuzhi@gmail.com>
This commit should be ammended to existing ones when the PR is approved

Signed-off-by: muzimuzhi <muzimuzhi@gmail.com>
This commit should be ammended to existing ones when the PR is approved.

Signed-off-by: muzimuzhi <muzimuzhi@gmail.com>
This commit should be ammended to existing ones when the PR is approved.

Signed-off-by: muzimuzhi <muzimuzhi@gmail.com>
@hmenke
Copy link
Member

hmenke commented Feb 1, 2022

This PR is blocked by #1116.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

declare function: Spaces not allowed in function's arg list
3 participants