#########################################################
#########################################################
#
# pubrc for xmlspec doctype
#       version 1998 April 30 (pbg)
#
# 1999 Jan 03, added "require spec2html" at the bottom (pbg)
#
# This entire file is not necessary for operation of this
# doctype, but it defines some useful things.
#
#########################################################
#########################################################

#########################################################
#
# Some utility mappings and settings
#
#########################################################

# mappings to allow keyboard tag entry.  Note that the , key is
# the same as the < key and the . key is the same as the > key;
# so using control instead of shift and aiming for the < and >
# keys allows you to type the element name and have it inserted
# at the cursor position.

map edit control-,              compose_tag_begin
map cmd control-.               compose_tag_end

# Likewise, here are mappings to allow keyboard entity ref entry.
# Using the control key and aiming for the & and ; key allows
# you to type the entity name and have it inserted at the cursor.

alias compose_entref_begin {
        caret cmd;
        delete_line;
        insert_string 'insert_string -sgml /&';
}
alias compose_entref_end {
        insert_string ';/';
        execute;
        if ($status == 0) { caret edit; }
}
map edit control-7              compose_entref_begin
map cmd control-;               compose_entref_end

# The following alias and two mappings allows one to toggle
# between standard Adept editing and "untagged" (ASCII mode)
# editing of an entire document.  The F11 key causes the
# current document to be re-edited in standard mode, and
# Shift-F11 causes the current document to be re-edited
# in untagged (ascii) mode.

alias edit_current_untagged {
        edit -current -untagged
        find -q -noselect "<?Pub Caret"
}
execute map edit+cmd f11 edit -current
execute map edit+cmd shift-f11 edit_current_untagged

#########################################################
#
# Start of function definitions and invocations to
# implement some higher level "display view" options.
#
#########################################################

function set_tags_mode(mode)
{
# This function does a few extra things in switching tag viewing modes.
# It operates on the current doc

        switch (mode)
        {
        case "notags":
                set -local tagdisplay=none
                set hidesuppressed=on
                set showcomments=off
                set shownewlines=off
                set showemptyelement=off
                set showentities=all
                if (option("gentext")=="off") { set -local gentext=on }
                break
        case "partial":
                set -local tagdisplay=partial
                set hidesuppressed=off
                set showcomments=on
                set shownewlines=on
                set showemptyelement=on
                set showentities=text
                if (option("gentext")=="off") { set -local gentext=on }
                break
        case "full":
                set -local tagdisplay=full
                set hidesuppressed=off
                set showcomments=on
                set shownewlines=on
                set showemptyelement=on
                set showentities=none
                set -local gentext=off
                break
        default:
        }
}

function Init_menus()
{
        local doc = current_doc()
        local win = doc_window(doc)

        # Only add theses menus if the window is an edit window.
        if (window_class(win) != 'edit') { return; }

        if (menu_exists('.View') && menu_exists('.View.Full_Tags') && !menu_exists('.View.Full_Markup'))
                {
                menu_add -before .View.Full_Tags "Full Markup" -cmd {execute set_tags_mode("full")}
                menu_add -before .View.Full_Tags "Partial Markup" -cmd {execute set_tags_mode("partial")}
                menu_add -before .View.Full_Tags "No Markup" -cmd {execute set_tags_mode("notags")}
                menu_add -before .View.Full_Tags "" -separator
                }
}

Init_menus()

set_tags_mode("partial")

#########################################################
#
# redefine mouse_link() [usually mapped to double-m1]
# to do idref-to-id linking (in addition to its usual
# linking and word-highlighting.  With this redefinition,
# double-M1 inside an element with a "def" attribute will
# cause a link to the element with the corresponding id value.
# Note that a subsequent "link b" (usually mapped to
# control-shift-M1) will link back to from whence you came.
#
#########################################################

function make_companion_doc_current()
{
        local file
        local companion_doc

        if (doc_type()=='xmlspeciss')
                {
                file="c:/pbg/xsl/xsl/xsl.xml"
                }
        else
                {
                file="c:/pbg/xsl/issues/xslissues.xml"
                }

        if (path_doc(file) >= 0)
                {
                # if already open, get its doc identifier
                companion_doc = path_doc(file)
                }
        else
                {
                companion_doc = doc_open(file)
                if (companion_doc < 0)
                        {
                        message "could not open companion doc $file"
                        return
                        }
                }
        current_doc(companion_doc)

        window_show(doc_window(companion_doc),1)
        window_open(doc_window(companion_doc))
        window_raise(doc_window(companion_doc))
}

function find_issid(issid)
{
        local o = oid_first()
        while (oid_valid(o) && (oid_name(o) != "issue" || \
                                oid_attr(o,"id") != issid))
                { o = oid_forward(o) }
        if (oid_valid(o))
                {
                goto_oid(o)
                }
}

function mouse_link()
{
    # if inside issue tag, search for issue tag with same id
    # in the companion document.  If there is no companion
    # document set yet, request it.
    if (inside_tag("issue")==1)
        {
        # find ancestral issue tag
        local o, issid=""
        o = oid_caret()
        while (oid_valid(o) && oid_name(o) != "issue")
                {
                o = oid_parent(o)
                }
        if (oid_valid(o)) { issid = oid_attr(o,"id") }
        if (issid != "")
                {
                # search for issid in companion doc
                make_companion_doc_current()
                find_issid(issid)
                }
        }
    else if (oid_has_attr(oid_caret(),"def")==1)
        {
        # If the element pointed to by the mouse has a def attribute,
        # we will link to the element in this document with an id attribute
        # whose value matches that of the def attribute's value
        local o, curoid
        curoid = oid_caret()
        local def=oid_attr(curoid,"def")
        local name='#'
        # now search for an element with id="$def"
        for (o=oid_first(); oid_valid(o); o=oid_forward(o))
                {
                if (oid_has_attr(o,"id")==1)
                        {
                        name=oid_attr(o,"id")
                        if (name==def)
                                {
                                # to make "link b" work, we do "link f" from curoid,
                                # then goto o anyway.
                                # [ goto_oid(curoid) should be unnecessary here ]
                                link f
                                goto_oid(o)
                                clear_mark
                                break
                                }
                        }
                }
        if (name!=def)
                {
                mark_word(1,1,0); # if a linking fails, then start marking words
                }
        }
    else
    {
        # below is the original code
        link mouse
        if ($status)
        {
                mark_word(1,1,0); # if link fails, then start marking words
        }
    }
}

append_load_path('c:\myadept\doctypes\xmlspec\packages',1)
require spec2html

