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