Add :print --pdf flag to skip printing dialog

This commit is contained in:
Jeremy Kaplan 2016-07-10 21:42:43 -04:00
parent 3bb5b5d1c9
commit a6a030e92f
2 changed files with 14 additions and 3 deletions

View File

@ -487,12 +487,13 @@ If the pasted text contains newlines, each line gets opened in its own tab.
[[print]] [[print]]
=== print === print
Syntax: +:print [*--preview*]+ Syntax: +:print [*--preview*] [*--pdf* 'file']+
Print the current/[count]th tab. Print the current/[count]th tab.
==== optional arguments ==== optional arguments
* +*-p*+, +*--preview*+: Show preview instead of printing. * +*-p*+, +*--preview*+: Show preview instead of printing.
* +*-f*+, +*--pdf*+: The file path to write the PDF to.
==== count ==== count
The tab index to print. The tab index to print.

View File

@ -31,7 +31,7 @@ from PyQt5.QtWebKit import QWebSettings
from PyQt5.QtWidgets import QApplication, QTabBar from PyQt5.QtWidgets import QApplication, QTabBar
from PyQt5.QtCore import Qt, QUrl, QEvent from PyQt5.QtCore import Qt, QUrl, QEvent
from PyQt5.QtGui import QKeyEvent from PyQt5.QtGui import QKeyEvent
from PyQt5.QtPrintSupport import QPrintDialog, QPrintPreviewDialog from PyQt5.QtPrintSupport import QPrintDialog, QPrinter, QPrintPreviewDialog
from PyQt5.QtWebKitWidgets import QWebPage from PyQt5.QtWebKitWidgets import QWebPage
import pygments import pygments
import pygments.lexers import pygments.lexers
@ -301,12 +301,14 @@ class CommandDispatcher:
@cmdutils.register(instance='command-dispatcher', name='print', @cmdutils.register(instance='command-dispatcher', name='print',
scope='window') scope='window')
@cmdutils.argument('count', count=True) @cmdutils.argument('count', count=True)
def printpage(self, preview=False, count=None): @cmdutils.argument('pdf', flag='f')
def printpage(self, preview=False, count=None, *, pdf=None):
"""Print the current/[count]th tab. """Print the current/[count]th tab.
Args: Args:
preview: Show preview instead of printing. preview: Show preview instead of printing.
count: The tab index to print, or None. count: The tab index to print, or None.
pdf: The file path to write the PDF to.
""" """
if not qtutils.check_print_compat(): if not qtutils.check_print_compat():
# WORKAROUND (remove this when we bump the requirements to 5.3.0) # WORKAROUND (remove this when we bump the requirements to 5.3.0)
@ -322,6 +324,14 @@ class CommandDispatcher:
Qt.WindowMinimizeButtonHint) Qt.WindowMinimizeButtonHint)
diag.paintRequested.connect(tab.print) diag.paintRequested.connect(tab.print)
diag.exec_() diag.exec_()
elif pdf:
pdf = os.path.expanduser(pdf)
directory = os.path.dirname(pdf)
if directory and not os.path.exists(directory):
os.mkdir(directory)
printer = QPrinter()
printer.setOutputFileName(pdf)
tab.print(printer)
else: else:
diag = QPrintDialog() diag = QPrintDialog()
diag.setAttribute(Qt.WA_DeleteOnClose) diag.setAttribute(Qt.WA_DeleteOnClose)