Check for ncurses.
[bpt/emacs.git] / src / casefiddle.c
index 5d00a2b..a630025 100644 (file)
@@ -5,7 +5,7 @@ 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)
+the Free Software Foundation; either version 2, or (at your option)
 any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
@@ -15,7 +15,8 @@ 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.  */
+the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
 
 
 #include <config.h>
@@ -36,31 +37,32 @@ casify_object (flag, obj)
 
   while (1)
     {
-      if (XTYPE (obj) == Lisp_Int)
+      if (INTEGERP (obj))
        {
          c = XINT (obj);
          if (c >= 0 && c <= 0400)
            {
              if (inword)
-               XFASTINT (obj) = DOWNCASE (c);
+               XSETFASTINT (obj, DOWNCASE (c));
              else if (!UPPERCASEP (c))
-               XFASTINT (obj) = UPCASE1 (c);
+               XSETFASTINT (obj, UPCASE1 (c));
            }
          return obj;
        }
-      if (XTYPE (obj) == Lisp_String)
+      if (STRINGP (obj))
        {
          obj = Fcopy_sequence (obj);
          len = XSTRING (obj)->size;
          for (i = 0; i < len; i++)
            {
              c = XSTRING (obj)->data[i];
-             if (inword)
+             if (inword && flag != CASE_CAPITALIZE_UP)
                c = DOWNCASE (c);
-             else if (!UPPERCASEP (c))
+             else if (!UPPERCASEP (c)
+                      && (!inword || flag != CASE_CAPITALIZE_UP))
                c = UPCASE1 (c);
              XSTRING (obj)->data[i] = c;
-             if (flag == CASE_CAPITALIZE)
+             if ((int) flag >= (int) CASE_CAPITALIZE)
                inword = SYNTAX (c) == Sword;
            }
          return obj;
@@ -72,7 +74,8 @@ casify_object (flag, obj)
 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'.")
+The argument object is not altered--the value is a copy.\n\
+See also `capitalize', `downcase' and `upcase-initials'.")
   (obj)
      Lisp_Object obj;
 {
@@ -82,7 +85,7 @@ The argument object is not altered.  See also `capitalize'.")
 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.")
+The argument object is not altered--the value is a copy.")
   (obj)
      Lisp_Object obj;
 {
@@ -94,12 +97,25 @@ DEFUN ("capitalize", Fcapitalize, Scapitalize, 1, 1, 0,
 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.")
+The argument object is not altered--the value is a copy.")
   (obj)
      Lisp_Object obj;
 {
   return casify_object (CASE_CAPITALIZE, obj);
 }
+
+/* Like Fcapitalize but change only the initials.  */
+
+DEFUN ("upcase-initials", Fupcase_initials, Supcase_initials, 1, 1, 0,
+  "Convert the initial of each word in the argument to upper case.\n\
+Do not change the other letters of each word.\n\
+The argument may be a character or string.  The result has the same type.\n\
+The argument object is not altered--the value is a copy.")
+  (obj)
+     Lisp_Object obj;
+{
+  return casify_object (CASE_CAPITALIZE_UP, obj);
+}
 \f
 /* flag is CASE_UP, CASE_DOWN or CASE_CAPITALIZE or CASE_CAPITALIZE_UP.
    b and e specify range of buffer to operate on. */
@@ -111,16 +127,19 @@ casify_region (flag, b, e)
   register int i;
   register int c;
   register int inword = flag == CASE_DOWN;
+  int start, end;
 
   if (EQ (b, e))
     /* Not modifying because nothing marked */
     return;
 
   validate_region (&b, &e);
-  modify_region (current_buffer, XFASTINT (b), XFASTINT (e));
-  record_change (XFASTINT (b), XFASTINT (e) - XFASTINT (b));
+  start = XFASTINT (b);
+  end = XFASTINT (e);
+  modify_region (current_buffer, start, end);
+  record_change (start, end - start);
 
-  for (i = XFASTINT (b); i < XFASTINT (e); i++)
+  for (i = start; i < end; i++)
     {
       c = FETCH_CHAR (i);
       if (inword && flag != CASE_CAPITALIZE_UP)
@@ -133,9 +152,7 @@ casify_region (flag, b, e)
        inword = SYNTAX (c) == Sword;
     }
 
-  signal_after_change (XFASTINT (b),
-                      XFASTINT (e) - XFASTINT (b), 
-                      XFASTINT (e) - XFASTINT (b));
+  signal_after_change (start, end - start, end - start);
 }
 
 DEFUN ("upcase-region", Fupcase_region, Supcase_region, 2, 2, "r",
@@ -144,10 +161,10 @@ 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;
+  (beg, end)
+     Lisp_Object beg, end;
 {
-  casify_region (CASE_UP, b, e);
+  casify_region (CASE_UP, beg, end);
   return Qnil;
 }
 
@@ -156,10 +173,10 @@ DEFUN ("downcase-region", Fdowncase_region, Sdowncase_region, 2, 2, "r",
 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;
+  (beg, end)
+     Lisp_Object beg, end;
 {
-  casify_region (CASE_DOWN, b, e);
+  casify_region (CASE_DOWN, beg, end);
   return Qnil;
 }
 
@@ -169,20 +186,25 @@ 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;
+  (beg, end)
+     Lisp_Object beg, end;
 {
-  casify_region (CASE_CAPITALIZE, b, e);
+  casify_region (CASE_CAPITALIZE, beg, end);
   return Qnil;
 }
 
-/* Like Fcapitalize but change only the initials.  */
+/* Like Fcapitalize_region but change only the initials.  */
 
-Lisp_Object
-upcase_initials_region (b, e)
-     Lisp_Object b, e;
+DEFUN ("upcase-initials-region", Fupcase_initials_region,
+       Supcase_initials_region, 2, 2, "r",
+  "Upcase the initial of each word in the region.\n\
+Subsequent letters of each word are not changed.\n\
+In programs, give two arguments, the starting and ending\n\
+character positions to operate on.")
+  (beg, end)
+     Lisp_Object beg, end;
 {
-  casify_region (CASE_CAPITALIZE_UP, b, e);
+  casify_region (CASE_CAPITALIZE_UP, beg, end);
   return Qnil;
 }
 \f
@@ -193,14 +215,16 @@ operate_on_word (arg, newpoint)
 {
   Lisp_Object val;
   int farend;
+  int iarg;
 
   CHECK_NUMBER (arg, 0);
-  farend = scan_words (point, XINT (arg));
+  iarg = XINT (arg);
+  farend = scan_words (point, iarg);
   if (!farend)
-    farend = XINT (arg) > 0 ? ZV : BEGV;
+    farend = iarg > 0 ? ZV : BEGV;
 
   *newpoint = point > farend ? point : farend;
-  XFASTINT (val) = farend;
+  XSETFASTINT (val, farend);
 
   return val;
 }
@@ -214,7 +238,7 @@ See also `capitalize-word'.")
 {
   Lisp_Object beg, end;
   int newpoint;
-  XFASTINT (beg) = point;
+  XSETFASTINT (beg, point);
   end = operate_on_word (arg, &newpoint);
   casify_region (CASE_UP, beg, end);
   SET_PT (newpoint);
@@ -229,7 +253,7 @@ With negative argument, convert previous words but do not move.")
 {
   Lisp_Object beg, end;
   int newpoint;
-  XFASTINT (beg) = point;
+  XSETFASTINT (beg, point);
   end = operate_on_word (arg, &newpoint);
   casify_region (CASE_DOWN, beg, end);
   SET_PT (newpoint);
@@ -246,7 +270,7 @@ With negative argument, capitalize previous words but do not move.")
 {
   Lisp_Object beg, end;
   int newpoint;
-  XFASTINT (beg) = point;
+  XSETFASTINT (beg, point);
   end = operate_on_word (arg, &newpoint);
   casify_region (CASE_CAPITALIZE, beg, end);
   SET_PT (newpoint);
@@ -258,9 +282,11 @@ syms_of_casefiddle ()
   defsubr (&Supcase);
   defsubr (&Sdowncase);
   defsubr (&Scapitalize);
+  defsubr (&Supcase_initials);
   defsubr (&Supcase_region);
   defsubr (&Sdowncase_region);
   defsubr (&Scapitalize_region);
+  defsubr (&Supcase_initials_region);
   defsubr (&Supcase_word);
   defsubr (&Sdowncase_word);
   defsubr (&Scapitalize_word);