zsh-workers
 help / color / mirror / code / Atom feed
* zed-set-filename breaks zed keymap?
@ 2020-05-27  3:30 Bart Schaefer
  2020-05-27  4:25 ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2020-05-27  3:30 UTC (permalink / raw)
  To: Zsh hackers list

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

I was trying to get zed-set-file-name to work with my "zed -h" change,
and discovered that even with plain "zed file" after entering the file
name into read-from-minibuffer, ^X^W no longer works to exit from zed,
and a bare return DOES exit it.  Apparently returning from
read-from-minibuffer does not constitute "the next command" for
purposes of zle -K restoring the keymap?  Did this work at one time?

Also, it is possible to execute zed-set-file-name recursively from
inside read-from-minibuffer, which is a bit strange.

xtrace output attached in case somebody needs help finding a clue.

[-- Attachment #2: xtrace.txt --]
[-- Type: text/plain, Size: 7800 bytes --]

Just "zed /tmp/one", an existing file from a previous test:

+zed:10> setopt localoptions noksharrays
+zed:12> local var opts zed_file_name
+zed:14> integer TMOUT=0 okargs=1 fun hist bind
+zed:15> local -a expand
+zed:17> zparseopts -D -A opts f h b x:
+zed:18> fun=0 
+zed:19> hist=0 
+zed:20> bind=0 
+zed:21> [[ '' == <-> ]]
+zed:23> ((  0  ))
+zed:28> [[ zed = fned ]]
+zed:29> [[ zed = histed ]]
+zed:30> ((  hist && 1 <= 2  ))
+zed:31> ((  bind  ))
+zed:33> ((  1 != okargs || bind + fun + hist > 1  ))
+zed:42> local curcontext=zed:::
+zed:44> '(anon)'
+(anon):3> local -a match mbegin mend
+(anon):4> zstyle -m ':completion:zed:*' insert-tab '*'
+(anon):5> zstyle ':completion:zed:*' insert-tab yes
+zed:52> zmodload zsh/terminfo
+zed:70> zle -la __zed_pg_up __zed_pg_down
+zed:71> zle -N __zed_pg_up
+zed:72> zle -N __zed_pg_down
+zed:75> ((  bind  ))
+zed:75> bindkey -M zed
+zed:77> bindkey -N zed main
+zed:80> bindkey -A main zed-normal-keymap
+zed:86> zle -N __zed_init
+zed:93> bindkey -M zed '^x^w' accept-line
+zed:94> bindkey -M zed '^M' self-insert-unmeta
+zed:96> [[ 1 = 1 ]]
+zed:97> [[ -n $'\C-[[5~' ]]
+zed:97> bindkey -M zed $'\C-[[5~' __zed_pg_up
+zed:98> [[ -n $'\C-[[6~' ]]
+zed:98> bindkey -M zed $'\C-[[6~' __zed_pg_down
+zed:99> [[ -n '' ]]
+zed:100> [[ -n '' ]]
+zed:103> bindkey -M zed '^[[H' beginning-of-line
+zed:104> bindkey -M zed '^[[F' end-of-line
+zed:109> autoload -Uz zed-set-file-name
+zed:110> zle -N zed-set-file-name
+zed:112> ((  bind  ))
+zed:112> bindkey -M zed-vicmd
+zed:113> bindkey -N zed-vicmd vicmd
+zed:115> bindkey -M zed-vicmd ZZ accept-line
+zed:116> [[ 1 = 1 ]]
+zed:117> [[ -n $'\C-[[5~' ]]
+zed:117> bindkey -M zed-vicmd $'\C-[[5~' __zed_pg_up
+zed:118> [[ -n $'\C-[[6~' ]]
+zed:118> bindkey -M zed-vicmd $'\C-[[6~' __zed_pg_down
+zed:119> [[ -n '' ]]
+zed:120> [[ -n '' ]]
+zed:123> bindkey -M zed-vicmd '^[[H' vi-beginning-of-line
+zed:124> bindkey -M zed-vicmd '^[[F' vi-end-of-line
+zed:128> ((  bind  ))
+zed:131> setopt localoptions nobanghist
+zed:133> (( fun ))
+zed:143> (( hist ))
+zed:173> zed_file_name=/tmp/one 
+zed:174> [[ -f /tmp/one ]]
+zed:174> var='Where does this go?' 
+zed:175> vared -M zed -m zed-vicmd -i __zed_init var
+zed-set-file-name:1> emulate -L zsh
+zed-set-file-name:3> autoload -Uz read-from-minibuffer
+zed-set-file-name:5> zle -K zed-normal-keymap
+zed-set-file-name:7> local REPLY
+zed-set-file-name:8> read-from-minibuffer 'File name: '
+read-from-minibuffer:1> emulate -L zsh
+read-from-minibuffer:2> setopt extendedglob
+read-from-minibuffer:4> local opt keys
+read-from-minibuffer:5> integer stat
+read-from-minibuffer:7> getopts k: opt
+read-from-minibuffer:20> ((  OPTIND > 1  ))
+read-from-minibuffer:22> local readprompt='File name: ' lbuf_init='' rbuf_init=''
+read-from-minibuffer:23> integer savelim=1 changeno=1
+read-from-minibuffer:29> '(anon)'
+(anon):1> local pretext=$'Where does this go?\n'
+(anon):3> local +h LBUFFER=''
+(anon):4> local +h RBUFFER=''
+(anon):5> local +h PREDISPLAY=$'Where does this go?\nFile name: '
+(anon):6> local +h POSTDISPLAY=''
+(anon):7> local +h -a region_highlight
+(anon):8> region_highlight=( 'P20 31 bold' ) 
+(anon):10> [[ -n '' ]]
+(anon):15> local NUMERIC
+(anon):16> unset NUMERIC
+(anon):17> zle split-undo
+(anon):18> UNDO_LIMIT_NO=2 
+(anon):19> zle recursive-edit -K main
+(anon):20> stat=0 
+(anon):21> ((  stat  ))
+(anon):21> REPLY=/tmp/five 
+read-from-minibuffer:56> zle undo 1
+read-from-minibuffer:57> UNDO_LIMIT_NO=savelim 
+read-from-minibuffer:60> return 0
+zed-set-file-name:9> zed_file_name=/tmp/five 
+zed:178> print -r -- 'Where does this go?  Here'
+zed:180> ((  TRY_BLOCK_ERROR = 0  ))
+zed:181> break
+zed:186> return 0

Now "zed /tmp/five" (the file written above) with recursive invocation of
zed-set-file-name; note value assigned to zed_file_name:

+zed:10> setopt localoptions noksharrays
+zed:12> local var opts zed_file_name
+zed:14> integer TMOUT=0 okargs=1 fun hist bind
+zed:15> local -a expand
+zed:17> zparseopts -D -A opts f h b x:
+zed:18> fun=0 
+zed:19> hist=0 
+zed:20> bind=0 
+zed:21> [[ '' == <-> ]]
+zed:23> ((  0  ))
+zed:28> [[ zed = fned ]]
+zed:29> [[ zed = histed ]]
+zed:30> ((  hist && 1 <= 2  ))
+zed:31> ((  bind  ))
+zed:33> ((  1 != okargs || bind + fun + hist > 1  ))
+zed:42> local curcontext=zed:::
+zed:44> '(anon)'
+(anon):3> local -a match mbegin mend
+(anon):4> zstyle -m ':completion:zed:*' insert-tab '*'
+zed:52> zmodload zsh/terminfo
+zed:70> zle -la __zed_pg_up __zed_pg_down
+zed:75> ((  bind  ))
+zed:75> bindkey -M zed
+zed:112> ((  bind  ))
+zed:112> bindkey -M zed-vicmd
+zed:128> ((  bind  ))
+zed:131> setopt localoptions nobanghist
+zed:133> (( fun ))
+zed:143> (( hist ))
+zed:173> zed_file_name=/tmp/five 
+zed:174> [[ -f /tmp/five ]]
+zed:174> var='Where does this go?  Here' 
+zed:175> vared -M zed -m zed-vicmd -i __zed_init var
+zed-set-file-name:1> emulate -L zsh
+zed-set-file-name:3> autoload -Uz read-from-minibuffer
+zed-set-file-name:5> zle -K zed-normal-keymap
+zed-set-file-name:7> local REPLY
+zed-set-file-name:8> read-from-minibuffer 'File name: '
+read-from-minibuffer:1> emulate -L zsh
+read-from-minibuffer:2> setopt extendedglob
+read-from-minibuffer:4> local opt keys
+read-from-minibuffer:5> integer stat
+read-from-minibuffer:7> getopts k: opt
+read-from-minibuffer:20> ((  OPTIND > 1  ))
+read-from-minibuffer:22> local readprompt='File name: ' lbuf_init='' rbuf_init=''
+read-from-minibuffer:23> integer savelim=1 changeno=1
+read-from-minibuffer:29> '(anon)'
+(anon):1> local pretext=$'Where does this go?  Here\n'
+(anon):3> local +h LBUFFER=''
+(anon):4> local +h RBUFFER=''
+(anon):5> local +h PREDISPLAY=$'Where does this go?  Here\nFile name: '
+(anon):6> local +h POSTDISPLAY=''
+(anon):7> local +h -a region_highlight
+(anon):8> region_highlight=( 'P26 37 bold' ) 
+(anon):10> [[ -n '' ]]
+(anon):15> local NUMERIC
+(anon):16> unset NUMERIC
+(anon):17> zle split-undo
+(anon):18> UNDO_LIMIT_NO=2 
+(anon):19> zle recursive-edit -K main
+zed-set-file-name:1> emulate -L zsh
+zed-set-file-name:3> autoload -Uz read-from-minibuffer
+zed-set-file-name:5> zle -K zed-normal-keymap
+zed-set-file-name:7> local REPLY
+zed-set-file-name:8> read-from-minibuffer 'File name: '
+read-from-minibuffer:1> emulate -L zsh
+read-from-minibuffer:2> setopt extendedglob
+read-from-minibuffer:4> local opt keys
+read-from-minibuffer:5> integer stat
+read-from-minibuffer:7> getopts k: opt
+read-from-minibuffer:20> ((  OPTIND > 1  ))
+read-from-minibuffer:22> local readprompt='File name: ' lbuf_init='' rbuf_init=''
+read-from-minibuffer:23> integer savelim=2 changeno=15
+read-from-minibuffer:29> '(anon)'
+(anon):1> local pretext=$'Where does this go?  Here\nFile name: /tmp/six\n\n'
+(anon):3> local +h LBUFFER=''
+(anon):4> local +h RBUFFER=''
+(anon):5> local +h PREDISPLAY=$'Where does this go?  Here\nFile name: /tmp/six\n\nFile name: '
+(anon):6> local +h POSTDISPLAY=''
+(anon):7> local +h -a region_highlight
+(anon):8> region_highlight=( 'P47 58 bold' ) 
+(anon):10> [[ -n '' ]]
+(anon):15> local NUMERIC
+(anon):16> unset NUMERIC
+(anon):17> zle split-undo
+(anon):18> UNDO_LIMIT_NO=16 
+(anon):19> zle recursive-edit -K main
+(anon):20> stat=0 
+(anon):21> ((  stat  ))
+(anon):21> REPLY=/tmp/seven 
+read-from-minibuffer:56> zle undo 15
+read-from-minibuffer:57> UNDO_LIMIT_NO=savelim 
+read-from-minibuffer:60> return 0
+zed-set-file-name:9> zed_file_name=/tmp/seven 
+(anon):20> stat=0 
+(anon):21> ((  stat  ))
+(anon):21> REPLY=$'/tmp/six\n' 
+read-from-minibuffer:56> zle undo 1
+read-from-minibuffer:57> UNDO_LIMIT_NO=savelim 
+read-from-minibuffer:60> return 0
+zed-set-file-name:9> zed_file_name=$'/tmp/six\n' 
+zed:178> print -r -- 'Where does this go?  There'
+zed:180> ((  TRY_BLOCK_ERROR = 0  ))
+zed:181> break
+zed:186> return 0

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

* Re: zed-set-filename breaks zed keymap?
  2020-05-27  3:30 zed-set-filename breaks zed keymap? Bart Schaefer
@ 2020-05-27  4:25 ` Bart Schaefer
  2021-04-25 19:35   ` [PATCH] " Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2020-05-27  4:25 UTC (permalink / raw)
  To: Zsh hackers list

On Tue, May 26, 2020 at 8:30 PM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> I discovered that even with plain "zed file" after entering the file
> name into read-from-minibuffer, ^X^W no longer works to exit from zed,
> and a bare return DOES exit it.

The documentation for zed-set-file-name seems contradictory as well.
In the first paragraph it says:

     ... because of zed's rebindings you will have to type ^j at the
     end instead of the return key ...

but then the second paragraph says

     While zed-set-file-name is running, zed uses the keymap
     zed-normal-keymap, which is linked from the main keymap in effect
     at the time zed initialised its bindings.  (This is to make the
     return key operate normally.) ...

The return key does not in fact "operate normally" during
zed-set-file-name, but something else strange is going on with the
keymaps.

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

* [PATCH] Re: zed-set-filename breaks zed keymap?
  2020-05-27  4:25 ` Bart Schaefer
@ 2021-04-25 19:35   ` Bart Schaefer
  2021-04-25 20:55     ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2021-04-25 19:35 UTC (permalink / raw)
  To: Zsh hackers list

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

Reviving this from a year-ish ago, so including that thread for
context ... please see below.

On Tue, May 26, 2020 at 8:30 PM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> I was trying to get zed-set-file-name to work with my "zed -h" change,
> and discovered that even with plain "zed file" after entering the file
> name into read-from-minibuffer, ^X^W no longer works to exit from zed,
> and a bare return DOES exit it.  Apparently returning from
> read-from-minibuffer does not constitute "the next command" for
> purposes of zle -K restoring the keymap?  Did this work at one time?
>
> Also, it is possible to execute zed-set-file-name recursively from
> inside read-from-minibuffer, which is a bit strange.

On Tue, May 26, 2020 at 9:25 PM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> The documentation for zed-set-file-name seems contradictory as well.
> In the first paragraph it says:
>
>      ... because of zed's rebindings you will have to type ^j at the
>      end instead of the return key ...
>
> but then the second paragraph says
>
>      While zed-set-file-name is running, zed uses the keymap
>      zed-normal-keymap, which is linked from the main keymap in effect
>      at the time zed initialised its bindings.  (This is to make the
>      return key operate normally.) ...
>
> The return key does not in fact "operate normally" during
> zed-set-file-name, but something else strange is going on with the
> keymaps.

After some experimentation ...

zed does these things up front:
1. Creates the keymap zed as a copy of main (bindkey -N zed main)
2. Aliases zed-normal-keymap (bindkey -A main zed-normal-keymap)
3. Creates the keymap zed-vicmd as a copy of vicmd

It then eventually runs:  vared -M zed -m zed-vicmd ...

Next bin_vared hides the "main" keymap and renames the keymap passed
for -M as "main", so $KEYMAP is not "zed".

zed-set-file-name then does this:
1. zle -K zed-normal-keymap
2. read-from-minibuffer

However, zle -K is useless, because eventually read-from-minibuffer runs
  zle recursive-edit -K main
and because of vared's dirty trick, this pulls in the "zed" keymap,
not the keymap that read-from-minibuffer actually wanted.

So ... what's the correct fix here?  Shortest is to change
zed-set-file-name to use
  bindkey -A zed-normal-keymap main
Attached patch does that.  Other options:

Change vared to act more like "zle widget -K"?  I don't think that
breaks any promises, but implicitly $KEYMAP has been "main" inside
vared all these years (when not viins).

Change read-from-minibuffer to make its own keymap?  It's unclear how
to accomplish that cleanly when vared has already done its thing,
unless we force an explicit set of keybindings.

Change read-from-minibuffer to pass in the keymap it should bind for
recursive-edit?  How?  -K option to go with the single existing -k
option?

Rewrite zed-set-file-name to invoke zle recursive-edit directly, with
the correct keymap?

[-- Attachment #2: zsfn-patch.txt --]
[-- Type: text/plain, Size: 985 bytes --]

diff --git a/Functions/Zle/zed-set-file-name b/Functions/Zle/zed-set-file-name
index da3421e71..c77a9d26e 100644
--- a/Functions/Zle/zed-set-file-name
+++ b/Functions/Zle/zed-set-file-name
@@ -2,8 +2,25 @@ emulate -L zsh
 
 autoload -Uz read-from-minibuffer
 
-zle -K zed-normal-keymap
-
-local REPLY
-read-from-minibuffer "File name: "
-zed_file_name=$REPLY
+case $curcontext in
+  (zed:::)
+    local curcontext=zed-set-filename:::
+    # The call to vared from zed does the equivalent of
+    #  bindkey -A zed main
+    # which confuses read-from-minibuffer.  Fix it.
+    bindkey -A zed-normal-keymap main;;
+  (zed-set-filename:::)
+    zle -M "zed-set-file-name: may not be called recursively"
+    return 1;;
+  (*)
+    zle -M "zed-set-file-name: not called from within zed"
+    return 1;;
+esac
+{
+  local REPLY
+  read-from-minibuffer "File name: "
+  zed_file_name=$REPLY
+} always {
+  # Re-install the zed keymap in the way vared should have all along
+  zle -K zed
+}

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

* Re: [PATCH] Re: zed-set-filename breaks zed keymap?
  2021-04-25 19:35   ` [PATCH] " Bart Schaefer
@ 2021-04-25 20:55     ` Bart Schaefer
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2021-04-25 20:55 UTC (permalink / raw)
  To: Zsh hackers list

On Sun, Apr 25, 2021 at 12:35 PM Bart Schaefer
<schaefer@brasslantern.com> wrote:
>
> So ... what's the correct fix here?  Shortest is to change
> zed-set-file-name to use
>   bindkey -A zed-normal-keymap main
> Attached patch does that.
>
> +    local curcontext=zed-set-filename:::

(Typo, should be "file-name", will fix if/when committing)

Incidentally, invoking _complete_help from either zed or
zed-set-file-name gives output like this:
-----
% zed foo

File name:
_help_sort_tags:comptry:8: no tags registered

File name:
tags in context :completion:zed-set-file-name:complete:-value-,var,::
    use-compctl  (_default _value _value _in_vared)
-----

Is an error message from comptry really appropriate?  Easily enough
discarded in _complete_help, but under what circumstances would one
ever want that error?


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

end of thread, other threads:[~2021-04-25 20:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-27  3:30 zed-set-filename breaks zed keymap? Bart Schaefer
2020-05-27  4:25 ` Bart Schaefer
2021-04-25 19:35   ` [PATCH] " Bart Schaefer
2021-04-25 20:55     ` 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).