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.

363 lines
12 KiB

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