def expand_with_snippet(self, abbr, mode = 0): # 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()) else: content = self.core_wrap_with_abbreviation(abbr) if content: 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[self.view].apply_snippet(snippet, iter_start, iter_end) return content
def expand_abbreviation(self): zen_core.set_caret_placeholder(self.placeholder) abbr = zen_actions.find_abbreviation(self) if abbr: if self.snippet_document[self.view]: self.expand_with_snippet(abbr) else: self.buffer.begin_user_action() content = zen_core.expand_abbreviation(abbr, self.get_syntax(), self.get_profile_name()) if content: content = content.replace(self.placeholder, '') content = re.sub('\$\d+|\$\{\d+:[^\}]*\}', '', content) unused, offset_end = self.get_selection_range() self.replace_content(content, offset_end - len(abbr), offset_end) self.start_edit() self.buffer.end_user_action() zen_core.set_caret_placeholder('')
def do_expand_with_abbreviation(self, done, abbr): 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: self.replace_content(content, self.get_insert_offset()) self.buffer.end_user_action() return not not content
def code(): text = str(raw_input("Enter text")) print(text) p=zencoding.find_abbr_in_line(text,len(text)) d=zencoding.expand_abbreviation(text,'html','xhtml') d=zencoding.wrap_with_abbreviation('body',d) print(d) print(p)
def expand_zencode(self, window): """Take the shorthand code, expand it, and stick it back in the document.""" view = window.get_active_view() buffer = view.get_buffer() cursor_iter = buffer.get_iter_at_mark(buffer.get_insert()) statusbar = window.get_statusbar() # Grab the current line. line = self.get_line(buffer, cursor_iter) # Get shorthand from selection... is_selection = False if buffer.get_has_selection(): insert_iter = buffer.get_iter_at_mark(buffer.get_insert()) bound_iter = buffer.get_iter_at_mark(buffer.get_selection_bound()) before = buffer.get_text(bound_iter, insert_iter) cursor_iter = (bound_iter if bound_iter.compare(insert_iter) == 1 else insert_iter).copy() buffer.place_cursor(cursor_iter) is_selection = True # ... or from previous word else: before = self.get_shorthand(line) if not before: return # Generate expanded code from the shorthand code based on the document's language. lang = self.get_language(window) if lang == 'CSS': lang = 'css' elif lang == 'XSLT': lang = 'xsl' else: lang = 'html' after = zen_core.expand_abbreviation(before, lang, 'xhtml') if not after: if is_selection: buffer.select_range(insert_iter, bound_iter) return # Indent the expanded code according to editor's preferences. after = self.indent_code(line, after, window) # Replace the shorthand code with the expanded code. if self.replace_with_expanded(cursor_iter, buffer, before, after, window.get_active_document()): statusbar.push(statusbar.get_context_id('ZenCodingPlugin'), 'Expanded shorthand code into the real stuff.') else: statusbar.push(statusbar.get_context_id('ZenCodingPlugin'), 'Code couldn\'t expand. Try checking your syntax for mistakes.')
def expand_abbreviation(editor, syntax, profile_name='xhtml'): """ Find from current caret position and expand abbreviation in editor @param editor: Editor instance @type editor: ZenEditor @param syntax: Syntax type (html, css, etc.) @type syntax: str @param profile_name: Output profile name (html, xml, xhtml) @type profile_name: str @return: True if abbreviation was expanded successfully """ range_start, caret_pos = editor.get_selection_range() abbr = find_abbreviation(editor) content = '' if abbr: content = zen.expand_abbreviation(abbr, syntax, profile_name) if content: editor.replace_content(content, caret_pos - len(abbr), caret_pos) return True return False
def callback_expand_with_abbreviation(self, done, abbr, last = False): self.buffer.begin_user_action() if done: self.buffer.undo() self.restore_selection() if last and self.snippet_document[self.view]: content = self.expand_with_snippet(abbr, 1) else: content = zen_core.expand_abbreviation(abbr, self.get_syntax(), self.get_profile_name()) if content: content = content.replace(self.placeholder, '') content = re.sub('\$\d+|\$\{\d+:[^\}]*\}', '', content) self.replace_content(content, self.get_insert_offset()) self.buffer.end_user_action() return not not content
# 4th argument: profile name if argl > 4: profile = sys.argv[4] # Find starting white space if (re.search('^(\s+)', abbr) != None): indent = re.search('^(\s+)', abbr).group(1) # Capture any white space at end if (re.search('(\s+)$', abbr) != None): endwhite = re.search('(\s+)$', abbr).group(1) # Remove white space for processing, including any end white space abbr = re.sub('(^\s+)|(\s+$)', '', abbr) if abbr: if exp_type == 'exp': result = zen_core.expand_abbreviation(abbr, doc_type, profile) elif exp_type == 'wrap': try: # Need to get selected text selFileDest = (os.path.expanduser("~")) # get home directory selFile = open(selFileDest+'/Library/Application Support/TextWrangler/Scripts/zencoding_1_1_1/zen_selected_temp', 'r') selected = selFile.read() except IOError: selected = '' # empty selection result = zen_core.wrap_with_abbreviation(abbr, selected, doc_type, profile) if result: # Add indentation to result result = re.sub(re.compile('^(.)', re.M), indent+r'\1', result) # Remove for first line result = re.sub(re.compile('^'+indent), r'', result) sys.stdout.write(result+endWhite)
# 4th argument: profile name if argl > 4: profile = sys.argv[4] # Find starting white space if (re.search('^(\s+)', abbr) != None): indent = re.search('^(\s+)', abbr).group(1) # Capture any white space at end if (re.search('(\s+)$', abbr) != None): endwhite = re.search('(\s+)$', abbr).group(1) # Remove white space for processing, including any end white space abbr = re.sub('(^\s+)|(\s+$)', '', abbr) if abbr: if exp_type == 'exp': result = zen_core.expand_abbreviation(abbr, doc_type, profile) elif exp_type == 'wrap': try: # Need to get selected text selFileDest = (os.path.expanduser("~")) # get home directory selFile = open( '~/Dropbox/Library/Application Support/BBEdit/Scripts/zencoding_BBEdit/zen_selected_temp', 'r') selected = selFile.read() except IOError: selected = '' # empty selection result = zen_core.wrap_with_abbreviation(abbr, selected, doc_type, profile) if result: # Add indentation to result result = re.sub(re.compile('^(.)', re.M), indent + r'\1', result) sys.stdout.write(result + endWhite)