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