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