unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] Make rectangle-select able to skip lines(empty one, for example)
@ 2023-01-30 13:47 Constantin Kulikov
  2023-01-30 14:36 ` Constantin Kulikov
  0 siblings, 1 reply; 6+ messages in thread
From: Constantin Kulikov @ 2023-01-30 13:47 UTC (permalink / raw)
  To: emacs-devel


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

Code to try:

(with-eval-after-load "rect.el"
  (defvar *rectangle-skip-lines* nil
"If `t' -- skip empty lines, if `function' -- skip line if it returns `nil'.
The function gets all arguments of `apply-to-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
 (/= (line-beginning-position) (line-end-position))))
(apply function startcol endcol args))
 (setq final-point (point))
 (and (zerop (forward-line 1)) (bolp)
  (<= (point) endpt))))
final-point))))

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

[-- Attachment #2: rect.el.patch --]
[-- Type: application/octet-stream, Size: 1449 bytes --]

--- rect.el	2023-01-30 14:18:22.542176710 +0300
+++ rect-patched.el	2023-01-30 15:44:14.953579566 +0300
@@ -143,12 +143,16 @@
   (rectangle--reset-point-crutches))
 
 ;;; Rectangle operations.
+(defvar *rectangle-skip-lines* nil
+  "If `t' -- skip empty lines, if `function' -- skip line if it returns `nil'.
+The function gets all arguments of `apply-to-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 +170,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
+					(/= (line-beginning-position) (line-end-position))))
+			  (apply function startcol endcol args))
             (setq final-point (point))
             (and (zerop (forward-line 1)) (bolp)
                  (<= (point) endpt))))

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

end of thread, other threads:[~2023-02-02 19:53 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2023-01-31 16:18     ` [External] : " Drew Adams
2023-02-02 18:26       ` Constantin Kulikov
2023-02-02 19:53         ` Drew Adams

Code repositories for project(s) associated with this public inbox

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

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 read-only IMAP folder(s) and NNTP newsgroup(s).