TABLE OF CONTENTS


::pwtk::display

SYNOPSIS

proc ::pwtk::display {args} {

USAGE

   display  ?-type DOCUMENT_TYPE?  FILE1  ?FILE2? ... ?&?

PURPOSE

Display document(s) with a viewer. By default, the document type is obtained from the document file extension, unless the document type is explicitly given with the -type option.

If the last argument is "&", then the document viewer is executed in the background mode.

OPTION

ARGUMENTS

SOURCE

    set lastarg [lindex $args end]
    set bg {}
    if { $lastarg eq "&" } {
        set bg &
        set args [lrange $args 0 end-1]
    }
    ifexist ::env(PWTK_PAUSE) {
        # check if PWTK_PAUSE is a positive number: in this case open the viewer for $PWTK_PAUSE seconds
        if { [::pwtk::type::number posreal -strict $::env(PWTK_PAUSE)] } {
            set bg &
            set pause [time2ms ${::env(PWTK_PAUSE)}sec]
        }
    }
    
    set nargmin 1
    set nargmax -1    
    set usage "-type DOCUMENT_TYPE  FILE1  ?FILE2? ..."
    set options {
        {type.arg {} "type of the document (eps, pdf, png...)"}
    }
    ::pwtk::parseOpt_
    
    foreach document $args {    
        if { $opt(type) eq {} } {
            # get the doc type from the file extension
            set ext [string trimleft [file extension $document] .]
        } else {
            set ext $opt(type)
        }

        switch -nocase -- $ext {
            ps - eps - pdf {
                set viewer [docViewer]
            }
            svg {
                set viewer [svgViewer]
            }
            default {
                set viewer [imgViewer]
            }
        }

        if { $viewer != {} } {
            try {
                set pid [exec -ignorestderr $viewer $document {*}$bg]
                ifexist pause {
                    # kill the viewer after $pause milliseconds
                    # (we use "catch" because a user may already closed the window)
                    delayed_eval $pause catch [list exec kill $pid]
                }
            } on error err {
                ::pwtk::error "an error occured while trying to display the document \"$document\"\n\n$err"
            }
        } else {
            ::pwtk::error "cannot display \"$document\"; no usable viewer found"
        }
    }
}