-
Notifications
You must be signed in to change notification settings - Fork 259
/
Copy pathutil.lua
1353 lines (1196 loc) · 35.8 KB
/
util.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
local iter = require("obsidian.itertools").iter
local enumerate = require("obsidian.itertools").enumerate
local log = require "obsidian.log"
local compat = require "obsidian.compat"
local util = {}
-------------------
-- Table methods --
-------------------
---Check if a list table contains a value.
---
---@param table any[]
---@param val any
---@return boolean
util.tbl_contains = function(table, val)
for i = 1, #table do
if vim.deep_equal(table[i], val) then
return true
end
end
return false
end
---Check if a table contains a key.
---
---@param table table
---@param needle any
---@return boolean
util.tbl_contains_key = function(table, needle)
for key, _ in pairs(table) do
if key == needle then
return true
end
end
return false
end
---Check if an object is an array-like table.
---@param t any
---@return boolean
util.tbl_is_array = function(t)
if type(t) ~= "table" then
return false
end
return compat.is_list(t)
end
---Check if an object is an non-array table.
---@param t any
---@return boolean
util.tbl_is_mapping = function(t)
return type(t) == "table" and (vim.tbl_isempty(t) or not util.tbl_is_array(t))
end
---Return a new list table with only the unique values of the original table.
---
---@param table table
---@return any[]
util.tbl_unique = function(table)
local out = {}
for _, val in pairs(table) do
if not util.tbl_contains(out, val) then
out[#out + 1] = val
end
end
return out
end
--- Clear all values from a table.
---
---@param t table
util.tbl_clear = function(t)
for k, _ in pairs(t) do
t[k] = nil
end
end
--------------------
-- String methods --
--------------------
---Iterate over all matches of 'pattern' in 's'. 'gfind' is to 'find' as 'gsub' is to 'sub'.
---@param s string
---@param pattern string
---@param init integer|?
---@param plain boolean|?
util.gfind = function(s, pattern, init, plain)
init = init and init or 1
return function()
if init < #s then
local m_start, m_end = string.find(s, pattern, init, plain)
if m_start ~= nil and m_end ~= nil then
init = m_end + 1
return m_start, m_end
end
end
return nil
end
end
---Quote a string for safe command-line usage.
---
---@param str string
---@return string
util.quote = function(str)
return vim.fn.shellescape(str)
end
local char_to_hex = function(c)
return string.format("%%%02X", string.byte(c))
end
local hex_to_char = function(hex)
return string.char(tonumber(hex, 16))
end
--- Encode a string into URL-safe version.
---
---@param str string
---@param opts { keep_path_sep: boolean|? }|?
---
---@return string
util.urlencode = function(str, opts)
opts = opts or {}
local url = str
url = url:gsub("\n", "\r\n")
url = url:gsub("([^/%w _%%%-%.~])", char_to_hex)
if not opts.keep_path_sep then
url = url:gsub("/", char_to_hex)
end
-- Spaces in URLs are always safely encoded with `%20`, but not always safe
-- with `+`. For example, `+` in a query param's value will be interpreted
-- as a literal plus-sign if the decoder is using JavaScript's `decodeURI`
-- function.
url = url:gsub(" ", "%%20")
return url
end
--- Decode a URL-encoded string.
---
---@param str string
---
---@return string
util.urldecode = function(str)
str = str:gsub("%%(%x%x)", hex_to_char)
return str
end
---Match the case of 'key' to the given 'prefix' of the key.
---
---@param prefix string
---@param key string
---@return string|?
util.match_case = function(prefix, key)
local out_chars = {}
for i = 1, string.len(key) do
local c_key = string.sub(key, i, i)
local c_pre = string.sub(prefix, i, i)
if c_pre:lower() == c_key:lower() then
table.insert(out_chars, c_pre)
elseif c_pre:len() > 0 then
return nil
else
table.insert(out_chars, c_key)
end
end
return table.concat(out_chars, "")
end
util.escape_magic_characters = function(text)
return text:gsub("([%(%)%.%%%+%-%*%?%[%]%^%$])", "%%%1")
end
---Check if a string is a valid URL.
---@param s string
---@return boolean
util.is_url = function(s)
local search = require "obsidian.search"
if
string.match(util.strip_whitespace(s), "^" .. search.Patterns[search.RefTypes.NakedUrl] .. "$")
or string.match(util.strip_whitespace(s), "^" .. search.Patterns[search.RefTypes.FileUrl] .. "$")
or string.match(util.strip_whitespace(s), "^" .. search.Patterns[search.RefTypes.MailtoUrl] .. "$")
then
return true
else
return false
end
end
util.is_img = function(s)
for _, suffix in ipairs { ".png", ".jpg", ".jpeg", ".heic", ".gif", ".svg", ".ico" } do
if vim.endswith(s, suffix) then
return true
end
end
return false
end
util.is_pdf = function(s)
for _, suffix in ipairs { ".pdf" } do
if vim.endswith(s, suffix) then
return true
end
end
return false
end
-- This function removes a single backslash within double square brackets
util.unescape_single_backslash = function(text)
return text:gsub("(%[%[[^\\]+)\\(%|[^\\]+]])", "%1%2")
end
util.string_enclosing_chars = { [["]], [[']] }
---Count the indentation of a line.
---@param str string
---@return integer
util.count_indent = function(str)
local indent = 0
for i = 1, #str do
local c = string.sub(str, i, i)
-- space or tab both count as 1 indent
if c == " " or c == " " then
indent = indent + 1
else
break
end
end
return indent
end
---Check if a string is only whitespace.
---@param str string
---@return boolean
util.is_whitespace = function(str)
return string.match(str, "^%s+$") ~= nil
end
---Get the substring of `str` starting from the first character and up to the stop character,
---ignoring any enclosing characters (like double quotes) and stop characters that are within the
---enclosing characters. For example, if `str = [=["foo", "bar"]=]` and `stop_char = ","`, this
---would return the string `[=[foo]=]`.
---
---@param str string
---@param stop_chars string[]
---@param keep_stop_char boolean|?
---@return string|?, string
util.next_item = function(str, stop_chars, keep_stop_char)
local og_str = str
-- Check for enclosing characters.
local enclosing_char = nil
local first_char = string.sub(str, 1, 1)
for _, c in ipairs(util.string_enclosing_chars) do
if first_char == c then
enclosing_char = c
str = string.sub(str, 2)
break
end
end
local result
local hits
for _, stop_char in ipairs(stop_chars) do
-- First check for next item when `stop_char` is present.
if enclosing_char ~= nil then
result, hits = string.gsub(
str,
"([^" .. enclosing_char .. "]+)([^\\]?)" .. enclosing_char .. "%s*" .. stop_char .. ".*",
"%1%2"
)
result = enclosing_char .. result .. enclosing_char
else
result, hits = string.gsub(str, "([^" .. stop_char .. "]+)" .. stop_char .. ".*", "%1")
end
if hits ~= 0 then
local i = string.find(str, stop_char, string.len(result), true)
if keep_stop_char then
return result .. stop_char, string.sub(str, i + 1)
else
return result, string.sub(str, i + 1)
end
end
-- Now check for next item without the `stop_char` after.
if not keep_stop_char and enclosing_char ~= nil then
result, hits = string.gsub(str, "([^" .. enclosing_char .. "]+)([^\\]?)" .. enclosing_char .. "%s*$", "%1%2")
result = enclosing_char .. result .. enclosing_char
elseif not keep_stop_char then
result = str
hits = 1
else
result = nil
hits = 0
end
if hits ~= 0 then
if keep_stop_char then
result = result .. stop_char
end
return result, ""
end
end
return nil, og_str
end
---Strip whitespace from the ends of a string.
---@param str string
---@return string
util.strip_whitespace = function(str)
return util.rstrip_whitespace(util.lstrip_whitespace(str))
end
---Strip whitespace from the right end of a string.
---@param str string
---@return string
util.rstrip_whitespace = function(str)
str = string.gsub(str, "%s+$", "")
return str
end
---Strip whitespace from the left end of a string.
---@param str string
---@param limit integer|?
---@return string
util.lstrip_whitespace = function(str, limit)
if limit ~= nil then
local num_found = 0
while num_found < limit do
str = string.gsub(str, "^%s", "")
num_found = num_found + 1
end
else
str = string.gsub(str, "^%s+", "")
end
return str
end
---Strip enclosing characters like quotes from a string.
---@param str string
---@return string
util.strip_enclosing_chars = function(str)
local c_start = string.sub(str, 1, 1)
local c_end = string.sub(str, #str, #str)
for _, enclosing_char in ipairs(util.string_enclosing_chars) do
if c_start == enclosing_char and c_end == enclosing_char then
str = string.sub(str, 2, #str - 1)
break
end
end
return str
end
---Check if a string has enclosing characters like quotes.
---@param str string
---@return boolean
util.has_enclosing_chars = function(str)
for _, enclosing_char in ipairs(util.string_enclosing_chars) do
if vim.startswith(str, enclosing_char) and vim.endswith(str, enclosing_char) then
return true
end
end
return false
end
---Strip YAML comments from a string.
---@param str string
---@return string
util.strip_comments = function(str)
if vim.startswith(str, "# ") then
return ""
elseif not util.has_enclosing_chars(str) then
return select(1, string.gsub(str, [[%s+#%s.*$]], ""))
else
return str
end
end
---Check if a string contains a substring.
---@param str string
---@param substr string
---@return boolean
util.string_contains = function(str, substr)
local i = string.find(str, substr, 1, true)
return i ~= nil
end
---Replace up to `n` occurrences of `what` in `s` with `with`.
---@param s string
---@param what string
---@param with string
---@param n integer|?
---@return string
---@return integer
util.string_replace = function(s, what, with, n)
local count = 0
local function replace(s_)
if n ~= nil and count >= n then
return s_
end
local b_idx, e_idx = string.find(s_, what, 1, true)
if b_idx == nil or e_idx == nil then
return s_
end
count = count + 1
return string.sub(s_, 1, b_idx - 1) .. with .. replace(string.sub(s_, e_idx + 1))
end
s = replace(s)
return s, count
end
--- Count occurrences of the `pattern` in `s`.
---
---@param s string
---@param pattern string
---
---@return integer
util.string_count = function(s, pattern)
return select(2, string.gsub(s, pattern, ""))
end
------------------------------------
-- Miscellaneous helper functions --
------------------------------------
---@enum OSType
util.OSType = {
Linux = "Linux",
Wsl = "Wsl",
Windows = "Windows",
Darwin = "Darwin",
FreeBSD = "FreeBSD",
}
util._current_os = nil
---Get the running operating system.
---Reference https://vi.stackexchange.com/a/2577/33116
---@return OSType
util.get_os = function()
if util._current_os ~= nil then
return util._current_os
end
local this_os
if vim.fn.has "win32" == 1 then
this_os = util.OSType.Windows
else
local sysname = vim.loop.os_uname().sysname
local release = vim.loop.os_uname().release:lower()
if sysname:lower() == "linux" and string.find(release, "microsoft") then
this_os = util.OSType.Wsl
else
this_os = sysname
end
end
assert(this_os)
util._current_os = this_os
return this_os
end
---Get the strategy for opening notes
---
---@param opt obsidian.config.OpenStrategy
---@return string
util.get_open_strategy = function(opt)
local OpenStrategy = require("obsidian.config").OpenStrategy
-- either 'leaf', 'row' for vertically split windows, or 'col' for horizontally split windows
local cur_layout = vim.fn.winlayout()[1]
if vim.startswith(OpenStrategy.hsplit, opt) then
if cur_layout ~= "col" then
return "split "
else
return "e "
end
elseif vim.startswith(OpenStrategy.vsplit, opt) then
if cur_layout ~= "row" then
return "vsplit "
else
return "e "
end
elseif vim.startswith(OpenStrategy.current, opt) then
return "e "
else
log.err("undefined open strategy '%s'", opt)
return "e "
end
end
---Create a new unique Zettel ID.
---
---@return string
util.zettel_id = function()
local suffix = ""
for _ = 1, 4 do
suffix = suffix .. string.char(math.random(65, 90))
end
return tostring(os.time()) .. "-" .. suffix
end
---Toggle the checkbox on the line that the cursor is on.
util.toggle_checkbox = function(opts, line_num)
-- Allow line_num to be optional, defaulting to the current line if not provided
line_num = line_num or unpack(vim.api.nvim_win_get_cursor(0))
local line = vim.api.nvim_buf_get_lines(0, line_num - 1, line_num, false)[1]
local checkbox_pattern = "^%s*- %[.] "
local checkboxes = opts or { " ", "x" }
if not string.match(line, checkbox_pattern) then
local unordered_list_pattern = "^(%s*)[-*+] (.*)"
if string.match(line, unordered_list_pattern) then
line = string.gsub(line, unordered_list_pattern, "%1- [ ] %2")
else
line = string.gsub(line, "^(%s*)", "%1- [ ] ")
end
else
for i, check_char in enumerate(checkboxes) do
if string.match(line, "^%s*- %[" .. util.escape_magic_characters(check_char) .. "%].*") then
if i == #checkboxes then
i = 0
end
line = util.string_replace(line, "- [" .. check_char .. "]", "- [" .. checkboxes[i + 1] .. "]", 1)
break
end
end
end
-- 0-indexed
vim.api.nvim_buf_set_lines(0, line_num - 1, line_num, true, { line })
end
---Determines if the given date is a working day (not weekend)
---
---@param time integer
---
---@return boolean
util.is_working_day = function(time)
local is_saturday = (os.date("%w", time) == "6")
local is_sunday = (os.date("%w", time) == "0")
return not (is_saturday or is_sunday)
end
---Determines the last working day before a given time
---
---@param time integer
---@return integer
util.working_day_before = function(time)
local previous_day = time - (24 * 60 * 60)
if util.is_working_day(previous_day) then
return previous_day
else
return util.working_day_before(previous_day)
end
end
---Determines the next working day before a given time
---
---@param time integer
---@return integer
util.working_day_after = function(time)
local next_day = time + (24 * 60 * 60)
if util.is_working_day(next_day) then
return next_day
else
return util.working_day_after(next_day)
end
end
---@return table - tuple containing {bufnr, winnr, row, col}
util.get_active_window_cursor_location = function()
local buf = vim.api.nvim_win_get_buf(0)
local win = vim.api.nvim_get_current_win()
local row, col = unpack(vim.api.nvim_win_get_cursor(win))
local location = { buf, win, row, col }
return location
end
---Determines if cursor is currently inside markdown link.
---
---@param line string|nil - line to check or current line if nil
---@param col integer|nil - column to check or current column if nil (1-indexed)
---@param include_naked_urls boolean|?
---@param include_file_urls boolean|?
---@param include_block_ids boolean|?
---@return integer|nil, integer|nil, obsidian.search.RefTypes|? - start and end column of link (1-indexed)
util.cursor_on_markdown_link = function(line, col, include_naked_urls, include_file_urls, include_block_ids)
local search = require "obsidian.search"
local current_line = line and line or vim.api.nvim_get_current_line()
local _, cur_col = unpack(vim.api.nvim_win_get_cursor(0))
cur_col = col or cur_col + 1 -- nvim_win_get_cursor returns 0-indexed column
for match in
iter(search.find_refs(current_line, {
include_naked_urls = include_naked_urls,
include_file_urls = include_file_urls,
include_block_ids = include_block_ids,
}))
do
local open, close, m_type = unpack(match)
if open <= cur_col and cur_col <= close then
return open, close, m_type
end
end
return nil
end
--- Deprecated, use `parse_cursor_link()` instead.
---
---@param line string|?
---@param col integer|?
---@param include_naked_urls boolean|?
---@param include_file_urls boolean|?
---
---@return string|?, string|?, obsidian.search.RefTypes|?
util.cursor_link = function(line, col, include_naked_urls, include_file_urls)
return util.parse_cursor_link {
line = line,
col = col,
include_naked_urls = include_naked_urls,
include_file_urls = include_file_urls,
}
end
--- Get the link location and name of the link under the cursor, if there is one.
---
---@param opts { line: string|?, col: integer|?, include_naked_urls: boolean|?, include_file_urls: boolean|?, include_block_ids: boolean|? }|?
---
---@return string|?, string|?, obsidian.search.RefTypes|?
util.parse_cursor_link = function(opts)
opts = opts and opts or {}
local current_line = opts.line and opts.line or vim.api.nvim_get_current_line()
local open, close, link_type = util.cursor_on_markdown_link(
current_line,
opts.col,
opts.include_naked_urls,
opts.include_file_urls,
opts.include_block_ids
)
if open == nil or close == nil then
return
end
local link = current_line:sub(open, close)
return util.parse_link(link, {
link_type = link_type,
include_naked_urls = opts.include_naked_urls,
include_file_urls = opts.include_file_urls,
include_block_ids = opts.include_block_ids,
})
end
---@param link string
---@param opts { include_naked_urls: boolean|?, include_file_urls: boolean|?, include_block_ids: boolean|?, link_type: obsidian.search.RefTypes|? }|?
---
---@return string|?, string|?, obsidian.search.RefTypes|?
util.parse_link = function(link, opts)
local search = require "obsidian.search"
opts = opts and opts or {}
local link_type = opts.link_type
if link_type == nil then
for match in
iter(search.find_refs(link, {
include_naked_urls = opts.include_naked_urls,
include_file_urls = opts.include_file_urls,
include_block_ids = opts.include_block_ids,
}))
do
local _, _, m_type = unpack(match)
if m_type then
link_type = m_type
break
end
end
end
if link_type == nil then
return nil
end
local link_location, link_name
if link_type == search.RefTypes.Markdown then
link_location = link:gsub("^%[(.-)%]%((.*)%)$", "%2")
link_name = link:gsub("^%[(.-)%]%((.*)%)$", "%1")
elseif link_type == search.RefTypes.NakedUrl then
link_location = link
link_name = link
elseif link_type == search.RefTypes.FileUrl then
link_location = link
link_name = link
elseif link_type == search.RefTypes.WikiWithAlias then
link = util.unescape_single_backslash(link)
-- remove boundary brackets, e.g. '[[XXX|YYY]]' -> 'XXX|YYY'
link = link:sub(3, #link - 2)
-- split on the "|"
local split_idx = link:find "|"
link_location = link:sub(1, split_idx - 1)
link_name = link:sub(split_idx + 1)
elseif link_type == search.RefTypes.Wiki then
-- remove boundary brackets, e.g. '[[YYY]]' -> 'YYY'
link = link:sub(3, #link - 2)
link_location = link
link_name = link
elseif link_type == search.RefTypes.BlockID then
link_location = util.standardize_block(link)
link_name = link
else
error("not implemented for " .. link_type)
end
return link_location, link_name, link_type
end
--- Get the tag under the cursor, if there is one.
---
---@param line string|?
---@param col integer|?
---
---@return string|?
util.cursor_tag = function(line, col)
local search = require "obsidian.search"
local current_line = line and line or vim.api.nvim_get_current_line()
local _, cur_col = unpack(vim.api.nvim_win_get_cursor(0))
cur_col = col or cur_col + 1 -- nvim_win_get_cursor returns 0-indexed column
for match in iter(search.find_tags(current_line)) do
local open, close, _ = unpack(match)
if open <= cur_col and cur_col <= close then
return string.sub(current_line, open + 1, close)
end
end
return nil
end
util.gf_passthrough = function()
if util.cursor_on_markdown_link(nil, nil, true) then
return "<cmd>ObsidianFollowLink<CR>"
else
return "gf"
end
end
util.smart_action = function()
-- follow link if possible
if util.cursor_on_markdown_link(nil, nil, true) then
return "<cmd>ObsidianFollowLink<CR>"
end
-- toggle task if possible
-- cycles through your custom UI checkboxes, default: [ ] [~] [>] [x]
return "<cmd>ObsidianToggleCheckbox<CR>"
end
---Get the path to where a plugin is installed.
---@param name string|?
---@return string|?
util.get_src_root = function(name)
name = name and name or "obsidian.nvim"
for _, path in ipairs(vim.api.nvim_list_runtime_paths()) do
if vim.endswith(path, name) then
return path
end
end
return nil
end
--- Get info about a plugin.
---
---@param name string|?
---
---@return { commit: string|?, path: string }|?
util.get_plugin_info = function(name)
name = name and name or "obsidian.nvim"
local src_root = util.get_src_root(name)
if src_root == nil then
return nil
end
local out = { path = src_root }
local Job = require "plenary.job"
local output, exit_code = Job:new({ ---@diagnostic disable-line: missing-fields
command = "git",
args = { "rev-parse", "HEAD" },
cwd = src_root,
enable_recording = true,
}):sync(1000)
if exit_code == 0 then
out.commit = output[1]
end
return out
end
---@param cmd string
---@return string|?
util.get_external_dependency_info = function(cmd)
local Job = require "plenary.job"
local output, exit_code = Job:new({ ---@diagnostic disable-line: missing-fields
command = cmd,
args = { "--version" },
enable_recording = true,
}):sync(1000)
if exit_code == 0 then
return output[1]
end
end
---Get an iterator of (bufnr, bufname) over all named buffers. The buffer names will be absolute paths.
---
---@return function () -> (integer, string)|?
util.get_named_buffers = function()
local idx = 0
local buffers = vim.api.nvim_list_bufs()
---@return integer|?
---@return string|?
return function()
while idx < #buffers do
idx = idx + 1
local bufnr = buffers[idx]
if vim.api.nvim_buf_is_loaded(bufnr) then
return bufnr, vim.api.nvim_buf_get_name(bufnr)
end
end
end
end
---Insert text at current cursor position.
---@param text string
util.insert_text = function(text)
local curpos = vim.fn.getcurpos()
local line_num, line_col = curpos[2], curpos[3]
local indent = string.rep(" ", line_col)
-- Convert text to lines table so we can handle multi-line strings.
local lines = {}
for line in text:gmatch "[^\r\n]+" do
lines[#lines + 1] = line
end
for line_index, line in pairs(lines) do
local current_line_num = line_num + line_index - 1
local current_line = vim.fn.getline(current_line_num)
assert(type(current_line) == "string")
-- Since there's no column 0, remove extra space when current line is blank.
if current_line == "" then
indent = indent:sub(1, -2)
end
local pre_txt = current_line:sub(1, line_col)
local post_txt = current_line:sub(line_col + 1, -1)
local inserted_txt = pre_txt .. line .. post_txt
vim.fn.setline(current_line_num, inserted_txt)
-- Create new line so inserted_txt doesn't replace next lines
if line_index ~= #lines then
vim.fn.append(current_line_num, indent)
end
end
end
---@param bufnr integer
---@return string
util.buf_get_full_text = function(bufnr)
local text = table.concat(vim.api.nvim_buf_get_lines(bufnr, 0, -1, true), "\n")
if vim.api.nvim_get_option_value("eol", { buf = bufnr }) then
text = text .. "\n"
end
return text
end
--- Get the current visual selection of text and exit visual mode.
---
---@param opts { strict: boolean|? }|?
---
---@return { lines: string[], selection: string, csrow: integer, cscol: integer, cerow: integer, cecol: integer }|?
util.get_visual_selection = function(opts)
opts = opts or {}
-- Adapted from fzf-lua:
-- https://github.com/ibhagwan/fzf-lua/blob/6ee73fdf2a79bbd74ec56d980262e29993b46f2b/lua/fzf-lua/utils.lua#L434-L466
-- this will exit visual mode
-- use 'gv' to reselect the text
local _, csrow, cscol, cerow, cecol
local mode = vim.fn.mode()
if opts.strict and not vim.endswith(string.lower(mode), "v") then
return
end
if mode == "v" or mode == "V" or mode == "" then
-- if we are in visual mode use the live position
_, csrow, cscol, _ = unpack(vim.fn.getpos ".")
_, cerow, cecol, _ = unpack(vim.fn.getpos "v")
if mode == "V" then
-- visual line doesn't provide columns
cscol, cecol = 0, 999
end
-- exit visual mode
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>", true, false, true), "n", true)
else
-- otherwise, use the last known visual position
_, csrow, cscol, _ = unpack(vim.fn.getpos "'<")
_, cerow, cecol, _ = unpack(vim.fn.getpos "'>")
end
-- Swap vars if needed
if cerow < csrow then
csrow, cerow = cerow, csrow
cscol, cecol = cecol, cscol
elseif cerow == csrow and cecol < cscol then
cscol, cecol = cecol, cscol
end
local lines = vim.fn.getline(csrow, cerow)
assert(type(lines) == "table")
if vim.tbl_isempty(lines) then
return
end
-- When the whole line is selected via visual line mode ("V"), cscol / cecol will be equal to "v:maxcol"
-- for some odd reason. So change that to what they should be here. See ':h getpos' for more info.
local maxcol = vim.api.nvim_get_vvar "maxcol"
if cscol == maxcol then
cscol = string.len(lines[1])
end
if cecol == maxcol then
cecol = string.len(lines[#lines])
end
---@type string
local selection
local n = #lines
if n <= 0 then
selection = ""
elseif n == 1 then
selection = string.sub(lines[1], cscol, cecol)
elseif n == 2 then
selection = string.sub(lines[1], cscol) .. "\n" .. string.sub(lines[n], 1, cecol)
else
selection = string.sub(lines[1], cscol)
.. "\n"
.. table.concat(lines, "\n", 2, n - 1)
.. "\n"
.. string.sub(lines[n], 1, cecol)
end
return {
lines = lines,
selection = selection,
csrow = csrow,
cscol = cscol,
cerow = cerow,
cecol = cecol,
}
end
---@param anchor obsidian.note.HeaderAnchor
---@return string
util.format_anchor_label = function(anchor)
return string.format(" ❯ %s", anchor.header)
end
---@param opts { path: string, label: string, id: string|integer|?, anchor: obsidian.note.HeaderAnchor|?, block: obsidian.note.Block|? }
---@return string
util.wiki_link_alias_only = function(opts)
---@type string
local header_or_block = ""
if opts.anchor then
header_or_block = string.format("#%s", opts.anchor.header)
elseif opts.block then
header_or_block = string.format("#%s", opts.block.id)
end
return string.format("[[%s%s]]", opts.label, header_or_block)
end