zsh-users
 help / color / mirror / code / Atom feed
* conditionally match part of file name to most recently modified file
@ 2007-06-17 21:16 Eric Smith
  2007-06-18 10:09 ` Peter Stephenson
  0 siblings, 1 reply; 9+ messages in thread
From: Eric Smith @ 2007-06-17 21:16 UTC (permalink / raw)
  To: zsh users

Zshers,

I want to be able to type
"vim foo"
in order to edit the last file whose name contains the string
"foo".

I guess this might be achieved with compdef and compadd 
with the *(om[1]).  

The function would only do all this when
the condition of a complete filename is not met.

thanks

-- 
Eric Smith


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

* Re: conditionally match part of file name to most recently modified file
  2007-06-17 21:16 conditionally match part of file name to most recently modified file Eric Smith
@ 2007-06-18 10:09 ` Peter Stephenson
  2007-06-18 13:41   ` Vincent Lefevre
  2007-06-18 21:46   ` conditionally match part of file name to most recently modified file - take 2 Eric Smith
  0 siblings, 2 replies; 9+ messages in thread
From: Peter Stephenson @ 2007-06-18 10:09 UTC (permalink / raw)
  To: Zsh Users

On Sun, 17 Jun 2007 23:16:44 +0200
Eric Smith <es@fruitcom.com> wrote:
> I want to be able to type
> "vim foo"
> in order to edit the last file whose name contains the string
> "foo".
> 
> I guess this might be achieved with compdef and compadd 
> with the *(om[1]).  

You could do it as a completion function, although the simple case
you've asked for can be done easily with an ordinary shell function:

vim() {
  # assume the file is the last argument
  if [[ ! -f $argv[-1] ]]; then
    argv[-1]=(*$argv[-1]*(om[1]))
  fi
  command vim "$@"
}

You can complete the most recent matching file using a generic completer:

  zstyle ':completion:most-recent-file:*' match-original both
  zstyle ':completion:most-recent-file::::' completer _menu _files _match
  zstyle ':completion:most-recent-file:*' file-sort modification
  zstyle ':completion:most-recent-file:*' file-patterns '*:all\ files'
  zstyle ':completion:most-recent-file:*' hidden all
  zle -C most-recent-file menu-complete _generic
  bindkey '^X.' most-recent-file

This is different from what you asked for in that you have to type a "*" at
the start yourself if you want to match a substring.

I had a look at writing a completion function that did exactly what you
wanted; must of it is simple but I got hopelessly stuck trying to persuade
the completion system not to sort the matches (the equivalent of the
file-sort style above).  That ought to be fixable.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php

To get further information regarding CSR, please visit our Investor Relations page at http://ir.csr.com/csr/about/overview


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

* Re: conditionally match part of file name to most recently modified file
  2007-06-18 10:09 ` Peter Stephenson
@ 2007-06-18 13:41   ` Vincent Lefevre
  2007-06-18 13:55     ` Peter Stephenson
  2007-06-18 21:46   ` conditionally match part of file name to most recently modified file - take 2 Eric Smith
  1 sibling, 1 reply; 9+ messages in thread
From: Vincent Lefevre @ 2007-06-18 13:41 UTC (permalink / raw)
  To: Zsh Users

On 2007-06-18 11:09:48 +0100, Peter Stephenson wrote:
> You can complete the most recent matching file using a generic completer:
> 
>   zstyle ':completion:most-recent-file:*' match-original both
>   zstyle ':completion:most-recent-file::::' completer _menu _files _match
>   zstyle ':completion:most-recent-file:*' file-sort modification
>   zstyle ':completion:most-recent-file:*' file-patterns '*:all\ files'
                                                             ^^^^^^^^^^
Is that all\ files or all-files (used in the manual)?

>   zstyle ':completion:most-recent-file:*' hidden all
>   zle -C most-recent-file menu-complete _generic
>   bindkey '^X.' most-recent-file

I like that. But the 4th line should probably be:

zstyle ':completion:most-recent-file:*' file-patterns '*(^/):all\ files'

to avoid completing on directories (which can often get recent).

For symbolic links, how can one take into account the target instead
of the link itself?

