def duplicate(self): if ed.get_sel_mode() != app.SEL_NORMAL: return app.msg_status(ONLY_NORM_SEL_MODE.format(DUPLICATION)) crts = ed.get_carets() if len(crts) > 1: return app.msg_status(ONLY_SINGLE_CRT.format(DUPLICATION)) (cCrt, rCrt, cEnd, rEnd) = crts[0] bEmpSel = -1 == rEnd bUseFLn = get_opt('duplicate_full_line_if_no_sel', True) bSkip = get_opt('duplicate_move_down', True) if bEmpSel: if not bUseFLn: return # Dup whole row row_txt = ed.get_text_line(rCrt) ed.insert(0, rCrt, row_txt + '\n') # Move crt to next row if bSkip and (rCrt + 1) < ed.get_line_count(): _move_caret_down(cCrt, rCrt) return (rFr, cFr), (rTo, cTo) = minmax((rCrt, cCrt), (rEnd, cEnd)) pass #LOG and log('(cFr , rFr , cTo , rTo) ={}',(cFr , rFr , cTo , rTo)) sel_txt = ed.get_text_substr(cFr, rFr, cTo, rTo) pass #LOG and log('sel_txt={}',repr(sel_txt)) ed.insert(cFr, rFr, sel_txt)
def duplicate(self): if ed.get_sel_mode() != app.SEL_NORMAL: return app.msg_status(ONLY_NORM_SEL_MODE.format(DUPLICATION)) crts = ed.get_carets() if len(crts)>1: return app.msg_status(ONLY_SINGLE_CRT.format(DUPLICATION)) (cCrt, rCrt, cEnd, rEnd) = crts[0] bEmpSel = -1==rEnd bUseFLn = get_opt('duplicate_full_line_if_no_sel', True) bSkip = get_opt('duplicate_move_down', True) if bEmpSel: if not bUseFLn: return # Dup whole row row_txt = ed.get_text_line(rCrt) ed.insert(0, rCrt, row_txt+'\n') # Move crt to next row if bSkip and (rCrt+1)<ed.get_line_count(): _move_caret_down(cCrt, rCrt) return (rFr, cFr), (rTo, cTo) = minmax((rCrt, cCrt), (rEnd, cEnd)) pass; #LOG and log('(cFr , rFr , cTo , rTo) ={}',(cFr , rFr , cTo , rTo)) sel_txt = ed.get_text_substr(cFr, rFr, cTo, rTo) pass; #LOG and log('sel_txt={}',repr(sel_txt)) ed.insert(cFr, rFr, sel_txt)
def duplicate(self): if ed.get_sel_mode() != app.SEL_NORMAL: return app.msg_status(ONLY_NORM_SEL_MODE.format(DUPLICATION)) crts = ed.get_carets() if len(crts)>1: return app.msg_status(ONLY_SINGLE_CRT.format(DUPLICATION)) (cCrt, rCrt, cEnd, rEnd) = crts[0] if -1==cEnd: # Empty sel -- dup whole row row_txt = ed.get_text_line(rCrt) ed.insert(0, rCrt, row_txt+'\n') # Move crt to next row if (rCrt+1)<ed.get_line_count(): colCrt = pos2pos(cCrt , rCrt, 'smb2col') smbCrt1 = pos2pos(colCrt, rCrt+1, 'col2smb') ed.set_caret(smbCrt1, rCrt+1) return (rFr, cFr), (rTo, cTo) = minmax((rCrt, cCrt), (rEnd, cEnd)) #pass; LOG and log('(cCrt, rCrt, cEnd, rEnd)={}',(cCrt, rCrt, cEnd, rEnd)) pass; LOG and log('(cFr , rFr , cTo , rTo) ={}',(cFr , rFr , cTo , rTo)) sel_txt = ed.get_text_substr(cFr, rFr, cTo, rTo) pass; LOG and log('sel_txt={}',repr(sel_txt)) ed.insert(cFr, rFr, sel_txt) if -1==rEnd: # or rCrt==rEnd: # Move crt to next row colCrt = pos2pos(cCrt , rCrt, 'smb2col') smbCrt1 = pos2pos(colCrt, rCrt+1, 'col2smb') ed.set_caret(smbCrt1, rCrt+1)
def update_tree(self, ed, lexer): getter = self.get_getter(lexer) if not getter: return filename = ed.get_filename() lines = ed.get_text_all().split("\n") heads = list(getter(filename, lines)) ed.set_prop(app.PROP_CODETREE, False) app.tree_proc(self.h_tree, app.TREE_LOCK) app.tree_proc(self.h_tree, app.TREE_ITEM_DELETE, 0) last_levels = {0: 0} for index, data in enumerate(heads): pos = data[0] level = data[1] header = data[2] icon_index = data[3] if len(data) > 3 else -1 for test_level in reversed(range(level)): parent = last_levels.get(test_level) if parent is None: continue identity = app.tree_proc(self.h_tree, app.TREE_ITEM_ADD, parent, index=-1, text=header, image_index=icon_index) # when adding level K, forget all levels > K last_levels = { k: v for k, v in last_levels.items() if k <= level } last_levels[level] = identity if type(pos) == int: if index == len(heads) - 1: end_y = len(lines) - 1 s = ed.get_text_line(end_y) if s is None: end_x = 0 else: end_x = len(s) else: end_y = heads[index + 1][0] # line_index of next header end_x = 0 rng = (0, pos, end_x, end_y) else: rng = pos app.tree_proc(self.h_tree, app.TREE_ITEM_SET_RANGE, identity, index=-1, text=rng) break app.tree_proc(self.h_tree, app.TREE_UNLOCK)
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 ,cEnd, rEnd)= ed.get_carets()[0] line_s = ed.get_text_line( rCrt) ed.set_text_line( rCrt, line_s + cmnt + self.bm_sign + ' ' + bm_msg)
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, cEnd, rEnd) = ed.get_carets()[0] line_s = ed.get_text_line(rCrt) ed.set_text_line(rCrt, line_s + cmnt + self.bm_sign + ' ' + bm_msg)
def cmt_toggle_stream(self): ''' ''' if ed.get_sel_mode() != app.SEL_NORMAL: return app.msg_status(ONLY_NORM_SEL_MODE.format(COMMENTING)) lex = ed.get_prop(app.PROP_LEXER_CARET) ((bgn_sgn, end_sgn), bOnlyLn) = self._get_cmt_pair(lex) if not bgn_sgn: return app.msg_status(CMT_NO_STRM_4LEX.format(lex)) bUseFLn = get_opt('comment_full_line_if_no_sel', True) crts = ed.get_carets() pass #LOG and log('lex, get_carets()={}', (lex, crts)) pass #LOG and log('(bgn_sgn,end_sgn),bOnlyLn,bUseFLn={}', ((bgn_sgn,end_sgn),bOnlyLn,bUseFLn)) for icrt, (cCrt, rCrt, cEnd, rEnd) in enumerate(crts): pass #LOG and log('(cCrt, rCrt), (cEnd, rEnd)={}', ((cCrt, rCrt), (cEnd, rEnd))) bEmpSel = -1 == rEnd bDrtSel = -1 == rEnd or (rCrt, cCrt) > (rEnd, cEnd) if False: pass elif bEmpSel and (bUseFLn or bOnlyLn): # Use full line line = ed.get_text_line(rCrt) (cTx1, rTx1), (cTx2, rTx2) = (0, rCrt), (len(line), rCrt) elif bOnlyLn: # and not bEmpSel # Only full lines rTx1, rTx2 = minmax(rCrt, rEnd) line = ed.get_text_line(rTx2) (cTx1, rTx1), (cTx2, rTx2) = (0, rTx1), (len(line), rTx2) elif bEmpSel: # and not bUseFLn and not bOnlyLn continue else: (rTx1, cTx1), (rTx2, cTx2) = minmax((rCrt, cCrt), (rEnd, cEnd)) selTx = ed.get_text_substr(cTx1, rTx1, cTx2, rTx2) pass #LOG and log('(rTx1, cTx1), (rTx2, cTx2), selTx={}', ((rTx1, cTx1), (rTx2, cTx2), repr(selTx))) do_uncmt = selTx.startswith(bgn_sgn) and selTx.endswith(end_sgn) pass #LOG and log('do_uncmt={}', (do_uncmt)) if False: pass elif not do_uncmt and bOnlyLn: # Comment! ed.insert(0, rTx2 + 1, end_sgn + '\n') #! true insert sequence ed.insert(0, rTx1, bgn_sgn + '\n') #! true insert sequence (cNSel1, rNSel1, cNSel2, rNSel2) = 0, rTx1, len(end_sgn), rTx2 + 2 elif not do_uncmt: # Comment! ed.insert(cTx2, rTx2, end_sgn) #! true insert sequence ed.insert(cTx1, rTx1, bgn_sgn) #! true insert sequence if False: pass elif rTx1 == rTx2: # sel into one row (cNSel1, rNSel1, cNSel2, rNSel2 ) = cTx1, rTx1, cTx2 + len(bgn_sgn) + len(end_sgn), rTx2 elif rTx1 != rTx2: # sel ends on diff rows (cNSel1, rNSel1, cNSel2, rNSel2) = cTx1, rTx1, cTx2 + len(end_sgn), rTx2 elif do_uncmt and bOnlyLn: # UnComment! ed.delete(0, rTx2, 0, rTx2 + 1) #! true delete sequence ed.delete(0, rTx1, 0, rTx1 + 1) #! true delete sequence (cNSel1, rNSel1, cNSel2, rNSel2) = 0, rTx1, len(ed.get_text_line(rTx2 - 2)), rTx2 - 2 elif do_uncmt: # UnComment! ed.delete(cTx2 - len(end_sgn), rTx2, cTx2, rTx2) #! true delete sequence ed.delete(cTx1, rTx1, cTx1 + len(bgn_sgn), rTx1) #! true delete sequence if False: pass elif rTx1 == rTx2: # sel into one row (cNSel1, rNSel1, cNSel2, rNSel2 ) = cTx1, rTx1, cTx2 - len(bgn_sgn) - len(end_sgn), rTx2 elif rTx1 != rTx2: # sel ends on diff rows (cNSel1, rNSel1, cNSel2, rNSel2) = cTx1, rTx1, cTx2 - len(end_sgn), rTx2 pass #LOG and log('bDrtSel, (cNSel1, rNSel1), (cNSel2, rNSel2)={}', (bDrtSel, (cNSel1, rNSel1), (cNSel2, rNSel2))) if bDrtSel: ed.set_caret(cNSel2, rNSel2, cNSel1, rNSel1, app.CARET_SET_INDEX + icrt) else: ed.set_caret(cNSel1, rNSel1, cNSel2, rNSel2, app.CARET_SET_INDEX + icrt) #for icrt bSkip = get_opt('comment_move_down', True) if False: pass elif 1 == len(crts) and bEmpSel and bUseFLn and bSkip: _move_caret_down(cCrt, rCrt) if bOnlyLn and not do_uncmt: crt = ed.get_carets()[0] _move_caret_down(crt[0], crt[1]) crt = ed.get_carets()[0] _move_caret_down(crt[0], crt[1])
def _cmt_toggle_line(self, cmt_type): ''' Add/Remove line comment Params cmt_type '1st' at begin of line 'bod' at first not blank ''' if not _check_API('1.0.107'): return if ed.get_sel_mode() != app.SEL_NORMAL: return app.msg_status(ONLY_NORM_SEL_MODE.format(COMMENTING)) lex = ed.get_prop(app.PROP_LEXER_CARET) cmt_sgn = app.lexer_proc(app.LEXER_GET_COMMENT, lex) pass; LOG and log('lex, cmt_sgn={}', (lex, cmt_sgn)) if not cmt_sgn: return app.msg_status(CMT_NO_LINE_4LEX.format(lex)) crts = ed.get_carets() if len(crts)>1: return app.msg_status(ONLY_SINGLE_CRT.format(COMMENTING)) no_tab = ed.get_prop(app.PROP_TAB_SPACES) # only blanks opts = self._get_opts('cmt') save_bd_col = no_tab and opts.get('save_body_col', False) blnks4cmt = '\t'.expandtabs(len(cmt_sgn)) (cCrt, rCrt ,cEnd, rEnd)= crts[0] (rCmtBgn ,rCmtEnd) = minmax(rCrt, rEnd if -1!=rEnd else rCrt) uncmtAll = ed.get_text_line(rCmtBgn).lstrip().startswith(cmt_sgn) pass; LOG and log('rCmtBgn,rCmtEnd,uncmtAll={}', (rCmtBgn,rCmtEnd,uncmtAll)) for rCmt in range(rCmtBgn, rCmtEnd+1): line = ed.get_text_line(rCmt) pos_body= line.index(line.lstrip()) pass; LOG and log('rCmtBgn,rCmtEnd,uncmtAll={}', (rCmtBgn,rCmtEnd,uncmtAll)) if uncmtAll: # Uncomment! if not line[pos_body:].startswith(cmt_sgn): # Already no comment continue if save_bd_col: line = line.replace(cmt_sgn, blnks4cmt, 1) else: line = line.replace(cmt_sgn, '' , 1) else: # Comment! if cmt_type=='bod' and line[pos_body:].startswith(cmt_sgn): # Body comment already sets - willnot double it continue if False:pass elif cmt_type=='1st' and save_bd_col and line.startswith(blnks4cmt) : line = line.replace(blnks4cmt, cmt_sgn, 1) # elif cmt_type=='1st' and save_bd_col # !line.startswith(blnks4cmt) : elif cmt_type=='1st':# !save_bd_col line = cmt_sgn+line elif cmt_type=='bod' and save_bd_col and line.startswith(blnks4cmt) : line = line.replace(blnks4cmt, cmt_sgn, 1) line = line[:pos_body-len(cmt_sgn)]+cmt_sgn+line[pos_body:] # elif cmt_type=='bod' and save_bd_col # !line.startswith(blnks4cmt) : elif cmt_type=='bod':# !save_bd_col line = line[:pos_body] +cmt_sgn+line[pos_body:] ed.set_text_line(rCmt, line) #for rCmt if -1==rEnd and opts.get('skip-line-after', True) and (rCrt+1)<ed.get_line_count(): colCrt = pos2pos(cCrt , rCrt, 'smb2col') smbCrt1 = pos2pos(colCrt, rCrt+1, 'col2smb') ed.set_caret(smbCrt1, rCrt+1)
def cmt_toggle_stream(self): ''' ''' if ed.get_sel_mode() != app.SEL_NORMAL: return app.msg_status(f(_('{} works only with normal selection'), _('Commenting'))) lex = ed.get_prop(app.PROP_LEXER_CARET) ((bgn_sgn ,end_sgn) ,bOnlyLn)=self._get_cmt_pair(lex) if not bgn_sgn: return app.msg_status(f(_('No stream comment for lexer "{}"'), lex)) bUseFLn = apx.get_opt('comment_full_line_if_no_sel', True) crts = ed.get_carets() pass; #LOG and log('lex, get_carets()={}', (lex, crts)) pass; #LOG and log('(bgn_sgn,end_sgn),bOnlyLn,bUseFLn={}', ((bgn_sgn,end_sgn),bOnlyLn,bUseFLn)) for icrt, (cCrt, rCrt, cEnd, rEnd) in enumerate(crts): pass; #LOG and log('(cCrt, rCrt), (cEnd, rEnd)={}', ((cCrt, rCrt), (cEnd, rEnd))) bEmpSel = -1==rEnd bDrtSel = -1==rEnd or (rCrt, cCrt)>(rEnd, cEnd) if False:pass elif bEmpSel and (bUseFLn or bOnlyLn): # Use full line line = ed.get_text_line(rCrt) (cTx1, rTx1), (cTx2, rTx2) = (0, rCrt), (len(line), rCrt) elif bOnlyLn: # and not bEmpSel # Only full lines rTx1, rTx2 = apx.minmax(rCrt, rEnd) line = ed.get_text_line(rTx2) (cTx1, rTx1), (cTx2, rTx2) = (0, rTx1), (len(line), rTx2) elif bEmpSel: # and not bUseFLn and not bOnlyLn continue else: (rTx1, cTx1), (rTx2, cTx2) = apx.minmax((rCrt, cCrt), (rEnd, cEnd)) selTx = ed.get_text_substr(cTx1, rTx1, cTx2, rTx2) pass; #LOG and log('(rTx1, cTx1), (rTx2, cTx2), selTx={}', ((rTx1, cTx1), (rTx2, cTx2), repr(selTx))) do_uncmt= selTx.startswith(bgn_sgn) and selTx.endswith(end_sgn) pass; #LOG and log('do_uncmt={}', (do_uncmt)) if False:pass elif not do_uncmt and bOnlyLn: # Comment! ed.insert(0, rTx2+1, end_sgn+'\n') #! true insert sequence ed.insert(0, rTx1, bgn_sgn+'\n') #! true insert sequence (cNSel1, rNSel1 ,cNSel2, rNSel2) = 0, rTx1, len(end_sgn), rTx2+2 elif not do_uncmt: # Comment! ed.insert(cTx2, rTx2, end_sgn) #! true insert sequence ed.insert(cTx1, rTx1, bgn_sgn) #! true insert sequence if False:pass elif rTx1==rTx2: # sel into one row (cNSel1, rNSel1 ,cNSel2, rNSel2) = cTx1, rTx1, cTx2+len(bgn_sgn)+len(end_sgn), rTx2 elif rTx1!=rTx2: # sel ends on diff rows (cNSel1, rNSel1 ,cNSel2, rNSel2) = cTx1, rTx1, cTx2 +len(end_sgn), rTx2 elif do_uncmt and bOnlyLn: # UnComment! ed.delete(0, rTx2, 0, rTx2+1) #! true delete sequence ed.delete(0, rTx1, 0, rTx1+1) #! true delete sequence (cNSel1, rNSel1 ,cNSel2, rNSel2) = 0, rTx1, len(ed.get_text_line(rTx2-2)), rTx2-2 elif do_uncmt: # UnComment! ed.delete(cTx2-len(end_sgn), rTx2, cTx2, rTx2) #! true delete sequence ed.delete(cTx1, rTx1, cTx1+len(bgn_sgn), rTx1) #! true delete sequence if False:pass elif rTx1==rTx2: # sel into one row (cNSel1, rNSel1 ,cNSel2, rNSel2) = cTx1, rTx1, cTx2-len(bgn_sgn)-len(end_sgn), rTx2 elif rTx1!=rTx2: # sel ends on diff rows (cNSel1, rNSel1 ,cNSel2, rNSel2) = cTx1, rTx1, cTx2 -len(end_sgn), rTx2 pass; #LOG and log('bDrtSel, (cNSel1, rNSel1), (cNSel2, rNSel2)={}', (bDrtSel, (cNSel1, rNSel1), (cNSel2, rNSel2))) if bDrtSel: ed.set_caret(cNSel2, rNSel2, cNSel1, rNSel1, app.CARET_SET_INDEX+icrt) else: ed.set_caret(cNSel1, rNSel1, cNSel2, rNSel2, app.CARET_SET_INDEX+icrt) #for icrt bSkip = apx.get_opt('comment_move_down', True) if False:pass elif 1==len(crts) and bEmpSel and bUseFLn and bSkip: apx._move_caret_down(cCrt, rCrt) if bOnlyLn and not do_uncmt: crt=ed.get_carets()[0]; apx._move_caret_down(crt[0], crt[1]) crt=ed.get_carets()[0]; apx._move_caret_down(crt[0], crt[1])
def get_line(n): # limit max length of line return ed.get_text_line(n, opt.MAX_LINE_LEN)
def _prep_sess(self, wdex='word'): """ Params wdex Type of sess: 'word' / 'expr' """ crts = ed.get_carets() if len(crts) > 1: return app.msg_status(_("Command doesnt work with multi-carets")) (cCrt, rCrt, cEnd, rEnd) = crts[0] if -1 != rEnd and rCrt != rEnd: return app.msg_status( _("Command doesnt work with multi-line selection")) stayed = self.sess \ and self.sess.pre_mver == ed.get_prop(app.PROP_MODIFIED_VERSION) \ and self.sess.pre_crt0 == crts[0] cEnd, rEnd = (cCrt, rCrt) if -1 == rEnd else (cEnd, rEnd) sel_be_eb = 'be' if cCrt > cEnd else 'eb' if cCrt < cEnd else '' ((rSelB, cSelB), (rSelE, cSelE)) = apx.minmax((rCrt, cCrt), (rEnd, cEnd)) pass #LOG and stayed and log('stayed,(wdex,self.sess.wdex)={}',(stayed,(wdex,self.sess.wdex))) if stayed and wdex == self.sess.wdex: # Sess OK return True what = '' # Str to find what_b = -1 # Pos what begin what_e = -1 # Pos what end kill_b = -1 # Pos kill begin kill_e = -1 # Pos kill end wbnd = True # Need left word bound sel = '' if stayed and wdex != self.sess.wdex: # Change sess type what = self.sess.src_what what_b = self.sess.src_what_b what_e = self.sess.src_what_e kill_b = self.sess.src_kill_b kill_e = self.sess.src_kill_e wbnd = self.sess.src_wbnd if not stayed: # New sess line = ed.get_text_line(rCrt) sel = ed.get_text_sel() if sel: # Use selection to find what = sel what_b = cSelB what_e = cSelE wbnd = 0 == cSelB or line[cSelB - 1].isspace() else: # Use "chars around caret" to find what tx_bfr = line[:cCrt] ch_bfr = tx_bfr[-1] if tx_bfr else ' ' tx_aft = line[cCrt:] ch_aft = tx_aft[0] if tx_aft else ' ' pass #LOG and log('ch_bfr,ch_aft,tx_bfr,tx_aft={}',(ch_bfr,ch_aft,tx_bfr,tx_aft)) shf_l = 0 shf_r = 0 if ch_bfr.isalnum() or ch_bfr in self.wdsgns: tx_bfr_r = ''.join(reversed(tx_bfr)) shf_l = len(self.base_re.match(tx_bfr_r).group()) if ch_aft.isalnum() or ch_aft in self.wdsgns: shf_r = len(self.base_re.match(tx_aft).group()) what_b = cCrt - shf_l if self.kill: what_e = cCrt kill_b = cCrt kill_e = cCrt + shf_r else: what_e = cCrt + shf_r what = line[what_b:what_e] pass #LOG and log('(shf_l,shf_r), (what_b,what_e), what={}',((shf_l,shf_r), (what_b,what_e), what)) if not what \ or not what.strip(): return app.msg_status(_('No data for search')) if len(what) < self.min_len: return app.msg_status( f(_('Need |base|>={}, but |"{}"|=={}'), self.min_len, what, len(what))) pass #LOG and log('what,wbnd={}',(what,wbnd)) asword = wdex == 'word' asexpr = wdex == 'expr' # Make new Sess if not stayed: pass #LOG and log('new sess',()) self.sess = Command.Sess() self.sess.row = rCrt self.sess.sel_sub = sel_be_eb self.sess.src_crt = cCrt self.sess.src_what = what self.sess.src_what_b = what_b self.sess.src_what_e = what_e self.sess.src_kill_b = kill_b self.sess.src_kill_e = kill_e self.sess.src_wbnd = wbnd self.sess.wdex = wdex what_re = re.compile('' + (r'\b' if wbnd else '') + re.escape(what) + (self.wdcmpl if asword else self.excmpl)) # + r'[\w'+re.escape(self.wdsgns)+']+') pass #LOG and log('what_re={}',(what_re.pattern)) bids_d = OrdDict() for line_n in range(ed.get_line_count()): if line_n == self.sess.row: continue #for line_n line = ed.get_text_line(line_n) if asexpr and self.expair: # Find+expand upto close brackets/quotes lbids_l = [] for m in what_re.finditer(line): lbid = m.group(0) ok = False for op, cl in self.opn2cls.items(): if op in lbid and lbid.count(op) > lbid.count( cl): # Try expand to good cl ext_bgn = m.end() ext_end = line.find(cl, ext_bgn) while -1 != ext_end: lbid_ext = lbid + line[ext_bgn:ext_end + 1] if lbid_ext.count(op) == lbid_ext.count(cl): lbid, ok = lbid_ext, True break #while ext_end = line.find(cl, ext_end + 1) if ok: break #for op #for op ok = False for qu in self.quotes: if qu in lbid and 1 == lbid.count( qu) % 2: # Try expand to good qu ext_bgn = m.end() ext_end = line.find(qu, ext_bgn) while -1 != ext_end: lbid_ext = lbid + line[ext_bgn:ext_end + 1] if 0 == lbid_ext.count(qu) % 2: lbid, ok = lbid_ext, True break #while ext_end = line.find(cl, ext_end + 1) if ok: break #for qu #for qu lbids_l.append(lbid) #for m else: # Find only lbids_l = what_re.findall(line) if lbids_l: bids_d.update({ bid: line_n for bid in lbids_l if bid not in bids_d or abs(line_n - self.sess.row) < abs( bids_d[bid] - self.sess.row) }) # closest if not bids_d: return app.msg_status(_('No in-text completions')) self.sess.bids = list(bids_d.keys()) self.sess.bids_c = list(bids_d.values()) self.sess.bids_i= min([(abs(bid_r-self.sess.row), bid_i) for (bid_i,bid_r) in enumerate(self.sess.bids_c) ])[1] \ if len(self.sess.bids)>1 and self.near else \ 0 self.incr_bfr = False # Dont increment before use pass #LOG and log('bids={}',(list(zip(self.sess.bids, self.sess.bids_c)))) pass #LOG and log('sess={}',(self.sess)) pass #return False return True
def find_cb_string(self, updn, bgn_crt_fin='crt'): ''' Find clipboard value in text. Params updn 'up'|'dn' - direction bgn_crt_fin 'bgn'|'crt'|'fin' - start point ''' clip = app.app_proc(app.PROC_GET_CLIP, '') if ''==clip: return clip = clip.replace('\r\n', '\n').replace('\r', '\n') pass; #LOG and log('clip={}',repr(clip)) crts = ed.get_carets() if len(crts)>1: return app.msg_status(ONLY_SINGLE_CRT.format('Command')) # Prepare bgn-, crt-, fin-point (cBgn, rBgn) = (0, 0) (cCrt, rCrt ,cEnd, rEnd) = crts[0] lst_line_ind = ed.get_line_count()-1 lst_line = ed.get_text_line(lst_line_ind) (cFin, rFin) = (max(0, len(lst_line)-1), lst_line_ind) if bgn_crt_fin=='crt': # Some cases for natural (not wrap) find if updn=='dn' and (cFin, rFin) == (cCrt, rCrt): # Caret at finish - immediately find from start return self.find_cb_string(updn, bgn_crt_fin='bgn') if updn=='up' and (cBgn, rBgn) == (cCrt, rCrt): # Caret at start - immediately find from finish return self.find_cb_string(updn, bgn_crt_fin='fin') if updn=='dn' and (cBgn, rBgn) == (cCrt, rCrt): # Caret already at start - switch wrap off bgn_crt_fin = 'bgn' if updn=='up' and (cFin, rFin) == (cCrt, rCrt): # Caret already at finish - switch wrap off bgn_crt_fin = 'fin' (cPnt, rPnt ,cEnd, rEnd) = apx.icase(False,0 ,bgn_crt_fin=='bgn', (cBgn, rBgn, cBgn, rBgn) ,bgn_crt_fin=='crt', (cCrt, rCrt, cEnd, rEnd) ,bgn_crt_fin=='fin', (cFin, rFin, cFin, rFin) ) # Main part if '\n' not in clip: # 1) Find inside each line row = rPnt line = ed.get_text_line(row) pos = line.find(clip, cPnt) if updn=='dn' else line.rfind(clip, 0, cPnt) while -1==pos: row = apx.icase(updn=='dn', row+1, updn=='up', row-1, -1) if row<0 or row==ed.get_line_count(): break #while line = ed.get_text_line(row) pos = line.find(clip) if updn=='dn' else line.rfind(clip) if False:pass elif -1==pos and bgn_crt_fin!='crt': return app.msg_status(FIND_FAIL_FOR_STR.format(clip)) elif -1==pos:#and bgn_crt_fin=='crt' # Wrap! return self.find_cb_string(updn, bgn_crt_fin=apx.icase(updn=='dn', 'bgn', 'fin')) elif updn=='dn': ed.set_caret(pos+len(clip), row, pos, row) elif updn=='up': ed.set_caret(pos, row, pos+len(clip), row) return # 2) Find m-line pass; #LOG and log('') clpls = clip.split('\n') pass; #LOG and log('clpls={}',(clpls)) clip = repr(clip) if False:pass elif updn=='dn': found = False row = max(rPnt, rEnd if rEnd!=-1 else rPnt) if row+len(clpls) < ed.get_line_count(): txtls = [ed.get_text_line(r) for r in range(row, row+len(clpls))] pass; #LOG and log('txtls={}',(txtls)) while True: if self._find_cb_string_included_mlines(txtls, clpls): # Found! found = True break #while row = row+1 pass; #LOG and log('row={}',(row)) if row+len(clpls) >= ed.get_line_count(): pass; #LOG and log('nfnd12',) break #while txtls = txtls[1:]+[ed.get_text_line(row+len(clpls)-1)] pass; #LOG and log('txtls={}',(txtls)) #while if False:pass elif not found and bgn_crt_fin!='crt': return app.msg_status(FIND_FAIL_FOR_STR.format(clip)) elif not found:#and bgn_crt_fin=='crt' # Wrap! return self.find_cb_string(updn, bgn_crt_fin=apx.icase(updn=='dn', 'bgn', 'fin')) ed.set_caret(len(clpls[-1]), row+len(clpls)-1, len(txtls[0])-len(clpls[0]), row) elif updn=='up': found = False row = min(rPnt, rEnd if rEnd!=-1 else rPnt) if row-len(clpls)+1 >= 0: txtls = [ed.get_text_line(r) for r in range(row-len(clpls)+1, row+1)] pass; #LOG and log('txtls={}',(txtls)) while True: if self._find_cb_string_included_mlines(txtls, clpls): # Found! found = True break #while row = row-1 pass; #LOG and log('row={}',(row)) if row-len(clpls)+1 < 0: break #while txtls = [ed.get_text_line(row-len(clpls)+1)]+txtls[:-1] pass; #LOG and log('txtls={}',(txtls)) #while if False:pass elif not found and bgn_crt_fin!='crt': return app.msg_status(FIND_FAIL_FOR_STR.format(clip)) elif not found:#and bgn_crt_fin=='crt' # Wrap! return self.find_cb_string(updn, bgn_crt_fin=apx.icase(updn=='dn', 'bgn', 'fin')) ed.set_caret(len(clpls[-1]), row, len(txtls[0])-len(clpls[0]), row-len(clpls)+1)
def cmt_toggle_stream(self): ''' ''' if ed.get_sel_mode() != app.SEL_NORMAL: return app.msg_status(f(_('{} works only with normal selection'), _('Commenting'))) lex = ed.get_prop(app.PROP_LEXER_CARET) ((bgn_sgn ,end_sgn) ,bOnlyLn)=self._get_cmt_pair(lex) if not bgn_sgn: return app.msg_status(f(_('No stream comment for lexer "{}"'), lex)) bUseFLn = True crts = ed.get_carets() pass; #LOG and log('lex, get_carets()={}', (lex, crts)) pass; #LOG and log('(bgn_sgn,end_sgn),bOnlyLn,bUseFLn={}', ((bgn_sgn,end_sgn),bOnlyLn,bUseFLn)) for icrt, (cCrt, rCrt, cEnd, rEnd) in enumerate(crts): pass; #LOG and log('(cCrt, rCrt), (cEnd, rEnd)={}', ((cCrt, rCrt), (cEnd, rEnd))) bEmpSel = -1==rEnd bDrtSel = -1==rEnd or (rCrt, cCrt)>(rEnd, cEnd) bEntireLn = (rEnd>=0) and (cEnd==0) and (cCrt==0) bEntireLn1 = bEntireLn and abs(rEnd-rCrt)==1 bEntireLn2 = bEntireLn and abs(rEnd-rCrt)>1 if False:pass elif bEmpSel and (bUseFLn or bOnlyLn): # Use full line line = ed.get_text_line(rCrt) (cTx1, rTx1), (cTx2, rTx2) = (0, rCrt), (len(line), rCrt) elif bOnlyLn: # and not bEmpSel # Only full lines rTx1, rTx2 = apx.minmax(rCrt, rEnd) line = ed.get_text_line(rTx2) (cTx1, rTx1), (cTx2, rTx2) = (0, rTx1), (len(line), rTx2) elif bEmpSel: # and not bUseFLn and not bOnlyLn continue else: (rTx1, cTx1), (rTx2, cTx2) = apx.minmax((rCrt, cCrt), (rEnd, cEnd)) selTx = ed.get_text_substr(cTx1, rTx1, cTx2, rTx2) pass; #LOG and log('(rTx1, cTx1), (rTx2, cTx2), selTx={}', ((rTx1, cTx1), (rTx2, cTx2), repr(selTx))) do_uncmt= selTx.startswith(bgn_sgn) #and selTx.endswith(end_sgn) # don't check for ending of selection - for HTML and entire selected line(s) pass; #LOG and log('do_uncmt={}', (do_uncmt)) if False:pass elif not do_uncmt and bOnlyLn: # Comment! ed.insert(0, rTx2+1, end_sgn+'\n') #! true insert sequence ed.insert(0, rTx1, bgn_sgn+'\n') #! true insert sequence (cNSel1, rNSel1 ,cNSel2, rNSel2) = 0, rTx1, len(end_sgn), rTx2+2 elif not do_uncmt: # Comment! if bEntireLn1: s = ed.get_text_line(rTx1) ed.set_text_line(rTx1, bgn_sgn+s+end_sgn) (cNSel1, rNSel1 ,cNSel2, rNSel2) = (0, rTx1, 0, rTx2) elif bEntireLn2: ed.insert(0, rTx2, end_sgn+'\n') ed.insert(0, rTx1, bgn_sgn+'\n') (cNSel1, rNSel1 ,cNSel2, rNSel2) = (0, rTx1, 0, rTx2+2) else: ed.insert(cTx2, rTx2, end_sgn) #! true insert sequence ed.insert(cTx1, rTx1, bgn_sgn) #! true insert sequence if False:pass elif rTx1==rTx2: # sel into one row (cNSel1, rNSel1 ,cNSel2, rNSel2) = cTx1, rTx1, cTx2+len(bgn_sgn)+len(end_sgn), rTx2 elif rTx1!=rTx2: # sel ends on diff rows (cNSel1, rNSel1 ,cNSel2, rNSel2) = cTx1, rTx1, cTx2 +len(end_sgn), rTx2 elif do_uncmt and bOnlyLn: # UnComment! ed.delete(0, rTx2, 0, rTx2+1) #! true delete sequence ed.delete(0, rTx1, 0, rTx1+1) #! true delete sequence (cNSel1, rNSel1 ,cNSel2, rNSel2) = 0, rTx1, len(ed.get_text_line(rTx2-2)), rTx2-2 elif do_uncmt: # UnComment! if selTx.endswith(end_sgn): ed.delete(cTx2-len(end_sgn), rTx2, cTx2, rTx2) #! true delete sequence ed.delete(cTx1, rTx1, cTx1+len(bgn_sgn), rTx1) #! true delete sequence if False:pass elif rTx1==rTx2: # sel into one row (cNSel1, rNSel1 ,cNSel2, rNSel2) = cTx1, rTx1, cTx2-len(bgn_sgn)-len(end_sgn), rTx2 elif rTx1!=rTx2: # sel ends on diff rows (cNSel1, rNSel1 ,cNSel2, rNSel2) = cTx1, rTx1, cTx2 -len(end_sgn), rTx2 elif bEntireLn1: s = ed.get_text_line(rTx1) if s.startswith(bgn_sgn): s = s[len(bgn_sgn):] if s.endswith(end_sgn): s = s[:-len(end_sgn)] ed.set_text_line(rTx1, s) (cNSel1, rNSel1 ,cNSel2, rNSel2) = (0, rTx1, 0, rTx2) elif bEntireLn2: ed.delete(0, rTx2-1, 0, rTx2) ed.delete(0, rTx1, 0, rTx1+1) (cNSel1, rNSel1 ,cNSel2, rNSel2) = (0, rTx1, 0, rTx2-2) pass; #LOG and log('bDrtSel, (cNSel1, rNSel1), (cNSel2, rNSel2)={}', (bDrtSel, (cNSel1, rNSel1), (cNSel2, rNSel2))) if bDrtSel: ed.set_caret(cNSel2, rNSel2, cNSel1, rNSel1, app.CARET_SET_INDEX+icrt) else: ed.set_caret(cNSel1, rNSel1, cNSel2, rNSel2, app.CARET_SET_INDEX+icrt) #for icrt bSkip = apx.get_opt('comment_move_down', True) if False:pass elif 1==len(crts) and bEmpSel and bUseFLn and bSkip: apx._move_caret_down(cCrt, rCrt) if bOnlyLn and not do_uncmt: crt=ed.get_carets()[0]; apx._move_caret_down(crt[0], crt[1]) crt=ed.get_carets()[0]; apx._move_caret_down(crt[0], crt[1])
def _prep_sess(self, wdex='word'): """ Params wdex Type of sess: 'word' / 'expr' """ crts = ed.get_carets() if len(crts)>1: return app.msg_status(_("Command doesnt work with multi-carets")) (cCrt, rCrt ,cEnd, rEnd)= crts[0] if -1!=rEnd and rCrt!=rEnd: return app.msg_status(_("Command doesnt work with multi-line selection")) stayed = self.sess \ and self.sess.pre_mver == ed.get_prop(app.PROP_MODIFIED_VERSION) \ and self.sess.pre_crt0 == crts[0] cEnd, rEnd = (cCrt, rCrt) if -1==rEnd else (cEnd, rEnd) sel_be_eb = 'be' if cCrt>cEnd else 'eb' if cCrt<cEnd else '' ((rSelB, cSelB) ,(rSelE, cSelE))= apx.minmax((rCrt, cCrt), (rEnd, cEnd)) pass; #LOG and stayed and log('stayed,(wdex,self.sess.wdex)={}',(stayed,(wdex,self.sess.wdex))) if stayed and wdex==self.sess.wdex: # Sess OK return True what = '' # Str to find what_b = -1 # Pos what begin what_e = -1 # Pos what end kill_b = -1 # Pos kill begin kill_e = -1 # Pos kill end wbnd = True # Need left word bound sel = '' if stayed and wdex!=self.sess.wdex: # Change sess type what = self.sess.src_what what_b = self.sess.src_what_b what_e = self.sess.src_what_e kill_b = self.sess.src_kill_b kill_e = self.sess.src_kill_e wbnd = self.sess.src_wbnd if not stayed: # New sess line = ed.get_text_line(rCrt) sel = ed.get_text_sel() if sel: # Use selection to find what = sel what_b = cSelB what_e = cSelE wbnd = 0==cSelB or line[cSelB-1].isspace() else: # Use "chars around caret" to find what tx_bfr = line[:cCrt ] ch_bfr = tx_bfr[-1] if tx_bfr else ' ' tx_aft = line[ cCrt:] ch_aft = tx_aft[ 0] if tx_aft else ' ' pass; #LOG and log('ch_bfr,ch_aft,tx_bfr,tx_aft={}',(ch_bfr,ch_aft,tx_bfr,tx_aft)) shf_l = 0 shf_r = 0 if ch_bfr.isalnum() or ch_bfr in self.wdsgns: tx_bfr_r= ''.join(reversed(tx_bfr)) shf_l = len(self.base_re.match(tx_bfr_r).group()) if ch_aft.isalnum() or ch_aft in self.wdsgns: shf_r = len(self.base_re.match(tx_aft).group()) what_b = cCrt-shf_l if self.kill: what_e = cCrt kill_b = cCrt kill_e = cCrt+shf_r else: what_e = cCrt+shf_r what = line[what_b:what_e] pass; #LOG and log('(shf_l,shf_r), (what_b,what_e), what={}',((shf_l,shf_r), (what_b,what_e), what)) if not what \ or not what.strip(): return app.msg_status(_('No data for search')) if len(what) < self.min_len: return app.msg_status(f(_('Need |base|>={}, but |"{}"|=={}'), self.min_len, what, len(what))) pass; #LOG and log('what,wbnd={}',(what,wbnd)) asword = wdex=='word' asexpr = wdex=='expr' # Make new Sess if not stayed: pass; #LOG and log('new sess',()) self.sess = Command.Sess() self.sess.row = rCrt self.sess.sel_sub = sel_be_eb self.sess.src_crt = cCrt self.sess.src_what = what self.sess.src_what_b= what_b self.sess.src_what_e= what_e self.sess.src_kill_b= kill_b self.sess.src_kill_e= kill_e self.sess.src_wbnd = wbnd self.sess.wdex = wdex what_re = re.compile('' + (r'\b' if wbnd else '') + re.escape(what) + (self.wdcmpl if asword else self.excmpl) ) # + r'[\w'+re.escape(self.wdsgns)+']+') pass; #LOG and log('what_re={}',(what_re.pattern)) bids_d = OrdDict() for line_n in range(ed.get_line_count()): if line_n == self.sess.row: continue#for line_n line = ed.get_text_line(line_n) if asexpr and self.expair: # Find+expand upto close brackets/quotes lbids_l = [] for m in what_re.finditer(line): lbid = m.group(0) ok = False for op, cl in self.opn2cls.items(): if op in lbid and lbid.count(op) > lbid.count(cl): # Try expand to good cl ext_bgn = m.end() ext_end = line.find(cl, ext_bgn) while -1!=ext_end: lbid_ext = lbid + line[ext_bgn:ext_end+1] if lbid_ext.count(op) == lbid_ext.count(cl): lbid,ok = lbid_ext, True break#while ext_end = line.find(cl, ext_end+1) if ok: break#for op #for op ok = False for qu in self.quotes: if qu in lbid and 1 == lbid.count(qu) % 2: # Try expand to good qu ext_bgn = m.end() ext_end = line.find(qu, ext_bgn) while -1!=ext_end: lbid_ext = lbid + line[ext_bgn:ext_end+1] if 0 == lbid_ext.count(qu) % 2: lbid,ok = lbid_ext, True break#while ext_end = line.find(cl, ext_end+1) if ok: break#for qu #for qu lbids_l.append(lbid) #for m else: # Find only lbids_l = what_re.findall(line) if lbids_l: bids_d.update({bid:line_n for bid in lbids_l if bid not in bids_d or abs(line_n-self.sess.row)<abs(bids_d[bid]-self.sess.row) }) # closest if not bids_d: return app.msg_status(_('No in-text completions')) self.sess.bids = list(bids_d.keys()) self.sess.bids_c= list(bids_d.values()) self.sess.bids_i= min([(abs(bid_r-self.sess.row), bid_i) for (bid_i,bid_r) in enumerate(self.sess.bids_c) ])[1] \ if len(self.sess.bids)>1 and self.near else \ 0 self.incr_bfr = False # Dont increment before use pass; #LOG and log('bids={}',(list(zip(self.sess.bids, self.sess.bids_c)))) pass; #LOG and log('sess={}',(self.sess)) pass; #return False return True