<commit_before>#!/usr/bin/env ruby

require 'erb'
require 'json'

def deepcopy(data)
  Marshal.load(Marshal.dump(data))
end

def to_array(data)
  unless data.is_a? Array
    data = [data]
  end
  data
end

def make_data(data, as_json=true)
  if as_json
    JSON.generate(data)
  else
    data
  end
end

def key(data, key)
  if key == "any"
    data['any'] = "key_code"
  elsif key.start_with? "button"
    data['pointing_button'] = key
  else
    data['key_code'] = key
  end
end

def from(key_code, mandatory_modifiers=[], optional_modifiers=[], as_json=true)
  mandatory_modifiers = to_array(mandatory_modifiers)
  optional_modifiers = to_array(optional_modifiers)

  data = {}
  key(data, key_code)
  mandatory_modifiers.each do |m|
    data['modifiers'] = {} if data['modifiers'].nil?
    data['modifiers']['mandatory'] = [] if data['modifiers']['mandatory'].nil?
    data['modifiers']['mandatory'] << m
  end
  optional_modifiers.each do |m|
    data['modifiers'] = {} if data['modifiers'].nil?
    data['modifiers']['optional'] = [] if data['modifiers']['optional'].nil?
    data['modifiers']['optional'] << m
  end
  make_data(data, as_json)
end

def hash_from(key_code, mandatory_modifiers=[], optional_modifiers=[])
  from(key_code, mandatory_modifiers, optional_modifiers, false)
end

def to(events, as_json=true, repeat=1)
  data = []
  events.each do |e|
    d = {}
    if e.is_a? Array
      key(d, e[0])
      unless e[1].nil?
        d['modifiers'] = e[1]
      end
    elsif e.is_a? String
      key(d, e)
    else
      d = deepcopy(e)
    end
    data << d
  end
  data_total = []
  repeat.times do |i|
    data_total += data
  end
  make_data(data_total, as_json)
end

def hash_to(events, repeat=1)
  to(events, false, repeat)
end

def each_key(source_keys_list: :source_keys_list, dest_keys_list: :dest_keys_list, from_mandatory_modifiers: [], from_optional_modifiers: [], to_pre_events: [], to_modifiers: [], to_post_events: [], to_if_alone: [], to_after_key_up: [], conditions: [], as_json: false)
  unless source_keys_list.is_a? Array
    source_keys_list = [source_keys_list]
    dest_keys_list = [dest_keys_list]
    to_if_alone = [to_array(to_if_alone)]
  end
  data = []
  source_keys_list.each_with_index do |from_key, index|
    to_key = dest_keys_list[index]
    d = {}
    d['type'] = 'basic'
    if from_key.is_a? String
      d['from'] = from(from_key, from_mandatory_modifiers, from_optional_modifiers, false)
    else
      d['from'] = from_key
    end

    # Compile list of events to add to "to" section
    events = []
    to_pre_events.each do |e|
      events << e
    end
    if to_key.is_a? String
      if to_modifiers[0].nil?
        events << [to_key]
      else
        events << [to_key, to_modifiers]
      end
    elsif to_key.is_a? Array
      to_key.each do |e|
        events << e
      end
    else
      events << to_key
    end
    to_post_events.each do |e|
      events << e
    end
    d['to'] = hash_to(events)
    d['to_if_alone'] = to_if_alone[index] if (to_if_alone[index] and to_if_alone[index].size != 0)
    d['to_after_key_up'] = to_after_key_up unless to_after_key_up.size == 0

    if conditions.any?
      d['conditions'] = []
      conditions.each do |c|
        d['conditions'] << c
      end
    end
    data << d
  end

  make_data(data, as_json)
end

