diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index 768a4643..965f3451 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -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 diff --git a/data/core/common.lua b/data/core/common.lua index e459476a..379ca795 100644 --- a/data/core/common.lua +++ b/data/core/common.lua @@ -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 diff --git a/data/core/doc/init.lua b/data/core/doc/init.lua index 3f1c39cd..b2416d3a 100644 --- a/data/core/doc/init.lua +++ b/data/core/doc/init.lua @@ -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 diff --git a/data/core/init.lua b/data/core/init.lua index 45490148..7f904fb7 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -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)