[lice @ simulate interrupt key when waiting for input]
[lice.git] / textprop.lisp
blob23aea980250acc693ab656fc04c25940ac92cc10
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 (while (> len 0)
109 (when (null i)
110 (error "borked."))
111 (when (>= (interval-text-length i) len)
112 (when (> (interval-text-length i) len)
113 (setf i (split-interval-left i len)))
114 (set-properties properties i buffer)
115 (when prev-changed
116 (merge-interval-left i))
117 (return-from set-text-properties-1))
118 (decf len (interval-text-length i))
119 ;; We have to call set_properties even if we are going
120 ;; to merge the intervals, so as to make the undo
121 ;; records and cause redisplay to happen.
122 (set-properties properties i buffer)
123 (if (null prev-changed)
124 (setf prev-changed i)
125 (setf prev-changed (merge-interval-left i)
126 i prev-changed))
127 (setf i (next-interval i)))))
129 (defun copy-properties (source target)
130 (when (and (default-interval-p source)
131 (default-interval-p target))
132 (return-from copy-properties))
133 (setf (interval-plist target) (copy-list (interval-plist source))))
135 (defun set-properties (properties interval object)
136 (when (typep object 'buffer)
137 ;; record undo info
139 (setf (interval-plist interval) (copy-tree properties)))
141 (defun add-properties (plist i object)
142 "Add the properties in plist to interval I. OBJECT should be the
143 string of buffer containing the interval."
144 (declare (ignore object))
145 (let ((changed nil))
146 (doplist (sym val plist changed)
147 (let ((found (getf (interval-plist i) sym)))
148 (if found
149 (progn
150 ;; record-property-change
151 (when (not (eql found val))
152 (setf (getf (interval-plist i) sym) val
153 changed t)))
154 (progn
155 ;; record-property-change
156 (setf (getf (interval-plist i) sym) val
157 changed t)))))))
159 (defun validate-plist (list)
160 "/* Validate LIST as a property list. If LIST is not a list, then
161 make one consisting of (LIST nil). Otherwise, verify that LIST is
162 even numbered and thus suitable as a plist. */"
163 (cond ((null list) nil)
164 ((consp list)
165 (if (oddp (length list))
166 (error "odd length property list")
167 list))
168 (t (list (list list nil)))))
170 (defun interval-has-all-properties (plist i)
171 "/* Return nonzero if interval I has all the properties, with the same
172 values, of list PLIST. */"
173 (doplist (sym val plist t)
174 (let ((found (getf (interval-plist i) sym)))
175 (if found
176 (when (not (eql found val))
177 (return-from interval-has-all-properties nil))
178 (return-from interval-has-all-properties nil)))))
180 (defun add-text-properties (start end properties &optional (object (current-buffer)))
181 "Add properties to the text from START to END. The third argument
182 PROPERTIES is a property list specifying the property values to add.
183 If the optional fourth argument OBJECT is a buffer (or nil, which
184 means the current buffer), START and END are buffer positions
185 (integers or markers). If OBJECT is a string, START and END are
186 0-based indices into it. Return t if any property value actually
187 changed, nil otherwise."
188 (let ((len (- end start))
189 (modified 0)
190 i unchanged)
191 (setf properties (validate-plist properties))
192 (when (null properties)
193 (return-from add-text-properties nil))
194 (multiple-value-setq (i start end) (validate-interval-range object start end t))
195 (when (null i)
196 (return-from add-text-properties nil))
197 ;; If we're not starting on an interval boundary, we have to split
198 ;; this interval.
199 (when (/= (interval-pt i) start)
200 (if (interval-has-all-properties properties i)
201 (let ((got (- (interval-text-length i) (- start (interval-pt i)))))
202 (when (>= got len)
203 (return-from add-text-properties nil))
204 (decf len got)
205 (setf i (next-interval i)))
206 (progn
207 (setf unchanged i)
208 (setf i (split-interval-right unchanged (- start (interval-pt unchanged))))
209 (copy-properties unchanged i))))
210 ;; if (BUFFERP (object))
211 ;; modify_region (XBUFFER (object), XINT (start), XINT (end));
213 ;; We are at the beginning of interval I, with LEN chars to scan.
214 (loop
215 (when (null i)
216 (error "BORK."))
217 (when (>= (interval-text-length i) len)
218 (when (interval-has-all-properties properties i)
219 (return-from add-text-properties modified))
220 (when (= (interval-text-length i) len)
221 (add-properties properties i object)
222 (return-from add-text-properties t))
223 (setf unchanged i
224 i (split-interval-left unchanged len))
225 (copy-properties unchanged i)
226 (add-properties properties i object)
227 (return-from add-text-properties t))
228 (decf len (interval-text-length i))
229 (setf modified (add-properties properties i object)
230 i (next-interval i)))))
232 (defun put-text-property (start end property value object)
233 "Set one property of the text from START to END. The third and
234 fourth arguments PROPERTY and VALUE specify the property to add. If
235 the optional fifth argument OBJECT is a buffer (or nil, which means
236 the current buffer), START and END are buffer positions (integers or
237 markers). If OBJECT is a string, START and END are 0-based indices
238 into it."
239 (add-text-properties start end (list property value) object))
241 (defun remove-properties (plist list i object)
242 (declare (ignore object))
243 (doplist (sym val plist)
244 (declare (ignore val))
245 (remf sym (interval-plist i)))
246 (dolist (sym list)
247 (remf sym (interval-plist i))))
249 (defun remove-text-properties (start end properties &optional (object (current-buffer)))
250 "Remove some properties from text from START to END. The third
251 argument PROPERTIES is a property list whose property names specify
252 the properties to remove. \(The values stored in PROPERTIES are
253 ignored.) If the optional fourth argument OBJECT is a buffer (or nil,
254 which means the current buffer), START and END are buffer positions
255 (integers or markers). If OBJECT is a string, START and END are
256 0-based indices into it. Return t if any property was actually
257 removed, nil otherwise.
259 Use set-text-properties if you want to remove all text properties."
260 (let (i unchanged len (modified nil))
261 (multiple-value-setq (i start end)
262 (validate-interval-range object start end nil))
263 (when (null i)
264 (return-from remove-text-properties nil))
265 (setf len (- end start))
266 (when (/= (interval-pt i) start)
267 (if (not (interval-has-all-properties properties i))
268 (let ((got (- (interval-text-length i) (- start (interval-pt i)))))
269 (when (>= got len)
270 (return-from remove-text-properties nil))
271 (decf len got)
272 (setf i (next-interval i)))
273 (progn
274 (setf unchanged i
275 i (split-interval-right unchanged (- start (interval-pt unchanged))))
276 (copy-properties unchanged i))))
277 (loop
278 (unless i
279 (error "BORK."))
280 (when (>= (interval-text-length i) len)
281 (unless (interval-has-all-properties properties i)
282 (return-from remove-text-properties modified))
283 (when (= (interval-text-length i) len)
284 (remove-properties properties nil i object)
285 (return-from remove-text-properties t))
286 (setf unchanged i
287 i (split-interval-left i len))
288 (copy-properties unchanged i)
289 (remove-properties properties nil i object)
290 (return-from remove-text-properties t))
291 (decf len (interval-text-length i))
292 (setf modified (remove-properties properties nil i object)
293 i (next-interval i)))))
295 (defun next-single-char-property-change (position prop &optional (object (current-buffer)) limit)
296 "/* Return the position of next text property or overlay change for a specific property.
297 Scans characters forward from POSITION till it finds
298 a change in the PROP property, then returns the position of the change.
299 If the optional third argument OBJECT is a buffer (or nil, which means
300 the current buffer), POSITION is a buffer position (integer or marker).
301 If OBJECT is a string, POSITION is a 0-based index into it.
303 The property values are compared with `eql' by default.
304 If the property is constant all the way to the end of OBJECT, return the
305 last valid position in OBJECT.
306 If the optional fourth argument LIMIT is non-nil, don't search
307 past position LIMIT; return LIMIT if nothing is found before LIMIT. */"
308 (if (typep object 'pstring)
309 (progn
310 (setf position (next-single-property-change position prop object limit))
311 (unless position
312 (if (null limit)
313 (setf position (pstring-length object))
314 (setf position limit))))
315 (let ((initial-value (get-char-property position prop object))
316 value)
317 ;; (when (and (typep object 'buffer)
318 ;; (not (eq object (current-buffer))))
319 ;; (
320 (when (null limit)
321 (setf limit (buffer-max object)))
322 (loop
323 (setf position (next-char-property-change position limit object))
324 (when (>= position limit)
325 (return limit))
326 (setf value (get-char-property position prop object))
327 (unless (eq value initial-value)
328 (return position))))))
330 (defun text-properties-at (position &optional (object (current-buffer)))
331 (multiple-value-bind (i position) (validate-interval-range object position position t)
332 (unless (null i)
333 ;; If POSITION is at the end of the interval,
334 ;; it means it's the end of OBJECT.
335 ;; There are no properties at the very end,
336 ;; since no character follows.
337 (unless (= position (+ (interval-text-length i) (interval-pt i)))
338 (interval-plist i)))))
340 (defun get-text-property (position prop &optional (object (current-buffer)))
341 (getf (text-properties-at position object) prop))
343 (defun get-char-property-and-overlay (position prop object overlay)
344 (declare (ignore overlay))
345 (get-text-property position prop object))
347 (defun get-char-property (position prop &optional (object (current-buffer)))
348 (get-char-property-and-overlay position prop object 0))
350 (defun previous-single-char-property-change (position prop &optional (object (current-buffer)) limit)
351 (cond ((typep object 'pstring)
352 (setf position (previous-single-property-change position prop object limit))
353 (when (null position)
354 (setf position (or limit
355 (pstring-length object)))))
357 (unless limit
358 (setf limit (buffer-min object)))
359 (if (<= position limit)
360 (setf position limit)
361 (let ((initial-value (get-char-property (1- position) prop object))
362 value)
363 (loop
364 (setf position (previous-char-property-change position limit object))
365 (when (<= position limit)
366 (return limit))
367 (setf value (get-char-property (1- position) prop object))
368 (unless (eq value initial-value)
369 (return position))))))))
371 (defun next-property-change (position &optional object limit)
372 "Return the position of next property change.
373 Scans characters forward from POSITION in OBJECT till it finds
374 a change in some text property, then returns the position of the change.
375 If the optional second argument OBJECT is a buffer (or nil, which means
376 the current buffer), POSITION is a buffer position (integer or marker).
377 If OBJECT is a string, POSITION is a 0-based index into it.
378 Return nil if the property is constant all the way to the end of OBJECT.
379 If the value is non-nil, it is a position greater than POSITION, never equal.
381 If the optional third argument LIMIT is non-nil, don't search
382 past position LIMIT; return LIMIT if nothing is found before LIMIT."
383 (let (i next)
384 (multiple-value-setq (i position) (validate-interval-range object position position nil))
385 (when (eq limit t)
386 (setf next (if (null i)
388 (next-interval i)))
389 (setf position (if (null next)
390 (cond ((typep object 'pstring) (pstring-length object))
391 ((typep object 'buffer) (buffer-max object)))
392 (interval-pt next)))
393 (return-from next-property-change position))
395 (when (null i)
396 (return-from next-property-change limit))
397 (setf next (next-interval i))
398 (while (and next
399 (intervals-equal i next)
400 (or (null limit)
401 (< (interval-pt next)
402 limit)))
403 (setf next (next-interval next)))
404 (when (null next)
405 (return-from next-property-change limit))
406 (when (null limit)
407 (setf limit (cond ((typep object 'pstring) (pstring-length object))
408 ((typep object 'buffer) (buffer-max object)))))
409 (unless (< (interval-pt next) limit)
410 (return-from next-property-change limit))
411 ;; FIXME: This is silly code.
412 (setf position (interval-pt next))
413 position))
415 (defun next-char-property-change (position &optional limit (buffer (current-buffer)))
416 "Return the position of next text property or overlay change.
417 This scans characters forward in the current buffer from POSITION till
418 it finds a change in some text property, or the beginning or end of an
419 overlay, and returns the position of that.
420 If none is found, the function returns (point-max).
422 If the optional third argument LIMIT is non-nil, don't search
423 past position LIMIT; return LIMIT if nothing is found before LIMIT."
424 ;; temp = Fnext_overlay_change (position);
425 (next-property-change position buffer (or limit (buffer-max buffer))))
427 (defun previous-char-property-change (position &optional limit (buffer (current-buffer)))
428 "Return the position of previous text property or overlay change.
429 Scans characters backward in the current buffer from POSITION till it
430 finds a change in some text property, or the beginning or end of an
431 overlay, and returns the position of that.
432 If none is found, the function returns (point-max).
434 If the optional third argument LIMIT is non-nil, don't search
435 past position LIMIT; return LIMIT if nothing is found before LIMIT."
436 (previous-property-change position buffer (or limit (buffer-min buffer))))
438 (defun next-single-property-change (position prop &optional (object (current-buffer)) limit)
439 (let (i next here-val)
440 (multiple-value-setq (i position)
441 (validate-interval-range object position position nil))
442 (when (null i)
443 (return-from next-single-property-change limit))
444 (setf here-val (getf (interval-plist i) prop)
445 next (next-interval i))
446 ;; walk the intervals til we find one with a different plist val
447 ;; for prop.
448 (while (and next
449 (eql here-val (getf (interval-plist next) prop))
450 (or (null limit)
451 (< (interval-pt next) limit)))
452 (setf next (next-interval next)))
453 ;; FIXME: this code should be cleaned.
454 (when (null next)
455 (return-from next-single-property-change limit))
456 (setf limit (or limit
457 (cond ((typep object 'pstring) (pstring-length object))
458 ((typep object 'buffer) (buffer-max object)))))
459 (when (>= (interval-pt next) limit)
460 (return-from next-single-property-change limit))
461 (interval-pt next)))
463 (defun previous-single-property-change (position prop &optional (object (current-buffer)) limit)
464 (let (i previous here-val)
465 (multiple-value-setq (i position) (validate-interval-range object position position nil))
466 (when (and i
467 (= (interval-pt i) position))
468 (setf i (previous-interval i)))
469 (unless i
470 (return-from previous-single-property-change limit))
471 (setf here-val (getf (interval-plist i) prop)
472 previous (previous-interval i))
473 (while (and previous
474 (eql here-val (getf (interval-plist previous) prop))
475 (or (null limit)
476 (> (+ (interval-pt previous) (interval-text-length previous))
477 limit)))
478 (setf previous (previous-interval previous)))
479 ;; FIXME: this code should be cleaned.
480 (when (null previous)
481 (return-from previous-single-property-change limit))
482 (setf limit (or limit
483 (cond ((typep object 'pstring) 0)
484 ((typep object 'buffer) (buffer-min object)))))
485 (when (<= (+ (interval-pt previous) (interval-text-length previous))
486 limit)
487 (return-from previous-single-property-change limit))
488 (+ (interval-pt previous) (interval-text-length previous))))
490 (defun previous-property-change (position &optional (object (current-buffer)) limit)
491 "Return the position of previous property change.
492 Scans characters backwards from POSITION in OBJECT till it finds
493 a change in some text property, then returns the position of the change.
494 If the optional second argument OBJECT is a buffer (or nil, which means
495 the current buffer), POSITION is a buffer position (integer or marker).
496 If OBJECT is a string, POSITION is a 0-based index into it.
497 Return nil if the property is constant all the way to the start of OBJECT.
498 If the value is non-nil, it is a position less than POSITION, never equal.
500 If the optional third argument LIMIT is non-nil, don't search
501 back past position LIMIT; return LIMIT if nothing is found until LIMIT."
502 (let (i previous)
503 (multiple-value-setq (i position) (validate-interval-range object position position nil))
504 (unless i
505 (return-from previous-property-change limit))
506 (when (= (interval-pt i) position)
507 (setf i (previous-interval i)))
508 (setf previous (previous-interval i))
509 (while (and previous
510 (intervals-equal previous i)
511 (or (null limit)
512 (> (+ (interval-pt previous)
513 (interval-text-length previous))
514 limit)))
515 (setf previous (previous-interval previous)))
516 ;; FIXME: this code needs cleaning
517 (when (null previous)
518 (return-from previous-property-change limit))
519 (setf limit (or limit
520 (cond ((typep object 'pstring) 0)
521 ((typep object 'buffer) (buffer-min object)))))
522 (when (<= (+ (interval-pt previous) (interval-text-length previous))
523 limit)
524 (return-from previous-property-change limit))
525 (+ (interval-pt previous) (interval-text-length previous))))
527 (defun text-property-stickiness (prop pos &optional (buffer (current-buffer)))
528 "Return the direction from which the text-property PROP would be
529 inherited by any new text inserted at POS: AFTER if it would be
530 inherited from the char after POS, BEFORE if it would be inherited from
531 the char before POS, and NIL if from neither.
532 BUFFER can be either a buffer or nil (meaning current buffer)."
533 (labels ((tmem (sym set)
534 ;; Test for membership, allowing for t (actually any
535 ;; non-cons) to mean the universal set."
536 (if (consp set)
537 (find sym set)
538 set)))
539 (let ((is-rear-sticky t)
540 (is-front-sticky nil)
541 prev-pos front-sticky)
542 (when (> pos (begv buffer))
543 ;; Consider previous character.
544 (setf prev-pos (1- pos))
545 (let ((rear-non-sticky (get-text-property prev-pos 'rear-nonsticky buffer)))
546 (when (tmem prop rear-non-sticky)
547 ;; PROP is rear-non-sticky
548 (setf is-rear-sticky nil))))
549 ;; Consider following character.
550 (setf front-sticky (get-text-property pos 'front-sticky buffer))
551 (when (or (eq front-sticky t)
552 (and (consp front-sticky)
553 (find prop front-sticky)))
554 ;; PROP is inherited from after
555 (setf is-front-sticky t))
556 ;; return the symbol
557 (cond
558 ;; Simple cases, where the properties are consistent.
559 ((and is-rear-sticky
560 (not is-front-sticky))
561 'before)
562 ((and (not is-rear-sticky)
563 is-front-sticky)
564 'after)
565 ((and (not is-rear-sticky)
566 (not is-front-sticky))
567 nil)
568 ((or (= pos (begv buffer))
569 (null (get-text-property prev-pos prop buffer)))
570 'after)
572 'before)))))
574 (provide :lice-0.1/textprop)