# -*- tcl -*-
#
# $Id$
#
# Engine to convert a docidx document into plain text.
#
# Copyright (c) 2003 Andreas Kupries <andreas_kupries@sourceforge.net>
# Freely redistributable.
#
######################################################################

dt_source _idx_common.tcl
dt_source _text.tcl
proc c_copyrightsymbol {} {return "(c)"}

######################################################################
# Conversion specification.
# One-pass processing.

rename idx_postprocess {}
rename text_postprocess idx_postprocess
proc   fmt_plain_text {text} {return {}}

################################################################
## Backend for plain text markup

global map ; array set map {}
global key ; set key {}
global max ; set max 0

proc fmt_index_begin {label title} {
    TextInitialize

    global map ; unset map ; array set map {}
    global key ; set key {}
    global max ; set max 0

    set hdr ""
    append hdr "Index [textutil::uncap [c_provenance]]\n\n"

    if {($label != {}) && ($title != {})} {
	set title "$label -- $title"
    } elseif {$label != {}} {
	set title $label
    } elseif {$title != {}} {
	 # title is set
    }
    append hdr $title \n
    append hdr [textutil::strRepeat = [string length $title]]
    Text  $hdr
    CloseParagraph [Verbatim]
    return
}
proc fmt_index_end {} {
    global map max

    set break 0
    set rmargin [expr {80 - $max}]
    if {$rmargin < 20} {set rmargin 20}
    incr max
    set pfx [textutil::blank $max]

    foreach key [lsort [array names map]] {
	set   opfx $key[string range $pfx [string length $key] end]
	Text $opfx[textutil::indent [textutil::adjust [join $map($key) ", "] -length $rmargin] $pfx 1]
	CloseParagraph [Verbatim]
    }
    return
}
proc fmt_key {text} {
    global key max ; set key $text
    if {[string length $text] > $max} {set max [string length $text]}
    return
}
proc fmt_manpage {file label} {global map key ; lappend map($key) $file ; return}
proc fmt_url     {url label}  {global map key ; lappend map($key) $url ; return}
proc fmt_comment {text}       {return}

################################################################
