improve erc-sasl
[Emacs.git] / lisp / init-utils.el
blobed7656ecd9927e0284d15b3e1954f82e4dc2bf31
1 ;;; init-utils.el --- Elisp helper functions and commands -*- lexical-binding: t -*-
2 ;;; Commentary:
3 ;;; Code:
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)))
12 (prog1
13 full-window
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)
21 (when to-restore
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'."
55 (let ((result nil)
56 (pos 0)
57 (group (or group 0)))
58 (while (string-match regex str pos)
59 (push (match-string group str) result)
60 (setq pos (match-end group)))
61 result))
65 ;; Delete the current file
67 (defun delete-this-file ()
68 "Delete the current file, and kill the buffer."
69 (interactive)
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))
75 (kill-this-buffer)))
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)))
88 (unless filename
89 (error "Buffer '%s' is not visiting a file!" name))
90 (progn
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'."
101 (interactive)
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