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