@@ -91,6 +91,9 @@ class SpLatex
9191 // ! Process % TABULAR commands
9292 void tabular (size_t ln, size_t indent, const std::string& cmdline);
9393
94+ // ! Process % DEFMACRO commands
95+ void defmacro (size_t ln, size_t indent, const std::string& cmdline);
96+
9497 // ! Process Textlines
9598 SpLatex (TextLines& lines);
9699};
@@ -460,6 +463,55 @@ void SpLatex::tabular(size_t ln, size_t indent, const std::string& cmdline)
460463 }
461464}
462465
466+ // ! Process % DEFMACRO commands
467+ void SpLatex::defmacro (size_t ln, size_t indent, const std::string& cmdline)
468+ {
469+ std::string query = cmdline;
470+
471+ // find REFORMAT, parse format and remove clause from query
472+ Reformat reformat;
473+ reformat.parse_query (query);
474+
475+ // execute query
476+ SqlQuery sql = g_db->query (query);
477+
478+ sql->read_complete ();
479+
480+ // prepare reformatting
481+ reformat.prepare (sql);
482+
483+ std::ostringstream oss;
484+ unsigned int count = sql->num_cols ();
485+ while (sql->step ())
486+ {
487+ for (unsigned int col = 0 ; col < count; ++col) {
488+ if (col != 0 ) oss << std::endl;
489+ oss << " \\ def \\ "
490+ << str_reduce (sql->col_name (col))
491+ << " {"
492+ << reformat.format (0 , col, sql->text (0 , col))
493+ << " }" ;
494+ }
495+ }
496+
497+ std::string output = oss.str ();
498+
499+ // check whether line contains an \addplot command
500+ static const boost::regex
501+ re_defmacro (" [[:blank:]]*(\\\\ def \\\\ [^}]+ \\ {)[^}]+(\\ }.*)" );
502+ boost::smatch rm;
503+
504+ if (ln + count < m_lines.size () &&
505+ boost::regex_match (m_lines[ln], rm, re_defmacro))
506+ {
507+ m_lines.replace (ln, ln+count, indent, output, " DEFMACRO" );
508+ }
509+ else
510+ {
511+ m_lines.replace (ln, ln, indent, output, " DEFMACRO" );
512+ }
513+ }
514+
463515// ! process line-based file in place
464516SpLatex::SpLatex (TextLines& lines)
465517 : m_lines(lines)
@@ -515,6 +567,11 @@ SpLatex::SpLatex(TextLines& lines)
515567 OUT (ln << " % " << cmd);
516568 tabular (ln, indent, cmd.substr (space_pos+1 ));
517569 }
570+ else if (first_word == " DEFMACRO" )
571+ {
572+ OUT (ln << " % " << cmd);
573+ defmacro (ln, indent, cmd.substr (space_pos+1 ));
574+ }
518575 else
519576 {
520577 if (first_word.size () >= 4 && first_word[0 ] != ' -' )
0 commit comments