Example #1
0
File: edit.c Project: vigna/ne
int center(buffer * const b) {

	line_desc * const ld = b->cur_line_desc;
	const int right_margin = b->opt.right_margin ? b->opt.right_margin : ne_columns;

	int64_t
		len,
		start_pos = 0,
		end_pos = ld->line_len;

	while(start_pos < ld->line_len && isasciispace(ld->line[start_pos])) start_pos = next_pos(ld->line, start_pos, b->encoding);
	if (start_pos == ld->line_len) return OK;
	while(isasciispace(ld->line[prev_pos(ld->line, end_pos, b->encoding)])) end_pos = prev_pos(ld->line, end_pos, b->encoding);

	len = b->encoding == ENC_UTF8 ? utf8strlen(&ld->line[start_pos], end_pos - start_pos) : end_pos - start_pos;
	if (len >= right_margin) return OK;

	b->cur_pos = -1;
	start_undo_chain(b);

	delete_stream(b, ld, b->cur_line, end_pos, ld->line_len - end_pos);
	delete_stream(b, ld, b->cur_line, 0, start_pos);
	insert_spaces(b, ld, b->cur_line, 0, (right_margin - len) / 2);

	end_undo_chain(b);

	return OK;
}
void add_string_pair(char *in, char *out) {
    int *int_in, *int_out;
    int i, j;
    char *token;
    struct stringpair *newpair;
    /* Get int array */
    int_in  = malloc(sizeof(int) * (utf8strlen(in) + 1));
    int_out = malloc(sizeof(int) * (utf8strlen(out) + 1));
	if (g_input_format == INPUT_FORMAT_L2P) {
		for (i = 0, j = 0; in[i] != '\0'; i += utf8len(&in[i]), j++) {
			int_in[j] = get_set_char_num(strndup(&in[i], utf8len(&in[i])));
		}
		int_in[j] = -1;
		for (i = 0, j = 0; out[i] != '\0'; i += utf8len(&out[i]), j++) {
			int_out[j] = get_set_char_num(strndup(&out[i], utf8len(&out[i])));
		}
		int_out[j] = -1;
	} else if (g_input_format == INPUT_FORMAT_NEWS) {
		token = strtok(in, " ");
		for (j = 0; token != NULL; j++) {
			int_in[j] = get_set_char_num(token);
			token = strtok(NULL, " ");
		}
		int_in[j] = -1;
		token = strtok(out, " ");
		for (j = 0; token != NULL; j++) {
			int_out[j] = get_set_char_num(token);
			token = strtok(NULL, " ");
		}
		int_out[j] = -1;	
	}

	newpair = malloc(sizeof(struct stringpair));
	newpair->in = int_in;
	newpair->out = int_out;    
	newpair->next = NULL;
	if (g_stringpairs == NULL) {
		g_stringpairs = newpair;
		g_stringpairs_tail = newpair;
	} else {
		g_stringpairs_tail->next = newpair;
		g_stringpairs_tail = newpair;
	}    
}
void print_pair_aligned(int *in, int *out) {
	int i, fieldwidth;
	char *instr, *outstr;
	g_symboltable[0] = "_";
	for (i = 0; in[i] != -1 && out[i] != -1; i++) {
		instr = g_symboltable[ in[i] ];
		outstr =  g_symboltable[ out[i] ];
		fieldwidth = utf8strlen(instr) > utf8strlen(outstr) ? utf8strlen(instr) : utf8strlen(outstr);
		printf("%-*s", fieldwidth, instr);
		if (in[i+1] != -1 && out[i+1] != -1)
			printf("|");      
	}
	printf("\n");
	for (i = 0; in[i] != -1 && out[i] != -1; i++) {
		instr = g_symboltable[ in[i] ];
		outstr =  g_symboltable[ out[i] ];
		fieldwidth = utf8strlen(instr) > utf8strlen(outstr) ? utf8strlen(instr) : utf8strlen(outstr);
		printf("%-*s", fieldwidth, outstr);
		if (in[i+1] != -1 && out[i+1] != -1)
			printf("|");      
	}
	printf("\n\n");
}