]> code.delx.au - dotemacs/blob - lisp/my-editing-defuns.el
8e8ace2e90ae557bf4c324615eca088e1d3a8caa
[dotemacs] / lisp / my-editing-defuns.el
1 ;;; -*- lexical-binding: t -*-
2
3 (defun my/comment-dwim (arg)
4 "Toggles the comment on for the active region if present or the current line otherwise."
5 (interactive "*p")
6 (cond
7 ((and mark-active transient-mark-mode)
8 (let ((start (save-excursion (goto-char (region-beginning)) (line-beginning-position)))
9 (end (save-excursion (goto-char (region-end)) (line-end-position))))
10 (comment-or-uncomment-region start end)))
11 (t
12 (comment-or-uncomment-region (line-beginning-position) (line-end-position arg))
13 (forward-line arg))))
14
15 (defun my/copy-line (arg)
16 "Copy the current line into the kill ring. With ARG copies that many lines."
17 (interactive "*p")
18 (kill-ring-save (line-beginning-position 1)
19 (line-beginning-position (+ 1 arg)))
20 (message "Copied %d lines" arg))
21
22 (defun my/duplicate-line (arg)
23 "Duplicate current line, leaving point in lower line. With ARG duplicates the line that many lines."
24 (interactive "*p")
25 (let* ((start (line-beginning-position 1))
26 (end (line-beginning-position 2))
27 (at-eof (= end (line-end-position) (point-max))))
28 (kill-ring-save start end)
29 (when at-eof
30 (kill-append "\n" t))
31 (save-excursion
32 (forward-line)
33 (dotimes (ignored arg)
34 (yank)))
35 (forward-line)
36 (back-to-indentation)))
37
38 (defun my/open-line-above (arg)
39 "Open a new line above point with indentation. With ARG insert that many lines."
40 (interactive "*p")
41 (beginning-of-line)
42 (newline arg)
43 (forward-line (- arg))
44 (indent-for-tab-command))
45
46 (defun my/open-line-below (arg)
47 "Open a new line below point with indentation. With ARG insert that many lines."
48 (interactive "*p")
49 (end-of-line)
50 (newline arg)
51 (indent-for-tab-command))
52
53 (defun my/substitute-line (arg)
54 "Kill the current line and leave point at correct indentation level. With ARG kill that many lines first."
55 (interactive "*P")
56 (beginning-of-line)
57 (if (not (and (null arg) (equal (line-beginning-position) (line-end-position))))
58 (kill-line arg))
59 (if (not (string-equal major-mode "fundamental-mode"))
60 (indent-for-tab-command)))
61
62 (defun my/yank (arg)
63 "If the text to be yanked has a newline then move to beginning of line before yanking. Otherwise same as normal `yank'."
64 (interactive "*P")
65 (advice-add 'insert-for-yank :around #'my/yank/advice)
66 (unwind-protect
67 (yank arg)
68 (advice-remove 'insert-for-yank #'my/yank/advice)))
69
70 (defun my/yank/advice (original-function string)
71 (if (string-match-p "\n" string)
72 (beginning-of-line))
73 (funcall original-function string))