Skip to content

Instantly share code, notes, and snippets.

@rgieseke
Created June 15, 2010 16:26
Show Gist options
  • Save rgieseke/439330 to your computer and use it in GitHub Desktop.
Save rgieseke/439330 to your computer and use it in GitHub Desktop.
-- Experimental command line arguments processing
args = {}
local helptext = [[
Textadept - Version ...
Usage:
textadept [arguments] [file ..] - edit text files
Arguments:
-h or --help Print this help and exit
-n or --nosession Do not load session file
-u or --userhome <path> Set user home to <path>
]]
-- 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 args = lpeg.match(lpeg.Ct(param * (P(' ') * param)^0), arg[0])
for _, a in ipairs(args) do arg[#arg + 1] = a end
end
-- process command line arguments
if MAC and arg[1] and arg[1]:find('^%-psn_0') then table.remove(arg, 1) end
if #arg ~= 0 then
-- process command line switches
for i, switch in ipairs(arg) do
if switch == '-h' or switch == '--help' then
print(helptext)
args.help = true
break
-- quit()
elseif switch == '-u' or switch == '--userhome' then
_USERHOME = arg[i+1]
pass = true
elseif switch == '-n' or switch == '--nosession' then
args.NO_SESSION = true
elseif pass then
pass = nil
else
args.filenames = {}
for _, filename in ipairs(arg) do
-- TODO get full path
args.filenames[#args.filenames+1] = filename
end
end
end
end
--if args.help then quit() end
-- Copyright 2007-2010 Mitchell Foral mitchell<att>caladbolg.net. See LICENSE.
_RELEASE = "Textadept 2.2"
package.path = _HOME..'/core/?.lua;'..package.path
require 'args'
if not _USERHOME then
_USERHOME = os.getenv(not WIN32 and 'HOME' or 'USERPROFILE')..'/.textadept'
local lfs = require 'lfs'
if not lfs.attributes(_USERHOME) then lfs.mkdir(_USERHOME) end
end
_LEXERPATH = _USERHOME..'/lexers/?.lua;'.._HOME..'/lexers'
_THEME = 'light'
local f = io.open(_USERHOME..'/theme', 'rb')
if f then
local theme = f:read('*line'):match('[^\r\n]+')
f:close()
if theme and #theme > 0 then _THEME = theme end
end
if not _THEME:find('[/\\]') then
local theme = _THEME
_THEME = _HOME..'/themes/'..theme
if not lfs.attributes(_THEME) then _THEME = _USERHOME..'/themes/'..theme end
end
require 'iface'
require 'locale'
require 'events'
require 'file_io'
require 'gui'
if args.help then quit() end
rawset = nil -- do not allow modifications which could compromise stability
-- LuaDoc is in core/._G.lua.
function _G.user_dofile(filename)
if lfs.attributes(_USERHOME..'/'..filename) then
local ret, errmsg = pcall(dofile, _USERHOME..'/'..filename)
if not ret then gui.print(errmsg) end
return ret
end
return false
end
-- Copyright 2007-2010 Mitchell Foral mitchell<att>caladbolg.net. See LICENSE.
--if not RESETTING then
-- if args.help then quit() end
--end
local paths = {
_USERHOME..'/?.lua',
_USERHOME..'/modules/?.lua',
_USERHOME..'/modules/?/init.lua',
_HOME..'/modules/?.lua',
_HOME..'/modules/?/init.lua',
package.path
}
package.path = table.concat(paths, ';')
if not user_dofile('init.lua') then require 'textadept' end
if not RESETTING then
if not args.NO_SESSION then
_m.textadept.session.load()
else
_m.textadept.session.SAVE_ON_QUIT = false
end
if args.filenames then
for _, filename in ipairs(args.filenames) do io.open_file(filename) end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment