##@cooked comments # -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
## Fonctions pour gérer les modelines dans les fichiers
##@cooked nocomments
##@require base
uprovide modeline
urequire base

MODELINE="@prefix@-*- coding: utf-8 @mode@-*- vim:sw=@tabsize@:sts=@tabsize@:et:ai:si:sta:fenc=utf-8"

# modèles de modes à copier dans les fonctions __ml_* ci-dessous
#CSS=.css|css
#JS=.js|js|javascript
#JAVA=.java|java
#HTML=.html|.htm|html|html4|html5
#XML=.xml|xml
#PHP=.php|.phps|php|php4|php5
#SH=.sh|sh|shell
#PYTHON=.py|.pyw|py|python
#TEXT=.txt|txt|text
#CONF=.conf|conf
#MARKDOWN=.mdt|.mdwn|.md|.text|.twp|markdown
#PUML=.puml|puml|plantuml
#IUML=.iuml|iuml|plantumlinc
#SQL=.sql|sql

function __ml_getprefix() {
    case "$1" in
    .css|css|.js|js|javascript|.java|java) echo "/* ";;
    .html|.htm|html|html4|html5) echo "<!-- ";;
    .php|.phps|php|php4|php5) echo "<?php # ";;
    .sh|sh|shell|.py|.pyw|py|python|.txt|txt|text|.conf|conf) echo "# ";;
    .mdt|.mdwn|.md|.text|.twp|markdown|.puml|puml|plantuml) echo "# ";;
    .iuml|iuml|plantumlinc) echo "'' ";;
    .sql|sql) echo "-- ";;
    *)  # par défaut, utiliser le préfixe '# ', même si ce n'est pas forcément
        # approprié
        echo "# "
        ;;
    esac
}
function __ml_getmode() {
    case "$1" in
    .html|.htm|html|html4|html5) echo html;;
    .php|.phps|php|php5) echo php;;
    .sh|sh|shell) echo sh;;
    .py|.pyw|py|python) echo python;;
    .txt|txt|text) echo text;;
    .conf|conf) echo conf;;
    .mdt|.mdwn|.md|.text|.twp|markdown) echo markdown;;
    .puml|puml|plantuml|.iuml|iuml|plantumlinc) echo text;;
    .sql|sql) echo sql;;
    esac
}
function __ml_gettabsize() {
    case "$1" in
    .php|.phps|php|php5) echo 2;;
    *) echo 4;;
    esac
}
function __ml_getmodeline() {
    local prefix="$(__ml_getprefix "$1")"
    local mode="$(__ml_getmode "$1")"
    local tabsize="$(__ml_gettabsize "$1")"
    [ -n "$mode" ] && mode="mode: $mode "
    local modeline="$MODELINE"
    modeline="${modeline//@prefix@/$prefix}"
    modeline="${modeline//@mode@/$mode}"
    modeline="${modeline//@tabsize@/$tabsize}"
    echo "$modeline"
}
function __ml_getsuffix() {
    case "$1" in
    .css|css|.js|js|javascript|.java|java) echo "*/";;
    .html|.htm|html|html4|html5) echo "-->";;
    .php|.phps|php|php4|php5) echo "?>";;
    esac
}

function printml() {
    local atype=auto
    parse_opts "${PRETTYOPTS[@]}" \
        --help '$exit_with display_help' \
        -t:,--type: atype= \
        @ args -- "$@" && set -- "${args[@]}" || die "$args"

    local mode modeline suffix type
    if [ "$atype" == "auto" ]; then
        local basename ext
        splitname "$(basename "$1")" basename ext
        type="${ext:+.$ext}"
        type="${type:-shell}"
    else
        type="$atype"
    fi
    modeline="$(__ml_getmodeline "$type")"
    suffix="$(__ml_getsuffix "$type")"

    echo "$modeline${suffix:+
$suffix}"
}

function addml() {
    local atype=auto
    local edit=auto
    parse_opts "${PRETTYOPTS[@]}" \
        --help '$exit_with display_help' \
        -t:,--type: atype= \
        -e,--edit edit=1 \
        -n,--noedit edit= \
        @ args -- "$@" && set -- "${args[@]}" || die "$args"

    if [ "$edit" == "auto" ]; then
        [ $# -eq 1 ] && edit=1 || edit=
    fi

    local mode modeline suffix type
    local -a edits
    for file in "$@"; do
        if [ "$atype" == "auto" ]; then
            local basename ext
            splitname "$(basename "$file")" basename ext
            type="${ext:+.$ext}"
            type="${type:-shell}"
        else
            type="$atype"
        fi
        mode="$(__ml_getmode "$type")"
        modeline="$(__ml_getmodeline "$type")"
        suffix="$(__ml_getsuffix "$type")"

        if [ ! -e "$file" ]; then
            ask_yesno "Le fichier $file n'existe pas. Faut-il le créer?" O || continue
            touch "$file"
        fi
        array_add edits "$file"
        doinplacef "$file" awkrun mode="$mode" modeline="$modeline" suffix="$suffix" '
BEGIN { run = 1; done = 0; nextline = 0 }
run == 1 {
  if (substr($0, length($0) - length(modeline) + 1) == modeline) {
    run = 0; done = 1; nextline = 0
  } else if (nextline) {
    print modeline
    if (suffix != "") print suffix
    run = 0; done = 1; nextline = 0
  } else if (mode == "shell" && $0 ~ /^#!/) {
    nextline = 1
  } else if (mode == "shell" && $0 ~ /^#/) {
    print modeline
    if (suffix != "") print suffix
    done = 1; run = 0
  } else if (!done) {
    print modeline
    if (suffix != "") print suffix
    done = 1; run = 0
  } else {
    run = 0
  }
}
{ print }
END {
  if (run || nextline) {
    print modeline
    if (suffix != "") print suffix
  }    
}
'
    done
    [ -n "$edit" ] && "${EDITOR:-vi}" "${edits[@]}"
}
