zsh-users
 help / color / mirror / code / Atom feed
* Converting relative paths to full
@ 2021-08-10 19:53 Gürkan
  2021-08-10 20:44 ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Gürkan @ 2021-08-10 19:53 UTC (permalink / raw)
  To: zsh-users

Hello,

I'm trying to create a function to catch relative paths of
files/directories and converting those to full paths before sending
into history. Will hook it to "zshaddhistory", similar to this [0].

Looks like it's going to be a bit painful. Are there any pre-existing
function/variable or any example / shortcut you could point?

Thanks for your time,
Gürkan

[0]: https://github.com/phyber/zsh.d/blob/a595029597189c99bfb053383f4273d4a74e1688/hooks/zshaddhistory.zsh


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

* Re: Converting relative paths to full
  2021-08-10 19:53 Converting relative paths to full Gürkan
@ 2021-08-10 20:44 ` Bart Schaefer
  2021-08-10 21:39   ` Gürkan
  0 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2021-08-10 20:44 UTC (permalink / raw)
  To: Gürkan; +Cc: Zsh Users

On Tue, Aug 10, 2021 at 12:53 PM Gürkan <seqizz@gmail.com> wrote:
>
> I'm trying to create a function to catch relative paths of
> files/directories and converting those to full paths

The hard part is identifying what argument strings are meant to be
interpreted as file names.

Assuming you've done that, you can convert to an absolute path pretty
easily.  Suppose for example that you know $3 is a file name.

   3=${3:P}

See also :a and :A for slightly different interpretations of a fully
elucidated file path.

