aboutsummaryrefslogtreecommitdiff
path: root/data/core/commands
diff options
context:
space:
mode:
authorCukmekerb <cukmekerb@gmail.com>2021-06-17 18:17:01 -0700
committerGitHub <noreply@github.com>2021-06-17 18:17:01 -0700
commiteed5b790303023b64e4fb29021aedda4d068af9e (patch)
tree578949bcc4fe89027847b368248e1fd83558de2c /data/core/commands
parent27aa1621488b5301d80c02f58a0884843cc4db97 (diff)
parente9e1214e5959303cd87aa61e127fac6ebd1bc270 (diff)
downloadlite-xl-eed5b790303023b64e4fb29021aedda4d068af9e.tar.gz
lite-xl-eed5b790303023b64e4fb29021aedda4d068af9e.zip
Merge branch 'lite-xl:master' into master
Diffstat (limited to 'data/core/commands')
-rw-r--r--data/core/commands/command.lua8
-rw-r--r--data/core/commands/core.lua15
-rw-r--r--data/core/commands/dialog.lua35
-rw-r--r--data/core/commands/doc.lua85
-rw-r--r--data/core/commands/files.lua5
-rw-r--r--data/core/commands/findreplace.lua14
6 files changed, 100 insertions, 62 deletions
diff --git a/data/core/commands/command.lua b/data/core/commands/command.lua
index f0b80077..1a635a86 100644
--- a/data/core/commands/command.lua
+++ b/data/core/commands/command.lua
@@ -1,13 +1,7 @@
local core = require "core"
local command = require "core.command"
-local CommandView = require "core.commandview"
-local function has_commandview()
- return core.active_view:is(CommandView)
-end
-
-
-command.add(has_commandview, {
+command.add("core.commandview", {
["command:submit"] = function()
core.active_view:submit()
end,
diff --git a/data/core/commands/core.lua b/data/core/commands/core.lua
index c8233062..859fb066 100644
--- a/data/core/commands/core.lua
+++ b/data/core/commands/core.lua
@@ -66,6 +66,9 @@ command.add(nil, {
end,
["core:find-file"] = function()
+ if core.project_files_limit then
+ return command.perform "core:open-file"
+ end
local files = {}
for dir, item in core.get_project_files() do
if item.type == "file" then
@@ -88,17 +91,23 @@ command.add(nil, {
["core:open-file"] = function()
local view = core.active_view
if view.doc and view.doc.abs_filename then
- core.command_view:set_text(common.home_encode(view.doc.abs_filename))
+ local dirname, filename = view.doc.abs_filename:match("(.*)[/\\](.+)$")
+ if dirname then
+ dirname = core.normalize_to_project_dir(dirname)
+ local text = dirname == core.project_dir and "" or common.home_encode(dirname) .. PATHSEP
+ core.command_view:set_text(text)
+ end
end
core.command_view:enter("Open File", function(text)
- core.root_view:open_doc(core.open_doc(common.home_expand(text)))
+ local filename = system.absolute_path(common.home_expand(text))
+ core.root_view:open_doc(core.open_doc(filename))
end, function (text)
return common.home_encode_list(common.path_suggest(common.home_expand(text)))
end, nil, function(text)
local path_stat, err = system.get_file_info(common.home_expand(text))
if err then
core.error("Cannot open file %q: %q", text, err)
- elseif path_stat.type == 'dir' then
+ elseif path_stat.type == 'dir' then
core.error("Cannot open %q, is a folder", text)
else
return true
diff --git a/data/core/commands/dialog.lua b/data/core/commands/dialog.lua
new file mode 100644
index 00000000..90606abb
--- /dev/null
+++ b/data/core/commands/dialog.lua
@@ -0,0 +1,35 @@
+local core = require "core"
+local command = require "core.command"
+local common = require "core.common"
+
+command.add("core.nagview", {
+ ["dialog:previous-entry"] = function()
+ local v = core.active_view
+ local hover = v.hovered_item or 1
+ v:change_hovered(hover == 1 and #v.options or hover - 1)
+ end,
+ ["dialog:next-entry"] = function()
+ local v = core.active_view
+ local hover = v.hovered_item or 1
+ v:change_hovered(hover == #v.options and 1 or hover + 1)
+ end,
+ ["dialog:select-yes"] = function()
+ local v = core.active_view
+ if v ~= core.nag_view then return end
+ v:change_hovered(common.find_index(v.options, "default_yes"))
+ command.perform "dialog:select"
+ end,
+ ["dialog:select-no"] = function()
+ local v = core.active_view
+ if v ~= core.nag_view then return end
+ v:change_hovered(common.find_index(v.options, "default_no"))
+ command.perform "dialog:select"
+ end,
+ ["dialog:select"] = function()
+ local v = core.active_view
+ if v.hovered_item then
+ v.on_selected(v.options[v.hovered_item])
+ v:next()
+ end
+ end
+})
diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua
index 965f3451..c8c27509 100644
--- a/data/core/commands/doc.lua
+++ b/data/core/commands/doc.lua
@@ -33,33 +33,6 @@ local function doc_multiline_selection(sort)
return line1, col1, line2, col2, swap
end
-
-local function insert_at_start_of_selected_lines(text, skip_empty)
- local line1, col1, line2, col2, swap = doc_multiline_selection(true)
- for line = line1, line2 do
- local line_text = doc().lines[line]
- if (not skip_empty or line_text:find("%S")) then
- doc():insert(line, 1, text)
- end
- end
- doc():set_selection(line1, col1 + #text, line2, col2 + #text, swap)
-end
-
-
-local function remove_from_start_of_selected_lines(text, skip_empty)
- local line1, col1, line2, col2, swap = doc_multiline_selection(true)
- for line = line1, line2 do
- local line_text = doc().lines[line]
- if line_text:sub(1, #text) == text
- and (not skip_empty or line_text:find("%S"))
- then
- doc():remove(line, 1, line, #text + 1)
- end
- end
- doc():set_selection(line1, col1 - #text, line2, col2 - #text, swap)
-end
-
-
local function append_line_if_last_line(line)
if line >= #doc().lines then
doc():insert(line, math.huge, "\n")
@@ -74,7 +47,6 @@ local function save(filename)
core.log("Saved \"%s\"", saved_filename)
end
-
local commands = {
["doc:undo"] = function()
doc():undo()
@@ -183,17 +155,11 @@ local commands = {
end,
["doc:indent"] = function()
- local text = get_indent_string()
- if doc():has_selection() then
- insert_at_start_of_selected_lines(text)
- else
- doc():text_input(text)
- end
+ doc():indent_text(false, doc_multiline_selection(true))
end,
["doc:unindent"] = function()
- local text = get_indent_string()
- remove_from_start_of_selected_lines(text)
+ doc():indent_text(true, doc_multiline_selection(true))
end,
["doc:duplicate-lines"] = function()
@@ -237,19 +203,31 @@ local commands = {
["doc:toggle-line-comments"] = function()
local comment = doc().syntax.comment
if not comment then return end
+ local indentation = get_indent_string()
local comment_text = comment .. " "
- local line1, _, line2 = doc():get_selection(true)
+ local line1, _, line2 = doc_multiline_selection(true)
local uncomment = true
+ local start_offset = math.huge
for line = line1, line2 do
local text = doc().lines[line]
- if text:find("%S") and text:find(comment_text, 1, true) ~= 1 then
+ local s = text:find("%S")
+ local cs, ce = text:find(comment_text, s, true)
+ if s and cs ~= s then
uncomment = false
+ start_offset = math.min(start_offset, s)
end
end
- if uncomment then
- remove_from_start_of_selected_lines(comment_text, true)
- else
- insert_at_start_of_selected_lines(comment_text, true)
+ for line = line1, line2 do
+ local text = doc().lines[line]
+ local s = text:find("%S")
+ if uncomment then
+ local cs, ce = text:find(comment_text, s, true)
+ if ce then
+ doc():remove(line, cs, line, ce + 1)
+ end
+ elseif s then
+ doc():insert(line, start_offset, comment_text)
+ end
end
end,
@@ -260,7 +238,7 @@ local commands = {
["doc:lower-case"] = function()
doc():replace(string.lower)
end,
-
+
["doc:go-to-line"] = function()
local dv = dv()
@@ -297,8 +275,12 @@ local commands = {
end,
["doc:save-as"] = function()
+ local last_doc = core.last_active_view and core.last_active_view.doc
if doc().filename then
core.command_view:set_text(doc().filename)
+ elseif last_doc and last_doc.filename then
+ local dirname, filename = core.last_active_view.doc.abs_filename:match("(.*)[/\\](.+)$")
+ core.command_view:set_text(core.normalize_to_project_dir(dirname) .. PATHSEP)
end
core.command_view:enter("Save As", function(filename)
save(common.home_expand(filename))
@@ -315,7 +297,7 @@ local commands = {
end
end,
- ["doc:rename"] = function()
+ ["file:rename"] = function()
local old_filename = doc().filename
if not old_filename then
core.error("Cannot rename unsaved doc")
@@ -330,6 +312,21 @@ local commands = {
end
end, common.path_suggest)
end,
+
+
+ ["file:delete"] = function()
+ local filename = doc().abs_filename
+ if not filename then
+ core.error("Cannot remove unsaved doc")
+ return
+ end
+ for i,docview in ipairs(core.get_views_referencing_doc(doc())) do
+ local node = core.root_view.root_node:get_node_for_view(docview)
+ node:close_view(core.root_view, docview)
+ end
+ os.remove(filename)
+ core.log("Removed \"%s\"", filename)
+ end
}
diff --git a/data/core/commands/files.lua b/data/core/commands/files.lua
index a0106f47..b2fdb336 100644
--- a/data/core/commands/files.lua
+++ b/data/core/commands/files.lua
@@ -1,12 +1,13 @@
local core = require "core"
local command = require "core.command"
+local common = require "core.common"
command.add(nil, {
["files:create-directory"] = function()
core.command_view:enter("New directory name", function(text)
- local success, err = system.mkdir(text)
+ local success, err, path = common.mkdirp(text)
if not success then
- core.error("cannot create directory %q: %s", text, err)
+ core.error("cannot create directory %q: %s", path, err)
end
end)
end,
diff --git a/data/core/commands/findreplace.lua b/data/core/commands/findreplace.lua
index 937c410a..af60f33f 100644
--- a/data/core/commands/findreplace.lua
+++ b/data/core/commands/findreplace.lua
@@ -90,6 +90,7 @@ local function has_selection()
and core.active_view.doc:has_selection()
end
+
command.add(has_selection, {
["find-replace:select-next"] = function()
local l1, c1, l2, c2 = doc():get_selection(true)
@@ -107,9 +108,9 @@ command.add("core.docview", {
end)
end,
- ["find-replace:find-pattern"] = function()
- find("Find Text Pattern", function(doc, line, col, text)
- local opt = { wrap = true, no_case = true, pattern = true }
+ ["find-replace:find-regex"] = function()
+ find("Find Text Regex", function(doc, line, col, text)
+ local opt = { wrap = true, no_case = true, regex = true }
return search.find(doc, line, col, text, opt)
end)
end,
@@ -144,9 +145,10 @@ command.add("core.docview", {
end)
end,
- ["find-replace:replace-pattern"] = function()
- replace("Pattern", "", function(text, old, new)
- return text:gsub(old, new)
+ ["find-replace:replace-regex"] = function()
+ replace("Regex", "", function(text, old, new)
+ local re = regex.compile(old)
+ return regex.gsub(re, text, new)
end)
end,