*! v0.8.2 2018-01-04 - delete defaults
*! v0.8.1 2017-09-11 Scott Long  - workingdir.pkg
* v0.8.0 2017-09-11 Scott Long  - workingdir.pkg

// list cdproject commands in PERSONAL directory

capture program drop listcd
program define listcd
    version 13.0
    syntax [ , DETails DIRectories NODELete hold]

if "`nodelete'"=="" local delete delete

    //  setup

    * cd commands are located in PERSONAL
    local cdsavefiles : dir "`c(sysdir_personal)'" files "cd*.ado"

    * later restore current working directory
    local currentpwd `"`c(pwd)'"'

    //  length of command names to line up output

    local cmdwidth = 0
    foreach file in `cdsavefiles' {
        local filelen = strlen("`file'")
        if `filelen'>`cmdwidth' local cmdwidth = `filelen'
    }
    local cmdwidth = `cmdwidth' - 3 // for .ado

//  loop through cd commands

    display as text " "
    foreach file in `cdsavefiles' {

        local cmd : subinstr local file ".ado" "", all
        capture program drop `cmd' // to run command in PERSONAL
        capture quietly `cmd'

        local wdmissing = 0
        if _rc!=0 {
            local wdmissing = 1 // can't change to wd
        }

        * list globals
        if "`details'"=="details" {
            di `"{stata `cmd':`cmd'}"'
            _list_s_cd_globals
            di
            if `wdmissing' {
                di as error _col(15) "working directory not found"
            }
        }

        * list note or wd
        else {
            local note `"$S_cdnote"'
            if ("`note'"=="" | "`directories'"=="directories") ///
                local note `"$S_cdwd"'
            local nspaces = `cmdwidth'-strlen("`cmd'")
            di `"{space `nspaces'}{stata `cmd':`cmd'} -"' ///
                as text `" `note'"' _continue
            if "`hold'"=="hold" {
                di `" ({stata dropcd `cmd',hold:drop})"' _continue
            }
            else if "`delete'"=="delete" {
                di `" - {stata dropcd `cmd':drop}"' _continue
            }
            if `wdmissing' {
                local colloc = `cmdwidth' + 2
                di as error " (directory not found)" as text ""
            }
            else display
        }
    } // file loop

    if "`delete'"=="" {
        di _new as text "To delete commands, run " ///
                as result "help dropcd" ///
                as text " or " ///
                `"{stata listcd, delete:click here}"' ///
                as text "." _continue
    }

    if "`details'"=="" {
        di _new as text "Run " ///
                as result "listcd, details" ///
                as text " for list of " ///
                as result "S_cd*" ///
                as text " macros"
    }

    //  restore starting working directory

    quietly cd `"`currentpwd'"'

end
exit
* cdlist.ado v0.2.0 Scott Long 2017-07-27
* cdlist.ado v0.3.0 Scott Long 2017-07-30 add cdinfo
* cdlist.ado v0.4.0 Scott Long 2017-07-30
* cdlist.ado v0.5.0 Scott Long 2017-07-30 // links to commands
* cdlist.ado v0.5.1 Scott Long 2017-08-09
* cdlist.ado v0.6.0 Scott Long 2017-08-13
* listcd.ado v0.7.0 Scott Long 2017-09-07