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