Merge pull request #358 from renatofdds/master

Faster line-number-at-pos calculation
This commit is contained in:
Magnar Sveen 2019-08-20 09:49:53 +02:00 committed by GitHub
commit b9b851a767
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 8 deletions

View File

@ -54,8 +54,8 @@ other non-nil value will cause short lines to be padded."
(error "Mark a set of lines first")) (error "Mark a set of lines first"))
(mc/remove-fake-cursors) (mc/remove-fake-cursors)
(let* ((col (current-column)) (let* ((col (current-column))
(point-line (line-number-at-pos)) (point-line (mc/line-number-at-pos))
(mark-line (progn (exchange-point-and-mark) (line-number-at-pos))) (mark-line (progn (exchange-point-and-mark) (mc/line-number-at-pos)))
(direction (if (< point-line mark-line) :up :down)) (direction (if (< point-line mark-line) :up :down))
(style (cond (style (cond
;; called from lisp ;; called from lisp
@ -71,7 +71,7 @@ other non-nil value will cause short lines to be padded."
(previous-logical-line 1 nil) (previous-logical-line 1 nil)
(move-to-column col)) (move-to-column col))
;; Add the cursors ;; 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 ;; Pad the line
(when (eq style 'pad) (when (eq style 'pad)
(while (< (current-column) col) (while (< (current-column) col)

View File

@ -579,8 +579,8 @@ If the region is inactive or on a single line, it will behave like
(interactive "P") (interactive "P")
(if (and (use-region-p) (if (and (use-region-p)
(not (> (mc/num-cursors) 1)) (not (> (mc/num-cursors) 1))
(not (= (line-number-at-pos (region-beginning)) (not (= (mc/line-number-at-pos (region-beginning))
(line-number-at-pos (region-end))))) (mc/line-number-at-pos (region-end)))))
(if arg (if arg
(call-interactively 'mc/edit-lines) (call-interactively 'mc/edit-lines)
(call-interactively 'mc/mark-all-in-region)) (call-interactively 'mc/mark-all-in-region))

View File

@ -109,6 +109,19 @@
(and (listp cursor-type) (and (listp cursor-type)
(eq (car cursor-type) 'bar)))) (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) (defun mc/make-cursor-overlay-at-eol (pos)
"Create overlay to look like cursor at end of line." "Create overlay to look like cursor at end of line."
(let ((overlay (make-overlay pos pos nil nil nil))) (let ((overlay (make-overlay pos pos nil nil nil)))

View File

@ -75,9 +75,9 @@ an exceedingly quick way of adding multiple cursors to multiple lines."
(rrm/remove-rectangular-region-overlays) (rrm/remove-rectangular-region-overlays)
(let* ((annoying-arrows-mode nil) (let* ((annoying-arrows-mode nil)
(point-column (current-column)) (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-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)) (left-column (if (< point-column anchor-column) point-column anchor-column))
(right-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))) (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)) (set-mark (point))
(move-to-column point-column) (move-to-column point-column)
(mc/save-excursion (mc/save-excursion
(while (not (= anchor-line (line-number-at-pos))) (while (not (= anchor-line (mc/line-number-at-pos)))
(forward-line navigation-step) (forward-line navigation-step)
(move-to-column anchor-column) (move-to-column anchor-column)
(when (= anchor-column (current-column)) (when (= anchor-column (current-column))