.emacs.d/custom.el

138 lines
4.5 KiB
EmacsLisp

;;; Compilation
(setq compile-command "make ")
;; C-m functions as \r, so pressing C-x C-m C-m will call compile and skip the prompt.
(global-set-key (kbd "C-x C-m") 'compile)
;; Nicer movement in compilation-mode.
(add-hook 'compilation-mode-hook
(lambda () (local-set-key (kbd "n") 'compilation-next-error)))
(add-hook 'compilation-mode-hook
(lambda () (local-set-key (kbd "p") 'compilation-previous-error)))
;; Function for selecting the current line.
(defun fez/mark-line ()
"Selects the current line with the mark."
(interactive)
(move-beginning-of-line nil)
(set-mark (point))
(move-end-of-line nil))
(defun fez/toggle-indent ()
"Toggle between indenting with tabs or spaces."
(interactive)
(setq-default indent-tabs-mode (not indent-tabs-mode)))
(defun fez/current-line-empty-p ()
"Checks if the current line is empty or not."
(save-excursion
(beginning-of-line)
(looking-at-p "[[:space:]]*$")))
(defun fez/current-line-comment-p ()
"Checks if the current line is a commented out one or not."
(save-excursion
(beginning-of-line)
(let ((a (point)))
(end-of-line)
(let ((b (point)))
(comment-only-p a b)))))
(defun fez/flash-region (&optional timeout)
"Temporarily highlight region from START to END. Taken from SLIME source code and modified."
(interactive)
(transient-mark-mode)
(save-excursion
(mark-defun)
;; Don't mark the line or the comments above the s-expression.
(while (or (fez/current-line-empty-p)
(fez/current-line-comment-p))
;; Can probably be optimized...
(forward-char))
(let* ((start (region-beginning))
(end (region-end))
(overlay (make-overlay start end)))
(deactivate-mark)
(overlay-put overlay 'face 'secondary-selection)
(run-with-timer (or timeout 0.2) nil 'delete-overlay overlay)))
(transient-mark-mode))
(defun fez/insert-keybind ()
"Prompts the user for a key combination like C-h k does, then inserts that key combination."
(interactive)
(insert (concat "(kbd \""
(key-description (read-key-sequence-vector "Key sequence: "))
"\")")))
(defun fez/time-stamp ()
"Insert the current date."
(interactive)
(shell-command "LANG=nb_NO.UTF-8 date +%d-%m-%Y" t))
(defun fez/kill-all-buffers ()
"Kill all buffers other than the currently active one."
(interactive)
(mapc #'kill-buffer (delete (current-buffer) (buffer-list))))
(defun fez/view-kill-ring ()
"Display the kill ring in a buffer."
(interactive)
(get-buffer-create "*kill-buffer*")
(switch-to-buffer-other-window "*kill-buffer*")
(erase-buffer)
(dolist (l (reverse kill-ring))
(insert l)))
(defun fez/lookup-key ()
"Search for KEY in all known keymaps (outputs to *Messages*)"
(interactive)
(let ((key (read-key-sequence-vector "Key sequence: ")))
(mapatoms (lambda (ob) (when (and (boundp ob) (keymapp (symbol-value ob)))
(when (functionp (lookup-key (symbol-value ob) key))
(message (symbol-name ob)))))
obarray)))
(defun fez/ttm-upload ()
"Upload file in selected buffer to ttm.sh and output the link in the echo area (and in *Messages*)"
(interactive)
(message "%s" (shell-command-to-string (concat "curl -Ffile=@"
(buffer-file-name)
" https://ttm.sh"))))
;; Eshell convinience commands.
(defalias 'open 'find-file-other-window)
(defalias 'clean 'eshell/clear-scrollback)
;;; Keybinds
;; C-x C-z and C-z are normally mapped to suspend-frame, an absolutely useless function.
;; Unbind them before making the keybind useful.
(global-unset-key (kbd "C-x C-z"))
(global-unset-key (kbd "C-z"))
(global-set-key (kbd "C-x C-z") 'fez/mark-line)
;; Better buffer management (smol).
(global-set-key (kbd "C-x C-b") 'bs-show)
;; Insipiration from Nyxt, kill the current buffer.
(global-set-key (kbd "C-x C-k") 'kill-this-buffer)
(global-set-key (kbd "C-c t") 'fez/time-stamp)
;; zap-up-to-char > zap-to-char
(global-set-key (kbd "M-z") 'zap-up-to-char)
;; Binds for switching between light and dark themes.
(global-set-key [f5] (lambda ()
(interactive)
(disable-theme 'basic)
(load-theme 'magik)))
(global-set-key [f6] (lambda ()
(interactive)
(disable-theme 'magik)
(load-theme 'basic)))
(add-hook 'emacs-lisp-mode-hook
(lambda () (local-set-key (kbd "C-c k") #'fez/insert-keybind)))