From: Stefan Bund Date: Mon, 7 May 2012 08:23:55 +0000 (+0200) Subject: fix python keybindings, auto cleanup python imports on save X-Git-Url: http://g0dil.de/git?p=emacs-init.git;a=commitdiff_plain;h=0c167e76d08ccccac58ed8a04d66176647be580b fix python keybindings, auto cleanup python imports on save --- diff --git a/auto-install/gnus-autocheck.el b/auto-install/gnus-autocheck.el new file mode 100644 index 0000000..ae66ce8 --- /dev/null +++ b/auto-install/gnus-autocheck.el @@ -0,0 +1,169 @@ +;;; gnus-autocheck.el +;; Auto check for new incoming news and mails +;; Autocheck is binded to "vs" to start it and to "ve" to end it in Group mode +;; +;; Copyright (c) Olivier Sirven +;; +;; This file is not part of GNU Emacs. +;; +;; This program is free software; you can redistribute it and/or +;; modify it under the terms of the GNU General Public License as +;; published by the Free Software Foundation version 2. +;; +;; This program is distributed in the hope that it will be useful, but +;; WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;; General Public License for more details. +;; +;; For a copy of the GNU General Public License, search the Internet, +;; or write to the Free Software Foundation, Inc., 59 Temple Place, +;; Suite 330, Boston, MA 02111-1307 USA +;; + +(defvar gnus-autocheck-version-number "0.2" "Gnus autocheck version number") + +(require 'gnus) + +(defun gnus-autocheck-hourp(str) + "Returns non-nil value if STR is a valid time using format HH:MM" + + (let (hour minute) + + (when (and + (stringp str) + (string-match "^\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)$" str)) + (setq hour (string-to-number (match-string 1 str))) + (setq minute (string-to-number (match-string 2 str))) + + (and + (>= hour 0) + (< hour 24) + (>= minute 0) + (< minute 60))))) + +;; custom +(defgroup gnus-autocheck nil + "gnus-autocheck.el is an emacs package for interfacing with Gnus. +It makes then possible to define a time frame for gnus to automatically +check for new news and mails" + :version gnus-autocheck-version-number + :group 'gnus) + +(defcustom gnus-autocheck-interval + (* 60 5) + "Interval in seconds between two checks" + :type 'integer + :group 'gnus-autocheck) + +(defcustom gnus-autocheck-hook + '() + "Use this hook to customize what needs to be done before actually checking +for news and mails" + :type 'hook + :group 'gnus-autocheck) + +(defcustom gnus-autocheck-active-days + () + "List of days (from Sun to Sat) when the autocheck mode will be active. +For example you can definie it to be active on Saturday and Sunday. + +Leave blank if you don't need it." + :type '(repeat (choice (cons :tag "Sunday" 'Sun) + (cons :tag "Monday" 'Mon) + (cons :tag "Tuesday" 'Tue) + (cons :tag "Wednesday" 'Wed) + (cons :tag "Thursday" 'Thu) + (cons :tag "Friday" 'Fri) + (cons :tag "Saturday" 'Sat))) + :group 'gnus-autocheck) + +(defcustom gnus-autocheck-active-periods + () + "List of hours periods when the autocheck mode will be active. +For example you can define it to be active during 00:00 to 08:00 +and during 19:00 to 23:59. Time format is HH:MM and symbol is expected +to be a string. + +Leave blank if you don't need it." + :type '(repeat (list (restricted-sexp :tag "Start hour" :match-alternatives (gnus-autocheck-hourp)) + (restricted-sexp :tag "Stop hour" :match-alternatives (gnus-autocheck-hourp)))) + :group 'gnus-autocheck) + +(defvar gnus-autocheck-timer + nil + "Store the gnus autocheck timer refrence") + +(defun gnus-autocheck-get-new-mails-news() + "Get all new mails both by calling offlineimap and gnus-group-get-new-news +You can use gnus-autocheck-hook hook to customize what needs to be done before +checking for news" + (message "autocheck: checking news ...") + (run-hooks 'gnus-autocheck-hook) + ;(gnus-group-get-new-news) + t +) + +(defun gnus-auto-checkmails() + "Callback used auto check for new incoming emails during night hours" + (let ((current-day (substring (current-time-string) 0 3)) + (current-hour (string-to-number (substring (current-time-string) 11 13))) + (current-minute (string-to-number (substring (current-time-string) 14 16)))) + (if (and (or (not gnus-autocheck-active-days) + (loop for day-to-check in gnus-autocheck-active-days + thereis (string= day-to-check current-day))) + (or (not gnus-autocheck-active-periods) + (loop for period-to-check in gnus-autocheck-active-periods + thereis (let ((period-time-start-hour + (string-to-number (substring (nth 0 period-to-check) 0 2))) + (period-time-start-minute + (string-to-number (substring (nth 0 period-to-check) 3))) + (period-time-stop-hour + (string-to-number (substring (nth 1 period-to-check) 0 2))) + (period-time-stop-minute + (string-to-number (substring (nth 1 period-to-check) 3)))) + (and (>= current-hour period-time-start-hour) + (or (= current-hour period-time-start-hour) + (>= current-minute period-time-start-minute)) + (<= current-hour period-time-stop-hour) + (<= current-minute period-time-stop-minute)))))) + (gnus-autocheck-get-new-mails-news)))) + +(defun gnus-autocheck-start(&optional start-at-time) + "Start auto check for news and mails" + (interactive) + + (if (eq start-at-time nil) + (setq start-at-time gnus-autocheck-interval)) + + (if gnus-autocheck-timer + (message "autocheck is already enabled") + (setq gnus-autocheck-timer (run-at-time start-at-time gnus-autocheck-interval 'gnus-auto-checkmails)) + (message "start autocheck") + (add-to-list 'global-mode-string "--[GnusAuto]" t 'equal) + ) +) + +(defun gnus-autocheck-stop() + "Stop auto check for news and mails" + (interactive) + + (if (not gnus-autocheck-timer) + (message "autocheck is not enabled") + (cancel-timer gnus-autocheck-timer) + (setq gnus-autocheck-timer nil) + (message "stop autocheck") + (setq global-mode-string (loop for elt in global-mode-string + if (not (equal elt "--[GnusAuto]")) collect elt)) + ) +) + +;; bind autocheck to "vs" to start it and to "ve" to end it +(define-key gnus-group-mode-map (kbd "vs") '(lambda() + (interactive) + (gnus-autocheck-start 0))) +(define-key gnus-group-mode-map (kbd "ve") 'gnus-autocheck-stop) + +(gnus-autocheck-start) +(add-hook 'gnus-exit-gnus-hook 'gnus-autocheck-stop) + +(provide 'gnus-autocheck) diff --git a/python/init_python.el b/python/init_python.el index b238fcf..9a6e117 100644 --- a/python/init_python.el +++ b/python/init_python.el @@ -22,6 +22,15 @@ ;; '(add-to-list 'pymacs-load-path YOUR-PYMACS-DIRECTORY")) (pymacs-load "ropemacs" "rope-") (setq ropemacs-enable-autoimport t) + +;; M-/ rope-code-assist +;; C-c g rope-goto-definition +;; C-c d rope-show-doc +;; C-c f rope-find-occurrences +;; M-? rope-lucky-assist + +(define-key ropemacs-local-keymap "\M-/" 'hippie-expand) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Auto-completion ;;; Integrates: @@ -107,7 +116,7 @@ '("\\.py\\'" flymake-pyflakes-init))) (add-hook 'find-file-hook 'flymake-find-file-hook) -(defun py-cleanup-imports () +(defun py-cleanup-imports (&optional nowait) (interactive) (let (beg end) (if (region-active-p) @@ -150,7 +159,7 @@ (insert "\n" prefix)) (forward-line 1))) (sort-lines nil b (point)) - (if (boundp flymake-is-running) + (if (and (not nowait) (boundp flymake-is-running)) (progn (while flymake-is-running (sit-for .1)) (flymake-start-syntax-check) diff --git a/python/setup.el b/python/setup.el index f325608..ffb1b4d 100644 --- a/python/setup.el +++ b/python/setup.el @@ -4,4 +4,17 @@ (add-to-list 'load-path self-dir)) (load-library "auto-completion") + (require 'init_python) + +(defun write-file-py-cleanup-imports () + (save-excursion + (condition-case nil + (py-cleanup-imports) + (error . nil))) + nil) + +(defun python-init-auto-cleanup-imports-on-save () + (add-hook 'write-file-functions 'write-file-py-cleanup-imports nil t)) + +(add-hook 'python-mode-hook 'python-init-auto-cleanup-imports-on-save)