📄 editcmd.c
字号:
edit->search_start = edit->curs1; /* try and find from right here for next search */
edit_update_curs_col (edit);
edit->force |= REDRAW_PAGE;
edit_render_keypress (edit);
if (times_replaced) {
sprintf (fin_string, _(" %ld replacements made. "), times_replaced);
edit_message_dialog (_(" Replace "), fin_string);
} else
edit_message_dialog (_(" Replace "), _(" Search string not found. "));
replace_continue = 0;
}
} while (replace_continue);
free (exp1);
free (exp2);
free (exp3);
edit->force = REDRAW_COMPLETELY;
edit_scroll_screen_over_cursor (edit);
}
void edit_search_cmd (WEdit * edit, int again)
{
static char *old = NULL;
char *exp = "";
if (!edit) {
if (old) {
free (old);
old = 0;
}
return;
}
exp = old ? old : exp;
if (again) { /*ctrl-hotkey for search again. */
if (!old)
return;
exp = strdup (old);
} else {
edit_search_dialog (edit, &exp);
edit_push_action (edit, KEY_PRESS + edit->start_display);
}
if (exp) {
if (*exp) {
int len = 0;
if (old)
free (old);
old = strdup (exp);
if (edit->found_len && edit->search_start == edit->found_start + 1 && replace_backwards)
edit->search_start--;
if (edit->found_len && edit->search_start == edit->found_start - 1 && !replace_backwards)
edit->search_start++;
edit->search_start = edit_find (edit->search_start, (unsigned char *) exp, &len, edit->last_byte,
(int (*)(void *, long)) edit_get_byte, (void *) edit);
if (edit->search_start >= 0) {
edit->found_start = edit->search_start;
edit->found_len = len;
edit_cursor_move (edit, edit->search_start - edit->curs1);
edit_scroll_screen_over_cursor (edit);
if (replace_backwards)
edit->search_start--;
else
edit->search_start++;
} else if (edit->search_start == -3) {
edit->search_start = edit->curs1;
regexp_error (edit);
} else {
edit->search_start = edit->curs1;
edit_error_dialog (_(" Search "), _(" Search string not found. "));
}
}
free (exp);
}
edit->force |= REDRAW_COMPLETELY;
edit_scroll_screen_over_cursor (edit);
}
/* Real edit only */
void edit_quit_cmd (WEdit * edit)
{
edit_push_action (edit, KEY_PRESS + edit->start_display);
edit->force |= REDRAW_COMPLETELY;
if (edit->modified) {
#ifdef MIDNIGHT
switch (edit_query_dialog3 (_(" Quit "), _(" File was modified, Save with exit? "), _("Cancel quit"), _("&Yes"), _("&No"))) {
#else
/* Confirm 'Quit' dialog box */
switch (edit_query_dialog3 (_(" Quit "),
_(" Current text was modified without a file save. \n Save with exit? "), _(" &Cancel quit "), _(" &Yes "), _(" &No "))) {
#endif
case 1:
edit_push_markers (edit);
edit_set_markers (edit, 0, 0, 0, 0);
if (!edit_save_cmd (edit))
return;
break;
case 2:
#ifdef MIDNIGHT
if (edit->delete_file)
unlink (catstrs (edit->dir, edit->filename, 0));
#endif
break;
case 0:
case -1:
return;
}
}
#ifdef MIDNIGHT
else if (edit->delete_file)
unlink (catstrs (edit->dir, edit->filename, 0));
edit->widget.parent->running = 0;
#else
edit->stopped = 1;
#endif
}
#define TEMP_BUF_LEN 1024
extern int column_highlighting;
/* returns a null terminated length of text. Result must be free'd */
unsigned char *edit_get_block (WEdit * edit, long start, long finish, int *l)
{
unsigned char *s, *r;
r = s = malloc (finish - start + 1);
if (column_highlighting) {
*l = 0;
while (start < finish) { /* copy from buffer, excluding chars that are out of the column 'margins' */
int c, x;
x = edit_move_forward3 (edit, edit_bol (edit, start), 0, start);
c = edit_get_byte (edit, start);
if ((x >= edit->column1 && x < edit->column2)
|| (x >= edit->column2 && x < edit->column1) || c == '\n') {
*s++ = c;
(*l)++;
}
start++;
}
} else {
*l = finish - start;
while (start < finish)
*s++ = edit_get_byte (edit, start++);
}
*s = 0;
return r;
}
/* save block, returns 1 on success */
int edit_save_block (WEdit * edit, const char *filename, long start, long finish)
{
long i = start, end, filelen = finish - start;
int file;
unsigned char *buf;
if ((file = open ((char *) filename, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
return 0;
buf = malloc (TEMP_BUF_LEN);
while (start != finish) {
end = min (finish, start + TEMP_BUF_LEN);
for (; i < end; i++)
buf[i - start] = edit_get_byte (edit, i);
filelen -= write (file, (char *) buf, end - start);
start = end;
}
free (buf);
close (file);
if (filelen)
return 0;
return 1;
}
/* copies a block to clipboard file */
static int edit_save_block_to_clip_file (WEdit * edit, long start, long finish)
{
return edit_save_block (edit, catstrs (home_dir, CLIP_FILE, 0), start, finish);
}
#ifndef MIDNIGHT
void paste_text (WEdit * edit, unsigned char *data, unsigned int nitems)
{
if (data) {
data += nitems - 1;
while (nitems--)
edit_insert_ahead (edit, *data--);
}
edit->force |= REDRAW_COMPLETELY;
}
char *selection_get_line (void *data, int line)
{
static unsigned char t[1024];
struct selection *s;
int i = 0;
s = (struct selection *) data;
line = (current_selection + line + 1) % NUM_SELECTION_HISTORY;
if (s[line].text) {
unsigned char *p = s[line].text;
int c, j;
for (j = 0; j < s[line].len; j++) {
c = *p++;
if ((c < ' ' || (c > '~' && c < 160)) && c != '\t') {
t[i++] = '.';
t[i++] = '\b';
t[i++] = '.';
} else
t[i++] = c;
if (i > 1020)
break;
}
}
t[i] = 0;
return (char *) t;
}
void edit_paste_from_history (WEdit * edit)
{
int i, c;
edit_update_curs_col (edit);
edit_update_curs_row (edit);
c = max (20, edit->num_widget_columns - 5);
i = CListboxDialog (WIN_MESSAGES, c, 10,
0, NUM_SELECTION_HISTORY - 10, NUM_SELECTION_HISTORY - 1, NUM_SELECTION_HISTORY,
selection_get_line, (void *) selection_history);
if (i < 0)
return;
i = (current_selection + i + 1) % NUM_SELECTION_HISTORY;
paste_text (edit, selection_history[i].text, selection_history[i].len);
edit->force |= REDRAW_COMPLETELY;
}
/* copies a block to the XWindows buffer */
static int edit_XStore_block (WEdit * edit, long start, long finish)
{
edit_get_selection (edit);
if (selection.len <= 512 * 1024) { /* we don't want to fill up the server */
XStoreBytes (CDisplay, (char *) selection.text, (int) selection.len);
return 0;
} else
return 1;
}
int edit_copy_to_X_buf_cmd (WEdit * edit)
{
long start_mark, end_mark;
if (eval_marks (edit, &start_mark, &end_mark))
return 0;
edit_XStore_block (edit, start_mark, end_mark);
if (!edit_save_block_to_clip_file (edit, start_mark, end_mark)) {
edit_error_dialog (_(" Copy to clipboard "), get_sys_error (_(" Unable to save to file. ")));
return 1;
}
XSetSelectionOwner (CDisplay, XA_PRIMARY, edit->widget->winid, CurrentTime);
edit_mark_cmd (edit, 1);
return 0;
}
int edit_cut_to_X_buf_cmd (WEdit * edit)
{
long start_mark, end_mark;
if (eval_marks (edit, &start_mark, &end_mark))
return 0;
edit_XStore_block (edit, start_mark, end_mark);
if (!edit_save_block_to_clip_file (edit, start_mark, end_mark)) {
edit_error_dialog (_(" Cut to clipboard "), _(" Unable to save to file. "));
return 1;
}
edit_block_delete_cmd (edit);
XSetSelectionOwner (CDisplay, XA_PRIMARY, edit->widget->winid, CurrentTime);
edit_mark_cmd (edit, 1);
return 0;
}
void selection_paste (WEdit * edit, Window win, unsigned prop, int delete);
void edit_paste_from_X_buf_cmd (WEdit * edit)
{
if (selection.text)
paste_text (edit, selection.text, selection.len);
else if (!XGetSelectionOwner (CDisplay, XA_PRIMARY))
selection_paste (edit, CRoot, XA_CUT_BUFFER0, False);
else
XConvertSelection (CDisplay, XA_PRIMARY, XA_STRING,
XInternAtom (CDisplay, "VT_SELECTION", False),
edit->widget->winid, CurrentTime);
edit->force |= REDRAW_PAGE;
}
#else /* MIDNIGHT */
void edit_paste_from_history (WEdit *edit)
{
}
int edit_copy_to_X_buf_cmd (WEdit * edit)
{
long start_mark, end_mark;
if (eval_marks (edit, &start_mark, &end_mark))
return 0;
if (!edit_save_block_to_clip_file (edit, start_mark, end_mark)) {
edit_error_dialog (_(" Copy to clipboard "), get_sys_error (_(" Unable to save to file. ")));
return 1;
}
edit_mark_cmd (edit, 1);
return 0;
}
int edit_cut_to_X_buf_cmd (WEdit * edit)
{
long start_mark, end_mark;
if (eval_marks (edit, &start_mark, &end_mark))
return 0;
if (!edit_save_block_to_clip_file (edit, start_mark, end_mark)) {
edit_error_dialog (_(" Cut to clipboard "), _(" Unable to save to file. "));
return 1;
}
edit_block_delete_cmd (edit);
edit_mark_cmd (edit, 1);
return 0;
}
void edit_paste_from_X_buf_cmd (WEdit * edit)
{
edit_insert_file (edit, catstrs (home_dir, CLIP_FILE, 0));
}
#endif /* MIDMIGHT */
void edit_goto_cmd (WEdit *edit)
{
char *f;
static int l = 0;
#ifdef MIDNIGHT
char s[12];
sprintf (s, "%d", l);
f = input_dialog (_(" Goto line "), _(" Enter line: "), l ? s : "");
#else
f = CInputDialog ("goto", WIN_MESSAGES, 150, l ? itoa (l) : "", _(" Goto line "), _(" Enter line: "));
#endif
if (f) {
if (*f) {
l = atoi (f);
edit_move_display (edit, l - edit->num_widget_lines / 2 - 1);
edit_move_to_line (edit, l - 1);
edit->force |= REDRAW_COMPLETELY;
free (f);
}
}
}
/*returns 1 on success */
int edit_save_block_cmd (WEdit * edit) {
long start_mark, end_mark;
char *exp;
if (eval_marks (edit, &start_mark, &end_mark))
return 1;
exp = edit_get_save_file (edit->dir, catstrs (home_dir, CLIP_FILE, 0), _(" Save Block "));
edit->force |= REDRAW_COMPLETELY;
edit_push_action (edit, KEY_PRESS + edit->start_display);
if (exp) {
if (!*exp) {
free (exp);
return 0;
} else {
if (edit_save_block (edit, exp, start_mark, end_mark)) {
free (exp);
edit->force |= REDRAW_COMPLETELY;
return 1;
} else {
free (exp);
edit->force |= REDRAW_COMPLETELY;
edit_error_dialog (_(" Save Block "), get_sys_error (_(" Error trying to save file. ")));
return 0;
}
}
} else
return 0;
}
/* inserts a file at the cursor, returns 1 on success */
int edit_insert_file (WEdit * edit, const char *filename)
{
int i, file, blocklen;
long current = edit->curs1;
unsigned char *buf;
if ((file = open ((char *) filename, O_RDONLY)) == -1)
return 0;
buf = malloc (TEMP_BUF_LEN);
while ((blocklen = read (file, (char *) buf, TEMP_BUF_LEN)) > 0) {
for (i = 0; i < blocklen; i++)
edit_insert (edit, buf[i]);
}
edit_cursor_move (edit, current - edit->curs1);
free (buf);
close (file);
if (blocklen)
return 0;
return 1;
}
/* returns 1 on success */
int edit_insert_file_cmd (WEdit * edit) {
char *exp = edit_get_load_file (edit->dir, catstrs (home_dir, CLIP_FILE, 0), _(" Insert File "));
edit->force |= REDRAW_COMPLETELY;
edit_push_action (edit, KEY_PRESS + edit->start_display);
if (exp) {
if (!*exp) {
free (exp);
return 0;
} else {
if (edit_insert_file (edit, exp)) {
free (exp);
return 1;
} else {
free (exp);
edit_error_dialog (_(" Insert file "), get_sys_error (_(" Error trying to insert file. ")));
return 0;
}
}
} else
return 0;
}
#ifdef MIDNIGHT
/* sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
int edit_sort_cmd (WEdit * edit)
{
static char *old = 0;
char *exp;
long start_mark, end_mark;
int e;
if (eval_marks (edit, &start_mark, &end_mark)) {
/* Not essential to translate */
edit_error_dialog (_(" Sort block "), _(" You must first highlight a block of text. "));
return 0;
}
edit_save_block (edit, catstrs (home_dir, BLOCK_FILE, 0), start_mark, end_mark)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -