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.

388 lines
13 KiB

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