color-data-buffer exists

--HG--
branch : pmacs2
This commit is contained in:
moculus 2008-03-28 13:44:39 +00:00
parent 840814eded
commit 369511000e
5 changed files with 115 additions and 33 deletions

View File

@ -113,7 +113,7 @@ class Application(object):
'dir', 'elisp', 'hex', 'html', 'java', 'javascript', 'lisp', 'dir', 'elisp', 'hex', 'html', 'java', 'javascript', 'lisp',
'make', 'mini', 'mutt', 'nasm', 'ocaml', 'perl', 'python', 'make', 'mini', 'mutt', 'nasm', 'ocaml', 'perl', 'python',
'replace', 'rst', 'scheme', 'search', 'sh', 'sql', 'tt', 'replace', 'rst', 'scheme', 'search', 'sh', 'sql', 'tt',
'text', 'text2', 'which', 'xml', 'cheetah') 'text', 'text2', 'which', 'xml', 'cheetah', 'colortext')
for name in names: for name in names:
exec("import mode.%s; mode.%s.install(self)" % (name, name)) exec("import mode.%s; mode.%s.install(self)" % (name, name))

128
buffer.py
View File

@ -1,5 +1,5 @@
import datetime, grp, md5, os, pwd, re, sets, shutil, stat, string import datetime, grp, md5, os, pwd, re, sets, shutil, stat, string
import aes, dirutil, regex, highlight import aes, dirutil, regex, highlight, lex
from point import Point from point import Point
# undo/redo stack constants # undo/redo stack constants
@ -8,6 +8,19 @@ ACT_UNDO = 1
ACT_REDO = 2 ACT_REDO = 2
STACK_LIMIT = 1024 STACK_LIMIT = 1024
# used for multiple text additions/deletions
class GroupMove(object):
def __init__(self, buffer, p, moves):
self.buffer = buffer
self.lines = lines
self.moves = moves
def restore(self, act=ACT_UNDO):
assert act == ACT_UNDO or act == ACT_REDO
for move in self.moves:
move.restore(act)
def getpos(self):
return self.moves[-1].getpos()
# used for undo/redo stacks when text will need to be added back # used for undo/redo stacks when text will need to be added back
class AddMove(object): class AddMove(object):
def __init__(self, buffer, p, lines): def __init__(self, buffer, p, lines):
@ -110,8 +123,7 @@ class Buffer(object):
if modename not in self.highlights and w.mode.lexer is not None: if modename not in self.highlights and w.mode.lexer is not None:
self.highlights[modename] = highlight.Highlighter(w.mode.lexer) self.highlights[modename] = highlight.Highlighter(w.mode.lexer)
self.highlights[modename].highlight(self.lines) self.highlights[modename].highlight(self.lines)
#if modename not in self.tabbing and w.mode.tabber is not None:
# self.tabbing[modename] =
def remove_window(self, w): def remove_window(self, w):
if w in self.windows: if w in self.windows:
self.windows.remove(w) self.windows.remove(w)
@ -339,7 +351,7 @@ class DataBuffer(Buffer):
# console is another singleton # console is another singleton
console = None console = None
class ConsoleBuffer(Buffer): class ConsoleBuffer(Buffer):
btype = 'console' btype = 'console'
modename = 'console' modename = 'console'
def __new__(cls, *args, **kwargs): def __new__(cls, *args, **kwargs):
global console global console
@ -615,27 +627,89 @@ class PathListBuffer(DirBuffer):
def name(self): def name(self):
return self._name return self._name
ABOUT_DATA = ''' # NOTE: this highlighter will not reprocess the data given. it is intended to
================================================================================ # be used with read-only buffers like DataBuffer and ColorBuffer
class ColorHighlighter(highlight.Highlighter):
************ ********** ****** ****** **** ******** ********* color_re = re.compile(r'\[([a-zA-Z0-9\*:]+)\]')
************** ****************** ************* ********** *********** color_map = {
******* ***** ****** ***** **** **** ****** **** **** ***** **** 'B': 'black',
*** *** *** *** *** **** **** **** ******* 'r': 'red',
*** *** *** *** *** **** **** **** ******* 'g': 'green',
***** ***** ***** ***** **** **** ****** **** **** **** ***** 'y': 'yellow',
************ ***** ***** **** ************* ********** *********** 'b': 'blue',
********** ***** ***** **** ****** **** ******** ********* 'm': 'magenta',
*** 'c': 'cyan',
*** pmacs is a python-based text editor by Erik Osheim, (c) 2005-2008 'w': 'white',
***** pmacs is available to you under the GNU General Public License v2 'd': 'default',
***** '*': 'bold',
***** to see all available commands use: C-c M-h (show-bindings-buffer) }
================================================================================
'''
class AboutBuffer(DataBuffer):
btype = 'about'
modename = 'about'
def __init__(self): def __init__(self):
DataBuffer.__init__(self, '*About*', ABOUT_DATA) self.tokens = []
def append_token(self, y, x, s, color):
s = s.replace('\\[', '[')
s = s.replace('\\]', ']')
s = s.replace('\\\\', '\\')
t = lex.Token('color_data', None, y, x, s, color)
self.tokens[y].append(t)
def highlight(self, lines):
if self.tokens:
return
self.tokens = [None] * len(lines)
for y in range(0, len(lines)):
self.tokens[y] = []
line = lines[y]
c = ['default', 'default']
i = 0
offset = 0
while i < len(line):
m = self.color_re.search(line, i)
if m:
(j, k) = (m.start(), m.end())
if j > i:
self.append_token(y, i - offset, line[i:j], c)
fields = m.group(1).split(':')
c = [self.color_map.get(x, x) for x in fields]
offset += k - j
i = k
else:
self.append_token(y, i - offset, line[i:], c)
break
class ColorDataBuffer(DataBuffer):
btype = 'colordata'
modename = 'colortext'
color_re = re.compile(r'\[([a-z:]+)\]')
def __init__(self, name, data, nl='\n'):
data2 = ColorHighlighter.color_re.sub('', data)
data2 = data2.replace('\\[', '[')
data2 = data2.replace('\\]', ']')
data2 = data2.replace('\\\\', '\\')
DataBuffer.__init__(self, name, data2, nl)
lines = data.split(self.nl)
self.highlights = {
'Colortext': ColorHighlighter(),
}
self.highlights['Colortext'].highlight(lines)
ABOUT_DATA = '''
[r:d:*]================================================================================
[y:d:*]************ ********** ****** ****** **** ******** *********
[y:d:*]************** ****************** ************* ********** ***********
[y:d:*]******* ***** ****** ***** **** **** ****** **** **** ***** ****
[y:d:*] *** *** *** *** *** **** **** **** *******
[y:d:*] *** *** *** *** *** **** **** **** *******
[y:d:*] ***** ***** ***** ***** **** **** ****** **** **** **** *****
[y:d:*] ************ ***** ***** **** ************* ********** ***********
[y:d:*] ********** ***** ***** **** ****** **** ******** *********
[y:d:*] ***
[y:d:*] *** [c:d:*]pmacs[d:d:*] is a python-based text editor by [c:d:*]Erik Osheim[d:d:*], [b:d:*](c) 2005-2008
[y:d:*]***** [c:d:*]pmacs[d:d:*] is available to you under the [c:d:*]GNU General Public License v2
[y:d:*]*****
[y:d:*]***** [d:d:*]to see all available commands use: [c:d:*]C-c M-h [b:d:*](show-bindings-buffer)
[r:d:*]================================================================================
'''
class AboutBuffer(ColorDataBuffer):
def __init__(self):
ColorDataBuffer.__init__(self, '*About*', ABOUT_DATA)

View File

@ -33,8 +33,8 @@ def token_vmatch2(self, token, *pairs):
class Highlighter(object): class Highlighter(object):
def __init__(self, lexer): def __init__(self, lexer):
self.lexer = lexer self.lexer = lexer
self.tokens = [] self.tokens = []
def dump(self, fmt='(%3s, %2s) | %s'): def dump(self, fmt='(%3s, %2s) | %s'):
print fmt % ('y', 'x', 'string') print fmt % ('y', 'x', 'string')

View File

@ -197,9 +197,11 @@ class Fundamental(Handler):
# lexing for highlighting, etc. # lexing for highlighting, etc.
if self.grammar: if self.grammar:
self.lexer = Lexer(self, self.grammar) self.lexer = Lexer(self, self.grammar)
self.gstack = {} else:
self.ghist = {} self.lexer = None
self.gstack = {}
self.ghist = {}
# tab handling # tab handling
if self.tabbercls: if self.tabbercls:

6
mode/colortext.py Normal file
View File

@ -0,0 +1,6 @@
import mode
class Colortext(mode.Fundamental):
modename = 'Colortext'
install = Colortext.install