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)))
(define-key python-mode-map [f5] 'python-insert-quote)
and you store your custom functions wherever floats your float.