Emacs: Set / Reset python debug breakpoint

I am using python debugger pdb. I am using emacs for programming in python. I am using python-mode.el. My idea is to make emacs intuitive. So I need the following help for python programs (.py)

  • When I press the F9 key, emacs should put "import pdb; pdb.set_trace ();" statements in the current line and move the current line one line below. The sentence must be on one line. smart indentation can help a lot.

  • Anywhere: "import pdb; pdb.set_trace ();" the operator is presented in python code, emacs should display the left indicator and highlight this line.

  • When I press Alt-F9 on the current line and emacs detects "import pdb; pdb.set_trace ();" then emacs should remove "import pdb; pdb.set_trace ();" and move the current line one by one.

  • When I press the F8 key, emacs goes to "import pdb; pdb.set_trace ();" in the same buffer.

I am trying to learn elisp and quickly catch up with lisp in order to configure emacs myself. I will be grateful for your answers.

The answer will be big enough for me, and others who find this solution are very helpful.

+4
source share
2 answers

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

+7
source

I found that the Xah Elisp Tutorial is a great starting point in defining the basics of Emacs Lisp programming. There are also some SteveY articles from the past that go through methods that may be useful for learning the basics.

If you are serious about changing the Python mode, you might want to take a look at the GNU Emacs Extensions Writing , which is available as a PDF.

Finally, the most useful resource for me is Emacs itself. I often use Mx apropos and Mx describe-key to figure out how the built-in functions work, and is there something already in place to do what I want.

The specific things you want to see can be accomplished with a simple use of insert and a few search / replace functions, so this will be a good starting point.

0
source

Source: https://habr.com/ru/post/1302336/


All Articles