[lice @ 1/2 busted local vars]
[lice.git] / textprop.lisp
blobf9ddfd9a369c8024ecaad8a7121177b73fbf5438
1 (in-package :lice)
3 ;; This function is not translated well
4 (defun validate-interval-range (object begin end force)
5 (let (i searchpos)
6 ;; /* If we are asked for a point, but from a subr which operates
7 ;; on a range, then return nothing. */
8 ;; if (EQ (*begin, *end) && begin != end)
9 ;; return NULL_INTERVAL;
10 (when (> begin end)
11 ;; MOVITZ doesn't have psetf
12 (let ((tmp begin))
13 (setf begin end
14 end tmp))
15 ;; (psetf begin end
16 ;; end begin)
18 (if (typep object 'buffer)
19 (progn
20 (when (not (and (<= (buffer-min object) begin)
21 (<= begin end)
22 (<= end (buffer-max object))))
23 (signal 'args-out-of-range))
24 (setf i (intervals object))
25 (when (= (buffer-min object) (buffer-max object))
26 (return-from validate-interval-range (values nil begin end)))
27 (setf searchpos begin))
28 (let ((len (length (pstring-data object))))
29 (when (not (and (<= 0 begin)
30 (<= begin end)
31 (<= end len)))
32 (signal 'args-out-of-range))
33 (setf i (intervals object))
34 (when (zerop len)
35 (return-from validate-interval-range (values nil begin end)))
36 (setf searchpos begin)))
37 (if i
38 (values (find-interval i searchpos) begin end)
39 (if force
40 (values (create-root-interval object) begin end)
41 (values i begin end)))))
43 (defun set-text-properties (start end properties &optional (object (current-buffer)))
44 (let ((start-bk start)
45 (end-bk end)
47 (setf properties (validate-plist properties))
48 ;; If we want no properties for a whole string, get rid of its
49 ;; intervals.
50 (when (and (null properties)
51 (typep object 'pstring)
52 (zerop start)
53 (= end (length (pstring-data object))))
54 (when (null (intervals object))
55 (return-from set-text-properties t))
56 (setf (intervals object) nil)
57 (return-from set-text-properties t))
58 (multiple-value-setq (i start end)
59 (validate-interval-range object start end nil))
60 (when (null i)
61 (when (null properties)
62 (return-from set-text-properties nil))
63 ;; /* Restore the original START and END values because
64 ;; validate_interval_range increments them for strings. */
65 (setf start start-bk
66 end end-bk)
67 (multiple-value-setq (i start end)
68 (validate-interval-range object start end t))
69 ;; /* This can return if start == end. */
70 (when (null i)
71 (return-from set-text-properties nil)))
73 ;; TODO: add this
74 ;; (when (typep object 'buffer)
75 ;; ;; modify_region (XBUFFER (object), XINT (start), XINT (end));
76 (set-text-properties-1 start end properties object i)
78 ;; if (BUFFERP (object) && !NILP (signal_after_change_p))
79 ;; signal_after_change (XINT (start), XINT (end) - XINT (start),
80 ;; XINT (end) - XINT (start));
81 t))
83 (defun set-text-properties-1 (start end properties buffer i)
84 (let ((len (- end start))
85 (prev-changed nil)
86 unchanged)
87 (when (zerop len)
88 (return-from set-text-properties-1))
89 (when (minusp len)
90 (incf start len)
91 (setf len (abs len)))
92 (when (null i)
93 (setf i (find-interval (intervals buffer) start)))
94 (when (/= (interval-pt i) start)
95 (setf unchanged i
96 i (split-interval-right unchanged (- start (interval-pt unchanged))))
97 (when (> (interval-text-length i) len)
98 (copy-properties unchanged i)
99 (setf i (split-interval-left i len))
100 (set-properties properties i buffer)
101 (return-from set-text-properties-1))
102 (set-properties properties i buffer)
103 (when (= (interval-text-length i) len)
104 (return-from set-text-properties-1))
105 (setf prev-changed i)
106 (decf len (interval-text-length i))
107 (setf i (next-interval i)))
108 (loop while (> len 0)
109 do (progn
110 (when (null i)
111 (error "borked."))
112 (when (>= (interval-text-length i) len)
113 (when (> (interval-text-length i) len)
114 (setf i (split-interval-left i len)))
115 (set-properties properties i buffer)
116 (when prev-changed
117 (merge-interval-left i))
118 (return-from set-text-properties-1))
119 (decf len (interval-text-length i))
120 ;; We have to call set_properties even if we are going
121 ;; to merge the intervals, so as to make the undo
122 ;; records and cause redisplay to happen.
123 (set-properties properties i buffer)
124 (if (null prev-changed)
125 (setf prev-changed i)
126 (setf prev-changed (merge-interval-left i)
127 i prev-changed))
128 (setf i (next-interval i))))))
130 (defun copy-properties (source target)
131 (when (and (default-interval-p source)
132 (default-interval-p target))
133 (return-from copy-properties))
134 (setf (interval-plist target) (copy-list (interval-plist source))))
136 (defun set-properties (properties interval object)
137 (when (typep object 'buffer)
138 ;; record undo info
140 (setf (interval-plist interval) (copy-tree properties)))
142 (defun add-properties (plist i object)
143 "Add the properties in plist to interval I. OBJECT should be the
144 string of buffer containing the interval."
145 (declare (ignore object))
146 (let ((changed nil))
147 (doplist (sym val plist changed)
148 (let ((found (getf (interval-plist i) sym)))
149 (if found
150 (progn
151 ;; record-property-change
152 (when (not (eql found val))
153 (setf (getf (interval-plist i) sym) val
154 changed t)))
155 (progn
156 ;; record-property-change
157 (setf (getf (interval-plist i) sym) val
158 changed t)))))))
160 (defun validate-plist (list)
161 "/* Validate LIST as a property list. If LIST is not a list, then
162 make one consisting of (LIST nil). Otherwise, verify that LIST is
163 even numbered and thus suitable as a plist. */"
164 (cond ((null list) nil)
165 ((consp list)
166 (if (oddp (length list))
167 (error "odd length property list")
168 list))
169 (t (list (list list nil)))))
171 (defun interval-has-all-properties (plist i)
172 "/* Return nonzero if interval I has all the properties, with the same
173 values, of list PLIST. */"
174 (doplist (sym val plist t)
175 (let ((found (getf (interval-plist i) sym)))
176 (if found
177 (when (not (eql found val))
178 (return-from interval-has-all-properties nil))
179 (return-from interval-has-all-properties nil)))))
181 (defun add-text-properties (start end properties &optional (object (current-buffer)))
182 "Add properties to the text from START to END. The third argument
183 PROPERTIES is a property list specifying the property values to add.
184 If the optional fourth argument OBJECT is a buffer (or nil, which
185 means the current buffer), START and END are buffer positions
186 (integers or markers). If OBJECT is a string, START and END are
187 0-based indices into it. Return t if any property value actually
188 changed, nil otherwise."
189 (let ((len (- end start))
190 (modified 0)
191 i unchanged)
192 (setf properties (validate-plist properties))
193 (when (null properties)
194 (return-from add-text-properties nil))
195 (multiple-value-setq (i start end) (validate-interval-range object start end t))
196 (when (null i)
197 (return-from add-text-properties nil))
198 ;; If we're not starting on an interval boundary, we have to split
199 ;; this interval.
200 (when (/= (interval-pt i) start)
201 (if (interval-has-all-properties properties i)
202 (let ((got (- (interval-text-length i) (- start (interval-pt i)))))
203 (when (>= got len)
204 (return-from add-text-properties nil))
205 (decf len got)
206 (setf i (next-interval i)))
207 (progn
208 (setf unchanged i)
209 (setf i (split-interval-right unchanged (- start (interval-pt unchanged))))
210 (copy-properties unchanged i))))
211 ;; if (BUFFERP (object))
212 ;; modify_region (XBUFFER (object), XINT (start), XINT (end));
214 ;; We are at the beginning of interval I, with LEN chars to scan.
215 (loop
216 (when (null i)
217 (error "BORK."))
218 (when (>= (interval-text-length i) len)
219 (when (interval-has-all-properties properties i)
220 (return-from add-text-properties modified))
221 (when (= (interval-text-length i) len)
222 (add-properties properties i object)
223 (return-from add-text-properties t))
224 (setf unchanged i
225 i (split-interval-left unchanged len))
226 (copy-properties unchanged i)
227 (add-properties properties i object)
228 (return-from add-text-properties t))
229 (decf len (interval-text-length i))
230 (setf modified (add-properties properties i object)
231 i (next-interval i)))))
233 (defun put-text-property (start end property value object)
234 "Set one property of the text from START to END. The third and
235 fourth arguments PROPERTY and VALUE specify the property to add. If
236 the optional fifth argument OBJECT is a buffer (or nil, which means
237 the current buffer), START and END are buffer positions (integers or
238 markers). If OBJECT is a string, START and END are 0-based indices
239 into it."
240 (add-text-properties start end (list property value) object))
242 (defun remove-properties (plist list i object)
243 (declare (ignore object))
244 (doplist (sym val plist)
245 (declare (ignore val))
246 (remf sym (interval-plist i)))
247 (dolist (sym list)
248 (remf sym (interval-plist i))))
250 (defun remove-text-properties (start end properties &optional (object (current-buffer)))
251 "Remove some properties from text from START to END. The third
252 argument PROPERTIES is a property list whose property names specify
253 the properties to remove. \(The values stored in PROPERTIES are
254 ignored.) If the optional fourth argument OBJECT is a buffer (or nil,
255 which means the current buffer), START and END are buffer positions
256 (integers or markers). If OBJECT is a string, START and END are
257 0-based indices into it. Return t if any property was actually
258 removed, nil otherwise.
260 Use set-text-properties if you want to remove all text properties."
261 (let (i unchanged len (modified nil))
262 (multiple-value-setq (i start end)
263 (validate-interval-range object start end nil))
264 (when (null i)
265 (return-from remove-text-properties nil))
266 (setf len (- end start))
267 (when (/= (interval-pt i) start)
268 (if (not (interval-has-all-properties properties i))
269 (let ((got (- (interval-text-length i) (- start (interval-pt i)))))
270 (when (>= got len)
271 (return-from remove-text-properties nil))
272 (decf len got)
273 (setf i (next-interval i)))
274 (progn
275 (setf unchanged i
276 i (split-interval-right unchanged (- start (interval-pt unchanged))))
277 (copy-properties unchanged i))))
278 (loop
279 (unless i
280 (error "BORK."))
281 (when (>= (interval-text-length i) len)
282 (unless (interval-has-all-properties properties i)
283 (return-from remove-text-properties modified))
284 (when (= (interval-text-length i) len)
285 (remove-properties properties nil i object)
286 (return-from remove-text-properties t))
287 (setf unchanged i
288 i (split-interval-left i len))
289 (copy-properties unchanged i)
290 (remove-properties properties nil i object)
291 (return-from remove-text-properties t))
292 (decf len (interval-text-length i))
293 (setf modified (remove-properties properties nil i object)
294 i (next-interval i)))))
296 (defun next-single-char-property-change (position prop &optional (object (current-buffer)) limit)
297 "/* Return the position of next text property or overlay change for a specific property.
298 Scans characters forward from POSITION till it finds
299 a change in the PROP property, then returns the position of the change.
300 If the optional third argument OBJECT is a buffer (or nil, which means
301 the current buffer), POSITION is a buffer position (integer or marker).
302 If OBJECT is a string, POSITION is a 0-based index into it.
304 The property values are compared with `eql' by default.
305 If the property is constant all the way to the end of OBJECT, return the
306 last valid position in OBJECT.
307 If the optional fourth argument LIMIT is non-nil, don't search
308 past position LIMIT; return LIMIT if nothing is found before LIMIT. */"
309 (if (typep object 'pstring)
310 (progn
311 (setf position (next-single-property-change position prop object limit))
312 (unless position
313 (if (null limit)
314 (setf position (pstring-length object))
315 (setf position limit))))
316 (let ((initial-value (get-char-property position prop object))
317 value)
318 ;; (when (and (typep object 'buffer)
319 ;; (not (eq object (current-buffer))))
320 ;; (
321 (when (null limit)
322 (setf limit (buffer-max object)))
323 (loop
324 (setf position (next-char-property-change position limit object))
325 (when (>= position limit)
326 (return limit))
327 (setf value (get-char-property position prop object))
328 (unless (eq value initial-value)
329 (return position))))))
331 (defun text-properties-at (position &optional (object (current-buffer)))
332 (multiple-value-bind (i position) (validate-interval-range object position position t)
333 (unless (null i)
334 ;; If POSITION is at the end of the interval,
335 ;; it means it's the end of OBJECT.
336 ;; There are no properties at the very end,
337 ;; since no character follows.
338 (unless (= position (+ (interval-text-length i) (interval-pt i)))
339 (interval-plist i)))))
341 (defun get-text-property (position prop object)
342 (getf (text-properties-at position object) prop))
344 (defun get-char-property-and-overlay (position prop object overlay)
345 (declare (ignore overlay))
346 (get-text-property position prop object))
348 (defun get-char-property (position prop &optional (object (current-buffer)))
349 (get-char-property-and-overlay position prop object 0))
351 (defun previous-single-char-property-change (position prop &optional (object (current-buffer)) limit)
352 (cond ((typep object 'pstring)
353 (setf position (previous-single-property-change position prop object limit))
354 (when (null position)
355 (setf position (or limit
356 (pstring-length object)))))
358 (unless limit
359 (setf limit (buffer-min object)))
360 (if (<= position limit)
361 (setf position limit)
362 (let ((initial-value (get-char-property (1- position) prop object))
363 value)
364 (loop
365 (setf position (previous-char-property-change position limit object))
366 (when (<= position limit)
367 (return limit))
368 (setf value (get-char-property (1- position) prop object))
369 (unless (eq value initial-value)
370 (return position))))))))
372 (defun next-property-change (position &optional object limit)
373 "Return the position of next property change.
374 Scans characters forward from POSITION in OBJECT till it finds
375 a change in some text property, then returns the position of the change.
376 If the optional second argument OBJECT is a buffer (or nil, which means
377 the current buffer), POSITION is a buffer position (integer or marker).
378 If OBJECT is a string, POSITION is a 0-based index into it.
379 Return nil if the property is constant all the way to the end of OBJECT.
380 If the value is non-nil, it is a position greater than POSITION, never equal.
382 If the optional third argument LIMIT is non-nil, don't search
383 past position LIMIT; return LIMIT if nothing is found before LIMIT."
384 (let (i next)
385 (multiple-value-setq (i position) (validate-interval-range object position position nil))
386 (when (eq limit t)
387 (setf next (if (null i)
389 (next-interval i)))
390 (setf position (if (null next)
391 (cond ((typep object 'pstring) (pstring-length object))
392 ((typep object 'buffer) (buffer-max object)))
393 (interval-pt next)))
394 (return-from next-property-change position))
396 (when (null i)
397 (return-from next-property-change limit))
398 (setf next (next-interval i))
399 (loop while (and next
400 (intervals-equal i next)
401 (or (null limit)
402 (< (interval-pt next)
403 limit)))
404 do (setf next (next-interval next)))
405 (when (null next)
406 (return-from next-property-change limit))
407 (when (null limit)
408 (setf limit (cond ((typep object 'pstring) (pstring-length object))
409 ((typep object 'buffer) (buffer-max object)))))
410 (unless (< (interval-pt next) limit)
411 (return-from next-property-change limit))
412 ;; FIXME: This is silly code.
413 (setf position (interval-pt next))
414 position))
416 (defun next-char-property-change (position &optional limit (buffer (current-buffer)))
417 "Return the position of next text property or overlay change.
418 This scans characters forward in the current buffer from POSITION till
419 it finds a change in some text property, or the beginning or end of an
420 overlay, and returns the position of that.
421 If none is found, the function returns (point-max).
423 If the optional third argument LIMIT is non-nil, don't search
424 past position LIMIT; return LIMIT if nothing is found before LIMIT."
425 ;; temp = Fnext_overlay_change (position);
426 (next-property-change position buffer (or limit (buffer-max buffer))))
428 (defun previous-char-property-change (position &optional limit (buffer (current-buffer)))
429 "Return the position of previous text property or overlay change.
430 Scans characters backward in the current buffer from POSITION till it
431 finds a change in some text property, or the beginning or end of an
432 overlay, and returns the position of that.
433 If none is found, the function returns (point-max).
435 If the optional third argument LIMIT is non-nil, don't search
436 past position LIMIT; return LIMIT if nothing is found before LIMIT."
437 (previous-property-change position buffer (or limit (buffer-min buffer))))
439 (defun next-single-property-change (position prop &optional (object (current-buffer)) limit)
440 (let (i next here-val)
441 (multiple-value-setq (i position)
442 (validate-interval-range object position position nil))
443 (when (null i)
444 (return-from next-single-property-change limit))
445 (setf here-val (getf (interval-plist i) prop)
446 next (next-interval i))
447 ;; walk the intervals til we find one with a different plist val
448 ;; for prop.
449 (loop while (and next
450 (eql here-val (getf (interval-plist next) prop))
451 (or (null limit)
452 (< (interval-pt next) limit)))
453 do (setf next (next-interval next)))
454 ;; FIXME: this code should be cleaned.
455 (when (null next)
456 (return-from next-single-property-change limit))
457 (setf limit (or limit
458 (cond ((typep object 'pstring) (pstring-length object))
459 ((typep object 'buffer) (buffer-max object)))))
460 (when (>= (interval-pt next) limit)
461 (return-from next-single-property-change limit))
462 (interval-pt next)))
464 (defun previous-single-property-change (position prop &optional (object (current-buffer)) limit)
465 (let (i previous here-val)
466 (multiple-value-setq (i position) (validate-interval-range object position position nil))
467 (when (and i
468 (= (interval-pt i) position))
469 (setf i (previous-interval i)))
470 (unless i
471 (return-from previous-single-property-change limit))
472 (setf here-val (getf (interval-plist i) prop)
473 previous (previous-interval i))
474 (loop while (and previous
475 (eql here-val (getf (interval-plist previous) prop))
476 (or (null limit)
477 (> (+ (interval-pt previous) (interval-text-length previous))
478 limit)))
479 do (setf previous (previous-interval previous)))
480 ;; FIXME: this code should be cleaned.
481 (when (null previous)
482 (return-from previous-single-property-change limit))
483 (setf limit (or limit
484 (cond ((typep object 'pstring) 0)
485 ((typep object 'buffer) (buffer-min object)))))
486 (when (<= (+ (interval-pt previous) (interval-text-length previous))
487 limit)
488 (return-from previous-single-property-change limit))
489 (+ (interval-pt previous) (interval-text-length previous))))
491 (defun previous-property-change (position &optional (object (current-buffer)) limit)
492 "Return the position of previous property change.
493 Scans characters backwards from POSITION in OBJECT till it finds
494 a change in some text property, then returns the position of the change.
495 If the optional second argument OBJECT is a buffer (or nil, which means
496 the current buffer), POSITION is a buffer position (integer or marker).
497 If OBJECT is a string, POSITION is a 0-based index into it.
498 Return nil if the property is constant all the way to the start of OBJECT.
499 If the value is non-nil, it is a position less than POSITION, never equal.
501 If the optional third argument LIMIT is non-nil, don't search
502 back past position LIMIT; return LIMIT if nothing is found until LIMIT."
503 (let (i previous)
504 (multiple-value-setq (i position) (validate-interval-range object position position nil))
505 (unless i
506 (return-from previous-property-change limit))
507 (when (= (interval-pt i) position)
508 (setf i (previous-interval i)))
509 (setf previous (previous-interval i))
510 (loop while (and previous
511 (intervals-equal previous i)
512 (or (null limit)
513 (> (+ (interval-pt previous)
514 (interval-text-length previous))
515 limit)))
516 do (setf previous (previous-interval previous)))
517 ;; FIXME: this code needs cleaning
518 (when (null previous)
519 (return-from previous-property-change limit))
520 (setf limit (or limit
521 (cond ((typep object 'pstring) 0)
522 ((typep object 'buffer) (buffer-min object)))))
523 (when (<= (+ (interval-pt previous) (interval-text-length previous))
524 limit)
525 (return-from previous-property-change limit))
526 (+ (interval-pt previous) (interval-text-length previous))))
528 (defun text-property-stickiness (prop pos &optional (buffer (current-buffer)))
529 "Return the direction from which the text-property PROP would be
530 inherited by any new text inserted at POS: AFTER if it would be
531 inherited from the char after POS, BEFORE if it would be inherited from
532 the char before POS, and NIL if from neither.
533 BUFFER can be either a buffer or nil (meaning current buffer)."
534 (labels ((tmem (sym set)
535 ;; Test for membership, allowing for t (actually any
536 ;; non-cons) to mean the universal set."
537 (if (consp set)
538 (find sym set)
539 set)))
540 (let ((is-rear-sticky t)
541 (is-front-sticky nil)
542 prev-pos front-sticky)
543 (when (> pos (begv buffer))
544 ;; Consider previous character.
545 (setf prev-pos (1- pos))
546 (let ((rear-non-sticky (get-text-property prev-pos 'rear-nonsticky buffer)))
547 (when (tmem prop rear-non-sticky)
548 ;; PROP is rear-non-sticky
549 (setf is-rear-sticky nil))))
550 ;; Consider following character.
551 (setf front-sticky (get-text-property pos 'front-sticky buffer))
552 (when (or (eq front-sticky t)
553 (and (consp front-sticky)
554 (find prop front-sticky)))
555 ;; PROP is inherited from after
556 (setf is-front-sticky t))
557 ;; return the symbol
558 (cond
559 ;; Simple cases, where the properties are consistent.
560 ((and is-rear-sticky
561 (not is-front-sticky))
562 'before)
563 ((and (not is-rear-sticky)
564 is-front-sticky)
565 'after)
566 ((and (not is-rear-sticky)
567 (not is-front-sticky))
568 nil)
569 ((or (= pos (begv buffer))
570 (null (get-text-property prev-pos prop buffer)))
571 'after)
573 'before)))))
575 (provide :lice-0.1/textprop)