unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Re: moy-compilation-schedule-execute.el
       [not found] <vpq8yjalwzo.fsf@ecrins.imag.fr>
@ 2004-02-10 16:49 ` Kevin Rodgers
  2004-02-10 17:08   ` moy-compilation-schedule-execute.el Matthieu Moy
  0 siblings, 1 reply; 7+ messages in thread
From: Kevin Rodgers @ 2004-02-10 16:49 UTC (permalink / raw)


Matthieu Moy wrote:
 > In brief,  this allows you to  ask Emacs to execute  something after a
 > compilation launched with M-x compile RET is over.
 >
 > Comments/suggestions/feedback are welcome !
...
 > ;;; Commentary:
 >
 > ;; Adds the notion of `compilation-end-hook-once' : A list of functions
 > ;; that will be executed once (and then removed from the hook) at the
 > ;; end of the running compilation.
...
 > ;;;###autoload
 > (defvar compilation-end-hook-once nil)
 >
 > ;;;###autoload
 > (defvar compilation-end-hook nil)
 >
 > ;;;###autoload
 > (defadvice compilation-handle-exit
 >   (after compilation-end-advice activate)
 >   (run-hooks compilation-end-hook)
 >   (mapcar '(lambda (x)
 > 	       (funcall x)
 > 	       (remove-hook 'compilation-end-hook-once x))
 > 	    compilation-end-hook-once))

Why are the functions removed from the hook?  (That means they have to
be added again each time you compile, right?)

Why don't you use compilation-finish-functions, instead of defining a
new hook variable (that in turn requires you to advise
compilation-handle-exit to run it)?

-- 
Kevin Rodgers

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: moy-compilation-schedule-execute.el
  2004-02-10 16:49 ` moy-compilation-schedule-execute.el Kevin Rodgers
@ 2004-02-10 17:08   ` Matthieu Moy
  2004-02-10 17:44     ` moy-compilation-schedule-execute.el Stefan Monnier
  0 siblings, 1 reply; 7+ messages in thread
From: Matthieu Moy @ 2004-02-10 17:08 UTC (permalink / raw)


Kevin Rodgers <ihs_4664@yahoo.com> writes:

>  > ;;;###autoload
>  > (defvar compilation-end-hook-once nil)
>  >
>  > ;;;###autoload
>  > (defvar compilation-end-hook nil)

> Why are the functions removed from the hook?  (That means they have to
> be added again each time you compile, right?)

You have  both options :  compilation-end-hook is a  "permanent hook",
and compilation-end-hook-once  is the set  of functions to run  at the
end of /this/ compilation. 

the typical use is this :

(defun moy-execute-in-ansi-term (buffer)
  (switch-to-buffer buffer)
  (term-send-raw-string "\n")
  )

