;+=======================================================================+ ;| 单行代码 | ;+=======================================================================+ (setq user-full-name "xiebiao") ;; 设置用户名 (setq user-mail-address "1434513612@qq.com") ;; 设置用户邮箱 (setq frame-title-mode t) ;; 在窗口的标题栏上显示文件名称 (setq frame-title-format "%b@xiebiao");; 设置标题栏 (menu-bar-mode -1);; 隐藏菜单栏 (setq-default cursor-type ‘bar);; 设置光标为线状 (blink-cursor-mode 0);; 禁用光标闪烁 (tool-bar-mode -1);; 隐藏工具栏 (scroll-bar-mode -1);; 滚动栏隐藏 (add-hook ‘org-mode-hook (lambda () (setq truncate-lines nil)));; 自动换行 (setq inhibit-splash-screen t);; 关闭启动画面 (global-linum-mode -1);; 关闭行号 (setq make-backup-files nil);; 关闭自动备份文件~ (setq auto-save-mode nil);; 自动保存模式 ;(setq backup-by-copying t);; 备份设置方法,直接拷贝 (setq truncate-lines nil) ;; 自动换行 (setq org-src-fontify-natively t);; 设置代码高亮 (setq global-font-lock-mode t);; 进行语法加亮 ;;(set-frame-parameter (selected-frame) ‘alpha (list 85 50));; 设置屏幕透明 (tool-bar-mode -1);; 关闭工具栏,tool-bar-mode 即为一个 Minor Mode (scroll-bar-mode -1);; 关闭文件滑动控件 (global-set-key (kbd "RET") ‘newline-and-indent);; 换行 (setq default-cursor-type ‘bar);; 设置光标为条状 ;(setq c-basic-offset 2);; 设置tab为两个空格的宽度 (setq initial-frame-alist (quote ((fullscreen . maximized))));;默认全屏 ;;(set-frame-width (selected-frame) 77);;设置初始窗口宽度 ;;(set-frame-height (selected-frame) 34);;设置初始窗口高度 (add-hook ‘emacs-lisp-mode-hook ‘show-paren-mode);; 启用自动括号匹配? (show-paren-mode t);; ;当指针到一个括号时,自动显示所匹配的另一个括号 (electric-pair-mode t) ;;自动补全括号 (setq show-paren-style ‘parenthesis);只高亮括号 ;(setq show-paren-style ‘expression) ; 高亮整个括号内的内容 (global-hl-line-mode 1);; 高亮当前行 (setq mouse-yank-at-point 1);; 鼠标中键粘贴 ;(mouse-avoidance-mode ‘animate);; 光标靠近鼠标指针时,让鼠标指针自动让开,别挡住视线。 (setq require-final-newline t);; 自动的在文件末增加一新行 (setq track-eol t) ;; 当光标在行尾上下移动的时候,始终保持在行尾。 (global-auto-revert-mode 1);; 自动加载外部修改过的文件 (setq c-default-style "awk");; 设置c++编译颜色风格 (delete-selection-mode 1);; 将删除功能配置成与其他图形界面的编辑器相同,当你选中一段文字 之后输入一个字符会替换掉你选中部分的文字。 (fset ‘yes-or-no-p ‘y-or-n-p);;简化yes no (setq comint-scroll-to-bottom-on-input nil) (setq visible-bell t);;关闭提示音 ;(setq default-major-mode ‘text-mode);;打开就启用 text 模式 ;+=======================================================================+ ;| 子龙山人 | ;+=======================================================================+ (require ‘package) (package-initialize); 高耗时操作 (setq package-archives ‘(("gnu" . "http://elpa.emacs-china.org/gnu/") ("melpa" . "http://elpa.emacs-china.org/melpa/"))) ;; 注意 elpa.emacs-china.org 是 Emacs China 中文社区在国内搭建的一个 ELPA 镜像 ;; cl - Common Lisp Extension (require ‘cl) ;; Add Packages (defvar my/packages ‘( yasnippet ;;自定义补全 monokai-theme ;;Themes org-bullets ;;org-mode列表字符 htmlize ;;导出html ox-reveal ;;幻灯片 company cdlatex auctex ;ycmd ;company-ycmd ;flycheck-ycmd flycheck ;;语言检查 py-autopep8 elpy better-defaults ) "Default packages") (setq package-selected-packages my/packages) (defun my/packages-installed-p () (loop for pkg in my/packages when (not (package-installed-p pkg)) do (return nil) finally (return t))) (unless (my/packages-installed-p) (message "%s" "Refreshing package database...") (package-refresh-contents) (dolist (pkg my/packages) (when (not (package-installed-p pkg)) (package-install pkg)))) (add-to-list ‘my/packages ‘monokai-theme);; 安装主题 (load-theme ‘monokai 1);; 每次打开编辑器时加载主题 ;+=======================================================================+ ;| 字体代码 | ;+=======================================================================+ (set-default-font "Source Code Pro 11") ;(set-default-font "Courier New 11") (set-fontset-font "fontset-default" ‘unicode "仿宋 11") ;; for windows ;;org-table字体 (custom-set-faces ‘(org-table ((t (:foreground "#6c71c4" :family "Ubuntu Mono"))))) ;+=======================================================================+ ;| orgmode | ;+=======================================================================+ ;; GTD : 收集-处理-管理-执行-回顾-反省 (require ‘org) ;; 设置todo关键词 (setq org-todo-keywords ‘((sequence "TODO(t!)" "DOING(i!)" "NEXT(n!)" "|" "DONE(d@/!)" "ABORT(a@/!)"))) ;; 设置todo颜色 (setq org-todo-keyword-faces ‘(("TODO" . (:foreground "red" :weight bold)) ("DOING" . (:foreground "yellow" :weight bold)) ("NEXT" . (:foreground "orange" :weight bold)) ("DONE" . (:foreground "green" :weight bold)) ;;完成 ("ABORT" . (:foreground "ForestGreen"));;异常终止 ));red ForestGreen Goldenrod OrangeRed Brown green orange magenta ;yellow gray white DarkOrange DodgerBlue SkyBlue ;; 优先级范围和默认任务的优先级 (setq org-highest-priority ?A) (setq org-lowest-priority ?E) (setq org-default-priority ?B) ;; 优先级醒目外观 (setq org-priority-faces ‘((?A . (:foreground "DarkOrange" :weight bold)) (?B . (:foreground "yellow" :weight bold)) (?C . (:foreground "DarkGreen" :weight bold)) (?D . (:foreground "SkyBlue" :weight bold)) (?E . (:foreground "black" :weight bold)) ));;(?B . (:background "DarkOrange" :foreground "white" :weight bold)) ;; 设置标签 (setq org-tag-alist ‘(("read" . ?r) ("work" . ?w) ("study" . ?s) ("program" . ?p) ("cpp" . ?c) ("mind" . ?m) ("idea" . ?i) ("leetcode". ?l))) (require ‘org) ;(setq org-src-fontify-natively t);; 添加 Org-mode 文本内语法高亮 ;(setq default-major-mode ‘org-mode) ;;设置初始目录为project项目目录 (setq default-directory "~/.emacs.d/document/Orgmode") ;; 设置 org-agenda 打开快捷 (global-set-key (kbd "C-c a") ‘org-agenda) ;; 设置默认的笔记 ;(setq org-default-notes-file "~/.emacs.d/document/Inboxs/Inbox.org") ;; orgmode子弹符号 (add-hook ‘org-mode-hook (lambda () (org-bullets-mode 1)));;需要安装org-bullets包 ;; 设置默认 Org Agenda 文件目 方法一 ;(defvar org-agenda-dir "" "gtd org files location") ;(setq-default org-agenda-dir "/Path/To/Your/GTD/File/") ;(setq org-agenda-file-note (expand-file-name "notes.org" org-agenda-dir)) ;(setq org-agenda-file-task (expand-file-name "task.org" org-agenda-dir)) ;(setq org-agenda-file-calendar (expand-file-name "calendar.org" org-agenda-dir)) ;(setq org-agenda-file-finished (expand-file-name "finished.org" org-agenda-dir)) ;(setq org-agenda-file-canceled (expand-file-name "canceled.org" org-agenda-dir)) ;; 设置默认 Org Agenda 文件目 方法二 (setq org-agenda-files ‘("~/.emacs.d/document/GTD/Task.org" "~/.emacs.d/document/GTD/Plan.org" "~/.emacs.d/document/GTD/Todo.org")) ;+=======================================================================+ ;| org中插入截图 | ;+=======================================================================+ ;; 让图片不要真实大小显示 (setq org-image-actual-width nil) ;; 截图函数 (defun my-org-screenshot () "Take a screenshot into a time stamped unique-named file in the same directory as the org-buffer and insert a link to this file." (interactive) (setq filename (concat (make-temp-name (concat (buffer-file-name) "_" (format-time-string "%Y%m%d_%H%M%S_")) ) ".png")) (shell-command "snippingtool /clip") (shell-command (concat "powershell -command \"Add-Type -AssemblyName System.Windows.Forms; if ($([System.Windows.Forms.Clipboard]::ContainsImage())) {$image = [System.Windows.Forms.Clipboard]::GetImage(); [System.Drawing.Bitmap]$image.Save(‘" filename "‘,[System.Drawing.Imaging.ImageFormat]::Png); Write-Output ‘clipboard content saved as file‘} else { Write-Output ‘clipboard does not contain image data‘}\"")) (insert (concat "[[file:" filename "]]")) (org-display-inline-images)) ;; 绑定快捷键C-x C-a (define-key org-mode-map (kbd "C-x C-a") ‘my-org-screenshot) ;+=======================================================================+ ;| capture | ;+=======================================================================+ (global-set-key (kbd "C-c c") ‘org-capture);;打开capture的快捷键 ;; 设置capture模板 (setq org-capture-templates ‘( ;; agenda ;("a" "Agenda" table-line ; (file "~/.emacs.d/document/Orgmode/Agenda.org") ; "| %^{项目} | %^{姓名} | %^{手机号}| %U |")) ;; 插入链接 ("l" "Links " entry (file+headline "~/.emacs.d/document/Inboxs/Link.org" "Links") "* %^L" :prepend t) ;; todo 项目 ("t" "ToDo" entry (file+datetree "~/.emacs.d/document/GTD/Todo.org") "* TODO %? \n 时间:%T" :prepend t) ;; inbox 快速记录未归类的文本 ("i" "Inbox" entry (file+headline "~/.emacs.d/document/Inboxs/Inbox.org" "Inbox") "* %U - %^{heading} %^g \n %i \n") ;; 日志记录 ("j" "Journal" entry (file+datetree "~/.emacs.d/document/Inboxs/Journal.org") "* %U - %^{heading} \n %^g \n %? \n %i \n") ;; 笔记 ("n" "Note" entry (file+headline "~/.emacs.d/document/Notes/Note1.org" "Notes") "* %^{heading} \n %^g \n %? \n %i \n 时间:%U") ;; 博客 ("b" "Blog" entry (file+headline "~/.emacs.d/document/Inboxs/Blog.org" "Blog Topics") "* %^{博客名} \n %^g \n %? \n 时间:%U" :prepend t :clock-in t :clock-resume t) ;; 读书记录 ("r" "Book" entry (file+headline "~/.emacs.d/document/Inboxs/Book.org" "Book") "* %^{书名} \n 时间:%U \n %? \n %i \n":clock-in t :clock-resume t) ;; 工作情况 ("w" "Work" entry (file+headline "~/.emacs.d/document/Inboxs/Work.org" "Work") "* TODO [#B] %^{任务名} \n 时间:%U \n":clock-in t :clock-resume t) )) ;+=======================================================================+ ;| C++配置 | ;+=======================================================================+ (global-set-key [f9] ‘compile-file) (global-set-key [f12] ‘super-compile-file) (global-set-key [f10] ‘gud-gdb) (setq tap-width 6) (setq-default c-basic-offset 6) (setq scroll-step 1 scroll-margin 5 scroll-conservatively 10000 ) (defun compile-file () (interactive) (compile (format "g++ -o %s %s -lm -Wall -g -std=c++11 -O2 && t.exe" (file-name-sans-extension (buffer-name))(buffer-name) (file-name-sans-extension (buffer-name)))) ) (defun super-compile-file () (interactive) (compile (format "g++ -o %s %s -lm -Wall -g -std=c++11 -Ofast" (file-name-sans-extension (buffer-name))(buffer-name) (file-name-sans-extension (buffer-name)))) ) (require‘cc-mode);; 加载c模块 (defun my-g++-compile-and-run () (interactive) (compile (format "g++ %s && t.exe" (buffer-file-name)) ) (define-key c++-mode-map (kbd "<f8>") #‘my-g++-compile-and-run) ;+=======================================================================+ ;| company | ;+=======================================================================+ (require ‘company) (add-hook ‘after-init-hook ‘global-company-mode) ;; 全局开启 (global-company-mode t) (setq company-idle-delay 0.1 ;菜单延迟 company-minimum-prefix-length 2 ;开始补全字数 company-require-match nil company-dabbrev-ignore-case nil company-dabbrev-downcase nil;识别大小写 company-show-numbers t ;显示序号 company-transformers ‘(company-sort-by-backend-importance) company-continue-commands ‘(not helm-dabbrev)) ;; 补全快捷键 (global-set-key (kbd "<C-tab>") ‘company-complete) (global-set-key (kbd "RET") ‘newline-and-indent) ;; 补全菜单选项快捷键 (define-key company-active-map (kbd "C-n") ‘company-select-next) (define-key company-active-map (kbd "C-p") ‘company-select-previous) ;; Company 有时候补全功能并不是非常理想,这时就可以使用 Hippie Expand 来完成补全 (setq hippie-expand-try-function-list ‘(try-expand-debbrev try-expand-debbrev-all-buffers try-expand-debbrev-from-kill try-complete-file-name-partially try-complete-file-name try-expand-all-abbrevs try-expand-list try-expand-line try-complete-lisp-symbol-partially try-complete-lisp-symbol)) (global-set-key (kbd "s-/") ‘hippie-expand) ;+=======================================================================+ ;| Latex | ;+=======================================================================+ ;; cdlatex 模块 (require ‘cdlatex) (add-hook ‘LaTeX-mode-hook ‘turn-on-cdlatex) (add-hook ‘LaTex-mode-hook ‘turn-on-reftex) ;; outline 设置 (outline-minor-mode t) (setq outline-minor-mode-prefix [(control o)]) (add-hook ‘LaTex-mode-hook ‘outline-minor-mode) (add-hook ‘LaTex-mode-hook ‘outline-hide-body) ;;outline 好像没有效果 ;; 默认编辑器设置 ;(setq TeX-command-default "XeLaTeX") ;(setq TeX-save-query nil ) ;(setq TeX-show-compilation t);;可用代码 ;;----------- 新加的代码,如果有影响可以删除---; (load "auctex.el" nil t t) (setq TeX-output-view-style (quote (("^pdf$" "." "evince %o %(outpage)")))) ;(add-hook ‘LaTeX-mode-hook ; (lambda() ; (add-to-list ‘TeX-command-list ‘("XeLaTeX" "%`xelatex%(mode)%‘ %t" TeX-run-TeX nil t)) ; (setq TeX-command-default "XeLaTeX"))) (setq TeX-auto-save t) (setq TeX-parse-self t) (setq-default TeX-master nil) (setq preview-scale-function 1.3) (setq LaTeX-math-menu-unicode t) (setq TeX-insert-braces nil) (add-hook ‘LaTeX-mode-hook ‘LaTeX-math-mode) ;;Auctex for Latex (add-hook ‘LaTeX-mode-hook ‘LaTeX-install-toolbar) (add-hook ‘LaTeX-mode-hook ‘turn-on-auto-fill) (add-hook ‘LaTeX-mode-hook ‘turn-on-reftex) (add-hook ‘LaTeX-mode-hook (lambda() ;;调用latex命令 (add-to-list ‘TeX-command-list (list "LaTeX" "latex %s.tex" ‘TeX-run-command nil t)) ;;调用dvipdfmx命令 (add-to-list ‘TeX-command-list (list "dvi2pdf" "dvipdfmx %s.dvi" ‘TeX-run-command nil t)) ;;调用pdflatex命令;;调用pdflatex命令;;调用pdflatex命令 (add-to-list ‘TeX-command-list (list "pdf Latex" "pdflatex %s.tex" ‘TeX-run-command nil t)) ; View PDF (add-to-list ‘TeX-command-list (list "View pdf" "start .\\\"%s.pdf\""‘TeX-run-command nil t)) ;;设置环境为xelatex (add-to-list ‘TeX-command-list ‘("XeLaTeX" "%`xelatex%(mode)%‘ %t" TeX-run-TeX nil t)) ;;(setq TeX-command-default "XeLaTeX") ;;(setq TeX-save-query nil ) ;;(setq TeX-show-compilation t) )) ;;======================== latex-math-mode ================= ;; latex-math-mode (add math symbols to menu and shortcuts) ;; So that you can use, e.g., `a, to input \alpha. You may also define ;; your own customized shortcuts by setting the variable ;; LaTeX-math-list before loading LaTeX-math-mode. ;; to enable maths support (setq LaTeX-math-mode t) (setq TeX-electric-escape t) ;; you may like it or dislike it. I found it useful when inputting ;repeated commands -- you just need to type \ and ENTER to repeat the last latex command. (add-hook ‘LaTeX-mode-hook ‘latex-math-mode) ; with AUCTeX LaTeX mode (add-hook ‘latex-mode-hook ‘latex-math-mode) ; with Emacs latex mode (add-hook ‘LaTeX-mode-hook ‘LaTeX-math-mode) ;; ===================在.org文档中使用latex=========== (require ‘ox-latex) (setq org-latex-create-formula-image-program ‘dvipng) (org-babel-do-load-languages ‘org-babel-load-languages ‘( (R . t) (dot . t) (haskell . t) (python . t) (C . t) (emacs-lisp . nil) (latex . t) )) ;+=======================================================================+ ;| Python | ;+=======================================================================+ ;; python (setq python-indent-offset 4) (when (require ‘elpy nil t) (elpy-enable) (setq compile-command (concat "python3" (buffer-file-name))) (add-hook ‘python-mode-hook ‘elpy-mode)) (when (require ‘flycheck nil t) (add-hook ‘elpy-mode-hook ‘flycheck-mode)) ;; 开启cuakeys模式,可以像win10一样c-ac-zc-v复制粘贴操作 ;在设置中:options-Use CUA keys 勾选即可。 ;; 在python模式中自动启用 ;(add-hook ‘python-mode-hook ‘anaconda-mode) ;;(use-package python-mode ;; :mode (("SConstruct\\‘" . python-mode) ;; ("SConscript\\‘" . python-mode) ;; ("\\.py\\‘" . python-mode)) ;; :config ;; (use-package elpy ;; :init ;; (elpy-enable) ;; (elpy-use-ipython) ;; use flycheck not flymake with elpy ;; (when (require ‘flycheck nil t) ;; (setq elpy-modules (delq ‘elpy-module-flymake elpy-modules)) ;; (add-hook ‘elpy-mode-hook ‘flycheck-mode)))) ;; ----autopep8---- 按C-x C-s 自动格式化代码,无效 (require ‘py-autopep8) (add-hook ‘elpy-mode-hook ‘py-autopep8-enable-on-save) ;; ----elpy---- 支持缩进和语法高亮功能 (require ‘elpy) ;;(setq elpy-rpc-python-command "python3") ;;(setq python-shell-interpreter "python3") ;;(setq pyvenv-virtualenvwrapper-python "/usr/bin/python3") (elpy-enable) ;;(elpy-use-ipython) ;; 将自动补全设置成jedi ;;(setq elpy-rpc-backend "jedi") ;+=======================================================================+ ;| 其他小模块 | ;+=======================================================================+ ;; 记住光标上次打开文件时的位置 (setq-default save-place t) (require ‘saveplace) (save-place-mode 1) ;; 时间显示 ;(display-time-mode 1) ;; 常显 ;(setq display-time-24hr-format t) ;;格式 ;(setq display-time-day-and-date t) ;;显示时间、星期、日期 ;;设置粘贴缓冲条目数量.用一个kill ring(最多的记录个数). 这样防止不小心删掉重要的东西 (setq kill-ring-max 200) ;; 防止页面滚动时跳动scroll-margin 3可以在靠近屏幕边沿3行时就开始滚动,可以很好的看到上下文。 (setq scroll-step 1 scroll-margin 3;;可以在靠近屏幕边沿3行时就开始滚动 scroll-conservatively 10000);;设置为每次翻滚一行,可以使页面更连续 ;;鼠标滚轮,默认的滚动太快,这里改为3行 (defun up-slightly () (interactive) (scroll-up 1)) (defun down-slightly () (interactive) (scroll-down 1)) (global-set-key [mouse-4] ‘down-slightly) (global-set-key [mouse-5] ‘up-slightly) ;;设置最近访问文件可见 (require ‘recentf) (recentf-mode t) ;; 设置最近访问文件数量为10 (setq recentf-max-menu-items 10) ;; 打开最近文件快捷键,这个快捷键绑定可以用之后的插件 counsel 代替 (global-set-key (kbd "C-x C-r") ‘recentf-open-files) ;; 设定删除保存记录为200,可以方便以后无限恢复 (setq kill-ring-max 200) ;; 窗口管理:将当前窗口分割为上下两个,并切换到另外一个 buffer (defun split-window-new-buffer () (interactive) (split-window-below) (call-interactively ‘switch-to-buffer)) ;; 代码缩进,一次重新缩进全部缓冲区的代码 (defun indent-buffer() (interactive) (indent-region (point-min) (point-max))) (defun indent-region-or-buffer() (interactive) (save-excursion (if (region-active-p) (progn (indent-region (region-beginning) (region-end)) (message "Indent selected region.")) (progn (indent-buffer) (message "Indent buffer."))))) (global-set-key (kbd "C-M-\\") ‘indent-region-or-buffer) ;; 幻灯片模块oxreveal (require ‘ox-reveal) ;; yasnippet自定义补全 (require ‘yasnippet) (yas-global-mode 1) ;; 调整背景色 ;(set-background-color "grey15");; 背景颜色========== ;(set-foreground-color "grey");;颜色 ;; f4重置buffer ;(global-set-key (kbd "<f4>") ‘revert-buffer) ;; 窗口光标后退,C-x o的相反操作 (defun prev-window () (interactive) (other-window -1)) (global-set-key (kbd "C-x p") ‘prev-window) ;;----------加载延迟模块start------- (require ‘dired-x) ;; 使得 C-x C-j 就可以进入当前文件夹的所在路径 ;; 递归删除和复制目录 (setq dired-recursive-deletes ‘always) (setq dired-recursive-copies ‘always) ;; 让 Emacs 重用唯一的一个缓冲区作为 Dired Mode 显示专用缓冲区 (put ‘dired-find-alternate-file ‘disabled nil) ;; 主动加载 Dired Mode ;; (require ‘dired) ;; (defined-key dired-mode-map (kbd "RET") ‘dired-find-alternate-file) ;; 延迟加载 (with-eval-after-load ‘dired (define-key dired-mode-map (kbd "RET") ‘dired-find-alternate-file)) ;;----------加载延迟模块end------- (setq transient-mark-mode t);; 高亮显示选择区域 (auto-image-file-mode t);; 打开图片显示功能 (global-set-key [f5] ‘gdb);gbd F5调试程序 ;; 单词补全 ;(add-to-list ‘load-path "~/.emacs.d/elpa/company-english-helper") ;(require ‘company-english-helper);可以用 ;M-x toggle-company-english-helper 激活 ; 设置打开init.el文件的快捷键 (defun open-my-init-file() (interactive) (find-file "~/.emacs.d/init.el")) (global-set-key (kbd "<f7>") ‘open-my-init-file); 设置init.el文件的快捷键 ;; 撤消 (global-set-key (kbd "C-z") ‘undo) ;; 全选ctrl+a (global-set-key (kbd "C-q") ‘mark-whole-buffer) ;; 自定义缩写补全表 (setq-default abbrev-mode t) (define-abbrev-table ‘global-abbrev-table ‘ ( ;; Shifu ("8zl" "zilongshanren") ;; Tudi ("8lxy" "lixinyang") ("yasn" "yas-new-snippet") )) ;; -------------快速打开自定义的按键说明文件 (defun open-key-info-file1 () (interactive) (split-window-horizontally) (find-file-other-window "~/.emacs.d/document/Inboxs/Link.org") ;;(outline-mode) (hide-body)) (global-set-key (kbd "C-x C-1") ‘open-key-info-file1) (defun open-key-info-file2 () (interactive) (split-window-horizontally) (find-file-other-window "~/.emacs.d/document/Orgmode/emacsKeys.org") ;;(outline-mode) (hide-body)) (global-set-key (kbd "C-x C-2") ‘open-key-info-file2) ;; grammal check: flycheck (add-hook ‘after-init-hook #‘global-flycheck-mode);global enable ; close flymake, start flycheck (when (require ‘flycheck nil t) (setq elpy-modules(delq ‘elpy-module-flymake elpy-modules)) (add-hook ‘elpy-mode-hook ‘flycheck-mode)) ;; flycheck全局可用 (package-install ‘flycheck) (global-flycheck-mode) ;+=======================================================================+ ;| 自动保存 | ;+=======================================================================+ ;; defgroup 关键字的意思是定义一个工作组,执行 Alt + x customize-group 命令的时候可以进行图形化的模块配置 ;; 第一个参数是模块的名字, 比如 auto-save ;; 第二个参数是模块默认开启的状态, 在 elisp 中, t 表示 true, nil 表示 false ;; 第三个参数是对模块的文本解释 ;; 第四个参数表示对外提供 auto-save 这个 group (defgroup auto-save nil "Auto save file when emacs idle." :group ‘auto-save) ;; defcustom 关键字的意思是定义一个可以被用户自定义的变量, 当用户执行 Alt + x customize-variable ;的时候就可以补全 auto-save-idle 这个变量, defcustom 和 defvar 的区别主要是 defcustom 用于提供一 ;些参数让用户可以在 Emacs 中图形化定制变量内容, defvar 这只有变量名和 List 内容, 一般用于函数内部变 ;量值存储用, 不对外抛出给用户定制 ;; 第一个参数是变量的名字 autos-ave-idle ;; 第二个参数是变量的值, 这里我们定义为 1s, 表示自动保存的延迟秒数 ;; 第三个参数是变量的解释, 一般在 Alt + x describe-variable 的时候就会显示具体变量的文档描述 ;; 第四个参数用于定义变量的类型, 这里定义为整形, 这样在 customize-group 的时候只有输入整型才是正确保存 ;; 第五个参数表示这个变量属于 auto-save 这个组, 主要作用就是 customize-group 的时候能够在一个界面中 ;设置同一组的所有变量 (defcustom auto-save-idle 1 "The idle seconds to auto save file." :type ‘integer :group ‘auto-save) ;; autos-save-slient 的作用就是一个boolean值得变量, 设置为 nil 的时候, 表示每次自动保存都会在 ;minibuffer 提示, 设置成 t 的时候就会 shutup, 让我安安静静写会代码, 别闹... (defcustom auto-save-slient nil "Nothing to dirty minibuffer if this option is non-nil." :type ‘boolean :group ‘auto-save) ;; 这段代码的作用就是避免 Emacs 在保存文件的时候生成一大堆垃圾的 #foo# 文件, 这种文件最讨厌了, ;不但什么用都没有, 反而污染代码目录, 删除都删的我手酸 ;; 想当年为了找到关闭这个脑残功能的变量, 我把 emacs 几百个带有 save 的变量全部打出来, 一个一 ;个变量的试才找到你啊 (可惜当年我英文不好, 不知道怎么描述我想要的效果) (setq auto-save-default nil) ;; 前方高能核心代码, 请集中注意力 (defun auto-save-buffers () ;; 所有你在 Alt + x 以后可以调用的函数都要手动加上 (interactive) , 否则这段代码只能在 Elisp ;解释器中执行, 但是不能直接被用户从 Alt + x 调用, 就想 interactive 这个单词的意思一样 (interactive) ;; 创建 autosave-buffer-list 这个变量, 用于保存所有需要遍历的 buffer 列表 (let ((autosave-buffer-list)) ;; save-excursion 这个关键字的意思是, 所有在 save-excursion 里面的代码不管怎么折腾都不会对 ;save-excursion 之前的Emacs状态进行任何改变, 你可以理解为这个关键字的意思就是用于保护现场用的 ;) (save-excursion ;; dolist 的作用就和很多语言的 foreach 一个意思, 把 buffer-list 这个函数返回的所有 ;buffer 在循环内赋值给 buf 这个变量, 并在 dolist 的作用域中执行对 buf 影响的代码 (dolist (buf (buffer-list)) ;; 设置当前代码的 buffer 为 buf 变量值, 如果没有前面 save-excursion, 你会发现emacs ;会一直在快速的切换所有 buffer 的过程 (set-buffer buf) ;; 如果当前 buffer 有一个相关联文件 (buffer-file-name), 同时当前 buffer 已经被用户修改 ;了 (buffer-modified-p) 的情况下就执行自动保存 (if (and (buffer-file-name) (buffer-modified-p)) (progn ;; 把当前 buffer 的名字压进 autosave-buffer-list 列表, 用于后面的保存提示 (push (buffer-name) autosave-buffer-list) (if auto-save-slient ;; 如果 auto-save-slient 这个变量为 true, 就不显示任何保存信息, 因为 Emacs ;的保存函数 (basic-save-buffer) 本身机会 blabla 的告诉你文件已经保存了, 所以我 ;们用 with-temp-message 配合空字符串来禁止 with-temp-message 里面的代码在 ;minibuffer 显示任何内容 (with-temp-message "" (basic-save-buffer)) (basic-save-buffer)) ))) ;; unless 的意思是除非 auto-save-slient 为 false 就执行 (unless auto-save-slient ;; cond 就是 elisp 版的 switch, 用于条件语句对比执行 (cond ;; 如果 autosave-buffer-list 列表里面没有任何一个文件需要保存, 我们就不要去烦用户了 ;, 默默打酱油路过就好了 pp ;; 如果有一个文件需要保存, 我们就说 Saved ... ((= (length autosave-buffer-list) 1) (message "# Saved %s" (car autosave-buffer-list))) ;; 如果有多个文件需要保存, 就说 Saved ... files ((> (length autosave-buffer-list) 1) (message "# Saved %d files: %s" (length autosave-buffer-list) (mapconcat ‘identity autosave-buffer-list ", "))))) ))) (defun auto-save-enable () (interactive) ;; run-with-idle-timer 函数的意思就是在 auto-save-idle 定义的描述以后自动执行 ;auto-save-buffers 函数 ;; #‘ 的意思就是在 runtime 执行的时候再展开 auto-save-buffers 函数 (run-with-idle-timer auto-save-idle t #‘auto-save-buffers) ) (auto-save-enable);; 开启自动保存功能 (setq auto-save-slient t);; 自动保存的时候静悄悄的, 不要打扰我 ;;------------------------------代码测试部分-------------------------------