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.

530 lines
18 KiB

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. (indent-according-to-mode))
  78. (defun prelude-smart-open-line (arg)
  79. "Insert an empty line after the current line.
  80. Position the cursor at its beginning, according to the current mode.
  81. With a prefix ARG open line above the current line."
  82. (interactive "P")
  83. (if arg
  84. (prelude-smart-open-line-above)
  85. (progn
  86. (move-end-of-line nil)
  87. (newline-and-indent))))
  88. (defun prelude-top-join-line ()
  89. "Join the current line with the line beneath it."
  90. (interactive)
  91. (delete-indentation 1))
  92. (defun prelude-move-line-up ()
  93. "Move the current line up."
  94. (interactive)
  95. (transpose-lines 1)
  96. (forward-line -2)
  97. (indent-according-to-mode))
  98. (defun prelude-move-line-down ()
  99. "Move the current line down."
  100. (interactive)
  101. (forward-line 1)
  102. (transpose-lines 1)
  103. (forward-line -1)
  104. (indent-according-to-mode))
  105. (defun prelude-kill-whole-line (&optional arg)
  106. "A simple wrapper around command `kill-whole-line' that respects indentation.
  107. Passes ARG to command `kill-whole-line' when provided."
  108. (interactive "P")
  109. (kill-whole-line arg)
  110. (back-to-indentation))
  111. (defun prelude-move-beginning-of-line (arg)
  112. "Move point back to indentation of beginning of line.
  113. Move point to the first non-whitespace character on this line.
  114. If point is already there, move to the beginning of the line.
  115. Effectively toggle between the first non-whitespace character and
  116. the beginning of the line.
  117. If ARG is not nil or 1, move forward ARG - 1 lines first. If
  118. point reaches the beginning or end of the buffer, stop there."
  119. (interactive "^p")
  120. (setq arg (or arg 1))
  121. ;; Move lines first
  122. (when (/= arg 1)
  123. (let ((line-move-visual nil))
  124. (forward-line (1- arg))))
  125. (let ((orig-point (point)))
  126. (back-to-indentation)
  127. (when (= orig-point (point))
  128. (move-beginning-of-line 1))))
  129. (global-set-key [remap move-beginning-of-line]
  130. 'prelude-move-beginning-of-line)
  131. (defun prelude-indent-buffer ()
  132. "Indent the currently visited buffer."
  133. (interactive)
  134. (indent-region (point-min) (point-max)))
  135. (defun prelude-indent-region-or-buffer ()
  136. "Indent a region if selected, otherwise the whole buffer."
  137. (interactive)
  138. (save-excursion
  139. (if (region-active-p)
  140. (progn
  141. (indent-region (region-beginning) (region-end))
  142. (message "Indented selected region."))
  143. (progn
  144. (prelude-indent-buffer)
  145. (message "Indented buffer.")))))
  146. (defun prelude-indent-defun ()
  147. "Indent the current defun."
  148. (interactive)
  149. (save-excursion
  150. (mark-defun)
  151. (indent-region (region-beginning) (region-end))))
  152. (defun prelude-annotate-todo ()
  153. "Put fringe marker on TODO: lines in the curent buffer."
  154. (interactive)
  155. (save-excursion
  156. (goto-char (point-min))
  157. (while (re-search-forward "TODO:" nil t)
  158. (let ((overlay (make-overlay (- (point) 5) (point))))
  159. (overlay-put overlay
  160. 'before-string
  161. (propertize (format "A")
  162. 'display '(left-fringe right-triangle)))))))
  163. (defun prelude-copy-file-name-to-clipboard ()
  164. "Copy the current buffer file name to the clipboard."
  165. (interactive)
  166. (let ((filename (if (equal major-mode 'dired-mode)
  167. default-directory
  168. (buffer-file-name))))
  169. (when filename
  170. (kill-new filename)
  171. (message "Copied buffer file name '%s' to the clipboard." filename))))
  172. (defun prelude-duplicate-current-line-or-region (arg)
  173. "Duplicates the current line or region ARG times.
  174. If there's no region, the current line will be duplicated. However, if
  175. there's a region, all lines that region covers will be duplicated."
  176. (interactive "p")
  177. (let (beg end (origin (point)))
  178. (if (and mark-active (> (point) (mark)))
  179. (exchange-point-and-mark))
  180. (setq beg (line-beginning-position))
  181. (if mark-active
  182. (exchange-point-and-mark))
  183. (setq end (line-end-position))
  184. (let ((region (buffer-substring-no-properties beg end)))
  185. (-dotimes arg
  186. (lambda (n)
  187. (goto-char end)
  188. (newline)
  189. (insert region)
  190. (setq end (point))))
  191. (goto-char (+ origin (* (length region) arg) arg)))))
  192. ;; TODO: Remove code duplication by extracting something more generic
  193. (defun prelude-duplicate-and-comment-current-line-or-region (arg)
  194. "Duplicates and comments the current line or region ARG times.
  195. If there's no region, the current line will be duplicated. However, if
  196. there's a region, all lines that region covers will be duplicated."
  197. (interactive "p")
  198. (let (beg end (origin (point)))
  199. (if (and mark-active (> (point) (mark)))
  200. (exchange-point-and-mark))
  201. (setq beg (line-beginning-position))
  202. (if mark-active
  203. (exchange-point-and-mark))
  204. (setq end (line-end-position))
  205. (let ((region (buffer-substring-no-properties beg end)))
  206. (comment-or-uncomment-region beg end)
  207. (setq end (line-end-position))
  208. (-dotimes arg
  209. (lambda (n)
  210. (goto-char end)
  211. (newline)
  212. (insert region)
  213. (setq end (point))))
  214. (goto-char (+ origin (* (length region) arg) arg)))))
  215. (defun prelude-rename-file-and-buffer ()
  216. "Renames current buffer and file it is visiting."
  217. (interactive)
  218. (let ((filename (buffer-file-name)))
  219. (if (not (and filename (file-exists-p filename)))
  220. (message "Buffer is not visiting a file!")
  221. (let ((new-name (read-file-name "New name: " filename)))
  222. (cond
  223. ((vc-backend filename) (vc-rename-file filename new-name))
  224. (t
  225. (rename-file filename new-name t)
  226. (set-visited-file-name new-name t t)))))))
  227. (defun prelude-delete-file-and-buffer ()
  228. "Kill the current buffer and deletes the file it is visiting."
  229. (interactive)
  230. (let ((filename (buffer-file-name)))
  231. (when filename
  232. (if (vc-backend filename)
  233. (vc-delete-file filename)
  234. (progn
  235. (delete-file filename)
  236. (message "Deleted file %s" filename)
  237. (kill-buffer))))))
  238. (defun prelude-view-url ()
  239. "Open a new buffer containing the contents of URL."
  240. (interactive)
  241. (let* ((default (thing-at-point-url-at-point))
  242. (url (read-from-minibuffer "URL: " default)))
  243. (switch-to-buffer (url-retrieve-synchronously url))
  244. (rename-buffer url t)
  245. (cond ((search-forward "<?xml" nil t) (nxml-mode))
  246. ((search-forward "<html" nil t) (html-mode)))))
  247. (defun prelude-untabify-buffer ()
  248. "Remove all tabs from the current buffer."
  249. (interactive)
  250. (untabify (point-min) (point-max)))
  251. (defun prelude-cleanup-buffer ()
  252. "Perform a bunch of operations on the whitespace content of a buffer."
  253. (interactive)
  254. (prelude-indent-buffer)
  255. (prelude-untabify-buffer)
  256. (whitespace-cleanup))
  257. (defun prelude-eval-and-replace ()
  258. "Replace the preceding sexp with its value."
  259. (interactive)
  260. (backward-kill-sexp)
  261. (condition-case nil
  262. (prin1 (eval (read (current-kill 0)))
  263. (current-buffer))
  264. (error (message "Invalid expression")
  265. (insert (current-kill 0)))))
  266. (defun prelude-recompile-init ()
  267. "Byte-compile all your dotfiles again."
  268. (interactive)
  269. (byte-recompile-directory prelude-dir 0))
  270. (defun prelude-sudo-edit (&optional arg)
  271. "Edit currently visited file as root.
  272. With a prefix ARG prompt for a file to visit.
  273. Will also prompt for a file to visit if current
  274. buffer is not visiting a file."
  275. (interactive "P")
  276. (if (or arg (not buffer-file-name))
  277. (find-file (concat "/sudo:root@localhost:"
  278. (ido-read-file-name "Find file(as root): ")))
  279. (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
  280. (defadvice ido-find-file (after find-file-sudo activate)
  281. "Find file as root if necessary."
  282. (unless (or (equal major-mode 'dired-mode)
  283. (and (buffer-file-name)
  284. (not (file-exists-p (file-name-directory (buffer-file-name)))))
  285. (and (buffer-file-name)
  286. (file-writable-p buffer-file-name)))
  287. (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
  288. (defun prelude-start-or-switch-to (function buffer-name)
  289. "Invoke FUNCTION if there is no buffer with BUFFER-NAME.
  290. Otherwise switch to the buffer named BUFFER-NAME. Don't clobber
  291. the current buffer."
  292. (if (not (get-buffer buffer-name))
  293. (progn
  294. (split-window-sensibly (selected-window))
  295. (other-window 1)
  296. (funcall function))
  297. (switch-to-buffer-other-window buffer-name)))
  298. (defun prelude-insert-date ()
  299. "Insert a timestamp according to locale's date and time format."
  300. (interactive)
  301. (insert (format-time-string "%c" (current-time))))
  302. (defun prelude-recentf-ido-find-file ()
  303. "Find a recent file using ido."
  304. (interactive)
  305. (let ((file (ido-completing-read "Choose recent file: "
  306. (-map 'abbreviate-file-name recentf-list)
  307. nil t)))
  308. (when file
  309. (find-file file))))
  310. (defun prelude-swap-windows ()
  311. "If you have 2 windows, it swaps them."
  312. (interactive)
  313. (if (/= (count-windows) 2)
  314. (message "You need exactly 2 windows to do this.")
  315. (let* ((w1 (car (window-list)))
  316. (w2 (cadr (window-list)))
  317. (b1 (window-buffer w1))
  318. (b2 (window-buffer w2))
  319. (s1 (window-start w1))
  320. (s2 (window-start w2)))
  321. (set-window-buffer w1 b2)
  322. (set-window-buffer w2 b1)
  323. (set-window-start w1 s2)
  324. (set-window-start w2 s1)))
  325. (other-window 1))
  326. (defun prelude-switch-to-previous-buffer ()
  327. "Switch to previously open buffer.
  328. Repeated invocations toggle between the two most recently open buffers."
  329. (interactive)
  330. (switch-to-buffer (other-buffer (current-buffer) 1)))
  331. (defun prelude-kill-other-buffers ()
  332. "Kill all buffers but the current one.
  333. Doesn't mess with special buffers."
  334. (interactive)
  335. (-each
  336. (->> (buffer-list)
  337. (-filter #'buffer-file-name)
  338. (--remove (eql (current-buffer) it)))
  339. #'kill-buffer))
  340. (defun prelude-create-scratch-buffer ()
  341. "Create a new scratch buffer."
  342. (interactive)
  343. (progn
  344. (switch-to-buffer
  345. (get-buffer-create (generate-new-buffer-name "*scratch*")))
  346. (emacs-lisp-mode)))
  347. (defvar prelude-tips
  348. '("Press <C-c o> to open a file with external program."
  349. "Press <C-c p f> or <s-f> to navigate a project's files with ido."
  350. "Press <C-c p g> or <s-g> to run grep on a project."
  351. "Press <C-c p s> or <s-p> to switch between projects."
  352. "Press <C-=> or <s-x> to expand the selected region."
  353. "Press <jj> quickly to jump to the beginning of a visible word."
  354. "Press <jk> quickly to jump to a visible character."
  355. "Press <jl> quickly to jump to a visible line."
  356. "Press <C-c h> to navigate a project in Helm."
  357. "Press <C-c g> to search in Google."
  358. "Press <C-c r> to rename the current buffer and file it's visiting."
  359. "Press <C-c t> to open a terminal in Emacs."
  360. "Press <C-c k> to kill all the buffers, but the active one."
  361. "Press <C-x g> or <s-m> to run magit-status."
  362. "Press <C-c D> to delete the current file and buffer."
  363. "Press <C-c s> to swap two windows."
  364. "Press <S-RET> or <M-o> to open a new beneath the current one."
  365. "Press <s-o> to open a line above the current one."
  366. "Press <C-c C-z> in a Elisp buffer to launch an interactive Elisp shell."
  367. "Explore the Prelude menu to find out about some of Prelude extensions to Emacs."
  368. "Access the official Emacs manual by pressing <C-h r>."
  369. "Visit WikEmacs at http://wikemacs.org to find out even more about Emacs."))
  370. (defun prelude-tip-of-the-day ()
  371. "Display a random entry from `prelude-tips'."
  372. (interactive)
  373. (unless (window-minibuffer-p)
  374. ;; pick a new random seed
  375. (random t)
  376. (message
  377. (concat "Prelude tip: " (nth (random (length prelude-tips)) prelude-tips)))))
  378. (defun prelude-eval-after-init (form)
  379. "Add `(lambda () FORM)' to `after-init-hook'.
  380. If Emacs has already finished initialization, also eval FORM immediately."
  381. (let ((func (list 'lambda nil form)))
  382. (add-hook 'after-init-hook func)
  383. (when after-init-time
  384. (eval form))))
  385. (defun prelude-exchange-point-and-mark ()
  386. "Identical to `exchange-point-and-mark' but will not activate the region."
  387. (interactive)
  388. (exchange-point-and-mark)
  389. (deactivate-mark nil))
  390. (defun prelude-update ()
  391. "Update Prelude to its latest version."
  392. (interactive)
  393. (when (y-or-n-p "Do you want to update Prelude? ")
  394. (message "Updating Prelude...")
  395. (cd prelude-dir)
  396. (shell-command "git pull")
  397. (prelude-recompile-init)
  398. (message "Update finished. Restart Emacs to complete the process.")))
  399. (defun thing-at-point-goto-end-of-integer ()
  400. "Go to end of integer at point."
  401. (let ((inhibit-changing-match-data t))
  402. ;; Skip over optional sign
  403. (when (looking-at "[+-]")
  404. (forward-char 1))
  405. ;; Skip over digits
  406. (skip-chars-forward "[[:digit:]]")
  407. ;; Check for at least one digit
  408. (unless (looking-back "[[:digit:]]")
  409. (error "No integer here"))))
  410. (put 'integer 'beginning-op 'thing-at-point-goto-end-of-integer)
  411. (defun thing-at-point-goto-beginning-of-integer ()
  412. "Go to end of integer at point."
  413. (let ((inhibit-changing-match-data t))
  414. ;; Skip backward over digits
  415. (skip-chars-backward "[[:digit:]]")
  416. ;; Check for digits and optional sign
  417. (unless (looking-at "[+-]?[[:digit:]]")
  418. (error "No integer here"))
  419. ;; Skip backward over optional sign
  420. (when (looking-back "[+-]")
  421. (backward-char 1))))
  422. (put 'integer 'beginning-op 'thing-at-point-goto-beginning-of-integer)
  423. (defun thing-at-point-bounds-of-integer-at-point ()
  424. "Get boundaries of integer at point."
  425. (save-excursion
  426. (let (beg end)
  427. (thing-at-point-goto-beginning-of-integer)
  428. (setq beg (point))
  429. (thing-at-point-goto-end-of-integer)
  430. (setq end (point))
  431. (cons beg end))))
  432. (put 'integer 'bounds-of-thing-at-point 'thing-at-point-bounds-of-integer-at-point)
  433. (defun thing-at-point-integer-at-point ()
  434. "Get integer at point."
  435. (let ((bounds (bounds-of-thing-at-point 'integer)))
  436. (string-to-number (buffer-substring (car bounds) (cdr bounds)))))
  437. (put 'integer 'thing-at-point 'thing-at-point-integer-at-point)
  438. (defun prelude-increment-integer-at-point (&optional inc)
  439. "Increment integer at point by one.
  440. With numeric prefix arg INC, increment the integer by INC amount."
  441. (interactive "p")
  442. (let ((inc (or inc 1))
  443. (n (thing-at-point 'integer))
  444. (bounds (bounds-of-thing-at-point 'integer)))
  445. (delete-region (car bounds) (cdr bounds))
  446. (insert (int-to-string (+ n inc)))))
  447. (defun prelude-decrement-integer-at-point (&optional dec)
  448. "Decrement integer at point by one.
  449. With numeric prefix arg DEC, decrement the integer by DEC amount."
  450. (interactive "p")
  451. (prelude-increment-integer-at-point (- (or dec 1))))
  452. ;;; Emacs in OSX already has fullscreen support
  453. ;;; Emacs has a similar built-in command in 24.4
  454. (defun prelude-fullscreen ()
  455. "Make Emacs window fullscreen.
  456. This follows freedesktop standards, should work in X servers."
  457. (interactive)
  458. (if (eq window-system 'x)
  459. (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
  460. '(2 "_NET_WM_STATE_FULLSCREEN" 0))
  461. (error "Only X server is supported")))
  462. (provide 'prelude-core)
  463. ;;; prelude-core.el ends here