3 (defun my/copy-line (arg)
4 "Copy lines in the kill ring"
6 (kill-ring-save (line-beginning-position)
7 (line-beginning-position (+ 1 arg)))
8 (message "%d line%s copied" arg (if (= 1 arg) "" "s")))
10 (defun my/generate-frame-title ()
11 (if (buffer-file-name)
13 (file-name-nondirectory (buffer-file-name))
14 (if (buffer-modified-p)
17 (abbreviate-file-name (substring (file-name-directory (buffer-file-name)) 0 -1))
22 (if (buffer-modified-p)
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)))
30 (defun my/frame-list-ignoring-initial-frame ()
31 (filtered-frame-list (lambda (frame) (not (my/frame-initial-frame-p frame)))))
33 (defun my/git-reset-buffer ()
35 (call-process "git" nil nil nil "reset" (buffer-file-name))
38 (defun my/kill-buffer-safely (buffer)
39 "Kill the buffer if it is not special or modified"
41 (not (string-match "^ " (buffer-name buffer)))
42 (not (equal "*Messages*" (buffer-name buffer)))
44 (not (buffer-modified-p buffer))
45 (null (buffer-file-name buffer))))
46 (kill-buffer buffer)))
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))))
54 (defun my/kill-buffers-not-in-frame ()
55 "Kill buffers which are not loaded into some frame"
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)))
66 (defun my/open-line-above ()
67 "Open a new line above point with indentation"
72 (indent-for-tab-command))
74 (defun my/open-line-below ()
75 "Open a new line below point with indentation"
79 (indent-for-tab-command))
81 (defun my/scratch-buffer ()
82 "Create a new scratch buffer to work in. (could be *scratch* - *scratchX*)"
87 (setq bufname (concat "*scratch"
88 (if (= n 0) "" (int-to-string n))
91 (get-buffer bufname)))
92 (switch-to-buffer (get-buffer-create bufname))
96 (defun my/toggle-comment-on-line ()
97 "Toggles the comment on for the active region if present or the current line otherwise."
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)))
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"))))