diff -ur v4/argmnt.py v5/argmnt.py --- v4/argmnt.py 2018-10-17 02:11:40.000000000 +0900 +++ v5/argmnt.py 2018-10-17 02:54:24.000000000 +0900 @@ -34,8 +34,13 @@ i = ( lst.index(v) + 1 ) % len(lst) return lst[i] -def is_line(o): - return hasattr(o, 'typ') and o.typ == 'line' +has_typ = lambda o: hasattr(o, 'typ') + +def is_typ(o, typ): + return has_typ(o) and o.typ == typ + +is_line = lambda o: is_typ(o, 'line') +is_rect = lambda o: is_typ(o, 'rect') def get_p2_vh(p1, p2): (dx, dy) = rect.p_sub(p2, p1) @@ -101,6 +106,8 @@ def get_rect(o, frm_force=False): if is_line(o): return contain_rect( get_line_rects(o) ) + if is_rect(o): + return o.r if o.frm.kind or frm_force: return get_frm_rect(o) return get_tx_rect(o) @@ -108,6 +115,8 @@ def get_rects(o, frm_force=False): if is_line(o): return get_line_rects(o) + if is_rect(o): + return frame_rects(o.r) rects = [ get_tx_rect(o) ] if o.frm.kind or frm_force: rects += frame_rects( get_frm_rect(o) ) @@ -116,11 +125,8 @@ def p_on_frm(p, 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 - return False + r = o.r if is_rect(o) else get_frm_rect(o) + return rect.p_in_rects(p, frame_rects(r) ) def align_str(s, w, a): m = w - str_width(s) @@ -133,25 +139,28 @@ s = ' ' * m2 + s + ' ' * (m-m2) return s -def draw_buf_frm_atts(buf, o, atts): - cs = frm_info.get( o.frm.kind ) - (x, y, w, h) = get_frm_rect(o) - - s = cs[0] + cs[1]*(w-2) + cs[0] +def draw_buf_rect(buf, r, atts, kind): + (x, y, w, h) = r + cs = frm_info.get(kind) + s = cs[0] + cs[1]*(w-2) + (cs[0] if w > 1 else '') buf.set(x, y, s, atts) buf.set(x, y+h-1, s, atts) for i in range(1,h-1): buf.set(x, y+i, cs[2], atts) buf.set(x+w-1, y+i, cs[2], atts) -def draw_buf_o_atts(buf, o, atts, do_frm, frm_atts): +def draw_buf_frm(buf, o, atts): + r = get_frm_rect(o) + draw_buf_rect( buf, r, atts, o.frm.kind ) + +def draw_buf_o(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_buf_frm_atts(buf, o, frm_atts) + draw_buf_frm(buf, o, frm_atts) -def draw_buf_line_atts(buf, o, atts): +def draw_buf_line(buf, o, atts): if o.kind == 'd': # at sel_kind() return @@ -184,10 +193,13 @@ def draw_buf(buf, o, atts, frm_atts=None): if is_line(o): - draw_buf_line_atts(buf, o, atts) + draw_buf_line(buf, o, atts) + elif is_rect(o): + if o.kind: + draw_buf_rect(buf, o.r, atts, o.kind) else: do_frm = (frm_atts != None) - draw_buf_o_atts(buf, o, atts, do_frm, frm_atts) + draw_buf_o(buf, o, atts, do_frm, frm_atts) def new(lst, nkf_opt): e = Empty() @@ -200,7 +212,7 @@ e.cur = (x, y) def frm_visible(o): - if is_line(o): + if has_typ(o): return False if o.frm.kind != '': return True @@ -221,9 +233,12 @@ return False def get_atts(o, buf): if o == e.sel: - if is_line(o): - if e.mode in('sel_2', 'sel_kind'): + if is_line(o) or is_rect(o): + if e.mode in ('sel_2', 'sel_kind'): return [ buf.uline ] + if is_rect(o): + if e.mode in ('sel_3', 'resize'): + return [ buf.rev ] if e.mode in ('sel', 'move', 'frm_align'): return [ buf.rev ] if e.mode in ('sel_align'): @@ -245,14 +260,23 @@ for o in lst: if is_cross(o, r): atts = get_atts(o, buf) - frm_atts = get_frm_atts(o, buf) if not is_line(o) and frm_visible(o) else None + frm_atts = None + if not has_typ(o) and frm_visible(o): + frm_atts = get_frm_atts(o, buf) draw_buf(buf, o, atts, frm_atts) - # cursor - if e.mode in ('normal', 'sel_4', 'line'): - if rect.in_rect(e.cur, r): - (x, y) = e.cur - atts = [ buf.rev ] if e.mode in ('sel_4', 'line') else [] - buf.set(x, y, '+', atts) + + def do_cursor(): + if not rect.in_rect(e.cur, r): + return False + if is_rect(e.sel) and e.mode in ('sel_3', 'resize'): + return True + return e.mode in ('normal', 'sel_4', 'line', 'sel_5', 'rect') + + if do_cursor(): + (x, y) = e.cur + atts = [ buf.rev ] if e.mode in ('sel_4', 'line') else [] + c = '#' if e.mode in ('sel_5', 'rect') else '+' + buf.set(x, y, c, atts) buf.adjust() (rx, ry, rw, rh) = r @@ -288,9 +312,11 @@ return lambda : f( get_o_rect(o) ) def move_cur(dx, dy): - f = new_draw_rect( rect.p_wh_to( e.cur, (1, 1) ) ) - e.cur = rect.p_add( e.cur, (dx, dy) ) - f( rect.p_wh_to( e.cur, (1, 1) ) ) + r = rect.p_wh_to( e.cur, (1, 1) ) + f = new_draw_rect(r) + r = rect.move( r, (dx, dy) ) + e.cur = rect.get_p(r) + f(r) def get_o_under_cursor(): for o in reversed(lst): @@ -308,6 +334,8 @@ e.sel = None if is_line(o) and o.kind == 'd': lst.remove(o) + if is_rect(o) and o.kind == '': + lst.remove(o) if f: f() # cursor @@ -333,14 +361,30 @@ def resize(k): o = e.sel + (dx, dy) = dxy = key.dxy( k, [0, 0] ) f = new_draw(o) - (bx, by) = o.frm.bdr - (dx, dy) = key.dxy( k, [0, 0] ) - (bx, by) = ( max( bx+dx, 0 ), max( by+dy, 0 ) ) - if [bx, by] != o.frm.bdr: - o.frm.bdr = [bx, by] # list + if is_rect(o): + ((x1,y1),(x2,y2)) = rect.to_p2(o.r) + (x2, y2) = (x2-1, y2-1) + (xx, yy) = ( [x1,x2], [y1,y2] ) + (cx, cy) = e.cur + xi = xx.index(cx) if cx in xx else -1 + yi = yy.index(cy) if cy in yy else -1 + if xi >= 0: + xx[xi] += dx + if yi >= 0: + yy[yi] += dy + ((x1,x2),(y1,y2)) = ( sorted(xx), sorted(yy) ) + o.r = list( rect.p2_to((x1,y1),(x2+1,y2+1)) ) f() - #move_cur(dx, dy) # ... + move_cur(dx, dy) + else: + (bx, by) = o.frm.bdr + (bx, by) = ( max( bx+dx, 0 ), max( by+dy, 0 ) ) + if [bx, by] != o.frm.bdr: + o.frm.bdr = [bx, by] # list + f() + #move_cur(dx, dy) # ... def sel_align(k): o = e.sel @@ -350,12 +394,14 @@ def move(k): o = e.sel - (dx, dy) = key.dxy( k, [0, 0] ) + (dx, dy) = dxy = key.dxy( k, [0, 0] ) f = new_draw(o) if is_line(o): for p in o.lst: p[0] += dx p[1] += dy + elif is_rect(o): + o.r = list( rect.move(o.r, dxy) ) else: (o.x, o.y) = (o.x + dx, o.y + dy) f() @@ -364,10 +410,13 @@ def sel_kind(k): o = e.sel f = new_draw(o) + rlst = sorted( frm_info.keys() ) if is_line(o): o.kind = get_next( ['', 's', 'e', 'b', 'd'], o.kind ) + elif is_rect(o): + o.kind = get_next( rlst, o.kind ) else: - o.frm.kind = get_next( sorted( frm_info.keys() ), o.frm.kind ) + o.frm.kind = get_next( rlst, o.frm.kind ) f() def frm_align(k): @@ -385,7 +434,7 @@ def line(k): (dx, dy) = dxy = key.dxy( k, [0, 0] ) - if dx == 0 and dy == 0: + if dxy == (0, 0): return if e.sel: o = e.sel @@ -408,8 +457,42 @@ move_cur(dx, dy) + def f_rect(k): + (dx, dy) = dxy = key.dxy( k, [0, 0] ) + if dxy == (0, 0): + return + if e.sel: + o = e.sel + else: + o = Empty() + o.typ = 'rect' + (cx, cy) = e.cur + o.r = [ cx, cy, 1, 1 ] + o.kind = 'a' + e.sel = o + lst.append(o) + + f = new_draw(o) + + ((x1,y1),(x2,y2)) = rect.to_p2(o.r) + (x2, y2) = (x2-1, y2-1) + (xx, yy) = ( [x1,x2], [y1,y2] ) + (cx, cy) = e.cur + xi = 0 if xx[0] == cx else 1 + yi = 0 if yy[0] == cy else 1 + xx[xi] += dx + yy[yi] += dy + ((x1,x2),(y1,y2)) = ( sorted(xx), sorted(yy) ) + o.r = list( rect.p2_to((x1,y1),(x2+1,y2+1)) ) + + f() + move_cur(dx, dy) + def has_bdr(k): - (bx, by) = e.sel.frm.bdr + o = e.sel + if has_typ(o): + return False + (bx, by) = o.frm.bdr return bx > 0 or by > 0 hdl = { @@ -437,8 +520,10 @@ 'allow': [ 'resize', resize ] }, 'resize': { 'ok': 'normal', 'allow': resize }, 'frm_align': { 'ok': 'normal', 'allow': frm_align }, - 'sel_4': { 'ok': 'normal', 'allow': [ 'line', line ] }, + 'sel_4': { 'ok': 'sel_5', 'allow': [ 'line', line ] }, 'line': { 'ok': 'normal', 'allow': line }, + 'sel_5': { 'ok': 'normal', 'allow': [ 'rect', f_rect ] }, + 'rect': { 'ok': 'normal', 'allow': f_rect }, } def call(o, k): @@ -480,7 +565,7 @@ return e -def to_empty(d): +def to_class(d): e = Empty() for (k, v) in d.items(): setattr(e, k, v) @@ -492,10 +577,11 @@ if type(o) == list: o = { 's': o } # dict - if o.get('typ') == 'line': + typ = o.get('typ') + if typ == 'line': if 'kind' not in o: o['kind'] = '' - else: + elif typ == None: if 'xy' not in o: o['xy'] = [ 1, y+1 ] y += len( o.get('s') ) + 1 @@ -504,24 +590,27 @@ if 'frm' not in o: o['frm'] = { 'kind': '', 'va': 'c', 'ha': 'c', 'bdr': [ 0, 0 ] } - e = to_empty(o) - if not hasattr(e, 'typ'): + e = to_class(o) + if not has_typ(e): if type(e.s) != list: e.s = [ e.s ] (e.x, e.y) = e.xy e.w = max( map( str_width, e.s ) ) e.h = len(e.s) - e.frm = to_empty(e.frm) + e.frm = to_class(e.frm) return (e, y) def cvt_data_save(e): d = {} - if hasattr(e, 'typ'): + if has_typ(e): d['typ'] = e.typ if is_line(e): d['lst'] = e.lst d['kind'] = e.kind + elif is_rect(e): + d['r'] = e.r + d['kind'] = e.kind else: if len(e.s) == 1: e.s = e.s[0] @@ -545,7 +634,7 @@ buf = txbuf.new(r) for o in lst: atts = [] - frm_atts = [] if not is_line(o) and o.frm.kind else None + frm_atts = [] if not has_typ(o) and o.frm.kind else None draw_buf(buf, o, atts, frm_atts) s = nkf.enc( buf.txt_out() ) diff -ur v4/rect.py v5/rect.py --- v4/rect.py 2018-10-16 05:36:26.000000000 +0900 +++ v5/rect.py 2018-10-17 02:13:14.000000000 +0900 @@ -37,6 +37,10 @@ (w, h) = p_sub(p2, p) return (x, y, w, h) +def move(r, dxy): + (p, wh) = to_p_wh(r) + return p_wh_to( p_add(p, dxy), wh ) + def op_and(ra, rb): ( (ax, ay), (ax2, ay2) ) = to_p2(ra) ( (bx, by), (bx2, by2) ) = to_p2(rb)