-- 
Vincent Lefèvre <vincent@vinc17.org> - Web: <http://www.vinc17.org/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
Work: CR INRIA - computer arithmetic / Arenaire project (LIP, ENS-Lyon)


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

* Re: conditionally match part of file name to most recently modified file
  2007-06-18 13:41   ` Vincent Lefevre
@ 2007-06-18 13:55     ` Peter Stephenson
  2007-06-18 14:48       ` Vincent Lefevre
  2007-06-18 17:44       ` Phil Pennock
  0 siblings, 2 replies; 9+ messages in thread
From: Peter Stephenson @ 2007-06-18 13:55 UTC (permalink / raw)
  To: Zsh Users

Vincent Lefevre wrote:
> >   zstyle ':completion:most-recent-file:*' file-patterns '*:all\ files'
>                                                              ^^^^^^^^^^
> Is that all\ files or all-files (used in the manual)?

It's a tag, so it should be all-files.

> >   zstyle ':completion:most-recent-file:*' hidden all
> >   zle -C most-recent-file menu-complete _generic
> >   bindkey '^X.' most-recent-file
> 
> I like that. But the 4th line should probably be:
> 
> zstyle ':completion:most-recent-file:*' file-patterns '*(^/):all\ files'
> 
> to avoid completing on directories (which can often get recent).

That's possible.  You can restrict it on the command line, too; I
sometimes type "*(.)" and complete that.

> For symbolic links, how can one take into account the target instead
> of the link itself?

"*(-^/)" or "*(-/)" should do the trick.  I've had funny results with
non-existent targets when doing this on the command line.

I've wondered for quite a long time why that's not the default since the
times when you want to identify symbolic links themselves are limited.  I
wonder if it's time for another option?  What would it do, make it so
that both (/) and (-/) followed symbolic links but (--/) didn't?

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php

To get further information regarding CSR, please visit our Investor Relations page at http://ir.csr.com/csr/about/overview


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

* Re: conditionally match part of file name to most recently modified file
  2007-06-18 13:55     ` Peter Stephenson
@ 2007-06-18 14:48       ` Vincent Lefevre
  2007-06-18 17:38         ` Peter Stephenson
  2007-06-18 17:44       ` Phil Pennock
  1 sibling, 1 reply; 9+ messages in thread
From: Vincent Lefevre @ 2007-06-18 14:48 UTC (permalink / raw)
  To: Zsh Users

On 2007-06-18 14:55:45 +0100, Peter Stephenson wrote:
> "*(-^/)" or "*(-/)" should do the trick.  I've had funny results with
> non-existent targets when doing this on the command line.

OK. Another problem: if I type

% ls a[TAB]

I get a list of different possible files. Then I type '^X.', but
instead of getting the most recent file, I get the first item of
the list above. Is there a way to make '^X.' reset the above list
(while still being able to type ^X. several times and get different
results)?

-- 
Vincent Lefèvre <vincent@vinc17.org> - Web: <http://www.vinc17.org/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
Work: CR INRIA - computer arithmetic / Arenaire project (LIP, ENS-Lyon)


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

* Re: conditionally match part of file name to most recently modified file
  2007-06-18 14:48       ` Vincent Lefevre
@ 2007-06-18 17:38         ` Peter Stephenson
  0 siblings, 0 replies; 9+ messages in thread
From: Peter Stephenson @ 2007-06-18 17:38 UTC (permalink / raw)
  To: Zsh Users

On Mon, 18 Jun 2007 16:48:20 +0200
Vincent Lefevre <vincent@vinc17.org> wrote:
> OK. Another problem: if I type
> 
> % ls a[TAB]
> 
> I get a list of different possible files. Then I type '^X.', but
> instead of getting the most recent file, I get the first item of
> the list above. Is there a way to make '^X.' reset the above list
> (while still being able to type ^X. several times and get different
> results)?

There doesn't appear to be.  First impressions (using a (further) enhanced
_complete_debug setup for _generic which I'll post when the interface is a
bit smoother) suggest this is for some quite deep reason---I can see the
file matcher correctly getting the file-sort style the second time, so my
best guess is that in the core code it's spotting that you have the same
list of matches and not using the newly sorted form.  However, I'm not sure
about that.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php

