helm-mode打开文件支持中文搜索

*/-->

code {color: #FF0000}
pre.src {background-color: #002b36; color: #839496;}

helm-mode打开文件支持中文搜索

由于helm的很多功能比较好用,于是把所有的ido全部用helm替代了。但是,其中少了使用拼音首字母进行搜索的功能,于是自己捣鼓着弄出来。

1 效果

2 pinyin-search

首先,这个功能是基于pinyin-search里面的函数来创建pinyin字母到汉字的正则表达式创建。

(setq helm-pinyin-search-p t)
(when helm-pinyin-search-p
(require 'pinyin-search))

3 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))))))))

将下面文件名的正则表达式修改成中英文混合就可以实现了。

4 helm-multi-files和helm-projectile中的拼音搜索

这两个模式里面的搜索不一样,因为包含全路径。

4.1 match

用来判断模式pattern和string是否匹配。

4.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-03-18 周六 14:51

Emacs 26.0.50 (Org mode 8.2.10)

Validate

helm-mode打开文件支持中文搜索的更多相关文章

  1. Raneto部署知识库平台&支持中文搜索

    目录 环境 更新软件包 部署 Raneto 知识库平台 安装 Node 环境 安装 node 管理工具 查看 node 列表 安装需要的Node版本 使用 淘宝NPM源 git 使用代理设置,大陆地区 ...

  2. 解决 Ubuntu 下 gedit编辑器打开文件出现中文乱码问题

    解决 Ubuntu 中 gedit编辑器打开文件出现中文乱码问题 1. 问题分析 在 windows 系统下,.txt 文件默认编码方式为 gb18030 格式的中文编码,而 gedit 默认的编码方 ...

  3. Android 解压zip文件(支持中文)

    过了n多天后,当再次使用原先博客上写的那篇: Android 压缩解压zip文件 去做zip包的解压的时候,出现了原来没有发现的很多问题.首先是中文汉字问题,使用java的zip包不能很好的解决解压问 ...

  4. Dicom文件支持中文字符

    Dicom文件的默认字符集编码为ISO-IR6,这种字符集是不支持中文的,当使用Dicom工具修改病人姓名后,名字会成乱码而无法正常显示,如下图: 知道了原因就知道解决办法了,修改Dicom的字符集( ...

  5. Ubuntu 14.04中gedit打开文件出现中文乱码问题

    http://blog.csdn.net/cywosp/article/details/32325449/ 在中文支持配置还不完整的Ubuntu 14.04中,使用gedit打开带有中文字符的文件有时 ...

  6. zhuan:点滴记录——Ubuntu 14.04中gedit打开文件出现中文乱码问题

    在中文支持配置还不完整的Ubuntu 14.04中,使用gedit打开带有中文字符的文件有时会出现乱码的情况,这是由于gedit对字符编码匹配不正确导致的,解决方法如下: 在终端中输入如下命令,然后重 ...

  7. vscode打开文件,中文显示乱码(已解决)

    之前使用vscode打开keil的文件后,发现显示乱码,网上查找资料发现大多是这种方法:将files.autoGuessEncoding改为true,但是并没有用. 发现第二种方法为:在vscode中 ...

  8. C#导出csv文件 支持中文的解决方案

    #region 导出CSV下载 string exportFileName = "Export" + DateTime.Now.ToString("yyyyMMddHHm ...

  9. Raneto中文搜索支持

    背景 因业务部门需要在线软件使用说明文档,但我们资源不足,故我想找一个开源的知识库,发现 Raneto不错,决定使用. 官方文档相当清晰,部署完成,发布一些文章,启动项目,交由业务同事测试使用,于是我 ...

随机推荐

  1. CSS深入理解float

    初衷:图片环绕效果 1.会使父元素高度塌陷 2.包裹性 3.让元素block化 4.去空格化   5.清除浮动 .clearfix::after{ content:""; disp ...

  2. redis的set()方法参数

    redis 127.0.0.1:6379> SET KEY VALUE [EX seconds] [PX milliseconds] [NX|XX]   EX seconds − 设置指定的到期 ...

  3. 北京邀请赛 B. Beautiful Garden

    题意:给你坐标和n个点,求最少移动的点使得n个点成等差数列 思路:既然要成等差数列,那么最起码有两个点是不动的,然后枚举这两个点中间的点的个数,近期水的要死,看了队友的代码做的 #include &l ...

  4. JavaScript学习笔记(基础部分)

    一.JavaScript简介: 概念:JavaScript是一种解释性的.跨平台的.基于对象的脚本语言,一般用于客户端来给HTML页面增加动态的功能. 组成: 1.ECMAScript,描述了该语言的 ...

  5. JQuery通过URL获取图片宽高

    var img_url ='https://www.baidu.com/img/bd_logo1.png'; // 创建对象 var img = new Image(); // 改变图片的src im ...

  6. 深入学习Redis主从复制

    一.主从复制概述 主从复制,是指将一台Redis服务器的数据,复制到其他的Redis服务器.前者称为主节点(master),后者称为从节点(slave):数据的复制是单向的,只能由主节点到从节点. 默 ...

  7. 在linux中出现there are stopped jobs 的解决方法【转自:http://www.linuxdiyf.com/viewarticle.php?id=104604】

    在用管理员执行一个命令后,我用Ctrl+Z把命令转移到了后台天.导致我无法退出root的. 输入命令:logout终端显示:There are stopped jobs. 解决方法:输入命令:jobs ...

  8. 关于云计算三大服务模式LAAS,PAAS,SAAS的含义及区别

    根据NIST的权威定义,云计算有SPI,即SAAS,PAAS和LAAS三大服务模式,上层是SAAS,中间层是PAAS,底层是LAAS,一层支撑一层. LAAS(Infrastucture-as-a-S ...

  9. 【Flutter学习】可滚动组件之ScrollView

    一,概述 ScrollView 是一个带有滚动的视图组件. 二,组成部分 ScrollView 由三部分组成: Scrollable - 它监听各种用户手势并实现滚动的交互设计.可滚动Widget都直 ...

  10. 系统的重要文件/etc/inittab被删除了--急救办法!

    如果在生产环境中,系统的重要文件/etc/inittab被删除了(系统还没重启,崩溃前),不要急,下面告诉你该如何处理.1.模拟误删除文件[root@localhost ~]# rm -rf /etc ...