Repository: hannah-sten/texify-idea
Issue: Fix error message when forward search is used before compilation

### Description
In PyCharm, on compiling a latex file (stand-alone, not in a project), pdf is not opened automatically. When I tried to open PDF using Tools>Evince>Forward Search, this error occurred. I'm using Foxit Reader as default PDF reader. If that is the reason, can you suggest any alternate PDF readers for ubuntu? For some reason, Evince doesn't open in my system.

Run Configuration:
Compiler: pdfLaTeX
Separate Auxiliary files: Unchecked
Separate Output files: Unchecked
OS: Ubuntu 18.04

### Stacktrace
```
nl.hannahsten.texifyidea.TeXception: Could not execute forward search with Evince because something went wrong when finding the pdf file.
	at nl.hannahsten.texifyidea.run.evince.EvinceConversation.forwardSearch(EvinceConversation.kt:102)
	at nl.hannahsten.texifyidea.run.evince.EvinceConversation.forwardSearch$default(EvinceConversation.kt:90)
	at nl.hannahsten.texifyidea.action.evince.ForwardSearchAction.actionPerformed(ForwardSearchAction.kt:32)
	at nl.hannahsten.texifyidea.action.EditorAction.actionPerformed(EditorAction.java:98)
	at com.intellij.openapi.actionSystem.ex.ActionUtil$1.run(ActionUtil.java:265)
	at com.intellij.openapi.actionSystem.ex.ActionUtil.performActionDumbAware(ActionUtil.java:282)
	at com.intellij.openapi.actionSystem.impl.ActionMenuItem$ActionTransmitter.lambda$actionPerformed$0(ActionMenuItem.java:292)
	at com.intellij.openapi.wm.impl.FocusManagerImpl.runOnOwnContext(FocusManagerImpl.java:265)
	at com.intellij.openapi.wm.impl.IdeFocusManagerImpl.runOnOwnContext(IdeFocusManagerImpl.java:107)
	at com.intellij.openapi.actionSystem.impl.ActionMenuItem$ActionTransmitter.actionPerformed(ActionMenuItem.java:282)
	at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
	at com.intellij.openapi.actionSystem.impl.ActionMenuItem.lambda$fireActionPerformed$0(ActionMenuItem.java:111)
	at com.intellij.openapi.application.TransactionGuardImpl.runSyncTransaction(TransactionGuardImpl.java:82)
	at com.intellij.openapi.application.TransactionGuardImpl.lambda$submitTransaction$1(TransactionGuardImpl.java:106)
	at com.intellij.openapi.application.TransactionGuardImpl.submitTransaction(TransactionGuardImpl.java:115)
	at com.intellij.openapi.application.TransactionGuard.submitTransaction(TransactionGuard.java:121)
	at com.intellij.openapi.actionSystem.impl.ActionMenuItem.fireActionPerformed(ActionMenuItem.java:111)
	at com.intellij.ui.plaf.beg.BegMenuItemUI.doClick(BegMenuItemUI.java:524)
	at com.intellij.ui.plaf.beg.BegMenuItemUI.access$300(BegMenuItemUI.java:35)
	at com.intellij.ui.plaf.beg.BegMenuItemUI$MyMouseInputHandler.mouseReleased(BegMenuItemUI.java:546)
	at java.desktop/java.awt.Component.processMouseEvent(Component.java:6651)
	at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
	at java.desktop/java.awt.Component.processEvent(Component.java:6416)
	at java.desktop/java.awt.Container.processEvent(Container.java:2263)
	at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5026)
	at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
	at java.desktop/java.awt.Component.dispatchEvent(Component.java:4858)
	at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
	at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
	at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
	at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
	at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
	at java.desktop/java.awt.Component.dispatchEvent(Component.java:4858)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:778)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:727)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
	at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:751)
	at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:749)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:748)
	at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:817)
	at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:762)
	at com.intellij.ide.IdeEventQueue.lambda$dispatchEvent$8(IdeEventQueue.java:405)
	at com.intellij.openapi.progress.impl.CoreProgressManager.computePrioritized(CoreProgressManager.java:704)
	at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:404)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
```

Changed files:
src/nl/hannahsten/texifyidea/run/linuxpdfviewer/ViewerForwardSearch.kt, src/nl/hannahsten/texifyidea/action/okular/ForwardSearchAction.kt, src/nl/hannahsten/texifyidea/run/linuxpdfviewer/okular/OkularConversation.kt, src/nl/hannahsten/texifyidea/run/linuxpdfviewer/ViewerConversation.kt, src/nl/hannahsten/texifyidea/action/evince/ForwardSearchAction.kt, src/nl/hannahsten/texifyidea/run/linuxpdfviewer/OpenViewerListener.kt, src/nl/hannahsten/texifyidea/run/linuxpdfviewer/evince/EvinceConversation.kt

diff --git a/src/nl/hannahsten/texifyidea/action/evince/ForwardSearchAction.kt b/src/nl/hannahsten/texifyidea/action/evince/ForwardSearchAction.kt
index df74b73bb..f7db6ba3e 100644
--- a/src/nl/hannahsten/texifyidea/action/evince/ForwardSearchAction.kt
+++ b/src/nl/hannahsten/texifyidea/action/evince/ForwardSearchAction.kt
@@ -31,7 +31,7 @@ open class ForwardSearchAction : EditorAction(
         val document = textEditor.editor.document
         val line = document.getLineNumber(textEditor.editor.caretModel.offset) + 1
 
-        evince.conversation!!.forwardSearch(pdfPath = null, sourceFilePath = file.path, line = line)
+        evince.conversation!!.forwardSearch(pdfPath = null, sourceFilePath = file.path, line = line, project = project)
     }
 
     override fun update(e: AnActionEvent) {
diff --git a/src/nl/hannahsten/texifyidea/action/okular/ForwardSearchAction.kt b/src/nl/hannahsten/texifyidea/action/okular/ForwardSearchAction.kt
index d3b6ad76e..4b37c1c7a 100644
--- a/src/nl/hannahsten/texifyidea/action/okular/ForwardSearchAction.kt
+++ b/src/nl/hannahsten/texifyidea/action/okular/ForwardSearchAction.kt
@@ -30,7 +30,7 @@ open class ForwardSearchAction : EditorAction(
         val document = textEditor.editor.document
         val line = document.getLineNumber(textEditor.editor.caretModel.offset) + 1
 
-        okular.conversation!!.forwardSearch(null, file.path, line)
+        okular.conversation!!.forwardSearch(null, file.path, line, project)
     }
 
     override fun update(e: AnActionEvent) {
diff --git a/src/nl/hannahsten/texifyidea/run/linuxpdfviewer/OpenViewerListener.kt b/src/nl/hannahsten/texifyidea/run/linuxpdfviewer/OpenViewerListener.kt
index 985afe6a0..200c2ede9 100644
--- a/src/nl/hannahsten/texifyidea/run/linuxpdfviewer/OpenViewerListener.kt
+++ b/src/nl/hannahsten/texifyidea/run/linuxpdfviewer/OpenViewerListener.kt
@@ -2,6 +2,7 @@ package nl.hannahsten.texifyidea.run.linuxpdfviewer
 
 import com.intellij.execution.process.ProcessEvent
 import com.intellij.execution.process.ProcessListener
+import com.intellij.openapi.project.Project
 import com.intellij.openapi.util.Key
 import nl.hannahsten.texifyidea.TeXception
 import nl.hannahsten.texifyidea.run.latex.LatexRunConfiguration
@@ -14,14 +15,15 @@ class OpenViewerListener(
         private val viewer: PdfViewer,
         val runConfig: LatexRunConfiguration,
         val sourceFilePath: String,
-        val line: Int)
+        val line: Int,
+        val project: Project)
     : ProcessListener {
 
     override fun processTerminated(event: ProcessEvent) {
         if (event.exitCode == 0) {
             runAsync {
                 try {
-                    viewer.conversation!!.forwardSearch(pdfPath = runConfig.outputFilePath, sourceFilePath = sourceFilePath, line = line)
+                    viewer.conversation!!.forwardSearch(pdfPath = runConfig.outputFilePath, sourceFilePath = sourceFilePath, line = line, project = project)
                 } catch (ignored: TeXception) {
                 }
             }
diff --git a/src/nl/hannahsten/texifyidea/run/linuxpdfviewer/ViewerConversation.kt b/src/nl/hannahsten/texifyidea/run/linuxpdfviewer/ViewerConversation.kt
index a3ac1cb09..91fc462c7 100644
--- a/src/nl/hannahsten/texifyidea/run/linuxpdfviewer/ViewerConversation.kt
+++ b/src/nl/hannahsten/texifyidea/run/linuxpdfviewer/ViewerConversation.kt
@@ -1,10 +1,11 @@
 package nl.hannahsten.texifyidea.run.linuxpdfviewer
 
+import com.intellij.openapi.project.Project
 import java.io.IOException
 import java.util.concurrent.TimeUnit
 
 abstract class ViewerConversation {
-    abstract fun forwardSearch(pdfPath: String?, sourceFilePath: String, line: Int)
+    abstract fun forwardSearch(pdfPath: String?, sourceFilePath: String, line: Int, project: Project)
 }
 
 /**
diff --git a/src/nl/hannahsten/texifyidea/run/linuxpdfviewer/ViewerForwardSearch.kt b/src/nl/hannahsten/texifyidea/run/linuxpdfviewer/ViewerForwardSearch.kt
index ae1bca888..46b29684a 100644
--- a/src/nl/hannahsten/texifyidea/run/linuxpdfviewer/ViewerForwardSearch.kt
+++ b/src/nl/hannahsten/texifyidea/run/linuxpdfviewer/ViewerForwardSearch.kt
@@ -26,6 +26,6 @@ class ViewerForwardSearch(private val viewer: PdfViewer) {
         val currentPsiFile = editor.document.psiFile(environment.project) ?: return
 
         // Set the OpenOkularListener to execute when the compilation is done.
-        handler.addProcessListener(OpenViewerListener(viewer, runConfig, currentPsiFile.virtualFile.path, line))
+        handler.addProcessListener(OpenViewerListener(viewer, runConfig, currentPsiFile.virtualFile.path, line, environment.project))
     }
 }
\\ No newline at end of file
diff --git a/src/nl/hannahsten/texifyidea/run/linuxpdfviewer/evince/EvinceConversation.kt b/src/nl/hannahsten/texifyidea/run/linuxpdfviewer/evince/EvinceConversation.kt
index 42286747a..d060f8871 100644
--- a/src/nl/hannahsten/texifyidea/run/linuxpdfviewer/evince/EvinceConversation.kt
+++ b/src/nl/hannahsten/texifyidea/run/linuxpdfviewer/evince/EvinceConversation.kt
@@ -1,5 +1,8 @@
 package nl.hannahsten.texifyidea.run.linuxpdfviewer.evince
 
+import com.intellij.notification.Notification
+import com.intellij.notification.NotificationType
+import com.intellij.openapi.project.Project
 import nl.hannahsten.texifyidea.TeXception
 import nl.hannahsten.texifyidea.run.linuxpdfviewer.ViewerConversation
 import org.freedesktop.dbus.connections.impl.DBusConnection
@@ -47,7 +50,7 @@ object EvinceConversation : ViewerConversation() {
      * @param sourceFilePath Full path to the LaTeX source file.
      * @param line Line number in the source file to highlight in the pdf.
      */
-    override fun forwardSearch(pdfPath: String?, sourceFilePath: String, line: Int) {
+    override fun forwardSearch(pdfPath: String?, sourceFilePath: String, line: Int, project: Project) {
         if (pdfPath != null) {
             findProcessOwner(pdfPath)
         }
@@ -59,7 +62,13 @@ object EvinceConversation : ViewerConversation() {
             Runtime.getRuntime().exec(arrayOf("bash", "-c", command))
         }
         else {
-            throw TeXception("Could not execute forward search with Evince because something went wrong when finding the pdf file at $pdfPath")
+            // If the user used the forward search menu action
+            if (pdfPath == null) {
+                Notification("EvinceConversation", "Could not execute forward search", "Please make sure you have compiled the document first.", NotificationType.ERROR).notify(project)
+            }
+            else {
+                throw TeXception("Could not execute forward search with Evince because something went wrong when finding the pdf file at $pdfPath")
+            }
         }
     }
 
diff --git a/src/nl/hannahsten/texifyidea/run/linuxpdfviewer/okular/OkularConversation.kt b/src/nl/hannahsten/texifyidea/run/linuxpdfviewer/okular/OkularConversation.kt
index b9fc8a587..cb19c80f1 100644
--- a/src/nl/hannahsten/texifyidea/run/linuxpdfviewer/okular/OkularConversation.kt
+++ b/src/nl/hannahsten/texifyidea/run/linuxpdfviewer/okular/OkularConversation.kt
@@ -1,5 +1,8 @@
 package nl.hannahsten.texifyidea.run.linuxpdfviewer.okular
 
+import com.intellij.notification.Notification
+import com.intellij.notification.NotificationType
+import com.intellij.openapi.project.Project
 import nl.hannahsten.texifyidea.TeXception
 import nl.hannahsten.texifyidea.run.linuxpdfviewer.ViewerConversation
 
@@ -19,7 +22,7 @@ object OkularConversation : ViewerConversation() {
      * @param sourceFilePath Full path of the tex file.
      * @param line Line number in the source file to navigate to in the pdf.
      */
-    override fun forwardSearch(pdfPath: String?, sourceFilePath: String, line: Int) {
+    override fun forwardSearch(pdfPath: String?, sourceFilePath: String, line: Int, project: Project) {
         if (pdfPath != null) {
             pdfFilePath = pdfPath
         }
@@ -30,7 +33,7 @@ object OkularConversation : ViewerConversation() {
             Runtime.getRuntime().exec(arrayOf("bash", "-c", command))
         }
         else {
-            throw TeXception("Could not find the pdf file.")
+            Notification("OkularConversation", "Could not execute forward search", "Please make sure you have compiled the document first.", NotificationType.ERROR).notify(project)
         }
     }
 }
\\ No newline at end of file