% ----------------------------------------------------------------------------
% the EXSHEETS-LISTINGS package
% 
%   Using listings with exsheets
% 
% ----------------------------------------------------------------------------
% Clemens Niederberger
% Web:    https://bitbucket.org/cgnieder/exsheets/
% E-Mail: contact@mychemistry.eu
% ----------------------------------------------------------------------------
% Copyright 2011-2015 Clemens Niederberger
% 
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
% 
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Clemens Niederberger.
% ----------------------------------------------------------------------------
% If you have any ideas, questions, suggestions or bugs to report, please
% feel free to contact me.
% ----------------------------------------------------------------------------
\RequirePackage{exsheets,listings}
% we need this loaded before expl3 syntax is activated:
\lst@RequireAspects{writefile}
\ProvidesExplPackage{exsheets-listings}
  {\exsheets@date}
  {\exsheets@version}
  {listings in exsheets}

% ----------------------------------------------------------------------------
% variables:
\int_new:N \g__exsheets_listings_question_int
\int_new:N \g__exsheets_listings_solution_int

\tl_new:N  \l__exsheets_listings_question_pre_tl
\tl_new:N  \l__exsheets_listings_question_post_tl
\tl_new:N  \l__exsheets_listings_question_options_tl
\tl_new:N  \l__exsheets_listings_question_code_tl
\tl_new:N  \l__exsheets_listings_solution_pre_tl
\tl_new:N  \l__exsheets_listings_solution_post_tl
\tl_new:N  \l__exsheets_listings_solution_options_tl
\tl_new:N  \l__exsheets_listings_solution_code_tl
\tl_new:N  \l__exsheets_listings_question_points_tl

% ----------------------------------------------------------------------------
% listings style for questions and solutions:
\keys_define:nn {exsheets}
  {
    question / listings .tl_set:N = \l__exsheets_listings_question_code_tl ,
    solution / listings .tl_set:N = \l__exsheets_listings_solution_code_tl
  }

% ----------------------------------------------------------------------------
% expansion help:
\cs_new:Npn \__exsheets_question_expandopt:Nnnnn #1#2#3#4#5
  { #1{#2}[#3]{#4}#5 }
\cs_generate_variant:Nn \__exsheets_question_expandopt:Nnnnn { NnVox }

\cs_new:Npn \__exsheets_question_expand_opt_and_body:Nnnn #1#2#3#4
  { #1{#2}[#3]#4 }
\cs_generate_variant:Nn \__exsheets_question_expand_opt_and_body:Nnnn { NnVx }

% ----------------------------------------------------------------------------
% lstquestion environment:
\keys_define:nn { exsheets / listings / question }
  {
    pre      .tl_set:N = \l__exsheets_listings_question_pre_tl ,
    post     .tl_set:N = \l__exsheets_listings_question_post_tl ,
    options  .tl_set:N = \l__exsheets_listings_question_options_tl ,
    points   .tl_set:N = \l__exsheets_listings_question_points_tl ,
    listings .tl_set:N = \l__exsheets_listings_question_code_tl ,
  }

\keys_define:nn { exsheets / listings / solution }
  {
    pre      .tl_set:N = \l__exsheets_listings_solution_pre_tl ,
    post     .tl_set:N = \l__exsheets_listings_solution_post_tl ,
    options  .tl_set:N = \l__exsheets_listings_solution_options_tl ,
    listings .tl_set:N = \l__exsheets_listings_solution_code_tl ,
  }

\cs_new:Npn \exsheets_listings_new_lstenvironment:nnnnnnn #1#2#3#4#5#6#7
  {
    \lstnewenvironment{#2}[1][]
      {
        \keys_set:nn {exsheets/listings/question} {#1,#4,##1}
        \int_gincr:N \g__exsheets_listings_question_int
        \lst@BeginWriteFile
          { \c_job_name_tl - ex \int_use:N \g__exsheets_listings_question_int . lst }
      }
      {
        \lst@EndWriteFile
        \__exsheets_question_expandopt:NnVox
          \begin {#3}
          \l__exsheets_listings_question_options_tl
          { \l__exsheets_listings_question_points_tl }
          {
            \exp_not:V \l__exsheets_listings_question_pre_tl
            \exp_not:N \lstinputlisting
              [ \exp_not:V \l__exsheets_listings_question_code_tl ]
              { \c_job_name_tl - ex \int_use:N \g__exsheets_listings_question_int . lst }
            \exp_not:V \l__exsheets_listings_question_post_tl
          }
        \end {#3}
      }
    \lstnewenvironment{#5}[1][]
      {
        \keys_set:nn { exsheets / listings / solution } { #1,#7,##1 }
        \int_gincr:N \g__exsheets_listings_solution_int
        \lst@BeginWriteFile
          { \c_job_name_tl - sol \int_use:N \g__exsheets_listings_solution_int . lst }
      }
      {
        \lst@EndWriteFile
        \__exsheets_question_expand_opt_and_body:NnVx
          \begin {#6}
          \l__exsheets_listings_solution_options_tl
          {
            \exp_not:V \l__exsheets_listings_solution_pre_tl
            \exp_not:N \lstinputlisting
              [ \exp_not:V \l__exsheets_listings_solution_code_tl ]
              { \c_job_name_tl - sol \int_use:N \g__exsheets_listings_solution_int . lst }
            \exp_not:V \l__exsheets_listings_solution_post_tl
          }
        \end {#6}
      }
  }

\NewDocumentCommand \NewLstQuSolPair { O{}mmO{}mmO{} }
  { \exsheets_listings_new_lstenvironment:nnnnnnn {#1}{#2}{#3}{#4}{#5}{#6}{#7} }

\NewLstQuSolPair{lstquestion}{question}{lstsolution}{solution}

\tex_endinput:D

HISTORY
2013/09/18 v0.1 - first draft, see http://tex.stackexchange.com/a/133969/5049
2013/10/11 v0.2 - first official version bundled with ExSheets
