my-misc-defuns.el (7013B)
1 ;;; my-misc-defuns.el --- My miscellaneous functions -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2020 Jamie Beardslee 4 5 ;; Author: Jamie Beardslee <jdb@jamzattack.xyz> 6 ;; Version: 2021.11.15 7 ;; Keywords: 8 9 ;; This program is free software; you can redistribute it and/or modify 10 ;; it under the terms of the GNU General Public License as published by 11 ;; the Free Software Foundation, either version 3 of the License, or 12 ;; (at your option) any later version. 13 14 ;; This program is distributed in the hope that it will be useful, 15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 ;; GNU General Public License for more details. 18 19 ;; You should have received a copy of the GNU General Public License 20 ;; along with this program. If not, see <https://www.gnu.org/licenses/>. 21 22 ;;; Commentary: 23 24 ;; This is the file in which I put various uncategorised functions. 25 26 ;;; Code: 27 28 (require 'simple) 29 (require 'find-dired) 30 (require 'eww) 31 (require 'thingatpt) 32 (require 'url-util) 33 34 ;;;###autoload 35 (defun system-apropos (search) 36 "Run the \"apropos\" comamnd with search term SEARCH." 37 (interactive (list (read-string "Apropos (regex): "))) 38 (let* ((program (or (executable-find "apropos") 39 (user-error "`apropos' must be installed, usually packaged with man"))) 40 (buffer-name (format "*System Apropos %s*" search)) 41 (buffer (get-buffer-create buffer-name))) 42 (with-current-buffer buffer 43 (let ((inhibit-read-only t)) 44 (dolist (line (process-lines program "-l" search)) 45 (when (string-match "\\(.*\\) - " line) 46 (insert line) 47 (make-button (line-beginning-position) (line-end-position) 48 'action `(lambda (&rest _ignored) 49 (man ,(match-string 1 line))) 50 'face 'default) 51 (newline)))) 52 (special-mode)) 53 (pop-to-buffer-same-window buffer))) 54 55 56 ;;; Listing fellas 57 ;; These two list-* functions open up a dired buffer with a list of 58 ;; videos/documents. The package `openwith' might be nice, but I just 59 ;; use helm to open files externally. 60 61 ;;;###autoload 62 (defun list-documents (&optional dir) 63 "Using `find-dired', list all ps or pdf files in DIR. 64 If called interactively, prompt for directory. Else, DIR will 65 default to ~/Documents/." 66 (interactive (list (read-directory-name "Find videos where: " "~/Documents/"))) 67 (unless dir 68 (setq dir "~/Documents/")) 69 (find-dired dir 70 "-regex \".*\\\\.\\\\(ps\\\\|pdf\\\\)\"") 71 (dired-hide-details-mode t) 72 (setq truncate-lines t)) 73 74 ;;;###autoload 75 (defun list-videos (&optional dir) 76 "Using `find-dired', list all the videos in DIR. 77 If called interactively, prompt for directory. Else, DIR will 78 default to ~/Downloads/." 79 (interactive (list (read-directory-name "Find videos where: " "~/Downloads/"))) 80 (unless dir 81 (setq dir "~/Downloads/")) 82 (find-dired dir 83 "-regex \".*\\\\.\\\\(mkv\\\\|mp4\\\\|webm\\\\|avi\\\\|m4v\\\\)\"") 84 (dired-hide-details-mode t) 85 (setq truncate-lines t)) 86 87 88 ;;; Typesetting fellas 89 90 ;;;###autoload 91 (defun open-pdf-of-current-file () 92 "Open a pdf file of the same name as the current file. 93 May be useful for typesetting programs such as LaTeX, lilypond, 94 ox-latex, etc." 95 (interactive) 96 (find-file-other-window (concat 97 (file-name-sans-extension buffer-file-name) 98 ".pdf"))) 99 100 ;;;###autoload 101 (defun eww-open-html-of-current-file () 102 "Open an html file of the same name as the current file. 103 May be useful for writing in org-mode and exporting to html." 104 (interactive) 105 (eww-open-file (concat 106 (file-name-sans-extension buffer-file-name) 107 ".html"))) 108 109 110 111 ;;;###autoload 112 (defun indent-region-or-defun-please (&optional whole-buffer) 113 "Indent region if it is active, otherwise indent defun. 114 With prefix arg WHOLE-BUFFER, indent the whole buffer." 115 (interactive "*P") 116 (let ((bounds (cond 117 (whole-buffer 118 (cons (point-min) (point-max))) 119 ((region-active-p) 120 (car (region-bounds))) 121 (t (or (bounds-of-thing-at-point 'defun) 122 (cons (save-excursion 123 (backward-paragraph 1) 124 (point)) 125 (save-excursion 126 (forward-paragraph 1) 127 (point)))))))) 128 (indent-region (car bounds) (cdr bounds)))) 129 130 131 132 ;;;###autoload 133 (defun audacity (&rest args) 134 "Start up audacity, the audio editor. 135 This runs in a modified environment, with all environment 136 variables related to input method removed. This is because 137 audacity is buggy with these variables. All ARGS are passed onto 138 Audacity." 139 (interactive) 140 (let ((process-environment 141 (cl-remove-if 142 (lambda (string) 143 (string-match-p "\\(IM_MODULE\\|XMODIFIERS\\)" string)) 144 process-environment))) 145 (make-process 146 :name "audacity" 147 :buffer " audacity" 148 :command (cons "audacity" args)))) 149 150 ;;;###autoload 151 (fset 'eshell/audacity #'audacity) 152 153 154 155 ;;;###autoload 156 (defun copy-gpl-here () 157 "Copy the GPL into this directory." 158 (interactive) 159 (if (file-exists-p "LICENSE") 160 (user-error "File \"LICENSE\" already exists")) 161 (with-temp-file "LICENSE" 162 (insert-file-contents 163 (expand-file-name "COPYING" data-directory)))) 164 165 166 167 ;;;###autoload 168 (defun jamzattack-pastebin (beg end &optional name) 169 "Upload the active region to a personal pastebin. 170 The contents of the region BEG and END will be uploaded, or the 171 whole buffer if the region is inactive. The file saved is named 172 NAME, or randomly generated if left empty. Save the resulting 173 url in the kill ring." 174 (interactive (let ((string 175 (read-string "Paste name (leave empty for autogen): "))) 176 (if (region-active-p) 177 (list (region-beginning) (region-end) string) 178 (list nil nil string)))) 179 (let* ((string 180 (buffer-substring (or beg (point-min)) 181 (or end (point-max)))) 182 (dir "/ssh:jamzattack.xyz:/var/www/html/tmp/") 183 (file 184 (if (string-empty-p name) 185 (make-temp-file dir nil ".txt" string) 186 (expand-file-name (if (string-match-p "\\.[a-zA-Z]+\\'" name) 187 name 188 (concat name ".txt")) 189 dir))) 190 (url 191 (url-encode-url 192 (format "https://jamzattack.xyz/tmp/%s" 193 (file-name-nondirectory file))))) 194 (with-temp-file file 195 (insert string)) 196 (kill-new url) 197 (message "Pasted to: %s" url))) 198 199 200 ;;;###autoload 201 (defun find-file-in-source-directory (file &optional position) 202 "Find the current emacs file in the source directory. 203 This should work for any file installed with emacs." 204 (interactive (list (buffer-file-name) (point))) 205 (let* ((installation-directory 206 (or installation-directory 207 (expand-file-name (format "../share/emacs/%s" emacs-version) 208 invocation-directory))) 209 (relative (file-relative-name file installation-directory)) 210 (new (replace-regexp-in-string "\\.gz\\'" "" 211 (expand-file-name relative source-directory)))) 212 (if (file-exists-p new) 213 (progn (find-file new) 214 (goto-char position)) 215 (user-error "File `%s' not exist" new)))) 216 217 (provide 'my-misc-defuns) 218 ;;; my-misc-defuns.el ends here