-
Notifications
You must be signed in to change notification settings - Fork 7
/
history_labels.lua
65 lines (53 loc) · 2.32 KB
/
history_labels.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
--------------------------------------------------------------------------------
-- This script can automatically switch to different history files based on the
-- current directory. For example, you can configure it to use one history file
-- when in one git repo, another history file in another git repo, and yet
-- another history file in some other directory.
-- Set this in a Lua script to contain a list of pairs of directories and
-- history label qualifiers. When the current directory is within a listed
-- directory, then the script sets CLINK_HISTORY_LABEL to the associated history
-- label string. When the current directory is not within any listed directory,
-- then the script clears the CLINK_HISTORY_LABEL environment variable (but only
-- if the script has previously set it).
-- luacheck: globals history_label_dirs
history_label_dirs = history_label_dirs or {}
-- Example:
-- history_label_dirs = {
-- { "c:/repos/clink", "Clink Repo" },
-- { "c:/repos/workcode", "Work Repo" },
-- }
if not unicode or not unicode.normalize then
log.info("history_labels.lua requires a newer version of Clink; please upgrade.")
return
end
local using_label
local label_dir
local function select_label()
-- Get the current directory.
local cwd = unicode.normalize(3, string.lower(path.join(os.getcwd(), "")))
-- Find a label associated with the current directory.
local label
for _,x in ipairs(history_label_dirs) do
local candidate = unicode.normalize(3, string.lower(path.join(path.normalise(x[1]), "")))
if cwd:find(candidate, 1, true--[[plain]]) == 1 then
label = x[2]
label_dir = candidate
break
end
end
-- If no label and we haven't set a label, then there's nothing to do.
if not label and not using_label then
return
end
-- Apply the label. If there's no label, this reverts to the profile's
-- history file.
using_label = label
os.setenv("CLINK_HISTORY_LABEL", label)
-- Let other scripts know the directory that determined the history label.
os.setenv("=history_labels_dir", label_dir or "")
end
local cwdhistory_restore = settings.get("cwdhistory.restore")
if cwdhistory_restore and not os.getenv("=cwdhistory_injected") then
select_label()
end
clink.onbeginedit(select_label)