| author | wenzelm |
| Fri, 03 Feb 2023 19:00:29 +0100 | |
| changeset 77184 | 861777e58b77 |
| parent 77161 | 913c781ff6ba |
| child 77185 | 9dc4d9ed886f |
| permissions | -rw-r--r-- |
| 76606 | 1 |
/* Title: Pure/PIDE/document_editor.scala |
2 |
Author: Makarius |
|
3 |
||
| 76730 | 4 |
Central resources and configuration for interactive document preparation. |
| 76606 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
10 |
object Document_Editor {
|
|
11 |
/* document output */ |
|
12 |
||
13 |
def document_name: String = "document" |
|
14 |
def document_output_dir(): Path = Path.explode("$ISABELLE_HOME_USER/document_output")
|
|
15 |
def document_output(): Path = document_output_dir() + Path.basic(document_name) |
|
16 |
||
| 77184 | 17 |
def write_document(doc: Document_Build.Document_Output): Unit = {
|
18 |
val output = document_output() |
|
19 |
File.write(output.log, doc.log) |
|
20 |
Bytes.write(output.pdf, doc.pdf) |
|
21 |
} |
|
22 |
||
| 76606 | 23 |
def view_document(): Unit = {
|
24 |
val path = document_output().pdf |
|
25 |
if (path.is_file) Isabelle_System.pdf_viewer(path) |
|
26 |
} |
|
27 |
||
28 |
||
| 76730 | 29 |
/* configuration state */ |
|
76609
cc9ddf373bd2
maintain global state of document editor views, notably for is_active operation;
wenzelm
parents:
76606
diff
changeset
|
30 |
|
|
77144
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
31 |
sealed case class Session( |
|
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
32 |
background: Option[Sessions.Background], |
|
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
33 |
selection: Set[Document.Node.Name], |
|
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
34 |
snapshot: Option[Document.Snapshot] |
|
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
35 |
) {
|
|
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
36 |
def is_vacuous: Boolean = background.isEmpty |
|
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
37 |
def is_pending: Boolean = snapshot.isEmpty |
|
77149
3991a35cd740
automatically build document when selected theories are finished;
wenzelm
parents:
77147
diff
changeset
|
38 |
def is_ready: Boolean = background.isDefined && snapshot.isDefined |
|
77144
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
39 |
|
|
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
40 |
def get_background: Sessions.Background = background.get |
|
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
41 |
def get_variant: Document_Build.Document_Variant = get_background.info.documents.head |
|
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
42 |
def get_snapshot: Document.Snapshot = snapshot.get |
|
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
43 |
} |
|
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
44 |
|
|
76609
cc9ddf373bd2
maintain global state of document editor views, notably for is_active operation;
wenzelm
parents:
76606
diff
changeset
|
45 |
sealed case class State( |
| 76678 | 46 |
session_background: Option[Sessions.Background] = None, |
|
76715
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
47 |
selection: Set[Document.Node.Name] = Set.empty, |
|
76609
cc9ddf373bd2
maintain global state of document editor views, notably for is_active operation;
wenzelm
parents:
76606
diff
changeset
|
48 |
views: Set[AnyRef] = Set.empty, |
|
cc9ddf373bd2
maintain global state of document editor views, notably for is_active operation;
wenzelm
parents:
76606
diff
changeset
|
49 |
) {
|
| 76678 | 50 |
def is_active: Boolean = session_background.isDefined && views.nonEmpty |
|
76609
cc9ddf373bd2
maintain global state of document editor views, notably for is_active operation;
wenzelm
parents:
76606
diff
changeset
|
51 |
|
|
76715
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
52 |
def all_document_theories: List[Document.Node.Name] = |
|
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
53 |
session_background match {
|
|
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
54 |
case Some(background) => background.base.all_document_theories |
|
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
55 |
case None => Nil |
|
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
56 |
} |
|
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
57 |
|
|
76716
a7602257a825
clarified state document nodes for Theories_Status / Document_Dockable;
wenzelm
parents:
76715
diff
changeset
|
58 |
def active_document_theories: List[Document.Node.Name] = |
|
a7602257a825
clarified state document nodes for Theories_Status / Document_Dockable;
wenzelm
parents:
76715
diff
changeset
|
59 |
if (is_active) all_document_theories else Nil |
|
a7602257a825
clarified state document nodes for Theories_Status / Document_Dockable;
wenzelm
parents:
76715
diff
changeset
|
60 |
|
|
76715
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
61 |
def select( |
|
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
62 |
names: Iterable[Document.Node.Name], |
|
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
63 |
set: Boolean = false, |
|
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
64 |
toggle: Boolean = false |
|
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
65 |
): State = {
|
|
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
66 |
copy(selection = |
|
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
67 |
names.foldLeft(selection) {
|
|
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
68 |
case (sel, name) => |
|
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
69 |
val b = if (toggle) !selection(name) else set |
|
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
70 |
if (b) sel + name else sel - name |
|
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
71 |
}) |
|
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
72 |
} |
|
bf5ff407f32f
clarified state of document model vs. document editor selection (again, see also a9d52d02bd83);
wenzelm
parents:
76678
diff
changeset
|
73 |
|
|
76609
cc9ddf373bd2
maintain global state of document editor views, notably for is_active operation;
wenzelm
parents:
76606
diff
changeset
|
74 |
def register_view(id: AnyRef): State = copy(views = views + id) |
|
cc9ddf373bd2
maintain global state of document editor views, notably for is_active operation;
wenzelm
parents:
76606
diff
changeset
|
75 |
def unregister_view(id: AnyRef): State = copy(views = views - id) |
|
77144
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
76 |
|
|
77161
913c781ff6ba
support document preparation from already loaded theories;
wenzelm
parents:
77149
diff
changeset
|
77 |
def session(pide_session: isabelle.Session): Session = {
|
|
77144
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
78 |
val background = session_background.filter(_.info.documents.nonEmpty) |
|
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
79 |
val snapshot = |
|
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
80 |
if (background.isEmpty) None |
|
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
81 |
else {
|
|
77161
913c781ff6ba
support document preparation from already loaded theories;
wenzelm
parents:
77149
diff
changeset
|
82 |
val snapshot = pide_session.snapshot() |
|
913c781ff6ba
support document preparation from already loaded theories;
wenzelm
parents:
77149
diff
changeset
|
83 |
def document_ready(name: Document.Node.Name): Boolean = |
|
913c781ff6ba
support document preparation from already loaded theories;
wenzelm
parents:
77149
diff
changeset
|
84 |
pide_session.resources.session_base.loaded_theory(name) || |
|
913c781ff6ba
support document preparation from already loaded theories;
wenzelm
parents:
77149
diff
changeset
|
85 |
snapshot.node_consolidated(name) |
|
913c781ff6ba
support document preparation from already loaded theories;
wenzelm
parents:
77149
diff
changeset
|
86 |
if (snapshot.is_outdated || !selection.forall(document_ready)) None |
| 77147 | 87 |
else Some(snapshot) |
|
77144
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
88 |
} |
|
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
89 |
Session(background, selection, snapshot) |
|
42c3970e1ac1
clarified Document_Editor.Session: more explicit types, more robust operations;
wenzelm
parents:
76994
diff
changeset
|
90 |
} |
|
76609
cc9ddf373bd2
maintain global state of document editor views, notably for is_active operation;
wenzelm
parents:
76606
diff
changeset
|
91 |
} |
| 76606 | 92 |
} |