; --------------------------
; 	delete commit message
; --------------------------

; run with ESC-X, 'source', 'pg.mac'

store-procedure pg-hunt
	local %save_cmode %save_search

	; save search and mode
	set %save_cmode $cmode
	set %save_search $search

	add-mode "magic"
	add-mode "exact"

	beginning-of-line

	!if &seq %pghuntdir "F"
		; look for a full pattern of comment/blank-line, author, commit date and hash
		; ~n is a newline
		!force search-forward "^[-<!]*~nAuthor: .*~n20[0-9][0-9]-[0-9][0-9]-[0-9][0-9] \[[0-9a-f][0-9a-f]*\].*"
	!else
		;  going forward then backward wraps around at the top with an error, not sure why 2023-05-17
		3 previous-line

		!force search-reverse "^[-<!]*~nAuthor: .*~n20[0-9][0-9]-[0-9][0-9]-[0-9][0-9] \[[0-9a-f][0-9a-f]*\].*"
		3 next-line
	!endif

	!if &seq $status TRUE
		beginning-of-line
		; move three, not two, so reword is on a blank line
		2 next-line

		; If line is blank, move to the next line;  this happens when XML comments are added.
		!if &equ $lwidth 0
			!force next-line
		!endif

		; force window to center on the cursor
		redraw-display
	
		; must update so the move works
		update-screen
	
		; move up 25% of window width so we can see more of the commit
		; compute each time in case the window size has changed since loading the macro file
		set %downlines &DIVide $wline 4
		!if &gre $curline %downlines
			%downlines move-window-down
		!endif
	!endif

	; restore search and cmode
	set $search %save_search
	set $cmode %save_cmode

	run pg-keys
!endm


store-procedure pg-hunt-forward
	set %pghuntdir "F"
	run pg-hunt
!endm


store-procedure pg-hunt-backward
	set %pghuntdir "R"
	run pg-hunt
!endm


store-procedure pg-delete-commit
	local %save_cmode %save_search, %curline

	; save search and cmode
	set %save_cmode $cmode
	set %save_search $search

	add-mode "magic"
	add-mode "exact"

	beginning-of-line

	; We might be at the top of the commit comment, and if so, we need to move down three
	; lines so we can search backward and find the start of the comment.
	!force 3 next-line

	set %curline $curline

	!force search-reverse "^[-<!]*~nAuthor: .*~n20[0-9][0-9]-[0-9][0-9]-[0-9][0-9] \[[0-9a-f][0-9a-f]*\].*"
	; $status doesn't report the failure of wrapping to the bottom so test $curline
	!if &not &gre $curline %curline
		set-mark
	
		; go past search term
		!force 2 next-line
	
		search-forward "^[-<!]*~nAuthor: .*~n20[0-9][0-9]-[0-9][0-9]-[0-9][0-9] \[[0-9a-f][0-9a-f]*\].*"
		beginning-of-line
		!force 2 previous-line
		kill-region

		; restore search and cmode
		set $search %save_search
		set $cmode %save_cmode
	
		pg-hunt-forward
	!else	; restore search and cmode
		set $search %save_search
		set $cmode %save_cmode

		; could infinite loop
		!if &gre $curline %curline
			; need blank line at the top for function to work
			beginning-of-file
			insert-string "~n"
			beginning-of-file
	
			; recursion
			pg-delete-commit
		!endif
	!endif
!endm


store-procedure pg-delete-commit-text
	local %save_cmode %save_search

	; save search and cmode
	set %save_cmode $cmode
	set %save_search $search
	
	add-mode "magic"

	12 set-mark
	!force search-forward "^<!--"
	!if &seq $status TRUE
		12 goto-mark

		; prepare for newlines
		end-of-line

		; if current line has text, add a newline below it
		!if &not &equ $lwidth 0
			newline
		!endif
	
		; add blank line below
		newline

		set-mark
		search-forward "^<!--"
		beginning-of-line
		kill-region
	
		; restore search and cmode
		set $search %save_search
		set $cmode %save_cmode

		; this will save/restore the settings too
		pg-hunt-forward
	!else
		; restore search and cmode
		set $search %save_search
		set $cmode %save_cmode

		write-message "XML comment not found"
	!endif
!endm

macro-to-key pg-hunt-forward		FN3
macro-to-key pg-hunt-backward		FN4
macro-to-key pg-delete-commit		FN7
macro-to-key pg-delete-commit-text	FN8


store-procedure pg-keys
	write-message "F3/F4 for back/forward commits  |  F7 delete commit  |  F8 delete commit detail below cursor"
!endm


run pg-keys
