emacs 插件开发


;; global-set-key为函数 接收两个参数
;; 一个字符串 "\M-r" 代表Alt+r
;; 一个函数   前面用`符号表示该参数为函数

(global-set-key "\M-r" `indent-buffer)

;; 等价于
(global-set-key "\M-r" (quote indent-buffer))

string,Numbers, characters, vectors 求值是 self-evaluating , 不用加 quote


(defun other-window-backward ()
  ;; interactive 让该函数变为可交互式函数  ALT+x 会看到
  (interactive)
  (other-window -1))


(defun other-window-backward (n)
  ;; 有interactive "p" 这个标示的函数 n默认为1 指定次数调用的时候 n为指定的次数
  (interactive "p")
  (other-window (- n)))


(defun other-window-backward (&optional n)
  ;; &optional 代表参数可选
  (interactive "p")
  (if n
      (other-window (- n))
      (other-window (- 1))))


(defun other-window-backward (&optional n)
  ;; &optional 代表参数可选
  (interactive "p")
  (other-window (- (or n 1))))
;;(other-window (if n (- n ) -1)))


(defun other-window-backward (&optional n)
  ;; P 为大写
  (interactive "P")
  ;; prefix-numeric-value 为emacs内置函数 等价于以上
  (other-window (- (prefix-numeric-value n))))


(defalias `scroll-ahead `scroll-up)
(defalias `scroll-behind `scroll-down)
(defun scroll-n (&optional n)
  (interactive "P")
  (scroll-ahead (prefix-numeric-value n)))

hook本质上是一个变量,值为一个列表,列表中有一个或多个函数,这些函数会在某个指定的条件下依次执行


(defun read-only-if-symlink ()
  ;; file-symlink-p 函数返回一个文件是否为链接文件 p代表predicate(断言)
  ;; buffer-file-name 当前buffer的绝对路径
  (if (file-symlink-p buffer-file-name)
      ;; progn 顺序解析其后的所有表达式 最终值为最后一个表达式的值
      (progn
       (setq buffer-read-only t)
       (message "File is a symlink"))))

(add-hook `find-file-hooks `read-only-if-symlink)

;; (remove-hook `find-file-hooks `read-only-if-symlink)


;; 只是演示 在hook中不用使用lambda
(add-hook `find-file-hooks
          `(lambda ()
             (if (file-symlink-p buffer-file-name)
                 (progn
                  (setq buffer-read-only t)
                  (message "File is symlink")))))


;; 报错信息
(error "This is error")


;; 使用局部变量
(defun local-var (n)
  (let ((x 12))
    (* n x)))

(local-var 20)

命令配合

搜索和编辑buffer表

Minor Mode

调试

Major Mode

完整的例子