Class: Zoom::Editor

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

Instance Method Summary collapse

Constructor Details

#initialize(editor) ⇒ Editor



45
46
47
48
49
50
51
52
# File 'lib/zoom/editor.rb', line 45

def initialize(editor)
    @editor, _, @flags = editor.partition(" ") if (editor)
    if (editor.nil?)
        @editor = ENV["EDITOR"] || "vim"
        @editor= "vi" if (ScoobyDoo.where_are_you(@editor).nil?)
        @flags = ""
    end
end

Instance Method Details

#open(results) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/zoom/editor.rb', line 54

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

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

#vim(results) ⇒ Object



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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/zoom/editor.rb', line 85

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|
        if (result.grep_like?)
            if (result.filename.start_with?("/"))
                filename = result.filename
            else
                filename = result.pwd + "/" + result.filename
            end
        else
            if (result.contents.start_with?("/"))
                filename = result.contents
            else
                filename = result.pwd + "/" + result.contents
            end
        end

        lineno = result.lineno
        match = result.match

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

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

    zq.close
    zs.close

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

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