;;;###autoload
(defun moy-compilation-schedule-execute-in-ansi-term ()

[...]

  (interactive)
  (moy-compilation-schedule-execute 'moy-execute-in-ansi-term))

I mapped it to C-M-RET, and the typical use is

Edit the source code
M-x recompile RET
switch to the shell buffer
./name_of_the_executable C-M-RET
Drink a coffee
When I come back, the compilation  is over and the executable has been
run.

But I don't want it to be run more than once ! 

> Why don't you use compilation-finish-functions, instead of defining a
> new hook variable (that in turn requires you to advise
> compilation-handle-exit to run it)?

Probably because I wasn't aware of this ^_^ ! 

(M-x apropos RET -hook$ RET didn't find it ...)

-- 
Matthieu

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: moy-compilation-schedule-execute.el
  2004-02-10 17:08   ` moy-compilation-schedule-execute.el Matthieu Moy
@ 2004-02-10 17:44     ` Stefan Monnier
  2004-02-10 17:55       ` moy-compilation-schedule-execute.el Matthieu Moy
  0 siblings, 1 reply; 7+ messages in thread
From: Stefan Monnier @ 2004-02-10 17:44 UTC (permalink / raw)


> You have  both options :  compilation-end-hook is a  "permanent hook",
> and compilation-end-hook-once  is the set  of functions to run  at the
> end of /this/ compilation. 

How about not adding another hook but using a function like the one below
instead?

(defun add-hook-once (hook function append local)
  "Same as `add-hook', but FUN is only run once.
Also contrary to `add-hook', this is not idempotent."
  ;; FIXME: need to check if `function' was already added to the hook.
  (let ((code (list 'lambda)))
    (setcdr code `(() (,function) (remove-hook ',hook ',code ',local)))
    (add-hook hook code append local)))


-- Stefan

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: moy-compilation-schedule-execute.el
  2004-02-10 17:44     ` moy-compilation-schedule-execute.el Stefan Monnier
@ 2004-02-10 17:55       ` Matthieu Moy
  2004-02-10 18:24         ` moy-compilation-schedule-execute.el Matthieu Moy
  2004-02-12 15:51         ` moy-compilation-schedule-execute.el Kai Grossjohann
  0 siblings, 2 replies; 7+ messages in thread
From: Matthieu Moy @ 2004-02-10 17:55 UTC (permalink / raw)


Stefan Monnier <monnier@iro.umontreal.ca> writes:

> How about not adding another hook but using a function like the one below
> instead?
>
> (defun add-hook-once (hook function append local)
>   "Same as `add-hook', but FUN is only run once.
> Also contrary to `add-hook', this is not idempotent."
>   ;; FIXME: need to check if `function' was already added to the hook.
>   (let ((code (list 'lambda)))
>     (setcdr code `(() (,function) (remove-hook ',hook ',code ',local)))
>     (add-hook hook code append local)))

Ah, good. I  had been looking for something like  this. I was thinking
about something like

(defun f()
  (do something)
  (remove-hook 'compilation-end-hook /myself/))

But didn't find any way to code /myself/.

-- 
Matthieu

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: moy-compilation-schedule-execute.el
  2004-02-10 17:55       ` moy-compilation-schedule-execute.el Matthieu Moy
@ 2004-02-10 18:24         ` Matthieu Moy
  2004-02-12 15:51         ` moy-compilation-schedule-execute.el Kai Grossjohann
  1 sibling, 0 replies; 7+ messages in thread
From: Matthieu Moy @ 2004-02-10 18:24 UTC (permalink / raw)


Matthieu Moy <MatthieuNOSPAM.Moy@imag.fr.invalid> writes:

>> How about not adding another hook but using a function like the one below
>> instead?

OK, here's a new version. Not heavily tested, but seems to work.

;;; moy-compilation-schedule-execute.el --- Schedule a command to execute at the end of compilation

;; Copyright (C) 2004  Matthieu MOY

;; Author: Matthieu Moy <Matthieu.Moy@imag.fr>
;; Keywords: convenience, processes, terminals

;; This file 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; either version 2, or (at your option)
;; any later version.

;; This file 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.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;; Adds the notion of `compilation-end-hook-once' : A list of functions
;; that will be executed once (and then removed from the hook) at the
;; end of the running compilation.
;;
;; The main commands are
;; `moy-compilation-schedule-execute-in-{shell,eshell,ansi-term,gud}',
;; that are equivalent to pressing RET in the corresponding mode if no
;; compilation is running, or that simulate a RET just after the end
;; of compilation.
;;
;; Typical use :
;; You are developping a programm named "program_name". Edit the
;; source, then, compile with
;; M-x compile
;; Now, go to the shell buffer (I suggest you use shell-toggle for
;; that). Now, type in the shell
;; ./program_name
;; And
;; M-x moy-compilation-schedule-execute-in-shell RET (Or the key you
;; decided to bind it to ...)
;; Go to dring a coffee ;-)
;; When you come back, the compilation is over in the compilation
;; buffer, and the program has been ran.
;; 
;; I personnally bind it to C-M-RET, and I have "M-x recompile" bound
;; to f9, and shell-toggle bound to f11, so I just type
;; f9 f11 <up> C-M-RET
;; To compile and execute my programs.
;; I would add that I have a patched version of shell toggle
;; http://www-verimag.imag.fr/~moy/emacs/shell-toggle-patched.el
;; That allows to toggle to a *gud* buffer too.
;; So, after 
;; C-x b *gud* RET M-x shell-toggle-this-is-the-shell-buffer RET
;; Once, I can type
;; f9 f11 r C-M-RET
;; to compile and debug my programs.

;;; Changelog
;;
;; Version 1.0 : January 10, 2004
;; - Support for ansi-term, shell, eshell and gud simulation of RET at
;; the end of compilation.
;; 
;; Version 1.1 : January 17, 2004
;; - compilation-end-hook-once : Hook run once only,
;; compilation-end-hook : hook ran at the end of each compilation.
;;
;; Version 1.2 : February 10, 2004
;; - added the function add-hook-once. Compilation-end-hook-once is
;; now deprecated.
;; - Use of compilation-finish-functions instead of a defadvice. The
;; advice is now disabled by default.
;;; Code:

(require 'compile)

;;; Shell
(defun moy-execute-in-ansi-term (buffer)
  (switch-to-buffer buffer)
  (term-send-raw-string "\n")
  )

;;;###autoload
(defun moy-compilation-schedule-execute-in-ansi-term ()
  "Command designed to be mapped to a key in term mode (more precisely
in char mode). If no compilation is running, this is equivalent to
pressing RET. If a compilation is running, then at the end of the
compilation, a RET will be simulated. This is very usefull when you
want to execute in a shell the program you are compiling."
  (interactive)
  (moy-compilation-schedule-execute 'moy-execute-in-ansi-term))


;;; EShell
(defun moy-execute-in-eshell (buffer)
  (switch-to-buffer buffer)
  (eshell-send-input)
  )

;;;###autoload
(defun moy-compilation-schedule-execute-in-eshell ()
  "Command designed to be mapped to a key in eshell mode. If no
compilation is running, this is equivalent to pressing RET. If a
compilation is running, then at the end of the compilation, a RET will
be simulated. This is very usefull when you want to execute in a
eshell the program you are compiling."
  (interactive)
  (moy-compilation-schedule-execute 'moy-execute-in-eshell))



;;; Traditional "shell" mode
(defun moy-execute-in-shell (buffer)
  (switch-to-buffer buffer)
  (comint-send-input)
  )

;;;###autoload
(defun moy-compilation-schedule-execute-in-shell ()
  "Command designed to be mapped to a key in shell mode. If no
compilation is running, this is equivalent to pressing RET. If a
compilation is running, then at the end of the compilation, a RET will
be simulated. This is very usefull when you want to execute in a
shell the program you are compiling."
  (interactive)
  (moy-compilation-schedule-execute 'moy-execute-in-shell))


;;; gud
(defun moy-execute-in-gud (buffer)
  (switch-to-buffer buffer)
  (end-of-buffer)
  (comint-send-input)
  )

;;;###autoload
(defun moy-compilation-schedule-execute-in-gud ()
  "Command designed to be mapped to a key in gud mode. If no
compilation is running, this is equivalent to pressing RET. If a
compilation is running, then at the end of the compilation, a RET will
be simulated. This is very usefull when you want to debug the program
you are compiling."
  (interactive)
  (moy-compilation-schedule-execute 'moy-execute-in-gud))


(defun moy-compilation-add-finish-functions-once (hook function)
  "Same as `add-hook', but FUN is only run once.
Also contrary to `add-hook', this is not idempotent."
  ;; FIXME: need to check if `function' was already added to the hook.
  (let ((code (list 'lambda)))
    (setcdr code `((buffer string) ; arguments
                   (,function)     ; body
                   (remove-hook ',hook ',code)))
    (add-hook hook code)))

;;;###autoload
(defun moy-compilation-schedule-execute (function)
  "Schedules the execution of the function given as an argument for
the end of the current compilation process. If no compilation is
runnging, execute the command right now.

`function' should take one argument, which is the buffer from which
this function is called."
  (if compilation-in-progress
      (let ((func `(lambda ()
                     (,function ,(current-buffer)))))
        (moy-compilation-add-finish-functions-once
         'compilation-finish-functions func))
    (funcall function (current-buffer))))

(defvar compilation-end-hook-once nil
  "Deprecated. use add-hook-once instead.")

;;;###autoload
(defvar compilation-end-hook nil)

; Disabled by default.
(defadvice compilation-handle-exit
  (after compilation-end-advice)
  (run-hooks 'compilation-end-hook)
  (mapcar '(lambda (x) 
             (funcall x)
             (remove-hook 'compilation-end-hook-once x))
          compilation-end-hook-once))
  

(provide 'moy-compilation-schedule-execute)
;;; moy-compilation-schedule-execute.el ends here

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: moy-compilation-schedule-execute.el
  2004-02-10 17:55       ` moy-compilation-schedule-execute.el Matthieu Moy
  2004-02-10 18:24         ` moy-compilation-schedule-execute.el Matthieu Moy
@ 2004-02-12 15:51         ` Kai Grossjohann
  2004-02-12 16:16           ` moy-compilation-schedule-execute.el Matthieu Moy
  1 sibling, 1 reply; 7+ messages in thread
From: Kai Grossjohann @ 2004-02-12 15:51 UTC (permalink / raw)


Matthieu Moy <MatthieuNOSPAM.Moy@imag.fr.invalid> writes:

> (defun f()
>   (do something)
>   (remove-hook 'compilation-end-hook /myself/))
>
> But didn't find any way to code /myself/.

'f

Yes, it depends on the function name.

Kai

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: moy-compilation-schedule-execute.el
  2004-02-12 15:51         ` moy-compilation-schedule-execute.el Kai Grossjohann
@ 2004-02-12 16:16           ` Matthieu Moy
  0 siblings, 0 replies; 7+ messages in thread
From: Matthieu Moy @ 2004-02-12 16:16 UTC (permalink / raw)


Kai Grossjohann <kai@emptydomain.de> writes:

> Matthieu Moy <MatthieuNOSPAM.Moy@imag.fr.invalid> writes:
>
>> (defun f()
>>   (do something)
>>   (remove-hook 'compilation-end-hook /myself/))
>>
>> But didn't find any way to code /myself/.
>
> 'f
>
> Yes, it depends on the function name.

Yes, I've given the bad example with a named function. I meant

(lambda ()
  (...) 
  (remove-hook ...)) 

But Stefan's solution is nice. 

-- 
Matthieu

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2004-02-12 16:16 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <vpq8yjalwzo.fsf@ecrins.imag.fr>
2004-02-10 16:49 ` moy-compilation-schedule-execute.el Kevin Rodgers
2004-02-10 17:08   ` moy-compilation-schedule-execute.el Matthieu Moy
2004-02-10 17:44     ` moy-compilation-schedule-execute.el Stefan Monnier
2004-02-10 17:55       ` moy-compilation-schedule-execute.el Matthieu Moy
2004-02-10 18:24         ` moy-compilation-schedule-execute.el Matthieu Moy
2004-02-12 15:51         ` moy-compilation-schedule-execute.el Kai Grossjohann
2004-02-12 16:16           ` moy-compilation-schedule-execute.el Matthieu Moy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).