Permalink
Browse files

use window helper; replace spaces with tabs; big cleanup

  • Loading branch information...
1 parent f6f8c7b commit 714280b3912dfa47abaf63325b0100495276f65e @fmarcia committed Jun 4, 2010
Showing with 776 additions and 819 deletions.
  1. +233 −6 zencoding/__init__.py
  2. +0 −206 zencoding/plugin.py
  3. +543 −607 zencoding/zen_editor.py
View
239 zencoding/__init__.py
@@ -1,9 +1,236 @@
-'''
-Bootstrap for Zen Coding for Gedit
+# Zen Coding for Gedit
+#
+# Copyright (C) 2010 Franck Marcia
+#
+# 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 3 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, see <http://www.gnu.org/licenses/>.
-@author Franck Marcia (franck.marcia@gmail.com)
-@link http://github.com/fmarcia/zen-coding-gedit
-'''
+import gedit, gtk, os
+from zen_editor import ZenEditor
-from plugin import ZenCodingPlugin
+zencoding_ui_str = """
+<ui>
+ <menubar name="MenuBar">
+ <menu name="EditMenu" action="Edit">
+ <placeholder name="EditOps_5">
+ <menu action="ZenCodingMenuAction">
+ <menuitem name="ZenCodingExpand" action="ZenCodingExpandAction"/>
+ <menuitem name="ZenCodingExpandW" action="ZenCodingExpandWAction"/>
+ <menuitem name="ZenCodingWrap" action="ZenCodingWrapAction"/>
+ <separator/>
+ <placeholder name="EditOps_5">
+ <menu action="ZenCodingZenifyAction">
+ <menuitem name="ZenCodingZenify0" action="ZenCodingZenify0Action"/>
+ <menuitem name="ZenCodingZenify1" action="ZenCodingZenify1Action"/>
+ <menuitem name="ZenCodingZenify2" action="ZenCodingZenify2Action"/>
+ <menuitem name="ZenCodingZenify3" action="ZenCodingZenify3Action"/>
+ </menu>
+ </placeholder>
+ <separator/>
+ <menuitem name="LoremIpsum" action="LoremIpsumAction"/>
+ <separator/>
+ <menuitem name="ZenCodingInward" action="ZenCodingInwardAction"/>
+ <menuitem name="ZenCodingOutward" action="ZenCodingOutwardAction"/>
+ <separator/>
+ <menuitem name="ZenCodingPTag" action="ZenCodingPTagAction"/>
+ <menuitem name="ZenCodingNTag" action="ZenCodingNTagAction"/>
+ <menuitem name="ZenCodingPNode" action="ZenCodingPNodeAction"/>
+ <menuitem name="ZenCodingNNode" action="ZenCodingNNodeAction"/>
+ <menuitem name="ZenCodingPrev" action="ZenCodingPrevAction"/>
+ <menuitem name="ZenCodingNext" action="ZenCodingNextAction"/>
+ <separator/>
+ <menuitem name="ZenCodingSize" action="ZenCodingSizeAction"/>
+ <menuitem name="ZenCodingData" action="ZenCodingDataAction"/>
+ <separator/>
+ <menuitem name="ZenCodingMerge" action="ZenCodingMergeAction"/>
+ <menuitem name="ZenCodingRemove" action="ZenCodingRemoveAction"/>
+ <menuitem name="ZenCodingSplit" action="ZenCodingSplitAction"/>
+ <menuitem name="ZenCodingComment" action="ZenCodingCommentAction"/>
+ <separator/>
+ <menuitem name="ZenCodingSettings" action="ZenCodingSettingsAction"/>
+ </menu>
+ </placeholder>
+ </menu>
+ </menubar>
+</ui>
+"""
+
+class ZenCodingWindowHelper():
+
+ def __init__(self, window):
+
+ # window
+ self.window = window
+
+ # menu items
+ actions = [
+ ('ZenCodingMenuAction', None, '_Zen Coding', None, "Zen Coding tools", None),
+ ('ZenCodingExpandAction', None, '_Expand abbreviation', '<Ctrl>E', "Expand abbreviation to raw HTML/CSS", self.expand_abbreviation),
+ ('ZenCodingExpandWAction', None, 'E_xpand with abbreviation...', '<Ctrl><Alt>E', "Type in an abbreviation to expand", self.expand_with_abbreviation),
+ ('ZenCodingWrapAction', None, '_Wrap with abbreviation...', '<Ctrl><Shift>E', "Wrap with code expanded from abbreviation", self.wrap_with_abbreviation),
+ ('ZenCodingZenifyAction', None, '_Zenify', None, "Reduce to abbreviation", None),
+ ('ZenCodingZenify0Action', None, '_Tag names', '<Ctrl><Alt>Z', "Reduce to tag names only", self.zenify0),
+ ('ZenCodingZenify1Action', None, ' + _Ids and classes', None, "Reduce with ids and classes", self.zenify1),
+ ('ZenCodingZenify2Action', None, ' + All other _attributes', None, "Reduce with all attributes", self.zenify2),
+ ('ZenCodingZenify3Action', None, ' + _Values', None, "Reduce with all attributes and values", self.zenify3),
+ ('LoremIpsumAction', None, '_Lorem ipsum...', '<Ctrl><Alt>X', "Insert a lorem ipsum string", self.lorem_ipsum),
+ ('ZenCodingInwardAction', None, 'Select _inward', '<Ctrl><Alt>I', "Select inner tag's content", self.match_pair_inward),
+ ('ZenCodingOutwardAction', None, 'Select _outward', '<Ctrl><Alt>O', "Select outer tag's content", self.match_pair_outward),
+ ('ZenCodingPTagAction', None, 'Previous tag', '<Ctrl><Alt>Up', "Select the previous tag in HTML code", self.prev_tag),
+ ('ZenCodingNTagAction', None, 'Next tag', '<Ctrl><Alt>Down', "Select the next tag in HTML code", self.next_tag),
+ ('ZenCodingPNodeAction', None, 'Previous node', '<Ctrl><Alt>Left', "Select the previous HTML node", self.prev_node),
+ ('ZenCodingNNodeAction', None, 'Next node', '<Ctrl><Alt>Right', "Select the next HTML node", self.next_node),
+ ('ZenCodingPrevAction', None, '_Previous edit point', '<Alt>Left', "Place the cursor at the previous edit point", self.prev_edit_point),
+ ('ZenCodingNextAction', None, '_Next edit point', '<Alt>Right', "Place the cursor at the next edit point", self.next_edit_point),
+ ('ZenCodingSizeAction', None, 'Update image _size', '<Ctrl><Alt>S', "Update image size tag from file", self.update_image_size),
+ ('ZenCodingDataAction', None, 'Toggle image url/da_ta', '<Ctrl><Alt>A', "Toggle between image url and data", self.encode_decode_base64),
+ ('ZenCodingMergeAction', None, '_Merge lines', '<Ctrl><Alt>M', "Merge all lines of the current selection", self.merge_lines),
+ ('ZenCodingRemoveAction', None, '_Remove tag', '<Ctrl><Alt>R', "Remove a tag", self.remove_tag),
+ ('ZenCodingSplitAction', None, 'Split or _join tag', '<Ctrl><Alt>J', "Toggle between single and double tag", self.split_join_tag),
+ ('ZenCodingCommentAction', None, 'Toggle _comment', '<Ctrl><Alt>C', "Toggle an XML or HTML comment", self.toggle_comment),
+ ('ZenCodingSettingsAction', None, 'E_dit settings...', None, "Customize snippets and abbreviations", self.edit_settings)
+ ]
+ windowdata = dict()
+ self.window.set_data("ZenCodingPluginDataKey", windowdata)
+ windowdata["action_group"] = gtk.ActionGroup("GeditZenCodingPluginActions")
+ windowdata["action_group"].add_actions(actions)
+ manager = self.window.get_ui_manager()
+ manager.insert_action_group(windowdata["action_group"], -1)
+ windowdata["ui_id"] = manager.add_ui_from_string(zencoding_ui_str)
+ self.window.set_data("ZenCodingPluginInfo", windowdata)
+
+ # zen coding
+ self.editor = ZenEditor(self)
+ error = self.editor.get_user_settings_error()
+ if error:
+ md = gtk.MessageDialog(self.window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR,
+ gtk.BUTTONS_CLOSE, "There is an error in user settings:")
+ message = "{0} on line {1} at character {2}\n\nUser settings will not be available."
+ md.set_title("Zen Coding error")
+ md.format_secondary_text(message.format(error['msg'], error['lineno'], error['offset']))
+ md.run()
+ md.destroy()
+
+ def deactivate(self):
+
+ # zen coding
+ self.editor = None
+
+ # menu items
+ windowdata = self.window.get_data("ZenCodingPluginDataKey")
+ manager = self.window.get_ui_manager()
+ manager.remove_ui(windowdata["ui_id"])
+ manager.remove_action_group(windowdata["action_group"])
+
+ # window
+ self.window = None
+
+ def update_ui(self):
+
+ # disabled if not editable
+ view = self.window.get_active_view()
+ windowdata = self.window.get_data("ZenCodingPluginDataKey")
+ windowdata["action_group"].set_sensitive(bool(view and view.get_editable()))
+
+ # the content changed
+ self.editor.set_context()
+
+ # Menu handlers
+
+ def expand_abbreviation(self, action):
+ self.editor.expand_abbreviation()
+
+ def expand_with_abbreviation(self, action):
+ self.editor.expand_with_abbreviation()
+
+ def wrap_with_abbreviation(self, action):
+ self.editor.wrap_with_abbreviation()
+
+ def zenify0(self, action):
+ self.editor.zenify(0)
+
+ def zenify1(self, action):
+ self.editor.zenify(1)
+
+ def zenify2(self, action):
+ self.editor.zenify(2)
+
+ def zenify3(self, action):
+ self.editor.zenify(3)
+
+ def lorem_ipsum(self, action):
+ self.editor.lorem_ipsum()
+
+ def match_pair_inward(self, action):
+ self.editor.match_pair_inward()
+
+ def match_pair_outward(self, action):
+ self.editor.match_pair_outward()
+
+ def prev_tag(self, action):
+ self.editor.prev_tag()
+
+ def next_tag(self, action):
+ self.editor.next_tag()
+
+ def prev_node(self, action):
+ self.editor.prev_node()
+
+ def next_node(self, action):
+ self.editor.next_node()
+
+ def prev_edit_point(self, action):
+ self.editor.prev_edit_point()
+
+ def next_edit_point(self, action):
+ self.editor.next_edit_point()
+
+ def update_image_size(self, action):
+ self.editor.update_image_size()
+
+ def encode_decode_base64(self, action):
+ self.editor.encode_decode_base64()
+
+ def merge_lines(self, action):
+ self.editor.merge_lines()
+
+ def remove_tag(self, action):
+ self.editor.remove_tag()
+
+ def split_join_tag(self, action):
+ self.editor.split_join_tag()
+
+ def toggle_comment(self, action):
+ self.editor.toggle_comment()
+
+ def edit_settings(self, action):
+ name = 'file://' + os.path.join(os.path.dirname(__file__), 'my_zen_settings.py')
+ self.window.create_tab_from_uri(name, None, 0, True, True)
+
+
+class ZenCodingPlugin(gedit.Plugin):
+
+ def __init__(self):
+ gedit.Plugin.__init__(self)
+ self.instances = {}
+
+ def activate(self, window):
+ self.instances[window] = ZenCodingWindowHelper(window)
+
+ def deactivate(self, window):
+ self.instances[window].deactivate()
+ del self.instances[window]
+
+ def update_ui(self, window):
+ self.instances[window].update_ui()
View
206 zencoding/plugin.py
@@ -1,206 +0,0 @@
-# Zen Coding for Gedit
-#
-# Copyright (C) 2010 Franck Marcia
-#
-# 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 3 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, see <http://www.gnu.org/licenses/>.
-
-'''
-Connexion of Zen Coding to Gedit
-
-@author Franck Marcia (franck.marcia@gmail.com)
-@link http://github.com/fmarcia/zen-coding-gedit
-'''
-
-import gedit, gobject, gtk, os
-
-from zen_editor import ZenEditor
-
-zencoding_ui_str = """
-<ui>
- <menubar name="MenuBar">
- <menu name="EditMenu" action="Edit">
- <placeholder name="EditOps_5">
- <menu action="ZenCodingMenuAction">
- <menuitem name="ZenCodingExpand" action="ZenCodingExpandAction"/>
- <menuitem name="ZenCodingExpandW" action="ZenCodingExpandWAction"/>
- <menuitem name="ZenCodingWrap" action="ZenCodingWrapAction"/>
- <separator/>
- <placeholder name="EditOps_5">
- <menu action="ZenCodingZenifyAction">
- <menuitem name="ZenCodingZenify0" action="ZenCodingZenify0Action"/>
- <menuitem name="ZenCodingZenify1" action="ZenCodingZenify1Action"/>
- <menuitem name="ZenCodingZenify2" action="ZenCodingZenify2Action"/>
- <menuitem name="ZenCodingZenify3" action="ZenCodingZenify3Action"/>
- </menu>
- </placeholder>
- <separator/>
- <menuitem name="LoremIpsum" action="LoremIpsumAction"/>
- <separator/>
- <menuitem name="ZenCodingInward" action="ZenCodingInwardAction"/>
- <menuitem name="ZenCodingOutward" action="ZenCodingOutwardAction"/>
- <separator/>
- <menuitem name="ZenCodingPTag" action="ZenCodingPTagAction"/>
- <menuitem name="ZenCodingNTag" action="ZenCodingNTagAction"/>
- <menuitem name="ZenCodingPNode" action="ZenCodingPNodeAction"/>
- <menuitem name="ZenCodingNNode" action="ZenCodingNNodeAction"/>
- <menuitem name="ZenCodingPrev" action="ZenCodingPrevAction"/>
- <menuitem name="ZenCodingNext" action="ZenCodingNextAction"/>
- <separator/>
- <menuitem name="ZenCodingSize" action="ZenCodingSizeAction"/>
- <menuitem name="ZenCodingData" action="ZenCodingDataAction"/>
- <separator/>
- <menuitem name="ZenCodingMerge" action="ZenCodingMergeAction"/>
- <menuitem name="ZenCodingRemove" action="ZenCodingRemoveAction"/>
- <menuitem name="ZenCodingSplit" action="ZenCodingSplitAction"/>
- <menuitem name="ZenCodingComment" action="ZenCodingCommentAction"/>
- <separator/>
- <menuitem name="ZenCodingSettings" action="ZenCodingSettingsAction"/>
- </menu>
- </placeholder>
- </menu>
- </menubar>
-</ui>
-"""
-
-class ZenCodingPlugin(gedit.Plugin):
- """A Gedit plugin to implement Zen Coding's HTML and CSS shorthand expander."""
-
- def activate(self, window):
- actions = [
- ('ZenCodingMenuAction', None, '_Zen Coding', None, "Zen Coding tools", None),
- ('ZenCodingExpandAction', None, '_Expand abbreviation', '<Ctrl>E', "Expand abbreviation to raw HTML/CSS", self.expand_abbreviation),
- ('ZenCodingExpandWAction', None, 'E_xpand with abbreviation...', '<Ctrl><Alt>E', "Type in an abbreviation to expand", self.expand_with_abbreviation),
- ('ZenCodingWrapAction', None, '_Wrap with abbreviation...', '<Ctrl><Shift>E', "Wrap with code expanded from abbreviation", self.wrap_with_abbreviation),
- ('ZenCodingZenifyAction', None, '_Zenify', None, "Reduce to abbreviation", None),
- ('ZenCodingZenify0Action', None, '_Tag names', '<Ctrl><Alt>Z', "Reduce to tag names only", self.zenify0),
- ('ZenCodingZenify1Action', None, ' + _Ids and classes', None, "Reduce with ids and classes", self.zenify1),
- ('ZenCodingZenify2Action', None, ' + All other _attributes', None, "Reduce with all attributes", self.zenify2),
- ('ZenCodingZenify3Action', None, ' + _Values', None, "Reduce with all attributes and values", self.zenify3),
- ('LoremIpsumAction', None, '_Lorem ipsum...', '<Ctrl><Alt>X', "Insert a lorem ipsum string", self.lorem_ipsum),
- ('ZenCodingInwardAction', None, 'Select _inward', '<Ctrl><Alt>I', "Select inner tag's content", self.match_pair_inward),
- ('ZenCodingOutwardAction', None, 'Select _outward', '<Ctrl><Alt>O', "Select outer tag's content", self.match_pair_outward),
- ('ZenCodingPTagAction', None, 'Previous tag', '<Ctrl><Alt>Up', "Select the previous tag in HTML code", self.prev_tag),
- ('ZenCodingNTagAction', None, 'Next tag', '<Ctrl><Alt>Down', "Select the next tag in HTML code", self.next_tag),
- ('ZenCodingPNodeAction', None, 'Previous node', '<Ctrl><Alt>Left', "Select the previous HTML node", self.prev_node),
- ('ZenCodingNNodeAction', None, 'Next node', '<Ctrl><Alt>Right', "Select the next HTML node", self.next_node),
- ('ZenCodingPrevAction', None, '_Previous edit point', '<Alt>Left', "Place the cursor at the previous edit point", self.prev_edit_point),
- ('ZenCodingNextAction', None, '_Next edit point', '<Alt>Right', "Place the cursor at the next edit point", self.next_edit_point),
- ('ZenCodingSizeAction', None, 'Update image _size', '<Ctrl><Alt>S', "Update image size tag from file", self.update_image_size),
- ('ZenCodingDataAction', None, 'Toggle image url/da_ta', '<Ctrl><Alt>A', "Toggle between image url and data", self.encode_decode_base64),
- ('ZenCodingMergeAction', None, '_Merge lines', '<Ctrl><Alt>M', "Merge all lines of the current selection", self.merge_lines),
- ('ZenCodingRemoveAction', None, '_Remove tag', '<Ctrl><Alt>R', "Remove a tag", self.remove_tag),
- ('ZenCodingSplitAction', None, 'Split or _join tag', '<Ctrl><Alt>J', "Toggle between single and double tag", self.split_join_tag),
- ('ZenCodingCommentAction', None, 'Toggle _comment', '<Ctrl><Alt>C', "Toggle an XML or HTML comment", self.toggle_comment),
- ('ZenCodingSettingsAction', None, 'E_dit settings...', None, "Customize snippets and abbreviations", self.edit_settings)
- ]
- windowdata = dict()
- window.set_data("ZenCodingPluginDataKey", windowdata)
- windowdata["action_group"] = gtk.ActionGroup("GeditZenCodingPluginActions")
- windowdata["action_group"].add_actions(actions, window)
- manager = window.get_ui_manager()
- manager.insert_action_group(windowdata["action_group"], -1)
- windowdata["ui_id"] = manager.add_ui_from_string(zencoding_ui_str)
- window.set_data("ZenCodingPluginInfo", windowdata)
- self.editor = ZenEditor()
- error = self.editor.get_user_settings_error()
- if error:
- md = gtk.MessageDialog(window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR,
- gtk.BUTTONS_CLOSE, "There is an error in user settings:")
- message = "{0} on line {1} at character {2}\n\nUser settings will not be available."
- md.set_title("Zen Coding error")
- md.format_secondary_text(message.format(error['msg'], error['lineno'], error['offset']))
- md.run()
- md.destroy()
-
- def deactivate(self, window):
- windowdata = window.get_data("ZenCodingPluginDataKey")
- manager = window.get_ui_manager()
- manager.remove_ui(windowdata["ui_id"])
- manager.remove_action_group(windowdata["action_group"])
-
- def update_ui(self, window):
- view = window.get_active_view()
- windowdata = window.get_data("ZenCodingPluginDataKey")
- windowdata["action_group"].set_sensitive(bool(view and view.get_editable()))
-
- def expand_abbreviation(self, action, window):
- self.editor.expand_abbreviation(window)
-
- def expand_with_abbreviation(self, action, window):
- self.editor.expand_with_abbreviation(window)
-
- def wrap_with_abbreviation(self, action, window):
- self.editor.wrap_with_abbreviation(window)
-
- def zenify0(self, action, window):
- self.editor.zenify(window, 0)
-
- def zenify1(self, action, window):
- self.editor.zenify(window, 1)
-
- def zenify2(self, action, window):
- self.editor.zenify(window, 2)
-
- def zenify3(self, action, window):
- self.editor.zenify(window, 3)
-
- def lorem_ipsum(self, action, window):
- self.editor.lorem_ipsum(window)
-
- def match_pair_inward(self, action, window):
- self.editor.match_pair_inward(window)
-
- def match_pair_outward(self, action, window):
- self.editor.match_pair_outward(window)
-
- def merge_lines(self, action, window):
- self.editor.merge_lines(window)
-
- def prev_tag(self, action, window):
- self.editor.prev_tag(window)
-
- def next_tag(self, action, window):
- self.editor.next_tag(window)
-
- def prev_node(self, action, window):
- self.editor.prev_node(window)
-
- def next_node(self, action, window):
- self.editor.next_node(window)
-
- def prev_edit_point(self, action, window):
- self.editor.prev_edit_point(window)
-
- def next_edit_point(self, action, window):
- self.editor.next_edit_point(window)
-
- def update_image_size(self, action, window):
- self.editor.update_image_size(window)
-
- def encode_decode_base64(self, action, window):
- self.editor.encode_decode_base64(window)
-
- def remove_tag(self, action, window):
- self.editor.remove_tag(window)
-
- def split_join_tag(self, action, window):
- self.editor.split_join_tag(window)
-
- def toggle_comment(self, action, window):
- self.editor.toggle_comment(window)
-
- def edit_settings(self, action, window):
- name = 'file://' + os.path.join(os.path.dirname(__file__), 'my_zen_settings.py')
- window.create_tab_from_uri(name, None, 0, True, True)
-
View
1,150 zencoding/zen_editor.py
@@ -1,637 +1,573 @@
-'''
-High-level editor interface that communicates with underlying editor (like
-Espresso, Coda, etc.) or browser.
-Basically, you should call <code>set_context(obj)</code> method to
-set up undelying editor context before using any other method.
+# Zen Coding for Gedit
+#
+# Copyright (C) 2010 Franck Marcia
+#
+# 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 3 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, see <http://www.gnu.org/licenses/>.
-This interface is used by <i>zen_actions.py</i> for performing different
-actions like <b>Expand abbreviation</b>
-
-@example
-import zen_editor
-zen_editor.set_context(obj);
-//now you are ready to use editor object
-zen_editor.get_selection_range();
-
-@author Sergey Chikuyonok (serge.che@gmail.com)
-@link http://chikuyonok.ru
-
-Changes for Gedit Plugin
-@author Franck Marcia (franck.marcia@gmail.com)
-@link http://github.com/fmarcia/zen-coding-gedit
-'''
+import sys, os, re, locale
import zen_core, zen_actions, zen_file, html_matcher
-import sys, os, re, locale
from image_size import update_image_size
+
import zen_dialog
from html_navigation import HtmlNavigation
from lorem_ipsum import lorem_ipsum
class ZenSnippet():
- def __init__(self, abbreviation, content):
- self.valid = True
- self.properties = {
- 'text': content,
- 'drop-targets': '',
- 'tag': abbreviation,
- 'description': 'zencoding',
- 'accelerator': ''
- }
+ def __init__(self, abbreviation, content):
+ self.valid = True
+ self.properties = {
+ 'text': content,
+ 'drop-targets': '',
+ 'tag': abbreviation,
+ 'description': 'zencoding',
+ 'accelerator': ''
+ }
- def __getitem__(self, prop):
- return self.properties[prop]
+ def __getitem__(self, prop):
+ return self.properties[prop]
placeholder_count = 0
def placeholder_feed(m):
- global placeholder_count
- placeholder_count += 1
-
- skip = len(zen_core.get_caret_placeholder()) + 1
+ global placeholder_count
+ placeholder_count += 1
+
+ skip = len(zen_core.get_caret_placeholder()) + 1
- if m and m.group(1):
- if m.group(1).startswith('{{'):
- return '${' + repr(placeholder_count)
- elif m.group(1).startswith('>'):
- return '>${' + repr(placeholder_count) + ':' + m.group(1)[skip:-1] + '}<'
- else:
- return '$' + repr(placeholder_count)
- else:
- return ''
+ if m and m.group(1):
+ if m.group(1).startswith('{{'):
+ return '${' + repr(placeholder_count)
+ elif m.group(1).startswith('>'):
+ return '>${' + repr(placeholder_count) + ':' + m.group(1)[skip:-1] + '}<'
+ else:
+ return '$' + repr(placeholder_count)
+ else:
+ return ''
class ZenEditor():
- def __init__(self):
- self.last_wrap = ''
- self.last_expand = ''
- self.placeholder = zen_core.get_caret_placeholder()
- self.last_lorem_ipsum = 'list 5*5'
- self.html_navigation = None
-
- def set_context(self, context, use_original_placeholder = False):
- """
- Setup underlying editor context. You should call this method
- <code>before</code> using any Zen Coding action.
- @param context: context object
- """
- self.context = context # window
- self.view = context.get_active_view()
- self.buffer = self.view.get_buffer()
- self.document = context.get_active_document()
-
- default_locale = locale.getdefaultlocale()[0]
- if default_locale:
- lang = re.sub(r'_[^_]+$', '', default_locale)
- if lang != default_locale:
- zen_core.set_variable('lang', lang)
- zen_core.set_variable('locale', default_locale.replace('_', '-'))
- else:
- zen_core.set_variable('lang', default_locale)
- zen_core.set_variable('locale', default_locale)
-
- self.encoding = self.document.get_encoding().get_charset()
- zen_core.set_variable('charset', self.encoding)
-
- if self.view.get_insert_spaces_instead_of_tabs():
- zen_core.set_variable('indentation', " " * context.get_active_view().get_tab_width())
- else:
- zen_core.set_variable('indentation', "\t")
- #zen_core.set_newline(???)
-
- try:
- sys.path.append('/usr/lib/gedit-2/plugins')
- from snippets.Document import Document
- self.snippet_document = Document(None, self.view)
- except:
- self.snippet_document = None
-
- zen_core.set_caret_placeholder(self.placeholder if use_original_placeholder else '')
-
- def get_selection_range(self):
- """
- Returns character indexes of selected text
- @return: list of start and end indexes
- @example
- start, end = zen_editor.get_selection_range();
- print('%s, %s' % (start, end))
- """
- offset_start = self.get_insert_offset()
- offset_end = self.get_selection_bound_offset()
- if offset_start < offset_end:
- return offset_start, offset_end
- return offset_end, offset_start
-
- def create_selection(self, offset_start, offset_end=None):
- """
- Creates selection from <code>start</code> to <code>end</code> character
- indexes. If <code>end</code> is ommited, this method should place caret
- and <code>start</code> index
- @type start: int
- @type end: int
- @example
- zen_editor.create_selection(10, 40)
- # move caret to 15th character
- zen_editor.create_selection(15)
- """
- if offset_end is None:
- iter_start = self.buffer.get_iter_at_offset(offset_start)
- self.buffer.place_cursor(iter_start)
- else:
- iter_start = self.buffer.get_iter_at_offset(offset_start)
- iter_end = self.buffer.get_iter_at_offset(offset_end)
- self.buffer.select_range(iter_start, iter_end)
-
- def get_current_line_range(self):
- """
- Returns current line's start and end indexes
- @return: list of start and end indexes
- @example
- start, end = zen_editor.get_current_line_range();
- print('%s, %s' % (start, end))
- """
- iter_start = self.get_insert_iter()
- iter_start.set_line_offset(0)
- iter_end = iter_start.copy()
- if iter_end.forward_visible_line():
- iter_end.backward_char()
- else:
- iter_end = self.buffer.get_end_iter()
- return iter_start.get_offset(), iter_end.get_offset()
-
- def get_caret_pos(self):
- """ Returns current caret position """
- return self.get_insert_offset()
-
- def set_caret_pos(self, pos):
- """
- Set new caret position
- @type pos: int
- """
- self.buffer.place_cursor(self.buffer.get_iter_at_offset(pos))
-
- def get_current_line(self):
- """
- Returns content of current line
- @return: str
- """
- offset_start, offset_end = self.get_current_line_range()
- iter_start = self.buffer.get_iter_at_offset(offset_start)
- iter_end = self.buffer.get_iter_at_offset(offset_end)
- return self.buffer.get_text(iter_start, iter_end).decode('UTF-8')
-
- def replace_content(self, value, offset_start=None, offset_end=None):
- """
- Replace editor's content or it's part (from <code>start</code> to
- <code>end</code> index). If <code>value</code> contains
- <code>caret_placeholder</code>, the editor will put caret into
- this position. If you skip <code>start</code> and <code>end</code>
- arguments, the whole target's content will be replaced with
- <code>value</code>.
-
- If you pass <code>start</code> argument only,
- the <code>value</code> will be placed at <code>start</code> string
- index of current content.
-
- If you pass <code>start</code> and <code>end</code> arguments,
- the corresponding substring of current target's content will be
- replaced with <code>value</code>
- @param value: Content you want to paste
- @type value: str
- @param start: Start index of editor's content
- @type start: int
- @param end: End index of editor's content
- @type end: int
- """
- if offset_start is None and offset_end is None:
- iter_start = self.buffer.get_iter_at_offset(0)
- iter_end = self.get_end_iter()
- elif offset_end is None:
- iter_start = self.buffer.get_iter_at_offset(offset_start)
- iter_end = self.buffer.get_iter_at_offset(offset_start)
- else:
- iter_start = self.buffer.get_iter_at_offset(offset_start)
- iter_end = self.buffer.get_iter_at_offset(offset_end)
-
- self.buffer.delete(iter_start, iter_end)
- self.set_caret_pos(offset_start)
- self.insertion_start = self.get_insert_offset()
-
- padding = zen_actions.get_current_line_padding(self)
- padding = re.sub('[\r\n]', '', padding)
- self.buffer.insert_at_cursor(zen_core.pad_string(value, padding))
-
- self.insertion_end = self.get_insert_offset()
-
- def get_content(self):
- """
- Returns editor's content
- @return: str
- """
- iter_start = self.buffer.get_iter_at_offset(0)
- iter_end = self.get_end_iter()
- return self.buffer.get_text(iter_start, iter_end).decode('UTF-8')
-
- def get_syntax(self):
- """
- Returns current editor's syntax mode
- @return: str
- """
- lang = self.context.get_active_document().get_language()
- lang = lang and lang.get_name()
- if lang == 'CSS': lang = 'css'
- elif lang == 'XSLT': lang = 'xsl'
- else: lang = 'html'
- return lang
-
- def get_profile_name(self):
- """
- Returns current output profile name (@see zen_coding#setup_profile)
- @return {String}
- """
- return 'xhtml'
-
- def prompt(self, title):
- """
- Ask user to enter something
- @param title: Dialog title
- @type title: str
- @return: Entered data
- @since: 0.65
- """
- done, result = zen_dialog.main(self, self.context, None, title)
- if done:
- return result
- return ''
-
- def get_selection(self):
- """
- Returns current selection
- @return: str
- @since: 0.65
- """
- offset_start, offset_end = self.get_selection_range()
- iter_start = self.buffer.get_iter_at_offset(offset_start)
- iter_end = self.buffer.get_iter_at_offset(offset_end)
- return self.buffer.get_text(iter_start, iter_end).decode('UTF-8')
-
- def get_file_path(self):
- """
- Returns current editor's file path
- @return: str
- @since: 0.65
- """
- return re.sub('^file://', '', self.document.get_uri())
-
- #---------------------------------------------------------------------------------------
-
- def get_insert_iter(self):
- return self.buffer.get_iter_at_mark(self.buffer.get_insert())
-
- def get_insert_offset(self):
- return self.get_insert_iter().get_offset()
-
- def get_selection_bound_iter(self):
- return self.buffer.get_iter_at_mark(self.buffer.get_selection_bound())
-
- def get_selection_bound_offset(self):
- return self.get_selection_bound_iter().get_offset()
-
- def get_end_iter(self):
- return self.buffer.get_iter_at_offset(self.buffer.get_char_count())
-
- def get_end_offset(self):
- return self.get_end_iter().get_offset()
-
- #---------------------------------------------------------------------------------------
-
- def start_edit(self):
- # bug when the cursor is at the very beginning
- if self.insertion_start == 0:
- self.insertion_start = 1
- self.set_caret_pos(self.insertion_start)
- if not self.next_edit_point() or (self.get_insert_offset() > self.insertion_end):
- self.set_caret_pos(self.insertion_end)
-
- def show_caret(self):
- self.view.scroll_mark_onscreen(self.buffer.get_insert())
-
- #---------------------------------------------------------------------------------------
-
- def get_user_settings_error(self):
- return zen_core.get_variable('user_settings_error')
-
- #---------------------------------------------------------------------------------------
-
- def expand_with_snippet(self, abbr, mode = 0, content = ''):
- # mode_names = { 0: 'expand abbr', 1: 'expand with abbr', 2: 'wrap with abbr' }
-
- if mode < 2:
- content = zen_core.expand_abbreviation(abbr, self.get_syntax(), self.get_profile_name())
- if not content:
- return
-
- global placeholder_count
- placeholder_count = 0
- placeholder_string = zen_core.get_caret_placeholder()
- search_string = '(' + placeholder_string + '|\{' + placeholder_string + '|>' + placeholder_string + '[^<]+<' + ')'
- content = re.sub(search_string, placeholder_feed, content)
-
- offset_start, offset_end = self.get_selection_range()
- if offset_start == offset_end and mode == 0:
- offset_start -= len(abbr)
-
- snippet = ZenSnippet(abbr, content)
- iter_start = self.buffer.get_iter_at_offset(offset_start)
- iter_end = self.buffer.get_iter_at_offset(offset_end)
-
- self.snippet_document.apply_snippet(snippet, iter_start, iter_end)
-
- #---------------------------------------------------------------------------------------
-
- def expand_abbreviation(self, window):
-
- self.set_context(window, True)
-
- if self.snippet_document:
- abbr = zen_actions.find_abbreviation(self)
- if not abbr:
- return
- self.expand_with_snippet(abbr)
-
- else:
-
- self.buffer.begin_user_action()
- result = zen_actions.expand_abbreviation(self)
- if result:
- self.start_edit()
- self.buffer.end_user_action()
-
- #---------------------------------------------------------------------------------------
-
- def save_selection(self):
- self.save_offset_insert = self.get_insert_offset()
- self.save_offset_selection_bound = self.get_selection_bound_offset()
-
- def restore_selection(self):
- iter_insert = self.buffer.get_iter_at_offset(self.save_offset_insert)
- iter_selection_bound = self.buffer.get_iter_at_offset(self.save_offset_selection_bound)
- self.buffer.select_range(iter_insert, iter_selection_bound)
-
- #---------------------------------------------------------------------------------------
-
- def do_expand_with_abbreviation(self, done, abbr, last = False):
- self.buffer.begin_user_action()
- if done:
- self.buffer.undo()
- self.restore_selection()
- content = zen_core.expand_abbreviation(abbr, self.get_syntax(), self.get_profile_name())
- if content:
- if last and self.snippet_document:
- self.expand_with_snippet(abbr, 1)
- else:
- content = content.replace(zen_core.get_caret_placeholder(), '')
- self.replace_content(content, self.get_insert_offset())
- self.buffer.end_user_action()
- return not not content
-
- def expand_with_abbreviation(self, window):
- self.set_context(window, True)
- self.save_selection()
- done, self.last_expand = zen_dialog.main(self, window, self.do_expand_with_abbreviation, self.last_expand, True)
- if done and not self.snippet_document:
- self.start_edit()
-
- #---------------------------------------------------------------------------------------
-
- def _wrap_with_abbreviation(self, abbr):
-
- if not abbr:
- return None
-
- syntax = self.get_syntax()
- profile_name = self.get_profile_name()
-
- start_offset, end_offset = self.get_selection_range()
- content = self.get_content()
-
- if start_offset == end_offset:
- rng = html_matcher.match(content, start_offset, profile_name)
- if rng[0] is None:
- return None
- else:
- start_offset, end_offset = rng
-
- start_offset, end_offset = zen_actions.narrow_to_non_space(content, start_offset, end_offset)
- line_bounds = zen_actions.get_line_bounds(content, start_offset)
- padding = zen_actions.get_line_padding(content[line_bounds[0]:line_bounds[1]])
-
- new_content = content[start_offset:end_offset]
- return zen_core.wrap_with_abbreviation(abbr, zen_actions.unindent_text(new_content, padding), syntax, profile_name)
-
- def do_wrap_with_abbreviation(self, done, abbr, last = False):
- self.buffer.begin_user_action()
- if done:
- self.buffer.undo()
- self.restore_selection()
- content = self._wrap_with_abbreviation(abbr)
- if content:
- if last and self.snippet_document:
- self.expand_with_snippet(abbr, 2, content)
- else:
- content = content.replace(zen_core.get_caret_placeholder(), '')
- offset_start, offset_end = self.get_selection_range()
- self.replace_content(content, offset_start, offset_end)
- self.buffer.end_user_action()
- return not not content
-
- def wrap_with_abbreviation(self, window):
- self.set_context(window, True)
- self.save_selection()
- done, self.last_wrap = zen_dialog.main(self, window, self.do_wrap_with_abbreviation, self.last_wrap, True)
- if done and not self.snippet_document:
- self.start_edit()
-
- #---------------------------------------------------------------------------------------
-
- def zenify(self, window, mode):
- self.set_context(window)
- offset_start, offset_end, content = self.prepare_nav(window)
- result = self.html_navigation.zenify(offset_start, offset_end, content, mode)
- if result:
- self.save_selection()
- self.prompt(result)
- self.restore_selection()
-
- #---------------------------------------------------------------------------------------
-
- def match_pair_inward(self, window):
- offset_start, offset_end, content = self.prepare_nav(window)
- offset_start, offset_end = self.html_navigation.inner_bounds(offset_start, offset_end, content)
- if not (offset_start is None or offset_end is None):
- self.create_selection(offset_start, offset_end)
-
- def match_pair_outward(self, window):
- offset_start, offset_end, content = self.prepare_nav(window)
- offset_start, offset_end = self.html_navigation.outer_bounds(offset_start, offset_end, content)
- if not (offset_start is None or offset_end is None):
- self.create_selection(offset_start, offset_end)
-
- def merge_lines(self, window):
- self.set_context(window)
- self.buffer.begin_user_action()
- result = zen_actions.merge_lines(self)
- self.buffer.end_user_action()
- return result
-
- #---------------------------------------------------------------------------------------
-
- def prepare_nav(self, window):
- self.set_context(window)
- offset_start, offset_end = self.get_selection_range()
- content = self.get_content()
- if not self.html_navigation:
- self.html_navigation = HtmlNavigation(content)
- return offset_start, offset_end, content
-
- #---------------------------------------------------------------------------------------
-
- def new_tag(self, window, direction):
-
- offset_start, offset_end, content = self.prepare_nav(window)
-
- if direction == 'next':
- node = self.html_navigation.next_tag(offset_start, offset_end, content)
-
- else:
- node = self.html_navigation.previous_tag(offset_start, offset_end, content)
-
- if node:
- iter_start = self.buffer.get_iter_at_offset(node.start)
- iter_end = self.buffer.get_iter_at_offset(node.end)
- self.create_selection(node.start, node.end)
- self.show_caret()
-
- def prev_tag(self, window):
- self.new_tag(window, 'previous')
-
- def next_tag(self, window):
- self.new_tag(window, 'next')
-
- #---------------------------------------------------------------------------------------
-
- def new_node(self, window, direction, with_spaces = True):
-
- offset_start, offset_end, content = self.prepare_nav(window)
-
- while True:
-
- if direction == 'next':
- node = self.html_navigation.next_node(offset_start, offset_end, content)
-
- else:
- node = self.html_navigation.previous_node(offset_start, offset_end, content)
-
- if node:
-
- iter_start = self.buffer.get_iter_at_offset(node.start)
- iter_end = self.buffer.get_iter_at_offset(node.end)
-
- found = self.buffer.get_text(iter_start, iter_end).decode('UTF-8')
- if not with_spaces and found.isspace() and found.find('\n') != -1:
- offset_start = node.start
- offset_end = node.end
-
- else:
- self.create_selection(node.start, node.end)
- break
-
- else:
- break
-
- self.show_caret()
-
- def prev_node(self, window):
- self.new_node(window, 'previous')
-
- def next_node(self, window):
- self.new_node(window, 'next')
-
- #---------------------------------------------------------------------------------------
-
- def prev_edit_point(self, window=None):
- if window:
- self.set_context(window)
- result = zen_actions.prev_edit_point(self)
- self.show_caret()
- return result
-
- def next_edit_point(self, window=None):
- if window:
- self.set_context(window)
- result = zen_actions.next_edit_point(self)
- self.show_caret()
- return result
-
- #---------------------------------------------------------------------------------------
-
- def update_image_size(self, window):
- self.set_context(window)
- self.buffer.begin_user_action()
- update_image_size(self)
- self.buffer.end_user_action()
+ def __init__(self, window_helper):
+
+ self.window = window_helper.window
+
+ default_locale = locale.getdefaultlocale()[0]
+ if default_locale:
+ lang = re.sub(r'_[^_]+$', '', default_locale)
+ if lang != default_locale:
+ zen_core.set_variable('lang', lang)
+ zen_core.set_variable('locale', default_locale.replace('_', '-'))
+ else:
+ zen_core.set_variable('lang', default_locale)
+ zen_core.set_variable('locale', default_locale)
+
+ self.last_wrap = ''
+ self.last_expand = ''
+ self.last_lorem_ipsum = 'list 5*5'
+
+ self.placeholder = zen_core.get_caret_placeholder()
+
+ self.html_navigation = None
+ self.snippet_document = None
+
+ # --- Original interface ---------------------------------------------------
+
+ def set_context(self):
+
+ self.document = self.window.get_active_document()
+ self.view = self.window.get_active_view()
+ if self.view:
+ self.buffer = self.view.get_buffer()
+
+ if self.document:
+ zen_core.set_variable('charset', self.document.get_encoding().get_charset())
+
+ if self.view:
+ if self.view.get_insert_spaces_instead_of_tabs():
+ zen_core.set_variable('indentation', " " * self.view.get_tab_width())
+ else:
+ zen_core.set_variable('indentation', "\t")
+
+ #zen_core.set_newline(???)
+
+ if self.document:
+ if not self.snippet_document:
+ try:
+ sys.path.append('/usr/lib/gedit-2/plugins')
+ from snippets.Document import Document
+ self.snippet_document = Document(None, self.view)
+ except:
+ self.snippet_document = None
+
+ def get_selection_range(self):
+
+ offset_start = self.get_insert_offset()
+ offset_end = self.get_selection_bound_offset()
+
+ if offset_start < offset_end:
+ return offset_start, offset_end
+
+ return offset_end, offset_start
+
+ def create_selection(self, offset_start, offset_end=None):
+
+ if offset_end is None:
+ iter_start = self.buffer.get_iter_at_offset(offset_start)
+ self.buffer.place_cursor(iter_start)
+
+ else:
+ iter_start = self.buffer.get_iter_at_offset(offset_start)
+ iter_end = self.buffer.get_iter_at_offset(offset_end)
+ self.buffer.select_range(iter_start, iter_end)
+
+ def get_current_line_range(self):
+
+ iter_start = self.get_insert_iter()
+ iter_start.set_line_offset(0)
+ iter_end = iter_start.copy()
+
+ if iter_end.forward_visible_line():
+ iter_end.backward_char()
+
+ else:
+ iter_end = self.buffer.get_end_iter()
+
+ return iter_start.get_offset(), iter_end.get_offset()
+
+ def get_caret_pos(self):
+ return self.get_insert_offset()
+
+ def set_caret_pos(self, pos):
+ self.buffer.place_cursor(self.buffer.get_iter_at_offset(pos))
+
+ def get_current_line(self):
+
+ offset_start, offset_end = self.get_current_line_range()
+ iter_start = self.buffer.get_iter_at_offset(offset_start)
+ iter_end = self.buffer.get_iter_at_offset(offset_end)
+ return self.buffer.get_text(iter_start, iter_end).decode('UTF-8')
+
+ def replace_content(self, value, offset_start=None, offset_end=None):
+
+ if offset_start is None and offset_end is None:
+ iter_start = self.buffer.get_iter_at_offset(0)
+ iter_end = self.get_end_iter()
+
+ elif offset_end is None:
+ iter_start = self.buffer.get_iter_at_offset(offset_start)
+ iter_end = self.buffer.get_iter_at_offset(offset_start)
+
+ else:
+ iter_start = self.buffer.get_iter_at_offset(offset_start)
+ iter_end = self.buffer.get_iter_at_offset(offset_end)
+
+ self.buffer.delete(iter_start, iter_end)
+ self.set_caret_pos(offset_start)
+ self.insertion_start = self.get_insert_offset()
+
+ padding = zen_actions.get_current_line_padding(self)
+ padding = re.sub('[\r\n]', '', padding)
+ self.buffer.insert_at_cursor(zen_core.pad_string(value, padding))
+
+ self.insertion_end = self.get_insert_offset()
+
+ def get_content(self):
+ iter_start = self.buffer.get_iter_at_offset(0)
+ iter_end = self.get_end_iter()
+ return self.buffer.get_text(iter_start, iter_end).decode('UTF-8')
+
+ def get_syntax(self):
+ lang = self.window.get_active_document().get_language()
+ lang = lang and lang.get_name()
+ if lang == 'CSS': lang = 'css'
+ elif lang == 'XSLT': lang = 'xsl'
+ else: lang = 'html'
+ return lang
+
+ def get_profile_name(self):
+ return 'xhtml'
+
+ def prompt(self, title):
+ done, result = zen_dialog.main(self, self.window, None, title)
+ if done:
+ return result
+ return ''
+
+ def get_selection(self):
+ offset_start, offset_end = self.get_selection_range()
+ iter_start = self.buffer.get_iter_at_offset(offset_start)
+ iter_end = self.buffer.get_iter_at_offset(offset_end)
+ return self.buffer.get_text(iter_start, iter_end).decode('UTF-8')
+
+ def get_file_path(self):
+ return re.sub('^file://', '', self.document.get_uri())
+
+ # --- Iter and offset oneliners --------------------------------------------
+
+ def get_insert_iter(self):
+ return self.buffer.get_iter_at_mark(self.buffer.get_insert())
+
+ def get_insert_offset(self):
+ return self.get_insert_iter().get_offset()
+
+ def get_selection_bound_iter(self):
+ return self.buffer.get_iter_at_mark(self.buffer.get_selection_bound())
+
+ def get_selection_bound_offset(self):
+ return self.get_selection_bound_iter().get_offset()
+
+ def get_end_iter(self):
+ return self.buffer.get_iter_at_offset(self.buffer.get_char_count())
+
+ def get_end_offset(self):
+ return self.get_end_iter().get_offset()
+
+ #--- Miscellaneous stuff ---------------------------------------------------
+
+ def start_edit(self):
+ # bug when the cursor is at the very beginning
+ if self.insertion_start == 0:
+ self.insertion_start = 1
+ self.set_caret_pos(self.insertion_start)
+ if not self.next_edit_point() or (self.get_insert_offset() > self.insertion_end):
+ self.set_caret_pos(self.insertion_end)
+
+ def show_caret(self):
+ self.view.scroll_mark_onscreen(self.buffer.get_insert())
+
+ def get_user_settings_error(self):
+ return zen_core.get_variable('user_settings_error')
+
+ def save_selection(self):
+ self.save_offset_insert = self.get_insert_offset()
+ self.save_offset_selection_bound = self.get_selection_bound_offset()
+
+ def restore_selection(self):
+ iter_insert = self.buffer.get_iter_at_offset(self.save_offset_insert)
+ iter_selection_bound = self.buffer.get_iter_at_offset(self.save_offset_selection_bound)
+ self.buffer.select_range(iter_insert, iter_selection_bound)
+
+ def prepare_nav(self):
+ offset_start, offset_end = self.get_selection_range()
+ content = self.get_content()
+ if not self.html_navigation:
+ self.html_navigation = HtmlNavigation(content)
+ return offset_start, offset_end, content
+
+ #--- Snippet hook ----------------------------------------------------------
+
+ def expand_with_snippet(self, abbr, mode = 0, content = ''):
+ # mode_names = { 0: 'expand abbr', 1: 'expand with abbr', 2: 'wrap with abbr' }
+
+ if mode < 2:
+ content = zen_core.expand_abbreviation(abbr, self.get_syntax(), self.get_profile_name())
+ if not content:
+ return
+
+ global placeholder_count
+ placeholder_count = 0
+ search_string = '(' + self.placeholder + '|\{' + self.placeholder + '|>' + self.placeholder + '[^<]+<' + ')'
+ content = re.sub(search_string, placeholder_feed, content)
+
+ offset_start, offset_end = self.get_selection_range()
+ if offset_start == offset_end and mode == 0:
+ offset_start -= len(abbr)
+
+ snippet = ZenSnippet(abbr, content)
+ iter_start = self.buffer.get_iter_at_offset(offset_start)
+ iter_end = self.buffer.get_iter_at_offset(offset_end)
+
+ self.snippet_document.apply_snippet(snippet, iter_start, iter_end)
+
+ #--- Expand abbreviation ---------------------------------------------------
+
+ def expand_abbreviation(self):
+
+ zen_core.set_caret_placeholder(self.placeholder)
+
+ if self.snippet_document:
+ abbr = zen_actions.find_abbreviation(self)
+ if abbr:
+ self.expand_with_snippet(abbr)
+
+ else:
+
+ self.buffer.begin_user_action()
+ result = zen_actions.expand_abbreviation(self)
+ #TODO: filter explicit placerholders
+ if result:
+ self.start_edit()
+ self.buffer.end_user_action()
+
+ zen_core.set_caret_placeholder('')
+
+ #--- Expand with abbreviation ----------------------------------------------
+
+ def callback_expand_with_abbreviation(self, done, abbr, last = False):
+
+ self.buffer.begin_user_action()
+
+ if done:
+ self.buffer.undo()
+ self.restore_selection()
+
+ content = zen_core.expand_abbreviation(abbr, self.get_syntax(), self.get_profile_name())
+
+ if content:
+
+ if last and self.snippet_document:
+ self.expand_with_snippet(abbr, 1)
+
+ else:
+ content = content.replace(zen_core.get_caret_placeholder(), '')
+ #TODO: filter explicit placeholders
+ self.replace_content(content, self.get_insert_offset())
+
+ self.buffer.end_user_action()
+
+ return not not content
+
+ def expand_with_abbreviation(self):
+
+ zen_core.set_caret_placeholder(self.placeholder)
+ self.save_selection()
+
+ done, self.last_expand = zen_dialog.main(self, self.window, self.callback_expand_with_abbreviation, self.last_expand, True)
+
+ if done and not self.snippet_document:
+ self.start_edit()
+
+ zen_core.set_caret_placeholder('')
+
+ #--- Wrap with abbreviation ------------------------------------------------
+
+ def core_wrap_with_abbreviation(self, abbr):
+
+ if not abbr:
+ return None
+
+ syntax = self.get_syntax()
+ profile_name = self.get_profile_name()
+
+ start_offset, end_offset = self.get_selection_range()
+ content = self.get_content()
+
+ if start_offset == end_offset:
+ rng = html_matcher.match(content, start_offset, profile_name)
+ if rng[0] is None:
+ return None
+ else:
+ start_offset, end_offset = rng
+
+ start_offset, end_offset = zen_actions.narrow_to_non_space(content, start_offset, end_offset)
+ line_bounds = zen_actions.get_line_bounds(content, start_offset)
+ padding = zen_actions.get_line_padding(content[line_bounds[0]:line_bounds[1]])
+
+ new_content = content[start_offset:end_offset]
+ return zen_core.wrap_with_abbreviation(abbr, zen_actions.unindent_text(new_content, padding), syntax, profile_name)
+
+ def callback_wrap_with_abbreviation(self, done, abbr, last = False):
+
+ self.buffer.begin_user_action()
+
+ if done:
+ self.buffer.undo()
+ self.restore_selection()
+ content = self.core_wrap_with_abbreviation(abbr)
+
+ if content:
+
+ if last and self.snippet_document:
+ self.expand_with_snippet(abbr, 2, content)
+
+ else:
+ content = content.replace(zen_core.get_caret_placeholder(), '')
+ #TODO: filter explicit placeholders
+ offset_start, offset_end = self.get_selection_range()
+ self.replace_content(content, offset_start, offset_end)
+
+ self.buffer.end_user_action()
+
+ return not not content
+
+ def wrap_with_abbreviation(self):
+
+ zen_core.set_caret_placeholder(self.placeholder)
+ self.save_selection()
+
+ done, self.last_wrap = zen_dialog.main(self, self.window, self.callback_wrap_with_abbreviation, self.last_wrap, True)
+
+ if done and not self.snippet_document:
+ self.start_edit()
+
+ zen_core.set_caret_placeholder('')
+
+ #--- Zenify ----------------------------------------------------------------
+
+ def zenify(self, mode):
+
+ offset_start, offset_end, content = self.prepare_nav()
+ result = self.html_navigation.zenify(offset_start, offset_end, content, mode)
+
+ if result:
+ self.save_selection()
+ self.prompt(result)
+ self.restore_selection()
+
+ #--- Lorem ipsum -----------------------------------------------------------
+
+ def callback_lorem_ipsum(self, done, cmd, last = False):
+ self.buffer.begin_user_action()
+ if done:
+ self.buffer.undo()
+ self.restore_selection()
+ content = lorem_ipsum(cmd)
+ if content:
+ self.replace_content(content, self.get_insert_offset())
+ self.buffer.end_user_action()
+ return not not content
+
+ def lorem_ipsum(self):
+ self.save_selection()
+ done, self.last_lorem_ipsum = zen_dialog.main(self, self.window, self.callback_lorem_ipsum, self.last_lorem_ipsum, False)
+
+ #--- Select inward or outward ----------------------------------------------
+
+ def match_pair_inward(self):
+ offset_start, offset_end, content = self.prepare_nav()
+ offset_start, offset_end = self.html_navigation.inner_bounds(offset_start, offset_end, content)
+ if not (offset_start is None or offset_end is None):
+ self.create_selection(offset_start, offset_end)
+
+ def match_pair_outward(self):
+ offset_start, offset_end, content = self.prepare_nav()
+ offset_start, offset_end = self.html_navigation.outer_bounds(offset_start, offset_end, content)
+ if not (offset_start is None or offset_end is None):
+ self.create_selection(offset_start, offset_end)
+
+ #--- Tag jumps -------------------------------------------------------------
+
+ def new_tag(self, direction):
+
+ offset_start, offset_end, content = self.prepare_nav()
+
+ if direction == 'next':
+ node = self.html_navigation.next_tag(offset_start, offset_end, content)
+
+ else:
+ node = self.html_navigation.previous_tag(offset_start, offset_end, content)
+
+ if node:
+ iter_start = self.buffer.get_iter_at_offset(node.start)
+ iter_end = self.buffer.get_iter_at_offset(node.end)
+ self.create_selection(node.start, node.end)
+ self.show_caret()
+
+ def prev_tag(self):
+ self.new_tag('previous')
+
+ def next_tag(self):
+ self.new_tag('next')
+
+ #--- Node jumps ------------------------------------------------------------
+
+ def new_node(self, direction, with_spaces = True):
+
+ offset_start, offset_end, content = self.prepare_nav()
+
+ while True:
+
+ if direction == 'next':
+ node = self.html_navigation.next_node(offset_start, offset_end, content)
+
+ else:
+ node = self.html_navigation.previous_node(offset_start, offset_end, content)
+
+ if node:
+
+ iter_start = self.buffer.get_iter_at_offset(node.start)
+ iter_end = self.buffer.get_iter_at_offset(node.end)
+
+ found = self.buffer.get_text(iter_start, iter_end).decode('UTF-8')
+ if not with_spaces and found.isspace() and found.find('\n') != -1:
+ offset_start = node.start
+ offset_end = node.end
+
+ else:
+ self.create_selection(node.start, node.end)
+ break
+
+ else:
+ break
+
+ self.show_caret()
+
+ def prev_node(self):
+ self.new_node('previous')
+
+ def next_node(self):
+ self.new_node('next')
+
+ #--- Edit points jumps -----------------------------------------------------
+
+ def prev_edit_point(self):
+ result = zen_actions.prev_edit_point(self)
+ self.show_caret()
+ return result
+
+ def next_edit_point(self):
+ result = zen_actions.next_edit_point(self)
+ self.show_caret()
+ return result
- def encode_decode_base64(self, window):
- self.set_context(window)
- self.buffer.begin_user_action()
- try:
- zen_actions.encode_decode_base64(self)
- except:
- pass
- self.buffer.end_user_action()
+ #--- Image actions ---------------------------------------------------------
- #---------------------------------------------------------------------------------------
+ def update_image_size(self):
+ self.buffer.begin_user_action()
+ update_image_size(self)
+ self.buffer.end_user_action()
- def remove_tag(self, window):
- self.set_context(window)
- self.buffer.begin_user_action()
- result = zen_actions.remove_tag(self)
- self.buffer.end_user_action()
- return result
+ def encode_decode_base64(self):
+ self.buffer.begin_user_action()
+ try:
+ zen_actions.encode_decode_base64(self)
+ except:
+ pass
+ self.buffer.end_user_action()
- def split_join_tag(self, window):
- self.set_context(window)
- self.buffer.begin_user_action()
- result = zen_actions.split_join_tag(self)
- self.buffer.end_user_action()
- return result
+ #--- Other edition actions -------------------------------------------------
- def toggle_comment(self, window):
- self.set_context(window)
- self.buffer.begin_user_action()
- result = zen_actions.toggle_comment(self)
- self.buffer.end_user_action()
- return result
+ def merge_lines(self):
+ self.buffer.begin_user_action()
+ zen_actions.merge_lines(self)
+ self.buffer.end_user_action()
- #---------------------------------------------------------------------------------------
+ def remove_tag(self):
+ self.buffer.begin_user_action()
+ zen_actions.remove_tag(self)
+ self.buffer.end_user_action()
- def do_lorem_ipsum(self, done, cmd, last = False):
- self.buffer.begin_user_action()
- if done:
- self.buffer.undo()
- self.restore_selection()
- content = lorem_ipsum(cmd)
- if content:
- self.replace_content(content, self.get_insert_offset())
- self.buffer.end_user_action()
- return not not content
-
- def lorem_ipsum(self, window):
- self.set_context(window)
- self.save_selection()
- done, self.last_lorem_ipsum = zen_dialog.main(self, window, self.do_lorem_ipsum, self.last_lorem_ipsum, False)
+ def split_join_tag(self):
+ self.buffer.begin_user_action()
+ zen_actions.split_join_tag(self)
+ self.buffer.end_user_action()
+ def toggle_comment(self):
+ self.buffer.begin_user_action()
+ zen_actions.toggle_comment(self)
+ self.buffer.end_user_action()

0 comments on commit 714280b

Please sign in to comment.