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.

487 lines
18 KiB

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. (require 'ov)
  31. (defun prelude-open-with (arg)
  32. "Open visited file in default external program.
  33. When in dired mode, open file under the cursor.
  34. With a prefix ARG always prompt for command to use."
  35. (interactive "P")
  36. (let* ((current-file-name
  37. (if (eq major-mode 'dired-mode)
  38. (dired-get-file-for-visit)
  39. buffer-file-name))
  40. (open (pcase system-type
  41. (`darwin "open")
  42. ((pred (string-prefix-p "gnu")) "xdg-open")))
  43. (program (if (or arg (not open))
  44. (read-shell-command "Open current file with: ")
  45. open)))
  46. (start-process "prelude-open-with-process" nil program current-file-name)))
  47. (defun prelude-buffer-mode (buffer-or-name)
  48. "Retrieve the `major-mode' of BUFFER-OR-NAME."
  49. (with-current-buffer buffer-or-name
  50. major-mode))
  51. (defun prelude-visit-term-buffer ()
  52. "Create or visit a terminal buffer."
  53. (interactive)
  54. (prelude-start-or-switch-to (lambda ()
  55. (ansi-term (getenv "SHELL")))
  56. "*ansi-term*"))
  57. (defun prelude-search (query-url prompt)
  58. "Open the search url constructed with the QUERY-URL.
  59. PROMPT sets the `read-string prompt."
  60. (browse-url
  61. (concat query-url
  62. (url-hexify-string
  63. (if mark-active
  64. (buffer-substring (region-beginning) (region-end))
  65. (read-string prompt))))))
  66. (defmacro prelude-install-search-engine (search-engine-name search-engine-url search-engine-prompt)
  67. "Given some information regarding a search engine, install the interactive command to search through them"
  68. `(defun ,(intern (format "prelude-%s" search-engine-name)) ()
  69. ,(format "Search %s with a query or region if any." search-engine-name)
  70. (interactive)
  71. (prelude-search ,search-engine-url ,search-engine-prompt)))
  72. (prelude-install-search-engine "google" "http://www.google.com/search?q=" "Google: ")
  73. (prelude-install-search-engine "youtube" "http://www.youtube.com/results?search_query=" "Search YouTube: ")
  74. (prelude-install-search-engine "github" "https://github.com/search?q=" "Search GitHub: ")
  75. (prelude-install-search-engine "duckduckgo" "https://duckduckgo.com/?t=lm&q=" "Search DuckDuckGo: ")
  76. (defun prelude-indent-rigidly-and-copy-to-clipboard (begin end arg)
  77. "Indent region between BEGIN and END by ARG columns and copy to clipboard."
  78. (interactive "r\nP")
  79. (let ((arg (or arg 4))
  80. (buffer (current-buffer)))
  81. (with-temp-buffer
  82. (insert-buffer-substring-no-properties buffer begin end)
  83. (indent-rigidly (point-min) (point-max) arg)
  84. (clipboard-kill-ring-save (point-min) (point-max)))))
  85. (defun prelude-smart-open-line-above ()
  86. "Insert an empty line above the current line.
  87. Position the cursor at it's beginning, according to the current mode."
  88. (interactive)
  89. (move-beginning-of-line nil)
  90. (newline-and-indent)
  91. (forward-line -1)
  92. (indent-according-to-mode))
  93. (defun prelude-smart-open-line (arg)
  94. "Insert an empty line after the current line.
  95. Position the cursor at its beginning, according to the current mode.
  96. With a prefix ARG open line above the current line."
  97. (interactive "P")
  98. (if arg
  99. (prelude-smart-open-line-above)
  100. (progn
  101. (move-end-of-line nil)
  102. (newline-and-indent))))
  103. (defun prelude-top-join-line ()
  104. "Join the current line with the line beneath it."
  105. (interactive)
  106. (delete-indentation 1))
  107. (defun prelude-kill-whole-line (&optional arg)
  108. "A simple wrapper around command `kill-whole-line' that respects indentation.
  109. Passes ARG to command `kill-whole-line' when provided."
  110. (interactive "p")
  111. (kill-whole-line arg)
  112. (back-to-indentation))
  113. (defun prelude-move-beginning-of-line (arg)
  114. "Move point back to indentation of beginning of line.
  115. Move point to the first non-whitespace character on this line.
  116. If point is already there, move to the beginning of the line.
  117. Effectively toggle between the first non-whitespace character and
  118. the beginning of the line.
  119. If ARG is not nil or 1, move forward ARG - 1 lines first. If
  120. point reaches the beginning or end of the buffer, stop there."
  121. (interactive "^p")
  122. (setq arg (or arg 1))
  123. ;; Move lines first
  124. (when (/= arg 1)
  125. (let ((line-move-visual nil))
  126. (forward-line (1- arg))))
  127. (let ((orig-point (point)))
  128. (back-to-indentation)
  129. (when (= orig-point (point))
  130. (move-beginning-of-line 1))))
  131. (global-set-key [remap move-beginning-of-line]
  132. 'prelude-move-beginning-of-line)
  133. (defun prelude-indent-defun ()
  134. "Indent the current defun."
  135. (interactive)
  136. (save-excursion
  137. (mark-defun)
  138. (indent-region (region-beginning) (region-end))))
  139. (defun prelude-todo-ov-evaporate (_ov _after _beg _end &optional _length)
  140. (let ((inhibit-modification-hooks t))
  141. (if _after (ov-reset _ov))))
  142. (defun prelude-annotate-todo ()
  143. "Put fringe marker on TODO: lines in the curent buffer."
  144. (interactive)
  145. (ov-set (format "[[:space:]]*%s+[[:space:]]*TODO:" comment-start)
  146. 'before-string
  147. (propertize (format "A")
  148. 'display '(left-fringe right-triangle))
  149. 'modification-hooks '(prelude-todo-ov-evaporate)))
  150. (defun prelude-copy-file-name-to-clipboard ()
  151. "Copy the current buffer file name to the clipboard."
  152. (interactive)
  153. (let ((filename (if (equal major-mode 'dired-mode)
  154. default-directory
  155. (buffer-file-name))))
  156. (when filename
  157. (kill-new filename)
  158. (message "Copied buffer file name '%s' to the clipboard." filename))))
  159. (defun prelude-get-positions-of-line-or-region ()
  160. "Return positions (beg . end) of the current line
  161. or region."
  162. (let (beg end)
  163. (if (and mark-active (> (point) (mark)))
  164. (exchange-point-and-mark))
  165. (setq beg (line-beginning-position))
  166. (if mark-active
  167. (exchange-point-and-mark))
  168. (setq end (line-end-position))
  169. (cons beg end)))
  170. (defun prelude-duplicate-current-line-or-region (arg)
  171. "Duplicates the current line or region ARG times.
  172. If there's no region, the current line will be duplicated. However, if
  173. there's a region, all lines that region covers will be duplicated."
  174. (interactive "p")
  175. (pcase-let* ((origin (point))
  176. (`(,beg . ,end) (prelude-get-positions-of-line-or-region))
  177. (region (buffer-substring-no-properties beg end)))
  178. (-dotimes arg
  179. (lambda (n)
  180. (goto-char end)
  181. (newline)
  182. (insert region)
  183. (setq end (point))))
  184. (goto-char (+ origin (* (length region) arg) arg))))
  185. (defun prelude-duplicate-and-comment-current-line-or-region (arg)
  186. "Duplicates and comments the current line or region ARG times.
  187. If there's no region, the current line will be duplicated. However, if
  188. there's a region, all lines that region covers will be duplicated."
  189. (interactive "p")
  190. (pcase-let* ((origin (point))
  191. (`(,beg . ,end) (prelude-get-positions-of-line-or-region))
  192. (region (buffer-substring-no-properties beg end)))
  193. (comment-or-uncomment-region beg end)
  194. (setq end (line-end-position))
  195. (-dotimes arg
  196. (lambda (n)
  197. (goto-char end)
  198. (newline)
  199. (insert region)
  200. (setq end (point))))
  201. (goto-char (+ origin (* (length region) arg) arg))))
  202. (defun prelude-rename-buffer-and-file ()
  203. "Rename current buffer and if the buffer is visiting a file, rename it too."
  204. (interactive)
  205. (let ((filename (buffer-file-name)))
  206. (if (not (and filename (file-exists-p filename)))
  207. (rename-buffer (read-from-minibuffer "New name: " (buffer-name)))
  208. (let ((new-name (read-file-name "New name: " filename)))
  209. (cond
  210. ((vc-backend filename) (vc-rename-file filename new-name))
  211. (t
  212. (rename-file filename new-name t)
  213. (set-visited-file-name new-name t t)))))))
  214. (defun prelude-delete-file-and-buffer ()
  215. "Kill the current buffer and deletes the file it is visiting."
  216. (interactive)
  217. (let ((filename (buffer-file-name)))
  218. (when filename
  219. (if (vc-backend filename)
  220. (vc-delete-file filename)
  221. (when (y-or-n-p (format "Are you sure you want to delete %s? " filename))
  222. (delete-file filename)
  223. (message "Deleted file %s" filename)
  224. (kill-buffer))))))
  225. (defun prelude-view-url ()
  226. "Open a new buffer containing the contents of URL."
  227. (interactive)
  228. (let* ((default (thing-at-point-url-at-point))
  229. (url (read-from-minibuffer "URL: " default)))
  230. (switch-to-buffer (url-retrieve-synchronously url))
  231. (rename-buffer url t)
  232. (goto-char (point-min))
  233. (re-search-forward "^$")
  234. (delete-region (point-min) (point))
  235. (delete-blank-lines)
  236. (set-auto-mode)))
  237. (defun prelude-cleanup-buffer-or-region ()
  238. "Cleanup a region if selected, otherwise the whole buffer."
  239. (interactive)
  240. (call-interactively 'untabify)
  241. (unless (member major-mode prelude-indent-sensitive-modes)
  242. (call-interactively 'indent-region))
  243. (whitespace-cleanup))
  244. (defun prelude-eval-and-replace ()
  245. "Replace the preceding sexp with its value."
  246. (interactive)
  247. (let ((value (eval (preceding-sexp))))
  248. (backward-kill-sexp)
  249. (insert (format "%s" value))))
  250. (defun prelude-recompile-init ()
  251. "Byte-compile all your dotfiles again."
  252. (interactive)
  253. (byte-recompile-directory prelude-dir 0))
  254. (defun prelude-sudo-edit (&optional arg)
  255. "Edit currently visited file as root.
  256. With a prefix ARG prompt for a file to visit.
  257. Will also prompt for a file to visit if current
  258. buffer is not visiting a file."
  259. (interactive "P")
  260. (if (or arg (not buffer-file-name))
  261. (find-file (concat "/sudo:root@localhost:"
  262. (ido-read-file-name "Find file(as root): ")))
  263. (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
  264. (defadvice ido-find-file (after find-file-sudo activate)
  265. "Find file as root if necessary."
  266. (unless (or (tramp-tramp-file-p buffer-file-name)
  267. (equal major-mode 'dired-mode)
  268. (not (file-exists-p (file-name-directory buffer-file-name)))
  269. (file-writable-p buffer-file-name))
  270. (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
  271. (defun prelude-start-or-switch-to (function buffer-name)
  272. "Invoke FUNCTION if there is no buffer with BUFFER-NAME.
  273. Otherwise switch to the buffer named BUFFER-NAME. Don't clobber
  274. the current buffer."
  275. (if (not (get-buffer buffer-name))
  276. (progn
  277. (split-window-sensibly (selected-window))
  278. (other-window 1)
  279. (funcall function))
  280. (switch-to-buffer-other-window buffer-name)))
  281. (defun prelude-insert-date ()
  282. "Insert a timestamp according to locale's date and time format."
  283. (interactive)
  284. (insert (format-time-string "%c" (current-time))))
  285. (defun prelude-recentf-ido-find-file ()
  286. "Find a recent file using ido."
  287. (interactive)
  288. (let ((file (ido-completing-read "Choose recent file: "
  289. (-map 'abbreviate-file-name recentf-list)
  290. nil t)))
  291. (when file
  292. (find-file file))))
  293. (defun prelude-swap-windows ()
  294. "If you have 2 windows, it swaps them."
  295. (interactive)
  296. (if (/= (count-windows) 2)
  297. (message "You need exactly 2 windows to do this.")
  298. (let* ((w1 (car (window-list)))
  299. (w2 (cadr (window-list)))
  300. (b1 (window-buffer w1))
  301. (b2 (window-buffer w2))
  302. (s1 (window-start w1))
  303. (s2 (window-start w2)))
  304. (set-window-buffer w1 b2)
  305. (set-window-buffer w2 b1)
  306. (set-window-start w1 s2)
  307. (set-window-start w2 s1)))
  308. (other-window 1))
  309. (defun prelude-switch-to-previous-buffer ()
  310. "Switch to previously open buffer.
  311. Repeated invocations toggle between the two most recently open buffers."
  312. (interactive)
  313. (switch-to-buffer (other-buffer (current-buffer) 1)))
  314. (defun prelude-kill-other-buffers ()
  315. "Kill all buffers but the current one.
  316. Doesn't mess with special buffers."
  317. (interactive)
  318. (-each
  319. (->> (buffer-list)
  320. (-filter #'buffer-file-name)
  321. (--remove (eql (current-buffer) it)))
  322. #'kill-buffer))
  323. (defun prelude-create-scratch-buffer ()
  324. "Create a new scratch buffer."
  325. (interactive)
  326. (let ((buf (generate-new-buffer "*scratch*")))
  327. (switch-to-buffer buf)
  328. (funcall initial-major-mode)))
  329. (defvar prelude-tips
  330. '("Press <C-c o> to open a file with external program."
  331. "Press <C-c p f> to navigate a project's files with ido."
  332. "Press <C-c p s g> to run grep on a project."
  333. "Press <C-c p p> to switch between projects."
  334. "Press <C-=> to expand the selected region."
  335. "Press <C-c g> to search in Google."
  336. "Press <C-c G> to search in GitHub."
  337. "Press <C-c y> to search in YouTube."
  338. "Press <C-c U> to search in DuckDuckGo."
  339. "Press <C-c r> to rename the current buffer and the file it's visiting if any."
  340. "Press <C-c t> to open a terminal in Emacs."
  341. "Press <C-c k> to kill all the buffers, but the active one."
  342. "Press <C-x g> to run magit-status."
  343. "Press <C-c D> to delete the current file and buffer."
  344. "Press <C-c s> to swap two windows."
  345. "Press <S-RET> or <M-o> to open a line beneath the current one."
  346. "Press <s-o> to open a line above the current one."
  347. "Press <C-c C-z> in a Elisp buffer to launch an interactive Elisp shell."
  348. "Press <C-Backspace> to kill a line backwards."
  349. "Press <C-S-Backspace> or <s-k> to kill the whole line."
  350. "Press <f11> to toggle fullscreen mode."
  351. "Press <f12> to toggle the menu bar."
  352. "Explore the Tools->Prelude menu to find out about some of Prelude extensions to Emacs."
  353. "Access the official Emacs manual by pressing <C-h r>."
  354. "Visit the EmacsWiki at http://emacswiki.org to find out even more about Emacs."))
  355. (defun prelude-tip-of-the-day ()
  356. "Display a random entry from `prelude-tips'."
  357. (interactive)
  358. (unless (window-minibuffer-p)
  359. ;; pick a new random seed
  360. (random t)
  361. (message
  362. (concat "Prelude tip: " (nth (random (length prelude-tips)) prelude-tips)))))
  363. (defun prelude-eval-after-init (form)
  364. "Add `(lambda () FORM)' to `after-init-hook'.
  365. If Emacs has already finished initialization, also eval FORM immediately."
  366. (let ((func (list 'lambda nil form)))
  367. (add-hook 'after-init-hook func)
  368. (when after-init-time
  369. (eval form))))
  370. (require 'epl)
  371. (defun prelude-update ()
  372. "Update Prelude to its latest version."
  373. (interactive)
  374. (when (y-or-n-p "Do you want to update Prelude? ")
  375. (message "Updating installed packages...")
  376. (epl-upgrade)
  377. (message "Updating Prelude...")
  378. (cd prelude-dir)
  379. (shell-command "git pull")
  380. (prelude-recompile-init)
  381. (message "Update finished. Restart Emacs to complete the process.")))
  382. (defun prelude-update-packages (&optional arg)
  383. "Update Prelude's packages.
  384. This includes package installed via `prelude-require-package'.
  385. With a prefix ARG updates all installed packages."
  386. (interactive "P")
  387. (when (y-or-n-p "Do you want to update Prelude's packages? ")
  388. (if arg
  389. (epl-upgrade)
  390. (epl-upgrade (-filter (lambda (p) (memq (epl-package-name p) prelude-packages))
  391. (epl-installed-packages))))
  392. (message "Update finished. Restart Emacs to complete the process.")))
  393. ;;; Emacs in OSX already has fullscreen support
  394. ;;; Emacs has a similar built-in command in 24.4
  395. (defun prelude-fullscreen ()
  396. "Make Emacs window fullscreen.
  397. This follows freedesktop standards, should work in X servers."
  398. (interactive)
  399. (if (eq window-system 'x)
  400. (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
  401. '(2 "_NET_WM_STATE_FULLSCREEN" 0))
  402. (error "Only X server is supported")))
  403. (defun prelude-find-user-init-file (&optional arg)
  404. "Edit the `prelude-user-init-file', in another window.
  405. With a prefix argument ARG, find the `user-init-file' instead."
  406. (interactive "P")
  407. (if arg (find-file-other-window user-init-file)
  408. (find-file-other-window prelude-user-init-file)))
  409. (defun prelude-find-shell-init-file ()
  410. "Edit the shell init file in another window."
  411. (interactive)
  412. (let* ((shell (car (reverse (s-split "/" (getenv "SHELL")))))
  413. (shell-init-file (cond
  414. ((s-equals? "zsh" shell) ".zshrc")
  415. ((s-equals? "bash" shell) ".bashrc")
  416. (t (error "Unknown shell")))))
  417. (find-file-other-window (expand-file-name shell-init-file (getenv "HOME")))))
  418. (defun prelude-wrap-with (s)
  419. "Create a wrapper function for smartparens using S."
  420. `(lambda (&optional arg)
  421. (interactive "P")
  422. (sp-wrap-with-pair ,s)))
  423. (provide 'prelude-core)
  424. ;;; prelude-core.el ends here