+/* Find the field surrounding POS in *BEG and *END. If POS is nil,
+ the value of point is used instead. If BEG or END null,
+ means don't store the beginning or end of the field.
+
+ BEG_LIMIT and END_LIMIT serve to limit the ranged of the returned
+ results; they do not effect boundary behavior.
+
+ If MERGE_AT_BOUNDARY is nonzero, then if POS is at the very first
+ position of a field, then the beginning of the previous field is
+ returned instead of the beginning of POS's field (since the end of a
+ field is actually also the beginning of the next input field, this
+ behavior is sometimes useful). Additionally in the MERGE_AT_BOUNDARY
+ true case, if two fields are separated by a field with the special
+ value `boundary', and POS lies within it, then the two separated
+ fields are considered to be adjacent, and POS between them, when
+ finding the beginning and ending of the "merged" field.
+
+ Either BEG or END may be 0, in which case the corresponding value
+ is not stored. */
+
+static void
+find_field (pos, merge_at_boundary, beg_limit, beg, end_limit, end)
+ Lisp_Object pos;
+ Lisp_Object merge_at_boundary;
+ Lisp_Object beg_limit, end_limit;
+ int *beg, *end;
+{
+ /* Fields right before and after the point. */
+ Lisp_Object before_field, after_field;
+ /* If the fields came from overlays, the associated overlays.
+ Qnil means they came from text-properties. */
+ Lisp_Object before_overlay = Qnil, after_overlay = Qnil;
+ /* 1 if POS counts as the start of a field. */
+ int at_field_start = 0;
+ /* 1 if POS counts as the end of a field. */
+ int at_field_end = 0;
+
+ if (NILP (pos))
+ XSETFASTINT (pos, PT);
+ else
+ CHECK_NUMBER_COERCE_MARKER (pos);
+
+ after_field
+ = get_char_property_and_overlay (pos, Qfield, Qnil, &after_overlay);
+ before_field
+ = (XFASTINT (pos) > BEGV
+ ? get_char_property_and_overlay (make_number (XINT (pos) - 1),
+ Qfield, Qnil,
+ &before_overlay)
+ : Qnil);
+
+ /* See if we need to handle the case where MERGE_AT_BOUNDARY is nil
+ and POS is at beginning of a field, which can also be interpreted
+ as the end of the previous field. Note that the case where if
+ MERGE_AT_BOUNDARY is non-nil (see function comment) is actually the
+ more natural one; then we avoid treating the beginning of a field
+ specially. */
+ if (NILP (merge_at_boundary) && !EQ (after_field, before_field))
+ /* We are at a boundary, see which direction is inclusive. We
+ decide by seeing which field the `field' property sticks to. */
+ {
+ /* -1 means insertions go into before_field, 1 means they go
+ into after_field, 0 means neither. */
+ int stickiness;
+ /* Whether the before/after_field come from overlays. */
+ int bop = !NILP (before_overlay);
+ int aop = !NILP (after_overlay);
+
+ if (bop && XMARKER (OVERLAY_END (before_overlay))->insertion_type == 1)
+ /* before_field is from an overlay, which expands upon
+ end-insertions. Note that it's possible for after_overlay to
+ also eat insertions here, but then they will overlap, and
+ there's not much we can do. */
+ stickiness = -1;
+ else if (aop
+ && XMARKER (OVERLAY_START (after_overlay))->insertion_type == 0)
+ /* after_field is from an overlay, which expand to contain
+ start-insertions. */
+ stickiness = 1;
+ else if (bop && aop)
+ /* Both fields come from overlays, but neither will contain any
+ insertion here. */
+ stickiness = 0;
+ else if (bop)
+ /* before_field is an overlay that won't eat any insertion, but
+ after_field is from a text-property. Assume that the
+ text-property continues underneath the overlay, and so will
+ be inherited by any insertion, regardless of any stickiness
+ settings. */
+ stickiness = 1;
+ else if (aop)
+ /* Similarly, when after_field is the overlay. */
+ stickiness = -1;
+ else
+ /* Both fields come from text-properties. Look for explicit
+ stickiness properties. */
+ stickiness = text_property_stickiness (Qfield, pos);
+
+ if (stickiness > 0)
+ at_field_start = 1;
+ else if (stickiness < 0)
+ at_field_end = 1;
+ else
+ /* STICKINESS == 0 means that any inserted text will get a
+ `field' char-property of nil, so check to see if that
+ matches either of the adjacent characters (this being a
+ kind of "stickiness by default"). */
+ {
+ if (NILP (before_field))
+ at_field_end = 1; /* Sticks to the left. */
+ else if (NILP (after_field))
+ at_field_start = 1; /* Sticks to the right. */
+ }
+ }
+
+ /* Note about special `boundary' fields:
+
+ Consider the case where the point (`.') is between the fields `x' and `y':
+
+ xxxx.yyyy
+
+ In this situation, if merge_at_boundary is true, we consider the
+ `x' and `y' fields as forming one big merged field, and so the end
+ of the field is the end of `y'.
+
+ However, if `x' and `y' are separated by a special `boundary' field
+ (a field with a `field' char-property of 'boundary), then we ignore
+ this special field when merging adjacent fields. Here's the same
+ situation, but with a `boundary' field between the `x' and `y' fields:
+
+ xxx.BBBByyyy
+
+ Here, if point is at the end of `x', the beginning of `y', or
+ anywhere in-between (within the `boundary' field), we merge all
+ three fields and consider the beginning as being the beginning of
+ the `x' field, and the end as being the end of the `y' field. */
+
+ if (beg)
+ {
+ if (at_field_start)
+ /* POS is at the edge of a field, and we should consider it as
+ the beginning of the following field. */
+ *beg = XFASTINT (pos);
+ else
+ /* Find the previous field boundary. */
+ {
+ if (!NILP (merge_at_boundary) && EQ (before_field, Qboundary))
+ /* Skip a `boundary' field. */
+ pos = Fprevious_single_char_property_change (pos, Qfield, Qnil,
+ beg_limit);
+
+ pos = Fprevious_single_char_property_change (pos, Qfield, Qnil,
+ beg_limit);
+ *beg = NILP (pos) ? BEGV : XFASTINT (pos);
+ }
+ }
+
+ if (end)
+ {
+ if (at_field_end)
+ /* POS is at the edge of a field, and we should consider it as
+ the end of the previous field. */
+ *end = XFASTINT (pos);
+ else
+ /* Find the next field boundary. */
+ {
+ if (!NILP (merge_at_boundary) && EQ (after_field, Qboundary))
+ /* Skip a `boundary' field. */
+ pos = Fnext_single_char_property_change (pos, Qfield, Qnil,
+ end_limit);
+
+ pos = Fnext_single_char_property_change (pos, Qfield, Qnil,
+ end_limit);
+ *end = NILP (pos) ? ZV : XFASTINT (pos);
+ }
+ }
+}
+
+\f
+DEFUN ("delete-field", Fdelete_field, Sdelete_field, 0, 1, 0,
+ doc: /* Delete the field surrounding POS.
+A field is a region of text with the same `field' property.
+If POS is nil, the value of point is used for POS. */)
+ (pos)
+ Lisp_Object pos;
+{
+ int beg, end;
+ find_field (pos, Qnil, Qnil, &beg, Qnil, &end);
+ if (beg != end)
+ del_range (beg, end);
+ return Qnil;
+}
+
+DEFUN ("field-string", Ffield_string, Sfield_string, 0, 1, 0,
+ doc: /* Return the contents of the field surrounding POS as a string.
+A field is a region of text with the same `field' property.
+If POS is nil, the value of point is used for POS. */)
+ (pos)
+ Lisp_Object pos;
+{
+ int beg, end;
+ find_field (pos, Qnil, Qnil, &beg, Qnil, &end);
+ return make_buffer_string (beg, end, 1);
+}
+
+DEFUN ("field-string-no-properties", Ffield_string_no_properties, Sfield_string_no_properties, 0, 1, 0,
+ doc: /* Return the contents of the field around POS, without text-properties.
+A field is a region of text with the same `field' property.
+If POS is nil, the value of point is used for POS. */)
+ (pos)
+ Lisp_Object pos;
+{
+ int beg, end;
+ find_field (pos, Qnil, Qnil, &beg, Qnil, &end);
+ return make_buffer_string (beg, end, 0);
+}
+
+DEFUN ("field-beginning", Ffield_beginning, Sfield_beginning, 0, 3, 0,
+ doc: /* Return the beginning of the field surrounding POS.
+A field is a region of text with the same `field' property.
+If POS is nil, the value of point is used for POS.
+If ESCAPE-FROM-EDGE is non-nil and POS is at the beginning of its
+field, then the beginning of the *previous* field is returned.
+If LIMIT is non-nil, it is a buffer position; if the beginning of the field
+is before LIMIT, then LIMIT will be returned instead. */)
+ (pos, escape_from_edge, limit)
+ Lisp_Object pos, escape_from_edge, limit;
+{
+ int beg;
+ find_field (pos, escape_from_edge, limit, &beg, Qnil, 0);
+ return make_number (beg);
+}
+
+DEFUN ("field-end", Ffield_end, Sfield_end, 0, 3, 0,
+ doc: /* Return the end of the field surrounding POS.
+A field is a region of text with the same `field' property.
+If POS is nil, the value of point is used for POS.
+If ESCAPE-FROM-EDGE is non-nil and POS is at the end of its field,
+then the end of the *following* field is returned.
+If LIMIT is non-nil, it is a buffer position; if the end of the field
+is after LIMIT, then LIMIT will be returned instead. */)
+ (pos, escape_from_edge, limit)
+ Lisp_Object pos, escape_from_edge, limit;
+{
+ int end;
+ find_field (pos, escape_from_edge, Qnil, 0, limit, &end);
+ return make_number (end);
+}
+
+DEFUN ("constrain-to-field", Fconstrain_to_field, Sconstrain_to_field, 2, 5, 0,
+ doc: /* Return the position closest to NEW-POS that is in the same field as OLD-POS.
+
+A field is a region of text with the same `field' property.
+If NEW-POS is nil, then the current point is used instead, and set to the
+constrained position if that is different.
+
+If OLD-POS is at the boundary of two fields, then the allowable
+positions for NEW-POS depends on the value of the optional argument
+ESCAPE-FROM-EDGE: If ESCAPE-FROM-EDGE is nil, then NEW-POS is
+constrained to the field that has the same `field' char-property
+as any new characters inserted at OLD-POS, whereas if ESCAPE-FROM-EDGE
+is non-nil, NEW-POS is constrained to the union of the two adjacent
+fields. Additionally, if two fields are separated by another field with
+the special value `boundary', then any point within this special field is
+also considered to be `on the boundary'.
+
+If the optional argument ONLY-IN-LINE is non-nil and constraining
+NEW-POS would move it to a different line, NEW-POS is returned
+unconstrained. This useful for commands that move by line, like
+\\[next-line] or \\[beginning-of-line], which should generally respect field boundaries
+only in the case where they can still move to the right line.
+
+If the optional argument INHIBIT-CAPTURE-PROPERTY is non-nil, and OLD-POS has
+a non-nil property of that name, then any field boundaries are ignored.
+
+Field boundaries are not noticed if `inhibit-field-text-motion' is non-nil. */)
+ (new_pos, old_pos, escape_from_edge, only_in_line, inhibit_capture_property)
+ Lisp_Object new_pos, old_pos;
+ Lisp_Object escape_from_edge, only_in_line, inhibit_capture_property;
+{
+ /* If non-zero, then the original point, before re-positioning. */
+ int orig_point = 0;
+
+ if (NILP (new_pos))
+ /* Use the current point, and afterwards, set it. */
+ {
+ orig_point = PT;
+ XSETFASTINT (new_pos, PT);
+ }
+
+ if (NILP (Vinhibit_field_text_motion)
+ && !EQ (new_pos, old_pos)
+ && (!NILP (Fget_char_property (new_pos, Qfield, Qnil))
+ || !NILP (Fget_char_property (old_pos, Qfield, Qnil)))
+ && (NILP (inhibit_capture_property)
+ || NILP (Fget_char_property(old_pos, inhibit_capture_property, Qnil))))
+ /* NEW_POS is not within the same field as OLD_POS; try to
+ move NEW_POS so that it is. */
+ {
+ int fwd, shortage;
+ Lisp_Object field_bound;
+
+ CHECK_NUMBER_COERCE_MARKER (new_pos);
+ CHECK_NUMBER_COERCE_MARKER (old_pos);
+
+ fwd = (XFASTINT (new_pos) > XFASTINT (old_pos));
+
+ if (fwd)
+ field_bound = Ffield_end (old_pos, escape_from_edge, new_pos);
+ else
+ field_bound = Ffield_beginning (old_pos, escape_from_edge, new_pos);
+
+ if (/* See if ESCAPE_FROM_EDGE caused FIELD_BOUND to jump to the
+ other side of NEW_POS, which would mean that NEW_POS is
+ already acceptable, and it's not necessary to constrain it
+ to FIELD_BOUND. */
+ ((XFASTINT (field_bound) < XFASTINT (new_pos)) ? fwd : !fwd)
+ /* NEW_POS should be constrained, but only if either
+ ONLY_IN_LINE is nil (in which case any constraint is OK),
+ or NEW_POS and FIELD_BOUND are on the same line (in which
+ case the constraint is OK even if ONLY_IN_LINE is non-nil). */
+ && (NILP (only_in_line)
+ /* This is the ONLY_IN_LINE case, check that NEW_POS and
+ FIELD_BOUND are on the same line by seeing whether
+ there's an intervening newline or not. */
+ || (scan_buffer ('\n',
+ XFASTINT (new_pos), XFASTINT (field_bound),
+ fwd ? -1 : 1, &shortage, 1),
+ shortage != 0)))
+ /* Constrain NEW_POS to FIELD_BOUND. */
+ new_pos = field_bound;
+
+ if (orig_point && XFASTINT (new_pos) != orig_point)
+ /* The NEW_POS argument was originally nil, so automatically set PT. */
+ SET_PT (XFASTINT (new_pos));
+ }
+
+ return new_pos;
+}
+
+\f
+DEFUN ("line-beginning-position",
+ Fline_beginning_position, Sline_beginning_position, 0, 1, 0,
+ doc: /* Return the character position of the first character on the current line.
+With argument N not nil or 1, move forward N - 1 lines first.
+If scan reaches end of buffer, return that position.
+
+The scan does not cross a field boundary unless doing so would move
+beyond there to a different line; if N is nil or 1, and scan starts at a
+field boundary, the scan stops as soon as it starts. To ignore field
+boundaries bind `inhibit-field-text-motion' to t.
+
+This function does not move point. */)
+ (n)