do 1)
(defun add-py-debug () "add debug code and move line down" (interactive) (move-beginning-of-line 1) (insert "import pdb; pdb.set_trace();\n")) (local-set-key (kbd "<f9>") 'add-py-debug)
to do 2) you probably have to change the syntax highlighting in python mode or write your own small mode. You will need to study the font lock to get more. Unfortunately.
to do 3) although I set this as Cc F9 instead of Alt-F9
(defun remove-py-debug () "remove py debug code, if found" (interactive) (let ((x (line-number-at-pos)) (cur (point))) (search-forward-regexp "^[ ]*import pdb; pdb.set_trace();") (if (= x (line-number-at-pos)) (let () (move-beginning-of-line 1) (kill-line 1) (move-beginning-of-line 1)) (goto-char cur)))) (local-set-key (kbd "C c <f9>") 'remove-py-debug)
and do 4)
(local-set-key (kbd "<f3>") '(lambda () (interactive) (search-forward-regexp "^[ ]*import pdb; pdb.set_trace();") (move-beginning-of-line 1)))
Please note that this is not the best elisp code in the world, but I tried to make you understand what is happening and not make it completely idiomatic. The GNU Elsip book is a great place to start if you want to do more with elisp.
NTN
source share