diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/args.lua | 99 | ||||
-rw-r--r-- | core/events.lua | 2 | ||||
-rw-r--r-- | core/init.lua | 22 |
3 files changed, 106 insertions, 17 deletions
diff --git a/core/args.lua b/core/args.lua new file mode 100644 index 00000000..28ecfd4a --- /dev/null +++ b/core/args.lua @@ -0,0 +1,99 @@ +-- Copyright 2007-2010 Mitchell mitchell<att>caladbolg.net. See LICENSE. + +--- Processes command line arguments for Textadept. +module('args', package.seeall) + +--- +-- [Local table] Contains registered command line switches. +-- @type table +-- @name switches +local switches = {} + +--- +-- Registers a command line switch. +-- @param switch1 String switch (short version). +-- @param switch2 String switch (long version). +-- @param narg The number of expected parameters for the switch. +-- @param f The Lua function to run when the switch is tripped. +-- @param description Description of the switch. +function register(switch1, switch2, narg, f, description) + local t = { f, narg, description } + switches[switch1] = t + switches[switch2] = t +end + +--- +-- Processes command line arguments. +-- Add command line switches with args.register(). Any unrecognized arguments +-- are treated as filepaths and opened. +-- Generates an 'arg_none' event when no args are present. +-- @see register +function process() + if #arg == 0 then + events.emit('arg_none') + return + end + local i = 1 + while i <= #arg do + local switch = switches[arg[i]] + if switch then + local f, n = unpack(switch) + local args = {} + for j = i + 1, i + n do args[#args + 1] = arg[j] end + f(unpack(args)) + i = i + n + else + io.open_file(arg[i]) + end + i = i + 1 + end +end + +local function show_help() + local line = "%s [%d args] -- %s" + local help = {} + for key, switch in pairs(switches) do + help[#help + 1] = line:format(key, unpack(switch, 2)) + end + table.sort(help) + gui.dialog('textbox', + '--informative-text', 'Command line parameters', + '--text', table.concat(help, '\n'), + '--button1', 'gtk-ok') + events.emit('arg_none') +end +register('-h', '--help', 0, show_help, 'Displays this') + +-- For Windows, create arg table from single command line string (arg[0]). +if WIN32 and #arg[0] > 0 then + local lpeg = require 'lpeg' + local P, C = lpeg.P, lpeg.C + local param = P('"') * C((1 - P('"'))^0) * '"' + C((1 - P(' '))^1) + local params = lpeg.match(lpeg.Ct(param * (P(' ')^1 * param)^0), arg[0]) + for i = 1, #params do arg[#arg + 1] = params[i] end +end + +-- For Mac, remove junk parameter if necessary. +if MAC and arg[1] and arg[1]:find('^%-psn_0') then table.remove(arg, 1) end + +-- Set _G._USERHOME. +local userhome = os.getenv(not WIN32 and 'HOME' or 'USERPROFILE')..'/.textadept' +for i = 1, #arg do + if (arg[i] == '-u' or arg[i] == '--userhome') and arg[i + 1] then + userhome = table.remove(arg, i + 1) + table.remove(arg, i) + break + end +end +local lfs = require 'lfs' +if not lfs.attributes(userhome) then lfs.mkdir(userhome) end +if not lfs.attributes(userhome..'/init.lua') then + local f = io.open(userhome..'/init.lua', 'w') + if f then + f:write("require 'textadept'\n") + f:close() + end +end +_G._USERHOME = userhome + +register('-u', '--userhome', 1, function() end, 'Sets alternate _USERHOME') diff --git a/core/events.lua b/core/events.lua index 15c6f77c..eb0e5d0c 100644 --- a/core/events.lua +++ b/core/events.lua @@ -189,7 +189,7 @@ function emit(event, ...) if not handlers then return end for _, f in ipairs(handlers) do local result = f(unpack{...}) - if result == true or result == false then return result end + if type(result) == 'boolean' then return result end end end diff --git a/core/init.lua b/core/init.lua index dda18806..8eb06758 100644 --- a/core/init.lua +++ b/core/init.lua @@ -4,16 +4,12 @@ _RELEASE = "Textadept 2.2" package.path = _HOME..'/core/?.lua;'..package.path -_USERHOME = os.getenv(not WIN32 and 'HOME' or 'USERPROFILE')..'/.textadept' -local lfs = require 'lfs' -if not lfs.attributes(_USERHOME) then lfs.mkdir(_USERHOME) end -if not lfs.attributes(_USERHOME..'/init.lua') then - local f = io.open(_USERHOME..'/init.lua', 'w') - if f then - f:write("require 'textadept'\n") - f:close() - end -end +require 'iface' +require 'args' +require 'locale' +require 'events' +require 'file_io' +require 'gui' _LEXERPATH = _USERHOME..'/lexers/?.lua;'.._HOME..'/lexers' @@ -30,12 +26,6 @@ if not _THEME:find('[/\\]') then if not lfs.attributes(_THEME) then _THEME = _HOME..'/themes/'..theme end end -require 'iface' -require 'locale' -require 'events' -require 'file_io' -require 'gui' - -- LuaDoc is in core/._G.lua. function _G.user_dofile(filename) if lfs.attributes(_USERHOME..'/'..filename) then |