ld-emacs/site-lisp/extensions-local/ld-text-operations.el
2023-12-23 16:24:30 +08:00

110 lines
3.2 KiB
EmacsLisp

;; -*- coding: utf-8; -*-
;;; Require:
;;; Code:
; --- move lines
(defun ld-move-text-internal (arg)
(cond
((and mark-active transient-mark-mode)
(if (> (point) (mark))
(exchange-point-and-mark))
(let ((column (current-column))
(text (delete-and-extract-region (point) (mark))))
(forward-line arg)
(move-to-column column t)
(set-mark (point))
(insert text)
(exchange-point-and-mark)
(setq deactivate-mark nil)))
(t
(beginning-of-line)
(when (or (> arg 0) (not (bobp)))
(forward-line)
(when (or (< arg 0) (not (eobp)))
(transpose-lines arg))
(forward-line -1)))))
(defun ld-move-text-down (arg)
"Move region (transient-mark-mode active) or current line
arg lines down."
(interactive "*p")
(ld-move-text-internal arg))
(defun ld-move-text-up (arg)
"Move region (transient-mark-mode active) or current line
arg lines up."
(interactive "*p")
(ld-move-text-internal (- arg)))
; --- duplicate line
(defun ld-get-positions-of-line-or-region ()
"Return positions (beg . end) of the current line or region."
(let (beg end)
(if (and mark-active (> (point) (mark)))
(exchange-point-and-mark))
(setq beg (line-beginning-position))
(if mark-active
(exchange-point-and-mark))
(setq end (line-end-position))
(cons beg end)))
(defun ld-duplicate-current-line-or-region (arg)
"Duplicates the current line or region ARG times.
If there's no region, the current line will be duplicated. However, if
there's a region, all lines that region covers will be duplicated."
(interactive "p")
(pcase-let* ((origin (point))
(`(,beg . ,end) (ld-get-positions-of-line-or-region))
(region (buffer-substring-no-properties beg end)))
(dotimes (_i arg)
(goto-char end)
(unless (use-region-p)
(newline))
(insert region)
(setq end (point)))
(goto-char (+ origin (* (length region) arg) arg))))
(defun ld-duplicate-and-comment-current-line-or-region (arg)
"Duplicates and comments the current line or region ARG times.
If there's no region, the current line will be duplicated. However, if
there's a region, all lines that region covers will be duplicated."
(interactive "p")
(pcase-let* ((origin (point))
(`(,beg . ,end) (ld-get-positions-of-line-or-region))
(region (buffer-substring-no-properties beg end)))
(comment-or-uncomment-region beg end)
(setq end (line-end-position))
(dotimes (_ arg)
(goto-char end)
(unless (use-region-p)
(newline))
(insert region)
(setq end (point)))
(goto-char (+ origin (* (length region) arg) arg))))
; ---
(defun ld-delete-current-line ()
"Delete (not kill) the current line."
(interactive)
(save-excursion
(delete-region
(progn (forward-visible-line 0) (point))
(progn (forward-visible-line 1) (point)))))
; ---
(defun ld-mark-line ()
"Mark one whole line, similar to `mark-paragraph'."
(interactive)
(beginning-of-line)
(if mark-active
(exchange-point-and-mark)
(push-mark nil nil t))
(forward-line)
(exchange-point-and-mark))
(provide 'ld-text-operations)
;;; ld-text-operations.el ends here