2019-12-14 21:28:52 +00:00
|
|
|
|
;;; funcs.el --- Ivy Layer functions File for Spacemacs -*- lexical-binding: t; -*-
|
2016-05-16 02:00:44 +00:00
|
|
|
|
;;
|
2019-11-04 07:20:43 +00:00
|
|
|
|
;; Copyright (c) 2012-2019 Sylvain Benner & Contributors
|
2016-05-16 02:00:44 +00:00
|
|
|
|
;;
|
|
|
|
|
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
|
|
|
|
|
;; URL: https://github.com/syl20bnr/spacemacs
|
|
|
|
|
;;
|
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
|
;;
|
|
|
|
|
;;; License: GPLv3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Counsel
|
|
|
|
|
|
|
|
|
|
;; async
|
|
|
|
|
|
|
|
|
|
(defvar spacemacs--counsel-initial-cands-shown nil)
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//counsel-async-command (cmd)
|
|
|
|
|
(let* ((counsel--process " *counsel*")
|
|
|
|
|
(proc (get-process counsel--process))
|
|
|
|
|
(buff (get-buffer counsel--process)))
|
|
|
|
|
(when proc
|
|
|
|
|
(delete-process proc))
|
|
|
|
|
(when buff
|
|
|
|
|
(kill-buffer buff))
|
|
|
|
|
(setq proc (start-process-shell-command
|
|
|
|
|
counsel--process
|
|
|
|
|
counsel--process
|
|
|
|
|
cmd))
|
|
|
|
|
(setq spacemacs--counsel-initial-cands-shown nil)
|
|
|
|
|
(setq counsel--async-time (current-time))
|
|
|
|
|
(set-process-sentinel proc #'counsel--async-sentinel)
|
|
|
|
|
(set-process-filter proc #'spacemacs//counsel-async-filter)))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//counsel-async-filter (process str)
|
2019-03-03 15:20:12 +00:00
|
|
|
|
(with-current-buffer (process-buffer process)
|
|
|
|
|
(insert str))
|
|
|
|
|
(when (or (null spacemacs--counsel-initial-cands-shown)
|
|
|
|
|
(time-less-p
|
|
|
|
|
;; 0.5s
|
|
|
|
|
'(0 0 500000 0)
|
|
|
|
|
(time-since counsel--async-time)))
|
|
|
|
|
(let (size display-now)
|
|
|
|
|
(with-current-buffer (process-buffer process)
|
|
|
|
|
(goto-char (point-min))
|
|
|
|
|
(setq size (- (buffer-size) (forward-line (buffer-size))))
|
|
|
|
|
(when (and (null spacemacs--counsel-initial-cands-shown)
|
|
|
|
|
(> size spacemacs--counsel-initial-number-cand))
|
|
|
|
|
(setq ivy--all-candidates
|
|
|
|
|
(split-string (buffer-string) "\n" t))
|
|
|
|
|
(setq display-now t)
|
|
|
|
|
(setq spacemacs--counsel-initial-cands-shown t)))
|
|
|
|
|
(let ((ivy--prompt
|
2019-03-03 15:05:17 +00:00
|
|
|
|
(ivy-add-prompt-count
|
|
|
|
|
(format (ivy-state-prompt ivy-last)
|
|
|
|
|
size))))
|
2019-03-03 15:20:12 +00:00
|
|
|
|
(if display-now
|
|
|
|
|
(ivy--insert-minibuffer
|
|
|
|
|
(ivy--format ivy--all-candidates))
|
|
|
|
|
(ivy--insert-prompt))))
|
|
|
|
|
(setq counsel--async-time (current-time))))
|
2016-05-16 02:00:44 +00:00
|
|
|
|
|
|
|
|
|
;; search
|
|
|
|
|
|
|
|
|
|
(defvar spacemacs--counsel-search-cmd)
|
|
|
|
|
|
|
|
|
|
;; see `counsel-ag-function'
|
|
|
|
|
(defun spacemacs//make-counsel-search-function (tool)
|
2019-12-14 21:28:52 +00:00
|
|
|
|
(let ((base-cmd (cdr (assoc-string tool spacemacs--counsel-commands))))
|
2019-03-03 15:20:12 +00:00
|
|
|
|
(lambda (string &optional _pred &rest _unused)
|
|
|
|
|
"Grep in the current directory for STRING."
|
2019-10-14 08:30:27 +00:00
|
|
|
|
;; `ivy-more-chars' returns non-nil when more chars are needed,
|
|
|
|
|
;; minimal chars count is configurable via `ivy-more-chars-alist'
|
|
|
|
|
(or (ivy-more-chars)
|
2019-03-03 15:20:12 +00:00
|
|
|
|
(let* ((default-directory (ivy-state-directory ivy-last))
|
|
|
|
|
(args (if (string-match-p " -- " string)
|
|
|
|
|
(let ((split (split-string string " -- ")))
|
|
|
|
|
(prog1 (pop split)
|
|
|
|
|
(setq string (mapconcat #'identity split " -- "))))
|
|
|
|
|
""))
|
|
|
|
|
(regex (counsel--elisp-to-pcre
|
|
|
|
|
(setq ivy--old-re
|
|
|
|
|
(ivy--regex string)))))
|
|
|
|
|
(setq spacemacs--counsel-search-cmd (format base-cmd args regex))
|
|
|
|
|
(spacemacs//counsel-async-command spacemacs--counsel-search-cmd)
|
|
|
|
|
nil)))))
|
2016-05-16 02:00:44 +00:00
|
|
|
|
|
2016-06-08 09:32:47 +00:00
|
|
|
|
(defun spacemacs//counsel-save-in-buffer ()
|
|
|
|
|
(interactive)
|
|
|
|
|
(ivy-quit-and-run
|
2019-03-03 15:20:12 +00:00
|
|
|
|
(let ((buf "*ivy results*"))
|
|
|
|
|
(with-current-buffer (get-buffer-create buf)
|
|
|
|
|
(erase-buffer)
|
|
|
|
|
(dolist (c ivy--all-candidates)
|
|
|
|
|
(insert c "\n"))
|
|
|
|
|
(spacemacs//gne-init-counsel))
|
|
|
|
|
(pop-to-buffer buf))))
|
2016-06-08 09:32:47 +00:00
|
|
|
|
|
2016-12-28 06:12:51 +00:00
|
|
|
|
(defun spacemacs//counsel-edit ()
|
|
|
|
|
"Edit the current search results in a buffer using wgrep."
|
|
|
|
|
(interactive)
|
2018-07-16 16:42:50 +00:00
|
|
|
|
(run-with-idle-timer 0 nil 'spacemacs/ivy-wgrep-change-to-wgrep-mode)
|
2016-12-28 06:12:51 +00:00
|
|
|
|
(ivy-occur))
|
|
|
|
|
|
2016-06-08 09:32:47 +00:00
|
|
|
|
(defun spacemacs//gne-init-counsel ()
|
|
|
|
|
(with-current-buffer "*ivy results*"
|
|
|
|
|
(setq spacemacs--gne-min-line 1
|
|
|
|
|
spacemacs--gne-max-line
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char (point-max))
|
|
|
|
|
(previous-line)
|
|
|
|
|
(line-number-at-pos))
|
|
|
|
|
spacemacs--gne-line-func
|
|
|
|
|
(lambda (c)
|
2018-01-04 01:29:07 +00:00
|
|
|
|
(counsel-git-grep-action c))
|
2016-10-09 16:57:46 +00:00
|
|
|
|
next-error-function 'spacemacs/gne-next)))
|
2016-06-08 09:32:47 +00:00
|
|
|
|
|
2019-11-24 20:06:23 +00:00
|
|
|
|
(defun spacemacs//counsel-search-add-extra-bindings (map)
|
|
|
|
|
"Add extra counsel-search related keybindings to MAP, then return MAP.
|
|
|
|
|
See `spacemacs/counsel-search' and `counsel-ag'."
|
|
|
|
|
(define-key map (kbd "<f3>") 'spacemacs//counsel-save-in-buffer)
|
|
|
|
|
(define-key map (kbd "C-c C-e") 'spacemacs//counsel-edit)
|
|
|
|
|
map)
|
|
|
|
|
|
|
|
|
|
(defvar spacemacs--counsel-map (spacemacs//counsel-search-add-extra-bindings
|
|
|
|
|
(make-sparse-keymap)))
|
2016-06-08 09:32:47 +00:00
|
|
|
|
|
2018-07-13 06:03:40 +00:00
|
|
|
|
(defun spacemacs/ivy--regex-plus (str)
|
2018-07-14 19:52:25 +00:00
|
|
|
|
"Build a regex sequence from STR.
|
|
|
|
|
Same as `ivy--regex-plus', but with special consideration for
|
|
|
|
|
`spacemacs/counsel-search', thus providing correct highlighting
|
|
|
|
|
in the search results. Can be used in `ivy-re-builders-alist',
|
|
|
|
|
for example by setting the variable's value to:
|
|
|
|
|
((t . spacemacs/ivy--regex-plus))
|
|
|
|
|
"
|
2018-07-13 06:03:40 +00:00
|
|
|
|
(if (and (eq (ivy-state-caller ivy-last) 'spacemacs/counsel-search)
|
|
|
|
|
(string-match-p " -- " str))
|
|
|
|
|
(ivy--regex-plus (car (last (split-string str " -- "))))
|
|
|
|
|
(ivy--regex-plus str)))
|
|
|
|
|
|
2016-05-16 02:00:44 +00:00
|
|
|
|
;; see `counsel-ag'
|
|
|
|
|
(defun spacemacs/counsel-search
|
2019-03-03 15:20:12 +00:00
|
|
|
|
(&optional tools use-initial-input initial-directory)
|
|
|
|
|
"Search using the first available tool in TOOLS. Default tool
|
2016-05-16 02:00:44 +00:00
|
|
|
|
to try is grep. If INPUT is non nil, use the region or the symbol
|
|
|
|
|
around point as the initial input. If DIR is non nil start in
|
|
|
|
|
that directory."
|
2019-03-03 15:20:12 +00:00
|
|
|
|
(interactive)
|
|
|
|
|
(require 'counsel)
|
2020-02-03 10:05:37 +00:00
|
|
|
|
(cl-letf* ((initial-input (if use-initial-input
|
|
|
|
|
(if (region-active-p)
|
|
|
|
|
(buffer-substring-no-properties
|
|
|
|
|
(region-beginning) (region-end))
|
|
|
|
|
(thing-at-point 'symbol t))
|
|
|
|
|
""))
|
|
|
|
|
(tool (catch 'tool
|
|
|
|
|
(dolist (tool tools)
|
|
|
|
|
(when (and (assoc-string tool spacemacs--counsel-commands)
|
|
|
|
|
(executable-find tool))
|
|
|
|
|
(throw 'tool tool)))
|
|
|
|
|
(throw 'tool "grep")))
|
|
|
|
|
(default-directory
|
|
|
|
|
(or initial-directory (read-directory-name "Start from directory: ")))
|
|
|
|
|
(display-directory
|
|
|
|
|
(if (< (length default-directory)
|
|
|
|
|
spacemacs--counsel-search-max-path-length)
|
|
|
|
|
default-directory
|
|
|
|
|
(concat
|
|
|
|
|
"..." (substring default-directory
|
|
|
|
|
(- (length default-directory)
|
|
|
|
|
spacemacs--counsel-search-max-path-length)
|
|
|
|
|
(length default-directory))))))
|
2019-11-06 00:10:05 +00:00
|
|
|
|
(cond ((string= tool "ag")
|
2019-11-06 20:16:36 +00:00
|
|
|
|
(counsel-ag initial-input default-directory nil
|
2019-11-04 07:20:43 +00:00
|
|
|
|
(format "ag from [%s]: " display-directory)))
|
2019-11-06 00:10:05 +00:00
|
|
|
|
((string= tool "rg")
|
2019-11-06 20:16:36 +00:00
|
|
|
|
(counsel-rg initial-input default-directory nil
|
2019-11-04 07:20:43 +00:00
|
|
|
|
(format "rg from [%s]: " display-directory)))
|
|
|
|
|
(t
|
|
|
|
|
(ivy-read
|
|
|
|
|
(format "%s from [%s]: "
|
|
|
|
|
tool
|
|
|
|
|
display-directory)
|
|
|
|
|
(spacemacs//make-counsel-search-function tool)
|
|
|
|
|
:initial-input (when initial-input (rxt-quote-pcre initial-input))
|
|
|
|
|
:dynamic-collection t
|
|
|
|
|
:history 'counsel-git-grep-history
|
|
|
|
|
:action #'counsel-git-grep-action
|
|
|
|
|
:caller 'spacemacs/counsel-search
|
|
|
|
|
:keymap spacemacs--counsel-map
|
|
|
|
|
:unwind (lambda ()
|
|
|
|
|
(counsel-delete-process)
|
|
|
|
|
(swiper--cleanup)))))))
|
2016-05-16 02:00:44 +00:00
|
|
|
|
|
|
|
|
|
;; Define search functions for each tool
|
|
|
|
|
(cl-loop
|
2019-03-03 15:20:12 +00:00
|
|
|
|
for (tools tool-name) in '((dotspacemacs-search-tools "auto")
|
|
|
|
|
((list "rg") "rg")
|
|
|
|
|
((list "ag") "ag")
|
|
|
|
|
((list "pt") "pt")
|
|
|
|
|
((list "ack") "ack")
|
|
|
|
|
((list "grep") "grep"))
|
|
|
|
|
do
|
|
|
|
|
(eval
|
|
|
|
|
`(progn
|
|
|
|
|
(defun ,(intern (format "spacemacs/search-%s" tool-name)) ()
|
|
|
|
|
,(format
|
|
|
|
|
"Use `spacemacs/counsel-search' to search in the current
|
2016-05-16 02:00:44 +00:00
|
|
|
|
directory with %s." (if (string= tool-name "auto")
|
|
|
|
|
"a tool selected from `dotspacemacs-search-tools'."
|
|
|
|
|
tool-name))
|
2019-03-03 15:20:12 +00:00
|
|
|
|
(interactive)
|
|
|
|
|
(spacemacs/counsel-search ,tools))
|
|
|
|
|
(defun ,(intern (format "spacemacs/search-%s-region-or-symbol"
|
|
|
|
|
tool-name)) ()
|
|
|
|
|
,(format
|
|
|
|
|
"Use `spacemacs/counsel-search' to search for
|
2016-05-16 02:00:44 +00:00
|
|
|
|
the selected region or the symbol around point in the current
|
|
|
|
|
directory with %s." (if (string= tool-name "auto")
|
|
|
|
|
"a tool selected from `dotspacemacs-search-tools'."
|
|
|
|
|
tool-name))
|
2019-03-03 15:20:12 +00:00
|
|
|
|
(interactive)
|
|
|
|
|
(spacemacs/counsel-search ,tools t))
|
|
|
|
|
(defun ,(intern (format "spacemacs/search-project-%s" tool-name)) ()
|
|
|
|
|
,(format
|
|
|
|
|
"Use `spacemacs/counsel-search' to search in the current
|
2016-05-16 02:00:44 +00:00
|
|
|
|
project with %s." (if (string= tool-name "auto")
|
|
|
|
|
"a tool selected from `dotspacemacs-search-tools'."
|
|
|
|
|
tool-name))
|
2019-03-03 15:20:12 +00:00
|
|
|
|
(interactive)
|
|
|
|
|
(spacemacs/counsel-search ,tools nil (projectile-project-root)))
|
|
|
|
|
(defun ,(intern (format "spacemacs/search-project-%s-region-or-symbol"
|
|
|
|
|
tool-name)) ()
|
|
|
|
|
,(format
|
|
|
|
|
"Use `spacemacs/counsel-search' to search for
|
2016-05-16 02:00:44 +00:00
|
|
|
|
the selected region or the symbol around point in the current
|
|
|
|
|
project with %s." (if (string= tool-name "auto")
|
|
|
|
|
"a tool selected from `dotspacemacs-search-tools'."
|
|
|
|
|
tool-name))
|
2019-03-03 15:20:12 +00:00
|
|
|
|
(interactive)
|
|
|
|
|
(spacemacs/counsel-search ,tools t (projectile-project-root)))
|
|
|
|
|
(defun ,(intern (format "spacemacs/search-dir-%s" tool-name)) ()
|
|
|
|
|
,(format
|
|
|
|
|
"Use `spacemacs/counsel-search' to search in the current
|
2017-07-01 10:41:26 +00:00
|
|
|
|
directory with %s." (if (string= tool-name "auto")
|
2019-03-03 15:20:12 +00:00
|
|
|
|
"a tool selected from `dotspacemacs-search-tools'."
|
|
|
|
|
tool-name))
|
|
|
|
|
(interactive)
|
|
|
|
|
(spacemacs/counsel-search ,tools nil default-directory))
|
|
|
|
|
(defun ,(intern (format "spacemacs/search-dir-%s-region-or-symbol" tool-name)) ()
|
|
|
|
|
,(format
|
|
|
|
|
"Use `spacemacs/counsel-search' to search for
|
2017-07-01 10:41:26 +00:00
|
|
|
|
the selected region or the symbol around point in the current
|
|
|
|
|
directory with %s." (if (string= tool-name "auto")
|
2019-03-03 15:20:12 +00:00
|
|
|
|
"a tool selected from `dotspacemacs-search-tools'."
|
|
|
|
|
tool-name))
|
|
|
|
|
(interactive)
|
|
|
|
|
(spacemacs/counsel-search ,tools t default-directory)))))
|
2016-05-16 02:00:44 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs/counsel-git-grep-region-or-symbol ()
|
|
|
|
|
"Use `counsel-git-grep' to search for the selected region or
|
|
|
|
|
the symbol around point in the current project with git grep."
|
|
|
|
|
(interactive)
|
|
|
|
|
(let ((input (if (region-active-p)
|
|
|
|
|
(buffer-substring-no-properties
|
|
|
|
|
(region-beginning) (region-end))
|
|
|
|
|
(thing-at-point 'symbol t))))
|
2019-10-26 20:07:51 +00:00
|
|
|
|
(counsel-git-grep input)))
|
2016-05-16 02:00:44 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs/counsel-search-docs ()
|
|
|
|
|
"Search spacemacs docs using `spacemacs/counsel-search'"
|
|
|
|
|
(interactive)
|
|
|
|
|
(spacemacs/counsel-search dotspacemacs-search-tools
|
|
|
|
|
nil spacemacs-docs-directory))
|
|
|
|
|
|
2019-10-26 20:01:48 +00:00
|
|
|
|
(defun spacemacs//counsel-occur (&optional candidates)
|
2016-05-16 02:00:44 +00:00
|
|
|
|
"Generate a custom occur buffer for `counsel-git-grep'."
|
|
|
|
|
(ivy-occur-grep-mode)
|
2018-01-04 01:29:07 +00:00
|
|
|
|
(setq default-directory (ivy-state-directory ivy-last))
|
2019-10-31 09:55:23 +00:00
|
|
|
|
(let ((cands (or candidates ivy--old-cands))
|
2019-10-26 20:01:48 +00:00
|
|
|
|
(inhibit-read-only t))
|
2016-05-16 02:00:44 +00:00
|
|
|
|
;; Need precise number of header lines for `wgrep' to work.
|
|
|
|
|
(insert (format "-*- mode:grep; default-directory: %S -*-\n\n\n"
|
|
|
|
|
default-directory))
|
|
|
|
|
(insert (format "%d candidates:\n" (length cands)))
|
|
|
|
|
(ivy--occur-insert-lines
|
|
|
|
|
(mapcar
|
|
|
|
|
(lambda (cand) (concat "./" cand))
|
2019-10-26 20:01:48 +00:00
|
|
|
|
cands))))
|
2016-05-16 02:00:44 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs/counsel-up-directory-no-error ()
|
|
|
|
|
"`counsel-up-directory' ignoring errors."
|
|
|
|
|
(interactive)
|
|
|
|
|
(ignore-errors
|
|
|
|
|
(call-interactively 'counsel-up-directory)))
|
|
|
|
|
|
2017-07-02 13:48:06 +00:00
|
|
|
|
(when (configuration-layer/package-used-p 'counsel)
|
2016-05-16 02:00:44 +00:00
|
|
|
|
(with-eval-after-load 'counsel
|
|
|
|
|
(defun spacemacs/describe-mode ()
|
|
|
|
|
"Dummy wrapper to prevent an key binding error from helm.
|
|
|
|
|
|
|
|
|
|
By default the emacs leader is M-m, turns out that Helm does this:
|
|
|
|
|
(cl-dolist (k (where-is-internal 'describe-mode global-map))
|
|
|
|
|
(define-key map k 'helm-help))
|
|
|
|
|
after doing this:
|
|
|
|
|
(define-key map (kbd \"M-m\") 'helm-toggle-all-marks)
|
|
|
|
|
So when Helm is loaded we get the error:
|
|
|
|
|
Key sequence M-m h d m starts with non-prefix key M-m
|
|
|
|
|
|
|
|
|
|
To prevent this error we just wrap `describe-mode' to defeat the
|
|
|
|
|
Helm hack."
|
|
|
|
|
(interactive)
|
|
|
|
|
(call-interactively 'describe-mode))))
|
|
|
|
|
|
2016-05-18 00:08:08 +00:00
|
|
|
|
(defun spacemacs//counsel-with-git-grep (func x)
|
|
|
|
|
(when (string-match "\\`\\(.*?\\):\\([0-9]+\\):\\(.*\\)\\'" x)
|
|
|
|
|
(with-ivy-window
|
|
|
|
|
(let ((file-name (match-string-no-properties 1 x))
|
|
|
|
|
(line-number (match-string-no-properties 2 x)))
|
|
|
|
|
(funcall func
|
2018-01-04 01:29:07 +00:00
|
|
|
|
(expand-file-name file-name (ivy-state-directory ivy-last)))
|
2016-05-18 00:08:08 +00:00
|
|
|
|
(goto-char (point-min))
|
|
|
|
|
(forward-line (1- (string-to-number line-number)))
|
|
|
|
|
(re-search-forward (ivy--regex ivy-text t) (line-end-position) t)
|
|
|
|
|
(unless (eq ivy-exit 'done)
|
|
|
|
|
(swiper--cleanup)
|
|
|
|
|
(swiper--add-overlays (ivy--regex ivy-text)))))))
|
2018-01-06 16:51:19 +00:00
|
|
|
|
|
|
|
|
|
;; org
|
|
|
|
|
|
|
|
|
|
;; see https://github.com/abo-abo/swiper/issues/177
|
|
|
|
|
(defun spacemacs//counsel-org-ctrl-c-ctrl-c-org-tag ()
|
|
|
|
|
"Hook for `org-ctrl-c-ctrl-c-hook' to use `counsel-org-tag'."
|
|
|
|
|
(if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
|
|
|
|
|
(or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
|
|
|
|
|
(user-error "C-c C-c can do nothing useful at this location"))
|
|
|
|
|
(let* ((context (org-element-context))
|
|
|
|
|
(type (org-element-type context)))
|
2019-12-18 08:58:56 +00:00
|
|
|
|
(cl-case type
|
2018-01-06 16:51:19 +00:00
|
|
|
|
;; When at a link, act according to the parent instead.
|
|
|
|
|
(link (setq context (org-element-property :parent context))
|
|
|
|
|
(setq type (org-element-type context)))
|
|
|
|
|
;; Unsupported object types: refer to the first supported
|
|
|
|
|
;; element or object containing it.
|
|
|
|
|
((bold code entity export-snippet inline-babel-call inline-src-block
|
|
|
|
|
italic latex-fragment line-break macro strike-through subscript
|
|
|
|
|
superscript underline verbatim)
|
|
|
|
|
(setq context
|
|
|
|
|
(org-element-lineage
|
|
|
|
|
context '(radio-target paragraph verse-block table-cell)))))
|
|
|
|
|
;; For convenience: at the first line of a paragraph on the
|
|
|
|
|
;; same line as an item, apply function on that item instead.
|
|
|
|
|
(when (eq type 'paragraph)
|
|
|
|
|
(let ((parent (org-element-property :parent context)))
|
|
|
|
|
(when (and (eq (org-element-type parent) 'item)
|
|
|
|
|
(= (line-beginning-position)
|
|
|
|
|
(org-element-property :begin parent)))
|
|
|
|
|
(setq context parent type 'item))))
|
|
|
|
|
|
|
|
|
|
;; Act according to type of element or object at point.
|
2019-12-18 08:58:56 +00:00
|
|
|
|
(cl-case type
|
2018-01-06 16:51:19 +00:00
|
|
|
|
((headline inlinetask)
|
|
|
|
|
(save-excursion (goto-char (org-element-property :begin context))
|
|
|
|
|
(call-interactively 'counsel-org-tag)) t)))))
|
2018-06-06 19:37:58 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs/counsel-jump-in-buffer ()
|
2018-07-15 05:21:46 +00:00
|
|
|
|
"Jump in buffer with `counsel-imenu' or `counsel-org-goto' if in org-mode"
|
2018-06-06 19:37:58 +00:00
|
|
|
|
(interactive)
|
|
|
|
|
(call-interactively
|
|
|
|
|
(cond
|
|
|
|
|
((eq major-mode 'org-mode) 'counsel-org-goto)
|
|
|
|
|
(t 'counsel-imenu))))
|
|
|
|
|
|
2016-05-16 02:00:44 +00:00
|
|
|
|
|
|
|
|
|
;; Ivy
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//ivy-command-not-implemented-yet (key)
|
2019-12-14 21:28:52 +00:00
|
|
|
|
(let ((-key key))
|
2016-05-16 02:00:44 +00:00
|
|
|
|
(spacemacs/set-leader-keys
|
|
|
|
|
-key (lambda ()
|
|
|
|
|
(interactive)
|
2016-05-16 02:24:27 +00:00
|
|
|
|
(message (concat "The command usually bound to %s %s has "
|
|
|
|
|
"not been implemented for the `ivy' layer yet.")
|
2016-05-16 02:00:44 +00:00
|
|
|
|
dotspacemacs-leader-key -key)))))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/ivy-available-repls ()
|
|
|
|
|
"Show available repls."
|
|
|
|
|
(interactive)
|
|
|
|
|
(ivy-read "Repls: "
|
|
|
|
|
(mapcar #'car spacemacs-repl-list)
|
|
|
|
|
:action (lambda (candidate)
|
|
|
|
|
(let ((repl (cdr (assoc candidate spacemacs-repl-list))))
|
|
|
|
|
(require (car repl))
|
|
|
|
|
(call-interactively (cdr repl))))))
|
|
|
|
|
|
2018-07-16 16:42:50 +00:00
|
|
|
|
(defun spacemacs/ivy-wgrep-change-to-wgrep-mode ()
|
|
|
|
|
(interactive)
|
|
|
|
|
(ivy-wgrep-change-to-wgrep-mode)
|
|
|
|
|
(evil-normal-state))
|
|
|
|
|
|
2016-06-10 02:50:24 +00:00
|
|
|
|
;; Evil
|
2016-06-08 07:28:53 +00:00
|
|
|
|
|
2016-06-10 02:50:24 +00:00
|
|
|
|
(defun spacemacs/ivy-evil-registers ()
|
|
|
|
|
"Show evil registers"
|
|
|
|
|
(interactive)
|
|
|
|
|
(let ((ivy-height 24))
|
|
|
|
|
(ivy-read "Evil Registers:"
|
|
|
|
|
(cl-loop for (key . val) in (evil-register-list)
|
|
|
|
|
collect (eval `(format "%s : %s" (propertize ,(char-to-string key) 'face 'font-lock-builtin-face)
|
|
|
|
|
,(or (and val
|
|
|
|
|
(stringp val)
|
|
|
|
|
(replace-regexp-in-string "\n" "^J" val))
|
|
|
|
|
""))))
|
|
|
|
|
:action #'spacemacs/ivy-insert-evil-register)))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/ivy-insert-evil-register (candidate)
|
|
|
|
|
(insert (replace-regexp-in-string "\\^J" "\n"
|
|
|
|
|
(substring-no-properties candidate 4))))
|
2016-06-08 07:28:53 +00:00
|
|
|
|
|
2016-06-01 03:39:47 +00:00
|
|
|
|
;; Layouts
|
2016-05-16 02:00:44 +00:00
|
|
|
|
|
2016-06-01 03:39:47 +00:00
|
|
|
|
(defun spacemacs/ivy-spacemacs-layouts ()
|
|
|
|
|
"Control Panel for Spacemacs layouts. Has many actions.
|
2016-05-16 02:00:44 +00:00
|
|
|
|
If match is found
|
2016-06-01 03:39:47 +00:00
|
|
|
|
\(default) Select layout
|
|
|
|
|
c: Close Layout(s) <- mark with C-SPC to close more than one-window
|
|
|
|
|
k: Kill Layout(s)
|
spacemacs-layouts: Fix adding buffers to new persp
Delete the hook that commit 9fcf8c898dd1069ef362f1ede320e85065358ac6 added
to persp-created-functions, and change spacemacs/ivy-spacemacs-layouts,
spacemacs/helm-perspectives, spacemacs/helm-persp-switch-project, and
spacemacs/ivy-persp-switch-project to achieve the intended goal of adding
the desired buffers after creating a new perspective.
Change spacemacs/helm-persp-switch-project and
spacemacs/ivy-persp-switch-project as follows: If the user selects a
project but then quits without selecting a file or buffer, the new
perspective is now immediately killed. Otherwise, if the perspective did
not already exist, any buffers that belong to the selected project are
added to the perspective.
Add the following actions to spacemacs/ivy-spacemacs-layouts and
spacemacs/helm-perspectives:
* Create a new perspective with the Spacemacs home buffer (default action).
* Create a new perspective with the buffers that belong to the current
buffer's project.
* Create a new perspective with the buffers that belong to the current
perspective (i.e., make a copy of the current perspective).
This commit resolves the problem reported in
https://github.com/syl20bnr/spacemacs/commit/9fcf8c898dd1069ef362f1ede320e85065358ac6#commitcomment-33343455.
This commit also fixes a problem with the default action for
spacemacs/ivy-spacemacs-layouts and spacemacs/helm-perspectives. According to
commit 7b931a9f5b4f996394e902b254ba85e9fe1b22be, the default action is supposed
to display the home buffer if the action creates a new perspective, which was
determined by checking whether the perspective was missing from the list of
perspectives _before_ switching. However, commit
eb7ca651fe72241a5784c289543f73b6c273faf4 changed this logic so it was checking
whether the perspective was missing from the list of perspectives _after_
switching. This commit restores the correct logic.
* CHANGELOG.develop: Update.
* layers/+completion/ivy/funcs.el (spacemacs/ivy-spacemacs-layouts): Add
"Copy Current Layout" and "Create Project Layout" actions to the docstring.
Use spacemacs//create-persp-with-home-buffer for the default action.
* layers/+completion/ivy/packages.el (ivy/post-init-persp-mode): Add
actions for spacemacs//create-persp-with-current-project-buffers and
persp-copy.
* layers/+spacemacs/spacemacs-layouts/funcs.el
(spacemacs||switch-layout): New macro. Switch to the named perspective,
and initialize it using the provided forms if the perspective is new.
(spacemacs//create-persp-with-current-project-buffers): New function.
Create a new perspective with the current project's buffers.
(spacemacs||switch-project-persp): New macro. Switch to the named
perspective, and evaluate the provided forms with
projectile-after-switch-project-hook bound with a hook that adds the
current project's buffers to the perspective. If the user quits during the
evaluation of the forms, kill the perspective.
(spacemacs//create-persp-with-home-buffer): New function. Switch to the
named perspective, and go to the Spacemacs home buffer if the perspective
is new.
(spacemacs/helm-perspectives): Use spacemacs//create-persp-with-home-buffer
for the default action. Add actions for
spacemacs//create-persp-with-current-project-buffers and persp-copy.
(spacemacs//helm-persp-switch-project-action): New function. Switch to the
named perspective and call projectile-switch-project-by-name, using the new
spacemacs||switch-project-persp macro. Bind helm-quit-hook with a hook
that kills the new perspective if the user quits
projectile-switch-project-by-name.
(spacemacs/helm-persp-switch-project): Use
spacemacs//helm-persp-switch-project-action.
(spacemacs//ivy-persp-switch-project-action): New function. Switch to the
named perspective and call counsel-projectile-switch-project-action with a
hook to add the project's buffers to the new perspective.
(spacemacs/ivy-persp-switch-project): Delete advice for
counsel-projectile-switch-project-action. Use
spacemacs//ivy-persp-switch-project-action instead.
(spacemacs//add-project-buffers-to-persp): Deleted.
* layers/+spacemacs/spacemacs-layouts/packages.el
(spacemacs-layouts/init-persp-mode): Don't add a hook to
persp-created-functions.
2019-05-10 23:48:18 +00:00
|
|
|
|
n: Copy current layout
|
|
|
|
|
p: Create project layout
|
2016-05-16 02:00:44 +00:00
|
|
|
|
|
|
|
|
|
If match is not found
|
2016-06-01 03:39:47 +00:00
|
|
|
|
<enter> Creates layout
|
2016-05-16 02:00:44 +00:00
|
|
|
|
|
2016-06-01 03:39:47 +00:00
|
|
|
|
Closing doesn't kill buffers inside the layout while killing layouts does."
|
2016-05-16 02:00:44 +00:00
|
|
|
|
(interactive)
|
2016-06-01 03:39:47 +00:00
|
|
|
|
(ivy-read "Layouts: "
|
2016-05-16 02:00:44 +00:00
|
|
|
|
(persp-names)
|
2016-06-01 03:39:47 +00:00
|
|
|
|
:caller 'spacemacs/ivy-spacemacs-layouts
|
spacemacs-layouts: Fix adding buffers to new persp
Delete the hook that commit 9fcf8c898dd1069ef362f1ede320e85065358ac6 added
to persp-created-functions, and change spacemacs/ivy-spacemacs-layouts,
spacemacs/helm-perspectives, spacemacs/helm-persp-switch-project, and
spacemacs/ivy-persp-switch-project to achieve the intended goal of adding
the desired buffers after creating a new perspective.
Change spacemacs/helm-persp-switch-project and
spacemacs/ivy-persp-switch-project as follows: If the user selects a
project but then quits without selecting a file or buffer, the new
perspective is now immediately killed. Otherwise, if the perspective did
not already exist, any buffers that belong to the selected project are
added to the perspective.
Add the following actions to spacemacs/ivy-spacemacs-layouts and
spacemacs/helm-perspectives:
* Create a new perspective with the Spacemacs home buffer (default action).
* Create a new perspective with the buffers that belong to the current
buffer's project.
* Create a new perspective with the buffers that belong to the current
perspective (i.e., make a copy of the current perspective).
This commit resolves the problem reported in
https://github.com/syl20bnr/spacemacs/commit/9fcf8c898dd1069ef362f1ede320e85065358ac6#commitcomment-33343455.
This commit also fixes a problem with the default action for
spacemacs/ivy-spacemacs-layouts and spacemacs/helm-perspectives. According to
commit 7b931a9f5b4f996394e902b254ba85e9fe1b22be, the default action is supposed
to display the home buffer if the action creates a new perspective, which was
determined by checking whether the perspective was missing from the list of
perspectives _before_ switching. However, commit
eb7ca651fe72241a5784c289543f73b6c273faf4 changed this logic so it was checking
whether the perspective was missing from the list of perspectives _after_
switching. This commit restores the correct logic.
* CHANGELOG.develop: Update.
* layers/+completion/ivy/funcs.el (spacemacs/ivy-spacemacs-layouts): Add
"Copy Current Layout" and "Create Project Layout" actions to the docstring.
Use spacemacs//create-persp-with-home-buffer for the default action.
* layers/+completion/ivy/packages.el (ivy/post-init-persp-mode): Add
actions for spacemacs//create-persp-with-current-project-buffers and
persp-copy.
* layers/+spacemacs/spacemacs-layouts/funcs.el
(spacemacs||switch-layout): New macro. Switch to the named perspective,
and initialize it using the provided forms if the perspective is new.
(spacemacs//create-persp-with-current-project-buffers): New function.
Create a new perspective with the current project's buffers.
(spacemacs||switch-project-persp): New macro. Switch to the named
perspective, and evaluate the provided forms with
projectile-after-switch-project-hook bound with a hook that adds the
current project's buffers to the perspective. If the user quits during the
evaluation of the forms, kill the perspective.
(spacemacs//create-persp-with-home-buffer): New function. Switch to the
named perspective, and go to the Spacemacs home buffer if the perspective
is new.
(spacemacs/helm-perspectives): Use spacemacs//create-persp-with-home-buffer
for the default action. Add actions for
spacemacs//create-persp-with-current-project-buffers and persp-copy.
(spacemacs//helm-persp-switch-project-action): New function. Switch to the
named perspective and call projectile-switch-project-by-name, using the new
spacemacs||switch-project-persp macro. Bind helm-quit-hook with a hook
that kills the new perspective if the user quits
projectile-switch-project-by-name.
(spacemacs/helm-persp-switch-project): Use
spacemacs//helm-persp-switch-project-action.
(spacemacs//ivy-persp-switch-project-action): New function. Switch to the
named perspective and call counsel-projectile-switch-project-action with a
hook to add the project's buffers to the new perspective.
(spacemacs/ivy-persp-switch-project): Delete advice for
counsel-projectile-switch-project-action. Use
spacemacs//ivy-persp-switch-project-action instead.
(spacemacs//add-project-buffers-to-persp): Deleted.
* layers/+spacemacs/spacemacs-layouts/packages.el
(spacemacs-layouts/init-persp-mode): Don't add a hook to
persp-created-functions.
2019-05-10 23:48:18 +00:00
|
|
|
|
:action 'spacemacs//create-persp-with-home-buffer))
|
2016-05-16 02:00:44 +00:00
|
|
|
|
|
2016-06-01 03:39:47 +00:00
|
|
|
|
(defun spacemacs/ivy-spacemacs-layout-buffer ()
|
|
|
|
|
"Switch to layout buffer using ivy."
|
2016-05-16 02:00:44 +00:00
|
|
|
|
(interactive)
|
|
|
|
|
(let (ivy-use-virtual-buffers)
|
|
|
|
|
(with-persp-buffer-list ()
|
|
|
|
|
(call-interactively 'ivy-switch-buffer))))
|
|
|
|
|
|
2016-06-01 03:39:47 +00:00
|
|
|
|
(defun spacemacs/ivy-spacemacs-layout-close-other ()
|
|
|
|
|
"Kills layouts without killing the buffers"
|
2016-05-16 02:00:44 +00:00
|
|
|
|
(interactive)
|
2016-06-01 03:39:47 +00:00
|
|
|
|
(ivy-read (format "Close layout [current %s]: "
|
2016-05-16 02:00:44 +00:00
|
|
|
|
(spacemacs//current-layout-name))
|
|
|
|
|
(persp-names)
|
|
|
|
|
:action 'persp-kill-without-buffers))
|
|
|
|
|
|
2016-06-01 03:39:47 +00:00
|
|
|
|
(defun spacemacs/ivy-spacemacs-layout-kill-other ()
|
|
|
|
|
"Kills layouts with all their buffers"
|
2016-05-16 02:00:44 +00:00
|
|
|
|
(interactive)
|
2016-06-01 03:39:47 +00:00
|
|
|
|
(ivy-read (format "Kill layout [current %s]: "
|
2016-05-16 02:00:44 +00:00
|
|
|
|
(spacemacs//current-layout-name))
|
|
|
|
|
(persp-names)
|
|
|
|
|
:action 'persp-kill))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Swiper
|
|
|
|
|
|
2019-12-14 22:27:43 +00:00
|
|
|
|
(defun spacemacs//counsel-current-region-or-symbol ()
|
|
|
|
|
"Return contents of the region or symbol at point.
|
|
|
|
|
|
|
|
|
|
If region is active, mark will be deactivated in order to prevent region
|
|
|
|
|
expansion when jumping around the buffer with counsel. See `deactivate-mark'."
|
|
|
|
|
(if (region-active-p)
|
|
|
|
|
(prog1
|
|
|
|
|
(buffer-substring-no-properties (region-beginning) (region-end))
|
|
|
|
|
(deactivate-mark))
|
|
|
|
|
(thing-at-point 'symbol t)))
|
|
|
|
|
|
2016-05-16 02:00:44 +00:00
|
|
|
|
(defun spacemacs/swiper-region-or-symbol ()
|
|
|
|
|
"Run `swiper' with the selected region or the symbol
|
|
|
|
|
around point as the initial input."
|
|
|
|
|
(interactive)
|
2019-12-14 22:27:43 +00:00
|
|
|
|
(let ((input (spacemacs//counsel-current-region-or-symbol)))
|
2016-05-16 02:00:44 +00:00
|
|
|
|
(swiper input)))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/swiper-all-region-or-symbol ()
|
|
|
|
|
"Run `swiper-all' with the selected region or the symbol
|
|
|
|
|
around point as the initial input."
|
|
|
|
|
(interactive)
|
2019-12-14 22:27:43 +00:00
|
|
|
|
(let ((input (spacemacs//counsel-current-region-or-symbol)))
|
2018-02-27 10:01:40 +00:00
|
|
|
|
(swiper-all input)))
|