all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* emacs equivalent of vi %
@ 2008-08-13  6:52 Rustom Mody
  2008-08-13  9:34 ` Lennart Borgman (gmail)
  2008-08-13 11:37 ` Arnaldo Mandel
  0 siblings, 2 replies; 19+ messages in thread
From: Rustom Mody @ 2008-08-13  6:52 UTC (permalink / raw)
  To: help-gnu-emacs

In vi one can do, for example,
:!gcc %
and the current file gets compiled -- ingeneral :
:!someCommand options % otherStuff
and the filename gets interpolated.
Is there any way to get the '%' behavior in emacs?
[Note: I know about compile; I am asking a more general question about
getting the filename]




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

* Re: emacs equivalent of vi %
  2008-08-13  6:52 emacs equivalent of vi % Rustom Mody
@ 2008-08-13  9:34 ` Lennart Borgman (gmail)
  2008-08-13 10:44   ` Rustom Mody
  2008-08-13 11:37 ` Arnaldo Mandel
  1 sibling, 1 reply; 19+ messages in thread
From: Lennart Borgman (gmail) @ 2008-08-13  9:34 UTC (permalink / raw)
  To: Rustom Mody; +Cc: help-gnu-emacs

http://www.emacswiki.org/cgi-bin/wiki/ViKeys

Rustom Mody wrote:
> In vi one can do, for example,
> :!gcc %
> and the current file gets compiled -- ingeneral :
> :!someCommand options % otherStuff
> and the filename gets interpolated.
> Is there any way to get the '%' behavior in emacs?
> [Note: I know about compile; I am asking a more general question about
> getting the filename]
> 
> 
> 




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

* Re: emacs equivalent of vi %
  2008-08-13  9:34 ` Lennart Borgman (gmail)
@ 2008-08-13 10:44   ` Rustom Mody
  2008-08-13 11:10     ` Lennart Borgman (gmail)
  0 siblings, 1 reply; 19+ messages in thread
From: Rustom Mody @ 2008-08-13 10:44 UTC (permalink / raw)
  To: help-gnu-emacs

On Wed, Aug 13, 2008 at 3:04 PM, Lennart Borgman (gmail)
<lennart.borgman@gmail.com> wrote:
> http://www.emacswiki.org/cgi-bin/wiki/ViKeys

But this does not answer my problem.  In vi you can use the visual
(aka VIsual) part or the ex part -- commands that you give with a ':'
(somewhat like M-x which puts you in the minibuffer).

The % behavior in vi -- paren matchng -- is quite unrelated to the %
in ex which substitutes (interpolates) the current file name into the
command




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

* Re: emacs equivalent of vi %
  2008-08-13 10:44   ` Rustom Mody
@ 2008-08-13 11:10     ` Lennart Borgman (gmail)
  2008-08-13 13:24       ` Rustom Mody
  0 siblings, 1 reply; 19+ messages in thread
From: Lennart Borgman (gmail) @ 2008-08-13 11:10 UTC (permalink / raw)
  To: Rustom Mody; +Cc: help-gnu-emacs

Rustom Mody wrote:
> On Wed, Aug 13, 2008 at 3:04 PM, Lennart Borgman (gmail)
> <lennart.borgman@gmail.com> wrote:
>> http://www.emacswiki.org/cgi-bin/wiki/ViKeys
> 
> But this does not answer my problem.  In vi you can use the visual
> (aka VIsual) part or the ex part -- commands that you give with a ':'
> (somewhat like M-x which puts you in the minibuffer).
> 
> The % behavior in vi -- paren matchng -- is quite unrelated to the %
> in ex which substitutes (interpolates) the current file name into the
> command

Ah, sorry. Look at the compile command:

   C-h f compile




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

* RE: emacs equivalent of vi %
  2008-08-13  6:52 emacs equivalent of vi % Rustom Mody
  2008-08-13  9:34 ` Lennart Borgman (gmail)
