Commit | Line | Data |
---|---|---|
35692fe0 | 1 | /* Lisp functions pertaining to editing. |
31c8f881 | 2 | Copyright (C) 1985,86,87,89,93,94,95,96,97,98 Free Software Foundation, Inc. |
35692fe0 JB |
3 | |
4 | This file is part of GNU Emacs. | |
5 | ||
6 | GNU Emacs is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
7c938215 | 8 | the Free Software Foundation; either version 2, or (at your option) |
35692fe0 JB |
9 | any later version. |
10 | ||
11 | GNU Emacs is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU Emacs; see the file COPYING. If not, write to | |
5a7670bf RS |
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
19 | Boston, MA 02111-1307, USA. */ | |
35692fe0 JB |
20 | |
21 | ||
738429d1 JB |
22 | #include <sys/types.h> |
23 | ||
18160b98 | 24 | #include <config.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 | ||
35692fe0 | 32 | #include "lisp.h" |
74d6d8c5 | 33 | #include "intervals.h" |
35692fe0 | 34 | #include "buffer.h" |
fb8106e8 | 35 | #include "charset.h" |
35692fe0 JB |
36 | #include "window.h" |
37 | ||
956ace37 | 38 | #include "systime.h" |
35692fe0 JB |
39 | |
40 | #define min(a, b) ((a) < (b) ? (a) : (b)) | |
41 | #define max(a, b) ((a) > (b) ? (a) : (b)) | |
42 | ||
a03eaf1c RS |
43 | #ifndef NULL |
44 | #define NULL 0 | |
45 | #endif | |
46 | ||
c59b5089 PE |
47 | extern char **environ; |
48 | extern Lisp_Object make_time (); | |
b1b0ee5a | 49 | extern void insert_from_buffer (); |
94751666 | 50 | static int tm_diff (); |
260e2e2a | 51 | static void update_buffer_properties (); |
b771d0da | 52 | size_t emacs_strftime (); |
a92ae0ce | 53 | void set_time_zone_rule (); |
260e2e2a KH |
54 | |
55 | Lisp_Object Vbuffer_access_fontify_functions; | |
56 | Lisp_Object Qbuffer_access_fontify_functions; | |
57 | Lisp_Object Vbuffer_access_fontified_property; | |
b1b0ee5a | 58 | |
e3ed8469 KH |
59 | Lisp_Object Fuser_full_name (); |
60 | ||
35692fe0 JB |
61 | /* Some static data, and a function to initialize it for each run */ |
62 | ||
63 | Lisp_Object Vsystem_name; | |
35b34f72 KH |
64 | Lisp_Object Vuser_real_login_name; /* login name of current user ID */ |
65 | Lisp_Object Vuser_full_name; /* full name of current user */ | |
66 | Lisp_Object Vuser_login_name; /* user name from LOGNAME or USER */ | |
35692fe0 JB |
67 | |
68 | void | |
69 | init_editfns () | |
70 | { | |
52b14ac0 | 71 | char *user_name; |
35692fe0 JB |
72 | register unsigned char *p, *q, *r; |
73 | struct passwd *pw; /* password entry for the current user */ | |
35692fe0 JB |
74 | Lisp_Object tem; |
75 | ||
76 | /* Set up system_name even when dumping. */ | |
ac988277 | 77 | init_system_name (); |
35692fe0 JB |
78 | |
79 | #ifndef CANNOT_DUMP | |
80 | /* Don't bother with this on initial start when just dumping out */ | |
81 | if (!initialized) | |
82 | return; | |
83 | #endif /* not CANNOT_DUMP */ | |
84 | ||
85 | pw = (struct passwd *) getpwuid (getuid ()); | |
87485d6f MW |
86 | #ifdef MSDOS |
87 | /* We let the real user name default to "root" because that's quite | |
88 | accurate on MSDOG and because it lets Emacs find the init file. | |
89 | (The DVX libraries override the Djgpp libraries here.) */ | |
35b34f72 | 90 | Vuser_real_login_name = build_string (pw ? pw->pw_name : "root"); |
87485d6f | 91 | #else |
35b34f72 | 92 | Vuser_real_login_name = build_string (pw ? pw->pw_name : "unknown"); |
87485d6f | 93 | #endif |
35692fe0 | 94 | |
52b14ac0 JB |
95 | /* Get the effective user name, by consulting environment variables, |
96 | or the effective uid if those are unset. */ | |
2c9ae24e | 97 | user_name = (char *) getenv ("LOGNAME"); |
35692fe0 | 98 | if (!user_name) |
4691c06d RS |
99 | #ifdef WINDOWSNT |
100 | user_name = (char *) getenv ("USERNAME"); /* it's USERNAME on NT */ | |
101 | #else /* WINDOWSNT */ | |
2c9ae24e | 102 | user_name = (char *) getenv ("USER"); |
4691c06d | 103 | #endif /* WINDOWSNT */ |
52b14ac0 JB |
104 | if (!user_name) |
105 | { | |
106 | pw = (struct passwd *) getpwuid (geteuid ()); | |
107 | user_name = (char *) (pw ? pw->pw_name : "unknown"); | |
108 | } | |
35b34f72 | 109 | Vuser_login_name = build_string (user_name); |
35692fe0 | 110 | |
52b14ac0 JB |
111 | /* If the user name claimed in the environment vars differs from |
112 | the real uid, use the claimed name to find the full name. */ | |
35b34f72 | 113 | tem = Fstring_equal (Vuser_login_name, Vuser_real_login_name); |
3415b0e9 RS |
114 | Vuser_full_name = Fuser_full_name (NILP (tem)? make_number (geteuid()) |
115 | : Vuser_login_name); | |
35692fe0 | 116 | |
8f1e2d16 | 117 | p = (unsigned char *) getenv ("NAME"); |
9d36d071 RS |
118 | if (p) |
119 | Vuser_full_name = build_string (p); | |
3347526c RS |
120 | else if (NILP (Vuser_full_name)) |
121 | Vuser_full_name = build_string ("unknown"); | |
35692fe0 JB |
122 | } |
123 | \f | |
124 | DEFUN ("char-to-string", Fchar_to_string, Schar_to_string, 1, 1, 0, | |
5f75e666 | 125 | "Convert arg CHAR to a string containing that character.") |
2591ec64 EN |
126 | (character) |
127 | Lisp_Object character; | |
35692fe0 | 128 | { |
fb8106e8 | 129 | int len; |
d11ba98c | 130 | unsigned char workbuf[4], *str; |
fb8106e8 | 131 | |
2591ec64 | 132 | CHECK_NUMBER (character, 0); |
35692fe0 | 133 | |
fb8106e8 | 134 | len = CHAR_STRING (XFASTINT (character), workbuf, str); |
5f75e666 | 135 | return make_string_from_bytes (str, 1, len); |
35692fe0 JB |
136 | } |
137 | ||
138 | DEFUN ("string-to-char", Fstring_to_char, Sstring_to_char, 1, 1, 0, | |
fb8106e8 KH |
139 | "Convert arg STRING to a character, the first character of that string.\n\ |
140 | A multibyte character is handled correctly.") | |
2591ec64 EN |
141 | (string) |
142 | register Lisp_Object string; | |
35692fe0 JB |
143 | { |
144 | register Lisp_Object val; | |
145 | register struct Lisp_String *p; | |
2591ec64 | 146 | CHECK_STRING (string, 0); |
2591ec64 | 147 | p = XSTRING (string); |
35692fe0 | 148 | if (p->size) |
dc3620af | 149 | XSETFASTINT (val, STRING_CHAR (p->data, STRING_BYTES (p))); |
35692fe0 | 150 | else |
55561c63 | 151 | XSETFASTINT (val, 0); |
35692fe0 JB |
152 | return val; |
153 | } | |
154 | \f | |
155 | static Lisp_Object | |
ec1c14f6 RS |
156 | buildmark (charpos, bytepos) |
157 | int charpos, bytepos; | |
35692fe0 JB |
158 | { |
159 | register Lisp_Object mark; | |
160 | mark = Fmake_marker (); | |
ec1c14f6 | 161 | set_marker_both (mark, Qnil, charpos, bytepos); |
35692fe0 JB |
162 | return mark; |
163 | } | |
164 | ||
165 | DEFUN ("point", Fpoint, Spoint, 0, 0, 0, | |
166 | "Return value of point, as an integer.\n\ | |
167 | Beginning of buffer is position (point-min)") | |
168 | () | |
169 | { | |
170 | Lisp_Object temp; | |
6ec8bbd2 | 171 | XSETFASTINT (temp, PT); |
35692fe0 JB |
172 | return temp; |
173 | } | |
174 | ||
175 | DEFUN ("point-marker", Fpoint_marker, Spoint_marker, 0, 0, 0, | |
176 | "Return value of point, as a marker object.") | |
177 | () | |
178 | { | |
ec1c14f6 | 179 | return buildmark (PT, PT_BYTE); |
35692fe0 JB |
180 | } |
181 | ||
182 | int | |
183 | clip_to_bounds (lower, num, upper) | |
184 | int lower, num, upper; | |
185 | { | |
186 | if (num < lower) | |
187 | return lower; | |
188 | else if (num > upper) | |
189 | return upper; | |
190 | else | |
191 | return num; | |
192 | } | |
193 | ||
194 | DEFUN ("goto-char", Fgoto_char, Sgoto_char, 1, 1, "NGoto char: ", | |
195 | "Set point to POSITION, a number or marker.\n\ | |
fb8106e8 KH |
196 | Beginning of buffer is position (point-min), end is (point-max).\n\ |
197 | If the position is in the middle of a multibyte form,\n\ | |
198 | the actual point is set at the head of the multibyte form\n\ | |
199 | except in the case that `enable-multibyte-characters' is nil.") | |
2591ec64 EN |
200 | (position) |
201 | register Lisp_Object position; | |
35692fe0 | 202 | { |
fb8106e8 KH |
203 | int pos; |
204 | unsigned char *p; | |
205 | ||
72ef82ec RS |
206 | if (MARKERP (position) |
207 | && current_buffer == XMARKER (position)->buffer) | |
ec1c14f6 RS |
208 | { |
209 | pos = marker_position (position); | |
210 | if (pos < BEGV) | |
211 | SET_PT_BOTH (BEGV, BEGV_BYTE); | |
212 | else if (pos > ZV) | |
213 | SET_PT_BOTH (ZV, ZV_BYTE); | |
214 | else | |
215 | SET_PT_BOTH (pos, marker_byte_position (position)); | |
216 | ||
217 | return position; | |
218 | } | |
219 | ||
2591ec64 | 220 | CHECK_NUMBER_COERCE_MARKER (position, 0); |
35692fe0 | 221 | |
fb8106e8 | 222 | pos = clip_to_bounds (BEGV, XINT (position), ZV); |
fb8106e8 | 223 | SET_PT (pos); |
2591ec64 | 224 | return position; |
35692fe0 JB |
225 | } |
226 | ||
227 | static Lisp_Object | |
228 | region_limit (beginningp) | |
229 | int beginningp; | |
230 | { | |
646d9d18 | 231 | extern Lisp_Object Vmark_even_if_inactive; /* Defined in callint.c. */ |
35692fe0 | 232 | register Lisp_Object m; |
c9dd14e1 RM |
233 | if (!NILP (Vtransient_mark_mode) && NILP (Vmark_even_if_inactive) |
234 | && NILP (current_buffer->mark_active)) | |
235 | Fsignal (Qmark_inactive, Qnil); | |
35692fe0 | 236 | m = Fmarker_position (current_buffer->mark); |
56a98455 | 237 | if (NILP (m)) error ("There is no region now"); |
6ec8bbd2 KH |
238 | if ((PT < XFASTINT (m)) == beginningp) |
239 | return (make_number (PT)); | |
35692fe0 JB |
240 | else |
241 | return (m); | |
242 | } | |
243 | ||
244 | DEFUN ("region-beginning", Fregion_beginning, Sregion_beginning, 0, 0, 0, | |
245 | "Return position of beginning of region, as an integer.") | |
246 | () | |
247 | { | |
248 | return (region_limit (1)); | |
249 | } | |
250 | ||
251 | DEFUN ("region-end", Fregion_end, Sregion_end, 0, 0, 0, | |
252 | "Return position of end of region, as an integer.") | |
253 | () | |
254 | { | |
255 | return (region_limit (0)); | |
256 | } | |
257 | ||
35692fe0 JB |
258 | DEFUN ("mark-marker", Fmark_marker, Smark_marker, 0, 0, 0, |
259 | "Return this buffer's mark, as a marker object.\n\ | |
260 | Watch out! Moving this marker changes the mark position.\n\ | |
261 | If you set the marker not to point anywhere, the buffer will have no mark.") | |
262 | () | |
263 | { | |
264 | return current_buffer->mark; | |
265 | } | |
c9ed721d RS |
266 | \f |
267 | DEFUN ("line-beginning-position", Fline_beginning_position, Sline_beginning_position, | |
268 | 0, 1, 0, | |
269 | "Return the character position of the first character on the current line.\n\ | |
270 | With argument N not nil or 1, move forward N - 1 lines first.\n\ | |
271 | If scan reaches end of buffer, return that position.\n\ | |
272 | This function does not move point.") | |
273 | (n) | |
274 | Lisp_Object n; | |
275 | { | |
ec1c14f6 | 276 | register int orig, orig_byte, end; |
c9ed721d RS |
277 | |
278 | if (NILP (n)) | |
279 | XSETFASTINT (n, 1); | |
280 | else | |
281 | CHECK_NUMBER (n, 0); | |
282 | ||
283 | orig = PT; | |
ec1c14f6 | 284 | orig_byte = PT_BYTE; |
c9ed721d RS |
285 | Fforward_line (make_number (XINT (n) - 1)); |
286 | end = PT; | |
ec1c14f6 | 287 | SET_PT_BOTH (orig, orig_byte); |
35692fe0 | 288 | |
c9ed721d RS |
289 | return make_number (end); |
290 | } | |
291 | ||
292 | DEFUN ("line-end-position", Fline_end_position, Sline_end_position, | |
293 | 0, 1, 0, | |
294 | "Return the character position of the last character on the current line.\n\ | |
295 | With argument N not nil or 1, move forward N - 1 lines first.\n\ | |
296 | If scan reaches end of buffer, return that position.\n\ | |
297 | This function does not move point.") | |
298 | (n) | |
299 | Lisp_Object n; | |
300 | { | |
301 | if (NILP (n)) | |
302 | XSETFASTINT (n, 1); | |
303 | else | |
304 | CHECK_NUMBER (n, 0); | |
305 | ||
306 | return make_number (find_before_next_newline | |
307 | (PT, 0, XINT (n) - (XINT (n) <= 0))); | |
308 | } | |
309 | \f | |
35692fe0 JB |
310 | Lisp_Object |
311 | save_excursion_save () | |
312 | { | |
0e2c9c70 JB |
313 | register int visible = (XBUFFER (XWINDOW (selected_window)->buffer) |
314 | == current_buffer); | |
35692fe0 JB |
315 | |
316 | return Fcons (Fpoint_marker (), | |
aea4a109 | 317 | Fcons (Fcopy_marker (current_buffer->mark, Qnil), |
9772455e RS |
318 | Fcons (visible ? Qt : Qnil, |
319 | current_buffer->mark_active))); | |
35692fe0 JB |
320 | } |
321 | ||
322 | Lisp_Object | |
323 | save_excursion_restore (info) | |
4ad8681a | 324 | Lisp_Object info; |
35692fe0 | 325 | { |
4ad8681a RS |
326 | Lisp_Object tem, tem1, omark, nmark; |
327 | struct gcpro gcpro1, gcpro2, gcpro3; | |
35692fe0 JB |
328 | |
329 | tem = Fmarker_buffer (Fcar (info)); | |
330 | /* If buffer being returned to is now deleted, avoid error */ | |
331 | /* Otherwise could get error here while unwinding to top level | |
332 | and crash */ | |
333 | /* In that case, Fmarker_buffer returns nil now. */ | |
56a98455 | 334 | if (NILP (tem)) |
35692fe0 | 335 | return Qnil; |
4ad8681a RS |
336 | |
337 | omark = nmark = Qnil; | |
338 | GCPRO3 (info, omark, nmark); | |
339 | ||
35692fe0 JB |
340 | Fset_buffer (tem); |
341 | tem = Fcar (info); | |
342 | Fgoto_char (tem); | |
343 | unchain_marker (tem); | |
344 | tem = Fcar (Fcdr (info)); | |
03d18690 | 345 | omark = Fmarker_position (current_buffer->mark); |
35692fe0 | 346 | Fset_marker (current_buffer->mark, tem, Fcurrent_buffer ()); |
03d18690 | 347 | nmark = Fmarker_position (tem); |
35692fe0 JB |
348 | unchain_marker (tem); |
349 | tem = Fcdr (Fcdr (info)); | |
ef580991 RS |
350 | #if 0 /* We used to make the current buffer visible in the selected window |
351 | if that was true previously. That avoids some anomalies. | |
352 | But it creates others, and it wasn't documented, and it is simpler | |
353 | and cleaner never to alter the window/buffer connections. */ | |
9772455e RS |
354 | tem1 = Fcar (tem); |
355 | if (!NILP (tem1) | |
0e2c9c70 | 356 | && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer)) |
35692fe0 | 357 | Fswitch_to_buffer (Fcurrent_buffer (), Qnil); |
ef580991 | 358 | #endif /* 0 */ |
9772455e RS |
359 | |
360 | tem1 = current_buffer->mark_active; | |
361 | current_buffer->mark_active = Fcdr (tem); | |
9fed2b18 RS |
362 | if (!NILP (Vrun_hooks)) |
363 | { | |
03d18690 RS |
364 | /* If mark is active now, and either was not active |
365 | or was at a different place, run the activate hook. */ | |
9fed2b18 | 366 | if (! NILP (current_buffer->mark_active)) |
03d18690 RS |
367 | { |
368 | if (! EQ (omark, nmark)) | |
369 | call1 (Vrun_hooks, intern ("activate-mark-hook")); | |
370 | } | |
371 | /* If mark has ceased to be active, run deactivate hook. */ | |
9fed2b18 RS |
372 | else if (! NILP (tem1)) |
373 | call1 (Vrun_hooks, intern ("deactivate-mark-hook")); | |
374 | } | |
4ad8681a | 375 | UNGCPRO; |
35692fe0 JB |
376 | return Qnil; |
377 | } | |
378 | ||
379 | DEFUN ("save-excursion", Fsave_excursion, Ssave_excursion, 0, UNEVALLED, 0, | |
380 | "Save point, mark, and current buffer; execute BODY; restore those things.\n\ | |
381 | Executes BODY just like `progn'.\n\ | |
382 | The values of point, mark and the current buffer are restored\n\ | |
9772455e | 383 | even in case of abnormal exit (throw or error).\n\ |
85cac557 RS |
384 | The state of activation of the mark is also restored.\n\ |
385 | \n\ | |
386 | This construct does not save `deactivate-mark', and therefore\n\ | |
387 | functions that change the buffer will still cause deactivation\n\ | |
388 | of the mark at the end of the command. To prevent that, bind\n\ | |
389 | `deactivate-mark' with `let'.") | |
35692fe0 JB |
390 | (args) |
391 | Lisp_Object args; | |
392 | { | |
393 | register Lisp_Object val; | |
394 | int count = specpdl_ptr - specpdl; | |
395 | ||
396 | record_unwind_protect (save_excursion_restore, save_excursion_save ()); | |
4bc8c7d2 RS |
397 | |
398 | val = Fprogn (args); | |
399 | return unbind_to (count, val); | |
400 | } | |
401 | ||
402 | DEFUN ("save-current-buffer", Fsave_current_buffer, Ssave_current_buffer, 0, UNEVALLED, 0, | |
403 | "Save the current buffer; execute BODY; restore the current buffer.\n\ | |
404 | Executes BODY just like `progn'.") | |
405 | (args) | |
406 | Lisp_Object args; | |
407 | { | |
408 | register Lisp_Object val; | |
409 | int count = specpdl_ptr - specpdl; | |
410 | ||
cb5e5f74 | 411 | record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ()); |
4bc8c7d2 | 412 | |
35692fe0 JB |
413 | val = Fprogn (args); |
414 | return unbind_to (count, val); | |
415 | } | |
416 | \f | |
417 | DEFUN ("buffer-size", Fbufsize, Sbufsize, 0, 0, 0, | |
418 | "Return the number of characters in the current buffer.") | |
419 | () | |
420 | { | |
421 | Lisp_Object temp; | |
55561c63 | 422 | XSETFASTINT (temp, Z - BEG); |
35692fe0 JB |
423 | return temp; |
424 | } | |
425 | ||
426 | DEFUN ("point-min", Fpoint_min, Spoint_min, 0, 0, 0, | |
427 | "Return the minimum permissible value of point in the current buffer.\n\ | |
4c390850 | 428 | This is 1, unless narrowing (a buffer restriction) is in effect.") |
35692fe0 JB |
429 | () |
430 | { | |
431 | Lisp_Object temp; | |
55561c63 | 432 | XSETFASTINT (temp, BEGV); |
35692fe0 JB |
433 | return temp; |
434 | } | |
435 | ||
436 | DEFUN ("point-min-marker", Fpoint_min_marker, Spoint_min_marker, 0, 0, 0, | |
437 | "Return a marker to the minimum permissible value of point in this buffer.\n\ | |
4c390850 | 438 | This is the beginning, unless narrowing (a buffer restriction) is in effect.") |
35692fe0 JB |
439 | () |
440 | { | |
ec1c14f6 | 441 | return buildmark (BEGV, BEGV_BYTE); |
35692fe0 JB |
442 | } |
443 | ||
444 | DEFUN ("point-max", Fpoint_max, Spoint_max, 0, 0, 0, | |
445 | "Return the maximum permissible value of point in the current buffer.\n\ | |
4c390850 RS |
446 | This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\ |
447 | is in effect, in which case it is less.") | |
35692fe0 JB |
448 | () |
449 | { | |
450 | Lisp_Object temp; | |
55561c63 | 451 | XSETFASTINT (temp, ZV); |
35692fe0 JB |
452 | return temp; |
453 | } | |
454 | ||
455 | DEFUN ("point-max-marker", Fpoint_max_marker, Spoint_max_marker, 0, 0, 0, | |
456 | "Return a marker to the maximum permissible value of point in this buffer.\n\ | |
4c390850 RS |
457 | This is (1+ (buffer-size)), unless narrowing (a buffer restriction)\n\ |
458 | is in effect, in which case it is less.") | |
35692fe0 JB |
459 | () |
460 | { | |
ec1c14f6 | 461 | return buildmark (ZV, ZV_BYTE); |
35692fe0 JB |
462 | } |
463 | ||
7df74da6 RS |
464 | DEFUN ("position-bytes", Fposition_bytes, Sposition_bytes, 1, 1, 0, |
465 | "Return the byte position for character position POSITION.") | |
466 | (position) | |
80e01f8d | 467 | Lisp_Object position; |
7df74da6 | 468 | { |
fa8a5a15 KH |
469 | CHECK_NUMBER_COERCE_MARKER (position, 1); |
470 | return make_number (CHAR_TO_BYTE (XINT (position))); | |
7df74da6 RS |
471 | } |
472 | \f | |
850a8179 JB |
473 | DEFUN ("following-char", Ffollowing_char, Sfollowing_char, 0, 0, 0, |
474 | "Return the character following point, as a number.\n\ | |
fb8106e8 KH |
475 | At the end of the buffer or accessible region, return 0.\n\ |
476 | If `enable-multibyte-characters' is nil or point is not\n\ | |
477 | at character boundary, multibyte form is ignored,\n\ | |
478 | and only one byte following point is returned as a character.") | |
35692fe0 JB |
479 | () |
480 | { | |
481 | Lisp_Object temp; | |
6ec8bbd2 | 482 | if (PT >= ZV) |
55561c63 | 483 | XSETFASTINT (temp, 0); |
850a8179 | 484 | else |
ec1c14f6 | 485 | XSETFASTINT (temp, FETCH_CHAR (PT_BYTE)); |
35692fe0 JB |
486 | return temp; |
487 | } | |
488 | ||
850a8179 JB |
489 | DEFUN ("preceding-char", Fprevious_char, Sprevious_char, 0, 0, 0, |
490 | "Return the character preceding point, as a number.\n\ | |
fb8106e8 KH |
491 | At the beginning of the buffer or accessible region, return 0.\n\ |
492 | If `enable-multibyte-characters' is nil or point is not\n\ | |
493 | at character boundary, multi-byte form is ignored,\n\ | |
494 | and only one byte preceding point is returned as a character.") | |
35692fe0 JB |
495 | () |
496 | { | |
497 | Lisp_Object temp; | |
6ec8bbd2 | 498 | if (PT <= BEGV) |
55561c63 | 499 | XSETFASTINT (temp, 0); |
fb8106e8 KH |
500 | else if (!NILP (current_buffer->enable_multibyte_characters)) |
501 | { | |
ec1c14f6 | 502 | int pos = PT_BYTE; |
fb8106e8 KH |
503 | DEC_POS (pos); |
504 | XSETFASTINT (temp, FETCH_CHAR (pos)); | |
505 | } | |
35692fe0 | 506 | else |
ec1c14f6 | 507 | XSETFASTINT (temp, FETCH_BYTE (PT_BYTE - 1)); |
35692fe0 JB |
508 | return temp; |
509 | } | |
510 | ||
511 | DEFUN ("bobp", Fbobp, Sbobp, 0, 0, 0, | |
ec1c14f6 | 512 | "Return t if point is at the beginning of the buffer.\n\ |
35692fe0 JB |
513 | If the buffer is narrowed, this means the beginning of the narrowed part.") |
514 | () | |
515 | { | |
6ec8bbd2 | 516 | if (PT == BEGV) |
35692fe0 JB |
517 | return Qt; |
518 | return Qnil; | |
519 | } | |
520 | ||
521 | DEFUN ("eobp", Feobp, Seobp, 0, 0, 0, | |
ec1c14f6 | 522 | "Return t if point is at the end of the buffer.\n\ |
35692fe0 JB |
523 | If the buffer is narrowed, this means the end of the narrowed part.") |
524 | () | |
525 | { | |
6ec8bbd2 | 526 | if (PT == ZV) |
35692fe0 JB |
527 | return Qt; |
528 | return Qnil; | |
529 | } | |
530 | ||
531 | DEFUN ("bolp", Fbolp, Sbolp, 0, 0, 0, | |
ec1c14f6 | 532 | "Return t if point is at the beginning of a line.") |
35692fe0 JB |
533 | () |
534 | { | |
ec1c14f6 | 535 | if (PT == BEGV || FETCH_BYTE (PT_BYTE - 1) == '\n') |
35692fe0 JB |
536 | return Qt; |
537 | return Qnil; | |
538 | } | |
539 | ||
540 | DEFUN ("eolp", Feolp, Seolp, 0, 0, 0, | |
ec1c14f6 | 541 | "Return t if point is at the end of a line.\n\ |
35692fe0 JB |
542 | `End of a line' includes point being at the end of the buffer.") |
543 | () | |
544 | { | |
ec1c14f6 | 545 | if (PT == ZV || FETCH_BYTE (PT_BYTE) == '\n') |
35692fe0 JB |
546 | return Qt; |
547 | return Qnil; | |
548 | } | |
549 | ||
fa1d3816 | 550 | DEFUN ("char-after", Fchar_after, Schar_after, 0, 1, 0, |
35692fe0 JB |
551 | "Return character in current buffer at position POS.\n\ |
552 | POS is an integer or a buffer pointer.\n\ | |
fb8106e8 KH |
553 | If POS is out of range, the value is nil.\n\ |
554 | If `enable-multibyte-characters' is nil or POS is not at character boundary,\n\ | |
555 | multi-byte form is ignored, and only one byte at POS\n\ | |
556 | is returned as a character.") | |
35692fe0 JB |
557 | (pos) |
558 | Lisp_Object pos; | |
559 | { | |
ec1c14f6 | 560 | register int pos_byte; |
35692fe0 | 561 | register Lisp_Object val; |
35692fe0 | 562 | |
fa1d3816 | 563 | if (NILP (pos)) |
85cac557 RS |
564 | pos_byte = PT_BYTE; |
565 | else if (MARKERP (pos)) | |
566 | { | |
567 | pos_byte = marker_byte_position (pos); | |
568 | if (pos_byte < BEGV_BYTE || pos_byte >= ZV_BYTE) | |
569 | return Qnil; | |
570 | } | |
fa1d3816 RS |
571 | else |
572 | { | |
573 | CHECK_NUMBER_COERCE_MARKER (pos, 0); | |
85cac557 RS |
574 | if (pos < BEGV || pos >= ZV) |
575 | return Qnil; | |
ec1c14f6 RS |
576 | |
577 | pos_byte = CHAR_TO_BYTE (XINT (pos)); | |
fa1d3816 | 578 | } |
35692fe0 | 579 | |
ec1c14f6 | 580 | return make_number (FETCH_CHAR (pos_byte)); |
35692fe0 | 581 | } |
fb8106e8 | 582 | |
fa1d3816 | 583 | DEFUN ("char-before", Fchar_before, Schar_before, 0, 1, 0, |
fb8106e8 KH |
584 | "Return character in current buffer preceding position POS.\n\ |
585 | POS is an integer or a buffer pointer.\n\ | |
586 | If POS is out of range, the value is nil.\n\ | |
587 | If `enable-multibyte-characters' is nil or POS is not at character boundary,\n\ | |
588 | multi-byte form is ignored, and only one byte preceding POS\n\ | |
589 | is returned as a character.") | |
590 | (pos) | |
591 | Lisp_Object pos; | |
592 | { | |
593 | register Lisp_Object val; | |
ec1c14f6 | 594 | register int pos_byte; |
fb8106e8 | 595 | |
fa1d3816 | 596 | if (NILP (pos)) |
ec1c14f6 RS |
597 | pos_byte = PT_BYTE; |
598 | else if (MARKERP (pos)) | |
85cac557 RS |
599 | { |
600 | pos_byte = marker_byte_position (pos); | |
601 | ||
602 | if (pos_byte <= BEGV_BYTE || pos_byte > ZV_BYTE) | |
603 | return Qnil; | |
604 | } | |
fa1d3816 RS |
605 | else |
606 | { | |
607 | CHECK_NUMBER_COERCE_MARKER (pos, 0); | |
fb8106e8 | 608 | |
85cac557 RS |
609 | if (pos <= BEGV || pos > ZV) |
610 | return Qnil; | |
611 | ||
ec1c14f6 | 612 | pos_byte = CHAR_TO_BYTE (XINT (pos)); |
fa1d3816 | 613 | } |
fb8106e8 KH |
614 | |
615 | if (!NILP (current_buffer->enable_multibyte_characters)) | |
616 | { | |
ec1c14f6 RS |
617 | DEC_POS (pos_byte); |
618 | XSETFASTINT (val, FETCH_CHAR (pos_byte)); | |
fb8106e8 KH |
619 | } |
620 | else | |
621 | { | |
ec1c14f6 RS |
622 | pos_byte--; |
623 | XSETFASTINT (val, FETCH_BYTE (pos_byte)); | |
fb8106e8 KH |
624 | } |
625 | return val; | |
626 | } | |
35692fe0 | 627 | \f |
87485d6f | 628 | DEFUN ("user-login-name", Fuser_login_name, Suser_login_name, 0, 1, 0, |
35692fe0 JB |
629 | "Return the name under which the user logged in, as a string.\n\ |
630 | This is based on the effective uid, not the real uid.\n\ | |
2c9ae24e | 631 | Also, if the environment variable LOGNAME or USER is set,\n\ |
87485d6f MW |
632 | that determines the value of this function.\n\n\ |
633 | If optional argument UID is an integer, return the login name of the user\n\ | |
634 | with that uid, or nil if there is no such user.") | |
635 | (uid) | |
636 | Lisp_Object uid; | |
35692fe0 | 637 | { |
87485d6f MW |
638 | struct passwd *pw; |
639 | ||
f8a0e364 RS |
640 | /* Set up the user name info if we didn't do it before. |
641 | (That can happen if Emacs is dumpable | |
642 | but you decide to run `temacs -l loadup' and not dump. */ | |
35b34f72 | 643 | if (INTEGERP (Vuser_login_name)) |
f8a0e364 | 644 | init_editfns (); |
87485d6f MW |
645 | |
646 | if (NILP (uid)) | |
35b34f72 | 647 | return Vuser_login_name; |
87485d6f MW |
648 | |
649 | CHECK_NUMBER (uid, 0); | |
650 | pw = (struct passwd *) getpwuid (XINT (uid)); | |
651 | return (pw ? build_string (pw->pw_name) : Qnil); | |
35692fe0 JB |
652 | } |
653 | ||
654 | DEFUN ("user-real-login-name", Fuser_real_login_name, Suser_real_login_name, | |
655 | 0, 0, 0, | |
656 | "Return the name of the user's real uid, as a string.\n\ | |
9658bdd0 | 657 | This ignores the environment variables LOGNAME and USER, so it differs from\n\ |
b1da234a | 658 | `user-login-name' when running under `su'.") |
35692fe0 JB |
659 | () |
660 | { | |
f8a0e364 RS |
661 | /* Set up the user name info if we didn't do it before. |
662 | (That can happen if Emacs is dumpable | |
663 | but you decide to run `temacs -l loadup' and not dump. */ | |
35b34f72 | 664 | if (INTEGERP (Vuser_login_name)) |
f8a0e364 | 665 | init_editfns (); |
35b34f72 | 666 | return Vuser_real_login_name; |
35692fe0 JB |
667 | } |
668 | ||
669 | DEFUN ("user-uid", Fuser_uid, Suser_uid, 0, 0, 0, | |
670 | "Return the effective uid of Emacs, as an integer.") | |
671 | () | |
672 | { | |
673 | return make_number (geteuid ()); | |
674 | } | |
675 | ||
676 | DEFUN ("user-real-uid", Fuser_real_uid, Suser_real_uid, 0, 0, 0, | |
677 | "Return the real uid of Emacs, as an integer.") | |
678 | () | |
679 | { | |
680 | return make_number (getuid ()); | |
681 | } | |
682 | ||
c9ed721d RS |
683 | DEFUN ("user-full-name", Fuser_full_name, Suser_full_name, 0, 1, 0, |
684 | "Return the full name of the user logged in, as a string.\n\ | |
685 | If optional argument UID is an integer, return the full name of the user\n\ | |
ca38bbb2 | 686 | with that uid, or \"unknown\" if there is no such user.\n\ |
3415b0e9 RS |
687 | If UID is a string, return the full name of the user with that login\n\ |
688 | name, or \"unknown\" if no such user could be found.") | |
c9ed721d RS |
689 | (uid) |
690 | Lisp_Object uid; | |
35692fe0 | 691 | { |
c9ed721d | 692 | struct passwd *pw; |
b0e92acd | 693 | register unsigned char *p, *q; |
3415b0e9 RS |
694 | extern char *index (); |
695 | Lisp_Object full; | |
c9ed721d RS |
696 | |
697 | if (NILP (uid)) | |
3415b0e9 RS |
698 | return Vuser_full_name; |
699 | else if (NUMBERP (uid)) | |
700 | pw = (struct passwd *) getpwuid (XINT (uid)); | |
701 | else if (STRINGP (uid)) | |
702 | pw = (struct passwd *) getpwnam (XSTRING (uid)->data); | |
703 | else | |
704 | error ("Invalid UID specification"); | |
c9ed721d | 705 | |
3415b0e9 | 706 | if (!pw) |
3347526c | 707 | return Qnil; |
3415b0e9 RS |
708 | |
709 | p = (unsigned char *) USER_FULL_NAME; | |
710 | /* Chop off everything after the first comma. */ | |
711 | q = (unsigned char *) index (p, ','); | |
712 | full = make_string (p, q ? q - p : strlen (p)); | |
713 | ||
714 | #ifdef AMPERSAND_FULL_NAME | |
715 | p = XSTRING (full)->data; | |
716 | q = (unsigned char *) index (p, '&'); | |
717 | /* Substitute the login name for the &, upcasing the first character. */ | |
718 | if (q) | |
719 | { | |
b0e92acd | 720 | register unsigned char *r; |
3415b0e9 RS |
721 | Lisp_Object login; |
722 | ||
723 | login = Fuser_login_name (make_number (pw->pw_uid)); | |
724 | r = (unsigned char *) alloca (strlen (p) + XSTRING (login)->size + 1); | |
725 | bcopy (p, r, q - p); | |
726 | r[q - p] = 0; | |
727 | strcat (r, XSTRING (login)->data); | |
728 | r[q - p] = UPCASE (r[q - p]); | |
729 | strcat (r, q + 1); | |
730 | full = build_string (r); | |
731 | } | |
732 | #endif /* AMPERSAND_FULL_NAME */ | |
733 | ||
734 | return full; | |
35692fe0 JB |
735 | } |
736 | ||
737 | DEFUN ("system-name", Fsystem_name, Ssystem_name, 0, 0, 0, | |
738 | "Return the name of the machine you are running on, as a string.") | |
739 | () | |
740 | { | |
741 | return Vsystem_name; | |
742 | } | |
743 | ||
ac988277 KH |
744 | /* For the benefit of callers who don't want to include lisp.h */ |
745 | char * | |
746 | get_system_name () | |
747 | { | |
3d976a9a RS |
748 | if (STRINGP (Vsystem_name)) |
749 | return (char *) XSTRING (Vsystem_name)->data; | |
750 | else | |
751 | return ""; | |
ac988277 KH |
752 | } |
753 | ||
7fd233b3 RS |
754 | DEFUN ("emacs-pid", Femacs_pid, Semacs_pid, 0, 0, 0, |
755 | "Return the process ID of Emacs, as an integer.") | |
756 | () | |
757 | { | |
758 | return make_number (getpid ()); | |
759 | } | |
760 | ||
d940e0e4 | 761 | DEFUN ("current-time", Fcurrent_time, Scurrent_time, 0, 0, 0, |
e983fdb2 | 762 | "Return the current time, as the number of seconds since 1970-01-01 00:00:00.\n\ |
956ace37 JB |
763 | The time is returned as a list of three integers. The first has the\n\ |
764 | most significant 16 bits of the seconds, while the second has the\n\ | |
765 | least significant 16 bits. The third integer gives the microsecond\n\ | |
766 | count.\n\ | |
767 | \n\ | |
768 | The microsecond count is zero on systems that do not provide\n\ | |
769 | resolution finer than a second.") | |
d940e0e4 JB |
770 | () |
771 | { | |
956ace37 JB |
772 | EMACS_TIME t; |
773 | Lisp_Object result[3]; | |
774 | ||
775 | EMACS_GET_TIME (t); | |
d2fd0445 KH |
776 | XSETINT (result[0], (EMACS_SECS (t) >> 16) & 0xffff); |
777 | XSETINT (result[1], (EMACS_SECS (t) >> 0) & 0xffff); | |
778 | XSETINT (result[2], EMACS_USECS (t)); | |
956ace37 JB |
779 | |
780 | return Flist (3, result); | |
d940e0e4 JB |
781 | } |
782 | \f | |
783 | ||
e3120ab5 JB |
784 | static int |
785 | lisp_time_argument (specified_time, result) | |
786 | Lisp_Object specified_time; | |
787 | time_t *result; | |
788 | { | |
789 | if (NILP (specified_time)) | |
790 | return time (result) != -1; | |
791 | else | |
792 | { | |
793 | Lisp_Object high, low; | |
794 | high = Fcar (specified_time); | |
795 | CHECK_NUMBER (high, 0); | |
796 | low = Fcdr (specified_time); | |
ae683129 | 797 | if (CONSP (low)) |
e3120ab5 JB |
798 | low = Fcar (low); |
799 | CHECK_NUMBER (low, 0); | |
800 | *result = (XINT (high) << 16) + (XINT (low) & 0xffff); | |
801 | return *result >> 16 == XINT (high); | |
802 | } | |
803 | } | |
804 | ||
3efcc98a | 805 | /* |
b48382a0 RS |
806 | DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0, |
807 | "Use FORMAT-STRING to format the time TIME, or now if omitted.\n\ | |
808 | TIME is specified as (HIGH LOW . IGNORED) or (HIGH . LOW), as returned by\n\ | |
809 | `current-time' or `file-attributes'.\n\ | |
810 | The third, optional, argument UNIVERSAL, if non-nil, means describe TIME\n\ | |
811 | as Universal Time; nil means describe TIME in the local time zone.\n\ | |
812 | The value is a copy of FORMAT-STRING, but with certain constructs replaced\n\ | |
813 | by text that describes the specified date and time in TIME:\n\ | |
a82d387c | 814 | \n\ |
b48382a0 RS |
815 | %Y is the year, %y within the century, %C the century.\n\ |
816 | %G is the year corresponding to the ISO week, %g within the century.\n\ | |
b771d0da PE |
817 | %m is the numeric month.\n\ |
818 | %b and %h are the locale's abbreviated month name, %B the full name.\n\ | |
b48382a0 RS |
819 | %d is the day of the month, zero-padded, %e is blank-padded.\n\ |
820 | %u is the numeric day of week from 1 (Monday) to 7, %w from 0 (Sunday) to 6.\n\ | |
b771d0da | 821 | %a is the locale's abbreviated name of the day of week, %A the full name.\n\ |
b48382a0 RS |
822 | %U is the week number starting on Sunday, %W starting on Monday,\n\ |
823 | %V according to ISO 8601.\n\ | |
824 | %j is the day of the year.\n\ | |
825 | \n\ | |
826 | %H is the hour on a 24-hour clock, %I is on a 12-hour clock, %k is like %H\n\ | |
827 | only blank-padded, %l is like %I blank-padded.\n\ | |
b771d0da | 828 | %p is the locale's equivalent of either AM or PM.\n\ |
b48382a0 RS |
829 | %M is the minute.\n\ |
830 | %S is the second.\n\ | |
831 | %Z is the time zone name, %z is the numeric form.\n\ | |
832 | %s is the number of seconds since 1970-01-01 00:00:00 +0000.\n\ | |
833 | \n\ | |
834 | %c is the locale's date and time format.\n\ | |
835 | %x is the locale's \"preferred\" date format.\n\ | |
836 | %D is like \"%m/%d/%y\".\n\ | |
837 | \n\ | |
838 | %R is like \"%H:%M\", %T is like \"%H:%M:%S\", %r is like \"%I:%M:%S %p\".\n\ | |
839 | %X is the locale's \"preferred\" time format.\n\ | |
840 | \n\ | |
b771d0da | 841 | Finally, %n is a newline, %t is a tab, %% is a literal %.\n\ |
b48382a0 | 842 | \n\ |
3efcc98a | 843 | Certain flags and modifiers are available with some format controls.\n\ |
b48382a0 RS |
844 | The flags are `_' and `-'. For certain characters X, %_X is like %X,\n\ |
845 | but padded with blanks; %-X is like %X, but without padding.\n\ | |
846 | %NX (where N stands for an integer) is like %X,\n\ | |
847 | but takes up at least N (a number) positions.\n\ | |
848 | The modifiers are `E' and `O'. For certain characters X,\n\ | |
849 | %EX is a locale's alternative version of %X;\n\ | |
850 | %OX is like %X, but uses the locale's number symbols.\n\ | |
851 | \n\ | |
852 | For example, to produce full ISO 8601 format, use \"%Y-%m-%dT%T%z\".") | |
726ed0da | 853 | (format_string, time, universal) |
3efcc98a RS |
854 | */ |
855 | ||
856 | DEFUN ("format-time-string", Fformat_time_string, Sformat_time_string, 1, 3, 0, | |
857 | 0 /* See immediately above */) | |
b48382a0 RS |
858 | (format_string, time, universal) |
859 | Lisp_Object format_string, time, universal; | |
a82d387c RS |
860 | { |
861 | time_t value; | |
862 | int size; | |
863 | ||
864 | CHECK_STRING (format_string, 1); | |
865 | ||
866 | if (! lisp_time_argument (time, &value)) | |
867 | error ("Invalid time specification"); | |
868 | ||
869 | /* This is probably enough. */ | |
dc3620af | 870 | size = STRING_BYTES (XSTRING (format_string)) * 6 + 50; |
a82d387c RS |
871 | |
872 | while (1) | |
873 | { | |
b48382a0 RS |
874 | char *buf = (char *) alloca (size + 1); |
875 | int result; | |
876 | ||
bfbcc5ee | 877 | buf[0] = '\1'; |
b48382a0 RS |
878 | result = emacs_strftime (buf, size, XSTRING (format_string)->data, |
879 | (NILP (universal) ? localtime (&value) | |
880 | : gmtime (&value))); | |
bfbcc5ee | 881 | if ((result > 0 && result < size) || (result == 0 && buf[0] == '\0')) |
a82d387c | 882 | return build_string (buf); |
b48382a0 RS |
883 | |
884 | /* If buffer was too small, make it bigger and try again. */ | |
bfbcc5ee | 885 | result = emacs_strftime (NULL, 0x7fffffff, XSTRING (format_string)->data, |
b48382a0 RS |
886 | (NILP (universal) ? localtime (&value) |
887 | : gmtime (&value))); | |
888 | size = result + 1; | |
a82d387c RS |
889 | } |
890 | } | |
891 | ||
4691c06d RS |
892 | DEFUN ("decode-time", Fdecode_time, Sdecode_time, 0, 1, 0, |
893 | "Decode a time value as (SEC MINUTE HOUR DAY MONTH YEAR DOW DST ZONE).\n\ | |
894 | The optional SPECIFIED-TIME should be a list of (HIGH LOW . IGNORED)\n\ | |
895 | or (HIGH . LOW), as from `current-time' and `file-attributes', or `nil'\n\ | |
896 | to use the current time. The list has the following nine members:\n\ | |
145b0681 RS |
897 | SEC is an integer between 0 and 60; SEC is 60 for a leap second, which\n\ |
898 | only some operating systems support. MINUTE is an integer between 0 and 59.\n\ | |
4691c06d RS |
899 | HOUR is an integer between 0 and 23. DAY is an integer between 1 and 31.\n\ |
900 | MONTH is an integer between 1 and 12. YEAR is an integer indicating the\n\ | |
901 | four-digit year. DOW is the day of week, an integer between 0 and 6, where\n\ | |
902 | 0 is Sunday. DST is t if daylight savings time is effect, otherwise nil.\n\ | |
903 | ZONE is an integer indicating the number of seconds east of Greenwich.\n\ | |
2c6c7c72 | 904 | \(Note that Common Lisp has different meanings for DOW and ZONE.)") |
4691c06d RS |
905 | (specified_time) |
906 | Lisp_Object specified_time; | |
907 | { | |
908 | time_t time_spec; | |
3c887943 | 909 | struct tm save_tm; |
4691c06d RS |
910 | struct tm *decoded_time; |
911 | Lisp_Object list_args[9]; | |
912 | ||
913 | if (! lisp_time_argument (specified_time, &time_spec)) | |
914 | error ("Invalid time specification"); | |
915 | ||
916 | decoded_time = localtime (&time_spec); | |
3c887943 KH |
917 | XSETFASTINT (list_args[0], decoded_time->tm_sec); |
918 | XSETFASTINT (list_args[1], decoded_time->tm_min); | |
919 | XSETFASTINT (list_args[2], decoded_time->tm_hour); | |
920 | XSETFASTINT (list_args[3], decoded_time->tm_mday); | |
921 | XSETFASTINT (list_args[4], decoded_time->tm_mon + 1); | |
236ebf35 | 922 | XSETINT (list_args[5], decoded_time->tm_year + 1900); |
3c887943 | 923 | XSETFASTINT (list_args[6], decoded_time->tm_wday); |
4691c06d | 924 | list_args[7] = (decoded_time->tm_isdst)? Qt : Qnil; |
3c887943 KH |
925 | |
926 | /* Make a copy, in case gmtime modifies the struct. */ | |
927 | save_tm = *decoded_time; | |
928 | decoded_time = gmtime (&time_spec); | |
929 | if (decoded_time == 0) | |
930 | list_args[8] = Qnil; | |
931 | else | |
94751666 | 932 | XSETINT (list_args[8], tm_diff (&save_tm, decoded_time)); |
4691c06d RS |
933 | return Flist (9, list_args); |
934 | } | |
935 | ||
6ee9061c | 936 | DEFUN ("encode-time", Fencode_time, Sencode_time, 6, MANY, 0, |
2591ec64 | 937 | "Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time.\n\ |
6ee9061c RS |
938 | This is the reverse operation of `decode-time', which see.\n\ |
939 | ZONE defaults to the current time zone rule. This can\n\ | |
085e9fcb | 940 | be a string or t (as from `set-time-zone-rule'), or it can be a list\n\ |
7459d65b | 941 | \(as from `current-time-zone') or an integer (as from `decode-time')\n\ |
c59b5089 | 942 | applied without consideration for daylight savings time.\n\ |
6ee9061c RS |
943 | \n\ |
944 | You can pass more than 7 arguments; then the first six arguments\n\ | |
945 | are used as SECOND through YEAR, and the *last* argument is used as ZONE.\n\ | |
946 | The intervening arguments are ignored.\n\ | |
947 | This feature lets (apply 'encode-time (decode-time ...)) work.\n\ | |
948 | \n\ | |
c59b5089 PE |
949 | Out-of-range values for SEC, MINUTE, HOUR, DAY, or MONTH are allowed;\n\ |
950 | for example, a DAY of 0 means the day preceding the given month.\n\ | |
01ba8cce | 951 | Year numbers less than 100 are treated just like other year numbers.\n\ |
c59b5089 | 952 | If you want them to stand for years in this century, you must do that yourself.") |
6ee9061c RS |
953 | (nargs, args) |
954 | int nargs; | |
955 | register Lisp_Object *args; | |
cce7b8a0 | 956 | { |
1b8fa736 | 957 | time_t time; |
c59b5089 | 958 | struct tm tm; |
60653898 | 959 | Lisp_Object zone = (nargs > 6 ? args[nargs - 1] : Qnil); |
6ee9061c RS |
960 | |
961 | CHECK_NUMBER (args[0], 0); /* second */ | |
962 | CHECK_NUMBER (args[1], 1); /* minute */ | |
963 | CHECK_NUMBER (args[2], 2); /* hour */ | |
964 | CHECK_NUMBER (args[3], 3); /* day */ | |
965 | CHECK_NUMBER (args[4], 4); /* month */ | |
966 | CHECK_NUMBER (args[5], 5); /* year */ | |
967 | ||
968 | tm.tm_sec = XINT (args[0]); | |
969 | tm.tm_min = XINT (args[1]); | |
970 | tm.tm_hour = XINT (args[2]); | |
971 | tm.tm_mday = XINT (args[3]); | |
972 | tm.tm_mon = XINT (args[4]) - 1; | |
973 | tm.tm_year = XINT (args[5]) - 1900; | |
c59b5089 PE |
974 | tm.tm_isdst = -1; |
975 | ||
976 | if (CONSP (zone)) | |
977 | zone = Fcar (zone); | |
1b8fa736 | 978 | if (NILP (zone)) |
c59b5089 PE |
979 | time = mktime (&tm); |
980 | else | |
1b8fa736 | 981 | { |
c59b5089 PE |
982 | char tzbuf[100]; |
983 | char *tzstring; | |
984 | char **oldenv = environ, **newenv; | |
985 | ||
2e34157c | 986 | if (EQ (zone, Qt)) |
085e9fcb EN |
987 | tzstring = "UTC0"; |
988 | else if (STRINGP (zone)) | |
4d4c1514 | 989 | tzstring = (char *) XSTRING (zone)->data; |
c59b5089 | 990 | else if (INTEGERP (zone)) |
1b8fa736 | 991 | { |
c59b5089 PE |
992 | int abszone = abs (XINT (zone)); |
993 | sprintf (tzbuf, "XXX%s%d:%02d:%02d", "-" + (XINT (zone) < 0), | |
994 | abszone / (60*60), (abszone/60) % 60, abszone % 60); | |
995 | tzstring = tzbuf; | |
1b8fa736 | 996 | } |
c59b5089 PE |
997 | else |
998 | error ("Invalid time zone specification"); | |
999 | ||
1000 | /* Set TZ before calling mktime; merely adjusting mktime's returned | |
1001 | value doesn't suffice, since that would mishandle leap seconds. */ | |
1002 | set_time_zone_rule (tzstring); | |
1003 | ||
1004 | time = mktime (&tm); | |
1005 | ||
1006 | /* Restore TZ to previous value. */ | |
1007 | newenv = environ; | |
1008 | environ = oldenv; | |
c0efcacf | 1009 | xfree (newenv); |
c59b5089 PE |
1010 | #ifdef LOCALTIME_CACHE |
1011 | tzset (); | |
1012 | #endif | |
1b8fa736 | 1013 | } |
1b8fa736 | 1014 | |
c59b5089 PE |
1015 | if (time == (time_t) -1) |
1016 | error ("Specified time is not representable"); | |
1017 | ||
1018 | return make_time (time); | |
cce7b8a0 RS |
1019 | } |
1020 | ||
2148f2b4 | 1021 | DEFUN ("current-time-string", Fcurrent_time_string, Scurrent_time_string, 0, 1, 0, |
35692fe0 | 1022 | "Return the current time, as a human-readable string.\n\ |
2148f2b4 RS |
1023 | Programs can use this function to decode a time,\n\ |
1024 | since the number of columns in each field is fixed.\n\ | |
1025 | The format is `Sun Sep 16 01:03:52 1973'.\n\ | |
7a8630da RS |
1026 | However, see also the functions `decode-time' and `format-time-string'\n\ |
1027 | which provide a much more powerful and general facility.\n\ | |
1028 | \n\ | |
2148f2b4 RS |
1029 | If an argument is given, it specifies a time to format\n\ |
1030 | instead of the current time. The argument should have the form:\n\ | |
1031 | (HIGH . LOW)\n\ | |
1032 | or the form:\n\ | |
1033 | (HIGH LOW . IGNORED).\n\ | |
1034 | Thus, you can use times obtained from `current-time'\n\ | |
1035 | and from `file-attributes'.") | |
1036 | (specified_time) | |
1037 | Lisp_Object specified_time; | |
1038 | { | |
e3120ab5 | 1039 | time_t value; |
35692fe0 | 1040 | char buf[30]; |
2148f2b4 RS |
1041 | register char *tem; |
1042 | ||
e3120ab5 JB |
1043 | if (! lisp_time_argument (specified_time, &value)) |
1044 | value = -1; | |
2148f2b4 | 1045 | tem = (char *) ctime (&value); |
35692fe0 JB |
1046 | |
1047 | strncpy (buf, tem, 24); | |
1048 | buf[24] = 0; | |
1049 | ||
1050 | return build_string (buf); | |
1051 | } | |
c2662aea | 1052 | |
94751666 | 1053 | #define TM_YEAR_BASE 1900 |
e3120ab5 | 1054 | |
94751666 PE |
1055 | /* Yield A - B, measured in seconds. |
1056 | This function is copied from the GNU C Library. */ | |
1057 | static int | |
1058 | tm_diff (a, b) | |
e3120ab5 JB |
1059 | struct tm *a, *b; |
1060 | { | |
94751666 PE |
1061 | /* Compute intervening leap days correctly even if year is negative. |
1062 | Take care to avoid int overflow in leap day calculations, | |
1063 | but it's OK to assume that A and B are close to each other. */ | |
1064 | int a4 = (a->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (a->tm_year & 3); | |
1065 | int b4 = (b->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (b->tm_year & 3); | |
1066 | int a100 = a4 / 25 - (a4 % 25 < 0); | |
1067 | int b100 = b4 / 25 - (b4 % 25 < 0); | |
1068 | int a400 = a100 >> 2; | |
1069 | int b400 = b100 >> 2; | |
1070 | int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400); | |
1071 | int years = a->tm_year - b->tm_year; | |
1072 | int days = (365 * years + intervening_leap_days | |
1073 | + (a->tm_yday - b->tm_yday)); | |
1074 | return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour)) | |
1075 | + (a->tm_min - b->tm_min)) | |
8e718b4e | 1076 | + (a->tm_sec - b->tm_sec)); |
e3120ab5 JB |
1077 | } |
1078 | ||
1079 | DEFUN ("current-time-zone", Fcurrent_time_zone, Scurrent_time_zone, 0, 1, 0, | |
1080 | "Return the offset and name for the local time zone.\n\ | |
1081 | This returns a list of the form (OFFSET NAME).\n\ | |
1082 | OFFSET is an integer number of seconds ahead of UTC (east of Greenwich).\n\ | |
1083 | A negative value means west of Greenwich.\n\ | |
1084 | NAME is a string giving the name of the time zone.\n\ | |
1085 | If an argument is given, it specifies when the time zone offset is determined\n\ | |
1086 | instead of using the current time. The argument should have the form:\n\ | |
1087 | (HIGH . LOW)\n\ | |
1088 | or the form:\n\ | |
1089 | (HIGH LOW . IGNORED).\n\ | |
1090 | Thus, you can use times obtained from `current-time'\n\ | |
1091 | and from `file-attributes'.\n\ | |
773c1fd3 JB |
1092 | \n\ |
1093 | Some operating systems cannot provide all this information to Emacs;\n\ | |
2d88f747 | 1094 | in this case, `current-time-zone' returns a list containing nil for\n\ |
773c1fd3 | 1095 | the data it can't find.") |
e3120ab5 JB |
1096 | (specified_time) |
1097 | Lisp_Object specified_time; | |
c2662aea | 1098 | { |
e3120ab5 JB |
1099 | time_t value; |
1100 | struct tm *t; | |
c2662aea | 1101 | |
e3120ab5 | 1102 | if (lisp_time_argument (specified_time, &value) |
2d88f747 | 1103 | && (t = gmtime (&value)) != 0) |
e3120ab5 | 1104 | { |
2d88f747 | 1105 | struct tm gmt; |
94751666 | 1106 | int offset; |
e3120ab5 | 1107 | char *s, buf[6]; |
2d88f747 RS |
1108 | |
1109 | gmt = *t; /* Make a copy, in case localtime modifies *t. */ | |
1110 | t = localtime (&value); | |
94751666 | 1111 | offset = tm_diff (t, &gmt); |
e3120ab5 JB |
1112 | s = 0; |
1113 | #ifdef HAVE_TM_ZONE | |
1114 | if (t->tm_zone) | |
5fd4de15 | 1115 | s = (char *)t->tm_zone; |
a7971c39 RS |
1116 | #else /* not HAVE_TM_ZONE */ |
1117 | #ifdef HAVE_TZNAME | |
1118 | if (t->tm_isdst == 0 || t->tm_isdst == 1) | |
1119 | s = tzname[t->tm_isdst]; | |
c2662aea | 1120 | #endif |
a7971c39 | 1121 | #endif /* not HAVE_TM_ZONE */ |
e3120ab5 JB |
1122 | if (!s) |
1123 | { | |
1124 | /* No local time zone name is available; use "+-NNNN" instead. */ | |
00fc94d0 | 1125 | int am = (offset < 0 ? -offset : offset) / 60; |
e3120ab5 JB |
1126 | sprintf (buf, "%c%02d%02d", (offset < 0 ? '-' : '+'), am/60, am%60); |
1127 | s = buf; | |
1128 | } | |
1129 | return Fcons (make_number (offset), Fcons (build_string (s), Qnil)); | |
1130 | } | |
1131 | else | |
09dbcf71 | 1132 | return Fmake_list (make_number (2), Qnil); |
c2662aea JB |
1133 | } |
1134 | ||
260e2e2a KH |
1135 | /* This holds the value of `environ' produced by the previous |
1136 | call to Fset_time_zone_rule, or 0 if Fset_time_zone_rule | |
1137 | has never been called. */ | |
1138 | static char **environbuf; | |
1139 | ||
143cb9a9 RS |
1140 | DEFUN ("set-time-zone-rule", Fset_time_zone_rule, Sset_time_zone_rule, 1, 1, 0, |
1141 | "Set the local time zone using TZ, a string specifying a time zone rule.\n\ | |
085e9fcb EN |
1142 | If TZ is nil, use implementation-defined default time zone information.\n\ |
1143 | If TZ is t, use Universal Time.") | |
143cb9a9 RS |
1144 | (tz) |
1145 | Lisp_Object tz; | |
1146 | { | |
143cb9a9 RS |
1147 | char *tzstring; |
1148 | ||
1149 | if (NILP (tz)) | |
1150 | tzstring = 0; | |
2e34157c | 1151 | else if (EQ (tz, Qt)) |
085e9fcb | 1152 | tzstring = "UTC0"; |
143cb9a9 RS |
1153 | else |
1154 | { | |
1155 | CHECK_STRING (tz, 0); | |
4d4c1514 | 1156 | tzstring = (char *) XSTRING (tz)->data; |
143cb9a9 RS |
1157 | } |
1158 | ||
c59b5089 PE |
1159 | set_time_zone_rule (tzstring); |
1160 | if (environbuf) | |
1161 | free (environbuf); | |
1162 | environbuf = environ; | |
1163 | ||
1164 | return Qnil; | |
1165 | } | |
1166 | ||
e0bf9faf PE |
1167 | #ifdef LOCALTIME_CACHE |
1168 | ||
1169 | /* These two values are known to load tz files in buggy implementations, | |
1170 | i.e. Solaris 1 executables running under either Solaris 1 or Solaris 2. | |
1155c453 RS |
1171 | Their values shouldn't matter in non-buggy implementations. |
1172 | We don't use string literals for these strings, | |
1173 | since if a string in the environment is in readonly | |
1174 | storage, it runs afoul of bugs in SVR4 and Solaris 2.3. | |
1175 | See Sun bugs 1113095 and 1114114, ``Timezone routines | |
1176 | improperly modify environment''. */ | |
1177 | ||
e0bf9faf PE |
1178 | static char set_time_zone_rule_tz1[] = "TZ=GMT+0"; |
1179 | static char set_time_zone_rule_tz2[] = "TZ=GMT+1"; | |
1180 | ||
1181 | #endif | |
1155c453 | 1182 | |
c59b5089 PE |
1183 | /* Set the local time zone rule to TZSTRING. |
1184 | This allocates memory into `environ', which it is the caller's | |
1185 | responsibility to free. */ | |
a92ae0ce | 1186 | void |
c59b5089 PE |
1187 | set_time_zone_rule (tzstring) |
1188 | char *tzstring; | |
1189 | { | |
1190 | int envptrs; | |
1191 | char **from, **to, **newenv; | |
1192 | ||
aafe5147 | 1193 | /* Make the ENVIRON vector longer with room for TZSTRING. */ |
143cb9a9 RS |
1194 | for (from = environ; *from; from++) |
1195 | continue; | |
1196 | envptrs = from - environ + 2; | |
1197 | newenv = to = (char **) xmalloc (envptrs * sizeof (char *) | |
1198 | + (tzstring ? strlen (tzstring) + 4 : 0)); | |
aafe5147 RS |
1199 | |
1200 | /* Add TZSTRING to the end of environ, as a value for TZ. */ | |
143cb9a9 RS |
1201 | if (tzstring) |
1202 | { | |
1203 | char *t = (char *) (to + envptrs); | |
1204 | strcpy (t, "TZ="); | |
1205 | strcat (t, tzstring); | |
1206 | *to++ = t; | |
1207 | } | |
1208 | ||
aafe5147 RS |
1209 | /* Copy the old environ vector elements into NEWENV, |
1210 | but don't copy the TZ variable. | |
1211 | So we have only one definition of TZ, which came from TZSTRING. */ | |
143cb9a9 RS |
1212 | for (from = environ; *from; from++) |
1213 | if (strncmp (*from, "TZ=", 3) != 0) | |
1214 | *to++ = *from; | |
1215 | *to = 0; | |
1216 | ||
1217 | environ = newenv; | |
143cb9a9 | 1218 | |
aafe5147 RS |
1219 | /* If we do have a TZSTRING, NEWENV points to the vector slot where |
1220 | the TZ variable is stored. If we do not have a TZSTRING, | |
1221 | TO points to the vector slot which has the terminating null. */ | |
1222 | ||
143cb9a9 | 1223 | #ifdef LOCALTIME_CACHE |
aafe5147 RS |
1224 | { |
1225 | /* In SunOS 4.1.3_U1 and 4.1.4, if TZ has a value like | |
1226 | "US/Pacific" that loads a tz file, then changes to a value like | |
1227 | "XXX0" that does not load a tz file, and then changes back to | |
1228 | its original value, the last change is (incorrectly) ignored. | |
1229 | Also, if TZ changes twice in succession to values that do | |
1230 | not load a tz file, tzset can dump core (see Sun bug#1225179). | |
1231 | The following code works around these bugs. */ | |
1232 | ||
aafe5147 RS |
1233 | if (tzstring) |
1234 | { | |
1235 | /* Temporarily set TZ to a value that loads a tz file | |
1236 | and that differs from tzstring. */ | |
1237 | char *tz = *newenv; | |
1155c453 RS |
1238 | *newenv = (strcmp (tzstring, set_time_zone_rule_tz1 + 3) == 0 |
1239 | ? set_time_zone_rule_tz2 : set_time_zone_rule_tz1); | |
aafe5147 RS |
1240 | tzset (); |
1241 | *newenv = tz; | |
1242 | } | |
1243 | else | |
1244 | { | |
1245 | /* The implied tzstring is unknown, so temporarily set TZ to | |
1246 | two different values that each load a tz file. */ | |
1155c453 | 1247 | *to = set_time_zone_rule_tz1; |
aafe5147 RS |
1248 | to[1] = 0; |
1249 | tzset (); | |
1155c453 | 1250 | *to = set_time_zone_rule_tz2; |
aafe5147 RS |
1251 | tzset (); |
1252 | *to = 0; | |
1253 | } | |
1254 | ||
1255 | /* Now TZ has the desired value, and tzset can be invoked safely. */ | |
1256 | } | |
1257 | ||
143cb9a9 RS |
1258 | tzset (); |
1259 | #endif | |
143cb9a9 | 1260 | } |
35692fe0 | 1261 | \f |
fb8106e8 KH |
1262 | /* Insert NARGS Lisp objects in the array ARGS by calling INSERT_FUNC |
1263 | (if a type of object is Lisp_Int) or INSERT_FROM_STRING_FUNC (if a | |
1264 | type of object is Lisp_String). INHERIT is passed to | |
1265 | INSERT_FROM_STRING_FUNC as the last argument. */ | |
1266 | ||
d11ba98c | 1267 | void |
fb8106e8 KH |
1268 | general_insert_function (insert_func, insert_from_string_func, |
1269 | inherit, nargs, args) | |
d11ba98c | 1270 | void (*insert_func) P_ ((unsigned char *, int)); |
1f24f4fd | 1271 | void (*insert_from_string_func) P_ ((Lisp_Object, int, int, int, int, int)); |
fb8106e8 KH |
1272 | int inherit, nargs; |
1273 | register Lisp_Object *args; | |
1274 | { | |
1275 | register int argnum; | |
1276 | register Lisp_Object val; | |
1277 | ||
1278 | for (argnum = 0; argnum < nargs; argnum++) | |
1279 | { | |
1280 | val = args[argnum]; | |
1281 | retry: | |
1282 | if (INTEGERP (val)) | |
1283 | { | |
d11ba98c | 1284 | unsigned char workbuf[4], *str; |
fb8106e8 KH |
1285 | int len; |
1286 | ||
1287 | if (!NILP (current_buffer->enable_multibyte_characters)) | |
1288 | len = CHAR_STRING (XFASTINT (val), workbuf, str); | |
1289 | else | |
1290 | workbuf[0] = XINT (val), str = workbuf, len = 1; | |
1291 | (*insert_func) (str, len); | |
1292 | } | |
1293 | else if (STRINGP (val)) | |
1294 | { | |
1f24f4fd RS |
1295 | (*insert_from_string_func) (val, 0, 0, |
1296 | XSTRING (val)->size, | |
dc3620af | 1297 | STRING_BYTES (XSTRING (val)), |
1f24f4fd | 1298 | inherit); |
fb8106e8 KH |
1299 | } |
1300 | else | |
1301 | { | |
1302 | val = wrong_type_argument (Qchar_or_string_p, val); | |
1303 | goto retry; | |
1304 | } | |
1305 | } | |
1306 | } | |
1307 | ||
35692fe0 JB |
1308 | void |
1309 | insert1 (arg) | |
1310 | Lisp_Object arg; | |
1311 | { | |
1312 | Finsert (1, &arg); | |
1313 | } | |
1314 | ||
52b14ac0 JB |
1315 | |
1316 | /* Callers passing one argument to Finsert need not gcpro the | |
1317 | argument "array", since the only element of the array will | |
1318 | not be used after calling insert or insert_from_string, so | |
1319 | we don't care if it gets trashed. */ | |
1320 | ||
35692fe0 JB |
1321 | DEFUN ("insert", Finsert, Sinsert, 0, MANY, 0, |
1322 | "Insert the arguments, either strings or characters, at point.\n\ | |
fb8106e8 KH |
1323 | Point and before-insertion-markers move forward so that it ends up\n\ |
1324 | after the inserted text.\n\ | |
35692fe0 JB |
1325 | Any other markers at the point of insertion remain before the text.") |
1326 | (nargs, args) | |
1327 | int nargs; | |
1328 | register Lisp_Object *args; | |
1329 | { | |
fb8106e8 | 1330 | general_insert_function (insert, insert_from_string, 0, nargs, args); |
be91036a RS |
1331 | return Qnil; |
1332 | } | |
1333 | ||
1334 | DEFUN ("insert-and-inherit", Finsert_and_inherit, Sinsert_and_inherit, | |
1335 | 0, MANY, 0, | |
1336 | "Insert the arguments at point, inheriting properties from adjoining text.\n\ | |
fb8106e8 KH |
1337 | Point and before-insertion-markers move forward so that it ends up\n\ |
1338 | after the inserted text.\n\ | |
be91036a RS |
1339 | Any other markers at the point of insertion remain before the text.") |
1340 | (nargs, args) | |
1341 | int nargs; | |
1342 | register Lisp_Object *args; | |
1343 | { | |
fb8106e8 KH |
1344 | general_insert_function (insert_and_inherit, insert_from_string, 1, |
1345 | nargs, args); | |
35692fe0 JB |
1346 | return Qnil; |
1347 | } | |
1348 | ||
1349 | DEFUN ("insert-before-markers", Finsert_before_markers, Sinsert_before_markers, 0, MANY, 0, | |
1350 | "Insert strings or characters at point, relocating markers after the text.\n\ | |
fb8106e8 KH |
1351 | Point and before-insertion-markers move forward so that it ends up\n\ |
1352 | after the inserted text.\n\ | |
35692fe0 JB |
1353 | Any other markers at the point of insertion also end up after the text.") |
1354 | (nargs, args) | |
1355 | int nargs; | |
1356 | register Lisp_Object *args; | |
1357 | { | |
fb8106e8 KH |
1358 | general_insert_function (insert_before_markers, |
1359 | insert_from_string_before_markers, 0, | |
1360 | nargs, args); | |
be91036a RS |
1361 | return Qnil; |
1362 | } | |
1363 | ||
a0d76c27 EN |
1364 | DEFUN ("insert-before-markers-and-inherit", Finsert_and_inherit_before_markers, |
1365 | Sinsert_and_inherit_before_markers, 0, MANY, 0, | |
be91036a RS |
1366 | "Insert text at point, relocating markers and inheriting properties.\n\ |
1367 | Point moves forward so that it ends up after the inserted text.\n\ | |
1368 | Any other markers at the point of insertion also end up after the text.") | |
1369 | (nargs, args) | |
1370 | int nargs; | |
1371 | register Lisp_Object *args; | |
1372 | { | |
fb8106e8 KH |
1373 | general_insert_function (insert_before_markers_and_inherit, |
1374 | insert_from_string_before_markers, 1, | |
1375 | nargs, args); | |
35692fe0 JB |
1376 | return Qnil; |
1377 | } | |
1378 | \f | |
e2eeabbb | 1379 | DEFUN ("insert-char", Finsert_char, Sinsert_char, 2, 3, 0, |
2591ec64 | 1380 | "Insert COUNT (second arg) copies of CHARACTER (first arg).\n\ |
fb8106e8 | 1381 | Point and before-insertion-markers are affected as in the function `insert'.\n\ |
e2eeabbb RS |
1382 | Both arguments are required.\n\ |
1383 | The optional third arg INHERIT, if non-nil, says to inherit text properties\n\ | |
1384 | from adjoining text, if those properties are sticky.") | |
2591ec64 EN |
1385 | (character, count, inherit) |
1386 | Lisp_Object character, count, inherit; | |
35692fe0 JB |
1387 | { |
1388 | register unsigned char *string; | |
1389 | register int strlen; | |
1390 | register int i, n; | |
fb8106e8 KH |
1391 | int len; |
1392 | unsigned char workbuf[4], *str; | |
35692fe0 | 1393 | |
2591ec64 | 1394 | CHECK_NUMBER (character, 0); |
35692fe0 JB |
1395 | CHECK_NUMBER (count, 1); |
1396 | ||
fb8106e8 KH |
1397 | if (!NILP (current_buffer->enable_multibyte_characters)) |
1398 | len = CHAR_STRING (XFASTINT (character), workbuf, str); | |
1399 | else | |
1400 | workbuf[0] = XFASTINT (character), str = workbuf, len = 1; | |
1401 | n = XINT (count) * len; | |
35692fe0 JB |
1402 | if (n <= 0) |
1403 | return Qnil; | |
fb8106e8 | 1404 | strlen = min (n, 256 * len); |
35692fe0 JB |
1405 | string = (unsigned char *) alloca (strlen); |
1406 | for (i = 0; i < strlen; i++) | |
fb8106e8 | 1407 | string[i] = str[i % len]; |
35692fe0 JB |
1408 | while (n >= strlen) |
1409 | { | |
54e42e2d | 1410 | QUIT; |
e2eeabbb RS |
1411 | if (!NILP (inherit)) |
1412 | insert_and_inherit (string, strlen); | |
1413 | else | |
1414 | insert (string, strlen); | |
35692fe0 JB |
1415 | n -= strlen; |
1416 | } | |
1417 | if (n > 0) | |
83951f1e KH |
1418 | { |
1419 | if (!NILP (inherit)) | |
1420 | insert_and_inherit (string, n); | |
1421 | else | |
1422 | insert (string, n); | |
1423 | } | |
35692fe0 JB |
1424 | return Qnil; |
1425 | } | |
1426 | ||
1427 | \f | |
ffd56f97 JB |
1428 | /* Making strings from buffer contents. */ |
1429 | ||
1430 | /* Return a Lisp_String containing the text of the current buffer from | |
74d6d8c5 | 1431 | START to END. If text properties are in use and the current buffer |
eb8c3be9 | 1432 | has properties in the range specified, the resulting string will also |
260e2e2a | 1433 | have them, if PROPS is nonzero. |
ffd56f97 JB |
1434 | |
1435 | We don't want to use plain old make_string here, because it calls | |
1436 | make_uninit_string, which can cause the buffer arena to be | |
1437 | compacted. make_string has no way of knowing that the data has | |
1438 | been moved, and thus copies the wrong data into the string. This | |
1439 | doesn't effect most of the other users of make_string, so it should | |
1440 | be left as is. But we should use this function when conjuring | |
1441 | buffer substrings. */ | |
74d6d8c5 | 1442 | |
ffd56f97 | 1443 | Lisp_Object |
260e2e2a | 1444 | make_buffer_string (start, end, props) |
ffd56f97 | 1445 | int start, end; |
260e2e2a | 1446 | int props; |
ffd56f97 | 1447 | { |
ec1c14f6 RS |
1448 | int start_byte = CHAR_TO_BYTE (start); |
1449 | int end_byte = CHAR_TO_BYTE (end); | |
ffd56f97 | 1450 | |
88441c8e RS |
1451 | return make_buffer_string_both (start, start_byte, end, end_byte, props); |
1452 | } | |
1453 | ||
1454 | /* Return a Lisp_String containing the text of the current buffer from | |
1455 | START / START_BYTE to END / END_BYTE. | |
1456 | ||
1457 | If text properties are in use and the current buffer | |
1458 | has properties in the range specified, the resulting string will also | |
1459 | have them, if PROPS is nonzero. | |
1460 | ||
1461 | We don't want to use plain old make_string here, because it calls | |
1462 | make_uninit_string, which can cause the buffer arena to be | |
1463 | compacted. make_string has no way of knowing that the data has | |
1464 | been moved, and thus copies the wrong data into the string. This | |
1465 | doesn't effect most of the other users of make_string, so it should | |
1466 | be left as is. But we should use this function when conjuring | |
1467 | buffer substrings. */ | |
1468 | ||
1469 | Lisp_Object | |
1470 | make_buffer_string_both (start, start_byte, end, end_byte, props) | |
1471 | int start, start_byte, end, end_byte; | |
1472 | int props; | |
1473 | { | |
1474 | Lisp_Object result, tem, tem1; | |
1475 | ||
ffd56f97 JB |
1476 | if (start < GPT && GPT < end) |
1477 | move_gap (start); | |
1478 | ||
5f75e666 RS |
1479 | if (! NILP (current_buffer->enable_multibyte_characters)) |
1480 | result = make_uninit_multibyte_string (end - start, end_byte - start_byte); | |
1481 | else | |
1482 | result = make_uninit_string (end - start); | |
ec1c14f6 RS |
1483 | bcopy (BYTE_POS_ADDR (start_byte), XSTRING (result)->data, |
1484 | end_byte - start_byte); | |
ffd56f97 | 1485 | |
260e2e2a | 1486 | /* If desired, update and copy the text properties. */ |
60b96ee7 | 1487 | #ifdef USE_TEXT_PROPERTIES |
260e2e2a KH |
1488 | if (props) |
1489 | { | |
1490 | update_buffer_properties (start, end); | |
1491 | ||
1492 | tem = Fnext_property_change (make_number (start), Qnil, make_number (end)); | |
1493 | tem1 = Ftext_properties_at (make_number (start), Qnil); | |
1494 | ||
1495 | if (XINT (tem) != end || !NILP (tem1)) | |
ec1c14f6 RS |
1496 | copy_intervals_to_string (result, current_buffer, start, |
1497 | end - start); | |
260e2e2a | 1498 | } |
60b96ee7 | 1499 | #endif |
74d6d8c5 | 1500 | |
ffd56f97 JB |
1501 | return result; |
1502 | } | |
35692fe0 | 1503 | |
260e2e2a KH |
1504 | /* Call Vbuffer_access_fontify_functions for the range START ... END |
1505 | in the current buffer, if necessary. */ | |
1506 | ||
1507 | static void | |
1508 | update_buffer_properties (start, end) | |
1509 | int start, end; | |
1510 | { | |
1511 | #ifdef USE_TEXT_PROPERTIES | |
1512 | /* If this buffer has some access functions, | |
1513 | call them, specifying the range of the buffer being accessed. */ | |
1514 | if (!NILP (Vbuffer_access_fontify_functions)) | |
1515 | { | |
1516 | Lisp_Object args[3]; | |
1517 | Lisp_Object tem; | |
1518 | ||
1519 | args[0] = Qbuffer_access_fontify_functions; | |
1520 | XSETINT (args[1], start); | |
1521 | XSETINT (args[2], end); | |
1522 | ||
1523 | /* But don't call them if we can tell that the work | |
1524 | has already been done. */ | |
1525 | if (!NILP (Vbuffer_access_fontified_property)) | |
1526 | { | |
1527 | tem = Ftext_property_any (args[1], args[2], | |
1528 | Vbuffer_access_fontified_property, | |
1529 | Qnil, Qnil); | |
1530 | if (! NILP (tem)) | |
ced1d19a | 1531 | Frun_hook_with_args (3, args); |
260e2e2a KH |
1532 | } |
1533 | else | |
ced1d19a | 1534 | Frun_hook_with_args (3, args); |
260e2e2a KH |
1535 | } |
1536 | #endif | |
1537 | } | |
1538 | ||
35692fe0 JB |
1539 | DEFUN ("buffer-substring", Fbuffer_substring, Sbuffer_substring, 2, 2, 0, |
1540 | "Return the contents of part of the current buffer as a string.\n\ | |
1541 | The two arguments START and END are character positions;\n\ | |
1542 | they can be in either order.") | |
2591ec64 EN |
1543 | (start, end) |
1544 | Lisp_Object start, end; | |
35692fe0 | 1545 | { |
2591ec64 | 1546 | register int b, e; |
35692fe0 | 1547 | |
2591ec64 EN |
1548 | validate_region (&start, &end); |
1549 | b = XINT (start); | |
1550 | e = XINT (end); | |
35692fe0 | 1551 | |
2591ec64 | 1552 | return make_buffer_string (b, e, 1); |
260e2e2a KH |
1553 | } |
1554 | ||
1555 | DEFUN ("buffer-substring-no-properties", Fbuffer_substring_no_properties, | |
1556 | Sbuffer_substring_no_properties, 2, 2, 0, | |
1557 | "Return the characters of part of the buffer, without the text properties.\n\ | |
1558 | The two arguments START and END are character positions;\n\ | |
1559 | they can be in either order.") | |
2591ec64 EN |
1560 | (start, end) |
1561 | Lisp_Object start, end; | |
260e2e2a | 1562 | { |
2591ec64 | 1563 | register int b, e; |
260e2e2a | 1564 | |
2591ec64 EN |
1565 | validate_region (&start, &end); |
1566 | b = XINT (start); | |
1567 | e = XINT (end); | |
260e2e2a | 1568 | |
2591ec64 | 1569 | return make_buffer_string (b, e, 0); |
35692fe0 JB |
1570 | } |
1571 | ||
1572 | DEFUN ("buffer-string", Fbuffer_string, Sbuffer_string, 0, 0, 0, | |
af7bd86c KH |
1573 | "Return the contents of the current buffer as a string.\n\ |
1574 | If narrowing is in effect, this function returns only the visible part\n\ | |
1575 | of the buffer.") | |
35692fe0 JB |
1576 | () |
1577 | { | |
260e2e2a | 1578 | return make_buffer_string (BEGV, ZV, 1); |
35692fe0 JB |
1579 | } |
1580 | ||
1581 | DEFUN ("insert-buffer-substring", Finsert_buffer_substring, Sinsert_buffer_substring, | |
1582 | 1, 3, 0, | |
83ea6fc2 | 1583 | "Insert before point a substring of the contents of buffer BUFFER.\n\ |
35692fe0 JB |
1584 | BUFFER may be a buffer or a buffer name.\n\ |
1585 | Arguments START and END are character numbers specifying the substring.\n\ | |
1586 | They default to the beginning and the end of BUFFER.") | |
2591ec64 EN |
1587 | (buf, start, end) |
1588 | Lisp_Object buf, start, end; | |
35692fe0 | 1589 | { |
2591ec64 | 1590 | register int b, e, temp; |
260e2e2a | 1591 | register struct buffer *bp, *obuf; |
3fff2dfa | 1592 | Lisp_Object buffer; |
35692fe0 | 1593 | |
3fff2dfa RS |
1594 | buffer = Fget_buffer (buf); |
1595 | if (NILP (buffer)) | |
1596 | nsberror (buf); | |
1597 | bp = XBUFFER (buffer); | |
93b62e82 KH |
1598 | if (NILP (bp->name)) |
1599 | error ("Selecting deleted buffer"); | |
35692fe0 | 1600 | |
2591ec64 EN |
1601 | if (NILP (start)) |
1602 | b = BUF_BEGV (bp); | |
35692fe0 JB |
1603 | else |
1604 | { | |
2591ec64 EN |
1605 | CHECK_NUMBER_COERCE_MARKER (start, 0); |
1606 | b = XINT (start); | |
35692fe0 | 1607 | } |
2591ec64 EN |
1608 | if (NILP (end)) |
1609 | e = BUF_ZV (bp); | |
35692fe0 JB |
1610 | else |
1611 | { | |
2591ec64 EN |
1612 | CHECK_NUMBER_COERCE_MARKER (end, 1); |
1613 | e = XINT (end); | |
35692fe0 JB |
1614 | } |
1615 | ||
2591ec64 EN |
1616 | if (b > e) |
1617 | temp = b, b = e, e = temp; | |
35692fe0 | 1618 | |
2591ec64 EN |
1619 | if (!(BUF_BEGV (bp) <= b && e <= BUF_ZV (bp))) |
1620 | args_out_of_range (start, end); | |
35692fe0 | 1621 | |
260e2e2a KH |
1622 | obuf = current_buffer; |
1623 | set_buffer_internal_1 (bp); | |
2591ec64 | 1624 | update_buffer_properties (b, e); |
260e2e2a KH |
1625 | set_buffer_internal_1 (obuf); |
1626 | ||
2591ec64 | 1627 | insert_from_buffer (bp, b, e - b, 0); |
35692fe0 JB |
1628 | return Qnil; |
1629 | } | |
e9cf2084 RS |
1630 | |
1631 | DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings, Scompare_buffer_substrings, | |
1632 | 6, 6, 0, | |
1633 | "Compare two substrings of two buffers; return result as number.\n\ | |
1634 | the value is -N if first string is less after N-1 chars,\n\ | |
1635 | +N if first string is greater after N-1 chars, or 0 if strings match.\n\ | |
1636 | Each substring is represented as three arguments: BUFFER, START and END.\n\ | |
1637 | That makes six args in all, three for each substring.\n\n\ | |
1638 | The value of `case-fold-search' in the current buffer\n\ | |
1639 | determines whether case is significant or ignored.") | |
1640 | (buffer1, start1, end1, buffer2, start2, end2) | |
1641 | Lisp_Object buffer1, start1, end1, buffer2, start2, end2; | |
1642 | { | |
1643 | register int begp1, endp1, begp2, endp2, temp, len1, len2, length, i; | |
1644 | register struct buffer *bp1, *bp2; | |
2a8b0ff0 | 1645 | register Lisp_Object *trt |
e9cf2084 | 1646 | = (!NILP (current_buffer->case_fold_search) |
2a8b0ff0 | 1647 | ? XCHAR_TABLE (current_buffer->case_canon_table)->contents : 0); |
ec1c14f6 RS |
1648 | int chars = 0; |
1649 | int beg1_byte, beg2_byte; | |
e9cf2084 RS |
1650 | |
1651 | /* Find the first buffer and its substring. */ | |
1652 | ||
1653 | if (NILP (buffer1)) | |
1654 | bp1 = current_buffer; | |
1655 | else | |
1656 | { | |
3fff2dfa RS |
1657 | Lisp_Object buf1; |
1658 | buf1 = Fget_buffer (buffer1); | |
1659 | if (NILP (buf1)) | |
1660 | nsberror (buffer1); | |
1661 | bp1 = XBUFFER (buf1); | |
93b62e82 KH |
1662 | if (NILP (bp1->name)) |
1663 | error ("Selecting deleted buffer"); | |
e9cf2084 RS |
1664 | } |
1665 | ||
1666 | if (NILP (start1)) | |
1667 | begp1 = BUF_BEGV (bp1); | |
1668 | else | |
1669 | { | |
1670 | CHECK_NUMBER_COERCE_MARKER (start1, 1); | |
1671 | begp1 = XINT (start1); | |
1672 | } | |
1673 | if (NILP (end1)) | |
1674 | endp1 = BUF_ZV (bp1); | |
1675 | else | |
1676 | { | |
1677 | CHECK_NUMBER_COERCE_MARKER (end1, 2); | |
1678 | endp1 = XINT (end1); | |
1679 | } | |
1680 | ||
1681 | if (begp1 > endp1) | |
1682 | temp = begp1, begp1 = endp1, endp1 = temp; | |
1683 | ||
1684 | if (!(BUF_BEGV (bp1) <= begp1 | |
1685 | && begp1 <= endp1 | |
1686 | && endp1 <= BUF_ZV (bp1))) | |
1687 | args_out_of_range (start1, end1); | |
1688 | ||
1689 | /* Likewise for second substring. */ | |
1690 | ||
1691 | if (NILP (buffer2)) | |
1692 | bp2 = current_buffer; | |
1693 | else | |
1694 | { | |
3fff2dfa RS |
1695 | Lisp_Object buf2; |
1696 | buf2 = Fget_buffer (buffer2); | |
1697 | if (NILP (buf2)) | |
1698 | nsberror (buffer2); | |
3b1fdd85 | 1699 | bp2 = XBUFFER (buf2); |
93b62e82 KH |
1700 | if (NILP (bp2->name)) |
1701 | error ("Selecting deleted buffer"); | |
e9cf2084 RS |
1702 | } |
1703 | ||
1704 | if (NILP (start2)) | |
1705 | begp2 = BUF_BEGV (bp2); | |
1706 | else | |
1707 | { | |
1708 | CHECK_NUMBER_COERCE_MARKER (start2, 4); | |
1709 | begp2 = XINT (start2); | |
1710 | } | |
1711 | if (NILP (end2)) | |
1712 | endp2 = BUF_ZV (bp2); | |
1713 | else | |
1714 | { | |
1715 | CHECK_NUMBER_COERCE_MARKER (end2, 5); | |
1716 | endp2 = XINT (end2); | |
1717 | } | |
1718 | ||
1719 | if (begp2 > endp2) | |
1720 | temp = begp2, begp2 = endp2, endp2 = temp; | |
1721 | ||
1722 | if (!(BUF_BEGV (bp2) <= begp2 | |
1723 | && begp2 <= endp2 | |
1724 | && endp2 <= BUF_ZV (bp2))) | |
1725 | args_out_of_range (start2, end2); | |
1726 | ||
ec1c14f6 RS |
1727 | beg1_byte = buf_charpos_to_bytepos (bp1, begp1); |
1728 | beg2_byte = buf_charpos_to_bytepos (bp2, begp2); | |
1729 | len1 = buf_charpos_to_bytepos (bp1, endp1) - begp1; | |
1730 | len2 = buf_charpos_to_bytepos (bp2, endp2) - begp2; | |
e9cf2084 RS |
1731 | length = len1; |
1732 | if (len2 < length) | |
1733 | length = len2; | |
1734 | ||
1735 | for (i = 0; i < length; i++) | |
1736 | { | |
ec1c14f6 RS |
1737 | unsigned char *p1 = BUF_BYTE_ADDRESS (bp1, beg1_byte + i); |
1738 | int c1 = *p1; | |
1739 | int c2 = *BUF_BYTE_ADDRESS (bp2, beg2_byte + i); | |
1740 | ||
1741 | /* If a character begins here, | |
1742 | count the previous character now. */ | |
1743 | if (i > 0 | |
1744 | && (NILP (current_buffer->enable_multibyte_characters) | |
1745 | || CHAR_HEAD_P (*p1))) | |
1746 | chars++; | |
1747 | ||
e9cf2084 RS |
1748 | if (trt) |
1749 | { | |
1b10fb77 RS |
1750 | c1 = XINT (trt[c1]); |
1751 | c2 = XINT (trt[c2]); | |
e9cf2084 RS |
1752 | } |
1753 | if (c1 < c2) | |
ec1c14f6 | 1754 | return make_number (- 1 - chars); |
e9cf2084 | 1755 | if (c1 > c2) |
ec1c14f6 | 1756 | return make_number (chars + 1); |
e9cf2084 RS |
1757 | } |
1758 | ||
1759 | /* The strings match as far as they go. | |
1760 | If one is shorter, that one is less. */ | |
1761 | if (length < len1) | |
ec1c14f6 | 1762 | return make_number (chars + 1); |
e9cf2084 | 1763 | else if (length < len2) |
ec1c14f6 | 1764 | return make_number (- chars - 1); |
e9cf2084 RS |
1765 | |
1766 | /* Same length too => they are equal. */ | |
1767 | return make_number (0); | |
1768 | } | |
35692fe0 | 1769 | \f |
d5a539cd RS |
1770 | static Lisp_Object |
1771 | subst_char_in_region_unwind (arg) | |
1772 | Lisp_Object arg; | |
1773 | { | |
1774 | return current_buffer->undo_list = arg; | |
1775 | } | |
1776 | ||
c8e76b47 RS |
1777 | static Lisp_Object |
1778 | subst_char_in_region_unwind_1 (arg) | |
1779 | Lisp_Object arg; | |
1780 | { | |
1781 | return current_buffer->filename = arg; | |
1782 | } | |
1783 | ||
35692fe0 JB |
1784 | DEFUN ("subst-char-in-region", Fsubst_char_in_region, |
1785 | Ssubst_char_in_region, 4, 5, 0, | |
1786 | "From START to END, replace FROMCHAR with TOCHAR each time it occurs.\n\ | |
1787 | If optional arg NOUNDO is non-nil, don't record this change for undo\n\ | |
fb8106e8 KH |
1788 | and don't mark the buffer as really changed.\n\ |
1789 | Both characters must have the same length of multi-byte form.") | |
35692fe0 JB |
1790 | (start, end, fromchar, tochar, noundo) |
1791 | Lisp_Object start, end, fromchar, tochar, noundo; | |
1792 | { | |
84246b95 | 1793 | register int pos, pos_byte, stop, i, len, end_byte; |
60b96ee7 | 1794 | int changed = 0; |
fb8106e8 | 1795 | unsigned char fromwork[4], *fromstr, towork[4], *tostr, *p; |
d5a539cd | 1796 | int count = specpdl_ptr - specpdl; |
35692fe0 JB |
1797 | |
1798 | validate_region (&start, &end); | |
1799 | CHECK_NUMBER (fromchar, 2); | |
1800 | CHECK_NUMBER (tochar, 3); | |
1801 | ||
fb8106e8 KH |
1802 | if (! NILP (current_buffer->enable_multibyte_characters)) |
1803 | { | |
1804 | len = CHAR_STRING (XFASTINT (fromchar), fromwork, fromstr); | |
1805 | if (CHAR_STRING (XFASTINT (tochar), towork, tostr) != len) | |
1806 | error ("Characters in subst-char-in-region have different byte-lengths"); | |
1807 | } | |
1808 | else | |
1809 | { | |
1810 | len = 1; | |
1811 | fromwork[0] = XFASTINT (fromchar), fromstr = fromwork; | |
1812 | towork[0] = XFASTINT (tochar), tostr = towork; | |
1813 | } | |
1814 | ||
84246b95 KH |
1815 | pos = XINT (start); |
1816 | pos_byte = CHAR_TO_BYTE (pos); | |
ec1c14f6 RS |
1817 | stop = CHAR_TO_BYTE (XINT (end)); |
1818 | end_byte = stop; | |
35692fe0 | 1819 | |
d5a539cd RS |
1820 | /* If we don't want undo, turn off putting stuff on the list. |
1821 | That's faster than getting rid of things, | |
c8e76b47 RS |
1822 | and it prevents even the entry for a first change. |
1823 | Also inhibit locking the file. */ | |
d5a539cd RS |
1824 | if (!NILP (noundo)) |
1825 | { | |
1826 | record_unwind_protect (subst_char_in_region_unwind, | |
1827 | current_buffer->undo_list); | |
1828 | current_buffer->undo_list = Qt; | |
c8e76b47 RS |
1829 | /* Don't do file-locking. */ |
1830 | record_unwind_protect (subst_char_in_region_unwind_1, | |
1831 | current_buffer->filename); | |
1832 | current_buffer->filename = Qnil; | |
d5a539cd RS |
1833 | } |
1834 | ||
84246b95 | 1835 | if (pos_byte < GPT_BYTE) |
ec1c14f6 | 1836 | stop = min (stop, GPT_BYTE); |
fb8106e8 | 1837 | while (1) |
35692fe0 | 1838 | { |
84246b95 | 1839 | if (pos_byte >= stop) |
fb8106e8 | 1840 | { |
84246b95 | 1841 | if (pos_byte >= end_byte) break; |
ec1c14f6 | 1842 | stop = end_byte; |
fb8106e8 | 1843 | } |
84246b95 | 1844 | p = BYTE_POS_ADDR (pos_byte); |
fb8106e8 KH |
1845 | if (p[0] == fromstr[0] |
1846 | && (len == 1 | |
1847 | || (p[1] == fromstr[1] | |
1848 | && (len == 2 || (p[2] == fromstr[2] | |
1849 | && (len == 3 || p[3] == fromstr[3])))))) | |
35692fe0 | 1850 | { |
60b96ee7 RS |
1851 | if (! changed) |
1852 | { | |
fb8106e8 | 1853 | modify_region (current_buffer, XINT (start), XINT (end)); |
7653d030 RS |
1854 | |
1855 | if (! NILP (noundo)) | |
1856 | { | |
1e158d25 RS |
1857 | if (MODIFF - 1 == SAVE_MODIFF) |
1858 | SAVE_MODIFF++; | |
7653d030 RS |
1859 | if (MODIFF - 1 == current_buffer->auto_save_modified) |
1860 | current_buffer->auto_save_modified++; | |
1861 | } | |
1862 | ||
fb8106e8 | 1863 | changed = 1; |
60b96ee7 RS |
1864 | } |
1865 | ||
56a98455 | 1866 | if (NILP (noundo)) |
84246b95 | 1867 | record_change (pos, 1); |
fb8106e8 | 1868 | for (i = 0; i < len; i++) *p++ = tostr[i]; |
35692fe0 | 1869 | } |
84246b95 | 1870 | INC_BOTH (pos, pos_byte); |
35692fe0 JB |
1871 | } |
1872 | ||
60b96ee7 RS |
1873 | if (changed) |
1874 | signal_after_change (XINT (start), | |
84246b95 | 1875 | XINT (end) - XINT (start), XINT (end) - XINT (start)); |
60b96ee7 | 1876 | |
d5a539cd | 1877 | unbind_to (count, Qnil); |
35692fe0 JB |
1878 | return Qnil; |
1879 | } | |
1880 | ||
1881 | DEFUN ("translate-region", Ftranslate_region, Stranslate_region, 3, 3, 0, | |
1882 | "From START to END, translate characters according to TABLE.\n\ | |
1883 | TABLE is a string; the Nth character in it is the mapping\n\ | |
1f24f4fd RS |
1884 | for the character with code N.\n\ |
1885 | This function does not alter multibyte characters.\n\ | |
1886 | It returns the number of characters changed.") | |
35692fe0 JB |
1887 | (start, end, table) |
1888 | Lisp_Object start; | |
1889 | Lisp_Object end; | |
1890 | register Lisp_Object table; | |
1891 | { | |
ec1c14f6 | 1892 | register int pos_byte, stop; /* Limits of the region. */ |
35692fe0 | 1893 | register unsigned char *tt; /* Trans table. */ |
35692fe0 JB |
1894 | register int nc; /* New character. */ |
1895 | int cnt; /* Number of changes made. */ | |
35692fe0 | 1896 | int size; /* Size of translate table. */ |
1f24f4fd | 1897 | int pos; |
35692fe0 JB |
1898 | |
1899 | validate_region (&start, &end); | |
1900 | CHECK_STRING (table, 2); | |
1901 | ||
dc3620af | 1902 | size = STRING_BYTES (XSTRING (table)); |
35692fe0 JB |
1903 | tt = XSTRING (table)->data; |
1904 | ||
ec1c14f6 RS |
1905 | pos_byte = CHAR_TO_BYTE (XINT (start)); |
1906 | stop = CHAR_TO_BYTE (XINT (end)); | |
1907 | modify_region (current_buffer, XINT (start), XINT (end)); | |
1f24f4fd | 1908 | pos = XINT (start); |
35692fe0 JB |
1909 | |
1910 | cnt = 0; | |
1f24f4fd | 1911 | for (; pos_byte < stop; ) |
35692fe0 | 1912 | { |
ec1c14f6 | 1913 | register unsigned char *p = BYTE_POS_ADDR (pos_byte); |
1f24f4fd RS |
1914 | int len; |
1915 | int oc; | |
ec1c14f6 | 1916 | |
1f24f4fd RS |
1917 | oc = STRING_CHAR_AND_LENGTH (p, stop - pos_byte, len); |
1918 | if (oc < size && len == 1) | |
35692fe0 JB |
1919 | { |
1920 | nc = tt[oc]; | |
1921 | if (nc != oc) | |
1922 | { | |
1f24f4fd | 1923 | record_change (pos, 1); |
ec1c14f6 | 1924 | *p = nc; |
1f24f4fd | 1925 | signal_after_change (pos, 1, 1); |
35692fe0 JB |
1926 | ++cnt; |
1927 | } | |
1928 | } | |
1f24f4fd RS |
1929 | pos_byte += len; |
1930 | pos++; | |
35692fe0 JB |
1931 | } |
1932 | ||
ec1c14f6 | 1933 | return make_number (cnt); |
35692fe0 JB |
1934 | } |
1935 | ||
1936 | DEFUN ("delete-region", Fdelete_region, Sdelete_region, 2, 2, "r", | |
1937 | "Delete the text between point and mark.\n\ | |
1938 | When called from a program, expects two arguments,\n\ | |
1939 | positions (integers or markers) specifying the stretch to be deleted.") | |
2591ec64 EN |
1940 | (start, end) |
1941 | Lisp_Object start, end; | |
35692fe0 | 1942 | { |
2591ec64 EN |
1943 | validate_region (&start, &end); |
1944 | del_range (XINT (start), XINT (end)); | |
35692fe0 JB |
1945 | return Qnil; |
1946 | } | |
1947 | \f | |
1948 | DEFUN ("widen", Fwiden, Swiden, 0, 0, "", | |
1949 | "Remove restrictions (narrowing) from current buffer.\n\ | |
1950 | This allows the buffer's full text to be seen and edited.") | |
1951 | () | |
1952 | { | |
2cad2e34 RS |
1953 | if (BEG != BEGV || Z != ZV) |
1954 | current_buffer->clip_changed = 1; | |
35692fe0 | 1955 | BEGV = BEG; |
ec1c14f6 RS |
1956 | BEGV_BYTE = BEG_BYTE; |
1957 | SET_BUF_ZV_BOTH (current_buffer, Z, Z_BYTE); | |
52b14ac0 JB |
1958 | /* Changing the buffer bounds invalidates any recorded current column. */ |
1959 | invalidate_current_column (); | |
35692fe0 JB |
1960 | return Qnil; |
1961 | } | |
1962 | ||
1963 | DEFUN ("narrow-to-region", Fnarrow_to_region, Snarrow_to_region, 2, 2, "r", | |
1964 | "Restrict editing in this buffer to the current region.\n\ | |
1965 | The rest of the text becomes temporarily invisible and untouchable\n\ | |
1966 | but is not deleted; if you save the buffer in a file, the invisible\n\ | |
1967 | text is included in the file. \\[widen] makes all visible again.\n\ | |
1968 | See also `save-restriction'.\n\ | |
1969 | \n\ | |
1970 | When calling from a program, pass two arguments; positions (integers\n\ | |
1971 | or markers) bounding the text that should remain visible.") | |
2591ec64 EN |
1972 | (start, end) |
1973 | register Lisp_Object start, end; | |
35692fe0 | 1974 | { |
2591ec64 EN |
1975 | CHECK_NUMBER_COERCE_MARKER (start, 0); |
1976 | CHECK_NUMBER_COERCE_MARKER (end, 1); | |
35692fe0 | 1977 | |
2591ec64 | 1978 | if (XINT (start) > XINT (end)) |
35692fe0 | 1979 | { |
b5a6948e | 1980 | Lisp_Object tem; |
2591ec64 | 1981 | tem = start; start = end; end = tem; |
35692fe0 JB |
1982 | } |
1983 | ||
2591ec64 EN |
1984 | if (!(BEG <= XINT (start) && XINT (start) <= XINT (end) && XINT (end) <= Z)) |
1985 | args_out_of_range (start, end); | |
35692fe0 | 1986 | |
2cad2e34 RS |
1987 | if (BEGV != XFASTINT (start) || ZV != XFASTINT (end)) |
1988 | current_buffer->clip_changed = 1; | |
1989 | ||
ec1c14f6 | 1990 | SET_BUF_BEGV (current_buffer, XFASTINT (start)); |
2591ec64 | 1991 | SET_BUF_ZV (current_buffer, XFASTINT (end)); |
6ec8bbd2 | 1992 | if (PT < XFASTINT (start)) |
2591ec64 | 1993 | SET_PT (XFASTINT (start)); |
6ec8bbd2 | 1994 | if (PT > XFASTINT (end)) |
2591ec64 | 1995 | SET_PT (XFASTINT (end)); |
52b14ac0 JB |
1996 | /* Changing the buffer bounds invalidates any recorded current column. */ |
1997 | invalidate_current_column (); | |
35692fe0 JB |
1998 | return Qnil; |
1999 | } | |
2000 | ||
2001 | Lisp_Object | |
2002 | save_restriction_save () | |
2003 | { | |
2004 | register Lisp_Object bottom, top; | |
2005 | /* Note: I tried using markers here, but it does not win | |
2006 | because insertion at the end of the saved region | |
2007 | does not advance mh and is considered "outside" the saved region. */ | |
55561c63 KH |
2008 | XSETFASTINT (bottom, BEGV - BEG); |
2009 | XSETFASTINT (top, Z - ZV); | |
35692fe0 JB |
2010 | |
2011 | return Fcons (Fcurrent_buffer (), Fcons (bottom, top)); | |
2012 | } | |
2013 | ||
2014 | Lisp_Object | |
2015 | save_restriction_restore (data) | |
2016 | Lisp_Object data; | |
2017 | { | |
2018 | register struct buffer *buf; | |
2019 | register int newhead, newtail; | |
2020 | register Lisp_Object tem; | |
2cad2e34 | 2021 | int obegv, ozv; |
35692fe0 JB |
2022 | |
2023 | buf = XBUFFER (XCONS (data)->car); | |
2024 | ||
2025 | data = XCONS (data)->cdr; | |
2026 | ||
2027 | tem = XCONS (data)->car; | |
2028 | newhead = XINT (tem); | |
2029 | tem = XCONS (data)->cdr; | |
2030 | newtail = XINT (tem); | |
2031 | if (newhead + newtail > BUF_Z (buf) - BUF_BEG (buf)) | |
2032 | { | |
2033 | newhead = 0; | |
2034 | newtail = 0; | |
2035 | } | |
2cad2e34 RS |
2036 | |
2037 | obegv = BUF_BEGV (buf); | |
2038 | ozv = BUF_ZV (buf); | |
2039 | ||
ec1c14f6 | 2040 | SET_BUF_BEGV (buf, BUF_BEG (buf) + newhead); |
35692fe0 | 2041 | SET_BUF_ZV (buf, BUF_Z (buf) - newtail); |
2cad2e34 RS |
2042 | |
2043 | if (obegv != BUF_BEGV (buf) || ozv != BUF_ZV (buf)) | |
2044 | current_buffer->clip_changed = 1; | |
35692fe0 JB |
2045 | |
2046 | /* If point is outside the new visible range, move it inside. */ | |
ec1c14f6 RS |
2047 | SET_BUF_PT_BOTH (buf, |
2048 | clip_to_bounds (BUF_BEGV (buf), BUF_PT (buf), BUF_ZV (buf)), | |
2049 | clip_to_bounds (BUF_BEGV_BYTE (buf), BUF_PT_BYTE (buf), | |
2050 | BUF_ZV_BYTE (buf))); | |
35692fe0 JB |
2051 | |
2052 | return Qnil; | |
2053 | } | |
2054 | ||
2055 | DEFUN ("save-restriction", Fsave_restriction, Ssave_restriction, 0, UNEVALLED, 0, | |
2056 | "Execute BODY, saving and restoring current buffer's restrictions.\n\ | |
2057 | The buffer's restrictions make parts of the beginning and end invisible.\n\ | |
2058 | \(They are set up with `narrow-to-region' and eliminated with `widen'.)\n\ | |
2059 | This special form, `save-restriction', saves the current buffer's restrictions\n\ | |
2060 | when it is entered, and restores them when it is exited.\n\ | |
2061 | So any `narrow-to-region' within BODY lasts only until the end of the form.\n\ | |
2062 | The old restrictions settings are restored\n\ | |
2063 | even in case of abnormal exit (throw or error).\n\ | |
2064 | \n\ | |
2065 | The value returned is the value of the last form in BODY.\n\ | |
2066 | \n\ | |
2067 | `save-restriction' can get confused if, within the BODY, you widen\n\ | |
2068 | and then make changes outside the area within the saved restrictions.\n\ | |
2069 | \n\ | |
2070 | Note: if you are using both `save-excursion' and `save-restriction',\n\ | |
2071 | use `save-excursion' outermost:\n\ | |
2072 | (save-excursion (save-restriction ...))") | |
2073 | (body) | |
2074 | Lisp_Object body; | |
2075 | { | |
2076 | register Lisp_Object val; | |
2077 | int count = specpdl_ptr - specpdl; | |
2078 | ||
2079 | record_unwind_protect (save_restriction_restore, save_restriction_save ()); | |
2080 | val = Fprogn (body); | |
2081 | return unbind_to (count, val); | |
2082 | } | |
2083 | \f | |
671fbc4d KH |
2084 | /* Buffer for the most recent text displayed by Fmessage. */ |
2085 | static char *message_text; | |
2086 | ||
2087 | /* Allocated length of that buffer. */ | |
2088 | static int message_length; | |
2089 | ||
35692fe0 JB |
2090 | DEFUN ("message", Fmessage, Smessage, 1, MANY, 0, |
2091 | "Print a one-line message at the bottom of the screen.\n\ | |
98fc5c3c RS |
2092 | The first argument is a format control string, and the rest are data\n\ |
2093 | to be formatted under control of the string. See `format' for details.\n\ | |
2094 | \n\ | |
ccdac5be JB |
2095 | If the first argument is nil, clear any existing message; let the\n\ |
2096 | minibuffer contents show.") | |
35692fe0 JB |
2097 | (nargs, args) |
2098 | int nargs; | |
2099 | Lisp_Object *args; | |
2100 | { | |
ccdac5be | 2101 | if (NILP (args[0])) |
f0250249 JB |
2102 | { |
2103 | message (0); | |
2104 | return Qnil; | |
2105 | } | |
ccdac5be JB |
2106 | else |
2107 | { | |
2108 | register Lisp_Object val; | |
2109 | val = Fformat (nargs, args); | |
671fbc4d KH |
2110 | /* Copy the data so that it won't move when we GC. */ |
2111 | if (! message_text) | |
2112 | { | |
2113 | message_text = (char *)xmalloc (80); | |
2114 | message_length = 80; | |
2115 | } | |
dc3620af | 2116 | if (STRING_BYTES (XSTRING (val)) > message_length) |
671fbc4d | 2117 | { |
dc3620af | 2118 | message_length = STRING_BYTES (XSTRING (val)); |
671fbc4d KH |
2119 | message_text = (char *)xrealloc (message_text, message_length); |
2120 | } | |
dc3620af RS |
2121 | bcopy (XSTRING (val)->data, message_text, STRING_BYTES (XSTRING (val))); |
2122 | message2 (message_text, STRING_BYTES (XSTRING (val)), | |
1f24f4fd | 2123 | STRING_MULTIBYTE (val)); |
ccdac5be JB |
2124 | return val; |
2125 | } | |
35692fe0 JB |
2126 | } |
2127 | ||
cacc3e2c RS |
2128 | DEFUN ("message-box", Fmessage_box, Smessage_box, 1, MANY, 0, |
2129 | "Display a message, in a dialog box if possible.\n\ | |
2130 | If a dialog box is not available, use the echo area.\n\ | |
f8250f01 RS |
2131 | The first argument is a format control string, and the rest are data\n\ |
2132 | to be formatted under control of the string. See `format' for details.\n\ | |
2133 | \n\ | |
cacc3e2c RS |
2134 | If the first argument is nil, clear any existing message; let the\n\ |
2135 | minibuffer contents show.") | |
2136 | (nargs, args) | |
2137 | int nargs; | |
2138 | Lisp_Object *args; | |
2139 | { | |
2140 | if (NILP (args[0])) | |
2141 | { | |
2142 | message (0); | |
2143 | return Qnil; | |
2144 | } | |
2145 | else | |
2146 | { | |
2147 | register Lisp_Object val; | |
2148 | val = Fformat (nargs, args); | |
f8250f01 | 2149 | #ifdef HAVE_MENUS |
cacc3e2c RS |
2150 | { |
2151 | Lisp_Object pane, menu, obj; | |
2152 | struct gcpro gcpro1; | |
2153 | pane = Fcons (Fcons (build_string ("OK"), Qt), Qnil); | |
2154 | GCPRO1 (pane); | |
2155 | menu = Fcons (val, pane); | |
2156 | obj = Fx_popup_dialog (Qt, menu); | |
2157 | UNGCPRO; | |
2158 | return val; | |
2159 | } | |
f8250f01 | 2160 | #else /* not HAVE_MENUS */ |
cacc3e2c RS |
2161 | /* Copy the data so that it won't move when we GC. */ |
2162 | if (! message_text) | |
2163 | { | |
2164 | message_text = (char *)xmalloc (80); | |
2165 | message_length = 80; | |
2166 | } | |
dc3620af | 2167 | if (STRING_BYTES (XSTRING (val)) > message_length) |
cacc3e2c | 2168 | { |
dc3620af | 2169 | message_length = STRING_BYTES (XSTRING (val)); |
cacc3e2c RS |
2170 | message_text = (char *)xrealloc (message_text, message_length); |
2171 | } | |
dc3620af | 2172 | bcopy (XSTRING (val)->data, message_text, STRING_BYTES (XSTRING (val))); |
d13a8480 RS |
2173 | message2 (message_text, STRING_BYTES (XSTRING (val)), |
2174 | STRING_MULTIBYTE (val)); | |
cacc3e2c | 2175 | return val; |
f8250f01 | 2176 | #endif /* not HAVE_MENUS */ |
cacc3e2c RS |
2177 | } |
2178 | } | |
f8250f01 | 2179 | #ifdef HAVE_MENUS |
cacc3e2c RS |
2180 | extern Lisp_Object last_nonmenu_event; |
2181 | #endif | |
f8250f01 | 2182 | |
cacc3e2c RS |
2183 | DEFUN ("message-or-box", Fmessage_or_box, Smessage_or_box, 1, MANY, 0, |
2184 | "Display a message in a dialog box or in the echo area.\n\ | |
2185 | If this command was invoked with the mouse, use a dialog box.\n\ | |
2186 | Otherwise, use the echo area.\n\ | |
f8250f01 RS |
2187 | The first argument is a format control string, and the rest are data\n\ |
2188 | to be formatted under control of the string. See `format' for details.\n\ | |
cacc3e2c | 2189 | \n\ |
cacc3e2c RS |
2190 | If the first argument is nil, clear any existing message; let the\n\ |
2191 | minibuffer contents show.") | |
2192 | (nargs, args) | |
2193 | int nargs; | |
2194 | Lisp_Object *args; | |
2195 | { | |
f8250f01 | 2196 | #ifdef HAVE_MENUS |
cacc3e2c | 2197 | if (NILP (last_nonmenu_event) || CONSP (last_nonmenu_event)) |
0a56ee6b | 2198 | return Fmessage_box (nargs, args); |
cacc3e2c RS |
2199 | #endif |
2200 | return Fmessage (nargs, args); | |
2201 | } | |
2202 | ||
b14dda8a RS |
2203 | DEFUN ("current-message", Fcurrent_message, Scurrent_message, 0, 0, 0, |
2204 | "Return the string currently displayed in the echo area, or nil if none.") | |
2205 | () | |
2206 | { | |
2207 | return (echo_area_glyphs | |
2208 | ? make_string (echo_area_glyphs, echo_area_glyphs_length) | |
2209 | : Qnil); | |
2210 | } | |
2211 | ||
1f24f4fd RS |
2212 | /* Number of bytes that STRING will occupy when put into the result. |
2213 | MULTIBYTE is nonzero if the result should be multibyte. */ | |
2214 | ||
2215 | #define CONVERTED_BYTE_SIZE(MULTIBYTE, STRING) \ | |
2216 | (((MULTIBYTE) && ! STRING_MULTIBYTE (STRING)) \ | |
8d6179dc | 2217 | ? count_size_as_multibyte (XSTRING (STRING)->data, \ |
dc3620af RS |
2218 | STRING_BYTES (XSTRING (STRING))) \ |
2219 | : STRING_BYTES (XSTRING (STRING))) | |
1f24f4fd | 2220 | |
35692fe0 JB |
2221 | DEFUN ("format", Fformat, Sformat, 1, MANY, 0, |
2222 | "Format a string out of a control-string and arguments.\n\ | |
2223 | The first argument is a control string.\n\ | |
2224 | The other arguments are substituted into it to make the result, a string.\n\ | |
2225 | It may contain %-sequences meaning to substitute the next argument.\n\ | |
2226 | %s means print a string argument. Actually, prints any object, with `princ'.\n\ | |
2227 | %d means print as number in decimal (%o octal, %x hex).\n\ | |
9db1775a RS |
2228 | %e means print a number in exponential notation.\n\ |
2229 | %f means print a number in decimal-point notation.\n\ | |
2230 | %g means print a number in exponential notation\n\ | |
2231 | or decimal-point notation, whichever uses fewer characters.\n\ | |
35692fe0 JB |
2232 | %c means print a number as a single character.\n\ |
2233 | %S means print any object as an s-expression (using prin1).\n\ | |
9db1775a | 2234 | The argument used for %d, %o, %x, %e, %f, %g or %c must be a number.\n\ |
52b14ac0 | 2235 | Use %% to put a single % into the output.") |
35692fe0 JB |
2236 | (nargs, args) |
2237 | int nargs; | |
2238 | register Lisp_Object *args; | |
2239 | { | |
2240 | register int n; /* The number of the next arg to substitute */ | |
e781c49e | 2241 | register int total; /* An estimate of the final length */ |
1f24f4fd | 2242 | char *buf, *p; |
35692fe0 | 2243 | register unsigned char *format, *end; |
1f24f4fd RS |
2244 | int length, nchars; |
2245 | /* Nonzero if the output should be a multibyte string, | |
2246 | which is true if any of the inputs is one. */ | |
2247 | int multibyte = 0; | |
2248 | unsigned char *this_format; | |
e781c49e | 2249 | int longest_format; |
8d6179dc | 2250 | Lisp_Object val; |
1f24f4fd | 2251 | |
35692fe0 | 2252 | extern char *index (); |
1f24f4fd | 2253 | |
35692fe0 JB |
2254 | /* It should not be necessary to GCPRO ARGS, because |
2255 | the caller in the interpreter should take care of that. */ | |
2256 | ||
e781c49e RS |
2257 | /* Try to determine whether the result should be multibyte. |
2258 | This is not always right; sometimes the result needs to be multibyte | |
2259 | because of an object that we will pass through prin1, | |
2260 | and in that case, we won't know it here. */ | |
1f24f4fd RS |
2261 | for (n = 0; n < nargs; n++) |
2262 | if (STRINGP (args[n]) && STRING_MULTIBYTE (args[n])) | |
2263 | multibyte = 1; | |
2264 | ||
35692fe0 | 2265 | CHECK_STRING (args[0], 0); |
e781c49e RS |
2266 | |
2267 | /* If we start out planning a unibyte result, | |
2268 | and later find it has to be multibyte, we jump back to retry. */ | |
2269 | retry: | |
2270 | ||
35692fe0 | 2271 | format = XSTRING (args[0])->data; |
dc3620af | 2272 | end = format + STRING_BYTES (XSTRING (args[0])); |
e781c49e | 2273 | longest_format = 0; |
1f24f4fd RS |
2274 | |
2275 | /* Make room in result for all the non-%-codes in the control string. */ | |
e781c49e | 2276 | total = 5 + CONVERTED_BYTE_SIZE (multibyte, args[0]); |
1f24f4fd RS |
2277 | |
2278 | /* Add to TOTAL enough space to hold the converted arguments. */ | |
35692fe0 JB |
2279 | |
2280 | n = 0; | |
2281 | while (format != end) | |
2282 | if (*format++ == '%') | |
2283 | { | |
1f24f4fd RS |
2284 | int minlen, thissize = 0; |
2285 | unsigned char *this_format_start = format - 1; | |
35692fe0 JB |
2286 | |
2287 | /* Process a numeric arg and skip it. */ | |
2288 | minlen = atoi (format); | |
537dfb13 RS |
2289 | if (minlen < 0) |
2290 | minlen = - minlen; | |
2291 | ||
35692fe0 JB |
2292 | while ((*format >= '0' && *format <= '9') |
2293 | || *format == '-' || *format == ' ' || *format == '.') | |
2294 | format++; | |
2295 | ||
1f24f4fd RS |
2296 | if (format - this_format_start + 1 > longest_format) |
2297 | longest_format = format - this_format_start + 1; | |
2298 | ||
35692fe0 JB |
2299 | if (*format == '%') |
2300 | format++; | |
2301 | else if (++n >= nargs) | |
537dfb13 | 2302 | error ("Not enough arguments for format string"); |
35692fe0 JB |
2303 | else if (*format == 'S') |
2304 | { | |
2305 | /* For `S', prin1 the argument and then treat like a string. */ | |
2306 | register Lisp_Object tem; | |
2307 | tem = Fprin1_to_string (args[n], Qnil); | |
e781c49e RS |
2308 | if (STRING_MULTIBYTE (tem) && ! multibyte) |
2309 | { | |
2310 | multibyte = 1; | |
2311 | goto retry; | |
2312 | } | |
35692fe0 JB |
2313 | args[n] = tem; |
2314 | goto string; | |
2315 | } | |
ae683129 | 2316 | else if (SYMBOLP (args[n])) |
35692fe0 | 2317 | { |
d2fd0445 | 2318 | XSETSTRING (args[n], XSYMBOL (args[n])->name); |
7df74da6 RS |
2319 | if (STRING_MULTIBYTE (args[n]) && ! multibyte) |
2320 | { | |
2321 | multibyte = 1; | |
2322 | goto retry; | |
2323 | } | |
35692fe0 JB |
2324 | goto string; |
2325 | } | |
ae683129 | 2326 | else if (STRINGP (args[n])) |
35692fe0 JB |
2327 | { |
2328 | string: | |
b22e7ecc KH |
2329 | if (*format != 's' && *format != 'S') |
2330 | error ("format specifier doesn't match argument type"); | |
1f24f4fd | 2331 | thissize = CONVERTED_BYTE_SIZE (multibyte, args[n]); |
35692fe0 JB |
2332 | } |
2333 | /* Would get MPV otherwise, since Lisp_Int's `point' to low memory. */ | |
ae683129 | 2334 | else if (INTEGERP (args[n]) && *format != 's') |
35692fe0 | 2335 | { |
4746118a | 2336 | #ifdef LISP_FLOAT_TYPE |
eb8c3be9 | 2337 | /* The following loop assumes the Lisp type indicates |
35692fe0 JB |
2338 | the proper way to pass the argument. |
2339 | So make sure we have a flonum if the argument should | |
2340 | be a double. */ | |
2341 | if (*format == 'e' || *format == 'f' || *format == 'g') | |
2342 | args[n] = Ffloat (args[n]); | |
4746118a | 2343 | #endif |
f49a2d74 | 2344 | thissize = 30; |
25c9e7fb RS |
2345 | if (*format == 'c' |
2346 | && (! SINGLE_BYTE_CHAR_P (XINT (args[n])) | |
2347 | || XINT (args[n]) == 0)) | |
f49a2d74 KH |
2348 | { |
2349 | if (! multibyte) | |
2350 | { | |
2351 | multibyte = 1; | |
2352 | goto retry; | |
2353 | } | |
2354 | args[n] = Fchar_to_string (args[n]); | |
dc3620af | 2355 | thissize = STRING_BYTES (XSTRING (args[n])); |
f49a2d74 | 2356 | } |
35692fe0 | 2357 | } |
4746118a | 2358 | #ifdef LISP_FLOAT_TYPE |
ae683129 | 2359 | else if (FLOATP (args[n]) && *format != 's') |
35692fe0 JB |
2360 | { |
2361 | if (! (*format == 'e' || *format == 'f' || *format == 'g')) | |
247422ce | 2362 | args[n] = Ftruncate (args[n], Qnil); |
1f24f4fd | 2363 | thissize = 60; |
35692fe0 | 2364 | } |
4746118a | 2365 | #endif |
35692fe0 JB |
2366 | else |
2367 | { | |
2368 | /* Anything but a string, convert to a string using princ. */ | |
2369 | register Lisp_Object tem; | |
2370 | tem = Fprin1_to_string (args[n], Qt); | |
a4e91443 | 2371 | if (STRING_MULTIBYTE (tem) & ! multibyte) |
e781c49e RS |
2372 | { |
2373 | multibyte = 1; | |
2374 | goto retry; | |
2375 | } | |
35692fe0 JB |
2376 | args[n] = tem; |
2377 | goto string; | |
2378 | } | |
1f24f4fd RS |
2379 | |
2380 | if (thissize < minlen) | |
2381 | thissize = minlen; | |
2382 | ||
2383 | total += thissize + 4; | |
35692fe0 JB |
2384 | } |
2385 | ||
e781c49e RS |
2386 | /* Now we can no longer jump to retry. |
2387 | TOTAL and LONGEST_FORMAT are known for certain. */ | |
2388 | ||
1f24f4fd | 2389 | this_format = (unsigned char *) alloca (longest_format + 1); |
50aa2f90 | 2390 | |
1f24f4fd RS |
2391 | /* Allocate the space for the result. |
2392 | Note that TOTAL is an overestimate. */ | |
2393 | if (total < 1000) | |
2394 | buf = (unsigned char *) alloca (total + 1); | |
2395 | else | |
2396 | buf = (unsigned char *) xmalloc (total + 1); | |
35692fe0 | 2397 | |
1f24f4fd RS |
2398 | p = buf; |
2399 | nchars = 0; | |
2400 | n = 0; | |
35692fe0 | 2401 | |
1f24f4fd RS |
2402 | /* Scan the format and store result in BUF. */ |
2403 | format = XSTRING (args[0])->data; | |
2404 | while (format != end) | |
2405 | { | |
2406 | if (*format == '%') | |
2407 | { | |
2408 | int minlen; | |
25c9e7fb | 2409 | int negative = 0; |
1f24f4fd | 2410 | unsigned char *this_format_start = format; |
35692fe0 | 2411 | |
1f24f4fd | 2412 | format++; |
fb893977 | 2413 | |
1f24f4fd RS |
2414 | /* Process a numeric arg and skip it. */ |
2415 | minlen = atoi (format); | |
2416 | if (minlen < 0) | |
25c9e7fb | 2417 | minlen = - minlen, negative = 1; |
35692fe0 | 2418 | |
1f24f4fd RS |
2419 | while ((*format >= '0' && *format <= '9') |
2420 | || *format == '-' || *format == ' ' || *format == '.') | |
2421 | format++; | |
35692fe0 | 2422 | |
1f24f4fd RS |
2423 | if (*format++ == '%') |
2424 | { | |
2425 | *p++ = '%'; | |
2426 | nchars++; | |
2427 | continue; | |
2428 | } | |
2429 | ||
2430 | ++n; | |
2431 | ||
2432 | if (STRINGP (args[n])) | |
2433 | { | |
25c9e7fb RS |
2434 | int padding, nbytes; |
2435 | int width = strwidth (XSTRING (args[n])->data, | |
dc3620af | 2436 | STRING_BYTES (XSTRING (args[n]))); |
25c9e7fb RS |
2437 | |
2438 | /* If spec requires it, pad on right with spaces. */ | |
2439 | padding = minlen - width; | |
2440 | if (! negative) | |
2441 | while (padding-- > 0) | |
2442 | { | |
2443 | *p++ = ' '; | |
2444 | nchars++; | |
2445 | } | |
1f24f4fd RS |
2446 | |
2447 | nbytes = copy_text (XSTRING (args[n])->data, p, | |
dc3620af | 2448 | STRING_BYTES (XSTRING (args[n])), |
1f24f4fd RS |
2449 | STRING_MULTIBYTE (args[n]), multibyte); |
2450 | p += nbytes; | |
2451 | nchars += XSTRING (args[n])->size; | |
2452 | ||
25c9e7fb RS |
2453 | if (negative) |
2454 | while (padding-- > 0) | |
2455 | { | |
2456 | *p++ = ' '; | |
2457 | nchars++; | |
2458 | } | |
1f24f4fd RS |
2459 | } |
2460 | else if (INTEGERP (args[n]) || FLOATP (args[n])) | |
2461 | { | |
2462 | int this_nchars; | |
2463 | ||
2464 | bcopy (this_format_start, this_format, | |
2465 | format - this_format_start); | |
2466 | this_format[format - this_format_start] = 0; | |
2467 | ||
d0183d38 RS |
2468 | if (INTEGERP (args[n])) |
2469 | sprintf (p, this_format, XINT (args[n])); | |
2470 | else | |
2471 | sprintf (p, this_format, XFLOAT (args[n])->data); | |
1f24f4fd RS |
2472 | |
2473 | this_nchars = strlen (p); | |
2474 | p += this_nchars; | |
2475 | nchars += this_nchars; | |
2476 | } | |
2477 | } | |
7df74da6 RS |
2478 | else if (STRING_MULTIBYTE (args[0])) |
2479 | { | |
2480 | /* Copy a whole multibyte character. */ | |
2481 | *p++ = *format++; | |
2482 | while (! CHAR_HEAD_P (*format)) *p++ = *format++; | |
2483 | nchars++; | |
2484 | } | |
2485 | else if (multibyte) | |
1f24f4fd RS |
2486 | { |
2487 | /* Convert a single-byte character to multibyte. */ | |
2488 | int len = copy_text (format, p, 1, 0, 1); | |
2489 | ||
2490 | p += len; | |
2491 | format++; | |
2492 | nchars++; | |
2493 | } | |
2494 | else | |
2495 | *p++ = *format++, nchars++; | |
2496 | } | |
2497 | ||
5f75e666 | 2498 | val = make_specified_string (buf, nchars, p - buf, multibyte); |
8d6179dc | 2499 | |
1f24f4fd RS |
2500 | /* If we allocated BUF with malloc, free it too. */ |
2501 | if (total >= 1000) | |
2502 | xfree (buf); | |
35692fe0 | 2503 | |
8d6179dc | 2504 | return val; |
35692fe0 JB |
2505 | } |
2506 | ||
2507 | /* VARARGS 1 */ | |
2508 | Lisp_Object | |
2509 | #ifdef NO_ARG_ARRAY | |
2510 | format1 (string1, arg0, arg1, arg2, arg3, arg4) | |
679e18b1 | 2511 | EMACS_INT arg0, arg1, arg2, arg3, arg4; |
35692fe0 JB |
2512 | #else |
2513 | format1 (string1) | |
2514 | #endif | |
2515 | char *string1; | |
2516 | { | |
2517 | char buf[100]; | |
2518 | #ifdef NO_ARG_ARRAY | |
679e18b1 | 2519 | EMACS_INT args[5]; |
35692fe0 JB |
2520 | args[0] = arg0; |
2521 | args[1] = arg1; | |
2522 | args[2] = arg2; | |
2523 | args[3] = arg3; | |
2524 | args[4] = arg4; | |
e3670faa | 2525 | doprnt (buf, sizeof buf, string1, (char *)0, 5, (char **) args); |
35692fe0 | 2526 | #else |
ea4d2909 | 2527 | doprnt (buf, sizeof buf, string1, (char *)0, 5, &string1 + 1); |
35692fe0 JB |
2528 | #endif |
2529 | return build_string (buf); | |
2530 | } | |
2531 | \f | |
2532 | DEFUN ("char-equal", Fchar_equal, Schar_equal, 2, 2, 0, | |
2533 | "Return t if two characters match, optionally ignoring case.\n\ | |
2534 | Both arguments must be characters (i.e. integers).\n\ | |
2535 | Case is ignored if `case-fold-search' is non-nil in the current buffer.") | |
2536 | (c1, c2) | |
2537 | register Lisp_Object c1, c2; | |
2538 | { | |
1b5d98bb | 2539 | int i1, i2; |
35692fe0 JB |
2540 | CHECK_NUMBER (c1, 0); |
2541 | CHECK_NUMBER (c2, 1); | |
2542 | ||
1b5d98bb | 2543 | if (XINT (c1) == XINT (c2)) |
35692fe0 | 2544 | return Qt; |
1b5d98bb RS |
2545 | if (NILP (current_buffer->case_fold_search)) |
2546 | return Qnil; | |
2547 | ||
2548 | /* Do these in separate statements, | |
2549 | then compare the variables. | |
2550 | because of the way DOWNCASE uses temp variables. */ | |
2551 | i1 = DOWNCASE (XFASTINT (c1)); | |
2552 | i2 = DOWNCASE (XFASTINT (c2)); | |
2553 | return (i1 == i2 ? Qt : Qnil); | |
35692fe0 | 2554 | } |
b229b8d1 RS |
2555 | \f |
2556 | /* Transpose the markers in two regions of the current buffer, and | |
2557 | adjust the ones between them if necessary (i.e.: if the regions | |
2558 | differ in size). | |
2559 | ||
ec1c14f6 RS |
2560 | START1, END1 are the character positions of the first region. |
2561 | START1_BYTE, END1_BYTE are the byte positions. | |
2562 | START2, END2 are the character positions of the second region. | |
2563 | START2_BYTE, END2_BYTE are the byte positions. | |
2564 | ||
b229b8d1 RS |
2565 | Traverses the entire marker list of the buffer to do so, adding an |
2566 | appropriate amount to some, subtracting from some, and leaving the | |
2567 | rest untouched. Most of this is copied from adjust_markers in insdel.c. | |
2568 | ||
ec1c14f6 | 2569 | It's the caller's job to ensure that START1 <= END1 <= START2 <= END2. */ |
b229b8d1 RS |
2570 | |
2571 | void | |
ec1c14f6 RS |
2572 | transpose_markers (start1, end1, start2, end2, |
2573 | start1_byte, end1_byte, start2_byte, end2_byte) | |
b229b8d1 | 2574 | register int start1, end1, start2, end2; |
ec1c14f6 | 2575 | register int start1_byte, end1_byte, start2_byte, end2_byte; |
b229b8d1 | 2576 | { |
ec1c14f6 | 2577 | register int amt1, amt1_byte, amt2, amt2_byte, diff, diff_byte, mpos; |
b229b8d1 | 2578 | register Lisp_Object marker; |
b229b8d1 | 2579 | |
03240d11 | 2580 | /* Update point as if it were a marker. */ |
8de1d5f0 KH |
2581 | if (PT < start1) |
2582 | ; | |
2583 | else if (PT < end1) | |
ec1c14f6 RS |
2584 | TEMP_SET_PT_BOTH (PT + (end2 - end1), |
2585 | PT_BYTE + (end2_byte - end1_byte)); | |
8de1d5f0 | 2586 | else if (PT < start2) |
ec1c14f6 RS |
2587 | TEMP_SET_PT_BOTH (PT + (end2 - start2) - (end1 - start1), |
2588 | (PT_BYTE + (end2_byte - start2_byte) | |
2589 | - (end1_byte - start1_byte))); | |
8de1d5f0 | 2590 | else if (PT < end2) |
ec1c14f6 RS |
2591 | TEMP_SET_PT_BOTH (PT - (start2 - start1), |
2592 | PT_BYTE - (start2_byte - start1_byte)); | |
8de1d5f0 | 2593 | |
03240d11 KH |
2594 | /* We used to adjust the endpoints here to account for the gap, but that |
2595 | isn't good enough. Even if we assume the caller has tried to move the | |
2596 | gap out of our way, it might still be at start1 exactly, for example; | |
2597 | and that places it `inside' the interval, for our purposes. The amount | |
2598 | of adjustment is nontrivial if there's a `denormalized' marker whose | |
2599 | position is between GPT and GPT + GAP_SIZE, so it's simpler to leave | |
2600 | the dirty work to Fmarker_position, below. */ | |
b229b8d1 RS |
2601 | |
2602 | /* The difference between the region's lengths */ | |
2603 | diff = (end2 - start2) - (end1 - start1); | |
ec1c14f6 | 2604 | diff_byte = (end2_byte - start2_byte) - (end1_byte - start1_byte); |
b229b8d1 RS |
2605 | |
2606 | /* For shifting each marker in a region by the length of the other | |
ec1c14f6 | 2607 | region plus the distance between the regions. */ |
b229b8d1 RS |
2608 | amt1 = (end2 - start2) + (start2 - end1); |
2609 | amt2 = (end1 - start1) + (start2 - end1); | |
ec1c14f6 RS |
2610 | amt1_byte = (end2_byte - start2_byte) + (start2_byte - end1_byte); |
2611 | amt2_byte = (end1_byte - start1_byte) + (start2_byte - end1_byte); | |
b229b8d1 | 2612 | |
1e158d25 | 2613 | for (marker = BUF_MARKERS (current_buffer); !NILP (marker); |
03240d11 | 2614 | marker = XMARKER (marker)->chain) |
b229b8d1 | 2615 | { |
ec1c14f6 RS |
2616 | mpos = marker_byte_position (marker); |
2617 | if (mpos >= start1_byte && mpos < end2_byte) | |
2618 | { | |
2619 | if (mpos < end1_byte) | |
2620 | mpos += amt1_byte; | |
2621 | else if (mpos < start2_byte) | |
2622 | mpos += diff_byte; | |
2623 | else | |
2624 | mpos -= amt2_byte; | |
f3e1f752 | 2625 | XMARKER (marker)->bytepos = mpos; |
ec1c14f6 RS |
2626 | } |
2627 | mpos = XMARKER (marker)->charpos; | |
03240d11 KH |
2628 | if (mpos >= start1 && mpos < end2) |
2629 | { | |
2630 | if (mpos < end1) | |
2631 | mpos += amt1; | |
2632 | else if (mpos < start2) | |
2633 | mpos += diff; | |
2634 | else | |
2635 | mpos -= amt2; | |
03240d11 | 2636 | } |
ec1c14f6 | 2637 | XMARKER (marker)->charpos = mpos; |
b229b8d1 RS |
2638 | } |
2639 | } | |
2640 | ||
2641 | DEFUN ("transpose-regions", Ftranspose_regions, Stranspose_regions, 4, 5, 0, | |
2642 | "Transpose region START1 to END1 with START2 to END2.\n\ | |
2643 | The regions may not be overlapping, because the size of the buffer is\n\ | |
2644 | never changed in a transposition.\n\ | |
2645 | \n\ | |
ec1c14f6 | 2646 | Optional fifth arg LEAVE_MARKERS, if non-nil, means don't update\n\ |
b229b8d1 RS |
2647 | any markers that happen to be located in the regions.\n\ |
2648 | \n\ | |
2649 | Transposing beyond buffer boundaries is an error.") | |
2650 | (startr1, endr1, startr2, endr2, leave_markers) | |
2651 | Lisp_Object startr1, endr1, startr2, endr2, leave_markers; | |
2652 | { | |
ec1c14f6 RS |
2653 | register int start1, end1, start2, end2; |
2654 | int start1_byte, start2_byte, len1_byte, len2_byte; | |
2655 | int gap, len1, len_mid, len2; | |
3c6bc7d0 | 2656 | unsigned char *start1_addr, *start2_addr, *temp; |
dc3620af RS |
2657 | int combined_before_bytes_1, combined_after_bytes_1; |
2658 | int combined_before_bytes_2, combined_after_bytes_2; | |
2659 | struct gcpro gcpro1, gcpro2; | |
b229b8d1 RS |
2660 | |
2661 | #ifdef USE_TEXT_PROPERTIES | |
2662 | INTERVAL cur_intv, tmp_interval1, tmp_interval_mid, tmp_interval2; | |
1e158d25 | 2663 | cur_intv = BUF_INTERVALS (current_buffer); |
b229b8d1 RS |
2664 | #endif /* USE_TEXT_PROPERTIES */ |
2665 | ||
2666 | validate_region (&startr1, &endr1); | |
2667 | validate_region (&startr2, &endr2); | |
2668 | ||
2669 | start1 = XFASTINT (startr1); | |
2670 | end1 = XFASTINT (endr1); | |
2671 | start2 = XFASTINT (startr2); | |
2672 | end2 = XFASTINT (endr2); | |
2673 | gap = GPT; | |
2674 | ||
2675 | /* Swap the regions if they're reversed. */ | |
2676 | if (start2 < end1) | |
2677 | { | |
2678 | register int glumph = start1; | |
2679 | start1 = start2; | |
2680 | start2 = glumph; | |
2681 | glumph = end1; | |
2682 | end1 = end2; | |
2683 | end2 = glumph; | |
2684 | } | |
2685 | ||
b229b8d1 RS |
2686 | len1 = end1 - start1; |
2687 | len2 = end2 - start2; | |
2688 | ||
2689 | if (start2 < end1) | |
dc3620af | 2690 | error ("Transposed regions overlap"); |
b229b8d1 | 2691 | else if (start1 == end1 || start2 == end2) |
dc3620af | 2692 | error ("Transposed region has length 0"); |
b229b8d1 RS |
2693 | |
2694 | /* The possibilities are: | |
2695 | 1. Adjacent (contiguous) regions, or separate but equal regions | |
2696 | (no, really equal, in this case!), or | |
2697 | 2. Separate regions of unequal size. | |
2698 | ||
2699 | The worst case is usually No. 2. It means that (aside from | |
2700 | potential need for getting the gap out of the way), there also | |
2701 | needs to be a shifting of the text between the two regions. So | |
2702 | if they are spread far apart, we are that much slower... sigh. */ | |
2703 | ||
2704 | /* It must be pointed out that the really studly thing to do would | |
2705 | be not to move the gap at all, but to leave it in place and work | |
2706 | around it if necessary. This would be extremely efficient, | |
2707 | especially considering that people are likely to do | |
2708 | transpositions near where they are working interactively, which | |
2709 | is exactly where the gap would be found. However, such code | |
2710 | would be much harder to write and to read. So, if you are | |
2711 | reading this comment and are feeling squirrely, by all means have | |
2712 | a go! I just didn't feel like doing it, so I will simply move | |
2713 | the gap the minimum distance to get it out of the way, and then | |
2714 | deal with an unbroken array. */ | |
3c6bc7d0 RS |
2715 | |
2716 | /* Make sure the gap won't interfere, by moving it out of the text | |
2717 | we will operate on. */ | |
2718 | if (start1 < gap && gap < end2) | |
2719 | { | |
2720 | if (gap - start1 < end2 - gap) | |
2721 | move_gap (start1); | |
2722 | else | |
2723 | move_gap (end2); | |
2724 | } | |
ec1c14f6 RS |
2725 | |
2726 | start1_byte = CHAR_TO_BYTE (start1); | |
2727 | start2_byte = CHAR_TO_BYTE (start2); | |
2728 | len1_byte = CHAR_TO_BYTE (end1) - start1_byte; | |
2729 | len2_byte = CHAR_TO_BYTE (end2) - start2_byte; | |
dc3620af RS |
2730 | |
2731 | if (end1 == start2) | |
2732 | { | |
2733 | combined_before_bytes_2 | |
2734 | = count_combining_before (BYTE_POS_ADDR (start2_byte), | |
2735 | len2_byte, start1, start1_byte); | |
2736 | combined_before_bytes_1 | |
2737 | = count_combining_before (BYTE_POS_ADDR (start1_byte), | |
2738 | len1_byte, end2, start2_byte + len2_byte); | |
2739 | combined_after_bytes_1 | |
2740 | = count_combining_after (BYTE_POS_ADDR (start1_byte), | |
2741 | len1_byte, end2, start2_byte + len2_byte); | |
2742 | combined_after_bytes_2 = 0; | |
2743 | } | |
2744 | else | |
2745 | { | |
2746 | combined_before_bytes_2 | |
2747 | = count_combining_before (BYTE_POS_ADDR (start2_byte), | |
2748 | len2_byte, start1, start1_byte); | |
2749 | combined_before_bytes_1 | |
2750 | = count_combining_before (BYTE_POS_ADDR (start1_byte), | |
2751 | len1_byte, start2, start2_byte); | |
2752 | combined_after_bytes_2 | |
2753 | = count_combining_after (BYTE_POS_ADDR (start2_byte), | |
2754 | len2_byte, end1, start1_byte + len1_byte); | |
2755 | combined_after_bytes_1 | |
2756 | = count_combining_after (BYTE_POS_ADDR (start1_byte), | |
2757 | len1_byte, end2, start2_byte + len2_byte); | |
2758 | } | |
2759 | ||
2760 | /* If any combining is going to happen, do this the stupid way, | |
2761 | because replace handles combining properly. */ | |
2762 | if (combined_before_bytes_1 || combined_before_bytes_2 | |
2763 | || combined_after_bytes_1 || combined_after_bytes_2) | |
2764 | { | |
2765 | Lisp_Object text1, text2; | |
2766 | ||
2767 | text1 = text2 = Qnil; | |
2768 | GCPRO2 (text1, text2); | |
2769 | ||
2770 | text1 = make_buffer_string_both (start1, start1_byte, | |
2771 | end1, start1_byte + len1_byte, 1); | |
2772 | text2 = make_buffer_string_both (start2, start2_byte, | |
2773 | end2, start2_byte + len2_byte, 1); | |
2774 | ||
2775 | transpose_markers (start1, end1, start2, end2, | |
2776 | start1_byte, start1_byte + len1_byte, | |
2777 | start2_byte, start2_byte + len2_byte); | |
2778 | ||
8daf7106 AS |
2779 | replace_range (start2, end2, text1, 1, 0, 1); |
2780 | replace_range (start1, end1, text2, 1, 0, 1); | |
dc3620af RS |
2781 | |
2782 | UNGCPRO; | |
2783 | return Qnil; | |
2784 | } | |
b229b8d1 RS |
2785 | |
2786 | /* Hmmm... how about checking to see if the gap is large | |
2787 | enough to use as the temporary storage? That would avoid an | |
2788 | allocation... interesting. Later, don't fool with it now. */ | |
2789 | ||
2790 | /* Working without memmove, for portability (sigh), so must be | |
2791 | careful of overlapping subsections of the array... */ | |
2792 | ||
2793 | if (end1 == start2) /* adjacent regions */ | |
2794 | { | |
b229b8d1 RS |
2795 | modify_region (current_buffer, start1, end2); |
2796 | record_change (start1, len1 + len2); | |
2797 | ||
2798 | #ifdef USE_TEXT_PROPERTIES | |
2799 | tmp_interval1 = copy_intervals (cur_intv, start1, len1); | |
2800 | tmp_interval2 = copy_intervals (cur_intv, start2, len2); | |
09dbcf71 RS |
2801 | Fset_text_properties (make_number (start1), make_number (end2), |
2802 | Qnil, Qnil); | |
b229b8d1 RS |
2803 | #endif /* USE_TEXT_PROPERTIES */ |
2804 | ||
2805 | /* First region smaller than second. */ | |
ec1c14f6 | 2806 | if (len1_byte < len2_byte) |
b229b8d1 | 2807 | { |
3c6bc7d0 RS |
2808 | /* We use alloca only if it is small, |
2809 | because we want to avoid stack overflow. */ | |
ec1c14f6 RS |
2810 | if (len2_byte > 20000) |
2811 | temp = (unsigned char *) xmalloc (len2_byte); | |
3c6bc7d0 | 2812 | else |
ec1c14f6 | 2813 | temp = (unsigned char *) alloca (len2_byte); |
03240d11 KH |
2814 | |
2815 | /* Don't precompute these addresses. We have to compute them | |
2816 | at the last minute, because the relocating allocator might | |
2817 | have moved the buffer around during the xmalloc. */ | |
ec1c14f6 RS |
2818 | start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1_byte); |
2819 | start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2_byte); | |
03240d11 | 2820 | |
ec1c14f6 RS |
2821 | bcopy (start2_addr, temp, len2_byte); |
2822 | bcopy (start1_addr, start1_addr + len2_byte, len1_byte); | |
2823 | bcopy (temp, start1_addr, len2_byte); | |
2824 | if (len2_byte > 20000) | |
3c6bc7d0 | 2825 | free (temp); |
b229b8d1 RS |
2826 | } |
2827 | else | |
2828 | /* First region not smaller than second. */ | |
2829 | { | |
ec1c14f6 RS |
2830 | if (len1_byte > 20000) |
2831 | temp = (unsigned char *) xmalloc (len1_byte); | |
3c6bc7d0 | 2832 | else |
ec1c14f6 RS |
2833 | temp = (unsigned char *) alloca (len1_byte); |
2834 | start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1_byte); | |
2835 | start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2_byte); | |
2836 | bcopy (start1_addr, temp, len1_byte); | |
2837 | bcopy (start2_addr, start1_addr, len2_byte); | |
2838 | bcopy (temp, start1_addr + len2_byte, len1_byte); | |
2839 | if (len1_byte > 20000) | |
3c6bc7d0 | 2840 | free (temp); |
b229b8d1 RS |
2841 | } |
2842 | #ifdef USE_TEXT_PROPERTIES | |
2843 | graft_intervals_into_buffer (tmp_interval1, start1 + len2, | |
2844 | len1, current_buffer, 0); | |
2845 | graft_intervals_into_buffer (tmp_interval2, start1, | |
2846 | len2, current_buffer, 0); | |
2847 | #endif /* USE_TEXT_PROPERTIES */ | |
2848 | } | |
2849 | /* Non-adjacent regions, because end1 != start2, bleagh... */ | |
2850 | else | |
2851 | { | |
ec1c14f6 RS |
2852 | len_mid = start2_byte - (start1_byte + len1_byte); |
2853 | ||
2854 | if (len1_byte == len2_byte) | |
b229b8d1 RS |
2855 | /* Regions are same size, though, how nice. */ |
2856 | { | |
2857 | modify_region (current_buffer, start1, end1); | |
2858 | modify_region (current_buffer, start2, end2); | |
2859 | record_change (start1, len1); | |
2860 | record_change (start2, len2); | |
2861 | #ifdef USE_TEXT_PROPERTIES | |
2862 | tmp_interval1 = copy_intervals (cur_intv, start1, len1); | |
2863 | tmp_interval2 = copy_intervals (cur_intv, start2, len2); | |
09dbcf71 RS |
2864 | Fset_text_properties (make_number (start1), make_number (end1), |
2865 | Qnil, Qnil); | |
2866 | Fset_text_properties (make_number (start2), make_number (end2), | |
2867 | Qnil, Qnil); | |
b229b8d1 RS |
2868 | #endif /* USE_TEXT_PROPERTIES */ |
2869 | ||
ec1c14f6 RS |
2870 | if (len1_byte > 20000) |
2871 | temp = (unsigned char *) xmalloc (len1_byte); | |
3c6bc7d0 | 2872 | else |
ec1c14f6 RS |
2873 | temp = (unsigned char *) alloca (len1_byte); |
2874 | start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1_byte); | |
2875 | start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2_byte); | |
2876 | bcopy (start1_addr, temp, len1_byte); | |
2877 | bcopy (start2_addr, start1_addr, len2_byte); | |
2878 | bcopy (temp, start2_addr, len1_byte); | |
2879 | if (len1_byte > 20000) | |
3c6bc7d0 | 2880 | free (temp); |
b229b8d1 RS |
2881 | #ifdef USE_TEXT_PROPERTIES |
2882 | graft_intervals_into_buffer (tmp_interval1, start2, | |
2883 | len1, current_buffer, 0); | |
2884 | graft_intervals_into_buffer (tmp_interval2, start1, | |
2885 | len2, current_buffer, 0); | |
2886 | #endif /* USE_TEXT_PROPERTIES */ | |
2887 | } | |
2888 | ||
ec1c14f6 | 2889 | else if (len1_byte < len2_byte) /* Second region larger than first */ |
b229b8d1 RS |
2890 | /* Non-adjacent & unequal size, area between must also be shifted. */ |
2891 | { | |
b229b8d1 RS |
2892 | modify_region (current_buffer, start1, end2); |
2893 | record_change (start1, (end2 - start1)); | |
2894 | #ifdef USE_TEXT_PROPERTIES | |
2895 | tmp_interval1 = copy_intervals (cur_intv, start1, len1); | |
2896 | tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid); | |
2897 | tmp_interval2 = copy_intervals (cur_intv, start2, len2); | |
09dbcf71 RS |
2898 | Fset_text_properties (make_number (start1), make_number (end2), |
2899 | Qnil, Qnil); | |
b229b8d1 RS |
2900 | #endif /* USE_TEXT_PROPERTIES */ |
2901 | ||
3c6bc7d0 | 2902 | /* holds region 2 */ |
ec1c14f6 RS |
2903 | if (len2_byte > 20000) |
2904 | temp = (unsigned char *) xmalloc (len2_byte); | |
3c6bc7d0 | 2905 | else |
ec1c14f6 RS |
2906 | temp = (unsigned char *) alloca (len2_byte); |
2907 | start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1_byte); | |
2908 | start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2_byte); | |
2909 | bcopy (start2_addr, temp, len2_byte); | |
2910 | bcopy (start1_addr, start1_addr + len_mid + len2_byte, len1_byte); | |
2911 | safe_bcopy (start1_addr + len1_byte, start1_addr + len2_byte, len_mid); | |
2912 | bcopy (temp, start1_addr, len2_byte); | |
2913 | if (len2_byte > 20000) | |
3c6bc7d0 | 2914 | free (temp); |
b229b8d1 RS |
2915 | #ifdef USE_TEXT_PROPERTIES |
2916 | graft_intervals_into_buffer (tmp_interval1, end2 - len1, | |
2917 | len1, current_buffer, 0); | |
2918 | graft_intervals_into_buffer (tmp_interval_mid, start1 + len2, | |
2919 | len_mid, current_buffer, 0); | |
2920 | graft_intervals_into_buffer (tmp_interval2, start1, | |
2921 | len2, current_buffer, 0); | |
2922 | #endif /* USE_TEXT_PROPERTIES */ | |
2923 | } | |
2924 | else | |
2925 | /* Second region smaller than first. */ | |
2926 | { | |
b229b8d1 RS |
2927 | record_change (start1, (end2 - start1)); |
2928 | modify_region (current_buffer, start1, end2); | |
2929 | ||
2930 | #ifdef USE_TEXT_PROPERTIES | |
2931 | tmp_interval1 = copy_intervals (cur_intv, start1, len1); | |
2932 | tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid); | |
2933 | tmp_interval2 = copy_intervals (cur_intv, start2, len2); | |
09dbcf71 RS |
2934 | Fset_text_properties (make_number (start1), make_number (end2), |
2935 | Qnil, Qnil); | |
b229b8d1 RS |
2936 | #endif /* USE_TEXT_PROPERTIES */ |
2937 | ||
3c6bc7d0 | 2938 | /* holds region 1 */ |
ec1c14f6 RS |
2939 | if (len1_byte > 20000) |
2940 | temp = (unsigned char *) xmalloc (len1_byte); | |
3c6bc7d0 | 2941 | else |
ec1c14f6 RS |
2942 | temp = (unsigned char *) alloca (len1_byte); |
2943 | start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1_byte); | |
2944 | start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2_byte); | |
2945 | bcopy (start1_addr, temp, len1_byte); | |
2946 | bcopy (start2_addr, start1_addr, len2_byte); | |
2947 | bcopy (start1_addr + len1_byte, start1_addr + len2_byte, len_mid); | |
2948 | bcopy (temp, start1_addr + len2_byte + len_mid, len1_byte); | |
2949 | if (len1_byte > 20000) | |
3c6bc7d0 | 2950 | free (temp); |
b229b8d1 RS |
2951 | #ifdef USE_TEXT_PROPERTIES |
2952 | graft_intervals_into_buffer (tmp_interval1, end2 - len1, | |
2953 | len1, current_buffer, 0); | |
2954 | graft_intervals_into_buffer (tmp_interval_mid, start1 + len2, | |
2955 | len_mid, current_buffer, 0); | |
2956 | graft_intervals_into_buffer (tmp_interval2, start1, | |
2957 | len2, current_buffer, 0); | |
2958 | #endif /* USE_TEXT_PROPERTIES */ | |
2959 | } | |
2960 | } | |
2961 | ||
ec1c14f6 RS |
2962 | /* When doing multiple transpositions, it might be nice |
2963 | to optimize this. Perhaps the markers in any one buffer | |
2964 | should be organized in some sorted data tree. */ | |
b229b8d1 | 2965 | if (NILP (leave_markers)) |
8de1d5f0 | 2966 | { |
ec1c14f6 RS |
2967 | transpose_markers (start1, end1, start2, end2, |
2968 | start1_byte, start1_byte + len1_byte, | |
2969 | start2_byte, start2_byte + len2_byte); | |
8de1d5f0 KH |
2970 | fix_overlays_in_range (start1, end2); |
2971 | } | |
b229b8d1 RS |
2972 | |
2973 | return Qnil; | |
2974 | } | |
35692fe0 | 2975 | |
35692fe0 JB |
2976 | \f |
2977 | void | |
2978 | syms_of_editfns () | |
2979 | { | |
260e2e2a KH |
2980 | environbuf = 0; |
2981 | ||
2982 | Qbuffer_access_fontify_functions | |
2983 | = intern ("buffer-access-fontify-functions"); | |
2984 | staticpro (&Qbuffer_access_fontify_functions); | |
2985 | ||
2986 | DEFVAR_LISP ("buffer-access-fontify-functions", | |
2987 | &Vbuffer_access_fontify_functions, | |
2988 | "List of functions called by `buffer-substring' to fontify if necessary.\n\ | |
2989 | Each function is called with two arguments which specify the range\n\ | |
2990 | of the buffer being accessed."); | |
2991 | Vbuffer_access_fontify_functions = Qnil; | |
2992 | ||
af209db8 RS |
2993 | { |
2994 | Lisp_Object obuf; | |
2995 | extern Lisp_Object Vprin1_to_string_buffer; | |
2996 | obuf = Fcurrent_buffer (); | |
2997 | /* Do this here, because init_buffer_once is too early--it won't work. */ | |
2998 | Fset_buffer (Vprin1_to_string_buffer); | |
2999 | /* Make sure buffer-access-fontify-functions is nil in this buffer. */ | |
3000 | Fset (Fmake_local_variable (intern ("buffer-access-fontify-functions")), | |
3001 | Qnil); | |
3002 | Fset_buffer (obuf); | |
3003 | } | |
3004 | ||
0b6fd023 | 3005 | DEFVAR_LISP ("buffer-access-fontified-property", |
260e2e2a KH |
3006 | &Vbuffer_access_fontified_property, |
3007 | "Property which (if non-nil) indicates text has been fontified.\n\ | |
3008 | `buffer-substring' need not call the `buffer-access-fontify-functions'\n\ | |
3009 | functions if all the text being accessed has this property."); | |
3010 | Vbuffer_access_fontified_property = Qnil; | |
3011 | ||
f43754f6 KH |
3012 | DEFVAR_LISP ("system-name", &Vsystem_name, |
3013 | "The name of the machine Emacs is running on."); | |
3014 | ||
3015 | DEFVAR_LISP ("user-full-name", &Vuser_full_name, | |
3016 | "The full name of the user logged in."); | |
3017 | ||
35b34f72 | 3018 | DEFVAR_LISP ("user-login-name", &Vuser_login_name, |
f43754f6 KH |
3019 | "The user's name, taken from environment variables if possible."); |
3020 | ||
35b34f72 | 3021 | DEFVAR_LISP ("user-real-login-name", &Vuser_real_login_name, |
f43754f6 | 3022 | "The user's name, based upon the real uid only."); |
35692fe0 JB |
3023 | |
3024 | defsubr (&Schar_equal); | |
3025 | defsubr (&Sgoto_char); | |
3026 | defsubr (&Sstring_to_char); | |
3027 | defsubr (&Schar_to_string); | |
3028 | defsubr (&Sbuffer_substring); | |
260e2e2a | 3029 | defsubr (&Sbuffer_substring_no_properties); |
35692fe0 JB |
3030 | defsubr (&Sbuffer_string); |
3031 | ||
3032 | defsubr (&Spoint_marker); | |
3033 | defsubr (&Smark_marker); | |
3034 | defsubr (&Spoint); | |
3035 | defsubr (&Sregion_beginning); | |
3036 | defsubr (&Sregion_end); | |
7df74da6 RS |
3037 | |
3038 | defsubr (&Sline_beginning_position); | |
3039 | defsubr (&Sline_end_position); | |
3040 | ||
35692fe0 JB |
3041 | /* defsubr (&Smark); */ |
3042 | /* defsubr (&Sset_mark); */ | |
3043 | defsubr (&Ssave_excursion); | |
4bc8c7d2 | 3044 | defsubr (&Ssave_current_buffer); |
35692fe0 JB |
3045 | |
3046 | defsubr (&Sbufsize); | |
3047 | defsubr (&Spoint_max); | |
3048 | defsubr (&Spoint_min); | |
3049 | defsubr (&Spoint_min_marker); | |
3050 | defsubr (&Spoint_max_marker); | |
7df74da6 | 3051 | defsubr (&Sposition_bytes); |
c9ed721d | 3052 | |
35692fe0 JB |
3053 | defsubr (&Sbobp); |
3054 | defsubr (&Seobp); | |
3055 | defsubr (&Sbolp); | |
3056 | defsubr (&Seolp); | |
850a8179 JB |
3057 | defsubr (&Sfollowing_char); |
3058 | defsubr (&Sprevious_char); | |
35692fe0 | 3059 | defsubr (&Schar_after); |
fb8106e8 | 3060 | defsubr (&Schar_before); |
35692fe0 JB |
3061 | defsubr (&Sinsert); |
3062 | defsubr (&Sinsert_before_markers); | |
be91036a RS |
3063 | defsubr (&Sinsert_and_inherit); |
3064 | defsubr (&Sinsert_and_inherit_before_markers); | |
35692fe0 JB |
3065 | defsubr (&Sinsert_char); |
3066 | ||
3067 | defsubr (&Suser_login_name); | |
3068 | defsubr (&Suser_real_login_name); | |
3069 | defsubr (&Suser_uid); | |
3070 | defsubr (&Suser_real_uid); | |
3071 | defsubr (&Suser_full_name); | |
7fd233b3 | 3072 | defsubr (&Semacs_pid); |
d940e0e4 | 3073 | defsubr (&Scurrent_time); |
a82d387c | 3074 | defsubr (&Sformat_time_string); |
4691c06d | 3075 | defsubr (&Sdecode_time); |
cce7b8a0 | 3076 | defsubr (&Sencode_time); |
35692fe0 | 3077 | defsubr (&Scurrent_time_string); |
c2662aea | 3078 | defsubr (&Scurrent_time_zone); |
143cb9a9 | 3079 | defsubr (&Sset_time_zone_rule); |
35692fe0 | 3080 | defsubr (&Ssystem_name); |
35692fe0 | 3081 | defsubr (&Smessage); |
cacc3e2c RS |
3082 | defsubr (&Smessage_box); |
3083 | defsubr (&Smessage_or_box); | |
b14dda8a | 3084 | defsubr (&Scurrent_message); |
35692fe0 | 3085 | defsubr (&Sformat); |
35692fe0 JB |
3086 | |
3087 | defsubr (&Sinsert_buffer_substring); | |
e9cf2084 | 3088 | defsubr (&Scompare_buffer_substrings); |
35692fe0 JB |
3089 | defsubr (&Ssubst_char_in_region); |
3090 | defsubr (&Stranslate_region); | |
3091 | defsubr (&Sdelete_region); | |
3092 | defsubr (&Swiden); | |
3093 | defsubr (&Snarrow_to_region); | |
3094 | defsubr (&Ssave_restriction); | |
b229b8d1 | 3095 | defsubr (&Stranspose_regions); |
35692fe0 | 3096 | } |