From 96a439f534f779f17b5474213ea9c4718681d6b7 Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Tue, 8 Dec 2015 14:52:27 +1100 Subject: [PATCH] Use callback to return string from doc:get-str This way it is clear how ownership of the string is managed. Signed-off-by: NeilBrown --- core-doc.c | 27 ++++++++++++++++++++++++++- core.h | 14 +------------- doc-text.c | 3 ++- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/core-doc.c b/core-doc.c index 9f7a6598..a967ed12 100644 --- a/core-doc.c +++ b/core-doc.c @@ -628,7 +628,7 @@ struct pane *doc_find(struct editor *ed, char *name) DEF_CMD(doc_attr_callback) { - struct call_return *cr = container_of(ci->comm, struct call_return , c); + struct call_return *cr = container_of(ci->comm, struct call_return, c); cr->s = ci->str; return 1; } @@ -652,6 +652,31 @@ char *doc_attr(struct pane *dp, struct mark *m, bool forward, char *attr) return cr.s; } +DEF_CMD(doc_str_callback) +{ + struct call_return *cr = container_of(ci->comm, struct call_return, c); + cr->s = strdup(ci->str); + return 1; +} + +char *doc_getstr(struct pane *from, struct mark *to) +{ + struct cmd_info ci = {0}; + int ret; + struct call_return cr; + + ci.key = "doc:get-str"; + ci.focus = from; + ci.mark = to; + cr.c = doc_str_callback; + cr.s = NULL; + ci.comm2 = &cr.c; + ret = key_handle_focus(&ci); + if (!ret) + return NULL; + return cr.s; +} + /* the 'docs' document type is special in that there can only ever * be one instance - the list of documents. * So there is no 'doctype' registered, just a document which can never diff --git a/core.h b/core.h index 30c34cf8..aded137d 100644 --- a/core.h +++ b/core.h @@ -169,6 +169,7 @@ void point_notify_change(struct doc *d, struct mark *p, struct mark *m); void doc_notify_change(struct doc *d, struct mark *m); void doc_check_consistent(struct doc *d); char *doc_attr(struct pane *dp, struct mark *m, bool forward, char *attr); +char *doc_getstr(struct pane *from, struct mark *to); void point_to_mark(struct mark *p, struct mark *m); void mark_to_mark(struct mark *m, struct mark *target); int mark_same(struct doc *d, struct mark *m1, struct mark *m2); @@ -386,19 +387,6 @@ static inline int doc_load_file(struct pane *p, int fd, char *name) ci.key = "doc:load-file"; return key_handle_focus(&ci); } -static inline char *doc_getstr(struct pane *from, struct mark *to) -{ - struct cmd_info ci = {0}; - int ret; - - ci.key = "doc:get-str"; - ci.focus = from; - ci.mark = to; - ret = key_handle_focus(&ci); - if (!ret) - return NULL; - return ci.str; -} static inline int doc_set_attr(struct pane *p, struct mark *pt, char *attr, char *val) diff --git a/doc-text.c b/doc-text.c index 5cb55bf1..10165ba2 100644 --- a/doc-text.c +++ b/doc-text.c @@ -1155,7 +1155,8 @@ DEF_CMD(text_get_str) break; } ret[l] = 0; - ci->str = ret; + comm_call(ci->comm2, "callback:get-str", ci->focus, 0, NULL, ret, 0); + free(ret); return 1; } -- 2.39.5