我一直在网上四处寻找,但没有找到解决方案。有谁知道如何跳转到 html 文件中的 css 声明并打开一个新缓冲区,指向或接近该声明?
编辑
经过更多搜索后,我决定采用搜索字符串方法的打开缓冲区。
;; CSS search open buffers
(defun search-open-css-buffers-for-region-or-word ()
"Use the current region/point and search open css buffers"
(interactive)
(let (searchTerm)
(setq searchTerm
(if (region-active-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(thing-at-point 'symbol)))
(multi-occur (mapcar (lambda (buf)
(if (string-match "\w*.css" (buffer-name buf))
buf)) (buffer-list))
searchTerm 5)))
(global-set-key (kbd "M-s-.") 'search-open-css-buffers-for-region-or-word)
虽然感觉这是一个 hack。
最佳答案
有几件事使这个非通用:
- 根据您使用的 HTML 模式,您可能需要不同的方法来检测当前位置
- 你的 CSS 文件在哪里?人们有各种不同的元素结构
- 您希望输出结果如何?
话虽如此,这里有一个 nxml 模式的例子(尽管它很容易适应)。
(defun find-css-defun () (interactive) (when (memq 'nxml-attribute-value (get-text-property (point) 'face)) (let ((css-buffers (mapcar (lambda (file) (find-file-noselect file)) (directory-files default-directory t ".*\\.css$")))) (multi-occur css-buffers (thing-at-point 'symbol)))))
- 回答第一点,我们正在使用 nxml,因此这就是我们检测是否使用 CSS 属性的方式
- 回答第二点,我们只是扫描与 HTML 在同一目录中的所有 CSS 文件,css-buffers 是打开的 CSS 文件的列表;显然这是适应性强的
- 我们使用多次出现来呈现结果
它工作正常。
PS 这几天默认在 emacs 中需要 thing-at-point
关于html - Emacs 从 html 文件跳转到 css 声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14782952/