basic refactoring on python extension

This commit is contained in:
Sameer Rahmani 2015-07-21 16:42:38 +04:30
parent a391ad8fb1
commit 8753e13181
2 changed files with 60 additions and 21 deletions

View File

@ -7,9 +7,15 @@
(depends-on 'company-anaconda) (depends-on 'company-anaconda)
(depends-on 'pyvenv) (depends-on 'pyvenv)
(with-ability kivy-editor
(depends-on 'kivy-mode))
(with-ability cython-editor
(depends-on 'cython-mode))
;; Extension ------------------------------------- ;; Extension -------------------------------------
(extension python (extension python
:version "1.0" :version "2.31"
:on-initialize extensions/python-initialize) :on-initialize extensions/python-initialize)
(provide 'extensions/python) (provide 'extensions/python)

View File

@ -1,7 +1,16 @@
;; Functions ------------------------------------------------- ;; Functions -------------------------------------------------
(defun setup-keybindings ()
"Setup default key bindings for python mode"
;; FIXME: Replace the global key map with python-mode-map
(global-set-key (kbd "C-c C-b") 'python-add-breakpoint)
(global-set-key (kbd "C-c C-n") 'python-interactive))
(defun python--encoding-comment-required-p () (defun python--encoding-comment-required-p ()
(re-search-forward "[^\0-\177]" nil t)) (re-search-forward "[^\0-\177]" nil t))
;;;###autoload
(defun python--detect-encoding () (defun python--detect-encoding ()
(let ((coding-system (let ((coding-system
(or save-buffer-coding-system (or save-buffer-coding-system
@ -12,10 +21,12 @@
(coding-system-change-eol-conversion coding-system nil))) (coding-system-change-eol-conversion coding-system nil)))
"ascii-8bit"))) "ascii-8bit")))
;;;###autoload
(defun python--insert-coding-comment (encoding) (defun python--insert-coding-comment (encoding)
(let ((newlines (if (looking-at "^\\s *$") "\n" "\n\n"))) (let ((newlines (if (looking-at "^\\s *$") "\n" "\n\n")))
(insert (format "# coding: %s" encoding) newlines))) (insert (format "# coding: %s" encoding) newlines)))
;;;###autoload
(defun python-mode-set-encoding () (defun python-mode-set-encoding ()
"Insert a magic comment header with the proper encoding if necessary." "Insert a magic comment header with the proper encoding if necessary."
(save-excursion (save-excursion
@ -37,15 +48,15 @@
(when (buffer-modified-p) (when (buffer-modified-p)
(basic-save-buffer-1))))))) (basic-save-buffer-1)))))))
;; http://wenshanren.org/?p=351 ;;;###autoload
(defun python-add-breakpoint () (defun python-add-breakpoint ()
"Add a break point" "Add a break point"
(interactive) (interactive)
(newline-and-indent) (newline-and-indent)
(insert "import ipdb; ipdb.set_trace()") (insert "import ipdb; ipdb.set_trace()")
(highlight-lines-matching-regexp "^[ ]*import ipdb; ipdb.set_trace()")) (highlight-lines-matching-regexp "^[ ]*import ipdb; ipdb.set_trace()"))
(global-set-key (kbd "C-c C-b") 'python-add-breakpoint)
;;;###autoload
(defun python-interactive () (defun python-interactive ()
"Enter the interactive Python environment" "Enter the interactive Python environment"
(interactive) (interactive)
@ -53,43 +64,65 @@
(newline-and-indent) (newline-and-indent)
(insert "from IPython import embed; embed()") (insert "from IPython import embed; embed()")
(move-end-of-line 1))) (move-end-of-line 1)))
(global-set-key (kbd "C-c C-n") 'python-interactive)
;;;###autoload
(defun python-mode-defaults () (defun python-mode-defaults ()
"Defaults for Python programming." "Defaults for Python programming."
;(require 'anaconda-mode)
;(require 'eldoc-mode)
(subword-mode +1) (subword-mode +1)
(anaconda-mode 1) (anaconda-mode 1)
(eldoc-mode 1) (eldoc-mode 1)
(setq-local electric-layout-rules
'((?: . (lambda () (setup-keybindings)
(and (zerop (first (syntax-ppss))) (with-ability auto-pair
(python-info-statement-starts-block-p) (setq-local electric-layout-rules
'after))))) '((?: . (lambda ()
(and (zerop (first (syntax-ppss)))
(python-info-statement-starts-block-p)
'after))))))
;; FIXME: we don't use imenu either remove this or
;; setup imenu too
(when (fboundp #'python-imenu-create-flat-index) (when (fboundp #'python-imenu-create-flat-index)
(setq-local imenu-create-index-function (setq-local imenu-create-index-function
#'python-imenu-create-flat-index)) #'python-imenu-create-flat-index))
(add-hook 'post-self-insert-hook (add-hook 'post-self-insert-hook
#'electric-layout-post-self-insert-function nil 'local) #'electric-layout-post-self-insert-function nil 'local)
(add-hook 'after-save-hook 'prelude-python-mode-set-encoding nil 'local)) (add-hook 'after-save-hook 'python-mode-set-encoding nil 'local))
;;;###autoload ;;;###autoload
(defun extensions/python-initialize () (defun extensions/python-initialize ()
(message "Initializing 'python' extension.") (message "Initializing 'python' extension.")
(when (boundp 'company-backends) (ability python-editor ()
(add-to-list 'company-backends 'company-anaconda)) "Gives FG42 the ability to edit pytho codes."
(add-to-list 'auto-mode-alist '("\\.kv\\'" . kivy-mode)) (add-hook 'python-mode-hook 'python-mode-defaults)
(add-to-list 'auto-mode-alist '("\\.pyd\\'" . cython-mode))
(add-to-list 'auto-mode-alist '("\\.pyi\\'" . cython-mode)) (when (fboundp 'exec-path-from-shell-copy-env)
(add-to-list 'auto-mode-alist '("\\.pyx\\'" . cython-mode)) (exec-path-from-shell-copy-env "PYTHONPATH"))
(with-ability kivy-editor
(add-to-list 'auto-mode-alist
'("\\.kv\\'" . kivy-mode)))
(with-ability cython-editor
(add-to-list 'auto-mode-alist
'("\\.pyd\\'" . cython-mode))
(add-to-list 'auto-mode-alist
'("\\.pyi\\'" . cython-mode))
(add-to-list 'auto-mode-alist
'("\\.pyx\\'" . cython-mode))))
(when (fboundp 'exec-path-from-shell-copy-env) (ability python-code-completion ('code-completion)
(exec-path-from-shell-copy-env "PYTHONPATH")) "Gives FG42 the ability to complete python codes."
(add-hook 'python-mode-hook (lambda ()
(run-hooks 'python-mode-defaults))))
(when (boundp 'company-backends)
(add-to-list 'company-backends 'company-anaconda))))
(provide 'extensions/python/init) (provide 'extensions/python/init)