@ 2008-08-13 11:37 ` Arnaldo Mandel
  1 sibling, 0 replies; 19+ messages in thread
From: Arnaldo Mandel @ 2008-08-13 11:37 UTC (permalink / raw)
  To: help-gnu-emacs

Rustom Mody wrote (on Aug 13, 2008):
 > In vi one can do, for example,
 > :!gcc %
 > and the current file gets compiled -- ingeneral :
 > :!someCommand options % otherStuff
 > and the filename gets interpolated.
 > Is there any way to get the '%' behavior in emacs?
 > [Note: I know about compile; I am asking a more general question about
 > getting the filename]

See

   shell-command-on-region

or, better yet,

   shell-command-with-completion-on-region

-- 
Arnaldo Mandel                        
Departamento de Ciência da Computação - Computer Science Department
Universidade de São Paulo, Bra[sz]il	  
am@ime.usp.br
Talvez você seja um Bright http://the-brights.net Maybe you are a Bright.




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

* Re: emacs equivalent of vi %
  2008-08-13 11:10     ` Lennart Borgman (gmail)
@ 2008-08-13 13:24       ` Rustom Mody
  2008-08-13 23:27         ` Nikolaj Schumacher
  0 siblings, 1 reply; 19+ messages in thread
From: Rustom Mody @ 2008-08-13 13:24 UTC (permalink / raw)
  To: help-gnu-emacs

On Wed, Aug 13, 2008 at 4:40 PM, Lennart Borgman (gmail)
<lennart.borgman@gmail.com> wrote:
> Rustom Mody wrote:
>>
>> On Wed, Aug 13, 2008 at 3:04 PM, Lennart Borgman (gmail)
>> <lennart.borgman@gmail.com> wrote:
>>>
>>> http://www.emacswiki.org/cgi-bin/wiki/ViKeys
>>
>> But this does not answer my problem.  In vi you can use the visual
>> (aka VIsual) part or the ex part -- commands that you give with a ':'
>> (somewhat like M-x which puts you in the minibuffer).
>>
>> The % behavior in vi -- paren matchng -- is quite unrelated to the %
>> in ex which substitutes (interpolates) the current file name into the
>> command
>
> Ah, sorry. Look at the compile command:
>
>  C-h f compile
>

Yeah I know about compile but I am interested in interpolating the
file name into an arbitrary command (not just gcc)

Roughly I know what to do:

Bind the character '%' to
(file-name-nondirectory (buffer-file-name))
in the keymap that is in effect when shell-command ( M-! )  is running.

Im only not sure which keymap (and/or how to find it)




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

* Re: emacs equivalent of vi %
       [not found] <mailman.16547.1218610362.18990.help-gnu-emacs@gnu.org>
@ 2008-08-13 15:14 ` Xah
  2008-08-13 17:24 ` Scott Frazer
  1 sibling, 0 replies; 19+ messages in thread
From: Xah @ 2008-08-13 15:14 UTC (permalink / raw)
  To: help-gnu-emacs


In dired, you can type “!” to run a command on a file.
On Aug 12, 11:52 pm, "Rustom Mody" <rustompm...@gmail.com> wrote:
> In vi one can do, for example,
> :!gcc %
> and the current file gets compiled -- ingeneral :
> :!someCommand options % otherStuff
> and the filename gets interpolated.
> Is there any way to get the '%' behavior in emacs?
> [Note: I know about compile; I am asking a more general question about
> getting the filename]

as far as i know there's no similar mechanism. Here's related things i
know:

This code will run the current file:

