Merge branch 'vendor/LDNS'
[dragonfly.git] / contrib / texinfo / info / footnotes.c
1 /* footnotes.c -- Some functions for manipulating footnotes.
2    $Id: footnotes.c,v 1.9 2008/06/11 09:55:42 gray Exp $
3
4    Copyright (C) 1993, 1997, 1998, 1999, 2002, 2004, 2007,
5    2008 Free Software Foundation, Inc.
6
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.
11
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.
16
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/>.
19
20    Originally written by Brian Fox (bfox@ai.mit.edu). */
21
22 #include "info.h"
23
24 /* Nonzero means attempt to show footnotes when displaying a new window. */
25 int auto_footnotes_p = 0;
26
27 static char *footnote_nodename = "*Footnotes*";
28
29 NODE * make_footnotes_node (NODE *node);
30
31 #define FOOTNOTE_HEADER_FORMAT \
32    "*** Footnotes appearing in the node `%s' ***\n"
33
34 /* Find the window currently showing footnotes. */
35 static WINDOW *
36 find_footnotes_window (void)
37 {
38   WINDOW *win;
39
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))
44       break;
45
46   return win;
47 }
48
49 /* Manufacture a node containing the footnotes of this node, and
50    return the manufactured node.  If NODE has no footnotes, return a 
51    NULL pointer. */
52 NODE *
53 make_footnotes_node (NODE *node)
54 {
55   NODE *fn_node, *result = NULL;
56   long fn_start;
57
58   /* Make the initial assumption that the footnotes appear as simple
59      text within this windows node. */
60   fn_node = node;
61
62   /* See if this node contains the magic footnote label. */
63   fn_start =
64     info_search_in_node (FOOTNOTE_LABEL, node, 0, NULL, 1, 0);
65
66   /* If it doesn't, check to see if it has an associated footnotes node. */
67   if (fn_start == -1)
68     {
69       REFERENCE **refs;
70
71       refs = info_xrefs_of_node (node);
72
73       if (refs)
74         {
75           register int i;
76           char *refname;
77           int reflen = strlen ("-Footnotes") + strlen (node->nodename);
78
79           refname = xmalloc (reflen + 1);
80
81           strcpy (refname, node->nodename);
82           strcat (refname, "-Footnotes");
83
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]))))
92               {
93                 char *filename;
94
95                 filename = node->parent;
96                 if (!filename)
97                   filename = node->filename;
98
99                 fn_node = info_get_node (filename, refname);
100
101                 if (fn_node)
102                   fn_start = 0;
103
104                 break;
105               }
106
107           free (refname);
108           info_free_references (refs);
109         }
110     }
111
112   /* If we never found the start of a footnotes area, quit now. */
113   if (fn_start == -1)
114     return NULL;
115
116   /* Make the new node. */
117   result = xmalloc (sizeof (NODE));
118   result->flags = 0;
119   result->display_pos = 0;
120
121   /* Get the size of the footnotes appearing within this node. */
122   {
123     char *header;
124     long text_start = fn_start;
125
126     header = xmalloc
127       (1 + strlen (node->nodename) + strlen (FOOTNOTE_HEADER_FORMAT));
128     sprintf (header, FOOTNOTE_HEADER_FORMAT, node->nodename);
129
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')
134         break;
135   
136     result->nodelen = strlen (header) + fn_node->nodelen - text_start;
137
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);
143
144     name_internal_node (result, footnote_nodename);
145     free (header);
146   }
147
148 #if defined (NOTDEF)
149   /* If the footnotes were gleaned from the node that we were called with,
150      shorten the calling node's display length. */
151   if (fn_node == node)
152     narrow_node (node, 0, fn_start);
153 #endif /* NOTDEF */
154
155   return result;
156 }
157
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. */
163 int
164 info_get_or_remove_footnotes (WINDOW *window)
165 {
166   WINDOW *fn_win;
167   NODE *new_footnotes;
168
169   fn_win = find_footnotes_window ();
170
171   /* If we are in the footnotes window, change nothing. */
172   if (fn_win == window)
173     return FN_FOUND;
174
175   /* Try to find footnotes for this window's node. */
176   new_footnotes = make_footnotes_node (window->node);
177
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)
181     {
182       if (windows->next)
183         info_delete_window_internal (fn_win);
184     }
185
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)
189     {
190       WINDOW *old_active;
191       WINDOW *last, *win;
192
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);
196
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;
203
204       if (!fn_win)
205         {
206           free (new_footnotes->contents);
207           free (new_footnotes);
208
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"));
213           return FN_UNABLE;
214         }
215     }
216
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)
220     {
221       window_set_node_of_window (fn_win, new_footnotes);
222
223       window_change_window_height
224         (fn_win, fn_win->line_count - fn_win->height);
225
226       remember_window_and_node (fn_win, new_footnotes);
227       add_gcable_pointer (new_footnotes->contents);
228     }
229
230   if (!new_footnotes)
231     return FN_UNFOUND;
232   else
233     return FN_FOUND;
234 }
235
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"))
239 {
240   /* A negative argument means just make the window go away. */
241   if (count < 0)
242     {
243       WINDOW *fn_win = find_footnotes_window ();
244
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);
249     }
250   else
251     {
252       int result;
253
254       result = info_get_or_remove_footnotes (window);
255
256       switch (result)
257         {
258         case FN_UNFOUND:
259           info_error (msg_no_foot_node, NULL, NULL);
260           break;
261
262         case FN_UNABLE:
263           info_error (msg_win_too_small, NULL, NULL);
264           break;
265         }
266     }
267 }