1
# -*- coding: utf-8 -*-
2
# Copyright © 2005 Lateef Alabi-Oki
4
# This file is part of Scribes.
6
# Scribes is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
11
# Scribes is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with Scribes; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
Provide syntax highlighting for gtksourceview.Buffer objects.
23
@author: Lateef Alabi-Oki
24
@organiation: The Scribes Project
25
@copyright: Copyright © 2005 Lateef Alabi-Oki
26
@license: GNU GPLv2 or Later
27
@contact: mystilleef@gmail.com
30
def convert_string_to_boolean(string):
32
Convert a string representing True or False into their boolean equivalents.
34
@param string: A string representing True or False
35
@type string: A String object.
37
@return: True or False
38
@rtype: A Boolean object.
46
def get_style(style_elements):
48
Get a style to be used for syntax highlighting.
50
@param style_elements: Attributes of a stye to be used for syntax highlighting.
51
@type style_elements: A Tuple object.
53
@return: A style to be used for syntax highlighting.
54
@rtype: A gtk.Style object.
56
from gtksourceview import SourceTagStyle
57
style = SourceTagStyle()
58
from gtk.gdk import color_parse
59
style.foreground = color_parse(style_elements[0].get_string())
60
if style_elements[1].get_string() != "None":
61
style.background = color_parse(style_elements[1].get_string())
62
style.bold = convert_string_to_boolean(style_elements[2].get_string())
63
style.italic = convert_string_to_boolean(style_elements[3].get_string())
64
style.underline = convert_string_to_boolean(style_elements[4].get_string())
67
def set_language_style(language, entries, syntax_key):
69
Use syntax highlight color found in GConf.
71
This function modifies the default language style set by gtksourceview.
72
Language style represent the style of highlighted keywords in source code
73
file. This function uses the style attributes found in the GConf database
76
@param language: An object representing the language for a source code.
77
@type language: A gtksourceview.SourceLanguage object.
79
@param entries: Entries found for a particular language in the GConf database.
80
@type entries: A gconf.Entry object.
82
@param syntax_key: A GConf key.
83
@param syntax_key: A String object.
85
@return: An object representing the language for a source code file.
86
@rtype: A gtksourceview.SourceLanguage object.
89
# Syntax color keys in GConf
90
key_list = [item.key.replace(syntax_key + "/", "") for item in entries]
91
# Syntax color attributes for keys in GConf
92
value_list = [item.value.get_list() for item in entries]
95
style = get_style(value_list[count])
96
language.set_tag_style(key, style)
102
def activate_syntax_highlight(textbuffer, language=None):
104
Highlight keywords in source code for various programming languages.
106
This function can be called via a timeout_add function to it returns False
107
to prevent the function from being called repeatedly.
109
@param textbuffer: A buffer object.
110
@type textbuffer: A gtksourceview.SourceBuffer object.
112
@param language: An object representing the language for a source code.
113
@type language: A gtksourceview.SourceLanguage object.
115
@return: False to terminate the timeout_add function that calls this one.
116
@rtype: A Boolean object.
118
textbuffer.set_highlight(True)
121
syntax_key = "/apps/scribes/SyntaxHighlight/" + language.get_id()
122
from gconf import client_get_default
123
client = client_get_default()
124
if client.dir_exists(syntax_key):
125
entries = client.all_entries(syntax_key)
127
language = set_language_style(language, entries, syntax_key)
128
textbuffer.set_language(language)
130
print "Syntax Error Exceptions"