From 1993c5fb1b9cba759679403434f534849cd371d3 Mon Sep 17 00:00:00 2001 From: expwnent Date: Mon, 26 Jan 2015 17:18:30 -0500 Subject: [PATCH] Added support for multiline commands inside dfhack.init and the script command. --- NEWS | 8 ++++++++ Readme.rst | 3 +++ dfhack.init-example | 18 +++++++++++++++++- library/Core.cpp | 36 +++++++++++++++++++++++++----------- 4 files changed, 53 insertions(+), 12 deletions(-) diff --git a/NEWS b/NEWS index 883e9e6b8..11bf20509 100644 --- a/NEWS +++ b/NEWS @@ -1,4 +1,12 @@ DFHack Future + Internals + Fixes + New Plugins + New Scripts + New Tweaks + Removed + Misc Improvements + Multiline commands are now possible inside dfhack.init scripts. See dfhack.init-example for example usage. DFHack 0.40.24-r1 Internals diff --git a/Readme.rst b/Readme.rst index 624ceacb8..3965e2282 100644 --- a/Readme.rst +++ b/Readme.rst @@ -242,6 +242,9 @@ To include a double quote character, use ``\"`` inside double quotes. If the first non-whitespace character of a line is ``#``, the line is treated as a comment, i.e. a silent no-op command. +When reading commands from dfhack.init or with the ``script`` command, if the final character on a line is a backslash then the next uncommented line is considered a continuation of that line, with the backslash converted to a space character. +Commented lines are skipped, so it is possible to comment out parts of a command with the ``#`` character. + If the first non-whitespace character is ``:``, the command is parsed in a special alternative mode: first, non-whitespace characters immediately following the ``:`` are used as the command name; the remaining part of the line, starting with the first diff --git a/dfhack.init-example b/dfhack.init-example index 08e9dbe6c..caaa9dd75 100644 --- a/dfhack.init-example +++ b/dfhack.init-example @@ -195,7 +195,23 @@ enable search enable automaterial # Other interface improvement tools -enable dwarfmonitor mousequery automelt autotrade buildingplan resume trackstop zone stocks autochop stockpiles +# enable dwarfmonitor mousequery automelt autotrade buildingplan resume trackstop zone stocks autochop stockpiles +enable \ + dwarfmonitor \ + mousequery \ + automelt \ + autotrade \ + buildingplan \ + resume \ + trackstop \ + zone \ + stocks \ + autochop \ + stockpiles +#end a line with a backslash to make it continue to the next line. The \ is converted to a space for the final command. +# Multiline commands are ONLY supported for scripts like dfhack.init. You cannot do multiline command manually on the DFHack console. +# You cannot extend a commented line. +# You can comment out the extension of a line. # allow the fortress bookkeeper to queue jobs through the manager stockflow enable diff --git a/library/Core.cpp b/library/Core.cpp index e19878b2e..f2a86444f 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -824,22 +824,36 @@ bool Core::loadScriptFile(color_ostream &out, string fname, bool silent) if(!silent) out << "Loading script at " << fname << std::endl; ifstream script(fname.c_str()); - if (script.good()) - { - string command; - while (getline(script, command)) - { - if (!command.empty()) - runCommand(out, command); - } - return true; - } - else + if ( !script.good() ) { if(!silent) out.printerr("Error loading script\n"); return false; } + string command; + while(script.good()) { + string temp; + getline(script,temp); + bool doMore = false; + if ( temp.length() > 0 ) { + if ( temp[0] == '#' ) + continue; + if ( temp[temp.length()-1] == '\r' ) + temp = temp.substr(0,temp.length()-1); + if ( temp.length() > 0 ) { + if ( temp[temp.length()-1] == '\\' ) { + temp[temp.length()-1] = ' '; + doMore = true; + } + } + } + command = command + temp; + if ( (!doMore || !script.good()) && !command.empty() ) { + runCommand(out, command); + command = ""; + } + } + return true; } static void run_dfhack_init(color_ostream &out, Core *core)