2 * Copyright Neil Brown ©2016-2023 <neil@brown.name>
3 * May be distributed under terms of GPLv2 - see file:COPYING
5 * doc-multipart: Present a sequence of documents as though it were
7 * This is used for stitching together the parts of a MIME email message.
9 * The document is created empty, and then given subordinate documents
10 * using a "multipart-add" command which causes the "focus" to be added
12 * If more sophisticated edits are needed, they can come later.
21 #define PRIVATE_DOC_REF
25 int docnum; /* may be 'nparts', in which case 'm' == NULL */
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
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))
36 #define DOC_DATA_TYPE struct mp_info
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)
52 #include "core-pane.h"
54 static struct map *mp_map safe;
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.
60 static void pre_move(struct mark *m safe)
64 if (!m->ref.m || GET_REFS(m->ref.m) == 1)
66 /* Mark is shared, make it unshared */
67 m2 = mark_dup(m->ref.m);
68 ADD_REFS(m->ref.m, -1);
73 static void post_move(struct mark *m)
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.
80 struct mark *m2, *mtarget;
82 if (!m || hlist_unhashed(&m->all))
84 ASSERT(m->ref.m == NULL || GET_REFS(m->ref.m) == 1);
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)))
93 /* m should be after mtarget */
94 mark_to_mark_noref(m, mtarget);
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)))
104 /* m should be before mtarget */
105 mark_to_mark_noref(m, mtarget);
109 ASSERT(GET_REFS(m->ref.m) == 1);
110 /* Check if it should be shared */
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);
117 m->ref.m = m2->ref.m;
118 ADD_REFS(m->ref.m, 1);
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);
128 m->ref.m = m2->ref.m;
129 ADD_REFS(m->ref.m, 1);
135 static void mp_mark_refcnt(struct mark *m safe, int inc)
141 /* Duplicate being created of this mark */
142 ADD_REFS(m->ref.m, 1);
145 /* mark is being discarded, or ref over-written */
146 ADD_REFS(m->ref.m, -1);
147 if (GET_REFS(m->ref.m) == 0)
153 static void mp_check_consistent(struct mp_info *mpi safe)
156 struct doc *d = &mpi->doc;
159 doc_check_consistent(d);
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;
166 printf("%p %d %d\n", m, m->seq,
173 doc_check_consistent(d);
177 static void change_part(struct mp_info *mpi safe, struct mark *m safe,
183 if (part < 0 || part > mpi->nparts || !mpi->parts)
186 ASSERT(GET_REFS(m->ref.m) == 1);
187 SET_REFS(m->ref.m, 0);
191 if (part < mpi->nparts && (p = &mpi->parts[part]) && p->pane) {
192 m1 = mark_new(p->pane);
194 call("doc:set-ref", p->pane, !end, m1);
200 m->ref.docnum = part;
203 static void mp_normalize(struct mp_info *mpi safe, struct mark *m safe,
206 /* If points the end of a document, point to the start
207 * of the next instead.
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')
217 change_part(mpi, m, n, 0);
223 struct mp_info *mpi = ci->home->doc_data;
227 for (m = mark_first(&mpi->doc); m ; m = mark_next(m))
229 struct mark *m2 = m->ref.m;
232 if (GET_REFS(m2) == 0)
237 for (i = 0; i < mpi->nparts; i++) {
238 struct pane *p = mpi->parts[i].pane;
240 call("doc:closed", p);
249 struct mp_info *mpi = ci->home->doc_data;
250 const char *vis = ci->str && (int)strlen(ci->str) >= mpi->nparts ?
257 /* Need to trigger a point:moved notification. FIXME I wonder
258 * if this can be simpler
260 mark_step(ci->mark, 0);
262 if (!ci->mark->ref.m && !ci->mark->ref.docnum) {
263 /* First time set-ref was called */
265 change_part(mpi, ci->mark, 0, 0);
266 mark_to_end(ci->home, ci->mark, 0);
274 while (n < mpi->nparts && vis && vis[n] == 'i')
276 change_part(mpi, ci->mark, n, 0);
277 mp_normalize(mpi, ci->mark, vis);
279 change_part(mpi, ci->mark, mpi->nparts, 1);
282 mp_check_consistent(mpi);
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)
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 ?
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
305 mp_check_consistent(mpi);
308 mark_step(m, forward);
314 if (m->ref.docnum >= mpi->nparts || !mpi->parts)
317 ret = home_call(mpi->parts[m->ref.docnum].pane,
319 move ? (forward ? 1 : -1) : 0,
321 move ? 0 : (forward ? 1 : -1),
323 while (ret == CHAR_RET(WEOF) || ret == -1) {
324 if (!move && m == mark) {
325 /* don't change mark when not moving */
330 if (m->ref.docnum >= mpi->nparts)
332 n = m->ref.docnum + 1;
333 while (n < mpi->nparts && vis && vis[n] == 'i')
335 change_part(mpi, m, n, 0);
337 n = m->ref.docnum - 1;
338 while (n >= 0 && vis && vis[n] == 'i')
342 change_part(mpi, m, n, 1);
345 if (m->ref.docnum >= mpi->nparts || !mpi->parts)
348 ret = home_call(mpi->parts[m->ref.docnum].pane,
350 move ? (forward ? 1 : -1) : 0,
352 move ? 0 : (forward ? 1 : -1));
355 mp_normalize(mpi, mark, vis);
362 mp_check_consistent(mpi);
363 return ret == -1 ? (int)CHAR_RET(WEOF) : ret;
368 return do_char_byte(ci);
371 DEF_CMD(mp_step_part)
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.
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 ?
392 start = m->ref.docnum;
395 /* Forward - start of next part */
397 while (n < mpi->nparts && vis && vis[n] == 'i')
399 } else if (ci->num < 0) {
400 /* Backward - start of prev part */
402 while (n >= 0 && vis && vis[n] == 'i')
407 /* otherwise start of this part */
408 change_part(mpi, m, n, 0);
410 /* If this part is empty, need to move to next visible part */
411 mp_normalize(mpi, m, vis);
413 while (vis && vis[first_vis] == 'i')
415 while (ci->num < 0 && m->ref.docnum == start && n > first_vis) {
416 /* didn't move - must have an empty part, try further */
418 change_part(mpi, m, n, 0);
419 mp_normalize(mpi, m, vis);
422 if (ci->num && start == m->ref.docnum)
424 return m->ref.docnum + 1;
427 DEF_CMD(mp_get_boundary)
429 /* return a mark past which rendering must not go. */
430 struct mark *m = ci->mark;
432 if (!m || !ci->comm2)
435 call("doc:step-part", ci->home, ci->num, m, ci->str);
436 comm_call(ci->comm2, "cb", ci->focus, 0, m);
448 DEF_CB(mp_content_cb)
450 struct mp_cb *c = container_of(ci->comm, struct mp_cb, c);
451 struct mark *m1 = NULL;
457 mark_to_mark(m1->ref.m, ci->mark);
461 c->last_ret = comm_call(c->cb, ci->key, c->p,
462 ci->num, m1, ci->str,
463 ci->num2, NULL, ci->str2,
470 /* Call doc:content on any visible docs in the range.
471 * Callback must re-wrap any marks
473 struct mp_info *mpi = ci->home->doc_data;
476 const char *invis = ci->str;
479 if (!ci->mark || !ci->comm2)
481 m = mark_dup(ci->mark);
484 while (cb.last_ret > 0 && m->ref.docnum < mpi->nparts &&
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;
498 mtmp = mark_dup(m->ref.m);
499 if (m2 && m2->ref.docnum == n && m2->ref.m)
500 m2a = mark_dup(m2->ref.m);
502 ret = home_call_comm(mpi->parts[n].pane,
503 ci->key, ci->home, &cb.c,
511 if (cb.last_ret > 0) {
513 change_part(mpi, m, n+1, 0);
523 struct mp_info *mpi = ci->home->doc_data;
524 struct mark *m1 = NULL;
526 int ret = Efallthrough;
528 const char *attr = ci->str;
530 if (!ci->mark || !attr)
533 m1 = ci->mark->ref.m;
534 d = ci->mark->ref.docnum;
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 */
541 if (strstarts(attr, "multipart-next:")) {
544 if (d >= mpi->nparts)
546 } else if (strstarts(attr, "multipart-prev:")) {
551 } else if (strstarts(attr, "multipart-this:"))
554 if (strcmp(attr, "multipart:part-num") == 0) {
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);
562 if (d >= mpi->nparts || d < 0 || !mpi->parts)
565 if (attr != ci->str) {
566 /* Get a pane attribute, not char attribute */
567 char *s = pane_attr_get(mpi->parts[d].pane, attr);
569 return comm_call(ci->comm2, "callback", ci->focus,
570 0, ci->mark, s, 0, NULL, ci->str);
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);
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)
592 struct mp_info *mpi = ci->home->doc_data;
594 struct mark *m = ci->mark;
597 const char *attr = ci->str;
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,
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,
618 else if (strstarts(attr, "multipart-this:") &&
619 (p = &mpi->parts[dn]) && p->pane)
620 attr_set_str(&p->pane->attrs,
626 /* Send the request to a sub-document */
629 return call(ci->key, p->pane, ci->num, m1, ci->str,
634 DEF_CMD(mp_notify_close)
636 /* sub-document has been closed.
637 * Can we survive? or should we just shut down?
639 struct mp_info *mpi = ci->home->doc_data;
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?
647 mpi->parts[i].pane = NULL;
648 pane_close(ci->home);
651 /* Not a sub-pane, maybe an owner for vmarks */
655 DEF_CMD(mp_notify_viewers)
657 /* The autoclose document wants to know if it should close.
662 DEF_CMD(mp_doc_replaced)
664 /* Something changed in a component, report that the
665 * whole doc changed - simplest for now.
667 pane_notify("doc:replaced", ci->home);
671 static void mp_resize(struct mp_info *mpi safe, int size)
673 if (mpi->parts_size >= size)
676 mpi->parts = realloc(mpi->parts, size * sizeof(struct part));
677 mpi->parts_size = size;
682 struct mp_info *mpi = ci->home->doc_data;
686 mp_resize(mpi, mpi->nparts+1);
687 if (ci->mark == NULL)
690 n = ci->mark->ref.docnum;
691 memmove(&mpi->parts[n+1], &mpi->parts[n],
692 (mpi->nparts - n)*sizeof(mpi->parts[0]));
694 mpi->parts[n].pane = ci->focus;
695 hlist_for_each_entry(m, &mpi->doc.marks, all)
696 if (m->ref.docnum >= n)
699 /* move mark to start of new part */
700 change_part(mpi, ci->mark, n, 0);
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);
709 DEF_CMD(mp_forward_by_num)
711 struct mp_info *mpi = ci->home->doc_data;
712 struct mark *m1 = NULL, *m2 = NULL;
718 key = ksuffix(ci, "doc:multipart-");
720 key = strchr(key, '-');
725 if (d >= mpi->nparts || d < 0 || !mpi->parts)
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;
735 ret = call(key, p->pane, ci->num, m1, ci->str,
736 ci->num2, m2, ci->str2, ci->x, ci->y, ci->comm2);
744 struct mp_info *mpi = ci->home->doc_data;
748 if (d < 0 || d >= mpi->nparts || !mpi->parts)
752 comm_call(ci->comm2, "cb", p->pane);
758 /* forward this command to this/next/prev document based on
760 * ci->mark is forwarded if it is in same document
762 struct mp_info *mpi = ci->home->doc_data;
764 struct mark *m1, *m2;
772 m2 = ci->mark2->ref.m;
773 d = ci->mark2->ref.docnum;
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 */
780 if ((key = ksuffix(ci, "multipart-next:"))[0]) {
782 if (d >= mpi->nparts)
784 } else if ((key = ksuffix(ci, "multipart-prev:"))[0]) {
788 } else if ((key = ksuffix(ci, "multipart-this:"))[0]) {
790 } else return Einval;
792 if (d >= mpi->nparts || d < 0)
796 if (ci->mark && ci->mark->ref.docnum == d)
797 m1 = ci->mark->ref.m;
800 return call(key, p->pane, ci->num, m1, ci->str,
801 ci->num2, NULL, ci->str2, 0,0, ci->comm2);
805 DEF_CMD(mp_val_marks)
807 struct mark *m1, *m2;
809 if (!ci->mark || !ci->mark2)
812 if (ci->mark->ref.docnum < ci->mark2->ref.docnum)
814 if (ci->mark->ref.docnum > ci->mark2->ref.docnum) {
815 LOG("mp_val_marks: docs not in order");
819 m1 = ci->mark->ref.m;
820 m2 = ci->mark->ref.m;
823 if (m1 && m2 && m1->seq > m2->seq) {
824 LOG("mp_val_marks: subordinate marks out of order!");
828 LOG("mp_val_marks: m1 is NULL");
829 else if (!m2 || marks_validate(m1, m2))
834 static void mp_init_map(void)
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);
858 DEF_LOOKUP_CMD(mp_handle, mp_map);
865 h = doc_register(ci->home, &mp_handle.c);
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);
875 void edlib_init(struct pane *ed safe)
878 call_comm("global-set-command", ed, &attach_mp, 0, NULL,
879 "attach-doc-multipart");