summaryrefslogtreecommitdiff
path: root/support/autolatex/plugins/sublime-text-2/utils/runner_command.py
blob: 7b602c4826d6b1526b9d338a50d1d49d9f5e4749 (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# autolatex/utils/runner_command.py
# Copyright (C) 2013  Stephane Galland <galland@arakhne.org>
#
# 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; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import os
import sublime
import utils, runner
import gettext

#---------------------------------
# INTERNATIONALIZATION
#---------------------------------

_T = gettext.gettext


class AbstractRunnerCommand(runner.Listener):

	def __init__(self):
		runner.Listener.__init__(self)
		self._thread = None
		self._show_progress = True

	def cancel_task(self):
		if self._thread:
				self._thread.cancel()

	def start_task(self, show_progress, encoding, working_dir, directive, params):
		self._encoding = encoding
		self._show_progress = show_progress

		# Default the to the current files directory if no working directory was given
		if (working_dir == "" and self.window.active_view()
			        and self.window.active_view().file_name()):
		    working_dir = os.path.dirname(self.window.active_view().file_name())

		if not hasattr(self, 'output_view'):
		    # Try not to call get_output_panel until the regexes are assigned
		    self.output_view = self.window.get_output_panel("autolatex")

		self.output_view.settings().set("result_file_regex", "^(.*?):([0-9]+):(?:([0-9]+):)?\\s*(.*?)\\s*$")
		self.output_view.settings().set("result_line_regex", "^l\\.([0-9]+)\\s+")
		self.output_view.settings().set("result_base_dir", working_dir)

		# Call get_output_panel a second time after assigning the above
		# settings, so that it'll be picked up as a result buffer
		self.window.get_output_panel("autolatex")

		# Show the progress
		if self._show_progress:
			sublime.status_message(_T("Building [%d%%]") % int(0))

		autolatex_directory = utils.find_AutoLaTeX_directory(working_dir)
		self._thread = runner.Runner(
				self,
				autolatex_directory,
				directive,
				params)
		self._thread.start()
		

	def get_runner_progress(self):
		return self._show_progress

	def on_runner_add_ui(self):
		sublime.status_message(_T("Building [%d%%]") % 0)
		show_panel_on_build = sublime.load_settings("Preferences.sublime-settings").get("show_panel_on_build", True)
		if show_panel_on_build:
			self.window.run_command("show_panel", {"panel": "output.autolatex"})

	def on_runner_remove_ui(self):
		pass

	def on_runner_progress(self, amount, comment):
		if comment:
			sublime.status_message(_T("Building [%d%%]: %s") % (int(amount * 100), comment))
		else:
			sublime.status_message(_T("Building [%d%%]") % int(amount * 100))

	def on_runner_finalize_execution(self, retcode, output, latex_warnings):
		messages = []
		if retcode != 0:
			messages.append(output)
		else:
			for warning in latex_warnings:
				messages.append(warning[1]+":1: "+warning[0])

		for message in messages:
			try:
			    message = message.decode(self._encoding)
			except:
			    message = _T("[Decode error - output not %s]") % self._encoding

			# Normalize newlines, Sublime Text always uses a single \n separator
			# in memory.
			message = message.replace('\r\n', '\n').replace('\r', '\n')

		        selection_was_at_end = (len(self.output_view.sel()) == 1
			    and self.output_view.sel()[0]
				== sublime.Region(self.output_view.size()))

			self.output_view.set_read_only(False)
			edit = self.output_view.begin_edit()
			self.output_view.insert(edit, self.output_view.size(), message)
			if selection_was_at_end:
			    self.output_view.show(self.output_view.size())
			self.output_view.end_edit(edit)
			self.output_view.set_read_only(True)

		if retcode!=0:
		    sublime.status_message(_T("Build finished with an error"))
		elif len(latex_warnings) == 0:
		    sublime.status_message(_T("Build finished"))
		else:
		    sublime.status_message(_T("Build finished with %d warnings") % len(latex_warnings))

		# Set the selection to the start, so that next_result will work as expected
		edit = self.output_view.begin_edit()
		self.output_view.sel().clear()
		self.output_view.sel().add(sublime.Region(0))
		self.output_view.end_edit(edit)