aboutsummaryrefslogtreecommitdiffhomepage
path: root/core/ext/pm/modules_browser.lua
blob: 2fbef9459923a74bec0d1484caf37148bce00b80 (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
189
190
191
192
193
194
195
-- Copyright 2007-2009 Mitchell mitchell<att>caladbolg.net. See LICENSE.

local textadept = _G.textadept
local locale = _G.locale

---
-- Modules browser for the Textadept project manager.
-- It is enabled with the prefix 'modules' in the project manager entry field.
module('textadept.pm.browsers.modules', package.seeall)

textadept.pm.add_browser('modules')

local lfs = require 'lfs'
local os = require 'os'

local INIT = [[
-- Copyright 2007-2009 Mitchell mitchell<att>caladbolg.net. See LICENSE.

---
-- The $1 module.
-- It provides utilities for editing $2 code.
module('_m.$1', package.seeall)

if type(_G.snippets) == 'table' then
---
-- Container for $2-specific snippets.
-- @class table
-- @name snippets.$1
  _G.snippets.$1 = {}
end

if type(_G.keys) == 'table' then
---
-- Container for $2-specific key commands.
-- @class table
-- @name keys.$1
  _G.keys.$1 = {}
end

require '$1.commands'
require '$1.snippets'

function set_buffer_properties()

end
]]

local SNIPPETS = [[
-- Copyright 2007-2009 Mitchell mitchell<att>caladbolg.net. See LICENSE.

---
-- Snippets for the $1 module.
module('_m.$1.snippets', package.seeall)

local snippets = _G.snippets

if type(snippets) == 'table' then
  snippets.$1 = {}
end
]]

local COMMANDS = [[
-- Copyright 2007-2009 Mitchell mitchell<att>caladbolg.net. See LICENSE.

---
-- Commands for the $1 module.
module('_m.$1.commands', package.seeall)

-- $2-specific key commands.
local keys = _G.keys
if type(keys) == 'table' then
  keys.$1 = {
    al = {
      m = { textadept.io.open, _HOME..'/modules/$1/init.lua' },
    },
  }
end
]]

function matches(entry_text)
  return entry_text:sub(1, 7) == 'modules'
end

local function modify_path(path)
  path[1] = _HOME..'/modules'
  return path
end

function get_contents_for(full_path)
  full_path = modify_path(full_path)
  local dir = {}
  local dirpath = table.concat(full_path, '/')
  for name in lfs.dir(dirpath) do
    if not name:find('^%.') then
      dir[name] = { text = name }
      if lfs.attributes(dirpath..'/'..name, 'mode') == 'directory' then
        dir[name].parent = true
        dir[name].pixbuf = 'gtk-directory'
      end
    end
  end
  return dir
end

function perform_action(selected_item)
  selected_item = modify_path(selected_item)
  local filepath = table.concat(selected_item, '/')
  textadept.io.open(filepath)
  view:focus()
end

local ID = {
  NEW = 1, DELETE = 2, CONF_MIME_TYPES = 3, CONF_KEY_COMMANDS = 4, RELOAD = 5
}

function get_context_menu(selected_item)
  return {
    { locale.PM_BROWSER_MODULE_NEW, ID.NEW },
    { locale.PM_BROWSER_MODULE_DELETE, ID.DELETE },
    { locale.PM_BROWSER_MODULE_CONF_MIME_TYPES, ID.CONF_MIME_TYPES },
    { locale.PM_BROWSER_MODULE_CONF_KEY_COMMANDS, ID.CONF_KEY_COMMANDS },
    { 'separator', 0 },
    { locale.PM_BROWSER_MODULE_RELOAD, ID.RELOAD },
  }
end

function perform_menu_action(menu_id, selected_item)
  if menu_id == ID.NEW then
    local status, module_name =
      cocoa_dialog('standard-inputbox', {
        ['title'] = locale.PM_BROWSER_MODULE_NEW_TITLE,
        ['informative-text'] = locale.PM_BROWSER_MODULE_NEW_INFO_TEXT
      }):match('^(%d)%s+([^\n]+)%s+$')
    if status ~= '1' then return end
    local status, lang_name =
      cocoa_dialog('standard-inputbox', {
        ['title'] = locale.PM_BROWSER_MODULE_NEW_LANG_TITLE,
        ['informative-text'] = locale.PM_BROWSER_MODULE_NEW_LANG_INFO_TEXT
      }):match('^(%d)%s+([^\n]+)%s+$')
    if status ~= '1' then return end
    local module_dir = _HOME..'/modules/'..module_name
    if lfs.mkdir(module_dir) then
      -- write init.lua from template
      local f = io.open(module_dir..'/init.lua', 'w')
      local out = INIT:gsub('$1', module_name):gsub('$2', lang_name)
      f:write(out)
      f:close()
      -- write snippets.lua from template
      f = io.open(module_dir..'/snippets.lua', 'w')
      out = SNIPPETS:gsub('$1', module_name):gsub('$2', lang_name)
      f:write(out)
      f:close()
      -- write commands.lua from template
      f = io.open(module_dir..'/commands.lua', 'w')
      out = COMMANDS:gsub('$1', module_name):gsub('$2', lang_name)
      f:write(out)
      f:close()
    else
      cocoa_dialog('ok-msgbox', {
        ['text'] = locale.PM_BROWSER_MODULE_NEW_ERROR,
        ['informative-text'] = locale.PM_BROWSER_MODULE_NEW_ERROR_TEXT,
        ['no-cancel'] = true
      })
      return
    end
  elseif menu_id == ID.DELETE then
    local module_name = selected_item[2]
    if cocoa_dialog('yesno-msgbox', {
        ['text'] = locale.PM_BROWSER_MODULE_DELETE_TITLE,
        ['informative-text'] =
          string.format(locale.PM_BROWSER_MODULE_DELETE_TEXT, module_name),
        ['no-cancel'] = true,
        ['no-newline'] = true
      }) == '1' then
      local function remove_directory(dirpath)
        for name in lfs.dir(dirpath) do
          if not name:find('^%.%.?$') then os.remove(dirpath..'/'..name) end
        end
        lfs.rmdir(dirpath)
      end
      remove_directory(_HOME..'/modules/'..module_name)
    else
      return
    end
  elseif menu_id == ID.CONF_MIME_TYPES then
    textadept.io.open(_HOME..'/core/ext/mime_types.lua')
  elseif menu_id == ID.CONF_KEY_COMMANDS then
    if textadept.key_commands then
      textadept.io.open(_HOME..'/core/ext/key_commands.lua')
    end
  elseif menu_id == ID.RELOAD then
    textadept.reset()
  end
  textadept.pm.activate()
end