[lice @ indent-region (buggy) + eval-defun]
[lice.git] / editfns.lisp
blob9fe7b7a554ddbe5a9779c07e09ac5c1776f47617
1 (in-package :lice)
3 (defun get-pos-property (position prop &optional (object (current-buffer)))
4 "Return the value of property PROP, in OBJECT at POSITION.
5 It's the value of PROP that a char inserted at POSITION would get.
6 OBJECT is optional and defaults to the current buffer.
7 If OBJECT is a buffer, then overlay properties are considered as well as
8 text properties.
9 If OBJECT is a window, then that window's buffer is used, but
10 window-specific overlays are considered only if they are associated
11 with OBJECT."
12 (when (typep object 'window)
13 (setf object (window-buffer object)))
14 (if (not (typep object 'buffer))
15 (get-text-property position prop object)
16 ;;; XXX: handle overlays.
17 (let ((stickiness (text-property-stickiness prop position object)))
18 (cond
19 ((eq stickiness 'after)
20 (get-text-property position prop object))
21 ((eq stickiness 'before)
22 (get-text-property (1- position) prop object))
23 (t nil)))))
25 (defun find-field (pos merge-at-boundary &key beg-limit beg end-limit end (buf (current-buffer)))
26 "Find the field surrounding POS and return the beginning and end of
27 the field in a values list. If POS is nil, the value of point is used
28 instead. If BEG or END is nil then that boundary isn't calculated.
30 BEG_LIMIT and END_LIMIT serve to limit the ranged of the returned
31 results; they do not effect boundary behavior.
33 If MERGE_AT_BOUNDARY is nonzero, then if POS is at the very first
34 position of a field, then the beginning of the previous field is
35 returned instead of the beginning of POS's field (since the end of a
36 field is actually also the beginning of the next input field, this
37 behavior is sometimes useful). Additionally in the MERGE_AT_BOUNDARY
38 true case, if two fields are separated by a field with the special
39 value `boundary', and POS lies within it, then the two separated
40 fields are considered to be adjacent, and POS between them, when
41 finding the beginning and ending of the \"merged\" field.
43 Either BEG or END may be 0, in which case the corresponding value
44 is not stored."
45 (let ((at-field-start nil)
46 (at-field-end nil)
47 before-field after-field)
48 (unless pos
49 (setf pos (point)))
50 (setf after-field (get-char-property-and-overlay pos 'field buf nil)
51 before-field (if (> pos (begv buf))
52 (get-char-property-and-overlay (1- pos) 'field buf nil)
53 nil))
54 ;; See if we need to handle the case where MERGE_AT_BOUNDARY is nil
55 ;; and POS is at beginning of a field, which can also be interpreted
56 ;; as the end of the previous field. Note that the case where if
57 ;; MERGE_AT_BOUNDARY is non-nil (see function comment) is actually the
58 ;; more natural one; then we avoid treating the beginning of a field
59 ;; specially.
60 (unless merge-at-boundary
61 (let ((field (get-pos-property pos 'field buf)))
62 (when (not (eq field after-field))
63 (setf at-field-end t))
64 (when (not (eq field before-field))
65 (setf at-field-start t))
66 (when (and (null field)
67 at-field-start
68 at-field-end)
69 ;; If an inserted char would have a nil field while the surrounding
70 ;; text is non-nil, we're probably not looking at a
71 ;; zero-length field, but instead at a non-nil field that's
72 ;; not intended for editing (such as comint's prompts).
73 (setf at-field-end nil
74 at-field-start nil))))
75 ;; Note about special `boundary' fields:
77 ;; Consider the case where the point (`.') is between the fields `x' and `y':
79 ;; xxxx.yyyy
81 ;; In this situation, if merge_at_boundary is true, we consider the
82 ;; `x' and `y' fields as forming one big merged field, and so the end
83 ;; of the field is the end of `y'.
85 ;; However, if `x' and `y' are separated by a special `boundary' field
86 ;; (a field with a `field' char-property of 'boundary), then we ignore
87 ;; this special field when merging adjacent fields. Here's the same
88 ;; situation, but with a `boundary' field between the `x' and `y' fields:
90 ;; xxx.BBBByyyy
92 ;; Here, if point is at the end of `x', the beginning of `y', or
93 ;; anywhere in-between (within the `boundary' field), we merge all
94 ;; three fields and consider the beginning as being the beginning of
95 ;; the `x' field, and the end as being the end of the `y' field. */
97 ;; Return field boundary
98 (values (and beg
99 (if at-field-start
101 (let ((p pos))
102 (if (and (null merge-at-boundary)
103 (eq before-field 'boundary))
104 (setf p (previous-single-char-property-change p 'field buf beg-limit))
105 (setf p (previous-single-char-property-change p 'field buf beg-limit)))
106 (or p
107 (begv buf)))))
108 (and end
109 (if at-field-end
111 (progn
112 (when (and (null merge-at-boundary)
113 (eq after-field 'boundary))
114 (setf pos (next-single-char-property-change pos 'field buf end-limit)))
115 (setf pos (next-single-char-property-change pos 'field buf end-limit))
116 (or pos
117 (zv buf))))))))
119 (defun make-buffer-string (start end props &optional (buffer (current-buffer)))
120 "Making strings from buffer contents.
122 Return a Lisp_String containing the text of the current buffer from
123 START to END. If text properties are in use and the current buffer has
124 properties in the range specified, the resulting string will also have
125 them, if PROPS is nonzero.
127 We don't want to use plain old make_string here, because it calls
128 make_uninit_string, which can cause the buffer arena to be
129 compacted. make_string has no way of knowing that the data has
130 been moved, and thus copies the wrong data into the string. This
131 doesn't effect most of the other users of make_string, so it should
132 be left as is. But we should use this function when conjuring
133 buffer substrings."
134 (declare (ignore props))
135 ;; If the gap intersects with the range we wanna grab, move it.
136 (if (= start end)
138 (progn
139 (when (and (< start (buffer-gap-start buffer))
140 (< (buffer-gap-start buffer) end))
141 (gap-move-to buffer start))
142 (dformat +debug-v+ "substring: ~a ~a ~a~%" start end (length (buffer-data buffer)))
143 (subseq (buffer-data buffer)
144 (buffer-char-to-aref buffer start)
145 (1+ (buffer-char-to-aref buffer (1- end)))))))
147 (defun buffer-substring (start end &optional (buffer (current-buffer)))
148 "Return the contents of part of the current buffer as a string.
149 The two arguments START and END are character positions;
150 they can be in either order.
151 The string returned is multibyte if the buffer is multibyte.
153 This function copies the text properties of that part of the buffer
154 into the result string; if you don't want the text properties,
155 use `buffer-substring-no-properties' instead."
156 (multiple-value-setq (start end) (validate-region start end buffer))
157 (make-buffer-string start end t buffer))
159 (defun buffer-substring-no-properties (start end &optional (buffer (current-buffer)))
160 "Return the characters of part of the buffer, without the text properties.
161 The two arguments START and END are character positions;
162 they can be in either order."
163 (multiple-value-setq (start end) (validate-region start end buffer))
164 (make-buffer-string start end nil buffer))
167 (defun field-string (pos)
168 "Return the contents of the field surrounding POS as a string.
169 A field is a region of text with the same `field' property.
170 If POS is nil, the value of point is used for POS."
171 (multiple-value-bind (beg end) (find-field pos nil :beg t :end t)
172 (make-buffer-string beg end t)))
174 (defun field-beginning (&optional pos escape-from-edge limit)
175 "Return the beginning of the field surrounding POS.
176 A field is a region of text with the same `field' property.
177 If POS is nil, the value of point is used for POS.
178 If ESCAPE-FROM-EDGE is non-nil and POS is at the beginning of its
179 field, then the beginning of the *previous* field is returned.
180 If LIMIT is non-nil, it is a buffer position; if the beginning of the field
181 is before LIMIT, then LIMIT will be returned instead."
182 (declare (ignore escape-from-edge))
183 (multiple-value-bind (beg end) (find-field pos nil :beg-limit limit :beg t)
184 (declare (ignore end))
185 beg))
187 (defun field-end (&optional pos escape-from-edge limit)
188 "Return the end of the field surrounding POS.
189 A field is a region of text with the same `field' property.
190 If POS is nil, the value of point is used for POS.
191 If ESCAPE-FROM-EDGE is non-nil and POS is at the end of its field,
192 then the end of the *following* field is returned.
193 If LIMIT is non-nil, it is a buffer position; if the end of the field
194 is after LIMIT, then LIMIT will be returned instead."
195 (declare (ignore escape-from-edge))
196 (multiple-value-bind (beg end) (find-field pos nil :end-limit limit :end t)
197 (declare (ignore beg))
198 end))
200 (defvar *inhibit-field-text-motion* nil
201 "Non-nil means text motion commands don't notice fields.")
203 (defun constrain-to-field (new-pos old-pos &optional escape-from-edge only-in-line inhibit-capture-property)
204 "Return the position closest to NEW-POS that is in the same field as OLD-POS.
206 A field is a region of text with the same `field' property.
207 If NEW-POS is nil, then the current point is used instead, and set to the
208 constrained position if that is different.
210 If OLD-POS is at the boundary of two fields, then the allowable
211 positions for NEW-POS depends on the value of the optional argument
212 ESCAPE-FROM-EDGE: If ESCAPE-FROM-EDGE is nil, then NEW-POS is
213 constrained to the field that has the same `field' char-property
214 as any new characters inserted at OLD-POS, whereas if ESCAPE-FROM-EDGE
215 is non-nil, NEW-POS is constrained to the union of the two adjacent
216 fields. Additionally, if two fields are separated by another field with
217 the special value `boundary', then any point within this special field is
218 also considered to be `on the boundary'.
220 If the optional argument ONLY-IN-LINE is non-nil and constraining
221 NEW-POS would move it to a different line, NEW-POS is returned
222 unconstrained. This useful for commands that move by line, like
223 \\[next-line] or \\[beginning-of-line], which should generally respect field boundaries
224 only in the case where they can still move to the right line.
226 If the optional argument INHIBIT-CAPTURE-PROPERTY is non-nil, and OLD-POS has
227 a non-nil property of that name, then any field boundaries are ignored.
229 Field boundaries are not noticed if `inhibit-field-text-motion' is non-nil."
230 (let ((orig-point 0)
231 fwd prev-old prev-new)
232 (unless new-pos
233 ;; Use the current point, and afterwards, set it.
234 (setf new-pos (point)
235 orig-point new-pos))
236 (check-type new-pos number)
237 (check-type old-pos number)
238 (setf fwd (> new-pos old-pos)
239 prev-old (1- old-pos)
240 prev-new (1- new-pos))
241 (when (and (null *inhibit-field-text-motion*)
242 (/= new-pos old-pos)
243 (or (get-char-property new-pos 'field)
244 (get-char-property old-pos 'field)
245 ;; To recognize field boundaries, we must also look at the
246 ;; previous positions; we could use `get_pos_property'
247 ;; instead, but in itself that would fail inside non-sticky
248 ;; fields (like comint prompts).
249 (and (> new-pos (begv))
250 (get-char-property prev-new 'field))
251 (and (> old-pos (begv))
252 (get-char-property prev-old 'field)))
253 (or (null inhibit-capture-property)
254 (and (null (get-pos-property old-pos inhibit-capture-property nil))
255 (or (<= old-pos (begv))
256 (null (get-char-property old-pos inhibit-capture-property))
257 (null (get-char-property prev-old inhibit-capture-property))))))
258 ;; It is possible that NEW_POS is not within the same field as
259 ;; OLD_POS; try to move NEW_POS so that it is.
260 (let ((field-bound (if fwd
261 (field-end old-pos escape-from-edge new-pos)
262 (field-beginning old-pos escape-from-edge new-pos))))
263 (when (and
264 ;; See if ESCAPE_FROM_EDGE caused FIELD_BOUND to jump to the
265 ;; other side of NEW_POS, which would mean that NEW_POS is
266 ;; already acceptable, and it's not necessary to constrain it
267 ;; to FIELD_BOUND.
268 (if (< field-bound new-pos) fwd (not fwd))
269 ;; NEW_POS should be constrained, but only if either
270 ;; ONLY_IN_LINE is nil (in which case any constraint is OK),
271 ;; or NEW_POS and FIELD_BOUND are on the same line (in which
272 ;; case the constraint is OK even if ONLY_IN_LINE is non-nil). */
273 (or (null only-in-line)
274 ;; This is the ONLY_IN_LINE case, check that NEW_POS and
275 ;; FIELD_BOUND are on the same line by seeing whether
276 ;; there's an intervening newline or not.
277 (progn
278 (multiple-value-bind (p nfound)
279 (buffer-scan-newline (current-buffer) new-pos field-bound (if fwd -1 1))
280 (declare (ignore p))
281 (zerop nfound)))))
282 ;; Constrain NEW_POS to FIELD_BOUND.
283 (setf new-pos field-bound))
284 (when (and orig-point
285 (/= new-pos orig-point))
286 (set-point new-pos))))
287 new-pos))
289 (defun npropertize (string &rest props)
290 "Same as propertize but don't make a copy of STRING."
291 (declare (type string string))
292 (let ((ps (make-instance 'pstring
293 :data string)))
294 (create-root-interval ps)
295 (add-text-properties 0 (pstring-length ps) props ps)
296 ps))
298 (defun propertize (string &rest props)
299 "Return a copy of STRING with text properties added.
300 First argument is the string to copy.
301 Remaining arguments form a sequence of PROPERTY VALUE pairs for text
302 properties to add to the result.
303 usage: (propertize STRING &rest PROPERTIES)"
304 (declare (type string string))
305 (apply #'npropertize (copy-seq string) props))
307 (defun delete-region (start end &optional (buffer (current-buffer)))
308 "Delete the text between point and mark.
310 expects two arguments, positions (integers or markers) specifying
311 the stretch to be deleted."
312 (multiple-value-setq (start end) (validate-region start end buffer))
313 (buffer-delete buffer start (- end start)))
315 (defmacro save-current-buffer (&body body)
316 "Save the current buffer; execute BODY; restore the current buffer.
317 Executes BODY just like `progn'."
318 (let ((cb (gensym "CB")))
319 `(let ((,cb (current-buffer)))
320 (unwind-protect (progn ,@body)
321 (when (get-buffer ,cb)
322 (set-buffer cb))))))
324 (defmacro save-excursion (&body body)
325 "Save point, mark, and current buffer; execute BODY; restore those things.
326 Executes BODY just like `progn'.
327 The values of point, mark and the current buffer are restored
328 even in case of abnormal exit (throw or error).
329 *The state of activation of the mark is also restored.
331 *This construct does not save `deactivate-mark', and therefore
332 *functions that change the buffer will still cause deactivation
333 *of the mark at the end of the command. To prevent that, bind
334 *`deactivate-mark' with `let'."
335 (let ((cb (gensym "CB"))
336 (point (gensym "POINT"))
337 (mark (gensym "MARK")))
338 `(let ((,cb (current-buffer))
339 (,point (copy-marker (point-marker)))
340 (,mark (copy-marker (mark-marker))))
341 (unwind-protect (progn ,@body)
342 (when (get-buffer ,cb)
343 (set-buffer ,cb)
344 (setf (buffer-mark-marker ,cb) ,mark
345 (buffer-point ,cb) ,point))))))
347 (defun insert-buffer-substring (buffer start end)
348 "Insert before point a substring of the contents of buffer.
349 buffer may be a buffer or a buffer name.
350 Arguments start and end are character positions specifying the substring.
351 They default to the values of (point-min) and (point-max) in buffer."
352 (let* ((buf (get-buffer buffer))
353 (s (buffer-substring start end)))
354 (with-current-buffer buf
355 (insert s))))
357 (defun preceding-char ()
358 "Return the character preceding point.
359 At the beginning of the buffer or accessible region, return #\Nul."
360 (or (char-before (point))
361 #\Nul))
363 (defun following-char ()
364 "Return the character following point, as a number.
365 At the end of the buffer or accessible region, return #\Nul."
366 (if (>= (point) (zv))
367 #\Nul ; XXX return nil?
368 (buffer-fetch-char (buffer-char-to-aref (current-buffer) (point))
369 (current-buffer))))
371 (defun bolp ()
372 "Return t if point is at the beginning of a line."
373 (or (= (point) (point-min))
374 (char= (char-before (point)) #\Newline)))
376 (defun eolp ()
377 "Return t if point is at the end of a line.
378 `End of a line' includes point being at the end of the buffer."
379 (or (= (point) (point-max))
380 (char= (char-after (point)) #\Newline)))
382 (defun delete-and-extract-region (start end)
383 "Delete the text between start and end and return it."
384 (multiple-value-setq (start end) (validate-region start end))
385 (if (= start end)
387 (prog1
388 (make-buffer-string start end t)
389 (delete-region start end))))
391 (defun insert-char (character count &optional inherit)
392 "Insert COUNT copies of CHARACTER.
393 Point, and before-insertion markers, are relocated as in the function `insert'.
394 **The optional third arg INHERIT, if non-nil, says to inherit text properties
395 **from adjoining text, if those properties are sticky."
396 (declare (ignore inherit))
397 (check-type character character)
398 (check-type count number)
399 (unless (< count 0)
400 (dotimes (i count)
401 (insert character))))
403 (defun line-beginning-position (n)
404 "Return the character position of the first character on the current line.
405 With argument N not nil or 1, move forward N - 1 lines first.
406 If scan reaches end of buffer, return that position.
408 This function constrains the returned position to the current field
409 unless that would be on a different line than the original,
410 unconstrained result. If N is nil or 1, and a front-sticky field
411 starts at point, the scan stops as soon as it starts. To ignore field
412 boundaries bind `inhibit-field-text-motion' to t.
414 This function does not move point."
415 ;; FIXME: inhibit-point-motion-hooks
416 (let ((pt (save-excursion
417 (forward-line (if n (1- n) 0))
418 (point))))
419 (constrain-to-field pt (point) (not (eql n 1)) t nil)))
421 (provide :lice-0.1/editfns)