# -*- shell-script -*-

################################################################################

+configure-core-modes () {

    +mode run-dialog
      --terminal '__TERMINAL__'
      --geometry '40x1 40x8'
      --completion 'menu-complete complete 8'
      --unmap true
      --feedback true
      --logfile '/dev/null'

    +mode-run-dialog-init () {	
	
	HISTFILE="$($bashrun_data_home/history)"
	PS1='>'
	stty -ixon
	stty eof 0201
    }

    +mode terminal
      --terminal '__TERMINAL__'
      --feedback true
      --cmdnfh true

    +mode-terminal-init() {

      local keymaps="emacs"
      §set? vi && keymaps="vi-insert vi-command"

      for keymap in $keymaps; do	    
	+keymap "$keymap"

	+unbind run      '\C-m'
	+unbind run      '\C-j'
	+unbind term-run '\e\C-m'
	+unbind pass     '\ew'
	+unbind quit     '\C-q'
	+unbind abort    '\C-g'
	
	+bind run '\e\C-m'
	+bind term-run '\et'	
      done
    }
}

+configure-core-terminals () {
    
    +terminal default --command '__EXEC_TERMINAL__'

    +terminal notify 
      --command '__EXEC_TERMINAL__'
      --geometry "40x4+0+0"
}

+configure-core-templates () {

    # terminal command line templates

    # xterm needs metaSendsEscape for Alt-x bindings
    local xterm_fixes='-xrm XTerm**metaSendsEscape:True -xrm XTerm**allowWindowOps:True'

    # mlterm needs these for Alt-x bindings
    local mlterm_fixes='-k esc -K alt'

    # urxvt needs these keysyms for Ctrl-arrow bindings
    local urxvt_fixes='-keysym.Control-Up "\033[1;5A" -keysym.Control-Down "\033[1;5B" '
    urxvt_fixes+='-keysym.Control-Left "\033[1;5D" -keysym.Control-Right "\033[1;5C"'

    +template xterm --bashrun_options "+sb $xterm_fixes"
    +template urxvt --bashrun_options "+sb $urxvt_fixes"
    +template urxvtc --bashrun_options "+sb $urxvt_fixes"
    +template urxvtcd --bashrun_options "+sb $urxvt_fixes"
    +template mrxvt --bashrun_options "+sb -ht +showmenu"
    +template aterm

    +template mlterm 
      --boldfont "" 
      --bashrun_options "-s=false $mlterm_fixes"

    +template Eterm
      --classname "-n%n"
      --geometry "-g%g"
      --font "-F%f"
      --boldfont "--bold-font=%B"
      --foreground "-f%F"
      --background "-b%B"
      --bashrun_options "--scrollbar=0"

    +template gnome-terminal
      --classname "--name=%n"
      --geometry "--geometry=%g"
      --font ""
      --boldfont ""
      --foreground ""
      --background ""
      --bashrun "-e %@"
      --execute "-e bash\ -c\ %@@"
      --bashrun_options "--hide-menubar"
}

+configure-core-rules () {
    +rule run --match 'mplayer'
    +rule term-page --match 'dict'
 }

+configure-core-handlers () { :; }
+configure-core-bookmarks () { :; }
+configure-core-emacs-mode () { :; }
+configure-core-vi-mode () { :; }

