| 1 |
/************************************************************************** |
| 2 |
* This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is * |
| 3 |
* provided by Jonathan and Jovehacks without charge and without * |
| 4 |
* warranty. You may copy, modify, and/or distribute JOVE, provided that * |
| 5 |
* this notice is included in all the source files and documentation. * |
| 6 |
**************************************************************************/ |
| 7 |
|
| 8 |
#include "jove.h" |
| 9 |
#include "fmt.h" |
| 10 |
#include "marks.h" |
| 11 |
#include "disp.h" |
| 12 |
|
| 13 |
private Mark *FreeMarksList = NULL; |
| 14 |
|
| 15 |
#define RecycleMark(m) { \ |
| 16 |
(m)->m_next = FreeMarksList; \ |
| 17 |
FreeMarksList = (m); \ |
| 18 |
(m)->m_line = NULL; /* DEBUG */ \ |
| 19 |
} |
| 20 |
|
| 21 |
Mark * |
| 22 |
MakeMark(LinePtr line, int column) |
| 23 |
{ |
| 24 |
Mark *newmark; |
| 25 |
|
| 26 |
if ((newmark = FreeMarksList) != NULL) |
| 27 |
FreeMarksList = newmark->m_next; |
| 28 |
else |
| 29 |
newmark = (Mark *) emalloc(sizeof *newmark); |
| 30 |
MarkSet(newmark, line, column); |
| 31 |
newmark->m_big_delete = NO; |
| 32 |
newmark->m_next = curbuf->b_marks; |
| 33 |
curbuf->b_marks = newmark; |
| 34 |
return newmark; |
| 35 |
} |
| 36 |
|
| 37 |
void |
| 38 |
flush_marks(Buffer *b) |
| 39 |
{ |
| 40 |
Mark *m, |
| 41 |
*next; |
| 42 |
|
| 43 |
for (m = b->b_marks; m != NULL; m = next) { |
| 44 |
next = m->m_next; |
| 45 |
RecycleMark(m) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
private void |
| 50 |
DelBufMark(Buffer *b, Mark *m) |
| 51 |
{ |
| 52 |
Mark *mp = b->b_marks; |
| 53 |
|
| 54 |
if (MarkHighlighting) |
| 55 |
makedirty(m->m_line); |
| 56 |
if (m == mp) |
| 57 |
b->b_marks = m->m_next; |
| 58 |
else { |
| 59 |
while (mp != NULL && mp->m_next != m) |
| 60 |
mp = mp->m_next; |
| 61 |
if (mp == NULL) { |
| 62 |
complain("Unknown mark!"); |
| 63 |
/* NOTREACHED */ |
| 64 |
} |
| 65 |
mp->m_next = m->m_next; |
| 66 |
} |
| 67 |
RecycleMark(m); |
| 68 |
} |
| 69 |
|
| 70 |
void |
| 71 |
DelMark(Mark *m) |
| 72 |
{ |
| 73 |
DelBufMark(curbuf, m); |
| 74 |
} |
| 75 |
|
| 76 |
/* AllMarkReset is used when a buffer is completely replaced. |
| 77 |
* We delete the marks in the mark ring, but we cannot find |
| 78 |
* the references to other marks, so those we make point |
| 79 |
* to the start of the buffer. |
| 80 |
*/ |
| 81 |
|
| 82 |
void |
| 83 |
AllMarkReset(Buffer *b, LinePtr line) |
| 84 |
{ |
| 85 |
{ |
| 86 |
Mark **mpp; |
| 87 |
|
| 88 |
for (mpp = &b->b_markring[0]; mpp != &b->b_markring[NMARKS]; mpp++) { |
| 89 |
if (*mpp != NULL) { |
| 90 |
DelBufMark(b, *mpp); |
| 91 |
*mpp = NULL; |
| 92 |
} |
| 93 |
} |
| 94 |
b->b_themark = 0; |
| 95 |
} |
| 96 |
|
| 97 |
{ |
| 98 |
Mark *mp; |
| 99 |
|
| 100 |
for (mp = b->b_marks; mp != NULL; mp = mp->m_next) |
| 101 |
MarkSet(mp, line, 0); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
void |
| 106 |
MarkSet(Mark *m, LinePtr line, int column) |
| 107 |
{ |
| 108 |
m->m_line = line; |
| 109 |
m->m_char = column; |
| 110 |
m->m_big_delete = NO; |
| 111 |
if (MarkHighlighting) |
| 112 |
makedirty(line); |
| 113 |
} |
| 114 |
|
| 115 |
void |
| 116 |
PopMark(void) |
| 117 |
{ |
| 118 |
int pmark; |
| 119 |
|
| 120 |
if (curmark == NULL) |
| 121 |
return; |
| 122 |
|
| 123 |
ExchPtMark(); |
| 124 |
pmark = curbuf->b_themark; |
| 125 |
do { |
| 126 |
if (--pmark < 0) |
| 127 |
pmark = NMARKS - 1; |
| 128 |
} while (curbuf->b_markring[pmark] == NULL); |
| 129 |
curbuf->b_themark = pmark; |
| 130 |
if (MarkHighlighting) |
| 131 |
makedirty(curmark->m_line); |
| 132 |
} |
| 133 |
|
| 134 |
void |
| 135 |
SetMark(void) |
| 136 |
{ |
| 137 |
if (is_an_arg()) |
| 138 |
PopMark(); |
| 139 |
else |
| 140 |
set_mark(); |
| 141 |
} |
| 142 |
|
| 143 |
void |
| 144 |
set_mark(void) |
| 145 |
{ |
| 146 |
do_set_mark(curline, curchar); |
| 147 |
} |
| 148 |
|
| 149 |
void |
| 150 |
do_set_mark(LinePtr l, int c) |
| 151 |
{ |
| 152 |
Mark **mr = curbuf->b_markring; |
| 153 |
int tm = curbuf->b_themark; |
| 154 |
|
| 155 |
if (mr[tm] != NULL) { |
| 156 |
if (MarkHighlighting) |
| 157 |
makedirty(mr[tm]->m_line); |
| 158 |
curbuf->b_themark = tm = (tm + 1) % NMARKS; |
| 159 |
if (mr[NMARKS-1] == NULL) { |
| 160 |
/* there is an empty slot: make sure one is here */ |
| 161 |
int i; |
| 162 |
|
| 163 |
for (i = NMARKS-1; i != tm; i--) |
| 164 |
mr[i] = mr[i-1]; |
| 165 |
mr[i] = NULL; |
| 166 |
} |
| 167 |
} |
| 168 |
if (mr[tm] == NULL) |
| 169 |
mr[tm] = MakeMark(l, c); |
| 170 |
else |
| 171 |
MarkSet(mr[tm], l, c); |
| 172 |
s_mess("[Point pushed]"); |
| 173 |
} |
| 174 |
|
| 175 |
/* Move point to Mark */ |
| 176 |
|
| 177 |
void |
| 178 |
ToMark(Mark *m) |
| 179 |
{ |
| 180 |
int len; |
| 181 |
|
| 182 |
if (m == NULL) |
| 183 |
return; |
| 184 |
|
| 185 |
DotTo(m->m_line, m->m_char); |
| 186 |
if (curchar > (len = length(curline))) |
| 187 |
curchar = len; |
| 188 |
} |
| 189 |
|
| 190 |
Mark * |
| 191 |
CurMark(void) |
| 192 |
{ |
| 193 |
if (curmark == NULL) { |
| 194 |
complain("No mark."); |
| 195 |
/* NOTREACHED */ |
| 196 |
} |
| 197 |
return curmark; |
| 198 |
} |
| 199 |
|
| 200 |
void |
| 201 |
ExchPtMark(void) |
| 202 |
{ |
| 203 |
LinePtr mline; |
| 204 |
int mchar; |
| 205 |
Mark *m = CurMark(); |
| 206 |
|
| 207 |
mline = curline; |
| 208 |
mchar = curchar; |
| 209 |
|
| 210 |
ToMark(m); |
| 211 |
if (MarkHighlighting) |
| 212 |
makedirty(m->m_line); |
| 213 |
MarkSet(m, mline, mchar); |
| 214 |
} |
| 215 |
|
| 216 |
/* Fix marks after a deletion. */ |
| 217 |
|
| 218 |
void |
| 219 |
DFixMarks(LinePtr line1, int char1, LinePtr line2, int char2) |
| 220 |
{ |
| 221 |
Mark *m; |
| 222 |
LinePtr lp; |
| 223 |
|
| 224 |
if (curbuf->b_marks == NULL) |
| 225 |
return; |
| 226 |
|
| 227 |
for (lp = line1; lp != line2->l_next; lp = lp->l_next) { |
| 228 |
for (m = curbuf->b_marks; m != NULL; m = m->m_next) { |
| 229 |
if (m->m_line == lp |
| 230 |
&& (lp != line1 || m->m_char > char1)) |
| 231 |
{ |
| 232 |
if (lp == line2 && m->m_char > char2) { |
| 233 |
m->m_char -= char2-char1; |
| 234 |
} else { |
| 235 |
m->m_char = char1; |
| 236 |
if (line1 != line2) |
| 237 |
m->m_big_delete = YES; |
| 238 |
} |
| 239 |
m->m_line = line1; |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
/* Fix marks after an insertion. */ |
| 246 |
|
| 247 |
void |
| 248 |
IFixMarks(LinePtr line1, int char1, LinePtr line2, int char2) |
| 249 |
{ |
| 250 |
Mark *m; |
| 251 |
|
| 252 |
for (m = curbuf->b_marks; m != NULL; m = m->m_next) { |
| 253 |
if (m->m_line == line1 && m->m_char > char1) { |
| 254 |
m->m_line = line2; |
| 255 |
m->m_char += char2 - char1; |
| 256 |
} |
| 257 |
} |
| 258 |
} |