Index: Latex.tmbundle/Commands/LaTeX and view.plist
===================================================================
--- Latex.tmbundle/Commands/LaTeX and view.plist	(revision 7740)
+++ Latex.tmbundle/Commands/LaTeX and view.plist	(working copy)
@@ -4,6 +4,8 @@
 <dict>
 	<key>beforeRunningCommand</key>
 	<string>saveModifiedFiles</string>
+	<key>bundleUUID</key>
+	<string>46788DCE-6227-11D9-BFB1-000D93589AF6</string>
 	<key>command</key>
 	<string># Source some support functions we need.
 . "${TM_SUPPORT_PATH}/lib/html.sh"
@@ -13,10 +15,10 @@
 # Get the viewer program. Any program (that works with open -a &lt;name&gt;) can be used,
 # except 'html' which is reserved to mean the internal HTML window. This is also the
 # default option.
-V=${TM_LATEX_VIEWER:-html}
-EL=${TM_LATEX_ERRLVL:-1}
-M=${TM_LATEX_MASTER:-$TM_FILEPATH}
-DIR=`dirname "$M"`
+V="${TM_LATEX_VIEWER:-html}"
+EL="${TM_LATEX_ERRLVL:-1}"
+M="${TM_LATEX_MASTER:-$TM_FILEPATH}"
+DIR="$(dirname "$M")"
 
 parse_options "$M"
 # Now parse the options from the master file (if any)
@@ -31,29 +33,42 @@
   else M="$DIR/$OPTIONS_root"
   fi
   unset -v OPTIONS_root
-  DIR=`dirname "$M"`
+  DIR="$(dirname "$M")"
   parse_options "$M"
 done
 
+# Support the legacy TEX_PSTRICKS setting
+if [[ -z "$TM_LATEX_PROGRAM" &amp;&amp; "$TEX_PSTRICKS" == 1 ]]; then
+	TM_LATEX_PROGRAM=latex
+fi
+
+# Allow per-file setting to over-ride global default
+# (Make an exception for latexmk, for backwards compatibility.)
+if [ "$TM_LATEX_PROGRAM" != "latexmk.pl" ]; then
+    TM_LATEX_PROGRAM="${OPTIONS_program:-${TM_LATEX_PROGRAM:-pdflatex}}"
+fi
+
 # More variables
-FILE=`basename "$M"`
+FILE="$(basename "$M")"
 PDF="${FILE%.*}.pdf"
 DVI="${FILE%.*}.dvi"
 PS="${FILE%.*}.ps"
 CWD="`pwd`/"
 
 # Set up TeX compiler, fallback to xelatex if document indicates it, 
-if [[ "$OPTIONS_program" == "xelatex" ]] || grep -Esq '^[^%]*\\usepackage.*(xunicode|fontspec)|program=xelatex' "$M"; then
+if [[ "$TM_LATEX_PROGRAM" == "xelatex" ]] || grep -Esq '^[^%]*\\usepackage.*(xunicode|fontspec)|program=xelatex' "$M"; then
   DEF_TEX=xelatex
 # or to latex+dvi2ps if document indicates it, or if pstricks is there
-elif [[ "$OPTIONS_program" == "latex" ]] || grep -Esq '^[^%]*\\usepackage.*(pstricks|xyling|pst-asr|OTtablx)' "$M"; then
+elif [[ "$TM_LATEX_PROGRAM" == "latex" ]] || grep -Esq '^[^%]*\\usepackage.*(pstricks|xyling|pst-asr|OTtablx)' "$M"; then
   DEF_TEX=latex
-  TEX_PSTRICKS=1
 else
-  DEF_TEX=pdflatex
+  # If we don't recognise the setting, try it anyway
+  DEF_TEX="$TM_LATEX_PROGRAM"
 fi
-TEX=${TM_LATEX_COMPILER:-$DEF_TEX}
 
+# Allow TM_LATEX_COMPILER to over-ride everything (e.g. for latexmk)
+TEX="${TM_LATEX_COMPILER:-$DEF_TEX}"
+
 # Add our tex directory to the TEXINPUTS variable so e.g. pdfsync is found (even when the user hasn’t installed it)
 # preceeding colon stands for default search paths. Search them first so if user installed pdfsync it'll use that one
 export TEXINPUTS="$(kpsewhich -progname "$TEX" --expand-var '$TEXINPUTS'):$TM_BUNDLE_SUPPORT/tex//"
@@ -61,32 +76,33 @@
 # Switch to the right directory.
 cd "$TM_PROJECT_DIRECTORY"
 cd "$DIR"
+echo "&lt;!-- $DIR / $(pwd) --&gt;"
 
 # Prepare output window.
 html_header 'Typeset &amp;amp; View' "$FILE"
 echo '&lt;h2&gt;Compiling LaTeX…&lt;/h2&gt;'
 
 # Check if we actually have a tex compiler
