/* tell the user that this command is illegal while we are in * VIEW (read-only) mode */ int rdonly(void) { (*term.t_beep)(); emlwrite("Key illegal in VIEW mode", NULL); return(FALSE); }
/* * This routine allocates a block of memory large enough to hold a LINE * containing "used" characters. The block is always rounded up a bit. Return * a pointer to the new block, or NULL if there isn't any memory left. Print a * message in the message line if no space. */ LINE * lalloc(int used) { register LINE *lp; register int size; EML eml; static int displayed = 0; if((size = (used+NBLOCK-1) & ~(NBLOCK-1)) > NLINE) size *= 2; if (size == 0) /* Assume that an empty */ size = NBLOCK; /* line is for type-in. */ if (displayed == 0 && (lp = (LINE *) malloc(sizeof(LINE)+(size*sizeof(CELL)))) == NULL){ eml.s = comatose(size); emlwrite("Cannot allocate %s bytes (read file incomplete)", &eml); displayed++; return (NULL); } lp->l_size = size; lp->l_used = used; return (lp); }
/* * This routine blows away all of the text * in a buffer. If the buffer is marked as changed * then we ask if it is ok to blow it away; this is * to save the user the grief of losing text. The * window chain is nearly always wrong if this gets * called; the caller must arrange for the updates * that are required. Return TRUE if everything * looks good. */ int bclear(BUFFER *bp) { register LINE *lp; register int s = FALSE; if(Pmaster){ if ((bp->b_flag&BFTEMP) == 0 /* Not scratch buffer. */ && (bp->b_flag&BFCHG) != 0){ /* Something changed */ emlwrite("buffer lines not freed.", NULL); return (s); } } else{ if ((bp->b_flag&BFTEMP) == 0 /* Not scratch buffer. */ && (bp->b_flag&BFCHG) != 0 /* Something changed */ /* TRANSLATORS: A question asking whether to forget about the changes and revert to the unchanged version. */ && (s=mlyesno_utf8(_("Discard changes"), -1)) != TRUE){ return (s); } } bp->b_flag &= ~BFCHG; /* Not changed */ while ((lp=lforw(bp->b_linep)) != bp->b_linep) lfree(lp); bp->b_dotp = bp->b_linep; /* Fix "." */ bp->b_doto = 0; bp->b_markp = NULL; /* Invalidate "mark" */ bp->b_marko = 0; return (TRUE); }
/* * abort_composer - ask the question here, then go quit or * return FALSE */ int abort_composer(int f, int n) { char *result; result = ""; Pmaster->arm_winch_cleanup++; if(Pmaster->canceltest){ if(((Pmaster->pine_flags & MDHDRONLY) && !any_header_changes()) || (result = (*Pmaster->canceltest)(redraw_pico_for_callback))){ pico_all_done = COMP_CANCEL; emlwrite(result, NULL); Pmaster->arm_winch_cleanup--; return(TRUE); } else{ /* TRANSLATORS: The user typed the Cancel command and was asked to confirm that. Instead they canceled the cancel command. */ emlwrite(_("Cancel Cancelled"), NULL); curwp->w_flag |= WFMODE; /* and modeline so we */ sgarbk = TRUE; /* redraw the keymenu */ pclear(term.t_nrow-1, term.t_nrow); Pmaster->arm_winch_cleanup--; return(FALSE); } } else switch(mlyesno_utf8(Pmaster->headents ? _("Cancel message (answering \"Yes\" will abandon your mail message)") : (anycb() == FALSE) ? _("Cancel Edit (and abandon changes)") : _("Cancel Edit"), FALSE)){ case TRUE: pico_all_done = COMP_CANCEL; return(TRUE); case ABORT: emlwrite(_("\007Cancel Cancelled"), NULL); break; default: mlerase(); } return(FALSE); }
/* * This routine figures out the * bounds of the region in the current window, and * fills in the fields of the "REGION" structure pointed * to by "rp". Because the dot and mark are usually very * close together, we scan outward from dot looking for * mark. This should save time. Return a standard code. * Callers of this routine should be prepared to get * an "ABORT" status; we might make this have the * conform thing later. */ int getregion(REGION *rp, LINE *markp, int marko) { register LINE *flp; register LINE *blp; long fsize; register long bsize; if (markp == NULL) { return (FALSE); } if (curwp->w_dotp == markp) { rp->r_linep = curwp->w_dotp; if (curwp->w_doto < marko) { rp->r_offset = curwp->w_doto; rp->r_size = marko - curwp->w_doto; } else { rp->r_offset = marko; rp->r_size = curwp->w_doto - marko; } return (TRUE); } blp = curwp->w_dotp; bsize = curwp->w_doto; flp = curwp->w_dotp; fsize = llength(flp)-curwp->w_doto+1; while (flp!=curbp->b_linep || lback(blp)!=curbp->b_linep) { if (flp != curbp->b_linep) { flp = lforw(flp); if (flp == markp) { rp->r_linep = curwp->w_dotp; rp->r_offset = curwp->w_doto; rp->r_size = fsize + marko; return (TRUE); } fsize += llength(flp) + 1; } if (lback(blp) != curbp->b_linep) { blp = lback(blp); bsize += llength(blp)+1; if (blp == markp) { rp->r_linep = blp; rp->r_offset = marko; rp->r_size = bsize - marko; return (TRUE); } } } emlwrite("Bug: lost mark", NULL); return (FALSE); }
int composer_file_drop(int x, int y, char *filename) { int attached = 0; EML eml; if((ComposerTopLine > 2 && x <= ComposerTopLine) || !LikelyASCII(filename)){ AppendAttachment(filename, NULL, NULL); attached++; } else{ setimark(FALSE, 1); ifile(filename); swapimark(FALSE, 1); } if(ComposerEditing){ /* update display */ PaintBody(0); } else{ pico_refresh(0, 1); update(); } eml.s = filename; if(attached) emlwrite(_("Attached dropped file \"%s\""), &eml); else emlwrite(_("Inserted dropped file \"%s\""), &eml); if(ComposerEditing){ /* restore cursor */ HeaderPaintCursor(); } else{ curwp->w_flag |= WFHARD; update(); } return(1); }
int pico_file_drop(int x, int y, char *filename) { /* * if current buffer is unchanged * *or* "new buffer" and no current text */ if(((curwp->w_bufp->b_flag & BFCHG) == 0) || (curwp->w_bufp->b_fname[0] == '\0' && curwp->w_bufp->b_linep == lforw(curwp->w_bufp->b_linep) && curwp->w_doto == 0)){ register BUFFER *bp = curwp->w_bufp; char bname[NBUFN]; makename(bname, filename); strncpy(bp->b_bname, bname, sizeof(bp->b_bname)); bp->b_bname[sizeof(bp->b_bname)-1] = '\0'; strncpy(bp->b_fname, filename, sizeof(bp->b_fname)); bp->b_fname[sizeof(bp->b_fname)-1] = '\0'; bp->b_flag &= ~BFCHG; /* turn off change bit */ if (readin(filename, 1, 1) == ABORT) { strncpy(bp->b_bname, "", sizeof(bp->b_bname)); bp->b_bname[sizeof(bp->b_bname)-1] = '\0'; strncpy(bp->b_fname, "", sizeof(bp->b_fname)); bp->b_fname[sizeof(bp->b_fname)-1] = '\0'; } bp->b_dotp = bp->b_linep; bp->b_doto = 0; } else{ EML eml; ifile(filename); curwp->w_flag |= WFHARD; update(); eml.s = filename; emlwrite("Inserted dropped file \"%s\"", &eml); } curwp->w_flag |= WFHARD; update(); /* restore cursor */ return(1); }
/* * pico_get - return window struct pointer used as a handle * to the other pico_xxx routines. */ void * pico_get(void) { PICOTEXT *wp = NULL; LINE *lp = NULL; if((wp = (PICOTEXT *)malloc(sizeof(PICOTEXT))) != NULL){ wp->crinread = 0; if((lp = lalloc(0)) == NULL){ free(wp); return(NULL); } wp->dotp = wp->linep = lp->l_fp = lp->l_bp = lp; wp->doto = 0; } else emlwrite("Can't allocate space for text", NULL); return((void *)wp); }
/* * Write a line to the already opened file. The "buf" points to the buffer, * and the "nbuf" is its length, less the free newline. Return the status. * Check only at the newline. */ int ffputline(CELL buf[], int nbuf) { register int i; EML eml; for(i = 0; i < nbuf; ++i) if(write_a_wide_char((UCS) buf[i].c, g_pico_fio.fp) == EOF) break; if(i == nbuf) write_a_wide_char((UCS) '\n', g_pico_fio.fp); if(ferror(g_pico_fio.fp)){ eml.s = errstr(errno); emlwrite("\007Write error: %s", &eml); sleep(5); return FIOERR; } return FIOSUC; }
/* * Display the current position of the cursor, in origin 1 X-Y coordinates, * the character that is under the cursor (in octal), and the fraction of the * text that is before the cursor. The displayed column is not the current * column, but the column that would be used on an infinite width display. * Normally this is bound to "C-X =". */ int showcpos(int f, int n) { register LINE *clp; register long nch; register int cbo; register long nbc; register int lines; register int thisline; char buffer[80]; clp = lforw(curbp->b_linep); /* Grovel the data. */ cbo = 0; nch = 0L; lines = 0; for (;;) { if (clp==curwp->w_dotp && cbo==curwp->w_doto) { thisline = lines; nbc = nch; } if (cbo == llength(clp)) { if (clp == curbp->b_linep) break; clp = lforw(clp); cbo = 0; lines++; } else ++cbo; ++nch; } snprintf(buffer,sizeof(buffer),"line %d of %d (%d%%%%), character %ld of %ld (%d%%%%)", thisline+1, lines+1, (int)((100L*(thisline+1))/(lines+1)), nbc, nch, (nch) ? (int)((100L*nbc)/nch) : 0); emlwrite(buffer, NULL); return (TRUE); }
/* * ReadyForKey - return true if there's no timeout or we're told input * is available... */ int ReadyForKey(int timeout) { switch(input_ready(timeout)){ case READY_TO_READ: return(1); break; case NO_OP_COMMAND: case NO_OP_IDLE: case READ_INTR: return(0); case BAIL_OUT: case PANIC_NOW: emlwrite("\007Problem reading from keyboard!", NULL); kill(getpid(), SIGHUP); /* Bomb out (saving our work)! */ /* no return */ } /* can't happen */ return(0); }
/* * geninsert - do the actual work of inserting a character into * the list of lines. */ int geninsert(LINE **dotp, int *doto, LINE *linep, UCS c, int attb, int n, long *lines) { register LINE *lp1; register LINE *lp2; register CELL *cp1; register CELL *cp2; CELL ac; ac.a = attb; if (*dotp == linep) { /* At the end: special */ if (*doto != 0) { emlwrite("Programmer botch: geninsert", NULL); return (FALSE); } if ((lp1=lalloc(n)) == NULL) /* Allocate new line */ return (FALSE); lp2 = (*dotp)->l_bp; /* Previous line */ lp2->l_fp = lp1; /* Link in */ lp1->l_fp = *dotp; (*dotp)->l_bp = lp1; lp1->l_bp = lp2; *doto = n; *dotp = lp1; ac.c = (c & CELLMASK); cp1 = &(*dotp)->l_text[0]; while(n--) *cp1++ = ac; if(lines) (*lines)++; return (TRUE); } if ((*dotp)->l_used+n > (*dotp)->l_size) { /* Hard: reallocate */ if ((lp1=lalloc((*dotp)->l_used+n)) == NULL) return (FALSE); cp1 = &(*dotp)->l_text[0]; cp2 = &lp1->l_text[0]; while (cp1 != &(*dotp)->l_text[*doto]) *cp2++ = *cp1++; cp2 += n; while (cp1 != &(*dotp)->l_text[(*dotp)->l_used]) *cp2++ = *cp1++; (*dotp)->l_bp->l_fp = lp1; lp1->l_fp = (*dotp)->l_fp; (*dotp)->l_fp->l_bp = lp1; lp1->l_bp = (*dotp)->l_bp; /* global may be keeping track of mark/imark */ if(wheadp){ if (wheadp->w_imarkp == *dotp) wheadp->w_imarkp = lp1; if (wheadp->w_markp == *dotp) wheadp->w_markp = lp1; } free((char *) (*dotp)); *dotp = lp1; } else { /* Easy: in place */ (*dotp)->l_used += n; cp2 = &(*dotp)->l_text[(*dotp)->l_used]; cp1 = cp2-n; while (cp1 != &(*dotp)->l_text[*doto]) *--cp2 = *--cp1; } ac.c = (c & CELLMASK); while(n--) /* add the chars */ (*dotp)->l_text[(*doto)++] = ac; return(TRUE); }
/* * spell() - check for potentially missspelled words and offer them for * correction */ int spell(int f, int n) { int status, next, ret; char ccb[NLINE], *sp, *fn, *lp, *wsp, c, spc[NLINE]; UCS *b; UCS wb[NLINE], cb[NLINE]; EML eml; setimark(0, 1); emlwrite(_("Checking spelling..."), NULL); /* greetings */ if(alt_speller) return(alt_editor(1, 0)); /* f == 1 means fork speller */ if((fn = writetmp(0, NULL)) == NULL){ emlwrite(_("Can't write temp file for spell checker"), NULL); return(-1); } if((sp = (char *)getenv("SPELL")) == NULL) sp = SPELLER; /* exists? */ ret = (strlen(sp) + 1); snprintf(spc, sizeof(spc), "%s", sp); for(lp = spc, ret = FIOERR; *lp; lp++){ if((wsp = strpbrk(lp, " \t")) != NULL){ c = *wsp; *wsp = '\0'; } if(strchr(lp, '/')){ ret = fexist(lp, "x", (off_t *)NULL); } else{ char *path, fname[MAXPATH+1]; if(!(path = getenv("PATH"))) path = ":/bin:/usr/bin"; ret = ~FIOSUC; while(ret != FIOSUC && *path && pathcat(fname, &path, lp)) ret = fexist(fname, "x", (off_t *)NULL); } if(wsp) *wsp = c; if(ret == FIOSUC) break; } if(ret != FIOSUC){ eml.s = sp; emlwrite(_("\007Spell-checking file \"%s\" not found"), &eml); return(-1); } snprintf(ccb, sizeof(ccb), "( %s ) < %s", sp, fn); if(P_open(ccb) != FIOSUC){ /* read output from command */ our_unlink(fn); emlwrite(_("Can't fork spell checker"), NULL); return(-1); } ret = 1; while(ffgetline(wb, NLINE, NULL, 0) == FIOSUC && ret){ if((b = ucs4_strchr(wb, (UCS) '\n')) != NULL) *b = '\0'; ucs4_strncpy(cb, wb, NLINE); cb[NLINE-1] = '\0'; gotobob(0, 1); status = TRUE; next = 1; while(status){ if(next++) if(movetoword(wb) != TRUE) break; update(); (*term.t_rev)(1); pputs(wb, 1); /* highlight word */ (*term.t_rev)(0); if(ucs4_strcmp(cb, wb)){ char prompt[2*NLINE + 32]; char *wbu, *cbu; wbu = ucs4_to_utf8_cpystr(wb); cbu = ucs4_to_utf8_cpystr(cb); snprintf(prompt, sizeof(prompt), _("Replace \"%s\" with \"%s\""), wbu, cbu); status=mlyesno_utf8(prompt, TRUE); if(wbu) fs_give((void **) &wbu); if(cbu) fs_give((void **) &cbu); } else{ UCS *p; p = utf8_to_ucs4_cpystr(_("Edit a replacement: ")); status=mlreplyd(p, cb, NLINE, QDEFLT, NULL); if(p) fs_give((void **) &p); } curwp->w_flag |= WFMOVE; /* put cursor back */ sgarbk = 0; /* fake no-keymenu-change! */ update(); pputs(wb, 0); /* un-highlight */ switch(status){ case TRUE: chword(wb, cb, 0); /* correct word */ case FALSE: update(); /* place cursor */ break; case ABORT: emlwrite(_("Spell Checking Cancelled"), NULL); ret = FALSE; status = FALSE; break; case HELPCH: if(Pmaster){ VARS_TO_SAVE *saved_state; saved_state = save_pico_state(); (*Pmaster->helper)(pinespellhelp, _("Help with Spelling Checker"), 1); if(saved_state){ restore_pico_state(saved_state); free_pico_state(saved_state); } } else pico_help(spellhelp, _("Help with Spelling Checker"), 1); case (CTRL|'L'): next = 0; /* don't get next word */ sgarbf = TRUE; /* repaint full screen */ update(); status = TRUE; continue; default: emlwrite("Huh?", NULL); /* shouldn't get here, but.. */ status = TRUE; sleep(1); break; } forwword(0, 1); /* goto next word */ } } P_close(); /* clean up */ our_unlink(fn); swapimark(0, 1); curwp->w_flag |= WFHARD|WFMODE; sgarbk = TRUE; if(ret) emlwrite(_("Done checking spelling"), NULL); return(ret); }
int main(int argc, char *argv[]) #endif { UCS c; register int f; register int n; register BUFFER *bp; int viewflag = FALSE; /* are we starting in view mode?*/ int starton = 0; /* where's dot to begin with? */ int setlocale_collate = 1; char bname[NBUFN]; /* buffer name of file to read */ char *file_to_edit = NULL; char *display_charmap = NULL, *dc; char *keyboard_charmap = NULL; int use_system = 0; char *err = NULL; set_input_timeout(600); Pmaster = NULL; /* turn OFF composer functionality */ km_popped = 0; /* * Read command line flags before initializing, otherwise, we never * know to init for f_keys... */ file_to_edit = pico_args(argc, argv, &starton, &viewflag, &setlocale_collate); set_collation(setlocale_collate, 1); #define cpstr(s) strcpy((char *)fs_get(1+strlen(s)), s) #ifdef _WINDOWS init_utf8_display(1, NULL); #else /* UNIX */ if(display_character_set) display_charmap = cpstr(display_character_set); #if HAVE_LANGINFO_H && defined(CODESET) else if((dc = nl_langinfo_codeset_wrapper()) != NULL) display_charmap = cpstr(dc); #endif if(!display_charmap) display_charmap = cpstr("US-ASCII"); if(keyboard_character_set) keyboard_charmap = cpstr(keyboard_character_set); else keyboard_charmap = cpstr(display_charmap); if(use_system_translation){ #if PREREQ_FOR_SYS_TRANSLATION use_system++; /* This modifies its arguments */ if(setup_for_input_output(use_system, &display_charmap, &keyboard_charmap, &input_cs, &err) == -1){ fprintf(stderr, "%s\n", err ? err : "trouble with character set"); exit(1); } else if(err){ fprintf(stderr, "%s\n", err); fs_give((void **) &err); } #endif } if(!use_system){ if(setup_for_input_output(use_system, &display_charmap, &keyboard_charmap, &input_cs, &err) == -1){ fprintf(stderr, "%s\n", err ? err : "trouble with character set"); exit(1); } else if(err){ fprintf(stderr, "%s\n", err); fs_give((void **) &err); } } if(keyboard_charmap){ set_locale_charmap(keyboard_charmap); free((void *) keyboard_charmap); } if(display_charmap) free((void *) display_charmap); #endif /* UNIX */ /* * There are a couple arguments that we need to be sure * are converted for internal use. */ if(alt_speller) alt_speller = cpstr(fname_to_utf8(alt_speller)); if(opertree && opertree[0]){ strncpy(opertree, fname_to_utf8(opertree), sizeof(opertree)); opertree[sizeof(opertree)-1] = '\0'; } if(glo_quote_str_orig) glo_quote_str = utf8_to_ucs4_cpystr(fname_to_utf8(glo_quote_str_orig)); if(glo_wordseps_orig) glo_wordseps = utf8_to_ucs4_cpystr(fname_to_utf8(glo_wordseps_orig)); if(file_to_edit) file_to_edit = cpstr(fname_to_utf8(file_to_edit)); #undef cpstr #if defined(DOS) || defined(OS2) if(file_to_edit){ /* strip quotes? */ int l; if(strchr("'\"", file_to_edit[0]) && (l = strlen(file_to_edit)) > 1 && file_to_edit[l-1] == file_to_edit[0]){ file_to_edit[l-1] = '\0'; /* blat trailing quote */ file_to_edit++; /* advance past leading quote */ } } #endif if(!vtinit()) /* Displays. */ exit(1); strncpy(bname, "main", sizeof(bname)); /* default buffer name */ bname[sizeof(bname)-1] = '\0'; edinit(bname); /* Buffers, windows. */ update(); /* let the user know we are here */ #ifdef _WINDOWS mswin_setwindow(NULL, NULL, NULL, NULL, NULL, NULL); mswin_showwindow(); mswin_showcaret(1); /* turn on for main window */ mswin_allowpaste(MSWIN_PASTE_FULL); mswin_setclosetext("Use the ^X command to exit Pico."); mswin_setscrollcallback (pico_scroll_callback); #endif #if defined(USE_TERMCAP) || defined(USE_TERMINFO) || defined(VMS) if(kbesc == NULL){ /* will arrow keys work ? */ (*term.t_putchar)('\007'); emlwrite("Warning: keypad keys may be non-functional", NULL); } #endif /* USE_TERMCAP/USE_TERMINFO/VMS */ if(file_to_edit){ /* Any file to edit? */ makename(bname, file_to_edit); /* set up a buffer for this file */ bp = curbp; /* read in first file */ makename(bname, file_to_edit); strncpy(bp->b_bname, bname, sizeof(bp->b_bname)); bp->b_bname[sizeof(bp->b_bname)-1] = '\0'; if(strlen(file_to_edit) >= NFILEN){ char buf[128]; snprintf(buf, sizeof(buf), "Filename \"%.10s...\" too long", file_to_edit); emlwrite(buf, NULL); file_to_edit = NULL; } else{ strncpy(bp->b_fname, file_to_edit, sizeof(bp->b_fname)); bp->b_fname[sizeof(bp->b_fname)-1] = '\0'; if (((gmode&MDTREE) && !in_oper_tree(file_to_edit)) || readin(file_to_edit, (viewflag==FALSE), TRUE) == ABORT) { if ((gmode&MDTREE) && !in_oper_tree(file_to_edit)){ EML eml; eml.s = opertree; emlwrite(_("Can't read file from outside of %s"), &eml); } file_to_edit = NULL; } } if(!file_to_edit){ strncpy(bp->b_bname, "main", sizeof(bp->b_bname)); bp->b_bname[sizeof(bp->b_bname)-1] = '\0'; strncpy(bp->b_fname, "", sizeof(bp->b_fname)); bp->b_fname[sizeof(bp->b_fname)-1] = '\0'; } bp->b_dotp = bp->b_linep; bp->b_doto = 0; if (viewflag) /* set the view mode */ bp->b_mode |= MDVIEW; } /* setup to process commands */ lastflag = 0; /* Fake last flags. */ curbp->b_mode |= gmode; /* and set default modes*/ curwp->w_flag |= WFMODE; /* and force an update */ if(timeoutset){ EML eml; eml.s = comatose(get_input_timeout()); emlwrite(_("Checking for new mail every %s seconds"), &eml); } forwline(0, starton - 1); /* move dot to specified line */ while(1){ if(km_popped){ km_popped--; if(km_popped == 0) /* cause bottom three lines to be repainted */ curwp->w_flag |= WFHARD; } if(km_popped){ /* temporarily change to cause menu to be painted */ term.t_mrow = 2; curwp->w_ntrows -= 2; curwp->w_flag |= WFMODE; movecursor(term.t_nrow-2, 0); /* clear status line, too */ peeol(); } update(); /* Fix up the screen */ if(km_popped){ term.t_mrow = 0; curwp->w_ntrows += 2; } #ifdef MOUSE #ifdef EX_MOUSE /* New mouse function for real mouse text seletion. */ register_mfunc(mouse_in_pico, 2, 0, term.t_nrow - (term.t_mrow + 1), term.t_ncol); #else mouse_in_content(KEY_MOUSE, -1, -1, 0, 0); register_mfunc(mouse_in_content, 2, 0, term.t_nrow - (term.t_mrow + 1), term.t_ncol); #endif #endif #ifdef _WINDOWS mswin_setdndcallback (pico_file_drop); mswin_mousetrackcallback(pico_cursor); #endif c = GetKey(); #ifdef MOUSE #ifdef EX_MOUSE clear_mfunc(mouse_in_pico); #else clear_mfunc(mouse_in_content); #endif #endif #ifdef _WINDOWS mswin_cleardndcallback (); mswin_mousetrackcallback(NULL); #endif if(timeoutset && (c == NODATA || time_to_check())){ if(pico_new_mail()) emlwrite(_("You may possibly have new mail."), NULL); } if(km_popped) switch(c){ case NODATA: case (CTRL|'L'): km_popped++; break; default: /* clear bottom three lines */ mlerase(); break; } if(c == NODATA) continue; if(mpresf){ /* erase message line? */ if(mpresf++ > MESSDELAY) mlerase(); } f = FALSE; n = 1; #ifdef MOUSE clear_mfunc(mouse_in_content); #endif /* Do it. */ execute(normalize_cmd(c, fkm, 1), f, n); } }
/* Replace a pattern with the pattern the user types in one or more times. */ int replace_pat(UCS *defpat, int *wrapt) { register int status; UCS lpat[NPAT], origpat[NPAT]; /* case sensitive pattern */ EXTRAKEYS menu_pat[2]; int repl_all = FALSE; UCS *b; char utf8tmp[NPMT]; UCS prompt[NPMT]; UCS *promptp; forscan(wrapt, defpat, NULL, 0, PTBEG); /* go to word to be replaced */ lpat[0] = '\0'; /* additional 'replace all' menu option */ menu_pat[0].name = "^X"; menu_pat[0].key = (CTRL|'X'); menu_pat[0].label = N_("Repl All"); KS_OSDATASET(&menu_pat[0], KS_NONE); menu_pat[1].name = NULL; while(1) { update(); (*term.t_rev)(1); get_pat_cases(origpat, defpat); pputs(origpat, 1); /* highlight word */ (*term.t_rev)(0); snprintf(utf8tmp, NPMT, "Replace%s \"", repl_all ? " every" : ""); b = utf8_to_ucs4_cpystr(utf8tmp); if(b){ ucs4_strncpy(prompt, b, NPMT); prompt[NPMT-1] = '\0'; fs_give((void **) &b); } promptp = &prompt[ucs4_strlen(prompt)]; expandp(defpat, promptp, NPMT-(promptp-prompt)); prompt[NPMT-1] = '\0'; promptp += ucs4_strlen(promptp); b = utf8_to_ucs4_cpystr("\" with"); if(b){ ucs4_strncpy(promptp, b, NPMT-(promptp-prompt)); promptp += ucs4_strlen(promptp); prompt[NPMT-1] = '\0'; fs_give((void **) &b); } if(rpat[0] != '\0'){ if((promptp-prompt) < NPMT-2){ *promptp++ = ' '; *promptp++ = '['; *promptp = '\0'; } expandp(rpat, promptp, NPMT-(promptp-prompt)); prompt[NPMT-1] = '\0'; promptp += ucs4_strlen(promptp); if((promptp-prompt) < NPMT-1){ *promptp++ = ']'; *promptp = '\0'; } } if((promptp-prompt) < NPMT-3){ *promptp++ = ' '; *promptp++ = ':'; *promptp++ = ' '; *promptp = '\0'; } prompt[NPMT-1] = '\0'; status = mlreplyd(prompt, lpat, NPAT, QDEFLT, menu_pat); curwp->w_flag |= WFMOVE; switch(status){ case TRUE : case FALSE : if(lpat[0]){ ucs4_strncpy(rpat, lpat, NPAT); /* remember default */ rpat[NPAT-1] = '\0'; } else{ ucs4_strncpy(lpat, rpat, NPAT); /* use default */ lpat[NPAT-1] = '\0'; } if (repl_all){ status = replace_all(defpat, lpat); } else{ chword(defpat, lpat); /* replace word */ update(); status = TRUE; } if(status == TRUE) emlwrite("", NULL); return(status); case HELPCH: /* help requested */ if(Pmaster){ VARS_TO_SAVE *saved_state; saved_state = save_pico_state(); (*Pmaster->helper)(Pmaster->search_help, _("Help for Searching"), 1); if(saved_state){ restore_pico_state(saved_state); free_pico_state(saved_state); } } else pico_help(SearchHelpText, _("Help for Searching"), 1); case (CTRL|'L'): /* redraw requested */ pico_refresh(FALSE, 1); update(); break; case (CTRL|'X'): /* toggle replace all option */ if (repl_all){ repl_all = FALSE; /* TRANSLATORS: abbreviation for Replace All occurences */ menu_pat[0].label = N_("Repl All"); } else{ repl_all = TRUE; /* TRANSLATORS: Replace just one occurence */ menu_pat[0].label = N_("Repl One"); } break; default: if(status == ABORT){ emlwrite(_("Replacement Cancelled"), NULL); pico_refresh(FALSE, 1); } else{ mlerase(); chword(defpat, origpat); } update(); return(FALSE); } } }
/* * Yank text back from the kill buffer. This is really easy. All of the work * is done by the standard insert routines. All you do is run the loop, and * check for errors. Bound to "C-Y". */ int yank(int f, int n) { int c, i; REGION region, *added_region; LINE *dotp; if (curbp->b_mode&MDVIEW) /* don't allow this command if */ return(rdonly()); /* we are in read only mode */ if (n < 0) return (FALSE); if(TERM_OPTIMIZE && (curwp->w_dotp != curwp->w_bufp->b_linep)){ int l; if(worthit(&l) && !(lastflag&CFFILL)){ register int t = 0; register int i = 0; register int ch; while((ch=fremove(i++)) >= 0) if(ch == '\n') t++; if(t+l < curwp->w_toprow+curwp->w_ntrows) scrolldown(curwp, l, t); } } if(lastflag & CFFILL){ /* if last command was fillpara() */ if(lastflag & CFFLBF){ gotoeob(FALSE, 1); dotp = curwp->w_dotp; gotobob(FALSE, 1); curwp->w_doto = 0; getregion(®ion, dotp, llength(dotp)); } else{ added_region = get_last_region_added(); if(added_region){ curwp->w_dotp = added_region->r_linep; curwp->w_doto = added_region->r_offset; region = (*added_region); } else return(FALSE); } if(!ldelete(region.r_size, NULL)) return(FALSE); } /* then splat out the saved buffer */ while (n--) { i = 0; while ((c = ((lastflag&CFFILL) ? ((lastflag & CFFLBF) ? kremove(i) : fremove(i)) : kremove(i))) >= 0) { if (c == '\n') { if (lnewline() == FALSE) return (FALSE); } else { if (linsert(1, c) == FALSE) return (FALSE); } ++i; } } if(lastflag&CFFLPA){ /* if last command was fill paragraph */ curwp->w_dotp = lforw(curwp->w_dotp); curwp->w_doto = 0; curwp->w_flag |= WFMODE; if(!Pmaster){ sgarbk = TRUE; emlwrite("", NULL); } } return (TRUE); }
int setquotelevelinregion(int quotelevel, REGION *addedregion) { int i, standards_based = 0; int quote_chars = 0, backuptoprevline = 0; int starts_midline = 0, ends_midline = 0, offset_into_start; long c, sz; UCS qstr_def1[] = { '>', ' ', 0}, qstr_def2[] = { '>', 0}; LINE *lp, *line_before_start; REGION region; if(curbp->b_mode&MDVIEW) /* don't allow this command if */ return(rdonly()); /* we are in read only mode */ if(!glo_quote_str || !ucs4_strcmp(glo_quote_str, qstr_def1) || !ucs4_strcmp(glo_quote_str, qstr_def2)) standards_based++; if(!standards_based){ emlwrite("Quote level setting only works with standard \"> \" quotes", NULL); return(FALSE); } /* if region starts midline insert a newline */ if(curwp->w_doto > 0 && curwp->w_doto < llength(curwp->w_dotp)) starts_midline++; /* if region ends midline insert a newline at end */ if(curwp->w_marko > 0 && curwp->w_marko < llength(curwp->w_markp)){ ends_midline++; backuptoprevline++; /* count quote chars for re-insertion */ for(i = 0; i < llength(curwp->w_markp); ++i) if(lgetc(curwp->w_markp, i).c != '>') break; quote_chars = i; } else if(curwp->w_marko == 0) backuptoprevline++; /* find the size of the region */ getregion(®ion, curwp->w_markp, curwp->w_marko); /* cut the paragraph into our fill buffer */ fdelete(); if(!ldelete(region.r_size, finsert)) return(FALSE); line_before_start = lback(curwp->w_dotp); offset_into_start = curwp->w_doto; /* if region starts midline add a newline */ if(starts_midline) lnewline(); i = 0; while(fremove(i) >= 0){ /* remove all quote strs from current line */ if(standards_based){ while((c = fremove(i)) == '>') i++; if(c == ' ') i++; } else{ } /* insert quotelevel quote strs */ if(standards_based){ linsert(quotelevel, '>'); if(quotelevel > 0) linsert(1, ' '); } else{ } /* put back the actual line */ while((c = fremove(i++)) >= 0 && c != '\n') linsert(1, (UCS) c); if(c == '\n') lnewline(); } /* if region ends midline add a newline */ if(ends_midline){ lnewline(); if(quote_chars){ linsert(quote_chars, '>'); if(curwp->w_doto < llength(curwp->w_dotp) && lgetc(curwp->w_dotp, curwp->w_doto).c != ' ') linsert(1, ' '); } } /* * Calculate the size of the region that was added. */ swapmark(0,1); /* mark current location after adds */ addedregion->r_linep = lforw(line_before_start); addedregion->r_offset = offset_into_start; lp = addedregion->r_linep; sz = llength(lp) - addedregion->r_offset; if(lforw(lp) != curwp->w_markp->l_fp){ lp = lforw(lp); while(lp != curwp->w_markp->l_fp){ sz += llength(lp) + 1; lp = lforw(lp); } } sz -= llength(curwp->w_markp) - curwp->w_marko; addedregion->r_size = sz; swapmark(0,1); /* * This puts us at the end of the quoted region instead * of on the following line. This makes it convenient * for the user to follow a quotelevel adjustment with * a Justify if desired. */ if(backuptoprevline){ curwp->w_doto = 0; backchar(0, 1); } if(ends_midline){ /* doesn't need fixing otherwise */ unmarkbuffer(); markregion(1); } return (TRUE); }
/* * pico - the main routine for Pine's composer. * */ int pico(PICO *pm) { UCS c; register int f; register int n; char bname[NBUFN]; /* buffer name of file to read */ extern struct on_display ods; int checkpointcnt = 0, input = 0; int ret; char chkptfile[NLINE]; #ifdef _WINDOWS int cursor_shown; #endif Pmaster = pm; gmode = MDWRAP; gmode |= pm->pine_flags; /* high 4 bits rsv'd for pine */ alt_speller = pm->alt_spell; pico_all_done = 0; km_popped = 0; if(!vtinit()) /* Init Displays. */ return(COMP_CANCEL); strncpy(bname, "main", sizeof(bname)); /* default buffer name */ bname[sizeof(bname)-1] = '\0'; edinit(bname); /* Buffers, windows. */ if(InitMailHeader(pm)) /* init mail header structure */ gmode &= ~(P_BODY | P_HEADEND); /* flip off special header stuff */ /* setup to process commands */ lastflag = 0; /* Fake last flags. */ curbp->b_mode |= gmode; /* and set default modes*/ if(Pmaster->pine_anchor) pico_anchor = utf8_to_ucs4_cpystr(Pmaster->pine_anchor); else pico_anchor = NULL; if(Pmaster->quote_str) glo_quote_str = utf8_to_ucs4_cpystr(Pmaster->quote_str); else glo_quote_str = NULL; if(Pmaster->wordseps) glo_wordseps = ucs4_cpystr(Pmaster->wordseps); else glo_wordseps = NULL; bindtokey(DEL, (gmode & P_DELRUBS) ? forwdel : backdel); if(pm->msgtext) breplace(pm->msgtext); #ifdef _WINDOWS cursor_shown = mswin_showcaret(1); /* turn on for main window */ mswin_allowpaste(MSWIN_PASTE_FULL); mswin_setscrollcallback (pico_scroll_callback); #endif /* prepare for checkpointing */ chkptfile[0] = '\0'; chkptinit((*Pmaster->ckptdir)(chkptfile, sizeof(chkptfile)), sizeof(chkptfile)); if(gmode & P_CHKPTNOW) writeout(chkptfile, TRUE); pico_all_done = setjmp(finstate); /* jump out of HUP handler ? */ if(gmode & MDALTNOW){ while(!pico_all_done){ if(((gmode & P_BODY) || !Pmaster->headents) && alt_editor(0, 1) < 0) break; /* if problem, drop into pico */ if(Pmaster->headents){ update(); /* paint screen, n' start editing... */ HeaderEditor((gmode & (P_HEADEND | P_BODY)) ? 2 : 0, 0); gmode |= P_BODY; /* make sure we enter alt ed next */ } else pico_all_done = COMP_EXIT; } } else if(!pico_all_done){ if(gmode & P_BODY){ /* begin editing the header? */ ArrangeHeader(); /* line up pointers */ /* * Move to the offset pine asked us to move to. * Perhaps we should be checking to see if this is * a reasonable number before moving. */ if(Pmaster && Pmaster->edit_offset) forwchar(FALSE, Pmaster->edit_offset); } else{ update(); /* paint screen, */ HeaderEditor((gmode & P_HEADEND) ? 2 : 0, 0); } } while(1){ if(pico_all_done){ #ifdef _WINDOWS if(!cursor_shown) mswin_showcaret(0); mswin_allowpaste(MSWIN_PASTE_DISABLE); mswin_setscrollcallback (NULL); #endif ret = anycb() ? BUF_CHANGED : 0; switch(pico_all_done){ /* prepare for/handle final events */ case COMP_EXIT : /* already confirmed */ packheader(); if(Pmaster && (Pmaster->strip_ws_before_send || Pmaster->allow_flowed_text)) cleanwhitespace(); ret |= COMP_EXIT; break; case COMP_CANCEL : /* also already confirmed */ packheader(); ret = COMP_CANCEL; break; case COMP_GOTHUP: /* * pack up and let caller know that we've received a SIGHUP */ if(ComposerEditing) /* expand addr if needed */ call_builder(&headents[ods.cur_e], NULL, NULL); packheader(); ret |= COMP_GOTHUP; break; case COMP_SUSPEND : default: /* safest if internal error */ /* * If we're in the headers mark the current header line * with start_here bit so caller knows where to reset. * Also set the edit_offset, which is either the offset * into this header line or the offset into the body. * Packheader will adjust edit_offset for multi-line * headers. */ if(ComposerEditing){ /* in the headers */ headents[ods.cur_e].start_here = 1; Pmaster->edit_offset = ods.p_ind; } else{ register LINE *clp; register long offset; for(clp = lforw(curbp->b_linep), offset = 0L; clp != curwp->w_dotp; clp = lforw(clp)) offset += (llength(clp) + 1); Pmaster->edit_offset = offset + curwp->w_doto; } packheader(); ret |= COMP_SUSPEND; break; } if(pico_anchor) fs_give((void **) &pico_anchor); if(glo_quote_str) fs_give((void **) &glo_quote_str); if(glo_wordseps) fs_give((void **) &glo_wordseps); vttidy(); /* clean up tty modes */ zotdisplay(); /* blast display buffers */ zotedit(); our_unlink(chkptfile); Pmaster = NULL; /* blat global */ return(ret); } if(km_popped){ km_popped--; if(km_popped == 0) /* cause bottom three lines to be repainted */ curwp->w_flag |= WFHARD; } if(km_popped){ /* temporarily change to cause menu to be painted */ term.t_mrow = 2; curwp->w_ntrows -= 2; curwp->w_flag |= WFMODE; movecursor(term.t_nrow-2, 0); /* clear status line, too */ peeol(); } update(); /* Fix up the screen */ if(km_popped){ term.t_mrow = 0; curwp->w_ntrows += 2; } #ifdef MOUSE #ifdef EX_MOUSE /* New mouse function for real mouse text seletion. */ register_mfunc(mouse_in_pico, 2, 0, term.t_nrow - (term.t_mrow+1), term.t_ncol); #else mouse_in_content(KEY_MOUSE, -1, -1, -1, 0); register_mfunc(mouse_in_content, 2, 0, term.t_nrow - (term.t_mrow + 1), term.t_ncol); #endif #endif #ifdef _WINDOWS mswin_setdndcallback (composer_file_drop); mswin_mousetrackcallback(pico_cursor); #endif c = GetKey(); if (term.t_nrow < 6 && c != NODATA){ (*term.t_beep)(); emlwrite(_("Please make the screen bigger."), NULL); continue; } #ifdef MOUSE #ifdef EX_MOUSE clear_mfunc(mouse_in_pico); #else clear_mfunc(mouse_in_content); #endif #endif #ifdef _WINDOWS mswin_cleardndcallback (); mswin_mousetrackcallback(NULL); #endif if(c == NODATA || time_to_check()){ /* new mail ? */ if((*Pmaster->newmail)(c == NODATA ? 0 : 2, 1) >= 0){ int rv; if(km_popped){ term.t_mrow = 2; curwp->w_ntrows -= 2; curwp->w_flag |= WFHARD; km_popped = 0; } clearcursor(); mlerase(); rv = (*Pmaster->showmsg)(c); ttresize(); picosigs(); /* restore altered handlers */ if(rv) /* Did showmsg corrupt the display? */ PaintBody(0); /* Yes, repaint */ mpresf = 1; input = 0; } clearcursor(); movecursor(0, 0); } if(km_popped) switch(c){ case NODATA: case (CTRL|'L'): km_popped++; break; default: mlerase(); break; } if(c == NODATA) /* no op, getkey timed out */ continue; else if(!input++) (*Pmaster->keybinput)(); if (mpresf != FALSE) { /* message stay around only */ if (mpresf++ > NMMESSDELAY) /* so long! */ mlerase(); } f = FALSE; /* vestigial */ n = 1; /* Do it. */ execute(normalize_cmd(c, pfkm, 2), f, n); if(++checkpointcnt >= CHKPTDELAY){ checkpointcnt = 0; writeout(chkptfile, TRUE); } } }
/* * Abort. * Beep the beeper. Kill off any keyboard macro, etc., that is in progress. * Sometimes called as a routine, to do general aborting of stuff. */ int ctrlg(int f, int n) { emlwrite(_("Cancelled"), NULL); return (ABORT); }
/* * Quit command. If an argument, always quit. Otherwise confirm if a buffer * has been changed and not written out. Normally bound to "C-X C-C". */ int wquit(int f, int n) { register int s; if(Pmaster){ char *result = NULL; int ret; /* First, make sure there are no outstanding problems */ if(AttachError()){ emlwrite(_("\007Problem with attachments! Fix errors or delete attachments."), NULL); return(FALSE); } #ifdef SPELLER if(Pmaster->always_spell_check) if(spell(0, 0) == -1) sleep(3); /* problem, show error */ #endif /* * if we're not in header, show some of it as we verify sending... */ display_for_send(); packheader(); Pmaster->arm_winch_cleanup++; if((!(Pmaster->pine_flags & MDHDRONLY) || any_header_changes()) && (ret = (*Pmaster->exittest)(Pmaster->headents, redraw_pico_for_callback, Pmaster->allow_flowed_text, &result))){ Pmaster->arm_winch_cleanup--; if(ret == -1){ pico_all_done = COMP_CANCEL; } else{ if(sgarbf) update(); lchange(WFHARD); /* set update flags... */ curwp->w_flag |= WFMODE; /* and modeline so we */ sgarbk = TRUE; /* redraw the keymenu */ pclear(term.t_nrow-2, term.t_nrow); } if(result && *result) emlwrite(result, NULL); } else{ Pmaster->arm_winch_cleanup--; pico_all_done = COMP_EXIT; return(TRUE); } } else{ if (f != FALSE /* Argument forces it. */ || anycb() == FALSE /* All buffers clean. */ /* User says it's OK. */ /* TRANSLATORS: buffer is the in-memory copy of a file */ || (s=mlyesno_utf8(_("Save modified buffer (ANSWERING \"No\" WILL DESTROY CHANGES)"), -1)) == FALSE) { vttidy(); #if defined(USE_TERMCAP) || defined(USE_TERMINFO) || defined(VMS) kbdestroy(kbesc); #endif exit(0); } if(s == TRUE){ if(filewrite(0,1) == TRUE) wquit(1, 0); } else if(s == ABORT){ emlwrite(_("Exit cancelled"), NULL); if(term.t_mrow == 0) curwp->w_flag |= WFHARD; /* cause bottom 3 lines to paint */ } return(s); } return(FALSE); }
/* * Read a line from a file, and store the bytes in the supplied buffer. The * "nbuf" is the length of the buffer. Complain about long lines and lines * at the end of the file that don't have a newline present. Check for I/O * errors too. Return status. * * Translate the line from the user's locale charset to UCS-4. */ int ffgetline(UCS buf[], size_t nbuf, size_t *charsreturned, int msg) { size_t i; UCS ucs; if(charsreturned) *charsreturned = 0; i = 0; while((ucs = read_a_wide_char(g_pico_fio.fp, input_cs)) != CCONV_EOF && ucs != '\n'){ /* * Don't blat the CR should the newline be CRLF and we're * running on a unix system. NOTE: this takes care of itself * under DOS since the non-binary open turns newlines into '\n'. */ if(ucs == '\r'){ if((ucs = read_a_wide_char(g_pico_fio.fp, input_cs)) == CCONV_EOF || ucs == '\n') break; if(i < nbuf-2) /* Bare CR. Insert it and go on... */ buf[i++] = '\r'; /* else, we're up a creek */ } if(i >= nbuf-2){ buf[nbuf - 2] = ucs; /* store last char read */ buf[nbuf - 1] = '\0'; /* and terminate it */ if(charsreturned) *charsreturned = nbuf - 1; if(msg) emlwrite("File has long line", NULL); return FIOLNG; } buf[i++] = ucs; } if(ucs == CCONV_EOF){ if(ferror(g_pico_fio.fp)){ emlwrite("File read error", NULL); if(charsreturned) *charsreturned = i; return FIOERR; } if(i != 0) emlwrite("File doesn't end with newline. Adding one.", NULL); else{ if(charsreturned) *charsreturned = i; return FIOEOF; } } buf[i] = '\0'; if(charsreturned) *charsreturned = i; return FIOSUC; }
/* Ask the user about every occurence of orig pattern and replace it with a repl pattern if the response is affirmative. */ int replace_all(UCS *orig, UCS *repl) { register int status = 0; UCS *b; UCS realpat[NPAT]; char utf8tmp[NPMT]; UCS *promptp; UCS prompt[NPMT]; int wrapt, n = 0; LINE *stop_line = curwp->w_dotp; int stop_offset = curwp->w_doto; EML eml; while (1) if (forscan(&wrapt, orig, stop_line, stop_offset, PTBEG)){ curwp->w_flag |= WFMOVE; /* put cursor back */ update(); (*term.t_rev)(1); get_pat_cases(realpat, orig); pputs(realpat, 1); /* highlight word */ (*term.t_rev)(0); fflush(stdout); snprintf(utf8tmp, NPMT, "Replace \""); b = utf8_to_ucs4_cpystr(utf8tmp); if(b){ ucs4_strncpy(prompt, b, NPMT); prompt[NPMT-1] = '\0'; fs_give((void **) &b); } promptp = &prompt[ucs4_strlen(prompt)]; expandp(orig, promptp, NPMT-(promptp-prompt)); prompt[NPMT-1] = '\0'; promptp += ucs4_strlen(promptp); b = utf8_to_ucs4_cpystr("\" with \""); if(b){ ucs4_strncpy(promptp, b, NPMT-(promptp-prompt)); promptp += ucs4_strlen(promptp); prompt[NPMT-1] = '\0'; fs_give((void **) &b); } expandp(repl, promptp, NPMT-(promptp-prompt)); prompt[NPMT-1] = '\0'; promptp += ucs4_strlen(promptp); if((promptp-prompt) < NPMT-1){ *promptp++ = '\"'; *promptp = '\0'; } prompt[NPMT-1] = '\0'; status = mlyesno(prompt, TRUE); /* ask user */ if (status == TRUE){ n++; chword(realpat, repl); /* replace word */ update(); }else{ chword(realpat, realpat); /* replace word by itself */ update(); if(status == ABORT){ /* if cancelled return */ eml.s = comatose(n); emlwrite("Replace All cancelled after %s changes", &eml); return (ABORT); /* ... else keep looking */ } } } else{ char *utf8; utf8 = ucs4_to_utf8_cpystr(orig); if(utf8){ eml.s = utf8; emlwrite(_("No more matches for \"%s\""), &eml); fs_give((void **) &utf8); } else emlwrite(_("No more matches"), NULL); return (FALSE); } }
/* * pico_writec - write a char into picotext and advance pointers. * Up to calling routines to keep track of total chars * written. * Incoming text (c) is UTF-8 and written chars are UCS-4. * We need to collect up multiple chars to make a single * UCS-4 char, so there needs to be some state between calls. */ int pico_writec(void *w, int c, int flags) { int rv = 0; if(c == '\r') /* ignore CR's */ rv++; /* so fake it */ else if(c == '\n'){ /* insert newlines on LF */ /* * OK, if there are characters on the current line or * dotp is pointing to the delimiter line, insert a newline * No here's the tricky bit; preserve the implicit EOF newline. */ if(lforw(PT(w)->dotp) == PT(w)->linep && PT(w)->dotp != PT(w)->linep){ PT(w)->dotp = PT(w)->linep; PT(w)->doto = 0; } else{ register LINE *lp; if((lp = lalloc(0)) == NULL){ emlwrite("Can't allocate space for more characters",NULL); return(0); } if(PT(w)->dotp == PT(w)->linep){ lforw(lp) = PT(w)->linep; lback(lp) = lback(PT(w)->linep); lforw(lback(lp)) = lback(PT(w)->linep) = lp; } else{ lforw(lp) = lforw(PT(w)->dotp); lback(lp) = PT(w)->dotp; lback(lforw(lp)) = lforw(PT(w)->dotp) = lp; PT(w)->dotp = lp; PT(w)->doto = 0; } } rv++; } else{ if(flags & PICOREADC_NOUCS){ /* * With this flag we're reverting to the old behavior where no * UTF-8 to UCS-4 translation takes place. We assume nothing * about the incoming byte stream. We just write a byte at a * time. So even though it's being stored in PT each * item is really limited to a single octet value. */ rv = geninsert(&PT(w)->dotp, &PT(w)->doto, PT(w)->linep, c, 0, 1, NULL); } else{ static unsigned char cbuf[6]; static unsigned char *cbufp = cbuf; UCS obuf[MAX(MB_LEN_MAX,32)]; int i, outchars = 0; if(cbufp < cbuf+sizeof(cbuf)){ unsigned char *inputp; unsigned long remaining_octets; UCS ucs; *cbufp++ = (unsigned char) c; inputp = cbuf; remaining_octets = (cbufp - cbuf) * sizeof(unsigned char); ucs = (UCS) utf8_get(&inputp, &remaining_octets); switch(ucs){ case U8G_ENDSTRG: /* incomplete character, wait */ case U8G_ENDSTRI: /* incomplete character, wait */ break; default: if(ucs & U8G_ERROR || ucs == UBOGON){ /* * None of these cases is supposed to happen. If it * does happen then the input stream isn't UTF-8 * so something is wrong. Treat each character in the * input buffer as a separate error character and * print a '?' for each. */ for(inputp = cbuf; inputp < cbufp; inputp++) obuf[outchars++] = '?'; cbufp = cbuf; } else{ /* got a character */ if(ucs >= 0x80 && wcellwidth(ucs) < 0){ /* * This happens when we have a UTF-8 character that * we aren't able to print in our locale. For example, * if the locale is setup with the terminal * expecting ISO-8859-1 characters then there are * lots of UTF-8 characters that can't be printed. * Print a '?' instead. This may be the wrong thing to do. What happens if user is just forwarding and doesn't edit. We are going to lose the original value, aren't we? Maybe we do this only when printing to the screen instead. */ obuf[outchars++] = '?'; } else{ /* * A regular ucs character */ obuf[outchars++] = ucs; } /* update the input buffer */ if(inputp >= cbufp) /* this should be the case */ cbufp = cbuf; else{ /* extra chars for some reason? */ unsigned char *q, *newcbufp; newcbufp = (cbufp - inputp) + cbuf; q = cbuf; while(inputp < cbufp) *q++ = *inputp++; cbufp = newcbufp; } } break; } } else{ /* error */ obuf[0] = '?'; outchars = 1; cbufp = cbuf; /* start over */ } /* * Unless we have trouble translating outchars will be * either 1 or 0. It is the UCS-4 character that we converted * the input to. It's an array just so it can hold ? when * there is trouble. */ rv = 1; for(i = 0; rv && i < outchars; i++) if(!geninsert(&PT(w)->dotp, &PT(w)->doto, PT(w)->linep, obuf[i], 0, 1, NULL)) rv = 0; } } /* * Warning, this is no longer number written, because when we have a * multibyte UTF-8 character we won't write anything until we get all * the bytes. */ return((rv) ? 1 : 0); /* return number written */ }
/* * Fill the current paragraph according to the current fill column */ int fillpara(int f, int n) { UCS *qstr, qstr2[NSTRING], c; int quotelevel = -1; REGION addedregion; char action = 'P'; if(curbp->b_mode&MDVIEW){ /* don't allow this command if */ return(rdonly()); /* we are in read only mode */ } else if (fillcol == 0) { /* no fill column set */ mlwrite_utf8("No fill column set", NULL); return(FALSE); } else if(curwp->w_dotp == curbp->b_linep && !curwp->w_markp) /* don't wrap! */ return(FALSE); /* * If there is already a region set, then we may use it * instead of the current paragraph. */ if(curwp->w_markp){ int k, rv; KEYMENU menu_justify[12]; char prompt[100]; for(k = 0; k < 12; k++){ menu_justify[k].name = NULL; KS_OSDATASET(&menu_justify[k], KS_NONE); } menu_justify[1].name = "R"; menu_justify[1].label = "[" N_("Region") "]"; menu_justify[6].name = "^C"; menu_justify[6].label = N_("Cancel"); menu_justify[7].name = "P"; menu_justify[7].label = N_("Paragraph"); menu_justify[2].name = "Q"; menu_justify[2].label = N_("Quotelevel"); wkeyhelp(menu_justify); /* paint menu */ sgarbk = TRUE; if(Pmaster && curwp) curwp->w_flag |= WFMODE; strncpy(prompt, "justify Region, Paragraph; or fix Quotelevel ? ", sizeof(prompt)); prompt[sizeof(prompt)-1] = '\0'; mlwrite_utf8(prompt, NULL); (*term.t_rev)(1); rv = -1; while(1){ switch(c = GetKey()){ case (CTRL|'C') : /* Bail out! */ case F2 : pputs_utf8(_("ABORT"), 1); rv = ABORT; emlwrite("", NULL); break; case (CTRL|'M') : /* default */ case 'r' : case 'R' : case F3 : pputs_utf8(_("Region"), 1); rv = 'R'; break; case 'p' : case 'P' : case F7 : pputs_utf8(_("Paragraph"), 1); rv = 'P'; break; case 'q' : case 'Q' : case F8 : case '0' : case '1' : case '2' : case '3' : case '4' : case '5' : case '6' : case '7' : case '8' : case '9' : pputs_utf8(_("Quotelevel"), 1); while(rv == -1){ switch(c){ case 'q' : case 'Q' : case F8 : {char num[20]; num[0] = '\0'; switch(mlreplyd_utf8("Quote Level ? ", num, sizeof(num), QNORML, NULL)){ case TRUE: if(isdigit(num[0])){ quotelevel = atoi(num); if(quotelevel < 0){ emlwrite("Quote Level cannot be negative", NULL); sleep(3); } else if(quotelevel > 20){ emlwrite("Quote Level should be less than 20", NULL); rv = ABORT; } else{ rv = 'Q'; } } else if(num[0]){ emlwrite("Quote Level should be a number", NULL); sleep(3); } break; case HELPCH: emlwrite("Enter the number of quotes you want before the text", NULL); sleep(3); break; default: emlwrite("Quote Level is a number", NULL); rv = ABORT; break; } } break; case '0' : case '1' : case '2' : case '3' : case '4' : case '5' : case '6' : case '7' : case '8' : case '9' : rv = 'Q'; quotelevel = (int) (c - '0'); break; } } break; case (CTRL|'G') : if(term.t_mrow == 0 && km_popped == 0){ movecursor(term.t_nrow-2, 0); peeol(); term.t_mrow = 2; (*term.t_rev)(0); wkeyhelp(menu_justify); mlwrite_utf8(prompt, NULL); (*term.t_rev)(1); sgarbk = TRUE; /* mark menu dirty */ km_popped++; break; } /* else fall through */ default: (*term.t_beep)(); case NODATA : break; } (*term.t_flush)(); if(rv != -1){ (*term.t_rev)(0); if(km_popped){ term.t_mrow = 0; movecursor(term.t_nrow, 0); peeol(); sgarbf = 1; km_popped = 0; } action = rv; break; } } if(action != ABORT) emlwrite("", NULL); } if(action == 'R' && curwp->w_markp){ /* let yank() know that it may be restoring a paragraph */ thisflag |= CFFILL; if(!Pmaster) sgarbk = TRUE; curwp->w_flag |= WFMODE; swap_mark_and_dot_if_mark_comes_first(); /* determine if we're justifying quoted text or not */ qstr = (glo_quote_str && quote_match(glo_quote_str, curwp->w_doto > 0 ? curwp->w_dotp->l_fp : curwp->w_dotp, qstr2, NSTRING) && *qstr2) ? qstr2 : NULL; /* * Fillregion moves dot to the end of the filled region. */ if(!fillregion(qstr, &addedregion)) return(FALSE); set_last_region_added(&addedregion); } else if(action == 'P'){ /* * Justfiy the current paragraph. */ if(curwp->w_markp) /* clear mark if already set */ setmark(0,0); if(gotoeop(FALSE, 1) == FALSE) return(FALSE); /* determine if we're justifying quoted text or not */ qstr = (glo_quote_str && quote_match(glo_quote_str, curwp->w_dotp, qstr2, NSTRING) && *qstr2) ? qstr2 : NULL; setmark(0,0); /* mark last line of para */ /* jump back to the beginning of the paragraph */ gotobop(FALSE, 1); /* let yank() know that it may be restoring a paragraph */ thisflag |= (CFFILL | CFFLPA); if(!Pmaster) sgarbk = TRUE; curwp->w_flag |= WFMODE; curwp->w_doto = 0; /* start region at beginning of line */ /* * Fillregion moves dot to the end of the filled region. */ if(!fillregion(qstr, &addedregion)) return(FALSE); set_last_region_added(&addedregion); /* Leave cursor on first char of first line after justified region */ curwp->w_dotp = lforw(curwp->w_dotp); curwp->w_doto = 0; if(curwp->w_markp) setmark(0,0); /* clear mark */ } else if(action == 'Q'){ /* let yank() know that it may be restoring a paragraph */ thisflag |= CFFILL; if(!Pmaster) sgarbk = TRUE; curwp->w_flag |= WFHARD; swap_mark_and_dot_if_mark_comes_first(); if(!setquotelevelinregion(quotelevel, &addedregion)) return(FALSE); set_last_region_added(&addedregion); } else{ /* abort */ } return(TRUE); }
int forwsearch(int f, int n) { int status; int wrapt = FALSE, wrapt2 = FALSE; int repl_mode = FALSE; UCS defpat[NPAT]; int search = FALSE; EML eml; /* resolve the repeat count */ if (n == 0) n = 1; if (n < 1) /* search backwards */ FWS_RETURN(0); defpat[0] = '\0'; /* ask the user for the text of a pattern */ while(1){ if (gmode & MDREPLACE) status = srpat("Search", defpat, NPAT, repl_mode); else status = readpattern("Search", TRUE); switch(status){ case TRUE: /* user typed something */ search = TRUE; break; case HELPCH: /* help requested */ if(Pmaster){ VARS_TO_SAVE *saved_state; saved_state = save_pico_state(); (*Pmaster->helper)(Pmaster->search_help, _("Help for Searching"), 1); if(saved_state){ restore_pico_state(saved_state); free_pico_state(saved_state); } } else pico_help(SearchHelpText, _("Help for Searching"), 1); case (CTRL|'L'): /* redraw requested */ pico_refresh(FALSE, 1); update(); break; case (CTRL|'V'): gotoeob(0, 1); mlerase(); FWS_RETURN(TRUE); case (CTRL|'Y'): gotobob(0, 1); mlerase(); FWS_RETURN(TRUE); case (CTRL|'T') : switch(status = readnumpat(_("Search to Line Number : "))){ case -1 : emlwrite(_("Search to Line Number Cancelled"), NULL); FWS_RETURN(FALSE); case 0 : emlwrite(_("Line number must be greater than zero"), NULL); FWS_RETURN(FALSE); case -2 : emlwrite(_("Line number must contain only digits"), NULL); FWS_RETURN(FALSE); case -3 : continue; default : gotoline(0, status); mlerase(); FWS_RETURN(TRUE); } break; case (CTRL|'W'): { LINE *linep = curwp->w_dotp; int offset = curwp->w_doto; gotobop(0, 1); gotobol(0, 1); /* * if we're asked to backup and we're already * */ if((lastflag & CFSRCH) && linep == curwp->w_dotp && offset == curwp->w_doto && !(offset == 0 && lback(linep) == curbp->b_linep)){ backchar(0, 1); gotobop(0, 1); gotobol(0, 1); } } mlerase(); FWS_RETURN(TRUE); case (CTRL|'O'): if(curwp->w_dotp != curbp->b_linep){ gotoeop(0, 1); forwchar(0, 1); } mlerase(); FWS_RETURN(TRUE); case (CTRL|'U'): fillbuf(0, 1); mlerase(); FWS_RETURN(TRUE); case (CTRL|'R'): /* toggle replacement option */ repl_mode = !repl_mode; break; default: if(status == ABORT) emlwrite(_("Search Cancelled"), NULL); else mlerase(); FWS_RETURN(FALSE); } /* replace option is disabled */ if (!(gmode & MDREPLACE)){ ucs4_strncpy(defpat, pat, NPAT); defpat[NPAT-1] = '\0'; break; } else if (search){ /* search now */ ucs4_strncpy(pat, defpat, NPAT); /* remember this search for the future */ pat[NPAT-1] = '\0'; break; } } /* * This code is kind of dumb. What I want is successive C-W 's to * move dot to successive occurences of the pattern. So, if dot is * already sitting at the beginning of the pattern, then we'll move * forward a char before beginning the search. We'll let the * automatic wrapping handle putting the dot back in the right * place... */ status = 0; /* using "status" as int temporarily! */ while(1){ if(defpat[status] == '\0'){ forwchar(0, 1); break; /* find next occurence! */ } if(status + curwp->w_doto >= llength(curwp->w_dotp) || !eq(defpat[status],lgetc(curwp->w_dotp, curwp->w_doto + status).c)) break; /* do nothing! */ status++; } /* search for the pattern */ while (n-- > 0) { if((status = forscan(&wrapt,defpat,NULL,0,PTBEG)) == FALSE) break; } /* and complain if not there */ if (status == FALSE){ char *utf8; UCS x[1]; x[0] = '\0'; utf8 = ucs4_to_utf8_cpystr(defpat ? defpat : x); /* TRANSLATORS: reporting the result of a failed search */ eml.s = utf8; emlwrite(_("\"%s\" not found"), &eml); if(utf8) fs_give((void **) &utf8); } else if((gmode & MDREPLACE) && repl_mode == TRUE){ status = replace_pat(defpat, &wrapt2); /* replace pattern */ if (wrapt == TRUE || wrapt2 == TRUE){ eml.s = (status == ABORT) ? "cancelled but wrapped" : "Wrapped"; emlwrite("Replacement %s", &eml); } } else if(wrapt == TRUE){ emlwrite("Search Wrapped", NULL); } else if(status == TRUE){ emlwrite("", NULL); } FWS_RETURN(status); }