Am Dienstag, dem 29.08.2023 um 14:52 +0200 schrieb Jürgen Spitzmüller:
> OTOH if we would make the lyxpaperview script return a path rather
> than opening found documents itself, we could use the same message
> and be more precise here.

Like in the attached.

-- 
Jürgen
diff --git a/lib/scripts/lyxpaperview.py b/lib/scripts/lyxpaperview.py
index 1eb867a416..e9bf809521 100755
--- a/lib/scripts/lyxpaperview.py
+++ b/lib/scripts/lyxpaperview.py
@@ -13,16 +13,7 @@
 # file with a name containing year and author. If found,
 # it opens the file in a viewer. 
 
-import getopt, os, sys, subprocess
-
-pdf_viewers = ('pdfview', 'kpdf', 'okular', 'qpdfview --unique',
-               'evince', 'xreader', 'kghostview', 'xpdf', 'SumatraPDF',
-               'acrobat', 'acroread', 'mupdf',
-               'gv', 'ghostview', 'AcroRd32', 'gsview64', 'gsview32')
-
-ps_viewers = ("kghostview", "okular", "qpdfview --unique",
-              "evince", "xreader", "gv", "ghostview -swap",
-              "gsview64", "gsview32")
+import os, sys, subprocess
 
 def message(message):
     sys.stderr.write("lyxpaperview: %s\n" % message)
@@ -32,7 +23,7 @@ def error(message):
     exit(1)
 
 def usage(prog_name):
-    msg = "Usage: %s [-v pdfviewer] [-w psviewer] titletoken-1 [titletoken-2] ... [titletoken-n]\n" \
+    msg = "Usage: %s titletoken-1 [titletoken-2] ... [titletoken-n]\n" \
           "    Each title token must occur in the filename (at an arbitrary position).\n" \
           "    You might use quotes to enter multi-word tokens"
     return  msg % prog_name
@@ -66,13 +57,6 @@ def find_exe(candidates):
     return None
 
 
-def find_exe_or_terminate(candidates):
-    exe = find_exe(candidates)
-    if exe == None:
-        error("Unable to find executable from '%s'" % " ".join(candidates))
-
-    return exe
-
 def find(args, path):
     if os.name != 'nt':
         # use locate if possible (faster)
@@ -107,44 +91,14 @@ def find(args, path):
 def main(argv):
     progname = argv[0]
     
-    opts, args = getopt.getopt(sys.argv[1:], "v:w:")
-    pdfviewer = ""
-    psviewer = ""
-    for o, v in opts:
-      if o == "-v":
-        pdfviewer = v
-      if o == "-w":
-        psviewer = v
+    args = sys.argv[1:]
     
     if len(args) < 1:
       error(usage(progname))
 
     result = find(args, path = os.environ["HOME"])
-    if result == "":
-        message("no document found!")
-        exit(2)
-    else:
-        message("found document %s" % result)
-
-    viewer = ""
-    if result.lower().endswith('.ps'):
-        if psviewer == "":
-            viewer = find_exe_or_terminate(ps_viewers)
-        else:
-            viewer = psviewer
-    else:
-        if pdfviewer == "":
-           viewer = find_exe_or_terminate(pdf_viewers)
-        else:
-            viewer = pdfviewer
-    
-    cmdline = viewer.split(" -", 1)
-
-    if len(cmdline) == 1:
-        subprocess.Popen([viewer, result], stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
-    elif len(cmdline) == 2:
-        subprocess.Popen([cmdline[0], "-" + cmdline[1] , result], stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
-    
+     
+    print(result)
     exit(0)
 
 if __name__ == "__main__":
diff --git a/src/frontends/qt/GuiView.cpp b/src/frontends/qt/GuiView.cpp
index 273fa383e7..66d2456019 100644
--- a/src/frontends/qt/GuiView.cpp
+++ b/src/frontends/qt/GuiView.cpp
@@ -5072,12 +5072,7 @@ void GuiView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
 
 		case LFUN_CITATION_OPEN: {
 			LASSERT(doc_buffer, break);
-			string pdfv, psv;
-			if (theFormats().getFormat("pdf"))
-				pdfv = theFormats().getFormat("pdf")->viewer();
-			if (theFormats().getFormat("ps"))
-				psv = theFormats().getFormat("ps")->viewer();
-			frontend::showTarget(argument, doc_buffer->absFileName(), pdfv, psv);
+			frontend::showTarget(argument, doc_buffer->absFileName());
 			break;
 		}
 
diff --git a/src/frontends/qt/qt_helpers.cpp b/src/frontends/qt/qt_helpers.cpp
index 230c89b52a..d4a2b264ea 100644
--- a/src/frontends/qt/qt_helpers.cpp
+++ b/src/frontends/qt/qt_helpers.cpp
@@ -295,30 +295,45 @@ void showDirectory(FileName const & directory)
 				qstring_to_ucs4(qurl.toString())));
 }
 
