I've attached a commented/modified version of pdfview.nsi. There are some initial questions inside the file, there will likely be more. To archive the discussion, I'm CC:ing the developers list.

Some statistics:   Lines  Blank lines   Comment lines   Inline comments
pdfview.nsi        174    53            22              2
pdfview.mod.nsi    186    40            63              29

Since I don't know what is most convenient, and the file isn't that long, I also pasted the file on this page

        http://wiki.lyx.org/Devel/WindowsInstaller-pdfview

Maybe it'd be better if I committed it under another name, I don't know.


Here are some of the larger questions I have:

* What is the syntax of using pdfview.exe?

* Should it be possible to do 'pdview.exe -h' ?

* Why write this application in NSIS?

* Where are pdfopen/pdfclose stored?

* Where/how are they installed?

* Would this application be useful on non-Windows platforms?

* Does it matter that lyxc.exe, lyx.exe (launcher) and pdfview.exe all use
  the same icon?

* I thought $R0, $R1 etc were well suited as "dummy" variables, is there a
  reason for not using them? (I've used them in the modified version,
  but maybe it's just my aversion to global variables that's exposed here)

* This thing with macros still confuse me.... Must HideConsole be a macro?

Cheers
/Christian

--
Christian Ridderström, +46-8-768 39 44               http://www.md.kth.se/~chr
# 
# Windows PDF view helper
# Author: Joost Verburg
#
# This will be installed as pdfview.exe.
#
# This application is not an installer. It is a helper application for
# LyX that will launch the default PDF viewer to display a PDF file,
# but works around the file locking problems of Adobe Reader.
#
# Source code of pdfopen/pdfclose is available at:
#       http://magic.aladdin.cs.cmu.edu/2005/07/15/pdfopen-and-pdfclose/
#

# Include standard NSIS libraries
!include "LogicLib.nsh" # http://nsis.sourceforge.net/LogicLib
!include "FileFunc.nsh" # http://nsis.sourceforge.net/Docs/AppendixE.html#E.1

!insertmacro GetParameters      # Insert definition of function GetParameters
!insertmacro GetFileName        # Insert definition of function GetFileName ??

# --------------------------------
# Settings

Caption "PDF Viewer"    # Where will this caption be shown????
OutFile pdfview.exe                     # Set name of resulting binary
Icon "..\packaging\icons\lyx_32x32.ico" # Icon for start menu. Eh, same icon?
SilentInstall silent                    # The "installer" should be silent ??

# --------------------------------
# Constants

# ?? Why not use from LogicLib?         - are these really used?
#!define FALSE 0                # Can they be removed?
#!define TRUE 1                 # Can they be removed?

# --------------------------------
# Variables

Var Dummy               # For temporary use.  ?? Why not use $R1 etc? ??
Var OriginalFile        # Path to PDF file from LyX to be viewed
Var OriginalFileName    # ???
Var PDFFile             # Path to temporary copy of PDF used with Adobe viewer
Var Viewer              # Name of viewer for PDF files
Var OriginalTimeHigh    # Part of latest modificaion time of shown file
Var OriginalTimeLow     # Part of latest modificaion time of shown file

#--------------------------------
#PDF vieweing

