#include <config.h>
#include "lisp.h"
#include "commands.h"
#include "buffer.h"
#include "charset.h"
#include "syntax.h"
#include "window.h"
#include "keyboard.h"
#include "dispextern.h"
Lisp_Object Qkill_forward_chars, Qkill_backward_chars, Vblink_paren_function;
Lisp_Object Qoverwrite_mode_binary;
Lisp_Object Vself_insert_face;
Lisp_Object Vself_insert_face_command;
extern Lisp_Object Qface;
DEFUN ("forward-point", Fforward_point, Sforward_point, 1, 1, 0,
"Return buffer position N characters after (before if N negative) point.")
(n)
Lisp_Object n;
{
CHECK_NUMBER (n, 0);
return make_number (PT + XINT (n));
}
DEFUN ("forward-char", Fforward_char, Sforward_char, 0, 1, "p",
"Move point right N characters (left if N is negative).\n\
On reaching end of buffer, stop and signal error.")
(n)
Lisp_Object n;
{
if (NILP (n))
XSETFASTINT (n, 1);
else
CHECK_NUMBER (n, 0);
{
int new_point = PT + XINT (n);
if (new_point < BEGV)
{
SET_PT (BEGV);
Fsignal (Qbeginning_of_buffer, Qnil);
}
if (new_point > ZV)
{
SET_PT (ZV);
Fsignal (Qend_of_buffer, Qnil);
}
SET_PT (new_point);
}
return Qnil;
}
DEFUN ("backward-char", Fbackward_char, Sbackward_char, 0, 1, "p",
"Move point left N characters (right if N is negative).\n\
On attempt to pass beginning or end of buffer, stop and signal error.")
(n)
Lisp_Object n;
{
if (NILP (n))
XSETFASTINT (n, 1);
else
CHECK_NUMBER (n, 0);
XSETINT (n, - XINT (n));
return Fforward_char (n);
}
DEFUN ("forward-line", Fforward_line, Sforward_line, 0, 1, "p",
"Move N lines forward (backward if N is negative).\n\
Precisely, if point is on line I, move to the start of line I + N.\n\
If there isn't room, go as far as possible (no error).\n\
Returns the count of lines left to move. If moving forward,\n\
that is N - number of lines moved; if backward, N + number moved.\n\
With positive N, a non-empty line at the end counts as one line\n\
successfully moved (for the return value).")
(n)
Lisp_Object n;
{
int opoint = PT, opoint_byte = PT_BYTE;
int pos, pos_byte;
int count, shortage;
if (NILP (n))
count = 1;
else
{
CHECK_NUMBER (n, 0);
count = XINT (n);
}
if (count <= 0)
shortage = scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, count - 1, 1);
else
shortage = scan_newline (PT, PT_BYTE, ZV, ZV_BYTE, count, 1);
pos = PT;
pos_byte = PT_BYTE;
TEMP_SET_PT_BOTH (opoint, opoint_byte);
SET_PT_BOTH (pos, pos_byte);
if (shortage > 0
&& (count <= 0
|| (ZV > BEGV
&& PT != opoint
&& (FETCH_BYTE (PT_BYTE - 1) != '\n'))))
shortage--;
return make_number (count <= 0 ? - shortage : shortage);
}
DEFUN ("beginning-of-line", Fbeginning_of_line, Sbeginning_of_line, 0, 1, "p",
"Move point to beginning of current line.\n\
With argument N not nil or 1, move forward N - 1 lines first.\n\
If point reaches the beginning or end of buffer, it stops there.\n\
\n\
This command does not move point across a field boundary unless doing so\n\
would move beyond there to a different line; If N is nil or 1, and point\n\
starts at a field boundary, point does not move. To ignore field\n\
boundaries, either bind `inhibit-field-text-motion' to t, or use the\n\
`forward-line' function instead. For instance, `(forward-line 0)' does\n\
the same thing as `(beginning-of-line)', except that it ignores field\n\
boundaries.")
(n)
Lisp_Object n;
{
if (NILP (n))
XSETFASTINT (n, 1);
else
CHECK_NUMBER (n, 0);
SET_PT (XINT (Fline_beginning_position (n)));
return Qnil;
}
DEFUN ("end-of-line", Fend_of_line, Send_of_line, 0, 1, "p",
"Move point to end of current line.\n\
With argument N not nil or 1, move forward N - 1 lines first.\n\
If point reaches the beginning or end of buffer, it stops there.\n\
\n\
This command does not move point across a field boundary unless doing so\n\
would move beyond there to a different line; if N is nil or 1, and\n\
point starts at a field boundary, point does not move. To ignore field\n\
boundaries bind `inhibit-field-text-motion' to t.")
(n)
Lisp_Object n;
{
if (NILP (n))
XSETFASTINT (n, 1);
else
CHECK_NUMBER (n, 0);
SET_PT (XINT (Fline_end_position (n)));
return Qnil;
}
DEFUN ("delete-char", Fdelete_char, Sdelete_char, 1, 2, "p\nP",
"Delete the following N characters (previous if N is negative).\n\
Optional second arg KILLFLAG non-nil means kill instead (save in kill ring).\n\
Interactively, N is the prefix arg, and KILLFLAG is set if\n\
N was explicitly specified.")
(n, killflag)
Lisp_Object n, killflag;
{
int pos;
CHECK_NUMBER (n, 0);
pos = PT + XINT (n);
if (NILP (killflag))
{
if (XINT (n) < 0)
{
if (pos < BEGV)
Fsignal (Qbeginning_of_buffer, Qnil);
else
del_range (pos, PT);
}
else
{
if (pos > ZV)
Fsignal (Qend_of_buffer, Qnil);
else
del_range (PT, pos);
}
}
else
{
call1 (Qkill_forward_chars, n);
}
return Qnil;
}
DEFUN ("delete-backward-char", Fdelete_backward_char, Sdelete_backward_char,
1, 2, "p\nP",
"Delete the previous N characters (following if N is negative).\n\
Optional second arg KILLFLAG non-nil means kill instead (save in kill ring).\n\
Interactively, N is the prefix arg, and KILLFLAG is set if\n\
N was explicitly specified.")
(n, killflag)
Lisp_Object n, killflag;
{
Lisp_Object value;
int deleted_special = 0;
int pos, pos_byte, i;
CHECK_NUMBER (n, 0);
pos = PT;
pos_byte = PT_BYTE;
for (i = 0; i < XINT (n) && pos_byte > BEGV_BYTE; i++)
{
int c;
DEC_BOTH (pos, pos_byte);
c = FETCH_BYTE (pos_byte);
if (c == '\t' || c == '\n')
{
deleted_special = 1;
break;
}
}
if (XINT (n) > 0
&& ! NILP (current_buffer->overwrite_mode)
&& ! deleted_special
&& ! (PT == ZV || FETCH_BYTE (PT_BYTE) == '\n'))
{
int column = current_column ();
value = Fdelete_char (make_number (-XINT (n)), killflag);
i = column - current_column ();
Finsert_char (make_number (' '), make_number (i), Qnil);
SET_PT_BOTH (PT - i, PT_BYTE - i);
}
else
value = Fdelete_char (make_number (-XINT (n)), killflag);
return value;
}
DEFUN ("self-insert-command", Fself_insert_command, Sself_insert_command, 1, 1, "p",
"Insert the character you type.\n\
Whichever character you type to run this command is inserted.")
(n)
Lisp_Object n;
{
int character = XINT (last_command_char);
CHECK_NUMBER (n, 0);
if (!INTEGERP (last_command_char))
bitch_at_user ();
else if (XINT (n) >= 2 && NILP (current_buffer->overwrite_mode))
{
int modified_char = character;
if (! NILP (current_buffer->enable_multibyte_characters))
modified_char = unibyte_char_to_multibyte (modified_char);
XSETFASTINT (n, XFASTINT (n) - 2);
internal_self_insert (character, 1);
Finsert_char (make_number (modified_char), n, Qt);
internal_self_insert (character, 0);
}
else
while (XINT (n) > 0)
{
XSETFASTINT (n, XFASTINT (n) - 1);
internal_self_insert (character, XFASTINT (n) != 0);
}
return Qnil;
}
int
internal_self_insert (c, noautofill)
int c;
int noautofill;
{
extern Lisp_Object Fexpand_abbrev ();
int hairy = 0;
Lisp_Object tem;
register enum syntaxcode synt;
Lisp_Object overwrite, string;
int len;
unsigned char str[MAX_MULTIBYTE_LENGTH];
int chars_to_delete = 0;
int spaces_to_insert = 0;
overwrite = current_buffer->overwrite_mode;
if (!NILP (Vbefore_change_functions) || !NILP (Vafter_change_functions))
hairy = 1;
if (!NILP (current_buffer->enable_multibyte_characters))
{
c = unibyte_char_to_multibyte (c);
len = CHAR_STRING (c, str);
if (len == 1)
c = *str;
}
else
{
str[0] = (SINGLE_BYTE_CHAR_P (c)
? c
: multibyte_char_to_unibyte (c, Qnil));
len = 1;
}
if (!NILP (overwrite)
&& PT < ZV)
{
int c2 = FETCH_CHAR (PT_BYTE);
int target_clm = 0;
if (EQ (overwrite, Qoverwrite_mode_binary)
|| (c != '\n'
&& c2 != '\n'
&& ! (c2 == '\t'
&& XINT (current_buffer->tab_width) > 0
&& XFASTINT (current_buffer->tab_width) < 20
&& (target_clm = (current_column ()
+ XINT (Fchar_width (make_number (c)))),
target_clm % XFASTINT (current_buffer->tab_width)))))
{
int pos = PT;
int pos_byte = PT_BYTE;
if (target_clm == 0)
chars_to_delete = 1;
else
{
int actual_clm
= XFASTINT (Fmove_to_column (make_number (target_clm), Qnil));
chars_to_delete = PT - pos;
if (actual_clm > target_clm)
{
spaces_to_insert = actual_clm - target_clm;
}
}
SET_PT_BOTH (pos, pos_byte);
hairy = 2;
}
hairy = 2;
}
if (!NILP (current_buffer->abbrev_mode)
&& SYNTAX (c) != Sword
&& NILP (current_buffer->read_only)
&& PT > BEGV && SYNTAX (XFASTINT (Fprevious_char ())) == Sword)
{
int modiff = MODIFF;
Lisp_Object sym;
sym = Fexpand_abbrev ();
if (! NILP (sym) && ! NILP (XSYMBOL (sym)->function)
&& SYMBOLP (XSYMBOL (sym)->function))
{
Lisp_Object prop;
prop = Fget (XSYMBOL (sym)->function, intern ("no-self-insert"));
if (! NILP (prop))
return 1;
}
if (MODIFF != modiff)
hairy = 2;
}
if (chars_to_delete)
{
string = make_string_from_bytes (str, 1, len);
if (spaces_to_insert)
{
tem = Fmake_string (make_number (spaces_to_insert),
make_number (' '));
string = concat2 (tem, string);
}
replace_range (PT, PT + chars_to_delete, string, 1, 1, 1);
Fforward_char (make_number (1 + spaces_to_insert));
}
else
insert_and_inherit (str, len);
if ((CHAR_TABLE_P (Vauto_fill_chars)
? !NILP (CHAR_TABLE_REF (Vauto_fill_chars, c))
: (c == ' ' || c == '\n'))
&& !noautofill
&& !NILP (current_buffer->auto_fill_function))
{
Lisp_Object tem;
if (c == '\n')
SET_PT_BOTH (PT - 1, PT_BYTE - 1);
tem = call0 (current_buffer->auto_fill_function);
if (c == '\n')
SET_PT_BOTH (PT + 1, PT_BYTE + 1);
if (!NILP (tem))
hairy = 2;
}
if (!NILP (Vself_insert_face)
&& EQ (current_kboard->Vlast_command, Vself_insert_face_command))
{
Fput_text_property (make_number (PT - 1), make_number (PT),
Qface, Vself_insert_face, Qnil);
Vself_insert_face = Qnil;
}
synt = SYNTAX (c);
if ((synt == Sclose || synt == Smath)
&& !NILP (Vblink_paren_function) && INTERACTIVE
&& !noautofill)
{
call0 (Vblink_paren_function);
hairy = 2;
}
return hairy;
}
void
syms_of_cmds ()
{
Qkill_backward_chars = intern ("kill-backward-chars");
staticpro (&Qkill_backward_chars);
Qkill_forward_chars = intern ("kill-forward-chars");
staticpro (&Qkill_forward_chars);
Qoverwrite_mode_binary = intern ("overwrite-mode-binary");
staticpro (&Qoverwrite_mode_binary);
DEFVAR_LISP ("self-insert-face", &Vself_insert_face,
"If non-nil, set the face of the next self-inserting character to this.\n\
See also `self-insert-face-command'.");
Vself_insert_face = Qnil;
DEFVAR_LISP ("self-insert-face-command", &Vself_insert_face_command,
"This is the command that set up `self-insert-face'.\n\
If `last-command' does not equal this value, we ignore `self-insert-face'.");
Vself_insert_face_command = Qnil;
DEFVAR_LISP ("blink-paren-function", &Vblink_paren_function,
"Function called, if non-nil, whenever a close parenthesis is inserted.\n\
More precisely, a char with closeparen syntax is self-inserted.");
Vblink_paren_function = Qnil;
defsubr (&Sforward_point);
defsubr (&Sforward_char);
defsubr (&Sbackward_char);
defsubr (&Sforward_line);
defsubr (&Sbeginning_of_line);
defsubr (&Send_of_line);
defsubr (&Sdelete_char);
defsubr (&Sdelete_backward_char);
defsubr (&Sself_insert_command);
}
void
keys_of_cmds ()
{
int n;
initial_define_key (global_map, Ctl ('I'), "self-insert-command");
for (n = 040; n < 0177; n++)
initial_define_key (global_map, n, "self-insert-command");
#ifdef MSDOS
for (n = 0200; n < 0240; n++)
initial_define_key (global_map, n, "self-insert-command");
#endif
for (n = 0240; n < 0400; n++)
initial_define_key (global_map, n, "self-insert-command");
initial_define_key (global_map, Ctl ('A'), "beginning-of-line");
initial_define_key (global_map, Ctl ('B'), "backward-char");
initial_define_key (global_map, Ctl ('D'), "delete-char");
initial_define_key (global_map, Ctl ('E'), "end-of-line");
initial_define_key (global_map, Ctl ('F'), "forward-char");
initial_define_key (global_map, 0177, "delete-backward-char");
}