all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Constantin Kulikov <zxnotdead@gmail.com>
To: emacs-devel <emacs-devel@gnu.org>
Subject: Re: [PATCH] Make rectangle-select able to skip lines(empty one, for example)
Date: Tue, 31 Jan 2023 18:05:18 +0300	[thread overview]
Message-ID: <CAFkz2ypzSD5_g_anhfd35SfiX-HLUp_isN4R_kWVXNEkAkej=w@mail.gmail.com> (raw)
In-Reply-To: <CAFkz2yqKz-KRHnp6LFuT0_miJejsrji_KcHb-3RMdSGA4Z--Ng@mail.gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 3324 bytes --]

Treat lines with whitespaces only as 'empty'.

--- rect-orig.el 2023-01-31 17:25:08.498658466 +0300
+++ rect-patched.el 2023-01-31 17:30:02.163121185 +0300
@@ -144,11 +144,16 @@

 ;;; Rectangle operations.

+(defvar *rectangle-skip-lines* nil
+  "If `t' -- skip empty lines, if `function' -- skip line when it returns
`nil'.
+The function gets all arguments of `apply-on-rectangle' as input.")
+
 (defun apply-on-rectangle (function start end &rest args)
   "Call FUNCTION for each line of rectangle with corners at START, END.
 FUNCTION is called with two arguments: the start and end columns of the
 rectangle, plus ARGS extra arguments.  Point is at the beginning of line
when
 the function is called.
+Application of the FUNCTION is affected by the `*rectangle-skip-lines*'.
 The final point after the last operation will be returned."
   (save-excursion
     (let* ((cols (rectangle--pos-cols start end))
@@ -166,7 +171,14 @@
       (goto-char startpt)
       (while
           (progn
-            (apply function startcol endcol args)
+            (when (cond
+                   ((null *rectangle-skip-lines*)
+                    t)
+                   ((functionp *rectangle-skip-lines*)
+                    (apply *rectangle-skip-lines* function start end args))
+                   (t
+                    (not (string-match-p "\\`\\s-*$" (thing-at-point
'line)))))
+              (apply function startcol endcol args))
             (setq final-point (point))
             (and (zerop (forward-line 1)) (bolp)
                  (<= (point) endpt))))


------------------------------

(with-eval-after-load "rect.el"
  (defvar *rectangle-skip-lines* nil
"If `t' -- skip empty lines, if `function' -- skip line when it returns
`nil'.
The function gets all arguments of `apply-on-rectangle' as input.")
  (defun apply-on-rectangle (function start end &rest args)
"Call FUNCTION for each line of rectangle with corners at START, END.
FUNCTION is called with two arguments: the start and end columns of the
rectangle, plus ARGS extra arguments.  Point is at the beginning of line
when
the function is called.
Application of the FUNCTION is affected by the `*rectangle-skip-lines*'.
The final point after the last operation will be returned."
(save-excursion
 (let* ((cols (rectangle--pos-cols start end))
(startcol (car cols))
(endcol (cdr cols))
(startpt (progn (goto-char start) (line-beginning-position)))
(endpt (progn (goto-char end)
  (copy-marker (line-end-position))))
final-point)
;; Ensure the start column is the left one.
(if (< endcol startcol)
(let ((col startcol))
 (setq startcol endcol endcol col)))
;; Start looping over lines.
(goto-char startpt)
(while
(progn
 (when (cond
((null *rectangle-skip-lines*)
 t)
((functionp *rectangle-skip-lines*)
 (apply *rectangle-skip-lines* function start end args))
(t
 (not (string-match-p "\\`\\s-*$" (thing-at-point 'line)))))
(apply function startcol endcol args))
 (setq final-point (point))
 (and (zerop (forward-line 1)) (bolp)
  (<= (point) endpt))))
final-point))))


On Mon, 30 Jan 2023 at 17:36, Constantin Kulikov <zxnotdead@gmail.com>
wrote:

> Actually it must be named: "*Make `apply-on-rectangle' able to skip lines*
> ".
> Reattaching the patch as .txt
>
> On Mon, 30 Jan 2023 at 16:47, Constantin Kulikov <zxnotdead@gmail.com>
> wrote:
>
>>
>>

[-- Attachment #1.2: Type: text/html, Size: 4844 bytes --]

[-- Attachment #2: rect.el.patch.txt --]
[-- Type: text/plain, Size: 1517 bytes --]

--- rect-orig.el	2023-01-31 17:25:08.498658466 +0300
+++ rect-patched.el	2023-01-31 17:30:02.163121185 +0300
@@ -144,11 +144,16 @@
 
 ;;; Rectangle operations.
 
+(defvar *rectangle-skip-lines* nil
+  "If `t' -- skip empty lines, if `function' -- skip line when it returns `nil'.
+The function gets all arguments of `apply-on-rectangle' as input.")
+
 (defun apply-on-rectangle (function start end &rest args)
   "Call FUNCTION for each line of rectangle with corners at START, END.
 FUNCTION is called with two arguments: the start and end columns of the
 rectangle, plus ARGS extra arguments.  Point is at the beginning of line when
 the function is called.
+Application of the FUNCTION is affected by the `*rectangle-skip-lines*'.
 The final point after the last operation will be returned."
   (save-excursion
     (let* ((cols (rectangle--pos-cols start end))
@@ -166,7 +171,14 @@
       (goto-char startpt)
       (while
           (progn
-            (apply function startcol endcol args)
+            (when (cond
+                   ((null *rectangle-skip-lines*)
+                    t)
+                   ((functionp *rectangle-skip-lines*)
+                    (apply *rectangle-skip-lines* function start end args))
+                   (t
+                    (not (string-match-p "\\`\\s-*$" (thing-at-point 'line)))))
+              (apply function startcol endcol args))
             (setq final-point (point))
             (and (zerop (forward-line 1)) (bolp)
                  (<= (point) endpt))))

  reply	other threads:[~2023-01-31 15:05 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-30 13:47 [PATCH] Make rectangle-select able to skip lines(empty one, for example) Constantin Kulikov
2023-01-30 14:36 ` Constantin Kulikov
2023-01-31 15:05   ` Constantin Kulikov [this message]
2023-01-31 16:18     ` [External] : " Drew Adams
2023-02-02 18:26       ` Constantin Kulikov
2023-02-02 19:53         ` Drew Adams

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='CAFkz2ypzSD5_g_anhfd35SfiX-HLUp_isN4R_kWVXNEkAkej=w@mail.gmail.com' \
    --to=zxnotdead@gmail.com \
    --cc=emacs-devel@gnu.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 external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.