-void showTarget(string const & target, string const & docpath,
-		string const & pdfv, string const & psv)
+void showTarget(string const & target_in, string const & docpath)
 {
-	LYXERR(Debug::INSETS, "Showtarget:" << target << "\n");
+	LYXERR(Debug::INSETS, "Showtarget:" << target_in << "\n");
 
+	string target = target_in;
+
+	if (prefixIs(target, "EXTERNAL ")) {
+		if (!lyxrc.citation_search)
+			return;
+		string tmp, tar;
+		tar = split(target, tmp, ' ');
+		string const viewer = subst(lyxrc.citation_search_view, "$${python}", os::python());
+		string const command = viewer + " " + tar;
+		cmd_ret const ret = runCommand(commandPrep(command));
+		if (!ret.valid) {
+			// Script failed
+			frontend::Alert::error(_("Could not open file"),
+				_("The lyxpaperview script failed."));
+			return;
+		}
+		target = rtrim(ret.result, "\n");
+		if (target.empty()) {
+			frontend::Alert::error(_("Could not open file"),
+				bformat(_("No file was found using the pattern `%1$s'."),
+					from_utf8(tar)));
+			return;
+		}
+	}
 	// security measure: ask user before opening if document is not marked trusted.
 	QSettings settings;
 	if (!settings.value("trusted documents/" + toqstr(docpath), false).toBool()) {
 		QCheckBox * dontShowAgainCB = new QCheckBox();
 		dontShowAgainCB->setText(qt_("&Trust this document and do not ask me again!"));
 		dontShowAgainCB->setToolTip(qt_("If you check this, LyX will open all targets without asking for the given document in the future."));
-		docstring const warn =
-			prefixIs(target, "EXTERNAL ") ?
-					bformat(_("LyX will search your directory for files with the following keywords in their name "
-						  "and then open it in an external application, if a file is found:\n"
-						  "'%1$s'\n"
-						  "Be aware that this might entail security infringements!\n"
-						  "Only do this if you trust origin of the document and the keywords used!\n"
-						  "How do you want to proceed?"), from_utf8(target).substr(9, docstring::npos))
-				      : bformat(_("LyX wants to open the following link in an external application:\n"
-						  "%1$s\n"
-						  "Be aware that this might entail security infringements!\n"
-						  "Only do this if you trust origin of the document and the target of the link!\n"
-						  "How do you want to proceed?"), from_utf8(target));
+		docstring const warn = bformat(_("LyX wants to open the following target in an external application:\n"
+						 "%1$s\n"
+						 "Be aware that this might entail security infringements!\n"
+						 "Only do this if you trust origin of the document and the target of the link!\n"
+						 "How do you want to proceed?"), from_utf8(target));
 		QMessageBox box(QMessageBox::Warning, qt_("Open external target?"), toqstr(warn),
 				QMessageBox::NoButton, qApp->focusWidget());
 		QPushButton * openButton = box.addButton(qt_("&Open Target"), QMessageBox::ActionRole);
@@ -332,32 +347,7 @@ void showTarget(string const & target, string const & docpath,
 			settings.setValue("trusted documents/"
 				+ toqstr(docpath), true);
 	}
-	
-	if (prefixIs(target, "EXTERNAL ")) {
-		if (!lyxrc.citation_search)
-			return;
-		string tmp, tar, opts;
-		tar = split(target, tmp, ' ');
-		if (!pdfv.empty())
-			opts = " -v \"" + pdfv + "\"";
-		if (!psv.empty())
-			opts += " -w \"" + psv + "\"";
-		if (!opts.empty())
-			opts += " ";
-		Systemcall one;
-		string const viewer = subst(lyxrc.citation_search_view, "$${python}", os::python());
-		string const command = viewer + " " + opts + tar;
-		int const result = one.startscript(Systemcall::Wait, command);
-		if (result == 1)
-			// Script failed
-			frontend::Alert::error(_("Could not open file"),
-				_("The lyxpaperview script failed."));
-		else if (result == 2)
-			frontend::Alert::error(_("Could not open file"),
-				bformat(_("No file was found using the pattern `%1$s'."),
-					from_utf8(tar)));
-		return;
-	}
+
 	if (!QDesktopServices::openUrl(QUrl(toqstr(target), QUrl::TolerantMode)))
 		frontend::Alert::error(_("Could not open file"),
 			bformat(_("The target `%1$s' could not be resolved."),
diff --git a/src/frontends/qt/qt_helpers.h b/src/frontends/qt/qt_helpers.h
index dc19aea074..8dd6d258eb 100644
--- a/src/frontends/qt/qt_helpers.h
+++ b/src/frontends/qt/qt_helpers.h
@@ -93,12 +93,9 @@ void setMessageColour(std::list<QWidget *> highlighted,
 void showDirectory(support::FileName const & directory);
 /// handle request for showing citation content - shows pdf/ps or
 /// web page in target; external script can be used for pdf/ps view
-/// \p docpath holds the document path,
-/// \p pdfv takes a pad viewer, \p psv a ps viewer
+/// \p docpath holds the document path
 void showTarget(std::string const & target,
-		std::string const & docpath,
-		std::string const & pdfv,
-		std::string const & psv);
+		std::string const & docpath);
 
 } // namespace frontend
 
-- 
lyx-devel mailing list
lyx-devel@lists.lyx.org
http://lists.lyx.org/mailman/listinfo/lyx-devel

Reply via email to