首页 > 解决方案 > 在 Emacs 中使用 swiper 模拟 helm-swoop

问题描述

长期 Emacshelm用户在这里。我最近一直ivy在尝试,我正在尝试在 Emacs 内部helm-swoop进行模拟。swiper具体来说,如果我在框架内打开了许多窗口,我想要以下行为:

我目前有这个工作,但是当当前缓冲区的窗口在左侧时,点击RET最终搜索结果不会导航到原始缓冲区中的那个位置。但是,如果当前缓冲区的窗口位于右侧,则一切正常。

这是我到目前为止的ELisp代码:

(defun ivy-display-function-other-window (text)
  "Show ivy results in other window."
  (let ((buffer (get-buffer-create "*ivy-candidate-window*"))
        (str (with-current-buffer (get-buffer-create " *Minibuf-1*")
               (let ((point (point))
                     (string (concat (buffer-string) "  " text)))
                 (add-face-text-property
                  (- point 1) point 'ivy-cursor t string)
                 string))))
    (with-current-buffer buffer
      (let ((inhibit-read-only t))
        (erase-buffer)
        (insert str)))
    (with-ivy-window
      (display-buffer
       buffer
       `((display-buffer-reuse-window
          (lambda (buffer alist)
            (other-window 1)
            (switch-to-buffer buffer))))))))

(defun my-swiper (&optional initial-input)
  "Opens swiper in a vertical buffer."
  (interactive)
  (let ((position (point)))
    (save-window-excursion
      (let* ((buffer (current-buffer))
             (window (get-buffer-window))
             (should-swap
              (and
               (not (window-at-side-p window 'left))
               (window-at-side-p window 'right)))
             (ivy-height (1- (window-height window))))
        (delete-other-windows)
        (split-window-horizontally)
        (when should-swap
          (other-window 1)
          (switch-to-buffer buffer))
        (setq position (swiper initial-input))))
    (goto-char position)))

...

(use-package counsel
  :ensure t
  :demand t
  :bind
  (("M-i" . my-swiper))

...

(setq ivy-display-functions-alist
    '((swiper . ivy-display-function-other-window)))

这里是GitHub 上来自wikiivy-display-function-other-window的稍微编辑的版本。谢谢!ivy-display-function-windowivy

标签: searchemacswindow

解决方案


推荐阅读