zsh-users
 help / color / mirror / code / Atom feed
* Ignoring current directory with auto_cd
@ 2016-09-12 12:32 Jesper Nygårds
  2016-09-12 17:23 ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Jesper Nygårds @ 2016-09-12 12:32 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 322 bytes --]

I use AUTO_CD. I have also used this for a long time:

zstyle ':completion:*:cd:*' ignore-parents parent pwd

Is there a way to make auto_cd also ignore the current directory, in the
same way cd does with this zstyle set? I tried this without success:

zstyle ':completion::complete:-command-::' ignore-parents parent pwd

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Ignoring current directory with auto_cd
  2016-09-12 12:32 Ignoring current directory with auto_cd Jesper Nygårds
@ 2016-09-12 17:23 ` Bart Schaefer
  2016-09-12 19:43   ` Jesper Nygårds
  0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2016-09-12 17:23 UTC (permalink / raw)
  To: Zsh Users

On Sep 12,  2:32pm, Jesper Nygards wrote:
}
} Is there a way to make auto_cd also ignore the current directory, in the
} same way cd does with this zstyle set? I tried this without success:
} 
} zstyle ':completion::complete:-command-::' ignore-parents parent pwd

Styles of this sort only apply to completion (as might be obvious from
the word "completion" in the context) and only because there is a layer
of user-defined functions implementing most of the completion system.
There are no built-in shell operations that use zstyles values directly.

This might mostly do what you want:

    autoload -Uz add-zsh-hook

    no-local-autocd() {
      set -- "${(z)2}"
      [[ $1 = */* ]] || unsetopt autocd
    }

    reset-autocd {
      setopt autocd
    }

    add-zsh-hook preexec no-local-autocd
    add-zsh-hook precmd reset-autocd

I say "mostly" because if you do something like

    % echo foo ; /usr/bin

the autocd to /usr/bin won't happen because "echo" caused the option to
be turned off.

You can do a similar thing by overloading the accept-line ZLE widget, or
in more detail by using a DEBUG trap with the DEBUG_BEFORE_CMD option set,
but either of those has its own problems.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Ignoring current directory with auto_cd
  2016-09-12 17:23 ` Bart Schaefer
@ 2016-09-12 19:43   ` Jesper Nygårds
  2016-09-13  0:24     ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Jesper Nygårds @ 2016-09-12 19:43 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 2096 bytes --]

I am sorry, Bart. I realize now that my question was very badly worded.
I'll see if I can explain what I meant with an example:

Suppose I stand in a directory called "src". Since I have "zstyle
':completion:*:cd:*' ignore-parents parent pwd" set, if I type "cd
../s<tab>", I will not be offered "src" as a completion, since it gets
excluded by the zstyle setting.

However, if I only type "../s<tab>" (at the beginning of the command line),
"src" will be one of the alternatives offered by completion. I realize this
is because the completion system cannot know that it should exclude the
current directory.

So my question was: can I use something similar to the zstyle for cd
completion to always exclude the current directory in such a situation?


On Mon, Sep 12, 2016 at 7:23 PM, Bart Schaefer <schaefer@brasslantern.com>
wrote:

> On Sep 12,  2:32pm, Jesper Nygards wrote:
> }
> } Is there a way to make auto_cd also ignore the current directory, in the
> } same way cd does with this zstyle set? I tried this without success:
> }
> } zstyle ':completion::complete:-command-::' ignore-parents parent pwd
>
> Styles of this sort only apply to completion (as might be obvious from
> the word "completion" in the context) and only because there is a layer
> of user-defined functions implementing most of the completion system.
> There are no built-in shell operations that use zstyles values directly.
>
> This might mostly do what you want:
>
>     autoload -Uz add-zsh-hook
>
>     no-local-autocd() {
>       set -- "${(z)2}"
>       [[ $1 = */* ]] || unsetopt autocd
>     }
>
>     reset-autocd {
>       setopt autocd
>     }
>
>     add-zsh-hook preexec no-local-autocd
>     add-zsh-hook precmd reset-autocd
>
> I say "mostly" because if you do something like
>
>     % echo foo ; /usr/bin
>
> the autocd to /usr/bin won't happen because "echo" caused the option to
> be turned off.
>
> You can do a similar thing by overloading the accept-line ZLE widget, or
> in more detail by using a DEBUG trap with the DEBUG_BEFORE_CMD option set,
> but either of those has its own problems.
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Ignoring current directory with auto_cd
  2016-09-12 19:43   ` Jesper Nygårds
@ 2016-09-13  0:24     ` Bart Schaefer
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2016-09-13  0:24 UTC (permalink / raw)
  To: Zsh Users

On Sep 12,  9:43pm, Jesper Nygards wrote:
}
} [...] I have "zstyle ':completion:*:cd:*' ignore-parents parent pwd" set
} [...]
} However, if I only type "../s<tab>" (at the beginning of the command line),
} "src" will be one of the alternatives offered by completion. I realize this
} is because the completion system cannot know that it should exclude the
} current directory.

Actually it's not because the system doesn't know it should exclude the
current directory -- it's because the system doesn't know if you're
intending to type the name of a directory, or if you are intending to
type the path to a command file that happens to be inside some ../s*
directory.  In that word position, it has to complete both directory
names and command names, and the latter might include a relative path
as a prefix.

} So my question was: can I use something similar to the zstyle for cd
} completion to always exclude the current directory in such a situation?

If you never want to complete the path prefixes of non-absolute command
name paths, you can do this:

zstyle -e ':completion::complete:-command-::' tag-order \
    '[[ $words[CURRENT] = */* && $words[CURRENT] != /* ]] && reply=(
        commands executables builtins functions aliases
        suffix-aliases reserved-words jobs parameters -
	)'

This will complete all the usual things in command position except
that commands and executables are normally further subdivided by _files
into globbed-files, directories, and all-files, which the trailing "-"
will force to be excluded.

You can get fancier with the conditional expression, e.g., check for
$words[CURRENT]*/**/*(*) finding something, or some such.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-09-13  0:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-12 12:32 Ignoring current directory with auto_cd Jesper Nygårds
2016-09-12 17:23 ` Bart Schaefer
2016-09-12 19:43   ` Jesper Nygårds
2016-09-13  0:24     ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).