emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] Make rectangle-select able to skip lines(empty one, for exam


From: Constantin Kulikov
Subject: Re: [PATCH] Make rectangle-select able to skip lines(empty one, for example)
Date: Tue, 31 Jan 2023 18:05:18 +0300

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: rect.el.patch.txt
Description: Text document


reply via email to

[Prev in Thread] Current Thread [Next in Thread]