<commit_before>
# --------------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# <<EMAIL>> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return -<NAME>
# --------------------------------------------------------------------------------

import gtk
import pango
import gtksourceview2
from datetime import datetime

import Preferences


class TexPane:

	def __init__(self, config):
		self.config = config
		self.editorbuffer = gtksourceview2.Buffer()
		self.manager = gtksourceview2.LanguageManager()
		self.language = self.manager.get_language("latex")
		self.editorbuffer.set_language(self.language)
		self.editorbuffer.set_highlight_matching_brackets(True)
		self.editorbuffer.set_highlight_syntax(True)
		self.editorviewer = gtksourceview2.View(self.editorbuffer)
		self.editorviewer.modify_font(pango.FontDescription("monospace 10"))

		self.editorviewer.set_show_line_numbers(config.get_bool("tex_linenumbers"))
		self.editorviewer.set_highlight_current_line(config.get_bool("tex_highlighting"))
		self.editorviewer.set_wrap_mode(self.grab_wrapmode())

		self.start_iter = None
		self.current_iter = None
		self.textchange = datetime.now()
		self.prevchange = datetime.now()
		self.check_text_change()
		
		self.editorviewer.connect("key-press-event", self.set_text_change,)
		self.editorbuffer.set_modified(False)

	def fill_buffer(self, text):
		self.editorbuffer.set_text("")
		self.editorbuffer.begin_not_undoable_action()
		self.editorviewer.set_sensitive(False)
		start = self.editorbuffer.get_start_iter()
		self.editorbuffer.insert(start, text)		
		self.editorbuffer.set_modified(False)		
		self.editorviewer.set_sensitive(True)
		self.editorbuffer.end_not_undoable_action()

	def grab_buffer(self):
		buff = self.editorviewer.get_buffer()
		self.editorviewer.set_sensitive(False)
		content = buff.get_text(buff.get_start_iter(), buff.get_end_iter())
		self.editorviewer.set_sensitive(True)
		buff.set_modified(False)
		return content

	def insert_package(self, package, current_iter):
		pkgspace = "\\begin{document}"		
		start_iter = self.editorbuffer.get_start_iter()
		begin_iter, end = gtksourceview2.iter_forward_search(start_iter, pkgspace, flags=0, limit=None)
		pkgsearchstr = "{" + package + "}"
		if gtksourceview2.iter_forward_search(start_iter, pkgsearchstr, flags=0, limit=begin_iter):
			return
		else:
			self.editorbuffer.insert(begin_iter, "\\usepackage{" + package + "}\n")

	def set_selection_textstyle(self, widget):
		try: # maybe migrate tag insertion to own class soon
			ins = self.editorbuffer.get_selection_bounds()[0]
			if widget.get_name() == "tool_bold":
				self.editorbuffer.insert(ins, "\\textbf{")
			if widget.get_name() == "tool_italic":
				self.editorbuffer.insert(ins, "\\textit{")
			if widget.get_name() == "tool_unline":
				self.editorbuffer.insert(ins, "\\underline{")
			end = self.editorbuffer.get_selection_bounds()[1]
			self.editorbuffer.insert(end, "}")
			self.textchange = datetime.now()
		except:	return

	def set_selection_textindent(self, widget):
		try: # maybe migrate tag insertion to own class soon
			ins = self.editorbuffer.get_selection_bounds()[0]
			if widget.get_name() == "tool_left":
				self.editorbuffer.insert(ins, "\\begin{flushleft}")
				end = self.editorbuffer.get_selection_bounds()[1]
				self.editorbuffer.insert(end, "\\end{flushleft}")
			elif widget.get_name() == "tool_center":
				self.editorbuffer.insert(ins, "\\begin{center}")
				end = self.editorbuffer.get_selection_bounds()[1]
				self.editorbuffer.insert(end, "\\end{center}")
			elif widget.get_name() == "tool_right":
				self.editorbuffer.insert(ins, "\\begin{flushright}")
				end = self.editorbuffer.get_selection_bounds()[1]
				self.editorbuffer.insert(end, "\\end{flushright}")			
			self.textchange = datetime.now()
		except:	return

	def get_current_position(self):
		return self.editorbuffer.get_iter_at_mark(self.editorbuffer.get_insert())


	def start_searchfunction(self):
		self.start_iter = self.editorbuffer.get_start_iter()<commit_msg>fixed gtksourceview critical warning on tag insertion<commit_after>
# --------------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# <<EMAIL>> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return -<NAME>
# --------------------------------------------------------------------------------

import gtk
import pango
import gtksourceview2
from datetime import datetime

import Preferences


