summaryrefslogtreecommitdiffstats
path: root/plugins/externaltools/tools/functions.py
blob: bc755be8a01e5fa784ab84296023282dc3628ca6 (plain)
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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# -*- coding: utf-8 -*-
#    Gedit External Tools plugin
#    Copyright (C) 2005-2006  Steve Frécinaux <steve@istique.net>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os
from gi.repository import Gio, Gtk, Gdk, GtkSource, Gedit
from .capture import *

try:
    import gettext
    gettext.bindtextdomain('gedit')
    gettext.textdomain('gedit')
    _ = gettext.gettext
except:
    _ = lambda s: s

def default(val, d):
    if val is not None:
        return val
    else:
        return d


def current_word(document):
    piter = document.get_iter_at_mark(document.get_insert())
    start = piter.copy()

    if not piter.starts_word() and (piter.inside_word() or piter.ends_word()):
        start.backward_word_start()

    if not piter.ends_word() and piter.inside_word():
        piter.forward_word_end()

    return (start, piter)


def file_browser_root(window):
    bus = window.get_message_bus()

    if bus.is_registered('/plugins/filebrowser', 'get_root'):
        msg = bus.send_sync('/plugins/filebrowser', 'get_root')

        if msg:
            browser_root = msg.props.location

            if browser_root and browser_root.is_native():
                return browser_root.get_path()

    return None


# ==== Capture related functions ====
def run_external_tool(window, panel, node):
    # Configure capture environment
    try:
        cwd = os.getcwd()
    except OSError:
        cwd = os.getenv('HOME')

    capture = Capture(node.command, cwd)
    capture.env = os.environ.copy()
    capture.set_env(GEDIT_CWD=cwd)

    view = window.get_active_view()
    document = None

    if view is not None:
        # Environment vars relative to current document
        document = view.get_buffer()
        location = document.get_file().get_location()

        # Current line number
        piter = document.get_iter_at_mark(document.get_insert())
        capture.set_env(GEDIT_CURRENT_LINE_NUMBER=str(piter.get_line() + 1))

        # Current line text
        piter.set_line_offset(0)
        end = piter.copy()

        if not end.ends_line():
            end.forward_to_line_end()

        capture.set_env(GEDIT_CURRENT_LINE=piter.get_text(end))

        if document.get_language() is not None:
            capture.set_env(GEDIT_CURRENT_DOCUMENT_LANGUAGE=document.get_language().get_id())

        # Selected text (only if input is not selection)
        if node.input != 'selection' and node.input != 'selection-document':
            bounds = document.get_selection_bounds()

            if bounds:
                capture.set_env(GEDIT_SELECTED_TEXT=bounds[0].get_text(bounds[1]))

        bounds = current_word(document)
        capture.set_env(GEDIT_CURRENT_WORD=bounds[0].get_text(bounds[1]))

        capture.set_env(GEDIT_CURRENT_DOCUMENT_TYPE=document.get_mime_type())

        if location is not None:
            scheme = location.get_uri_scheme()
            name = location.get_basename()
            capture.set_env(GEDIT_CURRENT_DOCUMENT_URI=location.get_uri(),
                            GEDIT_CURRENT_DOCUMENT_NAME=name,
                            GEDIT_CURRENT_DOCUMENT_SCHEME=scheme)
            if location.has_uri_scheme('file'):
                path = location.get_path()
                cwd = os.path.dirname(path)
                capture.set_cwd(cwd)
                capture.set_env(GEDIT_CURRENT_DOCUMENT_PATH=path,
                                GEDIT_CURRENT_DOCUMENT_DIR=cwd)

        documents_location = [doc.get_file().get_location()
                              for doc in window.get_documents()
                              if doc.get_file().get_location() is not None]
        documents_uri = [location.get_uri()
                         for location in documents_location
                         if location.get_uri() is not None]
        documents_path = [location.get_path()
                          for location in documents_location
                          if location.has_uri_scheme('file')]
        capture.set_env(GEDIT_DOCUMENTS_URI=' '.join(documents_uri),
                        GEDIT_DOCUMENTS_PATH=' '.join(documents_path))

    # set file browser root env var if possible
    browser_root = file_browser_root(window)
    if browser_root:
        capture.set_env(GEDIT_FILE_BROWSER_ROOT=browser_root)

    flags = capture.CAPTURE_BOTH

    if not node.has_hash_bang():
        flags |= capture.CAPTURE_NEEDS_SHELL

    capture.set_flags(flags)

    # Get input text
    input_type = node.input
    output_type = node.output

    # Clear the panel
    panel.clear()

    if output_type == 'output-panel':
        panel.show()

    # Assign the error output to the output panel
    panel.set_process(capture)

    if input_type != 'nothing' and view is not None:
        if input_type == 'document':
            start, end = document.get_bounds()
        elif input_type == 'selection' or input_type == 'selection-document':
            try:
                start, end = document.get_selection_bounds()
            except ValueError:
                if input_type == 'selection-document':
                    start, end = document.get_bounds()

                    if output_type == 'replace-selection':
                        document.select_range(start, end)
                else:
                    start = document.get_iter_at_mark(document.get_insert())
                    end = start.copy()

        elif input_type == 'line':
            start = document.get_iter_at_mark(document.get_insert())
            end = start.copy()
            if not start.starts_line():
                start.set_line_offset(0)
            if not end.ends_line():
                end.forward_to_line_end()
        elif input_type == 'word':
            start = document.get_iter_at_mark(document.get_insert())
            end = start.copy()
            if not start.inside_word():
                panel.write(_('You must be inside a word to run this command'),
                            panel.error_tag)
                return
            if not start.starts_word():
                start.backward_word_start()
            if not end.ends_word():
                end.forward_word_end()

        input_text = document.get_text(start, end, False)
        capture.set_input(input_text)

    # Assign the standard output to the chosen "file"
    if output_type == 'new-document':
        tab = window.create_tab(True)
        view = tab.get_view()
        document = tab.get_document()
        pos = document.get_start_iter()
        capture.connect('stdout-line', capture_stdout_line_document, document, pos)
        document.begin_user_action()
        view.set_editable(False)
        view.set_cursor_visible(False)
    elif output_type != 'output-panel' and output_type != 'nothing' and view is not None:
        document.begin_user_action()
        view.set_editable(False)
        view.set_cursor_visible(False)

        if output_type.startswith('replace-'):
            if output_type == 'replace-selection':
                try:
                    start_iter, end_iter = document.get_selection_bounds()
                except ValueError:
                    start_iter = document.get_iter_at_mark(document.get_insert())
                    end_iter = start_iter.copy()
            elif output_type == 'replace-document':
                start_iter, end_iter = document.get_bounds()
            capture.connect('stdout-line', capture_delayed_replace,
                            document, start_iter, end_iter)
        else:
            if output_type == 'insert':
                pos = document.get_iter_at_mark(document.get_insert())
            else:
                pos = document.get_end_iter()
            capture.connect('stdout-line', capture_stdout_line_document, document, pos)
    elif output_type != 'nothing':
        capture.connect('stdout-line', capture_stdout_line_panel, panel)

        if not document is None:
            document.begin_user_action()

    capture.connect('stderr-line', capture_stderr_line_panel, panel)
    capture.connect('begin-execute', capture_begin_execute_panel, panel, view, node.name)
    capture.connect('end-execute', capture_end_execute_panel, panel, view, output_type)

    # Run the command
    capture.execute()

    if output_type != 'nothing':
        if not document is None:
            document.end_user_action()

