joefromct
joefromct

Reputation: 1556

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.

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? Are while loops the preferred option for this? 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.

the (working, albeit not best practice?) code is below.
(Additional info is cygwin emacs 24.4, with evil via 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];

Upvotes: 1

Views: 326

Answers (1)

abo-abo
abo-abo

Reputation: 20342

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

Upvotes: 1

Related Questions