Section "View PDF file"

  InitPluginsDir #Temporary directory for PDF file

  #Command line parameters
  Call GetParameters
  Pop $OriginalFile

  #Trim quotes
  StrCpy $Dummy $OriginalFile 1
  ${if} $Dummy == '"'
    StrCpy $OriginalFile $OriginalFile "" 1
  ${endif}
  StrCpy $Dummy $OriginalFile 1 -1
  ${if} $Dummy == '"'
    StrCpy $OriginalFile $OriginalFile -1
  ${endif}

  GetFullPathName $OriginalFile $OriginalFile
  Push $OriginalFile
  Call GetFileName
  Pop $OriginalFileName

  SetOutPath $TEMP #The LyX tmpbuf should not be locked

  StrCpy $PDFFile $PLUGINSDIR\$OriginalFileName

  # Check whether the file will be opened with Adobe Reader or Adobe Acrobat
  Push $OriginalFile
  Push "shell32::FindExecutable(t s, t '', t .s)"
  CallInstDLL "$EXEDIR\System.dll" Call
  Call GetFileName
  Pop $Viewer

  ${if} $Viewer == ""
    MessageBox MB_OK|MB_ICONEXCLAMATION "No PDF viewer is installed. \
        Please install a PDF viewer such as Adobe Reader."
    Quit        
  ${endif}

  ${if} $Viewer != "AcroRd32.exe"
  ${andif} $Viewer != "Acrobat.exe"     # Non-Adobe PDF viewer used?
    # No need for special actions, just forward to ShellExecute
    # ??? ShellExecute or ExecShell ???
    ExecShell open $OriginalFile
    
  ${else}                               # Using Adobe PDF viewer?
  
    # Store modification time of the original file
    GetFileTime $OriginalFile $OriginalTimeHigh $OriginalTimeLow
    
    Call Open_copy_with_Adobe           # Open a copy with Adobe
    
    ${do}                               # Loop until Adobe closes
      Sleep 500                         # Wait 0.5 s ???

      # What does 'a' mean??
      FileOpen $Dummy $PDFFile a        # Try opening temporary copy
      
      ${if} $Dummy != ""                # File no longer locked, reader closed?
        FileClose $Dummy
        Delete $PDFFile                 # Remove temporary copy
        Quit                            # and quit
      ${endif}

      # Why would the original file disappear? Maybe close Adobe then?

      # !! I wouldn't mind changing $Current... to e.g. $R0, $R1
      # !! Setting $OriginalTime.. could for clarity be done outside function.
      ${if} ${fileexists} $OriginalFile

        # Check if the original file has been modified
        GetFileTime $OriginalFile $R0 $R1 
        ${if} $OriginalTimeHigh != $R0
        ${orif} $OriginalTimeLow != $R1
        
          # Store modification time of the original file
          GetFileTime $OriginalFile $OriginalTimeHigh $OriginalTimeLow
          
          Call Open_copy_with_Adobe     # Open a copy with Adobe
          
        ${endif}
      ${endif}
    ${loop}                             # End of loop until Adobe closes
  ${endif}
    
SectionEnd

#--------------------------------
#Macros
#
# Macro: Hide the console (which console?)
# In:   COMMAND_LINE = Command that is to be executed (as a command line)
# Out:  -
#
# !! If this doesn't have to be a macro, I think it could be
# !! expanded in the new function Open_copy_with_Adobe()
# 
!macro HideConsole COMMAND_LINE

  Push `${COMMAND_LINE}`
  CallInstDLL "$EXEDIR\Console.dll" Exec
  Pop $Dummy
  
  ${if} $Dummy == "error"
    MessageBox MB_OK|MB_ICONSTOP "Error opening PDF file $R0."
  ${endif}

!macroend

#
# Copy a file from LyX, $OriginalFile, to a temporary file, $PDFFile,
# and then open the temporary file using pdfopen.exe. This leaves the
# original file from LyX unlocked, so that LyX can later overwrite it.
# In addition, set the variables keeping track of the modification time
# of the original file.
#
# In:   $OriginalFile   = Name of file to be copied to $PDFFile
#       $PDFFile        = Name of temporary file that is viewed
#       $Viewer         = ?
# Out:  $OriginalTimeHigh = Modification time of $OriginalFile, high part
#       $OriginalTimeLow  = Modification time of $OriginalFile, low part
# Note: What if LyX modifies $OriginalFile while it is copied?
# 
Function Open_copy_with_Adobe 
  ${if} ${fileexists} $PDFFile          # Close any existing view
    !insertmacro HideConsole '"$EXEDIR\pdfclose.exe" --file "$PDFFile"'
  ${endif}
    
  # Copy $OriginalFile to $PDFFile
  CopyFiles /SILENT $OriginalFile $PDFFile
    
  # Open a new view
  !insertmacro HideConsole '"$EXEDIR\pdfopen.exe" --back --file "$PDFFile"'
    
FunctionEnd

Reply via email to