int utag(BW *bw) { BW *pbw; pbw = wmkpw(bw->parent, joe_gettext(_("Tag search: ")), &taghist, dotag, NULL, NULL, tag_cmplt, NULL, NULL, locale_map, 0); if (pbw && joe_isalnum_(bw->b->o.charmap,brch(bw->cursor))) { P *p = pdup(bw->cursor, "utag"); P *q = pdup(p, "utag"); int c; while (joe_isalnum_(bw->b->o.charmap,(c = prgetc(p)))) /* do nothing */; if (c != NO_MORE_DATA) { pgetc(p); } pset(q, p); while (joe_isalnum_(bw->b->o.charmap,(c = pgetc(q)))) /* do nothing */; if (c != NO_MORE_DATA) { prgetc(q); } binsb(pbw->cursor, bcpy(p, q)); pset(pbw->cursor, pbw->b->eof); pbw->cursor->xcol = piscol(pbw->cursor); prm(p); prm(q); } if (pbw) { return 0; } else { return -1; } }
static long parserr(B *b) { P *p = pdup(b->bof, USTR "parserr"); P *q = pdup(p, USTR "parserr"); long nerrs = 0; if (b->scratch && b->name[0] == '*') { /* "* Build Log *", "* Grep Log *" */ b->changed = 0; } freeall(); if (errbuf != b) { if (errp != NULL) { prm(errp); errp = NULL; } errbuf = b; } do { unsigned char *s; pset(q, p); p_goto_eol(p); s = brvs(q, (int) (p->byte - q->byte)); if (s) { nerrs += parseit(b->o.charmap, s, q->line, (b->parseone ? b->parseone : parseone)); vsrm(s); } } while (pgetc(p) != NO_MORE_DATA); prm(p); prm(q); return nerrs; }
int ujump(BW *bw) { int rtn = -1; P *p = pdup(bw->cursor, USTR "ujump"); P *q = pdup(p, USTR "ujump"); unsigned char *s; p_goto_bol(p); p_goto_eol(q); s = brvs(p, (int) (q->byte - p->byte)); prm(p); prm(q); if (s) { unsigned char *name = NULL; long line = -1; if (bw->b->parseone) bw->b->parseone(bw->b->o.charmap,s,&name,&line); else parseone(bw->b->o.charmap,s,&name,&line); if (name && line != -1) { ERROR *p = srcherr(name, line); if (p != NULL) { errptr = p; setline(errbuf, errptr->src); name = p->file; line = p->line; } uprevvw((BASE *)bw); rtn = jump_to_file_line(maint->curwin->object, name, line, NULL /* errptr->msg */); vsrm(name); } vsrm(s); } return rtn; }
static void cdata(B *b, unsigned char *dat, int siz) { P *q = pdup(b->eof, USTR "cdata"); P *r = pdup(b->eof, USTR "cdata"); long byte = q->byte; unsigned char bf[1024]; int x, y; for (x = y = 0; x != siz; ++x) { if (dat[x] == 13 || dat[x] == 0) { ; } else if (dat[x] == 8 || dat[x] == 127) { if (y) { --y; } else { pset(q, r); prgetc(q); bdel(q, r); --byte; } } else if (dat[x] == 7) { ttputc(7); } else { bf[y++] = dat[x]; } } if (y) { binsm(r, bf, y); } prm(r); prm(q); cfollow(b,byte); undomark(); }
static void cdata(B *b, unsigned char *dat, int siz) { if (b->vt) { /* ANSI terminal emulator */ MACRO *m; do { cready(b, b->vt->vtcur->byte); m = vt_data(b->vt, &dat, &siz); cfollow(b, b->vt, b->vt->vtcur->byte); undomark(); if (m) { /* only do this if cursor is on window */ if ((maint->curwin->watom->what & TYPETW) && ((BW *)maint->curwin->object)->b == b) { exmacro(m, 1); edupd(1); } rmmacro(m); } } while (m); } else { /* Dumb terminal */ P *q = pdup(b->eof, USTR "cdata"); P *r = pdup(b->eof, USTR "cdata"); off_t byte = q->byte; unsigned char bf[1024]; int x, y; cready(b, byte); for (x = y = 0; x != siz; ++x) { if (dat[x] == 13 || dat[x] == 0) { ; } else if (dat[x] == 8 || dat[x] == 127) { if (y) { --y; } else { pset(q, r); prgetc(q); bdel(q, r); --byte; } } else if (dat[x] == 7) { ttputc(7); } else { bf[y++] = dat[x]; } } if (y) { binsm(r, bf, y); } prm(r); prm(q); cfollow(b, NULL, b->eof->byte); undomark(); } }
void promote_history(B *hist, long line) { P *q = pdup(hist->bof, USTR "promote_history"); P *r; P *t; pline(q, line); r = pdup(q, USTR "promote_history"); pnextl(r); t = pdup(hist->eof, USTR "promote_history"); binsb(t, bcpy(q, r)); bdel(q, r); prm(q); prm(r); prm(t); }
static void doundo(BW *bw, UNDOREC *ptr) { dostaupd = 1; if (ptr->del) { if (ptr->len < SMALL) binsm(bw->cursor, ptr->small, (int) ptr->len); else { B *b = ptr->big; bonline(b); binsb(bw->cursor, bcpy(b->bof, b->eof)); boffline(b); } } else { P *q = pdup(bw->cursor, "doundo"); pfwrd(q, ptr->len); bdel(bw->cursor, q); prm(q); } if (bw->b->changed && !ptr->changed) bw_unlock(bw); bw->b->changed = ptr->changed; }
static long prefix(BW *bw, P *p,int up) { long len; P *q = pdup(p, USTR "prefix"); p_goto_bol(q); while (cpara(bw, brch(q))) pgetc(q); while (!pisbol(q)) { /* int c; */ if (!joe_isblank(p->b->o.charmap, ( /* c = */ prgetc(q)))) { /* if (up && (c == '*' || c == '-')) { if (!pisbol(q)) { c = prgetc(q); pgetc(q); if (c == ' ' || c == '\t') goto skip; } else goto skip; } pgetc(q); */ break; /* skip:; */ } } len = piscol(q); prm(q); return len; }
P *pbop(BW *bw, P *p) { long indent; long prelen; P *last; p_goto_bol(p); indent = nindent(bw, p, 0); prelen = prefix(bw, p, 0); last = pdup(p, USTR "pbop"); while (!pisbof(p) && (!within || !markb || p->byte > markb->byte)) { long ind; long len; pprevl(p); p_goto_bol(p); ind = nindent(bw, p, 0); len = prefix(bw, p, 0); if (pisnpara(bw, p) || len != prelen) { pset(p, last); break; } if (ind > indent) { /* int ok = 1; P *q = pdup(p, USTR "pbop"); if (pprevl(q)) { p_goto_bol(q); if (nindent(bw, q, 0) == ind) ok = 0; } prm(q); if (!ok) pnextl(p); */ break; } if (ind < indent) { pset(p, last); break; /* if (pisbof(p)) { break; } pprevl(p); p_goto_bol(p); if (pisnpara(bw, p)) { pnextl(p); break; } else { pnextl(p); pnextl(p); break; } */ } pset(last, p); } prm(last); return p; }
void u_log_setup(BW *bw) { B *b = bw->b; if (!b->hist) { b->hist = bmk(NULL); b->hist_p = pdup(b->hist->bof); } }
void append_history(B *hist,unsigned char *s,int len) { P *q = pdup(hist->eof, USTR "append_history"); binsm(q, s, len); p_goto_eof(q); binsc(q, '\n'); prm(q); }
static char *get_context(BW *bw) { P *p = pdup(bw->cursor, "get_context"); static char buf1[stdsiz]; int i, j, spc; buf1[0] = 0; /* Find first line with 0 indentation which is not a comment line */ do { p_goto_bol(p); if (!pisindent(p) && !pisblank(p)) { /* next: */ brzs(p,stdbuf,stdsiz/8); /* To avoid buffer overruns with my_iconv */ /* Ignore comment and block structuring lines */ if (!(stdbuf[0]=='{' || (stdbuf[0]=='/' && stdbuf[1]=='*') || stdbuf[0]=='\f' || (stdbuf[0]=='/' && stdbuf[1]=='/') || stdbuf[0]=='#' || (stdbuf[0]=='b' && stdbuf[1]=='e' && stdbuf[2]=='g' && stdbuf[3]=='i' && stdbuf[4]=='n') || (stdbuf[0]=='B' && stdbuf[1]=='E' && stdbuf[2]=='G' && stdbuf[3]=='I' && stdbuf[4]=='N') || (stdbuf[0]=='-' && stdbuf[1]=='-') || stdbuf[0]==';')) { /* zlcpy(buf1, SIZEOF(buf1), stdbuf); */ /* replace tabs to spaces and remove adjoining spaces */ for (i=0,j=0,spc=0; stdbuf[i]; i++) { if (stdbuf[i]=='\t' || stdbuf[i]==' ') { if (spc) continue; spc = 1; } else spc = 0; if (stdbuf[i]=='\t') buf1[j++] = ' '; else if (stdbuf[i]=='\\') { buf1[j++] = '\\'; buf1[j++] = '\\'; } else buf1[j++] = stdbuf[i]; } buf1[j]= '\0'; /* Uncomment to get the last line instead of the first line (see above) if (pprevl(p)) { p_goto_bol(p); if (!pisindent(p) && !pisblank(p)) goto next; } */ break; } } } while (!buf1[0] && pprevl(p)); prm(p); return buf1; }
int ucenter(BW *bw) { P *p = bw->cursor, *q; long endcol, begcol, x; int c; p_goto_eol(p); while (joe_isblank(bw->b->o.charmap, (c = prgetc(p)))) /* do nothing */; if (c == '\n') { pgetc(p); goto done; } if (c == NO_MORE_DATA) goto done; pgetc(p); endcol = piscol(p); p_goto_bol(p); while (joe_isblank(bw->b->o.charmap, (c = pgetc(p)))) /* do nothing */; if (c == '\n') { prgetc(p); goto done; } if (c == NO_MORE_DATA) goto done; prgetc(p); begcol = piscol(p); if (endcol - begcol > bw->o.rmargin + bw->o.lmargin) goto done; q = pdup(p, USTR "ucenter"); p_goto_bol(q); bdel(q, p); prm(q); for (x = 0; x != (bw->o.lmargin + bw->o.rmargin) / 2 - (endcol - begcol) / 2; ++x) binsc(p, ' '); done: if (!pnextl(p)) { binsc(p, '\n'); pgetc(p); return -1; } else return 0; }
static int pisnpara(BW *bw, P *p) { P *q; int c; q = pdup(p, USTR "pisnpara"); p_goto_bol(q); while (cpara(bw, c = pgetc(q))) /* do nothing */; prm(q); if (c == '.' || c == '\r' || c == '\n') return 1; else return 0; }
int uyankpop(BW *bw) { if (bw->b == yankbuf && bw->cursor->byte == yankwhere) { P *q; UNDOREC *ptr = yanked.link.prev; deque(UNDOREC, link, &yanked); enqueb(UNDOREC, link, ptr, &yanked); q = pdup(bw->cursor, "uyankpop"); pbkwd(q, ptr->len); inyank = 1; bdel(q, bw->cursor); inyank = 0; prm(q); return uyank(bw); } else return uyank(bw); }
static void iappend(BW *bw, struct isrch *isrch, char *s, ptrdiff_t len) { /* Append text and search */ /* Append char and search */ IREC *i = alirec(); SRCH *srch; i->what = len; i->disp = bw->cursor->byte; isrch->pattern = vsncpy(sv(isrch->pattern), s, len); if (!qempty(IREC, link, &isrch->irecs)) { pgoto(bw->cursor, isrch->irecs.link.prev->start); if (globalsrch) globalsrch->wrap_flag = isrch->irecs.link.prev->wrap_flag; } i->start = bw->cursor->byte; if (!globalsrch) srch = mksrch(NULL,NULL,opt_icase,isrch->dir,-1,0,0,0,0); else { srch = globalsrch; globalsrch = 0; } srch->addr = bw->cursor->byte; if (!srch->wrap_p || srch->wrap_p->b!=bw->b) { prm(srch->wrap_p); srch->wrap_p = pdup(bw->cursor, "iappend"); srch->wrap_p->owner = &srch->wrap_p; srch->wrap_flag = 0; } i->wrap_flag = srch->wrap_flag; setpat(srch, vsncpy(NULL, 0, isrch->pattern, sLen(isrch->pattern))); srch->backwards = isrch->dir; if (dopfnext(bw, srch, NULL)) { if(joe_beep) ttputc(7); } enqueb(IREC, link, &isrch->irecs, i); }
static long nindent(BW *bw, P *p, int first) { P *q = pdup(p, USTR "nindent"); long col; int c; p_goto_bol(q); do { col = q->col; } while (cpara(bw, (c = pgetc(q)))); if (first && (c == '-' || c == '*')) { c = pgetc(q); if (c == ' ') { col = q->col; } } prm(q); return col; }
int ubop(BW *bw) { P *q = pdup(bw->cursor, USTR "ubop"); up: while (pisnpara(bw, q) && !pisbof(q) && (!within || !markb || q->byte > markb->byte)) pprevl(q); pbop(bw, q); if (q->byte != bw->cursor->byte) { pset(bw->cursor, q); prm(q); return 0; } else if (!pisbof(q)) { prgetc(q); goto up; } else { prm(q); return -1; } }
int ueop(BW *bw) { P *q = pdup(bw->cursor, USTR "ueop"); up: while (pisnpara(bw, q) && !piseof(q)) pnextl(q); pbop(bw, q); peop(bw, q); if (q->byte != bw->cursor->byte) { pset(bw->cursor, q); prm(q); return 0; } else if (!piseof(q)) { pnextl(q); goto up; } else { prm(q); return -1; } }
static int jump_to_error(BW *bw, ERROR *new_errptr) { W *w; if (new_errptr == NULL) { msgnw(bw->parent, joe_gettext(_("No more errors"))); return -1; } errptr = new_errptr; /* This moves the cursor to the beginning of line errptr->src, * and it also scrolls the window. */ setline(errbuf, errptr->src); /* Set errp to the beginning of line in errbuf. */ w = maint->curwin; do { if (w->watom->what == TYPETW) { BW *bw2 = w->object; if (bw2->b == errbuf) { if (errp != NULL) { pset(errp, bw2->cursor); } else { errp = pdup(bw2->cursor, USTR "errp"); } pgetc(errp); /* Move to next char to let the user type above. */ if (piseol(errp)) /* Undo the move for empty lines. */ p_goto_bol(errp); break; } } } while ((w = w->link.next) != maint->curwin); if (bw->b == errbuf) { uprevvw((BASE *)bw); bw = (BW*) maint->curwin->object; } return jump_to_file_line(bw,errptr->file,errptr->line,NULL /* errptr->msg */); }
/* When called with c==-1, it just creates the prompt */ static int itype(W *w, int c, void *obj, int *notify) { IREC *i; int omid; BW *bw; struct isrch *isrch = (struct isrch *)obj; WIND_BW(bw,w); if (isrch->quote) { goto in; } if (c == 8 || c == 127) { /* Backup */ if ((i = isrch->irecs.link.prev) != &isrch->irecs) { pgoto(bw->cursor, i->disp); if (globalsrch) globalsrch->wrap_flag = i->wrap_flag; omid = opt_mid; opt_mid = 1; dofollows(); opt_mid = omid; isrch->pattern = vstrunc(isrch->pattern, sLEN(isrch->pattern) - i->what); frirec(deque_f(IREC, link, i)); } else { if(joe_beep) ttputc(7); } } else if (c == 'Q' - '@' /* || c == '`' */) { isrch->quote = 1; } else if (c == 'S' - '@' || c == '\\' - '@' || c == 'L' - '@' || c == 'R' - '@') { /* Repeat */ if (c == 'R' - '@') { isrch->dir = 1; } else { isrch->dir = 0; } if (qempty(IREC, link, &isrch->irecs)) { if (lastpat && lastpat[0]) { iappend(bw, isrch, sv(lastpat)); } } else { SRCH *srch; i = alirec(); i->disp = i->start = bw->cursor->byte; i->what = 0; if (!globalsrch) srch = mksrch(NULL,NULL,opt_icase,isrch->dir,-1,0,0,0,0); else { srch = globalsrch; globalsrch = 0; } srch->addr = bw->cursor->byte; if (!srch->wrap_p || srch->wrap_p->b!=bw->b) { prm(srch->wrap_p); srch->wrap_p = pdup(bw->cursor, "itype"); srch->wrap_p->owner = &srch->wrap_p; srch->wrap_flag = 0; } i->wrap_flag = srch->wrap_flag; setpat(srch, vsncpy(NULL, 0, isrch->pattern, sLen(isrch->pattern))); srch->backwards = isrch->dir; if (dopfnext(bw, srch, NULL)) { if(joe_beep) ttputc(7); frirec(i); } else { enqueb(IREC, link, &isrch->irecs, i); } } } else if (c >= 0 && c < 32) { /* Done when a control character is received */ nungetc(c); if (notify) { *notify = 1; } smode = 2; if (lastisrch) { lastpat = vstrunc(lastpat, 0); lastpat = vsncpy(lastpat, 0, lastisrch->pattern, sLen(lastisrch->pattern)); rmisrch(lastisrch); } lastisrch = isrch; return 0; } else if (c != -1) { char buf[16]; ptrdiff_t buf_len; /* Search */ in: if (bw->b->o.charmap->type) { buf_len = utf8_encode(buf, c); } else { buf[0] = TO_CHAR_OK(from_uni(bw->b->o.charmap, c)); buf_len = 1; } isrch->quote = 0; iappend(bw, isrch, buf, buf_len); } omid = opt_mid; opt_mid = 1; bw->cursor->xcol = piscol(bw->cursor); dofollows(); opt_mid = omid; isrch->prompt = vstrunc(isrch->prompt, isrch->ofst); if (locale_map->type && !bw->b->o.charmap->type) { /* Translate bytes to utf-8 */ char buf[16]; int x; for (x=0; x!=sLEN(isrch->pattern); ++x) { int tc = to_uni(bw->b->o.charmap, isrch->pattern[x]); utf8_encode(buf, tc); isrch->prompt = vsncpy(sv(isrch->prompt),sz(buf)); } } else if (!locale_map->type && bw->b->o.charmap->type) { /* Translate utf-8 to bytes */ const char *p = isrch->pattern; ptrdiff_t len = sLEN(isrch->pattern); while (len) { int tc = utf8_decode_fwrd(&p, &len); if (tc >= 0) { tc = from_uni(locale_map, tc); isrch->prompt = vsadd(isrch->prompt, TO_CHAR_OK(tc)); } } } else { /* FIXME: translate when charmaps do not match */ isrch->prompt = vsncpy(sv(isrch->prompt),sv(isrch->pattern)); } if (mkqwnsr(bw->parent, sv(isrch->prompt), itype, iabrt, isrch, notify)) { return 0; } else { rmisrch(isrch); return -1; } }
int main(int argc, char **real_argv, char **envv) { CAP *cap; unsigned char **argv = (unsigned char **)real_argv; struct stat sbuf; unsigned char *s; unsigned char *t; long time_rc; unsigned char *run; #ifdef __MSDOS__ unsigned char *rundir; #endif SCRN *n; int opened = 0; int omid; int backopt; int c; init_bufs(); for (c = 1; argv[c] != NULL; ++c) { if (0 == zcmp((unsigned char*)argv[c], USTR "-nosys")) { read_sys_configs = 0; } else if (0 == zcmp((unsigned char*)argv[c], USTR "-nouser")) { read_user_configs = 0; } else { argv[--c] = argv[0]; argv += c; argc -= c; break; } } if (read_user_configs) { s = (unsigned char *)getenv("HOME"); if (s) { s = vsncpy(NULL, 0, sz(s)); s = vsncpy(sv(s), sc("/.joe-p37/only.rc")); if (!stat((char*)s, &sbuf)) read_sys_configs = 0; vsrm(s); } } joe_locale(); mainenv = (unsigned char **)envv; #ifdef __MSDOS__ _fmode = O_BINARY; zcpy(stdbuf, argv[0]); joesep(stdbuf); run = namprt(stdbuf); rundir = dirprt(stdbuf); for (c = 0; run[c]; ++c) if (run[c] == '.') { run = vstrunc(run, c); break; } #else run = namprt(argv[0]); #endif env_lines = 0; if ((s = (unsigned char *)getenv("LINES")) != NULL && sscanf((char *)s, "%d", &env_lines) != 1) env_lines = 0; env_columns = 0; if ((s = (unsigned char *)getenv("COLUMNS")) != NULL && sscanf((char *)s, "%d", &env_columns) != 1) env_columns = 0; if ((s = (unsigned char *)getenv("BAUD")) != NULL) sscanf((char *)s, "%u", (unsigned *)&Baud); if (getenv("DOPADDING")) dopadding = 1; if (getenv("NOXON")) noxon = 1; if ((s = (unsigned char *)getenv("JOETERM")) != NULL) joeterm = s; #ifndef __MSDOS__ if (!(cap = my_getcap(NULL, 9600, NULL, NULL))) { fprintf(stderr, (char *)joe_gettext(_("Couldn't load termcap/terminfo entry\n"))); return 1; } #endif #ifdef __MSDOS__ s = vsncpy(NULL, 0, sv(run)); s = vsncpy(sv(s), sc("rc")); c = procrc(cap, s); if (c == 0) goto donerc; if (c == 1) { unsigned char buf[8]; fprintf(stderr, (char *)joe_gettext(_("There were errors in '%s'. Use it anyway?")), s); fflush(stderr); if (NULL == fgets(buf, 8, stdin) || yn_checks(yes_key, buf)) goto donerc; } vsrm(s); s = vsncpy(NULL, 0, sv(rundir)); s = vsncpy(sv(s), sv(run)); s = vsncpy(sv(s), sc("rc")); c = procrc(cap, s); if (c != 0 && c != 1) { /* If built-in *fancyjoerc not present, process builtin *joerc, * which is always present. */ s = vstrunc(s, 0); s = vsncpy(sv(s), sc("*joerc")); c = procrc(cap, s); } if (c == 0) goto donerc; if (c == 1) { unsigned char buf[8]; fprintf(stderr, (char *)joe_gettext(_("There were errors in '%s'. Use it anyway?")), s); fflush(stderr); if (NULL == fgets(buf, 8, stdin) || yn_checks(yes_key, buf)) goto donerc; } #else /* Name of system joerc file. Try to find one with matching language... */ /* Try full language: like joerc.de_DE */ time_rc = 0; if (!read_sys_configs) { t = NULL; goto skip_joerc; } t = vsncpy(NULL, 0, sc(JOERC)); t = vsncpy(sv(t), sv(run)); t = vsncpy(sv(t), sc("rc.")); t = vsncpy(sv(t), sz(locale_msgs)); if (!stat((char *)t,&sbuf)) time_rc = sbuf.st_mtime; else { /* Try generic language: like joerc.de */ if (locale_msgs[0] && locale_msgs[1] && locale_msgs[2]=='_') { vsrm(t); t = vsncpy(NULL, 0, sc(JOERC)); t = vsncpy(sv(t), sv(run)); t = vsncpy(sv(t), sc("rc.")); t = vsncpy(sv(t), locale_msgs, 2); if (!stat((char *)t,&sbuf)) time_rc = sbuf.st_mtime; else goto nope; } else { nope: vsrm(t); /* Try Joe's bad english */ t = vsncpy(NULL, 0, sc(JOERC)); t = vsncpy(sv(t), sv(run)); t = vsncpy(sv(t), sc("rc")); if (!stat((char *)t,&sbuf)) time_rc = sbuf.st_mtime; else { /* If built-in *fancyjoerc not present, process builtin *joerc, * which is always present. */ t = vstrunc(s, 0); t = vsncpy(sv(s), sc("*joerc")); time_rc = stat((char *)t,&sbuf) ? 0 : sbuf.st_mtime; } } } skip_joerc: /* User's joerc file */ s = (unsigned char *)getenv("HOME"); if (s && !read_sys_configs) { if (read_user_configs) { /* TODO(pts): Don't read /dev/null */ s = vsncpy(NULL, 0, sz(s)); s = vsncpy(sv(s), sc("/.joe-p37/rc")); } else { s = vsncpy(NULL, 0, sc("/dev/null/missing")); } goto process_user_rc; } else if (!read_user_configs) { s = vsncpy(NULL, 0, sc("/dev/null/missing")); goto process_user_rc; } else if (s) { unsigned char buf[8]; s = vsncpy(NULL, 0, sz(s)); s = vsncpy(sv(s), sc("/.")); s = vsncpy(sv(s), sv(run)); s = vsncpy(sv(s), sc("rc")); if (!stat((char *)s,&sbuf)) { if (sbuf.st_mtime < time_rc) { fprintf(stderr,(char *)joe_gettext(_("Warning: %s is newer than your %s.\n")),t,s); fprintf(stderr,(char *)joe_gettext(_("You should update or delete %s\n")),s); fprintf(stderr,(char *)joe_gettext(_("Hit enter to continue with %s ")),t); fflush(stderr); (void)!fgets((char *)buf, 8, stdin); goto use_sys; } } process_user_rc: c = procrc(cap, s); if (c == 0) { vsrm(t); goto donerc; } if (c == 1) { fprintf(stderr,(char *)joe_gettext(_("There were errors in '%s'. Use it anyway (y,n)? ")), s); fflush(stderr); if (NULL == fgets((char *)buf, 8, stdin) || ynchecks(yes_key, buf)) { vsrm(t); goto donerc; } } } use_sys: vsrm(s); s = t; c = s ? procrc(cap, s) : -1; if (c == 0) goto donerc; if (c == 1) { unsigned char buf[8]; fprintf(stderr,(char *)joe_gettext(_("There were errors in '%s'. Use it anyway (y,n)? ")), s); fflush(stderr); if (NULL == fgets((char *)buf, 8, stdin) || ynchecks(yes_key, buf)) goto donerc; } /* Try built-in *fancyjoerc, e.g. "*joe-p37rc" */ vsrm(s); s = vsncpy(NULL, 0, sc("*")); s = vsncpy(sv(s), sv(run)); s = vsncpy(sv(s), sc("rc")); c = procrc(cap, s); if (c != 0 && c != 1) { /* If built-in *fancyjoerc not present, process builtin "*joerc", * which is always present. */ s = vstrunc(s, 0); s = vsncpy(sv(s), sc("*joerc")); c = procrc(cap, s); } if (c == 0) goto donerc; if (c == 1) { unsigned char buf[8]; fprintf(stderr,(char *)joe_gettext(_("There were errors in '%s'. Use it anyway (y,n)? ")), s); fflush(stderr); if (NULL == fgets((char *)buf, 8, stdin) || ynchecks(yes_key, buf)) goto donerc; } #endif fprintf(stderr,(char *)joe_gettext(_("Couldn't open '%s'\n")), s); vsrm(s); return 1; donerc: vsrm(s); if (validate_rc()) { fprintf(stderr,(char *)joe_gettext(_("rc file has no :main key binding section or no bindings. Bye.\n"))); return 1; } if (!isatty(fileno(stdin))) idleout = 0; for (c = 1; argv[c]; ++c) { if (argv[c][0] == '-') { if (argv[c][1]) switch (glopt(argv[c] + 1, argv[c + 1], NULL, 1)) { case 0: fprintf(stderr,(char *)joe_gettext(_("Unknown option '%s'\n")), argv[c]); break; case 1: break; case 2: ++c; break; } else idleout = 0; } } if (!dspasis) { /* Open all files as ASCII by default if `joe --asis' is specified. This is to avoid writing control characters to the UTF-8 terminal. */ fdefault.charmap = pdefault.charmap = find_charmap(USTR "ascii"); fdefault.map_name = pdefault.map_name = USTR "ascii"; } /* initialize mouse support */ if (xmouse && (s=(unsigned char *)getenv("TERM")) && strstr((char *)s,"xterm")) usexmouse=1; if (!(n = nopen(cap))) return 1; maint = screate(n); vmem = vtmp(); startup_log = bfind_scratch_incref(USTR "* Startup Log *"); startup_log->internal = 1; load_state(); /* It would be better if this ran uedit() to load files */ /* The business with backopt is to load the file first, then apply file * local options afterwords */ /* orphan is not compatible with exemac()- macros need a window to exist */ for (c = 1, backopt = 0; argv[c]; ++c) if (argv[c][0] == '+' && argv[c][1]>='0' && argv[c][1]<='9') { if (!backopt) backopt = c; } else if (argv[c][0] == '-' && argv[c][1]) { if (!backopt) backopt = c; if (glopt(argv[c] + 1, argv[c + 1], NULL, 0) == 2) ++c; } else { B *b = bfind_incref(argv[c]); BW *bw = NULL; int er = berror; /* This is too annoying */ /* set_current_dir(argv[c],1); */ setup_history(&filehist); append_history(filehist,sz(argv[c])); /* wmktw_takeref() inserts the window before maint->curwin */ if (!orphan || !opened) { bw = wmktw_takeref(maint, b); if (er) msgnwt(bw->parent, joe_gettext(msgs[-er])); } else { long line; b->orphan = 1; b->oldcur = pdup(b->bof, USTR "main"); pline(b->oldcur, get_file_pos(b->name)); line = b->oldcur->line - (maint->h - 1) / 2; if (line < 0) line = 0; b->oldtop = pdup(b->oldcur, USTR "main"); pline(b->oldtop, line); p_goto_bol(b->oldtop); } if (bw) { long lnum = 0; bw->o.readonly = bw->b->rdonly; if (backopt) { while (backopt != c) { if (argv[backopt][0] == '+') { sscanf((char *)(argv[backopt] + 1), "%ld", &lnum); ++backopt; } else { if (glopt(argv[backopt] + 1, argv[backopt + 1], &bw->o, 0) == 2) backopt += 2; else backopt += 1; lazy_opts(bw->b, &bw->o); } } } bw->b->o = bw->o; bw->b->rdonly = bw->o.readonly; /* Put cursor in window, so macros work properly */ maint->curwin = bw->parent; /* Execute macro */ if (er == -1 && bw->o.mnew) exmacro(bw->o.mnew,1); if (er == 0 && bw->o.mold) exmacro(bw->o.mold,1); /* Hmm... window might not exist any more... depends on what macro does... */ if (lnum > 0) pline(bw->cursor, lnum - 1); else pline(bw->cursor, get_file_pos(bw->b->name)); p_goto_bol(bw->cursor); /* Go back to first window so windows are in same order as command line */ if (opened) wnext(maint); } opened = 1; backopt = 0; } if (opened) { wshowall(maint); omid = mid; mid = 1; dofollows(); mid = omid; } else { BW *bw = wmktw_takeref(maint, bfind_incref(USTR "")); if (bw->o.mnew) exmacro(bw->o.mnew,1); } maint->curwin = maint->topwin; if (startup_log->eof->byte) { BW *bw = wmktw_takeref(maint, startup_log); startup_log = NULL; maint->curwin = bw->parent; wshowall(maint); uparserr(bw); } if (help) { help_on(maint); } if (!nonotice) { joe_snprintf_3(msgbuf,JOE_MSGBUFSIZE,joe_gettext(_("\\i** Joe's Own Editor v%s ** (%s) ** Copyright %s 2008 **\\i")),VERSION,locale_map->name,(locale_map->type ? "©" : "(C)")); msgnw(((BASE *)lastw(maint)->object)->parent, msgbuf); } if (!idleout) { if (!isatty(fileno(stdin)) && modify_logic(maint->curwin->object, ((BW *)maint->curwin->object)->b)) { /* Start shell going in first window */ unsigned char **a; unsigned char *cmd; a = vamk(10); cmd = vsncpy(NULL, 0, sc("/bin/sh")); a = vaadd(a, cmd); cmd = vsncpy(NULL, 0, sc("-c")); a = vaadd(a, cmd); cmd = vsncpy(NULL, 0, sc("/bin/cat")); a = vaadd(a, cmd); cstart (maint->curwin->object, USTR "/bin/sh", a, NULL, NULL, 0, 1); } } edloop(0); save_state(); /* Delete all buffer so left over locks get eliminated */ brmall(); vclose(vmem); nclose(n); if (exmsg) fprintf(stderr, "\n%s\n", exmsg); return 0; }
int main(int argc, char **real_argv, char **envv) { CAP *cap; unsigned char **argv = (unsigned char **)real_argv; struct stat sbuf; unsigned char *s; unsigned char *t; long time_rc; unsigned char *run; #ifdef __MSDOS__ unsigned char *rundir; #endif SCRN *n; int opened = 0; int omid; int backopt; int c; joe_locale(); mainenv = (unsigned char **)envv; vmem = vtmp(); startup_log = bfind_scratch(USTR "* Startup Log *"); startup_log->internal = 1; #ifdef __MSDOS__ _fmode = O_BINARY; zlcpy(stdbuf, sizeof(stdbuf), argv[0]); joesep(stdbuf); run = namprt(stdbuf); rundir = dirprt(stdbuf); for (c = 0; run[c]; ++c) if (run[c] == '.') { run = vstrunc(run, c); break; } #else run = namprt(argv[0]); #endif if ((s = (unsigned char *)getenv("LINES")) != NULL) sscanf((char *)s, "%d", &lines); if ((s = (unsigned char *)getenv("COLUMNS")) != NULL) sscanf((char *)s, "%d", &columns); if ((s = (unsigned char *)getenv("BAUD")) != NULL) sscanf((char *)s, "%u", (unsigned *)&Baud); if (getenv("DOPADDING")) dopadding = 1; if (getenv("NOXON")) noxon = 1; if ((s = (unsigned char *)getenv("JOETERM")) != NULL) joeterm = s; #ifndef __MSDOS__ if (!(cap = my_getcap(NULL, 9600, NULL, NULL))) { logerror_0((char *)joe_gettext(_("Couldn't load termcap/terminfo entry\n"))); goto exit_errors; } #endif #ifdef __MSDOS__ s = vsncpy(NULL, 0, sv(run)); s = vsncpy(sv(s), sc("rc")); c = procrc(cap, s); if (c == 0) goto donerc; if (c == 1) { logerror_1((char *)joe_gettext(_("There were errors in '%s'. Falling back on default.\n")), s); } vsrm(s); s = vsncpy(NULL, 0, sv(rundir)); s = vsncpy(sv(s), sv(run)); s = vsncpy(sv(s), sc("rc")); c = procrc(cap, s); if (c == 0) goto donerc; if (c == 1) { logerror_1((char *)joe_gettext(_("There were errors in '%s'. Falling back on default.\n")), s); } #else /* Name of system joerc file. Try to find one with matching language... */ /* Try full language: like joerc.de_DE */ t = vsncpy(NULL, 0, sc(JOERC)); t = vsncpy(sv(t), sv(run)); t = vsncpy(sv(t), sc("rc.")); t = vsncpy(sv(t), sz(locale_msgs)); if (!stat((char *)t,&sbuf)) time_rc = sbuf.st_mtime; else { /* Try generic language: like joerc.de */ if (locale_msgs[0] && locale_msgs[1] && locale_msgs[2]=='_') { vsrm(t); t = vsncpy(NULL, 0, sc(JOERC)); t = vsncpy(sv(t), sv(run)); t = vsncpy(sv(t), sc("rc.")); t = vsncpy(sv(t), locale_msgs, 2); if (!stat((char *)t,&sbuf)) time_rc = sbuf.st_mtime; else goto nope; } else { nope: vsrm(t); /* Try Joe's bad english */ t = vsncpy(NULL, 0, sc(JOERC)); t = vsncpy(sv(t), sv(run)); t = vsncpy(sv(t), sc("rc")); if (!stat((char *)t,&sbuf)) time_rc = sbuf.st_mtime; else time_rc = 0; } } /* User's joerc file */ s = (unsigned char *)getenv("HOME"); if (s) { s = vsncpy(NULL, 0, sz(s)); s = vsncpy(sv(s), sc("/.")); s = vsncpy(sv(s), sv(run)); s = vsncpy(sv(s), sc("rc")); if (!stat((char *)s,&sbuf)) { if (sbuf.st_mtime < time_rc) { logmessage_2((char *)joe_gettext(_("Warning: %s is newer than your %s.\n")),t,s); } } c = procrc(cap, s); if (c == 0) { vsrm(t); goto donerc; } if (c == 1) { logerror_1((char *)joe_gettext(_("There were errors in '%s'. Falling back on default.\n")), s); } } vsrm(s); s = t; c = procrc(cap, s); if (c == 0) goto donerc; if (c == 1) { logerror_1((char *)joe_gettext(_("There were errors in '%s'. Falling back on default.\n")), s); } /* Try built-in joerc */ s = vsncpy(NULL, 0, sc("*")); s = vsncpy(sv(s), sv(run)); s = vsncpy(sv(s), sc("rc")); c = procrc(cap, s); if (c != 0 && c != 1) { /* If *fancyjoerc not present, use *joerc which is always there */ s = vstrunc(s, 0); s = vsncpy(sv(s),sc("*joerc")); c = procrc(cap, s); } if (c == 0) goto donerc; if (c == 1) { logerror_1((char *)joe_gettext(_("There were errors in '%s'. Falling back on default.\n")), s); } #endif logerror_1((char *)joe_gettext(_("Couldn't open '%s'\n")), s); goto exit_errors; return 1; donerc: if (validate_rc()) { logerror_0((char *)joe_gettext(_("rc file has no :main key binding section or no bindings. Bye.\n"))); goto exit_errors; } { unsigned char buf[10]; int x; zlcpy(buf, sizeof(buf), USTR "\"`\" ` "); type_backtick = mparse(0, buf, &x, 0); } shell_kbd = mkkbd(kmap_getcontext(USTR "shell")); if (!isatty(fileno(stdin))) idleout = 0; for (c = 1; argv[c]; ++c) { if (argv[c][0] == '-') { if (argv[c][1]) switch (glopt(argv[c] + 1, argv[c + 1], NULL, 1)) { case 0: logerror_1((char *)joe_gettext(_("Unknown option '%s'\n")), argv[c]); break; case 1: break; case 2: ++c; break; } else idleout = 0; } } /* initialize mouse support */ if (xmouse && (s=(unsigned char *)getenv("TERM")) && strstr((char *)s,"xterm")) usexmouse=1; if (!(n = nopen(cap))) goto exit_errors; maint = screate(n); load_state(); /* It would be better if this ran uedit() to load files */ /* The business with backopt is to load the file first, then apply file * local options afterwords */ /* orphan is not compatible with exemac()- macros need a window to exist */ for (c = 1, backopt = 0; argv[c]; ++c) if (argv[c][0] == '+' && argv[c][1]>='0' && argv[c][1]<='9') { if (!backopt) backopt = c; } else if (argv[c][0] == '-' && argv[c][1]) { if (!backopt) backopt = c; if (glopt(argv[c] + 1, argv[c + 1], NULL, 0) == 2) ++c; } else { B *b = bfind(argv[c]); BW *bw = NULL; int er = berror; /* This is too annoying */ /* set_current_dir(argv[c],1); */ setup_history(&filehist); append_history(filehist,sz(argv[c])); /* wmktw inserts the window before maint->curwin */ if (!orphan || !opened) { bw = wmktw(maint, b); if (er) msgnwt(bw->parent, joe_gettext(msgs[-er])); } else { long line; b->orphan = 1; b->oldcur = pdup(b->bof, USTR "main"); pline(b->oldcur, get_file_pos(b->name)); p_goto_bol(b->oldcur); line = b->oldcur->line - (maint->h - 1) / 2; if (line < 0) line = 0; b->oldtop = pdup(b->oldcur, USTR "main"); pline(b->oldtop, line); p_goto_bol(b->oldtop); } if (bw) { long lnum = 0; bw->o.readonly = bw->b->rdonly; if (backopt) { while (backopt != c) { if (argv[backopt][0] == '+') { sscanf((char *)(argv[backopt] + 1), "%ld", &lnum); ++backopt; } else { if (glopt(argv[backopt] + 1, argv[backopt + 1], &bw->o, 0) == 2) backopt += 2; else backopt += 1; lazy_opts(bw->b, &bw->o); } } } bw->b->o = bw->o; bw->b->rdonly = bw->o.readonly; /* Put cursor in window, so macros work properly */ maint->curwin = bw->parent; /* Execute macro */ if (er == -1 && bw->o.mnew) exmacro(bw->o.mnew,1); if (er == 0 && bw->o.mold) exmacro(bw->o.mold,1); /* Hmm... window might not exist any more... depends on what macro does... */ if (lnum > 0) pline(bw->cursor, lnum - 1); else pline(bw->cursor, get_file_pos(bw->b->name)); p_goto_bol(bw->cursor); /* Go back to first window so windows are in same order as command line */ if (opened) wnext(maint); } opened = 1; backopt = 0; } if (opened) { wshowall(maint); omid = mid; mid = 1; dofollows(); mid = omid; } else { BW *bw = wmktw(maint, bfind(USTR "")); if (bw->o.mnew) exmacro(bw->o.mnew,1); } maint->curwin = maint->topwin; if (logerrors) { B *copied = bcpy(startup_log->bof, startup_log->eof); BW *bw = wmktw(maint, copied); copied->name = zdup(startup_log->name); copied->internal = 1; maint->curwin = bw->parent; wshowall(maint); } if (help) { help_on(maint); } if (!nonotice) { joe_snprintf_3(msgbuf,JOE_MSGBUFSIZE,joe_gettext(_("\\i** Joe's Own Editor v%s ** (%s) ** Copyright %s 2015 **\\i")),VERSION,locale_map->name,(locale_map->type ? "©" : "(C)")); msgnw(((BASE *)lastw(maint)->object)->parent, msgbuf); } if (!idleout) { if (!isatty(fileno(stdin)) && modify_logic(maint->curwin->object, ((BW *)maint->curwin->object)->b)) { /* Start shell going in first window */ unsigned char **a; unsigned char *cmd; a = vamk(10); cmd = vsncpy(NULL, 0, sc("/bin/sh")); a = vaadd(a, cmd); cmd = vsncpy(NULL, 0, sc("-c")); a = vaadd(a, cmd); cmd = vsncpy(NULL, 0, sc("/bin/cat")); a = vaadd(a, cmd); cstart (maint->curwin->object, USTR "/bin/sh", a, NULL, NULL, 0, 1, NULL, 0); } } edloop(0); save_state(); /* Delete all buffer so left over locks get eliminated */ brmall(); vclose(vmem); nclose(n); if (noexmsg) { if (notite) fprintf(stderr, "\n"); } else { if (exmsg) fprintf(stderr, "\n%s\n", exmsg); else if (notite) fprintf(stderr, "\n"); } return 0; exit_errors: /* Write out error log to console if we are exiting with errors. */ if (startup_log && startup_log->eof->byte) bsavefd(startup_log->bof, 2, startup_log->eof->byte); return 1; }
HIGHLIGHT_STATE lattr_get(struct lattr_db *db, struct high_syntax *y, P *p, ptrdiff_t line) { /* Past end of file? */ if (line > p->b->eof->line) { HIGHLIGHT_STATE x; clear_state(&x); return x; } /* Check if we need to expand */ if (line >= lattr_size(db)) { /* Expand by this amount */ ptrdiff_t amnt = line - lattr_size(db) + 1; /* Set position to end */ lattr_hole(db, lattr_size(db)); lattr_check(db, amnt); db->ehole -= amnt; /* Set invalid window to cover new space */ if (db->invalid_window == -1) { db->first_invalid = lattr_size(db) - amnt; db->invalid_window = amnt; } else { db->invalid_window = lattr_size(db) - db->first_invalid; } } /* Check if we need to shrink */ /* if (p->b->eof->line + 1 < lattr_size(db)) { lattr_hole(db, p->b->eof->line + 1); db->ehole = db->end; if (db->first_invalid > lattr_size(db)) { db->first_invalid = lattr_size(db); db->invalid_window = -1; } if (db->invalid_window != -1 && db->first_invalid + db->invalid_window > lattr_size(db)) { db->invalid_window = lattr_size(db) - db->first_invalid; } } */ /* Check if we are pointing to a valid record */ if (line >= db->first_invalid) { ptrdiff_t ln; P *tmp = 0; HIGHLIGHT_STATE state; tmp = pdup(p, "lattr_get"); ln = db->first_invalid; /* First line with known good state */ state = lattr_lvalue(db, ln - 1); /* Known good state */ /* Compute up to requested line */ pline(tmp, ln - 1); /* Recompute everything in invalid window */ while (ln < db->first_invalid + db->invalid_window) { state = parse(y, tmp, state, p->b->o.charmap); lattr_st(db, ln, &state); ++ln; } /* Update invalid window: hopefully we did the whole window */ db->invalid_window -= ln - db->first_invalid; db->first_invalid = ln; /* Recompute until match found. If match is found, we can assume rest is valid */ while (ln < lattr_size(db)) { HIGHLIGHT_STATE *prev; state = parse(y, tmp, state, p->b->o.charmap); prev = lattr_gt(db, ln); if (!eq_state(prev, &state)) lattr_st(db, ln, &state); else { db->first_invalid = lattr_size(db); db->invalid_window = -1; ++ln; break; } ++ln; } /* Update invalid pointer */ if (ln > db->first_invalid) { db->first_invalid = ln; db->invalid_window = 0; } if (ln == lattr_size(db)) { db->first_invalid = ln; db->invalid_window = -1; } prm(tmp); } /* Check it */ #ifdef junk { HIGHLIGHT_STATE st; P *tmp =pdup(p, "lattr_get"); pline(tmp, 0); clear_state(&st); for (z = 0; z != db->first_invalid; ++z) { HIGHLIGHT_STATE *prev; prev = lattr_gt(db, z); if (prev->state != st.state) { printf("** Mismatch!! %d %d %d %d **\n",z,tmp->line,prev->state,st.state); abort(); } st = parse(y, tmp, st, p->b->o.charmap); } prm(tmp); } #endif /* Return with attribute */ return lattr_lvalue(db, line); }
void wrapword(BW *bw, P *p, long int indent, int french, int no_over, unsigned char *indents) { P *q; P *r; P *s; int rmf = 0; int c; long to = p->byte; int my_indents = 0; /* autoindent when called by utype */ if (!indents) { /* Get indentation prefix from beginning of line */ s = pdup(p, USTR "wrapword"); p_goto_bol(s); pbop(bw, s); /* Record indentation of second line of paragraph, of first * line if there is only one line */ q = pdup(s, USTR "wrapword"); pnextl(q); if (q->line < p->line) { /* Second line */ P *r = pdup(q, USTR "wrapword"); indent = nindent(bw, q, 0); pcol(r, indent); indents = brs(q, r->byte - q->byte); prm(r); } else { /* First line */ P *r = pdup(s, USTR "uformat"); int x, y; indent = nindent(bw, s, 1); pcol(r, indent); indents = brs(s, r->byte - s->byte); prm(r); if (!bw->o.autoindent) { /* Don't indent second line of single-line paragraphs if autoindent is off */ int x = zlen(indents); while (x && (indents[x - 1] == ' ' || indents[x - 1] == '\t')) indents[--x] = 0; if (x) { indents[x++] = ' '; indents[x] = 0; } indent = txtwidth1(bw->o.charmap, bw->o.tab, indents, x); } for (x = 0; indents[x] && (indents[x] == ' ' || indents[x] == '\t'); ++x); y = zlen(indents); while (y && (indents[y - 1] == ' ' || indents[y - 1] == '\t')) --y; /* Don't duplicate bullet */ /* if (y && (indents[y - 1] == '*' || indents[y - 1] == '-') && (y == 1 || indents[y - 2] == ' ' || indents[y - 2] == '\t')) indents[y - 1] = ' '; */ /* Fix C comment */ if (indents[x] == '/' && indents[x + 1] == '*') indents[x] = ' '; } if (bw->o.lmargin > indent) { int x; for (x = 0; indents[x] == ' ' || indents[x] == '\t'; ++x); if (!indents[x]) { joe_free(indents); indent = bw->o.lmargin; indents = joe_malloc(indent+1); for (x = 0; x != indent; ++x) indents[x] = ' '; indents[x] = 0; } } my_indents = 1; prm(q); prm(s); } /* if(!indents) { int f = 0; P *r = pdup(p); p_goto_bol(r); q = pdup(r); while(cpara(c = brc(q))) { if(!joe_isblank(c)) f = 1; pgetc(q); } if(f) { indents = brs(r, q->byte-r->byte); rmf = 1; if(indents[0] == '/' && indents[1] == '*') indents[0] = ' '; } prm(r); prm(q); } */ /* Get to beginning of word */ while (!pisbol(p) && piscol(p) > indent && !joe_isblank(p->b->o.charmap, prgetc(p))) /* do nothing */; /* If we found the beginning of a word... */ if (!pisbol(p) && piscol(p) > indent) { /* Move q to two (or one if 'french' is set) spaces after end of previous word */ q = pdup(p, USTR "wrapword"); while (!pisbol(q)) if (!joe_isblank(p->b->o.charmap, (c = prgetc(q)))) { pgetc(q); if ((c == '.' || c == '?' || c == '!') && q->byte != p->byte && !french) pgetc(q); break; } pgetc(p); /* Delete space between start of word and end of previous word */ to -= p->byte - q->byte; bdel(q, p); prm(q); if (bw->o.flowed) { binsc(p, ' '); pgetc(p); ++to; } /* Move word to beginning of next line */ binsc(p, '\n'); /* When overtype is on, do not insert lines */ if (!no_over && p->b->o.overtype){ /* delete the next line break which is unnecessary */ r = pdup(p, USTR "wrapword"); /* p_goto_eol(r); */ pgetc(r); p_goto_eol(r); s = pdup(r, USTR "wrapword"); pgetc(r); bdel(s,r); binsc(r, ' '); /* Now we got to take care that all subsequent lines are not longer than the right margin */ /* Move cursor to right margin */ pfwrd(r, r->b->o.rmargin - r->col); /* Make a copy of the cursor and move the copied cursor to the end of the line */ prm(s); s = pdup(r, USTR "wrapword"); p_goto_eol(s); /* If s is located behind r then the line goes beyond the right margin and we need to call wordwrap() for that line. */ /* if (r->byte < s->byte){ wrapword(bw, r, indent, french, 1, indents); } */ prm(r); prm(s); } ++to; if (p->b->o.crlf) ++to; pgetc(p); /* Indent to left margin */ if (indents) { binss(p, indents); to += zlen(indents); } else while (indent--) { binsc(p, ' '); ++to; } if (rmf) joe_free(indents); } /* Move cursor back to original position */ pfwrd(p, to - p->byte); if (my_indents) joe_free(indents); }
int uformat(BW *bw) { long indent; unsigned char *indents; B *buf; P *b; long curoff; int c; P *p, *q; p = pdup(bw->cursor, USTR "uformat"); p_goto_bol(p); /* Do nothing if we're not on a paragraph line */ if (pisnpara(bw, p)) { prm(p); return 0; } /* Move p to beginning of paragraph, bw->cursor to end of paragraph and * set curoff to original cursor offset within the paragraph */ pbop(bw, p); curoff = bw->cursor->byte - p->byte; pset(bw->cursor, p); peop(bw, bw->cursor); /* Ensure that paragraph ends on a beginning of a line */ if (!pisbol(bw->cursor)) binsc(bw->cursor, '\n'), pgetc(bw->cursor); /* Record indentation of second line of paragraph, of first line if there * is only one line */ q = pdup(p, USTR "uformat"); pnextl(q); if (q->line != bw->cursor->line) { P *r = pdup(q, USTR "uformat"); indent = nindent(bw, q, 0); pcol(r, indent); indents = brs(q, r->byte - q->byte); prm(r); } else { P *r = pdup(p, USTR "uformat"); int x, y; indent = nindent(bw, p, 1); /* allowing * and - here */ pcol(r, indent); indents = brs(p, r->byte - p->byte); prm(r); if (!bw->o.autoindent) { /* Don't indent second line of single-line paragraphs if autoindent is off */ int x = zlen(indents); while (x && (indents[x - 1] == ' ' || indents[x - 1] == '\t')) indents[--x] = 0; if (x) { indents[x++] = ' '; indents[x] = 0; } indent = txtwidth1(bw->o.charmap, bw->o.tab, indents, x); } for (x = 0; indents[x] && (indents[x] == ' ' || indents[x] == '\t'); ++x); y = zlen(indents); while (y && (indents[y - 1] == ' ' || indents[y - 1] == '\t')) --y; /* Don't duplicate if it looks like a bullet */ /* if (y && (indents[y - 1] == '*' || indents[y - 1] == '-') && (y == 1 || indents[y - 2] == ' ' || indents[y - 2] == '\t')) indents[y - 1] = ' '; */ /* Fix C comment */ if (indents[x] == '/' && indents[x + 1] == '*') indents[x] = ' '; } prm(q); /* But if the left margin is greater, we use that instead */ if (bw->o.lmargin > indent) { int x; for (x = 0; indents[x] == ' ' || indents[x] == '\t'; ++x); if (!indents[x]) { joe_free(indents); indent = bw->o.lmargin; indents = joe_malloc(indent+1); for (x = 0; x != indent; ++x) indents[x] = ' '; indents[x] = 0; } } /* Cut paragraph into new buffer */ /* New buffer needs to inherit UTF-8 and CR-LF options */ buf = bcpy(p, bw->cursor); buf->o.crlf = p->b->o.crlf; buf->o.charmap = p->b->o.charmap; bdel(p, bw->cursor); /* text is in buffer. insert it at cursor */ /* Do first line */ b = pdup(buf->bof, USTR "uformat"); while (!piseof(b)) { /* Set cursor position if we're at original offset */ if (b->byte == curoff) pset(bw->cursor, p); /* Get character from buffer */ c = pgetc(b); /* Stop if we found end of line */ if (c == '\n') { prgetc(b); break; } /* Stop if we found white-space followed by end of line */ if (joe_isblank(b->b->o.charmap, c) && piseolblank(b)) { prgetc(b); break; } /* Insert character, advance pointer */ binsc(p, c); pgetc(p); /* Do word wrap if we reach right margin */ if (piscol(p) > bw->o.rmargin && !joe_isblank(p->b->o.charmap,c)) { wrapword(bw, p, indent, bw->o.french, 1, indents); break; } } /* Do rest */ while (!piseof(b)) { c = brch(b); if (joe_isblank(b->b->o.charmap,c) || c == '\n') { int f = 0; P *d; int g; /* Set f if there are two spaces after . ? or ! instead of one */ /* (What is c was '\n'?) */ d=pdup(b, USTR "uformat"); g=prgetc(d); if (g=='.' || g=='?' || g=='!') { f = 1; /* pset(d,b); pgetc(d); if (c == '\n' || piseol(d) || joe_isspace(bw->b->o.charmap,brch(d))) { f = 1; } */ } prm(d); /* Skip past the whitespace. Skip over indentations */ loop: c = brch(b); if (c == '\n') { if (b->byte == curoff) pset(bw->cursor, p); pgetc(b); while (cpara(bw, (c=brch(b)))) { if (b->byte == curoff) pset(bw->cursor, p); pgetc(b); } } if (joe_isblank(b->b->o.charmap,c)) { if(b->byte == curoff) pset(bw->cursor, p); pgetc(b); goto loop; } /* Insert proper amount of whitespace */ if (!piseof(b)) { if (f && !bw->o.french) binsc(p, ' '), pgetc(p); binsc(p, ' '); pgetc(p); } } else { /* Insert characters of word and wrap if necessary */ if (b->byte == curoff) pset(bw->cursor, p); binsc(p, pgetc(b)); pgetc(p); if (piscol(p) > bw->o.rmargin) wrapword(bw, p, indent, bw->o.french, 1, indents); } } binsc(p, '\n'); prm(p); brm(buf); joe_free(indents); return 0; }
void internal_msg(char *s) { P *t = pdup(startup_log->eof, "internal_msg"); binss(t, s); prm(t); }
void internal_msg(unsigned char *s) { P *t = pdup(startup_log->eof, USTR "internal_msg"); binss(t, s); prm(t); }