Commit | Line | Data |
---|---|---|
35692fe0 | 1 | /* Lisp functions pertaining to editing. |
64c60c2f | 2 | |
acaf905b | 3 | Copyright (C) 1985-1987, 1989, 1993-2012 Free Software Foundation, Inc. |
35692fe0 JB |
4 | |
5 | This file is part of GNU Emacs. | |
6 | ||
9ec0b715 | 7 | GNU Emacs is free software: you can redistribute it and/or modify |
35692fe0 | 8 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. | |
35692fe0 JB |
11 | |
12 | GNU Emacs is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 18 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
35692fe0 JB |
19 | |
20 | ||
18160b98 | 21 | #include <config.h> |
68c45bf0 | 22 | #include <sys/types.h> |
3c14598c | 23 | #include <stdio.h> |
bfb61299 | 24 | |
5b9c0a1d | 25 | #ifdef HAVE_PWD_H |
35692fe0 | 26 | #include <pwd.h> |
bfb61299 JB |
27 | #endif |
28 | ||
dfcf069d | 29 | #include <unistd.h> |
dfcf069d | 30 | |
3bb9abc8 ST |
31 | #ifdef HAVE_SYS_UTSNAME_H |
32 | #include <sys/utsname.h> | |
33 | #endif | |
34 | ||
b17f9379 DN |
35 | #include "lisp.h" |
36 | ||
3c14598c EZ |
37 | /* systime.h includes <sys/time.h> which, on some systems, is required |
38 | for <sys/resource.h>; thus systime.h must be included before | |
39 | <sys/resource.h> */ | |
40 | #include "systime.h" | |
21acf124 ST |
41 | |
42 | #if defined HAVE_SYS_RESOURCE_H | |
4211ee7d | 43 | #include <sys/resource.h> |
e0f712ba AC |
44 | #endif |
45 | ||
37910ab2 | 46 | #include <float.h> |
b8d9bd41 PE |
47 | #include <limits.h> |
48 | #include <intprops.h> | |
16c3e636 | 49 | #include <strftime.h> |
37910ab2 | 50 | #include <verify.h> |
409847a1 | 51 | |
74d6d8c5 | 52 | #include "intervals.h" |
40fbd254 | 53 | #include "character.h" |
e5560ff7 | 54 | #include "buffer.h" |
68c45bf0 | 55 | #include "coding.h" |
0ae83348 | 56 | #include "frame.h" |
35692fe0 | 57 | #include "window.h" |
b91834c3 | 58 | #include "blockinput.h" |
35692fe0 | 59 | |
aac18aa4 PE |
60 | #define TM_YEAR_BASE 1900 |
61 | ||
c433c134 | 62 | #ifdef WINDOWSNT |
361358ea | 63 | extern Lisp_Object w32_get_internal_run_time (void); |
c433c134 JR |
64 | #endif |
65 | ||
d35af63c | 66 | static Lisp_Object format_time_string (char const *, ptrdiff_t, EMACS_TIME, |
a08d4ba7 | 67 | bool, struct tm *); |
f57e2426 | 68 | static int tm_diff (struct tm *, struct tm *); |
d311d28c | 69 | static void update_buffer_properties (ptrdiff_t, ptrdiff_t); |
260e2e2a | 70 | |
955cbe7b | 71 | static Lisp_Object Qbuffer_access_fontify_functions; |
e3ed8469 | 72 | |
acb7cc89 GM |
73 | /* Symbol for the text property used to mark fields. */ |
74 | ||
75 | Lisp_Object Qfield; | |
76 | ||
77 | /* A special value for Qfield properties. */ | |
78 | ||
955cbe7b | 79 | static Lisp_Object Qboundary; |
acb7cc89 | 80 | |
5745a7df PE |
81 | /* The startup value of the TZ environment variable so it can be |
82 | restored if the user calls set-time-zone-rule with a nil | |
83 | argument. If null, the TZ environment variable was unset. */ | |
84 | static char const *initial_tz; | |
85 | ||
86 | /* True if the static variable tzvalbuf (defined in | |
87 | set_time_zone_rule) is part of 'environ'. */ | |
88 | static bool tzvalbuf_in_environ; | |
89 | ||
acb7cc89 | 90 | |
35692fe0 | 91 | void |
971de7fb | 92 | init_editfns (void) |
35692fe0 | 93 | { |
63c5d10b | 94 | const char *user_name; |
e7f8264d | 95 | register char *p; |
35692fe0 | 96 | struct passwd *pw; /* password entry for the current user */ |
35692fe0 JB |
97 | Lisp_Object tem; |
98 | ||
99 | /* Set up system_name even when dumping. */ | |
ac988277 | 100 | init_system_name (); |
35692fe0 JB |
101 | |
102 | #ifndef CANNOT_DUMP | |
103 | /* Don't bother with this on initial start when just dumping out */ | |
104 | if (!initialized) | |
105 | return; | |
106 | #endif /* not CANNOT_DUMP */ | |
107 | ||
5745a7df PE |
108 | initial_tz = getenv ("TZ"); |
109 | tzvalbuf_in_environ = 0; | |
110 | ||
63c5d10b | 111 | pw = getpwuid (getuid ()); |
87485d6f MW |
112 | #ifdef MSDOS |
113 | /* We let the real user name default to "root" because that's quite | |
114 | accurate on MSDOG and because it lets Emacs find the init file. | |
115 | (The DVX libraries override the Djgpp libraries here.) */ | |
35b34f72 | 116 | Vuser_real_login_name = build_string (pw ? pw->pw_name : "root"); |
87485d6f | 117 | #else |
35b34f72 | 118 | Vuser_real_login_name = build_string (pw ? pw->pw_name : "unknown"); |
87485d6f | 119 | #endif |
35692fe0 | 120 | |
52b14ac0 JB |
121 | /* Get the effective user name, by consulting environment variables, |
122 | or the effective uid if those are unset. */ | |
63c5d10b | 123 | user_name = getenv ("LOGNAME"); |
35692fe0 | 124 | if (!user_name) |
4691c06d | 125 | #ifdef WINDOWSNT |
63c5d10b | 126 | user_name = getenv ("USERNAME"); /* it's USERNAME on NT */ |
4691c06d | 127 | #else /* WINDOWSNT */ |
63c5d10b | 128 | user_name = getenv ("USER"); |
4691c06d | 129 | #endif /* WINDOWSNT */ |
52b14ac0 JB |
130 | if (!user_name) |
131 | { | |
63c5d10b PE |
132 | pw = getpwuid (geteuid ()); |
133 | user_name = pw ? pw->pw_name : "unknown"; | |
52b14ac0 | 134 | } |
35b34f72 | 135 | Vuser_login_name = build_string (user_name); |
35692fe0 | 136 | |
52b14ac0 JB |
137 | /* If the user name claimed in the environment vars differs from |
138 | the real uid, use the claimed name to find the full name. */ | |
35b34f72 | 139 | tem = Fstring_equal (Vuser_login_name, Vuser_real_login_name); |
d311d28c PE |
140 | if (! NILP (tem)) |
141 | tem = Vuser_login_name; | |
142 | else | |
143 | { | |
144 | uid_t euid = geteuid (); | |
145 | tem = make_fixnum_or_float (euid); | |
146 | } | |
147 | Vuser_full_name = Fuser_full_name (tem); | |
34a7a267 | 148 | |
e7f8264d | 149 | p = getenv ("NAME"); |
9d36d071 RS |
150 | if (p) |
151 | Vuser_full_name = build_string (p); | |
3347526c RS |
152 | else if (NILP (Vuser_full_name)) |
153 | Vuser_full_name = build_string ("unknown"); | |
3bb9abc8 ST |
154 | |
155 | #ifdef HAVE_SYS_UTSNAME_H | |
156 | { | |
157 | struct utsname uts; | |
158 | uname (&uts); | |
159 | Voperating_system_release = build_string (uts.release); | |
160 | } | |
161 | #else | |
162 | Voperating_system_release = Qnil; | |
163 | #endif | |
35692fe0 JB |
164 | } |
165 | \f | |
a7ca3326 | 166 | DEFUN ("char-to-string", Fchar_to_string, Schar_to_string, 1, 1, 0, |
06283081 PJ |
167 | doc: /* Convert arg CHAR to a string containing that character. |
168 | usage: (char-to-string CHAR) */) | |
5842a27b | 169 | (Lisp_Object character) |
35692fe0 | 170 | { |
13bdea59 | 171 | int c, len; |
d5c2c403 | 172 | unsigned char str[MAX_MULTIBYTE_LENGTH]; |
fb8106e8 | 173 | |
1b9c91ed | 174 | CHECK_CHARACTER (character); |
13bdea59 | 175 | c = XFASTINT (character); |
35692fe0 | 176 | |
13bdea59 | 177 | len = CHAR_STRING (c, str); |
e7f8264d | 178 | return make_string_from_bytes ((char *) str, 1, len); |
35692fe0 JB |
179 | } |
180 | ||
c3bb441d | 181 | DEFUN ("byte-to-string", Fbyte_to_string, Sbyte_to_string, 1, 1, 0, |
35f1de62 | 182 | doc: /* Convert arg BYTE to a unibyte string containing that byte. */) |
5842a27b | 183 | (Lisp_Object byte) |
c3bb441d | 184 | { |
64c60c2f | 185 | unsigned char b; |
c3bb441d | 186 | CHECK_NUMBER (byte); |
35f1de62 CY |
187 | if (XINT (byte) < 0 || XINT (byte) > 255) |
188 | error ("Invalid byte"); | |
64c60c2f | 189 | b = XINT (byte); |
e7f8264d | 190 | return make_string_from_bytes ((char *) &b, 1, 1); |
c3bb441d SM |
191 | } |
192 | ||
35692fe0 | 193 | DEFUN ("string-to-char", Fstring_to_char, Sstring_to_char, 1, 1, 0, |
da4adb04 | 194 | doc: /* Return the first character in STRING. */) |
5842a27b | 195 | (register Lisp_Object string) |
35692fe0 JB |
196 | { |
197 | register Lisp_Object val; | |
b7826503 | 198 | CHECK_STRING (string); |
4e491f8d | 199 | if (SCHARS (string)) |
d9d851ea KH |
200 | { |
201 | if (STRING_MULTIBYTE (string)) | |
62a6e103 | 202 | XSETFASTINT (val, STRING_CHAR (SDATA (string))); |
d9d851ea | 203 | else |
4e491f8d | 204 | XSETFASTINT (val, SREF (string, 0)); |
d9d851ea | 205 | } |
35692fe0 | 206 | else |
55561c63 | 207 | XSETFASTINT (val, 0); |
35692fe0 JB |
208 | return val; |
209 | } | |
35692fe0 | 210 | |
a7ca3326 | 211 | DEFUN ("point", Fpoint, Spoint, 0, 0, 0, |
7ee72033 MB |
212 | doc: /* Return value of point, as an integer. |
213 | Beginning of buffer is position (point-min). */) | |
5842a27b | 214 | (void) |
35692fe0 JB |
215 | { |
216 | Lisp_Object temp; | |
6ec8bbd2 | 217 | XSETFASTINT (temp, PT); |
35692fe0 JB |
218 | return temp; |
219 | } | |
220 | ||
a7ca3326 | 221 | DEFUN ("point-marker", Fpoint_marker, Spoint_marker, 0, 0, 0, |
7ee72033 | 222 | doc: /* Return value of point, as a marker object. */) |
5842a27b | 223 | (void) |
35692fe0 | 224 | { |
657924ff | 225 | return build_marker (current_buffer, PT, PT_BYTE); |
35692fe0 JB |
226 | } |
227 | ||
a7ca3326 | 228 | DEFUN ("goto-char", Fgoto_char, Sgoto_char, 1, 1, "NGoto char: ", |
7ee72033 | 229 | doc: /* Set point to POSITION, a number or marker. |
8696b557 EZ |
230 | Beginning of buffer is position (point-min), end is (point-max). |
231 | ||
232 | The return value is POSITION. */) | |
5842a27b | 233 | (register Lisp_Object position) |
35692fe0 | 234 | { |
d311d28c | 235 | ptrdiff_t pos; |
fb8106e8 | 236 | |
72ef82ec RS |
237 | if (MARKERP (position) |
238 | && current_buffer == XMARKER (position)->buffer) | |
ec1c14f6 RS |
239 | { |
240 | pos = marker_position (position); | |
241 | if (pos < BEGV) | |
242 | SET_PT_BOTH (BEGV, BEGV_BYTE); | |
243 | else if (pos > ZV) | |
244 | SET_PT_BOTH (ZV, ZV_BYTE); | |
245 | else | |
246 | SET_PT_BOTH (pos, marker_byte_position (position)); | |
247 | ||
248 | return position; | |
249 | } | |
250 | ||
b7826503 | 251 | CHECK_NUMBER_COERCE_MARKER (position); |
35692fe0 | 252 | |
fb8106e8 | 253 | pos = clip_to_bounds (BEGV, XINT (position), ZV); |
fb8106e8 | 254 | SET_PT (pos); |
2591ec64 | 255 | return position; |
35692fe0 JB |
256 | } |
257 | ||
acb7cc89 GM |
258 | |
259 | /* Return the start or end position of the region. | |
a08d4ba7 | 260 | BEGINNINGP means return the start. |
acb7cc89 GM |
261 | If there is no region active, signal an error. */ |
262 | ||
35692fe0 | 263 | static Lisp_Object |
a08d4ba7 | 264 | region_limit (bool beginningp) |
35692fe0 | 265 | { |
acb7cc89 | 266 | Lisp_Object m; |
177c0ea7 | 267 | |
acb7cc89 GM |
268 | if (!NILP (Vtransient_mark_mode) |
269 | && NILP (Vmark_even_if_inactive) | |
4b4deea2 | 270 | && NILP (BVAR (current_buffer, mark_active))) |
8a0ff744 | 271 | xsignal0 (Qmark_inactive); |
177c0ea7 | 272 | |
4b4deea2 | 273 | m = Fmarker_position (BVAR (current_buffer, mark)); |
acb7cc89 | 274 | if (NILP (m)) |
7b5ad687 | 275 | error ("The mark is not set now, so there is no region"); |
177c0ea7 | 276 | |
f520ef9b | 277 | /* Clip to the current narrowing (bug#11770). */ |
a08d4ba7 | 278 | return make_number ((PT < XFASTINT (m)) == beginningp |
f520ef9b PE |
279 | ? PT |
280 | : clip_to_bounds (BEGV, XFASTINT (m), ZV)); | |
35692fe0 JB |
281 | } |
282 | ||
283 | DEFUN ("region-beginning", Fregion_beginning, Sregion_beginning, 0, 0, 0, | |
7b0815ba | 284 | doc: /* Return the integer value of point or mark, whichever is smaller. */) |
5842a27b | 285 | (void) |
35692fe0 | 286 | { |
acb7cc89 | 287 | return region_limit (1); |
35692fe0 JB |
288 | } |
289 | ||
290 | DEFUN ("region-end", Fregion_end, Sregion_end, 0, 0, 0, | |
7b0815ba | 291 | doc: /* Return the integer value of point or mark, whichever is larger. */) |
5842a27b | 292 | (void) |
35692fe0 | 293 | { |
acb7cc89 | 294 | return region_limit (0); |
35692fe0 JB |
295 | } |
296 | ||
35692fe0 | 297 | DEFUN ("mark-marker", Fmark_marker, Smark_marker, 0, 0, 0, |
7ee72033 | 298 | doc: /* Return this buffer's mark, as a marker object. |
a1f17501 | 299 | Watch out! Moving this marker changes the mark position. |
7ee72033 | 300 | If you set the marker not to point anywhere, the buffer will have no mark. */) |
5842a27b | 301 | (void) |
35692fe0 | 302 | { |
4b4deea2 | 303 | return BVAR (current_buffer, mark); |
35692fe0 | 304 | } |
acb7cc89 | 305 | |
c9ed721d | 306 | \f |
58401a34 SM |
307 | /* Find all the overlays in the current buffer that touch position POS. |
308 | Return the number found, and store them in a vector in VEC | |
309 | of length LEN. */ | |
310 | ||
b081724f PE |
311 | static ptrdiff_t |
312 | overlays_around (EMACS_INT pos, Lisp_Object *vec, ptrdiff_t len) | |
58401a34 | 313 | { |
88006f77 SM |
314 | Lisp_Object overlay, start, end; |
315 | struct Lisp_Overlay *tail; | |
d311d28c | 316 | ptrdiff_t startpos, endpos; |
b081724f | 317 | ptrdiff_t idx = 0; |
58401a34 | 318 | |
fd318b54 | 319 | for (tail = current_buffer->overlays_before; tail; tail = tail->next) |
58401a34 | 320 | { |
88006f77 | 321 | XSETMISC (overlay, tail); |
58401a34 SM |
322 | |
323 | end = OVERLAY_END (overlay); | |
324 | endpos = OVERLAY_POSITION (end); | |
325 | if (endpos < pos) | |
326 | break; | |
327 | start = OVERLAY_START (overlay); | |
328 | startpos = OVERLAY_POSITION (start); | |
329 | if (startpos <= pos) | |
330 | { | |
331 | if (idx < len) | |
332 | vec[idx] = overlay; | |
333 | /* Keep counting overlays even if we can't return them all. */ | |
334 | idx++; | |
335 | } | |
336 | } | |
337 | ||
fd318b54 | 338 | for (tail = current_buffer->overlays_after; tail; tail = tail->next) |
58401a34 | 339 | { |
88006f77 | 340 | XSETMISC (overlay, tail); |
58401a34 SM |
341 | |
342 | start = OVERLAY_START (overlay); | |
343 | startpos = OVERLAY_POSITION (start); | |
344 | if (pos < startpos) | |
345 | break; | |
346 | end = OVERLAY_END (overlay); | |
347 | endpos = OVERLAY_POSITION (end); | |
348 | if (pos <= endpos) | |
349 | { | |
350 | if (idx < len) | |
351 | vec[idx] = overlay; | |
352 | idx++; | |
353 | } | |
354 | } | |
355 | ||
356 | return idx; | |
357 | } | |
358 | ||
359 | /* Return the value of property PROP, in OBJECT at POSITION. | |
360 | It's the value of PROP that a char inserted at POSITION would get. | |
361 | OBJECT is optional and defaults to the current buffer. | |
362 | If OBJECT is a buffer, then overlay properties are considered as well as | |
363 | text properties. | |
364 | If OBJECT is a window, then that window's buffer is used, but | |
365 | window-specific overlays are considered only if they are associated | |
366 | with OBJECT. */ | |
538f9462 | 367 | Lisp_Object |
971de7fb | 368 | get_pos_property (Lisp_Object position, register Lisp_Object prop, Lisp_Object object) |
58401a34 | 369 | { |
58401a34 SM |
370 | CHECK_NUMBER_COERCE_MARKER (position); |
371 | ||
372 | if (NILP (object)) | |
373 | XSETBUFFER (object, current_buffer); | |
dfe6cbf8 | 374 | else if (WINDOWP (object)) |
d3d50620 | 375 | object = XWINDOW (object)->buffer; |
dfe6cbf8 SM |
376 | |
377 | if (!BUFFERP (object)) | |
378 | /* pos-property only makes sense in buffers right now, since strings | |
379 | have no overlays and no notion of insertion for which stickiness | |
380 | could be obeyed. */ | |
381 | return Fget_text_property (position, prop, object); | |
382 | else | |
58401a34 | 383 | { |
29cdc13e | 384 | EMACS_INT posn = XINT (position); |
b081724f | 385 | ptrdiff_t noverlays; |
58401a34 SM |
386 | Lisp_Object *overlay_vec, tem; |
387 | struct buffer *obuf = current_buffer; | |
388 | ||
389 | set_buffer_temp (XBUFFER (object)); | |
390 | ||
391 | /* First try with room for 40 overlays. */ | |
392 | noverlays = 40; | |
38182d90 | 393 | overlay_vec = alloca (noverlays * sizeof *overlay_vec); |
58401a34 SM |
394 | noverlays = overlays_around (posn, overlay_vec, noverlays); |
395 | ||
396 | /* If there are more than 40, | |
397 | make enough space for all, and try again. */ | |
398 | if (noverlays > 40) | |
399 | { | |
38182d90 | 400 | overlay_vec = alloca (noverlays * sizeof *overlay_vec); |
58401a34 SM |
401 | noverlays = overlays_around (posn, overlay_vec, noverlays); |
402 | } | |
403 | noverlays = sort_overlays (overlay_vec, noverlays, NULL); | |
404 | ||
405 | set_buffer_temp (obuf); | |
406 | ||
407 | /* Now check the overlays in order of decreasing priority. */ | |
408 | while (--noverlays >= 0) | |
409 | { | |
410 | Lisp_Object ol = overlay_vec[noverlays]; | |
411 | tem = Foverlay_get (ol, prop); | |
412 | if (!NILP (tem)) | |
413 | { | |
414 | /* Check the overlay is indeed active at point. */ | |
415 | Lisp_Object start = OVERLAY_START (ol), finish = OVERLAY_END (ol); | |
416 | if ((OVERLAY_POSITION (start) == posn | |
417 | && XMARKER (start)->insertion_type == 1) | |
418 | || (OVERLAY_POSITION (finish) == posn | |
419 | && XMARKER (finish)->insertion_type == 0)) | |
420 | ; /* The overlay will not cover a char inserted at point. */ | |
421 | else | |
422 | { | |
423 | return tem; | |
424 | } | |
425 | } | |
426 | } | |
177c0ea7 | 427 | |
7a6a86ad | 428 | { /* Now check the text properties. */ |
dfe6cbf8 SM |
429 | int stickiness = text_property_stickiness (prop, position, object); |
430 | if (stickiness > 0) | |
431 | return Fget_text_property (position, prop, object); | |
432 | else if (stickiness < 0 | |
433 | && XINT (position) > BUF_BEGV (XBUFFER (object))) | |
434 | return Fget_text_property (make_number (XINT (position) - 1), | |
435 | prop, object); | |
436 | else | |
437 | return Qnil; | |
438 | } | |
58401a34 | 439 | } |
58401a34 SM |
440 | } |
441 | ||
a3caef99 | 442 | /* Find the field surrounding POS in *BEG and *END. If POS is nil, |
59062dce | 443 | the value of point is used instead. If BEG or END is null, |
acb7cc89 | 444 | means don't store the beginning or end of the field. |
a3caef99 | 445 | |
9ac741c5 MB |
446 | BEG_LIMIT and END_LIMIT serve to limit the ranged of the returned |
447 | results; they do not effect boundary behavior. | |
448 | ||
a08d4ba7 | 449 | If MERGE_AT_BOUNDARY is non-nil, then if POS is at the very first |
ee547125 MB |
450 | position of a field, then the beginning of the previous field is |
451 | returned instead of the beginning of POS's field (since the end of a | |
452 | field is actually also the beginning of the next input field, this | |
453 | behavior is sometimes useful). Additionally in the MERGE_AT_BOUNDARY | |
a08d4ba7 | 454 | non-nil case, if two fields are separated by a field with the special |
ee547125 MB |
455 | value `boundary', and POS lies within it, then the two separated |
456 | fields are considered to be adjacent, and POS between them, when | |
457 | finding the beginning and ending of the "merged" field. | |
a3caef99 RS |
458 | |
459 | Either BEG or END may be 0, in which case the corresponding value | |
460 | is not stored. */ | |
461 | ||
acb7cc89 | 462 | static void |
413d18e7 EZ |
463 | find_field (Lisp_Object pos, Lisp_Object merge_at_boundary, |
464 | Lisp_Object beg_limit, | |
d311d28c | 465 | ptrdiff_t *beg, Lisp_Object end_limit, ptrdiff_t *end) |
0daf6e8d | 466 | { |
ee547125 MB |
467 | /* Fields right before and after the point. */ |
468 | Lisp_Object before_field, after_field; | |
a08d4ba7 PE |
469 | /* True if POS counts as the start of a field. */ |
470 | bool at_field_start = 0; | |
471 | /* True if POS counts as the end of a field. */ | |
472 | bool at_field_end = 0; | |
ee547125 | 473 | |
0daf6e8d GM |
474 | if (NILP (pos)) |
475 | XSETFASTINT (pos, PT); | |
476 | else | |
b7826503 | 477 | CHECK_NUMBER_COERCE_MARKER (pos); |
0daf6e8d | 478 | |
acb7cc89 | 479 | after_field |
58401a34 | 480 | = get_char_property_and_overlay (pos, Qfield, Qnil, NULL); |
acb7cc89 GM |
481 | before_field |
482 | = (XFASTINT (pos) > BEGV | |
7ae1c032 | 483 | ? get_char_property_and_overlay (make_number (XINT (pos) - 1), |
58401a34 | 484 | Qfield, Qnil, NULL) |
e477bb04 KL |
485 | /* Using nil here would be a more obvious choice, but it would |
486 | fail when the buffer starts with a non-sticky field. */ | |
487 | : after_field); | |
ee547125 MB |
488 | |
489 | /* See if we need to handle the case where MERGE_AT_BOUNDARY is nil | |
490 | and POS is at beginning of a field, which can also be interpreted | |
491 | as the end of the previous field. Note that the case where if | |
492 | MERGE_AT_BOUNDARY is non-nil (see function comment) is actually the | |
493 | more natural one; then we avoid treating the beginning of a field | |
494 | specially. */ | |
58401a34 | 495 | if (NILP (merge_at_boundary)) |
ee547125 | 496 | { |
58401a34 SM |
497 | Lisp_Object field = get_pos_property (pos, Qfield, Qnil); |
498 | if (!EQ (field, after_field)) | |
ee547125 | 499 | at_field_end = 1; |
58401a34 SM |
500 | if (!EQ (field, before_field)) |
501 | at_field_start = 1; | |
2db1186a SM |
502 | if (NILP (field) && at_field_start && at_field_end) |
503 | /* If an inserted char would have a nil field while the surrounding | |
504 | text is non-nil, we're probably not looking at a | |
505 | zero-length field, but instead at a non-nil field that's | |
506 | not intended for editing (such as comint's prompts). */ | |
507 | at_field_end = at_field_start = 0; | |
0daf6e8d GM |
508 | } |
509 | ||
ee547125 MB |
510 | /* Note about special `boundary' fields: |
511 | ||
512 | Consider the case where the point (`.') is between the fields `x' and `y': | |
513 | ||
514 | xxxx.yyyy | |
515 | ||
a08d4ba7 | 516 | In this situation, if merge_at_boundary is non-nil, consider the |
ee547125 MB |
517 | `x' and `y' fields as forming one big merged field, and so the end |
518 | of the field is the end of `y'. | |
519 | ||
520 | However, if `x' and `y' are separated by a special `boundary' field | |
a08d4ba7 | 521 | (a field with a `field' char-property of 'boundary), then ignore |
ee547125 MB |
522 | this special field when merging adjacent fields. Here's the same |
523 | situation, but with a `boundary' field between the `x' and `y' fields: | |
524 | ||
525 | xxx.BBBByyyy | |
526 | ||
527 | Here, if point is at the end of `x', the beginning of `y', or | |
a08d4ba7 | 528 | anywhere in-between (within the `boundary' field), merge all |
ee547125 MB |
529 | three fields and consider the beginning as being the beginning of |
530 | the `x' field, and the end as being the end of the `y' field. */ | |
531 | ||
0daf6e8d | 532 | if (beg) |
acb7cc89 GM |
533 | { |
534 | if (at_field_start) | |
535 | /* POS is at the edge of a field, and we should consider it as | |
536 | the beginning of the following field. */ | |
537 | *beg = XFASTINT (pos); | |
538 | else | |
539 | /* Find the previous field boundary. */ | |
540 | { | |
58401a34 | 541 | Lisp_Object p = pos; |
acb7cc89 GM |
542 | if (!NILP (merge_at_boundary) && EQ (before_field, Qboundary)) |
543 | /* Skip a `boundary' field. */ | |
58401a34 | 544 | p = Fprevious_single_char_property_change (p, Qfield, Qnil, |
9ac741c5 | 545 | beg_limit); |
58401a34 SM |
546 | |
547 | p = Fprevious_single_char_property_change (p, Qfield, Qnil, | |
548 | beg_limit); | |
549 | *beg = NILP (p) ? BEGV : XFASTINT (p); | |
acb7cc89 GM |
550 | } |
551 | } | |
0daf6e8d GM |
552 | |
553 | if (end) | |
acb7cc89 GM |
554 | { |
555 | if (at_field_end) | |
556 | /* POS is at the edge of a field, and we should consider it as | |
557 | the end of the previous field. */ | |
558 | *end = XFASTINT (pos); | |
559 | else | |
560 | /* Find the next field boundary. */ | |
561 | { | |
562 | if (!NILP (merge_at_boundary) && EQ (after_field, Qboundary)) | |
563 | /* Skip a `boundary' field. */ | |
9ac741c5 MB |
564 | pos = Fnext_single_char_property_change (pos, Qfield, Qnil, |
565 | end_limit); | |
ee547125 | 566 | |
9ac741c5 MB |
567 | pos = Fnext_single_char_property_change (pos, Qfield, Qnil, |
568 | end_limit); | |
acb7cc89 GM |
569 | *end = NILP (pos) ? ZV : XFASTINT (pos); |
570 | } | |
571 | } | |
0daf6e8d | 572 | } |
acb7cc89 | 573 | |
0daf6e8d | 574 | \f |
d01f3570 | 575 | DEFUN ("delete-field", Fdelete_field, Sdelete_field, 0, 1, 0, |
7ee72033 | 576 | doc: /* Delete the field surrounding POS. |
a1f17501 | 577 | A field is a region of text with the same `field' property. |
f554db0f | 578 | If POS is nil, the value of point is used for POS. */) |
5842a27b | 579 | (Lisp_Object pos) |
0daf6e8d | 580 | { |
d311d28c | 581 | ptrdiff_t beg, end; |
9ac741c5 | 582 | find_field (pos, Qnil, Qnil, &beg, Qnil, &end); |
0daf6e8d GM |
583 | if (beg != end) |
584 | del_range (beg, end); | |
d01f3570 | 585 | return Qnil; |
0daf6e8d GM |
586 | } |
587 | ||
588 | DEFUN ("field-string", Ffield_string, Sfield_string, 0, 1, 0, | |
7ee72033 | 589 | doc: /* Return the contents of the field surrounding POS as a string. |
a1f17501 | 590 | A field is a region of text with the same `field' property. |
f554db0f | 591 | If POS is nil, the value of point is used for POS. */) |
5842a27b | 592 | (Lisp_Object pos) |
0daf6e8d | 593 | { |
d311d28c | 594 | ptrdiff_t beg, end; |
9ac741c5 | 595 | find_field (pos, Qnil, Qnil, &beg, Qnil, &end); |
0daf6e8d GM |
596 | return make_buffer_string (beg, end, 1); |
597 | } | |
598 | ||
599 | DEFUN ("field-string-no-properties", Ffield_string_no_properties, Sfield_string_no_properties, 0, 1, 0, | |
7a6a86ad | 600 | doc: /* Return the contents of the field around POS, without text properties. |
a1f17501 | 601 | A field is a region of text with the same `field' property. |
f554db0f | 602 | If POS is nil, the value of point is used for POS. */) |
5842a27b | 603 | (Lisp_Object pos) |
0daf6e8d | 604 | { |
d311d28c | 605 | ptrdiff_t beg, end; |
9ac741c5 | 606 | find_field (pos, Qnil, Qnil, &beg, Qnil, &end); |
0daf6e8d GM |
607 | return make_buffer_string (beg, end, 0); |
608 | } | |
609 | ||
9ac741c5 | 610 | DEFUN ("field-beginning", Ffield_beginning, Sfield_beginning, 0, 3, 0, |
7ee72033 | 611 | doc: /* Return the beginning of the field surrounding POS. |
a1f17501 PJ |
612 | A field is a region of text with the same `field' property. |
613 | If POS is nil, the value of point is used for POS. | |
614 | If ESCAPE-FROM-EDGE is non-nil and POS is at the beginning of its | |
9ac741c5 MB |
615 | field, then the beginning of the *previous* field is returned. |
616 | If LIMIT is non-nil, it is a buffer position; if the beginning of the field | |
f554db0f | 617 | is before LIMIT, then LIMIT will be returned instead. */) |
5842a27b | 618 | (Lisp_Object pos, Lisp_Object escape_from_edge, Lisp_Object limit) |
0daf6e8d | 619 | { |
d311d28c | 620 | ptrdiff_t beg; |
9ac741c5 | 621 | find_field (pos, escape_from_edge, limit, &beg, Qnil, 0); |
0daf6e8d GM |
622 | return make_number (beg); |
623 | } | |
624 | ||
a7ca3326 | 625 | DEFUN ("field-end", Ffield_end, Sfield_end, 0, 3, 0, |
7ee72033 | 626 | doc: /* Return the end of the field surrounding POS. |
a1f17501 PJ |
627 | A field is a region of text with the same `field' property. |
628 | If POS is nil, the value of point is used for POS. | |
629 | If ESCAPE-FROM-EDGE is non-nil and POS is at the end of its field, | |
9ac741c5 MB |
630 | then the end of the *following* field is returned. |
631 | If LIMIT is non-nil, it is a buffer position; if the end of the field | |
f554db0f | 632 | is after LIMIT, then LIMIT will be returned instead. */) |
5842a27b | 633 | (Lisp_Object pos, Lisp_Object escape_from_edge, Lisp_Object limit) |
0daf6e8d | 634 | { |
d311d28c | 635 | ptrdiff_t end; |
9ac741c5 | 636 | find_field (pos, escape_from_edge, Qnil, 0, limit, &end); |
0daf6e8d GM |
637 | return make_number (end); |
638 | } | |
639 | ||
a7ca3326 | 640 | DEFUN ("constrain-to-field", Fconstrain_to_field, Sconstrain_to_field, 2, 5, 0, |
7ee72033 | 641 | doc: /* Return the position closest to NEW-POS that is in the same field as OLD-POS. |
a1f17501 | 642 | A field is a region of text with the same `field' property. |
66c5eebd CY |
643 | |
644 | If NEW-POS is nil, then use the current point instead, and move point | |
645 | to the resulting constrained position, in addition to returning that | |
646 | position. | |
a1f17501 PJ |
647 | |
648 | If OLD-POS is at the boundary of two fields, then the allowable | |
649 | positions for NEW-POS depends on the value of the optional argument | |
650 | ESCAPE-FROM-EDGE: If ESCAPE-FROM-EDGE is nil, then NEW-POS is | |
651 | constrained to the field that has the same `field' char-property | |
652 | as any new characters inserted at OLD-POS, whereas if ESCAPE-FROM-EDGE | |
653 | is non-nil, NEW-POS is constrained to the union of the two adjacent | |
654 | fields. Additionally, if two fields are separated by another field with | |
655 | the special value `boundary', then any point within this special field is | |
656 | also considered to be `on the boundary'. | |
657 | ||
658 | If the optional argument ONLY-IN-LINE is non-nil and constraining | |
659 | NEW-POS would move it to a different line, NEW-POS is returned | |
660 | unconstrained. This useful for commands that move by line, like | |
661 | \\[next-line] or \\[beginning-of-line], which should generally respect field boundaries | |
662 | only in the case where they can still move to the right line. | |
663 | ||
664 | If the optional argument INHIBIT-CAPTURE-PROPERTY is non-nil, and OLD-POS has | |
665 | a non-nil property of that name, then any field boundaries are ignored. | |
666 | ||
7ee72033 | 667 | Field boundaries are not noticed if `inhibit-field-text-motion' is non-nil. */) |
5842a27b | 668 | (Lisp_Object new_pos, Lisp_Object old_pos, Lisp_Object escape_from_edge, Lisp_Object only_in_line, Lisp_Object inhibit_capture_property) |
0daf6e8d GM |
669 | { |
670 | /* If non-zero, then the original point, before re-positioning. */ | |
d311d28c | 671 | ptrdiff_t orig_point = 0; |
a08d4ba7 | 672 | bool fwd; |
d63b4018 | 673 | Lisp_Object prev_old, prev_new; |
aac18aa4 | 674 | |
0daf6e8d GM |
675 | if (NILP (new_pos)) |
676 | /* Use the current point, and afterwards, set it. */ | |
677 | { | |
678 | orig_point = PT; | |
679 | XSETFASTINT (new_pos, PT); | |
680 | } | |
681 | ||
e477bb04 KL |
682 | CHECK_NUMBER_COERCE_MARKER (new_pos); |
683 | CHECK_NUMBER_COERCE_MARKER (old_pos); | |
684 | ||
d311d28c | 685 | fwd = (XINT (new_pos) > XINT (old_pos)); |
e477bb04 | 686 | |
d311d28c PE |
687 | prev_old = make_number (XINT (old_pos) - 1); |
688 | prev_new = make_number (XINT (new_pos) - 1); | |
aac18aa4 | 689 | |
ee5cd4db GM |
690 | if (NILP (Vinhibit_field_text_motion) |
691 | && !EQ (new_pos, old_pos) | |
42ab8e36 MB |
692 | && (!NILP (Fget_char_property (new_pos, Qfield, Qnil)) |
693 | || !NILP (Fget_char_property (old_pos, Qfield, Qnil)) | |
e477bb04 KL |
694 | /* To recognize field boundaries, we must also look at the |
695 | previous positions; we could use `get_pos_property' | |
696 | instead, but in itself that would fail inside non-sticky | |
697 | fields (like comint prompts). */ | |
698 | || (XFASTINT (new_pos) > BEGV | |
42ab8e36 | 699 | && !NILP (Fget_char_property (prev_new, Qfield, Qnil))) |
e477bb04 | 700 | || (XFASTINT (old_pos) > BEGV |
42ab8e36 | 701 | && !NILP (Fget_char_property (prev_old, Qfield, Qnil)))) |
ee547125 | 702 | && (NILP (inhibit_capture_property) |
e477bb04 KL |
703 | /* Field boundaries are again a problem; but now we must |
704 | decide the case exactly, so we need to call | |
705 | `get_pos_property' as well. */ | |
706 | || (NILP (get_pos_property (old_pos, inhibit_capture_property, Qnil)) | |
707 | && (XFASTINT (old_pos) <= BEGV | |
42ab8e36 MB |
708 | || NILP (Fget_char_property (old_pos, inhibit_capture_property, Qnil)) |
709 | || NILP (Fget_char_property (prev_old, inhibit_capture_property, Qnil)))))) | |
2cb3aec4 KL |
710 | /* It is possible that NEW_POS is not within the same field as |
711 | OLD_POS; try to move NEW_POS so that it is. */ | |
0daf6e8d | 712 | { |
d311d28c | 713 | ptrdiff_t shortage; |
0daf6e8d GM |
714 | Lisp_Object field_bound; |
715 | ||
0daf6e8d | 716 | if (fwd) |
9ac741c5 | 717 | field_bound = Ffield_end (old_pos, escape_from_edge, new_pos); |
0daf6e8d | 718 | else |
9ac741c5 | 719 | field_bound = Ffield_beginning (old_pos, escape_from_edge, new_pos); |
0daf6e8d | 720 | |
10b0f752 MB |
721 | if (/* See if ESCAPE_FROM_EDGE caused FIELD_BOUND to jump to the |
722 | other side of NEW_POS, which would mean that NEW_POS is | |
723 | already acceptable, and it's not necessary to constrain it | |
724 | to FIELD_BOUND. */ | |
725 | ((XFASTINT (field_bound) < XFASTINT (new_pos)) ? fwd : !fwd) | |
726 | /* NEW_POS should be constrained, but only if either | |
727 | ONLY_IN_LINE is nil (in which case any constraint is OK), | |
728 | or NEW_POS and FIELD_BOUND are on the same line (in which | |
729 | case the constraint is OK even if ONLY_IN_LINE is non-nil). */ | |
730 | && (NILP (only_in_line) | |
731 | /* This is the ONLY_IN_LINE case, check that NEW_POS and | |
732 | FIELD_BOUND are on the same line by seeing whether | |
733 | there's an intervening newline or not. */ | |
734 | || (scan_buffer ('\n', | |
735 | XFASTINT (new_pos), XFASTINT (field_bound), | |
736 | fwd ? -1 : 1, &shortage, 1), | |
737 | shortage != 0))) | |
0daf6e8d GM |
738 | /* Constrain NEW_POS to FIELD_BOUND. */ |
739 | new_pos = field_bound; | |
740 | ||
741 | if (orig_point && XFASTINT (new_pos) != orig_point) | |
742 | /* The NEW_POS argument was originally nil, so automatically set PT. */ | |
743 | SET_PT (XFASTINT (new_pos)); | |
744 | } | |
745 | ||
746 | return new_pos; | |
747 | } | |
acb7cc89 | 748 | |
0daf6e8d | 749 | \f |
a7ca3326 | 750 | DEFUN ("line-beginning-position", |
6d57c318 | 751 | Fline_beginning_position, Sline_beginning_position, 0, 1, 0, |
7ee72033 | 752 | doc: /* Return the character position of the first character on the current line. |
c88b867f CY |
753 | With optional argument N, scan forward N - 1 lines first. |
754 | If the scan reaches the end of the buffer, return that position. | |
6d57c318 | 755 | |
c88b867f CY |
756 | This function ignores text display directionality; it returns the |
757 | position of the first character in logical order, i.e. the smallest | |
758 | character position on the line. | |
cd21226d | 759 | |
2cb3aec4 | 760 | This function constrains the returned position to the current field |
c88b867f | 761 | unless that position would be on a different line than the original, |
2cb3aec4 KL |
762 | unconstrained result. If N is nil or 1, and a front-sticky field |
763 | starts at point, the scan stops as soon as it starts. To ignore field | |
c88b867f | 764 | boundaries, bind `inhibit-field-text-motion' to t. |
a1f17501 | 765 | |
7ee72033 | 766 | This function does not move point. */) |
5842a27b | 767 | (Lisp_Object n) |
c9ed721d | 768 | { |
d311d28c PE |
769 | ptrdiff_t orig, orig_byte, end; |
770 | ptrdiff_t count = SPECPDL_INDEX (); | |
4e8f005c | 771 | specbind (Qinhibit_point_motion_hooks, Qt); |
c9ed721d RS |
772 | |
773 | if (NILP (n)) | |
774 | XSETFASTINT (n, 1); | |
775 | else | |
b7826503 | 776 | CHECK_NUMBER (n); |
c9ed721d RS |
777 | |
778 | orig = PT; | |
ec1c14f6 | 779 | orig_byte = PT_BYTE; |
c9ed721d RS |
780 | Fforward_line (make_number (XINT (n) - 1)); |
781 | end = PT; | |
e2dae3f2 | 782 | |
ec1c14f6 | 783 | SET_PT_BOTH (orig, orig_byte); |
35692fe0 | 784 | |
4e8f005c CY |
785 | unbind_to (count, Qnil); |
786 | ||
0daf6e8d | 787 | /* Return END constrained to the current input field. */ |
ee5cd4db GM |
788 | return Fconstrain_to_field (make_number (end), make_number (orig), |
789 | XINT (n) != 1 ? Qt : Qnil, | |
ee547125 | 790 | Qt, Qnil); |
c9ed721d RS |
791 | } |
792 | ||
a7ca3326 | 793 | DEFUN ("line-end-position", Fline_end_position, Sline_end_position, 0, 1, 0, |
7ee72033 | 794 | doc: /* Return the character position of the last character on the current line. |
a1f17501 PJ |
795 | With argument N not nil or 1, move forward N - 1 lines first. |
796 | If scan reaches end of buffer, return that position. | |
6d57c318 | 797 | |
c88b867f CY |
798 | This function ignores text display directionality; it returns the |
799 | position of the last character in logical order, i.e. the largest | |
800 | character position on the line. | |
cd21226d | 801 | |
2cb3aec4 KL |
802 | This function constrains the returned position to the current field |
803 | unless that would be on a different line than the original, | |
804 | unconstrained result. If N is nil or 1, and a rear-sticky field ends | |
805 | at point, the scan stops as soon as it starts. To ignore field | |
6d57c318 MB |
806 | boundaries bind `inhibit-field-text-motion' to t. |
807 | ||
7ee72033 | 808 | This function does not move point. */) |
5842a27b | 809 | (Lisp_Object n) |
c9ed721d | 810 | { |
d311d28c PE |
811 | ptrdiff_t clipped_n; |
812 | ptrdiff_t end_pos; | |
813 | ptrdiff_t orig = PT; | |
0daf6e8d | 814 | |
c9ed721d RS |
815 | if (NILP (n)) |
816 | XSETFASTINT (n, 1); | |
817 | else | |
b7826503 | 818 | CHECK_NUMBER (n); |
c9ed721d | 819 | |
d311d28c PE |
820 | clipped_n = clip_to_bounds (PTRDIFF_MIN + 1, XINT (n), PTRDIFF_MAX); |
821 | end_pos = find_before_next_newline (orig, 0, clipped_n - (clipped_n <= 0)); | |
0daf6e8d GM |
822 | |
823 | /* Return END_POS constrained to the current input field. */ | |
ee5cd4db | 824 | return Fconstrain_to_field (make_number (end_pos), make_number (orig), |
ee547125 | 825 | Qnil, Qt, Qnil); |
c9ed721d | 826 | } |
6d57c318 | 827 | |
62c2e5ed DA |
828 | /* Save current buffer state for `save-excursion' special form. |
829 | We (ab)use Lisp_Misc_Save_Value to allow explicit free and so | |
830 | offload some work from GC. */ | |
831 | ||
35692fe0 | 832 | Lisp_Object |
971de7fb | 833 | save_excursion_save (void) |
35692fe0 | 834 | { |
62c2e5ed DA |
835 | Lisp_Object save, *data = xmalloc (word_size * 4); |
836 | ||
837 | data[0] = Fpoint_marker (); | |
549c3414 | 838 | /* Do not copy the mark if it points to nowhere. */ |
62c2e5ed DA |
839 | data[1] = (XMARKER (BVAR (current_buffer, mark))->buffer |
840 | ? Fcopy_marker (BVAR (current_buffer, mark), Qnil) | |
841 | : Qnil); | |
842 | /* Selected window if current buffer is shown in it, nil otherwise. */ | |
843 | data[2] = ((XBUFFER (XWINDOW (selected_window)->buffer) == current_buffer) | |
844 | ? selected_window : Qnil); | |
845 | data[3] = BVAR (current_buffer, mark_active); | |
846 | ||
847 | save = make_save_value (data, 4); | |
848 | XSAVE_VALUE (save)->dogc = 1; | |
849 | return save; | |
35692fe0 JB |
850 | } |
851 | ||
62c2e5ed DA |
852 | /* Restore saved buffer before leaving `save-excursion' special form. */ |
853 | ||
35692fe0 | 854 | Lisp_Object |
eeaea515 | 855 | save_excursion_restore (Lisp_Object info) |
35692fe0 | 856 | { |
62c2e5ed | 857 | Lisp_Object tem, tem1, omark, nmark, *data = XSAVE_VALUE (info)->pointer; |
eeaea515 | 858 | struct gcpro gcpro1, gcpro2, gcpro3; |
eeaea515 | 859 | |
62c2e5ed DA |
860 | tem = Fmarker_buffer (data[0]); |
861 | /* If we're unwinding to top level, saved buffer may be deleted. This | |
862 | means that all of its markers are unchained and so tem is nil. */ | |
eeaea515 | 863 | if (NILP (tem)) |
62c2e5ed | 864 | goto out; |
eeaea515 DA |
865 | |
866 | omark = nmark = Qnil; | |
867 | GCPRO3 (info, omark, nmark); | |
868 | ||
869 | Fset_buffer (tem); | |
870 | ||
871 | /* Point marker. */ | |
62c2e5ed | 872 | tem = data[0]; |
eeaea515 DA |
873 | Fgoto_char (tem); |
874 | unchain_marker (XMARKER (tem)); | |
875 | ||
876 | /* Mark marker. */ | |
62c2e5ed | 877 | tem = data[1]; |
eeaea515 | 878 | omark = Fmarker_position (BVAR (current_buffer, mark)); |
549c3414 DA |
879 | if (NILP (tem)) |
880 | unchain_marker (XMARKER (BVAR (current_buffer, mark))); | |
881 | else | |
882 | { | |
883 | Fset_marker (BVAR (current_buffer, mark), tem, Fcurrent_buffer ()); | |
884 | nmark = Fmarker_position (tem); | |
885 | unchain_marker (XMARKER (tem)); | |
886 | } | |
eeaea515 | 887 | |
62c2e5ed DA |
888 | /* Mark active. */ |
889 | tem = data[3]; | |
eeaea515 | 890 | tem1 = BVAR (current_buffer, mark_active); |
39eb03f1 | 891 | bset_mark_active (current_buffer, tem); |
eeaea515 DA |
892 | |
893 | /* If mark is active now, and either was not active | |
894 | or was at a different place, run the activate hook. */ | |
895 | if (! NILP (tem)) | |
dee091a3 | 896 | { |
eeaea515 DA |
897 | if (! EQ (omark, nmark)) |
898 | { | |
899 | tem = intern ("activate-mark-hook"); | |
900 | Frun_hooks (1, &tem); | |
901 | } | |
902 | } | |
903 | /* If mark has ceased to be active, run deactivate hook. */ | |
904 | else if (! NILP (tem1)) | |
905 | { | |
906 | tem = intern ("deactivate-mark-hook"); | |
907 | Frun_hooks (1, &tem); | |
9fed2b18 | 908 | } |
2483cf58 | 909 | |
eeaea515 DA |
910 | /* If buffer was visible in a window, and a different window was |
911 | selected, and the old selected window is still showing this | |
912 | buffer, restore point in that window. */ | |
62c2e5ed DA |
913 | tem = data[2]; |
914 | if (WINDOWP (tem) | |
eeaea515 | 915 | && !EQ (tem, selected_window) |
d3d50620 | 916 | && (tem1 = XWINDOW (tem)->buffer, |
eeaea515 DA |
917 | (/* Window is live... */ |
918 | BUFFERP (tem1) | |
919 | /* ...and it shows the current buffer. */ | |
920 | && XBUFFER (tem1) == current_buffer))) | |
921 | Fset_window_point (tem, make_number (PT)); | |
922 | ||
923 | UNGCPRO; | |
62c2e5ed DA |
924 | |
925 | out: | |
926 | ||
927 | free_save_value (info); | |
35692fe0 JB |
928 | return Qnil; |
929 | } | |
930 | ||
931 | DEFUN ("save-excursion", Fsave_excursion, Ssave_excursion, 0, UNEVALLED, 0, | |
7ee72033 | 932 | doc: /* Save point, mark, and current buffer; execute BODY; restore those things. |
a1f17501 PJ |
933 | Executes BODY just like `progn'. |
934 | The values of point, mark and the current buffer are restored | |
935 | even in case of abnormal exit (throw or error). | |
936 | The state of activation of the mark is also restored. | |
937 | ||
938 | This construct does not save `deactivate-mark', and therefore | |
939 | functions that change the buffer will still cause deactivation | |
940 | of the mark at the end of the command. To prevent that, bind | |
33c2d29f MB |
941 | `deactivate-mark' with `let'. |
942 | ||
7450fd36 SM |
943 | If you only want to save the current buffer but not point nor mark, |
944 | then just use `save-current-buffer', or even `with-current-buffer'. | |
945 | ||
33c2d29f | 946 | usage: (save-excursion &rest BODY) */) |
5842a27b | 947 | (Lisp_Object args) |
35692fe0 JB |
948 | { |
949 | register Lisp_Object val; | |
d311d28c | 950 | ptrdiff_t count = SPECPDL_INDEX (); |
35692fe0 JB |
951 | |
952 | record_unwind_protect (save_excursion_restore, save_excursion_save ()); | |
4bc8c7d2 RS |
953 | |
954 | val = Fprogn (args); | |
955 | return unbind_to (count, val); | |
956 | } | |
957 | ||
958 | DEFUN ("save-current-buffer", Fsave_current_buffer, Ssave_current_buffer, 0, UNEVALLED, 0, | |
4abcdac8 CY |
959 | doc: /* Record which buffer is current; execute BODY; make that buffer current. |
960 | BODY is executed just like `progn'. | |
33c2d29f | 961 | usage: (save-current-buffer &rest BODY) */) |
5842a27b | 962 | (Lisp_Object args) |
4bc8c7d2 | 963 | { |
d311d28c | 964 | ptrdiff_t count = SPECPDL_INDEX (); |
4bc8c7d2 | 965 | |
66322887 DA |
966 | record_unwind_current_buffer (); |
967 | return unbind_to (count, Fprogn (args)); | |
35692fe0 JB |
968 | } |
969 | \f | |
95dccf75 | 970 | DEFUN ("buffer-size", Fbufsize, Sbufsize, 0, 1, 0, |
7ee72033 MB |
971 | doc: /* Return the number of characters in the current buffer. |
972 | If BUFFER, return the number of characters in that buffer instead. */) | |
5842a27b | 973 | (Lisp_Object buffer) |
35692fe0 | 974 | { |
95dccf75 RS |
975 | if (NILP (buffer)) |
976 | return make_number (Z - BEG); | |
02050596 RS |
977 | else |
978 | { | |
b7826503 | 979 | CHECK_BUFFER (buffer); |
02050596 RS |
980 | return make_number (BUF_Z (XBUFFER (buffer)) |
981 | - BUF_BEG (XBUFFER (buffer))); | |
982 | } | |
35692fe0 JB |
983 | } |
984 | ||
985 | DEFUN ("point-min", Fpoint_min, Spoint_min, 0, 0, 0, | |
7ee72033 MB |
986 | doc: /* Return the minimum permissible value of point in the current buffer. |
987 | This is 1, unless narrowing (a buffer restriction) is in effect. */) | |
5842a27b | 988 | (void) |
35692fe0 JB |
989 | { |
990 | Lisp_Object temp; | |
55561c63 | 991 | XSETFASTINT (temp, BEGV); |
35692fe0 JB |
992 | return temp; |
993 | } | |
994 | ||
995 | DEFUN ("point-min-marker", Fpoint_min_marker, Spoint_min_marker, 0, 0, 0, | |
7ee72033 MB |
996 | doc: /* Return a marker to the minimum permissible value of point in this buffer. |
997 | This is the beginning, unless narrowing (a buffer restriction) is in effect. */) | |
5842a27b | 998 | (void) |
35692fe0 | 999 | { |
657924ff | 1000 | return build_marker (current_buffer, BEGV, BEGV_BYTE); |
35692fe0 JB |
1001 | } |
1002 | ||
1003 | DEFUN ("point-max", Fpoint_max, Spoint_max, 0, 0, 0, | |
7ee72033 | 1004 | doc: /* Return the maximum permissible value of point in the current buffer. |
a1f17501 | 1005 | This is (1+ (buffer-size)), unless narrowing (a buffer restriction) |
7ee72033 | 1006 | is in effect, in which case it is less. */) |
5842a27b | 1007 | (void) |
35692fe0 JB |
1008 | { |
1009 | Lisp_Object temp; | |
55561c63 | 1010 | XSETFASTINT (temp, ZV); |
35692fe0 JB |
1011 | return temp; |
1012 | } | |
1013 | ||
a7ca3326 | 1014 | DEFUN ("point-max-marker", Fpoint_max_marker, Spoint_max_marker, 0, 0, 0, |
7ee72033 | 1015 | doc: /* Return a marker to the maximum permissible value of point in this buffer. |
a1f17501 | 1016 | This is (1+ (buffer-size)), unless narrowing (a buffer restriction) |
7ee72033 | 1017 | is in effect, in which case it is less. */) |
5842a27b | 1018 | (void) |
35692fe0 | 1019 | { |
657924ff | 1020 | return build_marker (current_buffer, ZV, ZV_BYTE); |
35692fe0 JB |
1021 | } |
1022 | ||
c86212b9 | 1023 | DEFUN ("gap-position", Fgap_position, Sgap_position, 0, 0, 0, |
7ee72033 MB |
1024 | doc: /* Return the position of the gap, in the current buffer. |
1025 | See also `gap-size'. */) | |
5842a27b | 1026 | (void) |
c86212b9 RS |
1027 | { |
1028 | Lisp_Object temp; | |
1029 | XSETFASTINT (temp, GPT); | |
1030 | return temp; | |
1031 | } | |
1032 | ||
1033 | DEFUN ("gap-size", Fgap_size, Sgap_size, 0, 0, 0, | |
7ee72033 MB |
1034 | doc: /* Return the size of the current buffer's gap. |
1035 | See also `gap-position'. */) | |
5842a27b | 1036 | (void) |
c86212b9 RS |
1037 | { |
1038 | Lisp_Object temp; | |
1039 | XSETFASTINT (temp, GAP_SIZE); | |
1040 | return temp; | |
1041 | } | |
1042 | ||
7df74da6 | 1043 | DEFUN ("position-bytes", Fposition_bytes, Sposition_bytes, 1, 1, 0, |
7ee72033 MB |
1044 | doc: /* Return the byte position for character position POSITION. |
1045 | If POSITION is out of range, the value is nil. */) | |
5842a27b | 1046 | (Lisp_Object position) |
7df74da6 | 1047 | { |
b7826503 | 1048 | CHECK_NUMBER_COERCE_MARKER (position); |
fcf9683e KH |
1049 | if (XINT (position) < BEG || XINT (position) > Z) |
1050 | return Qnil; | |
fa8a5a15 | 1051 | return make_number (CHAR_TO_BYTE (XINT (position))); |
7df74da6 | 1052 | } |
3ab0732d RS |
1053 | |
1054 | DEFUN ("byte-to-position", Fbyte_to_position, Sbyte_to_position, 1, 1, 0, | |
7ee72033 MB |
1055 | doc: /* Return the character position for byte position BYTEPOS. |
1056 | If BYTEPOS is out of range, the value is nil. */) | |
5842a27b | 1057 | (Lisp_Object bytepos) |
3ab0732d | 1058 | { |
b7826503 | 1059 | CHECK_NUMBER (bytepos); |
fcf9683e KH |
1060 | if (XINT (bytepos) < BEG_BYTE || XINT (bytepos) > Z_BYTE) |
1061 | return Qnil; | |
3ab0732d RS |
1062 | return make_number (BYTE_TO_CHAR (XINT (bytepos))); |
1063 | } | |
7df74da6 | 1064 | \f |
a7ca3326 | 1065 | DEFUN ("following-char", Ffollowing_char, Sfollowing_char, 0, 0, 0, |
7ee72033 MB |
1066 | doc: /* Return the character following point, as a number. |
1067 | At the end of the buffer or accessible region, return 0. */) | |
5842a27b | 1068 | (void) |
35692fe0 JB |
1069 | { |
1070 | Lisp_Object temp; | |
6ec8bbd2 | 1071 | if (PT >= ZV) |
55561c63 | 1072 | XSETFASTINT (temp, 0); |
850a8179 | 1073 | else |
ec1c14f6 | 1074 | XSETFASTINT (temp, FETCH_CHAR (PT_BYTE)); |
35692fe0 JB |
1075 | return temp; |
1076 | } | |
1077 | ||
a7ca3326 | 1078 | DEFUN ("preceding-char", Fprevious_char, Sprevious_char, 0, 0, 0, |
7ee72033 MB |
1079 | doc: /* Return the character preceding point, as a number. |
1080 | At the beginning of the buffer or accessible region, return 0. */) | |
5842a27b | 1081 | (void) |
35692fe0 JB |
1082 | { |
1083 | Lisp_Object temp; | |
6ec8bbd2 | 1084 | if (PT <= BEGV) |
55561c63 | 1085 | XSETFASTINT (temp, 0); |
4b4deea2 | 1086 | else if (!NILP (BVAR (current_buffer, enable_multibyte_characters))) |
fb8106e8 | 1087 | { |
d311d28c | 1088 | ptrdiff_t pos = PT_BYTE; |
fb8106e8 KH |
1089 | DEC_POS (pos); |
1090 | XSETFASTINT (temp, FETCH_CHAR (pos)); | |
1091 | } | |
35692fe0 | 1092 | else |
ec1c14f6 | 1093 | XSETFASTINT (temp, FETCH_BYTE (PT_BYTE - 1)); |
35692fe0 JB |
1094 | return temp; |
1095 | } | |
1096 | ||
a7ca3326 | 1097 | DEFUN ("bobp", Fbobp, Sbobp, 0, 0, 0, |
7ee72033 MB |
1098 | doc: /* Return t if point is at the beginning of the buffer. |
1099 | If the buffer is narrowed, this means the beginning of the narrowed part. */) | |
5842a27b | 1100 | (void) |
35692fe0 | 1101 | { |
6ec8bbd2 | 1102 | if (PT == BEGV) |
35692fe0 JB |
1103 | return Qt; |
1104 | return Qnil; | |
1105 | } | |
1106 | ||
a7ca3326 | 1107 | DEFUN ("eobp", Feobp, Seobp, 0, 0, 0, |
7ee72033 MB |
1108 | doc: /* Return t if point is at the end of the buffer. |
1109 | If the buffer is narrowed, this means the end of the narrowed part. */) | |
5842a27b | 1110 | (void) |
35692fe0 | 1111 | { |
6ec8bbd2 | 1112 | if (PT == ZV) |
35692fe0 JB |
1113 | return Qt; |
1114 | return Qnil; | |
1115 | } | |
1116 | ||
a7ca3326 | 1117 | DEFUN ("bolp", Fbolp, Sbolp, 0, 0, 0, |
7ee72033 | 1118 | doc: /* Return t if point is at the beginning of a line. */) |
5842a27b | 1119 | (void) |
35692fe0 | 1120 | { |
ec1c14f6 | 1121 | if (PT == BEGV || FETCH_BYTE (PT_BYTE - 1) == '\n') |
35692fe0 JB |
1122 | return Qt; |
1123 | return Qnil; | |
1124 | } | |
1125 | ||
a7ca3326 | 1126 | DEFUN ("eolp", Feolp, Seolp, 0, 0, 0, |
7ee72033 MB |
1127 | doc: /* Return t if point is at the end of a line. |
1128 | `End of a line' includes point being at the end of the buffer. */) | |
5842a27b | 1129 | (void) |
35692fe0 | 1130 | { |
ec1c14f6 | 1131 | if (PT == ZV || FETCH_BYTE (PT_BYTE) == '\n') |
35692fe0 JB |
1132 | return Qt; |
1133 | return Qnil; | |
1134 | } | |
1135 | ||
a7ca3326 | 1136 | DEFUN ("char-after", Fchar_after, Schar_after, 0, 1, 0, |
7ee72033 | 1137 | doc: /* Return character in current buffer at position POS. |
f555f8cf | 1138 | POS is an integer or a marker and defaults to point. |
7ee72033 | 1139 | If POS is out of range, the value is nil. */) |
5842a27b | 1140 | (Lisp_Object pos) |
35692fe0 | 1141 | { |
d311d28c | 1142 | register ptrdiff_t pos_byte; |
35692fe0 | 1143 | |
fa1d3816 | 1144 | if (NILP (pos)) |
39a4c932 RS |
1145 | { |
1146 | pos_byte = PT_BYTE; | |
3c52e568 | 1147 | XSETFASTINT (pos, PT); |
39a4c932 RS |
1148 | } |
1149 | ||
1150 | if (MARKERP (pos)) | |
85cac557 RS |
1151 | { |
1152 | pos_byte = marker_byte_position (pos); | |
1153 | if (pos_byte < BEGV_BYTE || pos_byte >= ZV_BYTE) | |
1154 | return Qnil; | |
1155 | } | |
fa1d3816 RS |
1156 | else |
1157 | { | |
b7826503 | 1158 | CHECK_NUMBER_COERCE_MARKER (pos); |
b98ef0dc | 1159 | if (XINT (pos) < BEGV || XINT (pos) >= ZV) |
85cac557 | 1160 | return Qnil; |
34a7a267 | 1161 | |
ec1c14f6 | 1162 | pos_byte = CHAR_TO_BYTE (XINT (pos)); |
fa1d3816 | 1163 | } |
35692fe0 | 1164 | |
ec1c14f6 | 1165 | return make_number (FETCH_CHAR (pos_byte)); |
35692fe0 | 1166 | } |
fb8106e8 | 1167 | |
fa1d3816 | 1168 | DEFUN ("char-before", Fchar_before, Schar_before, 0, 1, 0, |
7ee72033 | 1169 | doc: /* Return character in current buffer preceding position POS. |
f555f8cf | 1170 | POS is an integer or a marker and defaults to point. |
7ee72033 | 1171 | If POS is out of range, the value is nil. */) |
5842a27b | 1172 | (Lisp_Object pos) |
fb8106e8 KH |
1173 | { |
1174 | register Lisp_Object val; | |
d311d28c | 1175 | register ptrdiff_t pos_byte; |
fb8106e8 | 1176 | |
fa1d3816 | 1177 | if (NILP (pos)) |
39a4c932 RS |
1178 | { |
1179 | pos_byte = PT_BYTE; | |
3c52e568 | 1180 | XSETFASTINT (pos, PT); |
39a4c932 RS |
1181 | } |
1182 | ||
1183 | if (MARKERP (pos)) | |
85cac557 RS |
1184 | { |
1185 | pos_byte = marker_byte_position (pos); | |
1186 | ||
1187 | if (pos_byte <= BEGV_BYTE || pos_byte > ZV_BYTE) | |
1188 | return Qnil; | |
1189 | } | |
fa1d3816 RS |
1190 | else |
1191 | { | |
b7826503 | 1192 | CHECK_NUMBER_COERCE_MARKER (pos); |
fb8106e8 | 1193 | |
b98ef0dc | 1194 | if (XINT (pos) <= BEGV || XINT (pos) > ZV) |
85cac557 RS |
1195 | return Qnil; |
1196 | ||
ec1c14f6 | 1197 | pos_byte = CHAR_TO_BYTE (XINT (pos)); |
fa1d3816 | 1198 | } |
fb8106e8 | 1199 | |
4b4deea2 | 1200 | if (!NILP (BVAR (current_buffer, enable_multibyte_characters))) |
fb8106e8 | 1201 | { |
ec1c14f6 RS |
1202 | DEC_POS (pos_byte); |
1203 | XSETFASTINT (val, FETCH_CHAR (pos_byte)); | |
fb8106e8 KH |
1204 | } |
1205 | else | |
1206 | { | |
ec1c14f6 RS |
1207 | pos_byte--; |
1208 | XSETFASTINT (val, FETCH_BYTE (pos_byte)); | |
fb8106e8 KH |
1209 | } |
1210 | return val; | |
1211 | } | |
35692fe0 | 1212 | \f |
a7ca3326 | 1213 | DEFUN ("user-login-name", Fuser_login_name, Suser_login_name, 0, 1, 0, |
7ee72033 | 1214 | doc: /* Return the name under which the user logged in, as a string. |
a1f17501 | 1215 | This is based on the effective uid, not the real uid. |
412f1fab | 1216 | Also, if the environment variables LOGNAME or USER are set, |
a1f17501 PJ |
1217 | that determines the value of this function. |
1218 | ||
7b1c38a4 EZ |
1219 | If optional argument UID is an integer or a float, return the login name |
1220 | of the user with that uid, or nil if there is no such user. */) | |
5842a27b | 1221 | (Lisp_Object uid) |
35692fe0 | 1222 | { |
87485d6f | 1223 | struct passwd *pw; |
7b1c38a4 | 1224 | uid_t id; |
87485d6f | 1225 | |
f8a0e364 RS |
1226 | /* Set up the user name info if we didn't do it before. |
1227 | (That can happen if Emacs is dumpable | |
1228 | but you decide to run `temacs -l loadup' and not dump. */ | |
35b34f72 | 1229 | if (INTEGERP (Vuser_login_name)) |
f8a0e364 | 1230 | init_editfns (); |
87485d6f MW |
1231 | |
1232 | if (NILP (uid)) | |
35b34f72 | 1233 | return Vuser_login_name; |
87485d6f | 1234 | |
3f4eabd1 | 1235 | CONS_TO_INTEGER (uid, uid_t, id); |
4d7e6e51 | 1236 | block_input (); |
63c5d10b | 1237 | pw = getpwuid (id); |
4d7e6e51 | 1238 | unblock_input (); |
87485d6f | 1239 | return (pw ? build_string (pw->pw_name) : Qnil); |
35692fe0 JB |
1240 | } |
1241 | ||
1242 | DEFUN ("user-real-login-name", Fuser_real_login_name, Suser_real_login_name, | |
deb8e082 | 1243 | 0, 0, 0, |
7ee72033 | 1244 | doc: /* Return the name of the user's real uid, as a string. |
a1f17501 | 1245 | This ignores the environment variables LOGNAME and USER, so it differs from |
7ee72033 | 1246 | `user-login-name' when running under `su'. */) |
5842a27b | 1247 | (void) |
35692fe0 | 1248 | { |
f8a0e364 RS |
1249 | /* Set up the user name info if we didn't do it before. |
1250 | (That can happen if Emacs is dumpable | |
1251 | but you decide to run `temacs -l loadup' and not dump. */ | |
35b34f72 | 1252 | if (INTEGERP (Vuser_login_name)) |
f8a0e364 | 1253 | init_editfns (); |
35b34f72 | 1254 | return Vuser_real_login_name; |
35692fe0 JB |
1255 | } |
1256 | ||
1257 | DEFUN ("user-uid", Fuser_uid, Suser_uid, 0, 0, 0, | |
7ee72033 | 1258 | doc: /* Return the effective uid of Emacs. |
e00553bf | 1259 | Value is an integer or a float, depending on the value. */) |
5842a27b | 1260 | (void) |
35692fe0 | 1261 | { |
d311d28c | 1262 | uid_t euid = geteuid (); |
3aef3c0a | 1263 | return make_fixnum_or_float (euid); |
35692fe0 JB |
1264 | } |
1265 | ||
1266 | DEFUN ("user-real-uid", Fuser_real_uid, Suser_real_uid, 0, 0, 0, | |
7ee72033 | 1267 | doc: /* Return the real uid of Emacs. |
e00553bf | 1268 | Value is an integer or a float, depending on the value. */) |
5842a27b | 1269 | (void) |
35692fe0 | 1270 | { |
d311d28c | 1271 | uid_t uid = getuid (); |
3aef3c0a | 1272 | return make_fixnum_or_float (uid); |
35692fe0 JB |
1273 | } |
1274 | ||
c9ed721d | 1275 | DEFUN ("user-full-name", Fuser_full_name, Suser_full_name, 0, 1, 0, |
7ee72033 | 1276 | doc: /* Return the full name of the user logged in, as a string. |
a1f17501 PJ |
1277 | If the full name corresponding to Emacs's userid is not known, |
1278 | return "unknown". | |
1279 | ||
1280 | If optional argument UID is an integer or float, return the full name | |
1281 | of the user with that uid, or nil if there is no such user. | |
1282 | If UID is a string, return the full name of the user with that login | |
7ee72033 | 1283 | name, or nil if there is no such user. */) |
5842a27b | 1284 | (Lisp_Object uid) |
35692fe0 | 1285 | { |
c9ed721d | 1286 | struct passwd *pw; |
e7f8264d | 1287 | register char *p, *q; |
3415b0e9 | 1288 | Lisp_Object full; |
c9ed721d RS |
1289 | |
1290 | if (NILP (uid)) | |
34a7a267 | 1291 | return Vuser_full_name; |
3415b0e9 | 1292 | else if (NUMBERP (uid)) |
b91834c3 | 1293 | { |
3f4eabd1 PE |
1294 | uid_t u; |
1295 | CONS_TO_INTEGER (uid, uid_t, u); | |
4d7e6e51 | 1296 | block_input (); |
63c5d10b | 1297 | pw = getpwuid (u); |
4d7e6e51 | 1298 | unblock_input (); |
b91834c3 | 1299 | } |
34a7a267 | 1300 | else if (STRINGP (uid)) |
b91834c3 | 1301 | { |
4d7e6e51 | 1302 | block_input (); |
63c5d10b | 1303 | pw = getpwnam (SSDATA (uid)); |
4d7e6e51 | 1304 | unblock_input (); |
b91834c3 | 1305 | } |
3415b0e9 RS |
1306 | else |
1307 | error ("Invalid UID specification"); | |
c9ed721d | 1308 | |
3415b0e9 | 1309 | if (!pw) |
3347526c | 1310 | return Qnil; |
34a7a267 | 1311 | |
e7f8264d | 1312 | p = USER_FULL_NAME; |
3415b0e9 | 1313 | /* Chop off everything after the first comma. */ |
e7f8264d | 1314 | q = strchr (p, ','); |
3415b0e9 | 1315 | full = make_string (p, q ? q - p : strlen (p)); |
34a7a267 | 1316 | |
3415b0e9 | 1317 | #ifdef AMPERSAND_FULL_NAME |
e7f8264d PE |
1318 | p = SSDATA (full); |
1319 | q = strchr (p, '&'); | |
3415b0e9 RS |
1320 | /* Substitute the login name for the &, upcasing the first character. */ |
1321 | if (q) | |
1322 | { | |
e7f8264d | 1323 | register char *r; |
3415b0e9 RS |
1324 | Lisp_Object login; |
1325 | ||
1326 | login = Fuser_login_name (make_number (pw->pw_uid)); | |
38182d90 | 1327 | r = alloca (strlen (p) + SCHARS (login) + 1); |
72af86bd | 1328 | memcpy (r, p, q - p); |
3415b0e9 | 1329 | r[q - p] = 0; |
42a5b22f | 1330 | strcat (r, SSDATA (login)); |
5da9919f | 1331 | r[q - p] = upcase ((unsigned char) r[q - p]); |
3415b0e9 RS |
1332 | strcat (r, q + 1); |
1333 | full = build_string (r); | |
1334 | } | |
1335 | #endif /* AMPERSAND_FULL_NAME */ | |
1336 | ||
1337 | return full; | |
35692fe0 JB |
1338 | } |
1339 | ||
a7ca3326 | 1340 | DEFUN ("system-name", Fsystem_name, Ssystem_name, 0, 0, 0, |
1a7e0117 | 1341 | doc: /* Return the host name of the machine you are running on, as a string. */) |
5842a27b | 1342 | (void) |
35692fe0 JB |
1343 | { |
1344 | return Vsystem_name; | |
1345 | } | |
1346 | ||
7fd233b3 | 1347 | DEFUN ("emacs-pid", Femacs_pid, Semacs_pid, 0, 0, 0, |
d311d28c | 1348 | doc: /* Return the process ID of Emacs, as a number. */) |
5842a27b | 1349 | (void) |
7fd233b3 | 1350 | { |
d311d28c PE |
1351 | pid_t pid = getpid (); |
1352 | return make_fixnum_or_float (pid); | |
7fd233b3 RS |
1353 | } |
1354 | ||
b8d9bd41 PE |
1355 | \f |
1356 | ||
1357 | #ifndef TIME_T_MIN | |
1358 | # define TIME_T_MIN TYPE_MINIMUM (time_t) | |
1359 | #endif | |
1360 | #ifndef TIME_T_MAX | |
1361 | # define TIME_T_MAX TYPE_MAXIMUM (time_t) | |
1362 | #endif | |
1363 | ||
1364 | /* Report that a time value is out of range for Emacs. */ | |
d35af63c | 1365 | void |
b8d9bd41 PE |
1366 | time_overflow (void) |
1367 | { | |
1368 | error ("Specified time is not representable"); | |
1369 | } | |
1370 | ||
d35af63c | 1371 | /* Return the upper part of the time T (everything but the bottom 16 bits). */ |
b8d9bd41 PE |
1372 | static EMACS_INT |
1373 | hi_time (time_t t) | |
1374 | { | |
1375 | time_t hi = t >> 16; | |
313c1e54 PE |
1376 | |
1377 | /* Check for overflow, helping the compiler for common cases where | |
1378 | no runtime check is needed, and taking care not to convert | |
1379 | negative numbers to unsigned before comparing them. */ | |
1380 | if (! ((! TYPE_SIGNED (time_t) | |
1381 | || MOST_NEGATIVE_FIXNUM <= TIME_T_MIN >> 16 | |
1382 | || MOST_NEGATIVE_FIXNUM <= hi) | |
1383 | && (TIME_T_MAX >> 16 <= MOST_POSITIVE_FIXNUM | |
1384 | || hi <= MOST_POSITIVE_FIXNUM))) | |
b8d9bd41 | 1385 | time_overflow (); |
313c1e54 | 1386 | |
b8d9bd41 PE |
1387 | return hi; |
1388 | } | |
1389 | ||
1390 | /* Return the bottom 16 bits of the time T. */ | |
d311d28c | 1391 | static int |
b8d9bd41 PE |
1392 | lo_time (time_t t) |
1393 | { | |
1394 | return t & ((1 << 16) - 1); | |
1395 | } | |
1396 | ||
a7ca3326 | 1397 | DEFUN ("current-time", Fcurrent_time, Scurrent_time, 0, 0, 0, |
7ee72033 | 1398 | doc: /* Return the current time, as the number of seconds since 1970-01-01 00:00:00. |
d35af63c PE |
1399 | The time is returned as a list of integers (HIGH LOW USEC PSEC). |
1400 | HIGH has the most significant bits of the seconds, while LOW has the | |
1401 | least significant 16 bits. USEC and PSEC are the microsecond and | |
1402 | picosecond counts. */) | |
5842a27b | 1403 | (void) |
d940e0e4 | 1404 | { |
e9a9ae03 | 1405 | return make_lisp_time (current_emacs_time ()); |
d940e0e4 | 1406 | } |
4211ee7d | 1407 | |
a7ca3326 | 1408 | DEFUN ("get-internal-run-time", Fget_internal_run_time, Sget_internal_run_time, |
4211ee7d EZ |
1409 | 0, 0, 0, |
1410 | doc: /* Return the current run time used by Emacs. | |
d35af63c PE |
1411 | The time is returned as a list (HIGH LOW USEC PSEC), using the same |
1412 | style as (current-time). | |
4211ee7d | 1413 | |
9671c13a | 1414 | On systems that can't determine the run time, `get-internal-run-time' |
d35af63c | 1415 | does the same thing as `current-time'. */) |
5842a27b | 1416 | (void) |
4211ee7d EZ |
1417 | { |
1418 | #ifdef HAVE_GETRUSAGE | |
1419 | struct rusage usage; | |
b8d9bd41 PE |
1420 | time_t secs; |
1421 | int usecs; | |
4211ee7d EZ |
1422 | |
1423 | if (getrusage (RUSAGE_SELF, &usage) < 0) | |
1424 | /* This shouldn't happen. What action is appropriate? */ | |
8a0ff744 | 1425 | xsignal0 (Qerror); |
4211ee7d EZ |
1426 | |
1427 | /* Sum up user time and system time. */ | |
1428 | secs = usage.ru_utime.tv_sec + usage.ru_stime.tv_sec; | |
1429 | usecs = usage.ru_utime.tv_usec + usage.ru_stime.tv_usec; | |
1430 | if (usecs >= 1000000) | |
1431 | { | |
1432 | usecs -= 1000000; | |
1433 | secs++; | |
1434 | } | |
e9a9ae03 | 1435 | return make_lisp_time (make_emacs_time (secs, usecs * 1000)); |
c433c134 | 1436 | #else /* ! HAVE_GETRUSAGE */ |
43db14bb | 1437 | #ifdef WINDOWSNT |
c433c134 JR |
1438 | return w32_get_internal_run_time (); |
1439 | #else /* ! WINDOWSNT */ | |
4211ee7d | 1440 | return Fcurrent_time (); |
c433c134 JR |
1441 | #endif /* WINDOWSNT */ |
1442 | #endif /* HAVE_GETRUSAGE */ | |
4211ee7d | 1443 | } |
d940e0e4 JB |
1444 | \f |
1445 | ||
d35af63c PE |
1446 | /* Make a Lisp list that represents the time T with fraction TAIL. */ |
1447 | static Lisp_Object | |
1448 | make_time_tail (time_t t, Lisp_Object tail) | |
1449 | { | |
1450 | return Fcons (make_number (hi_time (t)), | |
1451 | Fcons (make_number (lo_time (t)), tail)); | |
1452 | } | |
1453 | ||
1454 | /* Make a Lisp list that represents the system time T. */ | |
1455 | static Lisp_Object | |
8be6f318 PE |
1456 | make_time (time_t t) |
1457 | { | |
d35af63c PE |
1458 | return make_time_tail (t, Qnil); |
1459 | } | |
1460 | ||
1461 | /* Make a Lisp list that represents the Emacs time T. T may be an | |
1462 | invalid time, with a slightly negative tv_nsec value such as | |
1463 | UNKNOWN_MODTIME_NSECS; in that case, the Lisp list contains a | |
1464 | correspondingly negative picosecond count. */ | |
1465 | Lisp_Object | |
1466 | make_lisp_time (EMACS_TIME t) | |
1467 | { | |
1468 | int ns = EMACS_NSECS (t); | |
1469 | return make_time_tail (EMACS_SECS (t), | |
1470 | list2 (make_number (ns / 1000), | |
1471 | make_number (ns % 1000 * 1000))); | |
8be6f318 PE |
1472 | } |
1473 | ||
1474 | /* Decode a Lisp list SPECIFIED_TIME that represents a time. | |
d35af63c | 1475 | Set *PHIGH, *PLOW, *PUSEC, *PPSEC to its parts; do not check their values. |
a08d4ba7 PE |
1476 | Return true if successful. */ |
1477 | static bool | |
d35af63c PE |
1478 | disassemble_lisp_time (Lisp_Object specified_time, Lisp_Object *phigh, |
1479 | Lisp_Object *plow, Lisp_Object *pusec, | |
1480 | Lisp_Object *ppsec) | |
1481 | { | |
1482 | if (CONSP (specified_time)) | |
1483 | { | |
1484 | Lisp_Object low = XCDR (specified_time); | |
1485 | Lisp_Object usec = make_number (0); | |
1486 | Lisp_Object psec = make_number (0); | |
1487 | if (CONSP (low)) | |
1488 | { | |
1489 | Lisp_Object low_tail = XCDR (low); | |
1490 | low = XCAR (low); | |
1491 | if (CONSP (low_tail)) | |
1492 | { | |
1493 | usec = XCAR (low_tail); | |
1494 | low_tail = XCDR (low_tail); | |
1495 | if (CONSP (low_tail)) | |
1496 | psec = XCAR (low_tail); | |
1497 | } | |
1498 | else if (!NILP (low_tail)) | |
1499 | usec = low_tail; | |
1500 | } | |
1501 | ||
1502 | *phigh = XCAR (specified_time); | |
1503 | *plow = low; | |
1504 | *pusec = usec; | |
1505 | *ppsec = psec; | |
1506 | return 1; | |
1507 | } | |
1508 | ||
1509 | return 0; | |
1510 | } | |
1511 | ||
1512 | /* From the time components HIGH, LOW, USEC and PSEC taken from a Lisp | |
31571fd7 PE |
1513 | list, generate the corresponding time value. |
1514 | ||
1515 | If RESULT is not null, store into *RESULT the converted time; | |
1516 | this can fail if the converted time does not fit into EMACS_TIME. | |
1517 | If *DRESULT is not null, store into *DRESULT the number of | |
1518 | seconds since the start of the POSIX Epoch. | |
1519 | ||
a08d4ba7 PE |
1520 | Return true if successful. */ |
1521 | bool | |
d35af63c | 1522 | decode_time_components (Lisp_Object high, Lisp_Object low, Lisp_Object usec, |
31571fd7 PE |
1523 | Lisp_Object psec, |
1524 | EMACS_TIME *result, double *dresult) | |
e3120ab5 | 1525 | { |
d35af63c | 1526 | EMACS_INT hi, lo, us, ps; |
d35af63c PE |
1527 | if (! (INTEGERP (high) && INTEGERP (low) |
1528 | && INTEGERP (usec) && INTEGERP (psec))) | |
1529 | return 0; | |
1530 | hi = XINT (high); | |
1531 | lo = XINT (low); | |
1532 | us = XINT (usec); | |
1533 | ps = XINT (psec); | |
1534 | ||
1535 | /* Normalize out-of-range lower-order components by carrying | |
1536 | each overflow into the next higher-order component. */ | |
1537 | us += ps / 1000000 - (ps % 1000000 < 0); | |
1538 | lo += us / 1000000 - (us % 1000000 < 0); | |
1539 | hi += lo >> 16; | |
1540 | ps = ps % 1000000 + 1000000 * (ps % 1000000 < 0); | |
1541 | us = us % 1000000 + 1000000 * (us % 1000000 < 0); | |
1542 | lo &= (1 << 16) - 1; | |
1543 | ||
31571fd7 PE |
1544 | if (result) |
1545 | { | |
1546 | if ((TYPE_SIGNED (time_t) ? TIME_T_MIN >> 16 <= hi : 0 <= hi) | |
1547 | && hi <= TIME_T_MAX >> 16) | |
1548 | { | |
1549 | /* Return the greatest representable time that is not greater | |
1550 | than the requested time. */ | |
1551 | time_t sec = hi; | |
e9a9ae03 | 1552 | *result = make_emacs_time ((sec << 16) + lo, us * 1000 + ps / 1000); |
31571fd7 PE |
1553 | } |
1554 | else | |
1555 | { | |
1556 | /* Overflow in the highest-order component. */ | |
1557 | return 0; | |
1558 | } | |
1559 | } | |
1560 | ||
1561 | if (dresult) | |
1562 | *dresult = (us * 1e6 + ps) / 1e12 + lo + hi * 65536.0; | |
d35af63c | 1563 | |
d35af63c PE |
1564 | return 1; |
1565 | } | |
1566 | ||
1567 | /* Decode a Lisp list SPECIFIED_TIME that represents a time. | |
1568 | If SPECIFIED_TIME is nil, use the current time. | |
31571fd7 PE |
1569 | |
1570 | Round the time down to the nearest EMACS_TIME value. | |
d35af63c PE |
1571 | Return seconds since the Epoch. |
1572 | Signal an error if unsuccessful. */ | |
1573 | EMACS_TIME | |
31571fd7 | 1574 | lisp_time_argument (Lisp_Object specified_time) |
d35af63c PE |
1575 | { |
1576 | EMACS_TIME t; | |
e3120ab5 | 1577 | if (NILP (specified_time)) |
e9a9ae03 | 1578 | t = current_emacs_time (); |
d35af63c | 1579 | else |
34a7a267 | 1580 | { |
d35af63c PE |
1581 | Lisp_Object high, low, usec, psec; |
1582 | if (! (disassemble_lisp_time (specified_time, &high, &low, &usec, &psec) | |
31571fd7 | 1583 | && decode_time_components (high, low, usec, psec, &t, 0))) |
d35af63c | 1584 | error ("Invalid time specification"); |
34a7a267 | 1585 | } |
d35af63c PE |
1586 | return t; |
1587 | } | |
1588 | ||
1589 | /* Like lisp_time_argument, except decode only the seconds part, | |
31571fd7 PE |
1590 | do not allow out-of-range time stamps, do not check the subseconds part, |
1591 | and always round down. */ | |
d35af63c PE |
1592 | static time_t |
1593 | lisp_seconds_argument (Lisp_Object specified_time) | |
1594 | { | |
1595 | if (NILP (specified_time)) | |
1596 | return time (NULL); | |
e3120ab5 JB |
1597 | else |
1598 | { | |
d35af63c PE |
1599 | Lisp_Object high, low, usec, psec; |
1600 | EMACS_TIME t; | |
1601 | if (! (disassemble_lisp_time (specified_time, &high, &low, &usec, &psec) | |
1602 | && decode_time_components (high, low, make_number (0), | |
1603 | make_number (0), &t, 0))) | |
1604 | error ("Invalid time specification"); | |
1605 | return EMACS_SECS (t); | |
e3120ab5 JB |
1606 | } |
1607 | } | |
1608 | ||
34a7a267 | 1609 | DEFUN ("float-time", Ffloat_time, Sfloat_time, 0, 1, 0, |
7ee72033 | 1610 | doc: /* Return the current time, as a float number of seconds since the epoch. |
412f1fab | 1611 | If SPECIFIED-TIME is given, it is the time to convert to float |
5668fbb8 | 1612 | instead of the current time. The argument should have the form |
d35af63c PE |
1613 | (HIGH LOW) or (HIGH LOW USEC) or (HIGH LOW USEC PSEC). Thus, |
1614 | you can use times from `current-time' and from `file-attributes'. | |
1615 | SPECIFIED-TIME can also have the form (HIGH . LOW), but this is | |
1616 | considered obsolete. | |
a1f17501 PJ |
1617 | |
1618 | WARNING: Since the result is floating point, it may not be exact. | |
d427a9fa EZ |
1619 | If precise time stamps are required, use either `current-time', |
1620 | or (if you need time as a string) `format-time-string'. */) | |
5842a27b | 1621 | (Lisp_Object specified_time) |
34a7a267 | 1622 | { |
31571fd7 PE |
1623 | double t; |
1624 | if (NILP (specified_time)) | |
1625 | { | |
e9a9ae03 | 1626 | EMACS_TIME now = current_emacs_time (); |
31571fd7 PE |
1627 | t = EMACS_SECS (now) + EMACS_NSECS (now) / 1e9; |
1628 | } | |
1629 | else | |
1630 | { | |
1631 | Lisp_Object high, low, usec, psec; | |
1632 | if (! (disassemble_lisp_time (specified_time, &high, &low, &usec, &psec) | |
1633 | && decode_time_components (high, low, usec, psec, 0, &t))) | |
1634 | error ("Invalid time specification"); | |
1635 | } | |
1636 | return make_float (t); | |
34a7a267 SS |
1637 | } |
1638 | ||
70ebbe5f PE |
1639 | /* Write information into buffer S of size MAXSIZE, according to the |
1640 | FORMAT of length FORMAT_LEN, using time information taken from *TP. | |
a08d4ba7 | 1641 | Default to Universal Time if UT, local time otherwise. |
a4180391 | 1642 | Use NS as the number of nanoseconds in the %N directive. |
70ebbe5f PE |
1643 | Return the number of bytes written, not including the terminating |
1644 | '\0'. If S is NULL, nothing will be written anywhere; so to | |
1645 | determine how many bytes would be written, use NULL for S and | |
1646 | ((size_t) -1) for MAXSIZE. | |
1647 | ||
16c3e636 PE |
1648 | This function behaves like nstrftime, except it allows null |
1649 | bytes in FORMAT and it does not support nanoseconds. */ | |
70ebbe5f | 1650 | static size_t |
a4180391 | 1651 | emacs_nmemftime (char *s, size_t maxsize, const char *format, |
a08d4ba7 | 1652 | size_t format_len, const struct tm *tp, bool ut, int ns) |
70ebbe5f PE |
1653 | { |
1654 | size_t total = 0; | |
1655 | ||
be09e6e6 PE |
1656 | /* Loop through all the null-terminated strings in the format |
1657 | argument. Normally there's just one null-terminated string, but | |
1658 | there can be arbitrarily many, concatenated together, if the | |
16c3e636 | 1659 | format contains '\0' bytes. nstrftime stops at the first |
be09e6e6 | 1660 | '\0' byte so we must invoke it separately for each such string. */ |
70ebbe5f PE |
1661 | for (;;) |
1662 | { | |
1663 | size_t len; | |
1664 | size_t result; | |
1665 | ||
1666 | if (s) | |
1667 | s[0] = '\1'; | |
1668 | ||
a4180391 | 1669 | result = nstrftime (s, maxsize, format, tp, ut, ns); |
70ebbe5f PE |
1670 | |
1671 | if (s) | |
1672 | { | |
1673 | if (result == 0 && s[0] != '\0') | |
1674 | return 0; | |
1675 | s += result + 1; | |
1676 | } | |
1677 | ||
1678 | maxsize -= result + 1; | |
1679 | total += result; | |
1680 | len = strlen (format); | |
1681 | if (len == format_len) | |
1682 | return total; | |
1683 | total++; | |
1684 | format += len + 1; | |
1685 | format_len -= len + 1; | |
1686 | } | |
1687 | } | |
1688 | ||
3efcc98a | 1689 | DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0, |
7ee72033 | 1690 | doc: /* Use FORMAT-STRING to format the time TIME, or now if omitted. |
d35af63c | 1691 | TIME is specified as (HIGH LOW USEC PSEC), as returned by |
5668fbb8 LT |
1692 | `current-time' or `file-attributes'. The obsolete form (HIGH . LOW) |
1693 | is also still accepted. | |
a1f17501 PJ |
1694 | The third, optional, argument UNIVERSAL, if non-nil, means describe TIME |
1695 | as Universal Time; nil means describe TIME in the local time zone. | |
1696 | The value is a copy of FORMAT-STRING, but with certain constructs replaced | |
1697 | by text that describes the specified date and time in TIME: | |
1698 | ||
1699 | %Y is the year, %y within the century, %C the century. | |
1700 | %G is the year corresponding to the ISO week, %g within the century. | |
1701 | %m is the numeric month. | |
1702 | %b and %h are the locale's abbreviated month name, %B the full name. | |
1703 | %d is the day of the month, zero-padded, %e is blank-padded. | |
1704 | %u is the numeric day of week from 1 (Monday) to 7, %w from 0 (Sunday) to 6. | |
1705 | %a is the locale's abbreviated name of the day of week, %A the full name. | |
1706 | %U is the week number starting on Sunday, %W starting on Monday, | |
1707 | %V according to ISO 8601. | |
1708 | %j is the day of the year. | |
1709 | ||
1710 | %H is the hour on a 24-hour clock, %I is on a 12-hour clock, %k is like %H | |
1711 | only blank-padded, %l is like %I blank-padded. | |
1712 | %p is the locale's equivalent of either AM or PM. | |
1713 | %M is the minute. | |
1714 | %S is the second. | |
a4180391 | 1715 | %N is the nanosecond, %6N the microsecond, %3N the millisecond, etc. |
a1f17501 PJ |
1716 | %Z is the time zone name, %z is the numeric form. |
1717 | %s is the number of seconds since 1970-01-01 00:00:00 +0000. | |
1718 | ||
1719 | %c is the locale's date and time format. | |
1720 | %x is the locale's "preferred" date format. | |
1721 | %D is like "%m/%d/%y". | |
1722 | ||
1723 | %R is like "%H:%M", %T is like "%H:%M:%S", %r is like "%I:%M:%S %p". | |
1724 | %X is the locale's "preferred" time format. | |
1725 | ||
1726 | Finally, %n is a newline, %t is a tab, %% is a literal %. | |
1727 | ||
1728 | Certain flags and modifiers are available with some format controls. | |
1729 | The flags are `_', `-', `^' and `#'. For certain characters X, | |
1730 | %_X is like %X, but padded with blanks; %-X is like %X, | |
a67a233b MR |
1731 | but without padding. %^X is like %X, but with all textual |
1732 | characters up-cased; %#X is like %X, but with letter-case of | |
a1f17501 PJ |
1733 | all textual characters reversed. |
1734 | %NX (where N stands for an integer) is like %X, | |
1735 | but takes up at least N (a number) positions. | |
1736 | The modifiers are `E' and `O'. For certain characters X, | |
1737 | %EX is a locale's alternative version of %X; | |
1738 | %OX is like %X, but uses the locale's number symbols. | |
1739 | ||
75bfc667 JL |
1740 | For example, to produce full ISO 8601 format, use "%Y-%m-%dT%T%z". |
1741 | ||
1742 | usage: (format-time-string FORMAT-STRING &optional TIME UNIVERSAL) */) | |
545b49b4 | 1743 | (Lisp_Object format_string, Lisp_Object timeval, Lisp_Object universal) |
a82d387c | 1744 | { |
31571fd7 | 1745 | EMACS_TIME t = lisp_time_argument (timeval); |
7ed806a7 | 1746 | struct tm tm; |
7da0b018 PE |
1747 | |
1748 | CHECK_STRING (format_string); | |
1749 | format_string = code_convert_string_norecord (format_string, | |
1750 | Vlocale_coding_system, 1); | |
1751 | return format_time_string (SSDATA (format_string), SBYTES (format_string), | |
d35af63c | 1752 | t, ! NILP (universal), &tm); |
7da0b018 PE |
1753 | } |
1754 | ||
1755 | static Lisp_Object | |
1756 | format_time_string (char const *format, ptrdiff_t formatlen, | |
a08d4ba7 | 1757 | EMACS_TIME t, bool ut, struct tm *tmp) |
7da0b018 | 1758 | { |
7ed806a7 PE |
1759 | char buffer[4000]; |
1760 | char *buf = buffer; | |
243e0530 | 1761 | ptrdiff_t size = sizeof buffer; |
7ed806a7 PE |
1762 | size_t len; |
1763 | Lisp_Object bufstring; | |
d35af63c | 1764 | int ns = EMACS_NSECS (t); |
177ea5f1 | 1765 | struct tm *tm; |
7ed806a7 | 1766 | USE_SAFE_ALLOCA; |
a82d387c | 1767 | |
a82d387c RS |
1768 | while (1) |
1769 | { | |
e9a9ae03 | 1770 | time_t *taddr = emacs_secs_addr (&t); |
4d7e6e51 | 1771 | block_input (); |
7ed806a7 PE |
1772 | |
1773 | synchronize_system_time_locale (); | |
1774 | ||
e9a9ae03 | 1775 | tm = ut ? gmtime (taddr) : localtime (taddr); |
7ed806a7 PE |
1776 | if (! tm) |
1777 | { | |
4d7e6e51 | 1778 | unblock_input (); |
7ed806a7 PE |
1779 | time_overflow (); |
1780 | } | |
1781 | *tmp = *tm; | |
b48382a0 | 1782 | |
bfbcc5ee | 1783 | buf[0] = '\1'; |
7ed806a7 PE |
1784 | len = emacs_nmemftime (buf, size, format, formatlen, tm, ut, ns); |
1785 | if ((0 < len && len < size) || (len == 0 && buf[0] == '\0')) | |
1786 | break; | |
b48382a0 | 1787 | |
7ed806a7 PE |
1788 | /* Buffer was too small, so make it bigger and try again. */ |
1789 | len = emacs_nmemftime (NULL, SIZE_MAX, format, formatlen, tm, ut, ns); | |
4d7e6e51 | 1790 | unblock_input (); |
7ed806a7 | 1791 | if (STRING_BYTES_BOUND <= len) |
da64016e | 1792 | string_overflow (); |
7ed806a7 | 1793 | size = len + 1; |
98c6f1e3 | 1794 | buf = SAFE_ALLOCA (size); |
a82d387c | 1795 | } |
7ed806a7 | 1796 | |
4d7e6e51 | 1797 | unblock_input (); |
7ed806a7 PE |
1798 | bufstring = make_unibyte_string (buf, len); |
1799 | SAFE_FREE (); | |
1800 | return code_convert_string_norecord (bufstring, Vlocale_coding_system, 0); | |
a82d387c RS |
1801 | } |
1802 | ||
4691c06d | 1803 | DEFUN ("decode-time", Fdecode_time, Sdecode_time, 0, 1, 0, |
7ee72033 | 1804 | doc: /* Decode a time value as (SEC MINUTE HOUR DAY MONTH YEAR DOW DST ZONE). |
5668fbb8 | 1805 | The optional SPECIFIED-TIME should be a list of (HIGH LOW . IGNORED), |
9671c13a | 1806 | as from `current-time' and `file-attributes', or nil to use the |
5668fbb8 LT |
1807 | current time. The obsolete form (HIGH . LOW) is also still accepted. |
1808 | The list has the following nine members: SEC is an integer between 0 | |
1809 | and 60; SEC is 60 for a leap second, which only some operating systems | |
1810 | support. MINUTE is an integer between 0 and 59. HOUR is an integer | |
1811 | between 0 and 23. DAY is an integer between 1 and 31. MONTH is an | |
1812 | integer between 1 and 12. YEAR is an integer indicating the | |
1813 | four-digit year. DOW is the day of week, an integer between 0 and 6, | |
f1767e2b | 1814 | where 0 is Sunday. DST is t if daylight saving time is in effect, |
5668fbb8 LT |
1815 | otherwise nil. ZONE is an integer indicating the number of seconds |
1816 | east of Greenwich. (Note that Common Lisp has different meanings for | |
1817 | DOW and ZONE.) */) | |
5842a27b | 1818 | (Lisp_Object specified_time) |
4691c06d | 1819 | { |
d35af63c | 1820 | time_t time_spec = lisp_seconds_argument (specified_time); |
3c887943 | 1821 | struct tm save_tm; |
4691c06d RS |
1822 | struct tm *decoded_time; |
1823 | Lisp_Object list_args[9]; | |
34a7a267 | 1824 | |
4d7e6e51 | 1825 | block_input (); |
4691c06d | 1826 | decoded_time = localtime (&time_spec); |
7ed806a7 PE |
1827 | if (decoded_time) |
1828 | save_tm = *decoded_time; | |
4d7e6e51 | 1829 | unblock_input (); |
b8d9bd41 | 1830 | if (! (decoded_time |
7ed806a7 PE |
1831 | && MOST_NEGATIVE_FIXNUM - TM_YEAR_BASE <= save_tm.tm_year |
1832 | && save_tm.tm_year <= MOST_POSITIVE_FIXNUM - TM_YEAR_BASE)) | |
fe31d94c | 1833 | time_overflow (); |
7ed806a7 PE |
1834 | XSETFASTINT (list_args[0], save_tm.tm_sec); |
1835 | XSETFASTINT (list_args[1], save_tm.tm_min); | |
1836 | XSETFASTINT (list_args[2], save_tm.tm_hour); | |
1837 | XSETFASTINT (list_args[3], save_tm.tm_mday); | |
1838 | XSETFASTINT (list_args[4], save_tm.tm_mon + 1); | |
71c3f28f EZ |
1839 | /* On 64-bit machines an int is narrower than EMACS_INT, thus the |
1840 | cast below avoids overflow in int arithmetics. */ | |
7ed806a7 PE |
1841 | XSETINT (list_args[5], TM_YEAR_BASE + (EMACS_INT) save_tm.tm_year); |
1842 | XSETFASTINT (list_args[6], save_tm.tm_wday); | |
1843 | list_args[7] = save_tm.tm_isdst ? Qt : Qnil; | |
3c887943 | 1844 | |
4d7e6e51 | 1845 | block_input (); |
3c887943 KH |
1846 | decoded_time = gmtime (&time_spec); |
1847 | if (decoded_time == 0) | |
1848 | list_args[8] = Qnil; | |
1849 | else | |
94751666 | 1850 | XSETINT (list_args[8], tm_diff (&save_tm, decoded_time)); |
4d7e6e51 | 1851 | unblock_input (); |
4691c06d RS |
1852 | return Flist (9, list_args); |
1853 | } | |
1854 | ||
b8d9bd41 PE |
1855 | /* Return OBJ - OFFSET, checking that OBJ is a valid fixnum and that |
1856 | the result is representable as an int. Assume OFFSET is small and | |
1857 | nonnegative. */ | |
1858 | static int | |
1859 | check_tm_member (Lisp_Object obj, int offset) | |
1860 | { | |
1861 | EMACS_INT n; | |
1862 | CHECK_NUMBER (obj); | |
1863 | n = XINT (obj); | |
1864 | if (! (INT_MIN + offset <= n && n - offset <= INT_MAX)) | |
1865 | time_overflow (); | |
1866 | return n - offset; | |
1867 | } | |
1868 | ||
6ee9061c | 1869 | DEFUN ("encode-time", Fencode_time, Sencode_time, 6, MANY, 0, |
7ee72033 | 1870 | doc: /* Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time. |
a1f17501 PJ |
1871 | This is the reverse operation of `decode-time', which see. |
1872 | ZONE defaults to the current time zone rule. This can | |
1873 | be a string or t (as from `set-time-zone-rule'), or it can be a list | |
b57c2708 | 1874 | \(as from `current-time-zone') or an integer (as from `decode-time') |
9c279ddf | 1875 | applied without consideration for daylight saving time. |
a1f17501 PJ |
1876 | |
1877 | You can pass more than 7 arguments; then the first six arguments | |
1878 | are used as SECOND through YEAR, and the *last* argument is used as ZONE. | |
1879 | The intervening arguments are ignored. | |
1880 | This feature lets (apply 'encode-time (decode-time ...)) work. | |
1881 | ||
412f1fab | 1882 | Out-of-range values for SECOND, MINUTE, HOUR, DAY, or MONTH are allowed; |
a1f17501 PJ |
1883 | for example, a DAY of 0 means the day preceding the given month. |
1884 | Year numbers less than 100 are treated just like other year numbers. | |
4bfbe194 MB |
1885 | If you want them to stand for years in this century, you must do that yourself. |
1886 | ||
f555f8cf KH |
1887 | Years before 1970 are not guaranteed to work. On some systems, |
1888 | year values as low as 1901 do work. | |
1889 | ||
4bfbe194 | 1890 | usage: (encode-time SECOND MINUTE HOUR DAY MONTH YEAR &optional ZONE) */) |
f66c7cf8 | 1891 | (ptrdiff_t nargs, Lisp_Object *args) |
cce7b8a0 | 1892 | { |
545b49b4 | 1893 | time_t value; |
c59b5089 | 1894 | struct tm tm; |
60653898 | 1895 | Lisp_Object zone = (nargs > 6 ? args[nargs - 1] : Qnil); |
6ee9061c | 1896 | |
b8d9bd41 PE |
1897 | tm.tm_sec = check_tm_member (args[0], 0); |
1898 | tm.tm_min = check_tm_member (args[1], 0); | |
1899 | tm.tm_hour = check_tm_member (args[2], 0); | |
1900 | tm.tm_mday = check_tm_member (args[3], 0); | |
1901 | tm.tm_mon = check_tm_member (args[4], 1); | |
1902 | tm.tm_year = check_tm_member (args[5], TM_YEAR_BASE); | |
c59b5089 PE |
1903 | tm.tm_isdst = -1; |
1904 | ||
1905 | if (CONSP (zone)) | |
7d7bbefd | 1906 | zone = XCAR (zone); |
1b8fa736 | 1907 | if (NILP (zone)) |
bcda42c8 | 1908 | { |
4d7e6e51 | 1909 | block_input (); |
545b49b4 | 1910 | value = mktime (&tm); |
4d7e6e51 | 1911 | unblock_input (); |
bcda42c8 | 1912 | } |
c59b5089 | 1913 | else |
1b8fa736 | 1914 | { |
5745a7df PE |
1915 | static char const tzbuf_format[] = "XXX%s%"pI"d:%02d:%02d"; |
1916 | char tzbuf[sizeof tzbuf_format + INT_STRLEN_BOUND (EMACS_INT)]; | |
1917 | char *old_tzstring; | |
8ea90aa3 | 1918 | const char *tzstring; |
5745a7df | 1919 | USE_SAFE_ALLOCA; |
34a7a267 | 1920 | |
2e34157c | 1921 | if (EQ (zone, Qt)) |
085e9fcb EN |
1922 | tzstring = "UTC0"; |
1923 | else if (STRINGP (zone)) | |
51b59d79 | 1924 | tzstring = SSDATA (zone); |
c59b5089 | 1925 | else if (INTEGERP (zone)) |
1b8fa736 | 1926 | { |
d311d28c PE |
1927 | EMACS_INT abszone = eabs (XINT (zone)); |
1928 | EMACS_INT zone_hr = abszone / (60*60); | |
1929 | int zone_min = (abszone/60) % 60; | |
1930 | int zone_sec = abszone % 60; | |
5745a7df | 1931 | sprintf (tzbuf, tzbuf_format, "-" + (XINT (zone) < 0), |
d311d28c | 1932 | zone_hr, zone_min, zone_sec); |
c59b5089 | 1933 | tzstring = tzbuf; |
1b8fa736 | 1934 | } |
c59b5089 PE |
1935 | else |
1936 | error ("Invalid time zone specification"); | |
1937 | ||
5745a7df PE |
1938 | old_tzstring = getenv ("TZ"); |
1939 | if (old_tzstring) | |
1940 | { | |
1941 | char *buf = SAFE_ALLOCA (strlen (old_tzstring) + 1); | |
1942 | old_tzstring = strcpy (buf, old_tzstring); | |
1943 | } | |
1944 | ||
4d7e6e51 | 1945 | block_input (); |
7ed806a7 | 1946 | |
34a7a267 | 1947 | /* Set TZ before calling mktime; merely adjusting mktime's returned |
c59b5089 PE |
1948 | value doesn't suffice, since that would mishandle leap seconds. */ |
1949 | set_time_zone_rule (tzstring); | |
1950 | ||
545b49b4 | 1951 | value = mktime (&tm); |
c59b5089 | 1952 | |
5745a7df | 1953 | set_time_zone_rule (old_tzstring); |
c59b5089 PE |
1954 | #ifdef LOCALTIME_CACHE |
1955 | tzset (); | |
1956 | #endif | |
4d7e6e51 | 1957 | unblock_input (); |
5745a7df | 1958 | SAFE_FREE (); |
1b8fa736 | 1959 | } |
1b8fa736 | 1960 | |
545b49b4 | 1961 | if (value == (time_t) -1) |
fe31d94c | 1962 | time_overflow (); |
c59b5089 | 1963 | |
545b49b4 | 1964 | return make_time (value); |
cce7b8a0 RS |
1965 | } |
1966 | ||
2148f2b4 | 1967 | DEFUN ("current-time-string", Fcurrent_time_string, Scurrent_time_string, 0, 1, 0, |
244b023e | 1968 | doc: /* Return the current local time, as a human-readable string. |
a1f17501 | 1969 | Programs can use this function to decode a time, |
d65b4235 PE |
1970 | since the number of columns in each field is fixed |
1971 | if the year is in the range 1000-9999. | |
a1f17501 PJ |
1972 | The format is `Sun Sep 16 01:03:52 1973'. |
1973 | However, see also the functions `decode-time' and `format-time-string' | |
1974 | which provide a much more powerful and general facility. | |
1975 | ||
5668fbb8 LT |
1976 | If SPECIFIED-TIME is given, it is a time to format instead of the |
1977 | current time. The argument should have the form (HIGH LOW . IGNORED). | |
1978 | Thus, you can use times obtained from `current-time' and from | |
1979 | `file-attributes'. SPECIFIED-TIME can also have the form (HIGH . LOW), | |
1980 | but this is considered obsolete. */) | |
5842a27b | 1981 | (Lisp_Object specified_time) |
2148f2b4 | 1982 | { |
d35af63c | 1983 | time_t value = lisp_seconds_argument (specified_time); |
aac18aa4 | 1984 | struct tm *tm; |
ab0fa4e4 PE |
1985 | char buf[sizeof "Mon Apr 30 12:49:17 " + INT_STRLEN_BOUND (int) + 1]; |
1986 | int len IF_LINT (= 0); | |
2148f2b4 | 1987 | |
ab0fa4e4 PE |
1988 | /* Convert to a string in ctime format, except without the trailing |
1989 | newline, and without the 4-digit year limit. Don't use asctime | |
1990 | or ctime, as they might dump core if the year is outside the | |
1991 | range -999 .. 9999. */ | |
4d7e6e51 | 1992 | block_input (); |
aac18aa4 | 1993 | tm = localtime (&value); |
ab0fa4e4 | 1994 | if (tm) |
7ed806a7 | 1995 | { |
ab0fa4e4 PE |
1996 | static char const wday_name[][4] = |
1997 | { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; | |
1998 | static char const mon_name[][4] = | |
1999 | { "Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
2000 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; | |
2001 | printmax_t year_base = TM_YEAR_BASE; | |
2002 | ||
2003 | len = sprintf (buf, "%s %s%3d %02d:%02d:%02d %"pMd, | |
2004 | wday_name[tm->tm_wday], mon_name[tm->tm_mon], tm->tm_mday, | |
2005 | tm->tm_hour, tm->tm_min, tm->tm_sec, | |
2006 | tm->tm_year + year_base); | |
7ed806a7 | 2007 | } |
4d7e6e51 | 2008 | unblock_input (); |
ab0fa4e4 | 2009 | if (! tm) |
fe31d94c | 2010 | time_overflow (); |
35692fe0 | 2011 | |
ab0fa4e4 | 2012 | return make_unibyte_string (buf, len); |
35692fe0 | 2013 | } |
c2662aea | 2014 | |
94751666 PE |
2015 | /* Yield A - B, measured in seconds. |
2016 | This function is copied from the GNU C Library. */ | |
2017 | static int | |
971de7fb | 2018 | tm_diff (struct tm *a, struct tm *b) |
e3120ab5 | 2019 | { |
94751666 PE |
2020 | /* Compute intervening leap days correctly even if year is negative. |
2021 | Take care to avoid int overflow in leap day calculations, | |
2022 | but it's OK to assume that A and B are close to each other. */ | |
2023 | int a4 = (a->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (a->tm_year & 3); | |
2024 | int b4 = (b->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (b->tm_year & 3); | |
2025 | int a100 = a4 / 25 - (a4 % 25 < 0); | |
2026 | int b100 = b4 / 25 - (b4 % 25 < 0); | |
2027 | int a400 = a100 >> 2; | |
2028 | int b400 = b100 >> 2; | |
2029 | int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400); | |
2030 | int years = a->tm_year - b->tm_year; | |
2031 | int days = (365 * years + intervening_leap_days | |
2032 | + (a->tm_yday - b->tm_yday)); | |
2033 | return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour)) | |
2034 | + (a->tm_min - b->tm_min)) | |
8e718b4e | 2035 | + (a->tm_sec - b->tm_sec)); |
e3120ab5 JB |
2036 | } |
2037 | ||
2038 | DEFUN ("current-time-zone", Fcurrent_time_zone, Scurrent_time_zone, 0, 1, 0, | |
7ee72033 | 2039 | doc: /* Return the offset and name for the local time zone. |
a1f17501 PJ |
2040 | This returns a list of the form (OFFSET NAME). |
2041 | OFFSET is an integer number of seconds ahead of UTC (east of Greenwich). | |
2042 | A negative value means west of Greenwich. | |
2043 | NAME is a string giving the name of the time zone. | |
412f1fab | 2044 | If SPECIFIED-TIME is given, the time zone offset is determined from it |
5668fbb8 LT |
2045 | instead of using the current time. The argument should have the form |
2046 | (HIGH LOW . IGNORED). Thus, you can use times obtained from | |
2047 | `current-time' and from `file-attributes'. SPECIFIED-TIME can also | |
2048 | have the form (HIGH . LOW), but this is considered obsolete. | |
a1f17501 PJ |
2049 | |
2050 | Some operating systems cannot provide all this information to Emacs; | |
2051 | in this case, `current-time-zone' returns a list containing nil for | |
7ee72033 | 2052 | the data it can't find. */) |
5842a27b | 2053 | (Lisp_Object specified_time) |
c2662aea | 2054 | { |
d35af63c | 2055 | EMACS_TIME value; |
7ed806a7 | 2056 | int offset; |
e3120ab5 | 2057 | struct tm *t; |
7da0b018 | 2058 | struct tm localtm; |
7da0b018 PE |
2059 | Lisp_Object zone_offset, zone_name; |
2060 | ||
2061 | zone_offset = Qnil; | |
e9a9ae03 | 2062 | value = make_emacs_time (lisp_seconds_argument (specified_time), 0); |
d35af63c | 2063 | zone_name = format_time_string ("%Z", sizeof "%Z" - 1, value, 0, &localtm); |
4d7e6e51 | 2064 | block_input (); |
e9a9ae03 | 2065 | t = gmtime (emacs_secs_addr (&value)); |
7ed806a7 PE |
2066 | if (t) |
2067 | offset = tm_diff (&localtm, t); | |
4d7e6e51 | 2068 | unblock_input (); |
bcda42c8 YM |
2069 | |
2070 | if (t) | |
e3120ab5 | 2071 | { |
7da0b018 PE |
2072 | zone_offset = make_number (offset); |
2073 | if (SCHARS (zone_name) == 0) | |
e3120ab5 JB |
2074 | { |
2075 | /* No local time zone name is available; use "+-NNNN" instead. */ | |
33ef5c64 PE |
2076 | int m = offset / 60; |
2077 | int am = offset < 0 ? - m : m; | |
7da0b018 | 2078 | char buf[sizeof "+00" + INT_STRLEN_BOUND (int)]; |
98c6f1e3 | 2079 | zone_name = make_formatted_string (buf, "%c%02d%02d", |
a8290ec3 DA |
2080 | (offset < 0 ? '-' : '+'), |
2081 | am / 60, am % 60); | |
e3120ab5 | 2082 | } |
e3120ab5 | 2083 | } |
7da0b018 PE |
2084 | |
2085 | return list2 (zone_offset, zone_name); | |
c2662aea JB |
2086 | } |
2087 | ||
143cb9a9 | 2088 | DEFUN ("set-time-zone-rule", Fset_time_zone_rule, Sset_time_zone_rule, 1, 1, 0, |
7ee72033 | 2089 | doc: /* Set the local time zone using TZ, a string specifying a time zone rule. |
a1f17501 | 2090 | If TZ is nil, use implementation-defined default time zone information. |
37e11a63 CY |
2091 | If TZ is t, use Universal Time. |
2092 | ||
2093 | Instead of calling this function, you typically want (setenv "TZ" TZ). | |
2094 | That changes both the environment of the Emacs process and the | |
2095 | variable `process-environment', whereas `set-time-zone-rule' affects | |
2096 | only the former. */) | |
5842a27b | 2097 | (Lisp_Object tz) |
143cb9a9 | 2098 | { |
8ea90aa3 | 2099 | const char *tzstring; |
7ed806a7 PE |
2100 | |
2101 | if (! (NILP (tz) || EQ (tz, Qt))) | |
2102 | CHECK_STRING (tz); | |
2103 | ||
143cb9a9 | 2104 | if (NILP (tz)) |
a03fc5a6 | 2105 | tzstring = initial_tz; |
2e34157c | 2106 | else if (EQ (tz, Qt)) |
085e9fcb | 2107 | tzstring = "UTC0"; |
143cb9a9 | 2108 | else |
7ed806a7 | 2109 | tzstring = SSDATA (tz); |
143cb9a9 | 2110 | |
5745a7df | 2111 | block_input (); |
c59b5089 | 2112 | set_time_zone_rule (tzstring); |
4d7e6e51 | 2113 | unblock_input (); |
7ed806a7 | 2114 | |
c59b5089 PE |
2115 | return Qnil; |
2116 | } | |
2117 | ||
2118 | /* Set the local time zone rule to TZSTRING. | |
5745a7df PE |
2119 | |
2120 | This function is not thread-safe, partly because putenv, unsetenv | |
2121 | and tzset are not, and partly because of the static storage it | |
2122 | updates. Other threads that invoke localtime etc. may be adversely | |
2123 | affected while this function is executing. */ | |
acb7cc89 | 2124 | |
a92ae0ce | 2125 | void |
a8fe7202 | 2126 | set_time_zone_rule (const char *tzstring) |
c59b5089 | 2127 | { |
5745a7df PE |
2128 | /* A buffer holding a string of the form "TZ=value", intended |
2129 | to be part of the environment. */ | |
2130 | static char *tzvalbuf; | |
2131 | static ptrdiff_t tzvalbufsize; | |
c59b5089 | 2132 | |
5745a7df PE |
2133 | int tzeqlen = sizeof "TZ=" - 1; |
2134 | ||
2135 | #ifdef LOCALTIME_CACHE | |
2136 | /* These two values are known to load tz files in buggy implementations, | |
2137 | i.e., Solaris 1 executables running under either Solaris 1 or Solaris 2. | |
2138 | Their values shouldn't matter in non-buggy implementations. | |
2139 | We don't use string literals for these strings, | |
2140 | since if a string in the environment is in readonly | |
2141 | storage, it runs afoul of bugs in SVR4 and Solaris 2.3. | |
2142 | See Sun bugs 1113095 and 1114114, ``Timezone routines | |
2143 | improperly modify environment''. */ | |
2144 | ||
2145 | static char set_time_zone_rule_tz[][sizeof "TZ=GMT+0"] | |
2146 | = { "TZ=GMT+0", "TZ=GMT+1" }; | |
2147 | ||
2148 | /* In SunOS 4.1.3_U1 and 4.1.4, if TZ has a value like | |
2149 | "US/Pacific" that loads a tz file, then changes to a value like | |
2150 | "XXX0" that does not load a tz file, and then changes back to | |
2151 | its original value, the last change is (incorrectly) ignored. | |
2152 | Also, if TZ changes twice in succession to values that do | |
2153 | not load a tz file, tzset can dump core (see Sun bug#1225179). | |
2154 | The following code works around these bugs. */ | |
aafe5147 | 2155 | |
143cb9a9 RS |
2156 | if (tzstring) |
2157 | { | |
5745a7df PE |
2158 | /* Temporarily set TZ to a value that loads a tz file |
2159 | and that differs from tzstring. */ | |
2160 | bool eq0 = strcmp (tzstring, set_time_zone_rule_tz[0] + tzeqlen) == 0; | |
2161 | xputenv (set_time_zone_rule_tz[eq0]); | |
143cb9a9 | 2162 | } |
5745a7df PE |
2163 | else |
2164 | { | |
2165 | /* The implied tzstring is unknown, so temporarily set TZ to | |
2166 | two different values that each load a tz file. */ | |
2167 | xputenv (set_time_zone_rule_tz[0]); | |
2168 | tzset (); | |
2169 | xputenv (set_time_zone_rule_tz[1]); | |
2170 | } | |
2171 | tzset (); | |
2172 | #endif | |
143cb9a9 | 2173 | |
5745a7df PE |
2174 | if (!tzstring) |
2175 | { | |
2176 | unsetenv ("TZ"); | |
2177 | tzvalbuf_in_environ = 0; | |
2178 | } | |
2179 | else | |
2180 | { | |
2181 | ptrdiff_t tzstringlen = strlen (tzstring); | |
143cb9a9 | 2182 | |
5745a7df PE |
2183 | if (tzvalbufsize <= tzeqlen + tzstringlen) |
2184 | { | |
2185 | unsetenv ("TZ"); | |
2186 | tzvalbuf_in_environ = 0; | |
2187 | tzvalbuf = xpalloc (tzvalbuf, &tzvalbufsize, | |
2188 | tzeqlen + tzstringlen - tzvalbufsize + 1, -1, 1); | |
2189 | memcpy (tzvalbuf, "TZ=", tzeqlen); | |
2190 | } | |
aafe5147 | 2191 | |
5745a7df | 2192 | strcpy (tzvalbuf + tzeqlen, tzstring); |
aafe5147 | 2193 | |
5745a7df PE |
2194 | if (!tzvalbuf_in_environ) |
2195 | { | |
2196 | xputenv (tzvalbuf); | |
2197 | tzvalbuf_in_environ = 1; | |
2198 | } | |
2199 | } | |
aafe5147 | 2200 | |
5745a7df | 2201 | #ifdef LOCALTIME_CACHE |
143cb9a9 RS |
2202 | tzset (); |
2203 | #endif | |
143cb9a9 | 2204 | } |
35692fe0 | 2205 | \f |
fb8106e8 KH |
2206 | /* Insert NARGS Lisp objects in the array ARGS by calling INSERT_FUNC |
2207 | (if a type of object is Lisp_Int) or INSERT_FROM_STRING_FUNC (if a | |
2208 | type of object is Lisp_String). INHERIT is passed to | |
2209 | INSERT_FROM_STRING_FUNC as the last argument. */ | |
2210 | ||
acb7cc89 | 2211 | static void |
9628fed7 | 2212 | general_insert_function (void (*insert_func) |
d311d28c | 2213 | (const char *, ptrdiff_t), |
9628fed7 | 2214 | void (*insert_from_string_func) |
d311d28c | 2215 | (Lisp_Object, ptrdiff_t, ptrdiff_t, |
a08d4ba7 PE |
2216 | ptrdiff_t, ptrdiff_t, bool), |
2217 | bool inherit, ptrdiff_t nargs, Lisp_Object *args) | |
fb8106e8 | 2218 | { |
f66c7cf8 | 2219 | ptrdiff_t argnum; |
a08d4ba7 | 2220 | Lisp_Object val; |
fb8106e8 KH |
2221 | |
2222 | for (argnum = 0; argnum < nargs; argnum++) | |
2223 | { | |
2224 | val = args[argnum]; | |
1b9c91ed | 2225 | if (CHARACTERP (val)) |
fb8106e8 | 2226 | { |
13bdea59 | 2227 | int c = XFASTINT (val); |
d5c2c403 | 2228 | unsigned char str[MAX_MULTIBYTE_LENGTH]; |
fb8106e8 KH |
2229 | int len; |
2230 | ||
4b4deea2 | 2231 | if (!NILP (BVAR (current_buffer, enable_multibyte_characters))) |
13bdea59 | 2232 | len = CHAR_STRING (c, str); |
fb8106e8 | 2233 | else |
13c148b8 | 2234 | { |
abbd3d23 | 2235 | str[0] = ASCII_CHAR_P (c) ? c : multibyte_char_to_unibyte (c); |
13c148b8 KH |
2236 | len = 1; |
2237 | } | |
b68864e5 | 2238 | (*insert_func) ((char *) str, len); |
fb8106e8 KH |
2239 | } |
2240 | else if (STRINGP (val)) | |
2241 | { | |
1f24f4fd | 2242 | (*insert_from_string_func) (val, 0, 0, |
d5db4077 KR |
2243 | SCHARS (val), |
2244 | SBYTES (val), | |
1f24f4fd | 2245 | inherit); |
fb8106e8 KH |
2246 | } |
2247 | else | |
b7f34213 | 2248 | wrong_type_argument (Qchar_or_string_p, val); |
fb8106e8 KH |
2249 | } |
2250 | } | |
2251 | ||
35692fe0 | 2252 | void |
971de7fb | 2253 | insert1 (Lisp_Object arg) |
35692fe0 JB |
2254 | { |
2255 | Finsert (1, &arg); | |
2256 | } | |
2257 | ||
52b14ac0 JB |
2258 | |
2259 | /* Callers passing one argument to Finsert need not gcpro the | |
2260 | argument "array", since the only element of the array will | |
2261 | not be used after calling insert or insert_from_string, so | |
2262 | we don't care if it gets trashed. */ | |
2263 | ||
a7ca3326 | 2264 | DEFUN ("insert", Finsert, Sinsert, 0, MANY, 0, |
7ee72033 | 2265 | doc: /* Insert the arguments, either strings or characters, at point. |
a1f17501 PJ |
2266 | Point and before-insertion markers move forward to end up |
2267 | after the inserted text. | |
2268 | Any other markers at the point of insertion remain before the text. | |
2269 | ||
2270 | If the current buffer is multibyte, unibyte strings are converted | |
72bb55c6 | 2271 | to multibyte for insertion (see `string-make-multibyte'). |
a1f17501 | 2272 | If the current buffer is unibyte, multibyte strings are converted |
72bb55c6 KS |
2273 | to unibyte for insertion (see `string-make-unibyte'). |
2274 | ||
2275 | When operating on binary data, it may be necessary to preserve the | |
2276 | original bytes of a unibyte string when inserting it into a multibyte | |
2277 | buffer; to accomplish this, apply `string-as-multibyte' to the string | |
2278 | and insert the result. | |
4bfbe194 MB |
2279 | |
2280 | usage: (insert &rest ARGS) */) | |
f66c7cf8 | 2281 | (ptrdiff_t nargs, Lisp_Object *args) |
35692fe0 | 2282 | { |
fb8106e8 | 2283 | general_insert_function (insert, insert_from_string, 0, nargs, args); |
be91036a RS |
2284 | return Qnil; |
2285 | } | |
2286 | ||
2287 | DEFUN ("insert-and-inherit", Finsert_and_inherit, Sinsert_and_inherit, | |
2288 | 0, MANY, 0, | |
7ee72033 | 2289 | doc: /* Insert the arguments at point, inheriting properties from adjoining text. |
a1f17501 PJ |
2290 | Point and before-insertion markers move forward to end up |
2291 | after the inserted text. | |
2292 | Any other markers at the point of insertion remain before the text. | |
2293 | ||
2294 | If the current buffer is multibyte, unibyte strings are converted | |
2295 | to multibyte for insertion (see `unibyte-char-to-multibyte'). | |
2296 | If the current buffer is unibyte, multibyte strings are converted | |
4bfbe194 MB |
2297 | to unibyte for insertion. |
2298 | ||
2299 | usage: (insert-and-inherit &rest ARGS) */) | |
f66c7cf8 | 2300 | (ptrdiff_t nargs, Lisp_Object *args) |
be91036a | 2301 | { |
fb8106e8 KH |
2302 | general_insert_function (insert_and_inherit, insert_from_string, 1, |
2303 | nargs, args); | |
35692fe0 JB |
2304 | return Qnil; |
2305 | } | |
2306 | ||
2307 | DEFUN ("insert-before-markers", Finsert_before_markers, Sinsert_before_markers, 0, MANY, 0, | |
7ee72033 | 2308 | doc: /* Insert strings or characters at point, relocating markers after the text. |
a1f17501 PJ |
2309 | Point and markers move forward to end up after the inserted text. |
2310 | ||
2311 | If the current buffer is multibyte, unibyte strings are converted | |
2312 | to multibyte for insertion (see `unibyte-char-to-multibyte'). | |
2313 | If the current buffer is unibyte, multibyte strings are converted | |
4bfbe194 MB |
2314 | to unibyte for insertion. |
2315 | ||
2316 | usage: (insert-before-markers &rest ARGS) */) | |
f66c7cf8 | 2317 | (ptrdiff_t nargs, Lisp_Object *args) |
35692fe0 | 2318 | { |
fb8106e8 KH |
2319 | general_insert_function (insert_before_markers, |
2320 | insert_from_string_before_markers, 0, | |
2321 | nargs, args); | |
be91036a RS |
2322 | return Qnil; |
2323 | } | |
2324 | ||
a0d76c27 EN |
2325 | DEFUN ("insert-before-markers-and-inherit", Finsert_and_inherit_before_markers, |
2326 | Sinsert_and_inherit_before_markers, 0, MANY, 0, | |
7ee72033 | 2327 | doc: /* Insert text at point, relocating markers and inheriting properties. |
a1f17501 PJ |
2328 | Point and markers move forward to end up after the inserted text. |
2329 | ||
2330 | If the current buffer is multibyte, unibyte strings are converted | |
2331 | to multibyte for insertion (see `unibyte-char-to-multibyte'). | |
2332 | If the current buffer is unibyte, multibyte strings are converted | |
4bfbe194 MB |
2333 | to unibyte for insertion. |
2334 | ||
2335 | usage: (insert-before-markers-and-inherit &rest ARGS) */) | |
f66c7cf8 | 2336 | (ptrdiff_t nargs, Lisp_Object *args) |
be91036a | 2337 | { |
fb8106e8 KH |
2338 | general_insert_function (insert_before_markers_and_inherit, |
2339 | insert_from_string_before_markers, 1, | |
2340 | nargs, args); | |
35692fe0 JB |
2341 | return Qnil; |
2342 | } | |
2343 | \f | |
ddfc8813 | 2344 | DEFUN ("insert-char", Finsert_char, Sinsert_char, 1, 3, |
1b6dbfeb CY |
2345 | "(list (or (read-char-by-name \"Insert character (Unicode name or hex): \")\ |
2346 | (error \"You did not specify a valid character\"))\ | |
2347 | (prefix-numeric-value current-prefix-arg)\ | |
2348 | t))", | |
9671c13a | 2349 | doc: /* Insert COUNT copies of CHARACTER. |
9ea10cc3 CY |
2350 | Interactively, prompt for CHARACTER. You can specify CHARACTER in one |
2351 | of these ways: | |
2352 | ||
2353 | - As its Unicode character name, e.g. \"LATIN SMALL LETTER A\". | |
2354 | Completion is available; if you type a substring of the name | |
2355 | preceded by an asterisk `*', Emacs shows all names which include | |
2356 | that substring, not necessarily at the beginning of the name. | |
2357 | ||
2358 | - As a hexadecimal code point, e.g. 263A. Note that code points in | |
2359 | Emacs are equivalent to Unicode up to 10FFFF (which is the limit of | |
2360 | the Unicode code space). | |
2361 | ||
2362 | - As a code point with a radix specified with #, e.g. #o21430 | |
2363 | (octal), #x2318 (hex), or #10r8984 (decimal). | |
2364 | ||
2365 | If called interactively, COUNT is given by the prefix argument. If | |
2366 | omitted or nil, it defaults to 1. | |
2367 | ||
2368 | Inserting the character(s) relocates point and before-insertion | |
2369 | markers in the same ways as the function `insert'. | |
2370 | ||
2371 | The optional third argument INHERIT, if non-nil, says to inherit text | |
2372 | properties from adjoining text, if those properties are sticky. If | |
2373 | called interactively, INHERIT is t. */) | |
5842a27b | 2374 | (Lisp_Object character, Lisp_Object count, Lisp_Object inherit) |
35692fe0 | 2375 | { |
21d890a4 | 2376 | int i, stringlen; |
d311d28c | 2377 | register ptrdiff_t n; |
13bdea59 | 2378 | int c, len; |
d5c2c403 | 2379 | unsigned char str[MAX_MULTIBYTE_LENGTH]; |
21d890a4 | 2380 | char string[4000]; |
35692fe0 | 2381 | |
13bdea59 | 2382 | CHECK_CHARACTER (character); |
ddfc8813 RK |
2383 | if (NILP (count)) |
2384 | XSETFASTINT (count, 1); | |
b7826503 | 2385 | CHECK_NUMBER (count); |
13bdea59 | 2386 | c = XFASTINT (character); |
35692fe0 | 2387 | |
4b4deea2 | 2388 | if (!NILP (BVAR (current_buffer, enable_multibyte_characters))) |
13bdea59 | 2389 | len = CHAR_STRING (c, str); |
fb8106e8 | 2390 | else |
13bdea59 | 2391 | str[0] = c, len = 1; |
2e6813b0 PE |
2392 | if (XINT (count) <= 0) |
2393 | return Qnil; | |
d1f3d2af | 2394 | if (BUF_BYTES_MAX / len < XINT (count)) |
99561444 | 2395 | buffer_overflow (); |
fb8106e8 | 2396 | n = XINT (count) * len; |
21d890a4 | 2397 | stringlen = min (n, sizeof string - sizeof string % len); |
545b49b4 | 2398 | for (i = 0; i < stringlen; i++) |
fb8106e8 | 2399 | string[i] = str[i % len]; |
21d890a4 | 2400 | while (n > stringlen) |
35692fe0 | 2401 | { |
54e42e2d | 2402 | QUIT; |
e2eeabbb | 2403 | if (!NILP (inherit)) |
545b49b4 | 2404 | insert_and_inherit (string, stringlen); |
e2eeabbb | 2405 | else |
545b49b4 PE |
2406 | insert (string, stringlen); |
2407 | n -= stringlen; | |
35692fe0 | 2408 | } |
21d890a4 PE |
2409 | if (!NILP (inherit)) |
2410 | insert_and_inherit (string, n); | |
2411 | else | |
2412 | insert (string, n); | |
35692fe0 JB |
2413 | return Qnil; |
2414 | } | |
2415 | ||
48ef988f KH |
2416 | DEFUN ("insert-byte", Finsert_byte, Sinsert_byte, 2, 3, 0, |
2417 | doc: /* Insert COUNT (second arg) copies of BYTE (first arg). | |
2418 | Both arguments are required. | |
2419 | BYTE is a number of the range 0..255. | |
2420 | ||
2421 | If BYTE is 128..255 and the current buffer is multibyte, the | |
2422 | corresponding eight-bit character is inserted. | |
2423 | ||
2424 | Point, and before-insertion markers, are relocated as in the function `insert'. | |
2425 | The optional third arg INHERIT, if non-nil, says to inherit text properties | |
2426 | from adjoining text, if those properties are sticky. */) | |
5842a27b | 2427 | (Lisp_Object byte, Lisp_Object count, Lisp_Object inherit) |
48ef988f KH |
2428 | { |
2429 | CHECK_NUMBER (byte); | |
2430 | if (XINT (byte) < 0 || XINT (byte) > 255) | |
2431 | args_out_of_range_3 (byte, make_number (0), make_number (255)); | |
2432 | if (XINT (byte) >= 128 | |
4b4deea2 | 2433 | && ! NILP (BVAR (current_buffer, enable_multibyte_characters))) |
48ef988f | 2434 | XSETFASTINT (byte, BYTE8_TO_CHAR (XINT (byte))); |
ed398b0a | 2435 | return Finsert_char (byte, count, inherit); |
48ef988f KH |
2436 | } |
2437 | ||
35692fe0 | 2438 | \f |
ffd56f97 JB |
2439 | /* Making strings from buffer contents. */ |
2440 | ||
2441 | /* Return a Lisp_String containing the text of the current buffer from | |
74d6d8c5 | 2442 | START to END. If text properties are in use and the current buffer |
eb8c3be9 | 2443 | has properties in the range specified, the resulting string will also |
a08d4ba7 | 2444 | have them, if PROPS is true. |
ffd56f97 JB |
2445 | |
2446 | We don't want to use plain old make_string here, because it calls | |
2447 | make_uninit_string, which can cause the buffer arena to be | |
2448 | compacted. make_string has no way of knowing that the data has | |
2449 | been moved, and thus copies the wrong data into the string. This | |
2450 | doesn't effect most of the other users of make_string, so it should | |
2451 | be left as is. But we should use this function when conjuring | |
2452 | buffer substrings. */ | |
74d6d8c5 | 2453 | |
ffd56f97 | 2454 | Lisp_Object |
a08d4ba7 | 2455 | make_buffer_string (ptrdiff_t start, ptrdiff_t end, bool props) |
ffd56f97 | 2456 | { |
d311d28c PE |
2457 | ptrdiff_t start_byte = CHAR_TO_BYTE (start); |
2458 | ptrdiff_t end_byte = CHAR_TO_BYTE (end); | |
ffd56f97 | 2459 | |
88441c8e RS |
2460 | return make_buffer_string_both (start, start_byte, end, end_byte, props); |
2461 | } | |
2462 | ||
2463 | /* Return a Lisp_String containing the text of the current buffer from | |
2464 | START / START_BYTE to END / END_BYTE. | |
2465 | ||
2466 | If text properties are in use and the current buffer | |
2467 | has properties in the range specified, the resulting string will also | |
a08d4ba7 | 2468 | have them, if PROPS is true. |
88441c8e RS |
2469 | |
2470 | We don't want to use plain old make_string here, because it calls | |
2471 | make_uninit_string, which can cause the buffer arena to be | |
2472 | compacted. make_string has no way of knowing that the data has | |
2473 | been moved, and thus copies the wrong data into the string. This | |
2474 | doesn't effect most of the other users of make_string, so it should | |
2475 | be left as is. But we should use this function when conjuring | |
2476 | buffer substrings. */ | |
2477 | ||
2478 | Lisp_Object | |
d311d28c | 2479 | make_buffer_string_both (ptrdiff_t start, ptrdiff_t start_byte, |
a08d4ba7 | 2480 | ptrdiff_t end, ptrdiff_t end_byte, bool props) |
88441c8e RS |
2481 | { |
2482 | Lisp_Object result, tem, tem1; | |
2483 | ||
ffd56f97 JB |
2484 | if (start < GPT && GPT < end) |
2485 | move_gap (start); | |
2486 | ||
4b4deea2 | 2487 | if (! NILP (BVAR (current_buffer, enable_multibyte_characters))) |
5f75e666 RS |
2488 | result = make_uninit_multibyte_string (end - start, end_byte - start_byte); |
2489 | else | |
2490 | result = make_uninit_string (end - start); | |
72af86bd | 2491 | memcpy (SDATA (result), BYTE_POS_ADDR (start_byte), end_byte - start_byte); |
ffd56f97 | 2492 | |
260e2e2a | 2493 | /* If desired, update and copy the text properties. */ |
260e2e2a KH |
2494 | if (props) |
2495 | { | |
2496 | update_buffer_properties (start, end); | |
2497 | ||
2498 | tem = Fnext_property_change (make_number (start), Qnil, make_number (end)); | |
2499 | tem1 = Ftext_properties_at (make_number (start), Qnil); | |
2500 | ||
2501 | if (XINT (tem) != end || !NILP (tem1)) | |
ec1c14f6 RS |
2502 | copy_intervals_to_string (result, current_buffer, start, |
2503 | end - start); | |
260e2e2a | 2504 | } |
74d6d8c5 | 2505 | |
ffd56f97 JB |
2506 | return result; |
2507 | } | |
35692fe0 | 2508 | |
260e2e2a KH |
2509 | /* Call Vbuffer_access_fontify_functions for the range START ... END |
2510 | in the current buffer, if necessary. */ | |
2511 | ||
2512 | static void | |
d311d28c | 2513 | update_buffer_properties (ptrdiff_t start, ptrdiff_t end) |
260e2e2a | 2514 | { |
260e2e2a KH |
2515 | /* If this buffer has some access functions, |
2516 | call them, specifying the range of the buffer being accessed. */ | |
2517 | if (!NILP (Vbuffer_access_fontify_functions)) | |
2518 | { | |
2519 | Lisp_Object args[3]; | |
2520 | Lisp_Object tem; | |
2521 | ||
2522 | args[0] = Qbuffer_access_fontify_functions; | |
2523 | XSETINT (args[1], start); | |
2524 | XSETINT (args[2], end); | |
2525 | ||
2526 | /* But don't call them if we can tell that the work | |
2527 | has already been done. */ | |
2528 | if (!NILP (Vbuffer_access_fontified_property)) | |
2529 | { | |
2530 | tem = Ftext_property_any (args[1], args[2], | |
2531 | Vbuffer_access_fontified_property, | |
2532 | Qnil, Qnil); | |
2533 | if (! NILP (tem)) | |
ced1d19a | 2534 | Frun_hook_with_args (3, args); |
260e2e2a KH |
2535 | } |
2536 | else | |
ced1d19a | 2537 | Frun_hook_with_args (3, args); |
260e2e2a | 2538 | } |
260e2e2a KH |
2539 | } |
2540 | ||
a7ca3326 | 2541 | DEFUN ("buffer-substring", Fbuffer_substring, Sbuffer_substring, 2, 2, 0, |
7ee72033 | 2542 | doc: /* Return the contents of part of the current buffer as a string. |
a1f17501 PJ |
2543 | The two arguments START and END are character positions; |
2544 | they can be in either order. | |
2545 | The string returned is multibyte if the buffer is multibyte. | |
2546 | ||
2547 | This function copies the text properties of that part of the buffer | |
2548 | into the result string; if you don't want the text properties, | |
7ee72033 | 2549 | use `buffer-substring-no-properties' instead. */) |
5842a27b | 2550 | (Lisp_Object start, Lisp_Object end) |
35692fe0 | 2551 | { |
d311d28c | 2552 | register ptrdiff_t b, e; |
35692fe0 | 2553 | |
2591ec64 EN |
2554 | validate_region (&start, &end); |
2555 | b = XINT (start); | |
2556 | e = XINT (end); | |
35692fe0 | 2557 | |
2591ec64 | 2558 | return make_buffer_string (b, e, 1); |
260e2e2a KH |
2559 | } |
2560 | ||
2561 | DEFUN ("buffer-substring-no-properties", Fbuffer_substring_no_properties, | |
2562 | Sbuffer_substring_no_properties, 2, 2, 0, | |
7ee72033 | 2563 | doc: /* Return the characters of part of the buffer, without the text properties. |
a1f17501 | 2564 | The two arguments START and END are character positions; |
7ee72033 | 2565 | they can be in either order. */) |
5842a27b | 2566 | (Lisp_Object start, Lisp_Object end) |
260e2e2a | 2567 | { |
d311d28c | 2568 | register ptrdiff_t b, e; |
260e2e2a | 2569 | |
2591ec64 EN |
2570 | validate_region (&start, &end); |
2571 | b = XINT (start); | |
2572 | e = XINT (end); | |
260e2e2a | 2573 | |
2591ec64 | 2574 | return make_buffer_string (b, e, 0); |
35692fe0 JB |
2575 | } |
2576 | ||
a7ca3326 | 2577 | DEFUN ("buffer-string", Fbuffer_string, Sbuffer_string, 0, 0, 0, |
7ee72033 | 2578 | doc: /* Return the contents of the current buffer as a string. |
a1f17501 | 2579 | If narrowing is in effect, this function returns only the visible part |
7ee72033 | 2580 | of the buffer. */) |
5842a27b | 2581 | (void) |
35692fe0 | 2582 | { |
0daf6e8d | 2583 | return make_buffer_string (BEGV, ZV, 1); |
35692fe0 JB |
2584 | } |
2585 | ||
2586 | DEFUN ("insert-buffer-substring", Finsert_buffer_substring, Sinsert_buffer_substring, | |
deb8e082 | 2587 | 1, 3, 0, |
658ec670 | 2588 | doc: /* Insert before point a substring of the contents of BUFFER. |
a1f17501 | 2589 | BUFFER may be a buffer or a buffer name. |
412f1fab JB |
2590 | Arguments START and END are character positions specifying the substring. |
2591 | They default to the values of (point-min) and (point-max) in BUFFER. */) | |
5842a27b | 2592 | (Lisp_Object buffer, Lisp_Object start, Lisp_Object end) |
35692fe0 | 2593 | { |
29cdc13e | 2594 | register EMACS_INT b, e, temp; |
260e2e2a | 2595 | register struct buffer *bp, *obuf; |
658ec670 | 2596 | Lisp_Object buf; |
35692fe0 | 2597 | |
658ec670 JB |
2598 | buf = Fget_buffer (buffer); |
2599 | if (NILP (buf)) | |
2600 | nsberror (buffer); | |
2601 | bp = XBUFFER (buf); | |
e578f381 | 2602 | if (!BUFFER_LIVE_P (bp)) |
93b62e82 | 2603 | error ("Selecting deleted buffer"); |
35692fe0 | 2604 | |
2591ec64 EN |
2605 | if (NILP (start)) |
2606 | b = BUF_BEGV (bp); | |
35692fe0 JB |
2607 | else |
2608 | { | |
b7826503 | 2609 | CHECK_NUMBER_COERCE_MARKER (start); |
2591ec64 | 2610 | b = XINT (start); |
35692fe0 | 2611 | } |
2591ec64 EN |
2612 | if (NILP (end)) |
2613 | e = BUF_ZV (bp); | |
35692fe0 JB |
2614 | else |
2615 | { | |
b7826503 | 2616 | CHECK_NUMBER_COERCE_MARKER (end); |
2591ec64 | 2617 | e = XINT (end); |
35692fe0 JB |
2618 | } |
2619 | ||
2591ec64 EN |
2620 | if (b > e) |
2621 | temp = b, b = e, e = temp; | |
35692fe0 | 2622 | |
2591ec64 EN |
2623 | if (!(BUF_BEGV (bp) <= b && e <= BUF_ZV (bp))) |
2624 | args_out_of_range (start, end); | |
35692fe0 | 2625 | |
260e2e2a KH |
2626 | obuf = current_buffer; |
2627 | set_buffer_internal_1 (bp); | |
2591ec64 | 2628 | update_buffer_properties (b, e); |
260e2e2a KH |
2629 | set_buffer_internal_1 (obuf); |
2630 | ||
2591ec64 | 2631 | insert_from_buffer (bp, b, e - b, 0); |
35692fe0 JB |
2632 | return Qnil; |
2633 | } | |
e9cf2084 RS |
2634 | |
2635 | DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings, Scompare_buffer_substrings, | |
deb8e082 | 2636 | 6, 6, 0, |
7ee72033 | 2637 | doc: /* Compare two substrings of two buffers; return result as number. |
a1f17501 PJ |
2638 | the value is -N if first string is less after N-1 chars, |
2639 | +N if first string is greater after N-1 chars, or 0 if strings match. | |
2640 | Each substring is represented as three arguments: BUFFER, START and END. | |
2641 | That makes six args in all, three for each substring. | |
2642 | ||
2643 | The value of `case-fold-search' in the current buffer | |
7ee72033 | 2644 | determines whether case is significant or ignored. */) |
5842a27b | 2645 | (Lisp_Object buffer1, Lisp_Object start1, Lisp_Object end1, Lisp_Object buffer2, Lisp_Object start2, Lisp_Object end2) |
e9cf2084 | 2646 | { |
29cdc13e | 2647 | register EMACS_INT begp1, endp1, begp2, endp2, temp; |
e9cf2084 | 2648 | register struct buffer *bp1, *bp2; |
1149fd6f | 2649 | register Lisp_Object trt |
4b4deea2 TT |
2650 | = (!NILP (BVAR (current_buffer, case_fold_search)) |
2651 | ? BVAR (current_buffer, case_canon_table) : Qnil); | |
d311d28c PE |
2652 | ptrdiff_t chars = 0; |
2653 | ptrdiff_t i1, i2, i1_byte, i2_byte; | |
e9cf2084 RS |
2654 | |
2655 | /* Find the first buffer and its substring. */ | |
2656 | ||
2657 | if (NILP (buffer1)) | |
2658 | bp1 = current_buffer; | |
2659 | else | |
2660 | { | |
3fff2dfa RS |
2661 | Lisp_Object buf1; |
2662 | buf1 = Fget_buffer (buffer1); | |
2663 | if (NILP (buf1)) | |
2664 | nsberror (buffer1); | |
2665 | bp1 = XBUFFER (buf1); | |
e578f381 | 2666 | if (!BUFFER_LIVE_P (bp1)) |
93b62e82 | 2667 | error ("Selecting deleted buffer"); |
e9cf2084 RS |
2668 | } |
2669 | ||
2670 | if (NILP (start1)) | |
2671 | begp1 = BUF_BEGV (bp1); | |
2672 | else | |
2673 | { | |
b7826503 | 2674 | CHECK_NUMBER_COERCE_MARKER (start1); |
e9cf2084 RS |
2675 | begp1 = XINT (start1); |
2676 | } | |
2677 | if (NILP (end1)) | |
2678 | endp1 = BUF_ZV (bp1); | |
2679 | else | |
2680 | { | |
b7826503 | 2681 | CHECK_NUMBER_COERCE_MARKER (end1); |
e9cf2084 RS |
2682 | endp1 = XINT (end1); |
2683 | } | |
2684 | ||
2685 | if (begp1 > endp1) | |
2686 | temp = begp1, begp1 = endp1, endp1 = temp; | |
2687 | ||
2688 | if (!(BUF_BEGV (bp1) <= begp1 | |
2689 | && begp1 <= endp1 | |
2690 | && endp1 <= BUF_ZV (bp1))) | |
2691 | args_out_of_range (start1, end1); | |
2692 | ||
2693 | /* Likewise for second substring. */ | |
2694 | ||
2695 | if (NILP (buffer2)) | |
2696 | bp2 = current_buffer; | |
2697 | else | |
2698 | { | |
3fff2dfa RS |
2699 | Lisp_Object buf2; |
2700 | buf2 = Fget_buffer (buffer2); | |
2701 | if (NILP (buf2)) | |
2702 | nsberror (buffer2); | |
3b1fdd85 | 2703 | bp2 = XBUFFER (buf2); |
e578f381 | 2704 | if (!BUFFER_LIVE_P (bp2)) |
93b62e82 | 2705 | error ("Selecting deleted buffer"); |
e9cf2084 RS |
2706 | } |
2707 | ||
2708 | if (NILP (start2)) | |
2709 | begp2 = BUF_BEGV (bp2); | |
2710 | else | |
2711 | { | |
b7826503 | 2712 | CHECK_NUMBER_COERCE_MARKER (start2); |
e9cf2084 RS |
2713 | begp2 = XINT (start2); |
2714 | } | |
2715 | if (NILP (end2)) | |
2716 | endp2 = BUF_ZV (bp2); | |
2717 | else | |
2718 | { | |
b7826503 | 2719 | CHECK_NUMBER_COERCE_MARKER (end2); |
e9cf2084 RS |
2720 | endp2 = XINT (end2); |
2721 | } | |
2722 | ||
2723 | if (begp2 > endp2) | |
2724 | temp = begp2, begp2 = endp2, endp2 = temp; | |
2725 | ||
2726 | if (!(BUF_BEGV (bp2) <= begp2 | |
2727 | && begp2 <= endp2 | |
2728 | && endp2 <= BUF_ZV (bp2))) | |
2729 | args_out_of_range (start2, end2); | |
2730 | ||
07422a12 RS |
2731 | i1 = begp1; |
2732 | i2 = begp2; | |
2733 | i1_byte = buf_charpos_to_bytepos (bp1, i1); | |
2734 | i2_byte = buf_charpos_to_bytepos (bp2, i2); | |
e9cf2084 | 2735 | |
07422a12 | 2736 | while (i1 < endp1 && i2 < endp2) |
e9cf2084 | 2737 | { |
07422a12 RS |
2738 | /* When we find a mismatch, we must compare the |
2739 | characters, not just the bytes. */ | |
2740 | int c1, c2; | |
ec1c14f6 | 2741 | |
2221451f RS |
2742 | QUIT; |
2743 | ||
4b4deea2 | 2744 | if (! NILP (BVAR (bp1, enable_multibyte_characters))) |
07422a12 RS |
2745 | { |
2746 | c1 = BUF_FETCH_MULTIBYTE_CHAR (bp1, i1_byte); | |
2747 | BUF_INC_POS (bp1, i1_byte); | |
2748 | i1++; | |
2749 | } | |
2750 | else | |
2751 | { | |
2752 | c1 = BUF_FETCH_BYTE (bp1, i1); | |
4c0354d7 | 2753 | MAKE_CHAR_MULTIBYTE (c1); |
07422a12 RS |
2754 | i1++; |
2755 | } | |
2756 | ||
4b4deea2 | 2757 | if (! NILP (BVAR (bp2, enable_multibyte_characters))) |
07422a12 RS |
2758 | { |
2759 | c2 = BUF_FETCH_MULTIBYTE_CHAR (bp2, i2_byte); | |
2760 | BUF_INC_POS (bp2, i2_byte); | |
2761 | i2++; | |
2762 | } | |
2763 | else | |
2764 | { | |
2765 | c2 = BUF_FETCH_BYTE (bp2, i2); | |
4c0354d7 | 2766 | MAKE_CHAR_MULTIBYTE (c2); |
07422a12 RS |
2767 | i2++; |
2768 | } | |
ec1c14f6 | 2769 | |
1149fd6f | 2770 | if (!NILP (trt)) |
e9cf2084 | 2771 | { |
e3ccf108 DA |
2772 | c1 = char_table_translate (trt, c1); |
2773 | c2 = char_table_translate (trt, c2); | |
e9cf2084 RS |
2774 | } |
2775 | if (c1 < c2) | |
ec1c14f6 | 2776 | return make_number (- 1 - chars); |
e9cf2084 | 2777 | if (c1 > c2) |
ec1c14f6 | 2778 | return make_number (chars + 1); |
07422a12 RS |
2779 | |
2780 | chars++; | |
e9cf2084 RS |
2781 | } |
2782 | ||
2783 | /* The strings match as far as they go. | |
2784 | If one is shorter, that one is less. */ | |
07422a12 | 2785 | if (chars < endp1 - begp1) |
ec1c14f6 | 2786 | return make_number (chars + 1); |
07422a12 | 2787 | else if (chars < endp2 - begp2) |
ec1c14f6 | 2788 | return make_number (- chars - 1); |
e9cf2084 RS |
2789 | |
2790 | /* Same length too => they are equal. */ | |
2791 | return make_number (0); | |
2792 | } | |
35692fe0 | 2793 | \f |
d5a539cd | 2794 | static Lisp_Object |
971de7fb | 2795 | subst_char_in_region_unwind (Lisp_Object arg) |
d5a539cd | 2796 | { |
39eb03f1 PE |
2797 | bset_undo_list (current_buffer, arg); |
2798 | return arg; | |
d5a539cd RS |
2799 | } |
2800 | ||
c8e76b47 | 2801 | static Lisp_Object |
971de7fb | 2802 | subst_char_in_region_unwind_1 (Lisp_Object arg) |
c8e76b47 | 2803 | { |
39eb03f1 PE |
2804 | bset_filename (current_buffer, arg); |
2805 | return arg; | |
c8e76b47 RS |
2806 | } |
2807 | ||
35692fe0 | 2808 | DEFUN ("subst-char-in-region", Fsubst_char_in_region, |
deb8e082 | 2809 | Ssubst_char_in_region, 4, 5, 0, |
7ee72033 | 2810 | doc: /* From START to END, replace FROMCHAR with TOCHAR each time it occurs. |
a1f17501 PJ |
2811 | If optional arg NOUNDO is non-nil, don't record this change for undo |
2812 | and don't mark the buffer as really changed. | |
7ee72033 | 2813 | Both characters must have the same length of multi-byte form. */) |
5842a27b | 2814 | (Lisp_Object start, Lisp_Object end, Lisp_Object fromchar, Lisp_Object tochar, Lisp_Object noundo) |
35692fe0 | 2815 | { |
d311d28c | 2816 | register ptrdiff_t pos, pos_byte, stop, i, len, end_byte; |
0f2e2a3b SM |
2817 | /* Keep track of the first change in the buffer: |
2818 | if 0 we haven't found it yet. | |
2819 | if < 0 we've found it and we've run the before-change-function. | |
2820 | if > 0 we've actually performed it and the value is its position. */ | |
d311d28c | 2821 | ptrdiff_t changed = 0; |
d5c2c403 KH |
2822 | unsigned char fromstr[MAX_MULTIBYTE_LENGTH], tostr[MAX_MULTIBYTE_LENGTH]; |
2823 | unsigned char *p; | |
d311d28c | 2824 | ptrdiff_t count = SPECPDL_INDEX (); |
aa801467 KH |
2825 | #define COMBINING_NO 0 |
2826 | #define COMBINING_BEFORE 1 | |
2827 | #define COMBINING_AFTER 2 | |
2828 | #define COMBINING_BOTH (COMBINING_BEFORE | COMBINING_AFTER) | |
2829 | int maybe_byte_combining = COMBINING_NO; | |
d311d28c | 2830 | ptrdiff_t last_changed = 0; |
a08d4ba7 PE |
2831 | bool multibyte_p |
2832 | = !NILP (BVAR (current_buffer, enable_multibyte_characters)); | |
13bdea59 | 2833 | int fromc, toc; |
35692fe0 | 2834 | |
0f2e2a3b SM |
2835 | restart: |
2836 | ||
35692fe0 | 2837 | validate_region (&start, &end); |
13bdea59 PE |
2838 | CHECK_CHARACTER (fromchar); |
2839 | CHECK_CHARACTER (tochar); | |
2840 | fromc = XFASTINT (fromchar); | |
2841 | toc = XFASTINT (tochar); | |
35692fe0 | 2842 | |
7439e5b9 | 2843 | if (multibyte_p) |
fb8106e8 | 2844 | { |
13bdea59 PE |
2845 | len = CHAR_STRING (fromc, fromstr); |
2846 | if (CHAR_STRING (toc, tostr) != len) | |
fdd6025e | 2847 | error ("Characters in `subst-char-in-region' have different byte-lengths"); |
aa801467 KH |
2848 | if (!ASCII_BYTE_P (*tostr)) |
2849 | { | |
2850 | /* If *TOSTR is in the range 0x80..0x9F and TOCHAR is not a | |
2851 | complete multibyte character, it may be combined with the | |
2852 | after bytes. If it is in the range 0xA0..0xFF, it may be | |
2853 | combined with the before and after bytes. */ | |
2854 | if (!CHAR_HEAD_P (*tostr)) | |
2855 | maybe_byte_combining = COMBINING_BOTH; | |
2856 | else if (BYTES_BY_CHAR_HEAD (*tostr) > len) | |
2857 | maybe_byte_combining = COMBINING_AFTER; | |
2858 | } | |
fb8106e8 KH |
2859 | } |
2860 | else | |
2861 | { | |
2862 | len = 1; | |
13bdea59 PE |
2863 | fromstr[0] = fromc; |
2864 | tostr[0] = toc; | |
fb8106e8 KH |
2865 | } |
2866 | ||
84246b95 KH |
2867 | pos = XINT (start); |
2868 | pos_byte = CHAR_TO_BYTE (pos); | |
ec1c14f6 RS |
2869 | stop = CHAR_TO_BYTE (XINT (end)); |
2870 | end_byte = stop; | |
35692fe0 | 2871 | |
d5a539cd RS |
2872 | /* If we don't want undo, turn off putting stuff on the list. |
2873 | That's faster than getting rid of things, | |
c8e76b47 RS |
2874 | and it prevents even the entry for a first change. |
2875 | Also inhibit locking the file. */ | |
0f2e2a3b | 2876 | if (!changed && !NILP (noundo)) |
d5a539cd RS |
2877 | { |
2878 | record_unwind_protect (subst_char_in_region_unwind, | |
4b4deea2 | 2879 | BVAR (current_buffer, undo_list)); |
39eb03f1 | 2880 | bset_undo_list (current_buffer, Qt); |
c8e76b47 RS |
2881 | /* Don't do file-locking. */ |
2882 | record_unwind_protect (subst_char_in_region_unwind_1, | |
4b4deea2 | 2883 | BVAR (current_buffer, filename)); |
39eb03f1 | 2884 | bset_filename (current_buffer, Qnil); |
d5a539cd RS |
2885 | } |
2886 | ||
84246b95 | 2887 | if (pos_byte < GPT_BYTE) |
ec1c14f6 | 2888 | stop = min (stop, GPT_BYTE); |
fb8106e8 | 2889 | while (1) |
35692fe0 | 2890 | { |
d311d28c | 2891 | ptrdiff_t pos_byte_next = pos_byte; |
a3360ff9 | 2892 | |
84246b95 | 2893 | if (pos_byte >= stop) |
fb8106e8 | 2894 | { |
84246b95 | 2895 | if (pos_byte >= end_byte) break; |
ec1c14f6 | 2896 | stop = end_byte; |
fb8106e8 | 2897 | } |
84246b95 | 2898 | p = BYTE_POS_ADDR (pos_byte); |
7439e5b9 GM |
2899 | if (multibyte_p) |
2900 | INC_POS (pos_byte_next); | |
2901 | else | |
2902 | ++pos_byte_next; | |
a3360ff9 KH |
2903 | if (pos_byte_next - pos_byte == len |
2904 | && p[0] == fromstr[0] | |
fb8106e8 KH |
2905 | && (len == 1 |
2906 | || (p[1] == fromstr[1] | |
2907 | && (len == 2 || (p[2] == fromstr[2] | |
2908 | && (len == 3 || p[3] == fromstr[3])))))) | |
35692fe0 | 2909 | { |
0f2e2a3b SM |
2910 | if (changed < 0) |
2911 | /* We've already seen this and run the before-change-function; | |
2912 | this time we only need to record the actual position. */ | |
2913 | changed = pos; | |
2914 | else if (!changed) | |
60b96ee7 | 2915 | { |
0f2e2a3b | 2916 | changed = -1; |
20edc1c9 | 2917 | modify_region_1 (pos, XINT (end), false); |
7653d030 RS |
2918 | |
2919 | if (! NILP (noundo)) | |
2920 | { | |
1e158d25 RS |
2921 | if (MODIFF - 1 == SAVE_MODIFF) |
2922 | SAVE_MODIFF++; | |
0b5397c2 SM |
2923 | if (MODIFF - 1 == BUF_AUTOSAVE_MODIFF (current_buffer)) |
2924 | BUF_AUTOSAVE_MODIFF (current_buffer)++; | |
7653d030 | 2925 | } |
0f2e2a3b SM |
2926 | |
2927 | /* The before-change-function may have moved the gap | |
2928 | or even modified the buffer so we should start over. */ | |
2929 | goto restart; | |
60b96ee7 RS |
2930 | } |
2931 | ||
0c1e3b85 | 2932 | /* Take care of the case where the new character |
34a7a267 | 2933 | combines with neighboring bytes. */ |
a3360ff9 | 2934 | if (maybe_byte_combining |
aa801467 KH |
2935 | && (maybe_byte_combining == COMBINING_AFTER |
2936 | ? (pos_byte_next < Z_BYTE | |
2937 | && ! CHAR_HEAD_P (FETCH_BYTE (pos_byte_next))) | |
2938 | : ((pos_byte_next < Z_BYTE | |
2939 | && ! CHAR_HEAD_P (FETCH_BYTE (pos_byte_next))) | |
2940 | || (pos_byte > BEG_BYTE | |
2941 | && ! ASCII_BYTE_P (FETCH_BYTE (pos_byte - 1)))))) | |
0c1e3b85 RS |
2942 | { |
2943 | Lisp_Object tem, string; | |
2944 | ||
2945 | struct gcpro gcpro1; | |
2946 | ||
4b4deea2 | 2947 | tem = BVAR (current_buffer, undo_list); |
0c1e3b85 RS |
2948 | GCPRO1 (tem); |
2949 | ||
aa801467 | 2950 | /* Make a multibyte string containing this single character. */ |
e7f8264d | 2951 | string = make_multibyte_string ((char *) tostr, 1, len); |
0c1e3b85 RS |
2952 | /* replace_range is less efficient, because it moves the gap, |
2953 | but it handles combining correctly. */ | |
2954 | replace_range (pos, pos + 1, string, | |
9869520f | 2955 | 0, 0, 1); |
a3360ff9 KH |
2956 | pos_byte_next = CHAR_TO_BYTE (pos); |
2957 | if (pos_byte_next > pos_byte) | |
2958 | /* Before combining happened. We should not increment | |
3f5409d3 KH |
2959 | POS. So, to cancel the later increment of POS, |
2960 | decrease it now. */ | |
2961 | pos--; | |
a3360ff9 | 2962 | else |
3f5409d3 | 2963 | INC_POS (pos_byte_next); |
34a7a267 | 2964 | |
0c1e3b85 | 2965 | if (! NILP (noundo)) |
39eb03f1 | 2966 | bset_undo_list (current_buffer, tem); |
0c1e3b85 RS |
2967 | |
2968 | UNGCPRO; | |
2969 | } | |
2970 | else | |
2971 | { | |
2972 | if (NILP (noundo)) | |
2973 | record_change (pos, 1); | |
2974 | for (i = 0; i < len; i++) *p++ = tostr[i]; | |
2975 | } | |
d5c2c403 | 2976 | last_changed = pos + 1; |
35692fe0 | 2977 | } |
3f5409d3 KH |
2978 | pos_byte = pos_byte_next; |
2979 | pos++; | |
35692fe0 JB |
2980 | } |
2981 | ||
0f2e2a3b | 2982 | if (changed > 0) |
d5c2c403 KH |
2983 | { |
2984 | signal_after_change (changed, | |
2985 | last_changed - changed, last_changed - changed); | |
2986 | update_compositions (changed, last_changed, CHECK_ALL); | |
2987 | } | |
60b96ee7 | 2988 | |
d5a539cd | 2989 | unbind_to (count, Qnil); |
35692fe0 JB |
2990 | return Qnil; |
2991 | } | |
2992 | ||
f555f8cf | 2993 | |
d311d28c | 2994 | static Lisp_Object check_translation (ptrdiff_t, ptrdiff_t, ptrdiff_t, |
c8a66ab8 | 2995 | Lisp_Object); |
f555f8cf KH |
2996 | |
2997 | /* Helper function for Ftranslate_region_internal. | |
2998 | ||
2999 | Check if a character sequence at POS (POS_BYTE) matches an element | |
3000 | of VAL. VAL is a list (([FROM-CHAR ...] . TO) ...). If a matching | |
3001 | element is found, return it. Otherwise return Qnil. */ | |
3002 | ||
3003 | static Lisp_Object | |
d311d28c | 3004 | check_translation (ptrdiff_t pos, ptrdiff_t pos_byte, ptrdiff_t end, |
c8a66ab8 | 3005 | Lisp_Object val) |
f555f8cf KH |
3006 | { |
3007 | int buf_size = 16, buf_used = 0; | |
3008 | int *buf = alloca (sizeof (int) * buf_size); | |
3009 | ||
3010 | for (; CONSP (val); val = XCDR (val)) | |
3011 | { | |
3012 | Lisp_Object elt; | |
d311d28c | 3013 | ptrdiff_t len, i; |
f555f8cf KH |
3014 | |
3015 | elt = XCAR (val); | |
3016 | if (! CONSP (elt)) | |
3017 | continue; | |
3018 | elt = XCAR (elt); | |
3019 | if (! VECTORP (elt)) | |
3020 | continue; | |
3021 | len = ASIZE (elt); | |
3022 | if (len <= end - pos) | |
3023 | { | |
3024 | for (i = 0; i < len; i++) | |
3025 | { | |
3026 | if (buf_used <= i) | |
3027 | { | |
3028 | unsigned char *p = BYTE_POS_ADDR (pos_byte); | |
c8a66ab8 | 3029 | int len1; |
f555f8cf KH |
3030 | |
3031 | if (buf_used == buf_size) | |
3032 | { | |
3033 | int *newbuf; | |
3034 | ||
3035 | buf_size += 16; | |
3036 | newbuf = alloca (sizeof (int) * buf_size); | |
3037 | memcpy (newbuf, buf, sizeof (int) * buf_used); | |
3038 | buf = newbuf; | |
3039 | } | |
c8a66ab8 EZ |
3040 | buf[buf_used++] = STRING_CHAR_AND_LENGTH (p, len1); |
3041 | pos_byte += len1; | |
f555f8cf KH |
3042 | } |
3043 | if (XINT (AREF (elt, i)) != buf[i]) | |
3044 | break; | |
3045 | } | |
3046 | if (i == len) | |
3047 | return XCAR (val); | |
3048 | } | |
3049 | } | |
3050 | return Qnil; | |
3051 | } | |
3052 | ||
3053 | ||
8583605b KH |
3054 | DEFUN ("translate-region-internal", Ftranslate_region_internal, |
3055 | Stranslate_region_internal, 3, 3, 0, | |
3056 | doc: /* Internal use only. | |
3057 | From START to END, translate characters according to TABLE. | |
f555f8cf KH |
3058 | TABLE is a string or a char-table; the Nth character in it is the |
3059 | mapping for the character with code N. | |
7ee72033 | 3060 | It returns the number of characters changed. */) |
5842a27b | 3061 | (Lisp_Object start, Lisp_Object end, register Lisp_Object table) |
35692fe0 | 3062 | { |
35692fe0 | 3063 | register unsigned char *tt; /* Trans table. */ |
35692fe0 JB |
3064 | register int nc; /* New character. */ |
3065 | int cnt; /* Number of changes made. */ | |
d311d28c PE |
3066 | ptrdiff_t size; /* Size of translate table. */ |
3067 | ptrdiff_t pos, pos_byte, end_pos; | |
a08d4ba7 PE |
3068 | bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters)); |
3069 | bool string_multibyte IF_LINT (= 0); | |
35692fe0 JB |
3070 | |
3071 | validate_region (&start, &end); | |
8583605b | 3072 | if (CHAR_TABLE_P (table)) |
f555f8cf KH |
3073 | { |
3074 | if (! EQ (XCHAR_TABLE (table)->purpose, Qtranslation_table)) | |
3075 | error ("Not a translation table"); | |
eb3d9ec7 | 3076 | size = MAX_CHAR; |
f555f8cf KH |
3077 | tt = NULL; |
3078 | } | |
8583605b KH |
3079 | else |
3080 | { | |
3081 | CHECK_STRING (table); | |
3082 | ||
eb3d9ec7 KH |
3083 | if (! multibyte && (SCHARS (table) < SBYTES (table))) |
3084 | table = string_make_unibyte (table); | |
8583605b KH |
3085 | string_multibyte = SCHARS (table) < SBYTES (table); |
3086 | size = SBYTES (table); | |
3087 | tt = SDATA (table); | |
3088 | } | |
35692fe0 | 3089 | |
1f24f4fd | 3090 | pos = XINT (start); |
8583605b | 3091 | pos_byte = CHAR_TO_BYTE (pos); |
e65837df | 3092 | end_pos = XINT (end); |
20edc1c9 | 3093 | modify_region_1 (pos, end_pos, false); |
35692fe0 JB |
3094 | |
3095 | cnt = 0; | |
f555f8cf | 3096 | for (; pos < end_pos; ) |
35692fe0 | 3097 | { |
ec1c14f6 | 3098 | register unsigned char *p = BYTE_POS_ADDR (pos_byte); |
8583605b KH |
3099 | unsigned char *str, buf[MAX_MULTIBYTE_LENGTH]; |
3100 | int len, str_len; | |
1f24f4fd | 3101 | int oc; |
f555f8cf | 3102 | Lisp_Object val; |
ec1c14f6 | 3103 | |
e8cce5af | 3104 | if (multibyte) |
62a6e103 | 3105 | oc = STRING_CHAR_AND_LENGTH (p, len); |
e8cce5af | 3106 | else |
eb3d9ec7 KH |
3107 | oc = *p, len = 1; |
3108 | if (oc < size) | |
35692fe0 | 3109 | { |
eb3d9ec7 | 3110 | if (tt) |
35692fe0 | 3111 | { |
fa056b08 KS |
3112 | /* Reload as signal_after_change in last iteration may GC. */ |
3113 | tt = SDATA (table); | |
8583605b | 3114 | if (string_multibyte) |
0c1e3b85 | 3115 | { |
8583605b | 3116 | str = tt + string_char_to_byte (table, oc); |
62a6e103 | 3117 | nc = STRING_CHAR_AND_LENGTH (str, str_len); |
0c1e3b85 RS |
3118 | } |
3119 | else | |
3120 | { | |
eb3d9ec7 KH |
3121 | nc = tt[oc]; |
3122 | if (! ASCII_BYTE_P (nc) && multibyte) | |
3123 | { | |
3124 | str_len = BYTE8_STRING (nc, buf); | |
3125 | str = buf; | |
3126 | } | |
3127 | else | |
3128 | { | |
3129 | str_len = 1; | |
3130 | str = tt + oc; | |
3131 | } | |
0c1e3b85 | 3132 | } |
35692fe0 | 3133 | } |
eb3d9ec7 | 3134 | else |
f555f8cf | 3135 | { |
eb3d9ec7 KH |
3136 | nc = oc; |
3137 | val = CHAR_TABLE_REF (table, oc); | |
045eb8d9 | 3138 | if (CHARACTERP (val)) |
eb3d9ec7 | 3139 | { |
045eb8d9 | 3140 | nc = XFASTINT (val); |
eb3d9ec7 KH |
3141 | str_len = CHAR_STRING (nc, buf); |
3142 | str = buf; | |
3143 | } | |
3144 | else if (VECTORP (val) || (CONSP (val))) | |
3145 | { | |
3146 | /* VAL is [TO_CHAR ...] or (([FROM-CHAR ...] . TO) ...) | |
3147 | where TO is TO-CHAR or [TO-CHAR ...]. */ | |
3148 | nc = -1; | |
3149 | } | |
f555f8cf | 3150 | } |
8583605b | 3151 | |
eb3d9ec7 | 3152 | if (nc != oc && nc >= 0) |
8583605b | 3153 | { |
f555f8cf KH |
3154 | /* Simple one char to one char translation. */ |
3155 | if (len != str_len) | |
3156 | { | |
3157 | Lisp_Object string; | |
8583605b | 3158 | |
f555f8cf KH |
3159 | /* This is less efficient, because it moves the gap, |
3160 | but it should handle multibyte characters correctly. */ | |
e7f8264d | 3161 | string = make_multibyte_string ((char *) str, 1, str_len); |
f555f8cf KH |
3162 | replace_range (pos, pos + 1, string, 1, 0, 1); |
3163 | len = str_len; | |
3164 | } | |
3165 | else | |
3166 | { | |
3167 | record_change (pos, 1); | |
3168 | while (str_len-- > 0) | |
3169 | *p++ = *str++; | |
3170 | signal_after_change (pos, 1, 1); | |
3171 | update_compositions (pos, pos + 1, CHECK_BORDER); | |
3172 | } | |
3173 | ++cnt; | |
8583605b | 3174 | } |
eb3d9ec7 | 3175 | else if (nc < 0) |
8583605b | 3176 | { |
f555f8cf KH |
3177 | Lisp_Object string; |
3178 | ||
3179 | if (CONSP (val)) | |
3180 | { | |
3181 | val = check_translation (pos, pos_byte, end_pos, val); | |
3182 | if (NILP (val)) | |
3183 | { | |
3184 | pos_byte += len; | |
3185 | pos++; | |
3186 | continue; | |
3187 | } | |
3188 | /* VAL is ([FROM-CHAR ...] . TO). */ | |
3189 | len = ASIZE (XCAR (val)); | |
3190 | val = XCDR (val); | |
3191 | } | |
3192 | else | |
3193 | len = 1; | |
3194 | ||
3195 | if (VECTORP (val)) | |
3196 | { | |
bde25748 | 3197 | string = Fconcat (1, &val); |
f555f8cf KH |
3198 | } |
3199 | else | |
3200 | { | |
3201 | string = Fmake_string (make_number (1), val); | |
3202 | } | |
3203 | replace_range (pos, pos + len, string, 1, 0, 1); | |
3204 | pos_byte += SBYTES (string); | |
3205 | pos += SCHARS (string); | |
3206 | cnt += SCHARS (string); | |
3207 | end_pos += SCHARS (string) - len; | |
3208 | continue; | |
8583605b | 3209 | } |
8583605b KH |
3210 | } |
3211 | pos_byte += len; | |
3f5409d3 | 3212 | pos++; |
35692fe0 JB |
3213 | } |
3214 | ||
ec1c14f6 | 3215 | return make_number (cnt); |
35692fe0 JB |
3216 | } |
3217 | ||
a7ca3326 | 3218 | DEFUN ("delete-region", Fdelete_region, Sdelete_region, 2, 2, "r", |
3bbd2265 | 3219 | doc: /* Delete the text between START and END. |
f0fcdf4b LMI |
3220 | If called interactively, delete the region between point and mark. |
3221 | This command deletes buffer text without modifying the kill ring. */) | |
5842a27b | 3222 | (Lisp_Object start, Lisp_Object end) |
35692fe0 | 3223 | { |
2591ec64 EN |
3224 | validate_region (&start, &end); |
3225 | del_range (XINT (start), XINT (end)); | |
35692fe0 JB |
3226 | return Qnil; |
3227 | } | |
7dae4502 SM |
3228 | |
3229 | DEFUN ("delete-and-extract-region", Fdelete_and_extract_region, | |
3230 | Sdelete_and_extract_region, 2, 2, 0, | |
7ee72033 | 3231 | doc: /* Delete the text between START and END and return it. */) |
5842a27b | 3232 | (Lisp_Object start, Lisp_Object end) |
7dae4502 SM |
3233 | { |
3234 | validate_region (&start, &end); | |
8550b998 | 3235 | if (XINT (start) == XINT (end)) |
977f6cfb | 3236 | return empty_unibyte_string; |
7dae4502 SM |
3237 | return del_range_1 (XINT (start), XINT (end), 1, 1); |
3238 | } | |
35692fe0 | 3239 | \f |
a7ca3326 | 3240 | DEFUN ("widen", Fwiden, Swiden, 0, 0, "", |
7ee72033 MB |
3241 | doc: /* Remove restrictions (narrowing) from current buffer. |
3242 | This allows the buffer's full text to be seen and edited. */) | |
5842a27b | 3243 | (void) |
35692fe0 | 3244 | { |
2cad2e34 RS |
3245 | if (BEG != BEGV || Z != ZV) |
3246 | current_buffer->clip_changed = 1; | |
35692fe0 | 3247 | BEGV = BEG; |
ec1c14f6 RS |
3248 | BEGV_BYTE = BEG_BYTE; |
3249 | SET_BUF_ZV_BOTH (current_buffer, Z, Z_BYTE); | |
52b14ac0 JB |
3250 | /* Changing the buffer bounds invalidates any recorded current column. */ |
3251 | invalidate_current_column (); | |
35692fe0 JB |
3252 | return Qnil; |
3253 | } | |
3254 | ||
a7ca3326 | 3255 | DEFUN ("narrow-to-region", Fnarrow_to_region, Snarrow_to_region, 2, 2, "r", |
7ee72033 | 3256 | doc: /* Restrict editing in this buffer to the current region. |
a1f17501 PJ |
3257 | The rest of the text becomes temporarily invisible and untouchable |
3258 | but is not deleted; if you save the buffer in a file, the invisible | |
3259 | text is included in the file. \\[widen] makes all visible again. | |
3260 | See also `save-restriction'. | |
3261 | ||
3262 | When calling from a program, pass two arguments; positions (integers | |
7ee72033 | 3263 | or markers) bounding the text that should remain visible. */) |
5842a27b | 3264 | (register Lisp_Object start, Lisp_Object end) |
35692fe0 | 3265 | { |
b7826503 PJ |
3266 | CHECK_NUMBER_COERCE_MARKER (start); |
3267 | CHECK_NUMBER_COERCE_MARKER (end); | |
35692fe0 | 3268 | |
2591ec64 | 3269 | if (XINT (start) > XINT (end)) |
35692fe0 | 3270 | { |
b5a6948e | 3271 | Lisp_Object tem; |
2591ec64 | 3272 | tem = start; start = end; end = tem; |
35692fe0 JB |
3273 | } |
3274 | ||
2591ec64 EN |
3275 | if (!(BEG <= XINT (start) && XINT (start) <= XINT (end) && XINT (end) <= Z)) |
3276 | args_out_of_range (start, end); | |
35692fe0 | 3277 | |
2cad2e34 RS |
3278 | if (BEGV != XFASTINT (start) || ZV != XFASTINT (end)) |
3279 | current_buffer->clip_changed = 1; | |
3280 | ||
ec1c14f6 | 3281 | SET_BUF_BEGV (current_buffer, XFASTINT (start)); |
2591ec64 | 3282 | SET_BUF_ZV (current_buffer, XFASTINT (end)); |
6ec8bbd2 | 3283 | if (PT < XFASTINT (start)) |
2591ec64 | 3284 | SET_PT (XFASTINT (start)); |
6ec8bbd2 | 3285 | if (PT > XFASTINT (end)) |
2591ec64 | 3286 | SET_PT (XFASTINT (end)); |
52b14ac0 JB |
3287 | /* Changing the buffer bounds invalidates any recorded current column. */ |
3288 | invalidate_current_column (); | |
35692fe0 JB |
3289 | return Qnil; |
3290 | } | |
3291 | ||
3292 | Lisp_Object | |
971de7fb | 3293 | save_restriction_save (void) |
35692fe0 | 3294 | { |
d6abb4c7 MB |
3295 | if (BEGV == BEG && ZV == Z) |
3296 | /* The common case that the buffer isn't narrowed. | |
3297 | We return just the buffer object, which save_restriction_restore | |
3298 | recognizes as meaning `no restriction'. */ | |
3299 | return Fcurrent_buffer (); | |
3300 | else | |
3301 | /* We have to save a restriction, so return a pair of markers, one | |
3302 | for the beginning and one for the end. */ | |
3303 | { | |
3304 | Lisp_Object beg, end; | |
3305 | ||
657924ff DA |
3306 | beg = build_marker (current_buffer, BEGV, BEGV_BYTE); |
3307 | end = build_marker (current_buffer, ZV, ZV_BYTE); | |
35692fe0 | 3308 | |
d6abb4c7 | 3309 | /* END must move forward if text is inserted at its exact location. */ |
5e617bc2 | 3310 | XMARKER (end)->insertion_type = 1; |
d6abb4c7 MB |
3311 | |
3312 | return Fcons (beg, end); | |
3313 | } | |
35692fe0 JB |
3314 | } |
3315 | ||
3316 | Lisp_Object | |
971de7fb | 3317 | save_restriction_restore (Lisp_Object data) |
35692fe0 | 3318 | { |
d528b1ce SM |
3319 | struct buffer *cur = NULL; |
3320 | struct buffer *buf = (CONSP (data) | |
3321 | ? XMARKER (XCAR (data))->buffer | |
3322 | : XBUFFER (data)); | |
3323 | ||
4b4deea2 | 3324 | if (buf && buf != current_buffer && !NILP (BVAR (buf, pt_marker))) |
d528b1ce SM |
3325 | { /* If `buf' uses markers to keep track of PT, BEGV, and ZV (as |
3326 | is the case if it is or has an indirect buffer), then make | |
3327 | sure it is current before we update BEGV, so | |
3328 | set_buffer_internal takes care of managing those markers. */ | |
3329 | cur = current_buffer; | |
3330 | set_buffer_internal (buf); | |
3331 | } | |
3332 | ||
d6abb4c7 MB |
3333 | if (CONSP (data)) |
3334 | /* A pair of marks bounding a saved restriction. */ | |
35692fe0 | 3335 | { |
d6abb4c7 MB |
3336 | struct Lisp_Marker *beg = XMARKER (XCAR (data)); |
3337 | struct Lisp_Marker *end = XMARKER (XCDR (data)); | |
d528b1ce | 3338 | eassert (buf == end->buffer); |
2cad2e34 | 3339 | |
63884563 RS |
3340 | if (buf /* Verify marker still points to a buffer. */ |
3341 | && (beg->charpos != BUF_BEGV (buf) || end->charpos != BUF_ZV (buf))) | |
d6abb4c7 MB |
3342 | /* The restriction has changed from the saved one, so restore |
3343 | the saved restriction. */ | |
3344 | { | |
d311d28c | 3345 | ptrdiff_t pt = BUF_PT (buf); |
d6abb4c7 MB |
3346 | |
3347 | SET_BUF_BEGV_BOTH (buf, beg->charpos, beg->bytepos); | |
3348 | SET_BUF_ZV_BOTH (buf, end->charpos, end->bytepos); | |
3349 | ||
3350 | if (pt < beg->charpos || pt > end->charpos) | |
3351 | /* The point is outside the new visible range, move it inside. */ | |
3352 | SET_BUF_PT_BOTH (buf, | |
3353 | clip_to_bounds (beg->charpos, pt, end->charpos), | |
63884563 | 3354 | clip_to_bounds (beg->bytepos, BUF_PT_BYTE (buf), |
d6abb4c7 | 3355 | end->bytepos)); |
177c0ea7 | 3356 | |
d6abb4c7 MB |
3357 | buf->clip_changed = 1; /* Remember that the narrowing changed. */ |
3358 | } | |
3628596a DA |
3359 | /* These aren't needed anymore, so don't wait for GC. */ |
3360 | free_marker (XCAR (data)); | |
3361 | free_marker (XCDR (data)); | |
3362 | free_cons (XCONS (data)); | |
d6abb4c7 MB |
3363 | } |
3364 | else | |
3365 | /* A buffer, which means that there was no old restriction. */ | |
3366 | { | |
63884563 RS |
3367 | if (buf /* Verify marker still points to a buffer. */ |
3368 | && (BUF_BEGV (buf) != BUF_BEG (buf) || BUF_ZV (buf) != BUF_Z (buf))) | |
d6abb4c7 MB |
3369 | /* The buffer has been narrowed, get rid of the narrowing. */ |
3370 | { | |
63884563 RS |
3371 | SET_BUF_BEGV_BOTH (buf, BUF_BEG (buf), BUF_BEG_BYTE (buf)); |
3372 | SET_BUF_ZV_BOTH (buf, BUF_Z (buf), BUF_Z_BYTE (buf)); | |
35692fe0 | 3373 | |
d6abb4c7 MB |
3374 | buf->clip_changed = 1; /* Remember that the narrowing changed. */ |
3375 | } | |
3376 | } | |
35692fe0 | 3377 | |
aca092ac SM |
3378 | /* Changing the buffer bounds invalidates any recorded current column. */ |
3379 | invalidate_current_column (); | |
3380 | ||
d528b1ce SM |
3381 | if (cur) |
3382 | set_buffer_internal (cur); | |
3383 | ||
35692fe0 JB |
3384 | return Qnil; |
3385 | } | |
3386 | ||
3387 | DEFUN ("save-restriction", Fsave_restriction, Ssave_restriction, 0, UNEVALLED, 0, | |
7ee72033 | 3388 | doc: /* Execute BODY, saving and restoring current buffer's restrictions. |
a1f17501 | 3389 | The buffer's restrictions make parts of the beginning and end invisible. |
9671c13a | 3390 | \(They are set up with `narrow-to-region' and eliminated with `widen'.) |
a1f17501 PJ |
3391 | This special form, `save-restriction', saves the current buffer's restrictions |
3392 | when it is entered, and restores them when it is exited. | |
3393 | So any `narrow-to-region' within BODY lasts only until the end of the form. | |
3394 | The old restrictions settings are restored | |
3395 | even in case of abnormal exit (throw or error). | |
3396 | ||
3397 | The value returned is the value of the last form in BODY. | |
3398 | ||
3399 | Note: if you are using both `save-excursion' and `save-restriction', | |
3400 | use `save-excursion' outermost: | |
33c2d29f MB |
3401 | (save-excursion (save-restriction ...)) |
3402 | ||
3403 | usage: (save-restriction &rest BODY) */) | |
5842a27b | 3404 | (Lisp_Object body) |
35692fe0 JB |
3405 | { |
3406 | register Lisp_Object val; | |
d311d28c | 3407 | ptrdiff_t count = SPECPDL_INDEX (); |
35692fe0 JB |
3408 | |
3409 | record_unwind_protect (save_restriction_restore, save_restriction_save ()); | |
3410 | val = Fprogn (body); | |
3411 | return unbind_to (count, val); | |
3412 | } | |
3413 | \f | |
0ae83348 | 3414 | /* Buffer for the most recent text displayed by Fmessage_box. */ |
671fbc4d KH |
3415 | static char *message_text; |
3416 | ||
3417 | /* Allocated length of that buffer. */ | |
c9f8d652 | 3418 | static ptrdiff_t message_length; |
671fbc4d | 3419 | |
a7ca3326 | 3420 | DEFUN ("message", Fmessage, Smessage, 1, MANY, 0, |
db18da59 | 3421 | doc: /* Display a message at the bottom of the screen. |
d56f2e49 GM |
3422 | The message also goes into the `*Messages*' buffer, if `message-log-max' |
3423 | is non-nil. (In keyboard macros, that's all it does.) | |
db18da59 | 3424 | Return the message. |
281c1721 | 3425 | |
a1f17501 PJ |
3426 | The first argument is a format control string, and the rest are data |
3427 | to be formatted under control of the string. See `format' for details. | |
3428 | ||
7bd5bcfb KS |
3429 | Note: Use (message "%s" VALUE) to print the value of expressions and |
3430 | variables to avoid accidentally interpreting `%' as format specifiers. | |
3431 | ||
fa056b08 KS |
3432 | If the first argument is nil or the empty string, the function clears |
3433 | any existing message; this lets the minibuffer contents show. See | |
3434 | also `current-message'. | |
4bfbe194 | 3435 | |
867b9600 | 3436 | usage: (message FORMAT-STRING &rest ARGS) */) |
f66c7cf8 | 3437 | (ptrdiff_t nargs, Lisp_Object *args) |
35692fe0 | 3438 | { |
6076e561 RS |
3439 | if (NILP (args[0]) |
3440 | || (STRINGP (args[0]) | |
3441 | && SBYTES (args[0]) == 0)) | |
f0250249 JB |
3442 | { |
3443 | message (0); | |
674a954a | 3444 | return args[0]; |
f0250249 | 3445 | } |
ccdac5be JB |
3446 | else |
3447 | { | |
3448 | register Lisp_Object val; | |
304f1f12 | 3449 | val = Fformat (nargs, args); |
d5db4077 | 3450 | message3 (val, SBYTES (val), STRING_MULTIBYTE (val)); |
ccdac5be JB |
3451 | return val; |
3452 | } | |
35692fe0 JB |
3453 | } |
3454 | ||
cacc3e2c | 3455 | DEFUN ("message-box", Fmessage_box, Smessage_box, 1, MANY, 0, |
7ee72033 | 3456 | doc: /* Display a message, in a dialog box if possible. |
a1f17501 PJ |
3457 | If a dialog box is not available, use the echo area. |
3458 | The first argument is a format control string, and the rest are data | |
3459 | to be formatted under control of the string. See `format' for details. | |
3460 | ||
fa056b08 KS |
3461 | If the first argument is nil or the empty string, clear any existing |
3462 | message; let the minibuffer contents show. | |
4bfbe194 | 3463 | |
867b9600 | 3464 | usage: (message-box FORMAT-STRING &rest ARGS) */) |
f66c7cf8 | 3465 | (ptrdiff_t nargs, Lisp_Object *args) |
cacc3e2c RS |
3466 | { |
3467 | if (NILP (args[0])) | |
3468 | { | |
3469 | message (0); | |
3470 | return Qnil; | |
3471 | } | |
3472 | else | |
3473 | { | |
3474 | register Lisp_Object val; | |
3475 | val = Fformat (nargs, args); | |
f8250f01 | 3476 | #ifdef HAVE_MENUS |
0ae83348 EZ |
3477 | /* The MS-DOS frames support popup menus even though they are |
3478 | not FRAME_WINDOW_P. */ | |
3479 | if (FRAME_WINDOW_P (XFRAME (selected_frame)) | |
3480 | || FRAME_MSDOS_P (XFRAME (selected_frame))) | |
cacc3e2c | 3481 | { |
f838ed7b | 3482 | Lisp_Object pane, menu; |
cacc3e2c RS |
3483 | struct gcpro gcpro1; |
3484 | pane = Fcons (Fcons (build_string ("OK"), Qt), Qnil); | |
3485 | GCPRO1 (pane); | |
3486 | menu = Fcons (val, pane); | |
f838ed7b | 3487 | Fx_popup_dialog (Qt, menu, Qt); |
cacc3e2c RS |
3488 | UNGCPRO; |
3489 | return val; | |
3490 | } | |
0ae83348 | 3491 | #endif /* HAVE_MENUS */ |
cacc3e2c | 3492 | /* Copy the data so that it won't move when we GC. */ |
d5db4077 | 3493 | if (SBYTES (val) > message_length) |
cacc3e2c | 3494 | { |
38182d90 PE |
3495 | ptrdiff_t new_length = SBYTES (val) + 80; |
3496 | message_text = xrealloc (message_text, new_length); | |
3497 | message_length = new_length; | |
cacc3e2c | 3498 | } |
72af86bd | 3499 | memcpy (message_text, SDATA (val), SBYTES (val)); |
d5db4077 | 3500 | message2 (message_text, SBYTES (val), |
d13a8480 | 3501 | STRING_MULTIBYTE (val)); |
cacc3e2c | 3502 | return val; |
cacc3e2c RS |
3503 | } |
3504 | } | |
f8250f01 | 3505 | |
cacc3e2c | 3506 | DEFUN ("message-or-box", Fmessage_or_box, Smessage_or_box, 1, MANY, 0, |
7ee72033 | 3507 | doc: /* Display a message in a dialog box or in the echo area. |
a1f17501 PJ |
3508 | If this command was invoked with the mouse, use a dialog box if |
3509 | `use-dialog-box' is non-nil. | |
3510 | Otherwise, use the echo area. | |
3511 | The first argument is a format control string, and the rest are data | |
3512 | to be formatted under control of the string. See `format' for details. | |
3513 | ||
fa056b08 KS |
3514 | If the first argument is nil or the empty string, clear any existing |
3515 | message; let the minibuffer contents show. | |
4bfbe194 | 3516 | |
867b9600 | 3517 | usage: (message-or-box FORMAT-STRING &rest ARGS) */) |
f66c7cf8 | 3518 | (ptrdiff_t nargs, Lisp_Object *args) |
cacc3e2c | 3519 | { |
f8250f01 | 3520 | #ifdef HAVE_MENUS |
5920df33 | 3521 | if ((NILP (last_nonmenu_event) || CONSP (last_nonmenu_event)) |
c01fbf95 | 3522 | && use_dialog_box) |
0a56ee6b | 3523 | return Fmessage_box (nargs, args); |
cacc3e2c RS |
3524 | #endif |
3525 | return Fmessage (nargs, args); | |
3526 | } | |
3527 | ||
a7ca3326 | 3528 | DEFUN ("current-message", Fcurrent_message, Scurrent_message, 0, 0, 0, |
7ee72033 | 3529 | doc: /* Return the string currently displayed in the echo area, or nil if none. */) |
5842a27b | 3530 | (void) |
b14dda8a | 3531 | { |
0634a78e | 3532 | return current_message (); |
b14dda8a RS |
3533 | } |
3534 | ||
2d9811c4 | 3535 | |
d2936d21 | 3536 | DEFUN ("propertize", Fpropertize, Spropertize, 1, MANY, 0, |
7ee72033 | 3537 | doc: /* Return a copy of STRING with text properties added. |
a1f17501 PJ |
3538 | First argument is the string to copy. |
3539 | Remaining arguments form a sequence of PROPERTY VALUE pairs for text | |
4bfbe194 MB |
3540 | properties to add to the result. |
3541 | usage: (propertize STRING &rest PROPERTIES) */) | |
f66c7cf8 | 3542 | (ptrdiff_t nargs, Lisp_Object *args) |
2d9811c4 GM |
3543 | { |
3544 | Lisp_Object properties, string; | |
3545 | struct gcpro gcpro1, gcpro2; | |
f66c7cf8 | 3546 | ptrdiff_t i; |
2d9811c4 GM |
3547 | |
3548 | /* Number of args must be odd. */ | |
c5101a77 | 3549 | if ((nargs & 1) == 0) |
2d9811c4 GM |
3550 | error ("Wrong number of arguments"); |
3551 | ||
3552 | properties = string = Qnil; | |
3553 | GCPRO2 (properties, string); | |
34a7a267 | 3554 | |
2d9811c4 | 3555 | /* First argument must be a string. */ |
b7826503 | 3556 | CHECK_STRING (args[0]); |
2d9811c4 GM |
3557 | string = Fcopy_sequence (args[0]); |
3558 | ||
3559 | for (i = 1; i < nargs; i += 2) | |
9b7a2369 | 3560 | properties = Fcons (args[i], Fcons (args[i + 1], properties)); |
2d9811c4 GM |
3561 | |
3562 | Fadd_text_properties (make_number (0), | |
d5db4077 | 3563 | make_number (SCHARS (string)), |
2d9811c4 GM |
3564 | properties, string); |
3565 | RETURN_UNGCPRO (string); | |
3566 | } | |
3567 | ||
a7ca3326 | 3568 | DEFUN ("format", Fformat, Sformat, 1, MANY, 0, |
867b9600 JL |
3569 | doc: /* Format a string out of a format-string and arguments. |
3570 | The first argument is a format control string. | |
a1f17501 | 3571 | The other arguments are substituted into it to make the result, a string. |
575b782f CY |
3572 | |
3573 | The format control string may contain %-sequences meaning to substitute | |
3574 | the next available argument: | |
3575 | ||
a1f17501 PJ |
3576 | %s means print a string argument. Actually, prints any object, with `princ'. |
3577 | %d means print as number in decimal (%o octal, %x hex). | |
3578 | %X is like %x, but uses upper case. | |
3579 | %e means print a number in exponential notation. | |
3580 | %f means print a number in decimal-point notation. | |
3581 | %g means print a number in exponential notation | |
3582 | or decimal-point notation, whichever uses fewer characters. | |
3583 | %c means print a number as a single character. | |
3584 | %S means print any object as an s-expression (using `prin1'). | |
575b782f CY |
3585 | |
3586 | The argument used for %d, %o, %x, %e, %f, %g or %c must be a number. | |
4bfbe194 MB |
3587 | Use %% to put a single % into the output. |
3588 | ||
575b782f CY |
3589 | A %-sequence may contain optional flag, width, and precision |
3590 | specifiers, as follows: | |
3591 | ||
3592 | %<flags><width><precision>character | |
3593 | ||
3594 | where flags is [+ #-0]+, width is [0-9]+, and precision is .[0-9]+ | |
3595 | ||
3596 | The + flag character inserts a + before any positive number, while a | |
3597 | space inserts a space before any positive number; these flags only | |
3598 | affect %d, %e, %f, and %g sequences, and the + flag takes precedence. | |
55802e4a CY |
3599 | The - and 0 flags affect the width specifier, as described below. |
3600 | ||
575b782f | 3601 | The # flag means to use an alternate display form for %o, %x, %X, %e, |
55802e4a CY |
3602 | %f, and %g sequences: for %o, it ensures that the result begins with |
3603 | \"0\"; for %x and %X, it prefixes the result with \"0x\" or \"0X\"; | |
3604 | for %e, %f, and %g, it causes a decimal point to be included even if | |
3605 | the precision is zero. | |
575b782f CY |
3606 | |
3607 | The width specifier supplies a lower limit for the length of the | |
3608 | printed representation. The padding, if any, normally goes on the | |
3609 | left, but it goes on the right if the - flag is present. The padding | |
3610 | character is normally a space, but it is 0 if the 0 flag is present. | |
a9ab721e LMI |
3611 | The 0 flag is ignored if the - flag is present, or the format sequence |
3612 | is something other than %d, %e, %f, and %g. | |
575b782f CY |
3613 | |
3614 | For %e, %f, and %g sequences, the number after the "." in the | |
3615 | precision specifier says how many decimal places to show; if zero, the | |
3616 | decimal point itself is omitted. For %s and %S, the precision | |
3617 | specifier truncates the string to the given width. | |
f555f8cf | 3618 | |
4bfbe194 | 3619 | usage: (format STRING &rest OBJECTS) */) |
f66c7cf8 | 3620 | (ptrdiff_t nargs, Lisp_Object *args) |
35692fe0 | 3621 | { |
f66c7cf8 | 3622 | ptrdiff_t n; /* The number of the next arg to substitute */ |
37910ab2 PE |
3623 | char initial_buffer[4000]; |
3624 | char *buf = initial_buffer; | |
d311d28c PE |
3625 | ptrdiff_t bufsize = sizeof initial_buffer; |
3626 | ptrdiff_t max_bufsize = STRING_BYTES_BOUND + 1; | |
37910ab2 PE |
3627 | char *p; |
3628 | Lisp_Object buf_save_value IF_LINT (= {0}); | |
fce31d69 | 3629 | char *format, *end, *format_start; |
d311d28c | 3630 | ptrdiff_t formatlen, nchars; |
fce31d69 PE |
3631 | /* True if the format is multibyte. */ |
3632 | bool multibyte_format = 0; | |
3633 | /* True if the output should be a multibyte string, | |
1f24f4fd | 3634 | which is true if any of the inputs is one. */ |
fce31d69 | 3635 | bool multibyte = 0; |
8f2917e4 KH |
3636 | /* When we make a multibyte string, we must pay attention to the |
3637 | byte combining problem, i.e., a byte may be combined with a | |
3b59c351 | 3638 | multibyte character of the previous string. This flag tells if we |
8f2917e4 | 3639 | must consider such a situation or not. */ |
a08d4ba7 | 3640 | bool maybe_combine_byte; |
8d6179dc | 3641 | Lisp_Object val; |
a08d4ba7 | 3642 | bool arg_intervals = 0; |
7e2c051b | 3643 | USE_SAFE_ALLOCA; |
d147ee84 RS |
3644 | |
3645 | /* discarded[I] is 1 if byte I of the format | |
3646 | string was not copied into the output. | |
3647 | It is 2 if byte I was not the first byte of its character. */ | |
37910ab2 | 3648 | char *discarded; |
d147ee84 RS |
3649 | |
3650 | /* Each element records, for one argument, | |
3651 | the start and end bytepos in the output string, | |
37910ab2 | 3652 | whether the argument has been converted to string (e.g., due to "%S"), |
d147ee84 RS |
3653 | and whether the argument is a string with intervals. |
3654 | info[0] is unused. Unused elements have -1 for start. */ | |
5e6d5493 GM |
3655 | struct info |
3656 | { | |
d311d28c | 3657 | ptrdiff_t start, end; |
a08d4ba7 PE |
3658 | unsigned converted_to_string : 1; |
3659 | unsigned intervals : 1; | |
5e6d5493 | 3660 | } *info = 0; |
1f24f4fd | 3661 | |
35692fe0 JB |
3662 | /* It should not be necessary to GCPRO ARGS, because |
3663 | the caller in the interpreter should take care of that. */ | |
3664 | ||
37910ab2 PE |
3665 | CHECK_STRING (args[0]); |
3666 | format_start = SSDATA (args[0]); | |
3667 | formatlen = SBYTES (args[0]); | |
3668 | ||
3669 | /* Allocate the info and discarded tables. */ | |
3670 | { | |
f66c7cf8 | 3671 | ptrdiff_t i; |
37910ab2 | 3672 | if ((SIZE_MAX - formatlen) / sizeof (struct info) <= nargs) |
531b0165 | 3673 | memory_full (SIZE_MAX); |
98c6f1e3 | 3674 | info = SAFE_ALLOCA ((nargs + 1) * sizeof *info + formatlen); |
37910ab2 PE |
3675 | discarded = (char *) &info[nargs + 1]; |
3676 | for (i = 0; i < nargs + 1; i++) | |
3677 | { | |
3678 | info[i].start = -1; | |
3679 | info[i].intervals = info[i].converted_to_string = 0; | |
3680 | } | |
3681 | memset (discarded, 0, formatlen); | |
3682 | } | |
3683 | ||
e781c49e RS |
3684 | /* Try to determine whether the result should be multibyte. |
3685 | This is not always right; sometimes the result needs to be multibyte | |
3686 | because of an object that we will pass through prin1, | |
3687 | and in that case, we won't know it here. */ | |
37910ab2 PE |
3688 | multibyte_format = STRING_MULTIBYTE (args[0]); |
3689 | multibyte = multibyte_format; | |
3690 | for (n = 1; !multibyte && n < nargs; n++) | |
3691 | if (STRINGP (args[n]) && STRING_MULTIBYTE (args[n])) | |
3692 | multibyte = 1; | |
67965a98 | 3693 | |
e781c49e | 3694 | /* If we start out planning a unibyte result, |
37910ab2 | 3695 | then discover it has to be multibyte, we jump back to retry. */ |
e781c49e RS |
3696 | retry: |
3697 | ||
37910ab2 PE |
3698 | p = buf; |
3699 | nchars = 0; | |
3700 | n = 0; | |
d147ee84 | 3701 | |
37910ab2 PE |
3702 | /* Scan the format and store result in BUF. */ |
3703 | format = format_start; | |
3704 | end = format + formatlen; | |
3705 | maybe_combine_byte = 0; | |
35692fe0 | 3706 | |
35692fe0 | 3707 | while (format != end) |
37910ab2 PE |
3708 | { |
3709 | /* The values of N and FORMAT when the loop body is entered. */ | |
f66c7cf8 | 3710 | ptrdiff_t n0 = n; |
37910ab2 | 3711 | char *format0 = format; |
35692fe0 | 3712 | |
37910ab2 | 3713 | /* Bytes needed to represent the output of this conversion. */ |
d311d28c | 3714 | ptrdiff_t convbytes; |
537dfb13 | 3715 | |
37910ab2 PE |
3716 | if (*format == '%') |
3717 | { | |
3718 | /* General format specifications look like | |
a432bfe5 | 3719 | |
37910ab2 | 3720 | '%' [flags] [field-width] [precision] format |
a432bfe5 | 3721 | |
37910ab2 | 3722 | where |
a432bfe5 | 3723 | |
37910ab2 PE |
3724 | flags ::= [-+0# ]+ |
3725 | field-width ::= [0-9]+ | |
3726 | precision ::= '.' [0-9]* | |
a432bfe5 | 3727 | |
37910ab2 PE |
3728 | If a field-width is specified, it specifies to which width |
3729 | the output should be padded with blanks, if the output | |
3730 | string is shorter than field-width. | |
a432bfe5 | 3731 | |
37910ab2 PE |
3732 | If precision is specified, it specifies the number of |
3733 | digits to print after the '.' for floats, or the max. | |
3734 | number of chars to print from a string. */ | |
a432bfe5 | 3735 | |
a08d4ba7 PE |
3736 | bool minus_flag = 0; |
3737 | bool plus_flag = 0; | |
3738 | bool space_flag = 0; | |
3739 | bool sharp_flag = 0; | |
3740 | bool zero_flag = 0; | |
d311d28c | 3741 | ptrdiff_t field_width; |
a08d4ba7 | 3742 | bool precision_given; |
37910ab2 PE |
3743 | uintmax_t precision = UINTMAX_MAX; |
3744 | char *num_end; | |
3745 | char conversion; | |
a432bfe5 | 3746 | |
37910ab2 PE |
3747 | while (1) |
3748 | { | |
3749 | switch (*++format) | |
3750 | { | |
3751 | case '-': minus_flag = 1; continue; | |
3752 | case '+': plus_flag = 1; continue; | |
3753 | case ' ': space_flag = 1; continue; | |
3754 | case '#': sharp_flag = 1; continue; | |
3755 | case '0': zero_flag = 1; continue; | |
3756 | } | |
3757 | break; | |
3758 | } | |
35692fe0 | 3759 | |
37910ab2 PE |
3760 | /* Ignore flags when sprintf ignores them. */ |
3761 | space_flag &= ~ plus_flag; | |
3762 | zero_flag &= ~ minus_flag; | |
1f24f4fd | 3763 | |
35692fe0 | 3764 | { |
37910ab2 PE |
3765 | uintmax_t w = strtoumax (format, &num_end, 10); |
3766 | if (max_bufsize <= w) | |
3767 | string_overflow (); | |
3768 | field_width = w; | |
35692fe0 | 3769 | } |
37910ab2 PE |
3770 | precision_given = *num_end == '.'; |
3771 | if (precision_given) | |
3772 | precision = strtoumax (num_end + 1, &num_end, 10); | |
3773 | format = num_end; | |
3774 | ||
3775 | if (format == end) | |
3776 | error ("Format string ends in middle of format specifier"); | |
3777 | ||
3778 | memset (&discarded[format0 - format_start], 1, format - format0); | |
3779 | conversion = *format; | |
3780 | if (conversion == '%') | |
7812ba2d | 3781 | goto copy_char; |
d147ee84 | 3782 | discarded[format - format_start] = 1; |
1f24f4fd | 3783 | format++; |
fb893977 | 3784 | |
37910ab2 PE |
3785 | ++n; |
3786 | if (! (n < nargs)) | |
3787 | error ("Not enough arguments for format string"); | |
3788 | ||
3789 | /* For 'S', prin1 the argument, and then treat like 's'. | |
3790 | For 's', princ any argument that is not a string or | |
3791 | symbol. But don't do this conversion twice, which might | |
3792 | happen after retrying. */ | |
3793 | if ((conversion == 'S' | |
3794 | || (conversion == 's' | |
3795 | && ! STRINGP (args[n]) && ! SYMBOLP (args[n])))) | |
f555f8cf | 3796 | { |
37910ab2 | 3797 | if (! info[n].converted_to_string) |
f555f8cf | 3798 | { |
37910ab2 PE |
3799 | Lisp_Object noescape = conversion == 'S' ? Qnil : Qt; |
3800 | args[n] = Fprin1_to_string (args[n], noescape); | |
3801 | info[n].converted_to_string = 1; | |
3802 | if (STRING_MULTIBYTE (args[n]) && ! multibyte) | |
3803 | { | |
3804 | multibyte = 1; | |
3805 | goto retry; | |
3806 | } | |
f555f8cf | 3807 | } |
37910ab2 | 3808 | conversion = 's'; |
f555f8cf | 3809 | } |
37910ab2 PE |
3810 | else if (conversion == 'c') |
3811 | { | |
3812 | if (FLOATP (args[n])) | |
3813 | { | |
3814 | double d = XFLOAT_DATA (args[n]); | |
3815 | args[n] = make_number (FIXNUM_OVERFLOW_P (d) ? -1 : d); | |
3816 | } | |
f555f8cf | 3817 | |
37910ab2 PE |
3818 | if (INTEGERP (args[n]) && ! ASCII_CHAR_P (XINT (args[n]))) |
3819 | { | |
3820 | if (!multibyte) | |
3821 | { | |
3822 | multibyte = 1; | |
3823 | goto retry; | |
3824 | } | |
3825 | args[n] = Fchar_to_string (args[n]); | |
3826 | info[n].converted_to_string = 1; | |
3827 | } | |
f555f8cf | 3828 | |
37910ab2 PE |
3829 | if (info[n].converted_to_string) |
3830 | conversion = 's'; | |
3831 | zero_flag = 0; | |
d147ee84 | 3832 | } |
35692fe0 | 3833 | |
37910ab2 | 3834 | if (SYMBOLP (args[n])) |
1f24f4fd | 3835 | { |
37910ab2 PE |
3836 | args[n] = SYMBOL_NAME (args[n]); |
3837 | if (STRING_MULTIBYTE (args[n]) && ! multibyte) | |
3838 | { | |
3839 | multibyte = 1; | |
3840 | goto retry; | |
3841 | } | |
1f24f4fd RS |
3842 | } |
3843 | ||
37910ab2 | 3844 | if (conversion == 's') |
1f24f4fd | 3845 | { |
ac42d7b9 KG |
3846 | /* handle case (precision[n] >= 0) */ |
3847 | ||
d311d28c PE |
3848 | ptrdiff_t width, padding, nbytes; |
3849 | ptrdiff_t nchars_string; | |
ac42d7b9 | 3850 | |
d311d28c PE |
3851 | ptrdiff_t prec = -1; |
3852 | if (precision_given && precision <= TYPE_MAXIMUM (ptrdiff_t)) | |
37910ab2 PE |
3853 | prec = precision; |
3854 | ||
ac42d7b9 KG |
3855 | /* lisp_string_width ignores a precision of 0, but GNU |
3856 | libc functions print 0 characters when the precision | |
3857 | is 0. Imitate libc behavior here. Changing | |
3858 | lisp_string_width is the right thing, and will be | |
3859 | done, but meanwhile we work with it. */ | |
3860 | ||
37910ab2 | 3861 | if (prec == 0) |
ac42d7b9 | 3862 | width = nchars_string = nbytes = 0; |
ac42d7b9 | 3863 | else |
37910ab2 | 3864 | { |
d311d28c | 3865 | ptrdiff_t nch, nby; |
37910ab2 PE |
3866 | width = lisp_string_width (args[n], prec, &nch, &nby); |
3867 | if (prec < 0) | |
3868 | { | |
3869 | nchars_string = SCHARS (args[n]); | |
3870 | nbytes = SBYTES (args[n]); | |
3871 | } | |
3872 | else | |
3873 | { | |
3874 | nchars_string = nch; | |
3875 | nbytes = nby; | |
3876 | } | |
ac42d7b9 | 3877 | } |
25c9e7fb | 3878 | |
37910ab2 PE |
3879 | convbytes = nbytes; |
3880 | if (convbytes && multibyte && ! STRING_MULTIBYTE (args[n])) | |
3881 | convbytes = count_size_as_multibyte (SDATA (args[n]), nbytes); | |
1f24f4fd | 3882 | |
37910ab2 | 3883 | padding = width < field_width ? field_width - width : 0; |
ac42d7b9 | 3884 | |
37910ab2 PE |
3885 | if (max_bufsize - padding <= convbytes) |
3886 | string_overflow (); | |
3887 | convbytes += padding; | |
3888 | if (convbytes <= buf + bufsize - p) | |
3889 | { | |
3890 | if (! minus_flag) | |
3891 | { | |
3892 | memset (p, ' ', padding); | |
3893 | p += padding; | |
3894 | nchars += padding; | |
3895 | } | |
ac42d7b9 | 3896 | |
37910ab2 PE |
3897 | if (p > buf |
3898 | && multibyte | |
3899 | && !ASCII_BYTE_P (*((unsigned char *) p - 1)) | |
3900 | && STRING_MULTIBYTE (args[n]) | |
3901 | && !CHAR_HEAD_P (SREF (args[n], 0))) | |
3902 | maybe_combine_byte = 1; | |
1f24f4fd | 3903 | |
37910ab2 PE |
3904 | p += copy_text (SDATA (args[n]), (unsigned char *) p, |
3905 | nbytes, | |
3906 | STRING_MULTIBYTE (args[n]), multibyte); | |
8f2c9ed8 | 3907 | |
37910ab2 PE |
3908 | info[n].start = nchars; |
3909 | nchars += nchars_string; | |
3910 | info[n].end = nchars; | |
3911 | ||
3912 | if (minus_flag) | |
3913 | { | |
3914 | memset (p, ' ', padding); | |
3915 | p += padding; | |
3916 | nchars += padding; | |
3917 | } | |
5e6d5493 | 3918 | |
37910ab2 PE |
3919 | /* If this argument has text properties, record where |
3920 | in the result string it appears. */ | |
0c94c8d6 | 3921 | if (string_intervals (args[n])) |
37910ab2 PE |
3922 | info[n].intervals = arg_intervals = 1; |
3923 | ||
3924 | continue; | |
3925 | } | |
1f24f4fd | 3926 | } |
37910ab2 PE |
3927 | else if (! (conversion == 'c' || conversion == 'd' |
3928 | || conversion == 'e' || conversion == 'f' | |
3929 | || conversion == 'g' || conversion == 'i' | |
3930 | || conversion == 'o' || conversion == 'x' | |
3931 | || conversion == 'X')) | |
3932 | error ("Invalid format operation %%%c", | |
3933 | STRING_CHAR ((unsigned char *) format - 1)); | |
3934 | else if (! (INTEGERP (args[n]) || FLOATP (args[n]))) | |
3935 | error ("Format specifier doesn't match argument type"); | |
3936 | else | |
1f24f4fd | 3937 | { |
37910ab2 PE |
3938 | enum |
3939 | { | |
3940 | /* Maximum precision for a %f conversion such that the | |
333f9019 | 3941 | trailing output digit might be nonzero. Any precision |
37910ab2 PE |
3942 | larger than this will not yield useful information. */ |
3943 | USEFUL_PRECISION_MAX = | |
3944 | ((1 - DBL_MIN_EXP) | |
3945 | * (FLT_RADIX == 2 || FLT_RADIX == 10 ? 1 | |
3946 | : FLT_RADIX == 16 ? 4 | |
3947 | : -1)), | |
3948 | ||
3949 | /* Maximum number of bytes generated by any format, if | |
81f7c12e | 3950 | precision is no more than USEFUL_PRECISION_MAX. |
37910ab2 PE |
3951 | On all practical hosts, %f is the worst case. */ |
3952 | SPRINTF_BUFSIZE = | |
a81d11a3 PE |
3953 | sizeof "-." + (DBL_MAX_10_EXP + 1) + USEFUL_PRECISION_MAX, |
3954 | ||
3955 | /* Length of pM (that is, of pMd without the | |
3956 | trailing "d"). */ | |
3957 | pMlen = sizeof pMd - 2 | |
37910ab2 PE |
3958 | }; |
3959 | verify (0 < USEFUL_PRECISION_MAX); | |
3960 | ||
3961 | int prec; | |
d311d28c | 3962 | ptrdiff_t padding, sprintf_bytes; |
37910ab2 PE |
3963 | uintmax_t excess_precision, numwidth; |
3964 | uintmax_t leading_zeros = 0, trailing_zeros = 0; | |
3965 | ||
3966 | char sprintf_buf[SPRINTF_BUFSIZE]; | |
3967 | ||
3968 | /* Copy of conversion specification, modified somewhat. | |
3969 | At most three flags F can be specified at once. */ | |
a81d11a3 | 3970 | char convspec[sizeof "%FFF.*d" + pMlen]; |
37910ab2 PE |
3971 | |
3972 | /* Avoid undefined behavior in underlying sprintf. */ | |
3973 | if (conversion == 'd' || conversion == 'i') | |
3974 | sharp_flag = 0; | |
3975 | ||
3976 | /* Create the copy of the conversion specification, with | |
3977 | any width and precision removed, with ".*" inserted, | |
a81d11a3 | 3978 | and with pM inserted for integer formats. */ |
37910ab2 PE |
3979 | { |
3980 | char *f = convspec; | |
3981 | *f++ = '%'; | |
3982 | *f = '-'; f += minus_flag; | |
3983 | *f = '+'; f += plus_flag; | |
3984 | *f = ' '; f += space_flag; | |
3985 | *f = '#'; f += sharp_flag; | |
3986 | *f = '0'; f += zero_flag; | |
3987 | *f++ = '.'; | |
3988 | *f++ = '*'; | |
3989 | if (conversion == 'd' || conversion == 'i' | |
3990 | || conversion == 'o' || conversion == 'x' | |
3991 | || conversion == 'X') | |
3992 | { | |
a81d11a3 PE |
3993 | memcpy (f, pMd, pMlen); |
3994 | f += pMlen; | |
37910ab2 PE |
3995 | zero_flag &= ~ precision_given; |
3996 | } | |
3997 | *f++ = conversion; | |
3998 | *f = '\0'; | |
3999 | } | |
1f24f4fd | 4000 | |
37910ab2 PE |
4001 | prec = -1; |
4002 | if (precision_given) | |
4003 | prec = min (precision, USEFUL_PRECISION_MAX); | |
4004 | ||
4005 | /* Use sprintf to format this number into sprintf_buf. Omit | |
4006 | padding and excess precision, though, because sprintf limits | |
4007 | output length to INT_MAX. | |
4008 | ||
4009 | There are four types of conversion: double, unsigned | |
4010 | char (passed as int), wide signed int, and wide | |
4011 | unsigned int. Treat them separately because the | |
4012 | sprintf ABI is sensitive to which type is passed. Be | |
4013 | careful about integer overflow, NaNs, infinities, and | |
4014 | conversions; for example, the min and max macros are | |
4015 | not suitable here. */ | |
4016 | if (conversion == 'e' || conversion == 'f' || conversion == 'g') | |
4017 | { | |
4018 | double x = (INTEGERP (args[n]) | |
4019 | ? XINT (args[n]) | |
4020 | : XFLOAT_DATA (args[n])); | |
4021 | sprintf_bytes = sprintf (sprintf_buf, convspec, prec, x); | |
4022 | } | |
4023 | else if (conversion == 'c') | |
4024 | { | |
4025 | /* Don't use sprintf here, as it might mishandle prec. */ | |
4026 | sprintf_buf[0] = XINT (args[n]); | |
4027 | sprintf_bytes = prec != 0; | |
4028 | } | |
4029 | else if (conversion == 'd') | |
4030 | { | |
4031 | /* For float, maybe we should use "%1.0f" | |
4032 | instead so it also works for values outside | |
4033 | the integer range. */ | |
a81d11a3 | 4034 | printmax_t x; |
37910ab2 PE |
4035 | if (INTEGERP (args[n])) |
4036 | x = XINT (args[n]); | |
4037 | else | |
4038 | { | |
4039 | double d = XFLOAT_DATA (args[n]); | |
4040 | if (d < 0) | |
4041 | { | |
a81d11a3 | 4042 | x = TYPE_MINIMUM (printmax_t); |
37910ab2 PE |
4043 | if (x < d) |
4044 | x = d; | |
4045 | } | |
4046 | else | |
4047 | { | |
a81d11a3 | 4048 | x = TYPE_MAXIMUM (printmax_t); |
37910ab2 PE |
4049 | if (d < x) |
4050 | x = d; | |
4051 | } | |
4052 | } | |
4053 | sprintf_bytes = sprintf (sprintf_buf, convspec, prec, x); | |
4054 | } | |
0f860bd7 | 4055 | else |
de92d4d4 | 4056 | { |
37910ab2 | 4057 | /* Don't sign-extend for octal or hex printing. */ |
a81d11a3 | 4058 | uprintmax_t x; |
37910ab2 PE |
4059 | if (INTEGERP (args[n])) |
4060 | x = XUINT (args[n]); | |
4061 | else | |
0f860bd7 | 4062 | { |
37910ab2 PE |
4063 | double d = XFLOAT_DATA (args[n]); |
4064 | if (d < 0) | |
4065 | x = 0; | |
4066 | else | |
4067 | { | |
a81d11a3 | 4068 | x = TYPE_MAXIMUM (uprintmax_t); |
37910ab2 PE |
4069 | if (d < x) |
4070 | x = d; | |
4071 | } | |
0f860bd7 | 4072 | } |
37910ab2 PE |
4073 | sprintf_bytes = sprintf (sprintf_buf, convspec, prec, x); |
4074 | } | |
0f860bd7 | 4075 | |
37910ab2 PE |
4076 | /* Now the length of the formatted item is known, except it omits |
4077 | padding and excess precision. Deal with excess precision | |
4078 | first. This happens only when the format specifies | |
4079 | ridiculously large precision. */ | |
4080 | excess_precision = precision - prec; | |
4081 | if (excess_precision) | |
4082 | { | |
4083 | if (conversion == 'e' || conversion == 'f' | |
4084 | || conversion == 'g') | |
ff6e6ac8 | 4085 | { |
37910ab2 PE |
4086 | if ((conversion == 'g' && ! sharp_flag) |
4087 | || ! ('0' <= sprintf_buf[sprintf_bytes - 1] | |
4088 | && sprintf_buf[sprintf_bytes - 1] <= '9')) | |
4089 | excess_precision = 0; | |
ff6e6ac8 | 4090 | else |
37910ab2 PE |
4091 | { |
4092 | if (conversion == 'g') | |
4093 | { | |
4094 | char *dot = strchr (sprintf_buf, '.'); | |
4095 | if (!dot) | |
4096 | excess_precision = 0; | |
4097 | } | |
4098 | } | |
4099 | trailing_zeros = excess_precision; | |
ff6e6ac8 | 4100 | } |
de92d4d4 | 4101 | else |
37910ab2 | 4102 | leading_zeros = excess_precision; |
de92d4d4 | 4103 | } |
1f24f4fd | 4104 | |
37910ab2 PE |
4105 | /* Compute the total bytes needed for this item, including |
4106 | excess precision and padding. */ | |
4107 | numwidth = sprintf_bytes + excess_precision; | |
4108 | padding = numwidth < field_width ? field_width - numwidth : 0; | |
4109 | if (max_bufsize - sprintf_bytes <= excess_precision | |
4110 | || max_bufsize - padding <= numwidth) | |
4111 | string_overflow (); | |
4112 | convbytes = numwidth + padding; | |
4113 | ||
4114 | if (convbytes <= buf + bufsize - p) | |
4115 | { | |
4116 | /* Copy the formatted item from sprintf_buf into buf, | |
4117 | inserting padding and excess-precision zeros. */ | |
4118 | ||
4119 | char *src = sprintf_buf; | |
4120 | char src0 = src[0]; | |
4121 | int exponent_bytes = 0; | |
a08d4ba7 | 4122 | bool signedp = src0 == '-' || src0 == '+' || src0 == ' '; |
37910ab2 | 4123 | int significand_bytes; |
172418ad AS |
4124 | if (zero_flag |
4125 | && ((src[signedp] >= '0' && src[signedp] <= '9') | |
4126 | || (src[signedp] >= 'a' && src[signedp] <= 'f') | |
4127 | || (src[signedp] >= 'A' && src[signedp] <= 'F'))) | |
37910ab2 PE |
4128 | { |
4129 | leading_zeros += padding; | |
4130 | padding = 0; | |
4131 | } | |
4132 | ||
4133 | if (excess_precision | |
4134 | && (conversion == 'e' || conversion == 'g')) | |
4135 | { | |
4136 | char *e = strchr (src, 'e'); | |
4137 | if (e) | |
4138 | exponent_bytes = src + sprintf_bytes - e; | |
4139 | } | |
4140 | ||
4141 | if (! minus_flag) | |
4142 | { | |
4143 | memset (p, ' ', padding); | |
4144 | p += padding; | |
4145 | nchars += padding; | |
4146 | } | |
4147 | ||
4148 | *p = src0; | |
4149 | src += signedp; | |
4150 | p += signedp; | |
4151 | memset (p, '0', leading_zeros); | |
4152 | p += leading_zeros; | |
4153 | significand_bytes = sprintf_bytes - signedp - exponent_bytes; | |
4154 | memcpy (p, src, significand_bytes); | |
4155 | p += significand_bytes; | |
4156 | src += significand_bytes; | |
4157 | memset (p, '0', trailing_zeros); | |
4158 | p += trailing_zeros; | |
4159 | memcpy (p, src, exponent_bytes); | |
4160 | p += exponent_bytes; | |
4161 | ||
4162 | info[n].start = nchars; | |
4163 | nchars += leading_zeros + sprintf_bytes + trailing_zeros; | |
4164 | info[n].end = nchars; | |
4165 | ||
4166 | if (minus_flag) | |
4167 | { | |
4168 | memset (p, ' ', padding); | |
4169 | p += padding; | |
4170 | nchars += padding; | |
4171 | } | |
4172 | ||
4173 | continue; | |
4174 | } | |
4175 | } | |
4176 | } | |
4177 | else | |
4178 | copy_char: | |
4179 | { | |
4180 | /* Copy a single character from format to buf. */ | |
4181 | ||
4182 | char *src = format; | |
4183 | unsigned char str[MAX_MULTIBYTE_LENGTH]; | |
4184 | ||
4185 | if (multibyte_format) | |
4186 | { | |
4187 | /* Copy a whole multibyte character. */ | |
8f2917e4 | 4188 | if (p > buf |
25aa5d64 | 4189 | && !ASCII_BYTE_P (*((unsigned char *) p - 1)) |
37910ab2 | 4190 | && !CHAR_HEAD_P (*format)) |
8f2917e4 | 4191 | maybe_combine_byte = 1; |
37910ab2 PE |
4192 | |
4193 | do | |
4194 | format++; | |
4195 | while (! CHAR_HEAD_P (*format)); | |
4196 | ||
a02719a3 | 4197 | convbytes = format - src; |
7812ba2d | 4198 | memset (&discarded[src + 1 - format_start], 2, convbytes - 1); |
37910ab2 PE |
4199 | } |
4200 | else | |
4201 | { | |
4202 | unsigned char uc = *format++; | |
4203 | if (! multibyte || ASCII_BYTE_P (uc)) | |
4204 | convbytes = 1; | |
9a599130 | 4205 | else |
37910ab2 PE |
4206 | { |
4207 | int c = BYTE8_TO_CHAR (uc); | |
4208 | convbytes = CHAR_STRING (c, str); | |
4209 | src = (char *) str; | |
4210 | } | |
1f24f4fd | 4211 | } |
d147ee84 | 4212 | |
37910ab2 | 4213 | if (convbytes <= buf + bufsize - p) |
d147ee84 | 4214 | { |
37910ab2 PE |
4215 | memcpy (p, src, convbytes); |
4216 | p += convbytes; | |
4217 | nchars++; | |
4218 | continue; | |
d147ee84 | 4219 | } |
7df74da6 | 4220 | } |
1f24f4fd | 4221 | |
37910ab2 PE |
4222 | /* There wasn't enough room to store this conversion or single |
4223 | character. CONVBYTES says how much room is needed. Allocate | |
4224 | enough room (and then some) and do it again. */ | |
4225 | { | |
c9f8d652 | 4226 | ptrdiff_t used = p - buf; |
37910ab2 PE |
4227 | |
4228 | if (max_bufsize - used < convbytes) | |
4229 | string_overflow (); | |
4230 | bufsize = used + convbytes; | |
4231 | bufsize = bufsize < max_bufsize / 2 ? bufsize * 2 : max_bufsize; | |
4232 | ||
4233 | if (buf == initial_buffer) | |
4234 | { | |
4235 | buf = xmalloc (bufsize); | |
4236 | sa_must_free = 1; | |
4237 | buf_save_value = make_save_value (buf, 0); | |
4238 | record_unwind_protect (safe_alloca_unwind, buf_save_value); | |
4239 | memcpy (buf, initial_buffer, used); | |
4240 | } | |
4241 | else | |
4242 | XSAVE_VALUE (buf_save_value)->pointer = buf = xrealloc (buf, bufsize); | |
4243 | ||
4244 | p = buf + used; | |
4245 | } | |
4246 | ||
4247 | format = format0; | |
4248 | n = n0; | |
1f24f4fd RS |
4249 | } |
4250 | ||
37910ab2 | 4251 | if (bufsize < p - buf) |
1088b922 | 4252 | emacs_abort (); |
a432bfe5 | 4253 | |
8f2917e4 | 4254 | if (maybe_combine_byte) |
e7f8264d | 4255 | nchars = multibyte_chars_in_text ((unsigned char *) buf, p - buf); |
5f75e666 | 4256 | val = make_specified_string (buf, nchars, p - buf, multibyte); |
8d6179dc | 4257 | |
1f24f4fd | 4258 | /* If we allocated BUF with malloc, free it too. */ |
e65837df | 4259 | SAFE_FREE (); |
35692fe0 | 4260 | |
5e6d5493 GM |
4261 | /* If the format string has text properties, or any of the string |
4262 | arguments has text properties, set up text properties of the | |
4263 | result string. */ | |
34a7a267 | 4264 | |
0c94c8d6 | 4265 | if (string_intervals (args[0]) || arg_intervals) |
5e6d5493 GM |
4266 | { |
4267 | Lisp_Object len, new_len, props; | |
4268 | struct gcpro gcpro1; | |
34a7a267 | 4269 | |
5e6d5493 | 4270 | /* Add text properties from the format string. */ |
d5db4077 | 4271 | len = make_number (SCHARS (args[0])); |
5e6d5493 GM |
4272 | props = text_property_list (args[0], make_number (0), len, Qnil); |
4273 | GCPRO1 (props); | |
34a7a267 | 4274 | |
5e6d5493 GM |
4275 | if (CONSP (props)) |
4276 | { | |
d311d28c PE |
4277 | ptrdiff_t bytepos = 0, position = 0, translated = 0; |
4278 | ptrdiff_t argn = 1; | |
d147ee84 RS |
4279 | Lisp_Object list; |
4280 | ||
4281 | /* Adjust the bounds of each text property | |
4282 | to the proper start and end in the output string. */ | |
d147ee84 | 4283 | |
15fad037 KS |
4284 | /* Put the positions in PROPS in increasing order, so that |
4285 | we can do (effectively) one scan through the position | |
4286 | space of the format string. */ | |
4287 | props = Fnreverse (props); | |
4288 | ||
4289 | /* BYTEPOS is the byte position in the format string, | |
d147ee84 RS |
4290 | POSITION is the untranslated char position in it, |
4291 | TRANSLATED is the translated char position in BUF, | |
4292 | and ARGN is the number of the next arg we will come to. */ | |
4293 | for (list = props; CONSP (list); list = XCDR (list)) | |
4294 | { | |
f3ce1df8 | 4295 | Lisp_Object item; |
d311d28c | 4296 | ptrdiff_t pos; |
d147ee84 RS |
4297 | |
4298 | item = XCAR (list); | |
4299 | ||
4300 | /* First adjust the property start position. */ | |
4301 | pos = XINT (XCAR (item)); | |
4302 | ||
4303 | /* Advance BYTEPOS, POSITION, TRANSLATED and ARGN | |
4304 | up to this position. */ | |
4305 | for (; position < pos; bytepos++) | |
4306 | { | |
4307 | if (! discarded[bytepos]) | |
4308 | position++, translated++; | |
4309 | else if (discarded[bytepos] == 1) | |
4310 | { | |
4311 | position++; | |
4312 | if (translated == info[argn].start) | |
4313 | { | |
4314 | translated += info[argn].end - info[argn].start; | |
4315 | argn++; | |
4316 | } | |
4317 | } | |
4318 | } | |
4319 | ||
4320 | XSETCAR (item, make_number (translated)); | |
4321 | ||
4322 | /* Likewise adjust the property end position. */ | |
4323 | pos = XINT (XCAR (XCDR (item))); | |
4324 | ||
d40ec4a0 | 4325 | for (; position < pos; bytepos++) |
d147ee84 RS |
4326 | { |
4327 | if (! discarded[bytepos]) | |
4328 | position++, translated++; | |
4329 | else if (discarded[bytepos] == 1) | |
4330 | { | |
4331 | position++; | |
4332 | if (translated == info[argn].start) | |
4333 | { | |
4334 | translated += info[argn].end - info[argn].start; | |
4335 | argn++; | |
4336 | } | |
4337 | } | |
4338 | } | |
4339 | ||
4340 | XSETCAR (XCDR (item), make_number (translated)); | |
4341 | } | |
4342 | ||
5e6d5493 GM |
4343 | add_text_properties_from_list (val, props, make_number (0)); |
4344 | } | |
4345 | ||
4346 | /* Add text properties from arguments. */ | |
d147ee84 | 4347 | if (arg_intervals) |
5e6d5493 | 4348 | for (n = 1; n < nargs; ++n) |
d147ee84 | 4349 | if (info[n].intervals) |
5e6d5493 | 4350 | { |
d5db4077 | 4351 | len = make_number (SCHARS (args[n])); |
5e6d5493 GM |
4352 | new_len = make_number (info[n].end - info[n].start); |
4353 | props = text_property_list (args[n], make_number (0), len, Qnil); | |
e398c61c CY |
4354 | props = extend_property_ranges (props, new_len); |
4355 | /* If successive arguments have properties, be sure that | |
be17069b KH |
4356 | the value of `composition' property be the copy. */ |
4357 | if (n > 1 && info[n - 1].end) | |
4358 | make_composition_value_copy (props); | |
5e6d5493 GM |
4359 | add_text_properties_from_list (val, props, |
4360 | make_number (info[n].start)); | |
4361 | } | |
4362 | ||
4363 | UNGCPRO; | |
4364 | } | |
4365 | ||
8d6179dc | 4366 | return val; |
35692fe0 JB |
4367 | } |
4368 | ||
35692fe0 | 4369 | Lisp_Object |
a8fe7202 | 4370 | format2 (const char *string1, Lisp_Object arg0, Lisp_Object arg1) |
d40dc1d0 RS |
4371 | { |
4372 | Lisp_Object args[3]; | |
d40dc1d0 RS |
4373 | args[0] = build_string (string1); |
4374 | args[1] = arg0; | |
4375 | args[2] = arg1; | |
4376 | return Fformat (3, args); | |
35692fe0 JB |
4377 | } |
4378 | \f | |
4379 | DEFUN ("char-equal", Fchar_equal, Schar_equal, 2, 2, 0, | |
7ee72033 | 4380 | doc: /* Return t if two characters match, optionally ignoring case. |
a1f17501 | 4381 | Both arguments must be characters (i.e. integers). |
7ee72033 | 4382 | Case is ignored if `case-fold-search' is non-nil in the current buffer. */) |
5842a27b | 4383 | (register Lisp_Object c1, Lisp_Object c2) |
35692fe0 | 4384 | { |
1b5d98bb | 4385 | int i1, i2; |
253c3c82 | 4386 | /* Check they're chars, not just integers, otherwise we could get array |
5da9919f | 4387 | bounds violations in downcase. */ |
253c3c82 SM |
4388 | CHECK_CHARACTER (c1); |
4389 | CHECK_CHARACTER (c2); | |
35692fe0 | 4390 | |
1b5d98bb | 4391 | if (XINT (c1) == XINT (c2)) |
35692fe0 | 4392 | return Qt; |
4b4deea2 | 4393 | if (NILP (BVAR (current_buffer, case_fold_search))) |
1b5d98bb RS |
4394 | return Qnil; |
4395 | ||
e5112ecb | 4396 | i1 = XFASTINT (c1); |
4b4deea2 | 4397 | if (NILP (BVAR (current_buffer, enable_multibyte_characters)) |
e5112ecb KH |
4398 | && ! ASCII_CHAR_P (i1)) |
4399 | { | |
4400 | MAKE_CHAR_MULTIBYTE (i1); | |
4401 | } | |
4402 | i2 = XFASTINT (c2); | |
4b4deea2 | 4403 | if (NILP (BVAR (current_buffer, enable_multibyte_characters)) |
e5112ecb KH |
4404 | && ! ASCII_CHAR_P (i2)) |
4405 | { | |
4406 | MAKE_CHAR_MULTIBYTE (i2); | |
4407 | } | |
0da09c43 | 4408 | return (downcase (i1) == downcase (i2) ? Qt : Qnil); |
35692fe0 | 4409 | } |
b229b8d1 RS |
4410 | \f |
4411 | /* Transpose the markers in two regions of the current buffer, and | |
4412 | adjust the ones between them if necessary (i.e.: if the regions | |
4413 | differ in size). | |
4414 | ||
ec1c14f6 RS |
4415 | START1, END1 are the character positions of the first region. |
4416 | START1_BYTE, END1_BYTE are the byte positions. | |
4417 | START2, END2 are the character positions of the second region. | |
4418 | START2_BYTE, END2_BYTE are the byte positions. | |
4419 | ||
b229b8d1 RS |
4420 | Traverses the entire marker list of the buffer to do so, adding an |
4421 | appropriate amount to some, subtracting from some, and leaving the | |
4422 | rest untouched. Most of this is copied from adjust_markers in insdel.c. | |
34a7a267 | 4423 | |
ec1c14f6 | 4424 | It's the caller's job to ensure that START1 <= END1 <= START2 <= END2. */ |
b229b8d1 | 4425 | |
acb7cc89 | 4426 | static void |
d311d28c PE |
4427 | transpose_markers (ptrdiff_t start1, ptrdiff_t end1, |
4428 | ptrdiff_t start2, ptrdiff_t end2, | |
4429 | ptrdiff_t start1_byte, ptrdiff_t end1_byte, | |
4430 | ptrdiff_t start2_byte, ptrdiff_t end2_byte) | |
b229b8d1 | 4431 | { |
d311d28c | 4432 | register ptrdiff_t amt1, amt1_byte, amt2, amt2_byte, diff, diff_byte, mpos; |
12038f9f | 4433 | register struct Lisp_Marker *marker; |
b229b8d1 | 4434 | |
03240d11 | 4435 | /* Update point as if it were a marker. */ |
8de1d5f0 KH |
4436 | if (PT < start1) |
4437 | ; | |
4438 | else if (PT < end1) | |
ec1c14f6 RS |
4439 | TEMP_SET_PT_BOTH (PT + (end2 - end1), |
4440 | PT_BYTE + (end2_byte - end1_byte)); | |
8de1d5f0 | 4441 | else if (PT < start2) |
ec1c14f6 RS |
4442 | TEMP_SET_PT_BOTH (PT + (end2 - start2) - (end1 - start1), |
4443 | (PT_BYTE + (end2_byte - start2_byte) | |
4444 | - (end1_byte - start1_byte))); | |
8de1d5f0 | 4445 | else if (PT < end2) |
ec1c14f6 RS |
4446 | TEMP_SET_PT_BOTH (PT - (start2 - start1), |
4447 | PT_BYTE - (start2_byte - start1_byte)); | |
8de1d5f0 | 4448 | |
03240d11 KH |
4449 | /* We used to adjust the endpoints here to account for the gap, but that |
4450 | isn't good enough. Even if we assume the caller has tried to move the | |
4451 | gap out of our way, it might still be at start1 exactly, for example; | |
4452 | and that places it `inside' the interval, for our purposes. The amount | |
4453 | of adjustment is nontrivial if there's a `denormalized' marker whose | |
4454 | position is between GPT and GPT + GAP_SIZE, so it's simpler to leave | |
4455 | the dirty work to Fmarker_position, below. */ | |
b229b8d1 RS |
4456 | |
4457 | /* The difference between the region's lengths */ | |
4458 | diff = (end2 - start2) - (end1 - start1); | |
ec1c14f6 | 4459 | diff_byte = (end2_byte - start2_byte) - (end1_byte - start1_byte); |
34a7a267 | 4460 | |
b229b8d1 | 4461 | /* For shifting each marker in a region by the length of the other |
ec1c14f6 | 4462 | region plus the distance between the regions. */ |
b229b8d1 RS |
4463 | amt1 = (end2 - start2) + (start2 - end1); |
4464 | amt2 = (end1 - start1) + (start2 - end1); | |
ec1c14f6 RS |
4465 | amt1_byte = (end2_byte - start2_byte) + (start2_byte - end1_byte); |
4466 | amt2_byte = (end1_byte - start1_byte) + (start2_byte - end1_byte); | |
b229b8d1 | 4467 | |
12038f9f | 4468 | for (marker = BUF_MARKERS (current_buffer); marker; marker = marker->next) |
b229b8d1 | 4469 | { |
12038f9f | 4470 | mpos = marker->bytepos; |
ec1c14f6 RS |
4471 | if (mpos >= start1_byte && mpos < end2_byte) |
4472 | { | |
4473 | if (mpos < end1_byte) | |
4474 | mpos += amt1_byte; | |
4475 | else if (mpos < start2_byte) | |
4476 | mpos += diff_byte; | |
4477 | else | |
4478 | mpos -= amt2_byte; | |
12038f9f | 4479 | marker->bytepos = mpos; |
ec1c14f6 | 4480 | } |
12038f9f | 4481 | mpos = marker->charpos; |
03240d11 KH |
4482 | if (mpos >= start1 && mpos < end2) |
4483 | { | |
4484 | if (mpos < end1) | |
4485 | mpos += amt1; | |
4486 | else if (mpos < start2) | |
4487 | mpos += diff; | |
4488 | else | |
4489 | mpos -= amt2; | |
03240d11 | 4490 | } |
12038f9f | 4491 | marker->charpos = mpos; |
b229b8d1 RS |
4492 | } |
4493 | } | |
4494 | ||
4495 | DEFUN ("transpose-regions", Ftranspose_regions, Stranspose_regions, 4, 5, 0, | |
412f1fab | 4496 | doc: /* Transpose region STARTR1 to ENDR1 with STARTR2 to ENDR2. |
27a69fd9 | 4497 | The regions should not be overlapping, because the size of the buffer is |
a1f17501 PJ |
4498 | never changed in a transposition. |
4499 | ||
412f1fab | 4500 | Optional fifth arg LEAVE-MARKERS, if non-nil, means don't update |
a1f17501 PJ |
4501 | any markers that happen to be located in the regions. |
4502 | ||
7ee72033 | 4503 | Transposing beyond buffer boundaries is an error. */) |
5842a27b | 4504 | (Lisp_Object startr1, Lisp_Object endr1, Lisp_Object startr2, Lisp_Object endr2, Lisp_Object leave_markers) |
b229b8d1 | 4505 | { |
d311d28c PE |
4506 | register ptrdiff_t start1, end1, start2, end2; |
4507 | ptrdiff_t start1_byte, start2_byte, len1_byte, len2_byte; | |
4508 | ptrdiff_t gap, len1, len_mid, len2; | |
3c6bc7d0 | 4509 | unsigned char *start1_addr, *start2_addr, *temp; |
b229b8d1 | 4510 | |
6cd0f478 | 4511 | INTERVAL cur_intv, tmp_interval1, tmp_interval_mid, tmp_interval2, tmp_interval3; |
916480c4 CY |
4512 | Lisp_Object buf; |
4513 | ||
4514 | XSETBUFFER (buf, current_buffer); | |
0c94c8d6 | 4515 | cur_intv = buffer_intervals (current_buffer); |
b229b8d1 RS |
4516 | |
4517 | validate_region (&startr1, &endr1); | |
4518 | validate_region (&startr2, &endr2); | |
4519 | ||
4520 | start1 = XFASTINT (startr1); | |
4521 | end1 = XFASTINT (endr1); | |
4522 | start2 = XFASTINT (startr2); | |
4523 | end2 = XFASTINT (endr2); | |
4524 | gap = GPT; | |
4525 | ||
4526 | /* Swap the regions if they're reversed. */ | |
4527 | if (start2 < end1) | |
4528 | { | |
d311d28c | 4529 | register ptrdiff_t glumph = start1; |
b229b8d1 RS |
4530 | start1 = start2; |
4531 | start2 = glumph; | |
4532 | glumph = end1; | |
4533 | end1 = end2; | |
4534 | end2 = glumph; | |
4535 | } | |
4536 | ||
b229b8d1 RS |
4537 | len1 = end1 - start1; |
4538 | len2 = end2 - start2; | |
4539 | ||
4540 | if (start2 < end1) | |
dc3620af | 4541 | error ("Transposed regions overlap"); |
0f4aebc0 LL |
4542 | /* Nothing to change for adjacent regions with one being empty */ |
4543 | else if ((start1 == end1 || start2 == end2) && end1 == start2) | |
4544 | return Qnil; | |
b229b8d1 RS |
4545 | |
4546 | /* The possibilities are: | |
4547 | 1. Adjacent (contiguous) regions, or separate but equal regions | |
4548 | (no, really equal, in this case!), or | |
4549 | 2. Separate regions of unequal size. | |
34a7a267 | 4550 | |
b229b8d1 RS |
4551 | The worst case is usually No. 2. It means that (aside from |
4552 | potential need for getting the gap out of the way), there also | |
4553 | needs to be a shifting of the text between the two regions. So | |
4554 | if they are spread far apart, we are that much slower... sigh. */ | |
4555 | ||
4556 | /* It must be pointed out that the really studly thing to do would | |
4557 | be not to move the gap at all, but to leave it in place and work | |
4558 | around it if necessary. This would be extremely efficient, | |
4559 | especially considering that people are likely to do | |
4560 | transpositions near where they are working interactively, which | |
4561 | is exactly where the gap would be found. However, such code | |
4562 | would be much harder to write and to read. So, if you are | |
4563 | reading this comment and are feeling squirrely, by all means have | |
4564 | a go! I just didn't feel like doing it, so I will simply move | |
4565 | the gap the minimum distance to get it out of the way, and then | |
4566 | deal with an unbroken array. */ | |
3c6bc7d0 RS |
4567 | |
4568 | /* Make sure the gap won't interfere, by moving it out of the text | |
4569 | we will operate on. */ | |
4570 | if (start1 < gap && gap < end2) | |
4571 | { | |
4572 | if (gap - start1 < end2 - gap) | |
4573 | move_gap (start1); | |
4574 | else | |
4575 | move_gap (end2); | |
4576 | } | |
ec1c14f6 RS |
4577 | |
4578 | start1_byte = CHAR_TO_BYTE (start1); | |
4579 | start2_byte = CHAR_TO_BYTE (start2); | |
4580 | len1_byte = CHAR_TO_BYTE (end1) - start1_byte; | |
4581 | len2_byte = CHAR_TO_BYTE (end2) - start2_byte; | |
dc3620af | 4582 | |
9a599130 | 4583 | #ifdef BYTE_COMBINING_DEBUG |
dc3620af RS |
4584 | if (end1 == start2) |
4585 | { | |
9a599130 KH |
4586 | if (count_combining_before (BYTE_POS_ADDR (start2_byte), |
4587 | len2_byte, start1, start1_byte) | |
4588 | || count_combining_before (BYTE_POS_ADDR (start1_byte), | |
4589 | len1_byte, end2, start2_byte + len2_byte) | |
4590 | || count_combining_after (BYTE_POS_ADDR (start1_byte), | |
4591 | len1_byte, end2, start2_byte + len2_byte)) | |
1088b922 | 4592 | emacs_abort (); |
dc3620af RS |
4593 | } |
4594 | else | |
4595 | { | |
9a599130 KH |
4596 | if (count_combining_before (BYTE_POS_ADDR (start2_byte), |
4597 | len2_byte, start1, start1_byte) | |
4598 | || count_combining_before (BYTE_POS_ADDR (start1_byte), | |
4599 | len1_byte, start2, start2_byte) | |
4600 | || count_combining_after (BYTE_POS_ADDR (start2_byte), | |
4601 | len2_byte, end1, start1_byte + len1_byte) | |
4602 | || count_combining_after (BYTE_POS_ADDR (start1_byte), | |
4603 | len1_byte, end2, start2_byte + len2_byte)) | |
1088b922 | 4604 | emacs_abort (); |
dc3620af | 4605 | } |
9a599130 | 4606 | #endif |
dc3620af | 4607 | |
b229b8d1 RS |
4608 | /* Hmmm... how about checking to see if the gap is large |
4609 | enough to use as the temporary storage? That would avoid an | |
4610 | allocation... interesting. Later, don't fool with it now. */ | |
4611 | ||
4612 | /* Working without memmove, for portability (sigh), so must be | |
4613 | careful of overlapping subsections of the array... */ | |
4614 | ||
4615 | if (end1 == start2) /* adjacent regions */ | |
4616 | { | |
20edc1c9 | 4617 | modify_region_1 (start1, end2, false); |
b229b8d1 RS |
4618 | record_change (start1, len1 + len2); |
4619 | ||
b229b8d1 RS |
4620 | tmp_interval1 = copy_intervals (cur_intv, start1, len1); |
4621 | tmp_interval2 = copy_intervals (cur_intv, start2, len2); | |
916480c4 CY |
4622 | /* Don't use Fset_text_properties: that can cause GC, which can |
4623 | clobber objects stored in the tmp_intervals. */ | |
6cd0f478 | 4624 | tmp_interval3 = validate_interval_range (buf, &startr1, &endr2, 0); |
77c7bcb1 | 4625 | if (tmp_interval3) |
6cd0f478 | 4626 | set_text_properties_1 (startr1, endr2, Qnil, buf, tmp_interval3); |
b229b8d1 RS |
4627 | |
4628 | /* First region smaller than second. */ | |
ec1c14f6 | 4629 | if (len1_byte < len2_byte) |
b229b8d1 | 4630 | { |
7e2c051b KS |
4631 | USE_SAFE_ALLOCA; |
4632 | ||
98c6f1e3 | 4633 | temp = SAFE_ALLOCA (len2_byte); |
03240d11 KH |
4634 | |
4635 | /* Don't precompute these addresses. We have to compute them | |
4636 | at the last minute, because the relocating allocator might | |
4637 | have moved the buffer around during the xmalloc. */ | |
23017390 KH |
4638 | start1_addr = BYTE_POS_ADDR (start1_byte); |
4639 | start2_addr = BYTE_POS_ADDR (start2_byte); | |
03240d11 | 4640 | |
72af86bd AS |
4641 | memcpy (temp, start2_addr, len2_byte); |
4642 | memcpy (start1_addr + len2_byte, start1_addr, len1_byte); | |
4643 | memcpy (start1_addr, temp, len2_byte); | |
e65837df | 4644 | SAFE_FREE (); |
b229b8d1 RS |
4645 | } |
4646 | else | |
4647 | /* First region not smaller than second. */ | |
4648 | { | |
7e2c051b KS |
4649 | USE_SAFE_ALLOCA; |
4650 | ||
98c6f1e3 | 4651 | temp = SAFE_ALLOCA (len1_byte); |
23017390 KH |
4652 | start1_addr = BYTE_POS_ADDR (start1_byte); |
4653 | start2_addr = BYTE_POS_ADDR (start2_byte); | |
72af86bd AS |
4654 | memcpy (temp, start1_addr, len1_byte); |
4655 | memcpy (start1_addr, start2_addr, len2_byte); | |
4656 | memcpy (start1_addr + len2_byte, temp, len1_byte); | |
e65837df | 4657 | SAFE_FREE (); |
b229b8d1 | 4658 | } |
b229b8d1 RS |
4659 | graft_intervals_into_buffer (tmp_interval1, start1 + len2, |
4660 | len1, current_buffer, 0); | |
4661 | graft_intervals_into_buffer (tmp_interval2, start1, | |
4662 | len2, current_buffer, 0); | |
d5c2c403 KH |
4663 | update_compositions (start1, start1 + len2, CHECK_BORDER); |
4664 | update_compositions (start1 + len2, end2, CHECK_TAIL); | |
b229b8d1 RS |
4665 | } |
4666 | /* Non-adjacent regions, because end1 != start2, bleagh... */ | |
4667 | else | |
4668 | { | |
ec1c14f6 RS |
4669 | len_mid = start2_byte - (start1_byte + len1_byte); |
4670 | ||
4671 | if (len1_byte == len2_byte) | |
b229b8d1 RS |
4672 | /* Regions are same size, though, how nice. */ |
4673 | { | |
7e2c051b KS |
4674 | USE_SAFE_ALLOCA; |
4675 | ||
20edc1c9 DA |
4676 | modify_region_1 (start1, end1, false); |
4677 | modify_region_1 (start2, end2, false); | |
b229b8d1 RS |
4678 | record_change (start1, len1); |
4679 | record_change (start2, len2); | |
b229b8d1 RS |
4680 | tmp_interval1 = copy_intervals (cur_intv, start1, len1); |
4681 | tmp_interval2 = copy_intervals (cur_intv, start2, len2); | |
6cd0f478 CY |
4682 | |
4683 | tmp_interval3 = validate_interval_range (buf, &startr1, &endr1, 0); | |
77c7bcb1 | 4684 | if (tmp_interval3) |
6cd0f478 CY |
4685 | set_text_properties_1 (startr1, endr1, Qnil, buf, tmp_interval3); |
4686 | ||
4687 | tmp_interval3 = validate_interval_range (buf, &startr2, &endr2, 0); | |
77c7bcb1 | 4688 | if (tmp_interval3) |
6cd0f478 | 4689 | set_text_properties_1 (startr2, endr2, Qnil, buf, tmp_interval3); |
b229b8d1 | 4690 | |
98c6f1e3 | 4691 | temp = SAFE_ALLOCA (len1_byte); |
23017390 KH |
4692 | start1_addr = BYTE_POS_ADDR (start1_byte); |
4693 | start2_addr = BYTE_POS_ADDR (start2_byte); | |
72af86bd AS |
4694 | memcpy (temp, start1_addr, len1_byte); |
4695 | memcpy (start1_addr, start2_addr, len2_byte); | |
4696 | memcpy (start2_addr, temp, len1_byte); | |
e65837df | 4697 | SAFE_FREE (); |
7e2c051b | 4698 | |
b229b8d1 RS |
4699 | graft_intervals_into_buffer (tmp_interval1, start2, |
4700 | len1, current_buffer, 0); | |
4701 | graft_intervals_into_buffer (tmp_interval2, start1, | |
4702 | len2, current_buffer, 0); | |
b229b8d1 RS |
4703 | } |
4704 | ||
ec1c14f6 | 4705 | else if (len1_byte < len2_byte) /* Second region larger than first */ |
b229b8d1 RS |
4706 | /* Non-adjacent & unequal size, area between must also be shifted. */ |
4707 | { | |
7e2c051b KS |
4708 | USE_SAFE_ALLOCA; |
4709 | ||
20edc1c9 | 4710 | modify_region_1 (start1, end2, false); |
b229b8d1 | 4711 | record_change (start1, (end2 - start1)); |
b229b8d1 RS |
4712 | tmp_interval1 = copy_intervals (cur_intv, start1, len1); |
4713 | tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid); | |
4714 | tmp_interval2 = copy_intervals (cur_intv, start2, len2); | |
6cd0f478 CY |
4715 | |
4716 | tmp_interval3 = validate_interval_range (buf, &startr1, &endr2, 0); | |
77c7bcb1 | 4717 | if (tmp_interval3) |
6cd0f478 | 4718 | set_text_properties_1 (startr1, endr2, Qnil, buf, tmp_interval3); |
b229b8d1 | 4719 | |
3c6bc7d0 | 4720 | /* holds region 2 */ |
98c6f1e3 | 4721 | temp = SAFE_ALLOCA (len2_byte); |
23017390 KH |
4722 | start1_addr = BYTE_POS_ADDR (start1_byte); |
4723 | start2_addr = BYTE_POS_ADDR (start2_byte); | |
72af86bd AS |
4724 | memcpy (temp, start2_addr, len2_byte); |
4725 | memcpy (start1_addr + len_mid + len2_byte, start1_addr, len1_byte); | |
4726 | memmove (start1_addr + len2_byte, start1_addr + len1_byte, len_mid); | |
4727 | memcpy (start1_addr, temp, len2_byte); | |
e65837df | 4728 | SAFE_FREE (); |
7e2c051b | 4729 | |
b229b8d1 RS |
4730 | graft_intervals_into_buffer (tmp_interval1, end2 - len1, |
4731 | len1, current_buffer, 0); | |
4732 | graft_intervals_into_buffer (tmp_interval_mid, start1 + len2, | |
4733 | len_mid, current_buffer, 0); | |
4734 | graft_intervals_into_buffer (tmp_interval2, start1, | |
4735 | len2, current_buffer, 0); | |
b229b8d1 RS |
4736 | } |
4737 | else | |
4738 | /* Second region smaller than first. */ | |
4739 | { | |
7e2c051b KS |
4740 | USE_SAFE_ALLOCA; |
4741 | ||
b229b8d1 | 4742 | record_change (start1, (end2 - start1)); |
20edc1c9 | 4743 | modify_region_1 (start1, end2, false); |
b229b8d1 | 4744 | |
b229b8d1 RS |
4745 | tmp_interval1 = copy_intervals (cur_intv, start1, len1); |
4746 | tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid); | |
4747 | tmp_interval2 = copy_intervals (cur_intv, start2, len2); | |
6cd0f478 CY |
4748 | |
4749 | tmp_interval3 = validate_interval_range (buf, &startr1, &endr2, 0); | |
77c7bcb1 | 4750 | if (tmp_interval3) |
6cd0f478 | 4751 | set_text_properties_1 (startr1, endr2, Qnil, buf, tmp_interval3); |
b229b8d1 | 4752 | |
3c6bc7d0 | 4753 | /* holds region 1 */ |
98c6f1e3 | 4754 | temp = SAFE_ALLOCA (len1_byte); |
23017390 KH |
4755 | start1_addr = BYTE_POS_ADDR (start1_byte); |
4756 | start2_addr = BYTE_POS_ADDR (start2_byte); | |
72af86bd AS |
4757 | memcpy (temp, start1_addr, len1_byte); |
4758 | memcpy (start1_addr, start2_addr, len2_byte); | |
4759 | memcpy (start1_addr + len2_byte, start1_addr + len1_byte, len_mid); | |
4760 | memcpy (start1_addr + len2_byte + len_mid, temp, len1_byte); | |
e65837df | 4761 | SAFE_FREE (); |
7e2c051b | 4762 | |
b229b8d1 RS |
4763 | graft_intervals_into_buffer (tmp_interval1, end2 - len1, |
4764 | len1, current_buffer, 0); | |
4765 | graft_intervals_into_buffer (tmp_interval_mid, start1 + len2, | |
4766 | len_mid, current_buffer, 0); | |
4767 | graft_intervals_into_buffer (tmp_interval2, start1, | |
4768 | len2, current_buffer, 0); | |
b229b8d1 | 4769 | } |
d5c2c403 KH |
4770 | |
4771 | update_compositions (start1, start1 + len2, CHECK_BORDER); | |
4772 | update_compositions (end2 - len1, end2, CHECK_BORDER); | |
b229b8d1 RS |
4773 | } |
4774 | ||
ec1c14f6 RS |
4775 | /* When doing multiple transpositions, it might be nice |
4776 | to optimize this. Perhaps the markers in any one buffer | |
4777 | should be organized in some sorted data tree. */ | |
b229b8d1 | 4778 | if (NILP (leave_markers)) |
8de1d5f0 | 4779 | { |
ec1c14f6 RS |
4780 | transpose_markers (start1, end1, start2, end2, |
4781 | start1_byte, start1_byte + len1_byte, | |
4782 | start2_byte, start2_byte + len2_byte); | |
6b61353c | 4783 | fix_start_end_in_overlays (start1, end2); |
8de1d5f0 | 4784 | } |
b229b8d1 | 4785 | |
c10b2810 | 4786 | signal_after_change (start1, end2 - start1, end2 - start1); |
b229b8d1 RS |
4787 | return Qnil; |
4788 | } | |
35692fe0 | 4789 | |
35692fe0 JB |
4790 | \f |
4791 | void | |
971de7fb | 4792 | syms_of_editfns (void) |
35692fe0 | 4793 | { |
cd3520a4 | 4794 | DEFSYM (Qbuffer_access_fontify_functions, "buffer-access-fontify-functions"); |
260e2e2a | 4795 | |
29208e82 | 4796 | DEFVAR_LISP ("inhibit-field-text-motion", Vinhibit_field_text_motion, |
7dcece14 | 4797 | doc: /* Non-nil means text motion commands don't notice fields. */); |
9a74e7e5 GM |
4798 | Vinhibit_field_text_motion = Qnil; |
4799 | ||
260e2e2a | 4800 | DEFVAR_LISP ("buffer-access-fontify-functions", |
29208e82 | 4801 | Vbuffer_access_fontify_functions, |
7ee72033 | 4802 | doc: /* List of functions called by `buffer-substring' to fontify if necessary. |
a1f17501 PJ |
4803 | Each function is called with two arguments which specify the range |
4804 | of the buffer being accessed. */); | |
260e2e2a KH |
4805 | Vbuffer_access_fontify_functions = Qnil; |
4806 | ||
af209db8 RS |
4807 | { |
4808 | Lisp_Object obuf; | |
af209db8 RS |
4809 | obuf = Fcurrent_buffer (); |
4810 | /* Do this here, because init_buffer_once is too early--it won't work. */ | |
4811 | Fset_buffer (Vprin1_to_string_buffer); | |
4812 | /* Make sure buffer-access-fontify-functions is nil in this buffer. */ | |
d67b4f80 | 4813 | Fset (Fmake_local_variable (intern_c_string ("buffer-access-fontify-functions")), |
af209db8 RS |
4814 | Qnil); |
4815 | Fset_buffer (obuf); | |
4816 | } | |
4817 | ||
0b6fd023 | 4818 | DEFVAR_LISP ("buffer-access-fontified-property", |
29208e82 | 4819 | Vbuffer_access_fontified_property, |
7ee72033 | 4820 | doc: /* Property which (if non-nil) indicates text has been fontified. |
a1f17501 PJ |
4821 | `buffer-substring' need not call the `buffer-access-fontify-functions' |
4822 | functions if all the text being accessed has this property. */); | |
260e2e2a KH |
4823 | Vbuffer_access_fontified_property = Qnil; |
4824 | ||
29208e82 | 4825 | DEFVAR_LISP ("system-name", Vsystem_name, |
1a7e0117 | 4826 | doc: /* The host name of the machine Emacs is running on. */); |
34a7a267 | 4827 | |
29208e82 | 4828 | DEFVAR_LISP ("user-full-name", Vuser_full_name, |
7ee72033 | 4829 | doc: /* The full name of the user logged in. */); |
f43754f6 | 4830 | |
29208e82 | 4831 | DEFVAR_LISP ("user-login-name", Vuser_login_name, |
7ee72033 | 4832 | doc: /* The user's name, taken from environment variables if possible. */); |
f43754f6 | 4833 | |
29208e82 | 4834 | DEFVAR_LISP ("user-real-login-name", Vuser_real_login_name, |
7ee72033 | 4835 | doc: /* The user's name, based upon the real uid only. */); |
35692fe0 | 4836 | |
29208e82 | 4837 | DEFVAR_LISP ("operating-system-release", Voperating_system_release, |
3bb9abc8 ST |
4838 | doc: /* The release of the operating system Emacs is running on. */); |
4839 | ||
0963334d | 4840 | defsubr (&Spropertize); |
35692fe0 JB |
4841 | defsubr (&Schar_equal); |
4842 | defsubr (&Sgoto_char); | |
4843 | defsubr (&Sstring_to_char); | |
4844 | defsubr (&Schar_to_string); | |
c3bb441d | 4845 | defsubr (&Sbyte_to_string); |
35692fe0 | 4846 | defsubr (&Sbuffer_substring); |
260e2e2a | 4847 | defsubr (&Sbuffer_substring_no_properties); |
35692fe0 JB |
4848 | defsubr (&Sbuffer_string); |
4849 | ||
4850 | defsubr (&Spoint_marker); | |
4851 | defsubr (&Smark_marker); | |
4852 | defsubr (&Spoint); | |
4853 | defsubr (&Sregion_beginning); | |
4854 | defsubr (&Sregion_end); | |
7df74da6 | 4855 | |
cd3520a4 JB |
4856 | DEFSYM (Qfield, "field"); |
4857 | DEFSYM (Qboundary, "boundary"); | |
0daf6e8d GM |
4858 | defsubr (&Sfield_beginning); |
4859 | defsubr (&Sfield_end); | |
4860 | defsubr (&Sfield_string); | |
4861 | defsubr (&Sfield_string_no_properties); | |
8bf64fe8 | 4862 | defsubr (&Sdelete_field); |
0daf6e8d GM |
4863 | defsubr (&Sconstrain_to_field); |
4864 | ||
7df74da6 RS |
4865 | defsubr (&Sline_beginning_position); |
4866 | defsubr (&Sline_end_position); | |
4867 | ||
35692fe0 JB |
4868 | /* defsubr (&Smark); */ |
4869 | /* defsubr (&Sset_mark); */ | |
4870 | defsubr (&Ssave_excursion); | |
4bc8c7d2 | 4871 | defsubr (&Ssave_current_buffer); |
35692fe0 JB |
4872 | |
4873 | defsubr (&Sbufsize); | |
4874 | defsubr (&Spoint_max); | |
4875 | defsubr (&Spoint_min); | |
4876 | defsubr (&Spoint_min_marker); | |
4877 | defsubr (&Spoint_max_marker); | |
c86212b9 RS |
4878 | defsubr (&Sgap_position); |
4879 | defsubr (&Sgap_size); | |
7df74da6 | 4880 | defsubr (&Sposition_bytes); |
3ab0732d | 4881 | defsubr (&Sbyte_to_position); |
c9ed721d | 4882 | |
35692fe0 JB |
4883 | defsubr (&Sbobp); |
4884 | defsubr (&Seobp); | |
4885 | defsubr (&Sbolp); | |
4886 | defsubr (&Seolp); | |
850a8179 JB |
4887 | defsubr (&Sfollowing_char); |
4888 | defsubr (&Sprevious_char); | |
35692fe0 | 4889 | defsubr (&Schar_after); |
fb8106e8 | 4890 | defsubr (&Schar_before); |
35692fe0 JB |
4891 | defsubr (&Sinsert); |
4892 | defsubr (&Sinsert_before_markers); | |
be91036a RS |
4893 | defsubr (&Sinsert_and_inherit); |
4894 | defsubr (&Sinsert_and_inherit_before_markers); | |
35692fe0 | 4895 | defsubr (&Sinsert_char); |
48ef988f | 4896 | defsubr (&Sinsert_byte); |
35692fe0 JB |
4897 | |
4898 | defsubr (&Suser_login_name); | |
4899 | defsubr (&Suser_real_login_name); | |
4900 | defsubr (&Suser_uid); | |
4901 | defsubr (&Suser_real_uid); | |
4902 | defsubr (&Suser_full_name); | |
7fd233b3 | 4903 | defsubr (&Semacs_pid); |
d940e0e4 | 4904 | defsubr (&Scurrent_time); |
4211ee7d | 4905 | defsubr (&Sget_internal_run_time); |
a82d387c | 4906 | defsubr (&Sformat_time_string); |
34a7a267 | 4907 | defsubr (&Sfloat_time); |
4691c06d | 4908 | defsubr (&Sdecode_time); |
cce7b8a0 | 4909 | defsubr (&Sencode_time); |
35692fe0 | 4910 | defsubr (&Scurrent_time_string); |
c2662aea | 4911 | defsubr (&Scurrent_time_zone); |
143cb9a9 | 4912 | defsubr (&Sset_time_zone_rule); |
35692fe0 | 4913 | defsubr (&Ssystem_name); |
35692fe0 | 4914 | defsubr (&Smessage); |
cacc3e2c RS |
4915 | defsubr (&Smessage_box); |
4916 | defsubr (&Smessage_or_box); | |
b14dda8a | 4917 | defsubr (&Scurrent_message); |
35692fe0 | 4918 | defsubr (&Sformat); |
35692fe0 JB |
4919 | |
4920 | defsubr (&Sinsert_buffer_substring); | |
e9cf2084 | 4921 | defsubr (&Scompare_buffer_substrings); |
35692fe0 | 4922 | defsubr (&Ssubst_char_in_region); |
8583605b | 4923 | defsubr (&Stranslate_region_internal); |
35692fe0 | 4924 | defsubr (&Sdelete_region); |
7dae4502 | 4925 | defsubr (&Sdelete_and_extract_region); |
35692fe0 JB |
4926 | defsubr (&Swiden); |
4927 | defsubr (&Snarrow_to_region); | |
4928 | defsubr (&Ssave_restriction); | |
b229b8d1 | 4929 | defsubr (&Stranspose_regions); |
35692fe0 | 4930 | } |