zsh-workers
 help / color / mirror / code / Atom feed
From: Sebastian Gniazdowski <sgniazdowski@gmail.com>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: "drop-in replacement" and transpose-words-match
Date: Mon, 18 Jan 2016 18:04:13 +0100	[thread overview]
Message-ID: <CAKc7PVDhxbVgmV7WcGgyDzMmWSjwQ=7=LExXTwr7YvPzPpn2RA@mail.gmail.com> (raw)
In-Reply-To: <160111233259.ZM6719@torch.brasslantern.com>

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

On 12 January 2016 at 08:32, Bart Schaefer <schaefer@brasslantern.com> wrote:
> This still isn't perfect -- if you are on or immediately after a space
> in the middle of a quoted string, for example, you'll swap around that
> space.  I haven't decided on the best way to deal with that.  But this
> works much better if you are at the end of a line and invoke transpose:
> it swaps the two words to the left, like the builtin.

I was looking at various sources in recent months and earlier, and was
getting an impression of how important LBUFFER is to them. Your
implementation of widen_for_history is recent example. I am in general
quite repelled by LBUFFER and can even see it as not actually working,
how it was with the widen_for_history – only after a while I saw that
it was working perfectly. However in my code I choose to use the
function I've attached, available also here:

https://github.com/psprint/zsh-editing-workbench/blob/master/zew-process-buffer

My point is: isn't it that sticking to LBUFFER is the source of
various problems? Its use is always more or less hackish and has
effects like "you should position cursor on beginning of a word to
swap it". Implementation generates features while it should be the
opposite.

It's hard for me to provide more evidence to what I'm stating, but
maybe you agree with me in general

Best regards,
Sebastian Gniazdowski

