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.

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