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))))))