-- text editor, particularly text drawing, horizontal wrap, vertical scrolling Text = {} require 'search' require 'select' require 'undo' require 'text_tests' -- return values: -- y coordinate drawn until in px -- position of start of final screen line drawn function Text.draw(State, line, line_index, top, left, right) --? print('text.draw', line_index) App.color(Text_color) -- wrap long lines local x = left local y = top local pos = 1 local screen_line_starting_pos = 1 if line.fragments == nil then Text.compute_fragments(line, left, right) end Text.populate_screen_line_starting_pos(line, left, right) --? print('--') for _, f in ipairs(line.fragments) do local frag, frag_text = f.data, f.text -- render fragment local frag_width = App.width(frag_text) local frag_len = utf8.len(frag) --? local s=tostring --? print('('..s(x)..','..s(y)..') '..frag..'('..s(frag_width)..' vs '..s(right)..') '..s(line_index)..' vs '..s(State.screen_top1.line)..'; '..s(pos)..' vs '..s(State.screen_top1.pos)..'; bottom: '..s(State.screen_bottom1.line)..'/'..s(State.screen_bottom1.pos)) if x + frag_width > right then assert(x > left) -- no overfull lines -- update y only after drawing the first screen line of screen top if Text.lt1(State.screen_top1, {line=line_index, pos=pos}) then y = y + State.line_height if y + State.line_height > App.screen.height then --? print('b', y, App.screen.height, '=>', screen_line_starting_pos) return y, screen_line_starting_pos end screen_line_starting_pos = pos --? print('text: new screen line', y, App.screen.height, screen_line_starting_pos) end x = left end --? print('checking to draw', pos, State.screen_top1.pos) -- don't draw text above screen top if Text.le1(State.screen_top1, {line=line_index, pos=pos}) then if State.selection1.line then local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len, left, right) Text.draw_highlight(State, line, x,y, pos, lo,hi) end --? print('drawing '..frag) App.screen.draw(frag_text, x,y) end -- render cursor if necessary if line_index == State.cursor1.line then if pos <= State.cursor1.pos and pos + frag_len > State.cursor1.pos then if State.search_term then if State.lines[State.cursor1.line].data:sub(State.cursor1.pos, State.cursor1.pos+utf8.len(State.search_term)-1) == State.search_term then local lo_px = Text.draw_highlight(line, x,y, pos, State.cursor1.pos, State.cursor1.pos+utf8.len(State.search_term)) App.color(Text_color) love.graphics.print(State.search_term, x+lo_px,y) end else Text.draw_cursor(State, x+Text.x(frag, State.cursor1.pos-pos+1), y) end end end x = x + frag_width pos = pos + frag_len end if State.search_term == nil then if line_index == State.cursor1.line and State.cursor1.pos == pos then Text.draw_cursor(State, x, y) end end return y, screen_line_starting_pos end -- manual tests: -- draw with small screen width of 100 function Text.draw_cursor(State, x, y) -- blink every 0.5s if math.floor(Cursor_time*2)%2 == 0 then App.color(Cursor_color) love.graphics.rectangle('fill', x,y, 3,State.line_height) App.color(Text_color) end State.cursor_x = x State.cursor_y = y+State.line_height end function Text.compute_fragments(line, left, right) --? print('compute_fragments', right) line.fragments = {} local x = left -- try to wrap at word boundaries for frag in line.data:gmatch('%S*%s*') do local frag_text = App.newText(love.graphics.getFont(), frag) local frag_width = App.width(frag_text) --? print('x: '..tostring(x)..'; '..tostring(right-x)..'px to go') --? print('frag: ^'..frag..'$ is '..tostring(frag_width)..'px wide') if x + frag_width > right then while x + frag_width > right do --? print(x, frag, frag_width, right) if x < 0.8*right then --? print(frag, x, frag_width, right) -- long word; chop it at some letter -- We're not going to reimplement TeX here. local bpos = Text.nearest_pos_less_than(frag, right - x) assert(bpos > 0) -- avoid infinite loop when window is too narrow local boffset = Text.offset(frag, bpos+1) -- byte _after_ bpos --? print('space for '..tostring(bpos)..' graphemes, '..tostring(boffset)..' bytes') local frag1 = string.sub(frag, 1, boffset-1) local frag1_text = App.newText(love.graphics.getFont(), frag1) local frag1_width = App.width(frag1_text) --? print(frag, x, frag1_width, right) assert(x + frag1_width <= right) --? print('inserting '..frag1..' of width '..tostring(frag1_width)..'px') table.insert(line.fragments, {data=frag1, text=frag1_text}) frag = string.sub(frag, boffset) frag_text = App.newText(love.graphics.getFont(), frag) frag_width = App.width(frag_text) end x = left -- new line end end if #frag > 0 then --? print('inserting '..frag..' of width '..tostring(frag_width)..'px') table.insert(line.fragments, {data=frag, text=frag_text}) end x = x + frag_width end end function Text.textinput(State, t) if App.mouse_down(1) then return end if App.ctrl_down() or App.alt_down() or App.cmd_down() then return end local before = snapshot(State, State.cursor1.line) --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos) Text.insert_at_cursor(State, t) if State.cursor_y >= App.screen.height - State.line_height then Text.populate_screen_line_starting_pos(State.lines[State.cursor1.line], State.margin_left, App.screen.width-State.margin_right) Text.snap_cursor_to_bottom_of_screen(State.margin_left, App.screen.width-State.margin_right) --? print('=>', State.screen_top1.line,
/* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
* See LICENSE file for license details.
*/
/* appearance */
#define BORDERPX 1
#define FONT "-*-terminus-medium-r-*-*-14-*-*-*-*-*-*-*"
#define NORMBORDERCOLOR "#333"
#define NORMBGCOLOR "#222"
#define NORMFGCOLOR "#ccc"
#define SELBORDERCOLOR "#69c"
#define SELBGCOLOR "#555"
#define SELFGCOLOR "#fff"
#define TOPBAR True /* False */
/* behavior */
#define SNAP 40 /* pixel */
#define TAGS \
const char *tags[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", NULL };
#define RULES \
static Rule rule[] = { \
/* class:instance:title regex tags regex isversatile */ \
{ "Firefox", "3", False }, \
{ "Gimp", NULL, True }, \
{ "MPlayer", NULL, True }, \
{ "Acroread", NULL, True }, \
};
/* layout(s) */
#define LAYOUTS \
static Layout layout[] = { \
/* symbol function */ \
{ "[]=", tile }, /* first entry is default */ \
{ "><>", versatile }, \
};
#define MASTER 600 /* per thousand */
#define NMASTER 1 /* clients in master area */
/* key definitions */
#define MODKEY Mod1Mask
#define KEYS \
static Key key[] = { \
/* modifier key function argument */ \
{ MODKEY|ShiftMask, XK_Return, spawn, \
{ .cmd = "exec uxterm -bg '#222' -fg '#eee' -cr '#eee' +sb -fn '"FONT"'" } }, \
{ MODKEY, XK_p, spawn, \
{ .cmd = "exe=\"$(lsx `echo $PATH | sed 's/:/ /g'` | sort -u " \
" | dmenu -fn '"FONT"' -nb '"NORMBGCOLOR"' -nf '"NORMFGCOLOR"' " \
"-sb '"SELBGCOLOR"' -sf '"SELFGCOLOR"')\" && exec $exe" } }, \
{ MODKEY, XK_j, focusnext, { 0 } }, \
{ MODKEY, XK_k, focusprev, { 0 } }, \
{ MODKEY, XK_Return, zoom, { 0 } }, \
{ MODKEY, XK_g, resizemaster, { .i = 15 } }, \
{ MODKEY, XK_s, resizemaster, { .i = -15 } }, \
{ MODKEY, XK_i, incnmaster, { .i = 1 } }, \
{ MODKEY, XK_d, incnmaster, { .i = -1 } }, \
{ MODKEY|ShiftMask, XK_0, tag, { .i = -1 } }, \
{ MODKEY|ShiftMask, XK_1, tag, { .i = 0 } }, \
{ MODKEY|ShiftMask, XK_2, tag, { .i = 1 } }, \
{ MODKEY|ShiftMask, XK_3, tag, { .i = 2 } }, \
{ MODKEY|ShiftMask, XK_4, tag, { .i = 3 } }, \
{ MODKEY|ShiftMask, XK_5, tag, { .i = 4 } }, \
{ MODKEY|ShiftMask, XK_6, tag, { .i = 5 } }, \
{ MODKEY|ShiftMask, XK_7, tag, { .i = 6 } }, \
{ MODKEY|ShiftMask, XK_8, tag, { .i = 7 } }, \
{ MODKEY|ShiftMask, XK_9, tag, { .i = 8 } }, \
{ MODKEY|ControlMask|ShiftMask, XK_1, toggletag, { .i = 0 } }, \
{ MODKEY|ControlMask|ShiftMask, XK_2, toggletag, { .i = 1 } }, \
{ MODKEY|ControlMask|ShiftMask, XK_3, toggletag, { .i = 2 } }, \
{ MODKEY|ControlMask|ShiftMask, XK_4, toggletag, { .i = 3 } }, \
{ MODKEY|ControlMask|ShiftMask, XK_5, toggletag, { .i = 4 } }, \
{ MODKEY|ControlMask|ShiftMask, XK_6, toggletag, { .i = 5 } }, \
{ MODKEY|ControlMask|ShiftMask, XK_7, toggletag, { .i = 6 } }, \
{ MODKEY|ControlMask|ShiftMask, XK_8, toggletag, { .i = 7 } }, \
{ MODKEY|ControlMask|ShiftMask, XK_9, toggletag, { .i = 8 } }, \
{ MODKEY|ShiftMask, XK_c, killclient, { 0 } }, \
{ MODKEY, XK_space, setlayout, { .i = -1 } }, \
{ MODKEY|ShiftMask, XK_space, toggleversatile,{ 0 } }, \
{ MODKEY, XK_0, view, { .i = -1 } }, \
{ MODKEY, XK_1, view, { .i = 0 } }, \
{ MODKEY, XK_2, view, { .i = 1 } }, \
{ MODKEY, XK_3, view, { .i = 2 } }, \
{ MODKEY, XK_4, view, { .i = 3 } }, \
{ MODKEY, XK_5, view, { .i = 4 } }, \
{ MODKEY, XK_6, view, { .i = 5 } }, \
{ MODKEY, XK_7, view, { .i = 6 } }, \
{ MODKEY, XK_8, view, { .i = 7 } }, \
{ MODKEY, XK_9, view, { .i = 8 } }, \
{ MODKEY|ControlMask, XK_1, toggleview, { .i = 0 } }, \
{ MODKEY|ControlMask, XK_2, toggleview, { .i = 1 } }, \
{ MODKEY|ControlMask, XK_3, toggleview, { .i = 2 } }, \
{ MODKEY|ControlMask, XK_4, toggleview, { .i = 3 } }, \
{ MODKEY|ControlMask, XK_5, toggleview, { .i = 4 } }, \
{ MODKEY|ControlMask, XK_6, toggleview, { .i = 5 } }, \
{ MODKEY|ControlMask, XK_7, toggleview, { .i = 6 } }, \
{ MODKEY|ControlMask, XK_8, toggleview, { .i = 7 } }, \
{ MODKEY|ControlMask, XK_9, toggleview, { .i = 8 } }, \
{ MODKEY|ShiftMask, XK_q, quit, { 0 } }, \
};