Speed up duplicate-line by a factor of 2
authorMattias Engdegård <mattiase@acm.org>
Tue, 27 Jun 2023 15:00:11 +0000 (17:00 +0200)
committerMattias Engdegård <mattiase@acm.org>
Tue, 27 Jun 2023 15:05:54 +0000 (17:05 +0200)
* lisp/misc.el (duplicate-line): Add the newline to the string to be
inserted instead of inserting it separately.
This makes duplicate-line as fast as duplicate-dwim with a contiguous
region.  Both could easily be made faster yet by making the code more
complex.

lisp/misc.el

index 81769696f956cbbd21655c20feb0d0bb8eed632c..de82b97fa6f64f88e6990e1cc09db49d4fbe7d15 100644 (file)
@@ -71,13 +71,15 @@ Also see the `copy-from-above-command' command."
   (interactive "p")
   (unless n
     (setq n 1))
-  (let ((line (buffer-substring (line-beginning-position) (line-end-position))))
+  (let ((line (concat (buffer-substring (line-beginning-position)
+                                        (line-end-position))
+                      "\n")))
     (save-excursion
       (forward-line 1)
       (unless (bolp)
         (insert "\n"))
       (dotimes (_ n)
-        (insert line "\n")))))
+        (insert line)))))
 
 (declare-function rectangle--duplicate-right "rect" (n))