Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
154 lines (130 sloc) 5.47 KB
;;; starter-kit-defuns.el --- Define some custom functions
;; Part of the Emacs Starter Kit
;; Network
(require 'thingatpt)
(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)))))
(defun map-coords (lat lng)
"Show a Yahoo map marked with the point LAT by LNG."
(interactive "BLatitude: \nBLongitude")
(browse-url (format ""
lat lng)))
;; Buffer-related
(defun ido-goto-symbol ()
"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 my-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))
(defun untabify-buffer ()
(untabify (point-min) (point-max)))
(defun indent-buffer ()
(indent-region (point-min) (point-max)))
(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 my-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 my-recompile-init ()
"Byte-compile everything in ~/.emacs.d again."
(byte-recompile-directory (expand-file-name "~/.emacs.d") 0))
(defun regen-autoloads ()
"Regenerate the autoload definitions file if necessary and load it."
(if (or (not (file-exists-p autoload-file))
;; TODO: make this more readable
(< (+ (car (nth 5 (file-attributes autoload-file))) 20)
(car (current-time))))
(let ((generated-autoload-file autoload-file))
(message "Updating autoloads...")
(update-directory-autoloads dotfiles-dir
(concat dotfiles-dir "/elpa-to-submit"))))
(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))))
(if (eq window-system 'x)
(defun handle-shift-selection (&rest args)
"Fix a bug that occurs when caps-lock is remapped to ctrl in X."))
(provide 'starter-kit-defuns)
;;; starter-kit-defuns.el ends here