class MultipleDocumentsSaver:
    def __init__(self, window, panel, all_docs, node):
        self._window = window
        self._panel = panel
        self._node = node

        if all_docs:
            docs = window.get_documents()
        else:
            docs = [window.get_active_document()]

        self._docs_to_save = [doc for doc in docs if doc.get_modified()]
        self.save_next_document()

    def save_next_document(self):
        if len(self._docs_to_save) == 0:
            # The documents are saved, we can run the tool.
            run_external_tool(self._window, self._panel, self._node)
        else:
            next_doc = self._docs_to_save[0]
            self._docs_to_save.remove(next_doc)

            Gedit.commands_save_document_async(next_doc,
                                               self._window,
                                               None,
                                               self.on_document_saved,
                                               None)

    def on_document_saved(self, doc, result, user_data):
        saved = Gedit.commands_save_document_finish(doc, result)
        if saved:
            self.save_next_document()


def capture_menu_action(action, parameter, window, panel, node):
    if node.save_files == 'document' and window.get_active_document():
        MultipleDocumentsSaver(window, panel, False, node)
        return
    elif node.save_files == 'all':
        MultipleDocumentsSaver(window, panel, True, node)
        return

    run_external_tool(window, panel, node)


def capture_stderr_line_panel(capture, line, panel):
    if not panel.visible():
        panel.show()

    panel.write(line, panel.error_tag)


def capture_begin_execute_panel(capture, panel, view, label):
    if view:
        view.get_window(Gtk.TextWindowType.TEXT).set_cursor(Gdk.Cursor.new(Gdk.CursorType.WATCH))

    panel['stop'].set_sensitive(True)
    panel.clear()
    panel.write(_("Running tool:"), panel.italic_tag)
    panel.write(" %s\n\n" % label, panel.bold_tag)


def capture_end_execute_panel(capture, exit_code, panel, view, output_type):
    panel['stop'].set_sensitive(False)

    if view:
        if output_type in ('new-document', 'replace-document'):
            doc = view.get_buffer()
            start = doc.get_start_iter()
            end = start.copy()
            end.forward_chars(300)
            uri = ''

            mtype, uncertain = Gio.content_type_guess(None, doc.get_text(start, end, False).encode('utf-8'))
            lmanager = GtkSource.LanguageManager.get_default()

            location = doc.get_file().get_location()
            if location:
                uri = location.get_uri()
            language = lmanager.guess_language(uri, mtype)

            if language is not None:
                doc.set_language(language)

        view.get_window(Gtk.TextWindowType.TEXT).set_cursor(Gdk.Cursor.new(Gdk.CursorType.XTERM))
        view.set_cursor_visible(True)
        view.set_editable(True)

    if exit_code == 0:
        panel.write("\n" + _("Done.") + "\n", panel.italic_tag)
    else:
        panel.write("\n" + _("Exited") + ":", panel.italic_tag)
        panel.write(" %d\n" % exit_code, panel.bold_tag)


def capture_stdout_line_panel(capture, line, panel):
    panel.write(line)


def capture_stdout_line_document(capture, line, document, pos):
    document.insert(pos, line)


def capture_delayed_replace(capture, line, document, start_iter, end_iter):
    document.delete(start_iter, end_iter)

    # Must be done after deleting the text
    pos = document.get_iter_at_mark(document.get_insert())

    capture_stdout_line_document(capture, line, document, pos)

    capture.disconnect_by_func(capture_delayed_replace)
    capture.connect('stdout-line', capture_stdout_line_document, document, pos)

# ex:ts=4:et: