1 ;;; init-utils.el --- Elisp helper functions and commands -*- lexical-binding: t -*-
5 (defun sanityinc/display-buffer-full-frame
(buffer alist
)
6 "If it's not visible, display buffer full-frame, saving the prior window config.
7 The saved config will be restored when the window is quit later.
8 BUFFER and ALIST are as for `display-buffer-full-frame'."
9 (let ((initial-window-configuration (current-window-configuration)))
10 (or (display-buffer-reuse-window buffer alist
)
11 (let ((full-window (display-buffer-full-frame buffer alist
)))
14 (set-window-parameter full-window
'sanityinc
/previous-config initial-window-configuration
))))))
16 (defun sanityinc/maybe-restore-window-configuration
(orig &optional kill window
)
17 (let* ((window (or window
(selected-window)))
18 (to-restore (window-parameter window
'sanityinc
/previous-config
)))
19 (set-window-parameter window
'sanityinc
/previous-config nil
)
20 (funcall orig kill window
)
22 (set-window-configuration to-restore
))))
24 (advice-add 'quit-window
:around
'sanityinc
/maybe-restore-window-configuration
)
26 (defmacro sanityinc
/fullframe-mode
(mode)
27 "Configure buffers that open in MODE to display in full-frame."
28 `(add-to-list 'display-buffer-alist
29 (cons (cons 'major-mode
,mode
)
30 (list 'sanityinc
/display-buffer-full-frame
))))
32 (sanityinc/fullframe-mode
'package-menu-mode
)
35 ;; Handier way to add modes to auto-mode-alist
36 (defun add-auto-mode (mode &rest patterns
)
37 "Add entries to `auto-mode-alist' to use `MODE' for all given file `PATTERNS'."
38 (dolist (pattern patterns
)
39 (add-to-list 'auto-mode-alist
(cons pattern mode
))))
41 ;; Like diminish, but for major modes
42 (defun sanityinc/set-major-mode-name
(name)
43 "Override the major mode NAME in this buffer."
44 (setq-local mode-name name
))
46 (defun sanityinc/major-mode-lighter
(mode name
)
47 (add-hook (derived-mode-hook-name mode
)
48 (apply-partially 'sanityinc
/set-major-mode-name name
)))
51 ;; String utilities missing from core emacs
53 (defun sanityinc/string-all-matches
(regex str
&optional group
)
54 "Find all matches for `REGEX' within `STR', returning the full match string or group `GROUP'."
58 (while (string-match regex str pos
)
59 (push (match-string group str
) result
)
60 (setq pos
(match-end group
)))
65 ;; Delete the current file
67 (defun delete-this-file ()
68 "Delete the current file, and kill the buffer."
70 (unless (buffer-file-name)
71 (error "No file is currently being edited"))
72 (when (yes-or-no-p (format "Really delete '%s'?"
73 (file-name-nondirectory buffer-file-name
)))
74 (delete-file (buffer-file-name))
79 ;; Rename the current file
81 (if (fboundp 'rename-visited-file
)
82 (defalias 'rename-this-file-and-buffer
'rename-visited-file
)
83 (defun rename-this-file-and-buffer (new-name)
84 "Renames both current buffer and file it's visiting to NEW-NAME."
85 (interactive "sNew name: ")
86 (let ((name (buffer-name))
87 (filename (buffer-file-name)))
89 (error "Buffer '%s' is not visiting a file!" name
))
91 (when (file-exists-p filename
)
92 (rename-file filename new-name
1))
93 (set-visited-file-name new-name
)
94 (rename-buffer new-name
)))))
97 ;; Browse current HTML file
99 (defun browse-current-file ()
100 "Open the current file as a URL using `browse-url'."
102 (let ((file-name (buffer-file-name)))
103 (if (and (fboundp 'tramp-tramp-file-p
)
104 (tramp-tramp-file-p file-name
))
105 (error "Cannot open tramp file")
106 (browse-url (concat "file://" file-name
)))))
109 (provide 'init-utils
)
110 ;;; init-utils.el ends here