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.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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

    # In case of vim server functionality
    @vimserver = ""
    @flags.match(/--servername\s+["']?([^"' ]+)/) do |m|
        @vimserver = m[1]
    end

    # In case of vim remote functionality
    if (!@vimserver.empty?)
        @flags.match(/(--remote(-tab)?(-wait)?(-silent)?)/) do |m|
            @flags.gsub!(/#{m[1]}/, "")
        end
    end
end

Instance Method Details

#open(results) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/zoom/editor.rb', line 67

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

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

#vim(results) ⇒ Object



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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/zoom/editor.rb', line 98

def vim(results)
    already_started = vim_remote_started?

    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) if (!already_started)

    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("edit! #{filename}\n")
            zs.write("#{lineno}\nbnext\n") if (result.grep_like?)
        end

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

    if (!already_started)
        zs.write("silent cfile #{quickfix}\n")
    else
        results.each do |result|
            zs.write("bprev\n") if (result.grep_like?)
        end
        zs.write("silent caddf #{quickfix}\n")
    end

    zq.close
    zs.close

    if (!already_started)
        system("#{@editor} -c \"source #{source}\" #{@flags}")
        FileUtils.rm_f(quickfix)
        FileUtils.rm_f(source)
    else
        system(
            [
                @editor,
                @flags,
                "--remote-send \":source #{source}\n\""
            ].join(" ")
        )
    end
end