1 /* infodoc.c -- functions which build documentation nodes.
2 $Id: infodoc.c,v 1.8 2004/04/11 17:56:45 karl Exp $
4 Copyright (C) 1993, 1997, 1998, 1999, 2001, 2002, 2003, 2004 Free Software
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 Written by Brian Fox (bfox@ai.mit.edu). */
26 /* HELP_NODE_GETS_REGENERATED is always defined now that keys may get
27 rebound, or other changes in the help text may occur. */
28 #define HELP_NODE_GETS_REGENERATED 1
30 /* The name of the node used in the help window. */
31 static char *info_help_nodename = "*Info Help*";
33 /* A node containing printed key bindings and their documentation. */
34 static NODE *internal_info_help_node = (NODE *)NULL;
36 /* A pointer to the contents of the help node. */
37 static char *internal_info_help_node_contents = (char *)NULL;
39 /* The (more or less) static text which appears in the internal info
40 help node. The actual key bindings are inserted. Keep the
41 underlines (****, etc.) in the same N_ call as the text lines they
42 refer to, so translations can make the number of *'s or -'s match. */
45 static char *info_internal_help_text[] = {
46 N_("Basic Commands in Info Windows\n\
47 ******************************\n"),
49 N_("\\%-10[quit-help] Quit this help.\n"),
50 N_("\\%-10[quit] Quit Info altogether.\n"),
51 N_("\\%-10[get-info-help-node] Invoke the Info tutorial.\n"),
53 N_("Selecting other nodes:\n\
54 ----------------------\n"),
55 N_("\\%-10[next-node] Move to the \"next\" node of this node.\n"),
56 N_("\\%-10[prev-node] Move to the \"previous\" node of this node.\n"),
57 N_("\\%-10[up-node] Move \"up\" from this node.\n"),
58 N_("\\%-10[menu-item] Pick menu item specified by name.\n\
59 Picking a menu item causes another node to be selected.\n"),
60 N_("\\%-10[xref-item] Follow a cross reference. Reads name of reference.\n"),
61 N_("\\%-10[history-node] Move to the last node seen in this window.\n"),
62 N_("\\%-10[move-to-next-xref] Skip to next hypertext link within this node.\n"),
63 N_("\\%-10[move-to-prev-xref] Skip to previous hypertext link within this node.\n"),
64 N_("\\%-10[select-reference-this-line] Follow the hypertext link under cursor.\n"),
65 N_("\\%-10[dir-node] Move to the `directory' node. Equivalent to `\\[goto-node] (DIR)'.\n"),
66 N_("\\%-10[top-node] Move to the Top node. Equivalent to `\\[goto-node] Top'.\n"),
68 N_("Moving within a node:\n\
69 ---------------------\n"),
70 N_("\\%-10[beginning-of-node] Go to the beginning of this node.\n"),
71 N_("\\%-10[end-of-node] Go to the end of this node.\n"),
72 N_("\\%-10[next-line] Scroll forward 1 line.\n"),
73 N_("\\%-10[prev-line] Scroll backward 1 line.\n"),
74 N_("\\%-10[scroll-forward] Scroll forward a page.\n"),
75 N_("\\%-10[scroll-backward] Scroll backward a page.\n"),
77 N_("Other commands:\n\
79 N_("\\%-10[menu-digit] Pick first ... ninth item in node's menu.\n"),
80 N_("\\%-10[last-menu-item] Pick last item in node's menu.\n"),
81 N_("\\%-10[index-search] Search for a specified string in the index entries of this Info\n\
82 file, and select the node referenced by the first entry found.\n"),
83 N_("\\%-10[goto-node] Move to node specified by name.\n\
84 You may include a filename as well, as in (FILENAME)NODENAME.\n"),
85 N_("\\%-10[search] Search forward for a specified string\n\
86 and select the node in which the next occurrence is found.\n"),
87 N_("\\%-10[search-backward] Search backward for a specified string\n\
88 and select the node in which the previous occurrence is found.\n"),
94 static char *info_internal_help_text[] = {
95 N_("Basic Commands in Info Windows\n\
96 ******************************\n"),
98 N_(" %-10s Quit this help.\n"),
99 N_(" %-10s Quit Info altogether.\n"),
100 N_(" %-10s Invoke the Info tutorial.\n"),
102 N_("Selecting other nodes:\n\
103 ----------------------\n",
104 N_(" %-10s Move to the `next' node of this node.\n"),
105 N_(" %-10s Move to the `previous' node of this node.\n"),
106 N_(" %-10s Move `up' from this node.\n"),
107 N_(" %-10s Pick menu item specified by name.\n"),
108 N_(" Picking a menu item causes another node to be selected.\n"),
109 N_(" %-10s Follow a cross reference. Reads name of reference.\n"),
110 N_(" %-10s Move to the last node seen in this window.\n"),
111 N_(" %-10s Skip to next hypertext link within this node.\n"),
112 N_(" %-10s Follow the hypertext link under cursor.\n"),
113 N_(" %-10s Move to the `directory' node. Equivalent to `g (DIR)'.\n"),
114 N_(" %-10s Move to the Top node. Equivalent to `g Top'.\n"),
116 N_("Moving within a node:\n\
117 ---------------------\n"),
118 N_(" %-10s Scroll forward a page.\n"),
119 N_(" %-10s Scroll backward a page.\n"),
120 N_(" %-10s Go to the beginning of this node.\n"),
121 N_(" %-10s Go to the end of this node.\n"),
122 N_(" %-10s Scroll forward 1 line.\n"),
123 N_(" %-10s Scroll backward 1 line.\n"),
125 N_("Other commands:\n\
127 N_(" %-10s Pick first ... ninth item in node's menu.\n"),
128 N_(" %-10s Pick last item in node's menu.\n"),
129 N_(" %-10s Search for a specified string in the index entries of this Info\n"),
130 N_(" file, and select the node referenced by the first entry found.\n"),
131 N_(" %-10s Move to node specified by name.\n"),
132 N_(" You may include a filename as well, as in (FILENAME)NODENAME.\n"),
133 N_(" %-10s Search forward for a specified string,\n"),
134 N_(" and select the node in which the next occurrence is found.\n"),
135 N_(" %-10s Search backward for a specified string\n"),
136 N_(" and select the node in which the next occurrence is found.\n"),
140 static char *info_help_keys_text[][2] = {
144 { "CTRL-x 0", "CTRL-x 0" },
154 { "ESC 1 SPC", "RET" },
155 { "ESC 1 DEL", "y" },
167 { "RET", "CTRL-x RET" },
173 { "1-9", "ESC 1-9" },
186 #endif /* !INFOKEY */
188 static char *where_is_internal (Keymap map, InfoCommand *cmd);
191 dump_map_to_message_buffer (char *prefix, Keymap map)
194 unsigned prefix_len = strlen (prefix);
195 char *new_prefix = (char *)xmalloc (prefix_len + 2);
197 strncpy (new_prefix, prefix, prefix_len);
198 new_prefix[prefix_len + 1] = '\0';
200 for (i = 0; i < 256; i++)
202 new_prefix[prefix_len] = i;
203 if (map[i].type == ISKMAP)
205 dump_map_to_message_buffer (new_prefix, (Keymap)map[i].function);
207 else if (map[i].function)
212 doc = function_documentation (map[i].function);
213 name = function_name (map[i].function);
218 /* Find out if there is a series of identical functions, as in
220 for (last = i + 1; last < 256; last++)
221 if ((map[last].type != ISFUNC) ||
222 (map[last].function != map[i].function))
227 printf_to_message_buffer ("%s .. ", pretty_keyseq (new_prefix),
229 new_prefix[prefix_len] = last - 1;
230 printf_to_message_buffer ("%s\t", pretty_keyseq (new_prefix),
235 printf_to_message_buffer ("%s\t", pretty_keyseq (new_prefix),
238 #if defined (NAMED_FUNCTIONS)
239 /* Print the name of the function, and some padding before the
240 documentation string is printed. */
243 int desired_doc_start = 40; /* Must be multiple of 8. */
245 printf_to_message_buffer ("(%s)", name, NULL, NULL);
246 length_so_far = message_buffer_length_this_line ();
248 if ((desired_doc_start + strlen (doc))
249 >= (unsigned int) the_screen->width)
250 printf_to_message_buffer ("\n ", NULL, NULL, NULL);
253 while (length_so_far < desired_doc_start)
255 printf_to_message_buffer ("\t", NULL, NULL, NULL);
256 length_so_far += character_width ('\t', length_so_far);
260 #endif /* NAMED_FUNCTIONS */
261 printf_to_message_buffer ("%s\n", doc, NULL, NULL);
267 /* How to create internal_info_help_node. HELP_IS_ONLY_WINDOW_P says
268 whether we're going to end up in a second (or more) window of our
269 own, or whether there's only one window and we're going to usurp it.
270 This determines how to quit the help window. Maybe we should just
271 make q do the right thing in both cases. */
274 create_internal_info_help_node (int help_is_only_window_p)
278 char *contents = NULL;
281 #ifndef HELP_NODE_GETS_REGENERATED
282 if (internal_info_help_node_contents)
283 contents = internal_info_help_node_contents;
284 #endif /* !HELP_NODE_GETS_REGENERATED */
288 int printed_one_mx = 0;
290 initialize_message_buffer ();
292 for (i = 0; info_internal_help_text[i]; i++)
295 printf_to_message_buffer (replace_in_documentation
296 ((char *) _(info_internal_help_text[i]), help_is_only_window_p),
299 /* Don't translate blank lines, gettext outputs the po file
300 header in that case. We want a blank line. */
301 char *msg = *(info_internal_help_text[i])
302 ? _(info_internal_help_text[i])
303 : info_internal_help_text[i];
304 char *key = info_help_keys_text[i][vi_keys_p];
306 /* If we have only one window (because the window size was too
307 small to split it), CTRL-x 0 doesn't work to `quit' help. */
308 if (STREQ (key, "CTRL-x 0") && help_is_only_window_p)
311 printf_to_message_buffer (msg, key, NULL, NULL);
312 #endif /* !INFOKEY */
315 printf_to_message_buffer ("---------------------\n\n", NULL, NULL, NULL);
316 printf_to_message_buffer ((char *) _("The current search path is:\n"),
318 printf_to_message_buffer (" %s\n", infopath, NULL, NULL);
319 printf_to_message_buffer ("---------------------\n\n", NULL, NULL, NULL);
320 printf_to_message_buffer ((char *) _("Commands available in Info windows:\n\n"),
322 dump_map_to_message_buffer ("", info_keymap);
323 printf_to_message_buffer ("---------------------\n\n", NULL, NULL, NULL);
324 printf_to_message_buffer ((char *) _("Commands available in the echo area:\n\n"),
326 dump_map_to_message_buffer ("", echo_area_keymap);
328 #if defined (NAMED_FUNCTIONS)
329 /* Get a list of commands which have no keystroke equivs. */
330 exec_keys = where_is (info_keymap, InfoCmd(info_execute_command));
332 exec_keys = xstrdup (exec_keys);
333 for (i = 0; function_doc_array[i].func; i++)
335 InfoCommand *cmd = DocInfoCmd(&function_doc_array[i]);
337 if (InfoFunction(cmd) != (VFunction *) info_do_lowercase_version
338 && !where_is_internal (info_keymap, cmd)
339 && !where_is_internal (echo_area_keymap, cmd))
343 printf_to_message_buffer ("---------------------\n\n",
345 if (exec_keys && exec_keys[0])
346 printf_to_message_buffer
347 ((char *) _("The following commands can only be invoked via %s:\n\n"),
348 exec_keys, NULL, NULL);
350 printf_to_message_buffer
351 ((char *) _("The following commands cannot be invoked at all:\n\n"),
356 printf_to_message_buffer
359 function_doc_array[i].func_name,
360 replace_in_documentation (strlen (function_doc_array[i].doc)
361 ? (char *) _(function_doc_array[i].doc) : "", 0)
368 printf_to_message_buffer ("\n", NULL, NULL, NULL);
370 maybe_free (exec_keys);
371 #endif /* NAMED_FUNCTIONS */
373 printf_to_message_buffer
374 ("%s", replace_in_documentation
375 ((char *) _("--- Use `\\[history-node]' or `\\[kill-node]' to exit ---\n"), 0),
377 node = message_buffer_to_node ();
378 internal_info_help_node_contents = node->contents;
382 /* We already had the right contents, so simply use them. */
383 node = build_message_node ("", 0, 0);
384 free (node->contents);
385 node->contents = contents;
386 node->nodelen = 1 + strlen (contents);
389 internal_info_help_node = node;
391 /* Do not GC this node's contents. It never changes, and we never need
392 to delete it once it is made. If you change some things (such as
393 placing information about dynamic variables in the help text) then
394 you will need to allow the contents to be gc'd, and you will have to
395 arrange to always regenerate the help node. */
396 #if defined (HELP_NODE_GETS_REGENERATED)
397 add_gcable_pointer (internal_info_help_node->contents);
400 name_internal_node (internal_info_help_node, info_help_nodename);
402 /* Even though this is an internal node, we don't want the window
403 system to treat it specially. So we turn off the internalness
405 internal_info_help_node->flags &= ~N_IsInternal;
408 /* Return a window which is the window showing help in this Info. */
410 /* If the eligible window's height is >= this, split it to make the help
411 window. Otherwise display the help window in the current window. */
412 #define HELP_SPLIT_SIZE 24
415 info_find_or_create_help_window (void)
417 int help_is_only_window_p;
418 WINDOW *eligible = NULL;
419 WINDOW *help_window = get_window_of_node (internal_info_help_node);
421 /* If we couldn't find the help window, then make it. */
427 for (window = windows; window; window = window->next)
429 if (window->height > max)
431 max = window->height;
439 #ifndef HELP_NODE_GETS_REGENERATED
441 /* help window is static, just return it. */
443 #endif /* not HELP_NODE_GETS_REGENERATED */
445 /* Make sure that we have a node containing the help text. The
446 argument is false if help will be the only window (so l must be used
447 to quit help), true if help will be one of several visible windows
448 (so CTRL-x 0 must be used to quit help). */
449 help_is_only_window_p = ((help_window && !windows->next)
450 || (!help_window && eligible->height < HELP_SPLIT_SIZE));
451 create_internal_info_help_node (help_is_only_window_p);
453 /* Either use the existing window to display the help node, or create
454 a new window if there was no existing help window. */
456 { /* Split the largest window into 2 windows, and show the help text
458 if (eligible->height >= HELP_SPLIT_SIZE)
460 active_window = eligible;
461 help_window = window_make_window (internal_info_help_node);
465 set_remembered_pagetop_and_point (active_window);
466 window_set_node_of_window (active_window, internal_info_help_node);
467 help_window = active_window;
471 { /* Case where help node always gets regenerated, and we have an
472 existing window in which to place the node. */
473 if (active_window != help_window)
475 set_remembered_pagetop_and_point (active_window);
476 active_window = help_window;
478 window_set_node_of_window (active_window, internal_info_help_node);
480 remember_window_and_node (help_window, help_window->node);
484 /* Create or move to the help window. */
485 DECLARE_INFO_COMMAND (info_get_help_window, _("Display help message"))
489 help_window = info_find_or_create_help_window ();
492 active_window = help_window;
493 active_window->flags |= W_UpdateWindow;
497 info_error ((char *) msg_cant_make_help, NULL, NULL);
501 /* Show the Info help node. This means that the "info" file is installed
502 where it can easily be found on your system. */
503 DECLARE_INFO_COMMAND (info_get_info_help_node, _("Visit Info node `(info)Help'"))
508 /* If there is a window on the screen showing the node "(info)Help" or
509 the node "(info)Help-Small-Screen", simply select that window. */
513 for (win = windows; win; win = win->next)
515 if (win->node && win->node->filename &&
517 (filename_non_directory (win->node->filename), "info") == 0) &&
518 ((strcmp (win->node->nodename, "Help") == 0) ||
519 (strcmp (win->node->nodename, "Help-Small-Screen") == 0)))
527 /* If the current window is small, show the small screen help. */
528 if (active_window->height < 24)
529 nodename = "Help-Small-Screen";
533 /* Try to get the info file for Info. */
534 node = info_get_node ("Info", nodename);
538 if (info_recent_file_error)
539 info_error (info_recent_file_error, NULL, NULL);
541 info_error ((char *) msg_cant_file_node, "Info", nodename);
545 /* If the current window is very large (greater than 45 lines),
546 then split it and show the help node in another window.
547 Otherwise, use the current window. */
549 if (active_window->height > 45)
550 active_window = window_make_window (node);
553 set_remembered_pagetop_and_point (active_window);
554 window_set_node_of_window (active_window, node);
557 remember_window_and_node (active_window, node);
561 /* **************************************************************** */
563 /* Groveling Info Keymaps and Docs */
565 /* **************************************************************** */
567 /* Return the documentation associated with the Info command FUNCTION. */
569 function_documentation (InfoCommand *cmd)
573 #if defined (INFOKEY)
581 for (i = 0; function_doc_array[i].func; i++)
582 if (InfoFunction(cmd) == function_doc_array[i].func)
585 doc = function_doc_array[i].func ? function_doc_array[i].doc : "";
587 #endif /* !INFOKEY */
589 return replace_in_documentation ((strlen (doc) == 0) ? doc : (char *) _(doc), 0);
592 #if defined (NAMED_FUNCTIONS)
593 /* Return the user-visible name of the function associated with the
594 Info command FUNCTION. */
596 function_name (InfoCommand *cmd)
598 #if defined (INFOKEY)
600 return cmd->func_name;
606 for (i = 0; function_doc_array[i].func; i++)
607 if (InfoFunction(cmd) == function_doc_array[i].func)
610 return (function_doc_array[i].func_name);
612 #endif /* !INFOKEY */
615 /* Return a pointer to the info command for function NAME. */
617 named_function (char *name)
621 for (i = 0; function_doc_array[i].func; i++)
622 if (strcmp (function_doc_array[i].func_name, name) == 0)
625 return (DocInfoCmd(&function_doc_array[i]));
627 #endif /* NAMED_FUNCTIONS */
629 /* Return the documentation associated with KEY in MAP. */
631 key_documentation (char key, Keymap map)
633 InfoCommand *function = map[key].function;
636 return (function_documentation (function));
638 return ((char *)NULL);
641 DECLARE_INFO_COMMAND (describe_key, _("Print documentation for KEY"))
644 unsigned char keystroke;
649 map = window->keymap;
653 message_in_echo_area ((char *) _("Describe key: %s"),
654 pretty_keyseq (keys), NULL);
655 keystroke = info_get_input_char ();
656 unmessage_in_echo_area ();
658 #if !defined (INFOKEY)
659 if (Meta_p (keystroke))
661 if (map[ESC].type != ISKMAP)
663 window_message_in_echo_area
664 (_("ESC %s is undefined."), pretty_keyname (UnMeta (keystroke)));
669 keystroke = UnMeta (keystroke);
670 map = (Keymap)map[ESC].function;
672 #endif /* !INFOKEY */
674 /* Add the KEYSTROKE to our list. */
678 if (map[keystroke].function == (InfoCommand *)NULL)
680 message_in_echo_area ((char *) _("%s is undefined."),
681 pretty_keyseq (keys), NULL);
684 else if (map[keystroke].type == ISKMAP)
686 map = (Keymap)map[keystroke].function;
691 char *keyname, *message, *fundoc, *funname = "";
693 #if defined (INFOKEY)
694 /* If the key is bound to do-lowercase-version, but its
695 lower-case variant is undefined, say that this key is
696 also undefined. This is especially important for unbound
697 edit keys that emit an escape sequence: it's terribly
698 confusing to see a message "Home (do-lowercase-version)"
699 or some such when Home is unbound. */
700 if (InfoFunction(map[keystroke].function)
701 == (VFunction *) info_do_lowercase_version)
703 unsigned char lowerkey = Meta_p(keystroke)
704 ? Meta (tolower (UnMeta (keystroke)))
705 : tolower (keystroke);
707 if (map[lowerkey].function == (InfoCommand *)NULL)
709 message_in_echo_area ((char *) _("%s is undefined."),
710 pretty_keyseq (keys), NULL);
716 keyname = pretty_keyseq (keys);
718 #if defined (NAMED_FUNCTIONS)
719 funname = function_name (map[keystroke].function);
720 #endif /* NAMED_FUNCTIONS */
722 fundoc = function_documentation (map[keystroke].function);
724 message = (char *)xmalloc
725 (10 + strlen (keyname) + strlen (fundoc) + strlen (funname));
727 #if defined (NAMED_FUNCTIONS)
728 sprintf (message, "%s (%s): %s.", keyname, funname, fundoc);
730 sprintf (message, _("%s is defined to %s."), keyname, fundoc);
731 #endif /* !NAMED_FUNCTIONS */
733 window_message_in_echo_area ("%s", message, NULL);
740 /* Return the pretty printable name of a single character. */
742 pretty_keyname (unsigned char key)
744 static char rep_buffer[30];
751 rep = pretty_keyname (UnMeta (key));
753 #if defined (INFOKEY)
754 sprintf (temp, "M-%s", rep);
756 sprintf (temp, "ESC %s", rep);
757 #endif /* !INFOKEY */
758 strcpy (rep_buffer, temp);
761 else if (Control_p (key))
765 case '\n': rep = "LFD"; break;
766 case '\t': rep = "TAB"; break;
767 case '\r': rep = "RET"; break;
768 case ESC: rep = "ESC"; break;
771 sprintf (rep_buffer, "C-%c", UnControl (key));
779 case ' ': rep = "SPC"; break;
780 case DEL: rep = "DEL"; break;
783 rep_buffer[1] = '\0';
790 /* Return the pretty printable string which represents KEYSEQ. */
792 static void pretty_keyseq_internal (char *keyseq, char *rep);
795 pretty_keyseq (char *keyseq)
797 static char keyseq_rep[200];
799 keyseq_rep[0] = '\0';
801 pretty_keyseq_internal (keyseq, keyseq_rep);
806 pretty_keyseq_internal (char *keyseq, char *rep)
808 if (term_kP && strncmp(keyseq, term_kP, strlen(term_kP)) == 0)
811 keyseq += strlen(term_kP);
813 else if (term_kN && strncmp(keyseq, term_kN, strlen(term_kN)) == 0)
816 keyseq += strlen(term_kN);
819 else if (term_kh && strncmp(keyseq, term_kh, strlen(term_kh)) == 0)
822 keyseq += strlen(term_kh);
824 else if (term_ke && strncmp(keyseq, term_ke, strlen(term_ke)) == 0)
827 keyseq += strlen(term_ke);
829 else if (term_ki && strncmp(keyseq, term_ki, strlen(term_ki)) == 0)
832 keyseq += strlen(term_ki);
834 else if (term_kx && strncmp(keyseq, term_kx, strlen(term_kx)) == 0)
837 keyseq += strlen(term_kx);
840 else if (term_ku && strncmp(keyseq, term_ku, strlen(term_ku)) == 0)
843 keyseq += strlen(term_ku);
845 else if (term_kd && strncmp(keyseq, term_kd, strlen(term_kd)) == 0)
848 keyseq += strlen(term_kd);
850 else if (term_kl && strncmp(keyseq, term_kl, strlen(term_kl)) == 0)
853 keyseq += strlen(term_kl);
855 else if (term_kr && strncmp(keyseq, term_kr, strlen(term_kr)) == 0)
857 strcpy(rep, "Right");
858 keyseq += strlen(term_kr);
862 strcpy (rep, pretty_keyname (keyseq[0]));
868 pretty_keyseq_internal (keyseq, rep + strlen(rep));
872 /* Return a pointer to the last character in s that is found in f. */
874 strrpbrk (const char *s, const char *f)
876 register const char *e = s + strlen(s);
877 register const char *t;
888 /* Replace the names of functions with the key that invokes them. */
890 replace_in_documentation (char *string, int help_is_only_window_p)
892 unsigned reslen = strlen (string);
893 register int i, start, next;
894 static char *result = (char *)NULL;
897 result = (char *)xmalloc (1 + reslen);
899 i = next = start = 0;
901 /* Skip to the beginning of a replaceable function. */
902 for (i = start; string[i]; i++)
906 /* Is this the start of a replaceable function name? */
907 if (string[i] == '\\')
915 if (string[++j] == '-')
917 if (isdigit(string[j]))
919 min = atoi(string + j);
920 while (isdigit(string[j]))
922 if (string[j] == '.' && isdigit(string[j + 1]))
925 max = atoi(string + j);
926 while (isdigit(string[j]))
929 fmt = (char *)xmalloc (j - i + 2);
930 strncpy (fmt, string + i + 1, j - i);
931 fmt[j - i - 1] = 's';
937 if (string[j] == '[')
941 char *rep_name, *fun_name, *rep;
942 InfoCommand *command;
946 /* Copy in the old text. */
947 strncpy (result + next, string + start, i - start);
951 /* Look for an optional numeric arg. */
953 if (isdigit(string[i])
954 || (string[i] == '-' && isdigit(string[i + 1])) )
956 arg = atoi(string + i);
957 if (string[i] == '-')
959 while (isdigit(string[i]))
964 /* Move to the end of the function name. */
965 for (i = start; string[i] && (string[i] != ']'); i++);
967 rep_name = (char *)xmalloc (1 + i - start);
968 strncpy (rep_name, string + start, i - start);
969 rep_name[i - start] = '\0';
971 /* If we have only one window (because the window size was too
972 small to split it), we have to quit help by going back one
973 noew in the history list, not deleting the window. */
974 if (strcmp (rep_name, "quit-help") == 0)
975 fun_name = help_is_only_window_p ? "history-node"
980 /* Find a key which invokes this function in the info_keymap. */
981 command = named_function (fun_name);
985 /* If the internal documentation string fails, there is a
986 serious problem with the associated command's documentation.
987 We croak so that it can be fixed immediately. */
995 argrep = where_is (info_keymap, InfoCmd(info_add_digit_to_numeric_arg));
996 p = argrep ? strrpbrk (argrep, "0123456789-") : NULL;
999 argstr = (char *)xmalloc (p - argrep + 21);
1000 strncpy (argstr, argrep, p - argrep);
1001 sprintf (argstr + (p - argrep), "%d", arg);
1006 rep = command ? where_is (info_keymap, command) : NULL;
1009 replen = (argstr ? strlen (argstr) : 0) + strlen (rep) + 1;
1010 repstr = (char *)xmalloc (replen);
1014 strcat(repstr, argstr);
1015 strcat(repstr, " ");
1018 strcat(repstr, rep);
1027 if (next + replen > reslen)
1029 reslen = next + replen + 1;
1030 result = (char *)xrealloc (result, reslen + 1);
1034 sprintf (result + next, fmt, repstr);
1036 strcpy (result + next, repstr);
1038 next = strlen (result);
1049 strcpy (result + next, string + start);
1053 /* Return a string of characters which could be typed from the keymap
1054 MAP to invoke FUNCTION. */
1055 static char *where_is_rep = (char *)NULL;
1056 static int where_is_rep_index = 0;
1057 static int where_is_rep_size = 0;
1060 where_is (Keymap map, InfoCommand *cmd)
1064 if (!where_is_rep_size)
1065 where_is_rep = (char *)xmalloc (where_is_rep_size = 100);
1066 where_is_rep_index = 0;
1068 rep = where_is_internal (map, cmd);
1070 /* If it couldn't be found, return "M-x Foo" (or equivalent). */
1075 name = function_name (cmd);
1077 return NULL; /* no such function */
1079 rep = where_is_internal (map, InfoCmd(info_execute_command));
1081 return ""; /* function exists but can't be got to by user */
1083 sprintf (where_is_rep, "%s %s", rep, name);
1090 /* Return the printed rep of the keystrokes that invoke FUNCTION,
1091 as found in MAP, or NULL. */
1093 where_is_internal (Keymap map, InfoCommand *cmd)
1095 #if defined(INFOKEY)
1097 register FUNCTION_KEYSEQ *k;
1099 for (k = cmd->keys; k; k = k->next)
1101 return pretty_keyseq (k->keyseq);
1105 #else /* !INFOKEY */
1106 /* There is a bug in that create_internal_info_help_node calls
1107 where_is_internal without setting where_is_rep_index to zero. This
1108 was found by Mandrake and reported by Thierry Vignaud
1109 <tvignaud@mandrakesoft.com> around April 24, 2002.
1111 I think the best fix is to make where_is_rep_index another
1112 parameter to this recursively-called function, instead of a static
1113 variable. But this [!INFOKEY] branch of the code is not enabled
1114 any more, so let's just skip the whole thing. --karl, 28sep02. */
1117 /* If the function is directly invokable in MAP, return the representation
1118 of that keystroke. */
1119 for (i = 0; i < 256; i++)
1120 if ((map[i].type == ISFUNC) && map[i].function == cmd)
1122 sprintf (where_is_rep + where_is_rep_index, "%s", pretty_keyname (i));
1123 return (where_is_rep);
1126 /* Okay, search subsequent maps for this function. */
1127 for (i = 0; i < 256; i++)
1129 if (map[i].type == ISKMAP)
1131 int saved_index = where_is_rep_index;
1134 sprintf (where_is_rep + where_is_rep_index, "%s ",
1135 pretty_keyname (i));
1137 where_is_rep_index = strlen (where_is_rep);
1138 rep = where_is_internal ((Keymap)map[i].function, cmd);
1141 return (where_is_rep);
1143 where_is_rep_index = saved_index;
1149 #endif /* INFOKEY */
1152 DECLARE_INFO_COMMAND (info_where_is,
1153 _("Show what to type to execute a given command"))
1157 command_name = read_function_name ((char *) _("Where is command: "), window);
1161 info_abort_key (active_window, count, key);
1167 InfoCommand *command;
1169 command = named_function (command_name);
1175 location = where_is (active_window->keymap, command);
1177 if (!location || !location[0])
1179 info_error ((char *) _("`%s' is not on any keys"),
1180 command_name, NULL);
1184 if (strstr (location, function_name (command)))
1185 window_message_in_echo_area
1186 ((char *) _("%s can only be invoked via %s."),
1187 command_name, location);
1189 window_message_in_echo_area
1190 ((char *) _("%s can be invoked via %s."),
1191 command_name, location);
1195 info_error ((char *) _("There is no function named `%s'"),
1196 command_name, NULL);
1199 free (command_name);