;**
;**		qh.m
;**
;**		Interface the MS OS/2 SDK Quick-Help facility to Brief
;**
;**			Manuel A. Ulloa
;**			cserv: 71535,2017
;**			bix: mulloa
;**
;**			Please forward me any relevant changes and/or enhancemets.
;**
;**			The macro will us QH.EXE to search for help on the specified
;**		topic. The topic can be specified by either marking some word
;**		and invoking qh (by typing Ctrl-Q) or by invoking qh and then
;**		typing the topic. It depends on QH being correctly installed
;**		(in the path, etc.) It places the info in a file called B.QH
;**		placed in the buffer specied in the TMP environment variable.
;**		Note that QH.EXE can not be invoked in the background as it
;**		hangs. Furthermore, if the topic is not found QH will put up
;**		a popup box. Pressing any key makes it go away. A succesful
;**		match returns 2 for some reason (not 0 as expected).
;**
;**		04/05/89		MAU
;**			First OS/2 Implementation (Brief V2.11)
;**


;**	It needs this function. Found in ARM.M. Uncomment
;**	to be compiled in diferent files.
;**
;**	(extern _armgt		; from the arm.m file
;**	)


;** (macro qh
;**		(assign_to_key "<Alt-q>" "do_qh")
;** )


(macro do_qh
	(
		(string	_hlptxt
					tfil
		)
		(int		ret_code
		)
		(global	_hlptxt
		)

		(= tfil (inq_environment "TMP"))

		; check if path is the root
		(if (== (substr tfil (strlen tfil) 1) "\\")
			(= tfil (+ tfil "b.qh"))
		;else
			(= tfil (+ tfil "\\b.qh"))
		)

		; get argumnet for qh
		(if (inq_marked)
			(= _hlptxt (_armgt))
		;else
			(
				(if ( ! (get_parm 0 _hlptxt "QH: " NULL _hlptxt) )
					(return)
				)
				;else
			)
		)


		(delete_buffer (create_buffer "b.qh" tfil 0))
		(message "Searching for \"%s\"..." _hlptxt)
		(= ret_code (dos (+ "QH " (+ _hlptxt (+ " -p " (+ tfil " -t all >& NUL")))) 0))
		(if (== ret_code 0)		
			(
				(message "Found \"%s\"." _hlptxt )
				(edit_file tfil)
			)
		;else
			(error "Topic not found")			
		)
	)
)


;**
;**	Return marked string
;**

(macro _armgt
	(
		(string	mtxt
		)
		(int		start_line
					end_line
					start_col
					end_col
					temp_int
		)

		(if (inq_marked)
			(
				(save_position)
				(inq_position end_line end_col)
				(swap_anchor)
				(inq_position start_line start_col)
				(raise_anchor)

				;**	If the starting column is greater than the ending
				;**	column, swap the variables

				(if (> start_col end_col)
					(
						(= temp_int start_col)
						(= start_col end_col)
						(= end_col temp_int)
						(move_abs 0 start_col)
					)
					;else
				)

				(if (== start_line end_line)
					(
						(= temp_int (+ (- end_col start_col) 1) )
						(= mtxt (read temp_int))
					)
					;else
					(= mtxt (read))
				)

				(restore_position)
			)
		;else
			(= mtxt "")
		)

		(returns mtxt)
	)
)
