2 * Copyright Neil Brown ©2017-2022 <neil@brown.name>
3 * May be distributed under terms of GPLv2 - see file:COPYING
7 * A copy-buffer stores a number of texts that have been copied from
8 * elsewhere. It would be nice to store these in a text document, but
9 * as undo cannot be disabled, that would not be good for now.
10 * So lets just have a linked list of things.
12 * New texts can be added, old texts (indexed from most recent: 0 is latest, 1 is second
13 * latest) can be requested.
14 * Never store more than 10 texts.
16 * Register global commands "copy:save" and "copy:get" to access texts.
23 #define PANE_DATA_TYPE struct copy_info
35 #include "core-pane.h"
37 static struct map *copy_map;
38 DEF_LOOKUP_CMD(copy_handle, copy_map);
40 static void free_txt(struct txt **tp safe)
51 DEF_CMD_CLOSED(copy_close)
53 struct copy_info *cyi = ci->home->data;
55 free_txt(&cyi->store);
61 struct copy_info *cyi = container_of(ci->comm, struct copy_info, cmd);
63 if (strcmp(ci->key, "copy:save") == 0 && ci->str && ci->num == 0) {
66 if (cyi->store && strcmp(ci->str, cyi->store->txt) == 0)
67 /* Identical to last save, don't bother */
70 if (cyi->count >= 10) {
71 struct txt **tp = &cyi->store;
73 while (*tp && cnt < 10) {
78 LOG("copy:save free %.20s", (*tp)->txt);
81 LOG("copy:save add %.20s", ci->str);
82 t = calloc(1, sizeof(*t));
84 t->txt = strdup(ci->str);
88 if (strcmp(ci->key, "copy:save") == 0 && ci->str && ci->num == 1) {
89 /* Append str to the latest copy */
93 LOG("copy:save append %.20s", ci->str);
97 t->txt = malloc(strlen(txt) + strlen(ci->str) + 1);
98 strcat(strcpy(t->txt, txt), ci->str);
101 t = calloc(1, sizeof(*t));
102 t->next = cyi->store;
103 t->txt = strdup(ci->str);
108 if (strcmp(ci->key, "copy:get") == 0) {
109 struct txt *t = cyi->store;
111 while (t && idx > 0) {
116 LOG("copy:get %d returns %.20s", ci->num, t->txt);
118 comm_call(ci->comm2, "callback", ci->focus, 0, NULL, t->txt);
124 void edlib_init(struct pane *ed safe)
126 struct copy_info *cyi;
130 copy_map = key_alloc();
131 key_add(copy_map, "Close", ©_close);
134 p = pane_register(ed, 0, ©_handle.c);
140 call_comm("global-set-command", ed, &cyi->cmd, 0, NULL, "copy:save");
141 call_comm("global-set-command", ed, &cyi->cmd, 0, NULL, "copy:get");