This is part of the Emacs Starter Kit.
Function definitions
(require 'thingatpt)
(require 'imenu)
Functions to interactively modify Emacs transparency.
(add-to-list 'default-frame-alist '(alpha . (93)))
;; interactive transparency
(defun transparency-set-initial-value ()
"Set initial value of alpha parameter for the current frame"
(interactive)
(if (equal (frame-parameter nil 'alpha) nil)
(set-frame-parameter nil 'alpha 93)))
;; (set-frame-parameter nil 'alpha 100)))
(transparency-set-initial-value)
(defun transparency-set-value (numb)
"Set level of transparency for the current frame"
(interactive "nEnter transparency level in range 0-100: ")
(if (> numb 100)
(message "Error! The maximum value for transparency is 100!")
(if (< numb 0)
(message "Error! The minimum value for transparency is 0!")
(set-frame-parameter nil 'alpha numb))))
;; want to get message to pop up telling me what variable it's at
(defun transparency-increase ()
"Increase level of transparency for the current frame"
(interactive)
(transparency-set-initial-value)
(if (> (frame-parameter nil 'alpha) 0)
(set-frame-parameter nil 'alpha (+ (frame-parameter nil 'alpha) -3))
;; (message "Transparency set to ")
(message "This is a minimum value of transparency!")))
(defun transparency-decrease ()
"Decrease level of transparency for the current frame"
(interactive)
(transparency-set-initial-value)
(if (< (frame-parameter nil 'alpha) 100)
(set-frame-parameter nil 'alpha (+ (frame-parameter nil 'alpha) +3))
(message "This is a minimum value of transparency!")))
;;; Following functions by Chris Beard
;;; wcbeard10@gmail.com
(defun matrixify ()
"Converts an org table to a matrix formatted in LaTeX."
(interactive)
(save-restriction
(narrow-to-region (my-table-top -1) (my-table-top))
(goto-char (point-min))
(while (search-forward-regexp "|\s*$" nil t) (replace-match "\\\\" nil t))
(goto-char (point-min))
(while (search-forward-regexp "^|" nil t) (replace-match " " nil t))
(goto-char (point-min))
(while (search-forward-regexp " | " nil t) (replace-match " & " nil t))
(goto-char (point-min))
(insert "\\\[ \\left(\n\\begin{matrix}\n")
(goto-char (point-max))
(insert "\n\\end{matrix}\n\\right) \\\]")
(goto-char (point-min))
))
(defun xymatrixify ()
"Converts an org table to a matrix formatted in LaTeX."
(interactive)
(save-restriction
(narrow-to-region (my-table-top -1) (my-table-top))
(goto-char (point-min))
(while (search-forward-regexp "|\s*$" nil t) (replace-match "\\\\" nil t))
(goto-char (point-min))
(while (search-forward-regexp "^|" nil t) (replace-match " " nil t))
(goto-char (point-min))
(while (search-forward-regexp " | " nil t) (replace-match " & " nil t))
(goto-char (point-min))
(insert "\\xymatrix\{\n")
(goto-char (point-max))
(insert "\n}")
(goto-char (point-min))
))
(defun i-matrixify (arg)
"Converts an org table to a matrix formatted in LaTeX. Takes an argument to specify delimiter: (, [ and |, etc give matrix with respective delimiters, but no environment enclosure (\\[ \\]), while p, b and v, etc. give same delimiters with the environment. If the delimiter is 'x,' then it will run the xymatrixify function."
(interactive "cEnter matrix delimiter: ")
(setq end-env "")
(setq begin-env "")
(cond
((equal (string arg) "|") (setq delim "v"))
((equal (string arg) "(") (setq delim "p"))
((equal (string arg) "[") (setq delim "b"))
((equal (string arg) "{") (setq delim "B"))
((equal (string arg) "=") (setq delim "V"))
((equal (string arg) " ") (setq delim ""))
(t (progn
(setq begin-env "\\\[\n")
(setq end-env "\n\\\]")
(setq delim (string arg)))))
(if (equal delim "x")
(xymatrixify)
(save-restriction
(narrow-to-region (my-table-top -1) (my-table-top))
(goto-char (point-min))
(while (search-forward-regexp "|\s*$" nil t) (replace-match "\\\\" nil t))
(goto-char (point-min))
(while (search-forward-regexp "^|" nil t) (replace-match " " nil t))
(goto-char (point-min))
(while (search-forward-regexp " | " nil t) (replace-match " & " nil t))
(goto-char (point-min))
(insert (concat begin-env "\\begin{" delim "matrix}\n"))
(goto-char (point-max))
(insert (concat "\n\\end{" delim "matrix}" end-env))
(goto-char (point-min)))))
(defun my-table-up-line (&optional arg)
"Helper function that recursively goes to the beginning of the table if arg is 1 or absent, and to the end of table if -1. Returns final position."
(if arg
(setq val arg)
(setq val 1))
(next-line (* -1 val))
(if (table-line?)
(my-table-up-line val)
(beginning-of-line (1+ val)))
(unless (>= val 0)
(end-of-line))
(point))
(defun my-table-top (&optional arg)
"my-table-up-line function wrapper, which gives error message if command isn't called from within a table"
(interactive)
(if arg
(setq val arg)
(setq val 1))
;; (beginning-of-line)
(if (table-line?)
(my-table-up-line (* 1 val))
(message "Not in table")))
(defun table-line? ()
(save-excursion
(beginning-of-line)
(equal (string (char-after (point))) "|")))
(defun unmatrixify (start end &optional arg)
"Converts a LaTeX matrix to an org table, where arg is the number of lines before and after the matrix that are deleted; default 0. "
(interactive "r")
(if arg
(progn
(setq val arg)
(insert "arg!"))
(setq val 1)) ;; originally 2, testing with 0
(save-excursion
(save-restriction
(narrow-to-region start end)
(goto-char (point-min))
(while (search-forward-regexp "\\\\\\{2\\}" nil t) (replace-match "|" nil t))
(goto-char (point-min))
(while (search-forward-regexp "^ " nil t) (replace-match "| " nil t))
(goto-char (point-min))
(while (search-forward-regexp " & " nil t) (replace-match " | " nil t))
(goto-char (point-min))))
(goto-char end)
(beginning-of-line (if (> val 0)
2
1))
(kill-line val)
(goto-char start)
(kill-line (- 0 val)))
Former python defuns.
;; https://github.com/EnigmaCurry/emacs/blob/master/ryan-python.el
;; http://www.enigmacurry.com/category/emacs/
;;=================
;;===CASE TOGGLE===
;;=================
;; http://xahlee.org/emacs/modernization_upcase-word.html
(defun toggle-letter-case ()
"Toggle the letter case of current word or text selection.
Toggles from 3 cases: UPPER CASE, lower case, Title Case,
in that cyclic order."
(interactive)
(let (pos1 pos2 (deactivate-mark nil) (case-fold-search nil))
(if (and transient-mark-mode mark-active)
(setq pos1 (region-beginning)
pos2 (region-end))
(setq pos1 (car (bounds-of-thing-at-point 'word))
pos2 (cdr (bounds-of-thing-at-point 'word))))
(when (not (eq last-command this-command))
(save-excursion
(goto-char pos1)
(cond
((looking-at "[[:lower:]][[:lower:]]") (put this-command 'state "all lower"))
((looking-at "[[:upper:]][[:upper:]]") (put this-command 'state "all caps") )
((looking-at "[[:upper:]][[:lower:]]") (put this-command 'state "init caps") )
(t (put this-command 'state "all lower") )
)
)
)
(cond
((string= "all lower" (get this-command 'state))
(upcase-initials-region pos1 pos2) (put this-command 'state "init caps"))
((string= "init caps" (get this-command 'state))
(upcase-region pos1 pos2) (put this-command 'state "all caps"))
((string= "all caps" (get this-command 'state))
(downcase-region pos1 pos2) (put this-command 'state "all lower"))
)
)
)
(defun ido-imenu ()
"Update the imenu index and then use ido to select a symbol to navigate to.
Symbols matching the text at point are put first in the completion list."
(interactive)
(imenu--make-index-alist)
(let ((name-and-pos '())
(symbol-names '()))
(flet ((addsymbols (symbol-list)
(when (listp symbol-list)
(dolist (symbol symbol-list)
(let ((name nil) (position nil))
(cond
((and (listp symbol) (imenu--subalist-p symbol))
(addsymbols symbol))
((listp symbol)
(setq name (car symbol))
(setq position (cdr symbol)))
((stringp symbol)
(setq name symbol)
(setq position (get-text-property 1 'org-imenu-marker symbol))))
(unless (or (null position) (null name))
(add-to-list 'symbol-names name)
(add-to-list 'name-and-pos (cons name position))))))))
(addsymbols imenu--index-alist))
;; If there are matching symbols at point, put them at the beginning of `symbol-names'.
(let ((symbol-at-point (thing-at-point 'symbol)))
(when symbol-at-point
(let* ((regexp (concat (regexp-quote symbol-at-point) "$"))
(matching-symbols (delq nil (mapcar (lambda (symbol)
(if (string-match regexp symbol) symbol))
symbol-names))))
(when matching-symbols
(sort matching-symbols (lambda (a b) (> (length a) (length b))))
(mapc (lambda (symbol) (setq symbol-names (cons symbol (delete symbol symbol-names))))
matching-symbols)))))
(let* ((selected-symbol (ido-completing-read "Symbol? " symbol-names))
(position (cdr (assoc selected-symbol name-and-pos))))
(goto-char position))))
(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)
"Modes in which to indent regions that are yanked (or yank-popped)")
(defvar yank-advised-indent-threshold 1000
"Threshold (# chars) over which indentation does not automatically occur.")
(defun yank-advised-indent-function (beg end)
"Do indentation, as long as the region isn't too large."
(if (<= (- end beg) yank-advised-indent-threshold)
(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)))))
;;===============================
;;===Resize windows and frames===
;;===============================
;; some kbd mixup with aquamacs
(defun gcm-scroll-down ()
(interactive)
(View-scroll-line-forward 9))
(defun gcm-scroll-up ()
(interactive)
(View-scroll-line-backward 9))
(defun big-move-down ()
(interactive)
(next-line 9))
(defun big-move-up ()
(interactive)
(previous-line 9))
(global-set-key (kbd "C-c d") 'my-insert-date)
(defun my-insert-date ()
"Insert the current date."
(interactive)
(insert (format-time-string "%d %B %Y")))
(defun insert-date (prefix)
"Insert the current date. With prefix-argument, use ISO format. With
two prefix arguments, write out the day and month name."
(interactive "P")
(let ((format (cond
((not prefix) "%d %B %Y")
((equal prefix '(4)) "%Y-%m-%d")
((equal prefix '(16)) "%A, %d. %B %Y")
))
(system-time-locale "en_EN"))
(insert (format-time-string format))))
(defun select-next-window ()
"Switch to the next window"
(interactive)
(select-window (next-window)))
(defun select-previous-window ()
"Switch to the previous window"
(interactive)
(select-window (previous-window)))
;(add-hook 'org-mode 'color-theme-inkpot)
;;when you try and do ^/_, it automatically includes braces
(setq TeX-electric-sub-and-superscript 1)
;;insert double "{}", hit C-c {
;;typset: C-c C-c
(add-hook 'paragraph-indent-text-mode-hook '(lambda ()
(local-set-key (kbd "RET") 'newline)))
;(setq left-margin 0)
(defun copy-line (&optional arg)
"Do a kill-line but copy rather than kill. This function directly calls
kill-line, so see documentation of kill-line for how to use it including prefix
argument and relevant variables. This function works by temporarily making the
buffer read-only."
(interactive "P")
(let ((buffer-read-only t)
(kill-read-only-ok t))
(kill-line arg)))
;; optional key binding
;; (global-set-key "\C-c\C-k" 'copy-line)
(defun copy-whole-line (&optional arg)
"Do a kill-line but copy rather than kill. This function directly calls
kill-line, so see documentation of kill-line for how to use it including prefix
argument and relevant variables. This function works by temporarily making the
buffer read-only."
(interactive "P")
(let ((buffer-read-only t)
(kill-read-only-ok t))
(kill-whole-line arg)))
;; Count buffers in Emacs
(defun count-buffers (&optional display-anyway)
"Display or return the number of buffers."
(interactive)
(let ((buf-count (length (buffer-list))))
(if (or (interactive-p) display-anyway)
(message "%d buffers in this Emacs" buf-count)) buf-count))
(defun view-url ()
"Open a new buffer containing the contents of URL."
(interactive)
(let* ((default (thing-at-point-url-at-point))
(url (read-from-minibuffer "URL: " default)))
(switch-to-buffer (url-retrieve-synchronously url))
(rename-buffer url t)
(cond ((search-forward "<?xml" nil t) (xml-mode))
((search-forward "<html" nil t) (html-mode)))))
(defun ido-imenu ()
"Update the imenu index and then use ido to select a symbol to navigate to.
Symbols matching the text at point are put first in the completion list."
(interactive)
(imenu--make-index-alist)
(let ((name-and-pos '())
(symbol-names '()))
(flet ((addsymbols (symbol-list)
(when (listp symbol-list)
(dolist (symbol symbol-list)
(let ((name nil) (position nil))
(cond
((and (listp symbol) (imenu--subalist-p symbol))
(addsymbols symbol))
((listp symbol)
(setq name (car symbol))
(setq position (cdr symbol)))
((stringp symbol)
(setq name symbol)
(setq position (get-text-property 1 'org-imenu-marker symbol))))
(unless (or (null position) (null name))
(add-to-list 'symbol-names name)
(add-to-list 'name-and-pos (cons name position))))))))
(addsymbols imenu--index-alist))
;; If there are matching symbols at point, put them at the beginning of `symbol-names'.
(let ((symbol-at-point (thing-at-point 'symbol)))
(when symbol-at-point
(let* ((regexp (concat (regexp-quote symbol-at-point) "$"))
(matching-symbols (delq nil (mapcar (lambda (symbol)
(if (string-match regexp symbol) symbol))
symbol-names))))
(when matching-symbols
(sort matching-symbols (lambda (a b) (> (length a) (length b))))
(mapc (lambda (symbol) (setq symbol-names (cons symbol (delete symbol symbol-names))))
matching-symbols)))))
(let* ((selected-symbol (ido-completing-read "Symbol? " symbol-names))
(position (cdr (assoc selected-symbol name-and-pos))))
(goto-char position))))
We have a number of turn-on-* functions since it’s advised that lambda functions not go in hooks. Repeatedly evaling an add-to-list with a hook value will repeatedly add it since there’s no way to ensure that a lambda doesn’t already exist in the list.
(defun local-column-number-mode ()
(make-local-variable 'column-number-mode)
(column-number-mode t))
(defun local-comment-auto-fill ()
(set (make-local-variable 'comment-auto-fill-only-comments) t)
(auto-fill-mode t))
(defun turn-on-hl-line-mode ()
(if window-system (hl-line-mode t)))
(defun turn-on-save-place-mode ()
(setq save-place t))
(defun turn-on-whitespace ()
(whitespace-mode t))
(add-hook 'coding-hook 'local-column-number-mode)
(add-hook 'coding-hook 'local-comment-auto-fill)
(add-hook 'coding-hook 'turn-on-hl-line-mode)
(add-hook 'coding-hook 'pretty-lambdas)
(defun run-coding-hook ()
"Enable things that are convenient across all coding buffers."
(run-hooks 'coding-hook))
(defun untabify-buffer ()
(interactive)
(untabify (point-min) (point-max)))
(defun indent-buffer ()
(interactive)
(indent-region (point-min) (point-max)))
(defun cleanup-buffer ()
"Perform a bunch of operations on the whitespace content of a buffer."
(interactive)
(indent-buffer)
(untabify-buffer)
(delete-trailing-whitespace))
(defun recentf-ido-find-file ()
"Find a recent file using ido."
(interactive)
(let ((file (ido-completing-read "Choose recent file: " recentf-list nil t)))
(when file
(find-file file))))
(defun pretty-lambdas ()
(font-lock-add-keywords
nil `(("(?\\(lambda\\>\\)"
(0 (progn (compose-region (match-beginning 1) (match-end 1)
,(make-char 'greek-iso8859-7 107))
nil))))))
(defun eval-and-replace ()
"Replace the preceding sexp with its value."
(interactive)
(backward-kill-sexp)
(condition-case nil
(prin1 (eval (read (current-kill 0)))
(current-buffer))
(error (message "Invalid expression")
(insert (current-kill 0)))))
(defun recompile-init ()
"Byte-compile all your dotfiles again."
(interactive)
(byte-recompile-directory dotfiles-dir 0)
;; TODO: remove elpa-to-submit once everything's submitted.
(byte-recompile-directory (concat dotfiles-dir "elpa-to-submit/" 0)))
(defun regen-autoloads (&optional force-regen)
"Regenerate the autoload definitions file if necessary and load it."
(interactive "P")
(let ((autoload-dir (concat dotfiles-dir "/elpa-to-submit"))
(generated-autoload-file autoload-file))
(when (or force-regen
(not (file-exists-p autoload-file))
(some (lambda (f) (file-newer-than-file-p f autoload-file))
(directory-files autoload-dir t "\\.el$")))
(message "Updating autoloads...")
(let (emacs-lisp-mode-hook)
(update-directory-autoloads autoload-dir))))
(load autoload-file))
TODO: fix this
(defun sudo-edit (&optional arg)
(interactive "p")
(if arg
(find-file (concat "/sudo:root@localhost:" (ido-read-file-name "File: ")))
(find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
Useful when a large block of text is required (e.g. for testing)
(defun lorem ()
"Insert a lorem ipsum."
(interactive)
(insert "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do "
"eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim"
"ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut "
"aliquip ex ea commodo consequat. Duis aute irure dolor in "
"reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla "
"pariatur. Excepteur sint occaecat cupidatat non proident, sunt in "
"culpa qui officia deserunt mollit anim id est laborum."))
(defun switch-or-start (function buffer)
"If the buffer is current, bury it, otherwise invoke the function."
(if (equal (buffer-name (current-buffer)) buffer)
(bury-buffer)
(if (get-buffer buffer)
(switch-to-buffer buffer)
(funcall function))))
(defun insert-date ()
"Insert a time-stamp according to locale's date and time format."
(interactive)
(insert (format-time-string "%c" (current-time))))
(defun pairing-bot ()
"If you can't pair program with a human, use this instead."
(interactive)
(message (if (y-or-n-p "Do you have a test for that? ") "Good." "Bad!")))
A monkeypatch to cause annotate to ignore whitespace
(defun vc-git-annotate-command (file buf &optional rev)
(let ((name (file-relative-name file)))
(vc-git-command buf 0 name "blame" "-w" rev)))