src/Tools/jEdit/src/jedit_sessions.scala
author wenzelm
Wed, 15 Mar 2017 14:29:55 +0100
changeset 65262 0fe4ebab9fdf
parent 65256 c3d6dd17d626
child 65420 695d4e22345a
permissions -rw-r--r--
resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD"; PIDE._plugin indicates intialization state of Plugin; tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62973
744266e32612 clarified modules;
wenzelm
parents: 62972
diff changeset
     1
/*  Title:      Tools/jEdit/src/jedit_sessions.scala
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
     3
65256
c3d6dd17d626 more explicit options;
wenzelm
parents: 65254
diff changeset
     4
Isabelle/jEdit session information, based on implicit process environment
c3d6dd17d626 more explicit options;
wenzelm
parents: 65254
diff changeset
     5
and explicit options.
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
     6
*/
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
     7
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
     8
package isabelle.jedit
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
     9
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
    10
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
    11
import isabelle._
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
    12
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
    13
import scala.swing.ComboBox
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
    14
import scala.swing.event.SelectionChanged
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
    15
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
    16
62973
744266e32612 clarified modules;
wenzelm
parents: 62972
diff changeset
    17
object JEdit_Sessions
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
    18
{
62972
wenzelm
parents: 62754
diff changeset
    19
  /* session info */
wenzelm
parents: 62754
diff changeset
    20
50403
87868964733c more uniform default logic, using settings, options, args etc.;
wenzelm
parents: 50380
diff changeset
    21
  private val option_name = "jedit_logic"
87868964733c more uniform default logic, using settings, options, args etc.;
wenzelm
parents: 50380
diff changeset
    22
64602
8edca3465758 added isabelle jedit -R;
wenzelm
parents: 63986
diff changeset
    23
  sealed case class Info(name: String, open_root: Position.T)
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
    24
62972
wenzelm
parents: 62754
diff changeset
    25
  def session_dirs(): List[Path] = Path.split(Isabelle_System.getenv("JEDIT_SESSION_DIRS"))
wenzelm
parents: 62754
diff changeset
    26
65256
c3d6dd17d626 more explicit options;
wenzelm
parents: 65254
diff changeset
    27
  def session_options(options: Options): Options =
63986
c7a4b03727ae options for process policy, notably for multiprocessor machines;
wenzelm
parents: 62974
diff changeset
    28
    Isabelle_System.getenv("JEDIT_ML_PROCESS_POLICY") match {
65256
c3d6dd17d626 more explicit options;
wenzelm
parents: 65254
diff changeset
    29
      case "" => options
c3d6dd17d626 more explicit options;
wenzelm
parents: 65254
diff changeset
    30
      case s => options.string("ML_process_policy") = s
63986
c7a4b03727ae options for process policy, notably for multiprocessor machines;
wenzelm
parents: 62974
diff changeset
    31
    }
c7a4b03727ae options for process policy, notably for multiprocessor machines;
wenzelm
parents: 62974
diff changeset
    32
65256
c3d6dd17d626 more explicit options;
wenzelm
parents: 65254
diff changeset
    33
  def session_info(options: Options): Info =
64602
8edca3465758 added isabelle jedit -R;
wenzelm
parents: 63986
diff changeset
    34
  {
8edca3465758 added isabelle jedit -R;
wenzelm
parents: 63986
diff changeset
    35
    val logic =
8edca3465758 added isabelle jedit -R;
wenzelm
parents: 63986
diff changeset
    36
      Isabelle_System.default_logic(
8edca3465758 added isabelle jedit -R;
wenzelm
parents: 63986
diff changeset
    37
        Isabelle_System.getenv("JEDIT_LOGIC"),
65256
c3d6dd17d626 more explicit options;
wenzelm
parents: 65254
diff changeset
    38
        options.string(option_name))
64602
8edca3465758 added isabelle jedit -R;
wenzelm
parents: 63986
diff changeset
    39
8edca3465758 added isabelle jedit -R;
wenzelm
parents: 63986
diff changeset
    40
    (for {
8edca3465758 added isabelle jedit -R;
wenzelm
parents: 63986
diff changeset
    41
      tree <-
65256
c3d6dd17d626 more explicit options;
wenzelm
parents: 65254
diff changeset
    42
        try { Some(Sessions.load(session_options(options), dirs = session_dirs())) }
64602
8edca3465758 added isabelle jedit -R;
wenzelm
parents: 63986
diff changeset
    43
        catch { case ERROR(_) => None }
8edca3465758 added isabelle jedit -R;
wenzelm
parents: 63986
diff changeset
    44
      info <- tree.lift(logic)
8edca3465758 added isabelle jedit -R;
wenzelm
parents: 63986
diff changeset
    45
      parent <- info.parent
8edca3465758 added isabelle jedit -R;
wenzelm
parents: 63986
diff changeset
    46
      if Isabelle_System.getenv("JEDIT_LOGIC_ROOT") == "true"
8edca3465758 added isabelle jedit -R;
wenzelm
parents: 63986
diff changeset
    47
    } yield Info(parent, info.pos)) getOrElse Info(logic, Position.none)
8edca3465758 added isabelle jedit -R;
wenzelm
parents: 63986
diff changeset
    48
  }
8edca3465758 added isabelle jedit -R;
wenzelm
parents: 63986
diff changeset
    49
65256
c3d6dd17d626 more explicit options;
wenzelm
parents: 65254
diff changeset
    50
  def session_name(options: Options): String = session_info(options).name
64602
8edca3465758 added isabelle jedit -R;
wenzelm
parents: 63986
diff changeset
    51
62972
wenzelm
parents: 62754
diff changeset
    52
  def session_build_mode(): String = Isabelle_System.getenv("JEDIT_BUILD_MODE")
wenzelm
parents: 62754
diff changeset
    53
65256
c3d6dd17d626 more explicit options;
wenzelm
parents: 65254
diff changeset
    54
  def session_build(
c3d6dd17d626 more explicit options;
wenzelm
parents: 65254
diff changeset
    55
    options: Options, progress: Progress = No_Progress, no_build: Boolean = false): Int =
62972
wenzelm
parents: 62754
diff changeset
    56
  {
wenzelm
parents: 62754
diff changeset
    57
    val mode = session_build_mode()
wenzelm
parents: 62754
diff changeset
    58
65256
c3d6dd17d626 more explicit options;
wenzelm
parents: 65254
diff changeset
    59
    Build.build(options = session_options(options), progress = progress,
62972
wenzelm
parents: 62754
diff changeset
    60
      build_heap = true, no_build = no_build, system_mode = mode == "" || mode == "system",
65256
c3d6dd17d626 more explicit options;
wenzelm
parents: 65254
diff changeset
    61
      dirs = session_dirs(), sessions = List(session_name(options))).rc
62972
wenzelm
parents: 62754
diff changeset
    62
  }
wenzelm
parents: 62754
diff changeset
    63
65256
c3d6dd17d626 more explicit options;
wenzelm
parents: 65254
diff changeset
    64
  def session_start(options: Options)
62972
wenzelm
parents: 62754
diff changeset
    65
  {
wenzelm
parents: 62754
diff changeset
    66
    val modes =
65256
c3d6dd17d626 more explicit options;
wenzelm
parents: 65254
diff changeset
    67
      (space_explode(',', options.string("jedit_print_mode")) :::
62972
wenzelm
parents: 62754
diff changeset
    68
       space_explode(',', Isabelle_System.getenv("JEDIT_PRINT_MODE"))).reverse
63986
c7a4b03727ae options for process policy, notably for multiprocessor machines;
wenzelm
parents: 62974
diff changeset
    69
65256
c3d6dd17d626 more explicit options;
wenzelm
parents: 65254
diff changeset
    70
    Isabelle_Process.start(PIDE.session, session_options(options),
c3d6dd17d626 more explicit options;
wenzelm
parents: 65254
diff changeset
    71
      logic = session_name(options), dirs = session_dirs(), modes = modes,
65226
wenzelm
parents: 65216
diff changeset
    72
      store = Sessions.store(session_build_mode() == "system"),
wenzelm
parents: 65216
diff changeset
    73
      phase_changed = PIDE.plugin.session_phase_changed)
62972
wenzelm
parents: 62754
diff changeset
    74
  }
wenzelm
parents: 62754
diff changeset
    75
65256
c3d6dd17d626 more explicit options;
wenzelm
parents: 65254
diff changeset
    76
  def session_list(options: Options): List[String] =
62972
wenzelm
parents: 62754
diff changeset
    77
  {
65256
c3d6dd17d626 more explicit options;
wenzelm
parents: 65254
diff changeset
    78
    val session_tree = Sessions.load(options, dirs = session_dirs())
62972
wenzelm
parents: 62754
diff changeset
    79
    val (main_sessions, other_sessions) =
wenzelm
parents: 62754
diff changeset
    80
      session_tree.topological_order.partition(p => p._2.groups.contains("main"))
wenzelm
parents: 62754
diff changeset
    81
    main_sessions.map(_._1).sorted ::: other_sessions.map(_._1).sorted
wenzelm
parents: 62754
diff changeset
    82
  }
wenzelm
parents: 62754
diff changeset
    83
wenzelm
parents: 62754
diff changeset
    84
wenzelm
parents: 62754
diff changeset
    85
  /* logic selector */
wenzelm
parents: 62754
diff changeset
    86
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
    87
  private class Logic_Entry(val name: String, val description: String)
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
    88
  {
57912
wenzelm
parents: 57612
diff changeset
    89
    override def toString: String = description
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
    90
  }
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
    91
65256
c3d6dd17d626 more explicit options;
wenzelm
parents: 65254
diff changeset
    92
  def logic_selector(options: Options, autosave: Boolean): Option_Component =
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
    93
  {
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 56890
diff changeset
    94
    GUI_Thread.require {}
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
    95
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
    96
    val entries =
65256
c3d6dd17d626 more explicit options;
wenzelm
parents: 65254
diff changeset
    97
      new Logic_Entry("", "default (" + session_name(options) + ")") ::
c3d6dd17d626 more explicit options;
wenzelm
parents: 65254
diff changeset
    98
        session_list(options).map(name => new Logic_Entry(name, name))
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
    99
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
   100
    val component = new ComboBox(entries) with Option_Component {
50375
c101127a7f37 clarified logic argument: session name, not path name;
wenzelm
parents: 50205
diff changeset
   101
      name = option_name
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49248
diff changeset
   102
      val title = "Logic"
49247
ffd9ad9dc35b more detailed option tooltip;
wenzelm
parents: 49246
diff changeset
   103
      def load: Unit =
ffd9ad9dc35b more detailed option tooltip;
wenzelm
parents: 49246
diff changeset
   104
      {
65256
c3d6dd17d626 more explicit options;
wenzelm
parents: 65254
diff changeset
   105
        val logic = options.string(option_name)
49247
ffd9ad9dc35b more detailed option tooltip;
wenzelm
parents: 49246
diff changeset
   106
        entries.find(_.name == logic) match {
ffd9ad9dc35b more detailed option tooltip;
wenzelm
parents: 49246
diff changeset
   107
          case Some(entry) => selection.item = entry
ffd9ad9dc35b more detailed option tooltip;
wenzelm
parents: 49246
diff changeset
   108
          case None =>
ffd9ad9dc35b more detailed option tooltip;
wenzelm
parents: 49246
diff changeset
   109
        }
ffd9ad9dc35b more detailed option tooltip;
wenzelm
parents: 49246
diff changeset
   110
      }
65256
c3d6dd17d626 more explicit options;
wenzelm
parents: 65254
diff changeset
   111
      def save: Unit = options.string(option_name) = selection.item.name
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
   112
    }
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
   113
49247
ffd9ad9dc35b more detailed option tooltip;
wenzelm
parents: 49246
diff changeset
   114
    component.load()
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
   115
    if (autosave) {
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
   116
      component.listenTo(component.selection)
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
   117
      component.reactions += { case SelectionChanged(_) => component.save() }
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
   118
    }
50380
b1cb76418760 select logic session names, not paths;
wenzelm
parents: 50375
diff changeset
   119
    component.tooltip = "Logic session name (change requires restart)"
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
   120
    component
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
   121
  }
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents:
diff changeset
   122
}