| 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 |
(eval-when-compile (require 'cl)) |
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
(defstruct (ewoc--node |
|---|
| 107 |
(:type vector) |
|---|
| 108 |
(:constructor nil) |
|---|
| 109 |
(:constructor ewoc--node-create (start-marker data))) |
|---|
| 110 |
left right data start-marker) |
|---|
| 111 |
|
|---|
| 112 |
(defun ewoc--node-next (dll node) |
|---|
| 113 |
"Return the node after NODE, or nil if NODE is the last node." |
|---|
| 114 |
(let ((R (ewoc--node-right node))) |
|---|
| 115 |
(unless (eq dll R) R))) |
|---|
| 116 |
|
|---|
| 117 |
(defun ewoc--node-prev (dll node) |
|---|
| 118 |
"Return the node before NODE, or nil if NODE is the first node." |
|---|
| 119 |
(let ((L (ewoc--node-left node))) |
|---|
| 120 |
(unless (eq dll L) L))) |
|---|
| 121 |
|
|---|
| 122 |
(defun ewoc--node-nth (dll n) |
|---|
| 123 |
"Return the Nth node from the doubly linked list `dll'. |
|---|
| 124 |
N counts from zero. If N is negative, return the -(N+1)th last element. |
|---|
| 125 |
If N is out of range, return nil. |
|---|
| 126 |
Thus, (ewoc--node-nth dll 0) returns the first node, |
|---|
| 127 |
and (ewoc--node-nth dll -1) returns the last node." |
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
(let* ((branch (if (< n 0) 0 1)) |
|---|
| 132 |
(node (aref dll branch))) |
|---|
| 133 |
(if (< n 0) (setq n (- -1 n))) |
|---|
| 134 |
(while (and (not (eq dll node)) (> n 0)) |
|---|
| 135 |
(setq node (aref node branch)) |
|---|
| 136 |
(setq n (1- n))) |
|---|
| 137 |
(unless (eq dll node) node))) |
|---|
| 138 |
|
|---|
| 139 |
(defun ewoc-location (node) |
|---|
| 140 |
"Return the start location of NODE." |
|---|
| 141 |
(ewoc--node-start-marker node)) |
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
(defstruct (ewoc |
|---|
| 147 |
(:constructor nil) |
|---|
| 148 |
(:constructor ewoc--create (buffer pretty-printer dll)) |
|---|
| 149 |
(:conc-name ewoc--)) |
|---|
| 150 |
buffer pretty-printer header footer dll last-node hf-pp) |
|---|
| 151 |
|
|---|
| 152 |
(defmacro ewoc--set-buffer-bind-dll-let* (ewoc varlist &rest forms) |
|---|
| 153 |
"Execute FORMS with ewoc--buffer selected as current buffer, |
|---|
| 154 |
`dll' bound to the dll, and VARLIST bound as in a let*. |
|---|
| 155 |
`dll' will be bound when VARLIST is initialized, but |
|---|
| 156 |
the current buffer will *not* have been changed. |
|---|
| 157 |
Return value of last form in FORMS." |
|---|
| 158 |
(let ((hnd (make-symbol "ewoc"))) |
|---|
| 159 |
`(let* ((,hnd ,ewoc) |
|---|
| 160 |
(dll (ewoc--dll ,hnd)) |
|---|
| 161 |
,@varlist) |
|---|
| 162 |
(with-current-buffer (ewoc--buffer ,hnd) |
|---|
| 163 |
,@forms)))) |
|---|
| 164 |
|
|---|
| 165 |
(defmacro ewoc--set-buffer-bind-dll (ewoc &rest forms) |
|---|
| 166 |
`(ewoc--set-buffer-bind-dll-let* ,ewoc nil ,@forms)) |
|---|
| 167 |
|
|---|
| 168 |
(defsubst ewoc--filter-hf-nodes (ewoc node) |
|---|
| 169 |
"Evaluate NODE once and return it. |
|---|
| 170 |
BUT if it is the header or the footer in EWOC return nil instead." |
|---|
| 171 |
(unless (or (eq node (ewoc--header ewoc)) |
|---|
| 172 |
(eq node (ewoc--footer ewoc))) |
|---|
| 173 |
node)) |
|---|
| 174 |
|
|---|
| 175 |
(defun ewoc--adjust (beg end node dll) |
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
(when (< beg end) |
|---|
| 184 |
(let (m) |
|---|
| 185 |
(while (and (= beg (setq m (ewoc--node-start-marker node))) |
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
(progn |
|---|
| 190 |
(set-marker m end) |
|---|
| 191 |
(not (eq dll node)))) |
|---|
| 192 |
(setq node (ewoc--node-right node)))))) |
|---|
| 193 |
|
|---|
| 194 |
(defun ewoc--insert-new-node (node data pretty-printer dll) |
|---|
| 195 |
"Insert before NODE a new node for DATA, displayed by PRETTY-PRINTER. |
|---|
| 196 |
Fourth arg DLL -- from `(ewoc--dll EWOC)' -- is for internal purposes. |
|---|
| 197 |
Call PRETTY-PRINTER with point at NODE's start, thus pushing back |
|---|
| 198 |
NODE and leaving the new node's start there. Return the new node." |
|---|
| 199 |
(save-excursion |
|---|
| 200 |
(let ((elemnode (ewoc--node-create |
|---|
| 201 |
(copy-marker (ewoc--node-start-marker node)) data))) |
|---|
| 202 |
(setf (ewoc--node-left elemnode) (ewoc--node-left node) |
|---|
| 203 |
(ewoc--node-right elemnode) node |
|---|
| 204 |
(ewoc--node-right (ewoc--node-left node)) elemnode |
|---|
| 205 |
(ewoc--node-left node) elemnode) |
|---|
| 206 |
(ewoc--refresh-node pretty-printer elemnode dll) |
|---|
| 207 |
elemnode))) |
|---|
| 208 |
|
|---|
| 209 |
(defun ewoc--refresh-node (pp node dll) |
|---|
| 210 |
"Redisplay the element represented by NODE using the pretty-printer PP." |
|---|
| 211 |
(let ((inhibit-read-only t) |
|---|
| 212 |
(m (ewoc--node-start-marker node)) |
|---|
| 213 |
(R (ewoc--node-right node))) |
|---|
| 214 |
|
|---|
| 215 |
(delete-region m (ewoc--node-start-marker R)) |
|---|
| 216 |
|
|---|
| 217 |
(goto-char m) |
|---|
| 218 |
(funcall pp (ewoc--node-data node)) |
|---|
| 219 |
(ewoc--adjust m (point) R dll))) |
|---|
| 220 |
|
|---|
| 221 |
(defun ewoc--wrap (func) |
|---|
| 222 |
(lexical-let ((ewoc--user-pp func)) |
|---|
| 223 |
(lambda (data) |
|---|
| 224 |
(funcall ewoc--user-pp data) |
|---|
| 225 |
(insert "\n")))) |
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
(defun ewoc-create (pretty-printer &optional header footer nosep) |
|---|
| 233 |
"Create an empty ewoc. |
|---|
| 234 |
|
|---|
| 235 |
The ewoc will be inserted in the current buffer at the current position. |
|---|
| 236 |
|
|---|
| 237 |
PRETTY-PRINTER should be a function that takes one argument, an |
|---|
| 238 |
element, and inserts a string representing it in the buffer (at |
|---|
| 239 |
point). The string PRETTY-PRINTER inserts may be empty or span |
|---|
| 240 |
several lines. The PRETTY-PRINTER should use `insert', and not |
|---|
| 241 |
`insert-before-markers'. |
|---|
| 242 |
|
|---|
| 243 |
Optional second and third arguments HEADER and FOOTER are strings, |
|---|
| 244 |
possibly empty, that will always be present at the top and bottom, |
|---|
| 245 |
respectively, of the ewoc. |
|---|
| 246 |
|
|---|
| 247 |
Normally, a newline is automatically inserted after the header, |
|---|
| 248 |
the footer and every node's printed representation. Optional |
|---|
| 249 |
fourth arg NOSEP non-nil inhibits this." |
|---|
| 250 |
(let* ((dummy-node (ewoc--node-create 'DL-LIST 'DL-LIST)) |
|---|
| 251 |
(dll (progn (setf (ewoc--node-right dummy-node) dummy-node) |
|---|
| 252 |
(setf (ewoc--node-left dummy-node) dummy-node) |
|---|
| 253 |
dummy-node)) |
|---|
| 254 |
(wrap (if nosep 'identity 'ewoc--wrap)) |
|---|
| 255 |
(new-ewoc (ewoc--create (current-buffer) |
|---|
| 256 |
(funcall wrap pretty-printer) |
|---|
| 257 |
dll)) |
|---|
| 258 |
(hf-pp (funcall wrap 'insert)) |
|---|
| 259 |
(pos (point)) |
|---|
| 260 |
head foot) |
|---|
| 261 |
(ewoc--set-buffer-bind-dll new-ewoc |
|---|
| 262 |
|
|---|
| 263 |
(unless header (setq header "")) |
|---|
| 264 |
(unless footer (setq footer "")) |
|---|
| 265 |
(setf (ewoc--node-start-marker dll) (copy-marker pos) |
|---|
| 266 |
foot (ewoc--insert-new-node dll footer hf-pp dll) |
|---|
| 267 |
head (ewoc--insert-new-node foot header hf-pp dll) |
|---|
| 268 |
(ewoc--hf-pp new-ewoc) hf-pp |
|---|
| 269 |
(ewoc--footer new-ewoc) foot |
|---|
| 270 |
(ewoc--header new-ewoc) head)) |
|---|
| 271 |
|
|---|
| 272 |
new-ewoc)) |
|---|
| 273 |
|
|---|
| 274 |
(defalias 'ewoc-data 'ewoc--node-data |
|---|
| 275 |
"Extract the data encapsulated by NODE and return it. |
|---|
| 276 |
|
|---|
| 277 |
\(fn NODE)") |
|---|
| 278 |
|
|---|
| 279 |
(defun ewoc-set-data (node data) |
|---|
| 280 |
"Set NODE to encapsulate DATA." |
|---|
| 281 |
(setf (ewoc--node-data node) data)) |
|---|
| 282 |
|
|---|
| 283 |
(defun ewoc-enter-first (ewoc data) |
|---|
| 284 |
"Enter DATA first in EWOC. |
|---|
| 285 |
Return the new node." |
|---|
| 286 |
(ewoc--set-buffer-bind-dll ewoc |
|---|
| 287 |
(ewoc-enter-after ewoc (ewoc--node-nth dll 0) data))) |
|---|
| 288 |
|
|---|
| 289 |
(defun ewoc-enter-last (ewoc data) |
|---|
| 290 |
"Enter DATA last in EWOC. |
|---|
| 291 |
Return the new node." |
|---|
| 292 |
(ewoc--set-buffer-bind-dll ewoc |
|---|
| 293 |
(ewoc-enter-before ewoc (ewoc--node-nth dll -1) data))) |
|---|
| 294 |
|
|---|
| 295 |
(defun ewoc-enter-after (ewoc node data) |
|---|
| 296 |
"Enter a new element DATA after NODE in EWOC. |
|---|
| 297 |
Return the new node." |
|---|
| 298 |
(ewoc--set-buffer-bind-dll ewoc |
|---|
| 299 |
(ewoc-enter-before ewoc (ewoc--node-next dll node) data))) |
|---|
| 300 |
|
|---|
| 301 |
(defun ewoc-enter-before (ewoc node data) |
|---|
| 302 |
"Enter a new element DATA before NODE in EWOC. |
|---|
| 303 |
Return the new node." |
|---|
| 304 |
(ewoc--set-buffer-bind-dll ewoc |
|---|
| 305 |
(ewoc--insert-new-node node data (ewoc--pretty-printer ewoc) dll))) |
|---|
| 306 |
|
|---|
| 307 |
(defun ewoc-next (ewoc node) |
|---|
| 308 |
"Return the node in EWOC that follows NODE. |
|---|
| 309 |
Return nil if NODE is nil or the last element." |
|---|
| 310 |
(when node |
|---|
| 311 |
(ewoc--filter-hf-nodes |
|---|
| 312 |
ewoc (ewoc--node-next (ewoc--dll ewoc) node)))) |
|---|
| 313 |
|
|---|
| 314 |
(defun ewoc-prev (ewoc node) |
|---|
| 315 |
"Return the node in EWOC that precedes NODE. |
|---|
| 316 |
Return nil if NODE is nil or the first element." |
|---|
| 317 |
(when node |
|---|
| 318 |
(ewoc--filter-hf-nodes |
|---|
| 319 |
ewoc (ewoc--node-prev (ewoc--dll ewoc) node)))) |
|---|
| 320 |
|
|---|
| 321 |
(defun ewoc-nth (ewoc n) |
|---|
| 322 |
"Return the Nth node. |
|---|
| 323 |
N counts from zero. Return nil if there is less than N elements. |
|---|
| 324 |
If N is negative, return the -(N+1)th last element. |
|---|
| 325 |
Thus, (ewoc-nth ewoc 0) returns the first node, |
|---|
| 326 |
and (ewoc-nth ewoc -1) returns the last node. |
|---|
| 327 |
Use `ewoc-data' to extract the data from the node." |
|---|
| 328 |
|
|---|
| 329 |
(setq n (if (< n 0) (1- n) (1+ n))) |
|---|
| 330 |
(ewoc--filter-hf-nodes ewoc |
|---|
| 331 |
(ewoc--node-nth (ewoc--dll ewoc) n))) |
|---|
| 332 |
|
|---|
| 333 |
(defun ewoc-map (map-function ewoc &rest args) |
|---|
| 334 |
"Apply MAP-FUNCTION to all elements in EWOC. |
|---|
| 335 |
MAP-FUNCTION is applied to the first element first. |
|---|
| 336 |
If MAP-FUNCTION returns non-nil the element will be refreshed (its |
|---|
| 337 |
pretty-printer will be called once again). |
|---|
| 338 |
|
|---|
| 339 |
Note that the buffer for EWOC will be the current buffer when |
|---|
| 340 |
MAP-FUNCTION is called. MAP-FUNCTION must restore the current |
|---|
| 341 |
buffer before it returns, if it changes it. |
|---|
| 342 |
|
|---|
| 343 |
If more than two arguments are given, the remaining |
|---|
| 344 |
arguments will be passed to MAP-FUNCTION." |
|---|
| 345 |
(ewoc--set-buffer-bind-dll-let* ewoc |
|---|
| 346 |
((footer (ewoc--footer ewoc)) |
|---|
| 347 |
(pp (ewoc--pretty-printer ewoc)) |
|---|
| 348 |
(node (ewoc--node-nth dll 1))) |
|---|
| 349 |
(save-excursion |
|---|
| 350 |
(while (not (eq node footer)) |
|---|
| 351 |
(if (apply map-function (ewoc--node-data node) args) |
|---|
| 352 |
(ewoc--refresh-node pp node dll)) |
|---|
| 353 |
(setq node (ewoc--node-next dll node)))))) |
|---|
| 354 |
|
|---|
| 355 |
(defun ewoc-delete (ewoc &rest nodes) |
|---|
| 356 |
"Delete NODES from EWOC." |
|---|
| 357 |
(ewoc--set-buffer-bind-dll-let* ewoc |
|---|
| 358 |
((L nil) (R nil) (last (ewoc--last-node ewoc))) |
|---|
| 359 |
(dolist (node nodes) |
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 |
(when (eq last node) |
|---|
| 363 |
(setf last nil (ewoc--last-node ewoc) nil)) |
|---|
| 364 |
(delete-region (ewoc--node-start-marker node) |
|---|
| 365 |
(ewoc--node-start-marker (ewoc--node-next dll node))) |
|---|
| 366 |
(set-marker (ewoc--node-start-marker node) nil) |
|---|
| 367 |
(setf L (ewoc--node-left node) |
|---|
| 368 |
R (ewoc--node-right node) |
|---|
| 369 |
|
|---|
| 370 |
(ewoc--node-right L) R |
|---|
| 371 |
(ewoc--node-left R) L |
|---|
| 372 |
|
|---|
| 373 |
(ewoc--node-left node) nil |
|---|
| 374 |
(ewoc--node-right node) nil)))) |
|---|
| 375 |
|
|---|
| 376 |
(defun ewoc-filter (ewoc predicate &rest args) |
|---|
| 377 |
"Remove all elements in EWOC for which PREDICATE returns nil. |
|---|
| 378 |
Note that the buffer for EWOC will be current-buffer when PREDICATE |
|---|
| 379 |
is called. PREDICATE must restore the current buffer before it returns |
|---|
| 380 |
if it changes it. |
|---|
| 381 |
The PREDICATE is called with the element as its first argument. If any |
|---|
| 382 |
ARGS are given they will be passed to the PREDICATE." |
|---|
| 383 |
(ewoc--set-buffer-bind-dll-let* ewoc |
|---|
| 384 |
((node (ewoc--node-nth dll 1)) |
|---|
| 385 |
(footer (ewoc--footer ewoc)) |
|---|
| 386 |
(goodbye nil) |
|---|
| 387 |
(inhibit-read-only t)) |
|---|
| 388 |
(while (not (eq node footer)) |
|---|
| 389 |
(unless (apply predicate (ewoc--node-data node) args) |
|---|
| 390 |
(push node goodbye)) |
|---|
| 391 |
(setq node (ewoc--node-next dll node))) |
|---|
| 392 |
(apply 'ewoc-delete ewoc goodbye))) |
|---|
| 393 |
|
|---|
| 394 |
(defun ewoc-locate (ewoc &optional pos guess) |
|---|
| 395 |
"Return the node that POS (a buffer position) is within. |
|---|
| 396 |
POS may be a marker or an integer. It defaults to point. |
|---|
| 397 |
GUESS should be a node that it is likely to be near POS. |
|---|
| 398 |
|
|---|
| 399 |
If POS points before the first element, the first node is returned. |
|---|
| 400 |
If POS points after the last element, the last node is returned. |
|---|
| 401 |
If the EWOC is empty, nil is returned." |
|---|
| 402 |
(unless pos (setq pos (point))) |
|---|
| 403 |
(ewoc--set-buffer-bind-dll ewoc |
|---|
| 404 |
|
|---|
| 405 |
(cond |
|---|
| 406 |
|
|---|
| 407 |
((eq (ewoc--node-nth dll 1) (ewoc--node-nth dll -1)) |
|---|
| 408 |
nil) |
|---|
| 409 |
|
|---|
| 410 |
|
|---|
| 411 |
((< pos (ewoc--node-start-marker (ewoc--node-nth dll 2))) |
|---|
| 412 |
(ewoc--node-nth dll 1)) |
|---|
| 413 |
|
|---|
| 414 |
|
|---|
| 415 |
((>= pos (ewoc--node-start-marker (ewoc--node-nth dll -2))) |
|---|
| 416 |
(ewoc--node-nth dll -2)) |
|---|
| 417 |
|
|---|
| 418 |
|
|---|
| 419 |
(t |
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 |
(let* ((best-guess (ewoc--node-nth dll 1)) |
|---|
| 423 |
(distance (abs (- pos (ewoc--node-start-marker best-guess))))) |
|---|
| 424 |
(when guess |
|---|
| 425 |
(let ((d (abs (- pos (ewoc--node-start-marker guess))))) |
|---|
| 426 |
(when (< d distance) |
|---|
| 427 |
(setq distance d) |
|---|
| 428 |
(setq best-guess guess)))) |
|---|
| 429 |
|
|---|
| 430 |
(let* ((g (ewoc--node-nth dll -1)) |
|---|
| 431 |
(d (abs (- pos (ewoc--node-start-marker g))))) |
|---|
| 432 |
(when (< d distance) |
|---|
| 433 |
(setq distance d) |
|---|
| 434 |
(setq best-guess g))) |
|---|
| 435 |
|
|---|
| 436 |
(when (ewoc--last-node ewoc) |
|---|
| 437 |
(let* ((g (ewoc--last-node ewoc)) |
|---|
| 438 |
(d (abs (- pos (ewoc--node-start-marker g))))) |
|---|
| 439 |
(when (< d distance) |
|---|
| 440 |
(setq distance d) |
|---|
| 441 |
(setq best-guess g)))) |
|---|
| 442 |
|
|---|
| 443 |
|
|---|
| 444 |
|
|---|
| 445 |
|
|---|
| 446 |
|
|---|
| 447 |
(cond |
|---|
| 448 |
|
|---|
| 449 |
((>= pos |
|---|
| 450 |
(ewoc--node-start-marker best-guess)) |
|---|
| 451 |
|
|---|
| 452 |
(while (>= pos (ewoc--node-start-marker best-guess)) |
|---|
| 453 |
(setq best-guess (ewoc--node-next dll best-guess))) |
|---|
| 454 |
|
|---|
| 455 |
(ewoc--node-prev dll best-guess)) |
|---|
| 456 |
|
|---|
| 457 |
|
|---|
| 458 |
(t |
|---|
| 459 |
(while (< pos (ewoc--node-start-marker best-guess)) |
|---|
| 460 |
(setq best-guess (ewoc--node-prev dll best-guess))) |
|---|
| 461 |
best-guess))))))) |
|---|
| 462 |
|
|---|
| 463 |
(defun ewoc-invalidate (ewoc &rest nodes) |
|---|
| 464 |
"Call EWOC's pretty-printer for each element in NODES. |
|---|
| 465 |
Delete current text first, thus effecting a \"refresh\"." |
|---|
| 466 |
(ewoc--set-buffer-bind-dll-let* ewoc |
|---|
| 467 |
((pp (ewoc--pretty-printer ewoc))) |
|---|
| 468 |
(save-excursion |
|---|
| 469 |
(dolist (node nodes) |
|---|
| 470 |
(ewoc--refresh-node pp node dll))))) |
|---|
| 471 |
|
|---|
| 472 |
(defun ewoc-goto-prev (ewoc arg) |
|---|
| 473 |
"Move point to the ARGth previous element in EWOC. |
|---|
| 474 |
Don't move if we are at the first element, or if EWOC is empty. |
|---|
| 475 |
Return the node we moved to." |
|---|
| 476 |
(ewoc--set-buffer-bind-dll-let* ewoc |
|---|
| 477 |
((node (ewoc-locate ewoc (point)))) |
|---|
| 478 |
(when node |
|---|
| 479 |
|
|---|
| 480 |
(when (>= (point) (ewoc--node-start-marker (ewoc--node-right node))) |
|---|
| 481 |
(setq arg (1- arg))) |
|---|
| 482 |
(while (and node (> arg 0)) |
|---|
| 483 |
(setq arg (1- arg)) |
|---|
| 484 |
(setq node (ewoc--node-prev dll node))) |
|---|
| 485 |
|
|---|
| 486 |
(unless (ewoc--filter-hf-nodes ewoc node) |
|---|
| 487 |
(setq node (ewoc--node-nth dll 1))) |
|---|
| 488 |
(ewoc-goto-node ewoc node)))) |
|---|
| 489 |
|
|---|
| 490 |
(defun ewoc-goto-next (ewoc arg) |
|---|
| 491 |
"Move point to the ARGth next element in EWOC. |
|---|
| 492 |
Return the node (or nil if we just passed the last node)." |
|---|
| 493 |
(ewoc--set-buffer-bind-dll-let* ewoc |
|---|
| 494 |
((node (ewoc-locate ewoc (point)))) |
|---|
| 495 |
(while (and node (> arg 0)) |
|---|
| 496 |
(setq arg (1- arg)) |
|---|
| 497 |
(setq node (ewoc--node-next dll node))) |
|---|
| 498 |
|
|---|
| 499 |
|
|---|
| 500 |
|
|---|
| 501 |
(ewoc-goto-node ewoc node))) |
|---|
| 502 |
|
|---|
| 503 |
(defun ewoc-goto-node (ewoc node) |
|---|
| 504 |
"Move point to NODE in EWOC." |
|---|
| 505 |
(ewoc--set-buffer-bind-dll ewoc |
|---|
| 506 |
(goto-char (ewoc--node-start-marker node)) |
|---|
| 507 |
(if goal-column (move-to-column goal-column)) |
|---|
| 508 |
(setf (ewoc--last-node ewoc) node))) |
|---|
| 509 |
|
|---|
| 510 |
(defun ewoc-refresh (ewoc) |
|---|
| 511 |
"Refresh all data in EWOC. |
|---|
| 512 |
The pretty-printer that was specified when the EWOC was created |
|---|
| 513 |
will be called for all elements in EWOC. |
|---|
| 514 |
Note that `ewoc-invalidate' is more efficient if only a small |
|---|
| 515 |
number of elements needs to be refreshed." |
|---|
| 516 |
(ewoc--set-buffer-bind-dll-let* ewoc |
|---|
| 517 |
((footer (ewoc--footer ewoc))) |
|---|
| 518 |
(let ((inhibit-read-only t)) |
|---|
| 519 |
(delete-region (ewoc--node-start-marker (ewoc--node-nth dll 1)) |
|---|
| 520 |
(ewoc--node-start-marker footer)) |
|---|
| 521 |
(goto-char (ewoc--node-start-marker footer)) |
|---|
| 522 |
(let ((pp (ewoc--pretty-printer ewoc)) |
|---|
| 523 |
(node (ewoc--node-nth dll 1))) |
|---|
| 524 |
(while (not (eq node footer)) |
|---|
| 525 |
(set-marker (ewoc--node-start-marker node) (point)) |
|---|
| 526 |
(funcall pp (ewoc--node-data node)) |
|---|
| 527 |
(setq node (ewoc--node-next dll node))))) |
|---|
| 528 |
(set-marker (ewoc--node-start-marker footer) (point)))) |
|---|
| 529 |
|
|---|
| 530 |
(defun ewoc-collect (ewoc predicate &rest args) |
|---|
| 531 |
"Select elements from EWOC using PREDICATE. |
|---|
| 532 |
Return a list of all selected data elements. |
|---|
| 533 |
PREDICATE is a function that takes a data element as its first |
|---|
| 534 |
argument. The elements on the returned list will appear in the |
|---|
| 535 |
same order as in the buffer. You should not rely on the order of |
|---|
| 536 |
calls to PREDICATE. |
|---|
| 537 |
Note that the buffer the EWOC is displayed in is the current |
|---|
| 538 |
buffer when PREDICATE is called. PREDICATE must restore it if it |
|---|
| 539 |
changes it. |
|---|
| 540 |
If more than two arguments are given the |
|---|
| 541 |
remaining arguments will be passed to PREDICATE." |
|---|
| 542 |
(ewoc--set-buffer-bind-dll-let* ewoc |
|---|
| 543 |
((header (ewoc--header ewoc)) |
|---|
| 544 |
(node (ewoc--node-nth dll -2)) |
|---|
| 545 |
result) |
|---|
| 546 |
(while (not (eq node header)) |
|---|
| 547 |
(if (apply predicate (ewoc--node-data node) args) |
|---|
| 548 |
(push (ewoc--node-data node) result)) |
|---|
| 549 |
(setq node (ewoc--node-prev dll node))) |
|---|
| 550 |
(nreverse result))) |
|---|
| 551 |
|
|---|
| 552 |
(defun ewoc-buffer (ewoc) |
|---|
| 553 |
"Return the buffer that is associated with EWOC. |
|---|
| 554 |
Return nil if the buffer has been deleted." |
|---|
| 555 |
(let ((buf (ewoc--buffer ewoc))) |
|---|
| 556 |
(when (buffer-name buf) buf))) |
|---|
| 557 |
|
|---|
| 558 |
(defun ewoc-get-hf (ewoc) |
|---|
| 559 |
"Return a cons cell containing the (HEADER . FOOTER) of EWOC." |
|---|
| 560 |
(cons (ewoc--node-data (ewoc--header ewoc)) |
|---|
| 561 |
(ewoc--node-data (ewoc--footer ewoc)))) |
|---|
| 562 |
|
|---|
| 563 |
(defun ewoc-set-hf (ewoc header footer) |
|---|
| 564 |
"Set the HEADER and FOOTER of EWOC." |
|---|
| 565 |
(ewoc--set-buffer-bind-dll-let* ewoc |
|---|
| 566 |
((head (ewoc--header ewoc)) |
|---|
| 567 |
(foot (ewoc--footer ewoc)) |
|---|
| 568 |
(hf-pp (ewoc--hf-pp ewoc))) |
|---|
| 569 |
(setf (ewoc--node-data head) header |
|---|
| 570 |
(ewoc--node-data foot) footer) |
|---|
| 571 |
(save-excursion |
|---|
| 572 |
(ewoc--refresh-node hf-pp head dll) |
|---|
| 573 |
(ewoc--refresh-node hf-pp foot dll)))) |
|---|
| 574 |
|
|---|
| 575 |
|
|---|
| 576 |
(provide 'ewoc) |
|---|
| 577 |
|
|---|
| 578 |
|
|---|
| 579 |
|
|---|
| 580 |
|
|---|
| 581 |
|
|---|
| 582 |
|
|---|
| 583 |
|
|---|
| 584 |
|
|---|
| 585 |
|
|---|