Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs function to add symbol to __all__ in Python mode?

Tags:

python

emacs

Is there an existing Emacs function that adds the symbol currently under the point to __all__ when editing Python code?

E.g., say the cursor was on the first o in foo:

#    v---- cursor is on that 'o'
def foo():
    return 42

If you did M-x python-add-to-all (or whatever) it would add 'foo' to __all__.

I didn't see one when I googled around, but, as always, maybe I'm missing something obvious.

Update

I tried out Trey Jackson's answer (thanks, Trey!) and made a few fixes/enhancements, in case anyone's interested (won't double-insert anymore, and puts __all__ in a more typical spot if it doesn't already exist):

(defun python-add-to-all ()
  "Take the symbol under the point and add it to the __all__
list, if it's not already there."
  (interactive)
  (save-excursion
    (let ((thing (thing-at-point 'symbol)))
      (if (progn (goto-char (point-min))
                 (let (found)
                   (while (and (not found)
                               (re-search-forward (rx symbol-start "__all__" symbol-end
                                                      (0+ space) "=" (0+ space)
                                                      (syntax open-parenthesis))
                                                  nil t))
                     (setq found (not (python-in-string/comment))))
                   found))
          (when (not (looking-at (rx-to-string
                                  `(and (0+ (not (syntax close-parenthesis)))
                                        (syntax string-quote) ,thing (syntax string-quote)))))
            (insert (format "\'%s\', " thing)))
        (beginning-of-buffer)
        ;; Put before any import lines, or if none, the first class or
        ;; function.
        (when (not (re-search-forward (rx bol (or "import" "from") symbol-end) nil t))
          (re-search-forward (rx symbol-start (or "def" "class") symbol-end) nil t))
        (forward-line -1)
        (insert (format "\n__all__ = [\'%s\']\n" thing))))))
like image 261
Jacob Gabrielson Avatar asked May 13 '09 20:05

Jacob Gabrielson


1 Answers

Not being a python programmer, I'm not sure this covers all the cases, but works for me in a simple case. It'll add the symbol to the array if the array exists, and create __all__ if it doesn't exist. Note: it does not parse the array to avoid double insertion.

(defun python-add-to-all ()
  "take the symbol under the point and add to the __all__ routine"
  (interactive)
  (save-excursion
    (let ((thing (thing-at-point 'word))
          p)
      (if (progn (goto-char (point-min))
                 (re-search-forward "^__all__ = \\[" nil t))
          (insert (format "\"%s\", " thing))
        (goto-char (point-min))
        (insert (format "__all__ = [\"%s\"]\n" thing))))))
like image 65
Trey Jackson Avatar answered Oct 06 '22 18:10

Trey Jackson