(defun run-current-file ()
  "Execute or compile the current file.
For example, if the current buffer is the file x.pl,
then it'll call “perl x.pl” in a shell.
The file can be php, perl, python, bash, java.
File suffix is used to determine what program to run."
(interactive)
  (let (ext-map file-name file-ext prog-name cmd-str)
; get the file name
; get the program name
; run it
    (setq ext-map
          '(
            ("php" . "php")
            ("pl" . "perl")
            ("py" . "python")
            ("sh" . "bash")
            ("java" . "javac")
            )
          )
    (setq file-name (buffer-file-name))
    (setq file-ext (file-name-extension file-name))
    (setq prog-name (cdr (assoc file-ext ext-map)))
    (setq cmd-str (concat prog-name " " file-name))
    (shell-command cmd-str)))

For detail, see:
http://xahlee.org/emacs/elisp_run_current_file.html

You can easily modify it so that it takes a string, replace % with
filename, and run it as a shell command, like vi's %.

What you need to do is query users by a prompt, then take the string
and do replacement on it. Then call shell command on it as above.
Here's a tutorial on these:
 http://xahlee.org/emacs/elisp_idioms.html


"Rustom Mody" wrote:
> Roughly I know what to do:
>
> Bind the character '%' to
> (file-name-nondirectory (buffer-file-name))
> in the keymap that is in effect when shell-command ( M-! )  is running.
>
> Im only not sure which keymap (and/or how to find it)

if you implement it as i suggested above, then you don't need to
override any keymaps.

For what's worth, the minibuffer's map is minibuffer-local-map, and
the local keymap used by shell is comint-mode-map. For some detail,
see:
 http://xahlee.org/emacs/ergonomic_emacs_keybinding_minibuffer.html

  Xah
∑ http://xahlee.org/^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: emacs equivalent of vi %
       [not found] <mailman.16547.1218610362.18990.help-gnu-emacs@gnu.org>
  2008-08-13 15:14 ` Xah
@ 2008-08-13 17:24 ` Scott Frazer
  1 sibling, 0 replies; 19+ messages in thread
From: Scott Frazer @ 2008-08-13 17:24 UTC (permalink / raw)
  To: help-gnu-emacs

Rustom Mody wrote:
> In vi one can do, for example,
> :!gcc %
> and the current file gets compiled -- ingeneral :
> :!someCommand options % otherStuff
> and the filename gets interpolated.
> Is there any way to get the '%' behavior in emacs?
> [Note: I know about compile; I am asking a more general question about
> getting the filename]
> 
> 

How about this:

