*! version 1.0.4  26mar2003 ESH add compound quotes
*! version 1.0.3  04jan2001
program define notes2
	version 4.0

	if `"`*'"'=="" {
		dlnote list
		exit
	}

	if `"`1'"'==":" {
		mac shift
		mknote _dta `"`*'"'
		exit
	}
	if substr(`"`1'"',-1,1)==":" {
		local vn = substr(`"`1'"',1,length(`"`1'"')-1)
		mac shift
		mknote `vn' `"`*'"'
		exit
	}
	if `"`2'"'==":" {
		local vn `"`1'"'
		mac shift 2
		mknote `vn' `"`*'"'
		exit
	}
	if `"`1'"'==substr("list",1,max(1,length(`"`1'"'))) {
		local 1 "list"
	}
	if `"`1'"'=="drop" | `"`1'"'=="list" {
		dlnote `*'
		exit
	}
	dlnote list `*'
end


program define mknote /* name text */
	version 4.0
	local vn `"`1'"'
	local text `"`2'"'

	if `"`text'"'=="" { error 198 }

	local n : char `vn'[note0]
	if "`n'"=="" {
		local n 1
	}
	else	local n = `n' + 1

	parse `"`text'"', parse(" ")
	local i 1
	while `"``i''"' != "" {
		if `"``i''"' == "TS" {
			local ts = substr("$S_DATE $S_TIME",1,17)
			local `i' "`ts'"
		}
		local i=`i'+1
	}
	local text `"`*'"'
	char `vn'[note0] `n'
	char `vn'[note`n'] `"`text'"'
end


program define dlnote
	version 4.0
	local cmd `"`1'"'
	mac shift

	parse `"`*'"', parse(" /")
	local i 1
	while `"``i''"' != "" & `"``i''"' != "in" {
		if `"``i''"'=="_dta" {
			local dta "_dta"
		}
		else {
			local list "`list' ``i''"
			if `"``i''"' == "_all" { local dta "_dta" }
		}
		local i=`i'+1
	}
	if `i'==1 {
		if "`cmd'"=="list" {
			local list "_all"
			local dta "_dta"
		}
		else	error 198
	}
	if `"``i''"' == "in" {
		local i=`i'+1
		confirm integer number ``i''
		local num0 ``i''
		local num1 ``i''
		local i=`i'+1
		if `"``i''"'=="/" {
			local i=`i'+1
			if `"``i''"'=="l" {
				local num1 9999
			}
			else {
				confirm integer number ``i''
				local num1 ``i''
			}
			local i=`i'+1
		}
		if `"``i''"' != "" { error 198 }
	}
	else {
		local num0 1
		local num1 9999
	}

	if "`list'" != "" {
		local varlist "req ex"
		parse "`list'"
		parse "`dta' `varlist'", parse(" ")
	}
	else {
		local 1 `dta'
		local 2
	}

	if `num1'<`num0' { error 198 }

	local cnt 0
	global S_1 0
	local i 1
	while `"``i''"' != "" {
		local n : char ``i''[note0]
		if "`n'"!="" {
			if `num0' <= `n' {
				local max = min(`n',`num1')
				note`cmd' ``i'' `num0' `max'
				local cnt = `cnt' + $S_1
			}
		}
		local i=`i'+1
	}
	if "`cmd'"=="list" { exit }
	if `cnt' != 1 {
		local plural "s"
	}
	di in gr "(`cnt' note`plural' dropped)"
end

program define notedrop /* [varname] # [-#] */
	version 3.1
	local vn `"`1'"'
	local num0 `2'
	local num1 `3'

	global S_1 0
	local i `num0'
	while `i' <= `num1' {
		local text : char `vn'[note`i']
		if `"`text'"' != "" {
			char `vn'[note`i']
			global S_1 = $S_1 + 1
		}
		local i=`i'+1
	}

	local i : char `vn'[note0]
	while `i' >= 1 {
		local text : char `vn'[note`i']
		if `"`text'"' != "" {
			char `vn'[note0] `i'
			exit
		}
		local i=`i'-1
	}
	char `vn'[note0]
end

program define notelist /* name # # */
	version 7
	args name n1 n2
	di in ye _n `"`name':"'
	forvalues i = `n1'/`n2' {
		local text : char `name'[note`i']
		if `"`text'"' != "" {
			local l = max(0,3-length("`i'"))
			di in gr "{p `l' 6}"
			di in gr "`i'."
			di in gr `"`text'"'
			di "{p_end}"
		}
	}
end