b->x = 0;
b->y = 0;
+ b->point = 0;
return b;
}
return b;
}
-void prev_char(struct te_buffer *buf)
+/* Compute the length of a string between two '\n' */
+
+int line_length(bstring b, int point)
+{
+ if (b == NULL)
+ return -1;
+
+ int c_offset = bstrchrp(b, '\n', point);
+ if (c_offset == BSTR_ERR)
+ c_offset = 0;
+
+ int p_offset = bstrrchrp(b, '\n', c_offset - 1);
+ if (p_offset == BSTR_ERR)
+ p_offset = 0;
+
+ return c_offset - p_offset;
+}
+
+/*
+ The length of a line as displayed on the screen.
+ */
+int screen_line_length(bstring b, int point)
+{
+ if (b == NULL)
+ return -1;
+
+ int len = line_length(b, point);
+ int i = 0;
+ int count = 0;
+
+ for (i = 0; i < len; i++) {
+ if (bchar(b, point - i) == '\t')
+ count += 8;
+ else
+ count += 1;
+ }
+
+ return count;
+}
+
+int prev_char(struct te_buffer *buf)
{
if (buf == NULL)
return;
if (buf->point > 0)
buf->point--;
+ else
+ return ERR;
+
+ return OK;
}
-void next_char(struct te_buffer *buf)
+int next_char(struct te_buffer *buf)
{
if (buf == NULL)
return;
if (buf->point < blength(buf->contents))
buf->point++;
+ else
+ return ERR;
+ return OK;
}
#include <stdio.h>
#include "bstrlib.h"
#include "queue.h"
-
+#include "config.h"
struct te_buffer {
char *name;
struct te_buffer* load_buffer(char *filename);
void init_buffers(void);
-void prev_char(struct te_buffer *buf);
-void next_char(struct te_buffer *buf);
+int line_length(bstring b, int point);
+int screen_line_length(bstring b, int point);
+int prev_char(struct te_buffer *buf);
+int next_char(struct te_buffer *buf);
--- /dev/null
+#ifndef __CONFIG_H__
+#define __CONFIG_H__
+
+#define TAB_LEN 8
+
+#endif
/*
te, a small text editor for unix.
- */
+*/
#include <stdio.h>
#include <curses.h>
return;
if (buf->x > 0)
- buf->x--;
+ if (bchar(buf->contents, buf->point) == '\t')
+ buf->x -= TAB_LEN;
+ else
+ buf->x--;
+
else if (buf->y > 0) {
buf->y--;
- /* Move the cursor to the last character of the previous string */
- int c_offset = bstrrchrp(buf->contents, '\n', buf->point);
- int p_offset = bstrrchrp(buf->contents, '\n', c_offset);
-
- /* We compute the difference between the two numbers to get the row number to move the cursor to */
- buf->x = c_offset - p_offset;
+ buf->x = screen_line_length(buf->contents, buf->point);
}
prev_char(buf);
- wmove(buffer_win, buf->y, buf->x);
+ move(buf->y, buf->x);
}
void move_right(struct te_buffer *buf)
if (buf == NULL)
return;
- if (buf->x < COLS)
- buf->x++;
- else if (buf->y < LINES) {
+ if (next_char(buf) == ERR) /* last char of the document */
+ return;
+
+ if (buf->x < screen_line_length(buf->contents, buf->point)) {
+ /* tab is the only character larger than 1 */
+ if (bchar(buf->contents, buf->point) == '\t')
+ buf->x += TAB_LEN;
+ else
+ buf->x++;
+ }
+ else if (buf->y < LINES - 3) {
buf->y++;
buf->x = 0;
}
- next_char(buf);
- wmove(buffer_win, buf->y, buf->x);
+ move(buf->y, buf->x);
+ refresh();
+ miniprintf("%c, y: %d, x%d", bchar(buf->contents, buf->point), buf->y, buf->x);
}
void move_up(struct te_buffer *buf)
#include <curses.h>
#include "buffer.h"
+#include "config.h"
+
WINDOW *buffer_win, *status_win, *minibuffer_win;