summaryrefslogtreecommitdiff
path: root/emacs/site-lisp/my-funcs.el
blob: 37a8b7bf6ee6e4b504c8a9bcf5ff51047c772f56 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
;;; Various functions functions that are not part of an external package

(provide 'my-funcs)


;; 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 arg ?\( ?\)))
    (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)))

(defun toggle-window-other-reachability ()
  (interactive)
  (let* ((win (get-buffer-window (current-buffer)))
        (current (window-parameter win 'no-other-window)))
    (set-window-parameter win 'no-other-window (not current))))

;; 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)))))

(if want-evil
    (progn
      (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)))))

(defun open-window-by-buffer (buffer)
  "Move focus to an extant window by the buffer it contains."
  (interactive "bBuffer: ")
  (let ((win (get-buffer-window buffer t)))
    (if win
        (select-window win)
      (switch-to-buffer buffer))))

(defun dired-do-find-marked-files ()
  (interactive)
  (seq-do 'find-file (dired-get-marked-files)))

(defun shorten-path (path)
  "Shortens each directory in PATH except for the last directory to its first character."
  (let* ((components (split-string path "/"))  ; Split the path into components
         (last-component (-last (lambda (s) (not (string-empty-p s))) components))  ; Extract the last component
         (shortened-components  ; Shorten all components except the last
          (mapcar (lambda (comp)
                    (if (or (string-equal comp last-component) (string-empty-p comp))
                        comp
                      (substring comp 0 1)))
                  components)))
    (mapconcat 'identity shortened-components "/")))  ; Recombine components into a path