diff options
author | 2014-04-06 19:42:42 -0400 | |
---|---|---|
committer | 2014-04-06 19:42:42 -0400 | |
commit | a46502a160ca4a2a9094c9d9ae2b2d49b71a471b (patch) | |
tree | e9bb8efb5d86632cf297e77456bc738f5136e2a1 | |
parent | eff5df2952e26b5a1a25e95befe485068df10c81 (diff) | |
download | textadept-a46502a160ca4a2a9094c9d9ae2b2d49b71a471b.tar.gz textadept-a46502a160ca4a2a9094c9d9ae2b2d49b71a471b.zip |
Added `lfs.abspath()` in order to always use absolute paths.
Thanks to Pedro Andres Aranda Gutierrez.
-rw-r--r-- | core/args.lua | 7 | ||||
-rw-r--r-- | core/file_io.lua | 7 | ||||
-rw-r--r-- | core/lfs_ext.lua | 20 |
3 files changed, 21 insertions, 13 deletions
diff --git a/core/args.lua b/core/args.lua index 108820eb..6fd18841 100644 --- a/core/args.lua +++ b/core/args.lua @@ -57,12 +57,7 @@ function M.process(arg) f(table.unpack(args)) i = i + n else - if not arg[i]:find(not WIN32 and '^/' or '^%a:[/\\]') then - -- Convert relative path to absolute path. - local cwd = arg[-1] or lfs.currentdir() - arg[i] = cwd..(not WIN32 and '/' or '\\')..arg[i] - end - io.open_file(arg[i]) + io.open_file(lfs.abspath(arg[i])) no_args = false end i = i + 1 diff --git a/core/file_io.lua b/core/file_io.lua index 3df966c5..264de408 100644 --- a/core/file_io.lua +++ b/core/file_io.lua @@ -114,12 +114,7 @@ function io.open_file(filenames) } if not filenames then return end for i = 1, #filenames do - local filename = filenames[i]:gsub('^file://', '') - if WIN32 then filename = filename:gsub('/', '\\') end - filename = filename:gsub('%f[^/\\]%.[/\\]', '') -- clean up './' - while filename:find('[^/\\]+[/\\]%.%.[/\\]') do - filename = filename:gsub('[^/\\]+[/\\]%.%.[/\\]', '') -- clean up '../' - end + local filename = lfs.abspath(filenames[i]:gsub('^file://', '')) for i, buffer in ipairs(_BUFFERS) do if filename == buffer.filename then view:goto_buffer(i) goto continue end end diff --git a/core/lfs_ext.lua b/core/lfs_ext.lua index b4ebe588..7fe04b89 100644 --- a/core/lfs_ext.lua +++ b/core/lfs_ext.lua @@ -2,7 +2,8 @@ --[[ This comment is for LuaDoc. --- --- Extends the `lfs` library to find files in directories. +-- Extends the `lfs` library to find files in directories and determine absolute +-- file paths. module('lfs')]] --- @@ -92,3 +93,20 @@ function lfs.dir_foreach(dir, f, filter, exclude_FILTER, recursing) end end end + +--- +-- Returns the absolute path to string *filename*. +-- `lfs.currentdir()` is prepended to a relative filename. The returned path is +-- not guaranteed to exist. +-- @param filename The relative or absolute path to a file. +-- @return string absolute path +function lfs.abspath(filename) + if filename:find(not WIN32 and '^/' or '^%a:[/\\]') then return filename end + if WIN32 then filename = filename:gsub('/', '\\') end + filename = lfs.currentdir()..(not WIN32 and '/' or '\\')..filename + filename = filename:gsub('%f[^/\\]%.[/\\]', '') -- clean up './' + while filename:find('[^/\\]+[/\\]%.%.[/\\]') do + filename = filename:gsub('[^/\\]+[/\\]%.%.[/\\]', '') -- clean up '../' + end + return filename +end |