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

dtx file support? #248

Closed
OopsYao opened this issue Feb 23, 2021 · 8 comments
Closed

dtx file support? #248

OopsYao opened this issue Feb 23, 2021 · 8 comments
Labels
answered question has been answered question user-centred question about behaviour of latexindent.pl

Comments

@OopsYao
Copy link

OopsYao commented Feb 23, 2021

I'd love to use this for formatting my .dtx file, but it cannot handle the comment indent properly. And I could not find any docs about the comment indent nor the dtx support.
Suppose I have a demo.dtx file with content

%    \begin{macrocode}
\def\somecommand{
  do something
%    \end{macrocode}
% Some notes for the rest fragment
%    \begin{macrocode}
  do another thing
}
%    \end{macrocode}

After I run

latexindent -y="defaultIndent:'  '" demo.dtx | diff demo.dtx -

It gives

4,6c4,6
< %    \end{macrocode}
< % Some notes for the rest fragment
< %    \begin{macrocode}
---
>   %    \end{macrocode}
>   % Some notes for the rest fragment
>   %    \begin{macrocode}

But I don't want the doc comment to be indented. So I wonder if there is an option to ignore comment indent for .dtx file?

@cmhughes
Copy link
Owner

Take a look at the replacement switch, you should be able to remove leading space in front of comment symbols after indenting.

Let me know how it goes.

@OopsYao
Copy link
Author

OopsYao commented Feb 24, 2021

Thanks for your quick reply, it works, though not perfectly.
With localSettings.yaml

defaultIndent: "  "
replacements:
  - substitution: s/^ +%/%/gm
    when: after

then

latexindent -l -r demo.dtx | diff demo.dtx -

would be ok. The regex is simple, but also trims the leading space of comments inside macro code

% ^^A demo2.dtx
%    \begin{macrocode}
\def\somecommand{
  % comment inside macrocode
  do something
%    \end{macrocode}
% Some notes for the rest fragment
%    \begin{macrocode}
  do another thing
}
%    \end{macrocode}
4c4
<   % comment inside macrocode
---
> % comment inside macrocode

In most cases, the dtx doc code would be comment lines between % \end{macrocode} and % \begin{macrocode}. So dealing with those lines is properer. But I could not find a proper regex to only match leading spaces in such pattern (not familiar with perl regex, but I believe it exists. 👀)
And I also tried the specialBeginEnd option to specify indent rules, but it seems not working with lines with leading %.
And the noIndentBlock would be another approach, but it assumes the environment begins with % \begin{macrocode} and ends with % \end{macrocode}, not inversely.

It would be better if it can handle doc code separately, as if there is no leading %, thus supporting .dtx file almostly.

@cmhughes
Copy link
Owner

Your original post and your response seem to contradict each other.

@OopsYao
Copy link
Author

OopsYao commented Feb 24, 2021

You missed my point. I do not want to trim the leading space of all comment lines, just those between % \end{macrocode} and % \begin{macrocode}.
In the example below, line 4 and line 6-8 should be treated differently. As the former is comment in macrocode, which should be indented as normal, and the latter is the doc code (do no indent).

carbon

@cmhughes
Copy link
Owner

So, just to be completely clear, your desired result is

% ^^A demo2.dtx
%    \begin{macrocode}
\def\somecommand{
  % comment inside macrocode
  do something
%    \end{macrocode}
% Some notes for the rest fragment
%    \begin{macrocode}
  do another thing
}
%    \end{macrocode}

If not, please provide your desired output as text, not a screenshot.

@OopsYao
Copy link
Author

OopsYao commented Feb 27, 2021

Yes it is. (The screenshot was intended for showing line numbers 👀)

@cmhughes
Copy link
Owner

cmhughes commented Mar 6, 2021

I don't have a way to do this in a single call, but it can be done in two calls. Starting with your original code, if you run

latexindent.pl -w myfile.tex
latexindent.pl -l oopsyao.yaml -rr myfile.tex

with oopsyao.yaml as follows:

defaultIndent: "  "
replacements:
  - 
    when: after
    substitution: |-
       s/\h*(\%\h*\\end\{macrocode\})
           (.*?)
         \h*(\%\h*\\begin\{macrocode\})/
         my $begin = $1;
         my $body  = $2;
         my $end   = $3;
         $body =~ s|^\h*\%|%|mg;
         $begin.$body.$end;/smgxe

then the output is

% ^^A demo2.dtx
%    \begin{macrocode}
\def\somecommand{
  % comment inside macrocode
  do something
%    \end{macrocode}
% Some notes for the rest fragment
% more comments
%    \begin{macrocode}
  do another thing
}
%    \end{macrocode}

The reason that two calls to latexindent.pl are necessary is because if the indentation mode is active (which it is when anything other than the -rr switch is active) then trailing comments are stored so that latexindent doesn't know what string is contained within the comment.

I hope this helps. For reference, there's also https://tex.stackexchange.com/a/528732/6621. Let me know.

@cmhughes cmhughes added the question user-centred question about behaviour of latexindent.pl label Mar 6, 2021
@cmhughes
Copy link
Owner

I'll close this for the moment, but let me know if you have any other questions.

@cmhughes cmhughes added the answered question has been answered label Aug 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered question has been answered question user-centred question about behaviour of latexindent.pl
Projects
None yet
Development

No branches or pull requests

2 participants