Back to index

tetex-bin  3.0
nodemenu.c
Go to the documentation of this file.
00001 /* nodemenu.c -- produce a menu of all visited nodes.
00002    $Id: nodemenu.c,v 1.5 2004/04/11 17:56:46 karl Exp $
00003 
00004    Copyright (C) 1993, 1997, 1998, 2002, 2003, 2004 Free Software
00005    Foundation, Inc.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2, or (at your option)
00010    any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program; if not, write to the Free Software
00019    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00020 
00021    Written by Brian Fox (bfox@ai.mit.edu). */
00022 
00023 #include "info.h"
00024 
00025 NODE * get_visited_nodes (Function *filter_func);
00026 
00027 /* Return a line describing the format of a node information line. */
00028 static const char *
00029 nodemenu_format_info (void)
00030 {
00031   return (_("\n\
00032 * Menu:\n\
00033   (File)Node                        Lines   Size   Containing File\n\
00034   ----------                        -----   ----   ---------------"));
00035 }
00036 
00037 /* Produce a formatted line of information about NODE.  Here is what we want
00038    the output listing to look like:
00039 
00040 * Menu:
00041   (File)Node                        Lines   Size   Containing File
00042   ----------                        -----   ----   ---------------
00043 * (emacs)Buffers::                  48      2230   /usr/gnu/info/emacs/emacs-1
00044 * (autoconf)Writing configure.in::  123     58789  /usr/gnu/info/autoconf/autoconf-1
00045 * (dir)Top::                        40      589    /usr/gnu/info/dir
00046 */
00047 static char *
00048 format_node_info (NODE *node)
00049 {
00050   register int i, len;
00051   char *parent, *containing_file;
00052   static char *line_buffer = (char *)NULL;
00053 
00054   if (!line_buffer)
00055     line_buffer = (char *)xmalloc (1000);
00056 
00057   if (node->parent)
00058     {
00059       parent = filename_non_directory (node->parent);
00060       if (!parent)
00061         parent = node->parent;
00062     }
00063   else
00064     parent = (char *)NULL;
00065 
00066   containing_file = node->filename;
00067 
00068   if (!parent && !*containing_file)
00069     sprintf (line_buffer, "* %s::", node->nodename);
00070   else
00071     {
00072       char *file = (char *)NULL;
00073 
00074       if (parent)
00075         file = parent;
00076       else
00077         file = filename_non_directory (containing_file);
00078 
00079       if (!file)
00080         file = containing_file;
00081 
00082       if (!*file)
00083         file = "dir";
00084 
00085       sprintf (line_buffer, "* (%s)%s::", file, node->nodename);
00086     }
00087 
00088   len = pad_to (36, line_buffer);
00089 
00090   {
00091     int lines = 1;
00092 
00093     for (i = 0; i < node->nodelen; i++)
00094       if (node->contents[i] == '\n')
00095         lines++;
00096 
00097     sprintf (line_buffer + len, "%d", lines);
00098   }
00099 
00100   len = pad_to (44, line_buffer);
00101   sprintf (line_buffer + len, "%ld", node->nodelen);
00102 
00103   if (node->filename && *(node->filename))
00104     {
00105       len = pad_to (51, line_buffer);
00106       strcpy (line_buffer + len, node->filename);
00107     }
00108 
00109   return xstrdup (line_buffer);
00110 }
00111 
00112 /* Little string comparison routine for qsort (). */
00113 static int
00114 compare_strings (const void *entry1, const void *entry2)
00115 {
00116   char **e1 = (char **) entry1;
00117   char **e2 = (char **) entry2;
00118 
00119   return (strcasecmp (*e1, *e2));
00120 }
00121 
00122 /* The name of the nodemenu node. */
00123 static char *nodemenu_nodename = "*Node Menu*";
00124 
00125 /* Produce an informative listing of all the visited nodes, and return it
00126    in a node.  If FILTER_FUNC is non-null, it is a function which filters
00127    which nodes will appear in the listing.  FILTER_FUNC takes an argument
00128    of NODE, and returns non-zero if the node should appear in the listing. */
00129 NODE *
00130 get_visited_nodes (Function *filter_func)
00131 {
00132   register int i, iw_index;
00133   INFO_WINDOW *info_win;
00134   NODE *node;
00135   char **lines = (char **)NULL;
00136   int lines_index = 0, lines_slots = 0;
00137 
00138   if (!info_windows)
00139     return ((NODE *)NULL);
00140 
00141   for (iw_index = 0; (info_win = info_windows[iw_index]); iw_index++)
00142     {
00143       for (i = 0; i < info_win->nodes_index; i++)
00144         {
00145           node = info_win->nodes[i];
00146 
00147           /* We skip mentioning "*Node Menu*" nodes. */
00148           if (internal_info_node_p (node) &&
00149               (strcmp (node->nodename, nodemenu_nodename) == 0))
00150             continue;
00151 
00152           if (node && (!filter_func || (*filter_func) (node)))
00153             {
00154               char *line;
00155 
00156               line = format_node_info (node);
00157               add_pointer_to_array
00158                 (line, lines_index, lines, lines_slots, 20, char *);
00159             }
00160         }
00161     }
00162 
00163   /* Sort the array of information lines, if there are any. */
00164   if (lines)
00165     {
00166       register int j, newlen;
00167       char **temp;
00168 
00169       qsort (lines, lines_index, sizeof (char *), compare_strings);
00170 
00171       /* Delete duplicates. */
00172       for (i = 0, newlen = 1; i < lines_index - 1; i++)
00173         {
00174          /* Use FILENAME_CMP here, since the most important piece
00175             of info in each line is the file name of the node.  */
00176           if (FILENAME_CMP (lines[i], lines[i + 1]) == 0)
00177             {
00178               free (lines[i]);
00179               lines[i] = (char *)NULL;
00180             }
00181           else
00182             newlen++;
00183         }
00184 
00185       /* We have free ()'d and marked all of the duplicate slots.
00186          Copy the live slots rather than pruning the dead slots. */
00187       temp = (char **)xmalloc ((1 + newlen) * sizeof (char *));
00188       for (i = 0, j = 0; i < lines_index; i++)
00189         if (lines[i])
00190           temp[j++] = lines[i];
00191 
00192       temp[j] = (char *)NULL;
00193       free (lines);
00194       lines = temp;
00195       lines_index = newlen;
00196     }
00197 
00198   initialize_message_buffer ();
00199 
00200   printf_to_message_buffer
00201     ("%s", replace_in_documentation
00202      ((char *) _("Here is the menu of nodes you have recently visited.\n\
00203 Select one from this menu, or use `\\[history-node]' in another window.\n"), 0),
00204      NULL, NULL);
00205 
00206   printf_to_message_buffer ("%s\n", (char *) nodemenu_format_info (),
00207       NULL, NULL);
00208 
00209   for (i = 0; (lines != (char **)NULL) && (i < lines_index); i++)
00210     {
00211       printf_to_message_buffer ("%s\n", lines[i], NULL, NULL);
00212       free (lines[i]);
00213     }
00214 
00215   if (lines)
00216     free (lines);
00217 
00218   node = message_buffer_to_node ();
00219   add_gcable_pointer (node->contents);
00220   return (node);
00221 }
00222 
00223 DECLARE_INFO_COMMAND (list_visited_nodes,
00224    _("Make a window containing a menu of all of the currently visited nodes"))
00225 {
00226   WINDOW *new;
00227   NODE *node;
00228 
00229   set_remembered_pagetop_and_point (window);
00230 
00231   /* If a window is visible and showing the buffer list already, re-use it. */
00232   for (new = windows; new; new = new->next)
00233     {
00234       node = new->node;
00235 
00236       if (internal_info_node_p (node) &&
00237           (strcmp (node->nodename, nodemenu_nodename) == 0))
00238         break;
00239     }
00240 
00241   /* If we couldn't find an existing window, try to use the next window
00242      in the chain. */
00243   if (!new)
00244     {
00245       if (window->next)
00246         new = window->next;
00247       /* If there is more than one window, wrap around. */
00248       else if (window != windows)
00249         new = windows;
00250     }
00251 
00252   /* If we still don't have a window, make a new one to contain the list. */
00253   if (!new)
00254     {
00255       WINDOW *old_active;
00256 
00257       old_active = active_window;
00258       active_window = window;
00259       new = window_make_window ((NODE *)NULL);
00260       active_window = old_active;
00261     }
00262 
00263   /* If we couldn't make a new window, use this one. */
00264   if (!new)
00265     new = window;
00266 
00267   /* Lines do not wrap in this window. */
00268   new->flags |= W_NoWrap;
00269   node = get_visited_nodes ((Function *)NULL);
00270   name_internal_node (node, nodemenu_nodename);
00271 
00272 #if 0
00273   /* Even if this is an internal node, we don't want the window
00274      system to treat it specially.  So we turn off the internalness
00275      of it here. */
00276   /* Why?  We depend on internal_info_node_p returning true, so we must
00277      not remove the flag.  Otherwise, the *Node Menu* nodes themselves
00278      appear in the node menu.  --Andreas Schwab
00279      <schwab@issan.informatik.uni-dortmund.de>.  */
00280   node->flags &= ~N_IsInternal;
00281 #endif
00282 
00283   /* If this window is already showing a node menu, reuse the existing node
00284      slot. */
00285   {
00286     int remember_me = 1;
00287 
00288 #if defined (NOTDEF)
00289     if (internal_info_node_p (new->node) &&
00290         (strcmp (new->node->nodename, nodemenu_nodename) == 0))
00291       remember_me = 0;
00292 #endif /* NOTDEF */
00293 
00294     window_set_node_of_window (new, node);
00295 
00296     if (remember_me)
00297       remember_window_and_node (new, node);
00298   }
00299 
00300   active_window = new;
00301 }
00302 
00303 DECLARE_INFO_COMMAND (select_visited_node,
00304       _("Select a node which has been previously visited in a visible window"))
00305 {
00306   char *line;
00307   NODE *node;
00308   REFERENCE **menu;
00309 
00310   node = get_visited_nodes ((Function *)NULL);
00311 
00312   menu = info_menu_of_node (node);
00313   free (node);
00314 
00315   line =
00316     info_read_completing_in_echo_area (window,
00317         (char *) _("Select visited node: "), menu);
00318 
00319   window = active_window;
00320 
00321   /* User aborts, just quit. */
00322   if (!line)
00323     {
00324       info_abort_key (window, 0, 0);
00325       info_free_references (menu);
00326       return;
00327     }
00328 
00329   if (*line)
00330     {
00331       REFERENCE *entry;
00332 
00333       /* Find the selected label in the references. */
00334       entry = info_get_labeled_reference (line, menu);
00335 
00336       if (!entry)
00337         info_error ((char *) _("The reference disappeared! (%s)."), line, NULL);
00338       else
00339         info_select_reference (window, entry);
00340     }
00341 
00342   free (line);
00343   info_free_references (menu);
00344 
00345   if (!info_error_was_printed)
00346     window_clear_echo_area ();
00347 }