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