summaryrefslogtreecommitdiff
path: root/emacs/personal-lisp/custom-funcs.el
blob: 79946ff1c9847d680a8fd0d4f0c0f9ccca0fc836 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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)))))