def cut(self): import clipboard i=editor.get_selection() t=editor.get_text() clipboard.set(t[i[0]:i[1]]) editor.replace_text(i[0],i[1], '') editor.set_selection(i[0],i[0])
def setsel(): sel=new_tab['selection'] while editor.get_selection()[0] < sel[0]: import time editor.replace_text(sel[0],sel[0],'') editor.replace_text(sel[0]+400,sel[0]+400,'') time.sleep(0.25) editor.set_selection(*sel)
def indent(self): """indent selected lines by one tab""" import editor import re i=editor.get_line_selection() t=editor.get_text() # replace every occurance of newline with newline plus indent, except last newline editor.replace_text(i[0],i[1]-1,INDENTSTR+re.sub(r'\n',r'\n'+INDENTSTR,t[i[0]:i[1]-1])) editor.set_selection(i[0],i[1]-len(t)+len(editor.get_text()))
def unindent(self): """unindent selected lines all the way""" import editor import textwrap i=editor.get_line_selection() t=editor.get_text() editor.replace_text(i[0],i[1], textwrap.dedent(t[i[0]:i[1]])) editor.set_selection(i[0],i[1]-len(t)+len(editor.get_text()))
def comment(self): """" comment out selected lines""" import editor import re COMMENT='#' i=editor.get_line_selection() t=editor.get_text() # replace every occurance of newline with ewline plus COMMENT, except last newline editor.replace_text(i[0],i[1]-1,COMMENT+re.sub(r'\n',r'\n'+COMMENT,t[i[0]:i[1]-1])) editor.set_selection(i[0],i[1]-len(t)+len(editor.get_text()))
def comment_action(sender): """" comment out selected lines""" import re COMMENT = '#' i = editor.get_line_selection() t = editor.get_text() # replace every occurance of newline with ewline plus COMMENT, except last newline editor.replace_text( i[0], i[1] - 1, COMMENT + re.sub(r'\n', r'\n' + COMMENT, t[i[0]:i[1] - 1])) editor.set_selection(i[0], i[1] - len(t) + len(editor.get_text()))
def unindent(self): """unindent selected lines all the way""" import editor import textwrap i = editor.get_line_selection() t = editor.get_text() editor.replace_text(i[0], i[1], textwrap.dedent(t[i[0]:i[1]])) editor.set_selection(i[0], i[1] - len(t) + len(editor.get_text()))
def refresh_editor(): #reload current file in editor # TODO: only reload if the file was recently updated... try: sel=editor.get_selection() editor.open_file(editor.get_path()) import time time.sleep(0.5) #let the file load editor.replace_text(sel[0],sel[0],'') #force scroll editor.set_selection(sel[0],sel[1]) except: print 'Could not refresh editor. continuing anyway'
def uncomment_action(self): """" uncomment selected lines""" import re COMMENT = '#' i = editor.get_line_selection() t = editor.get_text() # replace every occurance of newline # with newline, except last newline if all([x.startswith('#') for x in t[i[0]:i[1] - 1].split(r'\n')]): editor.replace_text( i[0], i[1] - 1, re.sub(r'^' + COMMENT, r'', t[i[0]:i[1] - 1], flags=re.MULTILINE)) editor.set_selection(i[0], i[1] - len(t) + len(editor.get_text()))
def refresh_editor(): #reload current file in editor # TODO: only reload if the file was recently updated... try: sel = editor.get_selection() editor.open_file(editor.get_path()) import time time.sleep(0.5) #let the file load editor.replace_text(sel[0], sel[0], '') #force scroll editor.set_selection(sel[0], sel[1]) except: print('Could not refresh editor. continuing anyway')
def indent(): """indent selected lines by one tab""" import editor import re INDENTSTR = '\t' #two spaces i = editor.get_line_selection() t = editor.get_text() # replace every occurance of newline with ewline plus indent, except last newline editor.replace_text( i[0], i[1] - 1, INDENTSTR + re.sub(r'\n', r'\n' + INDENTSTR, t[i[0]:i[1] - 1])) editor.set_selection(i[0], i[1] - len(t) + len(editor.get_text()))
def uncomment(self): """" uncomment selected lines""" import editor import re COMMENT='#' i=editor.get_line_selection() t=editor.get_text() # replace every occurance of newline # with newline, except last newline if all( [x.startswith('#') for x in t[i[0]:i[1]-1].split(r'\n')]): editor.replace_text(i[0],i[1]-1,re.sub(r'^'+COMMENT,r'',t[i[0]:i[1]-1],flags=re.MULTILINE)) editor.set_selection(i[0],i[1]-len(t)+len(editor.get_text()))
def close_settings(self): while self.__iR != 0: pass if self.settings_file[:7] == 'http://': pass else: sPl = plistlib.writePlistToString(self.settings_dict) sN = self.__sA[0:self.__iS] + sPl[:-1] + self.__sA[self.__iF:] with open(self.settings_file, 'w') as fS: fS.write(sN) if editor.get_path() == self.settings_file: tS = editor.get_selection() iD = 0 if tS[0] <= self.__iF else len(sN) - len(self.__sA) editor.replace_text(0, len(editor.get_text()), sN[:-1]) editor.set_selection(tS[0] + iD, tS[1] + iD) return True self.settings_dict = None
def uncomment(): """" uncomment selected lines""" import editor import re COMMENT = '#' i = editor.get_line_selection() t = editor.get_text() # replace every occurance of newline # with newline, except last newline # todo.. probably should verify every line has comment... # num lines= re.findall if all([x.startswith('#') for x in t[i[0]:i[1] - 1].split(r'\n')]): editor.replace_text( i[0], i[1] - 1, re.sub(r'^' + COMMENT, r'', t[i[0]:i[1] - 1], flags=re.MULTILINE)) editor.set_selection(i[0], i[1] - len(t) + len(editor.get_text()))
def toggle_comments(): selection_range = editor.get_selection() if not selection_range: # No file opened in the editor return text = editor.get_text() selected_lines_range = editor.get_line_selection() selected_lines_text = text[selected_lines_range[0]:selected_lines_range[1]] selected_lines = selected_lines_text.splitlines() if len(selected_lines) > 1: # Ignore the last line selection if there's just cursor at the beginning of # this line and nothing is selected last_line_length = len(selected_lines[-1]) if selection_range[1] == selected_lines_range[1] - last_line_length: del selected_lines[-1] selected_lines_range = (selected_lines_range[0], selected_lines_range[1] - last_line_length - 1) is_commented = selected_lines_text.strip().startswith('#') replacement = '' for line in selected_lines: if is_commented: if line.strip().startswith('#'): replacement += line[line.find('#') + 1:] + '\n' else: replacement += line + '\n' else: replacement += '#' + line + '\n' # Remove trailing \n to avoid empty new lines replacement = replacement[:-1] editor.replace_text(selected_lines_range[0], selected_lines_range[1], replacement) editor.set_selection(selected_lines_range[0], selected_lines_range[0] + len(replacement))
def __bCA(self, sender): # from: pythonista/docs/editor -------------- text = editor.get_text() selection = editor.get_line_selection() selected_text = text[selection[0]:selection[1]] is_comment = selected_text.strip().startswith('#') replacement = '' for line in selected_text.splitlines(): if is_comment: if line.strip().startswith('#'): replacement += line[line.find('#') + 1:] + '\n' else: replacement += line + '\n' else: replacement += '#' + line + '\n' editor.replace_text(selection[0], selection[1], replacement) editor.set_selection(selection[0], selection[0] + len(replacement) - 1) # end ----------------------------------------- self.close()
def apply_change_set(change_set, path=None, initial_selection=None): # # Why not project.do(change_set)? # # - iCloud, Files, ... - there're special functions to update files, not a simple file system # - we have to be sure just one file (= opened) is updated only, because of Pythonista reloading, ... # for change in change_set.changes: if path and not change.resource.real_path == path: # Make sure we modify opened file only continue if path and not path == editor.get_path(): # Make sure that user didn't switch tab, close tab, ... continue end = len(editor.get_text()) - 1 editor.replace_text(0, end, change.new_contents) if initial_selection: editor.set_selection(*initial_selection, scroll=True)
def scroll_to_line(line_number, relative=False): text = editor.get_text() if not text: return if relative: current_line = get_line_number() line_count = get_line_count() if current_line is None or line_count is None: return line_number = max(min(current_line + line_number, line_count), 1) # https://github.com/omz/Pythonista-Issues/issues/365 start = 0 for index, line in enumerate(text.splitlines(True)): if index == line_number - 1: editor.set_selection(start) return start += len(line) editor.set_selection(start)
def main(): path = editor.get_path() if not path: return if not path.endswith('.py'): return editor.clear_annotations() flake8_options = get_config_value('analyzer.flake8', None) selection = editor.get_selection() text = _editor_text() if flake8_options: annotations = _flake8_annotations(os.path.abspath(path), flake8_options) else: annotations = _pep8_annotations(text, ignore=_ignore_codes(), max_line_length=_max_line_length()) annotations += _pyflakes_annotations(path, text) if not annotations: if selection: editor.set_selection(selection[0], scroll=True) console.hud_alert('No Issues Found', 'iob:checkmark_32', _hud_alert_delay()) return None scroll = True by_line = sorted(annotations, key=lambda x: x.line) for l, a in groupby(by_line, lambda x: x.line): _annotate(l, a, scroll) scroll = False
def selectstart(self): import editor i=editor.get_selection() editor.set_selection(i[0],i[1]+1)
# -*- coding: utf-8 -*- import console import editor import io import sys try: line_num = int(console.input_alert('input line number')) except ValueError: sys.exit(1) if not isinstance(line_num, int): sys.exit(1) text = editor.get_text() idx = 0 with io.StringIO(text) as f: for i in range(line_num): line = f.readline() idx += len(line) editor.set_selection(idx)
def select_previous(sender): full_text = editor.get_text() marker = editor.get_selection()[0] start = full_text.rfind(find_text, 0, marker) force_scroll(start) editor.set_selection(start, start + len(find_text))
def select_text(): full_text = editor.get_text() marker = full_text.find(find_text) editor.set_selection(marker, marker + len(find_text))
def cut_action(sender): i = editor.get_selection() t = editor.get_text() clipboard.set(t[i[0]:i[1]]) editor.replace_text(i[0], i[1], '') editor.set_selection(i[0], i[0])
def paste(self): import clipboard i=editor.get_selection() t=editor.get_text() editor.replace_text(i[0],i[1], clipboard.get()) editor.set_selection(i[0],i[1]-len(t)+len(editor.get_text()))
def select_next(sender): full_text = editor.get_text() marker = editor.get_selection()[1] start = full_text.find(find_text, marker) force_scroll(start) editor.set_selection(start, start + len(find_text))
# https://gist.github.com/GuyCarver/4143014 # unindent selection import editor text = editor.get_text() selection = editor.get_line_selection() selected_text = text[selection[0]:selection[1]] replacement = '' for line in selected_text.splitlines(): replacement += line[line.find('\t') + 1:] + '\n' editor.replace_text(selection[0], selection[1], replacement) editor.set_selection(selection[0], selection[0] + len(replacement) - 1)
def select_previous(sender): full_text = editor.get_text() marker = editor.get_selection()[0] start = full_text.rfind(find_text, 0, marker) editor.set_selection(start, start + len(find_text))
def select_next(sender): full_text = editor.get_text() marker = editor.get_selection()[1] start = full_text.find(find_text, marker) editor.set_selection(start, start + len(find_text))
def paste_action(sender): i = editor.get_selection() t = editor.get_text() editor.replace_text(i[0], i[1], clipboard.get()) editor.set_selection(i[0], i[1] - len(t) + len(editor.get_text()))
def select_action_r(self): i = editor.get_selection() editor.set_selection(i[0], i[1] + 1)
import editor full_text = editor.get_text() cursor = editor.get_selection()[1] while True: try: if full_text[cursor - 3] + full_text[cursor - 2] + full_text[cursor - 1] == 'def': editor.replace_text(cursor, cursor, ' func_name():') editor.set_selection(cursor + 1, cursor + 10) if full_text[cursor - 3] + full_text[cursor - 2] + full_text[cursor - 1] == 'ifc': editor.replace_text(cursor, cursor, ' condition:') editor.set_selection(cursor + 1, cursor + 10) if full_text[cursor - 3] + full_text[cursor - 2] + full_text[cursor - 1] == 'wlt': editor.replace_text(cursor - 2, cursor, 'hile i < num:') editor.set_selection(cursor + 7, cursor + 10) if full_text[cursor - 3] + full_text[cursor - 2] + full_text[cursor - 1] == 'fea': editor.replace_text(cursor - 2, cursor, 'or entry in array:') editor.set_selection(cursor + 10, cursor + 15) break except: pass
# https://gist.github.com/jefflovejapan/5076080 # Comment/Uncomment selected lines import editor text = editor.get_text() selection = editor.get_line_selection() selected_text = text[selection[0]:selection[1]] is_comment = selected_text.strip().startswith('#') replacement = '' for line in selected_text.splitlines(): if is_comment: if line.strip().startswith('#'): replacement += line[line.find('#') + 1:] + '\n' else: replacement += line + '\n' else: replacement += '#' + line + '\n' editor.replace_text(selection[0], selection[1], replacement) editor.set_selection(selection[0], selection[0] + len(replacement) - 1)
def select_action_l(self): i = editor.get_selection() editor.set_selection(i[0] - 1, i[1])
def selectstart(): import editor i = editor.get_selection() editor.set_selection(i[0], i[1] + 1)
sel = editor.get_selection() selection_text = editor.get_text()[sel[0]:sel[1]] if selection_text: clipboard.set(selection_text) if not os.path.exists(target_dir): os.mkdir(target_dir) if not os.path.exists(target_file): ''' not using editor.make_new_file() here as far as i know you cant control how it opens. it opens replacing the current tab, instead of opening in a new tab ''' creation_date_str = datetime.datetime.now().strftime(_creation_date_str) with open(target_file, 'w') as f: f.writelines(_file_header_text.format(src_name=file_name, src_path=file_path, creation_date=creation_date_str, sep='*' * 60)) # open the file in the editor editor.open_file(target_file, True) # position the cursor at the end of the file editor.set_selection(len(editor.get_text()))
def paste(): import clipboard i = editor.get_selection() t = editor.get_text() editor.replace_text(i[0], i[1], clipboard.get()) editor.set_selection(i[0], i[1] - len(t) + len(editor.get_text()))
# coding: utf-8 # Put this script into the editor action (wrench) menu. # Whenever you want to import an Objective-C class, # you jutst need to type the classname, select it and invoke this script via the action menu. # This script will check if a classname with the name you selected exists # and add ` = ObjcClass(_classname_)` to the end of the line import editor, console, sys from objc_util import ObjCClass selection = editor.get_selection() line = editor.get_line_selection() text = editor.get_text() classname = text[int(selection[0]):int(selection[1])] try: ObjCClass(classname) editor.set_selection(selection[1]) editor.replace_text(selection[1], selection[1], ' = ObjCClass(\'{}\')'.format(classname)) except: console.hud_alert(sys.exc_info()[1].message, 'error')
def mvcsr(x): # move cursor editor.set_selection(x,x)
def select_text(): full_text = editor.get_text() marker = full_text.find(find_text) force_scroll(marker) editor.set_selection(marker, marker + len(find_text))