(defun shell-command-on-current-file (command &optional output-buffer error-buffer)
   (interactive (list (read-from-minibuffer "Shell command: "
                                            nil nil nil 'shell-command-history)
                      current-prefix-arg
                      shell-command-default-error-buffer))
   (unless (buffer-file-name)
     (error "No file associated with this buffer"))
   (setq command (replace-regexp-in-string "%" (buffer-file-name) command nil t))
   (shell-command command output-buffer error-buffer))


Scott


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

* Re: emacs equivalent of vi %
  2008-08-13 13:24       ` Rustom Mody
@ 2008-08-13 23:27         ` Nikolaj Schumacher
  2008-08-13 23:50           ` Nikolaj Schumacher
       [not found]           ` <mailman.16634.1218671422.18990.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 19+ messages in thread
From: Nikolaj Schumacher @ 2008-08-13 23:27 UTC (permalink / raw)
  To: Rustom Mody; +Cc: help-gnu-emacs

"Rustom Mody" <rustompmody@gmail.com> wrote:

> Roughly I know what to do:
>
> Bind the character '%' to
> (file-name-nondirectory (buffer-file-name))
> in the keymap that is in effect when shell-command ( M-! )  is running.
>
> Im only not sure which keymap (and/or how to find it)

You can find it with M-x apropos.
The minibuffer-.*-map variables look interesting.

Unfortunately I think there's no guarantee it will work everywhere, as
other commands might override the keymap.  You might be able to override
them once more by hooking into: `minibuffer-setup-hook'.

Also, note that you'll need to make an interactive function out of the
form to bind it to a key.


regards,
Nikolaj Schumacher




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

* Re: emacs equivalent of vi %
  2008-08-13 23:27         ` Nikolaj Schumacher
@ 2008-08-13 23:50           ` Nikolaj Schumacher
  2008-08-14  0:14             ` Lennart Borgman (gmail)
       [not found]           ` <mailman.16634.1218671422.18990.help-gnu-emacs@gnu.org>
  1 sibling, 1 reply; 19+ messages in thread
From: Nikolaj Schumacher @ 2008-08-13 23:50 UTC (permalink / raw)
  To: Rustom Mody; +Cc: help-gnu-emacs

Nikolaj Schumacher <n_schumacher@web.de> wrote:

> "Rustom Mody" <rustompmody@gmail.com> wrote:
>
>> Bind the character '%' to
>> (file-name-nondirectory (buffer-file-name))
>
> Also, note that you'll need to make an interactive function out of the
> form to bind it to a key.

Oups, that's not all...
It won't work, because `buffer-file-name' will of course return the
minibuffer's file name (i.e. none).  I'm currently not aware of an easy
way to get the originating buffer from the minibuffer.  Sorry.

regards,
Nikolaj Schumacher




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

* Re: emacs equivalent of vi %
  2008-08-13 23:50           ` Nikolaj Schumacher
@ 2008-08-14  0:14             ` Lennart Borgman (gmail)
  2008-08-14  2:04               ` Kevin Rodgers
  2008-08-14  7:13               ` Rustom Mody
  0 siblings, 2 replies; 19+ messages in thread
From: Lennart Borgman (gmail) @ 2008-08-14  0:14 UTC (permalink / raw)
  To: Nikolaj Schumacher; +Cc: Rustom Mody, help-gnu-emacs

Nikolaj Schumacher wrote:
> Nikolaj Schumacher <n_schumacher@web.de> wrote:
> 
>> "Rustom Mody" <rustompmody@gmail.com> wrote:
>>
>>> Bind the character '%' to
>>> (file-name-nondirectory (buffer-file-name))
>> Also, note that you'll need to make an interactive function out of the
>> form to bind it to a key.
> 
> Oups, that's not all...
> It won't work, because `buffer-file-name' will of course return the
> minibuffer's file name (i.e. none).  I'm currently not aware of an easy
> way to get the originating buffer from the minibuffer.  Sorry.


Maybe minibuffer-selected-window + window-buffer?




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

* Re: emacs equivalent of vi %
  2008-08-14  0:14             ` Lennart Borgman (gmail)
@ 2008-08-14  2:04               ` Kevin Rodgers
  2008-08-14  7:13               ` Rustom Mody
  1 sibling, 0 replies; 19+ messages in thread
From: Kevin Rodgers @ 2008-08-14  2:04 UTC (permalink / raw)
  To: help-gnu-emacs

Lennart Borgman (gmail) wrote:
> Nikolaj Schumacher wrote:
>> Nikolaj Schumacher <n_schumacher@web.de> wrote:
>>
>>> "Rustom Mody" <rustompmody@gmail.com> wrote:
>>>
>>>> Bind the character '%' to
>>>> (file-name-nondirectory (buffer-file-name))
>>> Also, note that you'll need to make an interactive function out of the
>>> form to bind it to a key.
>>
>> Oups, that's not all...
>> It won't work, because `buffer-file-name' will of course return the
>> minibuffer's file name (i.e. none).  I'm currently not aware of an easy
>> way to get the originating buffer from the minibuffer.  Sorry.
> 
> 
> Maybe minibuffer-selected-window + window-buffer?

Brilliant!

(defvar minibuffer-insert-buffer-directory nil)

(defun minibuffer-insert-buffer-file-name ()
   (interactive)
   (unless (minibuffer-window-active-p (selected-window))
     (error "Minibuffer window is not active"))
   (let ((minibuffer-selected-buffer-file-name
	 (buffer-file-name (window-buffer (minibuffer-selected-window)))))
     (insert (if minibuffer-insert-buffer-directory
		minibuffer-selected-buffer-file-name
	      (file-name-nondirectory minibuffer-selected-buffer-file-name)))))

(define-key minibuffer-local-map "%" 'minibuffer-insert-buffer-file-name)
(define-key minibuffer-local-completion-map "%" nil)

-- 
Kevin Rodgers
Denver, Colorado, USA





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

* Re: emacs equivalent of vi %
       [not found]           ` <mailman.16634.1218671422.18990.help-gnu-emacs@gnu.org>
@ 2008-08-14  2:51             ` Jason Rumney
  0 siblings, 0 replies; 19+ messages in thread
From: Jason Rumney @ 2008-08-14  2:51 UTC (permalink / raw)
  To: help-gnu-emacs

On Aug 14, 7:50 am, Nikolaj Schumacher <n_schumac...@web.de> wrote:

> Oups, that's not all...
> It won't work, because `buffer-file-name' will of course return the
> minibuffer's file name (i.e. none).  I'm currently not aware of an easy
> way to get the originating buffer from the minibuffer.  Sorry.

(window-buffer (minibuffer-selected-window))



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

* Re: emacs equivalent of vi %
  2008-08-14  0:14             ` Lennart Borgman (gmail)
  2008-08-14  2:04               ` Kevin Rodgers
@ 2008-08-14  7:13               ` Rustom Mody
  2008-08-14 10:01                 ` Lennart Borgman (gmail)
  2008-08-15  1:38                 ` Kevin Rodgers
  1 sibling, 2 replies; 19+ messages in thread
From: Rustom Mody @ 2008-08-14  7:13 UTC (permalink / raw)
  To: Lennart Borgman (gmail); +Cc: help-gnu-emacs

Thanks folks!  Heres the code I have:

(define-key minibuffer-local-map "%" 'percentsub)
(defun percentsub ()
  (interactive)
  (insert (file-name-nondirectory
	   (buffer-file-name
	    (window-buffer (minibuffer-selected-window))))))

Prior to Lennarts suggestion I had hacked together
(cadr (buffer-list))
in place of the last line which was of course quite unacceptable

Any further suggested improvements?  I guess this is going to fail in
some cases when theres no filename...

On Thu, Aug 14, 2008 at 5:44 AM, Lennart Borgman (gmail)
<lennart.borgman@gmail.com> wrote:
> Nikolaj Schumacher wrote:
>>
>> Nikolaj Schumacher <n_schumacher@web.de> wrote:
>>
>>> "Rustom Mody" <rustompmody@gmail.com> wrote:
>>>
>>>> Bind the character '%' to
>>>> (file-name-nondirectory (buffer-file-name))
>>>
>>> Also, note that you'll need to make an interactive function out of the
>>> form to bind it to a key.
>>
>> Oups, that's not all...
>> It won't work, because `buffer-file-name' will of course return the
>> minibuffer's file name (i.e. none).  I'm currently not aware of an easy
>> way to get the originating buffer from the minibuffer.  Sorry.
>
>
> Maybe minibuffer-selected-window + window-buffer?
>




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

* Re: emacs equivalent of vi %
  2008-08-14  7:13               ` Rustom Mody
@ 2008-08-14 10:01                 ` Lennart Borgman (gmail)
  2008-08-15  1:38                 ` Kevin Rodgers
  1 sibling, 0 replies; 19+ messages in thread
From: Lennart Borgman (gmail) @ 2008-08-14 10:01 UTC (permalink / raw)
  To: Rustom Mody; +Cc: help-gnu-emacs

Rustom Mody wrote:
> Thanks folks!  Heres the code I have:
> 
> (define-key minibuffer-local-map "%" 'percentsub)
> (defun percentsub ()
>   (interactive)
>   (insert (file-name-nondirectory
> 	   (buffer-file-name
> 	    (window-buffer (minibuffer-selected-window))))))
> 
> Prior to Lennarts suggestion I had hacked together
> (cadr (buffer-list))
> in place of the last line which was of course quite unacceptable
> 
> Any further suggested improvements?  I guess this is going to fail in
> some cases when theres no filename...

If you want to add it just to the ":" prompt:

(eval-after-load 'viper-keym
   '(define-key viper-ex-cmd-map [?%] 'percentsub))

> On Thu, Aug 14, 2008 at 5:44 AM, Lennart Borgman (gmail)
> <lennart.borgman@gmail.com> wrote:
>> Nikolaj Schumacher wrote:
>>> Nikolaj Schumacher <n_schumacher@web.de> wrote:
>>>
>>>> "Rustom Mody" <rustompmody@gmail.com> wrote:
>>>>
>>>>> Bind the character '%' to
>>>>> (file-name-nondirectory (buffer-file-name))
>>>> Also, note that you'll need to make an interactive function out of the
>>>> form to bind it to a key.
>>> Oups, that's not all...
>>> It won't work, because `buffer-file-name' will of course return the
>>> minibuffer's file name (i.e. none).  I'm currently not aware of an easy
>>> way to get the originating buffer from the minibuffer.  Sorry.
>>
>> Maybe minibuffer-selected-window + window-buffer?
>>
> 




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

* Re: emacs equivalent of vi %
  2008-08-14  7:13               ` Rustom Mody
  2008-08-14 10:01                 ` Lennart Borgman (gmail)
@ 2008-08-15  1:38                 ` Kevin Rodgers
  1 sibling, 0 replies; 19+ messages in thread
From: Kevin Rodgers @ 2008-08-15  1:38 UTC (permalink / raw)
  To: help-gnu-emacs

Rustom Mody wrote:
> Thanks folks!  Heres the code I have:
> 
> (define-key minibuffer-local-map "%" 'percentsub)
> (defun percentsub ()
>   (interactive)
>   (insert (file-name-nondirectory
> 	   (buffer-file-name
> 	    (window-buffer (minibuffer-selected-window))))))
> 
> Prior to Lennarts suggestion I had hacked together
> (cadr (buffer-list))
> in place of the last line which was of course quite unacceptable
> 
> Any further suggested improvements?  I guess this is going to fail in
> some cases when theres no filename...

As it should, no?  You can get a more informative error with

	(insert (or (file-name-nondirectory ...)
	            (error "Selected buffer is not visiting a file")))

or you can get a default value inserted (here ".") with

	(insert (or (file-name-nondirectory ...)
	            "."))

-- 
Kevin Rodgers
Denver, Colorado, USA





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

* Re: emacs equivalent of vi %
@ 2008-08-15 17:02 Emmett Grogan
  0 siblings, 0 replies; 19+ messages in thread
From: Emmett Grogan @ 2008-08-15 17:02 UTC (permalink / raw)
  To: help-gnu-emacs

Xah:
> This code will run the current file:
>
> (defun run-current-file ()
>   "Execute or compile the current file....
>   
I assembled a elisp module out of it. In case someone is interested:

;; Version: $Id: run-current-file.el 4518 2008-08-13 21:31:36Z 
emmettgrogan $
;;
;; 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; either version 2, or (at your option)
;; any later version.
;;
;; 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.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;;
;; Commentary:
;;
;; Original author: Xah Lee
;;; Adapted from http://xahlee.org/emacs/elisp_run_current_file.html
;;
;;; Code:
;;
;;;###autoload
;;;
(defgroup run-current-file-menu nil
"Execute or compile the current file.")
;;
(defcustom run-current-file-name-masks
'(
("php" . "php")
("pl" . "perl")
("py" . "python")
("sh" . "bash")
("java" . "javac")
)
"Filename extensions that switch on run-current-file.")
;;
(defun run-current-file ()
"Execute or compile the current file.
For example, if the current buffer is the file x.pl,
then it'll call “perl x.pl” in a shell.
The file can be php, perl, python, bash, java.
File suffix is used to determine what program to run."
(interactive)
(let (run-current-file-name-masks file-name file-ext prog-name cmd-str)
; get the file name
; get the program name
; run it
(setq file-name (buffer-file-name))
(setq file-ext (file-name-extension file-name))
(setq prog-name (cdr (assoc file-ext run-current-file-name-masks)))
(setq cmd-str (concat prog-name " " file-name))
(shell-command cmd-str))
)
;;
(provide 'run-current-file)
;;; (run-current-file) ends here





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

* Re: emacs equivalent of vi %
       [not found] <mailman.16820.1218819789.18990.help-gnu-emacs@gnu.org>
@ 2008-08-15 19:13 ` Xah
  2008-08-16  0:14 ` Chat
  1 sibling, 0 replies; 19+ messages in thread
From: Xah @ 2008-08-15 19:13 UTC (permalink / raw)
  To: help-gnu-emacs

On Aug 15, 10:02 am, Emmett Grogan <emmettgro...@freenet.de> wrote:
> Xah:> This code will run the current file:
>
> > (defun run-current-file ()
> >   "Execute or compile the current file....
>
> I assembled a elisp module out of it. In case someone is interested:
>
> ;; Version: $Id: run-current-file.el 4518 2008-08-13 21:31:36Z
> emmettgrogan $
> ;;
> ;; 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; either version 2, or (at your option)
> ;; any later version.
> ;;
> ;; 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.
> ;;
> ;; You should have received a copy of the GNU General Public License
> ;; along with this program; if not, write to the Free Software
> ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
> ;;
> ;; Commentary:
> ;;
> ;; Original author: Xah Lee
> ;;; Adapted from http://xahlee.org/emacs/elisp_run_current_file.html
> ;;
> ;;; Code:
> ;;
> ;;;###autoload
> ;;;
> (defgroup run-current-file-menu nil
> "Execute or compile the current file.")
> ;;
> (defcustom run-current-file-name-masks
> '(
> ("php" . "php")
> ("pl" . "perl")
> ("py" . "python")
> ("sh" . "bash")
> ("java" . "javac")
> )
> "Filename extensions that switch on run-current-file.")
> ;;
> (defun run-current-file ()
> "Execute or compile the current file.
> For example, if the current buffer is the file x.pl,
> then it'll call “perl x.pl” in a shell.
> The file can be php, perl, python, bash, java.
> File suffix is used to determine what program to run."
> (interactive)
> (let (run-current-file-name-masks file-name file-ext prog-name cmd-str)
> ; get the file name
> ; get the program name
> ; run it
> (setq file-name (buffer-file-name))
> (setq file-ext (file-name-extension file-name))
> (setq prog-name (cdr (assoc file-ext run-current-file-name-masks)))
> (setq cmd-str (concat prog-name " " file-name))
> (shell-command cmd-str))
> )
> ;;
> (provide 'run-current-file)
> ;;; (run-current-file) ends here

O, thank you.

The code i did could use some improvement. Right now it doesn't run in
the background. i.e. while it executes, your emacs is as good as
frozen. I meant to make it a background process... but there's more
coding needs to be done to get the outpt when it is done using some
elisp's “process sentinel”...

also, the hard-coded file name suffix and program name map could
perhaps employ magic-mode-alist and or auto-mode-alist...

anyone's very welcome to fork it of course.

  Xah
∑ http://xahlee.org/^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: emacs equivalent of vi %
       [not found] <mailman.16820.1218819789.18990.help-gnu-emacs@gnu.org>
  2008-08-15 19:13 ` Xah
@ 2008-08-16  0:14 ` Chat
  1 sibling, 0 replies; 19+ messages in thread
From: Chat @ 2008-08-16  0:14 UTC (permalink / raw)
  To: help-gnu-emacs

Emmett Grogan <emmettgrogan@freenet.de> writes:

> Xah:
>> This code will run the current file:
>>
>> (defun run-current-file ()
>>   "Execute or compile the current file....
>>   
> I assembled a elisp module out of it. In case someone is interested:
>
> ;; Version: $Id: run-current-file.el 4518 2008-08-13 21:31:36Z emmettgrogan $
> ;;
> ;; 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; either version 2, or (at your option)
> ;; any later version.
> ;;
> ;; 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.
> ;;
> ;; You should have received a copy of the GNU General Public License
> ;; along with this program; if not, write to the Free Software
> ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
> ;;
> ;; Commentary:
> ;;
> ;; Original author: Xah Lee
> ;;; Adapted from http://xahlee.org/emacs/elisp_run_current_file.html
> ;;
> ;;; Code:
> ;;
> ;;;###autoload
> ;;;
> (defgroup run-current-file-menu nil
> "Execute or compile the current file.")
> ;;
> (defcustom run-current-file-name-masks
> '(
> ("php" . "php")
> ("pl" . "perl")
> ("py" . "python")
> ("sh" . "bash")
> ("java" . "javac")
> )
> "Filename extensions that switch on run-current-file.")
> ;;
> (defun run-current-file ()
> "Execute or compile the current file.
> For example, if the current buffer is the file x.pl,
> then it'll call “perl x.pl” in a shell.
> The file can be php, perl, python, bash, java.
> File suffix is used to determine what program to run."
> (interactive)
> (let (run-current-file-name-masks file-name file-ext prog-name cmd-str)
> ; get the file name
> ; get the program name
> ; run it
> (setq file-name (buffer-file-name))
> (setq file-ext (file-name-extension file-name))
> (setq prog-name (cdr (assoc file-ext run-current-file-name-masks)))
> (setq cmd-str (concat prog-name " " file-name))
> (shell-command cmd-str))
> )
> ;;
> (provide 'run-current-file)
> ;;; (run-current-file) ends here
This seems to fall into two different cases - one that compiles the file and
another that runs or views the file. The first one can perhaps be done with a
wrapper on "compile" instead of shell-command so that all of the benefits of
compile are abailable. 
If you do not want to do that work, Perhaps a default makefile with rules for
all types of files would make M-x compile usable as it is.


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

end of thread, other threads:[~2008-08-16  0:14 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-08-13  6:52 emacs equivalent of vi % Rustom Mody
2008-08-13  9:34 ` Lennart Borgman (gmail)
2008-08-13 10:44   ` Rustom Mody
2008-08-13 11:10     ` Lennart Borgman (gmail)
2008-08-13 13:24       ` Rustom Mody
2008-08-13 23:27         ` Nikolaj Schumacher
2008-08-13 23:50           ` Nikolaj Schumacher
2008-08-14  0:14             ` Lennart Borgman (gmail)
2008-08-14  2:04               ` Kevin Rodgers
2008-08-14  7:13               ` Rustom Mody
2008-08-14 10:01                 ` Lennart Borgman (gmail)
2008-08-15  1:38                 ` Kevin Rodgers
     [not found]           ` <mailman.16634.1218671422.18990.help-gnu-emacs@gnu.org>
2008-08-14  2:51             ` Jason Rumney
2008-08-13 11:37 ` Arnaldo Mandel
     [not found] <mailman.16547.1218610362.18990.help-gnu-emacs@gnu.org>
2008-08-13 15:14 ` Xah
2008-08-13 17:24 ` Scott Frazer
  -- strict thread matches above, loose matches on Subject: below --
2008-08-15 17:02 Emmett Grogan
     [not found] <mailman.16820.1218819789.18990.help-gnu-emacs@gnu.org>
2008-08-15 19:13 ` Xah
2008-08-16  0:14 ` Chat

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.