home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / utility / text / emacssrc.lha / emacs-18.58 / src / doc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-26  |  12.0 KB  |  439 lines

  1. /* Record indices of function doc strings stored in a file.
  2.    Copyright (C) 1985, 1986 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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "config.h"
  22. #include "lisp.h"
  23. #include "buffer.h"
  24.  
  25. #include <sys/types.h>
  26. #include <sys/file.h>    /* Must be after sys/types.h for USG and BSD4_1*/
  27.  
  28. #ifdef USG5
  29. #include <fcntl.h>
  30. #endif
  31.  
  32. #ifndef O_RDONLY
  33. #define O_RDONLY 0
  34. #endif
  35.  
  36. Lisp_Object Vdoc_file_name;
  37.  
  38. Lisp_Object
  39. get_doc_string (filepos)
  40.      long filepos;
  41. {
  42.   char buf[512 * 32 + 1];
  43.   register int fd;
  44.   register char *name;
  45.   register char *p, *p1;
  46.   register int count;
  47.   extern char *index ();
  48.  
  49.   if (XTYPE (Vexec_directory) != Lisp_String
  50.       || XTYPE (Vdoc_file_name) != Lisp_String)
  51.     return Qnil;
  52.  
  53.   name = (char *) alloca (XSTRING (Vexec_directory)->size
  54.               + XSTRING (Vdoc_file_name)->size + 8);
  55.   strcpy (name, XSTRING (Vexec_directory)->data);
  56.   strcat (name, XSTRING (Vdoc_file_name)->data);
  57. #ifdef VMS
  58. #ifndef VMS4_4
  59.   /* For VMS versions with limited file name syntax,
  60.      convert the name to something VMS will allow.  */
  61.   p = name;
  62.   while (*p)
  63.     {
  64.       if (*p == '-')
  65.     *p = '_';
  66.       p++;
  67.     }
  68. #endif /* not VMS4_4 */
  69. #ifdef VMS4_4
  70.   strcpy (name, sys_translate_unix (name));
  71. #endif /* VMS4_4 */
  72. #endif /* VMS */
  73.  
  74.   fd = open (name, O_RDONLY, 0);
  75.   if (fd < 0)
  76.     error ("Cannot open doc string file \"%s\"", name);
  77.   if (0 > lseek (fd, filepos, 0))
  78.     {
  79.       close (fd);
  80.       error ("Position %ld out of range in doc string file \"%s\"",
  81.          filepos, name);
  82.     }
  83.   p = buf;
  84.   while (p != buf + sizeof buf - 1)
  85.     {
  86.       count = read (fd, p, 512);
  87.       p[count] = 0;
  88.       if (!count)
  89.     break;
  90.       p1 = index (p, '\037');
  91.       if (p1)
  92.     {
  93.       *p1 = 0;
  94.       p = p1;
  95.       break;
  96.     }
  97.       p += count;
  98.     }
  99.   close (fd);
  100.   return make_string (buf, p - buf);
  101. }
  102.  
  103. DEFUN ("documentation", Fdocumentation, Sdocumentation, 1, 1, 0,
  104.   "Return the documentation string of FUNCTION.")
  105.   (fun1)
  106.      Lisp_Object fun1;
  107. {
  108.   Lisp_Object fun;
  109.   Lisp_Object funcar;
  110.   Lisp_Object tem;
  111.  
  112.   fun = fun1;
  113.   while (XTYPE (fun) == Lisp_Symbol)
  114.     fun = Fsymbol_function (fun);
  115.   if (XTYPE (fun) == Lisp_Subr)
  116.     {
  117.       if (XSUBR (fun)->doc == 0) return Qnil;
  118.       if ((int) XSUBR (fun)->doc >= 0)
  119.     return Fsubstitute_command_keys (build_string (XSUBR (fun)->doc));
  120.       return Fsubstitute_command_keys (get_doc_string (- (int) XSUBR (fun)->doc));
  121.     }
  122.   if (XTYPE (fun) == Lisp_Vector)
  123.     return build_string ("Prefix command (definition is a Lisp vector of subcommands).");
  124.   if (XTYPE (fun) == Lisp_String)
  125.     return build_string ("Keyboard macro.");
  126.   if (!CONSP (fun))
  127.     return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
  128.   funcar = Fcar (fun);
  129.   if (XTYPE (funcar) != Lisp_Symbol)
  130.     return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
  131.   if (XSYMBOL (funcar) == XSYMBOL (Qkeymap))
  132.     return build_string ("Prefix command (definition is a list whose cdr is an alist of subcommands.)");
  133.   if (XSYMBOL (funcar) == XSYMBOL (Qlambda)
  134.       || XSYMBOL (funcar) == XSYMBOL (Qautoload))
  135.     {
  136.       tem = Fcar (Fcdr (Fcdr (fun)));
  137.       if (XTYPE (tem) == Lisp_String)
  138.     return Fsubstitute_command_keys (tem);
  139.       if (XTYPE (tem) == Lisp_Int && XINT (tem) >= 0)
  140.     return Fsubstitute_command_keys (get_doc_string (XFASTINT (tem)));
  141.       return Qnil;
  142.     }
  143.   if (XSYMBOL (funcar) == XSYMBOL (Qmocklisp))
  144.     return Qnil;
  145.   if (XSYMBOL (funcar) == XSYMBOL (Qmacro))
  146.     return Fdocumentation (Fcdr (fun));
  147.   else
  148.     return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
  149. }
  150.  
  151. DEFUN ("documentation-property", Fdocumentation_property, 
  152.        Sdocumentation_property, 2, 2, 0,
  153.   "Return the documentation string that is SYMBOL's PROP property.\n\
  154. This differs from using `get' only in that it can refer to strings\n\
  155. stored in the etc/DOC file.")
  156.   (sym, prop)
  157.      Lisp_Object sym, prop;
  158. {
  159.   register Lisp_Object tem;
  160.  
  161.   tem = Fget (sym, prop);
  162.   if (XTYPE (tem) == Lisp_Int)
  163.     tem = get_doc_string (XINT (tem) > 0 ? XINT (tem) : - XINT (tem));
  164.   return Fsubstitute_command_keys (tem);
  165. }
  166.  
  167. DEFUN ("Snarf-documentation", Fsnarf_documentation, Ssnarf_documentation,
  168.   1, 1, 0,
  169.   "Used during Emacs initialization, before dumping runnable Emacs,\n\
  170. to find pointers to doc strings stored in etc/DOC... and\n\
  171. record them in function definitions.\n\
  172. One arg, FILENAME, a string which does not include a directory.\n\
  173. The file is found in ../etc now; found in the exec-directory\n\
  174. when doc strings are referred to later in the dumped Emacs.")
  175.   (filename)
  176.      Lisp_Object filename;
  177. {
  178.   int fd;
  179.   char buf[1024 + 1];
  180.   register int filled;
  181.   register int pos;
  182.   register char *p, *end;
  183.   Lisp_Object sym, fun, tem;
  184.   char *name;
  185.   extern char *index ();
  186.  
  187.   CHECK_STRING (filename, 0);
  188.  
  189. #ifndef CANNOT_DUMP
  190.   name = (char *) alloca (XSTRING (filename)->size + 8);
  191. #ifdef AMIGA
  192.   strcpy (name, "/etc/");
  193. #else
  194.   strcpy (name, "../etc/");
  195. #endif
  196. #else /* CANNOT_DUMP */
  197.   CHECK_STRING (Vexec_directory, 0);
  198.   name = (char *) alloca (XSTRING (filename)->size +
  199.               XSTRING (Vexec_directory)->size + 1);
  200.   strcpy (name, XSTRING (Vexec_directory)->data);
  201. #endif /* CANNOT_DUMP */
  202.   strcat (name, XSTRING (filename)->data);     /*** Add this line ***/
  203. #ifdef VMS
  204. #ifndef VMS4_4
  205.   /* For VMS versions with limited file name syntax,
  206.      convert the name to something VMS will allow.  */
  207.   p = name;
  208.   while (*p)
  209.     {
  210.       if (*p == '-')
  211.     *p = '_';
  212.       p++;
  213.     }
  214. #endif /* not VMS4_4 */
  215. #ifdef VMS4_4
  216.   strcpy (name, sys_translate_unix (name));
  217. #endif /* VMS4_4 */
  218. #endif /* VMS */
  219.  
  220.   fd = open (name, O_RDONLY, 0);
  221.   if (fd < 0)
  222.     report_file_error ("Opening doc string file",
  223.                Fcons (build_string (name), Qnil));
  224.   Vdoc_file_name = filename;
  225.   filled = 0;
  226.   pos = 0;
  227.   while (1)
  228.     {
  229.       if (filled < 512)
  230.     filled += read (fd, &buf[filled], sizeof buf - 1 - filled);
  231.       if (!filled)
  232.     break;
  233.  
  234.       buf[filled] = 0;
  235.       p = buf;
  236.       end = buf + (filled < 512 ? filled : filled - 128);
  237.       while (p != end && *p != '\037') p++;
  238.       /* p points to ^_Ffunctionname\n or ^_Vvarname\n.  */
  239.       if (p != end)
  240.     {
  241.       end = index (p, '\n');
  242.       sym = oblookup (Vobarray, p + 2, end - p - 2);
  243.       if (XTYPE (sym) == Lisp_Symbol)
  244.         {
  245.           if (p[1] == 'V')
  246.         {
  247.           /* Install file-position as variable-documentation property
  248.              and make it negative for a user-variable
  249.              (doc starts with a `*').  */
  250.           Fput (sym, Qvariable_documentation,
  251.             make_number ((pos + end + 1 - buf)
  252.                      * (end[1] == '*' ? -1 : 1)));
  253.         }
  254.           else if (p[1] == 'F')
  255.         {
  256.           fun = XSYMBOL (sym)->function;
  257.           if (XTYPE (fun) == Lisp_Subr)
  258.             XSUBR (fun)->doc = (char *) - (pos + end + 1 - buf);
  259.           else if (CONSP (fun))
  260.             {
  261.               tem = XCONS (fun)->car;
  262.               if (EQ (tem, Qlambda) || EQ (tem, Qautoload))
  263.             {
  264.               tem = Fcdr (Fcdr (fun));
  265.               if (CONSP (tem) &&
  266.                   XTYPE (XCONS (tem)->car) == Lisp_Int)
  267.                 XFASTINT (XCONS (tem)->car) = (pos + end + 1 - buf);
  268.             }
  269.             }
  270.         }
  271.           else error ("DOC file invalid at position %d", pos);
  272.         }
  273.     }
  274.       pos += end - buf;
  275.       filled -= end - buf;
  276.       bcopy (end, buf, filled);
  277.     }
  278.   close (fd);
  279.   return Qnil;
  280. }
  281.  
  282. DEFUN ("substitute-command-keys", Fsubstitute_command_keys,
  283.   Ssubstitute_command_keys, 1, 1, 0,
  284.   "Return the STRING with substrings of the form \\=\\[COMMAND]\n\
  285. replaced by either:  a keystroke sequence that will invoke COMMAND,\n\
  286. or \"M-x COMMAND\" if COMMAND is not on any keys.\n\
  287. Substrings of the form \\=\\{MAPVAR} are replaced by summaries\n\
  288. \(made by describe-bindings) of the value of MAPVAR, taken as a keymap.\n\
  289. Substrings of the form \\=\\<MAPVAR> specify to use the value of MAPVAR\n\
  290. as the keymap for future \\=\\[COMMAND] substrings.\n\
  291. \\=\\= quotes the following character and is discarded;\n\
  292. thus, \\=\\=\\=\\= puts \\=\\= into the output, and \\=\\=\\=\\[ puts \\=\\[ into the output.")
  293.   (str)
  294.      Lisp_Object str;
  295. {
  296.   unsigned char *buf;
  297.   int changed = 0;
  298.   register unsigned char *strp;
  299.   register unsigned char *bufp;
  300.   int idx;
  301.   int bsize;
  302.   unsigned char *new;
  303.   register Lisp_Object tem;
  304.   Lisp_Object keymap;
  305.   unsigned char *start;
  306.   int length;
  307.   struct gcpro gcpro1; 
  308.  
  309.   if (NULL (str))
  310.     return Qnil;
  311.  
  312.   CHECK_STRING (str, 0);
  313.   GCPRO1 (str);
  314.  
  315.   keymap = current_buffer->keymap;
  316.  
  317.   bsize = XSTRING (str)->size;
  318.   bufp = buf = (unsigned char *) xmalloc (bsize);
  319.  
  320.   strp = (unsigned char *) XSTRING (str)->data;
  321.   while (strp - (unsigned char *) XSTRING (str)->data < XSTRING (str)->size)
  322.     {
  323.       if (strp[0] == '\\' && strp[1] == '=')
  324.     {
  325.       /* \= quotes the next character;
  326.          thus, to put in \[ without its special meaning, use \=\[.  */
  327.       changed = 1;
  328.       *bufp++ = strp[2];
  329.       strp += 3;
  330.     }
  331.       else if (strp[0] == '\\' && strp[1] == '[')
  332.     {
  333.       changed = 1;
  334.       strp += 2;        /* skip \[ */
  335.       start = strp;
  336.  
  337.       while (strp - (unsigned char *) XSTRING (str)->data < XSTRING (str)->size
  338.          && *strp != ']')
  339.         strp++;
  340.       length = strp - start;
  341.       strp++;        /* skip ] */
  342.  
  343.       /* Save STRP in IDX.  */
  344.       idx = strp - (unsigned char *) XSTRING (str)->data;
  345.       tem = Fintern (make_string (start, length), Qnil);
  346.       tem = Fwhere_is_internal (tem, keymap, Qt);
  347.  
  348.       if (NULL (tem))    /* but not on any keys */
  349.         {
  350.           new = (unsigned char *) xrealloc (buf, bsize += 4);
  351.           bufp += new - buf;
  352.           buf = new;
  353.           bcopy ("M-x ", bufp, 4);
  354.           bufp += 4;
  355.           goto subst;
  356.         }
  357.       else
  358.         {            /* function is on a key */
  359.           tem = Fkey_description (tem);
  360.           goto subst_string;
  361.         }
  362.     }
  363.       /* \{foo} is replaced with a summary of the keymap (symeval foo).
  364.      \<foo> just sets the keymap used for \[cmd].  */
  365.       else if (strp[0] == '\\' && (strp[1] == '{' || strp[1] == '<'))
  366.     {
  367.       struct buffer *oldbuf;
  368.       Lisp_Object name;
  369.  
  370.       changed = 1;
  371.       strp += 2;        /* skip \{ or \< */
  372.       start = strp;
  373.  
  374.       while (strp - (unsigned char *) XSTRING (str)->data < XSTRING (str)->size
  375.          && *strp != '}' && *strp != '>')
  376.         strp++;
  377.       length = strp - start;
  378.       strp++;            /* skip } or > */
  379.  
  380.       /* Save STRP in IDX.  */
  381.       idx = strp - (unsigned char *) XSTRING (str)->data;
  382.  
  383.       oldbuf = current_buffer;
  384.       set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
  385.       name = Fintern (make_string (start, length), Qnil);
  386.       if ((tem = (Fboundp (name)), NULL (tem)) ||
  387.           (tem = (Fsymbol_value (name)), NULL (tem)) ||
  388.           (tem = (get_keymap_1 (tem, 0)), NULL (tem)))
  389.         {
  390.           name = Fsymbol_name (name);
  391.           InsStr ("\nUses keymap \"");
  392.           insert (XSTRING (name)->data, XSTRING (name)->size);
  393.           InsStr ("\", which is not currently defined.\n");
  394.           if (start[-1] == '<') keymap = Qnil;
  395.         }
  396.       else if (start[-1] == '<')
  397.         keymap = tem;
  398.       else
  399.         describe_map_tree (tem, 1, Qnil);
  400.       tem = Fbuffer_string ();
  401.       Ferase_buffer ();
  402.       set_buffer_internal (oldbuf);
  403.  
  404.     subst_string:
  405.       start = XSTRING (tem)->data;
  406.       length = XSTRING (tem)->size;
  407.     subst:
  408.       new = (unsigned char *) xrealloc (buf, bsize += length);
  409.       bufp += new - buf;
  410.       buf = new;
  411.       bcopy (start, bufp, length);
  412.       bufp += length;
  413.       /* Check STR again in case gc relocated it.  */
  414.       strp = (unsigned char *) XSTRING (str)->data + idx;
  415.     }
  416.       else            /* just copy other chars */
  417.     *bufp++ = *strp++;
  418.      }
  419.  
  420.   if (changed)            /* don't bother if nothing substituted */
  421.     tem = make_string (buf, bufp - buf);
  422.   else
  423.     tem = str;
  424.   UNGCPRO;
  425.   free (buf);
  426.   return tem;
  427. }
  428.  
  429. syms_of_doc ()
  430. {
  431.   staticpro (&Vdoc_file_name);
  432.   Vdoc_file_name = Qnil;
  433.  
  434.   defsubr (&Sdocumentation);
  435.   defsubr (&Sdocumentation_property);
  436.   defsubr (&Ssnarf_documentation);
  437.   defsubr (&Ssubstitute_command_keys);
  438. }
  439.