2012-06-15 14 views
12

Quiero cerrar automáticamente el búfer de compilación cuando no haya ningún error ni advertencia, pero quiero mostrarlo cuando haya advertencias. ¿Alguien puede ayudarme? Este código de emacswiki solo hace el primer requisito. Cómo cambiarlo?emacs compile buffer auto close?

;; Helper for compilation. Close the compilation window if 
    ;; there was no error at all. 
    (defun compilation-exit-autoclose (status code msg) 
    ;; If M-x compile exists with a 0 
    (when (and (eq status 'exit) (zerop code)) 
     ;; then bury the *compilation* buffer, so that C-x b doesn't go there 
     (bury-buffer) 
     ;; and delete the *compilation* window 
     (delete-window (get-buffer-window (get-buffer "*compilation*")))) 
    ;; Always return the anticipated result of compilation-exit-message-function 
    (cons msg code)) 
    ;; Specify my function (maybe I should have done a lambda function) 
    (setq compilation-exit-message-function 'compilation-exit-autoclose) 
+0

¿Qué estás compilando? – Thomas

+0

@Thomas este no es el problema clave – Iceman

+1

Puede ser útil saber qué compilador se está ejecutando porque es posible que pueda usar el parámetro 'msg' para comprobar si hay errores o advertencias. – Thomas

Respuesta

15

Utilizo lo siguiente para la compilación. Mantiene el búfer de compilación si hay advertencias o errores, y lo entierra de lo contrario (después de 1 segundo).

(defun bury-compile-buffer-if-successful (buffer string) 
"Bury a compilation buffer if succeeded without warnings " 
(when (and 
     (buffer-live-p buffer) 
     (string-match "compilation" (buffer-name buffer)) 
     (string-match "finished" string) 
     (not 
      (with-current-buffer buffer 
      (goto-char (point-min)) 
      (search-forward "warning" nil t)))) 
    (run-with-timer 1 nil 
        (lambda (buf) 
         (bury-buffer buf) 
         (switch-to-prev-buffer (get-buffer-window buf) 'kill)) 
        buffer))) 
(add-hook 'compilation-finish-functions 'bury-compile-buffer-if-successful) 
+0

bueno, funciona, tal vez elimine el temporizador. – Iceman

+0

esto es genial, pero ¿por qué deja la ventana abierta después de que se cierra el búfer de compilación? esta ventana permanece abierta hasta que mueva el cursor, luego se cierra de repente. ¿Qué causa este comportamiento? – johnbakers

+0

@johnbakers: Porque todo lo que hace es cambiar el búfer en la ventana, sin cambiar el diseño de la ventana. Por lo general, no me gusta que Emacs cambie el diseño de mi ventana. Intenta jugar con 'delete-windows-on' en lugar de' switch-to-prev-buffer'. – jpkotta

2

jpkotta, funciona la mayoría de las veces. A veces, incluso si hay una advertencia, no cambia al búfer de compilación. Así que hice un cambio en su forma & que funciona ahora:

(defun bury-compile-buffer-if-successful (buffer string) 
    "Bury a compilation buffer if succeeded without warnings " 
    (if (and 
     (string-match "compilation" (buffer-name buffer)) 
     (string-match "finished" string) 
     (not 
     (with-current-buffer buffer 
      **(goto-char 1)** 
      (search-forward "warning" nil t)))) 
     (run-with-timer 1 nil 
         (lambda (buf) 
         (bury-buffer buf) 
         (switch-to-prev-buffer (get-buffer-window buf) 'kill)) 
         buffer))) 
(add-hook 'compilation-finish-functions 'bury-compile-buffer-if-successful) 
+0

Gracias, he actualizado mi respuesta. – jpkotta

Cuestiones relacionadas