home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / gedit-2 / plugins / snippets / functions.py < prev    next >
Encoding:
Python Source  |  2006-08-27  |  4.5 KB  |  171 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 gtk
  19. from gtk import gdk
  20. from xml.sax import saxutils
  21. from ElementTree import *
  22. import string
  23.  
  24. def message_dialog(par, typ, msg):
  25.     d = gtk.MessageDialog(par, gtk.DIALOG_MODAL, typ, \
  26.             gtk.BUTTONS_OK, msg)
  27.     d.run()
  28.     d.destroy()
  29.  
  30. def compute_indentation(view, piter):
  31.     line = piter.get_line()
  32.     start = view.get_buffer().get_iter_at_line(line)
  33.     end = start.copy()
  34.     
  35.     ch = end.get_char()
  36.     
  37.     while (ch.isspace() and ch != '\r' and ch != '\n' and \
  38.             end.compare(piter) < 0):
  39.         if not end.forward_char():
  40.             break;
  41.         
  42.         ch = end.get_char()
  43.     
  44.     if start.equal(end):
  45.         return ''
  46.     
  47.     return start.get_slice(end)
  48.  
  49. def markup_escape(text):
  50.     return saxutils.escape(text)
  51.  
  52. def spaces_instead_of_tabs(view, text):
  53.     if not view.get_insert_spaces_instead_of_tabs():
  54.         return text
  55.  
  56.     return text.replace("\t", view.get_tabs_width() * ' ')
  57.  
  58. def insert_with_indent(view, piter, text, indentfirst = True):
  59.     text = spaces_instead_of_tabs(view, text)
  60.     lines = text.split('\n')
  61.  
  62.     if len(lines) == 1:
  63.         view.get_buffer().insert(piter, text)
  64.     else:
  65.         # Compute indentation
  66.         indent = compute_indentation(view, piter)
  67.         text = ''
  68.  
  69.         for i in range(0, len(lines)):
  70.             if indentfirst or i > 0:
  71.                 text += indent + lines[i] + '\n'
  72.             else:
  73.                 text += lines[i] + '\n'
  74.         
  75.         view.get_buffer().insert(piter, text[:-1])
  76.  
  77. def snippets_debug(*s):
  78.     return
  79.  
  80. def write_xml(node, file, cdata_nodes=()):
  81.     assert node is not None
  82.  
  83.     if not hasattr(file, "write"):
  84.         file = open(file, "wb")
  85.  
  86.     # Encoding
  87.     file.write("<?xml version='1.0' encoding='utf-8'?>\n")
  88.  
  89.     _write_node(node, file, cdata_nodes)
  90.  
  91. def _write_indent(file, text, indent):
  92.     file.write('  ' * indent + text)
  93.  
  94. def _write_node(node, file, cdata_nodes=(), indent=0):
  95.     # write XML to file
  96.     tag = node.tag
  97.  
  98.     if node is Comment:
  99.         _write_indent(file, "<!-- %s -->\n" % _escape_cdata(node.text), indent)
  100.     elif node is ProcessingInstruction:
  101.         _write_indent(file, "<?%s?>\n" % _escape_cdata(node.text), \
  102.                 indent)
  103.     else:
  104.         items = node.items()
  105.         
  106.         if items or node.text or len(node):
  107.             _write_indent(file, "<" + tag.encode('utf-8'), indent)
  108.         
  109.             if items:
  110.                 items.sort() # lexical order
  111.                 for k, v in items:
  112.                     file.write(" %s=\"%s\"" % (k.encode('utf-8'),
  113.                             _escape_attrib(v)))
  114.             if node.text or len(node):
  115.                 file.write(">")
  116.                 if node.text and node.text.strip() != "":
  117.                     if tag in cdata_nodes:
  118.                         file.write(_cdata(node.text))
  119.                     else:
  120.                         file.write(_escape_cdata(node.text))
  121.                 else:
  122.                     file.write("\n")
  123.  
  124.                 for n in node:
  125.                     _write_node(n, file, cdata_nodes, indent + 1)
  126.             
  127.                 if not len(node):
  128.                     file.write("</" + tag.encode('utf-8') + ">\n")
  129.                 else:
  130.                     _write_indent(file, "</" + tag.encode('utf-8') + ">\n", \
  131.                             indent)
  132.             else:
  133.                 file.write(" />\n")
  134.  
  135.         if node.tail and node.tail.strip() != "":
  136.             file.write(_escape_cdata(node.tail))
  137.  
  138. def _cdata(text, replace=string.replace):
  139.     text = text.encode('utf-8')
  140.     return '<![CDATA[' + replace(text, ']]>', ']]]]><![CDATA[>') + ']]>'
  141.  
  142. def _escape_cdata(text, replace=string.replace):
  143.     # escape character data
  144.     text = text.encode('utf-8')
  145.     text = replace(text, "&", "&")
  146.     text = replace(text, "<", "<")
  147.     text = replace(text, ">", ">")
  148.     return text
  149.  
  150. def _escape_attrib(text, replace=string.replace):
  151.     # escape attribute value
  152.     text = text.encode('utf-8')
  153.     text = replace(text, "&", "&")
  154.     text = replace(text, "'", "'")
  155.     text = replace(text, "\"", """)
  156.     text = replace(text, "<", "<")
  157.     text = replace(text, ">", ">")
  158.     return text
  159.  
  160. def buffer_word_boundary(buf):
  161.     iter = buf.get_iter_at_mark(buf.get_insert())
  162.     start = iter.copy()
  163.     
  164.     if not iter.starts_word():
  165.         start.backward_word_start()
  166.     
  167.     if not iter.ends_word():
  168.         iter.forward_word_end()
  169.         
  170.     return (start, iter)
  171.