Fix filename problem

Close #163
This commit is contained in:
Francesco Abbate 2021-05-01 19:18:39 +02:00
parent 857807b23a
commit 5766329313
4 changed files with 22 additions and 8 deletions

View File

@ -68,9 +68,10 @@ end
local function save(filename)
doc():save(filename)
core.on_doc_save(filename)
core.log("Saved \"%s\"", doc().filename)
doc():save(filename and core.normalize_to_project_dir(filename))
local saved_filename = doc().filename
core.on_doc_save(saved_filename)
core.log("Saved \"%s\"", saved_filename)
end

View File

@ -247,6 +247,11 @@ local function split_on_slash(s, sep_pattern)
end
function common.path_belongs_to(filename, path)
return string.find(filename, path .. PATHSEP, 1, true) == 1
end
function common.relative_path(ref_dir, dir)
local ref_ls = split_on_slash(ref_dir)
local dir_ls = split_on_slash(dir)
@ -259,9 +264,9 @@ function common.relative_path(ref_dir, dir)
end
local ups = ""
for k = i, #ref_ls do
ups = ups .. "../"
ups = ups .. ".." .. PATHSEP
end
local rel_path = ups .. table.concat(dir_ls, "/", i)
local rel_path = ups .. table.concat(dir_ls, PATHSEP, i)
return rel_path ~= "" and rel_path or "."
end

View File

@ -66,7 +66,6 @@ end
function Doc:set_filename(filename)
filename = common.normalize_path(filename)
self.filename = filename
self.abs_filename = common.home_encode(system.absolute_path(filename))
end

View File

@ -732,18 +732,27 @@ function core.pop_clip_rect()
end
function core.normalize_to_project_dir(filename)
filename = common.normalize_path(filename)
if common.path_belongs_to(filename, core.project_dir) then
filename = common.relative_path(core.project_dir, filename)
end
return filename
end
function core.open_doc(filename)
if filename then
-- try to find existing doc for filename
local abs_filename = system.absolute_path(filename)
for _, doc in ipairs(core.docs) do
if doc.filename
and abs_filename == system.absolute_path(doc.filename) then
if doc.abs_filename and abs_filename == doc.abs_filename then
return doc
end
end
end
-- no existing doc for filename; create new
filename = core.normalize_to_project_dir(filename)
local doc = Doc(filename)
table.insert(core.docs, doc)
core.log_quiet(filename and "Opened doc \"%s\"" or "Opened new doc", filename)