+configure-core-actions () {

    # temporary function to set the (otherwise readonly) boolean
    # 'core' property
    function §action.set_core {

	local input="$1";
	local value=0;

	[[ -n "$input" ]] && value=1;
	[[ "$input" =~ ^([Yy]es|[Tt]rue|[Oo]n|1)$ ]] && value=1;
	[[ "$input" =~ ^([Nn]o|[Ff]alse|[Oo]ff|0)$ ]] && value=0;

	_bashrun_actions[${_bashrun_actions_position}+6]="$1"
    }

    +action 'reload'
      --description 'Reload configuration'
      --input 0
      --remote 0
      --core 1

      function +action-reload {
	  bashrun_status="Reloading..."
	  tput civis >&2
	  bashrun_engine_reload=1
	  return 2
      }

    +action 'pass'
      --description 'Passthrough command'
      --input 0
      --remote 0
      --core 1

      function +action-pass { 
 	  $bashrun_command
	  return 2 
      } 

    +action 'abort'
    --description 'Abort (unmap window)'
    --input 0
    --remote 0
    --core 1
    
    +action-abort () {
	READLINE_LINE=""
	§window.unmap
	return 1
    }

    +action 'quit'
    --description 'Exit bashrun2'
    --input 0
    --remote 0
    --core 1
    
    +action-quit () {
	shopt -u checkjobs
	exit 0
    }
    
    +action 'debug'
    --description "Toggle debugging"
    --input 0
    --remote 0
    --core 1
    
    +action-debug () {
	
	if ! (( bashrun_debug )); then
	    bashrun_debug=1
	    §debug "debug mode" -g "enabled"
	    §window.size --hint "80x24"
	else
	    bashrun_debug=0
	    §debug "debug mode" -r "disabled"
	    §window.size --hint "$(§mode.get_geometry $(§mode.get_current))"
	    tput clear

	fi
	return 2
    }
    
    +action 'run'
    --description 'Run command'
    --input 1
    --core 1
    
    +action-run () 
    { 
 	if ! executable?; then
	    if ! handlers; then
		return 1
	    fi
	fi
	rules
	return 0
    }
    
    +action 'term-run'
    --description 'Run command in terminal'
    --input 1
    --core 1
    
    +action-term-run () 
    { 
 	if ! executable?; then
	    if ! handlers; then
		return 1
	    fi
	fi

	terminal
	return 0
    }
    
    +action 'term-page'
    --description 'Run command in terminal and page ($PAGER)'
    --input 1
    --core 1
    
    +action-term-page () 
    { 
 	if ! executable?; then
	    if ! handlers; then
		return 1
	    fi
	fi

	pager
	terminal
	return 0
    }
    
    +action 'term-hold'
    --description 'Run command in terminal and hold'    
    --depends 'tput'
    --input 1
    --core 1
    
    +action-term-hold () 
    { 
 	if ! executable?; then
	    if ! handlers; then
		return 1
	    fi
	fi

	hold
	terminal
	return 0
    }

    +action 'term-notify'
      --description 'Run command in terminal and hold for two seconds'
      --depends 'tput sleep'
      --core 1
      --input 1

    +action-term-notify () 
    { 
	if ! executable?; then
	    if ! handlers; then
		return 1
	    fi
	fi

	hold 2
	terminal notify
	return 0
    }
    
    +action 'su-run'
    --description 'Run command as root'
    --input 1
    --depends 'su'
    --core 1
    
    +action-su-run () 
    { 
	action run || return 1
	user root
	return 0
    }
    
    +action 'su-term-run'
    --description 'Run command in terminal as root'
    --input 1
    --depends 'su'
    --core 1
    
    +action-su-term-run () 
    { 
	action term-run || return 1
	user root
	return 0
    }
    
    +action 'su-term-page'
    --description 'Run command in terminal as root and page ($PAGER)'
    --input 1
    --depends 'su'
    --core 1
    
    +action-su-term-page () 
    { 
	action term-page || return 1
	user root
	return 0
    }
    
    +action 'su-term-hold'
    --description 'Run command in terminal as root and hold'
    --input 1
    --depends 'su tput'
    --core 1
    
    +action-su-term-hold () 
    { 
	action term-hold || return 1
	user root
	return 0
    }

    +action 'su-term-notify'
      --description 'Run command in terminal as root and hold for two seconds'
      --core 1
      --input 1    
      --depends 'su tput sleep'
    
    +action-su-term-notify () 
    { 
	action term-notify || return 1
	user root
	return 0
    }
    
    +action 'show-manual'
    --description 'Show manual page for command'
    --input 1
    --depends 'man'
    --core 1
    
    +action-show-manual () {
	command "man $(word)"
	terminal
	return 0
    }
    
    +action 'show-info'
    --description 'Show info page for command'
    --input 1
    --depends 'info'
    --core 1
    
    function +action-show-info {
	command "info $(word)"
	terminal
	return 0
    }
    
    +action 'show-help'
    --description 'Show help for bash builtin'
    --input 1
    --core 1
    
    function +action-show-help {
	if builtin?; then
	    command "help $(word)"
	    pager
	    terminal
	    return 0
	else
	    debug "$(word): not a builtin, showing manual"
	    action show-manual
	fi	    
    }

    +action 'copy-to-clipboard'
      --description "Copy command output to clipboard"
      --depends 'xclip'
      --core 1

    function +action-copy-to-clipboard {
	command "($(command)) | xclip"
	return 0
    }
    
    +action 'filter-clipboard' 
      --description "Run command as filter on clipboard"
      --depends 'xclip'
      --core 1

    function +action-filter-clipboard {	
	command "xclip -o | ($(command)) | xclip -i"
    }
    
    +action 'browse'
      --description 'Open url ($BROWSER)'
      --input 1
      --core 1
    
    function +action-browse {
	quote-command
	command "$BROWSER $(command)"
	return 0
    }
    
    +action 'google-search'
      --description 'Google words ($BROWSER)'
      --input 1
      --core 1
    
    function +action-google-search {

	command "http://www.google.com/search?q=$(command)"
	action browse
    }
    
    +action 'dict-lookup'
      --description 'Lookup words in dictionary ($DICT)'
      --input 1
      --core 1
    
    function +action-dict-lookup {
	command "$DICT $(command)"
	action term-page
    }
    
    +action 'bashrun-manual'
      --description 'Show bashrun2 manual'
      --input 0
      --remote 0
      --depends 'man'
      --core 1
    
    function +action-bashrun-manual {
	command "bashrun2"
	action show-manual
    }

    +action 'bashrun-bindings'
    --description 'Show current keybindings ($PAGER)'
    --input 0
    --remote 0
    --core 1
    
    function +action-bashrun-bindings {

	local tmpfile="$bashrun_cache_home/keybindings"
	if [[ ! -f "$tmpfile" ]]; then
	    §bindings.print > "$tmpfile"
	    §bindings.modified 0

	elif §bindings.modified?; then
	    §bindings.print > "$tmpfile"
	    §bindings.modified 0
	fi
	command "cat '$tmpfile'"
	action term-page
    }

    +action 'cycle-size'
    --description 'Cycle through geometries of the current mode'
    --input 0
    --remote 0
    --core 1
    
    function +action-cycle-size {
	if ! (( bashrun_debug )); then
	    if [[ -n "$(§mode.get_geometry 1)" ]]; then
		
		local current="$(§mode.get_current)"
		((current+=1))
		
		local next="$(§mode.get_geometry $current)"
		if [[ -z "$next" ]]; then
		    current=0
		fi
		next="$(§mode.get_geometry $current)"
		
		§window.size --hint "$next"
		§mode.set_current $current	
	    fi
	fi
	tput clear
	return 2
    }

    +action resize-up
      --description "Resize terminal up"
      --input 0
      --remote 0
      --core 1

    +action-resize-up() {
	§window.resize up
	return 2
    }

    +action resize-down
      --description "Resize terminal down"
      --input 0
      --remote 0
      --core 1

    +action-resize-down() {
	§window.resize down
	return 2
    }

    +action resize-left
      --description "Resize terminal left"
      --input 0
      --remote 0
      --core 1

    +action-resize-left() {
	§window.resize left
	return 2
    }

    +action resize-right
      --description "Resize terminal right"
      --input 0
      --remote 0
      --core 1

    +action-resize-right() {
	§window.resize right
	return 2
    }
      
    # remove temporary setter function
    unset -f §action.set_core
}


