}
return pos;
}
+
+unsigned buf_getcol(Buf* buf, unsigned pos) {
+ return (pos - buf_bol(buf, pos));
+}
+
+unsigned buf_setcol(Buf* buf, unsigned pos, unsigned col) {
+ unsigned bol = buf_bol(buf, pos);
+ unsigned len = buf_eol(buf, pos) - bol;
+ return buf_byrune(buf, bol, (len > col ? col : len));
+}
+
unsigned buf_end(Buf* buf);
unsigned buf_byrune(Buf* buf, unsigned pos, int count);
unsigned buf_byline(Buf* buf, unsigned pos, int count);
+unsigned buf_getcol(Buf* buf, unsigned pos);
+unsigned buf_setcol(Buf* buf, unsigned pos, unsigned col);
+
/* Screen management functions
*****************************************************************************/
extern Buf Buffer;
extern unsigned CursorPos;
+extern unsigned TargetCol;
static void special_keys(Rune key);
static void control_keys(Rune key);
static void vi_keys(Rune key);
+static void cursor_up(void) {
+ CursorPos = buf_byline(&Buffer, CursorPos, -1);
+ CursorPos = buf_setcol(&Buffer, CursorPos, TargetCol);
+}
+
+static void cursor_dn(void) {
+ CursorPos = buf_byline(&Buffer, CursorPos, 1);
+ CursorPos = buf_setcol(&Buffer, CursorPos, TargetCol);
+}
+
+static void cursor_left(void) {
+ CursorPos = buf_byrune(&Buffer, CursorPos, -1);
+ TargetCol = buf_getcol(&Buffer, CursorPos);
+}
+
+static void cursor_right(void) {
+ CursorPos = buf_byrune(&Buffer, CursorPos, 1);
+ TargetCol = buf_getcol(&Buffer, CursorPos);
+}
+
void handle_key(Rune key) {
/* ignore invalid keys */
if (key == RUNE_ERR) return;
static void special_keys(Rune key) {
switch (key) {
- case KEY_F6: ColorBase = !ColorBase; break;
- case KEY_UP: CursorPos = buf_byline(&Buffer, CursorPos, -1); break;
- case KEY_DOWN: CursorPos = buf_byline(&Buffer, CursorPos, 1); break;
- case KEY_LEFT: CursorPos = buf_byrune(&Buffer, CursorPos, -1); break;
- case KEY_RIGHT: CursorPos = buf_byrune(&Buffer, CursorPos, 1); break;
- case KEY_INSERT: Buffer.insert_mode = !Buffer.insert_mode; break;
- case KEY_F1: Buffer.insert_mode = !Buffer.insert_mode; break;
- case KEY_DELETE: buf_del(&Buffer, CursorPos); break;
- case KEY_HOME: CursorPos = buf_bol(&Buffer, CursorPos); break;
- case KEY_END: CursorPos = buf_eol(&Buffer, CursorPos); break;
+ case KEY_F6: ColorBase = !ColorBase; break;
+ case KEY_UP: cursor_up(); break;
+ case KEY_DOWN: cursor_dn(); break;
+ case KEY_LEFT: cursor_left(); break;
+ case KEY_RIGHT: cursor_right(); break;
+ case KEY_INSERT: Buffer.insert_mode = !Buffer.insert_mode; break;
+ case KEY_F1: Buffer.insert_mode = !Buffer.insert_mode; break;
+ case KEY_DELETE: buf_del(&Buffer, CursorPos); break;
+ case KEY_HOME: CursorPos = buf_bol(&Buffer, CursorPos); break;
+ case KEY_END: CursorPos = buf_eol(&Buffer, CursorPos); break;
}
}
static void vi_keys(Rune key) {
(void)key;
}
+
+/*****************************************************************************/
extern Buf Buffer;
extern unsigned CursorPos;
+extern unsigned TargetCol;
#ifndef __MACH__
#include <time.h>
void move_cursor(MouseEvent* mevnt) {
CursorPos = screen_getoff(&Buffer, CursorPos, mevnt->y, mevnt->x);
+ TargetCol = buf_getcol(&Buffer, CursorPos);
}
void select_word(MouseEvent* mevnt) {
Buf Buffer;
unsigned CursorPos;
+unsigned TargetCol;
void die(char* m) {
(void)m;
Buf Buffer;
unsigned CursorPos = 0;
+unsigned TargetCol = 0;
enum ColorScheme ColorBase = DEFAULT_COLORSCHEME;
struct {
Display* display;
XftColor bkgclr = xftcolor(CLR_BASE03);
XftColor gtrclr = xftcolor(CLR_BASE02);
XftColor csrclr = xftcolor(CLR_BASE3);
- XftColor staclr = xftcolor(CLR_BASE2);
XftColor txtclr = xftcolor(CLR_BASE0);
/* draw the background colors */
XftDrawRect(X.xft, &bkgclr, 0, 0, X.width, X.height);
- XftDrawRect(X.xft, >rclr, 0, 0, X.width, fheight + X.font->descent);
XftDrawRect(X.xft, >rclr, 79 * fwidth, 0, fwidth, X.height);
+ XftDrawRect(X.xft, &txtclr, 0, 0, X.width, fheight + X.font->descent);
/* update the screen buffer and retrieve cursor coordinates */
unsigned csrx, csry;
/* flush the screen buffer */
unsigned nrows, ncols;
screen_getsize(&nrows, &ncols);
- screen_status("[%s]%s%s",
+ screen_status(" %s %s%s",
(Buffer.charset == BINARY ? "BIN" : "UTF-8"),
(Buffer.modified ? " * " : " "),
Buffer.path);
for (unsigned y = 0; y < nrows; y++) {
Row* row = screen_getrow(y);
- XftDrawString32(X.xft, (y==0 ? &staclr : &txtclr), X.font, 0, (y+1) * fheight, (FcChar32*)(row->cols), (row->len));
+ XftDrawString32(X.xft, (y==0 ? &bkgclr : &txtclr), X.font, 0, (y+1) * fheight, (FcChar32*)(row->cols), (row->len));
}
/* Place cursor on screen */