]> code.delx.au - dotemacs/blob - lisp/my-defuns.el
git-gutter
[dotemacs] / lisp / my-defuns.el
1 (require 'cl-lib)
2
3 (defun my/copy-line (arg)
4 "Copy lines in the kill ring"
5 (interactive "p")
6 (kill-ring-save (line-beginning-position)
7 (line-beginning-position (+ 1 arg)))
8 (message "%d line%s copied" arg (if (= 1 arg) "" "s")))
9
10 (defun my/generate-frame-title ()
11 (if (buffer-file-name)
12 (concat
13 (file-name-nondirectory (buffer-file-name))
14 (if (buffer-modified-p)
15 " +")
16 " ("
17 (abbreviate-file-name (substring (file-name-directory (buffer-file-name)) 0 -1))
18 ") - Emacs"
19 )
20 (concat
21 (buffer-name)
22 (if (buffer-modified-p)
23 " +")
24 " - Emacs")))
25
26 (defun my/frame-initial-frame-p (frame)
27 "Returns true if the given frame is the magic 'initial frame' that always exists in GUI emacs sessions"
28 (equal "initial_terminal" (terminal-name frame)))
29
30 (defun my/frame-list-ignoring-initial-frame ()
31 (filtered-frame-list (lambda (frame) (not (my/frame-initial-frame-p frame)))))
32
33 (defun my/git-reset-buffer ()
34 (interactive)
35 (call-process "git" nil nil nil "reset" (buffer-file-name))
36 (git-gutter))
37
38 (defun my/kill-buffer-safely (buffer)
39 "Kill the buffer if it is not special or modified"
40 (if (and
41 (not (string-match "^ " (buffer-name buffer)))
42 (not (equal "*Messages*" (buffer-name buffer)))
43 (or
44 (not (buffer-modified-p buffer))
45 (null (buffer-file-name buffer))))
46 (kill-buffer buffer)))
47
48 (defun my/kill-buffers-if-deleting-last-frame (frame)
49 "Kill all buffers when closing the last frame"
50 (when (equal (list frame) (my/frame-list-ignoring-initial-frame))
51 (dolist (buffer (buffer-list))
52 (my/kill-buffer-safely buffer))))
53
54 (defun my/kill-buffers-not-in-frame ()
55 "Kill buffers which are not loaded into some frame"
56 (interactive)
57 (let ((kill-count 0))
58 (dolist (buffer (buffer-list))
59 (let* ((window (get-buffer-window buffer t))
60 (frame (window-frame window)))
61 (if (or (null frame) (not (window-live-p window)) (my/frame-initial-frame-p frame))
62 (if (my/kill-buffer-safely buffer)
63 (cl-incf kill-count)))))
64 (message "Killed %d buffers" kill-count)))
65
66 (defun my/open-line-above ()
67 "Open a new line above point with indentation"
68 (interactive)
69 (beginning-of-line)
70 (newline)
71 (forward-line -1)
72 (indent-for-tab-command))
73
74 (defun my/open-line-below ()
75 "Open a new line below point with indentation"
76 (interactive)
77 (end-of-line)
78 (newline)
79 (indent-for-tab-command))
80
81 (defun my/scratch-buffer ()
82 "Create a new scratch buffer to work in. (could be *scratch* - *scratchX*)"
83 (interactive)
84 (let ((n 0)
85 bufname)
86 (while (progn
87 (setq bufname (concat "*scratch"
88 (if (= n 0) "" (int-to-string n))
89 "*"))
90 (setq n (1+ n))
91 (get-buffer bufname)))
92 (switch-to-buffer (get-buffer-create bufname))
93 (emacs-lisp-mode)
94 ))
95
96 (defun my/toggle-comment-on-line ()
97 "Toggles the comment on for the active region if present or the current line otherwise."
98 (interactive)
99 (if (and mark-active transient-mark-mode)
100 (comment-or-uncomment-region (region-beginning) (region-end))
101 (comment-or-uncomment-region (line-beginning-position) (line-end-position)))
102 (forward-line))
103
104 (defun my/terminal-update-title ()
105 "If using a terminal frame then sends the escape codes to update the title."
106 (if (terminal-parameter (frame-terminal) 'terminal-initted)
107 (send-string-to-terminal (concat "\033]0;" (my/generate-frame-title) "\007"))))