2 * Copyright (C) 2008 Diego Hernan Borghetti.
31 /* Function declaration. */
32 static int e_cmode_check(char *ext
);
33 static void e_cmode_init(void);
34 static int e_check_unsave(E_Eco
*ec
);
36 static void e_signal(int num
);
37 static void e_signal_size(int num
);
38 static void e_quit(E_Eco
*ec
);
39 static void e_keymap_init(void);
41 /* only global in this file!! */
42 static E_Eco
*E
= NULL
;
45 int main(int argc
, char **argv
)
51 E
= (E_Eco
*)malloc(sizeof(E_Eco
));
61 E
->sc
= e_screen_init(E
->tr
);
64 signal(SIGTERM
, e_signal
);
65 signal(SIGQUIT
, e_signal
);
66 signal(SIGWINCH
, e_signal_size
);
77 for (i
= 0; i
< argc
; i
++) {
78 /* parse options here!! */
79 bf
= e_file_read(argv
[i
], &st
);
81 bf
= e_buffer_new(argv
[i
]);
86 e_main_view_add(E
, v
);
93 bf
= e_buffer_new("main");
96 e_main_view_add(E
, v
);
100 /* and add to the view. */
101 E
->view
= E
->view_list
;
102 e_view_show(E
->view
);
103 e_view_redraw(E
->view
);
105 /* name to functions binding. */
106 e_func_add("e_quit", e_quit
);
107 e_func_add("e_cmd_write", e_cmd_write
);
108 e_func_add("e_cmd_killline", e_cmd_killline
);
109 e_func_add("e_cmd_rotate", e_cmd_rotate
);
110 e_func_add("e_cmd_goto", e_cmd_goto
);
111 e_func_add("e_cmd_cr", e_cmd_cr
);
112 e_func_add("e_cmd_ht", e_cmd_ht
);
113 e_func_add("e_cmd_backspace", e_cmd_backspace
);
114 e_func_add("e_cmd_find_file", e_cmd_find_file
);
115 e_func_add("e_cmd_cutline", e_cmd_cutline
);
116 e_func_add("e_cmd_paste", e_cmd_paste
);
117 e_func_add("e_cmd_cleankill", e_cmd_cleankill
);
119 /* load user keymap. */
122 /* init the kill buffer. */
125 /* default colors. */
129 /* clean term and screen. */
133 e_screen_move(E
->sc
, 0, 0);
134 e_screen_eeop(E
->sc
);
137 e_screen_swap(E
->tr
, E
->sc
, 1);
139 /* register the cmode in the system. */
142 /* and start the loop. */
149 /* Get the next character. */
153 * Clear the status message, before
154 * call any other functions.
159 * Depend on the character we have three possibilities here:
160 * 1) An escape secuence or command.
161 * 2) Combination of command (for example. CTRL X + C)
162 * 3) Normal text input for the buffer.
175 e_buffer_left(E
->view
->b
);
179 e_buffer_right(E
->view
->b
);
183 e_buffer_up(E
->view
->b
);
187 e_buffer_down(E
->view
->b
);
191 e_buffer_bol(E
->view
->b
);
195 e_buffer_eol(E
->view
->b
);
201 e_buffer_bol(E
->view
->b
);
207 if (E
->view
->b
->dot
== E
->view
->b
->line
->used
) {
208 e_buffer_down(E
->view
->b
);
209 e_buffer_joinline(E
->view
->b
);
212 e_buffer_del(E
->view
->b
);
220 if (E
->view
->b
->dot
== E
->view
->b
->line
->used
) {
221 e_buffer_down(E
->view
->b
);
222 e_buffer_joinline(E
->view
->b
);
225 e_buffer_del(E
->view
->b
);
232 e_buffer_eol(E
->view
->b
);
239 e_buffer_scroll(E
->view
->b
, E
->sc
->nrow
- 3, 1);
246 e_buffer_scroll(E
->view
->b
, E
->sc
->nrow
- 3, 0);
255 if (E
->view
->b
->dot
== E
->view
->b
->line
->used
) {
256 e_buffer_down(E
->view
->b
);
257 e_buffer_joinline(E
->view
->b
);
260 e_buffer_del(E
->view
->b
);
269 e_buffer_eol(E
->view
->b
);
273 e_buffer_bol(E
->view
->b
);
279 e_view_redraw(E
->view
);
281 /* Second case here. */
282 else if (c
> 0x00 && c
< 0x20) {
294 if (c
> 0x00 && c
< 0x20)
297 /* yes, ctrl+G always cancel the command. */
302 e_key_exec(E
, c
, ctrl_x
);
304 /* And normal input text for the buffer. */
305 else if (c
> 0x1f && c
<= 0x7f) {
307 e_buffer_backspace(E
->view
->b
);
309 e_buffer_insert(E
->view
->b
, c
, 1);
310 e_view_redraw(E
->view
);
312 /* update the last character. */
318 e_screen_swap(E
->tr
, E
->sc
, 0);
326 * Don't need check for changes, the e_quit
327 * function already handler it.
329 bf
= E
->buffers
->next
;
330 e_buffer_free(E
->buffers
);
337 while (E
->view_list
) {
338 v
= E
->view_list
->next
;
339 e_view_free(E
->view_list
);
343 e_screen_free(E
->sc
);
351 static int e_check_unsave(E_Eco
*ec
)
356 buf
= (char *)malloc(512);
359 if (!strcmp(p
->paths
->file
, "main")) {
364 if (p
->flag
& BUFFER_FLUSH
) {
365 sprintf(buf
, "Modified buffer (%s), save (y/n/c)? ", p
->paths
->file
);
366 while (!(s
= e_status_get_msg(ec
, buf
)))
371 else if (s
[0] == 'c') {
382 static void e_signal(int num
)
387 static void e_signal_size(int num
)
392 if (ioctl(0, TIOCGWINSZ
, &ws
))
395 if (ws
.ws_row
-1 == E
->tr
->nrow
&& ws
.ws_col
== E
->tr
->ncol
)
398 E
->tr
->nrow
= ws
.ws_row
-1;
399 E
->tr
->ncol
= ws
.ws_col
;
401 /* yes... fix this. */
402 e_screen_free(E
->sc
);
403 E
->sc
= e_screen_init(E
->tr
);
405 /* ok, we need resize all the view. */
408 e_view_resize(v
, E
->sc
->nrow
, E
->sc
->ncol
);
412 /* clearn term and screen. */
416 e_screen_move(E
->sc
, 0, 0);
417 e_screen_eeop(E
->sc
);
420 e_screen_swap(E
->tr
, E
->sc
, 1);
422 signal(SIGWINCH
, e_signal_size
);
425 static void e_quit(E_Eco
*ec
)
427 if (e_check_unsave(ec
))
432 static void e_keymap_init(void)
434 e_key_add('C', 1, e_quit
);
435 e_key_add('S', 1, e_cmd_write
);
436 e_key_add('K', 0, e_cmd_killline
);
437 e_key_add('R', 0, e_cmd_rotate
);
438 e_key_add('G', 0, e_cmd_goto
);
439 e_key_add('F', 1, e_cmd_find_file
);
440 e_key_add('U', 0, e_cmd_update
);
441 e_key_add('S', 0, e_cmd_search_forward
);
442 e_key_add('B', 0, e_cmd_search_backward
);
443 e_key_add('R', 1, e_cmd_replace
);
445 e_key_add('M', 0, e_cmd_cr
);
446 e_key_add('I', 0, e_cmd_ht
);
447 e_key_add('H', 0, e_cmd_backspace
);
448 e_key_add('C', 0, e_cmd_cutline
);
449 e_key_add('Y', 0, e_cmd_paste
);
450 e_key_add('L', 0, e_cmd_cleankill
);
451 e_key_add('V', 0, e_cmd_copyline
);
454 void e_main_rotate(E_Eco
*ec
)
456 e_view_unshow(ec
->view
);
458 ec
->view
= ec
->view
->next
;
460 ec
->view
= ec
->view_list
;
462 e_view_show(ec
->view
);
463 e_view_redraw(ec
->view
);
466 E_Buffer
*e_main_find(E_Eco
*ec
, char *name
)
471 /* get the absolute path */
472 paths
= e_file_get_paths(name
);
482 free((void *)paths
->path
);
489 if (!strcmp(p
->paths
->file
, paths
->file
)) {
490 free((void *)paths
->path
);
491 free((void *)paths
->file
);
497 free((void *)paths
->path
);
498 free((void *)paths
->file
);
503 void e_main_add(E_Eco
*ec
, E_Buffer
*bf
)
505 bf
->next
= ec
->buffers
;
509 void e_main_remove(E_Eco
*ec
, E_Buffer
*bf
)
520 ec
->buffers
= p
->next
;
530 void e_main_view_add(E_Eco
*ec
, E_View
*v
)
532 v
->next
= ec
->view_list
;
535 ec
->view_list
->prev
= v
;
539 void e_main_view_rem(E_Eco
*ec
, E_View
*v
)
542 v
->prev
->next
= v
->next
;
544 v
->next
->prev
= v
->prev
;
546 if (v
== ec
->view_list
) {
548 ec
->view_list
= v
->next
;
550 ec
->view_list
= v
->prev
;
554 E_View
*e_main_view_find(E_Eco
*ec
, E_Buffer
*b
)
567 /* make a C dictionary! */
568 static int e_cmode_check(char *ext
)
570 if (!strcmp(ext
, ".c"))
572 else if (!strcmp(ext
, ".C"))
577 static void e_cmode_init(void)
582 sy
->name
= strdup("C Mode");
583 sy
->check_type
= e_cmode_check
;
584 sy
->dirs
.first
= NULL
;
587 e_syntax_word_add(sy
, "void", E_TR_RED
, E_TR_BLACK
);
588 e_syntax_word_add(sy
, "int", E_TR_RED
, E_TR_BLACK
);
589 e_syntax_word_add(sy
, "char", E_TR_RED
, E_TR_BLACK
);
590 e_syntax_word_add(sy
, "float", E_TR_RED
, E_TR_BLACK
);
591 e_syntax_word_add(sy
, "double", E_TR_RED
, E_TR_BLACK
);
592 e_syntax_word_add(sy
, "long", E_TR_RED
, E_TR_BLACK
);
593 e_syntax_word_add(sy
, "struct", E_TR_RED
, E_TR_BLACK
);
594 e_syntax_word_add(sy
, "typedef", E_TR_RED
, E_TR_BLACK
);
595 e_syntax_word_add(sy
, "static", E_TR_RED
, E_TR_BLACK
);
596 e_syntax_word_add(sy
, "inline", E_TR_RED
, E_TR_BLACK
);
598 e_syntax_word_add(sy
, "if", E_TR_CYAN
, E_TR_BLACK
);
599 e_syntax_word_add(sy
, "else", E_TR_CYAN
, E_TR_BLACK
);
600 e_syntax_word_add(sy
, "while", E_TR_CYAN
, E_TR_BLACK
);
601 e_syntax_word_add(sy
, "for", E_TR_CYAN
, E_TR_BLACK
);
602 e_syntax_word_add(sy
, "do", E_TR_CYAN
, E_TR_BLACK
);
603 e_syntax_word_add(sy
, "return", E_TR_BLUE
, E_TR_BLACK
);
604 e_syntax_word_add(sy
, "ifndef", E_TR_CYAN
, E_TR_BLACK
);
605 e_syntax_word_add(sy
, "define", E_TR_CYAN
, E_TR_BLACK
);
606 e_syntax_word_add(sy
, "endif", E_TR_CYAN
, E_TR_BLACK
);
607 e_syntax_word_add(sy
, "include", E_TR_GREEN
, E_TR_BLACK
);
608 e_syntax_word_add(sy
, "malloc", E_TR_RED
, E_TR_BLACK
);
609 e_syntax_word_add(sy
, "free", E_TR_RED
, E_TR_BLACK
);
610 e_syntax_word_add(sy
, "realloc", E_TR_RED
, E_TR_BLACK
);
611 e_syntax_word_add(sy
, "++", E_TR_CYAN
, E_TR_BLACK
);
612 e_syntax_word_add(sy
, "--", E_TR_CYAN
, E_TR_BLACK
);
614 e_syntax_char_add(sy
, '{', E_TR_YELLOW
, E_TR_BLACK
);
615 e_syntax_char_add(sy
, '}', E_TR_YELLOW
, E_TR_BLACK
);
617 e_syntax_char_add(sy
, '(', E_TR_YELLOW
, E_TR_BLACK
);
618 e_syntax_char_add(sy
, ')', E_TR_YELLOW
, E_TR_BLACK
);
619 e_syntax_char_add(sy
, '[', E_TR_YELLOW
, E_TR_BLACK
);
620 e_syntax_char_add(sy
, ']', E_TR_YELLOW
, E_TR_BLACK
);
622 e_syntax_char_add(sy
, '=', E_TR_GREEN
, E_TR_BLACK
);
623 e_syntax_char_add(sy
, ',', E_TR_GREEN
, E_TR_BLACK
);
624 e_syntax_char_add(sy
, '.', E_TR_GREEN
, E_TR_BLACK
);
625 e_syntax_char_add(sy
, ';', E_TR_GREEN
, E_TR_BLACK
);
626 e_syntax_char_add(sy
, '*', E_TR_YELLOW
, E_TR_BLACK
);
627 e_syntax_char_add(sy
, '/', E_TR_YELLOW
, E_TR_BLACK
);
628 e_syntax_char_add(sy
, '"', E_TR_GREEN
, E_TR_BLACK
);
629 e_syntax_char_add(sy
, '+', E_TR_CYAN
, E_TR_BLACK
);
630 e_syntax_char_add(sy
, '-', E_TR_CYAN
, E_TR_BLACK
);
631 e_syntax_char_add(sy
, '>', E_TR_CYAN
, E_TR_BLACK
);
632 e_syntax_char_add(sy
, '<', E_TR_CYAN
, E_TR_BLACK
);