Function: python-base-mode

python-base-mode is an autoloaded, interactive and byte-compiled function defined in python.el.gz.

Signature

(python-base-mode)

Documentation

Generic major mode for editing Python files.

This is a generic major mode intended to be inherited by concrete implementations. Currently there are two concrete implementations: python-mode and python-ts-mode.

In addition to any hooks its parent mode prog-mode might have run, this mode runs the hook python-base-mode-hook, as the final or penultimate step during initialization.

           python-nav-up-list
<backtab> python-indent-dedent-line
C-M-h python-mark-defun
C-M-i completion-at-point
C-M-u python-nav-backward-up-list
C-M-x python-shell-send-defun
C-c < python-indent-shift-left
C-c > python-indent-shift-right
C-c C-b python-shell-send-block
C-c C-c python-shell-send-buffer
C-c C-d python-describe-at-point
C-c C-e python-shell-send-statement
C-c C-f python-eldoc-at-point
C-c C-j imenu
C-c C-l python-shell-send-file
C-c C-p run-python
C-c C-r python-shell-send-region
C-c C-s python-shell-send-string
C-c C-t c python-skeleton-class
C-c C-t d python-skeleton-def
C-c C-t f python-skeleton-for
C-c C-t i python-skeleton-if
C-c C-t m python-skeleton-import
C-c C-t t python-skeleton-try
C-c C-t w python-skeleton-while
C-c C-v python-check
C-c C-z python-shell-switch-to-shell
C-c TAB a python-add-import
C-c TAB f python-fix-imports
C-c TAB r python-remove-import
C-c TAB s python-sort-imports
DEL python-indent-dedent-line-backspace
M-a python-nav-backward-block
M-e python-nav-forward-block

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/python.el.gz
;;;###autoload
(define-derived-mode python-base-mode prog-mode "Python"
  "Generic major mode for editing Python files.

This is a generic major mode intended to be inherited by
concrete implementations.  Currently there are two concrete
implementations: `python-mode' and `python-ts-mode'."
  (setq-local tab-width 8)
  (setq-local indent-tabs-mode nil)

  (setq-local comment-start "# ")
  (setq-local comment-start-skip "#+\\s-*")

  (setq-local parse-sexp-lookup-properties t)
  (setq-local parse-sexp-ignore-comments t)

  (setq-local forward-sexp-function python-forward-sexp-function)

  (setq-local indent-line-function #'python-indent-line-function)
  (setq-local indent-region-function #'python-indent-region)
  ;; Because indentation is not redundant, we cannot safely reindent code.
  (setq-local electric-indent-inhibit t)
  (setq-local electric-indent-chars
              (cons ?: electric-indent-chars))
  (setq-local electric-layout-rules
              `((?: . ,(lambda ()
                         (and (zerop (car (syntax-ppss)))
                              (python-info-statement-starts-block-p)
                              ;; Heuristic for walrus operator :=
                              (save-excursion
                                (goto-char (- (point) 2))
                                (looking-at (rx (not space) ":" eol)))
                              'after)))))

  ;; Add """ ... """ pairing to electric-pair-mode.
  (add-hook 'post-self-insert-hook
            #'python-electric-pair-string-delimiter 'append t)

  (setq-local paragraph-start "\\s-*$")
  (setq-local fill-paragraph-function #'python-fill-paragraph)
  (setq-local normal-auto-fill-function #'python-do-auto-fill)

  (setq-local beginning-of-defun-function #'python-nav-beginning-of-defun)
  (setq-local end-of-defun-function #'python-nav-end-of-defun)

  (add-hook 'completion-at-point-functions
            #'python-completion-at-point nil 'local)

  (add-hook 'post-self-insert-hook
            #'python-indent-post-self-insert-function 'append 'local)

  (setq-local add-log-current-defun-function
              #'python-info-current-defun)

  (setq-local skeleton-further-elements
              '((abbrev-mode nil)
                (< '(backward-delete-char-untabify (min python-indent-offset
                                                        (current-column))))
                (^ '(- (1+ (current-indentation))))))

  (with-no-warnings
    ;; suppress warnings about eldoc-documentation-function being obsolete
    (if (null eldoc-documentation-function)
        ;; Emacs<25
        (setq-local eldoc-documentation-function #'python-eldoc-function)
      (if (boundp 'eldoc-documentation-functions)
          (add-hook 'eldoc-documentation-functions #'python-eldoc-function nil t)
        (add-function :before-until (local 'eldoc-documentation-function)
                      #'python-eldoc-function))))
  (eldoc-add-command-completions "python-indent-dedent-line-backspace")

  (if (< emacs-major-version 31)
      (dolist (mode '(python-mode python-ts-mode))
        (add-to-list
         'hs-special-modes-alist
         `(,mode
           ,python-nav-beginning-of-block-regexp
           ;; Use the empty string as end regexp so it doesn't default to
           ;; "\\s)".  This way parens at end of defun are properly hidden.
           ""
           "#"
           python-hideshow-forward-sexp-function
           nil
           python-nav-beginning-of-block
           python-hideshow-find-next-block
           python-info-looking-at-beginning-of-block)))
    (setq-local hs-block-start-regexp python-nav-beginning-of-block-regexp)
    ;; Use the empty string as end regexp so it doesn't default to
    ;; "\\s)".  This way parens at end of defun are properly hidden.
    (setq-local hs-block-end-regexp "")
    (setq-local hs-c-start-regexp "#")
    (setq-local hs-forward-sexp-function #'python-hideshow-forward-sexp-function)
    (setq-local hs-find-block-beginning-function #'python-nav-beginning-of-block)
    (setq-local hs-find-next-block-function #'python-hideshow-find-next-block)
    (setq-local hs-looking-at-block-start-predicate #'python-info-looking-at-beginning-of-block))

  (setq-local outline-regexp (python-rx (* space) block-start))
  (setq-local outline-level
              (lambda ()
                "`outline-level' function for Python mode."
                (1+ (/ (current-indentation) python-indent-offset))))

  (unless python--installed-grep-hook
    (setq python--installed-grep-hook t)
    (with-eval-after-load 'grep
      (defvar grep-files-aliases)
      (defvar grep-find-ignored-directories)
      (cl-pushnew '("py" . "*.py") grep-files-aliases :test #'equal)
      (dolist (dir '(".mypy_cache" ".pytest_cache" ".ropeproject"
                     ".ruff_cache" ".tox" ".venv"))
        (cl-pushnew dir grep-find-ignored-directories))))

  (setq-local prettify-symbols-alist python-prettify-symbols-alist)

  (make-local-variable 'python-shell-internal-buffer)

  (add-hook 'flymake-diagnostic-functions #'python-flymake nil t))