Class: Zoom::Editor

Inherits:
Object
  • Object
show all
Defined in:
lib/zoom/editor.rb

Instance Method Summary collapse

Constructor Details

#initialize(editor) ⇒ Editor

Returns a new instance of Editor.



44
45
46
# File 'lib/zoom/editor.rb', line 44

def initialize(editor)
    @editor = editor
end

Instance Method Details

#open(results) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/zoom/editor.rb', line 48

def open(results)
    return if (results.nil? || results.empty?)

    case @editor
    when /vim?$/
        vim(results)
    else
        default(results)
    end
end

#vim(results) ⇒ Object



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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/zoom/editor.rb', line 73

def vim(results)
    quickfix = Pathname.new("~/.cache/zoom/quickfix").expand_path
    source = Pathname.new("~/.cache/zoom/source").expand_path
    FileUtils.mkdir_p(quickfix.dirname)

    zq = File.open(quickfix, "w")
    zs = File.open(source, "w")

    vimscript = [
        "augroup Zoom",
        "  autocmd!",
        "  autocmd FileType qf nnoremap <cr> :.cc<cr>:cclose<cr>",
        "augroup END",
        "",
        "nnoremap <leader>z :copen<cr>",
        "nnoremap zn :cn<cr>",
        "nnoremap zp :cp<cr>",
        ""
    ].join("\n")
    zs.write(vimscript)

    files = Array.new
    results.each do |result|
        filename = result.filename if (result.grep_like?)
        filename ||= result.contents
        lineno = result.lineno
        match = result.match
        pwd = result.pwd

        if (!files.include?("#{pwd}/#{filename}"))
            files.push("#{pwd}/#{filename}")
            zs.write("#{lineno}\nbnext\n") if (result.grep_like?)
        end

        if (result.grep_like?)
            zq.write("#{pwd}/#{filename}|#{lineno}| #{match}\n")
        end
    end
    zs.write("silent cfile #{quickfix}\n")

    zq.close
    zs.close

    system("#{@editor} -S #{source} #{files.join(" ")}")

    FileUtils.rm_f(quickfix)
    FileUtils.rm_f(source)
end