0

I wrote some extra commands for Python mode: python-insert-quote and python-insert-double-quote. I want to have these commands be available in the minibuffer when python-mode is active. How can I add these custom functions into python-mode without write a new mode?

I was thinking it might be nice to keep these additional functions saved in ~/.emacs.d/lisp/extra-python.el

I'm not sure how hooks work or if this would be the right time to use hooks. When I look at hooks, It says that hooks are used to change the behavior of a mode, giving this example:

(add-hook 'text-mode-hook 'auto-fill-mode)

The example would seem antithetical to what I'm trying to do. I just what the functions to be callable form the mini-buffer only when in python-mode, not to be called when the mode is launched.


GNU Emacs 26.1

saved in ~/.emacs.d/lisp/extra-python.el

(defun python-insert-quote ()
  "Inesert python single quote"
  (interactive)
  (let ((myline (thing-at-point 'line)))
    (while (string-match "\\([^ ].*\\)" myline )
      ;; remove line
      (kill-whole-line)
      ;; add better line
      (insert (concat "'" (match-string 0 myline) "'" "
"))
      ;; fix indent
      (forward-line -1)
      (beginning-of-line)
      (indent-for-tab-command)
      (error ""))))         ;(break) the equivalent

(defun python-insert-double-quote ()
  "Inesert python double quote"
  (interactive)
  (let ((myline (thing-at-point 'line)))
    (while (string-match "\\([^ ].*\\)" myline )
      ;; remove line
      (kill-whole-line)
      ;; add better line
      (insert (concat "\"" (match-string 0 myline) "\"" "
"))
      ;; fix indent
      (forward-line -1)
      (beginning-of-line)
      (indent-for-tab-command)
      (error ""))))         ;(break) the equivalent

(provide 'extra-python)

saved in ~/.emacs.d/init.el

;; Add commands to python mode
(add-hook 'python-mode-hook (lambda ()
  (define-key python-mode-map [f6] 'python-insert-quote)
  (define-key python-mode-map [f5] 'python-insert-double-quote)))
2
  • The typical way this is done is by assigning them to keyboard shortcuts of the given major mode; e.g., (define-key python-mode-map [f5] 'python-insert-quote) and you store your custom functions wherever floats your float.
    – lawlist
    Nov 12, 2019 at 22:59
  • @lawlist "floats your float" Love it. Funny.
    – 9716278
    Nov 12, 2019 at 23:04

1 Answer 1

0

The typical way this is done is by assigning the custom commands to keyboard shortcuts of a given major-mode; e.g.,

add to your Emacs init file

;; Add commands to python mode
(require 'extra-python)

and then either

(eval-after-load "python-mode" '(progn
  (define-key python-mode-map [f5] 'python-insert-quote)
  (define-key python-mode-map [f6] 'python-insert-double-quote))

or

(add-hook 'python-mode-hook (lambda ()
  (define-key python-mode-map [f5] 'python-insert-quote)
  (define-key python-mode-map [f6] 'python-insert-double-quote)))

One can store his/her custom commands wherever it makes sense in the opinion of the user. At the very bottom of extra-python.el, you can insert a statement (provide 'extra-python) and then ensure that the ~/.emacs.d/lisp/ directory has been added to the load-path.

If the function definitions have been loaded and are interactive (they are commands), a user can always type:

M-x NAME-OF-COMMAND
4
  • I tried the example, but I keep getting back command-execute: Wrong type argument: commandp, python-insert-double-quote
    – 9716278
    Nov 13, 2019 at 1:09
  • You need to add the statement (interactive) to anything you intend to use with a keyboard shortcut or with M-x ...; e.g., (defun python-insert-double-quote () "Doc-string" (interactive) ... BODY...) And, when you get more advanced, you may wish to add interactive codes ....
    – lawlist
    Nov 13, 2019 at 1:21
  • I'm not sure if that's the problem. I added my files to the question for you to take a look at. Thanks for you help.
    – 9716278
    Nov 13, 2019 at 1:33
  • I fixed it. I had to add (require 'extra-python) before (add-hook ...
    – 9716278
    Nov 13, 2019 at 1:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.