From e950b705540c5371ef1d7efd6bce9d8e36d62f3f Mon Sep 17 00:00:00 2001 From: Thomas Ulmer Date: Sat, 27 Apr 2024 16:46:22 -0700 Subject: emacs config cleanup --- emacs/personal-lisp/custom-funcs.el | 127 ++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 emacs/personal-lisp/custom-funcs.el (limited to 'emacs/personal-lisp/custom-funcs.el') diff --git a/emacs/personal-lisp/custom-funcs.el b/emacs/personal-lisp/custom-funcs.el new file mode 100644 index 0000000..79946ff --- /dev/null +++ b/emacs/personal-lisp/custom-funcs.el @@ -0,0 +1,127 @@ + +;; see transparancy in init.el +(defun toggle-frame-transparency () + "Toggle transparency." + (interactive) + (let ((alpha-transparency 90)) + (if (eq alpha-transparency (frame-parameter nil 'alpha-background)) + (set-frame-parameter nil 'alpha-background 100) + (set-frame-parameter nil 'alpha-background alpha-transparency)))) + +(defun wrap-region-paren () + "Wraps the region in parens. +Similar to pressing ( with the region active." + (interactive) + (let ((beg (region-beginning)) + (end (region-end))) + (save-excursion + (goto-char beg) + (insert "(") + (goto-char (+ end 1)) + (insert ")") + ))) + +(defun zap-whitespace () + "zaps the whitespace around the point in both directions" + (interactive) + (push-mark) + (skip-chars-backward " \t\n") + (push-mark) + (skip-chars-forward " \t\n") + (delete-region (mark) (point)) + (pop-mark) + (goto-char (mark)) + (pop-mark)) + +(defun wrap-sexp (&optional arg) + "Wraps the following sexp in a pair of parens. +Places inside the new pair, with prefix arg do the same backwards, pushing the +mark. Does the same of ARG number sexps if given" + (interactive "P") + (message "%s" arg) + (if arg + (progn + (push-mark) + (insert-pair -1 ?\( ?\))) + (insert-pair 1 ?\( ?\)))) + +(defun close-or-kill () + "If this is the sole frame, kill emacs. Otherwise close the frame." + (interactive) + (if (eq 1 (length (frame-list))) + ;; sole frame, exit + (save-buffers-kill-terminal) + (delete-frame))) + +;; indent code when pasting, from +;; https://trey-jackson.blogspot.com/2008/03/emacs-tip-15-indent-yanked-code.html + +;; automatically indenting yanked text if in programming-modes +(defvar yank-indent-modes '(emacs-lisp-mode + c-mode c++-mode + tcl-mode sql-mode + perl-mode cperl-mode + java-mode jde-mode + lisp-interaction-mode + LaTeX-mode TeX-mode + rust-mode + rustic-mode) + "Modes in which to indent regions that are yanked (or yank-popped).") + +(defun yank-advised-indent-function (beg end) + "Do indentation, as long as the region isn't too large. +BEG and END define the region." + (indent-region beg end nil)) + +(defadvice yank (after yank-indent activate) + "If current mode is one of 'yank-indent-modes, indent yanked text (with prefix arg don't indent)." + (if (and (not (ad-get-arg 0)) + (member major-mode yank-indent-modes)) + (let ((transient-mark-mode nil)) + (yank-advised-indent-function (region-beginning) (region-end))))) + +(defadvice yank-pop (after yank-pop-indent activate) + "If current mode is one of 'yank-indent-modes, indent yanked text (with prefix arg don't indent)." + (if (and (not (ad-get-arg 0)) + (member major-mode yank-indent-modes)) + (let ((transient-mark-mode nil)) + (yank-advised-indent-function (region-beginning) (region-end))))) + +(defadvice evil-paste-before (after yank-indent activate) + "If current mode is one of 'yank-indent-modes, indent yanked text (with prefix arg don't indent)." + (if (and (not (ad-get-arg 0)) + (member major-mode yank-indent-modes)) + (let ((transient-mark-mode nil)) + (yank-advised-indent-function (region-beginning) (region-end))))) + +(defadvice evil-paste-after (after yank-indent activate) + "If current mode is one of 'yank-indent-modes, indent yanked text (with prefix arg don't indent)." + (if (and (not (ad-get-arg 0)) + (member major-mode yank-indent-modes)) + (let ((transient-mark-mode nil)) + (yank-advised-indent-function (region-beginning) (region-end))))) + +(defun fold-this-fold-forward (&optional arg) + "Fold forward ARG number of sexps, defaulting to one." + (interactive "p") + (push-mark) + (forward-sexp arg) + (fold-this (mark) (point)) + (pop-mark)) + +(defun fold-this-fold-backward (&optional arg) + "Fold backward ARG number of sexps, defaulting to one." + (interactive "p") + (push-mark) + (backward-sexp arg) + (fold-this (point) (mark)) + (pop-mark)) + +(defun clean-prog-file (buffer) + "Fixes whitespace in buffer" + (save-excursion + (with-current-buffer buffer + (untabify (point-min) (point-max)) + (goto-char (point-min)) + (while (re-search-forward " +$" nil t) + (replace-match "" nil nil))))) -- cgit v1.2.3