cd

Redlang documentation | custom utility

cd change directory (shell shortcut function). has been extended for fuzzy search and list directory. For example:

cd redlang.red

will go to

c:\projects\red\redlang.red

Usage

load cd library

use red core CD or load library:

load https://redlang.red/cd

cd child-directory

if a direct child directory exists, it will go straight to it, otherwise it will search and go to the first child which matches. for example:

cd c:\projects\red\redlang.red

will go straight to :

c:\projects\red\redlang.red

whereas

cd redlang

will search for redlang and after a while, find and goes to:

c:\projects\red\redlang.red

if it is the first child encountered.

cd/cd-only

/cd-only option will not list directory, for example:

cd/cd-only redlang.red

Source

You can get the source of cd by typing:

source cd

source code

source is below:



unless value? '.syscd [
    unless value? '.cache [
        unless value? '__OFFLINE_MODE__ [__OFFLINE_MODE__: false] unless value? '__DEBUG_MODE__ [__DEBUG_MODE__: false] either __OFFLINE_MODE__ [
            if error? try [
                do-thru http://redlang.red/cache.red 
                if __DEBUG_MODE__ [
                    print "do cache.red from cache"
                ]
            ] [
                if __DEBUG_MODE__ [
                    print "error do cache.red from cache"
                ]
            ]
        ] [
            if error? try [
                do-thru/update http://redlang.red/cache.red 
                if __DEBUG_MODE__ [
                    print "refreshing cache.red in cache"
                ]
            ] [
                __OFFLINE_MODE__: true 
                if error? try [
                    do-thru http://redlang.red/cache.red
                ] [
                    print "error do cache.red from cache"
                ]
            ]
        ]
    ] 
    unless value? '.do-cache [
        .do-cache: function [
            "0.0.1.0\11\cd.3.red" 
            param>urls 
            /_debug
        ] [
            unless value? '.do-trace [
                print "loading do-trace in 0.0.1.0\11\cd.2.red" 
                do https://redlang.red/do-trace
            ] 
            either block? param>urls [
                if _debug [
                    do-trace 49 [
                        ?? param>urls
                    ] %gen-html-page.red
                ] 
                foreach ~url param>urls [.do-cache ~url]
            ] [~url: param>urls 
                if _debug [
                    do-trace 57 [
                        ?? ~url
                    ] %gen-html-page.red
                ] 
                either value? '.cache [.cache ~url] [do ~url]
            ]
        ]
    ] do-cache: :.do-cache 
    .do-cache [
        https://redlang.red/section.red 
        https://redlang.red/do-trace.red 
        https://redlang.red/search-dir.red
    ] 
    .syscd: :cd 
    .cd: func [
        "Change directory (shell shortcut function)." 
        [catch] 'param>path [file! word! path! unset! string! paren! url! tuple! integer!] {Accepts %file, :variables and just words (as dirs)} /only "do not list folders" 
        /no-history 
        /search 
        /up 
        /_build 
        /no-autoexec "don't autoexecute %.red and %autoload.red" 
        /silent 
        /_debug 
        /local ~deep-counter ~first-path ~static>history
    ] [
        .section "cd function" [
            .section "internal functions" [
                ~.change-dir: does [
                    change-dir (~path) unless only [
                        dir
                    ] 
                    return (to-local-file ~path)
                ]
            ] ".finish internal functions" switch type?/word get/any 'param>path [
                unset! [
                    .section {case no arg: just list directory, return current directory} [
                        dir
                    ] {.finish case no arg: just list directory, return current directory} 
                    ~return: what-dir
                ] 
                file! word! path! unset! string! paren! url! tuple! integer! [
                    if ('? = param>path) [.section {case arg = ?, request dir, continue except none folder} [
                        param>path: request-dir/dir (what-dir) if none? (param>path) [.section "if none folder chosen, .end" [
                            ~return: none
                        ]]
                    ] {.finish case arg = ?, request dir, continue except none folder}] 
                    .section "1. get path" [
                        ~path: to-red-file form :param>path 
                        if _debug [
                            do-trace 96 [
                                ?? ~path
                            ] %cd2.7.red
                        ]
                    ] ".finish 1. get path" 
                    either exists? (~path) [.section "1.1/ if path exists, change path, .end" [
                        ~return: (~.change-dir) if _debug [
                            do-trace 63 [
                                ?? ~return
                            ] %03.cd-function.red
                        ]
                    ] ".finish 1.1/ if path exists, change path, .end"] [
                        .section "1.2/ if path doesn't exist, search in children" [
                            ~path: .search-dir (param>path) if _debug [
                                do-trace 68 [
                                    ?? ~path
                                ] %03.cd-function.red
                            ] 
                            either none? ~path [
                                .section "1.2.1/ case search-dir found none, return" [
                                    ~return: none
                                ] ".finish 1.2.1/ case search-dir found none, return"
                            ] [
                                .section {1.2.2/ case search-dir found a dir, change to that dir} [
                                    ~return: (~.change-dir) if _debug [
                                        do-trace 82 [
                                            ?? ~path 
                                            ?? ~return
                                        ] %03.cd-function.red
                                    ]
                                ] ".finish 1.2.1/ case search-dir found none, return"
                            ]
                        ] {.finish 1.2/ if path doesn't exist, search in children}
                    ]
                ]
            ]
        ] ".finish cd function" 
        print ~return 
        return ~return
    ] 
    system/words/..: function [] [
        .cd ".." 
        return to-local-file what-dir
    ] 
    system/words/...: function [] [
        .cd ".." 
        .cd ".." 
        return to-local-file what-dir
    ] 
    system/words/....: function [] [
        .cd ".." 
        .cd ".." 
        .cd ".." 
        return to-local-file what-dir
    ] 
    unless value? '.c [
        system/words/.c: function [] [
            .cd %/c/ 
            return to-local-file what-dir
        ]
    ] 
    unless value? 'c [c: :.c] unless value? '.d [
        system/words/.d: function [] [
            .cd %/d/ 
            return to-local-file what-dir
        ]
    ] 
    unless value? 'd [d: :.d] unless value? '.e [
        system/words/.e: function [] [
            .cd %/e/ 
            return to-local-file what-dir
        ]
    ] 
    unless value? 'e [e: :.e] unless value? '.f [
        system/words/.f: function [] [
            .cd %/f/ 
            return to-local-file what-dir
        ]
    ] 
    unless value? 'f [f: :.f] cd: :.cd
]

    

References

official documentation:

cd | rebol.com