-require_cmd "$TEX" "You can either set the &lt;tt&gt;TM_LATEX_COMPILER&lt;/tt&gt; variable to the full path of your LaTeX compiler (e.g. &lt;tt&gt;/opt/local/bin/pdflatex&lt;/tt&gt; or set the &lt;tt&gt;PATH&lt;/tt&gt; variable to include the path of &lt;tt&gt;pdflatex&lt;/tt&gt;."
+require_cmd "$TEX" "You can set the &lt;tt&gt;PATH&lt;/tt&gt; variable to include the path of &lt;tt&gt;$TM_LATEX_PROGRAM&lt;/tt&gt;."
 
 
-run_tex () {
+tex_command () {
 if [ "$TEX" != latexmk.pl ]
-  then "$TEX" ${TM_LATEX_OPTIONS:=-interaction=nonstopmode -file-line-error-style} "$1"
+  then echo "'$TEX' ${TM_LATEX_OPTIONS:=-interaction=nonstopmode -file-line-error-style}"
   else
-    if [[ $TEX_PSTRICKS ]]
+    if [[ "$TM_LATEX_PROGRAM" == latex ]]
       then
-        "$TEX" -f -ps "$1"
+        echo "'$TEX' -f -ps"
       else
-        "$TEX" -f -r "${TM_BUNDLE_SUPPORT}/latexmkrc" "$1"
+        echo "'$TEX' -f -r '${TM_BUNDLE_SUPPORT}/latexmkrc'"
     fi
 fi
 }
 
 # Compile.
-run_tex "$FILE" 2&gt;&amp;1| latexErrWarnHtml.py -v
+latexErrWarnHtml.py "$(tex_command)" "$FILE"
 RC=$?
-if [[ $TEX_PSTRICKS ]]; then
+if [[ "$TM_LATEX_PROGRAM" == latex ]]; then
   dvips "$DVI" -o "$PS" &amp;&amp; ps2pdf "$PS"
 fi
 # If there is output and it's newer than the current document, then we go directly to preview or link to it.
@@ -94,14 +110,14 @@
   if [[ "$V" == html ]]; then
     WEB_LOC="tm-file://${DIR}/${PDF}"
     WEB_LOC="${WEB_LOC//\\#/%23}"
-    if (($RC &lt; 3)); then
-      if (($RC &gt;= $EL))
+    if (("$RC" &lt; 3)); then
+      if (("$RC" &gt;= "$EL"))
         then link "${WEB_LOC}" 'Click Here to Preview'
         else redirect "${WEB_LOC}"
       fi
     fi
   else
-    V=${V%.app} # strip potential .app extension
+    V="${V%.app}" # strip potential .app extension
     if find_app &gt;/dev/null "$V.app"; then
       open -a "$V" "$PDF"
       if [[ "$V" == TeXniscope &amp;&amp; -f "${PDF}sync" ]]; then
@@ -116,7 +132,7 @@
         SCRIPT="$(find_app Skim.app)/Contents/Resources/displayline"
         [[ -x "$SCRIPT" ]] &amp;&amp; "$SCRIPT" "$TM_LINE_NUMBER" "$PDF" "$TM_FILEPATH"
       fi
-      if (($RC &lt; $EL)); then exit_discard; fi
+      if (("$RC" &lt; "$EL")); then exit_discard; fi
     else
       strong "Error locating external viewer: ‘${V}’"
     fi
Index: Latex.tmbundle/Support/lib/options.rb
===================================================================
--- Latex.tmbundle/Support/lib/options.rb	(revision 7740)
+++ Latex.tmbundle/Support/lib/options.rb	(working copy)
@@ -16,12 +16,12 @@
       File.open(filepath, "r") do |file|
         1.upto(20) do
           line = file.readline
-          if line =~ /^%!TEX (?>(.*?) )= (.*?) *$/
+          if line =~ /^%!TEX (\S*) =\s*(.*)\s*$/
             opts[$1] = $2
           end
         end
       end
-    rescue EOFError, Errno::ENOENT
+    rescue EOFError
       # Don't do anything
     end
     opts
Index: Latex.tmbundle/Support/bin/latexErrWarnHtml.py
===================================================================
--- Latex.tmbundle/Support/bin/latexErrWarnHtml.py	(revision 7740)
+++ Latex.tmbundle/Support/bin/latexErrWarnHtml.py	(working copy)
@@ -19,10 +19,19 @@
 incPat = re.compile('.*\<use (.*?)\>');
 miscWarnPat = re.compile('LaTeX Warning:.*')
 
-if sys.argv[0] == '-v':
+verbose = False
+if len(sys.argv) > 1 and sys.argv[1] == '-v':
     verbose = True
+    sys.argv[1:] = sys.argv[2:]
+if len(sys.argv) == 3:
+    texCommand = sys.argv[1]
+    fileName = sys.argv[2]
 else:
-    verbose = False
+    sys.stderr.write("Usage: "+argv[0]+" [-v] tex-command file.tex")
+    sys.exit(255)
+
+tex = os.popen(texCommand+" "+fileName)
+
 numWarns = 0
 numErrs = 0
 numRuns = 0
@@ -30,7 +39,7 @@
 isFatal = False
 
 print '<pre>'
-line = sys.stdin.readline()
+line = tex.readline()
 while line:
     line = line.rstrip("\n")
     # print out first line
@@ -136,13 +145,21 @@
     else:
         if verbose:
             print line[:-1]
-    line = sys.stdin.readline()
+    line = tex.readline()
+texStatus = tex.close()
 eCode = 0
-if numWarns > 0 or numErrs > 0:
+if texStatus != None or numWarns > 0 or numErrs > 0:
     print "Found " + str(numErrs) + " errors, and " + str(numWarns) + " warnings in " + str(numRuns) + " runs"
+    if texStatus != None:
+        signal = (texStatus & 255)
+        returnCode = texStatus >> 8
+        if signal != 0:
+            print "TeX killed by signal " + str(signal)
+        else:
+            print "TeX exited with error code " + str(returnCode)
     if isFatal:
         eCode = 3
-    elif numErrs > 0:
+    elif numErrs > 0 or texStatus != None:
         eCode = 2
     else:
         eCode = 1
