Is there an existing Emacs function that adds the character below the before point __all__when editing Python code?
For example, for example, the cursor was on the first oin foo:
def foo():
return 42
If you did Mx python-add-to-all (or something else), it would add 'foo'in __all__.
I did not see it when I was looking googled, but, as always, maybe I am missing something obvious.
Update
I tried Trey Jackson's answer (thanks, Trey!) And made several corrections / improvements in case someone is interested (will no longer insert the inserts, and put __all__in a more typical place if it does not already exist):
(defun python-add-to-all ()
"Take the symbol under the point and add it to the __all__
list, if it 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)))))))
source
share