+te, Copyright (c) 2008-2009 Karim Hamidou
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ Neither the name of the author nor the names of its contributors may be used
+ to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
Portions Copyright (c) 2002-2008 Paul Hsieh
All rights reserved.
#include "zalloc.h"
#include "error.h"
#include "buffer.h"
+#include "util.h"
void init_buffers(void)
{
if (b == NULL)
return -1;
- int c_offset = bstrchrp(b, '\n', point);
- if (c_offset == BSTR_ERR)
- c_offset = blength(b);
+ int s_offset = bstrrchrp(b, '\n', point - 1);
+ if (s_offset == BSTR_ERR)
+ s_offset = 0;
- int p_offset = bstrrchrp(b, '\n', c_offset - 1);
- if (p_offset == BSTR_ERR)
- p_offset = 0;
+ int e_offset = bstrchrp(b, '\n', point);
+ if (e_offset == BSTR_ERR)
+ e_offset = blength(b);
- return c_offset - p_offset;
+
+ return e_offset - s_offset;
+}
+
+bstring current_line_as_bstring(bstring b, int point)
+{
+ int s_offset = bstrrchrp(b, '\n', point - 1);
+ if (s_offset == BSTR_ERR)
+ s_offset = 0;
+
+ int e_offset = bstrchrp(b, '\n', point);
+ if (e_offset == BSTR_ERR)
+ e_offset = blength(b);
+
+ return bmidstr(b, s_offset, e_offset - s_offset);
}
/*
accessor and mutators for the contents of the buffer.
*/
+
int prev_char(struct te_buffer *buf)
{
if (buf == NULL)
if (buf == NULL)
return;
- if (buf->point > 0)
+ if (buf->point > 0) {
buf->point--;
+ if (bchar(buf->contents, buf->point) == '\n')
+ decrement_or_nullify(buf->lineno);
+ }
else
return ERR;
if (buf == NULL)
return;
- if (buf->point < blength(buf->contents))
+ if (buf->point < blength(buf->contents)) {
buf->point++;
+ if (bchar(buf->contents, buf->point) == '\n')
+ buf->lineno++;
+ }
else
return ERR;
return OK;
}
+void move_to_prev_line(struct te_buffer *buf)
+{
+ if (buf == NULL)
+ return;
+
+ while(curr_char(buf) != '\n')
+ move_left(buf);
+
+}
+
+void move_to_next_line(struct te_buffer *buf)
+{
+ if (buf == NULL)
+ return;
+
+ while(curr_char(buf) != '\n')
+ move_right(buf);
+
+ move_right(buf);
+}
+
void insert_char(struct te_buffer *buf, char c)
{
if (buf == NULL)
char *name;
int dirty;
int x, y;
+ int lineno; /* the number of '\n' seen so far */
bstring contents;
int point; /* offset in "contents" */
struct te_char *scr_top, *scr_bottom; /* the first and last char of the screen */
void init_buffers(void);
int line_length(bstring b, int point);
int screen_line_length(bstring b, int point);
+bstring current_line_as_bstring(bstring b, int point);
int prev_char(struct te_buffer *buf);
int curr_char(struct te_buffer *buf);
int next_char(struct te_buffer *buf);
int move_right(struct te_buffer *buf);
int move_left(struct te_buffer *buf);
-
+void insert_char(struct te_buffer *buf, char c);
#endif
case KEY_DOWN:
screen_move_down(current_buf);
break;
+
+ case 'v':
+ screen_insert_char(current_buf, 'X');
+ paint_buffer(current_buf);
+ break;
}
-
+
refresh();
miniprintf("%c, line length: %d, y: %d, x%d",
init_buffers();
init_windows();
- current_buf = load_buffer("README");
+ current_buf = load_buffer("main.c");
paint_buffer(current_buf);
refresh();
input_loop();
#include "screen.h"
+static int y, x;
+
+
void init_windows(void)
{
initscr();
wmove(buffer_win, 0, 0);
- char c;
+ bstring s;
int i = 0;
+ int pos = 0;
- for(i = 0; i < (LINES - 3) * COLS; i++)
- waddch(buffer_win, bchar(buf->contents, i));
+ for(i = 0; i < (LINES - 3); i++) {
+ s = current_line_as_bstring(buf->contents, pos);
+ draw_line(s, i);
+ pos += line_length(s, pos);
+ }
wmove(buffer_win, 0, 0);
refresh();
}
+void draw_line(bstring s, int y)
+{
+ int i;
+ int j;
+ /* FIXME : wrap line if we go beyond COLS */
+ for (i = 0; i < blength(s); i++)
+ if (bchar(s, i) == '\t')
+ for (j = 0; j < TAB_LEN; j++)
+ mvwaddch(buffer_win, y, i, ' ');
+ else
+ mvwaddch(buffer_win, y, i, bchar(s, i));
+}
void scroll_up(struct te_buffer *buf)
{
} while(curr_char(buf) != '\n');
screen_next_line(buf);
-
+
+ /* mimic emacs' behaviour of moving the user to the exact offset we were on */
for(count = 0; count < line_len && curr_char(buf) != '\n'; count++) {
move_right(buf);
screen_move_right(buf);
return;
}
+void screen_insert_char(struct te_buffer *buf, char c)
+{
+ if (buf == NULL)
+ return;
+
+ statusprintf("buf x : %d, y : %d", buf->x, buf->y);
+ insert_char(buf, c);
+
+ bstring s = current_line_as_bstring(buf->contents, buf->point);
+ draw_line(s, buf->y);
+
+
+}
+
+/*
+ display a message in the status bar.
+ */
void statusprintf(char *fmt, ...)
{
+
+ getyx(stdscr, y, x);
+
werase(status_win);
wmove(status_win, 0, 0);
}
wrefresh(status_win);
+
+ move(y, x);
}
void miniprintf(char *fmt, ...)
{
+ getyx(stdscr, y, x);
+
va_list ap;
va_start(ap, fmt);
werase(minibuffer_win);
va_end(ap);
wrefresh(minibuffer_win);
+
+ move(y, x);
}
void console_signal_handler(int sig)
}
}
+
void cleanup_windows(void);
void paint_buffer(struct te_buffer *buf);
+void draw_line(bstring s, int y);
void scroll_up(struct te_buffer *buf);
void scroll_down(struct te_buffer *buf);
#ifndef __TE_UTIL_H__
#define __TE_UTIL_H__
-#define decrement_or_nullify(x) (x > 0) ? x-- : x = 0;
+#define decrement_or_nullify(x) (x = (x > 0) ? x - 1 : 0)
+#define increment_or_maximize(x, max) (x = (x > max) ? x + 1 : max)
#endif