]> git.neil.brown.name Git - edlib.git/blob - doc-multipart.c
core-log: reduce number of statics
[edlib.git] / doc-multipart.c
1 /*
2  * Copyright Neil Brown ©2016-2023 <neil@brown.name>
3  * May be distributed under terms of GPLv2 - see file:COPYING
4  *
5  * doc-multipart: Present a sequence of documents as though it were
6  *   just one.
7  *   This is used for stitching together the parts of a MIME email message.
8  *
9  * The document is created empty, and then given subordinate documents
10  * using a "multipart-add" command which causes the "focus" to be added
11  * to a list.
12  * If more sophisticated edits are needed, they can come later.
13  */
14
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <fcntl.h>
18 #include <string.h>
19 #include <stdio.h>
20
21 #define PRIVATE_DOC_REF
22
23 struct doc_ref {
24         struct mark *m;
25         int docnum; /* may be 'nparts', in which case 'm' == NULL */
26 };
27
28 /* mark->mdata in marks we create on individual component documents
29  * is used to track if the mark is shared among multiple marks in the
30  * multipart document.
31  */
32 #define GET_REFS(_mark) ((unsigned long)((_mark)->mdata))
33 #define SET_REFS(_mark, val) ((_mark)->mdata = (void*)(unsigned long)(val))
34 #define ADD_REFS(_mark, inc) SET_REFS(_mark, GET_REFS(_mark) + (inc))
35
36 #define DOC_DATA_TYPE struct mp_info
37
38 #define DOC_NEXT(p,m,r,b) multipart_next_prev(p, m, r, 1, b, ci->str)
39 #define DOC_PREV(p,m,r,b) multipart_next_prev(p, m, r, 0, b, ci->str)
40 #define DOC_NEXT_DECL(p,m,r,b) multipart_next_prev(p, m, r, int forward, b, const char *str)
41 #define DOC_PREV_DECL(p,m,r,b) multipart_next_prev(p, m, r, int forward, b, const char *str)
42 #include "core.h"
43
44 struct mp_info {
45         struct doc      doc;
46         int             nparts;
47         int             parts_size;
48         struct part {
49                 struct pane     *pane;
50         } *parts;
51 };
52 #include "core-pane.h"
53
54 static struct map *mp_map safe;
55
56 /* Before moving a mark, we make sure m->ref.m is not shared.
57  * After moving, we make sure the mark is correctly ordered among
58  * siblings, and then share if m->ref.m should be shared.
59  */
60 static void pre_move(struct mark *m safe)
61 {
62         struct mark *m2;
63
64         if (!m->ref.m || GET_REFS(m->ref.m) == 1)
65                 return;
66         /* Mark is shared, make it unshared */
67         m2 = mark_dup(m->ref.m);
68         ADD_REFS(m->ref.m, -1);
69         SET_REFS(m2, 1);
70         m->ref.m = m2;
71 }
72
73 static void post_move(struct mark *m)
74 {
75         /* m->ref.m might have moved.  If so, move m in the list of
76          * marks so marks in this document are still properly ordered
77          * Then ensure that if neighbouring marks are at same location,
78          * they use same marks.
79          */
80         struct mark *m2, *mtarget;
81
82         if (!m || hlist_unhashed(&m->all))
83                 return;
84         ASSERT(m->ref.m == NULL || GET_REFS(m->ref.m) == 1);
85         mtarget = m;
86         while ((m2 = mark_next(mtarget)) != NULL &&
87                (m2->ref.docnum < m->ref.docnum ||
88                 (m2->ref.docnum == m->ref.docnum &&
89                  m2->ref.m && m->ref.m &&
90                  m2->ref.m->seq < m->ref.m->seq)))
91                 mtarget = m2;
92         if (mtarget != m)
93                 /* m should be after mtarget */
94                 mark_to_mark_noref(m, mtarget);
95
96         mtarget = m;
97         while ((m2 = mark_prev(mtarget)) != NULL &&
98                (m2->ref.docnum > m->ref.docnum||
99                 (m2->ref.docnum == m->ref.docnum &&
100                  m2->ref.m && m->ref.m &&
101                  m2->ref.m->seq > m->ref.m->seq)))
102                 mtarget = m2;
103         if (mtarget != m)
104                 /* m should be before mtarget */
105                 mark_to_mark_noref(m, mtarget);
106
107         if (!m->ref.m)
108                 return;
109         ASSERT(GET_REFS(m->ref.m) == 1);
110         /* Check if it should be shared */
111         m2 = mark_next(m);
112         if (m2 && m2->ref.docnum == m->ref.docnum && m2->ref.m) {
113                 if (m->ref.m != m2->ref.m &&
114                     mark_same(m->ref.m, m2->ref.m)) {
115                         SET_REFS(m->ref.m, 0);
116                         mark_free(m->ref.m);
117                         m->ref.m = m2->ref.m;
118                         ADD_REFS(m->ref.m, 1);
119                         return;
120                 }
121         }
122         m2 = mark_prev(m);
123         if (m2 && m2->ref.docnum == m->ref.docnum && m2->ref.m) {
124                 if (m->ref.m != m2->ref.m &&
125                     mark_same(m->ref.m, m2->ref.m)) {
126                         SET_REFS(m->ref.m, 0);
127                         mark_free(m->ref.m);
128                         m->ref.m = m2->ref.m;
129                         ADD_REFS(m->ref.m, 1);
130                         return;
131                 }
132         }
133 }
134
135 static void mp_mark_refcnt(struct mark *m safe, int inc)
136 {
137         if (!m->ref.m)
138                 return;
139
140         if (inc > 0)
141                 /* Duplicate being created of this mark */
142                 ADD_REFS(m->ref.m, 1);
143
144         if (inc < 0) {
145                 /* mark is being discarded, or ref over-written */
146                 ADD_REFS(m->ref.m, -1);
147                 if (GET_REFS(m->ref.m) == 0)
148                         mark_free(m->ref.m);
149                 m->ref.m = NULL;
150         }
151 }
152
153 static void mp_check_consistent(struct mp_info *mpi safe)
154 {
155 //      struct mark *m;
156         struct doc *d = &mpi->doc;
157 //      int s = -1;
158
159         doc_check_consistent(d);
160 #if 0
161         for (m = mark_first(d); m; m = mark_next(m)) {
162                 if (!m->ref.m || m->ref.m->seq <= s) {
163                         for (m = mark_first(d); m;
164                              m = mark_next(m))
165                                 if (m && m->ref.m)
166                                         printf("%p %d %d\n", m, m->seq,
167                                                m->ref.m->seq);
168
169                         abort();
170                 }
171                 s = m->ref.m->seq;
172         }
173         doc_check_consistent(d);
174 #endif
175 }
176
177 static void change_part(struct mp_info *mpi safe, struct mark *m safe,
178                         int part, int end)
179 {
180         struct mark *m1;
181         struct part *p;
182
183         if (part < 0 || part > mpi->nparts || !mpi->parts)
184                 return;
185         if (m->ref.m) {
186                 ASSERT(GET_REFS(m->ref.m) == 1);
187                 SET_REFS(m->ref.m, 0);
188                 mark_free(m->ref.m);
189                 m->ref.m = NULL;
190         }
191         if (part < mpi->nparts && (p = &mpi->parts[part]) && p->pane) {
192                 m1 = mark_new(p->pane);
193                 if (m1) {
194                         call("doc:set-ref", p->pane, !end, m1);
195                         m->ref.m = m1;
196                         SET_REFS(m1, 1);
197                 }
198         } else
199                 m->ref.m = NULL;
200         m->ref.docnum = part;
201 }
202
203 static void mp_normalize(struct mp_info *mpi safe, struct mark *m safe,
204                          const char *vis)
205 {
206         /* If points the end of a document, point to the start
207          * of the next instead.
208          */
209         struct part *p;
210         if (!mpi->parts)
211                 return;
212         while (m->ref.m && (p = &mpi->parts[m->ref.docnum]) && p->pane &&
213                doc_following(p->pane, m->ref.m) == WEOF) {
214                 int n = m->ref.docnum + 1;
215                 while (n < mpi->nparts && vis && vis[n] == 'i')
216                         n += 1;
217                 change_part(mpi, m, n, 0);
218         }
219 }
220
221 DEF_CMD(mp_close)
222 {
223         struct mp_info *mpi = ci->home->doc_data;
224         int i;
225         struct mark *m;
226
227         for (m = mark_first(&mpi->doc); m ; m = mark_next(m))
228                 if (m->ref.m) {
229                         struct mark *m2 = m->ref.m;
230                         m->ref.m = NULL;
231                         ADD_REFS(m2, -1);
232                         if (GET_REFS(m2) == 0)
233                                 mark_free(m2);
234                 }
235         if (!mpi->parts)
236                 return Efallthrough;
237         for (i = 0; i < mpi->nparts; i++) {
238                 struct pane *p = mpi->parts[i].pane;
239                 if (p)
240                         call("doc:closed", p);
241         }
242         free(mpi->parts);
243         mpi->parts = NULL;
244         return Efallthrough;
245 }
246
247 DEF_CMD(mp_set_ref)
248 {
249         struct mp_info *mpi = ci->home->doc_data;
250         const char *vis = ci->str && (int)strlen(ci->str) >= mpi->nparts ?
251                 ci->str : NULL;
252         int ret = 1;
253
254         if (!ci->mark)
255                 return Enoarg;
256
257         /* Need to trigger a point:moved notification.  FIXME I wonder
258          * if this can be simpler
259          */
260         mark_step(ci->mark, 0);
261
262         if (!ci->mark->ref.m && !ci->mark->ref.docnum) {
263                 /* First time set-ref was called */
264                 pre_move(ci->mark);
265                 change_part(mpi, ci->mark, 0, 0);
266                 mark_to_end(ci->home, ci->mark, 0);
267                 post_move(ci->mark);
268         }
269         pre_move(ci->mark);
270
271         if (ci->num == 1) {
272                 /* start */
273                 int n = 0;
274                 while (n < mpi->nparts && vis && vis[n] == 'i')
275                         n += 1;
276                 change_part(mpi, ci->mark, n, 0);
277                 mp_normalize(mpi, ci->mark, vis);
278         } else
279                 change_part(mpi, ci->mark, mpi->nparts, 1);
280
281         post_move(ci->mark);
282         mp_check_consistent(mpi);
283         return ret;
284 }
285
286 static inline wint_t multipart_next_prev(struct pane *home safe, struct mark *mark safe,
287                                          struct doc_ref *r safe,
288                                          int forward, bool bytes, const char *str)
289 {
290         int move = r == &mark->ref;
291         struct mp_info *mpi = home->doc_data;
292         struct mark *m1 = NULL;
293         struct mark *m = mark;
294         const char *vis = str && (int)strlen(str) >= mpi->nparts ?
295                 str : NULL;
296         int n;
297         int ret;
298
299         /* Document access commands are handled by the 'cropper'.  First
300          * we need to substitute the marks, then call the cropper which
301          * calls the document.  Then make sure the marks are still in
302          * order.
303          */
304
305         mp_check_consistent(mpi);
306
307         if (move) {
308                 mark_step(m, forward);
309                 pre_move(m);
310         }
311
312         m1 = m->ref.m;
313
314         if (m->ref.docnum >= mpi->nparts || !mpi->parts)
315                 ret = -1;
316         else
317                 ret = home_call(mpi->parts[m->ref.docnum].pane,
318                                 "doc:char", home,
319                                 move ? (forward ? 1 : -1) : 0,
320                                 m1, str,
321                                 move ? 0 : (forward ? 1 : -1),
322                                 NULL, NULL);
323         while (ret == CHAR_RET(WEOF) || ret == -1) {
324                 if (!move && m == mark) {
325                         /* don't change mark when not moving */
326                         m = mark_dup(m);
327                         pre_move(m);
328                 }
329                 if (forward) {
330                         if (m->ref.docnum >= mpi->nparts)
331                                 break;
332                         n = m->ref.docnum + 1;
333                         while (n < mpi->nparts && vis && vis[n] == 'i')
334                                 n += 1;
335                         change_part(mpi, m, n, 0);
336                 } else {
337                         n = m->ref.docnum - 1;
338                         while (n >= 0 && vis && vis[n] == 'i')
339                                 n -= 1;
340                         if (n < 0)
341                                 break;
342                         change_part(mpi, m, n, 1);
343                 }
344                 m1 = m->ref.m;
345                 if (m->ref.docnum >= mpi->nparts || !mpi->parts)
346                         ret = -1;
347                 else
348                         ret = home_call(mpi->parts[m->ref.docnum].pane,
349                                         "doc:char", home,
350                                         move ? (forward ? 1 : -1) : 0,
351                                         m1, str,
352                                         move ? 0 : (forward ? 1 : -1));
353         }
354         if (move) {
355                 mp_normalize(mpi, mark, vis);
356                 post_move(mark);
357         }
358
359         if (m != mark)
360                 mark_free(m);
361
362         mp_check_consistent(mpi);
363         return ret == -1 ? (int)CHAR_RET(WEOF) : ret;
364 }
365
366 DEF_CMD(mp_char)
367 {
368         return do_char_byte(ci);
369 }
370
371 DEF_CMD(mp_step_part)
372 {
373         /* Step forward or backward to part boundary.
374          * Stepping forward takes us to start of next part.
375          * Stepping backward takes us to start of this
376          * part - we might not move.
377          * if ->num is -1, step to start of previous part
378          * Return part number plus 1.
379          * If ->str is given, only consider visible parts.
380          */
381         struct mp_info *mpi = ci->home->doc_data;
382         struct mark *m = ci->mark;
383         const char *vis = ci->str && (int)strlen(ci->str) >= mpi->nparts ?
384                 ci->str : NULL;
385         int start;
386         int first_vis;
387         int n;
388
389         if (!m)
390                 return Enoarg;
391         pre_move(m);
392         start = m->ref.docnum;
393         n = start;
394         if (ci->num > 0) {
395                 /* Forward - start of next part */
396                 n += 1;
397                 while (n < mpi->nparts && vis && vis[n] == 'i')
398                         n += 1;
399         } else if (ci->num < 0) {
400                 /* Backward - start of prev part */
401                 n -= 1;
402                 while (n >= 0 && vis && vis[n] == 'i')
403                         n -= 1;
404                 if (n < 0)
405                         n = m->ref.docnum;
406         }
407         /* otherwise start of this part */
408         change_part(mpi, m, n, 0);
409
410         /* If this part is empty, need to move to next visible part */
411         mp_normalize(mpi, m, vis);
412         first_vis = 0;
413         while (vis && vis[first_vis] == 'i')
414                 first_vis++;
415         while (ci->num < 0 && m->ref.docnum == start && n > first_vis) {
416                 /* didn't move - must have an empty part, try further */
417                 n -= 1;
418                 change_part(mpi, m, n, 0);
419                 mp_normalize(mpi, m, vis);
420         }
421         post_move(m);
422         if (ci->num && start == m->ref.docnum)
423                 return Efail;
424         return m->ref.docnum + 1;
425 }
426
427 DEF_CMD(mp_get_boundary)
428 {
429         /* return a mark past which rendering must not go. */
430         struct mark *m = ci->mark;
431
432         if (!m || !ci->comm2)
433                 return Enoarg;
434         m = mark_dup(m);
435         call("doc:step-part", ci->home, ci->num, m, ci->str);
436         comm_call(ci->comm2, "cb", ci->focus, 0, m);
437         return 1;
438 }
439
440 struct mp_cb {
441         struct command c;
442         struct command *cb;
443         struct pane *p safe;
444         struct mark *m safe;
445         int last_ret;
446 };
447
448 DEF_CB(mp_content_cb)
449 {
450         struct mp_cb *c = container_of(ci->comm, struct mp_cb, c);
451         struct mark *m1 = NULL;
452
453         if (ci->mark) {
454                 m1 = c->m;
455                 pre_move(m1);
456                 if (m1->ref.m)
457                         mark_to_mark(m1->ref.m, ci->mark);
458                 post_move(m1);
459         }
460
461         c->last_ret = comm_call(c->cb, ci->key, c->p,
462                                 ci->num, m1, ci->str,
463                                 ci->num2, NULL, ci->str2,
464                                 ci->x, ci->y);
465         return c->last_ret;
466 }
467
468 DEF_CMD(mp_content)
469 {
470         /* Call doc:content on any visible docs in the range.
471          * Callback must re-wrap any marks
472          */
473         struct mp_info *mpi = ci->home->doc_data;
474         struct mp_cb cb;
475         struct mark *m, *m2;
476         const char *invis = ci->str;
477         int ret = 1;
478
479         if (!ci->mark || !ci->comm2)
480                 return Enoarg;
481         m = mark_dup(ci->mark);
482         m2 = ci->mark2;
483         cb.last_ret = 1;
484         while (cb.last_ret > 0 && m->ref.docnum < mpi->nparts &&
485                mpi->parts &&
486                (!m2 || m->ref.docnum <= m2->ref.docnum)) {
487                 /* Need to call doc:content on this document */
488                 int n = m->ref.docnum;
489                 if ((!invis || invis[n] != 'i') && m->ref.m) {
490                         struct mark *m2a = NULL;
491                         struct mark *mtmp = NULL;
492                         cb.c = mp_content_cb;
493                         cb.cb = ci->comm2;
494                         cb.p = ci->focus;
495                         cb.m = m;
496
497                         if (m->ref.m)
498                                 mtmp = mark_dup(m->ref.m);
499                         if (m2 && m2->ref.docnum == n && m2->ref.m)
500                                 m2a = mark_dup(m2->ref.m);
501
502                         ret = home_call_comm(mpi->parts[n].pane,
503                                              ci->key, ci->home, &cb.c,
504                                              ci->num, mtmp, NULL,
505                                              ci->num2, m2a);
506                         mark_free(m2a);
507                         mark_free(mtmp);
508                         if (ret < 0)
509                                 break;
510                 }
511                 if (cb.last_ret > 0) {
512                         pre_move(m);
513                         change_part(mpi, m, n+1, 0);
514                         post_move(m);
515                 }
516         }
517         mark_free(m);
518         return ret;
519 }
520
521 DEF_CMD(mp_attr)
522 {
523         struct mp_info *mpi = ci->home->doc_data;
524         struct mark *m1 = NULL;
525         struct part *p;
526         int ret = Efallthrough;
527         int d;
528         const char *attr = ci->str;
529
530         if (!ci->mark || !attr)
531                 return Enoarg;
532
533         m1 = ci->mark->ref.m;
534         d = ci->mark->ref.docnum;
535
536         if (d < mpi->nparts && m1 && mpi->parts && (p = &mpi->parts[d]) &&
537             p->pane &&  doc_following(p->pane, m1) == WEOF)
538                 /* at the wrong end of a part */
539                 d += 1;
540
541         if (strstarts(attr, "multipart-next:")) {
542                 d += 1;
543                 attr += 15;
544                 if (d >= mpi->nparts)
545                         return 1;
546         } else if (strstarts(attr, "multipart-prev:")) {
547                 d -= 1;
548                 attr += 15;
549                 if (d < 0)
550                         return 1;
551         } else if (strstarts(attr, "multipart-this:"))
552                 attr += 15;
553
554         if (strcmp(attr, "multipart:part-num") == 0) {
555                 char n[11];
556                 snprintf(n, sizeof(n), "%d", d);
557                 comm_call(ci->comm2, "callback:get_attr", ci->focus,
558                           0, ci->mark, n, 0, NULL, attr);
559                 return 1;
560         }
561
562         if (d >= mpi->nparts || d < 0 || !mpi->parts)
563                 return 1;
564
565         if (attr != ci->str) {
566                 /* Get a pane attribute, not char attribute */
567                 char *s = pane_attr_get(mpi->parts[d].pane, attr);
568                 if (s)
569                         return comm_call(ci->comm2, "callback", ci->focus,
570                                          0, ci->mark, s, 0, NULL, ci->str);
571                 return 1;
572         }
573
574         p = &mpi->parts[d];
575         if (d != ci->mark->ref.docnum && p->pane) {
576                 m1 = mark_new(p->pane);
577                 call("doc:set-ref", p->pane,
578                      (d > ci->mark->ref.docnum), m1);
579         }
580
581         if (p->pane)
582                 ret = home_call(p->pane,
583                                 ci->key, ci->focus, ci->num, m1, ci->str,
584                                 ci->num2, NULL, ci->str2, 0,0, ci->comm2);
585         if (d != ci->mark->ref.docnum)
586                 mark_free(m1);
587         return ret;
588 }
589
590 DEF_CMD(mp_set_attr)
591 {
592         struct mp_info *mpi = ci->home->doc_data;
593         struct part *p;
594         struct mark *m = ci->mark;
595         struct mark *m1;
596         int dn;
597         const char *attr = ci->str;
598
599         if (!attr)
600                 return Enoarg;
601         if (!m)
602                 return Efallthrough;
603         if (!mpi->parts)
604                 return Efail;
605         dn = m->ref.docnum;
606         m1 = m->ref.m;
607
608         if (strstarts(attr, "multipart-")) {
609                 /* Set an attribute on a part */
610                 if (strstarts(attr, "multipart-prev:") &&
611                     dn > 0 && (p = &mpi->parts[dn-1]) && p->pane)
612                         attr_set_str(&p->pane->attrs,
613                                      attr+15, ci->str2);
614                 else if (strstarts(attr, "multipart-next:") &&
615                          dn < mpi->nparts && (p = &mpi->parts[dn+1]) && p->pane)
616                         attr_set_str(&p->pane->attrs,
617                                      attr+15, ci->str2);
618                 else if (strstarts(attr, "multipart-this:") &&
619                          (p = &mpi->parts[dn]) && p->pane)
620                         attr_set_str(&p->pane->attrs,
621                                      attr+15, ci->str2);
622                 else
623                         return Efail;
624                 return 1;
625         }
626         /* Send the request to a sub-document */
627         p = &mpi->parts[dn];
628         if (p->pane)
629                 return call(ci->key, p->pane, ci->num, m1, ci->str,
630                             0, NULL, ci->str2);
631         return Efail;
632 }
633
634 DEF_CMD(mp_notify_close)
635 {
636         /* sub-document has been closed.
637          * Can we survive? or should we just shut down?
638          */
639         struct mp_info *mpi = ci->home->doc_data;
640         int i;
641
642         for (i = 0; i < mpi->nparts && mpi->parts; i++)
643                 if (mpi->parts[i].pane == ci->focus) {
644                         /* sub-document has been closed.
645                          * Can we survive? or should we just shut down?
646                          */
647                         mpi->parts[i].pane = NULL;
648                         pane_close(ci->home);
649                         return 1;
650                 }
651         /* Not a sub-pane, maybe an owner for vmarks */
652         return Efallthrough;
653 }
654
655 DEF_CMD(mp_notify_viewers)
656 {
657         /* The autoclose document wants to know if it should close.
658          * tell it "no" */
659         return 1;
660 }
661
662 DEF_CMD(mp_doc_replaced)
663 {
664         /* Something changed in a component, report that the
665          * whole doc changed - simplest for now.
666          */
667         pane_notify("doc:replaced", ci->home);
668         return 1;
669 }
670
671 static void mp_resize(struct mp_info *mpi safe, int size)
672 {
673         if (mpi->parts_size >= size)
674                 return;
675         size += 4;
676         mpi->parts = realloc(mpi->parts, size * sizeof(struct part));
677         mpi->parts_size = size;
678 }
679
680 DEF_CMD(mp_add)
681 {
682         struct mp_info *mpi = ci->home->doc_data;
683         struct mark *m;
684         int n;
685
686         mp_resize(mpi, mpi->nparts+1);
687         if (ci->mark == NULL)
688                 n = mpi->nparts;
689         else
690                 n = ci->mark->ref.docnum;
691         memmove(&mpi->parts[n+1], &mpi->parts[n],
692                 (mpi->nparts - n)*sizeof(mpi->parts[0]));
693         mpi->nparts += 1;
694         mpi->parts[n].pane = ci->focus;
695         hlist_for_each_entry(m, &mpi->doc.marks, all)
696                 if (m->ref.docnum >= n)
697                         m->ref.docnum ++;
698         if (ci->mark)
699                 /* move mark to start of new part */
700                 change_part(mpi, ci->mark, n, 0);
701
702         pane_add_notify(ci->home, ci->focus, "Notify:Close");
703         home_call(ci->focus, "doc:request:doc:notify-viewers", ci->home);
704         home_call(ci->focus, "doc:request:doc:replaced", ci->home);
705
706         return 1;
707 }
708
709 DEF_CMD(mp_forward_by_num)
710 {
711         struct mp_info *mpi = ci->home->doc_data;
712         struct mark *m1 = NULL, *m2 = NULL;
713         struct part *p;
714         const char *key;
715         int d;
716         int ret;
717
718         key = ksuffix(ci, "doc:multipart-");
719         d = atoi(key);
720         key = strchr(key, '-');
721         if (!key)
722                 return Einval;
723         key += 1;
724
725         if (d >= mpi->nparts || d < 0 || !mpi->parts)
726                 return 1;
727
728         if (ci->mark && ci->mark->ref.docnum == d)
729                 m1 = ci->mark->ref.m;
730         if (ci->mark2 && ci->mark2->ref.docnum == d)
731                 m2 = ci->mark2->ref.m;
732
733         p = &mpi->parts[d];
734         if (p->pane)
735                 ret = call(key, p->pane, ci->num, m1, ci->str,
736                            ci->num2, m2, ci->str2, ci->x, ci->y, ci->comm2);
737         else
738                 ret = Efail;
739         return ret;
740 }
741
742 DEF_CMD(mp_get_part)
743 {
744         struct mp_info *mpi = ci->home->doc_data;
745         struct part *p;
746         int d = ci->num;
747
748         if (d < 0 || d >= mpi->nparts || !mpi->parts)
749                 return Einval;
750         p = &mpi->parts[d];
751         if (p->pane)
752                 comm_call(ci->comm2, "cb", p->pane);
753         return 1;
754 }
755
756 DEF_CMD(mp_forward)
757 {
758         /* forward this command to this/next/prev document based on
759          * ci->mark2.
760          * ci->mark is forwarded if it is in same document
761          */
762         struct mp_info *mpi = ci->home->doc_data;
763         struct part *p;
764         struct mark *m1, *m2;
765         const char *key;
766         int d;
767
768         if (!ci->mark2)
769                 return Enoarg;
770         if (!mpi->parts)
771                 return Efail;
772         m2 = ci->mark2->ref.m;
773         d = ci->mark2->ref.docnum;
774
775         if (d < mpi->nparts && m2 && (p = &mpi->parts[d]) && p->pane &&
776             doc_following(p->pane, m2) == WEOF)
777                 /* at the wrong end of a part */
778                 d += 1;
779
780         if ((key = ksuffix(ci, "multipart-next:"))[0]) {
781                 d += 1;
782                 if (d >= mpi->nparts)
783                         return 1;
784         } else if ((key = ksuffix(ci, "multipart-prev:"))[0]) {
785                 d -= 1;
786                 if (d < 0)
787                         return 1;
788         } else if ((key = ksuffix(ci, "multipart-this:"))[0]) {
789                 ;
790         } else return Einval;
791
792         if (d >= mpi->nparts || d < 0)
793                 return 1;
794
795         m1 = NULL;
796         if (ci->mark && ci->mark->ref.docnum == d)
797                 m1 = ci->mark->ref.m;
798         p = &mpi->parts[d];
799         if (p->pane)
800                 return call(key, p->pane, ci->num, m1, ci->str,
801                             ci->num2, NULL, ci->str2, 0,0, ci->comm2);
802         return Efail;
803 }
804
805 DEF_CMD(mp_val_marks)
806 {
807         struct mark *m1, *m2;
808
809         if (!ci->mark || !ci->mark2)
810                 return Enoarg;
811
812         if (ci->mark->ref.docnum < ci->mark2->ref.docnum)
813                 return 1;
814         if (ci->mark->ref.docnum > ci->mark2->ref.docnum) {
815                 LOG("mp_val_marks: docs not in order");
816                 return Efalse;
817         }
818
819         m1 = ci->mark->ref.m;
820         m2 = ci->mark->ref.m;
821         if (m1 == m2)
822                 return 1;
823         if (m1 && m2 && m1->seq > m2->seq) {
824                 LOG("mp_val_marks: subordinate marks out of order!");
825                 return Efalse;
826         }
827         if (!m1)
828                 LOG("mp_val_marks: m1 is NULL");
829         else if (!m2 || marks_validate(m1, m2))
830                 return 1;
831         return Efalse;
832 }
833
834 static void mp_init_map(void)
835 {
836         mp_map = key_alloc();
837         key_add_chain(mp_map, doc_default_cmd);
838         key_add(mp_map, "doc:set-ref", &mp_set_ref);
839         key_add(mp_map, "doc:char", &mp_char);
840         key_add(mp_map, "doc:content", &mp_content);
841         key_add(mp_map, "doc:content-bytes", &mp_content);
842         key_add(mp_map, "doc:get-attr", &mp_attr);
843         key_add(mp_map, "doc:set-attr", &mp_set_attr);
844         key_add(mp_map, "doc:step-part", &mp_step_part);
845         key_add(mp_map, "doc:get-boundary", &mp_get_boundary);
846         key_add(mp_map, "Close", &mp_close);
847         key_add(mp_map, "Notify:Close", &mp_notify_close);
848         key_add(mp_map, "doc:notify-viewers", &mp_notify_viewers);
849         key_add(mp_map, "doc:replaced", &mp_doc_replaced);
850         key_add(mp_map, "multipart-add", &mp_add);
851         key_add(mp_map, "debug:validate-marks", &mp_val_marks);
852         key_add(mp_map, "doc:multipart:get-part", &mp_get_part);
853         key_add_prefix(mp_map, "multipart-this:", &mp_forward);
854         key_add_prefix(mp_map, "multipart-next:", &mp_forward);
855         key_add_prefix(mp_map, "multipart-prev:", &mp_forward);
856         key_add_prefix(mp_map, "doc:multipart-", &mp_forward_by_num);
857 }
858 DEF_LOOKUP_CMD(mp_handle, mp_map);
859
860 DEF_CMD(attach_mp)
861 {
862         struct mp_info *mpi;
863         struct pane *h;
864
865         h = doc_register(ci->home, &mp_handle.c);
866         if (!h)
867                 return Efail;
868         mpi = h->doc_data;
869
870         mpi->doc.refcnt = mp_mark_refcnt;
871         attr_set_str(&h->attrs, "render-default", "text");
872         return comm_call(ci->comm2, "callback:doc", h);
873 }
874
875 void edlib_init(struct pane *ed safe)
876 {
877         mp_init_map();
878         call_comm("global-set-command", ed, &attach_mp, 0, NULL,
879                   "attach-doc-multipart");
880 }