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.

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