From 7487f44fc8cb801cbf6d283833e7eed7f115dfdf Mon Sep 17 00:00:00 2001 From: lethosor Date: Tue, 23 Aug 2016 21:47:41 -0400 Subject: [PATCH] Implement a helper to create lua environments with shortcuts (e.g. scr, unit) Used in gui/gm-editor and lua Closes #977 --- library/lua/utils.lua | 42 ++++++++++++++++++++++++++++++++++++------ scripts | 2 +- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/library/lua/utils.lua b/library/lua/utils.lua index a677af3bf..24c24c074 100644 --- a/library/lua/utils.lua +++ b/library/lua/utils.lua @@ -611,15 +611,45 @@ function processArgs(args, validArgs) end function fillTable(table1,table2) - for k,v in pairs(table2) do - table1[k] = v - end + for k,v in pairs(table2) do + table1[k] = v + end end function unfillTable(table1,table2) - for k,v in pairs(table2) do - table1[k] = nil - end + for k,v in pairs(table2) do + table1[k] = nil + end +end + +function df_shortcut_var(k) + if k == 'scr' or k == 'screen' then + return dfhack.gui.getCurViewscreen() + elseif k == 'bld' or k == 'building' then + return dfhack.gui.getSelectedBuilding() + elseif k == 'item' then + return dfhack.gui.getSelectedItem() + elseif k == 'job' then + return dfhack.gui.getSelectedJob() + elseif k == 'wsjob' or k == 'workshop_job' then + return dfhack.gui.getSelectedWorkshopJob() + elseif k == 'unit' then + return dfhack.gui.getSelectedUnit() + else + for g in pairs(df.global) do + if g == k then + return df.global[k] + end + end + + return _G[k] + end +end + +function df_shortcut_env() + local env = {} + setmetatable(env, {__index = function(self, k) return df_shortcut_var(k) end}) + return env end return _ENV diff --git a/scripts b/scripts index 042fcffd0..3e3fb7bff 160000 --- a/scripts +++ b/scripts @@ -1 +1 @@ -Subproject commit 042fcffd051c1f1ceab268e54bed1ed197b1f91e +Subproject commit 3e3fb7bff6dce41e7ea85ee3addf44291b53f800