]> code.delx.au - dotemacs/blobdiff - lisp/my-editing-defuns.el
fix my/duplicate-line when at EOF without newline
[dotemacs] / lisp / my-editing-defuns.el
index 1799fdedba8e32d57eae844c1fc371fe53b17e74..8e8ace2e90ae557bf4c324615eca088e1d3a8caa 100644 (file)
 (defun my/duplicate-line (arg)
   "Duplicate current line, leaving point in lower line. With ARG duplicates the line that many lines."
   (interactive "*p")
-  (kill-ring-save (line-beginning-position 1)
-                  (line-beginning-position 2))
-  (forward-line)
-  (dotimes (ignored arg)
-    (yank))
-  (forward-line (- arg))
-  (back-to-indentation))
+  (let* ((start (line-beginning-position 1))
+         (end (line-beginning-position 2))
+         (at-eof (= end (line-end-position) (point-max))))
+    (kill-ring-save start end)
+    (when at-eof
+      (kill-append "\n" t))
+    (save-excursion
+      (forward-line)
+      (dotimes (ignored arg)
+        (yank)))
+    (forward-line)
+    (back-to-indentation)))
 
 (defun my/open-line-above (arg)
   "Open a new line above point with indentation. With ARG insert that many lines."