정렬이 엉망으로 된 소스코드 어떻게 정리해야하나요?(indent)

chocokeki의 이미지

가끔 define 코드 같은걸 보면 space랑 tab이 섞여서 엉망으로 알아보기 힘든 코드들이 있는데요
이런경우 어떻게 재정렬 시킬 수 있을까요?

#define code0                      0
#define code1      1
#define code2          2
....

요렇게 되어있을 때 다시 이쁘게 재정렬하는 방법을 찾습니다!

vim에서 gg=G 로도 시도해보고 notepad++에서 TextFX플러그인으로 reindnet 를 시켜봐도 이런건 정렬이 안되네요

ymir의 이미지

저는 주로 indent 뒤에 sed 로 필터 걸어서 정리합니다.
아래는 제가 .bash_aliases 에 등록해 놓고 쓰는 건데, 맨 아래쪽에 sed 부분 참고해 보세요.

alias xindent='indent -st -bad -bap -bli0 -cli4 -i4 -il0 -l179 -nbc -ncs -npcs -npsl -fca -lc179 -fc1 -ts4 -T bool -T uint8_t -T uint16_t -T uint32_t -T uint64_t -T size_t -T ssize_t'
 
function lstypedef()
{
    [ -n "$1" ] && ctags -f - $@ | egrep -i 'typedef|typeref:struct' \
        | while read -r line; do typedef=$(cut -d' ' -f1 <<<$line); grep -q "struct $typedef" <<<$line || echo $typedef; done | sort -u
}
 
function indent_opt_typedef()
{
    if [ -n "$1" ] ; then
        declare -a typedef_list=($(lstypedef $@))
        [ ${#typedef_list[@]} -ne 0 ] && echo ${typedef_list[@]/#/-T }
    fi
}
 
function indent_dir()
{
    if [ -z "$1" ] ; then
        echo "Usage : $0 src_dir [dst_dir]"
        return 1;
    fi
 
    SRC_DIR=${1%/}
    DST_DIR=${2-${SRC_DIR}_indented}
 
    TMPLIST=/tmp/indent.$$
    find $SRC_DIR -type f -name \*.c -o -name \*.h > $TMPLIST
 
    # create directory tree
    cat $TMPLIST | xargs dirname | sort -u | while read -r dir
    do
        [ ! -d ${dir/$SRC_DIR/$DST_DIR} ] && /bin/mkdir -p ${dir/$SRC_DIR/$DST_DIR} >/dev/null
    done
 
    # get typedef list
    opt_typedefs=$(indent_opt_typedef $(cat $TMPLIST))
 
    # run indent
    cat $TMPLIST | while read -r file
    do
        xindent $opt_typedefs $file | sed -r -e 's/^#(define|ifdef|if)[[:space:]]+/#\1 /g' \
            -e 's/^#(else|endif)[[:space:]]+\//#\1\t\//g' \
            -e 's/^#(define [^[:space:]]+)[[:space:]]+/#\1\t/g' > ${file/$SRC_DIR/$DST_DIR}
    done
    /bin/rm -f $TMPLIST
}

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

tyhan의 이미지

리눅스 커널소스의
scripts/Lindent 를 참조해 보세요.

tyhan의 이미지

내용을 처음 보았는데..
indent라는 명령어를 이용하고 있네요.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.