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.

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