aboutsummaryrefslogtreecommitdiff
path: root/plugins/gui_filepicker.lua
blob: 25e6908c28c6c001464e4c9e52a9c3862057cf0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
-- mod-version:3

-- ** LINUX ONLY **
-- KDE Dialog: Install kdialog package.
-- GTK Zenity: Install zenity package (default).

local core = require("core")
local command = require("core.command")
local common = require("core.common")
local keymap = require("core.keymap")
local config = require("core.config")
local TreeView = require("plugins.treeview")

config.plugins.gui_filepicker = common.merge({
	dialog_utility = "zenity",
	config_spec = {
		name = "GUI Filepicker",
		{
			label = "Dialog Utility",
			description = "The Default Filepicker Dialog Boxes Utility",
			path = "dialog_utility",
			type = "selection",
			default = "zenity",
			values = {
				{"Zenity", "zenity"},
				{"KDE Dialogs", "kdialog"}
			}
		}
	}
}, config.plugins.gui_filepicker)

local function dialog(kdialogFlags, zenityFlags, callback)
	local utility = config.plugins.gui_filepicker.dialog_utility
	local flags = zenityFlags -- The Default

	if utility == "kdialog" then
		flags = kdialogFlags
	end

	local proc, proc_err = process.start({
		utility,
		table.unpack(flags)
	})

	if not proc then
		core.error("Unable to run %s: %s", utility, proc_err)
		return
	end

	-- Run this in a coroutine, so we don't block the UI
	core.add_thread(function()
		local buffer = {}
		repeat
			local buf = proc:read_stdout()
			if buf and #buf > 0 then
				table.insert(buffer, buf)
			end
			coroutine.yield()
		until not buf
		local abs_path = table.concat(buffer) or ""

		abs_path = string.match(abs_path, "^[^\n]+") or ""
		if #abs_path == 0 then
			return
		end

		callback(abs_path)
	end)
end

command.add(nil, {
	["gui-filepicker:open-file"] = function()
		dialog(
			{
				"--getopenfilename"
			},
			{
				"--file-selection"
			},
			function(abs_path)
				core.root_view:open_doc(core.open_doc(common.home_expand(abs_path)))
			end
		)
	end,

	["gui-filepicker:open-project-folder"] = function()
		dialog(
			{
				"--getexistingdirectory"
			},
			{
					"--file-selection",
					"--directory"
			},
			function(abs_path)
				if abs_path == core.project_dir then
					return
				end
				os.execute(string.format("%q %q", EXEFILE, abs_path))
			end
		)
	end,

	["gui-filepicker:change-project-folder"] = function()
		dialog(
			{
				"--getexistingdirectory"
			},
			{
				"--file-selection",
				"--directory"
			},
			function(abs_path)
				if abs_path == core.project_dir then
					return
				end
				core.confirm_close_docs(core.docs, function(dirpath)
					core.open_folder_project(dirpath)
				end, abs_path)
			end
		)
	end,

	["gui-filepicker:add-directory"] = function()
		dialog(
			{
				"--getexistingdirectory",
			},
			{
				"--file-selection",
				"--directory"
			},
			function(abs_path)
				if abs_path == core.project_dir then
					return
				end
				core.add_project_directory(system.absolute_path(abs_path))
			end
		)
	end,
})

command.add("core.docview", {
	["gui-filepicker:save-as"] = function(dv)
		dialog(
			{
				"--getsavefilename"
			},
			{
				"--file-selection",
				"--save",
				"--filename",
				dv.doc.filename or "new_file"
			},
			function(abs_path)
				dv.doc:save(abs_path, abs_path)
				core.log('Saved as "%s"', dv.doc.filename)
			end
		)
	end,

	["gui-filepicker:save"] = function(dv)
		if dv.doc.filename then
			command.perform("doc:save")
		else
			command.perform("gui-filepicker:save-as")
		end
	end,
})

keymap.add({
	["ctrl+s"] = "gui-filepicker:save",
	["ctrl+shift+s"] = "gui-filepicker:save-as",
	["ctrl+shift+c"] = "gui-filepicker:change-project-folder",
	["ctrl+o"] = "gui-filepicker:open-file",
	["ctrl+shift+o"] = "gui-filepicker:open-project-folder",
})

local replacements = {
	["core:open-file"] = "gui-filepicker:open-file",
	["doc:save"] = "gui-filepicker:save",
}

for _, v in ipairs(TreeView.toolbar.toolbar_commands) do
	if replacements[v.command] then
		v.command = replacements[v.command]
	end
end