Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 103 lines (92 sloc) 3.236 kb
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
Vimdb.vim = 'mvim -v'
Vimdb.plugins_dir = '(plugins|autoload)'

# Additional commands for keys
class Vimdb::Runner
  option :list, :desc => 'only prints list of keys left'
  desc 'Determines keys left for a given key combo'
  def left(key_combo, options={})
    query = '^' + Regexp.escape(key_combo)
    query << '[a-z]' if query.end_with?('-')
    regex = Regexp.new query
    keys = Vimdb.user.items
    keys.select! {|e| e[:key] =~ regex }

    available = ('a' .. 'z').to_a
    if key_combo.end_with? '-'
      available.map! {|e| key_combo + e }
      available -= keys.map {|e| e[:key][/^\S+/] }.compact
    elsif key_combo[/-\S/]
      available.map! {|e| "#{key_combo} #{e}" }
      if !keys.empty? && keys.all? {|e| e[:key] == key_combo }
        abort "This key is a top level key"
      end
      available -= keys.map {|e| e[:key] }
    else
      abort "Invalid query. Query must be the beginning of a key combo" +
        " i.e. C- or C-a."
    end

    if options[:list]
      available << "No combinations left" if available.empty?
      puts available, "#{available.size} keys left"
    else
      keys += available.map {|key| {key: key, desc: '** AVAILABLE **'} }
      keys.sort_by! {|e| e[:key] }
      puts Hirb::Helpers::Table.render(keys,
       fields: [:key, :mode, :from, :desc], headers: {:desc => 'desc/action'})
    end
  end

  desc 'List conflict keys'
  def conflict
    keys = Vimdb.user.items
    defaults = keys.select {|e| e[:from] == 'default' }.
      each_with_object({}) {|e,t| t[e[:key]] = e }
    conflicts = []
    keys.each {|e|
      if e[:from] != 'default' && (default = defaults[e[:key]]) &&
          modes_are_same?(e, default)
        conflicts << default
        conflicts << e
      end
    }
    puts Hirb::Helpers::Table.render(conflicts,
      fields: [:key, :mode, :from, :desc], headers: {desc: 'desc/action'})
  end

  common_search_options
  desc 'Prints out item info in tab format for anki'
  def anki(query=nil, options={})
    require 'cgi'
    keys = Vimdb.user.search(query, options)
    keys.group_by {|e| e[:desc] }.each {|desc,common_keys|
      puts "#{CGI.escapeHTML(desc)}<br/>mode: #{common_keys.map {|e| e[:mode] }.uniq.join(',')}" +
        "\t" + CGI.escapeHTML(common_keys.map {|e| e[:key] }.uniq.join(', '))
    }
  end

  common_search_options
  option :tag, :required => true
  desc 'Tags a specific item with a tag'
  def tag(query, options={})
    items = Vimdb.user.search(query, options)
    if items.size > 1
      Hirb.enable
      items = Hirb::Menu.render(items, fields: Vimdb.item.fields)
    end
    items.each do |item|
      system('tag', 'add', "#{item[:key]}.#{item[:mode]}", '--tags', options[:tag])
    end
  end

  desc 'List items in tag'
  def tag_list(tag)
    ids = `tag list #{tag}`.split("\n")
    items = Vimdb.user.items
    keys = ids.map {|id| id.split('.') }.map do |(key, mode)|
      items.find {|item|
        item[:key] == key && item[:mode].include?(mode)
      } or raise "No key found for '#{key}.#{mode}'"
    end
    puts Hirb::Helpers::Table.render(keys, fields: Vimdb.item.fields)
  end

  private

  def modes_are_same?(key1, key2)
    (key1[:mode].split('') & key2[:mode].split('')).any?
  end
end

# vim: ft=ruby
Something went wrong with that request. Please try again.