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.

569 lines
20 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-process"
  36. "*prelude-open-with-output*"
  37. (cond
  38. ((and (not arg) (eq system-type 'darwin)) "open")
  39. ((and (not arg) (member system-type '(gnu gnu/linux gnu/kfreebsd))) "xdg-open")
  40. (t (read-shell-command "Open current file with: ")))
  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-search (query-url prompt)
  53. "Open the search url constructed with the QUERY-URL.
  54. PROMPT sets the `read-string prompt."
  55. (browse-url
  56. (concat query-url
  57. (url-hexify-string
  58. (if mark-active
  59. (buffer-substring (region-beginning) (region-end))
  60. (read-string prompt))))))
  61. (defmacro prelude-install-search-engine (search-engine-name search-engine-url search-engine-prompt)
  62. "Given some information regarding a search engine, install the interactive command to search through them"
  63. `(defun ,(intern (format "prelude-%s" search-engine-name)) ()
  64. ,(format "Search %s with a query or region if any." search-engine-name)
  65. (interactive)
  66. (prelude-search ,search-engine-url ,search-engine-prompt)))
  67. (prelude-install-search-engine "google" "http://www.google.com/search?q=" "Google: ")
  68. (prelude-install-search-engine "youtube" "http://www.youtube.com/results?search_query=" "Search YouTube: ")
  69. (prelude-install-search-engine "github" "https://github.com/search?q=" "Search GitHub: ")
  70. (prelude-install-search-engine "duckduckgo" "https://duckduckgo.com/?t=lm&q=" "Search DuckDuckGo: ")
  71. (defun prelude-indent-rigidly-and-copy-to-clipboard (begin end arg)
  72. "Indent region between BEGIN and END by ARG columns and copy to clipboard."
  73. (interactive "r\nP")
  74. (let ((arg (or arg 4))
  75. (buffer (current-buffer)))
  76. (with-temp-buffer
  77. (insert-buffer-substring-no-properties buffer begin end)
  78. (indent-rigidly (point-min) (point-max) arg)
  79. (clipboard-kill-ring-save (point-min) (point-max)))))
  80. (defun prelude-smart-open-line-above ()
  81. "Insert an empty line above the current line.
  82. Position the cursor at it's beginning, according to the current mode."
  83. (interactive)
  84. (move-beginning-of-line nil)
  85. (newline-and-indent)
  86. (forward-line -1)
  87. (indent-according-to-mode))
  88. (defun prelude-smart-open-line (arg)
  89. "Insert an empty line after the current line.
  90. Position the cursor at its beginning, according to the current mode.
  91. With a prefix ARG open line above the current line."
  92. (interactive "P")
  93. (if arg
  94. (prelude-smart-open-line-above)
  95. (progn
  96. (move-end-of-line nil)
  97. (newline-and-indent))))
  98. (defun prelude-top-join-line ()
  99. "Join the current line with the line beneath it."
  100. (interactive)
  101. (delete-indentation 1))
  102. (defun prelude-kill-whole-line (&optional arg)
  103. "A simple wrapper around command `kill-whole-line' that respects indentation.
  104. Passes ARG to command `kill-whole-line' when provided."
  105. (interactive "p")
  106. (kill-whole-line arg)
  107. (back-to-indentation))
  108. (defun prelude-move-beginning-of-line (arg)
  109. "Move point back to indentation of beginning of line.
  110. Move point to the first non-whitespace character on this line.
  111. If point is already there, move to the beginning of the line.
  112. Effectively toggle between the first non-whitespace character and
  113. the beginning of the line.
  114. If ARG is not nil or 1, move forward ARG - 1 lines first. If
  115. point reaches the beginning or end of the buffer, stop there."
  116. (interactive "^p")
  117. (setq arg (or arg 1))
  118. ;; Move lines first
  119. (when (/= arg 1)
  120. (let ((line-move-visual nil))
  121. (forward-line (1- arg))))
  122. (let ((orig-point (point)))
  123. (back-to-indentation)
  124. (when (= orig-point (point))
  125. (move-beginning-of-line 1))))
  126. (global-set-key [remap move-beginning-of-line]
  127. 'prelude-move-beginning-of-line)
  128. (defun prelude-indent-buffer ()
  129. "Indent the currently visited buffer."
  130. (interactive)
  131. (indent-region (point-min) (point-max)))
  132. (defun prelude-indent-region-or-buffer ()
  133. "Indent a region if selected, otherwise the whole buffer."
  134. (interactive)
  135. (save-excursion
  136. (if (region-active-p)
  137. (progn
  138. (indent-region (region-beginning) (region-end))
  139. (message "Indented selected region."))
  140. (progn
  141. (prelude-indent-buffer)
  142. (message "Indented buffer.")))))
  143. (defun prelude-indent-defun ()
  144. "Indent the current defun."
  145. (interactive)
  146. (save-excursion
  147. (mark-defun)
  148. (indent-region (region-beginning) (region-end))))
  149. (defun prelude-annotate-todo ()
  150. "Put fringe marker on TODO: lines in the curent buffer."
  151. (interactive)
  152. (save-excursion
  153. (goto-char (point-min))
  154. (while (re-search-forward "TODO:" nil t)
  155. (let ((overlay (make-overlay (- (point) 5) (point))))
  156. (overlay-put overlay
  157. 'before-string
  158. (propertize (format "A")
  159. 'display '(left-fringe right-triangle)))))))
  160. (defun prelude-copy-file-name-to-clipboard ()
  161. "Copy the current buffer file name to the clipboard."
  162. (interactive)
  163. (let ((filename (if (equal major-mode 'dired-mode)
  164. default-directory
  165. (buffer-file-name))))
  166. (when filename
  167. (kill-new filename)
  168. (message "Copied buffer file name '%s' to the clipboard." filename))))
  169. (defun prelude-get-positions-of-line-or-region ()
  170. "Return positions (beg . end) of the current line
  171. or region."
  172. (let (beg end)
  173. (if (and mark-active (> (point) (mark)))
  174. (exchange-point-and-mark))
  175. (setq beg (line-beginning-position))
  176. (if mark-active
  177. (exchange-point-and-mark))
  178. (setq end (line-end-position))
  179. (cons beg end)))
  180. (defun prelude-duplicate-current-line-or-region (arg)
  181. "Duplicates the current line or region ARG times.
  182. If there's no region, the current line will be duplicated. However, if
  183. there's a region, all lines that region covers will be duplicated."
  184. (interactive "p")
  185. (pcase-let* ((origin (point))
  186. (`(,beg . ,end) (prelude-get-positions-of-line-or-region))
  187. (region (buffer-substring-no-properties beg end)))
  188. (-dotimes arg
  189. (lambda (n)
  190. (goto-char end)
  191. (newline)
  192. (insert region)
  193. (setq end (point))))
  194. (goto-char (+ origin (* (length region) arg) arg))))
  195. (defun prelude-duplicate-and-comment-current-line-or-region (arg)
  196. "Duplicates and comments the current line or region ARG times.
  197. If there's no region, the current line will be duplicated. However, if
  198. there's a region, all lines that region covers will be duplicated."
  199. (interactive "p")
  200. (pcase-let* ((origin (point))
  201. (`(,beg . ,end) (prelude-get-positions-of-line-or-region))
  202. (region (buffer-substring-no-properties beg end)))
  203. (comment-or-uncomment-region beg end)
  204. (setq end (line-end-position))
  205. (-dotimes arg
  206. (lambda (n)
  207. (goto-char end)
  208. (newline)
  209. (insert region)
  210. (setq end (point))))
  211. (goto-char (+ origin (* (length region) arg) arg))))
  212. (defun prelude-rename-file-and-buffer ()
  213. "Renames current buffer and file it is visiting."
  214. (interactive)
  215. (let ((filename (buffer-file-name)))
  216. (if (not (and filename (file-exists-p filename)))
  217. (message "Buffer is not visiting a file!")
  218. (let ((new-name (read-file-name "New name: " filename)))
  219. (cond
  220. ((vc-backend filename) (vc-rename-file filename new-name))
  221. (t
  222. (rename-file filename new-name t)
  223. (set-visited-file-name new-name t t)))))))
  224. (defun prelude-delete-file-and-buffer ()
  225. "Kill the current buffer and deletes the file it is visiting."
  226. (interactive)
  227. (let ((filename (buffer-file-name)))
  228. (when filename
  229. (if (vc-backend filename)
  230. (vc-delete-file filename)
  231. (when (y-or-n-p (format "Are you sure you want to delete %s? " filename))
  232. (delete-file filename)
  233. (message "Deleted file %s" filename)
  234. (kill-buffer))))))
  235. (defun prelude-view-url ()
  236. "Open a new buffer containing the contents of URL."
  237. (interactive)
  238. (let* ((default (thing-at-point-url-at-point))
  239. (url (read-from-minibuffer "URL: " default)))
  240. (switch-to-buffer (url-retrieve-synchronously url))
  241. (rename-buffer url t)
  242. (cond ((search-forward "<?xml" nil t) (nxml-mode))
  243. ((search-forward "<html" nil t) (html-mode)))))
  244. (defun prelude-untabify-buffer ()
  245. "Remove all tabs from the current buffer."
  246. (interactive)
  247. (untabify (point-min) (point-max)))
  248. (defun prelude-cleanup-buffer ()
  249. "Perform a bunch of operations on the whitespace content of a buffer."
  250. (interactive)
  251. (prelude-indent-buffer)
  252. (prelude-untabify-buffer)
  253. (whitespace-cleanup))
  254. (defun prelude-eval-and-replace ()
  255. "Replace the preceding sexp with its value."
  256. (interactive)
  257. (let ((value (eval (preceding-sexp))))
  258. (backward-kill-sexp)
  259. (insert (format "%s" value))))
  260. (defun prelude-recompile-init ()
  261. "Byte-compile all your dotfiles again."
  262. (interactive)
  263. (byte-recompile-directory prelude-dir 0))
  264. (defun prelude-sudo-edit (&optional arg)
  265. "Edit currently visited file as root.
  266. With a prefix ARG prompt for a file to visit.
  267. Will also prompt for a file to visit if current
  268. buffer is not visiting a file."
  269. (interactive "P")
  270. (if (or arg (not buffer-file-name))
  271. (find-file (concat "/sudo:root@localhost:"
  272. (ido-read-file-name "Find file(as root): ")))
  273. (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
  274. (defadvice ido-find-file (after find-file-sudo activate)
  275. "Find file as root if necessary."
  276. (unless (or (equal major-mode 'dired-mode)
  277. (and (buffer-file-name)
  278. (not (file-exists-p (file-name-directory (buffer-file-name)))))
  279. (and (buffer-file-name)
  280. (file-writable-p buffer-file-name)))
  281. (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
  282. (defun prelude-start-or-switch-to (function buffer-name)
  283. "Invoke FUNCTION if there is no buffer with BUFFER-NAME.
  284. Otherwise switch to the buffer named BUFFER-NAME. Don't clobber
  285. the current buffer."
  286. (if (not (get-buffer buffer-name))
  287. (progn
  288. (split-window-sensibly (selected-window))
  289. (other-window 1)
  290. (funcall function))
  291. (switch-to-buffer-other-window buffer-name)))
  292. (defun prelude-insert-date ()
  293. "Insert a timestamp according to locale's date and time format."
  294. (interactive)
  295. (insert (format-time-string "%c" (current-time))))
  296. (defun prelude-recentf-ido-find-file ()
  297. "Find a recent file using ido."
  298. (interactive)
  299. (let ((file (ido-completing-read "Choose recent file: "
  300. (-map 'abbreviate-file-name recentf-list)
  301. nil t)))
  302. (when file
  303. (find-file file))))
  304. (defun prelude-swap-windows ()
  305. "If you have 2 windows, it swaps them."
  306. (interactive)
  307. (if (/= (count-windows) 2)
  308. (message "You need exactly 2 windows to do this.")
  309. (let* ((w1 (car (window-list)))
  310. (w2 (cadr (window-list)))
  311. (b1 (window-buffer w1))
  312. (b2 (window-buffer w2))
  313. (s1 (window-start w1))
  314. (s2 (window-start w2)))
  315. (set-window-buffer w1 b2)
  316. (set-window-buffer w2 b1)
  317. (set-window-start w1 s2)
  318. (set-window-start w2 s1)))
  319. (other-window 1))
  320. (defun prelude-switch-to-previous-buffer ()
  321. "Switch to previously open buffer.
  322. Repeated invocations toggle between the two most recently open buffers."
  323. (interactive)
  324. (switch-to-buffer (other-buffer (current-buffer) 1)))
  325. (defun prelude-kill-other-buffers ()
  326. "Kill all buffers but the current one.
  327. Doesn't mess with special buffers."
  328. (interactive)
  329. (-each
  330. (->> (buffer-list)
  331. (-filter #'buffer-file-name)
  332. (--remove (eql (current-buffer) it)))
  333. #'kill-buffer))
  334. (defun prelude-create-scratch-buffer ()
  335. "Create a new scratch buffer."
  336. (interactive)
  337. (progn
  338. (switch-to-buffer
  339. (get-buffer-create (generate-new-buffer-name "*scratch*")))
  340. (emacs-lisp-mode)))
  341. (defvar prelude-tips
  342. '("Press <C-c o> to open a file with external program."
  343. "Press <C-c p f> or <s-f> to navigate a project's files with ido."
  344. "Press <C-c p g> or <s-g> to run grep on a project."
  345. "Press <C-c p s> or <s-p> to switch between projects."
  346. "Press <C-=> or <s-x> to expand the selected region."
  347. "Press <jj> quickly to jump to the beginning of a visible word."
  348. "Press <jk> quickly to jump to a visible character."
  349. "Press <jl> quickly to jump to a visible line."
  350. "Press <C-c g> to search in Google."
  351. "Press <C-c G> to search in GitHub."
  352. "Press <C-c y> to search in YouTube."
  353. "Press <C-c U> to search in DuckDuckGo."
  354. "Press <C-c r> to rename the current buffer and file it's visiting."
  355. "Press <C-c t> to open a terminal in Emacs."
  356. "Press <C-c k> to kill all the buffers, but the active one."
  357. "Press <C-x g> or <s-m> to run magit-status."
  358. "Press <C-c D> to delete the current file and buffer."
  359. "Press <C-c s> to swap two windows."
  360. "Press <S-RET> or <M-o> to open a new beneath the current one."
  361. "Press <s-o> to open a line above the current one."
  362. "Press <C-c C-z> in a Elisp buffer to launch an interactive Elisp shell."
  363. "Press <C-Backspace> to kill a line backwards."
  364. "Press <C-S-Backspace> or <s-k> to kill the whole line."
  365. "Press <f11> to toggle fullscreen mode."
  366. "Press <f12> to toggle the menu bar."
  367. "Explore the Tools->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 the EmacsWiki at http://emacswiki.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. (require 'epl)
  391. (defun prelude-update ()
  392. "Update Prelude to its latest version."
  393. (interactive)
  394. (when (y-or-n-p "Do you want to update Prelude? ")
  395. (message "Updating installed packages...")
  396. (epl-upgrade)
  397. (message "Updating Prelude...")
  398. (cd prelude-dir)
  399. (shell-command "git pull")
  400. (prelude-recompile-init)
  401. (message "Update finished. Restart Emacs to complete the process.")))
  402. (defun prelude-update-packages (&optional arg)
  403. "Update Prelude's packages.
  404. This includes package installed via `prelude-require-package'.
  405. With a prefix ARG updates all installed packages."
  406. (interactive "P")
  407. (when (y-or-n-p "Do you want to update Prelude's packages? ")
  408. (if arg
  409. (epl-upgrade)
  410. (epl-upgrade (-filter (lambda (p) (memq (epl-package-name p) prelude-packages))
  411. (epl-installed-packages))))
  412. (message "Update finished. Restart Emacs to complete the process.")))
  413. (defun thing-at-point-goto-end-of-integer ()
  414. "Go to end of integer at point."
  415. (let ((inhibit-changing-match-data t))
  416. ;; Skip over optional sign
  417. (when (looking-at "[+-]")
  418. (forward-char 1))
  419. ;; Skip over digits
  420. (skip-chars-forward "[[:digit:]]")
  421. ;; Check for at least one digit
  422. (unless (looking-back "[[:digit:]]")
  423. (error "No integer here"))))
  424. (put 'integer 'beginning-op 'thing-at-point-goto-end-of-integer)
  425. (defun thing-at-point-goto-beginning-of-integer ()
  426. "Go to end of integer at point."
  427. (let ((inhibit-changing-match-data t))
  428. ;; Skip backward over digits
  429. (skip-chars-backward "[[:digit:]]")
  430. ;; Check for digits and optional sign
  431. (unless (looking-at "[+-]?[[:digit:]]")
  432. (error "No integer here"))
  433. ;; Skip backward over optional sign
  434. (when (looking-back "[+-]")
  435. (backward-char 1))))
  436. (put 'integer 'beginning-op 'thing-at-point-goto-beginning-of-integer)
  437. (defun thing-at-point-bounds-of-integer-at-point ()
  438. "Get boundaries of integer at point."
  439. (save-excursion
  440. (let (beg end)
  441. (thing-at-point-goto-beginning-of-integer)
  442. (setq beg (point))
  443. (thing-at-point-goto-end-of-integer)
  444. (setq end (point))
  445. (cons beg end))))
  446. (put 'integer 'bounds-of-thing-at-point 'thing-at-point-bounds-of-integer-at-point)
  447. (defun thing-at-point-integer-at-point ()
  448. "Get integer at point."
  449. (let ((bounds (bounds-of-thing-at-point 'integer)))
  450. (string-to-number (buffer-substring (car bounds) (cdr bounds)))))
  451. (put 'integer 'thing-at-point 'thing-at-point-integer-at-point)
  452. (defun prelude-increment-integer-at-point (&optional inc)
  453. "Increment integer at point by one.
  454. With numeric prefix arg INC, increment the integer by INC amount."
  455. (interactive "p")
  456. (let ((inc (or inc 1))
  457. (n (thing-at-point 'integer))
  458. (bounds (bounds-of-thing-at-point 'integer)))
  459. (delete-region (car bounds) (cdr bounds))
  460. (insert (int-to-string (+ n inc)))))
  461. (defun prelude-decrement-integer-at-point (&optional dec)
  462. "Decrement integer at point by one.
  463. With numeric prefix arg DEC, decrement the integer by DEC amount."
  464. (interactive "p")
  465. (prelude-increment-integer-at-point (- (or dec 1))))
  466. ;;; Emacs in OSX already has fullscreen support
  467. ;;; Emacs has a similar built-in command in 24.4
  468. (defun prelude-fullscreen ()
  469. "Make Emacs window fullscreen.
  470. This follows freedesktop standards, should work in X servers."
  471. (interactive)
  472. (if (eq window-system 'x)
  473. (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
  474. '(2 "_NET_WM_STATE_FULLSCREEN" 0))
  475. (error "Only X server is supported")))
  476. (defun prelude-find-user-init-file ()
  477. "Edit the `user-init-file', in another window."
  478. (interactive)
  479. (find-file-other-window user-init-file))
  480. (defun prelude-find-shell-init-file ()
  481. "Edit the shell init file in another window."
  482. (interactive)
  483. (let* ((shell (car (reverse (s-split "/" (getenv "SHELL")))))
  484. (shell-init-file (cond
  485. ((s-equals? "zsh" shell) ".zshrc")
  486. ((s-equals? "bash" shell) ".bashrc")
  487. (t (error "Unknown shell")))))
  488. (find-file-other-window (expand-file-name shell-init-file (getenv "HOME")))))
  489. (defun prelude-wrap-with (s)
  490. "Create a wrapper function for smartparens using S."
  491. `(lambda (&optional arg)
  492. (interactive "P")
  493. (sp-wrap-with-pair ,s)))
  494. (provide 'prelude-core)
  495. ;;; prelude-core.el ends here