2013-03-07 09:57:33 +02:00
|
|
|
;;; prelude-core.el --- Emacs Prelude: Core Prelude functions.
|
2011-10-08 23:05:06 +03:00
|
|
|
;;
|
2015-03-03 13:04:34 +02:00
|
|
|
;; Copyright © 2011-2015 Bozhidar Batsov
|
2011-10-08 23:05:06 +03:00
|
|
|
;;
|
2012-04-17 17:35:21 +03:00
|
|
|
;; Author: Bozhidar Batsov <bozhidar@batsov.com>
|
2013-03-07 09:57:33 +02:00
|
|
|
;; URL: https://github.com/bbatsov/prelude
|
2011-10-08 23:05:06 +03:00
|
|
|
;; Version: 1.0.0
|
|
|
|
;; Keywords: convenience
|
|
|
|
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Here are the definitions of most of the functions added by Prelude.
|
|
|
|
|
|
|
|
;;; License:
|
|
|
|
|
|
|
|
;; This program is free software; you can redistribute it and/or
|
|
|
|
;; modify it under the terms of the GNU General Public License
|
|
|
|
;; as published by the Free Software Foundation; either version 3
|
|
|
|
;; of the License, or (at your option) any later version.
|
|
|
|
;;
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
;;
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
|
|
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
;; Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(require 'thingatpt)
|
2013-04-25 15:13:52 +03:00
|
|
|
(require 'dash)
|
2014-04-27 17:39:04 +03:00
|
|
|
(require 'ov)
|
2011-10-08 23:05:06 +03:00
|
|
|
|
2013-05-06 09:40:20 +03:00
|
|
|
(defun prelude-open-with (arg)
|
|
|
|
"Open visited file in default external program.
|
2014-05-06 12:15:18 +03:00
|
|
|
When in dired mode, open file under the cursor.
|
2013-05-06 09:40:20 +03:00
|
|
|
|
|
|
|
With a prefix ARG always prompt for command to use."
|
|
|
|
(interactive "P")
|
2014-09-04 18:37:24 +03:00
|
|
|
(let* ((current-file-name
|
|
|
|
(if (eq major-mode 'dired-mode)
|
|
|
|
(dired-get-file-for-visit)
|
|
|
|
buffer-file-name))
|
|
|
|
(open (pcase system-type
|
|
|
|
(`darwin "open")
|
2014-09-08 05:56:35 +03:00
|
|
|
((or `gnu `gnu/linux `gnu/kfreebsd) "xdg-open")))
|
2014-09-04 18:37:24 +03:00
|
|
|
(program (if (or arg (not open))
|
|
|
|
(read-shell-command "Open current file with: ")
|
|
|
|
open)))
|
|
|
|
(start-process "prelude-open-with-process" nil program current-file-name)))
|
2011-10-08 23:05:06 +03:00
|
|
|
|
|
|
|
(defun prelude-buffer-mode (buffer-or-name)
|
2013-03-07 09:57:33 +02:00
|
|
|
"Retrieve the `major-mode' of BUFFER-OR-NAME."
|
2013-04-05 14:43:15 +03:00
|
|
|
(with-current-buffer buffer-or-name
|
|
|
|
major-mode))
|
2011-10-08 23:05:06 +03:00
|
|
|
|
2014-11-06 17:12:23 +02:00
|
|
|
(defvar prelude-term-buffer-name "ansi"
|
|
|
|
"The default `ansi-term' name used by `prelude-visit-term-buffer'.
|
|
|
|
This variable can be set via .dir-locals.el to provide multi-term support.")
|
|
|
|
|
2011-10-08 23:05:06 +03:00
|
|
|
(defun prelude-visit-term-buffer ()
|
2013-03-07 09:57:33 +02:00
|
|
|
"Create or visit a terminal buffer."
|
2011-10-08 23:05:06 +03:00
|
|
|
(interactive)
|
2013-04-29 17:12:40 +03:00
|
|
|
(prelude-start-or-switch-to (lambda ()
|
2014-11-06 17:12:23 +02:00
|
|
|
(ansi-term (getenv "SHELL") (concat prelude-term-buffer-name "-term")))
|
|
|
|
(format "*%s-term*" prelude-term-buffer-name)))
|
2011-10-08 23:05:06 +03:00
|
|
|
|
2013-09-15 08:53:08 +03:00
|
|
|
(defun prelude-search (query-url prompt)
|
|
|
|
"Open the search url constructed with the QUERY-URL.
|
|
|
|
PROMPT sets the `read-string prompt."
|
|
|
|
(browse-url
|
|
|
|
(concat query-url
|
|
|
|
(url-hexify-string
|
|
|
|
(if mark-active
|
|
|
|
(buffer-substring (region-beginning) (region-end))
|
|
|
|
(read-string prompt))))))
|
|
|
|
|
2014-01-11 20:16:04 +02:00
|
|
|
(defmacro prelude-install-search-engine (search-engine-name search-engine-url search-engine-prompt)
|
|
|
|
"Given some information regarding a search engine, install the interactive command to search through them"
|
|
|
|
`(defun ,(intern (format "prelude-%s" search-engine-name)) ()
|
|
|
|
,(format "Search %s with a query or region if any." search-engine-name)
|
|
|
|
(interactive)
|
|
|
|
(prelude-search ,search-engine-url ,search-engine-prompt)))
|
|
|
|
|
2014-01-11 20:17:59 +02:00
|
|
|
(prelude-install-search-engine "google" "http://www.google.com/search?q=" "Google: ")
|
|
|
|
(prelude-install-search-engine "youtube" "http://www.youtube.com/results?search_query=" "Search YouTube: ")
|
|
|
|
(prelude-install-search-engine "github" "https://github.com/search?q=" "Search GitHub: ")
|
|
|
|
(prelude-install-search-engine "duckduckgo" "https://duckduckgo.com/?t=lm&q=" "Search DuckDuckGo: ")
|
2013-08-26 17:58:02 +03:00
|
|
|
|
2013-03-07 13:09:13 +02:00
|
|
|
(defun prelude-indent-rigidly-and-copy-to-clipboard (begin end arg)
|
|
|
|
"Indent region between BEGIN and END by ARG columns and copy to clipboard."
|
|
|
|
(interactive "r\nP")
|
|
|
|
(let ((arg (or arg 4))
|
|
|
|
(buffer (current-buffer)))
|
2011-10-08 23:05:06 +03:00
|
|
|
(with-temp-buffer
|
|
|
|
(insert-buffer-substring-no-properties buffer begin end)
|
2013-03-07 13:09:13 +02:00
|
|
|
(indent-rigidly (point-min) (point-max) arg)
|
2011-10-08 23:05:06 +03:00
|
|
|
(clipboard-kill-ring-save (point-min) (point-max)))))
|
|
|
|
|
2013-03-31 15:20:36 +03:00
|
|
|
(defun prelude-smart-open-line-above ()
|
|
|
|
"Insert an empty line above the current line.
|
2013-04-03 12:14:04 +03:00
|
|
|
Position the cursor at it's beginning, according to the current mode."
|
2013-03-31 15:20:36 +03:00
|
|
|
(interactive)
|
2013-05-30 06:18:18 +03:00
|
|
|
(move-beginning-of-line nil)
|
|
|
|
(newline-and-indent)
|
2013-04-03 12:14:04 +03:00
|
|
|
(forward-line -1)
|
2013-06-15 16:57:29 +03:00
|
|
|
(indent-according-to-mode))
|
2013-03-31 15:20:36 +03:00
|
|
|
|
2013-06-29 09:26:23 +03:00
|
|
|
(defun prelude-smart-open-line (arg)
|
2013-03-07 13:09:13 +02:00
|
|
|
"Insert an empty line after the current line.
|
2013-06-29 09:26:23 +03:00
|
|
|
Position the cursor at its beginning, according to the current mode.
|
|
|
|
|
|
|
|
With a prefix ARG open line above the current line."
|
|
|
|
(interactive "P")
|
|
|
|
(if arg
|
|
|
|
(prelude-smart-open-line-above)
|
|
|
|
(progn
|
|
|
|
(move-end-of-line nil)
|
2013-06-29 11:44:40 +03:00
|
|
|
(newline-and-indent))))
|
2011-10-08 23:05:06 +03:00
|
|
|
|
2013-05-30 16:50:55 +03:00
|
|
|
(defun prelude-top-join-line ()
|
|
|
|
"Join the current line with the line beneath it."
|
|
|
|
(interactive)
|
|
|
|
(delete-indentation 1))
|
|
|
|
|
2013-04-09 15:18:08 +03:00
|
|
|
(defun prelude-kill-whole-line (&optional arg)
|
|
|
|
"A simple wrapper around command `kill-whole-line' that respects indentation.
|
|
|
|
Passes ARG to command `kill-whole-line' when provided."
|
2013-10-03 18:00:23 +03:00
|
|
|
(interactive "p")
|
2013-04-09 15:18:08 +03:00
|
|
|
(kill-whole-line arg)
|
|
|
|
(back-to-indentation))
|
|
|
|
|
2013-05-23 11:27:01 +03:00
|
|
|
(defun prelude-move-beginning-of-line (arg)
|
|
|
|
"Move point back to indentation of beginning of line.
|
|
|
|
|
|
|
|
Move point to the first non-whitespace character on this line.
|
|
|
|
If point is already there, move to the beginning of the line.
|
|
|
|
Effectively toggle between the first non-whitespace character and
|
|
|
|
the beginning of the line.
|
|
|
|
|
|
|
|
If ARG is not nil or 1, move forward ARG - 1 lines first. If
|
|
|
|
point reaches the beginning or end of the buffer, stop there."
|
|
|
|
(interactive "^p")
|
|
|
|
(setq arg (or arg 1))
|
|
|
|
|
|
|
|
;; Move lines first
|
|
|
|
(when (/= arg 1)
|
|
|
|
(let ((line-move-visual nil))
|
|
|
|
(forward-line (1- arg))))
|
|
|
|
|
|
|
|
(let ((orig-point (point)))
|
|
|
|
(back-to-indentation)
|
|
|
|
(when (= orig-point (point))
|
|
|
|
(move-beginning-of-line 1))))
|
|
|
|
|
|
|
|
(global-set-key [remap move-beginning-of-line]
|
|
|
|
'prelude-move-beginning-of-line)
|
|
|
|
|
2013-03-28 12:43:45 +02:00
|
|
|
(defun prelude-indent-defun ()
|
|
|
|
"Indent the current defun."
|
|
|
|
(interactive)
|
|
|
|
(save-excursion
|
|
|
|
(mark-defun)
|
|
|
|
(indent-region (region-beginning) (region-end))))
|
|
|
|
|
2014-04-27 17:39:04 +03:00
|
|
|
(defun prelude-todo-ov-evaporate (_ov _after _beg _end &optional _length)
|
|
|
|
(let ((inhibit-modification-hooks t))
|
|
|
|
(if _after (ov-reset _ov))))
|
|
|
|
|
2011-10-08 23:05:06 +03:00
|
|
|
(defun prelude-annotate-todo ()
|
|
|
|
"Put fringe marker on TODO: lines in the curent buffer."
|
|
|
|
(interactive)
|
2014-04-27 17:39:04 +03:00
|
|
|
(ov-set (format "[[:space:]]*%s+[[:space:]]*TODO:" comment-start)
|
|
|
|
'before-string
|
|
|
|
(propertize (format "A")
|
|
|
|
'display '(left-fringe right-triangle))
|
|
|
|
'modification-hooks '(prelude-todo-ov-evaporate)))
|
2011-10-08 23:05:06 +03:00
|
|
|
|
2014-03-20 05:05:40 +02:00
|
|
|
(defun prelude-get-positions-of-line-or-region ()
|
|
|
|
"Return positions (beg . end) of the current line
|
|
|
|
or region."
|
|
|
|
(let (beg end)
|
2011-10-08 23:05:06 +03:00
|
|
|
(if (and mark-active (> (point) (mark)))
|
|
|
|
(exchange-point-and-mark))
|
|
|
|
(setq beg (line-beginning-position))
|
|
|
|
(if mark-active
|
|
|
|
(exchange-point-and-mark))
|
|
|
|
(setq end (line-end-position))
|
2014-03-20 05:05:40 +02:00
|
|
|
(cons beg end)))
|
|
|
|
|
|
|
|
(defun prelude-duplicate-current-line-or-region (arg)
|
|
|
|
"Duplicates the current line or region ARG times.
|
|
|
|
If there's no region, the current line will be duplicated. However, if
|
|
|
|
there's a region, all lines that region covers will be duplicated."
|
|
|
|
(interactive "p")
|
|
|
|
(pcase-let* ((origin (point))
|
|
|
|
(`(,beg . ,end) (prelude-get-positions-of-line-or-region))
|
|
|
|
(region (buffer-substring-no-properties beg end)))
|
|
|
|
(-dotimes arg
|
|
|
|
(lambda (n)
|
|
|
|
(goto-char end)
|
|
|
|
(newline)
|
|
|
|
(insert region)
|
|
|
|
(setq end (point))))
|
|
|
|
(goto-char (+ origin (* (length region) arg) arg))))
|
|
|
|
|
2013-07-13 14:15:16 +03:00
|
|
|
(defun prelude-duplicate-and-comment-current-line-or-region (arg)
|
|
|
|
"Duplicates and comments the current line or region ARG times.
|
|
|
|
If there's no region, the current line will be duplicated. However, if
|
|
|
|
there's a region, all lines that region covers will be duplicated."
|
|
|
|
(interactive "p")
|
2014-03-20 05:05:40 +02:00
|
|
|
(pcase-let* ((origin (point))
|
|
|
|
(`(,beg . ,end) (prelude-get-positions-of-line-or-region))
|
|
|
|
(region (buffer-substring-no-properties beg end)))
|
|
|
|
(comment-or-uncomment-region beg end)
|
2013-07-13 14:15:16 +03:00
|
|
|
(setq end (line-end-position))
|
2014-03-20 05:05:40 +02:00
|
|
|
(-dotimes arg
|
|
|
|
(lambda (n)
|
|
|
|
(goto-char end)
|
|
|
|
(newline)
|
|
|
|
(insert region)
|
|
|
|
(setq end (point))))
|
|
|
|
(goto-char (+ origin (* (length region) arg) arg))))
|
2013-07-13 14:15:16 +03:00
|
|
|
|
2014-05-31 20:11:42 +03:00
|
|
|
(defun prelude-rename-buffer-and-file ()
|
|
|
|
"Rename current buffer and if the buffer is visiting a file, rename it too."
|
2011-10-08 23:05:06 +03:00
|
|
|
(interactive)
|
2013-05-04 10:28:04 +03:00
|
|
|
(let ((filename (buffer-file-name)))
|
2011-10-08 23:05:06 +03:00
|
|
|
(if (not (and filename (file-exists-p filename)))
|
2014-05-31 20:11:42 +03:00
|
|
|
(rename-buffer (read-from-minibuffer "New name: " (buffer-name)))
|
2011-10-08 23:05:06 +03:00
|
|
|
(let ((new-name (read-file-name "New name: " filename)))
|
2013-05-04 10:28:04 +03:00
|
|
|
(cond
|
|
|
|
((vc-backend filename) (vc-rename-file filename new-name))
|
|
|
|
(t
|
|
|
|
(rename-file filename new-name t)
|
2013-05-09 18:55:26 +03:00
|
|
|
(set-visited-file-name new-name t t)))))))
|
2011-10-08 23:05:06 +03:00
|
|
|
|
|
|
|
(defun prelude-delete-file-and-buffer ()
|
2013-03-07 09:57:33 +02:00
|
|
|
"Kill the current buffer and deletes the file it is visiting."
|
2011-10-08 23:05:06 +03:00
|
|
|
(interactive)
|
|
|
|
(let ((filename (buffer-file-name)))
|
|
|
|
(when filename
|
2013-04-03 12:53:37 +03:00
|
|
|
(if (vc-backend filename)
|
|
|
|
(vc-delete-file filename)
|
2013-09-18 11:49:57 +03:00
|
|
|
(when (y-or-n-p (format "Are you sure you want to delete %s? " filename))
|
2013-04-03 12:53:37 +03:00
|
|
|
(delete-file filename)
|
|
|
|
(message "Deleted file %s" filename)
|
|
|
|
(kill-buffer))))))
|
2011-10-08 23:05:06 +03:00
|
|
|
|
|
|
|
(defun prelude-view-url ()
|
|
|
|
"Open a new buffer containing the contents of URL."
|
|
|
|
(interactive)
|
|
|
|
(let* ((default (thing-at-point-url-at-point))
|
|
|
|
(url (read-from-minibuffer "URL: " default)))
|
|
|
|
(switch-to-buffer (url-retrieve-synchronously url))
|
|
|
|
(rename-buffer url t)
|
2014-07-30 15:24:33 +03:00
|
|
|
(goto-char (point-min))
|
|
|
|
(re-search-forward "^$")
|
|
|
|
(delete-region (point-min) (point))
|
|
|
|
(delete-blank-lines)
|
|
|
|
(set-auto-mode)))
|
2011-10-08 23:05:06 +03:00
|
|
|
|
2014-05-30 20:52:30 +03:00
|
|
|
(defun prelude-cleanup-buffer-or-region ()
|
|
|
|
"Cleanup a region if selected, otherwise the whole buffer."
|
|
|
|
(interactive)
|
2014-06-25 04:36:23 +03:00
|
|
|
(call-interactively 'untabify)
|
2014-06-25 05:03:15 +03:00
|
|
|
(unless (member major-mode prelude-indent-sensitive-modes)
|
|
|
|
(call-interactively 'indent-region))
|
2011-11-23 22:44:49 +02:00
|
|
|
(whitespace-cleanup))
|
2011-10-08 23:05:06 +03:00
|
|
|
|
|
|
|
(defun prelude-eval-and-replace ()
|
|
|
|
"Replace the preceding sexp with its value."
|
|
|
|
(interactive)
|
2013-12-21 22:15:52 +02:00
|
|
|
(let ((value (eval (preceding-sexp))))
|
|
|
|
(backward-kill-sexp)
|
|
|
|
(insert (format "%s" value))))
|
2011-10-08 23:05:06 +03:00
|
|
|
|
|
|
|
(defun prelude-recompile-init ()
|
|
|
|
"Byte-compile all your dotfiles again."
|
|
|
|
(interactive)
|
2012-04-17 17:47:05 +03:00
|
|
|
(byte-recompile-directory prelude-dir 0))
|
2011-10-08 23:05:06 +03:00
|
|
|
|
|
|
|
(defun prelude-sudo-edit (&optional arg)
|
2013-04-21 10:01:43 +03:00
|
|
|
"Edit currently visited file as root.
|
|
|
|
|
|
|
|
With a prefix ARG prompt for a file to visit.
|
|
|
|
Will also prompt for a file to visit if current
|
|
|
|
buffer is not visiting a file."
|
|
|
|
(interactive "P")
|
2011-10-08 23:05:06 +03:00
|
|
|
(if (or arg (not buffer-file-name))
|
2013-04-21 10:01:43 +03:00
|
|
|
(find-file (concat "/sudo:root@localhost:"
|
|
|
|
(ido-read-file-name "Find file(as root): ")))
|
|
|
|
(find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
|
|
|
|
|
|
|
|
(defadvice ido-find-file (after find-file-sudo activate)
|
|
|
|
"Find file as root if necessary."
|
2014-07-30 15:04:38 +03:00
|
|
|
(unless (or (tramp-tramp-file-p buffer-file-name)
|
|
|
|
(equal major-mode 'dired-mode)
|
|
|
|
(not (file-exists-p (file-name-directory buffer-file-name)))
|
|
|
|
(file-writable-p buffer-file-name))
|
2011-10-08 23:05:06 +03:00
|
|
|
(find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
|
|
|
|
|
2013-04-29 07:59:51 +03:00
|
|
|
(defun prelude-start-or-switch-to (function buffer-name)
|
|
|
|
"Invoke FUNCTION if there is no buffer with BUFFER-NAME.
|
|
|
|
Otherwise switch to the buffer named BUFFER-NAME. Don't clobber
|
|
|
|
the current buffer."
|
|
|
|
(if (not (get-buffer buffer-name))
|
|
|
|
(progn
|
|
|
|
(split-window-sensibly (selected-window))
|
|
|
|
(other-window 1)
|
|
|
|
(funcall function))
|
|
|
|
(switch-to-buffer-other-window buffer-name)))
|
2011-10-08 23:05:06 +03:00
|
|
|
|
|
|
|
(defun prelude-insert-date ()
|
2013-03-07 09:57:33 +02:00
|
|
|
"Insert a timestamp according to locale's date and time format."
|
2011-10-08 23:05:06 +03:00
|
|
|
(interactive)
|
|
|
|
(insert (format-time-string "%c" (current-time))))
|
|
|
|
|
|
|
|
(defun prelude-recentf-ido-find-file ()
|
|
|
|
"Find a recent file using ido."
|
|
|
|
(interactive)
|
2013-04-28 17:35:24 +03:00
|
|
|
(let ((file (ido-completing-read "Choose recent file: "
|
|
|
|
(-map 'abbreviate-file-name recentf-list)
|
|
|
|
nil t)))
|
2011-10-08 23:05:06 +03:00
|
|
|
(when file
|
|
|
|
(find-file file))))
|
|
|
|
|
|
|
|
(defun prelude-swap-windows ()
|
|
|
|
"If you have 2 windows, it swaps them."
|
|
|
|
(interactive)
|
2011-10-10 17:50:23 +03:00
|
|
|
(if (/= (count-windows) 2)
|
|
|
|
(message "You need exactly 2 windows to do this.")
|
2012-12-17 17:43:35 +02:00
|
|
|
(let* ((w1 (car (window-list)))
|
|
|
|
(w2 (cadr (window-list)))
|
2011-10-10 17:50:23 +03:00
|
|
|
(b1 (window-buffer w1))
|
|
|
|
(b2 (window-buffer w2))
|
|
|
|
(s1 (window-start w1))
|
|
|
|
(s2 (window-start w2)))
|
|
|
|
(set-window-buffer w1 b2)
|
|
|
|
(set-window-buffer w2 b1)
|
|
|
|
(set-window-start w1 s2)
|
|
|
|
(set-window-start w2 s1)))
|
2011-10-08 23:05:06 +03:00
|
|
|
(other-window 1))
|
|
|
|
|
2013-04-24 11:24:30 +03:00
|
|
|
(defun prelude-switch-to-previous-buffer ()
|
|
|
|
"Switch to previously open buffer.
|
|
|
|
Repeated invocations toggle between the two most recently open buffers."
|
|
|
|
(interactive)
|
|
|
|
(switch-to-buffer (other-buffer (current-buffer) 1)))
|
|
|
|
|
2012-01-26 14:44:02 +02:00
|
|
|
(defun prelude-kill-other-buffers ()
|
2013-03-07 09:57:33 +02:00
|
|
|
"Kill all buffers but the current one.
|
|
|
|
Doesn't mess with special buffers."
|
2012-01-26 14:44:02 +02:00
|
|
|
(interactive)
|
2015-02-13 09:53:07 +02:00
|
|
|
(when (y-or-n-p "Are you sure you want to kill all buffers but the current one? ")
|
|
|
|
(-each
|
|
|
|
(->> (buffer-list)
|
|
|
|
(-filter #'buffer-file-name)
|
|
|
|
(--remove (eql (current-buffer) it)))
|
|
|
|
#'kill-buffer)))
|
2012-01-26 14:44:02 +02:00
|
|
|
|
2012-09-06 14:09:47 +03:00
|
|
|
(defun prelude-create-scratch-buffer ()
|
|
|
|
"Create a new scratch buffer."
|
|
|
|
(interactive)
|
2014-07-25 13:55:00 +03:00
|
|
|
(let ((buf (generate-new-buffer "*scratch*")))
|
|
|
|
(switch-to-buffer buf)
|
|
|
|
(funcall initial-major-mode)))
|
2012-09-06 14:09:47 +03:00
|
|
|
|
2012-05-07 18:04:03 +03:00
|
|
|
(defvar prelude-tips
|
|
|
|
'("Press <C-c o> to open a file with external program."
|
2014-08-14 15:19:48 +03:00
|
|
|
"Press <C-c p f> to navigate a project's files with ido."
|
2015-03-09 08:57:02 +02:00
|
|
|
"Press <s-r> to open a recently visited file."
|
2014-08-23 18:59:50 +03:00
|
|
|
"Press <C-c p s g> to run grep on a project."
|
|
|
|
"Press <C-c p p> to switch between projects."
|
2014-08-14 13:43:58 +03:00
|
|
|
"Press <C-=> to expand the selected region."
|
2012-05-07 18:04:03 +03:00
|
|
|
"Press <C-c g> to search in Google."
|
2013-09-15 08:53:08 +03:00
|
|
|
"Press <C-c G> to search in GitHub."
|
|
|
|
"Press <C-c y> to search in YouTube."
|
2014-01-11 20:19:59 +02:00
|
|
|
"Press <C-c U> to search in DuckDuckGo."
|
2014-05-31 20:11:42 +03:00
|
|
|
"Press <C-c r> to rename the current buffer and the file it's visiting if any."
|
2012-05-07 18:04:03 +03:00
|
|
|
"Press <C-c t> to open a terminal in Emacs."
|
2013-07-23 16:51:02 +03:00
|
|
|
"Press <C-c k> to kill all the buffers, but the active one."
|
2014-08-14 15:19:48 +03:00
|
|
|
"Press <C-x g> to run magit-status."
|
2013-07-23 16:51:02 +03:00
|
|
|
"Press <C-c D> to delete the current file and buffer."
|
|
|
|
"Press <C-c s> to swap two windows."
|
2014-07-16 03:26:05 +03:00
|
|
|
"Press <S-RET> or <M-o> to open a line beneath the current one."
|
2013-07-23 16:51:02 +03:00
|
|
|
"Press <s-o> to open a line above the current one."
|
|
|
|
"Press <C-c C-z> in a Elisp buffer to launch an interactive Elisp shell."
|
2013-07-26 17:54:20 +03:00
|
|
|
"Press <C-Backspace> to kill a line backwards."
|
|
|
|
"Press <C-S-Backspace> or <s-k> to kill the whole line."
|
2015-03-09 08:51:27 +02:00
|
|
|
"Press <s-j> or <C-^> to join lines."
|
2015-03-09 09:12:21 +02:00
|
|
|
"Press <s-.> or <C-c j> to jump to the start of a word in any visible window."
|
2013-07-26 17:54:20 +03:00
|
|
|
"Press <f11> to toggle fullscreen mode."
|
|
|
|
"Press <f12> to toggle the menu bar."
|
|
|
|
"Explore the Tools->Prelude menu to find out about some of Prelude extensions to Emacs."
|
2012-05-07 18:06:32 +03:00
|
|
|
"Access the official Emacs manual by pressing <C-h r>."
|
2013-11-19 13:52:27 +02:00
|
|
|
"Visit the EmacsWiki at http://emacswiki.org to find out even more about Emacs."))
|
2012-05-07 18:04:03 +03:00
|
|
|
|
|
|
|
(defun prelude-tip-of-the-day ()
|
2013-03-07 09:57:33 +02:00
|
|
|
"Display a random entry from `prelude-tips'."
|
2012-05-07 18:04:03 +03:00
|
|
|
(interactive)
|
2013-05-19 14:16:43 +03:00
|
|
|
(unless (window-minibuffer-p)
|
|
|
|
;; pick a new random seed
|
|
|
|
(random t)
|
|
|
|
(message
|
|
|
|
(concat "Prelude tip: " (nth (random (length prelude-tips)) prelude-tips)))))
|
2012-05-07 18:04:03 +03:00
|
|
|
|
2012-05-16 14:50:30 +03:00
|
|
|
(defun prelude-eval-after-init (form)
|
|
|
|
"Add `(lambda () FORM)' to `after-init-hook'.
|
|
|
|
|
|
|
|
If Emacs has already finished initialization, also eval FORM immediately."
|
|
|
|
(let ((func (list 'lambda nil form)))
|
|
|
|
(add-hook 'after-init-hook func)
|
|
|
|
(when after-init-time
|
|
|
|
(eval form))))
|
|
|
|
|
2013-10-22 15:37:05 +03:00
|
|
|
(require 'epl)
|
|
|
|
|
2013-03-11 20:13:06 +02:00
|
|
|
(defun prelude-update ()
|
|
|
|
"Update Prelude to its latest version."
|
|
|
|
(interactive)
|
2013-05-23 11:29:22 +03:00
|
|
|
(when (y-or-n-p "Do you want to update Prelude? ")
|
2013-10-22 15:37:05 +03:00
|
|
|
(message "Updating installed packages...")
|
|
|
|
(epl-upgrade)
|
2013-03-11 20:13:06 +02:00
|
|
|
(message "Updating Prelude...")
|
|
|
|
(cd prelude-dir)
|
|
|
|
(shell-command "git pull")
|
2013-05-31 13:05:59 +03:00
|
|
|
(prelude-recompile-init)
|
2013-03-11 20:13:06 +02:00
|
|
|
(message "Update finished. Restart Emacs to complete the process.")))
|
|
|
|
|
2013-12-07 01:01:54 +02:00
|
|
|
(defun prelude-update-packages (&optional arg)
|
2013-12-06 16:31:58 +02:00
|
|
|
"Update Prelude's packages.
|
2013-12-07 01:01:54 +02:00
|
|
|
This includes package installed via `prelude-require-package'.
|
2013-12-06 16:31:58 +02:00
|
|
|
|
2013-12-07 01:01:54 +02:00
|
|
|
With a prefix ARG updates all installed packages."
|
|
|
|
(interactive "P")
|
2013-12-06 16:31:58 +02:00
|
|
|
(when (y-or-n-p "Do you want to update Prelude's packages? ")
|
2013-12-07 01:01:54 +02:00
|
|
|
(if arg
|
|
|
|
(epl-upgrade)
|
2013-12-07 01:21:54 +02:00
|
|
|
(epl-upgrade (-filter (lambda (p) (memq (epl-package-name p) prelude-packages))
|
|
|
|
(epl-installed-packages))))
|
2013-12-06 16:31:58 +02:00
|
|
|
(message "Update finished. Restart Emacs to complete the process.")))
|
|
|
|
|
2013-07-26 15:17:34 +03:00
|
|
|
;;; Emacs in OSX already has fullscreen support
|
2013-07-26 17:36:18 +03:00
|
|
|
;;; Emacs has a similar built-in command in 24.4
|
|
|
|
(defun prelude-fullscreen ()
|
|
|
|
"Make Emacs window fullscreen.
|
2013-07-26 15:17:34 +03:00
|
|
|
|
|
|
|
This follows freedesktop standards, should work in X servers."
|
2013-07-26 17:36:18 +03:00
|
|
|
(interactive)
|
|
|
|
(if (eq window-system 'x)
|
|
|
|
(x-send-client-message nil 0 nil "_NET_WM_STATE" 32
|
|
|
|
'(2 "_NET_WM_STATE_FULLSCREEN" 0))
|
|
|
|
(error "Only X server is supported")))
|
2013-07-25 20:01:02 +03:00
|
|
|
|
2014-05-15 23:54:38 +03:00
|
|
|
(defun prelude-find-user-init-file (&optional arg)
|
2014-05-15 23:48:24 +03:00
|
|
|
"Edit the `prelude-user-init-file', in another window.
|
|
|
|
With a prefix argument ARG, find the `user-init-file' instead."
|
|
|
|
(interactive "P")
|
|
|
|
(if arg (find-file-other-window user-init-file)
|
|
|
|
(find-file-other-window prelude-user-init-file)))
|
2013-09-27 11:18:47 +03:00
|
|
|
|
2013-09-27 11:36:47 +03:00
|
|
|
(defun prelude-find-shell-init-file ()
|
|
|
|
"Edit the shell init file in another window."
|
|
|
|
(interactive)
|
|
|
|
(let* ((shell (car (reverse (s-split "/" (getenv "SHELL")))))
|
|
|
|
(shell-init-file (cond
|
|
|
|
((s-equals? "zsh" shell) ".zshrc")
|
|
|
|
((s-equals? "bash" shell) ".bashrc")
|
|
|
|
(t (error "Unknown shell")))))
|
|
|
|
(find-file-other-window (expand-file-name shell-init-file (getenv "HOME")))))
|
|
|
|
|
2013-12-07 09:14:44 +02:00
|
|
|
(defun prelude-wrap-with (s)
|
|
|
|
"Create a wrapper function for smartparens using S."
|
|
|
|
`(lambda (&optional arg)
|
|
|
|
(interactive "P")
|
|
|
|
(sp-wrap-with-pair ,s)))
|
|
|
|
|
2015-01-26 19:33:34 +02:00
|
|
|
;; needed for prelude-goto-symbol
|
|
|
|
(require 'imenu)
|
|
|
|
|
2014-09-19 12:05:50 +03:00
|
|
|
(defun prelude-goto-symbol (&optional symbol-list)
|
2014-09-14 09:13:16 +03:00
|
|
|
"Refresh imenu and jump to a place in the buffer using Ido."
|
|
|
|
(interactive)
|
|
|
|
(cond
|
|
|
|
((not symbol-list)
|
2014-09-14 09:21:15 +03:00
|
|
|
(let (name-and-pos symbol-names position)
|
2014-09-14 09:13:16 +03:00
|
|
|
(while (progn
|
|
|
|
(imenu--cleanup)
|
|
|
|
(setq imenu--index-alist nil)
|
2014-09-19 12:05:50 +03:00
|
|
|
(prelude-goto-symbol (imenu--make-index-alist))
|
2014-09-14 09:13:16 +03:00
|
|
|
(setq selected-symbol
|
2014-09-19 12:05:50 +03:00
|
|
|
(completing-read "Symbol? " (reverse symbol-names)))
|
2014-09-14 09:13:16 +03:00
|
|
|
(string= (car imenu--rescan-item) selected-symbol)))
|
|
|
|
(unless (and (boundp 'mark-active) mark-active)
|
|
|
|
(push-mark nil t nil))
|
|
|
|
(setq position (cdr (assoc selected-symbol name-and-pos)))
|
|
|
|
(cond
|
|
|
|
((overlayp position)
|
|
|
|
(goto-char (overlay-start position)))
|
|
|
|
(t
|
|
|
|
(goto-char position)))
|
|
|
|
(recenter)))
|
|
|
|
((listp symbol-list)
|
|
|
|
(dolist (symbol symbol-list)
|
|
|
|
(let (name position)
|
|
|
|
(cond
|
|
|
|
((and (listp symbol) (imenu--subalist-p symbol))
|
2014-09-19 12:05:50 +03:00
|
|
|
(prelude-goto-symbol symbol))
|
2014-09-14 09:13:16 +03:00
|
|
|
((listp symbol)
|
|
|
|
(setq name (car symbol))
|
|
|
|
(setq position (cdr symbol)))
|
|
|
|
((stringp symbol)
|
|
|
|
(setq name symbol)
|
|
|
|
(setq position
|
|
|
|
(get-text-property 1 'org-imenu-marker symbol))))
|
|
|
|
(unless (or (null position) (null name)
|
|
|
|
(string= (car imenu--rescan-item) name))
|
|
|
|
(add-to-list 'symbol-names (substring-no-properties name))
|
|
|
|
(add-to-list 'name-and-pos (cons (substring-no-properties name) position))))))))
|
|
|
|
|
2011-10-08 23:05:06 +03:00
|
|
|
(provide 'prelude-core)
|
|
|
|
;;; prelude-core.el ends here
|