margins are complicated

--HG--
branch : pmacs2
This commit is contained in:
moculus 2008-04-07 07:16:34 +00:00
parent 14b19a14ff
commit 560bf76984
3 changed files with 69 additions and 57 deletions

View File

@ -524,6 +524,7 @@ class Application(object):
self.draw_cursor() self.draw_cursor()
self.win.refresh() self.win.refresh()
except Exception, e: except Exception, e:
raise
# ok, so there was a problem... # ok, so there was a problem...
# let's see if the screen changed sizes and if so, resize our slots # let's see if the screen changed sizes and if so, resize our slots
self.resize_event() self.resize_event()
@ -547,23 +548,24 @@ class Application(object):
else: else:
slot = self.bufferlist.slots[self.active_slot] slot = self.bufferlist.slots[self.active_slot]
w = slot.window w = slot.window
swidth = slot.width - w.mode.lmargin - w.mode.rmargin
if w.active_point is not None and w.point_is_visible(w.active_point): if w.active_point is not None and w.point_is_visible(w.active_point):
p = w.active_point p = w.active_point
else: else:
p = w.logical_cursor() p = w.logical_cursor()
count = 0 count = 0
(x, y) = w.first.xy() (x, y) = w.first.xy()
(vy, vx) = (None, None) (vy, vx) = (None, None)
while count < slot.height: while count < slot.height:
if p.y == y and p.x >= x and p.x <= x + slot.width: if p.y == y and p.x >= x and p.x <= x + swidth:
#(vy, vx) = (slot.y_offset + count, p.x - x + slot.lmargin)
(vy, vx) = (slot.y_offset + count, p.x - x + w.mode.lmargin) (vy, vx) = (slot.y_offset + count, p.x - x + w.mode.lmargin)
break break
if x + slot.width >= len(w.buffer.lines[y]): if x + swidth >= len(w.buffer.lines[y]):
x = 0 x = 0
y += 1 y += 1
else: else:
x += slot.width x += swidth
count += 1 count += 1
if vy is None or vx is None: if vy is None or vx is None:
return return
@ -609,7 +611,6 @@ class Application(object):
self._draw_slot_lit(i) self._draw_slot_lit(i)
else: else:
self._draw_slot_raw(i) self._draw_slot_raw(i)
self.draw_slot_margins(i)
# highlighted regions # highlighted regions
for (high_w, p1, p2, fg, bg) in self.highlighted_ranges: for (high_w, p1, p2, fg, bg) in self.highlighted_ranges:
@ -620,11 +621,9 @@ class Application(object):
while count < slot.height: while count < slot.height:
if p1.y == y and px >= x and px - x < slot.width: if p1.y == y and px >= x and px - x < slot.width:
if slot.width > p2.x - x: if slot.width > p2.x - x:
#self.highlight_chars(slot.y_offset + count, px-x + slot.lmargin, p2.x-x, fg, bg)
self.highlight_chars(slot.y_offset + count, px-x + w.mode.lmargin, p2.x-x, fg, bg) self.highlight_chars(slot.y_offset + count, px-x + w.mode.lmargin, p2.x-x, fg, bg)
break break
else: else:
#self.highlight_chars(slot.y_offset + count, px-x + slot.lmargin, slot.width, fg, bg)
self.highlight_chars(slot.y_offset + count, px-x + w.mode.lmargin, slot.width, fg, bg) self.highlight_chars(slot.y_offset + count, px-x + w.mode.lmargin, slot.width, fg, bg)
px += slot.width - px + x px += slot.width - px + x
if x + slot.width >= len(w.buffer.lines[y]): if x + slot.width >= len(w.buffer.lines[y]):
@ -640,26 +639,25 @@ class Application(object):
for j in range(0, slot.height): for j in range(0, slot.height):
char = chr(self.win.inch(j + slot.y_offset, limit) & 255) char = chr(self.win.inch(j + slot.y_offset, limit) & 255)
attr = color.build('default', shade, 'bold') attr = color.build('default', shade, 'bold')
#self.win.addstr(j + slot.y_offset, limit + slot.lmargin, char, attr)
self.win.addstr(j + slot.y_offset, limit + w.mode.lmargin, char, attr) self.win.addstr(j + slot.y_offset, limit + w.mode.lmargin, char, attr)
def draw_slot_margins(self, i): def _draw_line_margins(self, slot, count, w, y, x):
slot = self.bufferlist.slots[i] lm, rm = w.mode.lmargin, w.mode.rmargin
w = slot.window if y >= len(w.buffer.lines):
b = w.buffer ended = True
#x = slot.lmargin + slot.width + 1 cont = False
x = w.mode.lmargin + slot.width + 1 else:
for i in range(0, slot.height): ended = False
y = i + slot.y_offset cont = x + slot.width - lm - rm < len(w.buffer.lines[y])
#if slot.lmargin:
if w.mode.lmargin:
(lmargin, lattr) = w.mode.get_lmargin(i)
self.win.addstr(y, 0, lmargin, lattr)
#if slot.rmargin:
if w.mode.rmargin:
(rmargin, rattr) = w.mode.get_rmargin(i)
self.win.addstr(y, x, rmargin, rattr)
i = slot.y_offset + count
if lm:
(lmargin, lattr) = w.mode.get_lmargin(y, x, ended, cont)
self.win.addstr(i, 0, lmargin, lattr)
if rm:
(rmargin, rattr) = w.mode.get_rmargin(y, x, ended, cont)
self.win.addstr(i, slot.width - rm, rmargin, rattr)
def _draw_slot_raw(self, i): def _draw_slot_raw(self, i):
slot = self.bufferlist.slots[i] slot = self.bufferlist.slots[i]
w = slot.window w = slot.window
@ -669,31 +667,32 @@ class Application(object):
(x, y) = w.first.xy() (x, y) = w.first.xy()
lines = w.buffer.lines lines = w.buffer.lines
count = 0 count = 0
swidth = slot.width - w.mode.lmargin - w.mode.rmargin lm, rm = w.mode.lmargin, w.mode.rmargin
swidth = slot.width - lm - rm
while count < slot.height: while count < slot.height:
#draw some margins
self._draw_line_margins(slot, count, w, y, x)
# if the window has no more lines, display the "empty" symbol
if y >= len(lines): if y >= len(lines):
#self.win.addstr(slot.y_offset + count, 0 + slot.lmargin, '~', redattr) self.win.addstr(slot.y_offset + count, 0 + lm, '~', redattr)
self.win.addstr(slot.y_offset + count, 0 + w.mode.lmargin, '~', redattr)
count += 1 count += 1
continue continue
# get the line to draw and draw it
line = lines[y] line = lines[y]
#s = line[x:x + slot.width]
s = line[x:x + swidth] s = line[x:x + swidth]
try: self.win.addstr(slot.y_offset + count, 0 + lm, s)
#self.win.addstr(slot.y_offset + count, 0 + slot.lmargin, s)
self.win.addstr(slot.y_offset + count, 0 + w.mode.lmargin, s) # see if we need to draw a line-continuation symbol or not
except:
#self.set_error("addstr(%r+%r, %r+%r, %r)" % (slot.y_offset, count, 0, slot.lmargin, s))
self.set_error("addstr(%r+%r, %r+%r, %r)" % (slot.y_offset, count, 0, w.mode.lmargin, s))
#if x + slot.width >= len(line):
if x + swidth >= len(line): if x + swidth >= len(line):
x = 0 x = 0
y += 1 y += 1
else: else:
#self.win.addstr(slot.y_offset + count, slot.width + slot.lmargin, '\\', redattr) self.win.addstr(slot.y_offset + count, slot.width, '\\', redattr)
self.win.addstr(slot.y_offset + count, swidth + w.mode.lmargin, '\\', redattr)
x += swidth x += swidth
# move on to the next physical line
count += 1 count += 1
def _draw_slot_lit(self, i): def _draw_slot_lit(self, i):
@ -706,6 +705,8 @@ class Application(object):
(x, y) = w.first.xy() (x, y) = w.first.xy()
j = 0 j = 0
count = 0 count = 0
(lm, rm) = (w.mode.lmargin, w.mode.rmargin)
swidth = slot.width - lm - rm
assert len(w.buffer.lines) == len(highlighter.tokens) assert len(w.buffer.lines) == len(highlighter.tokens)
while count < slot.height: while count < slot.height:
if y < len(w.buffer.lines): if y < len(w.buffer.lines):
@ -720,23 +721,24 @@ class Application(object):
s_offset = max(x - token.x, 0) s_offset = max(x - token.x, 0)
x_offset = max(token.x - x, 0) x_offset = max(token.x - x, 0)
swidth = slot.width - w.mode.lmargin - w.mode.rmargin
assert x_offset <= swidth, '%d <= %d' % (x_offset, swidth) assert x_offset <= swidth, '%d <= %d' % (x_offset, swidth)
s = tstring[s_offset:] s = tstring[s_offset:]
token_done = x_offset + len(s) <= swidth token_done = x_offset + len(s) <= swidth
token_wrap = x_offset + len(s) > swidth token_wrap = x_offset + len(s) > swidth
# for debugging things like lexing/relexing/etc. # for debugging things like lexing/relexing/etc.
if token._debug: if token._debug:
attr = color.build('blue', 'green') attr = color.build('blue', 'green')
else: else:
attr = color.build(*token.color) attr = color.build(*token.color)
#self.win.addstr(slot.y_offset + count, x_offset + slot.lmargin, s[:slot.width - x_offset], attr)
self.win.addstr(slot.y_offset + count, x_offset + w.mode.lmargin, s[:swidth - x_offset], attr) k = slot.y_offset + count
self.win.addstr(k, x_offset + lm, s[:swidth - x_offset], attr)
if token_wrap: if token_wrap:
#self.win.addstr(slot.y_offset + count, slot.width + slot.lmargin, '\\', redattr) self._draw_line_margins(slot, count, w, y, x)
self.win.addstr(slot.y_offset + count, swidth + w.mode.lmargin, '\\', redattr) self.win.addstr(k, slot.width, '\\', redattr)
x += swidth x += swidth
count += 1 count += 1
if token_done: if token_done:
@ -745,12 +747,13 @@ class Application(object):
break break
# we have finished this logical line of tokens # we have finished this logical line of tokens
self._draw_line_margins(slot, count, w, y, x)
j = x = 0 j = x = 0
y += 1 y += 1
count += 1 count += 1
else: else:
#self.win.addstr(slot.y_offset + count, 0 + slot.lmargin, '~', redattr) self._draw_line_margins(slot, count, w, y, x)
self.win.addstr(slot.y_offset + count, 0 + w.mode.lmargin, '~', redattr) self.win.addstr(slot.y_offset + count, 0 + lm, '~', redattr)
count += 1 count += 1
def draw_status_bar(self, slotname): def draw_status_bar(self, slotname):

