Skip to content

Commit

Permalink
Use less global variables
Browse files Browse the repository at this point in the history
  • Loading branch information
tihirvon committed Apr 16, 2013
1 parent 7751a21 commit 9c60224
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 74 deletions.
75 changes: 25 additions & 50 deletions buffer.c
Expand Up @@ -3,11 +3,9 @@
#include "editor.h"
#include "change.h"
#include "block.h"
#include "move.h"
#include "filetype.h"
#include "state.h"
#include "syntax.h"
#include "file-history.h"
#include "file-option.h"
#include "lock.h"
#include "selection.h"
Expand Down Expand Up @@ -239,25 +237,25 @@ struct buffer *find_buffer_by_id(unsigned int id)
return NULL;
}

bool guess_filetype(void)
bool buffer_detect_filetype(struct buffer *b)
{
char *interpreter = detect_interpreter(buffer);
char *interpreter = detect_interpreter(b);
const char *ft = NULL;

if (BLOCK(buffer->blocks.next)->size) {
BLOCK_ITER(bi, &buffer->blocks);
if (BLOCK(b->blocks.next)->size) {
BLOCK_ITER(bi, &b->blocks);
struct lineref lr;

fill_line_ref(&bi, &lr);
ft = find_ft(buffer->abs_filename, interpreter, lr.line, lr.size);
} else if (buffer->abs_filename) {
ft = find_ft(buffer->abs_filename, interpreter, NULL, 0);
ft = find_ft(b->abs_filename, interpreter, lr.line, lr.size);
} else if (b->abs_filename) {
ft = find_ft(b->abs_filename, interpreter, NULL, 0);
}
free(interpreter);

if (ft && !streq(ft, buffer->options.filetype)) {
free(buffer->options.filetype);
buffer->options.filetype = xstrdup(ft);
if (ft && !streq(ft, b->options.filetype)) {
free(b->options.filetype);
b->options.filetype = xstrdup(ft);
return true;
}
return false;
Expand All @@ -280,34 +278,23 @@ void update_short_filename(struct buffer *b)
set_display_filename(b, short_filename(b->abs_filename));
}

void filetype_changed(void)
{
set_file_options(buffer);
syntax_changed();
}

void syntax_changed(void)
void buffer_update_syntax(struct buffer *b)
{
struct syntax *syn = NULL;

if (!buffer) {
// syntax option was changed in config file
return;
}

if (buffer->options.syntax) {
if (b->options.syntax) {
/* even "none" can have syntax */
syn = find_syntax(buffer->options.filetype);
syn = find_syntax(b->options.filetype);
if (!syn)
syn = load_syntax_by_filetype(buffer->options.filetype);
syn = load_syntax_by_filetype(b->options.filetype);
}
if (syn == buffer->syn)
if (syn == b->syn)
return;

buffer->syn = syn;
b->syn = syn;
if (syn) {
// start state of first line is constant
struct ptr_array *s = &buffer->line_start_states;
struct ptr_array *s = &b->line_start_states;
if (!s->alloc) {
s->alloc = 64;
s->ptrs = xnew(void *, s->alloc);
Expand All @@ -316,28 +303,16 @@ void syntax_changed(void)
s->count = 1;
}

mark_all_lines_changed(buffer);
}

static void restore_cursor_from_history(void)
{
int row, col;

if (!find_file_in_history(buffer->abs_filename, &row, &col))
return;

move_to_line(row);
move_to_column(col);
mark_all_lines_changed(b);
}

void setup_buffer(void)
void buffer_setup(struct buffer *b)
{
buffer->setup = true;
guess_filetype();
filetype_changed();
if (buffer->options.detect_indent && buffer->abs_filename) {
detect_indent(buffer);
b->setup = true;
buffer_detect_filetype(b);
set_file_options(b);
buffer_update_syntax(b);
if (b->options.detect_indent && b->abs_filename != NULL) {
detect_indent(b);
}
if (buffer->options.file_history && buffer->abs_filename)
restore_cursor_from_history();
}
8 changes: 3 additions & 5 deletions buffer.h
Expand Up @@ -99,15 +99,13 @@ struct buffer *find_buffer(const char *abs_filename);
struct buffer *find_buffer_by_id(unsigned int id);
struct buffer *buffer_new(const char *encoding);
struct buffer *open_empty_buffer(void);
void setup_buffer(void);
void free_buffer(struct buffer *b);
bool buffer_detect_filetype(struct buffer *b);
void buffer_update_syntax(struct buffer *b);
void buffer_setup(struct buffer *b);

long buffer_get_char(struct block_iter *bi, unsigned int *up);
long buffer_next_char(struct block_iter *bi, unsigned int *up);
long buffer_prev_char(struct block_iter *bi, unsigned int *up);

bool guess_filetype(void);
void syntax_changed(void);
void filetype_changed(void);

#endif
8 changes: 5 additions & 3 deletions commands.c
Expand Up @@ -416,7 +416,7 @@ static void cmd_line(const char *pf, char **args)

line = atoi(args[0]);
if (line > 0) {
move_to_line(line);
move_to_line(view, line);
move_to_preferred_x(x);
}
}
Expand Down Expand Up @@ -897,8 +897,10 @@ static void cmd_save(const char *pf, char **args)
}
if (!old_mode && streq(buffer->options.filetype, "none")) {
/* new file and most likely user has not changed the filetype */
if (guess_filetype())
filetype_changed();
if (buffer_detect_filetype(buffer)) {
set_file_options(buffer);
buffer_update_syntax(buffer);
}
}
return;
error:
Expand Down
8 changes: 4 additions & 4 deletions file-location.c
Expand Up @@ -64,9 +64,9 @@ bool file_location_go(struct file_location *loc)
search_tag(loc->pattern, &err);
ok = !err;
} else if (loc->line > 0) {
move_to_line(loc->line);
move_to_line(v, loc->line);
if (loc->column > 0) {
move_to_column(loc->column);
move_to_column(v, loc->column);
}
}
return ok;
Expand All @@ -92,8 +92,8 @@ bool file_location_return(struct file_location *loc)
return true;
}
set_view(v);
move_to_line(loc->line);
move_to_column(loc->column);
move_to_line(v, loc->line);
move_to_column(v, loc->column);
return true;
}

Expand Down
16 changes: 8 additions & 8 deletions move.c
Expand Up @@ -148,26 +148,26 @@ void move_eof(void)
view_reset_preferred_x(view);
}

void move_to_line(int line)
void move_to_line(struct view *v, int line)
{
block_iter_goto_line(&view->cursor, line - 1);
view->center_on_scroll = true;
block_iter_goto_line(&v->cursor, line - 1);
v->center_on_scroll = true;
}

void move_to_column(int column)
void move_to_column(struct view *v, int column)
{
block_iter_bol(&view->cursor);
block_iter_bol(&v->cursor);
while (column-- > 1) {
unsigned int u;

if (!buffer_next_char(&view->cursor, &u))
if (!buffer_next_char(&v->cursor, &u))
break;
if (u == '\n') {
buffer_prev_char(&view->cursor, &u);
buffer_prev_char(&v->cursor, &u);
break;
}
}
view_reset_preferred_x(view);
view_reset_preferred_x(v);
}

static enum char_type get_char_type(unsigned int u)
Expand Down
5 changes: 3 additions & 2 deletions move.h
Expand Up @@ -4,6 +4,7 @@
#include "libc.h"

struct block_iter;
struct view;

void move_to_preferred_x(int preferred_x);
void move_cursor_left(void);
Expand All @@ -14,8 +15,8 @@ void move_up(int count);
void move_down(int count);
void move_bof(void);
void move_eof(void);
void move_to_line(int line);
void move_to_column(int column);
void move_to_line(struct view *v, int line);
void move_to_column(struct view *v, int column);

long word_fwd(struct block_iter *bi, bool skip_non_word);
long word_bwd(struct block_iter *bi, bool skip_non_word);
Expand Down
14 changes: 14 additions & 0 deletions options.c
@@ -1,6 +1,7 @@
#include "options.h"
#include "buffer.h"
#include "completion.h"
#include "file-option.h"
#include "filetype.h"
#include "common.h"
#include "regexp.h"
Expand Down Expand Up @@ -142,6 +143,19 @@ struct option_ops {
#define G(member) OLG(offsetof(struct global_options, member), false, true)
#define C(member) OLG(offsetof(struct common_options, member), true, true)

static void filetype_changed(void)
{
set_file_options(buffer);
buffer_update_syntax(buffer);
}

static void syntax_changed(void)
{
if (buffer != NULL) {
buffer_update_syntax(buffer);
}
}

static bool validate_statusline_format(const char *value)
{
static const char chars[] = "fmryxXpEMnstu%";
Expand Down
19 changes: 17 additions & 2 deletions window.c
Expand Up @@ -5,6 +5,7 @@
#include "lock.h"
#include "load-save.h"
#include "error.h"
#include "move.h"

PTR_ARRAY(windows);
struct window *window;
Expand Down Expand Up @@ -204,6 +205,16 @@ void close_current_view(void)
set_view(window->views.ptrs[idx]);
}

static void restore_cursor_from_history(struct view *v)
{
int row, col;

if (find_file_in_history(v->buffer->abs_filename, &row, &col)) {
move_to_line(v, row);
move_to_column(v, col);
}
}

void set_view(struct view *v)
{
int i;
Expand All @@ -220,8 +231,12 @@ void set_view(struct view *v)

window->view = v;

if (!buffer->setup)
setup_buffer();
if (!v->buffer->setup) {
buffer_setup(v->buffer);
if (v->buffer->options.file_history && v->buffer->abs_filename != NULL) {
restore_cursor_from_history(v);
}
}

// view.cursor can be invalid if same buffer was modified from another view
if (view->restore_cursor) {
Expand Down

0 comments on commit 9c60224

Please sign in to comment.