views:

262

answers:

2

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

  1. Whenever I press 'F9' key, the emacs should put "import pdb; pdb.set_trace();" statements in the current line and move the current line to one line below. Sentence to be in same line. smart indentation may help very much.

  2. Wherever "import pdb; pdb.set_trace();" statement presents in the python code, emacs should display left indicator and highlight that line.

  3. When I press 'Alt-F9' keys at the current line and emacs found the "import pdb; pdb.set_trace();" statement then, emacs should remove the "import pdb; pdb.set_trace();" line and move the current line to one up.

  4. Whenever I press "F8" key, emacs to jump to "import pdb; pdb.set_trace();" in the same buffer.

I am trying to learn elisp and catch up lisp soon to customize emacs myself. I will appreciate your answers.

The answer shall be great enough for me and others who find this solution is very useful.

+3  A: 

to 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 of the python mode, or write you own minor mode. You'd have to look into font-lock to get more. Sorry.

to do 3) though I've set this to be C-c 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 to do 4)

(local-set-key (kbd "<f3>") '(lambda ()  
                                 (interactive)   
                                 (search-forward-regexp "^[ ]*import pdb; pdb.set_trace();")   
                                 (move-beginning-of-line 1)))

Note, this is not the best elisp code in the world, but I've tried to make it clear to you what's going on rather than make it totally idiomatic. The GNU Elsip book is a great place to start if you want to do more with elisp.

HTH

Joshua Smith
For 2, I have followed my another question http://stackoverflow.com/questions/2242572/emacs-todo-indicator-at-left-side and it solves the purpose. All I have to do is replace TODO string with the import pdb*** . You solution rocks.. Thanks a lot
Gopalakrishnan Subramani
+1  A: 

I've found that Xah's Elisp Tutorial is an excellent starting point in figuring out the basics of Emacs Lisp programming. There are also some SteveY articles from a while ago that go through techniques you might find useful for learning the basics.

If you're serious about making an amended Python mode, you'll do well to take a look at Writing GNU Emacs Extensions, which is available as a PDF.

Finally, the most useful resource for me is actually Emacs itself. I make frequent use of M-x apropos and M-x describe-key to figure out how built-in functions work, and whether there's something already in place to do what I want.

The specific things you want to look like they can be done through some simple use of insert, and a few search/replace functions, so that'll be a good starting point.

Inaimathi
Thanks for wonderful references materials. This year I will get to learn the basic elisp.
Gopalakrishnan Subramani