class TexPane:

	def __init__(self, config):
		self.config = config
		self.editorbuffer = gtksourceview2.Buffer()
		self.manager = gtksourceview2.LanguageManager()
		self.language = self.manager.get_language("latex")
		self.editorbuffer.set_language(self.language)
		self.editorbuffer.set_highlight_matching_brackets(True)
		self.editorbuffer.set_highlight_syntax(True)
		self.editorviewer = gtksourceview2.View(self.editorbuffer)
		self.editorviewer.modify_font(pango.FontDescription("monospace 10"))

		self.editorviewer.set_show_line_numbers(config.get_bool("tex_linenumbers"))
		self.editorviewer.set_highlight_current_line(config.get_bool("tex_highlighting"))
		self.editorviewer.set_wrap_mode(self.grab_wrapmode())

		self.start_iter = None
		self.current_iter = None
		self.textchange = datetime.now()
		self.prevchange = datetime.now()
		self.check_text_change()
		
		self.editorviewer.connect("key-press-event", self.set_text_change,)
		self.editorbuffer.set_modified(False)

	def fill_buffer(self, text):
		self.editorbuffer.set_text("")
		self.editorbuffer.begin_not_undoable_action()
		self.editorviewer.set_sensitive(False)
		start = self.editorbuffer.get_start_iter()
		self.editorbuffer.insert(start, text)		
		self.editorbuffer.set_modified(False)		
		self.editorviewer.set_sensitive(True)
		self.editorbuffer.end_not_undoable_action()

	def grab_buffer(self):
		buff = self.editorviewer.get_buffer()
		self.editorviewer.set_sensitive(False)
		content = buff.get_text(buff.get_start_iter(), buff.get_end_iter())
		self.editorviewer.set_sensitive(True)
		buff.set_modified(False)
		return content

	def insert_package(self, package, current_iter):
		pkgspace = "\\begin{document}"		
		start_iter = self.editorbuffer.get_start_iter()
		begin_iter, end = gtksourceview2.iter_forward_search(start_iter, pkgspace, flags=0, limit=None)
		pkgsearchstr = "{" + package + "}"
		if gtksourceview2.iter_forward_search(start_iter, pkgsearchstr, flags=0, limit=begin_iter):
			return
		else:
			self.editorbuffer.insert(begin_iter, "\\usepackage{" + package + "}\n")

	def set_selection_textstyle(self, widget):
		try: # maybe migrate tag insertion to own class soon
			self.editorbuffer.begin_user_action()
			ins = self.editorbuffer.get_selection_bounds()[0]
			if widget.get_name() == "tool_bold":
				self.editorbuffer.insert(ins, "\\textbf{")
			if widget.get_name() == "tool_italic":
				self.editorbuffer.insert(ins, "\\textit{")
			if widget.get_name() == "tool_unline":
				self.editorbuffer.insert(ins, "\\underline{")
			end = self.editorbuffer.get_selection_bounds()[1]
			self.editorbuffer.insert(end, "}")
			self.editorbuffer.end_user_action()
			self.textchange = datetime.now()
		except:	return

	def set_selection_textindent(self, widget):
		try: # maybe migrate tag insertion to own class soon
			self.editorbuffer.begin_user_action()
			ins = self.editorbuffer.get_selection_bounds()[0]
			if widget.get_name() == "tool_left":
				self.editorbuffer.insert(ins, "\\begin{flushleft}")
				end = self.editorbuffer.get_selection_bounds()[1]
				self.editorbuffer.insert(end, "\\end{flushleft}")
			elif widget.get_name() == "tool_center":
				self.editorbuffer.insert(ins, "\\begin{center}")
				end = self.editorbuffer.get_selection_bounds()[1]
				self.editorbuffer.insert(end, "\\end{center}")
			elif widget.get_name() == "tool_right":
				self.editorbuffer.insert(ins, "\\begin{flushright}")
				end = self.editorbuffer.get_selection_bounds()[1]
				self.editorbuffer.insert(end, "\\end{flushright}")
			self.editorbuffer.end_user_action()			
			self.textchange = datetime.now()
		except:	return

	def get_current_position(self):
		return self.editorbuffer.get_iter_at_mark(self.editorbuffer.get_insert())


	def start_searchfunction(self):
		self.start_iter = self.editorbuffer.get_start_iter()
{'fraction_non_alphanumeric': 0.15656213704994193, 'fraction_numerical': 0.004181184668989547, 'mean_word_length': 4.215626892792248, 'pattern_counts': {'":': 12, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6226002', 'n_tokens_mistral': 3033, 'n_tokens_neox': 2716, 'n_words': 481}