emacs.d

My Emacs configuration
git clone https://git.jamzattack.xyz/emacs.d
Log | Files | Refs | LICENSE

my-misc-defuns.el (6230B)


      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: 2020.09.10
      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 ;;; Emacs is sorely missing an interface for "apropos".  This is my
     35 ;;; meagre attempt at making it useful within emacs.
     36 ;;;###autoload
     37 (defun system-apropos (search &optional args)
     38   "Run the \"apropos\" comamnd with search term SEARCH and
     39 optional arguments ARGS."
     40   (interactive (list (read-string "Apropos (regex): ")
     41 		     (when current-prefix-arg
     42 		       (read-string "apropos arguments: "))))
     43   (let* ((command (or (executable-find "apropos")
     44 		      (user-error "apropos must be installed, usually packaged with man")))
     45 	 (buffer-name (format "*System Apropos %s*" search))
     46 	 (buffer (or (get-buffer buffer-name)
     47 		     (generate-new-buffer buffer-name))))
     48     (with-current-buffer buffer
     49       (insert
     50        (shell-command-to-string (concat command " " args " " search))))
     51     (switch-to-buffer buffer)))
     52 
     53 
     54 ;;; These two list-* functions open up a dired buffer with a list of
     55 ;;; videos/documents.  The package `openwith' might be nice, but I just
     56 ;;; use helm to open files externally.
     57 ;;;###autoload
     58 (defun list-documents (&optional dir)
     59   "Using `find-dired', list all ps or pdf files in DIR.
     60 If called interactively, prompt for directory.  Else, DIR will
     61 default to ~/Documents/."
     62   (interactive (list (read-directory-name "Find videos where: " "~/Documents/")))
     63   (unless dir
     64     (setq dir "~/Documents/"))
     65   (find-dired dir
     66               "-regex \".*\\\\.\\\\(ps\\\\|pdf\\\\)\"")
     67   (dired-hide-details-mode t)
     68   (setq truncate-lines t))
     69 
     70 ;;;###autoload
     71 (defun list-videos (&optional dir)
     72   "Using `find-dired', list all the videos in DIR.
     73 If called interactively, prompt for directory.  Else, DIR will
     74 default to ~/Downloads/."
     75   (interactive (list (read-directory-name "Find videos where: " "~/Downloads/")))
     76   (unless dir
     77     (setq dir "~/Downloads/"))
     78   (find-dired dir
     79               "-regex  \".*\\\\.\\\\(mkv\\\\|mp4\\\\|webm\\\\|avi\\\\|m4v\\\\)\"")
     80   (dired-hide-details-mode t)
     81   (setq truncate-lines t))
     82 
     83 
     84 ;;; Open the pdf file with the same name as the current buffer.
     85 ;;; Useful for typesetting programs such as LaTeX, lilypond, ox-latex,
     86 ;;; etc.
     87 ;;;###autoload
     88 (defun open-pdf-of-current-file ()
     89   "Opens a pdf file of the same name as the current file"
     90   (interactive)
     91   (find-file-other-window (concat
     92                            (file-name-sans-extension buffer-file-name)
     93                            ".pdf")))
     94 
     95 ;;; Similar to `open-pdf-of-current-file' - but open an html file in
     96 ;;; eww.  Useful for writing in org-mode and exporting to html.
     97 ;;;###autoload
     98 (defun eww-open-html-of-current-file ()
     99   "Opens an html file of the same name as the current file"
    100   (interactive)
    101   (eww-open-file (concat
    102                   (file-name-sans-extension buffer-file-name)
    103                   ".html")))
    104 
    105 
    106 
    107 ;;; If region is active, indent it.  Otherwise, indent defun.
    108 ;;;###autoload
    109 (defun indent-region-or-defun-please (&optional whole-buffer)
    110   "Indent region if it is active, otherwise indent defun.  With
    111 prefix arg, indent the whole buffer."
    112   (interactive "*P")
    113   (let ((bounds (cond
    114   		 (whole-buffer
    115   		  (cons (point-min) (point-max)))
    116   		 ((region-active-p)
    117   		  (car (region-bounds)))
    118   		 (t (or (bounds-of-thing-at-point 'defun)
    119 			(cons (save-excursion
    120 				(backward-paragraph 1)
    121 				(point))
    122 			      (save-excursion
    123 				(forward-paragraph 1)
    124 				(point))))))))
    125     (indent-region (car bounds) (cdr bounds))))
    126 
    127 
    128 
    129 ;;;###autoload
    130 (defun audacity (&rest args)
    131   "Start up audacity, the audio editor.
    132 
    133 This runs in a modified environment, with all environment
    134 variables related to input method removed.  This is because
    135 audacity is buggy with these variables."
    136   (interactive)
    137   (let ((process-environment
    138          (cl-remove-if
    139           (lambda (string)
    140             (string-match-p "\\(IM_MODULE\\|XMODIFIERS\\)" string))
    141           process-environment)))
    142     (make-process
    143      :name "audacity"
    144      :buffer " audacity"
    145      :command `("audacity" ,@args))))
    146 
    147 ;;;###autoload
    148 (fset 'eshell/audacity #'audacity)
    149 
    150 
    151 
    152 ;;;###autoload
    153 (defun copy-gpl-here ()
    154   (interactive)
    155   (if (file-exists-p "LICENSE")
    156       (user-error "File \"LICENSE\" already exists"))
    157   (with-temp-file "LICENSE"
    158     (insert-file-contents
    159      (expand-file-name "COPYING" data-directory))))
    160 
    161 
    162 
    163 ;;;###autoload
    164 (defun jamzattack-pastebin (beg end &optional name)
    165   "Upload the active region to a personal pastebin.
    166 The contents of the region BEG and END will be uploaded, or the
    167 whole buffer if the region is inactive.  Save the resulting url
    168 in the kill ring."
    169   (interactive `(,@(if (region-active-p)
    170 		       (list (region-beginning) (region-end))
    171 		     '(nil nil))
    172 		 ,(read-string "Paste name (leave empty for autogen): ")))
    173   (let* ((string
    174 	  (buffer-substring (or beg (point-min))
    175 			    (or end (point-max))))
    176 	 (dir "/ssh:jamzattack.xyz:/var/www/html/tmp/")
    177 	 (file
    178 	  (if (string-empty-p name)
    179 	      (make-temp-file dir nil ".txt" string)
    180 	    (expand-file-name (concat name ".txt") dir)))
    181 	 (url
    182 	  (url-encode-url
    183 	   (format "https://jamzattack.xyz/tmp/%s"
    184 		   (file-name-nondirectory file)))))
    185     (with-temp-file file
    186       (insert string))
    187     (kill-new url)
    188     (message "Pasted to: %s" url)))
    189 
    190 (provide 'my-misc-defuns)
    191 ;;; my-misc-defuns.el ends here