-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathCreateNewFile.lua
79 lines (77 loc) · 2.47 KB
/
CreateNewFile.lua
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
-- CreateNewFile.lua (cross-platform)
-- 2024.11.16
--
-- Create new file using the template(s)
-- Params:
-- %"0%D
-- %dir-with-templates%
--
-- Button example:
--[[
DOUBLECMD#TOOLBAR#XMLDATA<?xml version="1.0" encoding="UTF-8"?>
<doublecmd>
<Command>
<ID>{78AE07D0-42A0-4238-92BB-BE802AFB6F48}</ID>
<Icon>cm_editnew</Icon>
<Hint>Create new file</Hint>
<Command>cm_ExecuteScript</Command>
<Param>%COMMANDER_PATH%/utils/CreateNewFile.lua</Param>
<Param>%"0%D</Param>
<Param>%COMMANDER_PATH%/utils/newfiles</Param>
</Command>
</doublecmd>
]]
local params = {...}
local items = {}
local hd, fd, rt, st, i, ba, tp, tf, bp, h, ct
local mode = 'wb'
local sn = 'new'
if #params ~= 2 then
Dialogs.MessageBox('Check the number of parameters!\nRequires only two.', 'Create new file', 0x0030)
return
end
-- Get list of new files
hd, fd = SysUtils.FindFirst(params[2] .. SysUtils.PathDelim .. '*')
if hd ~= nil then
repeat
if (fd.Name ~= '.') and (fd.Name ~= '..') then table.insert(items, fd.Name) end
rt, fd = SysUtils.FindNext(hd)
until rt == nil
SysUtils.FindClose(hd)
end
-- Get new file
if #items >= 1 then
table.sort(items)
st = Dialogs.InputListBox('Create new file', 'Select type:', items, 1)
if st == nil then return end
-- Get new filename
while i ~= 1 do
ba, sn = Dialogs.InputQuery('Type: ' .. st, 'Enter filename:', false, sn)
if ba == false then return end
if (SysUtils.PathDelim == "\\") and (string.len(params[1]) == 3) and (string.sub(params[1], 2, 3) == ":\\") then
tp = string.sub(params[1], 1, 2)
else
tp = params[1]
end
tf = tp .. SysUtils.PathDelim .. string.gsub(st, '(.+)(%.[^%.]+)$', sn .. '%2')
if SysUtils.FileExists(tf) == false then break end
bp = Dialogs.MessageBox('File "' .. tf .. '" already exists!\n\nOverwrite?', 'Create new file', 0x0003 + 0x0030 + 0x0100)
if bp == 0x0006 then
break
else
if bp ~= 0x0007 then return end
end
end
-- Read-copy
h = io.open(params[2] .. SysUtils.PathDelim .. st, 'rb')
ct = h:read('*a')
h:close()
-- Write-paste
if SysUtils.FileExists(tf) == true then mode = 'w+b' end
h = io.open(tf, mode)
h:write(ct)
h:close()
DC.ExecuteCommand('cm_Refresh')
DC.ExecuteCommand('cm_QuickSearch', 'search=on', 'direction=first', 'matchbeginning=on', 'matchending=off', 'casesensitive=on', 'files=on', 'directories=off', 'text=' .. string.gsub(st, '(.+)(%.[^%.]+)$', sn .. '%2'))
DC.ExecuteCommand('cm_QuickSearch', 'search=off')
end