Commit dcfdbac7 authored by Jim Blandy's avatar Jim Blandy

Initial revision

parent 8a281f86
/* GNU Emacs case conversion functions.
Copyright (C) 1985 Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "config.h"
#include "lisp.h"
#include "buffer.h"
#include "commands.h"
#include "syntax.h"
enum case_action {CASE_UP, CASE_DOWN, CASE_CAPITALIZE, CASE_CAPITALIZE_UP};
Lisp_Object
casify_object (flag, obj)
enum case_action flag;
Lisp_Object obj;
{
register int i, c, len;
register int inword = flag == CASE_DOWN;
while (1)
{
if (XTYPE (obj) == Lisp_Int)
{
c = XINT (obj);
if (c >= 0 && c <= 0400)
{
if (inword)
XFASTINT (obj) = DOWNCASE (c);
else if (!UPPERCASEP (c))
XFASTINT (obj) = UPCASE1 (c);
}
return obj;
}
if (XTYPE (obj) == Lisp_String)
{
obj = Fcopy_sequence (obj);
len = XSTRING (obj)->size;
for (i = 0; i < len; i++)
{
c = XSTRING (obj)->data[i];
if (inword)
c = DOWNCASE (c);
else if (!UPPERCASEP (c))
c = UPCASE1 (c);
XSTRING (obj)->data[i] = c;
if (flag == CASE_CAPITALIZE)
inword = SYNTAX (c) == Sword;
}
return obj;
}
obj = wrong_type_argument (Qchar_or_string_p, obj, 0);
}
}
DEFUN ("upcase", Fupcase, Supcase, 1, 1, 0,
"Convert argument to upper case and return that.\n\
The argument may be a character or string. The result has the same type.\n\
The argument object is not altered. See also `capitalize'.")
(obj)
Lisp_Object obj;
{
return casify_object (CASE_UP, obj);
}
DEFUN ("downcase", Fdowncase, Sdowncase, 1, 1, 0,
"Convert argument to lower case and return that.\n\
The argument may be a character or string. The result has the same type.\n\
The argument object is not altered.")
(obj)
Lisp_Object obj;
{
return casify_object (CASE_DOWN, obj);
}
DEFUN ("capitalize", Fcapitalize, Scapitalize, 1, 1, 0,
"Convert argument to capitalized form and return that.\n\
This means that each word's first character is upper case\n\
and the rest is lower case.\n\
The argument may be a character or string. The result has the same type.\n\
The argument object is not altered.")
(obj)
Lisp_Object obj;
{
return casify_object (CASE_CAPITALIZE, obj);
}
/* flag is CASE_UP, CASE_DOWN or CASE_CAPITALIZE or CASE_CAPITALIZE_UP.
b and e specify range of buffer to operate on. */
casify_region (flag, b, e)
enum case_action flag;
Lisp_Object b, e;
{
register int i;
register int c;
register int inword = flag == CASE_DOWN;
if (EQ (b, e))
/* Not modifying because nothing marked */
return;
validate_region (&b, &e);
modify_region (XFASTINT (b), XFASTINT (e));
record_change (XFASTINT (b), XFASTINT (e) - XFASTINT (b));
for (i = XFASTINT (b); i < XFASTINT (e); i++)
{
c = FETCH_CHAR (i);
if (inword && flag != CASE_CAPITALIZE_UP)
c = DOWNCASE (c);
else if (!UPPERCASEP (c)
&& (!inword || flag != CASE_CAPITALIZE_UP))
c = UPCASE1 (c);
FETCH_CHAR (i) = c;
if ((int) flag >= (int) CASE_CAPITALIZE)
inword = SYNTAX (c) == Sword;
}
signal_after_change (XFASTINT (b),
XFASTINT (e) - XFASTINT (b),
XFASTINT (e) - XFASTINT (b));
}
DEFUN ("upcase-region", Fupcase_region, Supcase_region, 2, 2, "r",
"Convert the region to upper case. In programs, wants two arguments.\n\
These arguments specify the starting and ending character numbers of\n\
the region to operate on. When used as a command, the text between\n\
point and the mark is operated on.\n\
See also `capitalize-region'.")
(b, e)
Lisp_Object b, e;
{
casify_region (CASE_UP, b, e);
return Qnil;
}
DEFUN ("downcase-region", Fdowncase_region, Sdowncase_region, 2, 2, "r",
"Convert the region to lower case. In programs, wants two arguments.\n\
These arguments specify the starting and ending character numbers of\n\
the region to operate on. When used as a command, the text between\n\
point and the mark is operated on.")
(b, e)
Lisp_Object b, e;
{
casify_region (CASE_DOWN, b, e);
return Qnil;
}
DEFUN ("capitalize-region", Fcapitalize_region, Scapitalize_region, 2, 2, "r",
"Convert the region to capitalized form.\n\
Capitalized form means each word's first character is upper case\n\
and the rest of it is lower case.\n\
In programs, give two arguments, the starting and ending\n\
character positions to operate on.")
(b, e)
Lisp_Object b, e;
{
casify_region (CASE_CAPITALIZE, b, e);
return Qnil;
}
/* Like Fcapitalize but change only the initials. */
Lisp_Object
upcase_initials_region (b, e)
Lisp_Object b, e;
{
casify_region (CASE_CAPITALIZE_UP, b, e);
return Qnil;
}
Lisp_Object
operate_on_word (arg)
Lisp_Object arg;
{
Lisp_Object val, end;
int farend;
CHECK_NUMBER (arg, 0);
farend = scan_words (point, XINT (arg));
if (!farend)
farend = XINT (arg) > 0 ? ZV : BEGV;
end = point > farend ? point : farend;
SET_PT (end);
XFASTINT (val) = farend;
return val;
}
DEFUN ("upcase-word", Fupcase_word, Supcase_word, 1, 1, "p",
"Convert following word (or ARG words) to upper case, moving over.\n\
With negative argument, convert previous words but do not move.\n\
See also `capitalize-word'.")
(arg)
Lisp_Object arg;
{
Lisp_Object opoint;
XFASTINT (opoint) = point;
casify_region (CASE_UP, opoint, operate_on_word (arg));
return Qnil;
}
DEFUN ("downcase-word", Fdowncase_word, Sdowncase_word, 1, 1, "p",
"Convert following word (or ARG words) to lower case, moving over.\n\
With negative argument, convert previous words but do not move.")
(arg)
Lisp_Object arg;
{
Lisp_Object opoint;
XFASTINT (opoint) = point;
casify_region (CASE_DOWN, opoint, operate_on_word (arg));
return Qnil;
}
DEFUN ("capitalize-word", Fcapitalize_word, Scapitalize_word, 1, 1, "p",
"Capitalize the following word (or ARG words), moving over.\n\
This gives the word(s) a first character in upper case\n\
and the rest lower case.\n\
With negative argument, capitalize previous words but do not move.")
(arg)
Lisp_Object arg;
{
Lisp_Object opoint;
XFASTINT (opoint) = point;
casify_region (CASE_CAPITALIZE, opoint, operate_on_word (arg));
return Qnil;
}
syms_of_casefiddle ()
{
defsubr (&Supcase);
defsubr (&Sdowncase);
defsubr (&Scapitalize);
defsubr (&Supcase_region);
defsubr (&Sdowncase_region);
defsubr (&Scapitalize_region);
defsubr (&Supcase_word);
defsubr (&Sdowncase_word);
defsubr (&Scapitalize_word);
}
keys_of_casefiddle ()
{
initial_define_key (control_x_map, Ctl('U'), "upcase-region");
initial_define_key (control_x_map, Ctl('L'), "downcase-region");
initial_define_key (meta_map, 'u', "upcase-word");
initial_define_key (meta_map, 'l', "downcase-word");
initial_define_key (meta_map, 'c', "capitalize-word");
}
/* GNU Emacs routines to deal with case tables.
Copyright (C) 1987 Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
/* Written by Howard Gayle. See chartab.c for details. */
#include "config.h"
#include "lisp.h"
#include "buffer.h"
Lisp_Object Qcase_table_p;
Lisp_Object Vascii_downcase_table, Vascii_upcase_table;
Lisp_Object Vascii_canon_table, Vascii_eqv_table;
void compute_trt_inverse ();
DEFUN ("case-table-p", Fcase_table_p, Scase_table_p, 1, 1, 0,
"Return t iff ARG is a case table.\n\
See `set-case-table' for more information on these data structures.")
(table)
Lisp_Object table;
{
Lisp_Object down, up, canon, eqv;
down = Fcar_safe (table);
up = Fcar_safe (Fcdr_safe (table));
canon = Fcar_safe (Fcdr_safe (Fcdr_safe (table)));
eqv = Fcar_safe (Fcdr_safe (Fcdr_safe (Fcdr_safe (table))));
#define STRING256_P(obj) \
(XTYPE (obj) == Lisp_String && XSTRING (obj)->size == 256)
return (STRING256_P (down)
&& (NULL (up) || STRING256_P (up))
&& ((NULL (canon) && NULL (eqv))
|| (STRING256_P (canon) && STRING256_P (eqv)))
? Qt : Qnil);
}
static Lisp_Object
check_case_table (obj)
Lisp_Object obj;
{
register Lisp_Object tem;
while (tem = Fcase_table_p (obj), NULL (tem))
obj = wrong_type_argument (Qcase_table_p, obj, 0);
return (obj);
}
DEFUN ("current-case-table", Fcurrent_case_table, Scurrent_case_table, 0, 0, 0,
"Return the case table of the current buffer.")
()
{
Lisp_Object down, up, canon, eqv;
down = current_buffer->downcase_table;
up = current_buffer->upcase_table;
canon = current_buffer->case_canon_table;
eqv = current_buffer->case_eqv_table;
return Fcons (down, Fcons (up, Fcons (canon, Fcons (eqv, Qnil))));
}
DEFUN ("standard-case-table", Fstandard_case_table,
Sstandard_case_table, 0, 0, 0,
"Return the standard case table.\n\
This is the one used for new buffers.")
()
{
return Fcons (Vascii_downcase_table,
Fcons (Vascii_upcase_table,
Fcons (Vascii_canon_table,
Fcons (Vascii_eqv_table, Qnil))));
}
DEFUN ("set-case-table", Fset_case_table, Sset_case_table, 1, 1, 0,
"Select a new case table for the current buffer.\n\
A case table is a list (DOWNCASE UPCASE CANONICALIZE EQUIVALENCES)\n\
where each element is either nil or a string of length 256.\n\
DOWNCASE maps each character to its lower-case equivalent.\n\
UPCASE maps each character to its upper-case equivalent;\n\
if lower and upper case characters are in 1-1 correspondence,\n\
you may use nil and the upcase table will be deduced from DOWNCASE.\n\
CANONICALIZE maps each character to a canonical equivalent;\n\
any two characters that are related by case-conversion have the same\n\
canonical equivalent character.\n\
EQUIVALENCES is a map that cyclicly permutes each equivalence class\n\
(of characters with the same canonical equivalent).\n\
Both CANONICALIZE and EQUIVALENCES may be nil, in which case\n\
both are deduced from DOWNCASE and UPCASE.")
(table)
Lisp_Object table;
{
set_case_table (table, 0);
}
DEFUN ("set-standard-case-table",
Fset_standard_case_table, Sset_standard_case_table, 1, 1, 0,
"Select a new standard case table for new buffers.\n\
See `set-case-table' for more info on case tables.")
(table)
Lisp_Object table;
{
set_case_table (table, 1);
}
set_case_table (table, standard)
Lisp_Object table;
int standard;
{
Lisp_Object down, up, canon, eqv;
check_case_table (table);
down = Fcar_safe (table);
up = Fcar_safe (Fcdr_safe (table));
canon = Fcar_safe (Fcdr_safe (Fcdr_safe (table)));
eqv = Fcar_safe (Fcdr_safe (Fcdr_safe (Fcdr_safe (table))));
if (NULL (up))
{
up = Fmake_string (make_number (256), make_number (0));
compute_trt_inverse (XSTRING (down)->data, XSTRING (up)->data);
}
if (NULL (canon))
{
register int i;
unsigned char *upvec = XSTRING (up)->data;
unsigned char *downvec = XSTRING (down)->data;
canon = Fmake_string (make_number (256), make_number (0));
eqv = Fmake_string (make_number (256), make_number (0));
/* Set up the CANON vector; for each character,
this sequence of upcasing and downcasing ought to
get the "preferred" lowercase equivalent. */
for (i = 0; i < 256; i++)
XSTRING (canon)->data[i] = downvec[upvec[downvec[i]]];
compute_trt_inverse (XSTRING (canon)->data, XSTRING (eqv)->data);
}
if (standard)
{
Vascii_downcase_table = down;
Vascii_upcase_table = up;
Vascii_canon_table = canon;
Vascii_eqv_table = eqv;
}
else
{
current_buffer->downcase_table = down;
current_buffer->upcase_table = up;
current_buffer->case_canon_table = canon;
current_buffer->case_eqv_table = eqv;
}
return table;
}
/* Given a translate table TRT, store the inverse mapping into INVERSE.
Since TRT is not one-to-one, INVERSE is not a simple mapping.
Instead, it divides the space of characters into equivalence classes.
All characters in a given class form one circular list, chained through
the elements of INVERSE. */
void
compute_trt_inverse (trt, inverse)
register unsigned char *trt;
register unsigned char *inverse;
{
register int i = 0400;
register unsigned char c, q;
while (i--)
inverse[i] = i;
i = 0400;
while (i--)
{
if ((q = trt[i]) != (unsigned char) i)
{
c = inverse[q];
inverse[q] = i;
inverse[i] = c;
}
}
}
init_casetab_once ()
{
register int i;
Lisp_Object tem;
tem = Fmake_string (make_number (256), make_number (0));
Vascii_downcase_table = tem;
Vascii_canon_table = tem;
for (i = 0; i < 256; i++)
XSTRING (tem)->data[i] = (i >= 'A' && i <= 'Z') ? i + 040 : i;
tem = Fmake_string (make_number (256), make_number (0));
Vascii_upcase_table = tem;
Vascii_eqv_table = tem;
for (i = 0; i < 256; i++)
XSTRING (tem)->data[i]
= ((i >= 'A' && i <= 'Z')
? i + ('a' - 'A')
: ((i >= 'a' && i <= 'z')
? i + ('A' - 'a')
: i));
}
syms_of_casetab ()
{
Qcase_table_p = intern ("case-table-p");
staticpro (&Qcase_table_p);
staticpro (&Vascii_downcase_table);
staticpro (&Vascii_upcase_table);
staticpro (&Vascii_canon_table);
staticpro (&Vascii_eqv_table);
defsubr (&Scase_table_p);
defsubr (&Scurrent_case_table);
defsubr (&Sstandard_case_table);
defsubr (&Sset_case_table);
defsubr (&Sset_standard_case_table);
#if 0
DEFVAR_LISP ("ascii-downcase-table", &Vascii_downcase_table,
"String mapping ASCII characters to lowercase equivalents.");
DEFVAR_LISP ("ascii-upcase-table", &Vascii_upcase_table,
"String mapping ASCII characters to uppercase equivalents.");
#endif
}
/* Markers: examining, setting and killing.
Copyright (C) 1985 Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "config.h"
#include "lisp.h"
#include "buffer.h"
/* Operations on markers. */
DEFUN ("marker-buffer", Fmarker_buffer, Smarker_buffer, 1, 1, 0,
"Return the buffer that MARKER points into, or nil if none.\n\
Returns nil if MARKER points into a dead buffer.")
(marker)
register Lisp_Object marker;
{
register Lisp_Object buf;
CHECK_MARKER (marker, 0);
if (XMARKER (marker)->buffer)
{
XSET (buf, Lisp_Buffer, XMARKER (marker)->buffer);
/* Return marker's buffer only if it is not dead. */
if (!NULL (XBUFFER (buf)->name))
return buf;
}
return Qnil;
}
DEFUN ("marker-position", Fmarker_position, Smarker_position, 1, 1, 0,
"Return the position MARKER points at, as a character number.")
(marker)
Lisp_Object marker;
{
register Lisp_Object pos;
register int i;
register struct buffer *buf;
CHECK_MARKER (marker, 0);
if (XMARKER (marker)->buffer)
{
buf = XMARKER (marker)->buffer;
i = XMARKER (marker)->bufpos;
if (i > BUF_GPT (buf) + BUF_GAP_SIZE (buf))
i -= BUF_GAP_SIZE (buf);
else if (i > BUF_GPT (buf))
i = BUF_GPT (buf);
if (i < BUF_BEG (buf) || i > BUF_Z (buf))
abort ();
XFASTINT (pos) = i;
return pos;
}
return Qnil;
}
DEFUN ("set-marker", Fset_marker, Sset_marker, 2, 3, 0,
"Position MARKER before character number NUMBER in BUFFER.\n\
BUFFER defaults to the current buffer.\n\
If NUMBER is nil, makes marker point nowhere.\n\
Then it no longer slows down editing in any buffer.\n\
Returns MARKER.")
(marker, pos, buffer)
Lisp_Object marker, pos, buffer;
{
register int charno;
register struct buffer *b;
register struct Lisp_Marker *m;
CHECK_MARKER (marker, 0);
/* If position is nil or a marker that points nowhere,
make this marker point nowhere. */
if (NULL (pos)
|| (XTYPE (pos) == Lisp_Marker && !XMARKER (pos)->buffer))
{
unchain_marker (marker);
return marker;
}
CHECK_NUMBER_COERCE_MARKER (pos, 1);
if (NULL (buffer))
b = current_buffer;
else
{
CHECK_BUFFER (buffer, 1);
b = XBUFFER (buffer);
/* If buffer is dead, set marker to point nowhere. */
if (EQ (b->name, Qnil))
{
unchain_marker (marker);
return marker;
}
}
charno = XINT (pos);
m = XMARKER (marker);
if (charno < BUF_BEG (b))
charno = BUF_BEG (b);
if (charno > BUF_Z (b))
charno = BUF_Z (b);
if (charno > BUF_GPT (b)) charno += BUF_GAP_SIZE (b);
m->bufpos = charno;
if (m->buffer != b)
{
unchain_marker (marker);
m->chain = b->markers;
b->markers = marker;
m->buffer = b;
}
return marker;
}
/* This version of Fset_marker won't let the position
be outside the visible part. */
Lisp_Object
set_marker_restricted (marker, pos, buffer)
<