From b39e9631d6541de8f32524c26fe922e90776abc1 Mon Sep 17 00:00:00 2001 From: Renato Ferreira Date: Thu, 2 May 2019 21:41:14 -0300 Subject: [PATCH] Faster line-number-at-pos calculation --- mc-edit-lines.el | 6 +++--- mc-mark-more.el | 4 ++-- multiple-cursors-core.el | 13 +++++++++++++ rectangular-region-mode.el | 6 +++--- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/mc-edit-lines.el b/mc-edit-lines.el index e38d1c1..55772af 100644 --- a/mc-edit-lines.el +++ b/mc-edit-lines.el @@ -54,8 +54,8 @@ other non-nil value will cause short lines to be padded." (error "Mark a set of lines first")) (mc/remove-fake-cursors) (let* ((col (current-column)) - (point-line (line-number-at-pos)) - (mark-line (progn (exchange-point-and-mark) (line-number-at-pos))) + (point-line (mc/line-number-at-pos)) + (mark-line (progn (exchange-point-and-mark) (mc/line-number-at-pos))) (direction (if (< point-line mark-line) :up :down)) (style (cond ;; called from lisp @@ -71,7 +71,7 @@ other non-nil value will cause short lines to be padded." (previous-logical-line 1 nil) (move-to-column col)) ;; Add the cursors - (while (not (eq (line-number-at-pos) point-line)) + (while (not (eq (mc/line-number-at-pos) point-line)) ;; Pad the line (when (eq style 'pad) (while (< (current-column) col) diff --git a/mc-mark-more.el b/mc-mark-more.el index da8cedf..2e63130 100644 --- a/mc-mark-more.el +++ b/mc-mark-more.el @@ -579,8 +579,8 @@ If the region is inactive or on a single line, it will behave like (interactive "P") (if (and (use-region-p) (not (> (mc/num-cursors) 1)) - (not (= (line-number-at-pos (region-beginning)) - (line-number-at-pos (region-end))))) + (not (= (mc/line-number-at-pos (region-beginning)) + (mc/line-number-at-pos (region-end))))) (if arg (call-interactively 'mc/edit-lines) (call-interactively 'mc/mark-all-in-region)) diff --git a/multiple-cursors-core.el b/multiple-cursors-core.el index 12e9e75..d93ace9 100644 --- a/multiple-cursors-core.el +++ b/multiple-cursors-core.el @@ -109,6 +109,19 @@ (and (listp cursor-type) (eq (car cursor-type) 'bar)))) +(defun mc/line-number-at-pos (&optional pos absolute) + "Faster implementation of `line-number-at-pos'." + (if pos + (save-excursion + (if absolute + (save-restriction + (widen) + (goto-char pos) + (string-to-number (format-mode-line "%l"))) + (goto-char pos) + (string-to-number (format-mode-line "%l")))) + (string-to-number (format-mode-line "%l")))) + (defun mc/make-cursor-overlay-at-eol (pos) "Create overlay to look like cursor at end of line." (let ((overlay (make-overlay pos pos nil nil nil))) diff --git a/rectangular-region-mode.el b/rectangular-region-mode.el index 01a078d..d8051cc 100644 --- a/rectangular-region-mode.el +++ b/rectangular-region-mode.el @@ -75,9 +75,9 @@ an exceedingly quick way of adding multiple cursors to multiple lines." (rrm/remove-rectangular-region-overlays) (let* ((annoying-arrows-mode nil) (point-column (current-column)) - (point-line (line-number-at-pos)) + (point-line (mc/line-number-at-pos)) (anchor-column (save-excursion (goto-char rrm/anchor) (current-column))) - (anchor-line (save-excursion (goto-char rrm/anchor) (line-number-at-pos))) + (anchor-line (save-excursion (goto-char rrm/anchor) (mc/line-number-at-pos))) (left-column (if (< point-column anchor-column) point-column anchor-column)) (right-column (if (> point-column anchor-column) point-column anchor-column)) (navigation-step (if (< point-line anchor-line) 1 -1))) @@ -85,7 +85,7 @@ an exceedingly quick way of adding multiple cursors to multiple lines." (set-mark (point)) (move-to-column point-column) (mc/save-excursion - (while (not (= anchor-line (line-number-at-pos))) + (while (not (= anchor-line (mc/line-number-at-pos))) (forward-line navigation-step) (move-to-column anchor-column) (when (= anchor-column (current-column))