1 /* footnotes.c -- Some functions for manipulating footnotes.
2 $Id: footnotes.c,v 1.9 2008/06/11 09:55:42 gray Exp $
4 Copyright (C) 1993, 1997, 1998, 1999, 2002, 2004, 2007,
5 2008 Free Software Foundation, Inc.
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 3 of the License, or
10 (at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
20 Originally written by Brian Fox (bfox@ai.mit.edu). */
24 /* Nonzero means attempt to show footnotes when displaying a new window. */
25 int auto_footnotes_p = 0;
27 static char *footnote_nodename = "*Footnotes*";
29 NODE * make_footnotes_node (NODE *node);
31 #define FOOTNOTE_HEADER_FORMAT \
32 "*** Footnotes appearing in the node `%s' ***\n"
34 /* Find the window currently showing footnotes. */
36 find_footnotes_window (void)
40 /* Try to find an existing window first. */
41 for (win = windows; win; win = win->next)
42 if (internal_info_node_p (win->node) &&
43 (strcmp (win->node->nodename, footnote_nodename) == 0))
49 /* Manufacture a node containing the footnotes of this node, and
50 return the manufactured node. If NODE has no footnotes, return a
53 make_footnotes_node (NODE *node)
55 NODE *fn_node, *result = NULL;
58 /* Make the initial assumption that the footnotes appear as simple
59 text within this windows node. */
62 /* See if this node contains the magic footnote label. */
64 info_search_in_node (FOOTNOTE_LABEL, node, 0, NULL, 1, 0);
66 /* If it doesn't, check to see if it has an associated footnotes node. */
71 refs = info_xrefs_of_node (node);
77 int reflen = strlen ("-Footnotes") + strlen (node->nodename);
79 refname = xmalloc (reflen + 1);
81 strcpy (refname, node->nodename);
82 strcat (refname, "-Footnotes");
84 for (i = 0; refs[i]; i++)
85 if ((refs[i]->nodename != NULL) &&
86 /* Support both the older "foo-Footnotes" and the new
87 style "foo-Footnote-NN" references. */
88 (strcmp (refs[i]->nodename, refname) == 0 ||
89 (strncmp (refs[i]->nodename, refname, reflen - 1) == 0 &&
90 refs[i]->nodename[reflen - 1] == '-' &&
91 isdigit (refs[i]->nodename[reflen]))))
95 filename = node->parent;
97 filename = node->filename;
99 fn_node = info_get_node (filename, refname);
108 info_free_references (refs);
112 /* If we never found the start of a footnotes area, quit now. */
116 /* Make the new node. */
117 result = xmalloc (sizeof (NODE));
119 result->display_pos = 0;
121 /* Get the size of the footnotes appearing within this node. */
124 long text_start = fn_start;
127 (1 + strlen (node->nodename) + strlen (FOOTNOTE_HEADER_FORMAT));
128 sprintf (header, FOOTNOTE_HEADER_FORMAT, node->nodename);
130 /* Move the start of the displayed text to right after the first line.
131 This effectively skips either "---- footno...", or "File: foo...". */
132 while (text_start < fn_node->nodelen)
133 if (fn_node->contents[text_start++] == '\n')
136 result->nodelen = strlen (header) + fn_node->nodelen - text_start;
138 /* Set the contents of this node. */
139 result->contents = xmalloc (1 + result->nodelen);
140 sprintf (result->contents, "%s", header);
141 memcpy (result->contents + strlen (header),
142 fn_node->contents + text_start, fn_node->nodelen - text_start);
144 name_internal_node (result, footnote_nodename);
149 /* If the footnotes were gleaned from the node that we were called with,
150 shorten the calling node's display length. */
152 narrow_node (node, 0, fn_start);
158 /* Create or delete the footnotes window depending on whether footnotes
159 exist in WINDOW's node or not. Returns FN_FOUND if footnotes were found
160 and displayed. Returns FN_UNFOUND if there were no footnotes found
161 in WINDOW's node. Returns FN_UNABLE if there were footnotes, but the
162 window to show them couldn't be made. */
164 info_get_or_remove_footnotes (WINDOW *window)
169 fn_win = find_footnotes_window ();
171 /* If we are in the footnotes window, change nothing. */
172 if (fn_win == window)
175 /* Try to find footnotes for this window's node. */
176 new_footnotes = make_footnotes_node (window->node);
178 /* If there was a window showing footnotes, and there are no footnotes
179 for the current window, delete the old footnote window. */
180 if (fn_win && !new_footnotes)
183 info_delete_window_internal (fn_win);
186 /* If there are footnotes for this window's node, but no window around
187 showing footnotes, try to make a new window. */
188 if (new_footnotes && !fn_win)
193 /* Always make this window be the last one appearing in the list. Find
194 the last window in the chain. */
195 for (win = windows, last = windows; win; last = win, win = win->next);
197 /* Try to split this window, and make the split window the one to
198 contain the footnotes. */
199 old_active = active_window;
200 active_window = last;
201 fn_win = window_make_window (new_footnotes);
202 active_window = old_active;
206 free (new_footnotes->contents);
207 free (new_footnotes);
209 /* If we are hacking automatic footnotes, and there are footnotes
210 but we couldn't display them, print a message to that effect. */
211 if (auto_footnotes_p)
212 inform_in_echo_area (_("Footnotes could not be displayed"));
217 /* If there are footnotes, and there is a window to display them,
218 make that window be the number of lines appearing in the footnotes. */
219 if (new_footnotes && fn_win)
221 window_set_node_of_window (fn_win, new_footnotes);
223 window_change_window_height
224 (fn_win, fn_win->line_count - fn_win->height);
226 remember_window_and_node (fn_win, new_footnotes);
227 add_gcable_pointer (new_footnotes->contents);
236 /* Show the footnotes associated with this node in another window. */
237 DECLARE_INFO_COMMAND (info_show_footnotes,
238 _("Show the footnotes associated with this node in another window"))
240 /* A negative argument means just make the window go away. */
243 WINDOW *fn_win = find_footnotes_window ();
245 /* If there is an old footnotes window, and it isn't the only window
246 on the screen, delete it. */
247 if (fn_win && windows->next)
248 info_delete_window_internal (fn_win);
254 result = info_get_or_remove_footnotes (window);
259 info_error (msg_no_foot_node, NULL, NULL);
263 info_error (msg_win_too_small, NULL, NULL);