]> code.delx.au - dotemacs/blob - lisp/my-defuns.el
rebind M-{ and M-} to jump by blocks in markdown-mode, it has a weird definition...
[dotemacs] / lisp / my-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 (kill-ring-save (line-beginning-position 1)
26 (line-beginning-position 2))
27 (forward-line)
28 (dotimes (ignored arg)
29 (yank))
30 (forward-line (- arg))
31 (back-to-indentation))
32
33 (defun my/git-reset-buffer ()
34 "Runs git-reset to unstage all changes on the current file. Then updates the git-gutter."
35 (interactive)
36 (call-process "git" nil nil nil "reset" (buffer-file-name))
37 (git-gutter)
38 (message "Finished git reset"))
39
40 (defun my/open-line-above (arg)
41 "Open a new line above point with indentation. With ARG insert that many lines."
42 (interactive "*p")
43 (beginning-of-line)
44 (newline arg)
45 (forward-line (- arg))
46 (indent-for-tab-command))
47
48 (defun my/open-line-below (arg)
49 "Open a new line below point with indentation. With ARG insert that many lines."
50 (interactive "*p")
51 (end-of-line)
52 (newline arg)
53 (indent-for-tab-command))
54
55 (defun my/scratch-buffer ()
56 "Create a new scratch buffer to work in. (could be *scratch* - *scratchX*)"
57 (interactive)
58 (let ((n 0)
59 bufname)
60 (while (progn
61 (setq bufname (concat "*scratch"
62 (if (= n 0) "" (int-to-string n))
63 "*"))
64 (setq n (1+ n))
65 (get-buffer bufname)))
66 (switch-to-buffer (get-buffer-create bufname))
67 (emacs-lisp-mode)))
68
69 (defun my/substitute-line (arg)
70 "Kill the current line and leave point at correct indentation level. With ARG kill that many lines first."
71 (interactive "*p")
72 (beginning-of-line)
73 (kill-line arg)
74 (indent-for-tab-command))
75
76 (defun my/yank (arg)
77 "If the text to be yanked has a newline then move to beginning of line before yanking. Otherwise same as normal `yank'."
78 (interactive "*P")
79 (advice-add 'insert-for-yank :around #'my/yank/advice)
80 (unwind-protect
81 (yank arg)
82 (advice-remove 'insert-for-yank #'my/yank/advice)))
83
84 (defun my/yank/advice (original-function string)
85 (if (string-match-p "\n" string)
86 (beginning-of-line))
87 (funcall original-function string))