1;; This file is part of Imprimatur.
2;; Copyright (C) 2006, 2007, 2010, 2011 Sergey Poznyakoff
3;;
4;; Imprimatur is free software; you can redistribute it and/or modify
5;; it under the terms of the GNU General Public License as published by
6;; the Free Software Foundation; either version 3, or (at your option)
7;; any later version.
8;;
9;; Imprimatur is distributed in the hope that it will be useful,
10;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12;; GNU General Public License for more details.
13;;
14;; You should have received a copy of the GNU General Public License
15;; along with Imprimatur.  If not, see <http://www.gnu.org/licenses/>.
16
17;;; Commentary:
18
19;; This file redefines texinfo-master-menu-list so that it takes into
20;; account included files.
21
22;; Known bugs: @menu without previous sectioning command will inherit
23;; documentation string from the previous menu. However, since such a
24;; menu is illegal in a texinfo file, we can live with it.
25
26(require 'texinfo)
27(require 'texnfo-upd)
28
29(defun texinfo-master-menu-list-recursive (title)
30  "Auxiliary function used by `texinfo-master-menu-list'."
31  (save-excursion
32    (let (master-menu-list)
33      (while (re-search-forward "\\(^@menu\\|^@include\\)" nil t)
34	(cond
35	 ((string= (match-string 0) "@include")
36	  (skip-chars-forward " \t")
37	  (let ((included-name (let ((start (point)))
38				 (end-of-line)
39				 (skip-chars-backward " \t")
40				 (buffer-substring start (point)))))
41	    (end-of-line)
42	    (let ((prev-title (texinfo-copy-menu-title)))
43	      (save-excursion
44		(set-buffer (find-file-noselect included-name))
45		(setq master-menu-list
46		      (append (texinfo-master-menu-list-recursive prev-title)
47			      master-menu-list))))))
48	 (t
49	  (setq master-menu-list
50		(cons (list
51		       (texinfo-copy-menu)
52		       (let ((menu-title (texinfo-copy-menu-title)))
53			 (if (string= menu-title "")
54			     title
55			   menu-title)))
56		      master-menu-list)))))
57      master-menu-list)))
58
59(defun texinfo-master-menu-list ()
60  "Return a list of menu entries and header lines for the master menu,
61recursing into included files.
62
63Start with the menu for chapters and indices and then find each
64following menu and the title of the node preceding that menu.
65
66The master menu list has this form:
67
68    \(\(\(... \"entry-1-2\"  \"entry-1\"\) \"title-1\"\)
69      \(\(... \"entry-2-2\"  \"entry-2-1\"\) \"title-2\"\)
70      ...\)
71
72However, there does not need to be a title field."
73
74  (reverse (texinfo-master-menu-list-recursive "")))
75
76(defun make-master-menu ()
77  "Create master menu in the first Emacs argument."
78  (find-file (car command-line-args-left))
79  (texinfo-master-menu nil)
80  (save-buffer))
81
82
83;;; mastermenu.el ends here
84