🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.emacs.help
Article #189170

Re: Grabbing filename with doing M-x compile

#189170
From: Ray Nickson
Date: Sun, 21 Apr 1991 21:00
48 lines
2822 bytes
In article <JC.91Apr21010147@condor.bu.edu> jc@condor.bu.edu (James Cameron) writes:

   Basically, if I am in Buffer foo.c and hit M-x compile and in my startup
   file I have:

   (setq compile-command "gcc ")

   Then I want to see:

   Compile command: gcc foo.c

   Anyone have any suggestions or pointers?

You can put compile-command: "gcc foo.c" in your Local Variables
section, if you have local variables turned on (though the version of
the emacsclient server I use seems to turn it off, presumably under
the mistaken assumption that security is more important than
usability.  One day, I'll get around to trying gnuserv).

I use the following solution, which has problems but does the job for
me.

;; Ray Nickson [nickson@circa.comp.vuw.ac.nz] - Fri Sep  8 12:09:42 1989
(defun substitute-in-string (str from-str to-str)
  "Return STR with all occurrences of FROM-STR replaced by TO-STR.  No case
folding or delimiting etc."
  ;; I hate temp buffers
  (let ((res "") pos (from-re (regexp-quote from-str)))
    (while (setq pos (string-match from-re str))
      (setq res (concat res (substring str 0 pos) to-str)
	    str (substring str (match-end 0))))
    (concat res str)))

(defun set-compile-command (cmd &rest args)
  "Set the compile command.  First arg is the format string, which can include
all the usual %s etc. (see format), and also %f which gives the 
buffer-file-name with .c removed, and does not absorb any args."
  (let* ((f (file-name-nondirectory (buffer-file-name)))
	 (r (substring f 0 (- (length f) 2))))
    (setq cmd (substitute-in-string cmd "%f" r))
    (make-variable-buffer-local 'compile-command)
    (setq compile-command (apply 'format cmd args))))

Then I put something like 
eval: (set-compile-command "gcc -o %f %f.c <other stuff>") 
in my Local Variables --- this has the advantage that it doesn't
matter if I rename the file.  Biggest disadvantage is that people
complain when emacs dings at them when they load my files ...
Technical Details
Message-ID: <1991Apr21.210043.16994@comp.vuw.ac.nz>
Path: arc1-ca.pugleaf.net!gmdzi!unido!fauern!ira.uka.de!sol.ctr.columbia.edu!emory!wuarchive!waikato.ac.nz!comp.vuw.ac.nz!nickson
References: <JC.91Apr21010147@condor.bu.edu>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 103b8b,c20840b6081e9682
X-Google-Attributes: gid103b8b,public
X-Google-ArrivalTime: 1991-04-21 14:58:28 PST
Path: arc1-ca.pugleaf.net!gmdzi!unido!fauern!ira.uka.de!sol.ctr.columbia.edu!emory!wuarchive!waikato.ac.nz!comp.vuw.ac.nz!nickson
From: Ray.Nickson@comp.vuw.ac.nz (Ray Nickson)
Newsgroups: gnu.emacs.help
Subject: Re: Grabbing filename with doing M-x compile
Message-ID: <1991Apr21.210043.16994@comp.vuw.ac.nz>
Date: 21 Apr 91 21:00:43 GMT
References: <JC.91Apr21010147@condor.bu.edu>
Sender: news@comp.vuw.ac.nz (News Admin)
Distribution: gnu
Organization: Victoria University of Wellington, Wellington, New Zealand
Lines: 48
Nntp-Posting-Host: turakirae.comp.vuw.ac.nz
In-Reply-To: jc@condor.bu.edu's message of 21 Apr 91 05: 01:47 GMT