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