You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

275 lines
8.5 KiB

;;; 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."
(move-beginning-of-line nil)
(set-mark (point))
(defun fez/toggle-indent ()
"Toggle between indenting with tabs or spaces."
(setq-default indent-tabs-mode (not indent-tabs-mode)))
(defun fez/current-line-empty-p ()
"Checks if the current line is empty or not."
(looking-at-p "[[:space:]]*$")))
(defun fez/current-line-comment-p ()
"Checks if the current line is a commented out one or not."
(let ((a (point)))
(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."
;; Don't mark the line or the comments above the s-expression.
(while (or (fez/current-line-empty-p)
;; Can probably be optimized...
(let* ((start (region-beginning))
(end (region-end))
(overlay (make-overlay start end)))
(overlay-put overlay 'face 'secondary-selection)
(run-with-timer (or timeout 0.2) nil 'delete-overlay overlay)))
(defun fez/insert-keybind ()
"Prompts the user for a key combination like C-h k does, then inserts that key combination."
(insert (concat "(kbd \""
(key-description (read-key-sequence-vector "Key sequence: "))
(defun fez/time-stamp ()
"Insert the current date."
(shell-command "LANG=nb_NO.UTF-8 date +%Y-%m-%d" t))
(defun fez/kill-all-buffers ()
"Kill all buffers other than the currently active one."
(mapc #'kill-buffer (delete (current-buffer) (buffer-list))))
(defun fez/view-kill-ring ()
"Display the kill ring in a buffer."
(get-buffer-create "*kill-buffer*")
(switch-to-buffer-other-window "*kill-buffer*")
(dolist (l (reverse kill-ring))
(insert l)))
(defun fez/lookup-key ()
"Search for KEY in all known keymaps (outputs to *Messages*)"
(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)))))
(defun fez/ttm-upload ()
"Upload file in selected buffer to and output the link in the echo area (and in *Messages*)"
(let ((link (shell-command-to-string (concat "curl -Ffile=@"
" -s"))))
(message "%s" link)
(kill-new link)))
(defun fez/last-occurrence-of-char (c)
"Go to the previous occurrence of C in the current buffer."
(when (equal (following-char) c)
(while (not (equal (following-char)
(defun fez/last-open-paren ()
(fez/last-occurrence-of-char ?\())
(defun fez/send-vterm-command (cmd)
"Executes CMD in a vterm buffer."
(kill-new cmd)
(defun fez/cl ()
"Prepares the environment for Common Lisp development."
(let ((original-buffer (current-buffer)))
(let ((vterm-buffer (current-buffer)))
(fez/send-vterm-command "slystart")
(switch-to-buffer original-buffer)
;; This is probably useless for anyone that doesn't use frames-only-mode
(switch-to-buffer vterm-buffer)))
(sleep-for 1)
(sly-connect "localhost" 4005))
(defmacro fez/save-buffer-excursion (&rest body)
(declare (indent defun))
(let ((original-buffer (gensym)))
`(let ((,original-buffer (current-buffer)))
(switch-to-buffer ,original-buffer))))
(defun fez/goto-first-occurrence (str)
(goto-char (point-min))
(search-forward-regexp str)
(beginning-of-thing 'symbol))
(defmacro with-keybinding (map key command &rest body)
`(let* ((map ,map)
(key ,key)
(newcommand ,command)
(prevbinding (lookup-key map key)))
(define-key map key newcommand)
(define-key map key prevbinding))))
(defun fez/org-roam-new-note ()
"Creates a new org-roam note, and adds an entry to that note in my file."
(let ((category (read-string "Category: ")))
(find-file (concat org-roam-directory "/"))
(fez/goto-first-occurrence (concat "^\* " category))
(with-keybinding selectrum-minibuffer-map (kbd "SPC") (lambda () (interactive (insert-char ?-)))
(setq x-mouse-on t)
(setq x-mouse-value "13")
(defun fez/toggle-x-mouse ()
"Toggle the X mouse."
(setq x-mouse-on (not x-mouse-on))
(shell-command (concat "xinput "
(if x-mouse-on "--enable " "--disable ")
(message (concat "Mouse "
(if x-mouse-on "enabled." "disabled."))))
(defun fez/screenshot ()
"Prompts the user for a filename and saves a screenshot at that path."
(let ((path (read-string "Filename: ")))
(shell-command (concat "import -window root "
(setq current-layout "nous")
(defun fez/swap-keyboard ()
"Swap between my selected keyboard layouts."
(cond ((string= current-layout "nous") (setq current-layout "ru"))
(t (setq current-layout "nous")))
(shell-command (concat "setxkbmap " current-layout)))
(defun fez/man-at-point ()
(let ((word (current-word)))
(if word
(man word)
"Not a word.")))
(defun fez/insert-line ()
(move-beginning-of-line nil)
(open-line 1))
;; TODO: Put this in C-mode or something
(defun fez/include-guard ()
(let ((guard (concat (upcase (file-name-base (buffer-file-name)))
(goto-char (point-min))
(insert (concat "#ifndef "
"\n#define "
(goto-char (point-max))
(insert (concat "\n\n\n#endif /* "
" */\n")))
(forward-line -3))
(defun fez/tex-word-count ()
(concat "detex " (buffer-file-name) " | wc -w"))
0 -1)))
;; 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)
;; Inspiration 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)
(global-set-key (kbd "C-z C-m") 'fez/man-at-point)
(global-set-key (kbd "C-S-o") 'open-line)
(global-set-key (kbd "C-o") 'fez/insert-line)
;; 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.
(defmacro fez/switch-theme (new-theme)
`(lambda ()
(disable-theme (car custom-enabled-themes))
(enable-theme ,new-theme)))
(global-set-key [f5] (fez/switch-theme 'magik))
(global-set-key [f6] (fez/switch-theme 'basic))
(add-hook 'emacs-lisp-mode-hook
(lambda () (local-set-key (kbd "C-c k") #'fez/insert-keybind)))