summaryrefslogtreecommitdiff
path: root/plugins/snippets/snippets/Helper.py
blob: 60ae1c798c8e23a3db359034488e7538b5d4dca5 (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
#    Pluma snippets plugin
#    Copyright (C) 2005-2006  Jesse van den Kieboom <jesse@icecrew.nl>
#
#    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 string
from xml.sax import saxutils
from xml.etree.ElementTree import *
import re

import gtk
from gtk import gdk

def message_dialog(par, typ, msg):
        d = gtk.MessageDialog(par, gtk.DIALOG_MODAL, typ, gtk.BUTTONS_OK, msg)
        d.set_property('use-markup', True)

        d.run()
        d.destroy()

def compute_indentation(view, piter):
        line = piter.get_line()
        start = view.get_buffer().get_iter_at_line(line)
        end = start.copy()
        
        ch = end.get_char()
        
        while (ch.isspace() and ch != '\r' and ch != '\n' and \
                        end.compare(piter) < 0):
                if not end.forward_char():
                        break;
                
                ch = end.get_char()
        
        if start.equal(end):
                return ''
        
        return start.get_slice(end)

def markup_escape(text):
        return saxutils.escape(text)

def spaces_instead_of_tabs(view, text):
        if not view.get_insert_spaces_instead_of_tabs():
                return text

        return text.replace("\t", view.get_tab_width() * ' ')

def insert_with_indent(view, piter, text, indentfirst = True, context = None):
        text = spaces_instead_of_tabs(view, text)
        lines = text.split('\n')

        view.get_buffer().set_data('PlumaSnippetsPluginContext', context)

        if len(lines) == 1:
                view.get_buffer().insert(piter, text)
        else:
                # Compute indentation
                indent = compute_indentation(view, piter)
                text = ''

                for i in range(0, len(lines)):
                        if indentfirst or i > 0:
                                text += indent + lines[i] + '\n'
                        else:
                                text += lines[i] + '\n'
                
                view.get_buffer().insert(piter, text[:-1])

        view.get_buffer().set_data('PlumaSnippetsPluginContext', None)

def get_buffer_context(buf):
        return buf.get_data('PlumaSnippetsPluginContext')

def snippets_debug(*s):
        return

def write_xml(node, f, cdata_nodes=()):
        assert node is not None

        if not hasattr(f, "write"):
                f = open(f, "wb")

        # Encoding
        f.write("<?xml version='1.0' encoding='utf-8'?>\n")

        _write_node(node, f, cdata_nodes)

def _write_indent(file, text, indent):
        file.write('  ' * indent + text)

def _write_node(node, file, cdata_nodes=(), indent=0):
        # write XML to file
        tag = node.tag

        if node is Comment:
                _write_indent(file, "<!-- %s -->\n" % saxutils.escape(node.text.encode('utf-8')), indent)
        elif node is ProcessingInstruction:
                _write_indent(file, "<?%s?>\n" % saxutils.escape(node.text.encode('utf-8')), indent)
        else:
                items = node.items()
                
                if items or node.text or len(node):
                        _write_indent(file, "<" + tag.encode('utf-8'), indent)

                        if items:
                                items.sort() # lexical order
                                for k, v in items:
                                        file.write(" %s=%s" % (k.encode('utf-8'), saxutils.quoteattr(v.encode('utf-8'))))
                        if node.text or len(node):
                                file.write(">")
                                if node.text and node.text.strip() != "":
                                        if tag in cdata_nodes:
                                                file.write(_cdata(node.text))
                                        else:
                                                file.write(saxutils.escape(node.text.encode('utf-8')))
                                else:
                                        file.write("\n")

                                for n in node:
                                        _write_node(n, file, cdata_nodes, indent + 1)
                        
                                if not len(node):
                                        file.write("</" + tag.encode('utf-8') + ">\n")
                                else:
                                        _write_indent(file, "</" + tag.encode('utf-8') + ">\n", \
                                                        indent)
                        else:
                                file.write(" />\n")

                if node.tail and node.tail.strip() != "":
                        file.write(saxutils.escape(node.tail.encode('utf-8')))

def _cdata(text, replace=string.replace):
        text = text.encode('utf-8')
        return '<![CDATA[' + replace(text, ']]>', ']]]]><![CDATA[>') + ']]>'

def buffer_word_boundary(buf):
        iter = buf.get_iter_at_mark(buf.get_insert())
        start = iter.copy()
        
        if not iter.starts_word() and (iter.inside_word() or iter.ends_word()):
                start.backward_word_start()
        
        if not iter.ends_word() and iter.inside_word():
                iter.forward_word_end()
                
        return (start, iter)

def buffer_line_boundary(buf):
        iter = buf.get_iter_at_mark(buf.get_insert())
        start = iter.copy()
        start.set_line_offset(0)
        
        if not iter.ends_line():
                iter.forward_to_line_end()
        
        return (start, iter)

def drop_get_uris(selection):
        lines = re.split('\\s*[\\n\\r]+\\s*', selection.data.strip())
        result = []
        
        for line in lines:
                if not line.startswith('#'):
                        result.append(line)
        
        return result

# ex:ts=8:et: