(require 'cl-lib) (defun my/copy-line (arg) "Copy lines in the kill ring" (interactive "p") (kill-ring-save (line-beginning-position) (line-beginning-position (+ 1 arg))) (message "%d line%s copied" arg (if (= 1 arg) "" "s"))) (defun my/generate-frame-title () (if (buffer-file-name) (concat (file-name-nondirectory (buffer-file-name)) (if (buffer-modified-p) " +") " (" (abbreviate-file-name (substring (file-name-directory (buffer-file-name)) 0 -1)) ") - Emacs" ) (concat (buffer-name) (if (buffer-modified-p) " +") " - Emacs"))) (defun my/frame-initial-frame-p (frame) "Returns true if the given frame is the magic 'initial frame' that always exists in GUI emacs sessions" (equal "initial_terminal" (terminal-name frame))) (defun my/frame-list-ignoring-initial-frame () (filtered-frame-list (lambda (frame) (not (my/frame-initial-frame-p frame))))) (defun my/kill-buffer-safely (buffer) "Kill the buffer if it is not special or modified" (if (and (not (string-match "^ " (buffer-name buffer))) (not (equal "*Messages*" (buffer-name buffer))) (or (not (buffer-modified-p buffer)) (null (buffer-file-name buffer)))) (kill-buffer buffer))) (defun my/kill-buffers-if-deleting-last-frame (frame) "Kill all buffers when closing the last frame" (when (equal (list frame) (my/frame-list-ignoring-initial-frame)) (dolist (buffer (buffer-list)) (my/kill-buffer-safely buffer)))) (defun my/kill-buffers-not-in-frame () "Kill buffers which are not loaded into some frame" (interactive) (let ((kill-count 0)) (dolist (buffer (buffer-list)) (let* ((window (get-buffer-window buffer t)) (frame (window-frame window))) (if (or (null frame) (not (window-live-p window)) (my/frame-initial-frame-p frame)) (if (my/kill-buffer-safely buffer) (cl-incf kill-count))))) (message "Killed %d buffers" kill-count))) (defun my/open-line-above () "Open a new line above point with indentation" (interactive) (beginning-of-line) (newline) (forward-line -1) (indent-for-tab-command)) (defun my/open-line-below () "Open a new line below point with indentation" (interactive) (end-of-line) (newline) (indent-for-tab-command)) (defun my/scratch-buffer () "Create a new scratch buffer to work in. (could be *scratch* - *scratchX*)" (interactive) (let ((n 0) bufname) (while (progn (setq bufname (concat "*scratch" (if (= n 0) "" (int-to-string n)) "*")) (setq n (1+ n)) (get-buffer bufname))) (switch-to-buffer (get-buffer-create bufname)) (emacs-lisp-mode) )) (defun my/toggle-comment-on-line () "Toggles the comment on for the active region if present or the current line otherwise." (interactive) (if (and mark-active transient-mark-mode) (comment-or-uncomment-region (region-beginning) (region-end)) (comment-or-uncomment-region (line-beginning-position) (line-end-position))) (forward-line)) (defun my/terminal-update-title () "If using a terminal frame then sends the escape codes to update the title." (if (terminal-parameter (frame-terminal) 'terminal-initted) (send-string-to-terminal (concat "\033]0;" (my/generate-frame-title) "\007")))) (defun my/use-eslint-from-node-modules () (let* ((root (locate-dominating-file (or (buffer-file-name) default-directory) "node_modules")) (eslint (and root (expand-file-name "node_modules/.bin/eslint" root)))) (when (and eslint (file-executable-p eslint)) (setq-local flycheck-javascript-eslint-executable eslint))))