#!/home/melville/p9/bin/rc
# ^ Adjust shebang accordingly.

. 9.rc
. $PLAN9/lib/acme.rc

fn acmeinit {
	# Create new window in acme, change name
	newwindow
	winname none

	# Add commands to tag in acme
	echo '| i? i* i= | DDG  | b http://' | winwrite tag
	echo -n 'Back Refresh | Print Go! Go2! | Info URLs Bookmarks!' \
		'Write! | Javascript Quit!' | winwrite tag
}

fn clearwin {
	# Select all content of window in acme and erase it
	echo -n , | winwrite addr
	winctl 'dot=addr'
	winwrite data
}

fn ebecho {
	# Write command also to acme, so it's visible.
	echo $* | winwrite body
	echo $*
}

fn setwinname {
	# Use '_ variable in eb for winname, if empty replace with "none".
	echo '!echo name ''_ | sed -e "s/\.browse$//" -e "s/^name$/& none/"' \
		'| 9p write acme/' ^ $winid ^ '/ctl'
}

fn topofwin {
	# Block till edbrowse is done.
	sleep 60 &
	echo '!kill' $apid
	wait $apid >[2=]

	# Jump to the top/beginning of text in acme
	echo -n 0 | winwrite addr
	winctl 'dot=addr'
	winctl show
}

fn sanitizere {
	# Turn special characters into dots, handle {m,n} pattern
	echo $* | tr '^$[]+*?\/' '.' | sed 's/{([0-9]+(,[0-9]+)?)}/.\1./g'
}

fn parsecmd {
	switch ($1) {
	case Back	# Go back one level
		ebecho '^'

	case Bookmarks	# Open bookmark file and print
		ebecho 'b ~/.eb/bookmarks'
		ebecho ,

	case Bookmarks!	# Add bookmark, used together with URLs command.
		ebecho 'w+ ~/.eb/bookmarks'

	case DDG*	# Searching in duckduckgo.com
		ddg = `{echo $1}
		ddg =  $ddg(2-)
		ddg = 'b http://ddg.gg/lite?q=' ^ $"ddg
		ebecho $ddg
		ebecho ,

	case Go		# Follow 1st link in the line and print
		ebecho g1
		ebecho ,

	case Go2	# Follow 2nd link and...
		ebecho g2
		ebecho ,

	case Go!	# Follow without print - for binary files etc
		ebecho g1

	case Go2!
		ebecho g2

	case Info	# Show title and address of current page
		ebecho ft
		ebecho f

	case Javascript	# Toggle off/on javascript
		ebecho js

	case Print	# Print whole file
		ebecho ,

	case Quit!	# With exclamation mark, must be drag-selected.
		ebecho qt
		windel sure
		exit

	case Refresh	# Refresh page - can be useful for JS
		ebecho rf

	case URLs	# Show addresses behind links in selected line.
		ebecho A
		ebecho ,

	case Write!	# Save (binary) file to disk.
		ebecho w/

	case *		# Send selection as plain command to edbrowse
		ebecho $1
	}

}

# man acmeevent(1)
fn event {
	# $1 - c1 origin of event
	# $2 - c2 type of action
	# $3 - q0 beginning of selection
	# $4 - q1 end of selection
	# $5 - eq0 beginning of expanded selection
	# $6 - eq1 end of expanded selection
	# $7 - flag
	# $8 - nr number of runes in $9
	# $9 - text
	# $10 - chorded argument
	# $11 - origin of chorded argument

	switch ($1$2) {
	case E*		# write to body or tag
	case F*		# generated by ourselves; ignore
	case K*		# type away we do not care
	case Mi		# mouse: text inserted in tag
	case MI		# mouse: text inserted in body
	case Md		# mouse: text deleted from tag
	case MD		# mouse: text deleted from body
			# We don't care about those events
		winwriteevent $*

	case Mx MX	# button 2 in tag or body
		if (~$9 Cut Look Paste Snarf)
			winwriteevent $*
		if not {
			clearwin
			parsecmd $9
			setwinname
			topofwin
		}

	case Ml ML	# button 3 in tag or body
			# Send selection as regexp to edbrowse
		clearwin
		regexp = `{sanitizere $9}
		ebecho / ^ $"regexp ^ /
		setwinname
		topofwin
	}
}

# Initialize acme and start loop
acmeinit
@{wineventloop} | edbrowse | winwrite body
