分享刚刚发现的一个elisp入门文档

你没发现这是个 if statement,后面的 (setq foo (cons '("a" . "this is a") foo)) 不执行吗

问个语法问题

(let ((load-path-list
       '(
         "~/.emacs.d.customize/emacs-libvterm"
         "~/.emacs.d.customize/customs"
         )))
  (setq load-path (append load-path load-path-list)))

由于 "~/.emacs.d.customize/emacs-libvterm"这个字符串,我在别的地方还要用,所以就改成下面的写法

(let* ((libvterm-dir "~/.emacs.d.customize/emacs-libvterm")
       (load-path-list
        '(
          libvterm-dir
          "~/.emacs.d.customize/customs"
          )))
  (setq load-path (append load-path load-path-list))
  (if (file-directory-p libvterm-dir)
      (let (vterm-install)
        (require 'vterm))))

libvterm-dir在load-path-list里没展开,该怎么改?

`(,libvterm-dir "~/.emacs.d.customize/customs")

或者用list函数构建列表

(list libvterm-dir "~/.emacs.d.customize/customs")

哦,想起来了。

另外宏里,是这么的写法

`(,xxx ....)

宏之外用

'(,xxx ....)

为什么要分`和‘

,,@都只能用在` 里。

为什么你觉得`只能在宏里用,是因为大部分场合都是在宏里用导致你产生了错觉(宏本质就是返回一个列表)。此外'不支持, ,@

好的,正在学习中,所以会提一些入门问题。

\` is a Lisp macro in ‘backquote.el’.

`STRUCTURE

Argument STRUCTURE describes a template to build.

The whole structure acts as if it were quoted except for certain
places where expressions are evaluated and inserted or spliced in.

For example:

b              => (ba bb bc)		; assume b has this value
`(a b c)       => (a b c)		; backquote acts like quote
`(a ,b c)      => (a (ba bb bc) c)	; insert the value of b
`(a ,@b c)     => (a ba bb bc c)	; splice in the value of b

Vectors work just like lists.  Nested backquotes are permitted.

;; When the Lisp reader sees `(...), it generates (\` (...)).
;; When it sees ,... inside such a backquote form, it generates (\, ...).
;; For ,@... it generates (\,@ ...).

;; This backquote will generate calls to the backquote-list* form.
;; Both a function version and a macro version are included.
;; The macro version is used by default because it is faster
;; and needs no run-time support.  It should really be a subr.

From backquote.el

(evil-define-key 'insert vterm-mode-map (kbd "C-a") (lambda () (interactive) (vterm-send-string "\C-a")))
(evil-define-key 'insert vterm-mode-map (kbd "C-e") (lambda () (interactive) (vterm-send-string "\C-e")))
(evil-define-key 'insert vterm-mode-map (kbd "C-k") (lambda () (interactive) (vterm-send-string "\C-k")))
(evil-define-key 'insert vterm-mode-map (kbd "C-u") (lambda () (interactive) (vterm-send-string "\C-u")))

改成下面的写法,执行后vterm-send-string把key的值当中普通的字符串了

(dolist (key '("C-a" "C-e" "C-k" "C-u"))
  (evil-define-key 'insert vterm-mode-map (kbd key)
    `(lambda () (interactive) (vterm-send-string (concat "\\" ,key)))))

改成下面的写法,提示 Wrong type argument: listp, “^A”

(dolist (key-pair '(("C-a" . "\C-a") ("C-e" . "\C-e") ("C-k" . "\C-k") ("C-u" . "\C-u")))
  (evil-define-key 'insert vterm-mode-map (kbd (car key-pair))
    `(lambda () (interactive) (vterm-send-string (cdr ,key-pair)))))

OK了

;; 方法一
(dolist (key-pair '(("C-a" . "\C-a") ("C-e" . "\C-e") ("C-k" . "\C-k") ("C-u" . "\C-u")))
  (evil-define-key 'insert vterm-mode-map (kbd (car key-pair))
    `(lambda () (interactive) (vterm-send-string (cdr ',key-pair)))))

;; 方法二
(dolist (key-pair '(("C-a" "\C-a") ("C-e" "\C-e") ("C-k" "\C-k") ("C-u" "\C-u")))
  (evil-define-key 'insert vterm-mode-map (kbd (car key-pair))
    `(lambda () (interactive) (vterm-send-string (cadr ',key-pair)))))

通过这个例子,进一步理解`(backquote)这个宏的作用了。

为了更好的理解backquote,想简化成(dolist (key '("a" "e" "k" "u")) body)的样子,可以吗?

(define-key vterm-mode-map [f1] nil))
(define-key vterm-mode-map [f2] nil))
...
(define-key vterm-mode-map [f12] nil))

简化成下面的样子

(dolist (key '([f1] [f2] [f3] [f4] [f5] [f6] [f7] [f8] [f9] [f10] [f11] [f12]))
     (define-key vterm-mode-map `,key nil))

上面的写法是正确的,进一步简化如下,但有问题,错在哪里?

(dotimes (n 12)
     (let ((key (concat "[f" (int-to-string (1+ n)) "]")))
          (define-key vterm-mode-map `,key nil)))

[f1]是个vector,不是字符串

你这个太逗了

是用来绑定F键

明天我改一下,把括号放在宏外面

谢谢 ~ 这回看懂了。 :grinning:

哈哈哈 就是啊 在忙着过年呢

不错, 这个教程很好.

这个教程。。。没读懂,感觉停晦涩的,错别字,还有语法问题,看着很别扭

毕竟是 08 年左右写成,最近更新在 13 年的文章了。