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