To get further information regarding CSR, please visit our Investor Relations page at http://ir.csr.com/csr/about/overview


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

* Re: conditionally match part of file name to most recently modified file
  2007-06-18 13:55     ` Peter Stephenson
  2007-06-18 14:48       ` Vincent Lefevre
@ 2007-06-18 17:44       ` Phil Pennock
  1 sibling, 0 replies; 9+ messages in thread
From: Phil Pennock @ 2007-06-18 17:44 UTC (permalink / raw)
  To: Zsh Users

On 2007-06-18 at 14:55 +0100, Peter Stephenson wrote:
> I've wondered for quite a long time why that's not the default since the
> times when you want to identify symbolic links themselves are limited.  I
> wonder if it's time for another option?  What would it do, make it so
> that both (/) and (-/) followed symbolic links but (--/) didn't?

In combination with recursive globs looking at stuff, wanting to look at
a user's files, not whatever they might be linking to?

Ie, I might use ***/*(-.) in some places, but **/*(.) is the safer mode,
especially when invoked as root.

Changing the default behaviour would be just too clever and likely to
have unintended security repercussions so if it's a new option, please
make it default to off?


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

* Re: conditionally match part of file name to most recently modified file - take 2
  2007-06-18 10:09 ` Peter Stephenson
  2007-06-18 13:41   ` Vincent Lefevre
@ 2007-06-18 21:46   ` Eric Smith
  2007-06-19  0:18     ` Bart Schaefer
  1 sibling, 1 reply; 9+ messages in thread
From: Eric Smith @ 2007-06-18 21:46 UTC (permalink / raw)
  To: Zsh Users

Thanks Peter

Let me increase verbosity to describe the desired functionality.

I would like to have certain commands automatically run this function.
This I currently do with the following:
#compdef vim less sed perl ooffice ...

<function_definition>

Then I would like to type
"ooffice foo"

And the function would execute:
ooffice foo

if file foo existed, otherwise it would execute:

ooffice *foo*(om[1])

I would like this to be as efficient as possible.

Eric

> On Sun, 17 Jun 2007 23:16:44 +0200
> > I want to be able to type
> > "vim foo"
> > in order to edit the last file whose name contains the string
> > "foo".
...


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

* Re: conditionally match part of file name to most recently modified file - take 2
  2007-06-18 21:46   ` conditionally match part of file name to most recently modified file - take 2 Eric Smith
@ 2007-06-19  0:18     ` Bart Schaefer
  0 siblings, 0 replies; 9+ messages in thread
From: Bart Schaefer @ 2007-06-19  0:18 UTC (permalink / raw)
  To: Zsh Users

On Jun 18, 11:46pm, Eric Smith wrote:
}
} I would like to have certain commands automatically run this function.
} This I currently do with the following:
} #compdef vim less sed perl ooffice ...

Well, *that* you don't do with the #compdef.  What you do with the
compdef is arrange that a certain completer is run for all of those
functions when you press tab, which is not what you asked for.  Which
do you actually want?

} Then I would like to type
} "ooffice foo"
} 
} And the function would execute:
} ooffice foo

You can define multiple functions at once:

    setopt function_argzero
    function vim less sed perl ooffice ... {
      set -- $1 ${${1:h}:-.}/*${1:t}*(Nom[1]) $1
      [[ -f $1 ]] || shift
      command $0 $1
    }

Tweaks, such as not doing this when there's more than one argument,
are left as an excercise.


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

end of thread, other threads:[~2007-06-19  0:18 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-06-17 21:16 conditionally match part of file name to most recently modified file Eric Smith
2007-06-18 10:09 ` Peter Stephenson
2007-06-18 13:41   ` Vincent Lefevre
2007-06-18 13:55     ` Peter Stephenson
2007-06-18 14:48       ` Vincent Lefevre
2007-06-18 17:38         ` Peter Stephenson
2007-06-18 17:44       ` Phil Pennock
2007-06-18 21:46   ` conditionally match part of file name to most recently modified file - take 2 Eric Smith
2007-06-19  0:18     ` 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).