322528ca8d
Delete the hook that commit9fcf8c898d
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 in9fcf8c898d (commitcomment-33343455)
. This commit also fixes a problem with the default action for spacemacs/ivy-spacemacs-layouts and spacemacs/helm-perspectives. According to commit7b931a9f5b
, 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, commiteb7ca651fe
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.
473 lines
18 KiB
EmacsLisp
473 lines
18 KiB
EmacsLisp
;;; funcs.el --- Ivy Layer functions File for Spacemacs
|
||
;;
|
||
;; Copyright (c) 2012-2018 Sylvain Benner & Contributors
|
||
;;
|
||
;; 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)
|
||
(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
|
||
(ivy-add-prompt-count
|
||
(format (ivy-state-prompt ivy-last)
|
||
size))))
|
||
(if display-now
|
||
(ivy--insert-minibuffer
|
||
(ivy--format ivy--all-candidates))
|
||
(ivy--insert-prompt))))
|
||
(setq counsel--async-time (current-time))))
|
||
|
||
;; search
|
||
|
||
(defvar spacemacs--counsel-search-cmd)
|
||
|
||
;; see `counsel-ag-function'
|
||
(defun spacemacs//make-counsel-search-function (tool)
|
||
(lexical-let ((base-cmd
|
||
(cdr (assoc-string tool spacemacs--counsel-commands))))
|
||
(lambda (string &optional _pred &rest _unused)
|
||
"Grep in the current directory for STRING."
|
||
;; `counsel-more-chars' returns non-nil when more chars are needed,
|
||
;; minimal chars count is configurable via `counsel-more-chars-alist'
|
||
(or (counsel-more-chars)
|
||
(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)))))
|
||
|
||
(defun spacemacs//counsel-save-in-buffer ()
|
||
(interactive)
|
||
(ivy-quit-and-run
|
||
(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))))
|
||
|
||
(defun spacemacs//counsel-edit ()
|
||
"Edit the current search results in a buffer using wgrep."
|
||
(interactive)
|
||
(run-with-idle-timer 0 nil 'spacemacs/ivy-wgrep-change-to-wgrep-mode)
|
||
(ivy-occur))
|
||
|
||
(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)
|
||
(counsel-git-grep-action c))
|
||
next-error-function 'spacemacs/gne-next)))
|
||
|
||
(defvar spacemacs--counsel-map
|
||
(let ((map (make-sparse-keymap)))
|
||
(define-key map (kbd "<f3>") 'spacemacs//counsel-save-in-buffer)
|
||
(define-key map (kbd "C-c C-e") 'spacemacs//counsel-edit)
|
||
map))
|
||
|
||
(defun spacemacs/ivy--regex-plus (str)
|
||
"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))
|
||
"
|
||
(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)))
|
||
|
||
;; see `counsel-ag'
|
||
(defun spacemacs/counsel-search
|
||
(&optional tools use-initial-input initial-directory)
|
||
"Search using the first available tool in TOOLS. Default tool
|
||
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."
|
||
(interactive)
|
||
(require 'counsel)
|
||
(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: "))))
|
||
(ivy-read
|
||
(format "%s from [%s]: "
|
||
tool
|
||
(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)))))
|
||
(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)))))
|
||
|
||
;; Define search functions for each tool
|
||
(cl-loop
|
||
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
|
||
directory with %s." (if (string= tool-name "auto")
|
||
"a tool selected from `dotspacemacs-search-tools'."
|
||
tool-name))
|
||
(interactive)
|
||
(spacemacs/counsel-search ,tools))
|
||
(defun ,(intern (format "spacemacs/search-%s-region-or-symbol"
|
||
tool-name)) ()
|
||
,(format
|
||
"Use `spacemacs/counsel-search' to search for
|
||
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))
|
||
(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
|
||
project with %s." (if (string= tool-name "auto")
|
||
"a tool selected from `dotspacemacs-search-tools'."
|
||
tool-name))
|
||
(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
|
||
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))
|
||
(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
|
||
directory with %s." (if (string= tool-name "auto")
|
||
"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
|
||
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))
|
||
(interactive)
|
||
(spacemacs/counsel-search ,tools t default-directory)))))
|
||
|
||
(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))))
|
||
(counsel-git-grep nil input)))
|
||
|
||
(defun spacemacs/counsel-search-docs ()
|
||
"Search spacemacs docs using `spacemacs/counsel-search'"
|
||
(interactive)
|
||
(spacemacs/counsel-search dotspacemacs-search-tools
|
||
nil spacemacs-docs-directory))
|
||
|
||
(defun spacemacs//counsel-occur ()
|
||
"Generate a custom occur buffer for `counsel-git-grep'."
|
||
(ivy-occur-grep-mode)
|
||
(setq default-directory (ivy-state-directory ivy-last))
|
||
(let ((cands ivy--old-cands))
|
||
;; 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))
|
||
ivy--old-cands))))
|
||
|
||
(defun spacemacs/counsel-up-directory-no-error ()
|
||
"`counsel-up-directory' ignoring errors."
|
||
(interactive)
|
||
(ignore-errors
|
||
(call-interactively 'counsel-up-directory)))
|
||
|
||
(when (configuration-layer/package-used-p 'counsel)
|
||
(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))))
|
||
|
||
(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
|
||
(expand-file-name file-name (ivy-state-directory ivy-last)))
|
||
(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)))))))
|
||
|
||
;; 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)))
|
||
(case type
|
||
;; 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.
|
||
(case type
|
||
((headline inlinetask)
|
||
(save-excursion (goto-char (org-element-property :begin context))
|
||
(call-interactively 'counsel-org-tag)) t)))))
|
||
|
||
(defun spacemacs/counsel-jump-in-buffer ()
|
||
"Jump in buffer with `counsel-imenu' or `counsel-org-goto' if in org-mode"
|
||
(interactive)
|
||
(call-interactively
|
||
(cond
|
||
((eq major-mode 'org-mode) 'counsel-org-goto)
|
||
(t 'counsel-imenu))))
|
||
|
||
|
||
;; Ivy
|
||
|
||
(defun spacemacs//ivy-command-not-implemented-yet (key)
|
||
(lexical-let ((-key key))
|
||
(spacemacs/set-leader-keys
|
||
-key (lambda ()
|
||
(interactive)
|
||
(message (concat "The command usually bound to %s %s has "
|
||
"not been implemented for the `ivy' layer yet.")
|
||
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))))))
|
||
|
||
(defun spacemacs/ivy-wgrep-change-to-wgrep-mode ()
|
||
(interactive)
|
||
(ivy-wgrep-change-to-wgrep-mode)
|
||
(evil-normal-state))
|
||
|
||
;; Evil
|
||
|
||
(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))))
|
||
|
||
;; Layouts
|
||
|
||
(defun spacemacs/ivy-spacemacs-layouts ()
|
||
"Control Panel for Spacemacs layouts. Has many actions.
|
||
If match is found
|
||
\(default) Select layout
|
||
c: Close Layout(s) <- mark with C-SPC to close more than one-window
|
||
k: Kill Layout(s)
|
||
n: Copy current layout
|
||
p: Create project layout
|
||
|
||
If match is not found
|
||
<enter> Creates layout
|
||
|
||
Closing doesn't kill buffers inside the layout while killing layouts does."
|
||
(interactive)
|
||
(ivy-read "Layouts: "
|
||
(persp-names)
|
||
:caller 'spacemacs/ivy-spacemacs-layouts
|
||
:action 'spacemacs//create-persp-with-home-buffer))
|
||
|
||
(defun spacemacs/ivy-spacemacs-layout-buffer ()
|
||
"Switch to layout buffer using ivy."
|
||
(interactive)
|
||
(let (ivy-use-virtual-buffers)
|
||
(with-persp-buffer-list ()
|
||
(call-interactively 'ivy-switch-buffer))))
|
||
|
||
(defun spacemacs/ivy-spacemacs-layout-close-other ()
|
||
"Kills layouts without killing the buffers"
|
||
(interactive)
|
||
(ivy-read (format "Close layout [current %s]: "
|
||
(spacemacs//current-layout-name))
|
||
(persp-names)
|
||
:action 'persp-kill-without-buffers))
|
||
|
||
(defun spacemacs/ivy-spacemacs-layout-kill-other ()
|
||
"Kills layouts with all their buffers"
|
||
(interactive)
|
||
(ivy-read (format "Kill layout [current %s]: "
|
||
(spacemacs//current-layout-name))
|
||
(persp-names)
|
||
:action 'persp-kill))
|
||
|
||
|
||
;; Swiper
|
||
|
||
(defun spacemacs/swiper-region-or-symbol ()
|
||
"Run `swiper' with the selected region or the symbol
|
||
around point as the initial input."
|
||
(interactive)
|
||
(let ((input (if (region-active-p)
|
||
(buffer-substring-no-properties
|
||
(region-beginning) (region-end))
|
||
(thing-at-point 'symbol t))))
|
||
(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)
|
||
(let ((input (if (region-active-p)
|
||
(buffer-substring-no-properties
|
||
(region-beginning) (region-end))
|
||
(thing-at-point 'symbol t))))
|
||
(swiper-all input)))
|