src/Tools/jEdit/src/jedit_sessions.scala
author wenzelm
Thu Nov 02 10:16:22 2017 +0100 (17 months ago)
changeset 66987 352b23c97ac8
parent 66979 58b166fd8447
child 66988 7f8c1dd7576a
permissions -rw-r--r--
support focus_session, for much faster startup of Isabelle/jEdit;
more options for "isabelle jedit";
     1 /*  Title:      Tools/jEdit/src/jedit_sessions.scala
     2     Author:     Makarius
     3 
     4 Isabelle/jEdit session information, based on implicit process environment
     5 and explicit options.
     6 */
     7 
     8 package isabelle.jedit
     9 
    10 
    11 import isabelle._
    12 
    13 import scala.swing.ComboBox
    14 import scala.swing.event.SelectionChanged
    15 
    16 
    17 object JEdit_Sessions
    18 {
    19   /* session options */
    20 
    21   def session_options(options: Options): Options =
    22     Isabelle_System.getenv("JEDIT_ML_PROCESS_POLICY") match {
    23       case "" => options
    24       case s => options.string.update("ML_process_policy", s)
    25     }
    26 
    27   def session_dirs(): List[Path] =
    28     Path.split(Isabelle_System.getenv("JEDIT_SESSION_DIRS"))
    29 
    30   def session_build_mode(): String = Isabelle_System.getenv("JEDIT_BUILD_MODE")
    31 
    32 
    33   /* raw logic info */
    34 
    35   private val jedit_logic_option = "jedit_logic"
    36 
    37   def logic_name(options: Options): String =
    38     Isabelle_System.default_logic(
    39       Isabelle_System.getenv("JEDIT_LOGIC"),
    40       options.string(jedit_logic_option))
    41 
    42   def logic_focus: Boolean = Isabelle_System.getenv("JEDIT_LOGIC_FOCUS") == "true"
    43   def logic_base: Boolean = Isabelle_System.getenv("JEDIT_LOGIC_BASE") == "true"
    44   def logic_parent: Boolean = Isabelle_System.getenv("JEDIT_LOGIC_PARENT") == "true"
    45 
    46   def logic_info(options: Options): Option[Sessions.Info] =
    47     try { Sessions.load(session_options(options), dirs = session_dirs()).get(logic_name(options)) }
    48     catch { case ERROR(_) => None }
    49 
    50   def logic_root(options: Options): Position.T =
    51     if (Isabelle_System.getenv("JEDIT_LOGIC_ROOT") == "true") {
    52       logic_info(options).map(_.pos) getOrElse Position.none
    53     }
    54     else Position.none
    55 
    56 
    57   /* logic selector */
    58 
    59   private class Logic_Entry(val name: String, val description: String)
    60   {
    61     override def toString: String = description
    62   }
    63 
    64   def logic_selector(options: Options_Variable, autosave: Boolean): Option_Component =
    65   {
    66     GUI_Thread.require {}
    67 
    68     val session_list =
    69     {
    70       val sessions = Sessions.load(options.value, dirs = session_dirs())
    71       val (main_sessions, other_sessions) =
    72         sessions.imports_topological_order.partition(info => info.groups.contains("main"))
    73       main_sessions.map(_.name).sorted ::: other_sessions.map(_.name).sorted
    74     }
    75 
    76     val entries =
    77       new Logic_Entry("", "default (" + logic_name(options.value) + ")") ::
    78         session_list.map(name => new Logic_Entry(name, name))
    79 
    80     val component = new ComboBox(entries) with Option_Component {
    81       name = jedit_logic_option
    82       val title = "Logic"
    83       def load: Unit =
    84       {
    85         val logic = options.string(jedit_logic_option)
    86         entries.find(_.name == logic) match {
    87           case Some(entry) => selection.item = entry
    88           case None =>
    89         }
    90       }
    91       def save: Unit = options.string(jedit_logic_option) = selection.item.name
    92     }
    93 
    94     component.load()
    95     if (autosave) {
    96       component.listenTo(component.selection)
    97       component.reactions += { case SelectionChanged(_) => component.save() }
    98     }
    99     component.tooltip = "Logic session name (change requires restart)"
   100     component
   101   }
   102 
   103 
   104   /* session build process */
   105 
   106   def session_base_info(options: Options): Sessions.Base_Info =
   107     Sessions.base_info(options,
   108       session =
   109         if (logic_parent) logic_info(options).flatMap(_.parent) getOrElse logic_name(options)
   110         else logic_name(options),
   111       dirs = JEdit_Sessions.session_dirs(),
   112       all_known = !logic_focus,
   113       focus_session = logic_focus,
   114       required_session = logic_base)
   115 
   116   def session_build(
   117     options: Options, progress: Progress = No_Progress, no_build: Boolean = false): Int =
   118   {
   119     val mode = session_build_mode()
   120 
   121     Build.build(options = session_options(options), progress = progress,
   122       build_heap = true, no_build = no_build, system_mode = mode == "" || mode == "system",
   123       dirs = session_dirs(), infos = PIDE.resources.session_base_info.infos,
   124       sessions = List(PIDE.resources.session_name)).rc
   125   }
   126 
   127   def session_start(options: Options)
   128   {
   129     Isabelle_Process.start(PIDE.session, session_options(options),
   130       sessions = Some(PIDE.resources.session_base_info.sessions),
   131       logic = PIDE.resources.session_name,
   132       store = Sessions.store(session_build_mode() == "system"),
   133       modes =
   134         (space_explode(',', options.string("jedit_print_mode")) :::
   135          space_explode(',', Isabelle_System.getenv("JEDIT_PRINT_MODE"))).reverse,
   136       phase_changed = PIDE.plugin.session_phase_changed)
   137   }
   138 }