Skip to content

Instantly share code, notes, and snippets.

@julianeon
Last active November 2, 2023 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julianeon/11f71c1a860c292b1699a9f75a6c3e0e to your computer and use it in GitHub Desktop.
Save julianeon/11f71c1a860c292b1699a9f75a6c3e0e to your computer and use it in GitHub Desktop.
Helper functions I used in Emacs. Most were written by me but some were copied from the Internet.
(defun insert-and-indent-line-above ()
(interactive)
(push-mark)
(let*
((ipt (progn (back-to-indentation) (point)))
(bol (progn (move-beginning-of-line 1) (point)))
(indent (buffer-substring bol ipt)))
(newline)
(previous-line)
(insert indent)))
(defun insert-and-indent-line-below ()
(interactive)
(push-mark)
(let*
((ipt (progn (back-to-indentation) (point)))
(bol (progn (move-beginning-of-line 1) (point)))
(indent (buffer-substring bol ipt)))
(newline)
(next-line)
(insert indent)))
(defun backward-kill-line (arg)
"Kill ARG lines backward."
(interactive "p")
(kill-line (- 1 arg)))
(defun vim-open-line-above ()
"Insert a newline above the current line and put point at beginning."
(interactive)
(unless (bolp)
(beginning-of-line))
(newline)
(forward-line -1)
(indent-according-to-mode))
(defun vim-open-line-below ()
"Insert a newline below the current line and put point at beginning."
(interactive)
(unless (eolp)
(end-of-line))
(newline-and-indent))
(defun my-toggle-window-split ()
"Vertical split shows more of each line, horizontal split shows more lines."
(interactive)
(if (= (count-windows) 2)
(let* ((this-win-buffer (window-buffer))
(next-win-buffer (window-buffer (next-window)))
(this-win-edges (window-edges (selected-window)))
(next-win-edges (window-edges (next-window)))
(this-win-2nd (not (and (<= (car this-win-edges)
(car next-win-edges))
(<= (cadr this-win-edges)
(cadr next-win-edges)))))
(splitter
(if (= (car this-win-edges)
(car (window-edges (next-window))))
'split-window-horizontally
'split-window-vertically)))
(delete-other-windows)
(let ((first-win (selected-window)))
(funcall splitter)
(if this-win-2nd (other-window 1))
(set-window-buffer (selected-window) this-win-buffer)
(set-window-buffer (next-window) next-win-buffer)
(select-window first-win)
(if this-win-2nd (other-window 1))))))
(defun kill-other-buffers ()
"Kill all other buffers."
(interactive)
(mapc 'kill-buffer
(delq (current-buffer)
(remove-if-not 'buffer-file-name (buffer-list)))))
(defun embiggen ()
(interactive)
(set-face-attribute 'default nil :height 300))
(defun emsmallen ()
(interactive)
(set-face-attribute 'default nil :height 200))
(defun melpa ()
(interactive)
(list-packages))
(defun theme-manoj ()
(interactive)
(load-theme 'manoj-dark t))
(defun theme-leuven ()
(interactive)
(load-theme 'leuven t)
(set-face-attribute 'default nil :height 300))
(defun theme-deeper-blue ()
(interactive)
(load-theme 'deeper-blue t)
(set-face-attribute 'default nil :height 300))
(defun keep-num ()
(interactive)
(goto-char 1)
(keep-lines "^[0-9]*:[678][0-9][0-9][0-9]"))
(defun forward-five ()
(interactive)
(forward-line 5))
(defun backward-five ()
(interactive)
(forward-line -5))
(defun forward-ten ()
(interactive)
(forward-line 10))
(defun backward-ten ()
(interactive)
(forward-line -10))
(defun kill-three ()
(interactive)
(kill-whole-line 3))
(defun kill-four ()
(interactive)
(kill-whole-line 4))
(defun print-region-boundary (x y)
"Prints region start and end positions"
(interactive "r")
(message "Region begin at: %d, end at: %d" x y))
(defun wcnt (start end)
"Word count"
(interactive "r")
(save-excursion
(let ((n 0))
(goto-char start)
(while (< (point) end)
(if (forward-word 1)
(setq n (1+ n))))
(message "Region has %d words" n)
n)))
(defun lcnt (start end)
"Line count"
(interactive "r")
(save-excursion
(let ((n 0))
(goto-char start)
(while (< (point) end)
(if (forward-line 1)
(setq n (1+ n))))
(message "Region has %d lines" n)
n)))
(defun gotoer (x y)
"Prints region start and end positions"
(interactive "r")
(if (use-region-p)
(progn
(setq text (buffer-substring (region-beginning) (region-end)))
(shell-command-to-string (concat openg " " text)))
(message "no text")))
(defun killn (&optional n)
(interactive "p")
(save-excursion
(beginning-of-line)
(let ((kill-whole-line t))
(kill-line n))))
(defvar coindeskbtc "https://api.coindesk.com/v1/bpi/currentprice.json")
(defvar minbtc "https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=BTC,USD,EUR")
(defvar mineth "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=BTC,USD,EUR")
(defvar minxrp "https://min-api.cryptocompare.com/data/price?fsym=XRP&tsyms=BTC,USD,EUR")
(defun xbtc ()
(with-current-buffer
(url-retrieve-synchronously coindeskbtc)
(goto-char (point-min))
(re-search-forward "^$")
(delete-region (point) (point-min))
(format "%s" (assoc 'rate_float (assoc 'USD (assoc 'bpi (json-read-from-string (buffer-string))))))))
(defun ucrypto (url-crypto)
(with-current-buffer
(url-retrieve-synchronously url-crypto)
(goto-char (point-min))
(re-search-forward "^$")
(delete-region (point) (point-min))
(format "%s" (assoc 'USD (json-read-from-string (buffer-string))))))
(defun ubtc ()
(ucrypto minbtc))
(defun ueth ()
(ucrypto mineth))
(defun uxrp ()
(ucrypto minxrp))
(defun reqqero ()
(interactive)
(progn (request
"http://httpbin.org/get"
:params '(("key" . "value") ("key2" . "value2"))
:parser 'json-read
:success (cl-function
(lambda (&key data &allow-other-keys)
(message "I sent: %S" (assoc-default 'args data)))))))
(defun insert-image (&optional url)
(interactive)
(unless url (setq url (url-get-url-at-point)))
(unless url
(error "Couldn't find URL."))
(let ((buffer (url-retrieve-synchronously url)))
(unwind-protect
(let ((data (with-current-buffer buffer
(goto-char (point-min))
(search-forward "\n\n")
(buffer-substring (point) (point-max)))))
(insert-image (create-image data nil t)))
(kill-buffer buffer))))
(defun open-line-up ()
"Insert a newline above the current line and put point at beginning."
(interactive)
(unless (bolp)
(beginning-of-line))
(newline)
(forward-line -1)
(indent-according-to-mode))
(defun open-line-down ()
"Insert a newline below the current line and put point at beginning."
(interactive)
(unless (eolp)
(end-of-line))
(newline-and-indent))
(defun insert-p-tag ()
"Insert <p></p> at cursor point."
(interactive)
(insert "<p></p>")
(backward-char 6))
(defun ask-name-and-age (x y)
"Ask name and age"
(interactive "sEnter you name:
sEnter your age: ")
(insert (format "Name is: %s" concat(x y))))
(random t) ; seed it randomly
(defun insert-random-number ()
"Insert a random number between 0 to 999999."
(interactive)
(insert (number-to-string (random 999999))) )
(defun hit ()
(insert (format "Hi,\n\nThanks for writing in. ")))
(defun hitn (name)
(insert (format "Hi %s,\n\nThanks for writing in. " name)))
(defun hitnc (name)
(insert (format "Hi %s,\n\nThanks for writing in. " (capitalize-word name))))
(defun date () (interactive)
(insert (shell-command-to-string "echo -n $(date +%m-%d-%Y)")))
(defun adderab (a b)
"Sum example."
(interactive
(list
(read-number "First num: ")
(read-number "Second num: ")))
(insert (number-to-string (+ a b))))
(defun hello (someone)
"Say hello to someone."
(interactive "sTo who? ")
(insert (format "Hi %s,\n\n" someone)))
(defun hope (x)
"Hope example."
(interactive "s0. clarifies-other-quest 1. clarifies-else-explain 2. helps-clarify-said 3. info-helps-any-quest 4. helps-quest-shared-here 5. meets-needs-any-quest 6. takes-care-of-request")
(cond
((equal x "0")
(insert (format "I hope this clarifies the situation, but please let me know if you have other questions." )))
((equal x "1")
(insert (format "I hope this clarifies the situation, but please let me know if there's anything else I can explain." )))
((equal x "2")
(insert (format "I hope this helps, but please let me know if I can clarify anything I've said here." )))
((equal x "3")
(insert (format "I hope this information helps, but please let me know if you have any questions about this." )))
((equal x "4")
(insert (format "I hope this information helps, but please let me know if you have any questions about anything I've shared here. " )))
((equal x "5")
(insert (format "I hope this meets your needs, but please let me know if you have any other questions. " )))
((equal x "6")
(insert (format "I hope this takes care of your request, but please let me know if you need anything else. " )))))
(defun glad (x)
"Glad example."
;(interactive "s0. glad-helped 1. glad-resolved 2. happy-hear-worked 3. very-happy-hear-worked 4. glad-helped-else! 5. happy-worked-else ")
(cond
((equal x "0")
(insert (format "I'm glad to hear this helped." )))
((equal x "1")
(insert (format "I'm glad to hear this resolved the issue." )))
((equal x "2")
(insert (format "I'm happy to hear this worked." )))
((equal x "3")
(insert (format "I'm very happy to hear this worked." )))
((equal x "4")
(insert (format "I'm glad to hear this helped. I take it this resolves the issue, but please let me know if you need anything else!" )))
((equal x "5")
(insert (format "I'm happy to hear this worked. I take it this resolves the issue, but please let me know if you need anything else!" )))))
(defun shell-mode-in-new-frame ()
(interactive)
(select-frame (make-frame))
(color-theme-monokai-terminal)
(shell-mode))
(defun closer (x)
"Closing types."
(interactive "s1. Cheers 2. Sincerely 3. Best 4. Yours 5. Cordially ")
(cond
((equal x "1")
(insert (format "Cheers,\nJulian\n" )))
((equal x "2")
(insert (format "Sincerely,\nJulian\n" )))
((equal x "3")
(insert (format "Best,\nJulian\n" )))
((equal x "4")
(insert (format "Yours,\nJulian\n" )))
((equal x "5")
(insert (format "Cordially,\nJulian\n" )))))
(defun reddit ()
(browse-url "http://www.reddit.com/"))
(defun writex (input)
(interactive "MFile: ")
(let ((buf buffer-file-name)
(homeput (concat homevar input ".txt")))
(shell-command (concat "touch " homeput))
(write-file homeput)
(write-file buf)
(message (concat "Wrote " input ".txt and reverted to " buf))
))
(defun wikipedia ()
(interactive)
(let (myWord myUrl)
(setq myWord
(if (use-region-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(thing-at-point 'word)))
(setq myWord (replace-regexp-in-string " " "_" myWord))
(setq myUrl (concat "http://en.wikipedia.org/wiki/" myWord))
;(browse-url myUrl)
(eww myUrl) ; emacs's own browser
))
(defun hn ()
(interactive)
(eww "http://news.ycombinator.com")
)
(defun rreddit (sub)
(interactive "sWhere? ")
(eww (format "http://www.reddit.com/r/%s" sub)))
(defun gotos (fileName)
(shell-command (concat "open -a /Applications/Google\ Chrome.app/ " fileName)))
(defun xclip ()
(interactive)
(shell-command (concat "cat " (buffer-file-name (window-buffer (minibuffer-selected-window))) " | pbcopy")))
(defun timed ()
(interactive)
(current-time-string))
(defun carday ()
(interactive)
(car (cdr (split-string (current-time-string)))))
(defun day ()
(interactive)
(let ((tstr (cdr (split-string (current-time-string)))))
(format "%s %s" (pop tstr) (car tstr))))
(defun clock ()
(interactive)
(let ((cstr (cdr (split-string (current-time-string)))))
(pop cstr)
(pop cstr)
(format "%s" (pop cstr))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment