+/* Return nonzero if POS1 and POS2 have the same value
+ for the text property PROP. */
+
+static int
+char_property_eq (prop, pos1, pos2)
+ Lisp_Object prop;
+ Lisp_Object pos1, pos2;
+{
+ Lisp_Object pval1, pval2;
+
+ pval1 = Fget_char_property (pos1, prop, Qnil);
+ pval2 = Fget_char_property (pos2, prop, Qnil);
+
+ return EQ (pval1, pval2);
+}
+
+/* Return the direction from which the char-property PROP would be
+ inherited by any new text inserted at POS: 1 if it would be
+ inherited from the char after POS, -1 if it would be inherited from
+ the char before POS, and 0 if from neither. */
+
+static int
+char_property_stickiness (prop, pos)
+ Lisp_Object prop;
+ Lisp_Object pos;
+{
+ Lisp_Object front_sticky;
+
+ if (XINT (pos) > BEGV)
+ /* Consider previous character. */
+ {
+ Lisp_Object prev_pos, rear_non_sticky;
+
+ prev_pos = make_number (XINT (pos) - 1);
+ rear_non_sticky = Fget_char_property (prev_pos, Qrear_nonsticky, Qnil);
+
+ if (EQ (rear_non_sticky, Qnil)
+ || (CONSP (rear_non_sticky)
+ && NILP (Fmemq (prop, rear_non_sticky))))
+ /* PROP is not rear-non-sticky, and since this takes precedence over
+ any front-stickiness, PROP is inherited from before. */
+ return -1;
+ }
+
+ /* Consider following character. */
+ front_sticky = Fget_char_property (pos, Qfront_sticky, Qnil);
+
+ if (EQ (front_sticky, Qt)
+ || (CONSP (front_sticky)
+ && !NILP (Fmemq (prop, front_sticky))))
+ /* PROP is inherited from after. */
+ return 1;
+
+ /* PROP is not inherited from either side. */
+ return 0;
+}
+\f
+/* Symbol for the text property used to mark fields. */
+Lisp_Object Qfield;
+
+/* A special value for Qfield properties. */
+Lisp_Object Qboundary;
+
+/* Find the field surrounding POS in *BEG and *END. If POS is nil,
+ the value of point is used instead.
+
+ 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. */
+
+void
+find_field (pos, merge_at_boundary, beg, end)
+ Lisp_Object pos;
+ Lisp_Object merge_at_boundary;
+ int *beg, *end;
+{
+ /* Fields right before and after the point. */
+ Lisp_Object before_field, after_field;
+ /* 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, 0);
+
+ after_field =
+ Fget_char_property (pos, Qfield, Qnil);
+ before_field =
+ (XFASTINT (pos) > BEGV
+ ? Fget_char_property (make_number (XINT (pos) - 1), Qfield, Qnil)
+ : 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. */
+ {
+ int stickiness = char_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,Qnil);
+
+ pos = Fprevious_single_char_property_change (pos, Qfield, Qnil, Qnil);
+ *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, Qnil);
+
+ pos = Fnext_single_char_property_change (pos, Qfield, Qnil, Qnil);
+ *end = NILP (pos) ? ZV : XFASTINT (pos);
+ }
+}
+\f
+DEFUN ("delete-field", Fdelete_field, Sdelete_field, 0, 1, 0,
+ "Delete the field surrounding POS.\n\
+A field is a region of text with the same `field' property.\n\
+If POS is nil, the value of point is used for POS.")
+ (pos)
+ Lisp_Object pos;
+{
+ int beg, end;
+ find_field (pos, Qnil, &beg, &end);
+ if (beg != end)
+ del_range (beg, end);
+ return Qnil;
+}
+
+DEFUN ("field-string", Ffield_string, Sfield_string, 0, 1, 0,
+ "Return the contents of the field surrounding POS as a string.\n\
+A field is a region of text with the same `field' property.\n\
+If POS is nil, the value of point is used for POS.")
+ (pos)
+ Lisp_Object pos;
+{
+ int beg, end;
+ find_field (pos, Qnil, &beg, &end);
+ return make_buffer_string (beg, end, 1);
+}
+
+DEFUN ("field-string-no-properties", Ffield_string_no_properties, Sfield_string_no_properties, 0, 1, 0,
+ "Return the contents of the field around POS, without text-properties.\n\
+A field is a region of text with the same `field' property.\n\
+If POS is nil, the value of point is used for POS.")
+ (pos)
+ Lisp_Object pos;
+{
+ int beg, end;
+ find_field (pos, Qnil, &beg, &end);
+ return make_buffer_string (beg, end, 0);
+}
+
+DEFUN ("field-beginning", Ffield_beginning, Sfield_beginning, 0, 2, 0,
+ "Return the beginning of the field surrounding POS.\n\
+A field is a region of text with the same `field' property.\n\
+If POS is nil, the value of point is used for POS.\n\
+If ESCAPE-FROM-EDGE is non-nil and POS is at the beginning of its\n\
+field, then the beginning of the *previous* field is returned.")
+ (pos, escape_from_edge)
+ Lisp_Object pos, escape_from_edge;
+{
+ int beg;
+ find_field (pos, escape_from_edge, &beg, 0);
+ return make_number (beg);
+}
+
+DEFUN ("field-end", Ffield_end, Sfield_end, 0, 2, 0,
+ "Return the end of the field surrounding POS.\n\
+A field is a region of text with the same `field' property.\n\
+If POS is nil, the value of point is used for POS.\n\
+If ESCAPE-FROM-EDGE is non-nil and POS is at the end of its field,\n\
+then the end of the *following* field is returned.")
+ (pos, escape_from_edge)
+ Lisp_Object pos, escape_from_edge;
+{
+ int end;
+ find_field (pos, escape_from_edge, 0, &end);
+ return make_number (end);
+}
+
+DEFUN ("constrain-to-field", Fconstrain_to_field, Sconstrain_to_field, 2, 5, 0,
+ "Return the position closest to NEW-POS that is in the same field as OLD-POS.\n\
+\n\
+A field is a region of text with the same `field' property.\n\
+If NEW-POS is nil, then the current point is used instead, and set to the\n\
+constrained position if that is is different.\n\
+\n\
+If OLD-POS is at the boundary of two fields, then the allowable\n\
+positions for NEW-POS depends on the value of the optional argument\n\
+ESCAPE-FROM-EDGE: If ESCAPE-FROM-EDGE is nil, then NEW-POS is\n\
+constrained to the field that has the same `field' char-property\n\
+as any new characters inserted at OLD-POS, whereas if ESCAPE-FROM-EDGE\n\
+is non-nil, NEW-POS is constrained to the union of the two adjacent\n\
+fields. Additionally, if two fields are separated by another field with\n\
+the special value `boundary', then any point within this special field is\n\
+also considered to be `on the boundary'.\n\
+\n\
+If the optional argument ONLY-IN-LINE is non-nil and constraining\n\
+NEW-POS would move it to a different line, NEW-POS is returned\n\
+unconstrained. This useful for commands that move by line, like\n\
+\\[next-line] or \\[beginning-of-line], which should generally respect field boundaries\n\
+only in the case where they can still move to the right line.\n\
+\n\
+If the optional argument INHIBIT-CAPTURE-PROPERTY is non-nil, and OLD-POS has\n\
+a non-nil property of that name, then any field boundaries are ignored.\n\
+\n\
+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)
+ && !char_property_eq (Qfield, new_pos, old_pos)
+ && (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, 0);
+ CHECK_NUMBER_COERCE_MARKER (old_pos, 0);
+
+ fwd = (XFASTINT (new_pos) > XFASTINT (old_pos));
+
+ if (fwd)
+ field_bound = Ffield_end (old_pos, escape_from_edge);
+ else
+ field_bound = Ffield_beginning (old_pos, escape_from_edge);
+
+ 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