简体   繁体   English

emacs lisp-文本搜索并将列表替换为模板

[英]emacs lisp - text search and replace list into template

I'm somewhat new to emacs lisp and trying to learn the best way to do things. 我是emacs lisp的新手,正在尝试学习做事的最佳方法。

My example task at hand is to generate a collection of "grant permissions on database" statements (this might be something i often do). 我手头的示例任务是生成“数据库授予权限”语句的集合(这可能是我经常做的事情)。

In order to do this most efficiently i thought i would need two lists, one of the databases and one of the permissions to apply. 为了最有效地执行此操作,我认为我需要两个列表,一个数据库和一个要应用的权限。

I wrote one generic function to search and replace, and another to call that function and insert the needed text into my buffer. 我编写了一个通用函数来搜索和替换,另一个编写了该函数以将该函数插入并将所需的文本插入到我的缓冲区中。

Is this the best way to do this? 这是最好的方法吗? Should i be looking at yasnippets, or maybe macros? 我应该看看yasnippets,还是宏? Are while loops the preferred option for this? 是while循环的首选选项吗? I'm just looking to be pointed in the right direction to be doing this type of work the emacs-way... in my vim days I'd probably do something like this in python or bash. 我只是想指出正确的方向,以便以emacs方式进行此类工作...在我的vim时代,我可能会在python或bash中做类似的事情。

the (working, albeit not best practice?) code is below. 下面的代码(虽然不是最佳实践,但是正在运行)。
(Additional info is cygwin emacs 24.4, with evil via spacemacs.) (其他信息是cygwin emacs 24.4,其中通过spacemacs产生了恶意。)

(setq database-list  
      (list 
       "[database_1]"
       "[database_2]"
       "[database_3]"))  

(setq perm-list 
      (list "EXECUTE"
            "SELECT"
            "SHOWPLAN"))  

(defun generic-string-replace-list (template search in-list )  
  "takes a string template in, a search token, and a list.  Iterates
through the search list generating an output string with the
searh/replace of each list item."
  (setq out "" )
  (while in-list
    (setq out (concat 
                    out 
                    (replace-regexp-in-string search (car in-list) template) 
                    "\n" ))  
    (setq in-list (cdr in-list)))
  out )


(defun generate-perm-list-for-db-list (perm-list database-list ) 
  (forward-line) 
  (while database-list
    (insert (concat "\nuse " (car database-list) ";\n" ))  
    (setq template (concat 
                       "grant \$perm to " 
                        (car database-list )  
                        " to [Public];" )) 
    (insert (generic-string-replace-list 
                            template 
                            "\$perm" 
                            perm-list))
    (setq database-list (cdr database-list))))   

;; Call things above with this: 
(generate-perm-list-for-db-list  perm-list database-list)

;; sample output from the functions:

use [database_1];
grant EXECUTE to [database_1] to [Public];
grant SELECT to [database_1] to [Public];
grant SHOWPLAN to [database_1] to [Public];

use [database_2];
grant EXECUTE to [database_2] to [Public];
grant SELECT to [database_2] to [Public];
grant SHOWPLAN to [database_2] to [Public];

use [database_3];
grant EXECUTE to [database_3] to [Public];
grant SELECT to [database_3] to [Public];
grant SHOWPLAN to [database_3] to [Public];

Here's your code, simplified: 这是简化的代码:

(setq database-list '("[database_1]" "[database_2]" "[database_3]"))

(setq perm-list '("EXECUTE" "SELECT" "SHOWPLAN"))

(defun generate-perm-list-for-db-list (perm-list database-list)
  (forward-line)
  (dolist (db database-list)
    (insert
     "\nuse " db ";\n"
     (mapconcat
      (lambda (x)
        (format "grant %s to %s to [Public];" x db))
      perm-list
      "\n")
     "\n")))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM