以下是我的完整最小配置,改一下包的位置应该在任何电脑上都能表现一致:
;;; test-ivy-swiper-visible.el --- Test ivy swiper search visible content -*- lexical-binding: t; -*-
;;; Usage: /path/to/emacs -nw -Q -l /path/to/test-ivy-swiper-visible.el
;;; Date: 2023-10-23_15.04.21
(toggle-debug-on-error)
;; ---------------------------------------------------------------------------
;; elpa
(require 'seq)
(setq user-emacs-directory "~/.emacs.d/")
(setq package-user-dir
(car (seq-filter
#'file-exists-p
(list (format "~/.emacs.d/%s/elpa" emacs-version)
(format "~/.emacs.d/%s.%s/elpa" emacs-major-version emacs-minor-version)
"~/.emacs.d/elpa"))))
(package-initialize)
;; Add addtional local package here:
(add-to-list 'load-path "~/repos/emacs-ivy")
;; ---------------------------------------------------------------------------
(require 'ivy)
(require 'swiper)
(require 'org)
(defun swiper--visible-p (pos)
(let ((invisible (get-char-property pos 'invisible)))
(or (null invisible)
(eq t buffer-invisibility-spec)
(null (assoc invisible buffer-invisibility-spec)))))
(defun swiper-visible (&optional initial-input)
"Like `swiper' but only search for visible content."
(interactive)
(let ((ad-swiper--line
(lambda (orig-fn)
(cl-letf (((symbol-function 'buffer-substring)
(lambda (start end)
(let (visible-chars)
(save-excursion
(goto-char start)
(while (< (point) end)
(when (swiper--visible-p (point))
(push (char-after (point)) visible-chars))
(forward-char 1))
(mapconcat #'char-to-string (reverse visible-chars) ""))))))
(funcall orig-fn)))))
(advice-add 'swiper--line :around ad-swiper--line)
(unwind-protect
(swiper initial-input)
(advice-remove 'swiper--line ad-swiper--line))))
(global-set-key (kbd "C-s") #'swiper-visible)
(add-hook 'emacs-startup-hook
(lambda ()
(view-echo-area-messages)
(switch-to-buffer "*test*")
(insert "[[foo][bar]]\n"
"[[https://example.com][Example]]\n"
"[[https://example.com][Example]]\n"
"[[https://example.com][Example]]\n"
"[[https://example.com][Example]]\n")
(insert "[[quz][baz]]\n")
(setq-local org-hide-emphasis-markers t) ;; hide markers
(org-mode)
(font-lock-ensure)
))
;;; test-ivy-swiper-visible.el ends here