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