look.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. #include <thread.h>
  13. #include <cursor.h>
  14. #include <mouse.h>
  15. #include <keyboard.h>
  16. #include <frame.h>
  17. #include <fcall.h>
  18. #include <regexp.h>
  19. #include <plumb.h>
  20. #include "dat.h"
  21. #include "fns.h"
  22. Window* openfile(Text*, Expand*);
  23. int nuntitled;
  24. void
  25. look3(Text *t, uint q0, uint q1, int external)
  26. {
  27. int n, c, f, expanded;
  28. Text *ct;
  29. Expand e;
  30. Rune *r;
  31. uint p;
  32. Plumbmsg *m;
  33. Runestr dir;
  34. char buf[32];
  35. ct = seltext;
  36. if(ct == nil)
  37. seltext = t;
  38. expanded = expand(t, q0, q1, &e);
  39. if(!external && t->w!=nil && t->w->nopen[QWevent]>0){
  40. /* send alphanumeric expansion to external client */
  41. if(expanded == FALSE)
  42. return;
  43. f = 0;
  44. if((e.at!=nil && t->w!=nil) || (e.nname>0 && lookfile(e.name, e.nname)!=nil))
  45. f = 1; /* acme can do it without loading a file */
  46. if(q0!=e.q0 || q1!=e.q1)
  47. f |= 2; /* second (post-expand) message follows */
  48. if(e.nname)
  49. f |= 4; /* it's a file name */
  50. c = 'l';
  51. if(t->what == Body)
  52. c = 'L';
  53. n = q1-q0;
  54. if(n <= EVENTSIZE){
  55. r = runemalloc(n);
  56. bufread(&t->file->Buffer, q0, r, n);
  57. winevent(t->w, "%c%d %d %d %d %.*S\n", c, q0, q1, f, n, n, r);
  58. free(r);
  59. }else
  60. winevent(t->w, "%c%d %d %d 0 \n", c, q0, q1, f, n);
  61. if(q0==e.q0 && q1==e.q1)
  62. return;
  63. if(e.nname){
  64. n = e.nname;
  65. if(e.a1 > e.a0)
  66. n += 1+(e.a1-e.a0);
  67. r = runemalloc(n);
  68. runemove(r, e.name, e.nname);
  69. if(e.a1 > e.a0){
  70. r[e.nname] = ':';
  71. bufread(&e.at->file->Buffer, e.a0, r+e.nname+1, e.a1-e.a0);
  72. }
  73. }else{
  74. n = e.q1 - e.q0;
  75. r = runemalloc(n);
  76. bufread(&t->file->Buffer, e.q0, r, n);
  77. }
  78. f &= ~2;
  79. if(n <= EVENTSIZE)
  80. winevent(t->w, "%c%d %d %d %d %.*S\n", c, e.q0, e.q1, f, n, n, r);
  81. else
  82. winevent(t->w, "%c%d %d %d 0 \n", c, e.q0, e.q1, f, n);
  83. free(r);
  84. goto Return;
  85. }
  86. if(plumbsendfd >= 0){
  87. /* send whitespace-delimited word to plumber */
  88. m = emalloc(sizeof(Plumbmsg));
  89. m->src = estrdup("acme");
  90. m->dst = nil;
  91. dir = dirname(t, nil, 0);
  92. if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
  93. free(dir.r);
  94. dir.r = nil;
  95. dir.nr = 0;
  96. }
  97. if(dir.nr == 0)
  98. m->wdir = estrdup(wdir);
  99. else
  100. m->wdir = runetobyte(dir.r, dir.nr);
  101. free(dir.r);
  102. m->type = estrdup("text");
  103. m->attr = nil;
  104. buf[0] = '\0';
  105. if(q1 == q0){
  106. if(t->q1>t->q0 && t->q0<=q0 && q0<=t->q1){
  107. q0 = t->q0;
  108. q1 = t->q1;
  109. }else{
  110. p = q0;
  111. while(q0>0 && (c=tgetc(t, q0-1))!=' ' && c!='\t' && c!='\n')
  112. q0--;
  113. while(q1<t->file->Buffer.nc && (c=tgetc(t, q1))!=' ' && c!='\t' && c!='\n')
  114. q1++;
  115. if(q1 == q0){
  116. plumbfree(m);
  117. goto Return;
  118. }
  119. sprint(buf, "click=%d", p-q0);
  120. m->attr = plumbunpackattr(buf);
  121. }
  122. }
  123. r = runemalloc(q1-q0);
  124. bufread(&t->file->Buffer, q0, r, q1-q0);
  125. m->data = runetobyte(r, q1-q0);
  126. m->ndata = strlen(m->data);
  127. free(r);
  128. if(m->ndata<messagesize-1024 && plumbsend(plumbsendfd, m) >= 0){
  129. plumbfree(m);
  130. goto Return;
  131. }
  132. plumbfree(m);
  133. /* plumber failed to match; fall through */
  134. }
  135. /* interpret alphanumeric string ourselves */
  136. if(expanded == FALSE)
  137. return;
  138. if(e.name || e.at)
  139. openfile(t, &e);
  140. else{
  141. if(t->w == nil)
  142. return;
  143. ct = &t->w->body;
  144. if(t->w != ct->w)
  145. winlock(ct->w, 'M');
  146. if(t == ct)
  147. textsetselect(ct, e.q1, e.q1);
  148. n = e.q1 - e.q0;
  149. r = runemalloc(n);
  150. bufread(&t->file->Buffer, e.q0, r, n);
  151. if(search(ct, r, n) && e.jump)
  152. moveto(mousectl, addpt(frptofchar(&ct->Frame, ct->Frame.p0), Pt(4, ct->Frame.font->height-4)));
  153. if(t->w != ct->w)
  154. winunlock(ct->w);
  155. free(r);
  156. }
  157. Return:
  158. free(e.name);
  159. free(e.bname);
  160. }
  161. int
  162. plumbgetc(void *a, uint n)
  163. {
  164. Rune *r;
  165. r = a;
  166. if(n>runestrlen(r))
  167. return 0;
  168. return r[n];
  169. }
  170. void
  171. plumblook(Plumbmsg *m)
  172. {
  173. Expand e;
  174. char *addr;
  175. if(m->ndata >= BUFSIZE){
  176. warning(nil, "insanely long file name (%d bytes) in plumb message (%.32s...)\n", m->ndata, m->data);
  177. return;
  178. }
  179. e.q0 = 0;
  180. e.q1 = 0;
  181. if(m->data[0] == '\0')
  182. return;
  183. e.ar = nil;
  184. e.bname = m->data;
  185. e.name = bytetorune(e.bname, &e.nname);
  186. e.jump = TRUE;
  187. e.a0 = 0;
  188. e.a1 = 0;
  189. addr = plumblookup(m->attr, "addr");
  190. if(addr != nil){
  191. e.ar = bytetorune(addr, &e.a1);
  192. e.agetc = plumbgetc;
  193. }
  194. openfile(nil, &e);
  195. free(e.name);
  196. free(e.at);
  197. }
  198. void
  199. plumbshow(Plumbmsg *m)
  200. {
  201. Window *w;
  202. Rune rb[256], *r;
  203. int nb, nr;
  204. Runestr rs;
  205. char *name, *p, namebuf[16];
  206. w = makenewwindow(nil);
  207. name = plumblookup(m->attr, "filename");
  208. if(name == nil){
  209. name = namebuf;
  210. nuntitled++;
  211. snprint(namebuf, sizeof namebuf, "Untitled-%d", nuntitled);
  212. }
  213. p = nil;
  214. if(name[0]!='/' && m->wdir!=nil && m->wdir[0]!='\0'){
  215. nb = strlen(m->wdir) + 1 + strlen(name) + 1;
  216. p = emalloc(nb);
  217. snprint(p, nb, "%s/%s", m->wdir, name);
  218. name = p;
  219. }
  220. cvttorunes(name, strlen(name), rb, &nb, &nr, nil);
  221. free(p);
  222. rs = cleanrname((Runestr){rb, nr});
  223. winsetname(w, rs.r, rs.nr);
  224. r = runemalloc(m->ndata);
  225. cvttorunes(m->data, m->ndata, r, &nb, &nr, nil);
  226. textinsert(&w->body, 0, r, nr, TRUE);
  227. free(r);
  228. w->body.file->mod = FALSE;
  229. w->dirty = FALSE;
  230. winsettag(w);
  231. textscrdraw(&w->body);
  232. textsetselect(&w->tag, w->tag.file->Buffer.nc, w->tag.file->Buffer.nc);
  233. }
  234. int
  235. search(Text *ct, Rune *r, uint n)
  236. {
  237. uint q, nb, maxn;
  238. int around;
  239. Rune *s, *b, *c;
  240. if(n==0 || n>ct->file->Buffer.nc)
  241. return FALSE;
  242. if(2*n > RBUFSIZE){
  243. warning(nil, "string too long\n");
  244. return FALSE;
  245. }
  246. maxn = max(2*n, RBUFSIZE);
  247. s = fbufalloc();
  248. b = s;
  249. nb = 0;
  250. b[nb] = 0;
  251. around = 0;
  252. q = ct->q1;
  253. for(;;){
  254. if(q >= ct->file->Buffer.nc){
  255. q = 0;
  256. around = 1;
  257. nb = 0;
  258. b[nb] = 0;
  259. }
  260. if(nb > 0){
  261. c = runestrchr(b, r[0]);
  262. if(c == nil){
  263. q += nb;
  264. nb = 0;
  265. b[nb] = 0;
  266. if(around && q>=ct->q1)
  267. break;
  268. continue;
  269. }
  270. q += (c-b);
  271. nb -= (c-b);
  272. b = c;
  273. }
  274. /* reload if buffer covers neither string nor rest of file */
  275. if(nb<n && nb!=ct->file->Buffer.nc-q){
  276. nb = ct->file->Buffer.nc-q;
  277. if(nb >= maxn)
  278. nb = maxn-1;
  279. bufread(&ct->file->Buffer, q, s, nb);
  280. b = s;
  281. b[nb] = '\0';
  282. }
  283. /* this runeeq is fishy but the null at b[nb] makes it safe */
  284. if(runeeq(b, n, r, n)==TRUE){
  285. if(ct->w){
  286. textshow(ct, q, q+n, 1);
  287. winsettag(ct->w);
  288. }else{
  289. ct->q0 = q;
  290. ct->q1 = q+n;
  291. }
  292. seltext = ct;
  293. fbuffree(s);
  294. return TRUE;
  295. }
  296. --nb;
  297. b++;
  298. q++;
  299. if(around && q>=ct->q1)
  300. break;
  301. }
  302. fbuffree(s);
  303. return FALSE;
  304. }
  305. int
  306. isfilec(Rune r)
  307. {
  308. if(isalnum(r))
  309. return TRUE;
  310. if(runestrchr((Rune *)L".-+/:", r))
  311. return TRUE;
  312. return FALSE;
  313. }
  314. /* Runestr wrapper for cleanname */
  315. Runestr
  316. cleanrname(Runestr rs)
  317. {
  318. char *s;
  319. int nb, nulls;
  320. s = runetobyte(rs.r, rs.nr);
  321. cleanname(s);
  322. cvttorunes(s, strlen(s), rs.r, &nb, &rs.nr, &nulls);
  323. free(s);
  324. return rs;
  325. }
  326. Runestr
  327. includefile(Rune *dir, Rune *file, int nfile)
  328. {
  329. int m, n;
  330. char *a;
  331. Rune *r;
  332. m = runestrlen(dir);
  333. a = emalloc((m+1+nfile)*UTFmax+1);
  334. sprint(a, "%S/%.*S", dir, nfile, file);
  335. n = access(a, 0);
  336. free(a);
  337. if(n < 0)
  338. return (Runestr){nil, 0};
  339. r = runemalloc(m+1+nfile);
  340. runemove(r, dir, m);
  341. runemove(r+m, L"/", 1);
  342. runemove(r+m+1, file, nfile);
  343. free(file);
  344. return cleanrname((Runestr){r, m+1+nfile});
  345. }
  346. static Rune *objdir;
  347. Runestr
  348. includename(Text *t, Rune *r, int n)
  349. {
  350. Window *w;
  351. char buf[128];
  352. Runestr file;
  353. int i;
  354. if(objdir==nil && objtype!=nil){
  355. sprint(buf, "/%s/include", objtype);
  356. objdir = bytetorune(buf, &i);
  357. objdir = runerealloc(objdir, i+1);
  358. objdir[i] = '\0';
  359. }
  360. w = t->w;
  361. if(n==0 || r[0]=='/' || w==nil)
  362. goto Rescue;
  363. if(n>2 && r[0]=='.' && r[1]=='/')
  364. goto Rescue;
  365. file.r = nil;
  366. file.nr = 0;
  367. for(i=0; i<w->nincl && file.r==nil; i++)
  368. file = includefile(w->incl[i], r, n);
  369. if(file.r == nil)
  370. file = includefile((Rune *)L"/sys/include", r, n);
  371. if(file.r==nil && objdir!=nil)
  372. file = includefile(objdir, r, n);
  373. if(file.r == nil)
  374. goto Rescue;
  375. return file;
  376. Rescue:
  377. return (Runestr){r, n};
  378. }
  379. Runestr
  380. dirname(Text *t, Rune *r, int n)
  381. {
  382. Rune *b, c;
  383. uint m, nt;
  384. int slash;
  385. Runestr tmp;
  386. b = nil;
  387. if(t==nil || t->w==nil)
  388. goto Rescue;
  389. nt = t->w->tag.file->Buffer.nc;
  390. if(nt == 0)
  391. goto Rescue;
  392. if(n>=1 && r[0]=='/')
  393. goto Rescue;
  394. b = runemalloc(nt+n+1);
  395. bufread(&t->w->tag.file->Buffer, 0, b, nt);
  396. slash = -1;
  397. for(m=0; m<nt; m++){
  398. c = b[m];
  399. if(c == '/')
  400. slash = m;
  401. if(c==' ' || c=='\t')
  402. break;
  403. }
  404. if(slash < 0)
  405. goto Rescue;
  406. runemove(b+slash+1, r, n);
  407. free(r);
  408. return cleanrname((Runestr){b, slash+1+n});
  409. Rescue:
  410. free(b);
  411. tmp = (Runestr){r, n};
  412. if(r)
  413. return cleanrname(tmp);
  414. return tmp;
  415. }
  416. int
  417. expandfile(Text *t, uint q0, uint q1, Expand *e)
  418. {
  419. int i, n, nname, colon, eval;
  420. uint amin, amax;
  421. Rune *r, c;
  422. Window *w;
  423. Runestr rs;
  424. amax = q1;
  425. if(q1 == q0){
  426. colon = -1;
  427. while(q1<t->file->Buffer.nc && isfilec(c=textreadc(t, q1))){
  428. if(c == ':'){
  429. colon = q1;
  430. break;
  431. }
  432. q1++;
  433. }
  434. while(q0>0 && (isfilec(c=textreadc(t, q0-1)) || isaddrc(c) || isregexc(c))){
  435. q0--;
  436. if(colon<0 && c==':')
  437. colon = q0;
  438. }
  439. /*
  440. * if it looks like it might begin file: , consume address chars after :
  441. * otherwise terminate expansion at :
  442. */
  443. if(colon >= 0){
  444. q1 = colon;
  445. if(colon<t->file->Buffer.nc-1 && isaddrc(textreadc(t, colon+1))){
  446. q1 = colon+1;
  447. while(q1<t->file->Buffer.nc && isaddrc(textreadc(t, q1)))
  448. q1++;
  449. }
  450. }
  451. if(q1 > q0){
  452. if(colon >= 0){ /* stop at white space */
  453. for(amax=colon+1; amax<t->file->Buffer.nc; amax++)
  454. if((c=textreadc(t, amax))==' ' || c=='\t' || c=='\n')
  455. break;
  456. }else
  457. amax = t->file->Buffer.nc;
  458. }
  459. }
  460. amin = amax;
  461. e->q0 = q0;
  462. e->q1 = q1;
  463. n = q1-q0;
  464. if(n == 0)
  465. return FALSE;
  466. /* see if it's a file name */
  467. r = runemalloc(n);
  468. bufread(&t->file->Buffer, q0, r, n);
  469. /* first, does it have bad chars? */
  470. nname = -1;
  471. for(i=0; i<n; i++){
  472. c = r[i];
  473. if(c==':' && nname<0){
  474. if(q0+i+1<t->file->Buffer.nc && (i==n-1 || isaddrc(textreadc(t, q0+i+1))))
  475. amin = q0+i;
  476. else
  477. goto Isntfile;
  478. nname = i;
  479. }
  480. }
  481. if(nname == -1)
  482. nname = n;
  483. for(i=0; i<nname; i++)
  484. if(!isfilec(r[i]))
  485. goto Isntfile;
  486. /*
  487. * See if it's a file name in <>, and turn that into an include
  488. * file name if so. Should probably do it for "" too, but that's not
  489. * restrictive enough syntax and checking for a #include earlier on the
  490. * line would be silly.
  491. */
  492. if(q0>0 && textreadc(t, q0-1)=='<' && q1<t->file->Buffer.nc && textreadc(t, q1)=='>'){
  493. rs = includename(t, r, nname);
  494. r = rs.r;
  495. nname = rs.nr;
  496. }
  497. else if(amin == q0)
  498. goto Isfile;
  499. else{
  500. rs = dirname(t, r, nname);
  501. r = rs.r;
  502. nname = rs.nr;
  503. }
  504. e->bname = runetobyte(r, nname);
  505. /* if it's already a window name, it's a file */
  506. w = lookfile(r, nname);
  507. if(w != nil)
  508. goto Isfile;
  509. /* if it's the name of a file, it's a file */
  510. if(access(e->bname, 0) < 0){
  511. free(e->bname);
  512. e->bname = nil;
  513. goto Isntfile;
  514. }
  515. Isfile:
  516. e->name = r;
  517. e->nname = nname;
  518. e->at = t;
  519. e->a0 = amin+1;
  520. eval = FALSE;
  521. address(nil, nil, (Range){-1,-1}, (Range){0, 0}, t, e->a0, amax, tgetc, &eval, (uint*)&e->a1);
  522. return TRUE;
  523. Isntfile:
  524. free(r);
  525. return FALSE;
  526. }
  527. int
  528. expand(Text *t, uint q0, uint q1, Expand *e)
  529. {
  530. memset(e, 0, sizeof *e);
  531. e->agetc = tgetc;
  532. /* if in selection, choose selection */
  533. e->jump = TRUE;
  534. if(q1==q0 && t->q1>t->q0 && t->q0<=q0 && q0<=t->q1){
  535. q0 = t->q0;
  536. q1 = t->q1;
  537. if(t->what == Tag)
  538. e->jump = FALSE;
  539. }
  540. if(expandfile(t, q0, q1, e))
  541. return TRUE;
  542. if(q0 == q1){
  543. while(q1<t->file->Buffer.nc && isalnum(textreadc(t, q1)))
  544. q1++;
  545. while(q0>0 && isalnum(textreadc(t, q0-1)))
  546. q0--;
  547. }
  548. e->q0 = q0;
  549. e->q1 = q1;
  550. return q1 > q0;
  551. }
  552. Window*
  553. lookfile(Rune *s, int n)
  554. {
  555. int i, j, k;
  556. Window *w;
  557. Column *c;
  558. Text *t;
  559. /* avoid terminal slash on directories */
  560. if(n>1 && s[n-1] == '/')
  561. --n;
  562. for(j=0; j<row.ncol; j++){
  563. c = row.col[j];
  564. for(i=0; i<c->nw; i++){
  565. w = c->w[i];
  566. t = &w->body;
  567. k = t->file->nname;
  568. if(k>1 && t->file->name[k-1] == '/')
  569. k--;
  570. if(runeeq(t->file->name, k, s, n)){
  571. w = w->body.file->curtext->w;
  572. if(w->col != nil) /* protect against race deleting w */
  573. return w;
  574. }
  575. }
  576. }
  577. return nil;
  578. }
  579. Window*
  580. lookid(int id, int dump)
  581. {
  582. int i, j;
  583. Window *w;
  584. Column *c;
  585. for(j=0; j<row.ncol; j++){
  586. c = row.col[j];
  587. for(i=0; i<c->nw; i++){
  588. w = c->w[i];
  589. if(dump && w->dumpid == id)
  590. return w;
  591. if(!dump && w->id == id)
  592. return w;
  593. }
  594. }
  595. return nil;
  596. }
  597. Window*
  598. openfile(Text *t, Expand *e)
  599. {
  600. Range r;
  601. Window *w, *ow;
  602. int eval, i, n;
  603. Rune *rp;
  604. uint dummy;
  605. if(e->nname == 0){
  606. w = t->w;
  607. if(w == nil)
  608. return nil;
  609. }else
  610. w = lookfile(e->name, e->nname);
  611. if(w){
  612. t = &w->body;
  613. if(!t->col->safe && t->Frame.maxlines==0) /* window is obscured by full-column window */
  614. colgrow(t->col, t->col->w[0], 1);
  615. }else{
  616. ow = nil;
  617. if(t)
  618. ow = t->w;
  619. w = makenewwindow(t);
  620. t = &w->body;
  621. winsetname(w, e->name, e->nname);
  622. textload(t, 0, e->bname, 1);
  623. t->file->mod = FALSE;
  624. t->w->dirty = FALSE;
  625. winsettag(t->w);
  626. textsetselect(&t->w->tag, t->w->tag.file->Buffer.nc, t->w->tag.file->Buffer.nc);
  627. if(ow != nil){
  628. for(i=ow->nincl; --i>=0; ){
  629. n = runestrlen(ow->incl[i]);
  630. rp = runemalloc(n);
  631. runemove(rp, ow->incl[i], n);
  632. winaddincl(w, rp, n);
  633. }
  634. w->autoindent = ow->autoindent;
  635. }else
  636. w->autoindent = globalautoindent;
  637. }
  638. if(e->a1 == e->a0)
  639. eval = FALSE;
  640. else{
  641. eval = TRUE;
  642. r = address(nil, t, (Range){-1, -1}, (Range){t->q0, t->q1}, e->at, e->a0, e->a1, e->agetc, &eval, &dummy);
  643. if(eval == FALSE)
  644. e->jump = FALSE; /* don't jump if invalid address */
  645. }
  646. if(eval == FALSE){
  647. r.q0 = t->q0;
  648. r.q1 = t->q1;
  649. }
  650. textshow(t, r.q0, r.q1, 1);
  651. winsettag(t->w);
  652. seltext = t;
  653. if(e->jump)
  654. moveto(mousectl, addpt(frptofchar(&t->Frame, t->Frame.p0), Pt(4, font->height-4)));
  655. return w;
  656. }
  657. void
  658. new(Text *et, Text *t, Text *argt, int flag1, int flag2, Rune *arg, int narg)
  659. {
  660. int ndone;
  661. Rune *a, *f;
  662. int na, nf;
  663. Expand e;
  664. Runestr rs;
  665. getarg(argt, FALSE, TRUE, &a, &na);
  666. if(a){
  667. new(et, t, nil, flag1, flag2, a, na);
  668. if(narg == 0)
  669. return;
  670. }
  671. /* loop condition: *arg is not a blank */
  672. for(ndone=0; ; ndone++){
  673. a = findbl(arg, narg, &na);
  674. if(a == arg){
  675. if(ndone==0 && et->col!=nil)
  676. winsettag(coladd(et->col, nil, nil, -1));
  677. break;
  678. }
  679. nf = narg-na;
  680. f = runemalloc(nf);
  681. runemove(f, arg, nf);
  682. rs = dirname(et, f, nf);
  683. f = rs.r;
  684. nf = rs.nr;
  685. memset(&e, 0, sizeof e);
  686. e.name = f;
  687. e.nname = nf;
  688. e.bname = runetobyte(f, nf);
  689. e.jump = TRUE;
  690. openfile(et, &e);
  691. free(f);
  692. free(e.bname);
  693. arg = skipbl(a, na, &narg);
  694. }
  695. }