]> code.delx.au - dotemacs/blob - lisp/my-defuns.el
xterm frame title support
[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/kill-buffer-safely (buffer)
34 "Kill the buffer if it is not special or modified"
35 (if (and
36 (not (string-match "^ " (buffer-name buffer)))
37 (not (equal "*Messages*" (buffer-name buffer)))
38 (or
39 (not (buffer-modified-p buffer))
40 (null (buffer-file-name buffer))))
41 (kill-buffer buffer)))
42
43 (defun my/kill-buffers-if-deleting-last-frame (frame)
44 "Kill all buffers when closing the last frame"
45 (when (equal (list frame) (my/frame-list-ignoring-initial-frame))
46 (dolist (buffer (buffer-list))
47 (my/kill-buffer-safely buffer))))
48
49 (defun my/kill-buffers-not-in-frame ()
50 "Kill buffers which are not loaded into some frame"
51 (interactive)
52 (let ((kill-count 0))
53 (dolist (buffer (buffer-list))
54 (let* ((window (get-buffer-window buffer t))
55 (frame (window-frame window)))
56 (if (or (null frame) (not (window-live-p window)) (my/frame-initial-frame-p frame))
57 (if (my/kill-buffer-safely buffer)
58 (cl-incf kill-count)))))
59 (message "Killed %d buffers" kill-count)))
60
61 (defun my/open-line-above ()
62 "Open a new line above point with indentation"
63 (interactive)
64 (beginning-of-line)
65 (newline)
66 (forward-line -1)
67 (indent-for-tab-command))
68
69 (defun my/open-line-below ()
70 "Open a new line below point with indentation"
71 (interactive)
72 (end-of-line)
73 (newline)
74 (indent-for-tab-command))
75
76 (defun my/scratch-buffer ()
77 "Create a new scratch buffer to work in. (could be *scratch* - *scratchX*)"
78 (interactive)
79 (let ((n 0)
80 bufname)
81 (while (progn
82 (setq bufname (concat "*scratch"
83 (if (= n 0) "" (int-to-string n))
84 "*"))
85 (setq n (1+ n))
86 (get-buffer bufname)))
87 (switch-to-buffer (get-buffer-create bufname))
88 (emacs-lisp-mode)
89 ))
90
91 (defun my/toggle-comment-on-line ()
92 "Toggles the comment on for the active region if present or the current line otherwise."
93 (interactive)
94 (if (and mark-active transient-mark-mode)
95 (comment-or-uncomment-region (region-beginning) (region-end))
96 (comment-or-uncomment-region (line-beginning-position) (line-end-position)))
97 (forward-line))
98
99 (defun my/terminal-update-title ()
100 "If using a terminal frame then sends the escape codes to update the title."
101 (if (eq 'terminal-init-xterm (terminal-parameter (frame-terminal) 'terminal-initted))
102 (send-string-to-terminal (concat "\033]0;" (my/generate-frame-title) "\007"))))
103
104 (defun my/use-eslint-from-node-modules ()
105 (let* ((root (locate-dominating-file
106 (or (buffer-file-name) default-directory)
107 "node_modules"))
108 (eslint (and root
109 (expand-file-name "node_modules/.bin/eslint" root))))
110 (when (and eslint (file-executable-p eslint))
111 (setq-local flycheck-javascript-eslint-executable eslint))))