diff -ur v3/argmnt.py v4/argmnt.py --- v3/argmnt.py 2018-10-16 05:37:32.000000000 +0900 +++ v4/argmnt.py 2018-10-17 02:11:40.000000000 +0900 @@ -1,6 +1,8 @@ #!/usr/bin/env python import sys +import os +import subprocess as sp import six import nkf import yaml @@ -23,6 +25,14 @@ pass str_width = nkf.str_width +frame_rects = rect.frame_rects +contain_rect = rect.rects_contain_rect + +def get_next(lst, v): + i = 0 + if v in lst: + i = ( lst.index(v) + 1 ) % len(lst) + return lst[i] def is_line(o): return hasattr(o, 'typ') and o.typ == 'line' @@ -31,6 +41,18 @@ (dx, dy) = rect.p_sub(p2, p1) return 'v' if dx == 0 else 'h' +def get_d_p1_to_p2(p1, p2): # udlr + (dx, dy) = rect.p_sub(p2, p1) + if get_p2_vh(p1, p2) == 'h': + d = 'l' if dx < 0 else 'r' + else: + d = 'u' if dy < 0 else 'd' + return d + +def get_allow_p1_to_p2(p1, p2): # ^v<> + d = get_d_p1_to_p2(p1, p2) + return { 'u':'^', 'd': 'v', 'l': '<', 'r': '>' }.get(d, '') + def get_tx_rect(o): return (o.x, o.y, o.w, o.h) @@ -76,8 +98,25 @@ rects += [r] return rects +def get_rect(o, frm_force=False): + if is_line(o): + return contain_rect( get_line_rects(o) ) + if o.frm.kind or frm_force: + return get_frm_rect(o) + return get_tx_rect(o) + +def get_rects(o, frm_force=False): + if is_line(o): + return get_line_rects(o) + rects = [ get_tx_rect(o) ] + if o.frm.kind or frm_force: + rects += frame_rects( get_frm_rect(o) ) + return rects + def p_on_frm(p, o): - rects = rect.frame_rects( get_frm_rect(o) ) + if is_line(o): + return False + rects = frame_rects( get_frm_rect(o) ) for r in rects: if rect.in_rect(p, r): return True @@ -94,7 +133,7 @@ s = ' ' * m2 + s + ' ' * (m-m2) return s -def draw_frm_buf_atts(o, buf, atts): +def draw_buf_frm_atts(buf, o, atts): cs = frm_info.get( o.frm.kind ) (x, y, w, h) = get_frm_rect(o) @@ -105,14 +144,17 @@ buf.set(x, y+i, cs[2], atts) buf.set(x+w-1, y+i, cs[2], atts) -def draw_o_buf_atts(o, buf, atts, do_frm, frm_atts): +def draw_buf_o_atts(buf, o, atts, do_frm, frm_atts): for i in range(o.h): s = align_str( o.s[i], o.w, o.a ) buf.set( o.x, o.y+i, s, atts ) if do_frm: - draw_frm_buf_atts(o, buf, frm_atts) + draw_buf_frm_atts(buf, o, frm_atts) + +def draw_buf_line_atts(buf, o, atts): + if o.kind == 'd': # at sel_kind() + return -def draw_line_buf_atts(o, buf, atts): line_n = len(o.lst) - 1 for i in range( line_n ): ((x1,y1),(x2,y2)) = (p1, p2) = o.lst[i:i+2] @@ -128,54 +170,63 @@ for (x, y) in o.lst[1:-1]: buf.set(x, y, '+', atts) + if o.kind in ('s', 'b'): + (p2, p1) = o.lst[:2] + c = get_allow_p1_to_p2(p1, p2) + (x, y) = p2 + buf.set(x, y, c, atts) + + if o.kind in ('e', 'b'): + (p1, p2) = o.lst[-2:] + c = get_allow_p1_to_p2(p1, p2) + (x, y) = p2 + buf.set(x, y, c, atts) + +def draw_buf(buf, o, atts, frm_atts=None): + if is_line(o): + draw_buf_line_atts(buf, o, atts) + else: + do_frm = (frm_atts != None) + draw_buf_o_atts(buf, o, atts, do_frm, frm_atts) + def new(lst, nkf_opt): e = Empty() - rects = [] - for o in lst: - if is_line(o): - rs = get_line_rects(o) - else: - rs = [ get_frm_rect(o) ] - rects += rs - - r = rect.rects_contain_rect(rects) + rects = sum( map( get_rects, lst ), [] ) + r = contain_rect(rects) view = txview.new(r, nkf_opt) (x, y, w, h) = view.get_rect() e.cur = (x, y) def frm_visible(o): + if is_line(o): + return False if o.frm.kind != '': return True if o != e.sel: return False - return e.mode in ('sel_2', 'sel_frm') + return e.mode in ('sel_2', 'sel_kind') def get_o_rect(o): - return get_frm_rect(o) if frm_visible(o) else get_tx_rect(o) + return get_rect( o, frm_visible(o) ) def get_o_rects(o): - rects = [] - if is_line(o): - rects += get_line_rects(o) - else: - rects += [ get_tx_rect(o) ] - if frm_visible(o): - rects += rect.frame_rects( get_frm_rect(o) ) - return rects + return get_rects( o, frm_visible(o) ) def is_cross(o, r): - rects = get_o_rects(o) if is_line(o) else [ get_o_rect(o) ] - for r_ in rects: + for r_ in get_o_rects(o): if rect.is_cross(r_, r): return True return False def get_atts(o, buf): if o == e.sel: + if is_line(o): + if e.mode in('sel_2', 'sel_kind'): + return [ buf.uline ] if e.mode in ('sel', 'move', 'frm_align'): return [ buf.rev ] - elif e.mode in ('sel_align'): + if e.mode in ('sel_align'): return [ buf.uline ] return [] @@ -183,26 +234,19 @@ if o == e.sel: if e.mode in ('sel', 'move', 'sel_3', 'resize'): return [ buf.rev ] - if e.mode in ('sel_2', 'sel_frm'): + if e.mode in ('sel_2', 'sel_kind'): return [ buf.uline ] return [] - def draw_o_buf(o, buf): - atts = get_atts(o, buf) - if is_line(o): - draw_line_buf_atts(o, buf, atts) - else: - do_frm = frm_visible(o) - frm_atts = get_frm_atts(o, buf) - draw_o_buf_atts(o, buf, atts, do_frm, frm_atts) - def draw_r(r, out=None): r = view.scroll(r) buf = txbuf.new(r) for o in lst: if is_cross(o, r): - draw_o_buf(o, buf) + atts = get_atts(o, buf) + frm_atts = get_frm_atts(o, buf) if not is_line(o) and frm_visible(o) else None + draw_buf(buf, o, atts, frm_atts) # cursor if e.mode in ('normal', 'sel_4', 'line'): if rect.in_rect(e.cur, r): @@ -222,19 +266,16 @@ out(s) return s - def draw_o(o): - draw_r( get_o_rect(o), term.out ) - def new_draw_rect(bak): def f(r): - r = rect.rects_contain_rect( [ bak, r ] ) + r = contain_rect( [ bak, r ] ) draw_r( r, term.out ) return f def new_draw_rects(bak): def f(rects): for (b, r) in zip(bak, rects): - r = rect.rects_contain_rect( [ b, r ] ) + r = contain_rect( [ b, r ] ) draw_r( r, term.out ) return f @@ -265,6 +306,8 @@ e.mode = mode if mode == 'normal': e.sel = None + if is_line(o) and o.kind == 'd': + lst.remove(o) if f: f() # cursor @@ -301,9 +344,9 @@ def sel_align(k): o = e.sel - lst = ['l', 'c', 'r'] - o.a = lst[ ( lst.index(o.a) + 1 ) % len(lst) ] - draw_o(o) + f = new_draw(o) + o.a = get_next( ['l', 'c', 'r'], o.a ) + f() def move(k): o = e.sel @@ -318,11 +361,14 @@ f() move_cur(dx, dy) # hidden - def sel_frm(k): + def sel_kind(k): o = e.sel - lst = sorted( frm_info.keys() ) - o.frm.kind = lst[ ( lst.index(o.frm.kind) + 1 ) % len(lst) ] - draw_o(o) + f = new_draw(o) + if is_line(o): + o.kind = get_next( ['', 's', 'e', 'b', 'd'], o.kind ) + else: + o.frm.kind = get_next( sorted( frm_info.keys() ), o.frm.kind ) + f() def frm_align(k): o = e.sel @@ -347,6 +393,7 @@ o = Empty() o.typ = 'line' o.lst = [ list(e.cur) ] + o.kind = '' e.sel = o lst.append(o) @@ -378,12 +425,12 @@ 'f': ( lambda k: p_on_frm(e.cur, e.sel) ), True: 'sel_3', False: { - 'f': ( lambda k: len(e.sel.s) > 1 ), + 'f': ( lambda k: not is_line(e.sel) and len(e.sel.s) > 1 ), True: 'sel_align', False: 'normal' } }, - 'allow': [ 'sel_frm', sel_frm ] }, - 'sel_frm': { 'ok': 'normal', 'allow': sel_frm }, + 'allow': [ 'sel_kind', sel_kind ] }, + 'sel_kind': { 'ok': 'normal', 'allow': sel_kind }, 'sel_align': { 'ok': 'normal', 'allow': sel_align }, 'sel_3': { 'ok': { 'f': has_bdr, True: 'frm_align', False: 'normal' }, @@ -445,7 +492,10 @@ if type(o) == list: o = { 's': o } # dict - if 'typ' not in o: + if o.get('typ') == 'line': + if 'kind' not in o: + o['kind'] = '' + else: if 'xy' not in o: o['xy'] = [ 1, y+1 ] y += len( o.get('s') ) + 1 @@ -471,6 +521,7 @@ d['typ'] = e.typ if is_line(e): d['lst'] = e.lst + d['kind'] = e.kind else: if len(e.s) == 1: e.s = e.s[0] @@ -489,11 +540,13 @@ return d def txt_out(lst, nkf_opt): - rects = list( map( get_frm_rect, lst ) ) - r = rect.rects_contain_rect(rects) + rects = sum( map( get_rects, lst ), [] ) + r = contain_rect(rects) buf = txbuf.new(r) for o in lst: - draw_o_buf_atts(o, buf, [], o.frm.kind, []) + atts = [] + frm_atts = [] if not is_line(o) and o.frm.kind else None + draw_buf(buf, o, atts, frm_atts) s = nkf.enc( buf.txt_out() ) if nkf_opt != '-u': @@ -552,6 +605,11 @@ b = nkf.cvt(u8, nkf_opt) if nkf_opt != '-u' else u8 if fn_i != '-' and opt.index('-i') >= 0: + def bak_fn(fn, i=0): + bak = '{}.{}'.format(fn, i) + return bak_fn(fn, i+1) if os.path.exists(bak) else bak + + sp.call( 'mv {} {}'.format( fn_i, bak_fn(fn_i) ), shell=True ) f = open(fn_i, 'w') f.write(b) f.close()