1

I've written this command to get me "snippets" of code:

\newcommand\codesnippetsnippet[3]{
\immediate\write18{find ../../../.. -name '#1' -print | grep #2 | xargs cat > ./listingwithoutcomments}

\lstinputlisting[autogobble=true,linerange=#3,rangeprefix=//---,includerangemarker=false,language=mycodelanguage,frame=single]{./listingwithoutcomments}

\immediate\write18{rm -f -- 'listingwithoutcomments'}
}

It's used like this:

\codesnippet{option1}{option2}{option3}

More fully:

\codesnippet{thisissomecode.codefile}{thisisagrepfilter}{thisisalinerangemarker}

The motivation behind this is basically because I'm trying to use lstinputlisting to find files recursively within a (Linux) directory structure, and I'm using find from Bash to get around the fact that it can't. At least, I don't believe it can.

The thing is that I suspect this is not the best way to do this, but it does work in the sense that it does what I want. I'm not a huge fan of the way that I use three separate options, though, and that's what I really want to avoid if possible, but I'm really struggling to make sense of how I'd use xkeyval to achieve this, which I think is my best bet. What I would really like, for example, is to be able to use codesnippet like this:

\codesnippet[filename=,grepfilter=,linerangemarker=}

... or even better, actually use linerange per the lstinputlisting option, but I suspect this probably isn't possible:

\codesnippet[filename=,grepfilter=,linerange=}

I think my trouble stems from the fact that I'm embedding a command (lstinputlisting) inside my "newcommand", and since I haven't had any luck experimenting with xkeyval I just want to ask if anyone could:

A. Help me get on the right track understanding how I would use xkeyval to come up with something like this:

\codesnippet[filename=,grepfilter=,linerangemarker=}

B. Help me understand whether the use of lstinputlisting may be complicating things, as it has its own options

C. (Bonus) Help me understand whether there is a better way of doing this altogether, and what else I may be doing wrong ... e.g. is there a way to get lstinputlisting to find files recursively without using find altogether?

Any help would as always be greatly appreciated.

closed as unclear what you're asking by TeXnician, Stefan Pinnow, user36296, egreg, user31729 Jan 5 '18 at 0:09

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • You can certainly do this using xkeyval. You'd need to declare some options (maybe justfilter and marker so the usage is \codesnippet[filter=foo,marker=bar]{filename}) and then based on the options given construct the \write18 command and decide what options to pass to \lstinputlisting. There's no problem with using \lstinputlisting as you're doing it that I can see. – TH. Jul 11 '17 at 11:01
  • 1
    tex can't so easily cat the files but it could input a file of name #1 from some arbitrarily deep directory without needing shell escape. Just as article.cls can be input from anywhere – David Carlisle Jul 11 '17 at 11:04

Browse other questions tagged or ask your own question.