home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / gedit-2 / plugins / snippets / Helper.py < prev    next >
Encoding:
Python Source  |  2009-04-14  |  6.7 KB  |  194 lines

  1. #    Gedit snippets plugin
  2. #    Copyright (C) 2005-2006  Jesse van den Kieboom <jesse@icecrew.nl>
  3. #
  4. #    This program is free software; you can redistribute it and/or modify
  5. #    it under the terms of the GNU General Public License as published by
  6. #    the Free Software Foundation; either version 2 of the License, or
  7. #    (at your option) any later version.
  8. #
  9. #    This program is distributed in the hope that it will be useful,
  10. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. #    GNU General Public License for more details.
  13. #
  14. #    You should have received a copy of the GNU General Public License
  15. #    along with this program; if not, write to the Free Software
  16. #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  17.  
  18. import string
  19. from xml.sax import saxutils
  20. from ElementTree import *
  21. import re
  22.  
  23. import gtk
  24. from gtk import gdk
  25.  
  26. def message_dialog(par, typ, msg):
  27.         d = gtk.MessageDialog(par, gtk.DIALOG_MODAL, typ, gtk.BUTTONS_OK, msg)
  28.         d.set_property('use-markup', True)
  29.  
  30.         d.run()
  31.         d.destroy()
  32.  
  33. def compute_indentation(view, piter):
  34.         line = piter.get_line()
  35.         start = view.get_buffer().get_iter_at_line(line)
  36.         end = start.copy()
  37.         
  38.         ch = end.get_char()
  39.         
  40.         while (ch.isspace() and ch != '\r' and ch != '\n' and \
  41.                         end.compare(piter) < 0):
  42.                 if not end.forward_char():
  43.                         break;
  44.                 
  45.                 ch = end.get_char()
  46.         
  47.         if start.equal(end):
  48.                 return ''
  49.         
  50.         return start.get_slice(end)
  51.  
  52. def markup_escape(text):
  53.         return saxutils.escape(text)
  54.  
  55. def spaces_instead_of_tabs(view, text):
  56.         if not view.get_insert_spaces_instead_of_tabs():
  57.                 return text
  58.  
  59.         return text.replace("\t", view.get_tab_width() * ' ')
  60.  
  61. def insert_with_indent(view, piter, text, indentfirst = True, context = None):
  62.         text = spaces_instead_of_tabs(view, text)
  63.         lines = text.split('\n')
  64.  
  65.         view.get_buffer().set_data('GeditSnippetsPluginContext', context)
  66.  
  67.         if len(lines) == 1:
  68.                 view.get_buffer().insert(piter, text)
  69.         else:
  70.                 # Compute indentation
  71.                 indent = compute_indentation(view, piter)
  72.                 text = ''
  73.  
  74.                 for i in range(0, len(lines)):
  75.                         if indentfirst or i > 0:
  76.                                 text += indent + lines[i] + '\n'
  77.                         else:
  78.                                 text += lines[i] + '\n'
  79.                 
  80.                 view.get_buffer().insert(piter, text[:-1])
  81.  
  82.         view.get_buffer().set_data('GeditSnippetsPluginContext', None)
  83.  
  84. def get_buffer_context(buf):
  85.         return buf.get_data('GeditSnippetsPluginContext')
  86.  
  87. def snippets_debug(*s):
  88.         return
  89.  
  90. def write_xml(node, f, cdata_nodes=()):
  91.         assert node is not None
  92.  
  93.         if not hasattr(f, "write"):
  94.                 f = open(f, "wb")
  95.  
  96.         # Encoding
  97.         f.write("<?xml version='1.0' encoding='utf-8'?>\n")
  98.  
  99.         _write_node(node, f, cdata_nodes)
  100.  
  101. def _write_indent(file, text, indent):
  102.         file.write('  ' * indent + text)
  103.  
  104. def _write_node(node, file, cdata_nodes=(), indent=0):
  105.     # write XML to file
  106.         tag = node.tag
  107.  
  108.         if node is Comment:
  109.                 _write_indent(file, "<!-- %s -->\n" % _escape_cdata(node.text), indent)
  110.         elif node is ProcessingInstruction:
  111.                 _write_indent(file, "<?%s?>\n" % _escape_cdata(node.text), \
  112.                                 indent)
  113.         else:
  114.                 items = node.items()
  115.                 
  116.                 if items or node.text or len(node):
  117.                         _write_indent(file, "<" + tag.encode('utf-8'), indent)
  118.                 
  119.                         if items:
  120.                                 items.sort() # lexical order
  121.                                 for k, v in items:
  122.                                         file.write(" %s=\"%s\"" % (k.encode('utf-8'),
  123.                                                         _escape_attrib(v)))
  124.                         if node.text or len(node):
  125.                                 file.write(">")
  126.                                 if node.text and node.text.strip() != "":
  127.                                         if tag in cdata_nodes:
  128.                                                 file.write(_cdata(node.text))
  129.                                         else:
  130.                                                 file.write(_escape_cdata(node.text))
  131.                                 else:
  132.                                         file.write("\n")
  133.  
  134.                                 for n in node:
  135.                                         _write_node(n, file, cdata_nodes, indent + 1)
  136.                         
  137.                                 if not len(node):
  138.                                         file.write("</" + tag.encode('utf-8') + ">\n")
  139.                                 else:
  140.                                         _write_indent(file, "</" + tag.encode('utf-8') + ">\n", \
  141.                                                         indent)
  142.                         else:
  143.                                 file.write(" />\n")
  144.  
  145.                 if node.tail and node.tail.strip() != "":
  146.                         file.write(_escape_cdata(node.tail))
  147.  
  148. def _cdata(text, replace=string.replace):
  149.         text = text.encode('utf-8')
  150.         return '<![CDATA[' + replace(text, ']]>', ']]]]><![CDATA[>') + ']]>'
  151.  
  152. def _escape_cdata(text, replace=string.replace):
  153.         # escape character data
  154.         text = text.encode('utf-8')
  155.         text = replace(text, "&", "&")
  156.         text = replace(text, "<", "<")
  157.         text = replace(text, ">", ">")
  158.         return text
  159.  
  160. def _escape_attrib(text, replace=string.replace):
  161.         # escape attribute value
  162.         text = text.encode('utf-8')
  163.         text = replace(text, "&", "&")
  164.         text = replace(text, "'", "'")
  165.         text = replace(text, "\"", """)
  166.         text = replace(text, "<", "<")
  167.         text = replace(text, ">", ">")
  168.         return text
  169.  
  170. def buffer_word_boundary(buf):
  171.         iter = buf.get_iter_at_mark(buf.get_insert())
  172.         start = iter.copy()
  173.         
  174.         if not iter.starts_word():
  175.                 start.backward_word_start()
  176.         
  177.         if not iter.ends_word():
  178.                 iter.forward_word_end()
  179.                 
  180.         return (start, iter)
  181.         
  182.                 
  183. def drop_get_uris(selection):
  184.         lines = re.split('\\s*[\\n\\r]+\\s*', selection.data.strip())
  185.         result = []
  186.         
  187.         for line in lines:
  188.                 if not line.startswith('#'):
  189.                         result.append(line)
  190.         
  191.         return result
  192.  
  193. # ex:ts=8:et:
  194.