   (let* ((commands (sk/command (if (not (listp opts)) '() opts)))
 	 (command (alist-get 'command commands sk/executable))
 	 (output-file (alist-get 'output-file commands)))
-    (advice-add 'term-handle-exit :after (sk/wrap-after-term-handle-exit exit-handler output-file))
+    (advice-add 'term-handle-exit :after (sk/wrap-after-term-handle-exit exit-handler commands))
     (let ((buf (get-buffer-create "*SKIM*"))
 	  (window-height (if sk/position-bottom (- sk/window-height) sk/window-height)))
       (split-window-vertically window-height)

  "^\\(.+?\\):\\([1-9][0-9]*\\):\\([1-9][0-9]*\\):"
  "A regexp pattern that parses `filename:line_num:column_num`")

(defun ag/finished-hook (buffer how-finished)
  "once finished, open the first file."
  (with-current-buffer buffer
    (compile-goto-error)))

(define-compilation-mode ag-mode "SKIM-Ag"
  "Ag results compilation mode"

      ;; write the lines into a temporary file
      (write-region (mapconcat 'identity lines "\n") nil ag-result)
      (let ((compilation-auto-jump-to-first-error t))
	(defun on-finish ()
	  (delete-file ag-result)
	  (remove-hook 'ag-search-finish-hook #'on-finish))
	(add-hook 'ag-search-finish-hook #'on-finish)
	(compilation-start (concat "cat " ag-result) #'ag-mode)))))

;;;###autoload