[-- Attachment #2: zew-process-buffer --]
[-- Type: application/octet-stream, Size: 3056 bytes --]

# Input:
# $1 - buffer to process
#
# Output:
# ZEW_PB_WORDS - split of "$1" into shell words; array
# ZEW_PB_WORDS_BEGINNINGS - indexes of first letters of corresponding words in ZEW_PB_WORDS
# ZEW_PB_SPACES - white spaces before corresponding words in ZEW_PB_WORDS
# ZEW_PB_SELECTED_WORD - index in ZEW_PB_WORDS pointing to word activated by CURSOR position
# ZEW_PB_LEFT - left part of active word
# ZEW_PB_RIGHT - right part of active word
#

emulate -LR zsh
setopt typesetsilent extendedglob noshortloops

local MBEGIN MEND MATCH mbegin mend match

local buf="$1"
ZEW_PB_WORDS=( "${(Z+n+)BUFFER}" )
ZEW_PB_SPACES=( )
ZEW_PB_WORDS_BEGINNINGS=( )
ZEW_PB_SELECTED_WORD="-1"

integer nwords="${#ZEW_PB_WORDS}"

# Remove ZEW_PB_WORDS one by one, counting characters,
# computing beginning of each word, to find
# place to break the word into 2 halves (for
# complete_in_word option)

local i word
integer char_count=0

# (Z) handles spaces nicely, but we need them for the user
# Also compute words beginnings and the selected word
for (( i=1; i<=nwords; i++ )); do
    # Remove spurious space generated by Z-flag when
    # input is an unbound '$(' (happens with zsh < 5.1)
    # and also real spaces gathered by an unbound '$(',
    # to handle them in a way normal to this loop
    ZEW_PB_WORDS[i]="${ZEW_PB_WORDS[i]%% ##}"
    word="${ZEW_PB_WORDS[i]}"

    # In general, $buf can start with white spaces
    # We will not search for them, but instead for
    # leading character of current shell word,
    # negated. This is an ambition to completely
    # avoid character classes

    # Remove white spaces
    buf="${buf##(#m)[^$word[1]]#}"
    # Count them
    char_count=char_count+"$#MATCH"
    # This is the beginning of current word
    ZEW_PB_WORDS_BEGINNINGS[i]=$(( char_count + 1 ))
    # Remember the spaces
    ZEW_PB_SPACES[i]="$MATCH"

    # Remove the word
    MATCH=""
    buf="${buf#(#m)$word}"

    # If shell word not found, return. This shoudln't happen
    [ -z "$MATCH" ] && return 0

    # Spaces point to previous shell word
    # Visual cursor right after spaces (-ge) -> not enough to select previous word (-gt required)
    [[ "$ZEW_PB_SELECTED_WORD" -eq "-1" && "$char_count" -gt "$CURSOR" ]] && ZEW_PB_SELECTED_WORD=$(( i-1 ))

    # Actual characters point to current shell word
    # Visual cursor right after letters (-ge) -> enough to select current word
    char_count=char_count+"$#word"
    [[ "$ZEW_PB_SELECTED_WORD" -eq "-1" && "$char_count" -ge "$CURSOR" ]] && ZEW_PB_SELECTED_WORD="$i"
done 

# What's left in $buf can be only white spaces
char_count=char_count+"$#buf"
ZEW_PB_SPACES[i]="$buf"

# Visual cursor right after spaces (-ge) -> enough to select last word
[[ "$ZEW_PB_SELECTED_WORD" -eq "-1" && "$char_count" -ge "$CURSOR" ]] && ZEW_PB_SELECTED_WORD=$(( i-1 ))

# Divide active word into two halves
integer diff=$(( CURSOR - ZEW_PB_WORDS_BEGINNINGS[ZEW_PB_SELECTED_WORD] + 1 ))
word="${ZEW_PB_WORDS[ZEW_PB_SELECTED_WORD]}"
ZEW_PB_LEFT="${word[1,diff]}"
ZEW_PB_RIGHT="${word[diff+1,-1]}"

# vim:ft=zsh

  parent reply	other threads:[~2016-01-18 17:04 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-12  7:32 Bart Schaefer
2016-01-18  2:25 ` Daniel Shahaf
2016-01-18 16:29   ` Bart Schaefer
2016-01-18 17:04 ` Sebastian Gniazdowski [this message]
2016-01-19  6:31   ` Bart Schaefer
2016-01-19  8:28     ` Sebastian Gniazdowski
2016-01-20  3:56       ` Bart Schaefer
2016-01-23 23:53         ` Daniel Shahaf
2016-01-24  6:20           ` Slow highlighting (Re: "drop-in replacement" and transpose-words-match) Bart Schaefer
2016-01-26 22:50             ` Daniel Shahaf
2016-01-27  4:31               ` Bart Schaefer
2016-01-27  5:10                 ` Mikael Magnusson
2016-01-29  9:18                 ` Daniel Shahaf
2016-02-10 16:32             ` Sebastian Gniazdowski
2016-02-10 18:18               ` Bart Schaefer
2016-02-10 18:37                 ` Sebastian Gniazdowski
2016-02-11 10:43                 ` Sebastian Gniazdowski
2016-02-11 12:07                   ` Sebastian Gniazdowski
2016-02-14 14:34                     ` Daniel Shahaf
2016-02-11 16:11                   ` Sebastian Gniazdowski
2016-02-12  7:34                     ` Sebastian Gniazdowski
2016-02-12 10:05                     ` Bart Schaefer
2016-02-14 14:34                     ` Daniel Shahaf
2016-02-12  9:41                   ` Bart Schaefer
2016-05-06 13:15                     ` Sebastian Gniazdowski
2016-02-14 14:34                   ` Avoiding github link bitrot " Daniel Shahaf
2016-01-20  7:47       ` "drop-in replacement" and transpose-words-match Daniel Shahaf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAKc7PVDhxbVgmV7WcGgyDzMmWSjwQ=7=LExXTwr7YvPzPpn2RA@mail.gmail.com' \
    --to=sgniazdowski@gmail.com \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).