Emacs config utilizing prelude as a base
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

383 lines
13 KiB

14 years ago
14 years ago
  1. ;;; prelude-core.el --- Emacs Prelude: core Prelude defuns.
  2. ;;
  3. ;; Copyright (c) 2011-2012 Bozhidar Batsov
  4. ;;
  5. ;; Author: Bozhidar Batsov <bozhidar@batsov.com>
  6. ;; URL: http://batsov.com/emacs-prelude
  7. ;; Version: 1.0.0
  8. ;; Keywords: convenience
  9. ;; This file is not part of GNU Emacs.
  10. ;;; Commentary:
  11. ;; Here are the definitions of most of the functions added by Prelude.
  12. ;;; License:
  13. ;; This program is free software; you can redistribute it and/or
  14. ;; modify it under the terms of the GNU General Public License
  15. ;; as published by the Free Software Foundation; either version 3
  16. ;; of the License, or (at your option) any later version.
  17. ;;
  18. ;; This program is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;; GNU General Public License for more details.
  22. ;;
  23. ;; You should have received a copy of the GNU General Public License
  24. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  25. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  26. ;; Boston, MA 02110-1301, USA.
  27. ;;; Code:
  28. (require 'cl)
  29. (require 'thingatpt)
  30. (defun prelude-add-subfolders-to-load-path (parent-dir)
  31. "Adds all first level `parent-dir' subdirs to the
  32. Emacs load path."
  33. (dolist (f (directory-files parent-dir))
  34. (let ((name (concat parent-dir f)))
  35. (when (and (file-directory-p name)
  36. (not (equal f ".."))
  37. (not (equal f ".")))
  38. (add-to-list 'load-path name)))))
  39. (defun prelude-open-with ()
  40. "Simple function that allows us to open the underlying
  41. file of a buffer in an external program."
  42. (interactive)
  43. (when buffer-file-name
  44. (shell-command (concat
  45. (if (eq system-type 'darwin)
  46. "open"
  47. (read-shell-command "Open current file with: "))
  48. " "
  49. buffer-file-name))))
  50. (defun prelude-buffer-mode (buffer-or-name)
  51. (with-current-buffer buffer-or-name major-mode))
  52. (defun prelude-visit-term-buffer ()
  53. (interactive)
  54. (if (not (get-buffer "*ansi-term*"))
  55. (ansi-term (getenv "SHELL"))
  56. (switch-to-buffer "*ansi-term*")))
  57. (defun prelude-google ()
  58. "Googles a query or region if any."
  59. (interactive)
  60. (browse-url
  61. (concat
  62. "http://www.google.com/search?ie=utf-8&oe=utf-8&q="
  63. (url-hexify-string (if mark-active
  64. (buffer-substring (region-beginning) (region-end))
  65. (read-string "Google: "))))))
  66. (defun prelude-indent-rigidly-and-copy-to-clipboard (begin end indent)
  67. "Copy the selected code region to the clipboard, indented according
  68. to Markdown blockquote rules."
  69. (let ((buffer (current-buffer)))
  70. (with-temp-buffer
  71. (insert-buffer-substring-no-properties buffer begin end)
  72. (indent-rigidly (point-min) (point-max) indent)
  73. (clipboard-kill-ring-save (point-min) (point-max)))))
  74. (defun prelude-indent-blockquote-and-copy-to-clipboard (begin end)
  75. "Copy the selected code region to the clipboard, indented according
  76. to markdown blockquote rules (useful to copy snippets to StackOverflow, Assembla, Github."
  77. (interactive "r")
  78. (prelude-indent-rigidly-and-copy-to-clipboard begin end 4))
  79. (defun prelude-indent-nested-blockquote-and-copy-to-clipboard (begin end)
  80. "Copy the selected code region to the clipboard, indented according
  81. to markdown blockquote rules. Useful to add snippets under bullet points."
  82. (interactive "r")
  83. (prelude-indent-rigidly-and-copy-to-clipboard begin end 6))
  84. (defun prelude-insert-empty-line ()
  85. "Insert an empty line after the current line and positon
  86. the curson at its beginning, according to the current mode."
  87. (interactive)
  88. (move-end-of-line nil)
  89. (open-line 1)
  90. (forward-line 1)
  91. (indent-according-to-mode))
  92. (defun prelude-move-line-up ()
  93. "Move up the current line."
  94. (interactive)
  95. (transpose-lines 1)
  96. (forward-line -2))
  97. (defun prelude-move-line-down ()
  98. "Move down the current line."
  99. (interactive)
  100. (forward-line 1)
  101. (transpose-lines 1)
  102. (forward-line -1))
  103. ;; add the ability to copy and cut the current line, without marking it
  104. (defadvice kill-ring-save (before slick-copy activate compile)
  105. "When called interactively with no active region, copy a single line instead."
  106. (interactive
  107. (if mark-active (list (region-beginning) (region-end))
  108. (message "Copied line")
  109. (list (line-beginning-position)
  110. (line-beginning-position 2)))))
  111. (defadvice kill-region (before slick-cut activate compile)
  112. "When called interactively with no active region, kill a single line instead."
  113. (interactive
  114. (if mark-active (list (region-beginning) (region-end))
  115. (list (line-beginning-position)
  116. (line-beginning-position 2)))))
  117. (defun prelude-indent-buffer ()
  118. "Indents the entire buffer."
  119. (interactive)
  120. (indent-region (point-min) (point-max)))
  121. (defun prelude-indent-region-or-buffer ()
  122. "Indents a region if selected, otherwise the whole buffer."
  123. (interactive)
  124. (save-excursion
  125. (if (region-active-p)
  126. (progn
  127. (indent-region (region-beginning) (region-end))
  128. (message "Indented selected region."))
  129. (progn
  130. (prelude-indent-buffer)
  131. (message "Indented buffer.")))))
  132. (defun prelude-annotate-todo ()
  133. "Put fringe marker on TODO: lines in the curent buffer."
  134. (interactive)
  135. (save-excursion
  136. (goto-char (point-min))
  137. (while (re-search-forward "TODO:" nil t)
  138. (let ((overlay (make-overlay (- (point) 5) (point))))
  139. (overlay-put overlay
  140. 'before-string
  141. (propertize (format "A")
  142. 'display '(left-fringe right-triangle)))))))
  143. (defun prelude-copy-file-name-to-clipboard ()
  144. "Copy the current buffer file name to the clipboard."
  145. (interactive)
  146. (let ((filename (if (equal major-mode 'dired-mode)
  147. default-directory
  148. (buffer-file-name))))
  149. (when filename
  150. (kill-new filename)
  151. (message "Copied buffer file name '%s' to the clipboard." filename))))
  152. (defun prelude-duplicate-current-line-or-region (arg)
  153. "Duplicates the current line or region ARG times.
  154. If there's no region, the current line will be duplicated. However, if
  155. there's a region, all lines that region covers will be duplicated."
  156. (interactive "p")
  157. (let (beg end (origin (point)))
  158. (if (and mark-active (> (point) (mark)))
  159. (exchange-point-and-mark))
  160. (setq beg (line-beginning-position))
  161. (if mark-active
  162. (exchange-point-and-mark))
  163. (setq end (line-end-position))
  164. (let ((region (buffer-substring-no-properties beg end)))
  165. (dotimes (i arg)
  166. (goto-char end)
  167. (newline)
  168. (insert region)
  169. (setq end (point)))
  170. (goto-char (+ origin (* (length region) arg) arg)))))
  171. ;; TODO doesn't work with uniquify
  172. (defun prelude-rename-file-and-buffer ()
  173. "Renames current buffer and file it is visiting."
  174. (interactive)
  175. (let ((name (buffer-name))
  176. (filename (buffer-file-name)))
  177. (if (not (and filename (file-exists-p filename)))
  178. (message "Buffer '%s' is not visiting a file!" name)
  179. (let ((new-name (read-file-name "New name: " filename)))
  180. (cond ((get-buffer new-name)
  181. (message "A buffer named '%s' already exists!" new-name))
  182. (t
  183. (rename-file name new-name 1)
  184. (rename-buffer new-name)
  185. (set-visited-file-name new-name)
  186. (set-buffer-modified-p nil)))))))
  187. (defun prelude-delete-file-and-buffer ()
  188. "Kills the current buffer and deletes the file it is visiting"
  189. (interactive)
  190. (let ((filename (buffer-file-name)))
  191. (when filename
  192. (delete-file filename)
  193. (message "Deleted file %s" filename)))
  194. (kill-buffer))
  195. (defun prelude-view-url ()
  196. "Open a new buffer containing the contents of URL."
  197. (interactive)
  198. (let* ((default (thing-at-point-url-at-point))
  199. (url (read-from-minibuffer "URL: " default)))
  200. (switch-to-buffer (url-retrieve-synchronously url))
  201. (rename-buffer url t)
  202. ;; TODO: switch to nxml/nxhtml mode
  203. (cond ((search-forward "<?xml" nil t) (xml-mode))
  204. ((search-forward "<html" nil t) (html-mode)))))
  205. ;; We have a number of turn-on-* functions since it's advised that lambda
  206. ;; functions not go in hooks. Repeatedly evaluating an add-to-list with a
  207. ;; hook value will repeatedly add it since there's no way to ensure
  208. ;; that a lambda doesn't already exist in the list.
  209. (defun prelude-turn-on-whitespace ()
  210. (whitespace-mode +1))
  211. (defun prelude-turn-off-whitespace ()
  212. (whitespace-mode -1))
  213. (defun prelude-turn-on-abbrev ()
  214. (abbrev-mode +1))
  215. (defun prelude-turn-off-abbrev ()
  216. (abbrev-mode -1))
  217. (defun prelude-untabify-buffer ()
  218. (interactive)
  219. (untabify (point-min) (point-max)))
  220. (defun prelude-cleanup-buffer ()
  221. "Perform a bunch of operations on the whitespace content of a buffer."
  222. (interactive)
  223. (prelude-indent-buffer)
  224. (prelude-untabify-buffer)
  225. (whitespace-cleanup))
  226. (defun prelude-eval-and-replace ()
  227. "Replace the preceding sexp with its value."
  228. (interactive)
  229. (backward-kill-sexp)
  230. (condition-case nil
  231. (prin1 (eval (read (current-kill 0)))
  232. (current-buffer))
  233. (error (message "Invalid expression")
  234. (insert (current-kill 0)))))
  235. (defun prelude-recompile-init ()
  236. "Byte-compile all your dotfiles again."
  237. (interactive)
  238. (byte-recompile-directory prelude-dir 0))
  239. (defun prelude-sudo-edit (&optional arg)
  240. (interactive "p")
  241. (if (or arg (not buffer-file-name))
  242. (find-file (concat "/sudo:root@localhost:" (ido-read-file-name "File: ")))
  243. (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
  244. (defun prelude-switch-or-start (function buffer)
  245. "If the buffer is current, bury it, otherwise invoke the function."
  246. (if (equal (buffer-name (current-buffer)) buffer)
  247. (bury-buffer)
  248. (if (get-buffer buffer)
  249. (switch-to-buffer buffer)
  250. (funcall function))))
  251. (defun prelude-insert-date ()
  252. "Insert a time-stamp according to locale's date and time format."
  253. (interactive)
  254. (insert (format-time-string "%c" (current-time))))
  255. (defun prelude-conditionally-enable-paredit-mode ()
  256. "Enable paredit-mode in the minibuffer, during eval-expression."
  257. (if (eq this-command 'eval-expression)
  258. (paredit-mode 1)))
  259. (add-hook 'minibuffer-setup-hook 'prelude-conditionally-enable-paredit-mode)
  260. (defun prelude-recentf-ido-find-file ()
  261. "Find a recent file using ido."
  262. (interactive)
  263. (let ((file (ido-completing-read "Choose recent file: " recentf-list nil t)))
  264. (when file
  265. (find-file file))))
  266. (defun prelude-swap-windows ()
  267. "If you have 2 windows, it swaps them."
  268. (interactive)
  269. (if (/= (count-windows) 2)
  270. (message "You need exactly 2 windows to do this.")
  271. (let* ((w1 (first (window-list)))
  272. (w2 (second (window-list)))
  273. (b1 (window-buffer w1))
  274. (b2 (window-buffer w2))
  275. (s1 (window-start w1))
  276. (s2 (window-start w2)))
  277. (set-window-buffer w1 b2)
  278. (set-window-buffer w2 b1)
  279. (set-window-start w1 s2)
  280. (set-window-start w2 s1)))
  281. (other-window 1))
  282. (defun prelude-kill-other-buffers ()
  283. "Kill all buffers but the current one. Doesn't mess with special buffers."
  284. (interactive)
  285. (dolist (buffer (buffer-list))
  286. (unless (or (eql buffer (current-buffer)) (not (buffer-file-name buffer)))
  287. (kill-buffer buffer))))
  288. (require 'repeat)
  289. (defun make-repeatable-command (cmd)
  290. "Returns a new command that is a repeatable version of CMD.
  291. The new command is named CMD-repeat. CMD should be a quoted
  292. command.
  293. This allows you to bind the command to a compound keystroke and
  294. repeat it with just the final key. For example:
  295. (global-set-key (kbd \"C-c a\") (make-repeatable-command 'foo))
  296. will create a new command called foo-repeat. Typing C-c a will
  297. just invoke foo. Typing C-c a a a will invoke foo three times,
  298. and so on."
  299. (fset (intern (concat (symbol-name cmd) "-repeat"))
  300. `(lambda ,(help-function-arglist cmd) ;; arg list
  301. ,(format "A repeatable version of `%s'." (symbol-name cmd)) ;; doc string
  302. ,(interactive-form cmd) ;; interactive form
  303. ;; see also repeat-message-function
  304. (setq last-repeatable-command ',cmd)
  305. (repeat nil)))
  306. (intern (concat (symbol-name cmd) "-repeat")))
  307. (defvar prelude-tips
  308. '("Press <C-c o> to open a file with external program."
  309. "Press <C-c p f> to navigate a project's files with ido."
  310. "Press <C-c h> to navigate a project in Helm."
  311. "Press <C-c g> to search in Google."
  312. "Press <C-c r> to rename the current buffer and file it's visiting."
  313. "Press <C-c t> to open a terminal in Emacs."
  314. "Explore the Prelude menu to find out about some of Prelude extensions to Emacs."
  315. "Access the official Emacs manual by pressing <C-h r>."
  316. "Visit WikEmacs at http://wikemacs.org to find out even more about Emacs."))
  317. (defun prelude-tip-of-the-day ()
  318. (interactive)
  319. (message (concat "Prelude tip: " (nth (random (length prelude-tips)) prelude-tips))))
  320. (defun prelude-eval-after-init (form)
  321. "Add `(lambda () FORM)' to `after-init-hook'.
  322. If Emacs has already finished initialization, also eval FORM immediately."
  323. (let ((func (list 'lambda nil form)))
  324. (add-hook 'after-init-hook func)
  325. (when after-init-time
  326. (eval form))))
  327. (provide 'prelude-core)
  328. ;;; prelude-core.el ends here