;; $Header: /usr/build/vile/vile/macros/RCS/which.rc,v 1.19 2008/11/07 00:21:28 tom Exp $
;;
;; Show where vile looks for keyword file, and where it finds it.  If we
;; always implemented built-in filters, the natural place for this would be
;; within filters/filters.c - and no need for a macro.  But we may have
;; configured with external filters.  Rather than maintain two chunks of
;; similar C code, this is implemented as a macro (as an exercise ;-).

store-procedure WhichFilename f='Filename'
	~local %flag %attr %len %full %view
	newline
	~if &rd $1
		insert-string '* '
		setv %found="yes"
		~if &stoken 'hypertext' ',' $cfgopts
			setv %full=&pquote &path full $1
			~if &seq $os "win32"
				setv %full=&translate '\' '/' %full
			~endif
			setv %len=&len $1
			setv %view=&cat 'Hview ' %full
			setv %attr=&cat &chr 1 %len
			insert-string %attr
			insert-string %view
			insert-number 0
			insert-string 'R:'
		~endif
	~else
		insert-string '  '
	~endif
	insert-string $1
~endm

store-procedure which-filter major="MajorMode" "Prompt for, and display location of filter program"
	~if &seq '' $1
		~return
	~endif
	~local $autocolor $majormode
	~local %mybuffer %filtername %long %short %path %index %found

	setv %long=$1
	setv %short=&left $1 &sub &len $1 4

	setv %mybuffer='[Which Filter]'
	~if &isa buffer %mybuffer
		~force select-buffer %mybuffer
		setl noreadonly noview
		~if &greater $bchars 0
			goto-beginning-of-file
			delete-lines-til end-of-file
		~endif
	~else
		edit-buffer %mybuffer
		setl noreadonly noview
	~endif
	setl noai

	; turn off autocolor
	set autocolor=0
	; set the majormode so we can see what the filtername is
	~hidden setl %long
	; filtername is set only when it is not the default for the mode.
	; Capture a copy of its value, which may be ERROR.
	setv %filtername=$filtername
	~hidden unsetl %long

	; If $filtername was not set, we can still make a good guess.
	~if &error %filtername
		setv %filtername=&cat 'vile-' &cat %short '-filt'
	~elseif &seq %filtername ''
		setv %filtername=&cat 'vile-' &cat %short '-filt'
	~else
		setv %filtername=&cat &word 0 %filtername
	~endif
	setv %filtername=&cat %filtername $exec-suffix

	;
	insert-string 'Show which filter-files are tested for:'
	newline
	insert-string &cat &chr 9 %long
	newline
	insert-string '(* marks found-files)'
	2 newline
	;
	insert-string '$PATH'
	setv %index=0
	~while true
		setv %path=&token %index $pathlist-separator &env 'PATH'
		~if &seq %path ''
			~break
		~endif
		WhichFilename &pcat %path %filtername
		setv %index=&add %index 1
	~endwhile
	;
	newline
	insert-string '$cwd'
	WhichFilename %filtername
	;
	newline
	insert-string '$libdir-path'
	setv %index=0
	~while true
		setv %path=&token %index $pathlist-separator $libdir-path
		~if &seq %path ''
			~break
		~endif
		WhichFilename &pcat %path %filtername
		setv %index=&add %index 1
	~endwhile
	;
	~if &stoken 'hypertext' ',' $cfgopts
		1 goto-line
		attribute-cntl_a-sequences-til end-of-file
	~endif
	unmark-buffer
	setl readonly view
	goto-beginning-of-file

	~if &filter %short
		write-message &cat 'Filter for ' &cat %long ' is built-in'
	~else
		write-message &cat 'Filter name: ' %filtername
	~endif
~endm

store-procedure which-keywords major="MajorMode" "Prompt for, and display location of keyword files"
	~if &seq '' $1
		~return
	~endif
	~local %path %leaf %suffix %pathdot %dothides %index %mybuffer %found %long %short
	setv %found="no"
	~local $report $terse
	setv $report=0
	set terse

	setv %long=$1
	setv %short=&left $1 &sub &len $1 4
	;
	setv %mybuffer='[Which Keywords]'
	~force select-buffer %mybuffer
	~if $status
		setl noreadonly noview
		~if &greater $bchars 0
			goto-beginning-of-file
			delete-lines-til end-of-file
		~endif
	~else
		edit-buffer %mybuffer
		setl noreadonly noview
	~endif
	setl noai
	;
	insert-string 'Show which keyword-files are tested for:'
	newline
	insert-string &cat &chr 9 %long
	newline
	insert-string '(* marks found-files)'
	2 newline
	;
	setv %suffix='.keywords'
	~if &seq $os "win32"
		setv %dothides=''
		setv %pathdot='.'
	~elseif &seq $os "vms"
		setv %dothides=''
		setv %pathdot='[]'
	~else
		setv %dothides='.'
		setv %pathdot='.'
	~endif
	;
	setv %leaf=&cat %short %suffix
	insert-string '$cwd'
	WhichFilename &pcat %pathdot &cat %dothides %leaf
	;
	newline
	insert-string '$HOME'
	WhichFilename &pcat '~' &cat %dothides %leaf
	WhichFilename &pcat '~' &cat %dothides &pcat 'vile' %leaf
	;
	newline
	insert-string '$startup-path'
	setv %index=0
	~while true
		setv %path=&token %index $pathlist-separator $startup-path
		~if &seq %path ''
			~break
		~endif
		WhichFilename &pcat %path %leaf
		setv %index=&add %index 1
	~endwhile
	;
	~if &stoken 'hypertext' ',' $cfgopts
		1 goto-line
		attribute-cntl_a-sequences-til end-of-file
	~endif
	unmark-buffer
	setl readonly view
	goto-beginning-of-file
~endm

~if &seq $progname "xvile"
store-procedure which-menu "Display location of menu file"
	~local %path %leaf %suffix %pathdot %dothides %index %mybuffer %found %dotname
	setv %found="no"
	~local $report $terse
	set report=0
	set terse
	;
	setv %mybuffer='[Which Menufile]'
	~force select-buffer %mybuffer
	~if $status
		setl noreadonly noview
		~if &greater $bchars 0
			goto-beginning-of-file
			delete-lines-til end-of-file
		~endif
	~else
		edit-buffer %mybuffer
		setl noreadonly noview
	~endif
	setl noai
	;
	insert-string 'Show which menu-files are tested for:'
	newline
	insert-string &cat &chr 9 $menu-file
	newline
	insert-string '(* marks found-files)'
	2 newline
	;
	~if &seq $os "win32"
		setv %dothides=''
		setv %pathdot='.'
	~elseif &seq $os "vms"
		setv %dothides=''
		setv %pathdot='[]'
	~else
		setv %dothides='.'
		setv %pathdot='.'
	~endif
	;
	; Look for $menu-file in the current and home-directories
	setv %leaf=$menu-file
	insert-string '$cwd'
	WhichFilename &pcat %pathdot %leaf
	;
	newline
	insert-string '$HOME'
	WhichFilename &pcat '~' %leaf
	;
	; If dot hides a name, and $menu-file begins with a dot, we are done
	; with it.  Use "vilemenu.rc" in the other places.  If it does not
	; begin with a dot, continue using it.
	setv %dotname &left %leaf 1
	~if &and \
		&seq %dothides '.' \
		&seq %dotname '.'
		setv %leaf='vilemenu.rc'
	~endif
	;
	newline
	insert-string '$startup-path'
	setv %index=0
	~while true
		setv %path=&token %index $pathlist-separator $startup-path
		~if &seq %path ''
			~break
		~endif
		WhichFilename &pcat %path %leaf
		setv %index=&add %index 1
	~endwhile
	;
	~if &stoken 'hypertext' ',' $cfgopts
		1 goto-line
		attribute-cntl_a-sequences-til end-of-file
	~endif
	unmark-buffer
	setl readonly view
	goto-beginning-of-file
~endm
~endif
