基本配置

您已经知道了,Emacs 的配置文件为 ~/.emacs 。配置文件中,以 ; 起始到行末的部分为注释。

让我们些简单配置一下:

例 25.1. emacs 配置 ~/.emacs

  1. ;;========================================
  2. ;;添加 Emacs 搜索目录 可以将自定的扩展放该目录
  3. ;;========================================
  4. ;(add-to-list 'load-path "~/..emacs")
  5. ;如果有其它配置文件,使此命令读取
  6. ;(load "addon.el")
  7. ;;========================================
  8. ;; 外观设置
  9. ;;========================================
  10. ;;禁用工具栏
  11. (tool-bar-mode nil)
  12. ;;禁用菜单栏,F10 开启关闭菜单
  13. (menu-bar-mode nil)
  14. ;;禁用滚动栏,用鼠标滚轮代替
  15. ;;(scroll-bar-mode nil)
  16. ;;禁用启动画面
  17. (setq inhibit-startup-message t)
  18. ;;========================================
  19. ;; 键绑定
  20. ;;========================================
  21. ;; C-t 设置标记 ;;
  22. (global-set-key (kbd "C-t") 'set-mark-command)
  23. ;; C-x b => CRM bufer list
  24. (global-set-key "\C-xb" 'electric-buffer-list)
  25. ;;---------- redo
  26. (global-set-key ( kbd "C-.") 'redo)
  27. ;;========================================
  28. ;;关闭当前缓冲区 Alt+4 ;; C-x 0
  29. (global-set-key (kbd "M-4") 'delete-window)
  30. ;;关闭其它缓冲区 Alt+1 ;; C-x 1
  31. (global-set-key (kbd "M-1") 'delete-other-windows)
  32. ;;水平分割缓冲区 Alt+2 ;; C-x 2
  33. (global-set-key (kbd "M-2") 'split-window-vertically)
  34. ;;垂直分割缓冲区 Alt+3 ;; C-x 3
  35. (global-set-key (kbd "M-3") 'split-window-horizontally)
  36. ;;切换到其它缓冲区 Alt+0 ;; C-x o
  37. (global-set-key (kbd "M-0") 'other-window)
  38. ;;F10 显示/隐藏菜单栏 ;; M-x menu-bar-open
  39. ;;(global-set-key (kbd "F10") 'menu-bar-mode)
  40. ;;WIN+s 进入 Shell ;; M-x shell
  41. ;(global-set-key (kbd "s-s") 'shell)
  42. ;;(define-key ctl-x-map "\M-s" 'shell)
  43. ;;========================================
  44. ;; 缓冲区
  45. ;;========================================
  46. ;;设定行距
  47. (setq default-line-spacing 0)
  48. ;;页宽
  49. (setq default-fill-column 90)
  50. ;;缺省模式 text-mode
  51. (setq default-major-mode 'text-mode)
  52. ;;设置删除纪录
  53. (setq kill-ring-max 200)
  54. ;;以空行结束
  55. (setq require-final-newline t)
  56. ;;语法加亮
  57. (global-font-lock-mode t)
  58. ;;高亮显示区域选择
  59. (transient-mark-mode t)
  60. ;;页面平滑滚动, scroll-margin 5 靠近屏幕边沿3行时开始滚动,可以很好的看到上下文。
  61. (setq scroll-margin 5
  62. scroll-conservatively 10000)
  63. ;高亮显示成对括号,但不来回弹跳
  64. (show-paren-mode t)
  65. (setq show-paren-style 'parentheses)
  66. ;;鼠标指针规避光标
  67. ;(mouse-avoidance-mode 'animate)
  68. ;;粘贴于光标处,而不是鼠标指针处
  69. (setq mouse-yank-at-point t)
  70. ;;========================================
  71. ;; 回显区
  72. ;;========================================
  73. ;;闪屏报警
  74. (setq visible-bell t)
  75. ;;使用 y or n 提问
  76. (fset 'yes-or-no-p 'y-or-n-p)
  77. ;;锁定行高
  78. (setq resize-mini-windows nil)
  79. ;;递归 minibuffer
  80. (setq enable-recursive-minibuffers t)
  81. ;; 当使用 M-x COMMAND 后,过 1 秒钟显示该 COMMAND 绑定的键。
  82. ;;(setq suggest-key-bindings 1) ;;
  83. ;;========================================
  84. ;; 状态栏
  85. ;;========================================
  86. ;;显示时间
  87. (display-time)
  88. ;;时间格式
  89. (setq display-time-24hr-format t)
  90. (setq display-time-day-and-date t)
  91. (setq display-time-interval 10)
  92. ;;显示列号
  93. (setq column-number-mode t)
  94. ;;标题栏显示 %f 缓冲区完整路径 %p 页面百分数 %l 行号
  95. (setq frame-title-format "%f")
  96. ;;========================================
  97. ;; 编辑器设定
  98. ;;========================================
  99. ;;不生成临时文件
  100. ;;(setq-default make-backup-files nil)
  101. ;;只渲染当前屏幕语法高亮,加快显示速度
  102. (setq font-lock-maximum-decoration t)
  103. ;;将错误信息显示在回显区
  104. ;(condition-case err
  105. ; (progn
  106. ; (require 'xxx) )
  107. ; (error
  108. ; (message "Can't load xxx-mode %s" (cdr err))))
  109. ;;使用X剪贴板
  110. (setq x-select-enable-clipboard t)
  111. ;;;;;;;; 使用空格缩进 ;;;;;;;;
  112. ;; indent-tabs-mode t 使用 TAB 作格式化字符 nil 使用空格作格式化字符
  113. (setq indent-tabs-mode nil)
  114. (setq tab-always-indent nil)
  115. (setq tab-width 4)
  116. ;;========================================
  117. ;; 颜色设置
  118. ;;========================================
  119. ;; 指针颜色
  120. (set-cursor-color "black")
  121. ;; 鼠标颜色
  122. (set-mouse-color "black")
  123. ;; 背景和字体颜色
  124. (set-foreground-color "gainsboro")
  125. (set-background-color "grey30")
  126. (set-border-color "black")
  127. ;; 语法高亮显示,区域选择,二次选择 ;;前景和背景色
  128. (set-face-foreground 'highlight "white")
  129. (set-face-background 'highlight "blue")
  130. (set-face-foreground 'region "cyan")
  131. (set-face-background 'region "blue")
  132. (set-face-foreground 'secondary-selection "skyblue")
  133. (set-face-background 'secondary-selection "darkblue")
  134. ;;日历配色
  135. ;(setq calendar-load-hook
  136. ;'(lambda ()
  137. ;(set-face-foreground 'diary-face "skyblue")
  138. ;(set-face-background 'holiday-face "slate blue")
  139. ;(set-face-foreground 'holiday-face "white")))
  140. ;;========================================
  141. ;; 字体设置
  142. ;;========================================
  143. (set-default-font "Monospace-10")
  144. (if window-system
  145. (set-fontset-font (frame-parameter nil 'font)
  146. 'unicode '("Microsoft YaHei" . "unicode-bmp"))
  147. )
  148. ;;========================================
  149. ;; 必备扩展
  150. ;;========================================
  151. ;;---------- color-theme
  152. ;(require 'color-theme)
  153. ;(color-theme-gray30)
  154. ;;========== ibuffer
  155. (require 'ibuffer)
  156. (global-set-key ( kbd "C-x C-b ")' ibuffer)
  157. ;;========== outline
  158. (setq outline-minor-mode-prefix [(control o)])
  159. ;;---------- Docbook
  160. ;(require 'docbook-xml-mode)
  161. ;(add-hook 'docbook-xml-mode-hook
  162. ; (function (lambda ()
  163. ; (setq outline-regexp "<!\\-\\-\\*+")
  164. ; (outline-minor-mode)
  165. ; (hide-body)
  166. ; )))
  167. ;;开启服务器模式
  168. ;(server-start)
  169. ;;org-mode
  170. (setq org-hide-leading-stars t)
  171. (define-key global-map "\C-ca" 'org-agenda)
  172. (setq org-log-done 'time)
  173. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  174. ;;;;;;;;;;; F5 运行当前文件 ;;;;;;;;;;
  175. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  176. ;(defun run-current-file ()
  177. ; "Execute or compile the current file.
  178. ;For example, if the current buffer is the file x.pl,
  179. ;then it'll call “perl x.pl” in a shell.
  180. ;The file can be php, perl, python, bash, java.
  181. ;File suffix is used to determine what program to run."
  182. ;(interactive)
  183. ; (let (ext-map file-name file-ext prog-name cmd-str)
  184. ;; get the file name
  185. ;; get the program name
  186. ;; run it
  187. ; (setq ext-map
  188. ; '(
  189. ; ("php" . "php")
  190. ; ("pl" . "perl")
  191. ; ("py" . "python")
  192. ; ("sh" . "bash")
  193. ; ("java" . "javac")
  194. ; )
  195. ; )
  196. ; (setq file-name (buffer-file-name))
  197. ; (setq file-ext (file-name-extension file-name))
  198. ; (setq prog-name (cdr (assoc file-ext ext-map)))
  199. ; (setq cmd-str (concat prog-name " " file-name))
  200. ;; (compile cmd-str)))
  201. ; (shell-command cmd-str)))
  202. ;
  203. ;(global-set-key (kbd "<f5>") 'run-current-file)
  204. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  205. ;;;;;;; 以下为实现 redo 的代码 ;;;;;;;
  206. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  207. (provide 'redo)
  208. (defvar redo-version "1.02"
  209. "Version number for the Redo package.")
  210. (defvar last-buffer-undo-list nil
  211. "The head of buffer-undo-list at the last time an undo or redo was done.")
  212. (make-variable-buffer-local 'last-buffer-undo-list)
  213. (make-variable-buffer-local 'pending-undo-list)
  214. ;; Emacs 20 variable
  215. (defvar undo-in-progress)
  216. (defun redo (&optional count)
  217. "Redo the the most recent undo.
  218. Prefix arg COUNT means redo the COUNT most recent undos.
  219. If you have modified the buffer since the last redo or undo,
  220. then you cannot redo any undos before then."
  221. (interactive "*p")
  222. (if (eq buffer-undo-list t)
  223. (error "No undo information in this buffer"))
  224. (if (eq last-buffer-undo-list nil)
  225. (error "No undos to redo"))
  226. (or (eq last-buffer-undo-list buffer-undo-list)
  227. ;; skip one undo boundary and all point setting commands up
  228. ;; until the next undo boundary and try again.
  229. (let ((p buffer-undo-list))
  230. (and (null (car-safe p)) (setq p (cdr-safe p)))
  231. (while (and p (integerp (car-safe p)))
  232. (setq p (cdr-safe p)))
  233. (eq last-buffer-undo-list p))
  234. (error "Buffer modified since last undo/redo, cannot redo"))
  235. (and (or (eq buffer-undo-list pending-undo-list)
  236. (eq (cdr buffer-undo-list) pending-undo-list))
  237. (error "No further undos to redo in this buffer"))
  238. (or (eq (selected-window) (minibuffer-window))
  239. (message "Redo..."))
  240. (let ((modified (buffer-modified-p))
  241. (undo-in-progress t)
  242. (recent-save (recent-auto-save-p))
  243. (old-undo-list buffer-undo-list)
  244. (p (cdr buffer-undo-list))
  245. (records-between 0))
  246. ;; count the number of undo records between the head of the
  247. ;; undo chain and the pointer to the next change. Note that
  248. ;; by `record' we mean clumps of change records, not the
  249. ;; boundary records. The number of records will always be a
  250. ;; multiple of 2, because an undo moves the pending pointer
  251. ;; forward one record and prepend a record to the head of the
  252. ;; chain. Thus the separation always increases by two. When
  253. ;; we decrease it we will decrease it by a multiple of 2
  254. ;; also.
  255. (while p
  256. (cond ((eq p pending-undo-list)
  257. (setq p nil))
  258. ((null (car p))
  259. (setq records-between (1+ records-between))
  260. (setq p (cdr p)))
  261. (t
  262. (setq p (cdr p)))))
  263. ;; we're off by one if pending pointer is nil, because there
  264. ;; was no boundary record in front of it to count.
  265. (and (null pending-undo-list)
  266. (setq records-between (1+ records-between)))
  267. ;; don't allow the user to redo more undos than exist.
  268. ;; only half the records between the list head and the pending
  269. ;; pointer are undos that are a part of this command chain.
  270. (setq count (min (/ records-between 2) count)
  271. p (primitive-undo (1+ count) buffer-undo-list))
  272. (if (eq p old-undo-list)
  273. nil ;; nothing happened
  274. ;; set buffer-undo-list to the new undo list. if has been
  275. ;; shortened by `count' records.
  276. (setq buffer-undo-list p)
  277. ;; primitive-undo returns a list without a leading undo
  278. ;; boundary. add one.
  279. (undo-boundary)
  280. ;; now move the pending pointer backward in the undo list
  281. ;; to reflect the redo. sure would be nice if this list
  282. ;; were doubly linked, but no... so we have to run down the
  283. ;; list from the head and stop at the right place.
  284. (let ((n (- records-between count)))
  285. (setq p (cdr old-undo-list))
  286. (while (and p (> n 0))
  287. (if (null (car p))
  288. (setq n (1- n)))
  289. (setq p (cdr p)))
  290. (setq pending-undo-list p)))
  291. (and modified (not (buffer-modified-p))
  292. (delete-auto-save-file-if-necessary recent-save))
  293. (or (eq (selected-window) (minibuffer-window))
  294. (message "Redo!"))
  295. (setq last-buffer-undo-list buffer-undo-list)))
  296. (defun undo (&optional arg)
  297. "Undo some previous changes.
  298. Repeat this command to undo more changes.
  299. A numeric argument serves as a repeat count."
  300. (interactive "*p")
  301. (let ((modified (buffer-modified-p))
  302. (recent-save (recent-auto-save-p)))
  303. (or (eq (selected-window) (minibuffer-window))
  304. (message "Undo..."))
  305. (or (eq last-buffer-undo-list buffer-undo-list)
  306. ;; skip one undo boundary and all point setting commands up
  307. ;; until the next undo boundary and try again.
  308. (let ((p buffer-undo-list))
  309. (and (null (car-safe p)) (setq p (cdr-safe p)))
  310. (while (and p (integerp (car-safe p)))
  311. (setq p (cdr-safe p)))
  312. (eq last-buffer-undo-list p))
  313. (progn (undo-start)
  314. (undo-more 1)))
  315. (undo-more (or arg 1))
  316. ;; Don't specify a position in the undo record for the undo command.
  317. ;; Instead, undoing this should move point to where the change is.
  318. ;;
  319. ;;;; The old code for this was mad! It deleted all set-point
  320. ;;;; references to the position from the whole undo list,
  321. ;;;; instead of just the cells from the beginning to the next
  322. ;;;; undo boundary. This does what I think the other code
  323. ;;;; meant to do.
  324. (let ((list buffer-undo-list)
  325. (prev nil))
  326. (while (and list (not (null (car list))))
  327. (if (integerp (car list))
  328. (if prev
  329. (setcdr prev (cdr list))
  330. ;; impossible now, but maybe not in the future
  331. (setq buffer-undo-list (cdr list))))
  332. (setq prev list
  333. list (cdr list))))
  334. (and modified (not (buffer-modified-p))
  335. (delete-auto-save-file-if-necessary recent-save)))
  336. (or (eq (selected-window) (minibuffer-window))
  337. (message "Undo!"))
  338. (setq last-buffer-undo-list buffer-undo-list))