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

View File

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

View File

@ -1,4 +1,4 @@
import os, sets, string
import math, os, sets, string
import color, method
from lex import Lexer
from point import Point
@ -90,8 +90,8 @@ class Fundamental(Handler):
# margin/line numbering
show_line_numbers = False
lmargin = 0
rmargin = 0
lmargin = 0
rmargin = 1
def install(cls, app):
app.setmode(cls.modename.lower(), cls, paths=cls.paths,
@ -222,14 +222,27 @@ class Fundamental(Handler):
self.lmargin = 0
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'))
def _null_margin(self, i):
def _null_margin(self, y, x, ended=False, cont=False):
return ('', color.build('default', 'default'))
def _line_number_margin(self, i):
return ('% 3d ' % i, color.build('default', 'default', 'bold'))
def _continuation_margin(self, y, x, ended=False, cont=False):
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_rmargin = _null_margin
#get_rmargin = _null_margin
get_rmargin = _continuation_margin
# get mode name
def name(self):