;; $Header: /usr/build/vile/vile/macros/RCS/which.rc,v 1.8 2004/10/30 00:12:27 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
	newline
	~if &rd $1
		insert-string '* '
		setv %found="yes"
	~else
		insert-string '  '
	~endif
	insert-string $1
~endm

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

	~if &seq 'mode' &right $1 &sub &len $1 3
		; 9.4e
		setv %long=$1
		setv %short=&left $1 &sub &len $1 4
	~else
		; pre-9.4e
		setv %long=&cat $1 'mode'
		setv %short=$1
	~endif
	;
	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
	~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
	;
	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
	~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
	;
	unmark-buffer
	setl readonly view
	goto-beginning-of-file
~endm
~endif
