'Return a dictionary of emacs-style local variables in the head.
"Head" emacs vars on the ones in the \'-*- ... -*-\' one-liner.
Parsing is done loosely according to this spec (and according to
some in-practice deviations from this):
http://www.gnu.org/software/emacs/manual/html_node/emacs/Specifying-File-Variables.html#Specifying-File-Variables'

def _get_emacs_head_vars(self, head_bytes):

    if (self._emacs_head_vars_cache is not None):
        return self._emacs_head_vars_cache
    emacs_vars = {}
    if ('-*-' in head_bytes):
        match = self._emacs_vars_head_pat.search(head_bytes)
        if match:
            emacs_vars_str = match.group(1)
            if ('\n' in emacs_vars_str):
                raise ValueError('local    variables    error:    -*-    not    terminated    before    end    of    line')
            emacs_var_strs = [s.strip() for s in emacs_vars_str.split(';') if s.strip()]
            if ((len(emacs_var_strs) == 1) and (':' not in emacs_var_strs[0])):
                emacs_vars['mode'] = emacs_var_strs[0].strip()
            else:
                for emacs_var_str in emacs_var_strs:
                    try:
                        (variable, value) = emacs_var_str.strip().split(':', 1)
                    except ValueError:
                        log.debug('emacs    variables    error:    malformed    -*-    line:    %r', emacs_var_str)
                        continue
                    emacs_vars[variable.lower()] = value.strip()
    for (var, val) in emacs_vars.items():
        if ((len(val) > 1) and ((val.startswith('"') and val.endswith('"')) or (val.startswith('"') and val.endswith('"')))):
            emacs_vars[var] = val[1:(-1)]
    self._emacs_head_vars_cache = emacs_vars
    return emacs_vars