def frontmost_application(type, app_aliases, as_json=true)
  finder_bundle_identifiers = [
    '^com\.apple\.finder$',
  ]

  browser_bundle_identifiers = [
    '^org\.mozilla\.firefox$',
    '^com\.google\.Chrome$',
    '^com\.apple\.Safari$',
  ]

  emacs_bundle_identifiers = [
    '^org\.gnu\.Emacs$',
    '^org\.gnu\.AquamacsEmacs$',
    '^org\.gnu\.Aquamacs$',
    '^org\.pqrs\.unknownapp.conkeror$',
  ]

  remote_desktop_bundle_identifiers = [
    '^com\.microsoft\.rdc$',
    '^com\.microsoft\.rdc\.mac$',
    '^com\.microsoft\.rdc\.osx\.beta$',
    '^net\.sf\.cord$',
    '^com\.thinomenon\.RemoteDesktopConnection$',
    '^com\.itap-mobile\.qmote$',
    '^com\.nulana\.remotixmac$',
    '^com\.p5sys\.jump\.mac\.viewer$',
    '^com\.p5sys\.jump\.mac\.viewer\.web$',
    '^com\.vmware\.horizon$',
    '^com\.2X\.Client\.Mac$',
  ]

  iterm2_bundle_identifiers = [
    '^com\.googlecode\.iterm2$',
  ]
  # TerminalとiTerm2で異なる扱いをしたいので、「ターミナル関係アプリ全部を一括まとめて設定」ではなく、
  # iTerm2以外のアプリを設定する。
  # terminal_bundle_identifiers = iterm2_bundle_identifiers + [
  #   '^com\.apple\.Terminal$',
  #   '^co\.zeit\.hyperterm$',
  #   '^co\.zeit\.hyper$',
  # ]
  terminal_bundle_identifiers = [
    '^com\.apple\.Terminal$',
    '^co\.zeit\.hyperterm$',
    '^co\.zeit\.hyper$',
  ]

  vi_bundle_identifiers = [<commit_msg>Add 'Microsoft Remote Desktop App beta' to 'remote_desktop_bundle_identifiers'<commit_after>#!/usr/bin/env ruby

require 'erb'
require 'json'

def deepcopy(data)
  Marshal.load(Marshal.dump(data))
end

def to_array(data)
  unless data.is_a? Array
    data = [data]
  end
  data
end

def make_data(data, as_json=true)
  if as_json
    JSON.generate(data)
  else
    data
  end
end

def key(data, key)
  if key == "any"
    data['any'] = "key_code"
  elsif key.start_with? "button"
    data['pointing_button'] = key
  else
    data['key_code'] = key
  end
end

def from(key_code, mandatory_modifiers=[], optional_modifiers=[], as_json=true)
  mandatory_modifiers = to_array(mandatory_modifiers)
  optional_modifiers = to_array(optional_modifiers)

  data = {}
  key(data, key_code)
  mandatory_modifiers.each do |m|
    data['modifiers'] = {} if data['modifiers'].nil?
    data['modifiers']['mandatory'] = [] if data['modifiers']['mandatory'].nil?
    data['modifiers']['mandatory'] << m
  end
  optional_modifiers.each do |m|
    data['modifiers'] = {} if data['modifiers'].nil?
    data['modifiers']['optional'] = [] if data['modifiers']['optional'].nil?
    data['modifiers']['optional'] << m
  end
  make_data(data, as_json)
end

def hash_from(key_code, mandatory_modifiers=[], optional_modifiers=[])
  from(key_code, mandatory_modifiers, optional_modifiers, false)
end

def to(events, as_json=true, repeat=1)
  data = []
  events.each do |e|
    d = {}
    if e.is_a? Array
      key(d, e[0])
      unless e[1].nil?
        d['modifiers'] = e[1]
      end
    elsif e.is_a? String
      key(d, e)
    else
      d = deepcopy(e)
    end
    data << d
  end
  data_total = []
  repeat.times do |i|
    data_total += data
  end
  make_data(data_total, as_json)
end

def hash_to(events, repeat=1)
  to(events, false, repeat)
end

def each_key(source_keys_list: :source_keys_list, dest_keys_list: :dest_keys_list, from_mandatory_modifiers: [], from_optional_modifiers: [], to_pre_events: [], to_modifiers: [], to_post_events: [], to_if_alone: [], to_after_key_up: [], conditions: [], as_json: false)
  unless source_keys_list.is_a? Array
    source_keys_list = [source_keys_list]
    dest_keys_list = [dest_keys_list]
    to_if_alone = [to_array(to_if_alone)]
  end
  data = []
  source_keys_list.each_with_index do |from_key, index|
    to_key = dest_keys_list[index]
    d = {}
    d['type'] = 'basic'
    if from_key.is_a? String
      d['from'] = from(from_key, from_mandatory_modifiers, from_optional_modifiers, false)
    else
      d['from'] = from_key
    end

    # Compile list of events to add to "to" section
    events = []
    to_pre_events.each do |e|
      events << e
    end
    if to_key.is_a? String
      if to_modifiers[0].nil?
        events << [to_key]
      else
        events << [to_key, to_modifiers]
      end
    elsif to_key.is_a? Array
      to_key.each do |e|
        events << e
      end
    else
      events << to_key
    end
    to_post_events.each do |e|
      events << e
    end
    d['to'] = hash_to(events)
    d['to_if_alone'] = to_if_alone[index] if (to_if_alone[index] and to_if_alone[index].size != 0)
    d['to_after_key_up'] = to_after_key_up unless to_after_key_up.size == 0

    if conditions.any?
      d['conditions'] = []
      conditions.each do |c|
        d['conditions'] << c
      end
    end
    data << d
  end

  make_data(data, as_json)
end

def frontmost_application(type, app_aliases, as_json=true)
  finder_bundle_identifiers = [
    '^com\.apple\.finder$',
  ]

  browser_bundle_identifiers = [
    '^org\.mozilla\.firefox$',
    '^com\.google\.Chrome$',
    '^com\.apple\.Safari$',
  ]

  emacs_bundle_identifiers = [
    '^org\.gnu\.Emacs$',
    '^org\.gnu\.AquamacsEmacs$',
    '^org\.gnu\.Aquamacs$',
    '^org\.pqrs\.unknownapp.conkeror$',
  ]

  # Add 'Microsoft Remote Desktop App beta, 2017/10/08.'
  remote_desktop_bundle_identifiers = [
    '^com\.microsoft\.rdc$',
    '^com\.microsoft\.rdc\.mac$',
    '^com\.microsoft\.rdc\.osx\.beta$',
    '^net\.sf\.cord$',
    '^com\.thinomenon\.RemoteDesktopConnection$',
    '^com\.itap-mobile\.qmote$',
    '^com\.nulana\.remotixmac$',
    '^com\.p5sys\.jump\.mac\.viewer$',
    '^com\.p5sys\.jump\.mac\.viewer\.web$',
    '^com\.vmware\.horizon$',
    '^com\.2X\.Client\.Mac$',
    '^com\.microsoft\.rdc\.osx\.beta$',
  ]

  iterm2_bundle_identifiers = [
    '^com\.googlecode\.iterm2$',
  ]
  # TerminalとiTerm2で異なる扱いをしたいので、「ターミナル関係アプリ全部を一括まとめて設定」ではなく、
  # iTerm2以外のアプリを設定する。
  # terminal_bundle_identifiers = iterm2_bundle_identifiers + [
  #   '^com\.apple\.Terminal$',
  #   '^co\.zeit\.hyperterm$',
  #   '^co\.zeit\.hyper$',
  # ]
  terminal_bundle_identifiers = [
    '^com\.apple\.Terminal$',
    '^co\.zeit\.hyperterm$',
    '^co\.zeit\.hyper$',
  ]

  vi_bundle_identifiers = [
{'fraction_non_alphanumeric': 0.15918712955122777, 'fraction_numerical': 0.004233700254022015, 'mean_word_length': 3.1809734513274335, 'pattern_counts': {'":': 0, '<': 47, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '70653', 'n_tokens_mistral': 3847, 'n_tokens_neox': 3600, 'n_words': 633}