Last active
February 4, 2020 06:23
-
-
Save redraiment/4a25d2c88a0c77e3f7a078a3c4702d2b to your computer and use it in GitHub Desktop.
zsh的配置:elisp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env fish | |
function perror --description '显示信息到错误输出' | |
if test (count $argv) -gt 0 | |
printf -- $argv >&2 | |
end | |
echo >&2 | |
end | |
function raise --description '抛出异常消息并退出' | |
perror $argv | |
exit 1 | |
end | |
function usage --description 'elisp 命令用法' | |
perror 'Usage: elisp [OPTIONS] FILE [arguments]' | |
perror | |
perror 'elisp is a tool for run FILE as an Emacs Lisp script.' | |
perror | |
perror 'Action options:' | |
perror | |
perror ' FILE visit FILE, FILE is required if -e is not specified' | |
perror ' -e/--eval EXPR evaluate Emacs Lisp expression EXPR' | |
perror ' -l/--library DIR prepend DIR to load-path' | |
perror ' -i/--include FILE load Emacs Lisp FILE using the load function' | |
perror | |
perror 'Display options:' | |
perror | |
perror ' -h/--help display this help and exit' | |
perror | |
perror 'Report bugs to [email protected].' | |
exit 1 | |
end | |
set -l PARAMETERS --batch --no-x-resources --no-splash -L $PWD | |
set -l site ~/.emacs.d/site | |
if test -d "$site" | |
set -a PARAMETERS -L "$site" | |
set -l initrc "$site/init.el" | |
if test -f "$initrc" | |
set -a PARAMETERS -l "$initrc" | |
end | |
end | |
set -l oneline false | |
set -l index 1 | |
while set -q argv[$index] | |
if string match -q -r -- '^-' $argv[$index] | |
set -l option "$argv[$index]" | |
set -e argv[$index] | |
set -l instruct | |
switch "$option" | |
case -- | |
break | |
case -e --eval | |
set instruct --eval | |
set oneline true | |
case -l --library | |
set instruct -L | |
case -i --include | |
set instruct -l | |
case \* | |
usage | |
end | |
if not set -q argv[$index] | |
raise "OPTION `$option' requires an argument" | |
end | |
set -l argument "$argv[$index]" | |
set -e argv[$index] | |
set -a PARAMETERS "$instruct" "$argument" | |
else | |
set index (math $index + 1) | |
end | |
end | |
if set -q argv[1] | |
if test ! -f "$argv[1]" | |
raise "FILE `$argv[1]' does not exists." | |
end | |
set -l FILE "$argv[1]" | |
set -e argv[1] | |
set -l arguments '' | |
if set -q argv[1] | |
set arguments (string join ' ' '"'(string replace -a -r '(?=")' '\\\\\\\\' $argv)'"') | |
end | |
set -a PARAMETERS --eval "(setq command-line-args (list $arguments))" -l "$FILE" | |
else if not $oneline | |
usage | |
end | |
exec /usr/local/bin/emacs $PARAMETERS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
emacs_command='/usr/local/bin/emacs' | |
emacs_lisp_home=~/.emacs.d/scripts | |
if [ ! -d "$emacs_lisp_home" ]; then | |
mkdir -p "$emacs_lisp_home" | |
fi | |
emacs_lisp_initialize_file="${emacs_lisp_home}/initialize.el" | |
if [ ! -f "$emacs_lisp_initialize_file" ]; then | |
touch "$emacs_lisp_initialize_file" | |
fi | |
alias elisp="${emacs_command} --no-init-file --no-x-resources --no-splash --batch -L ${emacs_lisp_home} -l ${emacs_lisp_initialize_file} -l" | |
unset emacs_command | |
unset emacs_lisp_home | |
unset emacs_lisp_initialize_file |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require 'cl) | |
(defun printf (template &rest parameters) | |
"格式化输出到标准输出,模仿C语言的printf。" | |
(princ (apply #'format (cons template parameters)))) | |
(defun read-line (&optional prompt) | |
"从标准输入中读取一整行字符串" | |
(read-from-minibuffer (or prompt ""))) | |
(defun read-object (&optional prompt) | |
"从标准输入中读取一个Lisp对象" | |
(when prompt | |
(printf prompt)) | |
(read t)) | |
(defmacro map (fn seq &rest seqs) | |
"模拟Clojure的map函数" | |
(let* ((sequences (cons seq seqs)) | |
(var-names (loop repeat (length sequences) | |
collect (gensym "elisp-")))) | |
`(loop ,@(loop for sequence in sequences | |
for var-name in var-names | |
nconc (list 'for var-name 'in sequence)) | |
collect ,(cons 'funcall (cons fn var-names))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment