[bim] Options cleanup, add -c/-C to cat files with syntax
This commit is contained in:
parent
5774273c14
commit
55feb94517
161
apps/bim.c
161
apps/bim.c
@ -1916,6 +1916,19 @@ int num_width(void) {
|
|||||||
return w;
|
return w;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draw the gutter and line numbers.
|
||||||
|
*/
|
||||||
|
void draw_line_number(int x) {
|
||||||
|
/* Draw the line number */
|
||||||
|
set_colors(COLOR_NUMBER_FG, COLOR_NUMBER_BG);
|
||||||
|
int num_size = num_width();
|
||||||
|
for (int y = 0; y < num_size - log_base_10(x + 1); ++y) {
|
||||||
|
printf(" ");
|
||||||
|
}
|
||||||
|
printf("%d%c", x + 1, (x+1 == env->line_no && env->coffset > 0) ? '<' : ' ');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Redraw line.
|
* Redraw line.
|
||||||
*
|
*
|
||||||
@ -1938,20 +1951,14 @@ void redraw_line(int j, int x) {
|
|||||||
set_colors(COLOR_NUMBER_FG, COLOR_ALT_FG);
|
set_colors(COLOR_NUMBER_FG, COLOR_ALT_FG);
|
||||||
printf(" ");
|
printf(" ");
|
||||||
|
|
||||||
/* Draw the line number */
|
draw_line_number(x);
|
||||||
set_colors(COLOR_NUMBER_FG, COLOR_NUMBER_BG);
|
|
||||||
int num_size = num_width();
|
|
||||||
for (int y = 0; y < num_size - log_base_10(x + 1); ++y) {
|
|
||||||
printf(" ");
|
|
||||||
}
|
|
||||||
printf("%d%c", x + 1, (x+1 == env->line_no && env->coffset > 0) ? '<' : ' ');
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Draw the line text
|
* Draw the line text
|
||||||
* If this is the active line, the current character cell offset should be used.
|
* If this is the active line, the current character cell offset should be used.
|
||||||
* (Non-active lines are not shifted and always render from the start of the line)
|
* (Non-active lines are not shifted and always render from the start of the line)
|
||||||
*/
|
*/
|
||||||
render_line(env->lines[x], global_config.term_width - 3 - num_size, (x + 1 == env->line_no) ? env->coffset : 0);
|
render_line(env->lines[x], global_config.term_width - 3 - num_width(), (x + 1 == env->line_no) ? env->coffset : 0);
|
||||||
|
|
||||||
/* Clear the rest of the line */
|
/* Clear the rest of the line */
|
||||||
clear_to_end();
|
clear_to_end();
|
||||||
@ -2287,14 +2294,21 @@ void place_cursor_actual(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle terminal size changes
|
* Update screen size
|
||||||
*/
|
*/
|
||||||
void SIGWINCH_handler(int sig) {
|
void update_screen_size(void) {
|
||||||
(void)sig;
|
|
||||||
struct winsize w;
|
struct winsize w;
|
||||||
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
||||||
global_config.term_width = w.ws_col;
|
global_config.term_width = w.ws_col;
|
||||||
global_config.term_height = w.ws_row;
|
global_config.term_height = w.ws_row;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle terminal size changes
|
||||||
|
*/
|
||||||
|
void SIGWINCH_handler(int sig) {
|
||||||
|
(void)sig;
|
||||||
|
update_screen_size();
|
||||||
redraw_all();
|
redraw_all();
|
||||||
|
|
||||||
signal(SIGWINCH, SIGWINCH_handler);
|
signal(SIGWINCH, SIGWINCH_handler);
|
||||||
@ -2326,31 +2340,6 @@ void SIGCONT_handler(int sig) {
|
|||||||
signal(SIGTSTP, SIGTSTP_handler);
|
signal(SIGTSTP, SIGTSTP_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Run global initialization tasks
|
|
||||||
*/
|
|
||||||
void initialize(void) {
|
|
||||||
setlocale(LC_ALL, "");
|
|
||||||
|
|
||||||
buffers_avail = 4;
|
|
||||||
buffers = malloc(sizeof(buffer_t *) * buffers_avail);
|
|
||||||
|
|
||||||
set_alternate_screen();
|
|
||||||
|
|
||||||
struct winsize w;
|
|
||||||
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
|
||||||
global_config.term_width = w.ws_col;
|
|
||||||
global_config.term_height = w.ws_row;
|
|
||||||
get_initial_termios();
|
|
||||||
set_unbuffered();
|
|
||||||
|
|
||||||
mouse_enable();
|
|
||||||
|
|
||||||
signal(SIGWINCH, SIGWINCH_handler);
|
|
||||||
signal(SIGCONT, SIGCONT_handler);
|
|
||||||
signal(SIGTSTP, SIGTSTP_handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move the cursor to a specific line.
|
* Move the cursor to a specific line.
|
||||||
*/
|
*/
|
||||||
@ -2538,9 +2527,6 @@ void open_file(char * file) {
|
|||||||
recalculate_tabs(env->lines[i]);
|
recalculate_tabs(env->lines[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
update_title();
|
|
||||||
goto_line(0);
|
|
||||||
|
|
||||||
fclose(f);
|
fclose(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2952,6 +2938,8 @@ void process_command(char * cmd) {
|
|||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
/* This actually opens a new tab */
|
/* This actually opens a new tab */
|
||||||
open_file(argv[1]);
|
open_file(argv[1]);
|
||||||
|
update_title();
|
||||||
|
goto_line(0);
|
||||||
} else {
|
} else {
|
||||||
/* TODO: Reopen file? */
|
/* TODO: Reopen file? */
|
||||||
render_error("Expected a file to open...");
|
render_error("Expected a file to open...");
|
||||||
@ -4454,15 +4442,30 @@ void replace_mode(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void show_usage(char * argv[]) {
|
static void show_usage(char * argv[]) {
|
||||||
|
#define _S "\033[3m"
|
||||||
|
#define _E "\033[0m\n"
|
||||||
printf(
|
printf(
|
||||||
"bim - Text editor\n"
|
"bim - Text editor\n"
|
||||||
"\n"
|
"\n"
|
||||||
"usage: %s [-s] [path]\n"
|
"usage: %s [options] [file]\n"
|
||||||
|
" %s [options] -\n"
|
||||||
"\n"
|
"\n"
|
||||||
" -s \033[3mdisable automatic syntax highlighting\033[0m\n"
|
" -R " _S "open initial buffer read-only" _E
|
||||||
" -R \033[3mopen initial buffer read-only\033[0m\n"
|
" -O " _S "set various display options:" _E
|
||||||
" -? \033[3mshow this help text\033[0m\n"
|
" noscroll " _S "disable terminal scrolling" _E
|
||||||
"\n", argv[0]);
|
" noaltscreen " _S "disable alternate screen buffer" _E
|
||||||
|
" nomouse " _S "disable mouse support" _E
|
||||||
|
" nounicode " _S "disable unicode display" _E
|
||||||
|
" nobright " _S "disable bright next" _E
|
||||||
|
" nohideshow " _S "disable togglging cursor visibility" _E
|
||||||
|
" nosyntax " _S "disable syntax highlighting on load" _E
|
||||||
|
" -c,-C " _S "print file to stdout with syntax hilighting" _E
|
||||||
|
" " _S "-C includes line numbers, -c does not" _E
|
||||||
|
" -u " _S "override bimrc file" _E
|
||||||
|
" -? " _S "show this help text" _E
|
||||||
|
"\n", argv[0], argv[0]);
|
||||||
|
#undef _E
|
||||||
|
#undef _S
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -4564,32 +4567,88 @@ void detect_weird_terminals(void) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run global initialization tasks
|
||||||
|
*/
|
||||||
|
void initialize(void) {
|
||||||
|
setlocale(LC_ALL, "");
|
||||||
|
|
||||||
|
detect_weird_terminals();
|
||||||
|
load_colorscheme_ansi();
|
||||||
|
load_bimrc();
|
||||||
|
|
||||||
|
buffers_avail = 4;
|
||||||
|
buffers = malloc(sizeof(buffer_t *) * buffers_avail);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize terminal for editor display.
|
||||||
|
*/
|
||||||
|
void init_terminal(void) {
|
||||||
|
set_alternate_screen();
|
||||||
|
update_screen_size();
|
||||||
|
get_initial_termios();
|
||||||
|
set_unbuffered();
|
||||||
|
mouse_enable();
|
||||||
|
|
||||||
|
signal(SIGWINCH, SIGWINCH_handler);
|
||||||
|
signal(SIGCONT, SIGCONT_handler);
|
||||||
|
signal(SIGTSTP, SIGTSTP_handler);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char * argv[]) {
|
int main(int argc, char * argv[]) {
|
||||||
int opt;
|
int opt;
|
||||||
while ((opt = getopt(argc, argv, "?sR")) != -1) {
|
while ((opt = getopt(argc, argv, "?c:C:u:RO:")) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 's':
|
|
||||||
global_config.hilight_on_open = 0;
|
|
||||||
break;
|
|
||||||
case 'R':
|
case 'R':
|
||||||
global_config.initial_file_is_read_only = 1;
|
global_config.initial_file_is_read_only = 1;
|
||||||
break;
|
break;
|
||||||
|
case 'c':
|
||||||
|
case 'C':
|
||||||
|
/* Print file to stdout using our syntax highlighting and color theme */
|
||||||
|
initialize();
|
||||||
|
open_file(optarg);
|
||||||
|
for (int i = 0; i < env->line_count; ++i) {
|
||||||
|
if (opt == 'C') {
|
||||||
|
draw_line_number(i);
|
||||||
|
}
|
||||||
|
render_line(env->lines[i], 6 * (env->lines[i]->actual + 1), 0);
|
||||||
|
clear_to_end();
|
||||||
|
fprintf(stdout,"\n");
|
||||||
|
}
|
||||||
|
reset();
|
||||||
|
clear_to_end();
|
||||||
|
return 0;
|
||||||
case 'u':
|
case 'u':
|
||||||
global_config.bimrc_path = optarg;
|
global_config.bimrc_path = optarg;
|
||||||
break;
|
break;
|
||||||
|
case 'O':
|
||||||
|
/* Set various display options */
|
||||||
|
if (!strcmp(optarg,"noaltscreen")) global_config.can_altscreen = 0;
|
||||||
|
else if (!strcmp(optarg,"noscroll")) global_config.can_scroll = 0;
|
||||||
|
else if (!strcmp(optarg,"nomouse")) global_config.can_mouse = 0;
|
||||||
|
else if (!strcmp(optarg,"nounicode")) global_config.can_unicode = 0;
|
||||||
|
else if (!strcmp(optarg,"nobright")) global_config.can_bright = 0;
|
||||||
|
else if (!strcmp(optarg,"nohideshow")) global_config.can_hideshow = 0;
|
||||||
|
else if (!strcmp(optarg,"nosyntax")) global_config.hilight_on_open = 0;
|
||||||
|
else {
|
||||||
|
fprintf(stderr, "%s: unrecognized -O option: %s\n", argv[0], optarg);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
case '?':
|
case '?':
|
||||||
show_usage(argv);
|
show_usage(argv);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
detect_weird_terminals();
|
|
||||||
initialize();
|
initialize();
|
||||||
load_colorscheme_ansi();
|
init_terminal();
|
||||||
load_bimrc();
|
|
||||||
|
|
||||||
if (argc > optind) {
|
if (argc > optind) {
|
||||||
open_file(argv[optind]);
|
open_file(argv[optind]);
|
||||||
|
update_title();
|
||||||
|
goto_line(0);
|
||||||
if (global_config.initial_file_is_read_only) {
|
if (global_config.initial_file_is_read_only) {
|
||||||
env->readonly = 1;
|
env->readonly = 1;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user