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.

352 lines
11 KiB

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