1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
/*
* $Id: ml_cursor.c,v 1.1 2004/10/09 04:39:47 arakiken Exp $
*/
#include "ml_cursor.h"
#include <kiklib/kik_debug.h>
/* --- static functions --- */
static int
cursor_goto(
ml_cursor_t * cursor ,
int col_or_idx ,
int row ,
int is_by_col
)
{
int char_index ;
int cols_rest ;
ml_line_t * line ;
if( row > ml_model_end_row( cursor->model))
{
/* round row to end of row */
row = ml_model_end_row( cursor->model) ;
}
if( ( line = ml_model_get_line( cursor->model , row)) == NULL)
{
return 0 ;
}
if( is_by_col)
{
char_index = ml_convert_col_to_char_index( line , &cols_rest , col_or_idx , BREAK_BOUNDARY) ;
}
else
{
char_index = col_or_idx ;
cols_rest = 0 ;
}
if( ! ml_line_assure_boundary( line , char_index))
{
#ifdef DEBUG
kik_warn_printf(
KIK_DEBUG_TAG " cursor cannot goto char index %d(line length is %d)\n" ,
char_index , line->num_of_filled_chars) ;
#endif
char_index = ml_line_end_char_index(line) ;
/* In this case, cols_rest is always 0. */
}
cursor->char_index = char_index ;
cursor->row = row ;
cursor->col_in_char = cols_rest ;
cursor->col = ml_convert_char_index_to_col( ml_model_get_line( cursor->model , cursor->row) ,
cursor->char_index , 0) + cursor->col_in_char ;
return 1 ;
}
/* --- global functions --- */
int
ml_cursor_init(
ml_cursor_t * cursor ,
ml_model_t * model
)
{
cursor->model = model ;
cursor->row = 0 ;
cursor->char_index = 0 ;
cursor->col = 0 ;
cursor->col_in_char = 0 ;
cursor->saved_row = 0 ;
cursor->saved_char_index = 0 ;
cursor->saved_col = 0 ;
cursor->is_saved = 0 ;
return 1 ;
}
int
ml_cursor_final(
ml_cursor_t * cursor
)
{
/* Do nothing */
return 1 ;
}
int
ml_cursor_goto_by_char(
ml_cursor_t * cursor ,
int char_index ,
int row
)
{
return cursor_goto( cursor , char_index , row , 0) ;
}
/* Move horizontally */
int
ml_cursor_moveh_by_char(
ml_cursor_t * cursor ,
int char_index
)
{
return cursor_goto( cursor , char_index , cursor->row , 0) ;
}
int
ml_cursor_goto_by_col(
ml_cursor_t * cursor ,
int col ,
int row
)
{
return cursor_goto( cursor , col , row , 1) ;
}
/* Move horizontally */
int
ml_cursor_moveh_by_col(
ml_cursor_t * cursor ,
int col
)
{
return cursor_goto( cursor , col , cursor->row , 1) ;
}
int
ml_cursor_goto_home(
ml_cursor_t * cursor
)
{
cursor->row = 0 ;
cursor->char_index = 0 ;
cursor->col = 0 ;
cursor->col_in_char = 0 ;
return 1 ;
}
int
ml_cursor_goto_beg_of_line(
ml_cursor_t * cursor
)
{
cursor->char_index = 0 ;
cursor->col = 0 ;
cursor->col_in_char = 0 ;
return 1 ;
}
int
ml_cursor_reset_col_in_char(
ml_cursor_t * cursor
)
{
cursor->col = ml_convert_char_index_to_col( ml_get_cursor_line( cursor) , cursor->char_index , 0) ;
cursor->col_in_char = 0 ;
return 1 ;
}
int
ml_cursor_go_forward(
ml_cursor_t * cursor
)
{
/* full width char check. */
if( cursor->col_in_char + 1 < ml_char_cols( ml_get_cursor_char(cursor)))
{
#ifdef __DEBUG
kik_debug_printf( KIK_DEBUG_TAG " cursor is at 2nd byte of multi byte char.\n") ;
#endif
cursor->col ++ ;
cursor->col_in_char ++ ;
return 1 ;
}
else if( cursor->char_index < ml_line_end_char_index( ml_get_cursor_line(cursor)))
{
cursor->char_index ++ ;
ml_cursor_reset_col_in_char( cursor) ;
return 1 ;
}
else
{
/* Can't go forward in this line anymore. */
return 0 ;
}
}
int
ml_cursor_cr_lf(
ml_cursor_t * cursor
)
{
if( cursor->model->num_of_rows <= cursor->row + 1)
{
return 0 ;
}
cursor->row ++ ;
cursor->char_index = 0 ;
cursor->col = 0 ;
if( ! ml_line_assure_boundary( ml_get_cursor_line(cursor) , 0))
{
kik_error_printf( "Could cause unexpected behavior.\n") ;
return 0 ;
}
return 1 ;
}
ml_line_t *
ml_get_cursor_line(
ml_cursor_t * cursor
)
{
return ml_model_get_line( cursor->model , cursor->row) ;
}
ml_char_t *
ml_get_cursor_char(
ml_cursor_t * cursor
)
{
return ml_model_get_line( cursor->model , cursor->row)->chars + cursor->char_index ;
}
int
ml_cursor_char_is_cleared(
ml_cursor_t * cursor
)
{
cursor->char_index += cursor->col_in_char ;
cursor->col_in_char = 0 ;
return 1 ;
}
int
ml_cursor_left_chars_in_line_are_cleared(
ml_cursor_t * cursor
)
{
cursor->char_index = cursor->col ;
cursor->col_in_char = 0 ;
return 1 ;
}
int
ml_cursor_save(
ml_cursor_t * cursor
)
{
cursor->saved_col = cursor->col ;
cursor->saved_char_index = cursor->char_index ;
cursor->saved_row = cursor->row ;
cursor->is_saved = 1 ;
return 1 ;
}
int
ml_cursor_restore(
ml_cursor_t * cursor
)
{
if( ! cursor->is_saved)
{
return 0 ;
}
if( ! ml_cursor_goto_by_col( cursor , cursor->saved_col , cursor->saved_row))
{
return 0 ;
}
return 1 ;
}
#ifdef DEBUG
void
ml_cursor_dump(
ml_cursor_t * cursor
)
{
kik_msg_printf( "Cursor position => CH_IDX %d COL %d(+%d) ROW %d.\n" ,
cursor->char_index , cursor->col , cursor->col_in_char , cursor->row) ;
}
#endif
|