Refresh the buffer after remove empty lines.
[eco.git] / cmd.c
blobe9c4c7449fd575042e1f4ed441c835f55ef8cc3d
1 /*
2 * Copyright (C) 2008 Diego Hernan Borghetti.
3 * Eco
4 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
10 #include "term.h"
11 #include "screen.h"
12 #include "buffer.h"
13 #include "view.h"
14 #include "eco.h"
15 #include "file.h"
16 #include "status.h"
17 #include "cmd.h"
18 #include "kill.h"
19 #include "update.h"
22 void e_cmd_cr(E_Eco *ec)
24 if (ec->view->b->dot == ec->view->b->line->used)
25 e_buffer_newline(ec->view->b);
26 else if (ec->view->b->dot == 0) {
27 if (ec->view->b->line->prev) {
28 e_buffer_up(ec->view->b);
29 e_buffer_eol(ec->view->b);
30 e_buffer_newline(ec->view->b);
31 e_buffer_down(ec->view->b);
33 else {
34 e_buffer_newline_first(ec->view->b);
37 else {
38 e_buffer_splitline(ec->view->b);
40 e_view_redraw(ec->view);
43 void e_cmd_ht(E_Eco *ec)
45 e_buffer_insert(ec->view->b, '\t', 1);
46 e_view_redraw(ec->view);
49 void e_cmd_backspace(E_Eco *ec)
51 e_buffer_backspace(ec->view->b);
52 e_view_redraw(ec->view);
55 void e_cmd_write(E_Eco *ec)
57 char *buf;
59 if (!strcmp(ec->view->b->paths->file, "main")) {
60 buf= e_status_get_msg(ec, "File: ");
61 if (!buf)
62 return;
64 free((void *) ec->view->b->paths->file);
65 ec->view->b->paths->file= strdup(buf);
68 /* Remove empty lines before save. */
69 e_buffer_rem_empty_lines(ec->view->b);
71 /* and write the full file to disk. */
72 e_file_write(ec, ec->view->b);
75 void e_cmd_killline(E_Eco *ec)
77 e_buffer_killline(ec->view->b, 0);
78 e_view_redraw(ec->view);
81 void e_cmd_rotate(E_Eco *ec)
83 e_main_rotate(ec);
86 void e_cmd_goto(E_Eco *ec)
88 char *buf;
89 int line;
91 buf= e_status_get_msg(ec, "Line: ");
92 if (buf) {
93 line= atoi(buf);
94 e_buffer_goto(ec->view->b, line);
95 e_view_redraw(ec->view);
99 void e_cmd_find_file(E_Eco *ec)
101 E_Buffer *bf;
102 E_View *v;
103 char *buf;
104 int st;
106 buf= e_status_get_msg(ec, "Find File: ");
107 if (buf) {
108 bf= e_main_find(ec, buf);
109 if (bf) {
110 v= e_main_view_find(ec, bf);
111 e_view_unshow(ec->view);
112 e_view_show(v);
113 e_view_redraw(v);
114 ec->view= v;
116 else {
117 bf= e_file_read(buf, &st);
118 if (bf) {
119 v= e_view_new(ec->sc);
120 v->b= bf;
121 e_main_view_add(ec, v);
122 e_main_add(ec, bf);
124 e_view_show(v);
125 e_view_redraw(v);
126 e_view_unshow(ec->view);
127 ec->view= v;
129 else if (st == 2)
130 e_status_set_msg(ec, "(file is locked!)");
131 else if (st == 1)
132 e_status_set_msg(ec, "(file is not a regular file!)");
137 void e_cmd_cutline(E_Eco *ec)
139 e_buffer_killline(ec->view->b, 1);
140 e_view_redraw(ec->view);
143 void e_cmd_copyline(E_Eco *ec)
145 e_buffer_killline(ec->view->b, 2);
146 e_buffer_down(ec->view->b);
147 e_view_redraw(ec->view);
150 void e_cmd_paste(E_Eco *ec)
152 e_kill_paste(ec->view->b);
153 e_view_redraw(ec->view);
156 void e_cmd_cleankill(E_Eco *ec)
158 e_kill_clean();
161 void e_cmd_update(E_Eco *ec)
163 e_view_redraw(ec->view);
164 e_update(ec);
165 e_status_draw(ec);
166 e_screen_swap(ec->tr, ec->sc, 1);
167 e_term_flush();
170 void e_cmd_search_forward(E_Eco *ec)
172 char *buf;
174 buf= e_status_get_msg(ec, "Search: ");
175 if (buf) {
176 strcpy(ec->view->search, buf);
177 e_buffer_search(ec->view->b, ec->view->search, BUFFER_SEARCH_FORWARD);
178 e_status_set_msg(ec, "(Search: %s)", ec->view->search);
179 e_view_redraw(ec->view);
181 else if (ec->view->search) {
182 e_buffer_search(ec->view->b, ec->view->search, BUFFER_SEARCH_FORWARD);
183 e_status_set_msg(ec, "(Search: %s)", ec->view->search);
184 e_view_redraw(ec->view);
188 void e_cmd_search_backward(E_Eco *ec)
190 char *buf;
192 buf= e_status_get_msg(ec, "Search: ");
193 if (buf) {
194 strcpy(ec->view->search, buf);
195 e_buffer_search(ec->view->b, ec->view->search, BUFFER_SEARCH_BACKWARD);
196 e_status_set_msg(ec, "(Search: %s)", ec->view->search);
197 e_view_redraw(ec->view);
199 else if (ec->view->search) {
200 e_buffer_search(ec->view->b, ec->view->search, BUFFER_SEARCH_BACKWARD);
201 e_status_set_msg(ec, "(Search: %s)", ec->view->search);
202 e_view_redraw(ec->view);
206 void e_cmd_replace(E_Eco *ec)
208 char *buf;
209 int num;
211 buf= e_status_get_msg(ec, "Replace: ");
212 if (buf) {
213 strcpy(ec->view->search, buf);
214 buf= e_status_get_msg(ec, "With: ");
215 if (buf) {
216 num= e_buffer_replace(ec->view->b, ec->view->search, buf);
217 e_status_set_msg(ec, "(%d subsitutions)", num);
218 if (num)
219 e_view_redraw(ec->view);