aboutsummaryrefslogtreecommitdiff
path: root/modules/textadept/macros.lua
blob: 80a89d0f68e646dcc155c344bb5e5ba489fd97fb (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
196
197
198
199
200
201
202
203
-- Copyright 2007-2009 Mitchell mitchell<att>caladbolg.net. See LICENSE.

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

---
-- Support for recording, saving, and playing macros for the textadept module.
--
-- Events:
--   macro_saved()
--   macro_deleted()
module('_m.textadept.macros', package.seeall)

local MACRO_FILE = _HOME..'/saved_macros'

---
-- The list of available macros.
-- Each key is the macro name, and the value is a numerically indexed table of
-- commands. Each command is a table with a structure as follows:
-- { command, wParam, lParam }
-- where command is the buffer function and wParam and lParam are the arguments
-- for it.
-- @class table
-- @name list
list = {}

---
-- [Local table] The currently recording macro.
-- It is a numerically indexed table of commands with each command having the
-- same structure as described in list.
-- @class table
-- @name current
-- @see list
local current = {}

local recording = false

---
-- [Local function] Handles a Scintilla macro notification.
-- If a macro is being recorded, add this command to 'current'.
-- @param msg The Scintilla message ID.
local function macro_notification(msg, wParam, lParam)
  if recording then
    current[#current + 1] = { msg, wParam or 0, lParam or 0 }
    textadept.statusbar_text = locale.M_TEXTADEPT_MACRO_RECORDING
  end
end
textadept.events.add_handler('macro_record', macro_notification)

---
-- Starts recording a macro.
function start_recording()
  if recording then return end
  buffer:start_record()
  current = {}
  recording = true
  textadept.statusbar_text = 'Macro recording'
end

---
-- Stops recording a macro.
-- Each command's msg in the recorded macro is changed to the name of the
-- message Then the user is prompted for a macro name and the macro is saved to
-- the current macro list and macro file.
function stop_recording()
  if not recording then return end
  buffer:stop_record()
  recording = false
  local ret, macro_name =
    cocoa_dialog('standard-inputbox', {
      ['informative-text'] = locale.M_TEXTADEPT_MACRO_SAVE_TITLE,
      text = locale.M_TEXTADEPT_MACRO_SAVE_TEXT,
      ['no-newline'] = true
    }):match('^(%d)\n([^\n]+)$')

  if ret == '1' and macro_name and #macro_name > 0 then
    for _, command in ipairs(current) do
      local msg = command[1]
      for f, t in pairs(textadept.buffer_functions) do
        if t[1] == msg then command[1] = f break end
      end
    end
    list[macro_name] = current
    save()
    textadept.statusbar_text = locale.M_TEXTADEPT_MACRO_SAVED
    textadept.events.handle('macro_saved')
  else
    textadept.statusbar_text = locale.M_TEXTADEPT_MACRO_NOT_SAVED
  end
end

---
-- Toggles between recording a macro and not recording one.
function toggle_record()
  (not recording and start_recording or stop_recording)()
end

---
-- Plays a specified macro.
-- @param macro_name The name of the macro to play. If none specified, the user
--   is prompted to choose one from a list of available macros.
function play(macro_name)
  if not macro_name then
    local macros = {}
    for name, _ in pairs(list) do macros[#macros + 1] = name end
    if #macros > 0 then
      local ret
      ret, macro_name =
        cocoa_dialog('standard-dropdown', {
          title = locale.M_TEXTADEPT_MACRO_SELECT_TITLE,
          text = locale.M_TEXTADEPT_MACRO_SELECT_TEXT,
          items = '"'..table.concat(macros, '" "')..'"',
          ['string-output'] = true,
          ['no-newline'] = true
        }):match('^([^\n]+)\n([^\n]+)$')
      if ret == 'Cancel' then return end
    end
  end
  local macro = list[macro_name]
  if not macro then return end
  local buffer = buffer
  local bf = textadept.buffer_functions
  for _, command in ipairs(macro) do
    local cmd, wParam, lParam = unpack(command)
    local _, _, p1_type, p2_type  = unpack(bf[cmd])
    if p2_type == 7 and p1_type == 0 or p1_type == 2 then -- single string param
      buffer[cmd](buffer, lParam)
    else
      buffer[cmd](buffer, wParam, lParam)
    end
  end
end

---
-- Deletes a specified macro.
-- @param macro_name The name of the macro to delete.
function delete(macro_name)
  if list[macro_name] then
    list[macro_name] = nil
    save()
    textadept.events.handle('macro_deleted')
  end
end

---
-- Saves the current list of macros to a specified file.
-- @param filename The absolute path to the file to save the macros to.
function save(filename)
  if not filename then filename = MACRO_FILE end
  local f = assert(io.open(filename, 'w'))
  for name, macro in pairs(list) do
    f:write(name, '\n')
    for _, command in ipairs(macro) do
      local msg, wParam, lParam = unpack(command)
      if type(lParam) == 'string' then
        lParam =
          lParam:gsub('[\t\n\r\f]',
                      { ['\t'] = '\\t', ['\n'] = '\\n', ['\r'] = '\\r',
                        ['\f'] = '\\f' })
      end
      f:write(("%s\t%s\t%s\n"):format(msg, wParam, lParam))
    end
    f:write('\n')
  end
  f:close()
end

---
-- Loads macros from a specified file.
-- @param filename The absolute path to the file to load the macros from.
function load(filename)
  if not filename then filename = MACRO_FILE end
  local f = io.open(filename)
  if not f then return end
  local name, current_macro
  for line in f:lines() do
    if not name then -- new macro
      name = line
      current_macro = {}
    else
      if line == '' then -- finished; save current macro
        list[name] = current_macro
        name = nil
      else
        local cmd, wParam, lParam = line:match('^([^\t]+)\t([^\t]+)\t(.*)$')
        if cmd and wParam and lParam then
          lParam =
            lParam:gsub('\\[tnrf]',
                        { ['\\t'] = '\t', ['\\n'] = '\n', ['\\r'] = '\r',
                          ['\\f'] = '\f' })
          local num = wParam:match('^-?%d+$')
          if num then wParam = tonumber(num) end
          num = lParam:match('^-?%d+$')
          if num then lParam = tonumber(num) end
          local command = { cmd, wParam, lParam }
          current_macro[#current_macro + 1] = command
        end
      end
    end
  end
end

load() -- load saved macros on startup