Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Feature request: set extension for =( ) created file



On Sep 24,  3:55pm, Lawrence Velazquez wrote:
}
} > Extension-aware =( ) would be a masterpiece feature for Zsh, though,
} > IMO.
} 
} Unless it were implemented using libmagic or something, I would consider
} it a massive waste of time and effort.

Hence my suggestion of a wrapper function that calls "file".

You can generalize my example to have a mapping from "file" output strings
to file extensions, and then do something like this:

appendfileext() {
  local filetype="$(file $1)"
  if [[ -n "${filextcache[$filetype]} ]]; then
    REPLY="$1.${filextcache[$filetype]}"
  else
    case "$filetype" in 
    (...) : you fill this in;;
    (*) unset REPLY;;
    esac
  fi
}

fixfiletype() {
  local REPLY
  integer i
  declare -a removes
  { 
    for ((i=1; i <= $#; ++i)); do
      if [[ "${argv[i]}" = "$TMPPREFIX"* ]]; then
        appendfileext "${argv[i]}"
        if [[ -n "$REPLY" ]];then
          removes+="$REPLY"
          ln "${argv[i]}" "$REPLY"
          argv[i]="$REPLY"
        fi
      fi
    done
    "${argv[@]}"
  } always {
    [[ -n "$removes" ]] && /bin/rm -f "${removes[@]}"
  }
}

alias vim='fixfiletype vim '



Messages sorted by: Reverse Date, Date, Thread, Author