1 /* indices.c -- Commands for dealing with an Info file Index.
2 $Id: indices.c,v 1.1.1.2 1998/03/22 20:42:33 law Exp $
4 Copyright (C) 1993, 97 Free Software Foundation, Inc.
6 This program 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 2, or (at your option)
11 This program 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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 Written by Brian Fox (bfox@ai.mit.edu). */
25 /* User-visible variable controls the output of info-index-next. */
26 int show_index_match
= 1;
28 /* In the Info sense, an index is a menu. This variable holds the last
30 static REFERENCE
**index_index
= (REFERENCE
**)NULL
;
32 /* The offset of the most recently selected index element. */
33 static int index_offset
= 0;
35 /* Variable which holds the last string searched for. */
36 static char *index_search
= (char *)NULL
;
38 /* A couple of "globals" describing where the initial index was found. */
39 static char *initial_index_filename
= (char *)NULL
;
40 static char *initial_index_nodename
= (char *)NULL
;
42 /* A structure associating index names with index offset ranges. */
44 char *name
; /* The nodename of this index. */
45 int first
; /* The index in our list of the first entry. */
46 int last
; /* The index in our list of the last entry. */
49 /* An array associating index nodenames with index offset ranges. */
50 static INDEX_NAME_ASSOC
**index_nodenames
= (INDEX_NAME_ASSOC
**)NULL
;
51 static int index_nodenames_index
= 0;
52 static int index_nodenames_slots
= 0;
54 /* Add the name of NODE, and the range of the associated index elements
55 (passed in ARRAY) to index_nodenames. */
57 add_index_to_index_nodenames (array
, node
)
62 INDEX_NAME_ASSOC
*assoc
;
64 for (last
= 0; array
[last
]; last
++);
65 assoc
= (INDEX_NAME_ASSOC
*)xmalloc (sizeof (INDEX_NAME_ASSOC
));
66 assoc
->name
= xstrdup (node
->nodename
);
68 if (!index_nodenames_index
)
75 for (i
= 0; index_nodenames
[i
+ 1]; i
++);
76 assoc
->first
= 1 + index_nodenames
[i
]->last
;
77 assoc
->last
= assoc
->first
+ last
;
80 (assoc
, index_nodenames_index
, index_nodenames
, index_nodenames_slots
,
81 10, INDEX_NAME_ASSOC
*);
84 /* Find and return the indices of WINDOW's file. The indices are defined
85 as the first node in the file containing the word "Index" and any
86 immediately following nodes whose names also contain "Index". All such
87 indices are concatenated and the result returned. If WINDOW's info file
88 doesn't have any indices, a NULL pointer is returned. */
90 info_indices_of_window (window
)
95 fb
= file_buffer_of_window (window
);
97 return (info_indices_of_file_buffer (fb
));
101 info_indices_of_file_buffer (file_buffer
)
102 FILE_BUFFER
*file_buffer
;
105 REFERENCE
**result
= (REFERENCE
**)NULL
;
107 /* No file buffer, no indices. */
109 return ((REFERENCE
**)NULL
);
111 /* Reset globals describing where the index was found. */
112 maybe_free (initial_index_filename
);
113 maybe_free (initial_index_nodename
);
114 initial_index_filename
= (char *)NULL
;
115 initial_index_nodename
= (char *)NULL
;
119 for (i
= 0; index_nodenames
[i
]; i
++)
121 free (index_nodenames
[i
]->name
);
122 free (index_nodenames
[i
]);
125 index_nodenames_index
= 0;
126 index_nodenames
[0] = (INDEX_NAME_ASSOC
*)NULL
;
129 /* Grovel the names of the nodes found in this file. */
130 if (file_buffer
->tags
)
134 for (i
= 0; (tag
= file_buffer
->tags
[i
]); i
++)
136 if (string_in_line ("Index", tag
->nodename
) != -1)
141 /* Found one. Get its menu. */
142 node
= info_get_node (tag
->filename
, tag
->nodename
);
146 /* Remember the filename and nodename of this index. */
147 initial_index_filename
= xstrdup (file_buffer
->filename
);
148 initial_index_nodename
= xstrdup (tag
->nodename
);
150 menu
= info_menu_of_node (node
);
152 /* If we have a menu, add this index's nodename and range
153 to our list of index_nodenames. */
156 add_index_to_index_nodenames (menu
, node
);
158 /* Concatenate the references found so far. */
159 result
= info_concatenate_references (result
, menu
);
166 /* If there is a result, clean it up so that every entry has a filename. */
167 for (i
= 0; result
&& result
[i
]; i
++)
168 if (!result
[i
]->filename
)
169 result
[i
]->filename
= xstrdup (file_buffer
->filename
);
174 DECLARE_INFO_COMMAND (info_index_search
,
175 _("Look up a string in the index for this file"))
177 do_info_index_search (window
, count
, 0);
180 /* Look up SEARCH_STRING in the index for this file. If SEARCH_STRING
181 is NULL, prompt user for input. */
183 do_info_index_search (window
, count
, search_string
)
191 /* Reset the index offset, since this is not the info-index-next command. */
194 /* The user is selecting a new search string, so flush the old one. */
195 maybe_free (index_search
);
196 index_search
= (char *)NULL
;
198 /* If this window's file is not the same as the one that we last built an
199 index for, build and remember an index now. */
200 fb
= file_buffer_of_window (window
);
201 if (!initial_index_filename
||
202 (strcmp (initial_index_filename
, fb
->filename
) != 0))
204 info_free_references (index_index
);
205 window_message_in_echo_area (_("Finding index entries..."));
206 index_index
= info_indices_of_file_buffer (fb
);
209 /* If there is no index, quit now. */
212 info_error (_("No indices found."));
216 /* Okay, there is an index. Look for SEARCH_STRING, or, if it is
217 empty, prompt for one. */
218 if (search_string
&& *search_string
)
219 line
= xstrdup (search_string
);
222 line
= info_read_maybe_completing (window
, _("Index entry: "),
224 window
= active_window
;
229 info_abort_key (active_window
, 1, 0);
233 /* Empty line means move to the Index node. */
238 if (initial_index_filename
&& initial_index_nodename
)
242 node
= info_get_node (initial_index_filename
,
243 initial_index_nodename
);
244 set_remembered_pagetop_and_point (window
);
245 window_set_node_of_window (window
, node
);
246 remember_window_and_node (window
, node
);
247 window_clear_echo_area ();
253 /* The user typed either a completed index label, or a partial string.
254 Find an exact match, or, failing that, the first index entry containing
255 the partial string. So, we just call info_next_index_match () with minor
256 manipulation of INDEX_OFFSET. */
260 /* Start the search right after/before this index. */
264 for (i
= 0; index_index
[i
]; i
++);
270 old_offset
= index_offset
;
272 /* The "last" string searched for is this one. */
275 /* Find it, or error. */
276 info_next_index_match (window
, count
, 0);
278 /* If the search failed, return the index offset to where it belongs. */
279 if (index_offset
== old_offset
)
285 index_entry_exists (window
, string
)
292 /* If there is no previous search string, the user hasn't built an index
297 fb
= file_buffer_of_window (window
);
298 if (!initial_index_filename
299 || (strcmp (initial_index_filename
, fb
->filename
) != 0))
301 info_free_references (index_index
);
302 index_index
= info_indices_of_file_buffer (fb
);
305 /* If there is no index, that is an error. */
309 for (i
= 0; (i
> -1) && (index_index
[i
]); i
++)
310 if (strcmp (string
, index_index
[i
]->label
) == 0)
313 /* If that failed, look for the next substring match. */
314 if ((i
< 0) || (!index_index
[i
]))
316 for (i
= 0; (i
> -1) && (index_index
[i
]); i
++)
317 if (string_in_line (string
, index_index
[i
]->label
) != -1)
320 if ((i
> -1) && (index_index
[i
]))
321 string_in_line (string
, index_index
[i
]->label
);
324 /* If that failed, return 0. */
325 if ((i
< 0) || (!index_index
[i
]))
331 DECLARE_INFO_COMMAND (info_next_index_match
,
332 _("Go to the next matching index item from the last `\\[index-search]' command"))
338 /* If there is no previous search string, the user hasn't built an index
342 info_error (_("No previous index search string."));
346 /* If there is no index, that is an error. */
349 info_error (_("No index entries."));
353 /* The direction of this search is controlled by the value of the
360 /* Search for the next occurence of index_search. First try to find
364 for (i
= index_offset
+ dir
; (i
> -1) && (index_index
[i
]); i
+= dir
)
365 if (strcmp (index_search
, index_index
[i
]->label
) == 0)
368 /* If that failed, look for the next substring match. */
369 if ((i
< 0) || (!index_index
[i
]))
371 for (i
= index_offset
+ dir
; (i
> -1) && (index_index
[i
]); i
+= dir
)
372 if (string_in_line (index_search
, index_index
[i
]->label
) != -1)
375 if ((i
> -1) && (index_index
[i
]))
376 partial
= string_in_line (index_search
, index_index
[i
]->label
);
379 /* If that failed, print an error. */
380 if ((i
< 0) || (!index_index
[i
]))
382 info_error (_("No %sindex entries containing \"%s\"."),
383 index_offset
> 0 ? _("more ") : "", index_search
);
387 /* Okay, we found the next one. Move the offset to the current entry. */
390 /* Report to the user on what we have found. */
393 char *name
= _("CAN'T SEE THIS");
396 for (j
= 0; index_nodenames
[j
]; j
++)
398 if ((i
>= index_nodenames
[j
]->first
) &&
399 (i
<= index_nodenames
[j
]->last
))
401 name
= index_nodenames
[j
]->name
;
406 /* If we had a partial match, indicate to the user which part of the
408 match
= xstrdup (index_index
[i
]->label
);
410 if (partial
&& show_index_match
)
412 int j
, ls
, start
, upper
;
414 ls
= strlen (index_search
);
415 start
= partial
- ls
;
416 upper
= isupper (match
[start
]) ? 1 : 0;
418 for (j
= 0; j
< ls
; j
++)
420 match
[j
+ start
] = info_tolower (match
[j
+ start
]);
422 match
[j
+ start
] = info_toupper (match
[j
+ start
]);
428 format
= replace_in_documentation
429 (_("Found \"%s\" in %s. (`\\[next-index-match]' tries to find next.)"));
431 window_message_in_echo_area (format
, match
, name
);
437 /* Select the node corresponding to this index entry. */
438 node
= info_get_node (index_index
[i
]->filename
, index_index
[i
]->nodename
);
442 info_error (CANT_FILE_NODE
,
443 index_index
[i
]->filename
, index_index
[i
]->nodename
);
447 set_remembered_pagetop_and_point (window
);
448 window_set_node_of_window (window
, node
);
449 remember_window_and_node (window
, node
);
452 /* Try to find an occurence of LABEL in this node. */
456 start
= window
->line_starts
[1] - window
->node
->contents
;
457 loc
= info_target_search_node (node
, index_index
[i
]->label
, start
);
462 window_adjust_pagetop (window
);
467 /* **************************************************************** */
469 /* Info APROPOS: Search every known index. */
471 /* **************************************************************** */
473 /* For every menu item in DIR, search the indices of that file for
476 apropos_in_all_indices (search_string
, inform
)
480 register int i
, dir_index
;
481 REFERENCE
**all_indices
= (REFERENCE
**)NULL
;
482 REFERENCE
**dir_menu
= (REFERENCE
**)NULL
;
485 dir_node
= info_get_node ("dir", "Top");
487 dir_menu
= info_menu_of_node (dir_node
);
492 /* For every menu item in DIR, get the associated node's file buffer and
493 read the indices of that file buffer. Gather all of the indices into
495 for (dir_index
= 0; dir_menu
[dir_index
]; dir_index
++)
497 REFERENCE
**this_index
, *this_item
;
499 FILE_BUFFER
*this_fb
;
501 this_item
= dir_menu
[dir_index
];
503 if (!this_item
->filename
)
505 if (dir_node
->parent
)
506 this_item
->filename
= xstrdup (dir_node
->parent
);
508 this_item
->filename
= xstrdup (dir_node
->filename
);
511 /* Find this node. If we cannot find it, try using the label of the
512 entry as a file (i.e., "(LABEL)Top"). */
513 this_node
= info_get_node (this_item
->filename
, this_item
->nodename
);
515 if (!this_node
&& this_item
->nodename
&&
516 (strcmp (this_item
->label
, this_item
->nodename
) == 0))
517 this_node
= info_get_node (this_item
->label
, "Top");
522 /* Get the file buffer associated with this node. */
526 files_name
= this_node
->parent
;
528 files_name
= this_node
->filename
;
530 this_fb
= info_find_file (files_name
);
532 if (this_fb
&& inform
)
533 message_in_echo_area (_("Scanning indices of \"%s\"..."), files_name
);
535 this_index
= info_indices_of_file_buffer (this_fb
);
538 if (this_fb
&& inform
)
539 unmessage_in_echo_area ();
544 /* Remember the filename which contains this set of references. */
545 for (i
= 0; this_index
&& this_index
[i
]; i
++)
546 if (!this_index
[i
]->filename
)
547 this_index
[i
]->filename
= xstrdup (this_fb
->filename
);
549 /* Concatenate with the other indices. */
550 all_indices
= info_concatenate_references (all_indices
, this_index
);
554 info_free_references (dir_menu
);
556 /* Build a list of the references which contain SEARCH_STRING. */
559 REFERENCE
*entry
, **apropos_list
= (REFERENCE
**)NULL
;
560 int apropos_list_index
= 0;
561 int apropos_list_slots
= 0;
563 for (i
= 0; (entry
= all_indices
[i
]); i
++)
565 if (string_in_line (search_string
, entry
->label
) != -1)
568 (entry
, apropos_list_index
, apropos_list
, apropos_list_slots
,
573 maybe_free (entry
->label
);
574 maybe_free (entry
->filename
);
575 maybe_free (entry
->nodename
);
581 all_indices
= apropos_list
;
583 return (all_indices
);
586 #define APROPOS_NONE \
587 _("No available info files reference \"%s\" in their indices.")
590 info_apropos (string
)
593 REFERENCE
**apropos_list
;
595 apropos_list
= apropos_in_all_indices (string
, 0);
599 info_error (APROPOS_NONE
, string
);
606 for (i
= 0; (entry
= apropos_list
[i
]); i
++)
607 fprintf (stderr
, "\"(%s)%s\" -- %s\n",
608 entry
->filename
, entry
->nodename
, entry
->label
);
610 info_free_references (apropos_list
);
613 static char *apropos_list_nodename
= "*Apropos*";
615 DECLARE_INFO_COMMAND (info_index_apropos
,
616 _("Grovel all known info file's indices for a string and build a menu"))
620 line
= info_read_in_echo_area (window
, _("Index apropos: "));
622 window
= active_window
;
627 info_abort_key (window
, 1, 1);
631 /* User typed something? */
634 REFERENCE
**apropos_list
;
637 apropos_list
= apropos_in_all_indices (line
, 1);
641 info_error (APROPOS_NONE
, line
);
648 initialize_message_buffer ();
649 printf_to_message_buffer
650 (_("\n* Menu: Nodes whoses indices contain \"%s\":\n"), line
);
651 line_buffer
= (char *)xmalloc (500);
653 for (i
= 0; apropos_list
[i
]; i
++)
656 sprintf (line_buffer
, "* (%s)%s::",
657 apropos_list
[i
]->filename
, apropos_list
[i
]->nodename
);
658 len
= pad_to (36, line_buffer
);
659 sprintf (line_buffer
+ len
, "%s", apropos_list
[i
]->label
);
660 printf_to_message_buffer ("%s\n", line_buffer
);
665 apropos_node
= message_buffer_to_node ();
666 add_gcable_pointer (apropos_node
->contents
);
667 name_internal_node (apropos_node
, apropos_list_nodename
);
669 /* Even though this is an internal node, we don't want the window
670 system to treat it specially. So we turn off the internalness
672 apropos_node
->flags
&= ~N_IsInternal
;
674 /* Find/Create a window to contain this node. */
679 set_remembered_pagetop_and_point (window
);
681 /* If a window is visible and showing an apropos list already,
683 for (new = windows
; new; new = new->next
)
687 if (internal_info_node_p (node
) &&
688 (strcmp (node
->nodename
, apropos_list_nodename
) == 0))
692 /* If we couldn't find an existing window, try to use the next window
694 if (!new && window
->next
)
697 /* If we still don't have a window, make a new one to contain
703 old_active
= active_window
;
704 active_window
= window
;
705 new = window_make_window ((NODE
*)NULL
);
706 active_window
= old_active
;
709 /* If we couldn't make a new window, use this one. */
713 /* Lines do not wrap in this window. */
714 new->flags
|= W_NoWrap
;
716 window_set_node_of_window (new, apropos_node
);
717 remember_window_and_node (new, apropos_node
);
720 info_free_references (apropos_list
);
724 if (!info_error_was_printed
)
725 window_clear_echo_area ();