<commit_before>--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--           S H O R T C U T S    P R E F E R E N C E S    P A N E L          --
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

--- === plugins.core.preferences.panels.shortcuts ===
---
--- Shortcuts Preferences Panel

--------------------------------------------------------------------------------
--
-- EXTENSIONS:
--
--------------------------------------------------------------------------------
local log										= require("hs.logger").new("prefsShortcuts")

local fs										= require("hs.fs")
local image										= require("hs.image")
local inspect									= require("hs.inspect")
local keycodes									= require("hs.keycodes")
local timer										= require("hs.timer")
local toolbar                  					= require("hs.webview.toolbar")
local webview									= require("hs.webview")

local commands									= require("cp.commands")
local config									= require("cp.config")
local dialog									= require("cp.dialog")
local fcp										= require("cp.apple.finalcutpro")
local html										= require("cp.web.html")
local ui										= require("cp.web.ui")
local plist										= require("cp.plist")
local tools										= require("cp.tools")

local _											= require("moses")

--------------------------------------------------------------------------------
--
-- CONSTANTS:
--
--------------------------------------------------------------------------------
local DEFAULT_PRIORITY 							= 0
local DEFAULT_SHORTCUTS							= "Default Shortcuts"

--------------------------------------------------------------------------------
--
-- THE MODULE:
--
--------------------------------------------------------------------------------
local mod = {}

--------------------------------------------------------------------------------
-- SPLIT STRING:
--------------------------------------------------------------------------------
local function split(str, pat)
	local t = {}  -- NOTE: use {n = 0} in Lua-5.0
	local fpat = "(.-)" .. pat
	local last_end = 1
	local s, e, cap = str:find(fpat, 1)
	while s do
	  if s ~= 1 or cap ~= "" then
		 table.insert(t,cap)
	  end
	  last_end = e+1
	  s, e, cap = str:find(fpat, last_end)
	end
	if last_end <= #str then
	  cap = str:sub(last_end)
	  table.insert(t, cap)
	end
	return t
end

local function resetShortcuts()
	if dialog.displayYesNoQuestion(i18n("shortcutsResetConfirmation")) then
		-- Deletes the DEFAULT_SHORTCUTS, if present.
		local shortcutsFile = fs.pathToAbsolute(commands.getShortcutsPath(DEFAULT_SHORTCUTS))
		if shortcutsFile then
			log.df("Removing shortcuts file: '%s'", shortcutsFile)
			os.remove(shortcutsFile)
		end
		dialog.displayMessage(i18n("shortcutsResetComplete"), {"OK"})
		hs.reload()
	end
end

--------------------------------------------------------------------------------
-- CONTROLLER CALLBACK:
--------------------------------------------------------------------------------
local function controllerCallback(message)

	local body = message.body
	local action = body.action

	-- log.df("Callback message: %s", hs.inspect(message))

	-- TODO: Can this whole updateShortcut action be removed??

	if action == "updateShortcut" then
		--------------------------------------------------------------------------------<commit_msg>#733 Automatically deletes custom shortcuts when Trashing Prefs<commit_after>--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--           S H O R T C U T S    P R E F E R E N C E S    P A N E L          --
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

--- === plugins.core.preferences.panels.shortcuts ===
---
--- Shortcuts Preferences Panel

--------------------------------------------------------------------------------
--
-- EXTENSIONS:
--
--------------------------------------------------------------------------------
local log										= require("hs.logger").new("prefsShortcuts")

local fs										= require("hs.fs")
local image										= require("hs.image")
local inspect									= require("hs.inspect")
local keycodes									= require("hs.keycodes")
local timer										= require("hs.timer")
local toolbar                  					= require("hs.webview.toolbar")
local webview									= require("hs.webview")

local commands									= require("cp.commands")
local config									= require("cp.config")
local dialog									= require("cp.dialog")
local fcp										= require("cp.apple.finalcutpro")
local html										= require("cp.web.html")
local ui										= require("cp.web.ui")
local plist										= require("cp.plist")
local tools										= require("cp.tools")

local _											= require("moses")

--------------------------------------------------------------------------------
--
-- CONSTANTS:
--
--------------------------------------------------------------------------------
local DEFAULT_PRIORITY 							= 0
local DEFAULT_SHORTCUTS							= "Default Shortcuts"

--------------------------------------------------------------------------------
--
-- THE MODULE:
--
--------------------------------------------------------------------------------
local mod = {}

--------------------------------------------------------------------------------
-- SPLIT STRING:
--------------------------------------------------------------------------------
local function split(str, pat)
	local t = {}  -- NOTE: use {n = 0} in Lua-5.0
	local fpat = "(.-)" .. pat
	local last_end = 1
	local s, e, cap = str:find(fpat, 1)
	while s do
	  if s ~= 1 or cap ~= "" then
		 table.insert(t,cap)
	  end
	  last_end = e+1
	  s, e, cap = str:find(fpat, last_end)
	end
	if last_end <= #str then
	  cap = str:sub(last_end)
	  table.insert(t, cap)
	end
	return t
end

local function deleteShortcuts()
	-- Deletes the DEFAULT_SHORTCUTS, if present.
	local shortcutsFile = fs.pathToAbsolute(commands.getShortcutsPath(DEFAULT_SHORTCUTS))
	if shortcutsFile then
		local ok, err = os.remove(shortcutsFile)
		if not ok then
			log.ef("Unable to remove default shortcuts: %s", err)
			return false
		end
	end
	return true
end

local function resetShortcuts()
	if dialog.displayYesNoQuestion(i18n("shortcutsResetConfirmation")) then
		if deleteShortcuts() then
			dialog.displayMessage(i18n("shortcutsResetComplete"), {"OK"})
			hs.reload()
		end
	end
end

config.watch({
	reset = deleteShortcuts,
})

--------------------------------------------------------------------------------
-- CONTROLLER CALLBACK:
--------------------------------------------------------------------------------
local function controllerCallback(message)

	local body = message.body
	local action = body.action

	-- log.df("Callback message: %s", hs.inspect(message))

	-- TODO: Can this whole updateShortcut action be removed??

	if action == "updateShortcut" then
		--------------------------------------------------------------------------------
{'fraction_non_alphanumeric': 0.4168618266978923, 'fraction_numerical': 0.0037195205951232953, 'mean_word_length': 0.9458590190297508, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3431264', 'n_tokens_mistral': 2051, 'n_tokens_neox': 1691, 'n_words': 444}