1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
/*
* File: killring.c
* Purpose: Impelement the kill ring.
* Author: Lars Wirzenius
* Version: "@(#)SeX:$Id: killring.c,v 1.9 1996/12/17 14:31:34 liw Exp $"
*/
#include <stdlib.h>
#include <publib.h>
#include "killring.h"
#include "error.h"
#include "config.h"
/*
* Structure: kill
* Purpose: Describe an entry in the kill ring.
* Fields: buf the buffer containing the killed text
* mark the mark marking the whole text in the buffer
*/
struct kill {
Sbuf *buf;
Sbufmark *mark;
struct kill *next;
};
/*
* Variables: ring_head
* Purpose: Pointer to head of kill ring.
*/
static struct kill *ring_head = NULL;
/*
* Variable: current
* Purpose: Give the current position in the kill ring
*/
static struct kill *current = NULL;
/*
* Prototypes for local functions.
*/
static struct kill *new_entry(void);
static struct kill *get_latest(void);
static struct kill *get_next(struct kill *);
static void remove_extra(void);
static void remove_entry(struct kill *);
/*
* Function: killring_add
* Purpose: Add a new entry to the kill ring.
* Arguments: mark the deleted text
* Return: -1 for failure, 0 for success.
*/
int killring_add(Sbufmark *mark) {
struct kill *k;
k = new_entry();
if (k == NULL)
return -1;
if (sbuf_change(k->mark, mark) == -1) {
error(NULL, "error changing text (out of memory?)");
remove_entry(k);
return -1;
}
remove_extra();
return 0;
}
/*
* Function: killring_prepend_to_latest
* Purpose: Prepend text to the latest entry in kill ring
* Arguments: mark mark on the text to be prepended
* Return: -1 for failure, 0 for success.
*/
int killring_prepend_to_latest(Sbufmark *mark) {
struct kill *k;
k = get_latest();
if (k == NULL)
return killring_add(mark);
sbuf_remark(k->mark, 0, 0);
if (sbuf_change(k->mark, mark) == -1) {
error(NULL, "error changing text (out of memory?)");
return -1;
}
remove_extra();
return 0;
}
/*
* Function: killring_append_to_latest
* Purpose: Append text to the latest entry in kill ring
* Arguments: mark mark on the text to be appended
* Return: -1 for failure, 0 for success.
*/
int killring_append_to_latest(Sbufmark *mark) {
struct kill *k;
k = get_latest();
if (k == NULL)
return killring_add(mark);
sbuf_remark(k->mark, sbuf_length(k->buf), 0);
if (sbuf_change(k->mark, mark) == -1) {
error(NULL, "error changing text (out of memory?)");
return -1;
}
remove_extra();
return 0;
}
/*
* Function: killring_get_first_mark
* Purpose: Return the latest entry in the kill ring as mark.
* Arguments: none.
* Return: Pointer to mark on first kill ring entry, NULL if none.
*/
Sbufmark *killring_get_first_mark(void) {
current = get_latest();
if (current == NULL)
return NULL;
sbuf_remark(current->mark, 0, sbuf_length(current->buf));
return current->mark;
}
/*
* Function: killring_get_first
* Purpose: Return the latest entry in the kill ring.
* Arguments: mark the mark to set to the contents of the kill ring entry
* Return: -1 for failure, 0 for success.
* Description: killring_get_first will set the mark that it is given as
* the argument to the contents of the latest kill ring entry.
* Note: The position in the kill ring is global, and will be reset
* whenever the kill ring is modified.
*/
int killring_get_first(Sbufmark *mark) {
current = get_latest();
if (current == NULL)
return -1;
sbuf_remark(current->mark, 0, sbuf_length(current->buf));
if (sbuf_change(mark, current->mark) == -1) {
error(NULL, "error changing text (out of memory?)");
return -1;
}
return 0;
}
/*
* Function: killring_get_next
* Purpose: Return the next entry in the kill ring.
* Arguments: mark the mark to set to the contents of the kill ring entry
* Return: -1 for failure, 0 for success.
* Description: killring_get_next will set the mark that it is given as
* the argument to the contents of the next kill ring entry.
* Note: The position in the kill ring is global, and will be reset
* whenever the kill ring is modified.
*/
int killring_get_next(Sbufmark *mark) {
current = get_next(current);
if (current == NULL)
return -1;
sbuf_remark(current->mark, 0, sbuf_length(current->buf));
if (sbuf_change(mark, current->mark) == -1) {
error(NULL, "error changing text (out of memory?)");
return -1;
}
return 0;
}
/**********************************************************************
* Local functions *
**********************************************************************/
/*
* Function: new_entry
* Purpose: Create a new entry to kill ring.
*/
static struct kill *new_entry(void) {
struct kill *k;
k = malloc(sizeof(struct kill));
if (k == NULL)
return NULL;
k->buf = sbuf_create();
if (k->buf == NULL) {
error(NULL, "error creating text buffer (out of memory?)");
goto error;
}
k->mark = sbuf_mark(k->buf, 0, 0);
if (k->mark == NULL) {
error(NULL, "error creating text mark (out of memory?)");
goto error;
}
k->next = ring_head;
ring_head = k;
return k;
error:
if (k->buf != NULL)
sbuf_destroy(k->buf);
free(k);
return NULL;
}
/*
* Function: get_latest
* Purpose: Return pointer to first entry in kill ring.
*/
static struct kill *get_latest(void) {
return ring_head;
}
/*
* Function: get_next
* Purpose: Return pointer to the entry that follows k.
*/
static struct kill *get_next(struct kill *k) {
if (k == NULL || k->next == NULL)
return ring_head;
return k->next;
}
/*
* Function: remove_extra
* Purpose: Make sure there is not too much stuff in the kill ring.
*/
static void remove_extra(void) {
long size, ksize, max;
struct kill *k;
max = config_get_long(CONFIG_MAX_KILLRING);
size = 0;
for (k = ring_head; k != NULL; k = k->next) {
ksize = sbuf_length(k->buf);
if (size + ksize > max)
break;
size += ksize;
}
if (k == ring_head)
k = k->next;
if (k != NULL) {
while (k->next != NULL)
remove_entry(k->next);
}
current = NULL;
}
/*
* Function: remove_entry
* Purpose: Remove a specific entry from the kill ring.
*/
static void remove_entry(struct kill *k) {
struct kill *kk;
if (k == ring_head)
ring_head = k->next;
else {
for (kk = ring_head; kk->next != k; kk = kk->next)
continue;
kk->next = k->next;
}
sbuf_destroy(k->buf);
free(k);
}
|