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

completion function for mpc



Hello!

I've written a completion function for mpc
(http://www.musicpd.org/mpc.shtml), which seems to be in pretty good
shape, therefor i'd like to get it included to the zsh distribution. If
there are still any problems or mistakes left, please tell me, i'll fix them.

-- 
Stephen Rüger
stephen.rueger@xxxxxxxxxxxxxxx
#compdef mpc

# Thanks:
# to the person that wrote _pids

# TODO:
# 1) check if there is some way of enabling predict-on on a case by case base
# 2) use _call_program instead of direct calls of mpc? need to find out if
#    that's useful

# contact: Stephen Rueger <stephen.rueger@xxxxxxxxxxxxxxx>
# last modified: Fri Feb  4 00:08:10 CET 2005

local OUT foo MPD_MUSIC_DIR MPC_PLAYLIST_MATCHER MPC_FORMAT_STRING

# set this style to whatever --format string you want to use when
# constructing the list of playlist entries
zstyle -s ':completion:${curcontext}:*' mpc-format-string MPC_FORMAT_STRING
foo=(--format "${(q)MPC_FORMAT_STRING:-%file%}")

# set this style to the music_directory of mpd to get _files based completion
# for commands like "add"
zstyle -s ':completion:${curcontext}:*' mpd-music-directory MPD_MUSIC_DIR

# matcher used for playlist completion
zstyle -s ':completion:${curcontext}:*' mpd-playlist-matcher \
  MPC_PLAYLIST_MATCHER
: ${MPC_PLAYLIST_MATCHER:='m:{a-z}={A-Z} l:|=**'}

# this one is used to configure the behaviour of _mpc_helper_songnumbers,
# see _pids for the original
zstyle -s ":completion:${curcontext}:song-numbers" insert-song-numbers \
  OUT || OUT=single

_mpc_command() {
  local mpc_cmds

  mpc_cmds=(add:"Add a song to the current playlist"
    del:"Remove a song from the current playlist"
    play:"Start playing at <number>"
    next:"Play the next song in the current playlist"
    prev:"Play the previous song in the current playlist"
    pause:"Pauses the currently playing song"
    stop:"Stop the currently playing playlists"
    seek:"Seeks to the position specified in percent"
    clear:"Clear the current playlist"
    shuffle:"Shuffle the current playlist"
    move:"Move song in playlist"
    playlist:"Print the current playlist"
    listall:"List all songs in the music dir"
    ls:"List the contents of <dir>"
    lsplaylists:"Lists currently available playlists"
    load:"Load <file> as a playlist"
    save:"Saves a playlist as <file>"
    rm:"Removes a playlist"
    volume:"Sets volume to <num> or adjusts by [+-]<num>"
    repeat:"Toggle repeat mode, or specify state"
    random:"Toggle random mode, or specify state"
    search:"Search for a song"
    crossfade:"Set and display crossfade settings"
    update:"Scans music directory for updates"
    version:"Reports version of MPD")

  if (( CURRENT == 1 )); then
    _describe -t command "mpc commands" mpc_cmds
  else
    local curcontext="$curcontext"
  fi

  local cmd=$words[1]

  local curcontext="${curcontext%:*}:mpc-${cmd}"
  _call_function ret _mpc_$cmd
}

########################################################################
# helper functions

_mpc_helper_bool() {
  local expl states
  states=(on off yes no 1 0 true false)
  _wanted states expl boolean compadd $expl -a states
}

(( $+functions[_mpc_helper_songnumbers] )) ||
_mpc_helper_songnumbers() {
  # shamelessly stolen from _pids

  local out sn list expl MATCH desc all NM ret=1

  _tags song-numbers || return 1

  if [[ "$PREFIX" = [0-9]# ]]; then
    all=()
    MATCH="*${(Q)PREFIX}[0-9]#*"
  else
    all=(-U)
    MATCH="(#i)*${(Q)PREFIX}*"
    NM="$compstate[nmatches]"
  fi

  out=("${(@f)$(_call_program song-numbers mpc $foo playlist 2>/dev/null)}")
  out=("${(@M)out[@]:#${~MATCH}}")

  sn=("${(@)${(@M)out}//(#b)(#s)\#([0-9]#)*/$match[1]}")
  list=("${(@Mr:COLUMNS-1:)out}")

  _wanted -V 'song numbers' expl 'song number' \
    compadd "$@" -ld list "$all[@]" -a sn && ret=0

  if [[ -n "$all" ]]; then
    case "$OUT" in
      menu)
        compstate[insert]=menu
        ;;
      single)
        [[ $compstate[nmatches] -ne NM+1 ]] &&
        compstate[insert]=
        ;;
      *)
        [[ ${#:-$PREFIX} -gt ${#compstate[unambiguous]} ]] &&
        compstate[insert]=menu
        ;;
    esac
  fi

  return ret
}

(( $+functions[_mpc_helper_playlists] )) ||
_mpc_helper_playlists() {
  local list expl
  list=($(mpc lsplaylists))
  _wanted list expl playlist compadd -M $MPC_PLAYLIST_MATCHER $expl -a list
}

(( $+functions[_mpc_helper_files] )) ||
_mpc_helper_files() {
  if [[ -n $MPD_MUSIC_DIR ]]; then
    _files -W $MPD_MUSIC_DIR
    return
  fi

  local -U list expl
  if [[ $words[CURRENT] != */* ]]; then
    list=( ${${(f)"$(mpc listall)"}%%/*})
    _wanted files expl files compadd -qS/ -a list
  else
    list=(${(f)"$(mpc tab $words[CURRENT])"})
    _wanted files expl 'files' _multi_parts / list
  fi
}

(( $+functions[_mpc_helper_directories] )) ||
_mpc_helper_directories() {
  if [[ -n $MPD_MUSIC_DIR ]]; then
    _files -/ -W $MPD_MUSIC_DIR
    return
  fi

  local -U list expl
  if [[ $words[CURRENT] != */* ]]; then
    list=( ${${(M)${(f)"$(mpc listall)"}:#*/*}%%/*})
    _wanted directories expl directories compadd -qS/ -a list
  else
    list=(${(f)"$(mpc lstab $words[CURRENT])"})
    _wanted directories expl directories _multi_parts / list
  fi
}

########################################################################

_mpc_add() {
  _mpc_helper_files
}

_mpc_del() {
  _mpc_helper_songnumbers
}

_mpc_play() {
  _mpc_helper_songnumbers
}

_mpc_seek() {
  _message "floating point percent value"
}

_mpc_move() {
  if [[ $#words -le 3 ]]; then
    _mpc_helper_songnumbers
  else
    _message "nothing"
  fi
}

_mpc_listall() {
  _mpc_helper_files
}

_mpc_ls() {
  _mpc_helper_directories
}

_mpc_load() {
  _mpc_helper_playlists
}

_mpc_save() {
  _mpc_helper_playlists
}

_mpc_rm() {
  _mpc_helper_playlists
}

_mpc_volume() {
  local expl
  compset -P '[-+]'
  _wanted list expl volume compadd $expl - {0..100}
}

_mpc_repeat() {
  _mpc_helper_bool
}

_mpc_random() {
  _mpc_helper_bool
}

_mpc_search() {
  local list expl
  list=(album artist title filename)

  if [[ $#words -eq 2 ]]; then
    _wanted list expl table compadd $expl -a list
  else
    _message "pattern"
  fi
}

_mpc_update() {
  _mpc_helper_files
}

_arguments \
  '--format:format string:' \
  '*::mpc command:_mpc_command'

#Local Variables:
#mode: shell-script
#End:


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