help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: first emacs lisp script: hello world


From: Joel J. Adamson
Subject: Re: first emacs lisp script: hello world
Date: Thu, 13 Dec 2007 09:19:06 -0500

thorne <thorne@timbral.net> writes:

> jadamson@partners.org (Joel J. Adamson) writes:
>
>> Eric Hanchrow <offby1@blarg.net> writes:
>>
>>>     #!/usr/bin/emacs --script
>>>     (message "Hello Biscuit-lovers!")
>>>
>>> Waal, shoot, Jackson; ah dint know 'bout thet thar --script option.
>>
>> It's pretty sweet, and I got the impression that it's a fairly new
>> feature since there's very little about it on EmacsWiki.  I've already
>> written a couple shell-scripts in it.
>
> That is cool.  Goodbye bash, hello Emacs.

You can manipulate buffers in an Emacs script and save them using the
regular file-saving and file-writing commands.

> But i couldn't find anything about it in the Emacs info file (though i
> did not search high and low) now on EmacsWiki.org.  

Yeah, I found nothing myself, other than one note saying that it was a
very new feature and few people have used it.  So I decided to try it out.

Appendix C.2 "Initial Options"
|`--script FILE'
|    Run Emacs in batch mode, like `--batch', and then read and execute
|    the Lisp code in FILE.
|
|    The normal use of this option is in executable script files that
|    run Emacs.  They can start with this text on the first line
|
|         #!/usr/bin/emacs --script
|
|    which will invoke Emacs with `--script' and supply the name of the
|    script file as FILE.  Emacs Lisp then treats `#!'  as a comment
|    delimiter.


> I'd like to know basic stuff, like: Is there a way to get a
> command-line argument to the script?--etc.

The first two arguments are "/usr/bin/emacs" and "--script", so I access
command line options with (nthcdr 3 (command-line-args)), but think you
could use (command-line-args-left) instead.  For example, when I want a
list of files to load and edit:

(setq files (nthcdr 3 command-line-args))
(dolist (file files)
  (catch 'no-file
    ;; is the filereadable?
    (if (file-readable-p file)
        ;; then find the file
        (find-file file) ...)))

I started this to replace sed, since I mostly do substitutions, I wrote
a regex-replace function to take an alist of regexes and their
respective replacements.  This way I just have to type the alist,
instead of
s/.../.../Ig
a corresponding bunch of times:

(defun jedit-strip-regex (alist)
  "Takes a list of regex-replacement string pairs;
processes entire buffer."
  (interactive "sList: ")
  ;; for each cell in alist, define regex and replacement text
  (dolist (regex-cell alist)
    (let ((regex (car regex-cell))
          (replacement (cadr regex-cell)))
      ;; go to beginning of buffer
      (goto-char (point-min))
      ;; when you find the search string, replace it with replacement
      ;; text
      (while (re-search-forward regex nil t)
        (replace-match replacement nil nil)))))

I previously posted this on gnu.emacs.sources without the accompanying
script that uses it.
Joel
-- 
Joel J. Adamson
Biostatistician
Pediatric Psychopharmacology Research Unit
Massachusetts General Hospital
Boston, MA  02114
(617) 643-1432
(303) 880-3109


reply via email to

[Prev in Thread] Current Thread [Next in Thread]