;;
;; Macros for obtaining manual pages and then attributing them.
;;
;; Author: Kevin Buettner
;;
;; $Header: /usr/build/vile/vile/macros/RCS/manpage.rc,v 1.19 2004/06/11 11:49:41 cmorgan Exp $
;;

store-procedure ShowManpage "Prompt for, and display a manual-page"
	~local %filter %manpage %manpagebuf $error-buffer
	set-variable %filter &lookup e+l+p &cat vile-manfilt $exec-suffix
	~if &seq %filter ""
		write-message "[Cannot find manpage-filter]"
		~return
	~endif
	~force set-variable %manpage &query "man page? "
	~if &or &error %manpage &seq %manpage ""
		~return
	~endif
	set-variable %manpagebuf &cat "<" &cat %manpage ">"
	set terse
	~force select-buffer %manpagebuf
	~if &not $status
		edit-file &cat "!man " &cat %manpage &cat " | " %filter
		setl txtmode
		setl autocolor=0
		~force rename-buffer %manpagebuf
		~force error-buffer %manpagebuf
		write-message "[Attaching bold and underline attributes...]"
		goto-beginning-of-file
		setl noview
		attribute-cntl_a-sequences-til end-of-file
		unmark-buffer
		setl view
		goto-beginning-of-file
		unsetv $cfilname
		write-message "[Done formatting manual page.]"
	~endif
	set noterse
~endm
bind-key ShowManpage ^X-m

store-procedure ShowFormatted "Render the current buffer with nroff -man or -ms"
	~local %type %filter %manpagesrc %manpagebuf $error-buffer
	set-variable %filter &lookup e+l+p &cat vile-manfilt $exec-suffix
	~if &seq %filter ""
		write-message "[Cannot find manpage-filter]"
		~return
	~endif
	~if &sin $cbufname "<"
		set-variable %manpagebuf $cbufname
		set-variable %manpagesrc &mid $cbufname 2 &sub &len $cbufname 2
		~force select-buffer %manpagesrc
		~if &not $status
			write-message &cat "Buffer not found: " %manpagesrc
			~return
		~endif
	~else
		set-variable %manpagebuf &cat "<" &cat $cbufname ">"
	~endif
	set terse
	~force kill-buffer %manpagebuf
	setv %type &path end $cfilname
	~if &or &seq $majormode "html" \
		&seq $majormode "xml"
		edit-file &cat "!lynx -force_html -with_backspaces -dump -width=" &cat $pagewid &cat " " &cat &pquote $cfilname &cat " | " %filter
	~elseif &seq $majormode "perl"
		~if &seq $pathlist-separator ":"
			edit-file &cat "!pod2man " &cat &pquote $cfilname &cat " | tbl | nroff -man | " %filter
		~else
			edit-file &cat "!pod2text " &cat &pquote $cfilname &cat " | " %filter
		~endif
	~elseif &seq $pathlist-separator ":"
		~if &seq %type ".ms"
			edit-file &cat "!tbl " &cat &pquote $cfilname &cat " | nroff -ms | " %filter
		~else
			edit-file &cat "!tbl " &cat &pquote $cfilname &cat " | nroff -man | " %filter
		~endif
	~else
		~if &seq %type ".ms"
			edit-file &cat "!cawf -ms  " &cat &pquote $cfilname &cat " | " %filter
		~else
			edit-file &cat "!cawf -man " &cat &pquote $cfilname &cat " | " %filter
		~endif
	~endif
	setl txtmode
	setl autocolor=0
	~force rename-buffer %manpagebuf
	~force error-buffer %manpagebuf
	write-message "[Attaching bold and underline attributes...]"
	goto-beginning-of-file
	setl noview
	attribute-cntl_a-sequences-til end-of-file
	unmark-buffer
	setl view
	goto-beginning-of-file
	unsetv $cfilname
	write-message "[Done formatting manual page.]"
	set noterse
~endm
bind-key ShowFormatted ^X-n
