-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmisc.lua
1530 lines (1325 loc) · 38.6 KB
/
misc.lua
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
This is a miscellaneous module for lite-xl-vibe
(duh)
main intentions are
- to extend lite-xl (or lua) classes with necessary methods
- to add some minor things I personally find useful
- to dump things here if I don't have any other submodule for them
]]--
local core = require "core"
local command = require "core.command"
local config = require "core.config"
local keymap = require "core.keymap"
local DocView = require "core.docview"
local StatusView = require "core.statusview"
local LogView = require "core.docview"
local Doc = require "core.doc"
local CommandView = require "core.commandview"
local RootView = require "core.rootview"
local style = require "core.style"
local config = require "core.config"
local common = require "core.common"
local translate = require "core.doc.translate"
-- I mean. Why are these not exposed??
local Node = getmetatable(core.root_view.root_node)
local EmptyView = getmetatable(core.active_view)
-- for tests
local test
local misc = {}
local function dv()
return core.active_view
end
local function doc()
return core.active_view.doc
end
-------------------------------------------------------------------------------
-- Lite compatibility --
-------------------------------------------------------------------------------
local USERDIR = rawget(_G,'USERDIR')
if USERDIR == nil then
core.log("Lite compatibility..")
USERDIR = EXEDIR .. PATHSEP .. 'user' -- debug.getinfo(1).source:match("@?(.*/)")
keymap.add_direct = keymap.add_direct or keymap.add
core.project_dir = core.project_dir or os.getenv("PWD") or io.popen("cd"):read()
core.project_directories = core.project_directories or {{name=core.project_dir}}
core.normalize_to_project_dir = core.normalize_to_project_dir or function(path)
return path
end
common.home_expand = common.home_expand or function(a) return a end
common.home_encode = common.home_encode or function(a) return a end
common.home_encode_list = common.home_encode_list or function(a) return a end
common.serialize = common.serialize or function(val)
if type(val) == "string" then
return string.format("%q", val)
elseif type(val) == "table" then
local t = {}
for k, v in pairs(val) do
table.insert(t, "[" .. common.serialize(k) .. "]=" .. common.serialize(v))
end
return "{" .. table.concat(t, ",") .. "}"
end
return tostring(val)
end
misc.core__quit__orig = core.quit
core.quit = function()
core.confirm_close_docs(core.docs, function() misc.core__quit__orig(true) end)
end
RootView.get_active_node_default = RootView.get_active_node_default or RootView.get_active_node
Node.close_all_docviews = Node.close_all_docviews or function(self,keep_active)
if self.type == "leaf" then
local i = 1
while i <= #self.views do
local view = self.views[i]
if view:is(DocView)
and not view:is(CommandView)
and not view:is(StatusView)
and not view:is(misc.EmptyView)
-- and (not keep_active or view ~= self.active_view)
then
table.remove(self.views, i)
else
i = i + 1
end
end
if #self.views == 0 and self.is_primary_node then
self:add_view(EmptyView())
end
else
self.a:close_all_docviews(keep_active)
self.b:close_all_docviews(keep_active)
if self.a:is_empty() and not self.a.is_primary_node then
self:consume(self.b)
elseif self.b:is_empty() and not self.b.is_primary_node then
self:consume(self.a)
end
end
end
Node.is_empty = Node.is_empty or function (self)
if self.type == "leaf" then
return #self.views == 0 or (#self.views == 1 and self.views[1]:is(EmptyView))
else
return self.a:is_empty() and self.b:is_empty()
end
end
RootView.close_all_docviews = RootView.close_all_docviews or function(self, keep_active)
-- self.root_node:close_all_docviews(keep_active)
-- end
-- local function temp()
-- close current window while it changes anything
local nViews = 0
while (core.root_view.root_node:nViews() ~= nViews) do
nViews = core.root_view.root_node:nViews()
if core.active_view.vibe_parent_node then
core.active_view.vibe_parent_node:close()
else
break
end
end
-- self.root_node:close_all_docviews(keep_active)
end
core.set_project_dir = core.set_project_dir or function(new_dir, change_project_fn)
local chdir_ok = pcall(system.chdir, new_dir)
if chdir_ok then
if change_project_fn then change_project_fn() end
core.project_dir = core.project_dir or os.getenv("PWD") or io.popen("cd"):read()
core.project_directories = core.project_directories or {{name=core.project_dir}}
core.project_files = {}
core.project_files_limit = false
return true
end
return false
end
core.get_project_files = core.get_project_files or function()
return coroutine.wrap(function()
for _,f in ipairs(core.project_files) do
coroutine.yield(core.project_dir, f)
end
end)
end
end
misc.doc_abs_filename = function(doc)
if doc==nil then
doc = core.active_view.doc
end
return doc and (doc.abs_filename or doc.filename and system.absolute_path(doc.filename))
end
-- misc.USERDIR = USERDIR
misc.USERDIR = rawget(_G,'USERDIR') or debug.getinfo(1).source:match("@?(.*/)")
-------------------------------------------------------------------------------
-- vim-like save to clipboard of all deleted text --
-------------------------------------------------------------------------------
-- allright, it does work but damn it's useless when I use x
-- maybe I should accumulate characters from xs into one ring item? hmm..
-- for now moved all that to vibe:delete
-- local on_text_change__orig = Doc.on_text_change
-- function Doc:on_text_change(type)
-- on_text_change__orig(self,type)
-- if type == "remove" then
-- system.set_clipboard(self.undo_stack[self.undo_stack.idx-1][3])
-- end
-- end
-------------------------------------------------------------------------------
-- clipboard ring --
-------------------------------------------------------------------------------
core.vibe.clipboard_ring = {}
core.vibe.clipboard_ring_ix = 0
core.vibe.clipboard_ring_max = 0
misc.system__set_clipboard = system.set_clipboard
function system.set_clipboard(s, skip_ring)
core.log_quiet('vibe system system.set_clipboard')
if s == nil then
return
end
if core.vibe.flags['run_repeat_seq'] then
core.log_quiet(' run_repeat_seq')
if core.vibe.flags['run_repeat_seq__started_clipboard']==false then
core.vibe.clipboard_ring_max = core.vibe.clipboard_ring_max + 1
core.vibe.clipboard_ring[core.vibe.clipboard_ring_max]=''
end
core.vibe.flags['run_repeat_seq__started_clipboard'] = true
-- accumulate repeated stuff
core.vibe.clipboard_ring[core.vibe.clipboard_ring_max] =
core.vibe.clipboard_ring[core.vibe.clipboard_ring_max] .. s
core.vibe.clipboard_ring_ix = core.vibe.clipboard_ring_max
core.vibe.clipboard_ring[core.vibe.clipboard_ring_max
- config.vibe.clipboard_ring_max] = nil
misc.system__set_clipboard(core.vibe.clipboard_ring[core.vibe.clipboard_ring_max])
else
if skip_ring then
core.log_quiet(' skip ring')
core.log_quiet(' = %s', misc.str(skip_ring))
-- pass
else
core.vibe.clipboard_ring_max = core.vibe.clipboard_ring_max + 1
core.vibe.clipboard_ring[core.vibe.clipboard_ring_max] = s
core.vibe.clipboard_ring_ix = core.vibe.clipboard_ring_max
core.log_quiet('no skip, ix=%i', core.vibe.clipboard_ring_ix)
core.vibe.clipboard_ring[core.vibe.clipboard_ring_max - config.vibe.clipboard_ring_max] = nil
end
misc.system__set_clipboard(s)
end
end
function misc.clipboard_ring_rotate()
doc():undo()
core.vibe.clipboard_ring_ix = core.vibe.clipboard_ring_ix - 1
if core.vibe.clipboard_ring[core.vibe.clipboard_ring_ix] == nil then
core.vibe.clipboard_ring_ix = #core.vibe.clipboard_ring
end
misc.system__set_clipboard(core.vibe.clipboard_ring[core.vibe.clipboard_ring_ix])
command.perform("doc:paste")
end
-------------------------------------------------------------------------------
-- Really global stuff
-------------------------------------------------------------------------------
-- so that get_dotsep('misc.exec_history') == core.vibe.misc.exec_history
function misc.get_dotsep(s, obj)
core.log("get_dotsep, s=%s obj=%s", s, tostring(obj))
if not obj then
obj = core.vibe
end
local dotix = s:find_literal('.')
if dotix then
return misc.get_dotsep(s:sub(dotix+1), obj[s:sub(1,dotix-1)])
else
return obj[s]
end
end
function misc.set_dotsep(s, v, obj)
if obj == nil then
obj = core.vibe
end
local dotix = s:find_literal('.')
if dotix then
misc.set_dotsep(s:sub(dotix+1), v, obj[s:sub(1,dotix-1)])
else
obj[s] = v
end
end
function misc.is_fun(x)
return type(x) == "function"
end
misc.is_function = misc.is_fun
function misc.is_table(x)
return type(x) == "table"
end
function misc.is_string(x)
return type(x) == "string"
end
function misc.tables_equal(test, ref, force_simmetrical)
if force_simmetrical then
return misc.compare_tables(test, ref) and misc.compare_tables(ref, test)
else
for key, value in pairs(ref) do
if test[key] ~= value then
return false
end
end
return true
end
end
local function str_mul(str,num)
return string.rep(str, num)
end
function misc.tostring_vararg(...)
local r = ""
local arg = {...}
for i,v in ipairs(arg) do
r = r .. tostring(v) .. "\t"
end
return r
end
getmetatable('string').__mul = function(s,n)
if type(s)=='string' then
if type(n)=='string' then
-- like.. ??
return s*tonumber(n)
else
return str_mul(s,n)
end
else
return str_mul(n,s)
end
end
assert('test'*2 == 'testtest')
function string:starts_with(prefix)
return prefix and #self > #prefix and self:sub(1, #prefix)==prefix
end
function string:isUpperCase()
return self:upper()==self and self:lower()~=self
end
-- substitute suffix, literally (no patterns!)
-- kinda like :gsub(suffix..'$', sub)
-- but then suffix should be escaped and I'm lazy..
function string:sub_suffix_literal(suffix, sub)
if self:sub(#self-#suffix+1)==suffix then
return self:sub(1, #self - #suffix)..sub, 1 -- don't forget the count
end
return self, 0
end
test = 'string_suffix'
assert(test:sub_suffix_literal('suffix','sub')=='string_sub')
function string:find_literal(substr, init)
-- literal find, instead of pattern-based
return string.find(self, substr, init or 1, true)
end
function string:isNumber()
local s = '0123456789'
for j=1,#self do
if s:find_literal(self:sub(j,j)) == nil then
return false
end
end
return true
end
function string:is_command()
return command.map[self]~=nil
end
function string:is_stroke_seq()
-- well.
return not self:is_command()
end
function command.com_by_name(name)
return command.map[name]
end
function command.com_is_runnable(com)
return com and com.predicate()
end
function command.com_by_name_runnable(name)
return command.com_is_runnable(command.com_by_name(name))
end
-- I'm pretty sure I've read too much of refactoring manuals.
function command.can_execute(com_str)
-- which one is more readable?
-- Try 0 (my old one):
local com = command.map[com_str]
-- nil => no such command => sequence?, if not, check predicate
-- return com==nil or com.predicate() -- or misc.is_docview()
return com and com.predicate() or misc.is_docview()
-- (com==nil and misc.is_docview())
-- or (com~=nil and com.predicate())
-- try 1: <where everything's a function>
-- return com_str:is_stroke_seq() or (com_str:is_command()
-- and command.com_by_name_runnable(com_str))
end
function misc.path_is_win_drive(path)
return (#path==2)and(path:sub(2,2)==':')
end
function misc.path_up(path)
if misc.path_is_win_drive(path) then
-- Windows, drive level
-- up = '', alias for 'all drives are subfolders'
return ''
end
local rpath = path:gsub(PATHSEP..'[^'..PATHSEP..']+$','')
-- gsub returns two values, discard the second one
return rpath
end
function misc.path_join(path, dir, ...)
return dir and misc.path_join(path..PATHSEP..dir, ...) or path
end
function misc.path_shorten(path)
local r = path:gsub("([^\\/][^\\/])([^\\/]*)([\\/])","%1%3")
return r
end
function misc.slice(table,i0,i1)
i0 = i0 or 1
i1 = i1 or #table
local r = {}
for i=i0,i1 do
r[#r+1]=table[i]
end
return r
end
-- TODO: infinity fighter like in misc.str
function misc.copy(X, deep)
if type(X) ~= 'table' then
return X
end
return table.copy(X)
end
function table:copy(deep)
local r = {}
for k,v in pairs(self) do
r[k] = deep and misc.copy(v, deep) or v
end
return r
end
function misc.keys(table)
local r = {}
for a,_ in pairs(table) do
r[#r+1]=a
end
return r
end
function misc.table_join(a,...)
if a == nil then
return {}
end
local R = misc.copy(a)
for a,b in pairs(misc.table_join(...)) do
R[a] = b
end
return R
end
function misc.list_unique(list)
local r = {}
for _,a in ipairs(list) do
r[a] = 1
end
return misc.keys(r)
end
function misc.values(table)
local r = {}
for _,a in pairs(table) do
r[#r+1]=a
end
end
function table:find(value)
for k,v in pairs(self) do
if v == value then
return k
end
end
return nil
end
table.indexOf = table.find
function table:values()
return misc.values(self)
end
function table:keys()
return misc.keys(self)
end
function table:map(fun)
local ret = {}
for k,v in pairs(self) do
ret[k] = fun(v)
end
return ret
end
function table.list_to_dict_map(list, fun)
local ret = {}
for _,v in ipairs(list) do
ret[v] = fun(v)
end
return ret
end
function table:list_filter(fun)
local ret
for _,v in ipairs(self) do
if fun(v) then
table.insert(ret, v)
end
end
return ret
end
function table:take_keys(keys)
return table.list_to_dict_map(
keys,
function(key) return self[key] end
)
end
function table:map_with_ix(fun)
local ret = {}
for k,v in pairs(self) do
ret[k] = fun(k,v)
end
return ret
end
function misc.list_contains(list, fun)
for _,item in ipairs(list) do
if fun(item) then
return true
end
end
return false
end
function misc.list_reverse(list)
local A = {}
for j=#list,1,-1 do
table.insert(A,list[j])
end
return A
end
function misc.find_in_list(list, fun)
for _,item in ipairs(list) do
if fun(item) then
return item
end
end
return nil
end
function misc.compare_fun(fun)
return function(a,b) return fun(a)<fun(b) end
end
function misc.compare_key_fun(key)
return function(a,b) return a[key]<b[key] end
end
function misc.fuzzy_match_key(list, key, needle, files)
local res = {}
for _, item in ipairs(list) do
local score = system.fuzzy_match(tostring(item[key]), needle, files)
if score then
table.insert(res, { text = item, score = score })
end
end
table.sort(res, misc.compare_key_fun('score'))
for i, item in ipairs(res) do
res[i] = item.text
end
return res
end
function misc.literal_match_key(list, key, needle)
local res = {}
for _, item in ipairs(list) do
if string.find_literal(item[key], needle) then
table.insert(res, item)
end
end
return res
end
-------------------------------------------------------------------------------
-- Files
-------------------------------------------------------------------------------
function misc.list_drives()
local letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
local R = { dirs={}, files={} }
for j=1,#letters do
local path = letters:sub(j,j)..':'..'\\'
local info = system.get_file_info(path)
if info then
info.filename = path
info.abs_filename = letters:sub(j,j)..':'
table.insert(R.dirs, info)
end
end
return R
end
function misc.list_dir(path)
-- core.log('misc.list_dir, path=%s',path)
if path == '' then
return misc.list_drives()
end
local all = system.list_dir(misc.path_is_win_drive(path) and (path..'\\') or path) or {}
local R = { dirs={}, files = {} }
for _, file in ipairs(all) do
local info = system.get_file_info(path .. PATHSEP .. file)
info.filename = file
info.abs_filename = path .. PATHSEP .. file
table.insert(info.type == "dir" and R.dirs or R.files, info)
end
return R
end
-- https://stackoverflow.com/a/4991602/2624911
function misc.file_exists(name)
local f=io.open(name,"r")
if f~=nil then io.close(f) return true else return false end
end
-- https://stackoverflow.com/a/40195356/2624911
function misc.exists(path)
local ok, err, code = os.rename(path, path)
if not ok then
if code == 13 then
-- Permission denied, but it exists
return true
end
end
return ok, err
end
--- Check if a directory exists in this path
function misc.isdir(path)
-- "/" works on both Unix and Windows
return misc.exists(path.."/")
end
function misc.file_ext(filename)
local ext = filename:gsub(".*%.","")
return ext
end
function misc.file_touch(filepath)
if misc.file_exists(filepath) then
core.error("File exists!")
return nil, "File "..filepath.." exists!"
else
local file, err = io.open(filepath, 'w')
if err == nil then
file:close()
return true
else
return nil, err
end
end
end
function misc.filesize_str(size)
local sfxs = {"B", "KB", "MB", "GB", "TB", "PB"}
local exp = math.floor(math.log(size+1)/math.log(1024))
if exp>#sfxs then exp = #sfxs end
local v = size/(2 ^ (exp*10))
local s = string.format('%.3f', v)
local dot_ix = s:find_literal('.') or #s
s = (dot_ix > 4) and (s:sub(1,dot_ix-1)) or (s:sub(1,4))
if s:sub(#s,#s)=='.' then s = s:sub(1,#s-1) end
return s .. ' ' .. sfxs[exp + 1]
end
-----------------------------------
function misc.get_tabs_list()
local items = {}
for _, doc in ipairs(core.docs) do
table.insert(items, {
["text"] = doc.abs_filename,
["doc"] = doc,
["title"] = "",
})
end
core.log('items_fun : %i items',#items)
return items
end
function misc.text_width(font, _, text, _, x)
return x + font:get_width(text)
end
function misc.draw_items(items, x, y, w, h, draw_fn)
draw_fn = draw_fn or common.draw_text
local font = style.font
local color = style.text
if type(items)=="string" then
items = {items}
end
for _, item in ipairs(items) do
if type(item) == "userdata" then
font = item
elseif type(item) == "table" then
color = item
else
x = draw_fn(font, color, item, nil, x, y, w, h)
end
end
return x
end
function misc.get_items_width(items)
return misc.draw_items( items, 0, 0, 1e6, 1e6, misc.text_width )
end
-------------------------------------------------------------------------------
-- scratch
function misc.scratch_filepath()
return misc.USERDIR .. PATHSEP .. "scratch.lua"
end
if not misc.file_exists(misc.scratch_filepath()) then
local fp = assert( io.open(misc.scratch_filepath(), "wb") )
end
local function dv()
return core.active_view
end
local function doc()
return core.active_view.doc
end
function misc.is_docview()
return core.active_view:is(DocView)
end
local tablestr_depth = 0
local str_infighter = {} -- Infinity Fighter. you'll see.
local function str(a, cur_path, ignore_prefix)
cur_path = cur_path or 'root'
local prefix = ignore_prefix and '' or (' ' * tablestr_depth)
tablestr_depth = tablestr_depth + 1
local R = ''
if type(a) == "table" or type(a) == "function" then
if str_infighter[a] then
return str_infighter[a]
else
str_infighter[a] = cur_path
end
end
if type(a) == 'table' then
if tablestr_depth > (config.vibe.misc_str_max_depth or 4) then
R = '<'..tostring(a)..'>'
else
R = '{'
local listN = 0
local is_list = true
for j,ja in pairs(a) do
if type(j) ~= "number" then
is_list = false
break
end
end
for j,ja in pairs(a) do
listN = listN + 1
if not is_list or listN <= config.vibe.misc_str_max_list then
local s = '[' .. str(j, cur_path, true) .. ']'
R = R .. '\n' .. prefix .. s .. ' = ' .. str(ja,cur_path..s) ..','
end
end
if is_list and listN > config.vibe.misc_str_max_list then
R = R .. '\n' .. prefix
.. string.format("<%i more elements..>", listN - config.vibe.misc_str_max_list)
end
R = R .. '\n' .. prefix .. '}'
end
elseif type(a) == 'string' then
R = '"' .. a .. '"'
else
R = tostring(a)
end
tablestr_depth = tablestr_depth - 1
return prefix .. R
end
function misc.str(a, path)
tablestr_depth = 0
str_infighter = {}
return str(a, path)
end
function misc.has_selection()
return core.active_view:is(DocView) and core.active_view.doc:has_selection()
end
function misc.drop_selection()
local line,col = doc():get_selection()
doc():set_selection(line,col)
end
function misc.open_doc(abs_filename)
core.root_view:open_doc(core.open_doc(abs_filename))
end
function misc.update_mark_line_items(mark)
if core.normalize_to_project_dir(misc.doc_abs_filename())
== core.normalize_to_project_dir(mark.abs_filename) then
mark.line_items = core.active_view:get_line_draw_items(mark.line)
else
core.error(' mark is not in current doc! ')
end
end
function misc.goto_mark(mark)
-- mark = {abs_filename=..,line=..,col=..}
if misc.doc_abs_filename(doc()) ~= mark.abs_filename then
core.log('jumping to file %s', mark.abs_filename)
misc.open_doc(mark.abs_filename)
end
doc():set_selection(mark.line, mark.col)
-- dv:scroll_to_line(res.line, false, true)
misc.update_mark_line_items(mark)
end
function misc.move_to_line(line)
doc():move_to(function() return line,0 end, dv())
end
function misc.append_line_if_last_line(line)
if line >= #doc().lines then
doc():insert(line, math.huge, "\n")
end
end
function misc.find_in_line(symbol, backwards, include, _doc, _line, _col)
core.vibe.last_line_find = {
["backwards"] = backwards,
["symbol"] = symbol,
["include"] = include,
}
if _doc == nil then
_doc = doc()
_line, _col = doc():get_selection()
end
local line = _line
local col = _col
local char
while true do
local line2, col2 = _doc:position_offset(line, col, backwards and -1 or 1)
if char == symbol and (not backwards) and include then
-- going forward we need to get this extra symbol
return line2, col2
end
char = _doc:get_char(line2, col2)
if char==symbol then
if backwards then
if include then
return line2, col2
else
return line, col
end
else
if include then
-- pass
else
return line2, col2
end
end
end
if line ~= line2 or col == col2 then
core.vibe.debug_str = symbol .. ' not found'
return _line, _col
end
line, col = line2, col2
end
end
-- these are used later for translations and such
-- must be in order
misc.matching_objectss = {
['<>'] = {'<','>'},
['()'] = {'(',')'},
['[]'] = {'[',']'},
['{}'] = {'{','}'},
}
function misc.find_in_line_unmatched(symbol,symbol_match,backwards,include,_doc,_line,_col)
if _doc == nil then
_doc = doc()
_line, _col = doc():get_selection()
end
local line = _line
local col = _col
local char
local n_unmatched = 0
while true do
local line2, col2 = _doc:position_offset(line, col, backwards and -1 or 1)
if char == symbol and n_unmatched==0 and (not backwards) and include then
-- going forward we need to get this extra symbol
return line2, col2
end
if char==symbol then
n_unmatched = n_unmatched - 1
end
char = _doc:get_char(line2, col2)
if char==symbol_match then
n_unmatched = n_unmatched + 1
end
if char==symbol and n_unmatched == 0 then
if backwards then
if include then
return line2, col2
else
return line, col
end
else
if include then
-- pass
else
return line2, col2
end
end
end
if line==line2 and col==col2 then --line ~= line2 or col == col2 then
core.vibe.debug_str = symbol .. ' not found'
return _line, _col
end
line, col = line2, col2
end
end
-------------------------------------------------------------------------------
-- hooks, everyone?
-------------------------------------------------------------------------------
command.hooks = {}
local command_perform = command.perform
function command.perform(...)
local r = command_perform(...)
local list = {...}
local name = list[1]
if command.hooks[name] then
for _,hook in ipairs(command.hooks[name]) do
-- TODO : predicates?
core.try(table.unpack(hook)) -- yeah, just add function and arguments as hooks
end
end
return r -- wow, almost forgot this! man it took a long time to debug
end
function command.add_hook(com_name, hook)