Well, probably, my fourth name is the "GeanyLua's user"... Maybe something like

3.) See 7:

```lua
-- (cross-platform)
-- 2025.08.05
--[[
https://github.com/geany/geany/discussions/4382
]]

local sExt = ".py"

local sTmp = geany.dirsep .. "templates" .. geany.dirsep .. "files"
local sUser = geany.appinfo().configdir
local sSys = geany.appinfo().datadir

local function GetTemplate(sP, sE)
  local aS, sF, sR, sT 
  local iT = string.len(sE)
  for f in geany.dirlist(sP) do
    sF = sP .. geany.dirsep .. f
    aS = geany.stat(sF)
    if aS.type ~= "d" then
      sT = string.sub(sF, iT * -1, -1)
      if string.lower(sT) == sE then
        sR = sF
        break
      end
    end
  end
  return sR
end

local function ReadFile(sF)
  local h = io.open(sF, "rb")
  if h == nil then return nil end
  local s = h:read("*a")
  h:close()
  return s
end

local function GetType(sD, sE)
  local sR
  local sT = "*" .. sE .. ";"
  local h = io.open(sD .. geany.dirsep .. "filetype_extensions.conf", "r")
  if h == nil then return nil end
  for l in h:lines() do
    if string.find(l, sT, 1, true) ~= nil then
      sR = string.match(l, "^[^=]+")
      break
    end
  end
  h:close()
  return sR
end

local sFile = GetTemplate(sUser .. sTmp, sExt)
if sFile == nil then
  sFile = GetTemplate(sSys .. sTmp, sExt)
  if sFile == nil then
    geany.message("Not found.")
    return
  end
  local sTemplate = ReadFile(sFile)
  if sTemplate == nil then
    geany.message("Unknown error.")
    return
  end
  local sType = GetType(sUser, sExt)
  if sType == nil then
    sType = GetType(sSys, sExt)
    if sType == nil then
      geany.newfile()
    else
      geany.newfile("", sType)
    end
  end
  geany.text(sTemplate)
end
```

4.) See 7-13:
```lua
-- (cross-platform)
-- 2025.08.05
--[[
https://github.com/geany/geany/discussions/4382
]]

-- The name of the template
-- (from "~/.config/geany/templates/files"
-- or the corresponding directory in Windows):
local sTemplate = "file.lua"
-- The type from "filetype_extensions.conf"
-- (or "", if the type does not exist):
local sType = "Lua";

local function ReadFile(sF)
  local h = io.open(sF, "rb")
  if h == nil then return nil end
  local s = h:read("*a")
  h:close()
  return s
end

local sUser = geany.appinfo().configdir

local sTmp = ReadFile(sUser .. geany.dirsep .. "templates" .. geany.dirsep .. 
"files" .. sTemplate)
if sTmp == nil then
  geany.message("Unknown error.")
  return
end
if sType == "" then
  geany.newfile()
else
  geany.newfile("", sType)
end
geany.text(sTmp)
```

-- 
Reply to this email directly or view it on GitHub:
https://github.com/geany/geany/discussions/4382#discussioncomment-14004579
You are receiving this because you are subscribed to this thread.

Message ID: <geany/geany/repo-discussions/4382/comments/[email protected]>

Reply via email to