1 ;;; -*- lexical-binding: t -*-
5 (defun my/copy-line (arg)
6 "Copy lines in the kill ring"
8 (kill-ring-save (line-beginning-position)
9 (line-beginning-position (+ 1 arg)))
10 (message "%d line%s copied" arg (if (= 1 arg) "" "s")))
12 (defun my/generate-frame-title ()
13 (if (buffer-file-name)
15 (file-name-nondirectory (buffer-file-name))
16 (if (buffer-modified-p)
19 (abbreviate-file-name (substring (file-name-directory (buffer-file-name)) 0 -1))
24 (if (buffer-modified-p)
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)))
32 (defun my/frame-list-ignoring-initial-frame ()
33 (filtered-frame-list (lambda (frame) (not (my/frame-initial-frame-p frame)))))
35 (defun my/git-reset-buffer ()
37 (call-process "git" nil nil nil "reset" (buffer-file-name))
40 (defun my/kill-buffer-safely (buffer)
41 "Kill the buffer if it is not special or modified"
43 (not (string-match "^ " (buffer-name buffer)))
44 (not (equal "*Messages*" (buffer-name buffer)))
46 (not (buffer-modified-p buffer))
47 (null (buffer-file-name buffer))))
48 (kill-buffer buffer)))
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))))
56 (defun my/kill-buffers-not-in-frame ()
57 "Kill buffers which are not loaded into some frame"
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)))
68 (defun my/open-line-above ()
69 "Open a new line above point with indentation"
74 (indent-for-tab-command))
76 (defun my/open-line-below ()
77 "Open a new line below point with indentation"
81 (indent-for-tab-command))
83 (defun my/scratch-buffer ()
84 "Create a new scratch buffer to work in. (could be *scratch* - *scratchX*)"
89 (setq bufname (concat "*scratch"
90 (if (= n 0) "" (int-to-string n))
93 (get-buffer bufname)))
94 (switch-to-buffer (get-buffer-create bufname))
98 (defun my/toggle-comment-on-line ()
99 "Toggles the comment on for the active region if present or the current line otherwise."
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)))
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"))))