4 * Copyright (c) Tuomo Valkonen 1999-2001.
5 * See the included file LICENSE for details.
13 #include "selection.h"
18 #define EDLN_ALLOCUNIT 16
19 #define EDLN_HISTORY_SIZE 256
22 #define UPDATE(X) edln->ui_update(edln->uiptr, X, FALSE)
23 #define UPDATE_MOVED(X) edln->ui_update(edln->uiptr, X, TRUE)
29 static bool edln_pspc(Edln
*edln
, int n
)
34 if(edln
->palloced
<edln
->psize
+1+n
){
36 pa
|=(EDLN_ALLOCUNIT
-1);
44 memmove(np
, edln
->p
, edln
->point
*sizeof(char));
45 memmove(np
+edln
->point
+n
, edln
->p
+edln
->point
,
46 (edln
->psize
-edln
->point
+1)*sizeof(char));
51 memmove(edln
->p
+edln
->point
+n
, edln
->p
+edln
->point
,
52 (edln
->psize
-edln
->point
+1)*sizeof(char));
61 static bool edln_rspc(Edln
*edln
, int n
)
66 if(n
+edln
->point
>=edln
->psize
)
67 n
=edln
->psize
-edln
->point
;
72 if((edln
->psize
+1-n
)<(edln
->palloced
&~(EDLN_ALLOCUNIT
-1))){
73 pa
=edln
->palloced
&~(EDLN_ALLOCUNIT
-1);
81 memmove(np
, edln
->p
, edln
->point
*sizeof(char));
82 memmove(np
+edln
->point
, edln
->p
+edln
->point
+n
,
83 (edln
->psize
-edln
->point
+1-n
)*sizeof(char));
89 memmove(edln
->p
+edln
->point
, edln
->p
+edln
->point
+n
,
90 (edln
->psize
-edln
->point
+1-n
)*sizeof(char));
99 static void edln_clearstr(Edln
*edln
)
108 static bool edln_initstr(Edln
*edln
, const char *p
)
112 al
=(l
+1)|(EDLN_ALLOCUNIT
-1);
114 edln
->p
=ALLOC_N(char, al
);
129 static bool edln_setstr(Edln
*edln
, const char *p
)
132 return edln_initstr(edln
, p
);
142 bool edln_insch(Edln
*edln
, char ch
)
144 if(edln_pspc(edln
, 1)){
145 edln
->p
[edln
->point
]=ch
;
147 UPDATE_MOVED(edln
->point
-1);
154 bool edln_ovrch(Edln
*edln
, char ch
)
157 return edln_insch(edln
, ch
);
161 bool edln_insstr(Edln
*edln
, const char *str
)
170 return edln_insstr_n(edln
, str
, l
);
174 bool edln_insstr_n(Edln
*edln
, const char *str
, int l
)
176 if(!edln_pspc(edln
, l
))
179 memmove(&(edln
->p
[edln
->point
]), str
, l
);
181 UPDATE_MOVED(edln
->point
-l
);
193 void edln_back(Edln
*edln
)
197 UPDATE_MOVED(edln
->point
);
202 void edln_forward(Edln
*edln
)
204 if(edln
->point
<edln
->psize
){
206 UPDATE_MOVED(edln
->point
-1);
211 void edln_bol(Edln
*edln
)
220 void edln_eol(Edln
*edln
)
224 if(edln
->point
!=edln
->psize
){
225 edln
->point
=edln
->psize
;
231 void edln_bskip_word(Edln
*edln
)
236 if(isalnum(edln
->p
[p
]))
244 if(!isalnum(edln
->p
[p
])){
251 UPDATE_MOVED(edln
->point
);
255 void edln_skip_word(Edln
*edln
)
260 while(p
<edln
->psize
){
261 if(!isalnum(edln
->p
[p
]))
268 while(p
<edln
->psize
){
269 if(isalnum(edln
->p
[p
]))
278 void edln_set_point(Edln
*edln
, int point
)
284 else if(point
>edln
->psize
)
302 void edln_delete(Edln
*edln
)
309 void edln_backspace(Edln
*edln
)
314 UPDATE_MOVED(edln
->point
);
318 void edln_kill_to_eol(Edln
*edln
)
320 edln_rspc(edln
, edln
->psize
-edln
->point
);
325 void edln_kill_to_bol(Edln
*edln
)
336 void edln_kill_line(Edln
*edln
)
339 edln_kill_to_eol(edln
);
344 void edln_kill_word(Edln
*edln
)
349 if(!isalnum(edln
->p
[p
])){
350 while(p
<edln
->psize
){
352 if(isalnum(edln
->p
[p
]))
356 while(p
<edln
->psize
){
358 if(!isalnum(edln
->p
[p
]))
362 edln_rspc(edln
, p
-edln
->point
);
367 void edln_bkill_word(Edln
*edln
)
369 int p
=edln
->point
, p2
;
375 if(!isalnum(edln
->p
[p
])){
378 if(isalnum(edln
->p
[p
])){
386 if(!isalnum(edln
->p
[p
])){
395 edln_rspc(edln
, p2
-p
);
396 UPDATE_MOVED(edln
->point
);
406 void edln_set_mark(Edln
*edln
)
408 edln
->mark
=edln
->point
;
412 void edln_clear_mark(Edln
*edln
)
424 static void edln_do_copy(Edln
*edln
, bool del
)
428 if(edln
->mark
<0 || edln
->point
==edln
->mark
)
431 if(edln
->point
<edln
->mark
){
439 set_selection(edln
->p
+beg
, end
-beg
);
443 edln_rspc(edln
, end
-beg
);
451 void edln_cut(Edln
*edln
)
453 edln_do_copy(edln
, TRUE
);
457 void edln_copy(Edln
*edln
)
459 edln_do_copy(edln
, FALSE
);
469 static int hist_head
=EDLN_HISTORY_SIZE
;
470 static int hist_count
=0;
471 static char *hist
[EDLN_HISTORY_SIZE
];
474 void edlnhist_push(const char *str
)
476 char *strc
=scopy(str
);
485 hist_head
=EDLN_HISTORY_SIZE
-1;
487 if(hist_count
==EDLN_HISTORY_SIZE
)
488 free(hist
[hist_head
]);
492 hist
[hist_head
]=strc
;
496 void edlnhist_clear()
498 while(hist_count
!=0){
499 free(hist
[hist_head
]);
501 if(++hist_head
==EDLN_HISTORY_SIZE
)
504 hist_head
=EDLN_HISTORY_SIZE
;
508 static void edln_do_set_hist(Edln
*edln
, int e
)
511 edln_setstr(edln
, hist
[e
]);
512 edln
->point
=edln
->psize
;
514 edln
->modified
=FALSE
;
519 void edln_history_prev(Edln
*edln
)
523 if(edln
->histent
==-1){
531 if(e
==(hist_head
+hist_count
-1)%EDLN_HISTORY_SIZE
)
533 e
=(e
+1)%EDLN_HISTORY_SIZE
;
536 edln_do_set_hist(edln
, e
);
540 void edln_history_next(Edln
*edln
)
544 if(edln
->histent
==-1)
547 if(edln
->histent
==hist_head
){
553 edln
->point
=strlen(edln
->p
);
560 /* e=edln->histent-1;*/
562 edln_do_set_hist(edln
, (e
+EDLN_HISTORY_SIZE
-1)%EDLN_HISTORY_SIZE
);
572 bool edln_init(Edln
*edln
, const char *p
)
577 if(!edln_initstr(edln
, p
))
580 edln
->point
=edln
->psize
;
583 edln
->modified
=FALSE
;
584 edln
->completion_handler
=NULL
;
591 void edln_deinit(Edln
*edln
)
597 if(edln
->tmp_p
!=NULL
){
604 char* edln_finish(Edln
*edln
)
608 /*if(edln->modified)*/
612 edln
->psize
=edln
->palloced
=0;