forked from iGio90/filemanager-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
512 lines (400 loc) · 10.7 KB
/
main.lua
File metadata and controls
512 lines (400 loc) · 10.7 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
VERSION = "0.0.1"
local micro = import('micro')
local config = import('micro/config')
local os = import('os')
local utils = dofile(config.ConfigDir .. '/plug/filemanager/utils.lua')
local Filetab = dofile(config.ConfigDir .. '/plug/filemanager/filetab.lua')
local Settings = dofile(config.ConfigDir .. '/plug/filemanager/settings.lua')
local Directory = dofile(config.ConfigDir .. '/plug/filemanager/directory.lua')
local filetab_map = {}
local function get_filetab_by_bp(bp)
for _, ft in ipairs(filetab_map) do
if ft.bp == bp then
return ft
end
end
return nil
end
local function get_filetab_by_tab(tab)
for _, ft in ipairs(filetab_map) do
if ft.bp:Tab() == tab then
return ft
end
end
return nil
end
local function toggle_filetab()
local ft = get_filetab_by_tab(micro.CurTab())
if not ft then
ft = Filetab:new(micro.CurPane(), os.Getwd())
table.insert(filetab_map, ft)
end
ft:toggle()
end
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- All the events for certain Micro keys go below here
-- Other than things we flat-out fail
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- Up Arrow
function preCursorUp(bp)
local ft = get_filetab_by_bp(bp)
if not ft then return end
if ft.view:is_rename_at_cursor_happening() then
ft.view.virtual.cursor:move_to_file_name_start()
return false
end
if ft.view.virtual.cursor:get_loc_y() == Settings.Const.previousDirectoryLine then
return false
end
return not ft.view:is_action_happening()
end
function onCursorUp(bp)
local ft = get_filetab_by_bp(bp)
if not ft then return end
ft.view.virtual:select_line_on_cursor()
end
-- Down Arrow
function preCursorDown(bp)
local ft = get_filetab_by_bp(bp)
if not ft then return end
if ft.view:is_rename_at_cursor_happening() then
ft.view.virtual.cursor:move_to_end()
return false
end
return not ft.view:is_action_happening()
end
function onCursorDown(bp)
local ft = get_filetab_by_bp(bp)
if not ft then return end
ft.view.virtual:select_line_on_cursor()
end
-- Left Arrow
function preCursorLeft(bp)--todo this is bugged if a file stars with an empty space
local ft = get_filetab_by_bp(bp)
if not ft then return end
if ft.view:is_rename_at_cursor_happening() then
return ft.view.virtual.cursor:get_can_move_left()
else
local entry = ft.view:get_entry_at_line()
if entry:is_dir() then
ft.view:collapse_directory(entry)
end
return false
end
end
-- Right Arrow
function preCursorRight(bp)
local ft = get_filetab_by_bp(bp)
if not ft then return end
if ft.view:is_rename_at_cursor_happening() then
return ft.view.virtual.cursor:get_can_move_right()
else
local entry = ft.view:get_entry_at_line()
if entry:is_dir() then
ft.view:expand_directory(entry)
end
return false
end
end
-- Enter
function preInsertNewline(bp)
local ft = get_filetab_by_bp(bp)
if not ft then return end
local view = ft.view
if view:is_rename_at_cursor_happening() then
view:rename_at_cursor()
view:refresh()
elseif view.virtual.cursor:get_loc_y() == Settings.Const.previousDirectoryLine then
ft:load_back_directory()
else
local entry = ft.view:get_entry_at_cursor()
if entry:is_dir() then
view:toggle_directory(entry)
end
end
return false
end
-- Workaround for tab getting inserted into opened files --todo check if this is happening
-- Ref https://github.com/zyedidia/micro/issues/992
-- Tab
function preIndentSelection(bp)
local ft = get_filetab_by_bp(bp)
if not ft then return end
if ft.view.virtual.cursor:get_loc_y() == Settings.Const.previousDirectoryLine then
ft:load_back_directory()
else
ft:load(ft.view:get_entry_at_line(ft.view.virtual.cursor:get_line_num()).abs_path)
end
return false
end
-- Backspace
function preBackspace(bp)
local ft = get_filetab_by_bp(bp)
if not ft then return end
return ft.view:is_rename_at_cursor_happening() and ft.view.virtual.cursor:get_can_move_left()
end
-- PageUp
function preCursorPageUp(bp)
local ft = get_filetab_by_bp(bp)
if not ft then return end
if not ft.view:is_action_happening() then
ft.view.virtual:move_cursor_and_select_first_line()
end
return false
end
-- PageDown
function preCursorPageDown(bp) --todo not reaching bot
local ft = get_filetab_by_bp(bp)
if not ft then return end
if not ft.view:is_action_happening() then
ft.view.virtual:move_cursor_and_select_last_line()
end
return false
end
-- F2
function preSave(bp)
local ft = get_filetab_by_bp(bp)
if not ft then return end
if not ft.view:is_rename_at_cursor_happening() then
ft.view:pre_rename_at_cursor()
else
ft.view.virtual.cursor:select_file_name_no_extension()--todo do a cycling all/name/extension
end
return false
end
-- Ctrl + Q
-- If the target pane is the only one open aside from the file tab,
-- the file tab will close as well, causing the tab to be closed as well.
function preQuit(bp)
local ft = get_filetab_by_bp(bp)
if ft then
ft:close()
micro.CurPane():Quit()
return false
elseif utils.get_panes_quantity(micro.CurTab()) == 2 then --todo add setting, maybe need to create new setting
ft = get_filetab_by_tab(bp:Tab())
ft:close()
return true
end
end
-- Ctrl + A
function preSelectAll(bp)
local ft = get_filetab_by_bp(bp)
if not ft then return end
if ft.view:is_rename_at_cursor_happening() then
ft.view.virtual.cursor:select_file_name()
else
--ft.view.virtual.cursor:select_all()--todo
end
return false
end
-- Ctrl + Up Arrow
function preCursorStart(bp)
local ft = get_filetab_by_bp(bp)
if not ft then return end
if not ft.view:is_action_happening() then
ft.view:move_cursor_to_parent()
end
return false
end
-- Shift + Up
function preSelectUp(bp) -- bug the first line is nor selected entirly --todo
local ft = get_filetab_by_bp(bp)
if not ft then return end
if not ft.view:is_action_happening() then
ft.view:move_cursor_to_first_sibling()
end
return false
end
-- Shift + Down
function preSelectDown(bp) --todo
local ft = get_filetab_by_bp(bp)
if not ft then return end
if not ft.view:is_action_happening() then
ft.view:move_cursor_to_last_sibling()
end
return false
end
-- Ctrl + Down Arrow
function preCursorEnd(bp)
local ft = get_filetab_by_bp(bp)
if not ft then return end
if not ft.view:is_action_happening() then --todo visual bug
ft.view:move_cursor_to_next_dir_outside()
end
return false
end
-- Ctrl + Right Arrow
function preWordRight(bp)
local ft = get_filetab_by_bp(bp)
if not ft then return end
return ft.view:is_rename_at_cursor_happening() and ft.view.virtual.cursor:get_can_move_left()
end
-- Ctrl + Left Arrow
function preWordLeft(bp)
local ft = get_filetab_by_bp(bp)
if not ft then return end
return ft.view:is_rename_at_cursor_happening() and ft.view.virtual.cursor:get_can_move_left()
end
function onWordLeft(bp)
local ft = get_filetab_by_bp(bp)
if ft then ft.view.virtual.cursor:adjust() end
end
-- Alt + Down Arrow
function preMoveLinesDown(bp) --todo
return true
end
-- Alt + Up Arrow
function preMoveLinesUp(bp) --todo
return true
end
-- Alt + Right Arrow
function preEndOfLine(bp)
local ft = get_filetab_by_bp(bp)
if not ft then return end
return ft.view:is_rename_at_cursor_happening()
end
-- Alt + Left Arrow
function preStartOfTextToggle(bp)
local ft = get_filetab_by_bp(bp)
if not ft then return end
if ft.view:is_rename_at_cursor_happening() then
ft.view.virtual.cursor:move_to_file_name_start()
end
return false
end
-- Mouse Left Click
function onMousePress(bp)
local ft = get_filetab_by_bp(bp)
if not ft then return end
ft.view.virtual:click_event()
end
--Left Click Drag
function onMouseDrag(bp)
local ft = get_filetab_by_bp(bp)
if not ft then return end
ft.view.virtual:drag_event()
end
-- MouseWheel Down
function onScrollDown(bp)
bp:ScrollAdjust() --fix micro bug
end
-- CtrlF
function preFind(bp)
-- Since something is always selected, clear before a find
-- Prevents copying the selection into the find input
-- clearselection_if_tree(bp)
end
-- FIXME: doesn't work for whatever reason
function onFind(bp)
-- Select the whole line after a find, instead of just the input txt
--selectline_if_tree(bp)
end
-- CtrlN after CtrlF
function onFindNext(bp)
--selectline_if_tree(bp)
end
-- CtrlP after CtrlF
function onFindPrevious(bp)
-- selectline_if_tree(bp)
end
--TODO
-- Close all
function preQuitAll(_)
--tab:close()
end
function onNextSplit(bp)
--selectline_if_tree(bp)
end
function onPreviousSplit(bp)
--selectline_if_tree(bp)
end
------------------------------------------------------------------
-- Fail a bunch of useless actions
-- Some of these need to be removed (read-only makes some useless)
------------------------------------------------------------------
-- Ctrl + R
function preToggleRuler(bp)
return not is_action_on_any_tab(bp)
end
function preStartOfText(bp) --todo
return not is_action_on_any_asdastab(bp)
end
function preSelectLeft(bp)
return not is_action_on_any_tab(bp)
end
function preSelectRight(bp)
return not is_action_on_any_tab(bp)
end
function preSelectWordRight(bp)
return not is_action_on_any_tab(bp)
end
function preSelectWordLeft(bp)
return not is_action_on_any_tab(bp)
end
function preSelectToStartOfLine(bp)
return not is_action_on_any_tab(bp)
end
function preSelectToStartOfText(bp)
return not is_action_on_any_tab(bp)
end
function preSelectToEndOfLine(bp)
return not is_action_on_any_tab(bp)
end
function preSelectToStart(bp)
return not is_action_on_any_tab(bp)
end
function preSelectToEnd(bp)
return not is_action_on_any_tab(bp)
end
function preDeleteWordLeft(bp)
return not is_action_on_any_tab(bp)
end
function preDeleteWordRight(bp)
return not is_action_on_any_tab(bp)
end
function preOutdentSelection(bp)
return not is_action_on_any_tab(bp)
end
function preOutdentLine(bp)
return not is_action_on_any_tab(bp)
end
function preCut(bp)
return not is_action_on_any_tab(bp)
end
function preCutLine(bp)
return not is_action_on_any_tab(bp)
end
function preDuplicateLine(bp)
return true
end
function prePaste(bp)
return not is_action_on_any_tab(bp)
end
function prePastePrimary(bp)
return not is_action_on_any_tab(bp)
end
function preMouseMultiCursor(bp)
return not is_action_on_any_tab(bp)
end
function onMouseMultiCursor(bp)
return not is_action_on_any_tab(bp)
end
function preSpawnMultiCursor(bp)
return not is_action_on_any_tab(bp)
end
function is_action_on_any_tab(bp)
local ft = get_filetab_by_bp(bp)
return ft or true and false
end
local os = import("os")
function init()
-- Adds colors to the ".." and any dir's in the tree bp via syntax highlighting
-- TODO: Change it to work with git, based on untracked/changed/added/whatever
config.AddRuntimeFile('filemanager', config.RTSyntax, 'syntax/filemanager.yaml')
Settings.load_default()
config.MakeCommand('ft', toggle_filetab, config.NoComplete)
if Settings.get_option("openOnStart") then
toggle_filetab()
end
end