View File

@ -4,23 +4,19 @@ import window
class Slot(object): class Slot(object):
def __init__(self, height, width, y_offset=0, x_offset=0): def __init__(self, height, width, y_offset=0, x_offset=0):
self.window = None self.window = None
self.lmargin = 0
self.rmargin = 0
self.resize(height, width, y_offset, x_offset) self.resize(height, width, y_offset, x_offset)
def is_empty(self): def is_empty(self):
return self.window is None return self.window is None
def resize(self, height, width, y_offset=0, x_offset=0): def resize(self, height, width, y_offset=0, x_offset=0):
self.height = height self.height = height
self.total_width = width self.total_width = width
self.width = width - self.lmargin - self.rmargin self.width = width
self.y_offset = y_offset self.y_offset = y_offset
self.x_offset = x_offset self.x_offset = x_offset
if self.window is not None: if self.window is not None:
self.window.set_size(self.width, self.height) self.window.set_size(self.width, self.height)
def set(self, w): def set(self, w):
self.window = w self.window = w
self.lmargin = w.mode.lmargin
self.rmargin = w.mode.rmargin
self.resize(self.height, self.width, self.y_offset, self.x_offset) self.resize(self.height, self.width, self.y_offset, self.x_offset)
w.set_size(self.width, self.height) w.set_size(self.width, self.height)
def unset(self): def unset(self):

