summaryrefslogtreecommitdiff
path: root/src/casefiddle.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2019-09-22 10:43:21 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2019-09-22 10:45:14 -0700
commit2f600e97e7ca43965f55f019759582d93d8bca73 (patch)
treed885dcef77f04a60da6cec56a2750b19a8e64192 /src/casefiddle.c
parentdddff96a585531608d5e8d27375a6363679a9fb5 (diff)
downloademacs-2f600e97e7ca43965f55f019759582d93d8bca73.tar.gz
Avoid crashes when casifying noncontiguous regions
This is a followon fix for Bug#37477. * lisp/simple.el (region-extract-function): Use setq here, since the var is now defined in C code. * src/casefiddle.c (casify_pnc_region): New function. (Fupcase_region, Fdowncase_region, Fcapitalize_region) (Fupcase_initials_region): Use it. (Fupcase_initials_region): Add region-noncontiguous-p flag for consistency with the others. All uses changed. (syms_of_casefiddle): Define Qbounds, Vregion_extract_function. * src/insdel.c (prepare_to_modify_buffer_1): * src/keyboard.c (command_loop_1): Use Vregion_extraction_function. * src/insdel.c (syms_of_insdel): No need to define Qregion_extract_function. * test/src/casefiddle-tests.el (casefiddle-oldfunc): New var. (casefiddle-loopfunc, casefiddle-badfunc): New functions. (casefiddle-invalid-region-extract-function): New test.
Diffstat (limited to 'src/casefiddle.c')
-rw-r--r--src/casefiddle.c104
1 files changed, 49 insertions, 55 deletions
diff --git a/src/casefiddle.c b/src/casefiddle.c
index 3a1724b306d..774906df04d 100644
--- a/src/casefiddle.c
+++ b/src/casefiddle.c
@@ -516,34 +516,43 @@ casify_region (enum case_action flag, Lisp_Object b, Lisp_Object e)
return orig_end + added;
}
-DEFUN ("upcase-region", Fupcase_region, Supcase_region, 2, 3,
- "(list (region-beginning) (region-end) (region-noncontiguous-p))",
- doc: /* Convert the region to upper case. In programs, wants two arguments.
-These arguments specify the starting and ending character numbers of
-the region to operate on. When used as a command, the text between
-point and the mark is operated on.
-See also `capitalize-region'. */)
- (Lisp_Object beg, Lisp_Object end, Lisp_Object region_noncontiguous_p)
-{
- Lisp_Object bounds = Qnil;
+/* Casify a possibly noncontiguous region according to FLAG. BEG and
+ END specify the bounds, except that if REGION_NONCONTIGUOUS_P is
+ non-nil, the region's bounds are specified by (funcall
+ region-extract-function 'bounds) instead. */
+static Lisp_Object
+casify_pnc_region (enum case_action flag, Lisp_Object beg, Lisp_Object end,
+ Lisp_Object region_noncontiguous_p)
+{
if (!NILP (region_noncontiguous_p))
{
- bounds = call1 (Fsymbol_value (Qregion_extract_function),
- intern ("bounds"));
-
- while (CONSP (bounds))
+ Lisp_Object bounds = call1 (Vregion_extract_function, Qbounds);
+ FOR_EACH_TAIL (bounds)
{
- casify_region (CASE_UP, XCAR (XCAR (bounds)), XCDR (XCAR (bounds)));
- bounds = XCDR (bounds);
+ CHECK_CONS (XCAR (bounds));
+ casify_region (flag, XCAR (XCAR (bounds)), XCDR (XCAR (bounds)));
}
+ CHECK_LIST_END (bounds, bounds);
}
else
- casify_region (CASE_UP, beg, end);
+ casify_region (flag, beg, end);
return Qnil;
}
+DEFUN ("upcase-region", Fupcase_region, Supcase_region, 2, 3,
+ "(list (region-beginning) (region-end) (region-noncontiguous-p))",
+ doc: /* Convert the region to upper case. In programs, wants two arguments.
+These arguments specify the starting and ending character numbers of
+the region to operate on. When used as a command, the text between
+point and the mark is operated on.
+See also `capitalize-region'. */)
+ (Lisp_Object beg, Lisp_Object end, Lisp_Object region_noncontiguous_p)
+{
+ return casify_pnc_region (CASE_UP, beg, end, region_noncontiguous_p);
+}
+
DEFUN ("downcase-region", Fdowncase_region, Sdowncase_region, 2, 3,
"(list (region-beginning) (region-end) (region-noncontiguous-p))",
doc: /* Convert the region to lower case. In programs, wants two arguments.
@@ -552,23 +561,7 @@ the region to operate on. When used as a command, the text between
point and the mark is operated on. */)
(Lisp_Object beg, Lisp_Object end, Lisp_Object region_noncontiguous_p)
{
- Lisp_Object bounds = Qnil;
-
- if (!NILP (region_noncontiguous_p))
- {
- bounds = call1 (Fsymbol_value (Qregion_extract_function),
- intern ("bounds"));
-
- while (CONSP (bounds))
- {
- casify_region (CASE_DOWN, XCAR (XCAR (bounds)), XCDR (XCAR (bounds)));
- bounds = XCDR (bounds);
- }
- }
- else
- casify_region (CASE_DOWN, beg, end);
-
- return Qnil;
+ return casify_pnc_region (CASE_DOWN, beg, end, region_noncontiguous_p);
}
DEFUN ("capitalize-region", Fcapitalize_region, Scapitalize_region, 2, 3,
@@ -580,38 +573,23 @@ In programs, give two arguments, the starting and ending
character positions to operate on. */)
(Lisp_Object beg, Lisp_Object end, Lisp_Object region_noncontiguous_p)
{
- Lisp_Object bounds = Qnil;
-
- if (!NILP (region_noncontiguous_p))
- {
- bounds = call1 (Fsymbol_value (Qregion_extract_function),
- intern ("bounds"));
-
- while (CONSP (bounds))
- {
- casify_region (CASE_CAPITALIZE, XCAR (XCAR (bounds)), XCDR (XCAR (bounds)));
- bounds = XCDR (bounds);
- }
- }
- else
- casify_region (CASE_CAPITALIZE, beg, end);
-
- return Qnil;
+ return casify_pnc_region (CASE_CAPITALIZE, beg, end, region_noncontiguous_p);
}
/* Like Fcapitalize_region but change only the initials. */
DEFUN ("upcase-initials-region", Fupcase_initials_region,
- Supcase_initials_region, 2, 2, "r",
+ Supcase_initials_region, 2, 3,
+ "(list (region-beginning) (region-end) (region-noncontiguous-p))",
doc: /* Upcase the initial of each word in the region.
This means that each word's first character is converted to either
title case or upper case, and the rest are left unchanged.
In programs, give two arguments, the starting and ending
character positions to operate on. */)
- (Lisp_Object beg, Lisp_Object end)
+ (Lisp_Object beg, Lisp_Object end, Lisp_Object region_noncontiguous_p)
{
- casify_region (CASE_CAPITALIZE_UP, beg, end);
- return Qnil;
+ return casify_pnc_region (CASE_CAPITALIZE_UP, beg, end,
+ region_noncontiguous_p);
}
static Lisp_Object
@@ -668,12 +646,28 @@ With negative argument, capitalize previous words but do not move. */)
void
syms_of_casefiddle (void)
{
+ DEFSYM (Qbounds, "bounds");
DEFSYM (Qidentity, "identity");
DEFSYM (Qtitlecase, "titlecase");
DEFSYM (Qspecial_uppercase, "special-uppercase");
DEFSYM (Qspecial_lowercase, "special-lowercase");
DEFSYM (Qspecial_titlecase, "special-titlecase");
+ DEFVAR_LISP ("region-extract-function", Vregion_extract_function,
+ doc: /* Function to get the region's content.
+Called with one argument METHOD which can be:
+- nil: return the content as a string (list of strings for
+ non-contiguous regions).
+- `delete-only': delete the region; the return value is undefined.
+- `bounds': return the boundaries of the region as a list of one
+ or more cons cells of the form (START . END).
+- anything else: delete the region and return its content
+ as a string (or list of strings for non-contiguous regions),
+ after filtering it with `filter-buffer-substring', which
+ is called, for each contiguous sub-region, with METHOD as its
+ 3rd argument. */);
+ Vregion_extract_function = Qnil; /* simple.el sets this. */
+
defsubr (&Supcase);
defsubr (&Sdowncase);
defsubr (&Scapitalize);