<commit_before>local utils = require "utils"

-- ------------------
-- simple vi-mode
-- ------------------
local arrows = {
  h = 'left',
  j = 'down',
  k = 'up',
  l = 'right'
}
local enableSimpleViMode = function()
  for k, v in pairs(arrows) do
    utils.keymap(k, 'alt', v, nil)
    utils.keymap(k, 'alt+shift', v, 'alt')
    utils.keymap(k, 'alt+shift+ctrl', v, 'shift')
  end
end
local disableSimpleViMode = function()
  for k,v in pairs (arrows) do
    hs.hotkey.disableAll({'alt'}, k);
  end
end
utils.applyAppSpecific({'Emacs'}, disableSimpleViMode, nil, false)
utils.applyAppSpecific({'Emacs'}, enableSimpleViMode, nil, true)

-- ----------------------------
-- tab switching with Cmd++h/l
-- ----------------------------
local left_right = {h = '[', l = ']'}
local enableSimpleTabSwithing = function()
  for k, v in pairs(left_right) do
    utils.keymap(k, 'cmd', v, 'cmd+shift')
  end
end
local disableSimpleTabSwitching = function()
  for k, v in pairs(left_right) do
    hs.hotkey.disableAll({'cmd'}, k);
  end
end
local tabSwitchIn = {'Google Chrome', 'iTerm2'}
-- -- enables simple tab switching in listed apps, and ignores keybinding in others - Cmd-h/l can have different meaning in other apps
utils.applyAppSpecific(tabSwitchIn, enableSimpleTabSwithing, nil, nil)
utils.applyAppSpecific(tabSwitchIn, disableSimpleTabSwitching, nil, true)

--- setting conflicting Cmd+L (jump to address bar) keybinding to Cmd+Shift+L
utils.applyAppSpecific({'Google Chrome'},
  function()
    hs.hotkey.bind({'cmd', 'shift'}, 'l', function()
        local app = hs.window.focusedWindow():application()
        app:selectMenuItem({'File', 'Open Location…'})
    end)
  end, nil, nil)

-- ----------------------------
-- App switcher with Cmd++j/k
-- ----------------------------
switcher = hs.window.switcher.new(utils.globalfilter(),
                                  {textSize = 12,
                                   showTitles = false,
                                   showThumbnails = false,
                                   showSelectedTitle = false,
                                   selectedThumbnailSize = 640,
                                   backgroundColor = {0, 0, 0, 0}})

hs.hotkey.bind({'cmd'},'j', function() switcher:next() end)
hs.hotkey.bind({'cmd'},'k', function() switcher:previous() end)

function getChromeMenus()
  local app = hs.window.filter.new{'Google Chrome'}:getWindows()[1]:application()
  local mn =  app:findMenuItem({'File', 'Open Location…'})
  return app:selectMenuItem({'File', 'Open Location…'})
end<commit_msg>Using hs.application.watcher for appSpecific bindings<commit_after>local module = {}
local utils = require "utils"

-- ------------------
-- simple vi-mode
-- ------------------
local arrows = {
  h = 'left',
  j = 'down',
  k = 'up',
  l = 'right'
}
local enableSimpleViMode = function()
  for k, v in pairs(arrows) do
    utils.keymap(k, 'alt', v, nil)
    utils.keymap(k, 'alt+shift', v, 'alt')
    utils.keymap(k, 'alt+shift+ctrl', v, 'shift')
  end
end
local disableSimpleViMode = function()
  for k,v in pairs (arrows) do
    hs.hotkey.disableAll({'alt'}, k);
  end
end

-- ----------------------------
-- App switcher with Cmd++j/k
-- ----------------------------
switcher = hs.window.switcher.new(utils.globalfilter(),
                                  {textSize = 12,
                                   showTitles = false,
                                   showThumbnails = false,
                                   showSelectedTitle = false,
                                   selectedThumbnailSize = 640,
                                   backgroundColor = {0, 0, 0, 0}})

hs.hotkey.bind({'cmd'},'j', function() switcher:next() end)
hs.hotkey.bind({'cmd'},'k', function() switcher:previous() end)

-- ----------------------------
-- tab switching with Cmd++h/l
-- ----------------------------
local left_right = { h = "[", l = "]"}
local simpleTabSwitching = {}
for dir, key in pairs(left_right) do
  local tapFn = function() hs.eventtap.keyStroke({"shift", "cmd"}, key) end
  simpleTabSwitching[dir] = hs.hotkey.new({"cmd"}, dir, tapFn, nil, tapFn)
end
-- ------------------
-- App specific keybindings
-- ------------------
appSpecificKeys = {}

-- Given an app name and hs.hotkey, binds that hotkey when app activates
module.activateAppKey = function(app, hotkey)
  if not appSpecificKeys[app] then
    appSpecificKeys[app] = {}
  end
  for a, keys in pairs(appSpecificKeys) do
    if not keys[hotkey.idx] then
      keys[hotkey.idx] = hotkey
    end
    for idx, hk in pairs(keys) do
      if idx == hotkey.idx then
        hk:enable()
      end
    end
  end
end

-- Disables specific hotkeys for a given app name
module.deactivateAppKeys = function(app)
  for a, keys in pairs(appSpecificKeys) do
    if a == app then
      for _,hk in pairs(keys) do
        hk:disable()
      end
    end
  end
end

module.appSpecific = {
  ["*"] = {
    activated = function() enableSimpleViMode() end
  },
  ["Emacs"] = {
    activated = function() disableSimpleViMode() end
  },
  ["Google Chrome"] = {
    activated = function()
      --- setting conflicting Cmd+L (jump to address bar) keybinding to Cmd+Shift+L
      local hk = hs.hotkey.new({'cmd', 'shift'}, 'l', function()
          local app = hs.window.focusedWindow():application()
          app:selectMenuItem({'File', 'Open Location…'})
      end)
      module.activateAppKey("Google Chrome", hk)

      for k, hk in pairs(simpleTabSwitching) do
        module.activateAppKey("Google Chrome", hk)
      end
    end,
    deactivated = function() module.deactivateAppKeys("Google Chrome") end
  },
  ["iTerm2"] = {
    activated = function()
      for k, hk in pairs(simpleTabSwitching) do
        module.activateAppKey("iTerm2", hk)
      end
    end,
    deactivated = function() module.deactivateAppKeys("iTerm2") end
  }
}

-- Creates a new watcher and runs all the functions for specific `appName` and `events`
-- listed in the module in `module.appSpecific`
hs.application.watcher.new(
  function(appName, event, appObj)
    for app, modes in pairs(module.appSpecific) do
      if app == appName or app == "*" then
        for mode, fn in pairs(modes) do
          if event == hs.application.watcher[mode] then fn() end
        end
      end
    end
  end
):start()

return module
{'fraction_non_alphanumeric': 0.18187579214195185, 'fraction_numerical': 0.003643852978453739, 'mean_word_length': 2.224208375893769, 'pattern_counts': {'":': 0, '<': 3, '<?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': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6109498', 'n_tokens_mistral': 1983, 'n_tokens_neox': 1911, 'n_words': 528}