From 9f38630369baad837a61a149b6c48f068e6d41f2 Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Thu, 10 Dec 2015 11:45:35 +1100 Subject: [PATCH] Change doc_destroy to take a pane. This starts to reduce the number of doc_from_pane() callers. Signed-off-by: NeilBrown --- core-doc.c | 27 +++++++++++++++++++-------- core.h | 2 +- doc-dir.c | 4 +--- lib-popup.c | 12 ++++++++++-- mode-emacs.c | 5 +---- 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/core-doc.c b/core-doc.c index 63c780d4..1d545376 100644 --- a/core-doc.c +++ b/core-doc.c @@ -32,6 +32,8 @@ struct doc_ref { #include "core.h" +static int do_doc_destroy(struct doc *d); + static int do_doc_add_view(struct doc *d, struct command *c, int size) { struct docview *g; @@ -446,6 +448,8 @@ DEF_CMD(doc_handle) return comm_call7(ci->comm2, "callback:vmark", ci->focus, 0, m, NULL, 0, NULL, m2); } + if (strcmp(ci->key, "doc:destroy") == 0) + return do_doc_destroy(dd->doc); ret = key_lookup(dd->doc->map, ci); ret = ret ?: key_lookup(doc_default_cmd, ci); @@ -593,7 +597,7 @@ struct pane *doc_from_text(struct pane *parent, char *name, char *text) doc_set_name(d, name); p = doc_attach_view(parent, d->home, NULL); if (!p) { - doc_destroy(d); + do_doc_destroy(d); return p; } doc_replace(p, NULL, text, &first); @@ -847,9 +851,7 @@ DEF_CMD(docs_open) DEF_CMD(docs_bury) { - struct doc *d = doc_from_pane(ci->home); - - doc_destroy(d); + doc_destroy(ci->home); return 1; } @@ -926,12 +928,13 @@ void doc_promote(struct doc *d) docs_attach(d); } -int doc_destroy(struct doc *d) +static int do_doc_destroy(struct doc *d) { /* If there are no views on the document, then unlink from * the documents list and destroy it. */ int i; + struct cmd_info ci = {0}; d->deleting = 1; if (d == d->ed->docs) @@ -943,13 +946,16 @@ int doc_destroy(struct doc *d) for (i = 0; i < d->nviews; i++) if (d->views[i].notify) /* still in use */ - return 0; + return -1; if (d == d->ed->docs) - return 0; + return -1; docs_release(d); - call3("doc:destroy", d->home, 0, 0); + ci.home = ci.focus = d->home; + ci.key = "doc:destroy"; + key_lookup(d->map, &ci); + pane_close(d->home); free(d->views); @@ -966,3 +972,8 @@ int doc_destroy(struct doc *d) free(d); return 1; } + +int doc_destroy(struct pane *p) +{ + return call3("doc:destroy", p, 0, 0); +} diff --git a/core.h b/core.h index 382251cc..d4ebf74d 100644 --- a/core.h +++ b/core.h @@ -109,7 +109,7 @@ struct pane *doc_attach(struct pane *parent, struct doc *d); void doc_set_name(struct doc *d, char *name); struct pane *doc_find(struct editor *ed, char *name); void doc_promote(struct doc *d); -int doc_destroy(struct doc *d); +int doc_destroy(struct pane *p); struct pane *render_attach(char *name, struct pane *parent); diff --git a/doc-dir.c b/doc-dir.c index 9bea6ec3..a61ce86e 100644 --- a/doc-dir.c +++ b/doc-dir.c @@ -549,9 +549,7 @@ DEF_CMD(dir_reread) DEF_CMD(dir_close) { - struct doc *d = doc_from_pane(ci->home); - - doc_destroy(d); + doc_destroy(ci->home); return 1; } diff --git a/lib-popup.c b/lib-popup.c index 83e85785..53914e65 100644 --- a/lib-popup.c +++ b/lib-popup.c @@ -63,8 +63,6 @@ DEF_CMD(popup_handle) struct popup_info *ppi = p->data; if (strcmp(ci->key, "Close") == 0) { - if (ppi->doc) - doc_destroy(ppi->doc); free(ppi); return 1; } @@ -76,6 +74,11 @@ DEF_CMD(popup_handle) if (strcmp(ci->key, "popup:Abort") == 0) { pane_focus(ppi->target); call3("Abort", ppi->target, 0, NULL); + if (ppi->doc) { + /* FIXME make this doc auto-close */ + ppi->doc = NULL; + doc_destroy(ci->focus); + } pane_close(ppi->popup); return 1; } @@ -95,6 +98,11 @@ DEF_CMD(popup_handle) key_handle_focus(&ci2); if (ppi->doc) free(ci2.str); + if (ppi->doc) { + /* FIXME make this doc auto-close */ + ppi->doc = NULL; + doc_destroy(ci->focus); + } pane_close(ppi->popup); return 1; } diff --git a/mode-emacs.c b/mode-emacs.c index 1ff65581..4479d480 100644 --- a/mode-emacs.c +++ b/mode-emacs.c @@ -531,10 +531,7 @@ DEF_CMD(emacs_num) DEF_CMD(emacs_kill_doc) { - struct doc *d = doc_from_pane(ci->home); - - if (d) - doc_destroy(d); + doc_destroy(ci->home); return 1; } -- 2.39.5