In cases where you have an argument like "--file=name" you can do:

  3=${3%=*}=${${3#*=}:P}

If it's just "-fname" and you want a result like "-f$PWD/name" you're
on your own ... there's no generic way to know which commands have
that sort of argument format or which option letters introduce it.


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

* Re: Converting relative paths to full
  2021-08-10 20:44 ` Bart Schaefer
@ 2021-08-10 21:39   ` Gürkan
  2021-08-10 21:53     ` Gürkan
  0 siblings, 1 reply; 6+ messages in thread
From: Gürkan @ 2021-08-10 21:39 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

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


On 10/08/2021 22.44, Bart Schaefer wrote:
> The hard part is identifying what argument strings are meant to be
> interpreted as file names.
>
> Assuming you've done that, you can convert to an absolute path pretty
> easily.  Suppose for example that you know $3 is a file name.
>
>     3=${3:P}
That was a nice tip, thanks!
> In cases where you have an argument like "--file=name"

I actually don't, what I am looking for is just separated paths.

Did something like this with external tools like sed, looks like working:

	function _edit_command_to_register_full_path {
		emulate -L zsh

		REPLACED="$1"
		for element in `echo "$1"`; do
			# Don't care about parameters
			if [[ "$element" == -* ]]; then
				continue
			fi
			FULLPATH="${element:P}"
			if [[ -f $FULLPATH ]] || [[ -d $FULLPATH ]]; then
				REPLACED=`echo "$REPLACED" | sed "s!$element!$FULLPATH!"`
			fi
		done
	  print -sr -- ${REPLACED%%$'\n'}
	}

	add-zsh-hook zshaddhistory _edit_command_to_register_full_path


But having a duplication problem. The original is still applied to history.
Is there a way to just modify the string which is going to be accepted (thus respecting HIST* options),
or should I give up this method and just use preexec_functions to modify the BUFFER directly?


[-- Attachment #2: Type: text/html, Size: 1859 bytes --]

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

* Re: Converting relative paths to full
  2021-08-10 21:39   ` Gürkan
@ 2021-08-10 21:53     ` Gürkan
  2021-08-10 22:36       ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Gürkan @ 2021-08-10 21:53 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

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


On 10/08/2021 23.39, Gürkan wrote:
> But having a duplication problem. The original is still applied to history.
> Is there a way to just modify the string which is going to be accepted (thus respecting HIST* options),
> or should I give up this method and just use preexec_functions to modify the BUFFER directly?

Oh nevermind, I've found an old answer related to it[0]:

Now it works good enough:

	function _edit_command_to_register_full_path {
		emulate -L zsh

		REPLACED="$1"
		for element in `echo "$1"`; do
			# Don't care about parameters
			if [[ "$element" == -* ]]; then
				continue
			fi
			FULLPATH="${element:P}"
			if [[ -f $FULLPATH ]] || [[ -d $FULLPATH ]]; then
				REPLACED=`echo "$REPLACED" | sed "s!$element!$FULLPATH!"`
			fi
		done
	  print -Sr -- ${REPLACED%%$'\n'}
	  return 1
	}

	add-zsh-hook zshaddhistory _edit_command_to_register_full_path

Also needed to change -s to -S.

Thanks again.

[0]: https://www.zsh.org/mla/users/2011/msg00754.html

[-- Attachment #2: Type: text/html, Size: 1525 bytes --]

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

* Re: Converting relative paths to full
  2021-08-10 21:53     ` Gürkan
@ 2021-08-10 22:36       ` Bart Schaefer
  2021-08-11  6:24         ` Gürkan
  0 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2021-08-10 22:36 UTC (permalink / raw)
  To: Gürkan; +Cc: Zsh Users

On Tue, Aug 10, 2021 at 2:53 PM Gürkan <seqizz@gmail.com> wrote:
>
>
> function _edit_command_to_register_full_path {
> emulate -L zsh
>
> REPLACED="$1"
> for element in `echo "$1"`; do
> # Don't care about parameters
> if [[ "$element" == -* ]]; then
> continue
> fi
> FULLPATH="${element:P}"
> if [[ -f $FULLPATH ]] || [[ -d $FULLPATH ]]; then
> REPLACED=`echo "$REPLACED" | sed "s!$element!$FULLPATH!"`
> fi
> done
>  print -Sr -- ${REPLACED%%$'\n'}
>  return 1
> }

That's far more complicated than necessary.

function _edit_command_to_register_full_path {
  emulate -L zsh
  for element in ${(z)1}; do
    # Don't care about parameters
    [[ $element == -* ]] && continue
    FULLPATH="${element:P}"
    # Don't replace when already the full path
    [[ $element == $FULLPATH ]] && continue
    # Don't replace when path doesn't exist
    [[ -f $FULLPATH || -d $FULLPATH ]] || continue
    # [[:space:]] to protect previous replacements
    # (this is still not foolproof for path prefixes)
    set -- "${1/[[:space:]]$element/ $FULLPATH}"
  done
  print -Sr -- ${1%%$'\n'}
  return 1
}


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

* Re: Converting relative paths to full
  2021-08-10 22:36       ` Bart Schaefer
@ 2021-08-11  6:24         ` Gürkan
  0 siblings, 0 replies; 6+ messages in thread
From: Gürkan @ 2021-08-11  6:24 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users


On 11/08/2021 00.36, Bart Schaefer wrote:
> That's far more complicated than necessary.
>
> function _edit_command_to_register_full_path {
>    emulate -L zsh
>    for element in ${(z)1}; do
>      # Don't care about parameters
>      [[ $element == -* ]] && continue
>      FULLPATH="${element:P}"
>      # Don't replace when already the full path
>      [[ $element == $FULLPATH ]] && continue
>      # Don't replace when path doesn't exist
>      [[ -f $FULLPATH || -d $FULLPATH ]] || continue
>      # [[:space:]] to protect previous replacements
>      # (this is still not foolproof for path prefixes)
>      set -- "${1/[[:space:]]$element/ $FULLPATH}"
>    done
>    print -Sr -- ${1%%$'\n'}
>    return 1
> }

Thank you, that looks indeed much better.

I am still potato on shell builtins topic, will read and try to shorten 
my 400-line rc file :)



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

end of thread, other threads:[~2021-08-11  6:25 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-10 19:53 Converting relative paths to full Gürkan
2021-08-10 20:44 ` Bart Schaefer
2021-08-10 21:39   ` Gürkan
2021-08-10 21:53     ` Gürkan
2021-08-10 22:36       ` Bart Schaefer
2021-08-11  6:24         ` Gürkan

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).