aboutsummaryrefslogtreecommitdiffstats
path: root/src/editfns.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/editfns.c')
-rw-r--r--src/editfns.c46
1 files changed, 36 insertions, 10 deletions
diff --git a/src/editfns.c b/src/editfns.c
index 2f8b075817a..e326604467c 100644
--- a/src/editfns.c
+++ b/src/editfns.c
@@ -74,7 +74,6 @@ static Lisp_Object format_time_string (char const *, ptrdiff_t, struct timespec,
74static long int tm_gmtoff (struct tm *); 74static long int tm_gmtoff (struct tm *);
75static int tm_diff (struct tm *, struct tm *); 75static int tm_diff (struct tm *, struct tm *);
76static void update_buffer_properties (ptrdiff_t, ptrdiff_t); 76static void update_buffer_properties (ptrdiff_t, ptrdiff_t);
77static Lisp_Object styled_format (ptrdiff_t, Lisp_Object *, bool);
78 77
79#ifndef HAVE_TM_GMTOFF 78#ifndef HAVE_TM_GMTOFF
80# define HAVE_TM_GMTOFF false 79# define HAVE_TM_GMTOFF false
@@ -3959,7 +3958,7 @@ usage: (message FORMAT-STRING &rest ARGS) */)
3959 } 3958 }
3960 else 3959 else
3961 { 3960 {
3962 Lisp_Object val = Fformat_message (nargs, args); 3961 Lisp_Object val = styled_format (nargs, args, true, false);
3963 message3 (val); 3962 message3 (val);
3964 return val; 3963 return val;
3965 } 3964 }
@@ -3985,7 +3984,7 @@ usage: (message-box FORMAT-STRING &rest ARGS) */)
3985 } 3984 }
3986 else 3985 else
3987 { 3986 {
3988 Lisp_Object val = Fformat_message (nargs, args); 3987 Lisp_Object val = styled_format (nargs, args, true, false);
3989 Lisp_Object pane, menu; 3988 Lisp_Object pane, menu;
3990 3989
3991 pane = list1 (Fcons (build_string ("OK"), Qt)); 3990 pane = list1 (Fcons (build_string ("OK"), Qt));
@@ -4141,7 +4140,7 @@ produced text.
4141usage: (format STRING &rest OBJECTS) */) 4140usage: (format STRING &rest OBJECTS) */)
4142 (ptrdiff_t nargs, Lisp_Object *args) 4141 (ptrdiff_t nargs, Lisp_Object *args)
4143{ 4142{
4144 return styled_format (nargs, args, false); 4143 return styled_format (nargs, args, false, true);
4145} 4144}
4146 4145
4147DEFUN ("format-message", Fformat_message, Sformat_message, 1, MANY, 0, 4146DEFUN ("format-message", Fformat_message, Sformat_message, 1, MANY, 0,
@@ -4157,13 +4156,16 @@ and right quote replacement characters are specified by
4157usage: (format-message STRING &rest OBJECTS) */) 4156usage: (format-message STRING &rest OBJECTS) */)
4158 (ptrdiff_t nargs, Lisp_Object *args) 4157 (ptrdiff_t nargs, Lisp_Object *args)
4159{ 4158{
4160 return styled_format (nargs, args, true); 4159 return styled_format (nargs, args, true, true);
4161} 4160}
4162 4161
4163/* Implement ‘format-message’ if MESSAGE is true, ‘format’ otherwise. */ 4162/* Implement ‘format-message’ if MESSAGE is true, ‘format’ otherwise.
4163 If NEW_RESULT, the result is a new string; otherwise, the result
4164 may be one of the arguments. */
4164 4165
4165static Lisp_Object 4166Lisp_Object
4166styled_format (ptrdiff_t nargs, Lisp_Object *args, bool message) 4167styled_format (ptrdiff_t nargs, Lisp_Object *args, bool message,
4168 bool new_result)
4167{ 4169{
4168 ptrdiff_t n; /* The number of the next arg to substitute. */ 4170 ptrdiff_t n; /* The number of the next arg to substitute. */
4169 char initial_buffer[4000]; 4171 char initial_buffer[4000];
@@ -4193,6 +4195,9 @@ styled_format (ptrdiff_t nargs, Lisp_Object *args, bool message)
4193 /* The start and end bytepos in the output string. */ 4195 /* The start and end bytepos in the output string. */
4194 ptrdiff_t start, end; 4196 ptrdiff_t start, end;
4195 4197
4198 /* Whether the argument is a newly created string. */
4199 bool_bf new_string : 1;
4200
4196 /* Whether the argument is a string with intervals. */ 4201 /* Whether the argument is a string with intervals. */
4197 bool_bf intervals : 1; 4202 bool_bf intervals : 1;
4198 } *info; 4203 } *info;
@@ -4342,7 +4347,10 @@ styled_format (ptrdiff_t nargs, Lisp_Object *args, bool message)
4342 memset (&discarded[format0 - format_start], 1, 4347 memset (&discarded[format0 - format_start], 1,
4343 format - format0 - (conversion == '%')); 4348 format - format0 - (conversion == '%'));
4344 if (conversion == '%') 4349 if (conversion == '%')
4345 goto copy_char; 4350 {
4351 new_result = true;
4352 goto copy_char;
4353 }
4346 4354
4347 ++n; 4355 ++n;
4348 if (! (n < nargs)) 4356 if (! (n < nargs))
@@ -4352,6 +4360,7 @@ styled_format (ptrdiff_t nargs, Lisp_Object *args, bool message)
4352 if (nspec < ispec) 4360 if (nspec < ispec)
4353 { 4361 {
4354 spec->argument = args[n]; 4362 spec->argument = args[n];
4363 spec->new_string = false;
4355 spec->intervals = false; 4364 spec->intervals = false;
4356 nspec = ispec; 4365 nspec = ispec;
4357 } 4366 }
@@ -4369,6 +4378,7 @@ styled_format (ptrdiff_t nargs, Lisp_Object *args, bool message)
4369 { 4378 {
4370 Lisp_Object noescape = conversion == 'S' ? Qnil : Qt; 4379 Lisp_Object noescape = conversion == 'S' ? Qnil : Qt;
4371 spec->argument = arg = Fprin1_to_string (arg, noescape); 4380 spec->argument = arg = Fprin1_to_string (arg, noescape);
4381 spec->new_string = true;
4372 if (STRING_MULTIBYTE (arg) && ! multibyte) 4382 if (STRING_MULTIBYTE (arg) && ! multibyte)
4373 { 4383 {
4374 multibyte = true; 4384 multibyte = true;
@@ -4387,6 +4397,7 @@ styled_format (ptrdiff_t nargs, Lisp_Object *args, bool message)
4387 goto retry; 4397 goto retry;
4388 } 4398 }
4389 spec->argument = arg = Fchar_to_string (arg); 4399 spec->argument = arg = Fchar_to_string (arg);
4400 spec->new_string = true;
4390 } 4401 }
4391 4402
4392 if (!EQ (arg, args[n])) 4403 if (!EQ (arg, args[n]))
@@ -4409,6 +4420,11 @@ styled_format (ptrdiff_t nargs, Lisp_Object *args, bool message)
4409 4420
4410 if (conversion == 's') 4421 if (conversion == 's')
4411 { 4422 {
4423 if (format == end && format - format_start == 2
4424 && (!new_result || spec->new_string)
4425 && ! string_intervals (args[0]))
4426 return arg;
4427
4412 /* handle case (precision[n] >= 0) */ 4428 /* handle case (precision[n] >= 0) */
4413 4429
4414 ptrdiff_t prec = -1; 4430 ptrdiff_t prec = -1;
@@ -4487,6 +4503,7 @@ styled_format (ptrdiff_t nargs, Lisp_Object *args, bool message)
4487 if (string_intervals (arg)) 4503 if (string_intervals (arg))
4488 spec->intervals = arg_intervals = true; 4504 spec->intervals = arg_intervals = true;
4489 4505
4506 new_result = true;
4490 continue; 4507 continue;
4491 } 4508 }
4492 } 4509 }
@@ -4754,6 +4771,7 @@ styled_format (ptrdiff_t nargs, Lisp_Object *args, bool message)
4754 } 4771 }
4755 spec->end = nchars; 4772 spec->end = nchars;
4756 4773
4774 new_result = true;
4757 continue; 4775 continue;
4758 } 4776 }
4759 } 4777 }
@@ -4772,9 +4790,13 @@ styled_format (ptrdiff_t nargs, Lisp_Object *args, bool message)
4772 } 4790 }
4773 convsrc = format_char == '`' ? uLSQM : uRSQM; 4791 convsrc = format_char == '`' ? uLSQM : uRSQM;
4774 convbytes = 3; 4792 convbytes = 3;
4793 new_result = true;
4775 } 4794 }
4776 else if (format_char == '`' && quoting_style == STRAIGHT_QUOTING_STYLE) 4795 else if (format_char == '`' && quoting_style == STRAIGHT_QUOTING_STYLE)
4777 convsrc = "'"; 4796 {
4797 convsrc = "'";
4798 new_result = true;
4799 }
4778 else 4800 else
4779 { 4801 {
4780 /* Copy a single character from format to buf. */ 4802 /* Copy a single character from format to buf. */
@@ -4798,6 +4820,7 @@ styled_format (ptrdiff_t nargs, Lisp_Object *args, bool message)
4798 int c = BYTE8_TO_CHAR (format_char); 4820 int c = BYTE8_TO_CHAR (format_char);
4799 convbytes = CHAR_STRING (c, str); 4821 convbytes = CHAR_STRING (c, str);
4800 convsrc = (char *) str; 4822 convsrc = (char *) str;
4823 new_result = true;
4801 } 4824 }
4802 } 4825 }
4803 4826
@@ -4844,6 +4867,9 @@ styled_format (ptrdiff_t nargs, Lisp_Object *args, bool message)
4844 if (bufsize < p - buf) 4867 if (bufsize < p - buf)
4845 emacs_abort (); 4868 emacs_abort ();
4846 4869
4870 if (! new_result)
4871 return args[0];
4872
4847 if (maybe_combine_byte) 4873 if (maybe_combine_byte)
4848 nchars = multibyte_chars_in_text ((unsigned char *) buf, p - buf); 4874 nchars = multibyte_chars_in_text ((unsigned char *) buf, p - buf);
4849 Lisp_Object val = make_specified_string (buf, nchars, p - buf, multibyte); 4875 Lisp_Object val = make_specified_string (buf, nchars, p - buf, multibyte);