Find file
Fetching contributors…
Cannot retrieve contributors at this time
161 lines (137 sloc) 5.65 KB
;;; starter-kit-defuns.el --- Define some custom functions
;; Part of the Emacs Starter Kit
(require 'thingatpt)
(require 'imenu)
;; Network
(defun view-url ()
"Open a new buffer containing the contents of URL."
(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)
;; TODO: switch to nxml/nxhtml mode
(cond ((search-forward "<?xml" nil t) (xml-mode))
((search-forward "<html" nil t) (html-mode)))))
;; Buffer-related
(defun ido-imenu ()
"Update the imenu index and then use ido to select a symbol to navigate to."
(let ((name-and-pos '())
(symbol-names '()))
(flet ((addsymbols (symbol-list)
(when (listp symbol-list)
(dolist (symbol symbol-list)
(let ((name nil) (position nil))
((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))
(let* ((selected-symbol (ido-completing-read "Symbol? " symbol-names))
(position (cdr (assoc selected-symbol name-and-pos))))
(goto-char position))))
(defun coding-hook ()
"Enable things that are convenient across all coding buffers."
(set (make-local-variable 'comment-auto-fill-only-comments) t)
(make-local-variable 'column-number-mode)
(column-number-mode t)
(setq save-place t)
(auto-fill-mode) ;; in comments only
(if window-system (hl-line-mode t))
;; TODO: this breaks in js2-mode!
;;(if (functionp 'idle-highlight) (idle-highlight))
(defun untabify-buffer ()
(untabify (point-min) (point-max)))
(defun indent-buffer ()
(indent-region (point-min) (point-max)))
(defun cleanup-buffer ()
"Perform a bunch of operations on the whitespace content of a buffer."
(defun recentf-ido-find-file ()
"Find a recent file using ido."
(let ((file (ido-completing-read "Choose recent file: " recentf-list nil t)))
(when file
(find-file file))))
;; Cosmetic
(defun pretty-lambdas ()
nil `(("(?\\(lambda\\>\\)"
(0 (progn (compose-region (match-beginning 1) (match-end 1)
,(make-char 'greek-iso8859-7 107))
;; Other
(defun eval-and-replace ()
"Replace the preceding sexp with its value."
(condition-case nil
(prin1 (eval (read (current-kill 0)))
(error (message "Invalid expression")
(insert (current-kill 0)))))
(defun recompile-init ()
"Byte-compile all your dotfiles again."
(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...")
(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))))
(defun lorem ()
"Insert a lorem ipsum."
(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)
(if (get-buffer buffer)
(switch-to-buffer buffer)
(funcall function))))
(defun pairing-bot ()
"If you can't pair program with a human, use this instead."
(message (if (y-or-n-p "Do you have a test for that? ") "Good." "Bad!")))
(provide 'starter-kit-defuns)
;;; starter-kit-defuns.el ends here