美文网首页
开发自己的emacs插件

开发自己的emacs插件

作者: 余很多之很多code | 来源:发表于2022-11-10 13:51 被阅读0次

    emacs个人需求

    本地记录的笔记文章,想在emacs快速通过文件名打开

    通过第三方插件

    通过插件counsel-projectile-find-file 快速搜索找到并打开文件,但是只能针对当前项目下。

    自己写一个emacs插件

    lisp插件代码

    (defun open-vim-notepad()
      (interactive)
      (find-file "/opt/dir/yulove/yudata/code/soft/vim/vim-notepad.md"));
    

    以上lisp代码:通过输入open-vim-notepad,找到文件vim-notepad.md,
    并且快速打开文件

    java代码生成

    通过java代码遍历笔记文件,生成大量的open-xxx函数,

    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.util.HashMap;
    import java.util.Map;
    public class EmacsOpenSettings {
        private static StringBuffer buff = new StringBuffer();
        private static Map<String,String> map = new HashMap<String,String>();
        
        public static void main(String[] args) {
            String path = "/opt/dir/yulove/yudata"; 
            File file = new File(path); 
            func(file);
            buff.append("(provide 'open-settings)");
            System.out.println(buff);
            
            try {
                BufferedWriter writer = new BufferedWriter(
                new FileWriter(
                "/opt/dir/yulove/yuconfig/etc/.emacs.d/config/open-settings.el"
                )
                );
                writer.write(buff.toString());
                writer.close();
            }catch(Exception ex) {
                ex.printStackTrace();
            }
        
        }
        
        private static void func(File file){
            File[] fs = file.listFiles();
            for(File f:fs){
                if(f.isDirectory()) //若是目录,则递归打印该目录下的文件
                    func(f);
                if(f.isFile()) {
                    String name = f.getName().split("\\.")[0];
                    if(map.containsKey(name)||name.length()<=1) {
                        continue;
                    }
                    buff.append(open_settings(name,f.getPath()));
                    map.put(name, name);
                }
                    
            }
        }
        
        private static String open_settings(String name ,String path) {
            
            String s="(defun open-"+name+"()\n"
                    + "  (interactive)\n"
                    + "  (find-file \""+path+"\"));\n";     
            return s;
        }
    

    ps:代码很早以前写的,所以用java写脚本代码了,最后打成了jar包,通过命令行调用,如果有新文件加入,重新生成。
    文件/opt/dir/yulove/yuconfig/etc/.emacs.d/config/open-settings.el , 我是ln -s 软链接到了 ~/.emacs.d/config/open-settings.el

    效果

    tty.gif

    相关文章

      网友评论

          本文标题:开发自己的emacs插件

          本文链接:https://www.haomeiwen.com/subject/fxwztdtx.html