0

I wrote this function which takes a string and writes out self.var for each entry in the string. This helps me create python classes faster.

(defun elf-python-self (str)
  "Parse the arguemnts in STR and write out self.var = var."
  (interactive (list (read-string "Argument List: ")))
  (message str)
  (let ((var-list (split-string str ","))
        (var-name))
    (forward-line)
    (move-beginning-of-line nil)
    (while (> (length var-list) 0)
      (setq var-name (strip (pop var-list)))
      (message var-name)
      (insert (format "        self.%s = %s\n" var-name var-name)))))

If I type a, b, c then I get the following

    self.a = a
    self.b = b
    self.c = c

How do I modify this function to apply the code to the selected region?

0

I found this which describes the interactive region https://stackoverflow.com/questions/605846/how-do-i-access-the-contents-of-the-current-region-in-emacs-lisp and applied it to my function below using buffer-substring to extract the string from the region.

(defun elf-python-self-region (start end)
  "Split the region START to END based on comma and strings write out self.var = var."
  (interactive "r")
  (elf-python-self (buffer-substring start end)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.