首页 > 代码库 > helm-mode打开文件支持中文搜索
helm-mode打开文件支持中文搜索
<style type="text/css">.title { text-align: center; margin-bottom: .2em }
.subtitle { text-align: center; font-size: medium; font-weight: bold; margin-top: 0 }
.todo { font-family: monospace; color: red }
.done { font-family: monospace; color: green }
.priority { font-family: monospace; color: orange }
.tag { background-color: #eee; font-family: monospace; padding: 2px; font-size: 80%; font-weight: normal }
.timestamp { color: #bebebe }
.timestamp-kwd { color: #5f9ea0 }
.org-right { margin-left: auto; margin-right: 0px; text-align: right }
.org-left { margin-left: 0px; margin-right: auto; text-align: left }
.org-center { margin-left: auto; margin-right: auto; text-align: center }
.underline { text-decoration: underline }
#postamble p,#preamble p { font-size: 90%; margin: .2em }
p.verse { margin-left: 3% }
pre { border: 1px solid #ccc; padding: 8pt; font-family: monospace; overflow: auto; margin: 1.2em }
pre.src { position: relative; overflow: visible; padding-top: 1.2em }
pre.src::before { display: none; position: absolute; background-color: white; top: -10px; right: 10px; padding: 3px; border: 1px solid black }
pre.src:hover::before { display: inline }
pre.src-asymptote::before { content: "Asymptote" }
pre.src-awk::before { content: "Awk" }
pre.src-C::before { content: "C" }
pre.src-clojure::before { content: "Clojure" }
pre.src-css::before { content: "CSS" }
pre.src-D::before { content: "D" }
pre.src-ditaa::before { content: "ditaa" }
pre.src-dot::before { content: "Graphviz" }
pre.src-calc::before { content: "Emacs Calc" }
pre.src-emacs-lisp::before { content: "Emacs Lisp" }
pre.src-fortran::before { content: "Fortran" }
pre.src-gnuplot::before { content: "gnuplot" }
pre.src-haskell::before { content: "Haskell" }
pre.src-java::before { content: "Java" }
pre.src-js::before { content: "Javascript" }
pre.src-latex::before { content: "LaTeX" }
pre.src-ledger::before { content: "Ledger" }
pre.src-lisp::before { content: "Lisp" }
pre.src-lilypond::before { content: "Lilypond" }
pre.src-lua::before { content: "Lua" }
pre.src-matlab::before { content: "MATLAB" }
pre.src-mscgen::before { content: "Mscgen" }
pre.src-ocaml::before { content: "Objective Caml" }
pre.src-octave::before { content: "Octave" }
pre.src-org::before { content: "Org mode" }
pre.src-oz::before { content: "OZ" }
pre.src-plantuml::before { content: "Plantuml" }
pre.src-processing::before { content: "Processing.js" }
pre.src-python::before { content: "Python" }
pre.src-R::before { content: "R" }
pre.src-ruby::before { content: "Ruby" }
pre.src-sass::before { content: "Sass" }
pre.src-scheme::before { content: "Scheme" }
pre.src-screen::before { content: "Gnu Screen" }
pre.src-sed::before { content: "Sed" }
pre.src-sh::before { content: "shell" }
pre.src-sql::before { content: "SQL" }
pre.src-sqlite::before { content: "SQLite" }
pre.src-forth::before { content: "Forth" }
pre.src-io::before { content: "IO" }
pre.src-J::before { content: "J" }
pre.src-makefile::before { content: "Makefile" }
pre.src-maxima::before { content: "Maxima" }
pre.src-perl::before { content: "Perl" }
pre.src-picolisp::before { content: "Pico Lisp" }
pre.src-scala::before { content: "Scala" }
pre.src-shell::before { content: "Shell Script" }
pre.src-ebnf2ps::before { content: "ebfn2ps" }
pre.src-cpp::before { content: "C++" }
pre.src-abc::before { content: "ABC" }
pre.src-coq::before { content: "Coq" }
pre.src-groovy::before { content: "Groovy" }
pre.src-bash::before { content: "bash" }
pre.src-csh::before { content: "csh" }
pre.src-ash::before { content: "ash" }
pre.src-dash::before { content: "dash" }
pre.src-ksh::before { content: "ksh" }
pre.src-mksh::before { content: "mksh" }
pre.src-posh::before { content: "posh" }
pre.src-ada::before { content: "Ada" }
pre.src-asm::before { content: "Assembler" }
pre.src-caml::before { content: "Caml" }
pre.src-delphi::before { content: "Delphi" }
pre.src-html::before { content: "HTML" }
pre.src-idl::before { content: "IDL" }
pre.src-mercury::before { content: "Mercury" }
pre.src-metapost::before { content: "MetaPost" }
pre.src-modula-2::before { content: "Modula-2" }
pre.src-pascal::before { content: "Pascal" }
pre.src-ps::before { content: "PostScript" }
pre.src-prolog::before { content: "Prolog" }
pre.src-simula::before { content: "Simula" }
pre.src-tcl::before { content: "tcl" }
pre.src-tex::before { content: "TeX" }
pre.src-plain-tex::before { content: "Plain TeX" }
pre.src-verilog::before { content: "Verilog" }
pre.src-vhdl::before { content: "VHDL" }
pre.src-xml::before { content: "XML" }
pre.src-nxml::before { content: "XML" }
pre.src-conf::before { content: "Configuration File" }
table { border-collapse: collapse }
caption.t-above { caption-side: top }
caption.t-bottom { caption-side: bottom }
td,th { vertical-align: top }
th.org-right { text-align: center }
th.org-left { text-align: center }
th.org-center { text-align: center }
td.org-right { text-align: right }
td.org-left { text-align: left }
td.org-center { text-align: center }
dt { font-weight: bold }
.footpara { display: inline }
.footdef { margin-bottom: 1em }
.figure { padding: 1em }
.figure p { text-align: center }
.inlinetask { padding: 10px; border: 2px solid gray; margin: 10px; background: #ffffcc }
#org-div-home-and-up { text-align: right; font-size: 70%; white-space: nowrap }
textarea { }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00 }
.org-info-js_info-navigation { border-style: none }
#org-info-js_console-label { font-size: 10px; font-weight: bold; white-space: nowrap }
.org-info-js_search-highlight { background-color: #ffff00; color: #000000; font-weight: bold }
.org-svg { width: 90% }</style>
<style type="text/css">code { color: #FF0000 }
pre.src { background-color: #002b36; color: #839496 }</style>
<style type="text/css">code { color: #FF0000 }
pre.src { background-color: #002b36; color: #839496 }</style>
<style type="text/css">code { color: #FF0000 }
pre.src { background-color: #002b36; color: #839496 }</style>
<style type="text/css">code { color: #FF0000 }
pre.src { background-color: #002b36; color: #839496 }</style>
helm-mode打开文件支持中文搜索
Table of Contents
- 1. pinyin-search
- 2. helm-find-files中的拼音搜索
- 3. helm-multi-files和helm-projectile中的拼音搜索
- 3.1. match
- 3.2. search是用于真正的搜索过滤的函数
由于helm的很多功能比较好用,于是把所有的ido全部用helm替代了。但是,其中少了使用拼音首字母进行搜索的功能,于是自己捣鼓着弄出来。
1 pinyin-search
首先,这个功能是基于pinyin-search里面的函数来创建pinyin字母到汉字的正则表达式创建。
(setq helm-pinyin-search-p t) (when helm-pinyin-search-p (require ‘pinyin-search))
2 helm-find-files中的拼音搜索
将原来的创建搜索的正则表达式由纯英文的改为中英文混合: helm–mapconcat-pattern:
he => [h]*h[e]*e
helm–mapconcat-pinyin-pattern:
he => [h哈]*[h哈][e额]*[e额]
(defsubst helm--mapconcat-pinyin-pattern (pattern) "Transform string PATTERN in regexp for further fuzzy matching. e.g helm.el$ => \"[^h哈]*[h哈][^e额]*[e额][^l]*l[^m]*m[^.]*[.][^e]*e[^l]*l$\" ^helm.el$ => \"helm[.]el$\"." (let ((ls (split-string-and-unquote pattern ""))) (if (string= "^" (car ls)) ;; Exact match. (mapconcat (lambda (c) (if (and (string= c "$") (string-match "$\\‘" pattern)) c (regexp-quote c))) (cdr ls) "") ;; Fuzzy match. (mapconcat (lambda (c) (if (and (string= c "$") (string-match "$\\‘" pattern)) c (let ((pinyin-pattern (pinyinlib-build-regexp-string c))) (if (< (length pinyin-pattern) 3) c (format "[^%s]*%s" (substring pinyin-pattern 1 -1) pinyin-pattern))))) ls ""))))
再把查找文件函数里面的helm–mapconcat-pattern替换为helm–mapconcat-pinyin-pattern:
(defun helm-ff--transform-pattern-for-completion (pattern) "Maybe return PATTERN with it‘s basename modified as a regexp. This happen only when `helm-ff-fuzzy-matching‘ is enabled. This provide a similar behavior as `ido-enable-flex-matching‘. See also `helm--mapconcat-pinyin-pattern‘ If PATTERN is an url returns it unmodified. When PATTERN contain a space fallback to multi-match. If basename contain one or more space fallback to multi-match. If PATTERN is a valid directory name,return PATTERN unchanged." ;; handle bad filenames containing a backslash. (setq pattern (helm-ff-handle-backslash pattern)) (let ((bn (helm-basename pattern)) (bd (or (helm-basedir pattern) "")) ;; Trigger tramp connection with file-directory-p. (dir-p (file-directory-p pattern)) (tramp-p (cl-loop for (m . f) in tramp-methods thereis (string-match m pattern)))) ;; Always regexp-quote base directory name to handle ;; crap dirnames such e.g bookmark+ (cond ((or (and dir-p tramp-p (string-match ":\\‘" pattern)) (string= pattern "") (and dir-p (<= (length bn) 2)) ;; Fix Issue #541 when BD have a subdir similar ;; to BN, don‘t switch to match plugin ;; which will match both. (and dir-p (string-match (regexp-quote bn) bd))) ;; Use full PATTERN on e.g "/ssh:host:". (regexp-quote pattern)) ;; Prefixing BN with a space call multi-match completion. ;; This allow showing all files/dirs matching BN (Issue #518). ;; FIXME: some multi-match methods may not work here. (dir-p (concat (regexp-quote bd) " " (regexp-quote bn))) ((or (not (helm-ff-fuzzy-matching-p)) (string-match "\\s-" bn)) ; Fall back to multi-match. (concat (regexp-quote bd) bn)) ((or (string-match "[*][.]?.*" bn) ; Allow entering wilcard. (string-match "/$" pattern) ; Allow mkdir. (string-match helm-ff-url-regexp pattern) (and (string= helm-ff-default-directory "/") tramp-p)) ;; Don‘t treat wildcards ("*") as regexp char. ;; (e.g ./foo/*.el => ./foo/[*].el) (concat (regexp-quote bd) (replace-regexp-in-string "[*]" "[*]" bn))) (t (concat (regexp-quote bd) (if (>= (length bn) 2) ; wait 2nd char before concating. (progn ;; (print (helm--mapconcat-pinyin-pattern bn)) (helm--mapconcat-pinyin-pattern bn)) (concat ".*" (regexp-quote bn))))))))
将下面文件名的正则表达式修改成中英文混合就可以实现了。
3 helm-multi-files和helm-projectile中的拼音搜索
这两个模式里面的搜索不一样,因为包含全路径。
3.1 match
用来判断模式pattern和string是否匹配。
3.2 search是用于真正的搜索过滤的函数
用来搜索和过滤candidates。
(cl-defun helm-mm-3-match (str &optional (pattern helm-pattern)) "Check if PATTERN match STR. When PATTERN contain a space, it is splitted and matching is done with the several resulting regexps against STR. e.g \"bar foo\" will match \"foobar\" and \"barfoo\". Argument PATTERN, a string, is transformed in a list of cons cell with `helm-mm-3-get-patterns‘ if it contain a space. e.g \"foo bar\"=>((identity . \"foo\") (identity . \"bar\")). Then each predicate of cons cell(s) is called with regexp of same cons cell against STR (a candidate). i.e (identity (string-match \"foo\" \"foo bar\")) => t." (let ((pat (helm-mm-3-get-patterns pattern))) (let ((source-name (assoc-default ‘name (helm-get-current-source)))) ;; (print (concat "8 " source-name)) (if (string= source-name "Recentf") (cl-loop for (predicate . regexp) in pat always (funcall predicate (condition-case _err ;; FIXME: Probably do nothing when ;; using fuzzy leaving the job ;; to the fuzzy fn. (string-match (concat "\\(" regexp "\\)\\|\\(" (pinyin-search--pinyin-to-regexp regexp) "\\)") str) (invalid-regexp nil)))) (cl-loop for (predicate . regexp) in pat always (funcall predicate (condition-case _err ;; FIXME: Probably do nothing when ;; using fuzzy leaving the job ;; to the fuzzy fn. (string-match regexp str) (invalid-regexp nil))))))))
Date: 2017-01-26 10:26
Created: 2017-01-27 周五 10:22
Emacs 26.0.50.4 (Org mode 9.0.4)
Validate
helm-mode打开文件支持中文搜索
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。