From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 17715 invoked from network); 8 Aug 2020 10:19:18 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 8 Aug 2020 10:19:18 -0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20200801; h=Sender:List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:References:In-Reply-To:Message-Id:Date: Subject:To:From:Reply-To:Cc:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=hKfPyKVlENQ/NdJD+afrbkMfvwQak1PriRq5W7Q+YlA=; b=VOOpb7g8UaBjkHqqYINlYr2zSt 5E4Myru2DIP9pz3RrTo+b50CzNYzmKFZjPJgeRJcstvmCH9Yc4YRwG4wmI1XkrWOm7yEHeKYlno9g 4GpS53D3jU8q+eCKQTDcGZ9ZIHS5DQEWEDOgmRLMaVcFOczfuw6IOitcj0NZ7b55pmTExLvtIOlHv F36TfkwaXZPq49ifEXzP4kUxYw5KfzE5PmZ0pnXrvz1HsljHfyQO1VSOBUaMTAZGxWJDu7y6oxeqS culHzfC73sVdqeQVATT5ukoucVFONe+D+Nsi7DG2qYXKx2ej7rXA79OrlrRM9Why81zfGU+DB8yD3 wgPjoNtw==; Received: from authenticated user by zero.zsh.org with local id 1k4Lwb-0001O9-Bu; Sat, 08 Aug 2020 10:19:17 +0000 Authentication-Results: zsh.org; iprev=pass (mail-lj1-f182.google.com) smtp.remote-ip=209.85.208.182; dkim=pass header.d=gmail.com header.s=20161025 header.a=rsa-sha256 Received: from mail-lj1-f182.google.com ([209.85.208.182]:44813) by zero.zsh.org with esmtps (TLS1.2:ECDHE-RSA-AES128-GCM-SHA256:128) id 1k4LwP-0001FQ-KS; Sat, 08 Aug 2020 10:19:05 +0000 Received: by mail-lj1-f182.google.com with SMTP id g6so4690013ljn.11 for ; Sat, 08 Aug 2020 03:19:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=hKfPyKVlENQ/NdJD+afrbkMfvwQak1PriRq5W7Q+YlA=; b=YJAbbc6lwYWw+3LnKD6vONLY5JI2ydO3BUoM8gYpplyRIPbEbeYRpGVwav+wO7u7BD BUuJhlefBeNy5V91dZhg69IF4XQCQvCuiyEhhKr+hUdqpxUKfMgSEiuKJ8FojasCWmUk TZs+Hf3zeyjhV7Ahy/O/Jrn40yY9DDINCc2VsU9rT+0gY7RbUDdKrOwB2rT02zDECXIS 7sUz+QZeOL5qaw+RpJWiBrDeWPV6FIfStFhKXjEvXiC1IOxYvMYIUOBUPnJcCMrMIyOn PuyssRveCkkLU+WyPy7GfIrjkjEjXqzhJEbdWR5dU0WrrDD1HosTz/Gcspt+qk+ZFfYP +wLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=hKfPyKVlENQ/NdJD+afrbkMfvwQak1PriRq5W7Q+YlA=; b=gOFgswV4LOBwzuweBDNIzOXbdsAeqgaGmbaV2o7TchCg/hi0atVg+gRvME5olC6TgQ 2tY/LIABCoUENzlbR0f83ToY2JHj/PQFBhbplpt19mwiW/RJFVStpvb6nPasIGCwx62H 3KK5xfnYfAUAdxrdiHXZJeGgaldd03VzmNNT/Hq8DhJ1KttXJOoTlPEYAIbcqlNz6WN3 ScsqtIkPK5nUsqVks+wgR4tNVmEc7RDL4Egvdn+tTIqopc/4NVtka8jWiV8OYjIYU+JA H2perwvTMRdiKLap41MGaagrdJD1n7cryTscsW0rk1U2B4LS0hMKjqb9NEpYkEPUKFXA uAjw== X-Gm-Message-State: AOAM531DfJB/cAHJ+zLU/UTJt+li49C6DE+Svqs4cXOnfTUARgk9tyfy BHDbvmCa2CcwKUIJWgLVdrj5lJWQ X-Google-Smtp-Source: ABdhPJzS4VFMzHT2FR7gmym+jzDpYuMNTDDURsa6E8giX3RAP8VjHEgZx8qT7VCdUB5HhRYOK14aqA== X-Received: by 2002:a2e:97da:: with SMTP id m26mr8873802ljj.9.1596881944483; Sat, 08 Aug 2020 03:19:04 -0700 (PDT) Received: from localhost.localdomain (h-88-110.A230.priv.bahnhof.se. [212.85.88.110]) by smtp.gmail.com with ESMTPSA id q20sm4914655ljj.42.2020.08.08.03.19.03 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 08 Aug 2020 03:19:03 -0700 (PDT) From: Mikael Magnusson To: zsh-workers@zsh.org Subject: PATCH: edit-command-line: restrict editing to region if it is active Date: Sat, 8 Aug 2020 12:18:57 +0200 Message-Id: <20200808101857.26868-1-mikachu@gmail.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20200808095148.21555-1-mikachu@gmail.com> References: <20200808095148.21555-1-mikachu@gmail.com> X-Zsh-Org-Should-ARC-Sign: true X-Seq: 47307 X-Loop: zsh-workers@zsh.org Errors-To: zsh-workers-owner@zsh.org Precedence: list Precedence: bulk X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: Archived-At: Sender: Sympa Owner --- Functions/Zle/edit-command-line | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/Functions/Zle/edit-command-line b/Functions/Zle/edit-command-line index 8aaeb738e5..3781244b27 100644 --- a/Functions/Zle/edit-command-line +++ b/Functions/Zle/edit-command-line @@ -7,9 +7,20 @@ # except that it will handle multi-line buffers properly. emulate -L zsh -local prebuffer -# see below comment for why this is needed -if (( ! ZLE_RECURSIVE )); then +local left right prebuffer buffer=$BUFFER lbuffer=$LBUFFER +# set up parameters depending on which context we are called from, +# see below comment for more details +if (( REGION_ACTIVE )); then + if (( CURSOR < MARK )); then + left=$CURSOR right=$MARK + lbuffer= + else + left=$MARK right=$CURSOR + lbuffer[right-left,-1]= + fi + (( left++ )) + buffer=$BUFFER[left,right] +elif (( ! ZLE_RECURSIVE )); then prebuffer=$PREBUFFER fi @@ -29,10 +40,10 @@ fi fi case $editor in (*vim*) - integer byteoffset=$(( $#PREBUFFER + $#LBUFFER + 1 )) + integer byteoffset=$(( $#prebuffer + $#lbuffer + 1 )) "${(@)editor}" -c "normal! ${byteoffset}go" -- $1;; (*emacs*) - local lines=( "${(@f):-"$PREBUFFER$LBUFFER"}" ) + local lines=( "${(@f):-"$prebuffer$lbuffer"}" ) "${(@)editor}" +${#lines}:$((${#lines[-1]} + 1)) $1;; (*) "${(@)editor}" $1;; esac @@ -48,13 +59,24 @@ fi # the following point) # - when we are at PS2 (CONTEXT == cont && ! ZLE_RECURSIVE) we do want the # break or otherwise the text from PREBUFFER will be inserted twice + # - when the region is active, we only want to change the parts of BUFFER + # covered by the region, and any PREBUFFER stays as PREBUFFER # - in all other cases (that I can think of) we also just want to set # $BUFFER directly. - if [[ $CONTEXT != cont ]] || (( ZLE_RECURSIVE )); then + if (( REGION_ACTIVE )); then + # adjust the length of the region to the length of the edited text + local prelen=$#BUFFER + BUFFER[left,right]="$(<$1)" + if (( MARK > CURSOR )); then + (( MARK += $#BUFFER - prelen )) + else + (( CURSOR += $#BUFFER - prelen )) + fi + elif [[ $CONTEXT != cont ]] || (( ZLE_RECURSIVE )); then BUFFER="$(<$1)" else print -Rz - "$(<$1)" zle send-break fi -} =(<<<"$prebuffer$BUFFER") +} =(<<<"$prebuffer$buffer") -- 2.15.1