# jedit_prefs.tcl - preferences commands and procs for jedit
#   (jedit:prefs:menus is in jedit_menus.tcl, for convenience.)
#
######################################################################
# Copyright 1992-2000 by Jay Sekora.  This file may be freely        #
# distributed, modified or unmodified, for any purpose, provided     #
# that this copyright notice is retained verbatim in all copies and  #
# no attempt is made to obscure the authorship of this file.  If you #
# distribute any modified versions, I ask, but do not require, that  #
# you clearly mark any changes you make as such and that you provide #
# your users with instructions for getting the original sources.     #
######################################################################

# TO DO
#   MAKE ALL THIS PER-WINDOW!
#   file_modes panel
#   word-end punctuation (and heuristics) sd be mode-specific
######################################################################

catch {
  package require jldb
  namespace import ::jldb::shortcuts::*
}

::jldb::set_defaults {
  {JEbutton:done {Done}}
  {JEbutton:save {Save}}
  {JEbutton:default {Default}}
  {JEbutton:choose {Choose...}}
  {JEpref:textbg {Normal background:}}
  {JEpref:textfg {Normal foreground:}}
  {JEpref:textsb {Selected background:}}
  {JEpref:textsf {Selected foreground:}}
  {JEpref:textiw {Insert width:}}
  {JEpref:textsbw {Selection border width:}}
  {JEpref:textbw {Text border width:}}
  {JEpref:undolevels {Undo levels:}}
  {JEtitle:modeprefs {Mode\255Specific Preferences}}
  {JElabel:prefs_for_mode... {Preferences for mode "$mode"}}
  {JEpref:show...menu {Show $menu Menu}}
  {JEpref:buttonbar {Show Buttonbar}}
  {JEpref:savestate {Save/Load Visual State} 10}
    {LONG-JEpref:savestate {Save/Load Highlighting and Position} 10}
  {JEpref:autobreak {Break Lines on <space>} 0}
    {LONG-JEpref:autobreak {Break long lines with <Space>} 0}
  {JEpref:autoindent {Indent Lines on <Return>} 0}
    {LONG-JEpref:autoindent {Preserve indentation with <Return>} 0}
  {JEpref:parenflash {Flash Matching Brackets} 6}
    {LONG-JEpref:parenflash
    {Flash matching braces, brackets, and parentheses} 6}
  {JEpref:sabbrev {Static Abbreviation} 1}
    {LONG-JEpref:sabbrev {Expand static abbreviations with <Space>} 1}
  {JEpref:dabbrev {Dynamic Abbreviation} 1}
    {LONG-JEpref:dabbrev {Expand dynamic abbreviations with <Tab>} 1}
  {JEpref:wrap_none {Don't Wrap Lines} 2}
  {JEpref:wrap_char {Wrap Lines Anywhere} 11}
    {LONG-JEpref:wrap_char {Wrap Lines at Character Boundaries} 11}
  {JEpref:wrap_word {Wrap Lines at Words} 14}
    {LONG-JEpref:wrap_word {Wrap Lines at Word Boundaries} 14}
  {JEpref:font {Font:}}
  {JEpref:width {Width:}}
  {JEpref:height {Height:}}
}

######################################################################
# editor-wide preferences panel
#   { t args } lets it be used with j:tkb:mkmap
######################################################################

::aq::command_register jedit:cmd:edit_prefs {Editor Preferences...}
proc jedit:cmd:edit_prefs { t args } {
  global JEDIT_PREFS
  
  set old_focus [focus]			;# so we can restore original focus
  
  set w .edit_prefs
  toplevel $w
  wm title $w "Editor Preferences"
  
  ::aq::colour_chooser $w.textbg \
    -variable JEDIT_PREFS(textbg) \
    -label [::jldb::short_text JEpref:textbg]
  ::aq::colour_chooser $w.textfg \
    -variable JEDIT_PREFS(textfg) \
    -label [::jldb::short_text JEpref:textfg]
  ::aq::colour_chooser $w.textsb \
    -variable JEDIT_PREFS(textsb) \
    -label [::jldb::short_text JEpref:textsb]
  ::aq::colour_chooser $w.textsf \
    -variable JEDIT_PREFS(textsf) \
    -label [::jldb::short_text JEpref:textsf]
  
  label $w.textiw-label \
    -text [::jldb::short_text JEpref:textiw] \
    -anchor w
  scale $w.textiw \
    -from 1 -to 25 \
    -orient horizontal \
    -command {set JEDIT_PREFS(textiw)}
  $w.textiw set $JEDIT_PREFS(textiw)
  
  label $w.textsbw-label \
    -text [::jldb::short_text JEpref:textsbw] \
    -anchor w
  scale $w.textsbw \
    -from 0 -to 25 \
    -orient horizontal \
    -command {set JEDIT_PREFS(textsbw)}
  $w.textsbw set $JEDIT_PREFS(textsbw)
  
  label $w.textbw-label \
    -text [::jldb::short_text JEpref:textbw] \
    -anchor w
  scale $w.textbw \
    -from 0 -to 50 \
    -orient horizontal \
    -command {set JEDIT_PREFS(textbw)}
  $w.textbw set $JEDIT_PREFS(textbw)
  
  label $w.undolevels-label \
    -text [::jldb::short_text JEpref:undolevels] \
    -anchor w
  scale $w.undolevels \
    -from 0 -to 10 \
    -orient horizontal \
    -command {set JEDIT_PREFS(undolevels)}
  $w.undolevels set $JEDIT_PREFS(undolevels)
  
  ::aq::buttonbar $w.b -default save -buttons [format {
    {
      save JEbutton:save {
        j:write_prefs -array JEDIT_PREFS -directory $env(HOME)/.tk \
          -file jedit-defaults
        %s.b.done invoke
      }
    } {
      done JEbutton:done {
        jedit:apply_all_prefs [jedit:text_to_top %s]
        destroy %s
      }
    }
  } $w $t $w]
  
  pack \
    $w.textbg \
    [::aq::rule $w] \
    $w.textfg \
    [::aq::rule $w] \
    $w.textsb \
    [::aq::rule $w] \
    $w.textsf \
    [::aq::rule $w] \
    $w.textiw-label $w.textiw \
    [::aq::filler $w] \
    $w.textsbw-label $w.textsbw \
    [::aq::filler $w] \
    $w.textbw-label $w.textbw \
    [::aq::rule $w] \
    $w.undolevels-label $w.undolevels \
    [::aq::rule $w] \
    $w.b \
    -fill x
  
  ::aq::dialogue $w
  ::aq::default_button $w.b.save $w
  focus $w
  tkwait window $w
  focus -force $old_focus		;# can't figure out a better way...
}

######################################################################
# jedit:cmd:mode_prefs - mode-specific preferences panel
#   { t args } lets it be used with j:tkb:mkmap
######################################################################

::aq::command_register jedit:cmd:mode_prefs {Mode Preferences...}
proc jedit:cmd:mode_prefs { t args } {
  global JEDIT_MODEPREFS env tk_strictMotif
  
  set old_focus [focus]			;# so we can restore original focus
  
  set mode [jedit:get_mode $t]
  
  set window [jedit:text_to_top $t]

  toplevel .mode_prefs
  wm title .mode_prefs [::jldb::short_text JEtitle:modeprefs]
  
  label .mode_prefs.mode -text \
    [subst -nocommands -nobackslashes [= JElabel:prefs_for_mode...]]
  
  pack .mode_prefs.mode -expand y -fill x
  
  ::aq::buttonbar .mode_prefs.b -default save -buttons [format {
    {
      save JEbutton:save {
        set tmp_mode %s
        j:write_prefs -array JEDIT_MODEPREFS -prefix $tmp_mode \
          -directory $env(HOME)/.tk/jeditmodes \
          -file ${tmp_mode}-defaults
        .mode_prefs.b.done invoke
      }
    } {
      done JEbutton:done {
        set tmp_mode %s
        if {$JEDIT_MODEPREFS($tmp_mode,textwidth) < 20} {
          set JEDIT_MODEPREFS($tmp_mode,textwidth) 20
        }
        if {$JEDIT_MODEPREFS($tmp_mode,textheight) < 4} {
          set JEDIT_MODEPREFS($tmp_mode,textheight) 4
        }
        # noreread is a kludge!
        jedit:apply_all_prefs %s noreread
        destroy .mode_prefs
      }
    }
  } $mode $mode $window]
  
  frame .mode_prefs.ui_prefs
  lower .mode_prefs.ui_prefs
  frame .mode_prefs.other_prefs
  lower .mode_prefs.other_prefs
  frame .mode_prefs.window_prefs
  lower .mode_prefs.window_prefs
  pack \
    [::aq::rule .mode_prefs] \
    [jedit:prefs:menus .mode_prefs.menus $mode] \
    [::aq::rule .mode_prefs] \
    [jedit:prefs:buttonbar .mode_prefs.buttonbar $mode] \
    -in .mode_prefs.ui_prefs -fill x -expand 1
  pack \
    [::aq::rule .mode_prefs] \
    [jedit:prefs:io .mode_prefs.io $mode] \
    [::aq::rule .mode_prefs] \
    [jedit:prefs:autokeys .mode_prefs.autokeys $mode] \
    [::aq::rule .mode_prefs] \
    [jedit:prefs:abbrevs .mode_prefs.abbrevs $mode] \
    [::aq::rule .mode_prefs] \
    [jedit:prefs:wrap .mode_prefs.wrap $mode] \
    -in .mode_prefs.other_prefs -fill x -expand 1
  pack \
    [jedit:prefs:font .mode_prefs.font $mode] \
    [::aq::rule .mode_prefs] \
    [jedit:prefs:size .mode_prefs.size $mode] \
    -in .mode_prefs.window_prefs -fill x -expand 1
  pack \
    .mode_prefs.b \
    [::aq::rule .mode_prefs] \
    .mode_prefs.window_prefs \
    [::aq::rule .mode_prefs] \
    -fill x -side bottom -expand 1
  pack \
    .mode_prefs.ui_prefs \
    -side left -fill both -expand 1
  pack \
    [::aq::rule .mode_prefs] \
    -side left -fill y
  pack \
    .mode_prefs.other_prefs \
    -side left -fill both -expand 1
  
  ::aq::dialogue .mode_prefs	;# position in centre of screen

  focus .mode_prefs
  ::aq::default_button .mode_prefs.b.save \
    .mode_prefs.font.bot.e \
    .mode_prefs.size.we \
    .mode_prefs.size.he \
    .mode_prefs

  bind .mode_prefs <Key-Tab> {focus .mode_prefs.font.bot.e}
  tkwait window .mode_prefs
  focus -force $old_focus		;# can't figure out a better way...
}

proc jedit:prefs:buttonbar { w mode } {
  global JEDIT_MODEPREFS
  
  frame $w
  checkbutton $w.cb -anchor w \
    -text [::jldb::short_text JEpref:buttonbar] \
    -variable JEDIT_MODEPREFS($mode,buttonbar)
  pack $w.cb -side top -expand yes -fill x
  
  return $w
}

proc jedit:prefs:io { w mode } {
  global JEDIT_MODEPREFS
  
  frame $w
  checkbutton $w.cb -anchor w \
    -text [::jldb::short_text JEpref:savestate] \
    -variable JEDIT_MODEPREFS($mode,savestate)
  pack $w.cb -side top -expand yes -fill x
  
  return $w
}

proc jedit:prefs:autokeys { w mode } {
  global JEDIT_MODEPREFS 
  
  frame $w
  checkbutton $w.autobreak_cb -anchor w \
    -text [::jldb::short_text JEpref:autobreak] \
    -variable JEDIT_MODEPREFS($mode,autobreak)
  checkbutton $w.autoindent_cb -anchor w \
    -text [::jldb::short_text JEpref:autoindent] \
    -variable JEDIT_MODEPREFS($mode,autoindent)
  checkbutton $w.parenflash_cb -anchor w \
    -text [::jldb::short_text JEpref:parenflash] \
    -variable JEDIT_MODEPREFS($mode,parenflash)
  pack \
    $w.autobreak_cb \
    $w.autoindent_cb \
    $w.parenflash_cb \
    -side top -expand yes -fill x
  
  return $w
}

proc jedit:prefs:abbrevs { w mode } {
  global JEDIT_MODEPREFS
  
  frame $w
  checkbutton $w.sabbrev_cb -anchor w \
    -text [::jldb::short_text JEpref:sabbrev] \
    -variable JEDIT_MODEPREFS($mode,sabbrev)
  checkbutton $w.dabbrev_cb -anchor w \
    -text [::jldb::short_text JEpref:dabbrev] \
    -variable JEDIT_MODEPREFS($mode,dabbrev)
  pack \
    $w.sabbrev_cb \
    $w.dabbrev_cb \
    -side top -expand yes -fill x
  
  return $w
}

proc jedit:prefs:wrap { w mode } {
  global JEDIT_MODEPREFS
  
  frame $w
  radiobutton $w.none -anchor w \
    -text [::jldb::short_text JEpref:wrap_none] \
    -variable JEDIT_MODEPREFS($mode,textwrap) -value none
  radiobutton $w.char -anchor w \
    -text [::jldb::short_text JEpref:wrap_char] \
    -variable JEDIT_MODEPREFS($mode,textwrap) -value char
  radiobutton $w.word -anchor w \
    -text [::jldb::short_text JEpref:wrap_word] \
    -variable JEDIT_MODEPREFS($mode,textwrap) -value word
  pack \
    $w.none \
    $w.char \
    $w.word \
    -side top -expand yes -fill x
  
  return $w
}

proc jedit:prefs:font { w mode } {
  global JEDIT_MODEPREFS
  
  frame $w
  
  frame $w.top
  label $w.top.l -text [::jldb::short_text JEpref:font]
  button $w.top.default \
    -width 8 \
    -text [::jldb::short_text JEbutton:default] \
    -command "set JEDIT_MODEPREFS($mode,textfont) {default}"
  button $w.top.choose \
    -text [::jldb::short_text JEbutton:choose] \
    -command \
    "set JEDIT_MODEPREFS($mode,textfont) \[list \[::aq::prompt_font\]\]"
  
  frame $w.bot
  entry $w.bot.e -width 50 \
    -textvariable JEDIT_MODEPREFS($mode,textfont)
  
  pack $w.top.l -side left
  pack $w.top.choose -side right -padx 10 -pady 5
  pack $w.top.default -side right -pady 5
  
  pack $w.bot.e -side left -padx 10 -pady 5
  
  pack \
    $w.top \
    $w.bot \
    -side top -expand yes -fill x
  
  return $w
}

proc jedit:prefs:size { w mode } {
  global JEDIT_MODEPREFS
  
  frame $w
  label $w.wl -text [::jldb::short_text JEpref:width]
  entry $w.we -width 5 \
    -textvariable JEDIT_MODEPREFS($mode,textwidth)
  label $w.hl -text [::jldb::short_text JEpref:height]
  entry $w.he -width 5 \
    -textvariable JEDIT_MODEPREFS($mode,textheight)
  
  pack $w.wl -side left -fill x -pady 5
  pack $w.we -side left -pady 5
  pack $w.hl -side left -fill x -pady 5
  pack $w.he -side left -pady 5
  
  return $w
}






