【问题标题】:emacs lisp scripting optimization help requestemacs lisp 脚本优化帮助请求
【发布时间】:2012-06-22 10:33:09
【问题描述】:

我正在学习 emacs lisp,我正在尝试使用它编写脚本。我写了一个脚本,它运行良好,但我只是认为我在 bash 中做的很多事情我可以在 emacs lisp 中做。

这里很重要:我不确定我的启动过程是否正确

请建议/向我展示在我的脚本上编写脚本的 lisp 方式(例如):

#!/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;"))

谢谢!

【问题讨论】:

  • 同意。但它只是 shell 脚本。这里没有任何形式的 emacs lisp(也许不是启动进程),但是,我需要从一些东西开始,对吧 :)
  • (注释乱七八糟,因为我删除了它以重新添加,在编辑时间太长之后...)您可以在 elisp 中而不是 bash 中做很多事情,但这并没有'不一定意味着你应该。这个例子至少在我看来很疯狂。所有需要注意的代码都在传递给shell-command 的字符串文字中,所以这个实际上只是一个异常低效且不必要的冗长shell脚本。
  • (回复:“我需要从某事开始,对吗?”)我想是的,但我个人不确定如何回答这个问题,因为这不是我考虑写的东西在省略号中。希望其他人会从不同的角度看到它。
  • @wvxw 谢谢你的观点,但我认为 shell 有时可能很差。 perl 和 python 经常被用于脚本,为什么没有 emacs lisp?
  • 附带说明,建议using this on the first line,这样您就不必每次都运行 .emacs。

标签: emacs scripting elisp


【解决方案1】:

这实际上只是一个使用一些尴尬的 elisp 包装的 bash 脚本。以这种方式使用 elisp 没有任何优势。在您开始使用 elisp 实际计算某些东西之前,您可能应该坚持使用 bash。

您可以使用 emacs 编写打开文本文件和修改文本的脚本,就像使用 sed 或 awk 一样。那会更有意义。在这一点上,对我来说,使用 elisp 执行此操作比查找 awk 语法要快。

您确实必须从某个地方开始,但是您所做的对于 elisp 来说非常尴尬。最好通过利用语言的优势来学习,而不是强迫它做一些并非真正适合的事情。

此外,启动进程在脚本中没有意义。它会在 emacs 中启动一个进程,以允许您在脚本中不需要或不需要的持续通信。

【讨论】:

  • 我怎样才能异步运行进程?
  • 使用 async-shell-command,或者只是在你的 shell-command 中添加一个 &
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-06
  • 2011-05-27
  • 2022-01-26
  • 1970-01-01
  • 1970-01-01
  • 2012-01-06
  • 1970-01-01
相关资源
最近更新 更多