1

I'm learning emacs lisp and I'm trying to script using it. I wrote a script and it works fine but I just think there are a lot of things I make in bash that I can do in emacs lisp instead.

Big deal here: I'm not sure if my start-process works correct

Please suggest / show me the lisp way of scripting on my script (as example) :

#!/usr/bin/emacs --script
(message "Vision synchronization \n")
(let ((default-directory "/home/vision/"))
  (shell-command "git pull;")
  (princ (shell-command-to-string "git fetch upstream;git merge upstream/master;"))
  (princ (start-process "Vision push \n" "git" "git" "push")))

(message "Gentoo-haskell synchronization \n")
(let ((default-directory "/home/gentoo-haskell/"))
  (shell-command "git pull;")
  (princ (shell-command-to-string "git fetch upstream;git merge upstream/master;"))
  (princ (start-process "Gentoo-haskell push \n" "git" "git" "push")))

(message "Nengraphy synchronization \n")
(let ((default-directory "/home/nengraphy/"))
    (princ (start-process "Nengraphy pull \n" "git" "git" "pull")))

(message "Gentoo synchronization \n")
(let ((default-directory "/usr/portage/"))
  (message "Gentoo rsync (New files will be added, deprecated files will be deleted) : \n")
  (princ (shell-command-to-string "rsync --recursive --links --safe-links --perms --times --compress --force --whole-file --delete --timeout=180 --exclude=/.git --exclude=/metadata/cache/ --exclude=/distfiles --exclude=/local --exclude=/packages rsync://209.177.148.226/gentoo-portage/ /usr/portage/"))
  (message "We want to make extra-sure that we don't grab any metadata, since we don't keep metadata for the gentoo.org tree (space reasons)")
  (shell-command "[ -e metadata/cache ] && rm -rf metadata/cache")
  (shell-command "[ -e metadata/md5-cache ] && rm -rf metadata/md5-cache")
  (message "the rsync command wiped our critical .gitignore file, so recreate it.")
  (shell-command "echo \"distfiles/*\" > /usr/portage/.gitignore")
  (shell-command "echo \"packages/*\" >> /usr/portage/.gitignore")
  (message "profile formats fix")
  (shell-command "echo \"profile-formats = portage-1\" >> /usr/portage/metadata/layout.conf")
  (message "\"git add .\" will record all the changes to local files the git repo. So there must be no stray files.")
  (shell-command "if [ ! -d profiles/package.mask ]
  then
    mv profiles/package.mask profiles/package.mask.bak || exit 4
    install -d profiles/package.mask || exit 4
    mv profiles/package.mask.bak profiles/package.mask/gentoo || exit 4
  fi")
  (princ (shell-command-to-string "git add ."))
  (message "create a commit")
  (shell-command "git commit -a -m \"gentoo updates `date` update\"")
  (message "push these changes up.")
  (princ (shell-command-to-string "git push origin master")))

(message "Gentoo verification \n")
(princ (shell-command-to-string "emerge --sync;"))

(message "Layman synchronization \n")
(princ (shell-command-to-string "layman -S;"))

thank you!

cnd
  • 32,616
  • 62
  • 183
  • 313
  • Agreed. yet it's just shell-script. Nothing form emacs lisp here (maybe instead of start-process) but yet, I need to start from something, right :) – cnd Jun 20 '12 at 10:25
  • 4
    (comment out of order because I deleted it to re-add, after taking too long to edit...) There may be lots of things you can do in elisp instead of bash, but that doesn't necessarily mean that you *should*. This example at least seems crazy to me. All the code of note is inside string literals passed into `shell-command`, so this *is* really just an unusually inefficient and unnecessarily verbose shell script. – phils Jun 20 '12 at 10:26
  • (re: "I need to start from something, right?") I suppose so, but personally I'm not sure how to answer the question, because that's just not something I'd consider writing in elisp. Hopefully someone else will see it from a different angle. – phils Jun 20 '12 at 10:30
  • @wvxw thank you for magit point, but I think shell could be to poor sometimes. perl and python are often being used for scripting so why no emacs lisp? – cnd Jun 20 '12 at 12:36
  • The current address for magit is: http://magit.github.com/magit/ – Nicolas Dudebout Jun 20 '12 at 14:17
  • 1
    On a side note, suggest [using this on the first line](http://stackoverflow.com/questions/6238331/emacs-shell-scripts-how-to-put-initial-options-into-the-script#6259330), so you don't run your .emacs every time. – desudesudesu Jun 20 '12 at 23:34

1 Answers1

0

This is really just a bash script using some awkward elisp wrapping. There is no advantage to using elisp this way. Until you start using elisp to actually compute something, you should probably stick with bash.

You could use emacs to script opening text files and modifying the text, like what you might do with sed or awk. That would make a little more sense. At this point, it would be faster for me to do this with elisp than to look up the awk syntax.

You do have to start somewhere, but what you are doing is a very awkward fit for elisp. Better to learn by working with the strengths of a language than by forcing it to do something it is not really made for.

Also, start-process does not make sense in a script. It starts a process within emacs to allow for ongoing communication, which you do not want or need in a script.

Tyler
  • 9,872
  • 2
  • 33
  • 57