aboutsummaryrefslogtreecommitdiff
path: root/plugins/autoinsert.lua
blob: a31f4699dc1872b4ee8274e1788f72afd0e09319 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
-- mod-version:3
local core = require "core"
local translate = require "core.doc.translate"
local config = require "core.config"
local common = require "core.common"
local DocView = require "core.docview"
local command = require "core.command"
local keymap = require "core.keymap"


config.plugins.autoinsert = common.merge({ map = {
  ["["] = "]",
  ["{"] = "}",
  ["("] = ")",
  ['"'] = '"',
  ["'"] = "'",
  ["`"] = "`",
} }, config.plugins.autoinsert)


local function is_closer(chr)
  for _, v in pairs(config.plugins.autoinsert.map) do
    if v == chr then
      return true
    end
  end
end

local function count_char(text, chr)
  local count = 0
  for _ in text:gmatch(chr) do
    count = count + 1
  end
  return count
end


local on_text_input = DocView.on_text_input

function DocView:on_text_input(text)
  local mapping = config.plugins.autoinsert.map[text]

  -- prevents plugin from operating on `CommandView`
  if getmetatable(self) ~= DocView then
    return on_text_input(self, text)
  end

  -- wrap selection if we have a selection
  if mapping and self.doc:has_selection() then
    local l1, c1, l2, c2, swap = self.doc:get_selection(true)
    self.doc:insert(l2, c2, mapping)
    self.doc:insert(l1, c1, text)
    self.doc:set_selection(l1, c1, l2, c2 + 2, swap)
    return
  end

  -- skip inserting closing text
  local chr = self.doc:get_char(self.doc:get_selection())
  if text == chr and is_closer(chr) then
    self.doc:move_to(1)
    return
  end

  -- don't insert closing quote if we have a non-even number on this line
  local line = self.doc:get_selection()
  if text == mapping and count_char(self.doc.lines[line], text) % 2 == 1 then
    return on_text_input(self, text)
  end

  -- auto insert closing bracket
  if mapping and (chr:find("%s") or is_closer(chr) and chr ~= '"') then
    on_text_input(self, text)
    on_text_input(self, mapping)
    self.doc:move_to(-1)
    return
  end

  on_text_input(self, text)
end



local function predicate()
  return getmetatable(core.active_view) == DocView
     and not core.active_view.doc:has_selection()
end

command.add(predicate, {
  ["autoinsert:backspace"] = function()
    local doc = core.active_view.doc
    local l, c = doc:get_selection()
    if c > 1 then
      local chr = doc:get_char(l, c)
      local mapped = config.plugins.autoinsert.map[doc:get_char(l, c - 1)]
      if mapped and mapped == chr then
        doc:delete_to(1)
      end
    end
    command.perform "doc:backspace"
  end,

  ["autoinsert:delete-to-previous-word-start"] = function()
    local doc = core.active_view.doc
    local le, ce = translate.previous_word_start(doc, doc:get_selection())
    while true do
      local l, c = doc:get_selection()
      if l == le and c == ce then
        break
      end
      command.perform "autoinsert:backspace"
    end
  end,
})

keymap.add {
  ["backspace"]            = "autoinsert:backspace",
  ["ctrl+backspace"]       = "autoinsert:delete-to-previous-word-start",
  ["ctrl+shift+backspace"] = "autoinsert:delete-to-previous-word-start",
}








--]]






-- --[[



-- -- mod-version:3
-- local core = require "core"
-- local translate = require "core.doc.translate"
-- local config = require "core.config"
-- local common = require "core.common"
-- local DocView = require "core.docview"
-- local command = require "core.command"
-- local keymap = require "core.keymap"


-- config.plugins.autoinsert = common.merge({ map = {
--   ["["] = "]",
--   ["{"] = "}",
--   ["("] = ")",
--   ['"'] = '"',
--   ["'"] = "'",
--   ["`"] = "`",
-- } }, config.plugins.autoinsert)


-- local function is_closer(chr)
--   for _, v in pairs(config.plugins.autoinsert.map) do
--     if v == chr then
--       return true
--     end
--   end
-- end

-- local function count_char(text, chr)
--   local count = 0
--   local index = 1
--   while true do
--     local s, e = text:find(chr, index, true)
--     if not s then break end
--     count = count + 1
--     index = e + 1
--   end
--   return count
-- end


-- local on_text_input = DocView.on_text_input

-- function DocView:on_text_input(text)
--   local mapping = config.plugins.autoinsert.map[text]
--   local opener, closer = mapping and true, is_closer(text)

--   -- prevents plugin from operating on `CommandView`
--   if (not opener and not closer) or not self:is(DocView) then
--     return on_text_input(self, text)
--   end

--   local l1, c1, l2, c2, swap = self.doc:get_selection(true)
--   print(swap)

--   for idx, line1, col1, line2, col2, swap in self.doc:get_selections(true) do
--     if opener and closer then
--       -- if the opener and closer characters are the same
--       -- we only consider the opener case if we're not near one
--       if self.doc:get_char(line1, col1, line2, col2) == text then
--         opener = false
--       end
--     end
--     if opener then
--       -- is selection
--       if line1 ~= line2 or col1 ~= col2 then
--         self.doc:insert(line2, col2, mapping)
--         self.doc:insert(line1, col1, text)
--         print(line1, col1, line2, col2, swap)
--         self.doc:set_selections(idx, line1, col1+1, line2, col2+1, swap)
--       else
--         self.doc:text_input(text, idx)
--         -- if the opener and closer chars are the same, we only add the closer one
--         -- if there is an odd number of them in the line
--         if text ~= mapping or (text == mapping and count_char(self.doc.lines[line1], mapping) % 2 == 1) then
--           self.doc:text_input(mapping, idx)
--           self.doc:move_to_cursor(idx, -#mapping)
--         end
--       end
--     else -- closer
--       if line1 == line2 and col1 == col2 then
--         local chr = self.doc:get_char(line1, col1, line2, col2)
--         -- skip inserting closing text
--         if text == chr then
--           self.doc:move_to_cursor(idx, #text)
--         else
--           self.doc:text_input(text, idx)
--         end
--       else -- if we're in a selection we behave as normal
--         self.doc:text_input(text, idx)
--       end
--     end
--   end
-- end



-- local function predicate()
--   return getmetatable(core.active_view) == DocView
--      and not core.active_view.doc:has_selection()
-- end

-- command.add(predicate, {
--   ["autoinsert:backspace"] = function()
--     local doc = core.active_view.doc
--     local l, c = doc:get_selection()
--     local chr = doc:get_char(l, c)
--     if config.plugins.autoinsert.map[doc:get_char(l, c - 1)] and is_closer(chr) then
--       doc:delete_to(1)
--     end
--     command.perform "doc:backspace"
--   end,

--   ["autoinsert:delete-to-previous-word-start"] = function()
--     local doc = core.active_view.doc
--     local le, ce = translate.previous_word_start(doc, doc:get_selection())
--     while true do
--       local l, c = doc:get_selection()
--       if l == le and c == ce then
--         break
--       end
--       command.perform "autoinsert:backspace"
--     end
--   end,
-- })

-- keymap.add {
--   ["backspace"]            = "autoinsert:backspace",
--   ["ctrl+backspace"]       = "autoinsert:delete-to-previous-word-start",
--   ["ctrl+shift+backspace"] = "autoinsert:delete-to-previous-word-start",
-- }
-- --]]