View File

@ -1,4 +1,4 @@
import os, sets, string import math, os, sets, string
import color, method import color, method
from lex import Lexer from lex import Lexer
from point import Point from point import Point
@ -90,8 +90,8 @@ class Fundamental(Handler):
# margin/line numbering # margin/line numbering
show_line_numbers = False show_line_numbers = False
lmargin = 0 lmargin = 0
rmargin = 0 rmargin = 1
def install(cls, app): def install(cls, app):
app.setmode(cls.modename.lower(), cls, paths=cls.paths, app.setmode(cls.modename.lower(), cls, paths=cls.paths,
@ -222,14 +222,27 @@ class Fundamental(Handler):
self.lmargin = 0 self.lmargin = 0
self.get_lmargin = self._null_margin self.get_lmargin = self._null_margin
def get_rmargin(self, i): def get_rmargin(self, y, x, ended=False, cont=False):
return ('', color.build('default', 'default')) return ('', color.build('default', 'default'))
def _null_margin(self, i): def _null_margin(self, y, x, ended=False, cont=False):
return ('', color.build('default', 'default')) return ('', color.build('default', 'default'))
def _line_number_margin(self, i): def _continuation_margin(self, y, x, ended=False, cont=False):
return ('% 3d ' % i, color.build('default', 'default', 'bold')) if cont:
return ('\\', color.build('green', 'default'))
else:
return ('x', color.build('green', 'default'))
def _line_number_margin(self, y, x=0, ended=False, cont=False):
if ended:
i = int(math.log(y, 10)) + 1
s = ('% 3s' % ('-' * i))[-3:] + ' '
elif x == 0:
s = ('% 3d' % (y + 1))[-3:] + ' '
else:
s = ' '
return (s, color.build('default', 'default', 'bold'))
get_lmargin = _null_margin get_lmargin = _null_margin
get_rmargin = _null_margin #get_rmargin = _null_margin
get_rmargin = _continuation_margin
# get mode name # get mode name
def name(self): def name(self):