look.c 14 KB

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