diff --git a/src/Pure/PIDE/document_editor.scala b/src/Pure/PIDE/document_editor.scala --- a/src/Pure/PIDE/document_editor.scala +++ b/src/Pure/PIDE/document_editor.scala @@ -1,78 +1,57 @@ /* Title: Pure/PIDE/document_editor.scala Author: Makarius Central resources and configuration for interactive document preparation. */ package isabelle object Document_Editor { /* document output */ def document_name: String = "document" def document_output_dir(): Path = Path.explode("$ISABELLE_HOME_USER/document_output") def document_output(): Path = document_output_dir() + Path.basic(document_name) def view_document(): Unit = { val path = document_output().pdf if (path.is_file) Isabelle_System.pdf_viewer(path) } - /* progress */ - - class Log_Progress(session: Session) extends Progress { - def show(text: String): Unit = {} - - private val syslog = session.make_syslog() - - private def update(text: String = syslog.content()): Unit = show(text) - private val delay = Delay.first(session.update_delay, gui = true) { update() } - - override def echo(msg: String): Unit = { syslog += msg; delay.invoke() } - - def finish(text: String): Unit = GUI_Thread.require { - delay.revoke() - update(text) - } - - GUI_Thread.later { update() } - } - - /* configuration state */ sealed case class State( session_background: Option[Sessions.Background] = None, selection: Set[Document.Node.Name] = Set.empty, views: Set[AnyRef] = Set.empty, ) { def is_active: Boolean = session_background.isDefined && views.nonEmpty def all_document_theories: List[Document.Node.Name] = session_background match { case Some(background) => background.base.all_document_theories case None => Nil } def active_document_theories: List[Document.Node.Name] = if (is_active) all_document_theories else Nil def select( names: Iterable[Document.Node.Name], set: Boolean = false, toggle: Boolean = false ): State = { copy(selection = names.foldLeft(selection) { case (sel, name) => val b = if (toggle) !selection(name) else set if (b) sel + name else sel - name }) } def register_view(id: AnyRef): State = copy(views = views + id) def unregister_view(id: AnyRef): State = copy(views = views - id) } } diff --git a/src/Pure/System/progress.scala b/src/Pure/System/progress.scala --- a/src/Pure/System/progress.scala +++ b/src/Pure/System/progress.scala @@ -1,82 +1,168 @@ /* Title: Pure/System/progress.scala Author: Makarius Progress context for system processes. */ package isabelle import java.util.{Map => JMap} import java.io.{File => JFile} object Progress { sealed case class Theory(theory: String, session: String = "", percentage: Option[Int] = None) { def message: String = print_session + print_theory + print_percentage def print_session: String = if (session == "") "" else session + ": " def print_theory: String = "theory " + theory def print_percentage: String = percentage match { case None => "" case Some(p) => " " + p + "%" } } } class Progress { def echo(msg: String): Unit = {} def echo_if(cond: Boolean, msg: String): Unit = { if (cond) echo(msg) } def theory(theory: Progress.Theory): Unit = {} def nodes_status(nodes_status: Document_Status.Nodes_Status): Unit = {} def echo_warning(msg: String): Unit = echo(Output.warning_text(msg)) def echo_error_message(msg: String): Unit = echo(Output.error_message_text(msg)) def timeit[A](body: => A, message: Exn.Result[A] => String = null, enabled: Boolean = true): A = Timing.timeit(body, message = message, enabled = enabled, output = echo) @volatile protected var is_stopped = false def stop(): Unit = { is_stopped = true } def stopped: Boolean = { if (Thread.interrupted()) is_stopped = true is_stopped } def interrupt_handler[A](e: => A): A = POSIX_Interrupt.handler { stop() } { e } def expose_interrupt(): Unit = if (stopped) throw Exn.Interrupt() override def toString: String = if (stopped) "Progress(stopped)" else "Progress" def bash(script: String, cwd: JFile = null, env: JMap[String, String] = Isabelle_System.settings(), redirect: Boolean = false, echo: Boolean = false, watchdog: Time = Time.zero, strict: Boolean = true ): Process_Result = { val result = Isabelle_System.bash(script, cwd = cwd, env = env, redirect = redirect, progress_stdout = echo_if(echo, _), progress_stderr = echo_if(echo, _), watchdog = if (watchdog.is_zero) None else Some((watchdog, _ => stopped)), strict = strict) if (strict && stopped) throw Exn.Interrupt() else result } } class Console_Progress(verbose: Boolean = false, stderr: Boolean = false) extends Progress { override def echo(msg: String): Unit = Output.writeln(msg, stdout = !stderr, include_empty = true) override def theory(theory: Progress.Theory): Unit = if (verbose) echo(theory.message) } class File_Progress(path: Path, verbose: Boolean = false) extends Progress { override def echo(msg: String): Unit = File.append(path, msg + "\n") override def theory(theory: Progress.Theory): Unit = if (verbose) echo(theory.message) override def toString: String = path.toString } + + +/* structured program progress */ + +object Program_Progress { + class Program private[Program_Progress](title: String) { + private val output_buffer = new StringBuffer(256) // synchronized + + def echo(msg: String): Unit = synchronized { + if (output_buffer.length() > 0) output_buffer.append('\n') + output_buffer.append(msg) + } + + val start_time: Time = Time.now() + private var stop_time: Option[Time] = None + def stop_now(): Unit = synchronized { stop_time = Some(Time.now()) } + + def output(heading: String): (Command.Results, XML.Body) = synchronized { + val output_text = output_buffer.toString + val elapsed_time = stop_time.map(t => t - start_time) + + val message_prefix = heading + " " + val message_suffix = + elapsed_time match { + case None => " ..." + case Some(t) => " ... (" + t.message + " elapsed time)" + } + + val (results, message) = + if (output_text.isEmpty) { + (Command.Results.empty, XML.string(message_prefix + title + message_suffix)) + } + else { + val i = Document_ID.make() + val results = Command.Results.make(List(i -> Protocol.writeln_message(output_text))) + val message = + XML.string(message_prefix) ::: + List(XML.Elem(Markup(Markup.WRITELN, Markup.Serial(i)), XML.string(title))) ::: + XML.string(message_suffix) + (results, message) + } + + (results, List(XML.Elem(Markup(Markup.TRACING_MESSAGE, Nil), message))) + } + } +} + +abstract class Program_Progress extends Progress { + private var _finished_programs: List[Program_Progress.Program] = Nil + private var _running_program: Option[Program_Progress.Program] = None + + def output(heading: String = "Running"): (Command.Results, XML.Body) = synchronized { + val programs = (_running_program.toList ::: _finished_programs).reverse + val programs_output = programs.map(_.output(heading)) + val results = Command.Results.merge(programs_output.map(_._1)) + val body = Library.separate(Pretty.Separator, programs_output.map(_._2)).flatten + (results, body) + } + + private def start_program(title: String): Unit = synchronized { + _running_program = Some(new Program_Progress.Program(title)) + } + + def stop_program(): Unit = synchronized { + _running_program match { + case Some(program) => + program.stop_now() + _finished_programs ::= program + _running_program = None + case None => + } + } + + def detect_program(s: String): Option[String] + + override def echo(msg: String): Unit = synchronized { + detect_program(msg) match { + case Some(title) => + stop_program() + start_program(title) + case None => + if (_running_program.isEmpty) start_program("program") + _running_program.get.echo(msg) + } + } +} diff --git a/src/Pure/Thy/document_build.scala b/src/Pure/Thy/document_build.scala --- a/src/Pure/Thy/document_build.scala +++ b/src/Pure/Thy/document_build.scala @@ -1,562 +1,567 @@ /* Title: Pure/Thy/document_build.scala Author: Makarius Build theory document (PDF) from session database. */ package isabelle object Document_Build { /* document variants */ abstract class Document_Name { def name: String def path: Path = Path.basic(name) override def toString: String = name } object Document_Variant { def parse(opt: String): Document_Variant = Library.space_explode('=', opt) match { case List(name) => Document_Variant(name, Latex.Tags.empty) case List(name, tags) => Document_Variant(name, Latex.Tags(tags)) case _ => error("Malformed document variant: " + quote(opt)) } } sealed case class Document_Variant(name: String, tags: Latex.Tags) extends Document_Name { def print: String = if (tags.toString.isEmpty) name else name + "=" + tags.toString } sealed case class Document_Input(name: String, sources: SHA1.Digest) extends Document_Name { override def toString: String = name } sealed case class Document_Output(name: String, sources: SHA1.Digest, log_xz: Bytes, pdf: Bytes) extends Document_Name { override def toString: String = name def log: String = log_xz.uncompress().text def log_lines: List[String] = split_lines(log) def write(db: SQL.Database, session_name: String): Unit = write_document(db, session_name, this) def write(dir: Path): Path = { val path = dir + Path.basic(name).pdf Isabelle_System.make_directory(path.expand.dir) Bytes.write(path, pdf) path } } /* SQL data model */ object Data { val session_name = SQL.Column.string("session_name").make_primary_key val name = SQL.Column.string("name").make_primary_key val sources = SQL.Column.string("sources") val log_xz = SQL.Column.bytes("log_xz") val pdf = SQL.Column.bytes("pdf") val table = SQL.Table("isabelle_documents", List(session_name, name, sources, log_xz, pdf)) def where_equal(session_name: String, name: String = ""): SQL.Source = "WHERE " + Data.session_name.equal(session_name) + (if (name == "") "" else " AND " + Data.name.equal(name)) } def read_documents(db: SQL.Database, session_name: String): List[Document_Input] = { val select = Data.table.select(List(Data.name, Data.sources), Data.where_equal(session_name)) db.using_statement(select)(stmt => stmt.execute_query().iterator({ res => val name = res.string(Data.name) val sources = res.string(Data.sources) Document_Input(name, SHA1.fake_digest(sources)) }).toList) } def read_document( db: SQL.Database, session_name: String, name: String ): Option[Document_Output] = { val select = Data.table.select(sql = Data.where_equal(session_name, name)) db.using_statement(select)({ stmt => val res = stmt.execute_query() if (res.next()) { val name = res.string(Data.name) val sources = res.string(Data.sources) val log_xz = res.bytes(Data.log_xz) val pdf = res.bytes(Data.pdf) Some(Document_Output(name, SHA1.fake_digest(sources), log_xz, pdf)) } else None }) } def write_document(db: SQL.Database, session_name: String, doc: Document_Output): Unit = { db.using_statement(Data.table.insert()){ stmt => stmt.string(1) = session_name stmt.string(2) = doc.name stmt.string(3) = doc.sources.toString stmt.bytes(4) = doc.log_xz stmt.bytes(5) = doc.pdf stmt.execute() } } /* background context */ def session_background( options: Options, session: String, dirs: List[Path] = Nil, progress: Progress = new Progress, verbose: Boolean = false ): Sessions.Background = { Sessions.load_structure(options + "document", dirs = dirs). selection_deps(Sessions.Selection.session(session), progress = progress, verbose = verbose). background(session) } /* document context */ val texinputs: Path = Path.explode("~~/lib/texinputs") val isabelle_styles: List[Path] = List("isabelle.sty", "isabellesym.sty", "pdfsetup.sty", "railsetup.sty"). map(name => texinputs + Path.basic(name)) - def running_script(title: String): String = "echo 'Running " + quote(title) + " ...'; " - def is_running_script(msg: String): Boolean = - msg.startsWith("Running \"") && msg.endsWith("\" ...") + def running_script(title: String): String = + "echo " + Bash.string("Running program \"" + title + "\" ...") + ";" + + def detect_running_script(s: String): Option[String] = + for { + s1 <- Library.try_unprefix("Running program \"", s) + s2 <- Library.try_unsuffix("\" ...", s1) + } yield s2 sealed case class Document_Latex(name: Document.Node.Name, body: XML.Body) { def content: File.Content_XML = File.content(Path.basic(tex_name(name)), body) def file_pos: String = File.symbolic_path(name.path) def write(latex_output: Latex.Output, dir: Path): Unit = content.output(latex_output.make(_, file_pos = file_pos)).write(dir) } def context( session_context: Export.Session_Context, document_session: Option[Sessions.Base] = None, document_selection: Document.Node.Name => Boolean = _ => true, progress: Progress = new Progress ): Context = new Context(session_context, document_session, document_selection, progress) final class Context private[Document_Build]( val session_context: Export.Session_Context, document_session: Option[Sessions.Base], document_selection: Document.Node.Name => Boolean, val progress: Progress ) { context => /* session info */ private val base = document_session getOrElse session_context.session_base private val info = session_context.sessions_structure(base.session_name) def session: String = info.name def options: Options = info.options override def toString: String = session val classpath: List[File.Content] = session_context.classpath() def document_bibliography: Boolean = options.bool("document_bibliography") def document_logo: Option[String] = options.string("document_logo") match { case "" => None case "_" => Some("") case name => Some(name) } def document_build: String = options.string("document_build") def get_engine(): Engine = { val name = document_build Classpath(jar_contents = classpath).make_services(classOf[Engine]) .find(_.name == name).getOrElse(error("Bad document_build engine " + quote(name))) } /* document content */ def documents: List[Document_Variant] = info.documents def session_document_theories: List[Document.Node.Name] = base.proper_session_theories def all_document_theories: List[Document.Node.Name] = base.all_document_theories lazy val isabelle_logo: Option[File.Content] = { document_logo.map(logo_name => Isabelle_System.with_tmp_file("logo", ext = "pdf") { tmp_path => Logo.create_logo(logo_name, output_file = tmp_path, quiet = true) val path = Path.basic("isabelle_logo.pdf") val content = Bytes.read(tmp_path) File.content(path, content) }) } lazy val session_graph: File.Content = { val path = Browser_Info.session_graph_path val content = graphview.Graph_File.make_pdf(options, base.session_graph_display) File.content(path, content) } lazy val session_tex: File.Content = { val path = Path.basic("session.tex") val content = Library.terminate_lines( session_document_theories.map(name => "\\input{" + tex_name(name) + "}")) File.content(path, content) } lazy val document_latex: List[Document_Latex] = for (name <- all_document_theories) yield { val body = if (document_selection(name)) { val entry = session_context(name.theory, Export.DOCUMENT_LATEX, permissive = true) YXML.parse_body(entry.text) } else Nil Document_Latex(name, body) } /* build document */ def build_document(doc: Document_Variant, verbose: Boolean = false): Document_Output = { Isabelle_System.with_tmp_dir("document") { tmp_dir => val engine = get_engine() val directory = engine.prepare_directory(context, tmp_dir, doc) engine.build_document(context, directory, verbose) } } /* document directory */ def make_directory(dir: Path, doc: Document_Variant): Path = Isabelle_System.make_directory(dir + Path.basic(doc.name)) def prepare_directory( dir: Path, doc: Document_Variant, latex_output: Latex.Output ): Directory = { val doc_dir = make_directory(dir, doc) /* actual sources: with SHA1 digest */ isabelle_styles.foreach(Isabelle_System.copy_file(_, doc_dir)) val comment_latex = latex_output.options.bool("document_comment_latex") if (!comment_latex) { Isabelle_System.copy_file(texinputs + Path.basic("comment.sty"), doc_dir) } doc.tags.sty(comment_latex).write(doc_dir) for ((base_dir, src) <- info.document_files) { Isabelle_System.copy_file_base(info.dir + base_dir, src, doc_dir) } session_tex.write(doc_dir) document_latex.foreach(_.write(latex_output, doc_dir)) val root_name1 = "root_" + doc.name val root_name = if ((doc_dir + Path.explode(root_name1).tex).is_file) root_name1 else "root" val digests1 = List(doc.print, document_logo.toString, document_build).map(SHA1.digest) val digests2 = File.find_files(doc_dir.file, follow_links = true).map(SHA1.digest) val sources = SHA1.digest_set(digests1 ::: digests2) /* derived material: without SHA1 digest */ isabelle_logo.foreach(_.write(doc_dir)) session_graph.write(doc_dir) Directory(doc_dir, doc, root_name, sources) } def old_document(directory: Directory): Option[Document_Output] = for { db <- session_context.session_db() old_doc <- read_document(db, session, directory.doc.name) if old_doc.sources == directory.sources } yield old_doc } sealed case class Directory( doc_dir: Path, doc: Document_Variant, root_name: String, sources: SHA1.Digest ) { def root_name_script(ext: String = ""): String = Bash.string(if (ext.isEmpty) root_name else root_name + "." + ext) def conditional_script( ext: String, exe: String, title: String = "", after: String = "" ): String = { "if [ -f " + root_name_script(ext) + " ]\n" + "then\n" + " " + (if (title.nonEmpty) running_script(title) else "") + exe + " " + root_name_script() + "\n" + (if (after.isEmpty) "" else " " + after) + "fi\n" } def log_errors(): List[String] = Latex.latex_errors(doc_dir, root_name) ::: Bibtex.bibtex_errors(doc_dir, root_name) def make_document(log: List[String], errors: List[String]): Document_Output = { val root_pdf = Path.basic(root_name).pdf val result_pdf = doc_dir + root_pdf if (errors.nonEmpty) { val message = "Failed to build document " + quote(doc.name) throw new Build_Error(log, errors ::: List(message)) } else if (!result_pdf.is_file) { val message = "Bad document result: expected to find " + root_pdf throw new Build_Error(log, List(message)) } else { val log_xz = Bytes(cat_lines(log)).compress() val pdf = Bytes.read(result_pdf) Document_Output(doc.name, sources, log_xz, pdf) } } } /* build engines */ abstract class Engine(val name: String) extends Isabelle_System.Service { override def toString: String = name def prepare_directory(context: Context, dir: Path, doc: Document_Variant): Directory def build_document(context: Context, directory: Directory, verbose: Boolean): Document_Output } abstract class Bash_Engine(name: String) extends Engine(name) { def prepare_directory(context: Context, dir: Path, doc: Document_Variant): Directory = context.prepare_directory(dir, doc, new Latex.Output(context.options)) def use_pdflatex: Boolean = false def running_latex: String = running_script(if (use_pdflatex) "pdflatex" else "lualatex") def latex_script(context: Context, directory: Directory): String = running_latex + (if (use_pdflatex) "$ISABELLE_PDFLATEX" else "$ISABELLE_LUALATEX") + " " + directory.root_name_script() + "\n" def bibtex_script(context: Context, directory: Directory, latex: Boolean = false): String = { val ext = if (context.document_bibliography) "aux" else "bib" directory.conditional_script(ext, "$ISABELLE_BIBTEX", title = "bibtex", after = if (latex) latex_script(context, directory) else "") } def makeindex_script(context: Context, directory: Directory, latex: Boolean = false): String = directory.conditional_script("idx", "$ISABELLE_MAKEINDEX", title = "makeindex", after = if (latex) latex_script(context, directory) else "") def use_build_script: Boolean = false def build_script(context: Context, directory: Directory): String = { val has_build_script = (directory.doc_dir + Path.explode("build")).is_file if (!use_build_script && has_build_script) { error("Unexpected document build script for option document_build=" + quote(context.document_build)) } else if (use_build_script && !has_build_script) error("Missing document build script") else if (has_build_script) "./build pdf " + Bash.string(directory.doc.name) else { "set -e\n" + latex_script(context, directory) + bibtex_script(context, directory, latex = true) + makeindex_script(context, directory) + latex_script(context, directory) + makeindex_script(context, directory, latex = true) } } def build_document( context: Context, directory: Directory, verbose: Boolean ): Document_Output = { val result = context.progress.bash( build_script(context, directory), cwd = directory.doc_dir.file, echo = verbose, watchdog = Time.seconds(0.5)) val log = result.out_lines ::: result.err_lines val err = result.err val errors1 = directory.log_errors() val errors2 = if (result.ok) errors1 else if (err.nonEmpty) err :: errors1 else if (errors1.nonEmpty) errors1 else List("Error") directory.make_document(log, errors2) } } class LuaLaTeX_Engine extends Bash_Engine("lualatex") class PDFLaTeX_Engine extends Bash_Engine("pdflatex") { override def use_pdflatex: Boolean = true } class Build_Engine extends Bash_Engine("build") { override def use_build_script: Boolean = true } class LIPIcs_Engine(name: String) extends Bash_Engine(name) { def lipics_options(options: Options): Options = options + "document_heading_prefix=" + "document_comment_latex" override def use_pdflatex: Boolean = true override def prepare_directory(context: Context, dir: Path, doc: Document_Variant): Directory = { val doc_dir = context.make_directory(dir, doc) Build_LIPIcs.document_files.foreach(Isabelle_System.copy_file(_, doc_dir)) val latex_output = new Latex.Output(lipics_options(context.options)) context.prepare_directory(dir, doc, latex_output) } } class LIPIcs_LuaLaTeX_Engine extends LIPIcs_Engine("lipics") class LIPIcs_PDFLaTeX_Engine extends LIPIcs_Engine("lipics_pdflatex") { override def use_pdflatex: Boolean = true } /* build documents */ def tex_name(name: Document.Node.Name): String = name.theory_base_name + ".tex" class Build_Error(val log_lines: List[String], val log_errors: List[String]) extends Exn.User_Error(Exn.cat_message(log_errors: _*)) def build_documents( context: Context, output_sources: Option[Path] = None, output_pdf: Option[Path] = None, verbose: Boolean = false ): List[Document_Output] = { val progress = context.progress val engine = context.get_engine() val documents = for (doc <- context.documents) yield { Isabelle_System.with_tmp_dir("document") { tmp_dir => progress.echo("Preparing " + context.session + "/" + doc.name + " ...") val start = Time.now() output_sources.foreach(engine.prepare_directory(context, _, doc)) val directory = engine.prepare_directory(context, tmp_dir, doc) val document = context.old_document(directory) getOrElse engine.build_document(context, directory, verbose) val stop = Time.now() val timing = stop - start progress.echo("Finished " + context.session + "/" + doc.name + " (" + timing.message_hms + " elapsed time)") document } } for (dir <- output_pdf; doc <- documents) { val path = doc.write(dir) progress.echo("Document at " + path.absolute) } documents } /* Isabelle tool wrapper */ val isabelle_tool = Isabelle_Tool("document", "prepare session theory document", Scala_Project.here, { args => var output_sources: Option[Path] = None var output_pdf: Option[Path] = None var verbose_latex = false var dirs: List[Path] = Nil var options = Options.init() var verbose_build = false val getopts = Getopts(""" Usage: isabelle document [OPTIONS] SESSION Options are: -O DIR output directory for LaTeX sources and resulting PDF -P DIR output directory for resulting PDF -S DIR output directory for LaTeX sources -V verbose latex -d DIR include session directory -o OPTION override Isabelle system OPTION (via NAME=VAL or NAME) -v verbose build Prepare the theory document of a session. """, "O:" -> (arg => { val dir = Path.explode(arg) output_sources = Some(dir) output_pdf = Some(dir) }), "P:" -> (arg => { output_pdf = Some(Path.explode(arg)) }), "S:" -> (arg => { output_sources = Some(Path.explode(arg)) }), "V" -> (_ => verbose_latex = true), "d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))), "o:" -> (arg => options = options + arg), "v" -> (_ => verbose_build = true)) val more_args = getopts(args) val session = more_args match { case List(a) => a case _ => getopts.usage() } val progress = new Console_Progress(verbose = verbose_build) progress.interrupt_handler { val build_results = Build.build(options, selection = Sessions.Selection.session(session), dirs = dirs, progress = progress, verbose = verbose_build) if (!build_results.ok) error("Failed to build session " + quote(session)) if (output_sources.isEmpty && output_pdf.isEmpty) { progress.echo_warning("No output directory") } val session_background = Document_Build.session_background(options, session, dirs = dirs) using(Export.open_session_context(build_results.store, session_background)) { session_context => build_documents( context(session_context, progress = progress), output_sources = output_sources, output_pdf = output_pdf, verbose = verbose_latex) } } }) } diff --git a/src/Tools/jEdit/src/document_dockable.scala b/src/Tools/jEdit/src/document_dockable.scala --- a/src/Tools/jEdit/src/document_dockable.scala +++ b/src/Tools/jEdit/src/document_dockable.scala @@ -1,370 +1,380 @@ /* Title: Tools/jEdit/src/document_dockable.scala Author: Makarius Dockable window for document build support. */ package isabelle.jedit import isabelle._ import java.awt.BorderLayout import java.awt.event.{ComponentEvent, ComponentAdapter} import scala.swing.{ScrollPane, TextArea, Label, TabbedPane, BorderPanel, Component} import scala.swing.event.SelectionChanged import org.gjt.sp.jedit.{jEdit, View} object Document_Dockable { /* state */ object Status extends Enumeration { val WAITING = Value("waiting") val RUNNING = Value("running") val FINISHED = Value("finished") } object State { def init(): State = State() } sealed case class State( progress: Progress = new Progress, process: Future[Unit] = Future.value(()), status: Status.Value = Status.FINISHED, - output: List[XML.Tree] = Nil + output_results: Command.Results = Command.Results.empty, + output_main: XML.Body = Nil, + output_more: XML.Body = Nil ) { def run(progress: Progress, process: Future[Unit]): State = copy(progress = progress, process = process, status = Status.RUNNING) - def finish(output: List[XML.Tree]): State = - copy(process = Future.value(()), status = Status.FINISHED, output = output) + def running(results: Command.Results, body: XML.Body): State = + copy(status = Status.RUNNING, output_results = results, output_main = body) - def ok: Boolean = !output.exists(Protocol.is_error) + def finish(output: XML.Body): State = + copy(process = Future.value(()), status = Status.FINISHED, output_more = output) + + def output_body: XML.Body = + output_main ::: + (if (output_main.nonEmpty && output_more.nonEmpty) Pretty.Separator else Nil) ::: + output_more + + def ok: Boolean = !output_body.exists(Protocol.is_error) } } class Document_Dockable(view: View, position: String) extends Dockable(view, position) { dockable => GUI_Thread.require {} /* component state -- owned by GUI thread */ private val current_state = Synchronized(Document_Dockable.State.init()) private val process_indicator = new Process_Indicator private val pretty_text_area = new Pretty_Text_Area(view) private val message_pane = new TabbedPane private def show_state(): Unit = GUI_Thread.later { val st = current_state.value - pretty_text_area.update(Document.Snapshot.init, Command.Results.empty, st.output) + pretty_text_area.update(Document.Snapshot.init, st.output_results, st.output_body) st.status match { case Document_Dockable.Status.WAITING => process_indicator.update("Waiting for PIDE document content ...", 5) case Document_Dockable.Status.RUNNING => process_indicator.update("Running document build process ...", 15) case Document_Dockable.Status.FINISHED => process_indicator.update(null, 0) } } private def show_page(page: TabbedPane.Page): Unit = GUI_Thread.later { message_pane.selection.page = page } /* text area with zoom/resize */ override def detach_operation: Option[() => Unit] = pretty_text_area.detach_operation private val zoom = new Font_Info.Zoom { override def changed(): Unit = handle_resize() } private def handle_resize(): Unit = GUI_Thread.require { pretty_text_area.zoom(zoom) } private val delay_resize: Delay = Delay.first(PIDE.session.update_delay, gui = true) { handle_resize() } addComponentListener(new ComponentAdapter { override def componentResized(e: ComponentEvent): Unit = delay_resize.invoke() override def componentShown(e: ComponentEvent): Unit = delay_resize.invoke() }) - /* progress log */ - - private val log_area = - new TextArea { - editable = false - font = GUI.copy_font((new Label).font) - } - private val scroll_log_area = new ScrollPane(log_area) + /* progress */ - def log_progress(only_running: Boolean = false): Document_Editor.Log_Progress = - new Document_Editor.Log_Progress(PIDE.session) { - override def show(text: String): Unit = - if (text != log_area.text) { - log_area.text = text - val vertical = scroll_log_area.peer.getVerticalScrollBar - vertical.setValue(vertical.getMaximum) + class Log_Progress extends Program_Progress { + progress => + + override def detect_program(s: String): Option[String] = + Document_Build.detect_running_script(s) + + private val delay: Delay = + Delay.first(PIDE.session.output_delay) { + if (!stopped) { + running_process(progress) + GUI_Thread.later { show_state() } } - override def echo(msg: String): Unit = - if (!only_running || Document_Build.is_running_script(msg)) super.echo(msg) - } + } + + override def echo(msg: String): Unit = { super.echo(msg); delay.invoke() } + override def stop_program(): Unit = { super.stop_program(); delay.invoke() } + } /* document build process */ private def init_state(): Unit = - current_state.change { _ => Document_Dockable.State(progress = log_progress()) } + current_state.change { st => + st.progress.stop() + Document_Dockable.State(progress = new Log_Progress) + } private def cancel_process(): Unit = current_state.change { st => st.process.cancel(); st } private def await_process(): Unit = current_state.guarded_access(st => if (st.process.is_finished) None else Some((), st)) - private def finish_process(output: List[XML.Tree]): Unit = + private def running_process(progress: Log_Progress): Unit = { + val (results, body) = progress.output() + current_state.change(_.running(results, body)) + } + + private def finish_process(output: XML.Body): Unit = current_state.change(_.finish(output)) - private def run_process(only_running: Boolean = false)( - body: Document_Editor.Log_Progress => Unit - ): Boolean = { + private def run_process(only_running: Boolean = false)(body: Log_Progress => Unit): Boolean = { val started = current_state.change_result { st => if (st.process.is_finished) { - val progress = log_progress(only_running = only_running) + st.progress.stop() + val progress = new Log_Progress val process = Future.thread[Unit](name = "Document_Dockable.process") { await_process() body(progress) } (true, st.run(progress, process)) } else (false, st) } show_state() started } private def load_document(session: String): Boolean = { val options = PIDE.options.value run_process() { _ => try { val session_background = Document_Build.session_background( options, session, dirs = JEdit_Sessions.session_dirs) PIDE.editor.document_setup(Some(session_background)) finish_process(Nil) GUI_Thread.later { refresh_theories() show_state() show_page(theories_page) } } catch { case exn: Throwable if !Exn.is_interrupt(exn) => finish_process(List(Protocol.error_message(Exn.print(exn)))) GUI_Thread.later { show_state() show_page(output_page) } } } } private def document_build( session_background: Sessions.Background, - progress: Document_Editor.Log_Progress + progress: Log_Progress ): Unit = { val store = JEdit_Sessions.sessions_store(PIDE.options.value) val document_selection = PIDE.editor.document_selection() val snapshot = PIDE.session.await_stable_snapshot() val session_context = Export.open_session_context(store, PIDE.resources.session_background, document_snapshot = Some(snapshot)) try { val context = Document_Build.context(session_context, document_session = Some(session_background.base), document_selection = document_selection, progress = progress) + val variant = session_background.info.documents.head Isabelle_System.make_directory(Document_Editor.document_output_dir()) val doc = context.build_document(variant, verbose = true) - // log File.write(Document_Editor.document_output().log, doc.log) - GUI_Thread.later { progress.finish(doc.log) } - - // pdf Bytes.write(Document_Editor.document_output().pdf, doc.pdf) Document_Editor.view_document() } finally { session_context.close() } } private def build_document(): Unit = { PIDE.editor.document_session() match { case Some(session_background) if session_background.info.documents.nonEmpty => run_process(only_running = true) { progress => - show_page(log_page) + show_page(output_page) val result = Exn.capture { document_build(session_background, progress) } val msgs = result match { case Exn.Res(_) => List(Protocol.writeln_message("OK")) case Exn.Exn(exn: Document_Build.Build_Error) => exn.log_errors.map(s => Protocol.error_message(YXML.parse_body(s))) case Exn.Exn(exn) => List(Protocol.error_message(YXML.parse_body(Exn.print(exn)))) } + progress.stop_program() + running_process(progress) finish_process(Pretty.separate(msgs)) show_state() show_page(if (Exn.is_interrupt_exn(result)) theories_page else output_page) } case _ => } } /* controls */ private val document_session = JEdit_Sessions.document_selector(PIDE.options, standalone = true) private lazy val delay_load: Delay = Delay.last(PIDE.session.load_delay, gui = true) { for (session <- document_session.selection_value) { if (!load_document(session)) delay_load.invoke() } } document_session.reactions += { case SelectionChanged(_) => delay_load.invoke() } private val load_button = new GUI.Button("Load") { tooltip = "Load document theories" override def clicked(): Unit = PIDE.editor.document_select_all(set = true) } private val build_button = new GUI.Button("Build") { tooltip = "Build document" override def clicked(): Unit = build_document() } private val cancel_button = new GUI.Button("Cancel") { tooltip = "Cancel build process" override def clicked(): Unit = cancel_process() } private val view_button = new GUI.Button("View") { tooltip = "View document" override def clicked(): Unit = Document_Editor.view_document() } private val controls = Wrap_Panel(List(document_session, process_indicator.component, load_button, build_button, view_button, cancel_button)) add(controls.peer, BorderLayout.NORTH) override def focusOnDefaultComponent(): Unit = build_button.requestFocus() /* message pane with pages */ private val reset_button = new GUI.Button("Reset") { tooltip = "Deselect document theories" override def clicked(): Unit = PIDE.editor.document_select_all(set = false) } private val purge_button = new GUI.Button("Purge") { tooltip = "Remove theories that are no longer required" override def clicked(): Unit = PIDE.editor.purge() } private val theories_controls = Wrap_Panel(List(reset_button, purge_button)) private val theories = new Theories_Status(view, document = true) private def refresh_theories(): Unit = { val domain = PIDE.editor.document_theories().toSet theories.update(domain = Some(domain), trim = true, force = true) theories.refresh() } private val theories_page = new TabbedPane.Page("Theories", new BorderPanel { layout(theories_controls) = BorderPanel.Position.North layout(theories.gui) = BorderPanel.Position.Center }, "Selection and status of document theories") private val output_controls = Wrap_Panel(List(pretty_text_area.search_label, pretty_text_area.search_field, zoom)) private val output_page = new TabbedPane.Page("Output", new BorderPanel { layout(output_controls) = BorderPanel.Position.North layout(Component.wrap(pretty_text_area)) = BorderPanel.Position.Center }, "Output from build process") - private val log_page = - new TabbedPane.Page("Log", new BorderPanel { - layout(scroll_log_area) = BorderPanel.Position.Center - }, "Raw log of build process") - - message_pane.pages ++= List(theories_page, log_page, output_page) + message_pane.pages ++= List(theories_page, output_page) set_content(message_pane) /* main */ private val main = Session.Consumer[Any](getClass.getName) { case _: Session.Global_Options => GUI_Thread.later { document_session.load() handle_resize() refresh_theories() } case changed: Session.Commands_Changed => GUI_Thread.later { val domain = PIDE.editor.document_theories().filter(changed.nodes).toSet if (domain.nonEmpty) theories.update(domain = Some(domain)) } } override def init(): Unit = { PIDE.editor.document_init(dockable) init_state() PIDE.session.global_options += main PIDE.session.commands_changed += main handle_resize() delay_load.invoke() } override def exit(): Unit = { PIDE.session.global_options -= main PIDE.session.commands_changed -= main delay_resize.revoke() PIDE.editor.document_exit(dockable) } }