aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorFabián Ezequiel Gallina2014-07-28 00:35:37 -0300
committerFabián Ezequiel Gallina2014-07-28 00:35:37 -0300
commitca43b536253239560953b7755bc3fe154a559861 (patch)
tree03b22155d3b3dd9e8ef9414bdaaa2a4977c234c5 /lisp/progmodes/python.el
parentf5bed1021b782347c4f57449c8f7825a9a5f2bf6 (diff)
downloademacs-ca43b536253239560953b7755bc3fe154a559861.tar.gz
emacs-ca43b536253239560953b7755bc3fe154a559861.zip
* lisp/progmodes/python.el
(python-shell-with-shell-buffer): New macro. (python-shell-font-lock-get-or-create-buffer) (python-shell-font-lock-kill-buffer) (python-shell-font-lock-with-font-lock-buffer) (python-shell-font-lock-cleanup-buffer) (python-shell-font-lock-toggle): Use it. (python-shell-font-lock-turn-on) (python-shell-font-lock-turn-off): Use it. Make command.
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el138
1 files changed, 78 insertions, 60 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index a3e100f0578..f7556bcb3a9 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -2159,51 +2159,64 @@ Avoids `recenter' calls until OUTPUT is completely sent."
2159 2159
2160(defvar python-shell--parent-buffer nil) 2160(defvar python-shell--parent-buffer nil)
2161 2161
2162(defmacro python-shell-with-shell-buffer (&rest body)
2163 "Execute the forms in BODY with the shell buffer temporarily current.
2164Signals an error if no shell buffer is available for current buffer."
2165 (declare (indent 0) (debug t))
2166 (let ((shell-buffer (make-symbol "shell-buffer")))
2167 `(let ((,shell-buffer (python-shell-get-buffer)))
2168 (when (not ,shell-buffer)
2169 (error "No inferior Python buffer available."))
2170 (with-current-buffer ,shell-buffer
2171 ,@body))))
2172
2162(defvar python-shell--font-lock-buffer nil) 2173(defvar python-shell--font-lock-buffer nil)
2163 2174
2164(defun python-shell-font-lock-get-or-create-buffer () 2175(defun python-shell-font-lock-get-or-create-buffer ()
2165 "Get or create a font-lock buffer for current inferior process." 2176 "Get or create a font-lock buffer for current inferior process."
2166 (if python-shell--font-lock-buffer 2177 (python-shell-with-shell-buffer
2167 python-shell--font-lock-buffer 2178 (if python-shell--font-lock-buffer
2168 (let ((process-name 2179 python-shell--font-lock-buffer
2169 (process-name (get-buffer-process (current-buffer))))) 2180 (let ((process-name
2170 (generate-new-buffer 2181 (process-name (get-buffer-process (current-buffer)))))
2171 (format "*%s-font-lock*" process-name))))) 2182 (generate-new-buffer
2183 (format "*%s-font-lock*" process-name))))))
2172 2184
2173(defun python-shell-font-lock-kill-buffer () 2185(defun python-shell-font-lock-kill-buffer ()
2174 "Kill the font-lock buffer safely." 2186 "Kill the font-lock buffer safely."
2175 (when (and python-shell--font-lock-buffer 2187 (python-shell-with-shell-buffer
2176 (buffer-live-p python-shell--font-lock-buffer)) 2188 (when (and python-shell--font-lock-buffer
2177 (kill-buffer python-shell--font-lock-buffer) 2189 (buffer-live-p python-shell--font-lock-buffer))
2178 (when (eq major-mode 'inferior-python-mode) 2190 (kill-buffer python-shell--font-lock-buffer)
2179 (setq python-shell--font-lock-buffer nil)))) 2191 (when (eq major-mode 'inferior-python-mode)
2192 (setq python-shell--font-lock-buffer nil)))))
2180 2193
2181(defmacro python-shell-font-lock-with-font-lock-buffer (&rest body) 2194(defmacro python-shell-font-lock-with-font-lock-buffer (&rest body)
2182 "Execute the forms in BODY in the font-lock buffer. 2195 "Execute the forms in BODY in the font-lock buffer.
2183The value returned is the value of the last form in BODY. See 2196The value returned is the value of the last form in BODY. See
2184also `with-current-buffer'." 2197also `with-current-buffer'."
2185 (declare (indent 0) (debug t)) 2198 (declare (indent 0) (debug t))
2186 `(save-current-buffer 2199 `(python-shell-with-shell-buffer
2187 (when (not (eq major-mode 'inferior-python-mode)) 2200 (save-current-buffer
2188 (error "Current buffer is not in `inferior-python-mode'.")) 2201 (when (not (and python-shell--font-lock-buffer
2189 (when (not (and python-shell--font-lock-buffer 2202 (get-buffer python-shell--font-lock-buffer)))
2190 (get-buffer python-shell--font-lock-buffer))) 2203 (setq python-shell--font-lock-buffer
2191 (setq python-shell--font-lock-buffer 2204 (python-shell-font-lock-get-or-create-buffer)))
2192 (python-shell-font-lock-get-or-create-buffer))) 2205 (set-buffer python-shell--font-lock-buffer)
2193 (set-buffer python-shell--font-lock-buffer) 2206 (set (make-local-variable 'delay-mode-hooks) t)
2194 (set (make-local-variable 'delay-mode-hooks) t) 2207 (let ((python-indent-guess-indent-offset nil))
2195 (let ((python-indent-guess-indent-offset nil)) 2208 (when (not (eq major-mode 'python-mode))
2196 (when (not (eq major-mode 'python-mode)) 2209 (python-mode))
2197 (python-mode)) 2210 ,@body))))
2198 ,@body)))
2199 2211
2200(defun python-shell-font-lock-cleanup-buffer () 2212(defun python-shell-font-lock-cleanup-buffer ()
2201 "Cleanup the font-lock buffer. 2213 "Cleanup the font-lock buffer.
2202Provided as a command because this might be handy if something 2214Provided as a command because this might be handy if something
2203goes wrong and syntax highlighting in the shell gets messed up." 2215goes wrong and syntax highlighting in the shell gets messed up."
2204 (interactive) 2216 (interactive)
2205 (python-shell-font-lock-with-font-lock-buffer 2217 (python-shell-with-shell-buffer
2206 (delete-region (point-min) (point-max)))) 2218 (python-shell-font-lock-with-font-lock-buffer
2219 (delete-region (point-min) (point-max)))))
2207 2220
2208(defun python-shell-font-lock-comint-output-filter-function (output) 2221(defun python-shell-font-lock-comint-output-filter-function (output)
2209 "Clean up the font-lock buffer after any OUTPUT." 2222 "Clean up the font-lock buffer after any OUTPUT."
@@ -2258,49 +2271,54 @@ goes wrong and syntax highlighting in the shell gets messed up."
2258(defun python-shell-font-lock-turn-on (&optional msg) 2271(defun python-shell-font-lock-turn-on (&optional msg)
2259 "Turn on shell font-lock. 2272 "Turn on shell font-lock.
2260With argument MSG show activation message." 2273With argument MSG show activation message."
2261 (python-shell-font-lock-kill-buffer) 2274 (interactive "p")
2262 (set (make-local-variable 'python-shell--font-lock-buffer) nil) 2275 (python-shell-with-shell-buffer
2263 (add-hook 'post-command-hook 2276 (python-shell-font-lock-kill-buffer)
2264 #'python-shell-font-lock-post-command-hook nil 'local) 2277 (set (make-local-variable 'python-shell--font-lock-buffer) nil)
2265 (add-hook 'kill-buffer-hook 2278 (add-hook 'post-command-hook
2266 #'python-shell-font-lock-kill-buffer nil 'local) 2279 #'python-shell-font-lock-post-command-hook nil 'local)
2267 (add-hook 'comint-output-filter-functions 2280 (add-hook 'kill-buffer-hook
2268 #'python-shell-font-lock-comint-output-filter-function 2281 #'python-shell-font-lock-kill-buffer nil 'local)
2269 'append 'local) 2282 (add-hook 'comint-output-filter-functions
2270 (when msg 2283 #'python-shell-font-lock-comint-output-filter-function
2271 (message "Shell font-lock is enabled"))) 2284 'append 'local)
2285 (when msg
2286 (message "Shell font-lock is enabled"))))
2272 2287
2273(defun python-shell-font-lock-turn-off (&optional msg) 2288(defun python-shell-font-lock-turn-off (&optional msg)
2274 "Turn off shell font-lock. 2289 "Turn off shell font-lock.
2275With argument MSG show deactivation message." 2290With argument MSG show deactivation message."
2276 (python-shell-font-lock-kill-buffer) 2291 (interactive "p")
2277 (when (python-util-comint-last-prompt) 2292 (python-shell-with-shell-buffer
2278 ;; Cleanup current fontification 2293 (python-shell-font-lock-kill-buffer)
2279 (remove-text-properties 2294 (when (python-util-comint-last-prompt)
2280 (cdr (python-util-comint-last-prompt)) 2295 ;; Cleanup current fontification
2281 (line-end-position) 2296 (remove-text-properties
2282 '(face nil font-lock-face nil))) 2297 (cdr (python-util-comint-last-prompt))
2283 (set (make-local-variable 'python-shell--font-lock-buffer) nil) 2298 (line-end-position)
2284 (remove-hook 'post-command-hook 2299 '(face nil font-lock-face nil)))
2285 #'python-shell-font-lock-post-command-hook'local) 2300 (set (make-local-variable 'python-shell--font-lock-buffer) nil)
2286 (remove-hook 'kill-buffer-hook 2301 (remove-hook 'post-command-hook
2287 #'python-shell-font-lock-kill-buffer 'local) 2302 #'python-shell-font-lock-post-command-hook'local)
2288 (remove-hook 'comint-output-filter-functions 2303 (remove-hook 'kill-buffer-hook
2289 #'python-shell-font-lock-comint-output-filter-function 2304 #'python-shell-font-lock-kill-buffer 'local)
2290 'local) 2305 (remove-hook 'comint-output-filter-functions
2291 (when msg 2306 #'python-shell-font-lock-comint-output-filter-function
2292 (message "Shell font-lock is disabled"))) 2307 'local)
2308 (when msg
2309 (message "Shell font-lock is disabled"))))
2293 2310
2294(defun python-shell-font-lock-toggle (&optional msg) 2311(defun python-shell-font-lock-toggle (&optional msg)
2295 "Toggle font-lock for shell. 2312 "Toggle font-lock for shell.
2296With argument MSG show activation/deactivation message." 2313With argument MSG show activation/deactivation message."
2297 (interactive "p") 2314 (interactive "p")
2298 (set (make-local-variable 'python-shell-font-lock-enable) 2315 (python-shell-with-shell-buffer
2299 (not python-shell-font-lock-enable)) 2316 (set (make-local-variable 'python-shell-font-lock-enable)
2300 (if python-shell-font-lock-enable 2317 (not python-shell-font-lock-enable))
2301 (python-shell-font-lock-turn-on msg) 2318 (if python-shell-font-lock-enable
2302 (python-shell-font-lock-turn-off msg)) 2319 (python-shell-font-lock-turn-on msg)
2303 python-shell-font-lock-enable) 2320 (python-shell-font-lock-turn-off msg))
2321 python-shell-font-lock-enable))
2304 2322
2305(define-derived-mode inferior-python-mode comint-mode "Inferior Python" 2323(define-derived-mode inferior-python-mode comint-mode "Inferior Python"
2306 "Major mode for Python inferior process. 2324 "Major mode for Python inferior process.