def _jump_to_ibm(self, what): ibms, \ msg = self._ibms_in_tab(ed, self.bm_signs) if not ibms and msg: return app.msg_status(msg) if not ibms: return app.msg_status(_('No in-text bookmarks')) rCrt = ed.get_caret_xy()[1] if 1==len(ibms) \ and rCrt==ibms[0][1]: return app.msg_status(_('No more bookmarks')) line_ns = [ line_n for (tab_id, line_n, bm_msg, line_s, tab_info) in ibms ] if self.wrap: line_ns = [-line_ns[-1]] + line_ns + [line_ns[0] + 0xFFFFFFFF] line_cns = [ line_n for line_n in line_ns if (line_n > rCrt if what == 'next' else line_n < rCrt) ] if not line_cns: return app.msg_status(_('No bookmark for jump')) line_n = min(line_cns) if what == 'next' else max(line_cns) line_n = -line_n if line_n < 0 else line_n line_n = line_n - 0xFFFFFFFF if line_n >= 0xFFFFFFFF else line_n ed.set_caret_xy(0, line_n) if not (ed.get_prop(app.PROP_LINE_TOP) <= line_n <= ed.get_prop( app.PROP_LINE_BOTTOM)): ed.set_prop(app.PROP_LINE_TOP, str(max(0, line_n - max(5, self.find_indent))))
def _add_filename(self, fn): if not fn: return fvdata = get_fav_data() files = fvdata.get('fv_files', []) if any([os.path.samefile(fn, f) for f in files]): return files += [fn] fvdata['fv_files'] = files save_fav_data(fvdata) app.msg_status(_('Added to Favorites: ')+fn)
def dlg_ibms_in_tabs(self): ibms = [] for h_tab in app.ed_handles(): ted = app.Editor(h_tab) t_ibms, \ msg = self._ibms_in_tab(ted, self.bm_signs) ibms += t_ibms #for h_tab if not ibms: return app.msg_status(_('No in-text bookmarks in tabs')) line_max = max( [line_n for (tab_id, line_n, bm_msg, line_s, tab_info) in ibms]) ln_wd = len(str(line_max)) ibms = [(tab_id, line_n, bm_msg, f('{} {}', str(1 + line_n).rjust(ln_wd, ' '), line_s), tab_info) for (tab_id, line_n, bm_msg, line_s, tab_info) in ibms] tid = ed.get_prop(app.PROP_TAB_ID) rCrt = ed.get_caret_xy()[1] near = min([(abs(line_n - rCrt) if tid == tab_id else 0xFFFFFF, ind) for ind, (tab_id, line_n, bm_msg, line_s, tab_info) in enumerate(ibms)])[1] ans = app.dlg_menu( app.MENU_DOUBLE, 'List', '\n'.join([ f('({}) {}\t{}', tab_info, bm_msg, line_s) for tab_id, line_n, bm_msg, line_s, tab_info in ibms ]), near) if ans is None: return tab_id, line_n, bm_msg, line_s, tab_info = ibms[ans] ted = apx.get_tab_by_id(tab_id) ted.focus() ed.set_caret_xy(0, line_n) if not (ed.get_prop(app.PROP_LINE_TOP) <= line_n <= ed.get_prop( app.PROP_LINE_BOTTOM)): ed.set_prop(app.PROP_LINE_TOP, str(max(0, line_n - max(5, self.find_indent))))
def on_caret_move(self, ed_self): if ed_self.get_carets(): return #donot allow mul-carets if ed_self.get_text_len() > MAX_SIZE: return ed_self.marks(app.MARKS_DELETE_BY_TAG, 0, 0, MARKTAG) current_text = _get_current_text(ed_self) if not current_text: return text, caret_pos, is_selection = current_text if not SEL_ALLOW_WHITE_SPACE: text = text.strip() if not text: return if is_selection: case_sensitive = SEL_CASE_SENSITIVE words_only = SEL_WORDS_ONLY whole_words = SEL_WHOLE_WORDS if SEL_WORDS_ONLY else False else: case_sensitive = CARET_CASE_SENSITIVE words_only = True whole_words = CARET_WHOLE_WORDS if len(text) < MIN_LEN: return x0, y0, x1, y1 = caret_pos if x0 > x1: x0, x1 = x1, x0 items = find_all_occurrences(ed_self, text, case_sensitive, whole_words, words_only) if not items: return if len(items) == 1 and items[0] == (x0, y1): return for item in items: if item == (x0, y0): continue npos = ed_self.xy_pos(item[0], item[1]) ed_self.marks(app.MARKS_ADD, npos, len(text), MARKTAG) else: if CARET_ALLOW and not is_selection: npos = ed_self.xy_pos(x0, y0) ed_self.marks(app.MARKS_ADD, npos, len(text), MARKTAG) app.msg_status('Matches hilited: {}'.format(len(items)))
def do_lint(self, editor, show_panel=False): lexer = editor.get_prop(sw.PROP_LEXER_FILE) for linterName in linter_classes: Linter = linter_classes[linterName] if isinstance(Linter.syntax, (tuple, list)): match = lexer in Linter.syntax else: match = lexer == Linter.syntax if match: if not Linter.disabled: linter = Linter(editor) error_count = linter.lint() if error_count > 0: if show_panel: sw.ed.focus() sw.ed.cmd(sw_cmd.cmd_ToggleFocusValidate) sw.ed.focus() sw.msg_status('Linter "%s" found %d error(s)' % (linter.name, error_count)) else: if show_panel: sw.msg_status('Linter "%s" found no errors' % linter.name) return else: if show_panel: sw.msg_status('No linters for lexer "%s"' % lexer)
def do_lint(self, editor, show_panel=False): lexer = editor.get_prop(sw.PROP_LEXER_FILE) for linterName in linter_classes: Linter = linter_classes[linterName] if isinstance(Linter.syntax, (tuple, list)): match = lexer in Linter.syntax else: match = lexer == Linter.syntax if match: if not Linter.disabled: linter = Linter(editor) error_count = linter.lint() if error_count > 0: if show_panel: sw.ed.focus() sw.ed.cmd(sw_cmd.cmd_ToggleFocusValidate) sw.ed.focus() sw.msg_status('Linter "%s" found %d error(s)' % (linter.name, error_count)) else: if show_panel: sw.msg_status('Linter "%s" found no errors' % linter.name) return else: if show_panel: sw.msg_status('No linters for lexer "%s"' % lexer)
def dlg_ibms_in_tab(self): ibms, \ msg = self._ibms_in_tab(ed, self.bm_signs) if not ibms and msg: return app.msg_status(msg) if not ibms: return app.msg_status(_('No in-text bookmarks')) line_max = max( [line_n for (tab_id, line_n, bm_msg, line_s, tab_info) in ibms]) ln_wd = len(str(line_max)) pass #LOG and log('ln_wd={}',(ln_wd)) ibms = [(bm_msg, line_n, f('{} {}', str(1 + line_n).rjust(ln_wd, ' '), line_s)) for (tab_id, line_n, bm_msg, line_s, tab_info) in ibms] pass #LOG and log('ibms=¶{}',pf(ibms)) rCrt = ed.get_caret_xy()[1] near = min([(abs(line_n - rCrt), ind) for ind, (bm_msg, line_n, line_s) in enumerate(ibms)])[1] if self.show_wo_alt: ans = app.dlg_menu( app.MENU_SIMPLE, 'List', '\n'.join([ f('{}\t{}', line_nd, bm_msg) for bm_msg, line_n, line_nd in ibms ]), near) else: ans = app.dlg_menu( app.MENU_DOUBLE, 'List', '\n'.join([ f('{}\t{}', bm_msg, line_nd) for bm_msg, line_n, line_nd in ibms ]), near) if ans is None: return bm_msg, line_n, line_nd = ibms[ans] ed.set_caret_xy(0, line_n) if not (ed.get_prop(app.PROP_LINE_TOP) <= line_n <= ed.get_prop( app.PROP_LINE_BOTTOM)): ed.set_prop(app.PROP_LINE_TOP, str(max(0, line_n - max(5, self.find_indent))))
def run(self): ''' Add spaces for align text in some lines Example. Start lines a= 0 b c = 1 Aligned lines a = 0 b c = 1 ''' line_start, \ line_end= ed.get_sel_lines() if line_start == line_end: return sw.msg_status('Only for multiline selection') pSelBgn, \ nSelLen = ed.get_sel() spr = sw.dlg_input('Enter separator string:', self.data_sep, '', '') spr = '' if spr is None else spr.strip() if not spr: return # Esc self.data_sep = spr ls_txt = ed.get_text_substr(pSelBgn, nSelLen) if spr not in ls_txt: return sw.msg_status( "No separator '{}' in selected lines".format(spr)) lines = ls_txt.splitlines() ln_poss = [(ln, ln.find(spr)) for ln in lines] max_pos = max([p for (l, p) in ln_poss]) if max_pos == min([p if p >= 0 else max_pos for (l, p) in ln_poss]): return sw.msg_status('Text change not needed') nlines = [ ln if pos == -1 or max_pos == pos else ln[:pos] + ' ' * (max_pos - pos) + ln[pos:] for (ln, pos) in ln_poss ] ed.replace(pSelBgn, nSelLen, '\n'.join(nlines) + '\n') sw.msg_status('Selection aligned (%d lines)' % len(nlines))
def add_ibm(self): lxr = ed.get_prop(app.PROP_LEXER_FILE) lxr = lxr if lxr else NO_LXR_SIGN # if lxr not in self.lxr2cmnt: return app.msg_status(f(_('Cannot add in-text bookmark into document with Lexer {}. No to-end-of-line comment.'), lxr)) if lxr not in self.lxr2cmnt: return app.msg_status( f( _('Cannot add in-text bookmark: no line-comments defined for lexer {}.' ), lxr)) cmnt = self.lxr2cmnt[lxr] bm_msg = app.dlg_input( _('Enter message for in-text bookmark. Empty is good.'), '') if bm_msg is None: return (cCrt, rCrt) = ed.get_caret_xy() line_s = ed.get_text_line(rCrt) ed.set_text_line(rCrt, line_s + cmnt + self.bm_sign + ' ' + bm_msg)
def _ibms_in_tab(self, ted, bm_signs): """ Collect in-text bm in the ted. Params ted tab bm_signs list of parsed signs Return list [(tab_id, line_n, bm_msg, line_s, tab_info)] msg Reason of empty list """ lxr = ted.get_prop(app.PROP_LEXER_FILE) lxr = lxr if lxr else NO_LXR_SIGN if lxr not in self.lxr2cmnt: return [], app.msg_status( f(_('No in-text bookmark into document with Lexer {}'), lxr)) cmnt = self.lxr2cmnt[lxr] tab_sps = ' ' * ted.get_prop(app.PROP_TAB_SIZE) tab_grp = ted.get_prop(app.PROP_INDEX_GROUP) tab_num = ted.get_prop(app.PROP_INDEX_TAB) tab_cap = ted.get_prop(app.PROP_TAB_TITLE) tab_id = ted.get_prop(app.PROP_TAB_ID) tab_info = f('{}:{}. {}', 1 + tab_grp, 1 + tab_num, tab_cap) signs = [cmnt + sign + ' ' for sign in bm_signs] pass #LOG and log('signs={}',(signs)) ibms = [] for line_n in range(ted.get_line_count()): line_s = ted.get_text_line(line_n) for sign in signs: if sign in line_s: line_s = line_s.replace('\t', tab_sps) bm_msg = line_s[line_s.index(sign) + len(sign):] ibms += [(tab_id, line_n, bm_msg, line_s, tab_info)] break #for sign #for sign #for line_n return ibms, ''
def config(self): do_save_ops() if os.path.isfile(fn_ini): app.file_open(fn_ini) else: app.msg_status('Config file not exists')
def dlg_config(self): DLG_W, \ DLG_H = 400, 95 lxrs_l = apx.get_enabled_lexers() sgns_h = _( 'Space delimeted list.\rThe first word will be inserted by command.' ) dfcm_h = _( 'Default comment sign.\rIt is used when lexer has no line comment or file has no lexer.' ) cnts = [ dict(tp='lb', tid='sgns', l=GAP, w=130, cap=_('&Bookmark signs:'), hint=sgns_h) # &b , dict(cid='sgns', tp='ed', t=GAP, l=130, w=DLG_W - 130 - GAP) # , dict(tp='lb', tid='dfcm', l=GAP, w=130, cap=_('&Comment sign:'), hint=dfcm_h) # &c , dict(cid='dfcm', tp='ed', t=35, l=130, w=DLG_W - 130 - GAP) # , dict(cid='wrap', tp='ch', tid='!', l=GAP, w=120, cap=_('&Wrap for next/prev')) # &w # ,dict(cid='help',tp='bt' ,t=DLG_H-60 ,l=DLG_W-GAP-80 ,w=80 ,cap=_('Help') ) # , dict(cid='!', tp='bt', t=DLG_H - 30, l=DLG_W - GAP - 165, w=80, cap=_('Save'), props='1') # default , dict(cid='-', tp='bt', t=DLG_H - 30, l=DLG_W - GAP - 80, w=80, cap=_('Close')) # ] #NOTE: cfg focused = 'sgns' while True: act_cid, vals, chds = dlg_wrapper(_('In-text bookmarks'), DLG_W, DLG_H, cnts, dict(sgns=' '.join( self.bm_signs), dfcm=self.unlxr_cmnt, wrap=self.wrap), focus_cid=focused) if act_cid is None or act_cid == '-': return #while True focused = chds[0] if 1 == len(chds) else focused if act_cid == '!': if not vals['sgns'].strip(): app.msg_status(_('Need Bookmark sign')) focused = 'sgns' continue #while if not vals['dfcm'].strip(): app.msg_status(_('Need Comment sign')) focused = 'dfcm' continue #while if self.bm_signs != vals['sgns'].split(): self.bm_signs = vals['sgns'].split() app.ini_write(fn_config, 'op', 'intextbookmk_signs', ' '.join(self.bm_signs)) if self.unlxr_cmnt != vals['dfcm'].strip(): self.unlxr_cmnt = vals['dfcm'].strip() app.ini_write(fn_config, 'op', 'intextbookmk_no_lexer_comment', self.unlxr_cmnt) if self.wrap != vals['wrap']: self.wrap = vals['wrap'] app.ini_write(fn_config, 'op', 'intextbookmk_wrap', '1' if self.wrap else '0') break #while
def _check_API(ver): if app.app_api_version()<ver: app.msg_status(NEED_NEWER_API) return False return True