当前位置: 首页 > news >正文

Emacs定制:编译

本来自己的Emacs配置文件,是直接克隆的purcell的,然后又加入了一些自己的设定,结果导致现在的配置文件越来越臃肿,而且很多配置项发生了冲突,很多自己不需要的功能的配置,开始占用自己的精力。

所以,决定重新配置自己的Emacs。

从零开始,一项一项梳理,只保留自己需要的,自己用不到的一个也不加。

本文先整理出来编译代码相关的部分。大量来自purcell或者其它网友的配置,少量自己的定义。

编译之前

  • 自定义编译命令,把编译窗口减小一点。同时,编译完成还回到原来窗口。
(defun my-compile ()"Run compile and resize the compile window."(interactive)(save-buffer)(call-interactively 'compile)(let* ((cur (selected-window))(win (get-buffer-window "*compilation*")))(select-window win)(shrink-window-horizontally 10)(select-window cur)))
  • 复用编译窗口
;; 编译buffer变量
(defvar sanityinc/last-compilation-buffer nil"The last buffer in which compilation took place.");; 编译时自动保存编译buffer变量
(with-eval-after-load 'compile(defun sanityinc/save-compilation-buffer (&rest _)"Save the compilation buffer to find it later."(setq sanityinc/last-compilation-buffer next-error-last-buffer))(advice-add 'compilation-start :after 'sanityinc/save-compilation-buffer);; 编译前先查找是否编译buffer变量存在,而且可用(defun sanityinc/find-prev-compilation (orig &optional edit-command)"Find the previous compilation buffer, if present, and recompile there."(if (and (null edit-command)(not (derived-mode-p 'compilation-mode))sanityinc/last-compilation-buffer(buffer-live-p (get-buffer sanityinc/last-compilation-buffer)))(with-current-buffer sanityinc/last-compilation-buffer(funcall orig edit-command))(funcall orig edit-command)));; 添加编译前查找为recompile命令的advice(advice-add 'recompile :around 'sanityinc/find-prev-compilation))

编译中

  • 使用ansi-color显示编译过程中的颜色标识
(with-eval-after-load 'compile(require 'ansi-color);; 动态颜色显示区域回调函数(defun sanityinc/colourise-compilation-buffer ()(when (eq major-mode 'compilation-mode)(ansi-color-apply-on-region compilation-filter-start (point-max))));; 添加为回调(add-hook 'compilation-filter-hook 'sanityinc/colourise-compilation-buffer))
  • 设定自动滚动编译输出
(setq-default compilation-scroll-output t)

编译之后

  • 使用alert通知编译结果
(require-package 'alert)(defun sanityinc/alert-after-compilation-finish (buf result)"Use `alert' to report compilation RESULT if BUF is hidden."(when (buffer-live-p buf)(unless (catch 'is-visible(walk-windows (lambda (w)(when (eq (window-buffer w) buf)(throw 'is-visible t))))nil)(alert (concat "Compilation " result):buffer buf:category 'compilation))))(with-eval-after-load 'compile(add-hook 'compilation-finish-functions'sanityinc/alert-after-compilation-finish))
  • 如果编译的buffer没有位于多窗口中,切换到其它buffer;否则,关闭窗口。
(defun my-compile-exit (&optional buffer)"Exit compile BUFFER."(interactive)(if (one-window-p t)(progn(projectile-project-buffers-other-buffer))(progn(delete-window (get-buffer-window buffer)))))
  • 跳转到第一个错误。如果没有错误,调用前面定义的退出buffer。
(defun goto-first-error-or-exit (buffer string)"Call exit BUFFER when no errors."(interactive)(with-current-buffer buffer(goto-char (point-min))(condition-case err(compilation-next-error 1)(error(run-with-timer 1 nil(lambda (buf)(my-compile-exit buf))buffer)))))
  • 编译完成自动执行前面定义的跳转或者关闭。
(add-hook 'compilation-finish-functions 'goto-first-error-or-exit)

其中,关闭编译buffer之前等待1秒钟,方便简单留意一下编译信息。

  • 给编译窗口加几个快捷键
(add-hook 'compilation-mode-hook(lambda ()(local-set-key (kbd "n") 'compilation-next-error)(local-set-key (kbd "p") 'compilation-previous-error)(local-set-key (kbd "q") (lambda ()(interactive)(my-compile-exit (current-buffer))))))

其中,p跳转到前面一个错误,n跳转到后面一个错误,q退出。

CMake相关

因为很多项目使用Cmake做的构建系统,所以CMake相关的配置,也一并整理到编译相关的配置里。

  • 关联CMake配置文件为cmake-mode
(setq auto-mode-alist(append '(("CMakeLists\\.txt\\'" . cmake-mode)("\\.cmake\\'" . cmake-mode))auto-mode-alist))
  • 添加cmake的自动补全
(add-hook 'cmake-mode-hook(lambda()(add-to-list 'company-backends 'company-cmake)))

快捷键绑定

在cc-mode的模式下,使用Ctrl-x c编译;使用F7重编译。

(with-eval-after-load 'cc-mode(define-key c-mode-base-map (kbd "C-x c") 'my-compile)(define-key c-mode-base-map (kbd "<F7>") 'recompile))
http://www.xdnf.cn/news/904051.html

相关文章:

  • 深入了解linux系统—— 进程池
  • 使用docker 安装Redis 带配置文件(x86和arm)版本
  • 从边界防护到内生安全:企业网络安全进化路线图
  • docker数据管理
  • vue-print-nb 打印相关问题
  • 电脑同时连接内网和外网的方法,附外网连接局域网的操作设置
  • 【推荐算法】DeepFM:特征交叉建模的革命性架构
  • 前端实现视频/直播预览
  • 【Kubernetes】K8s 之 ETCD - 恢复备份
  • 职业生涯思考
  • Tomcat全方位监控实施方案指南
  • 撰写脚本,通过发布/joint_states话题改变机器人在Rviz中的关节角度
  • HakcMyVM -TheWall
  • 初识AI Agent
  • 面向开发者的提示词工程④——文本推断(Inferring)
  • 数学建模期末速成 聚类分析与判别分析
  • Caliper 配置文件解析:fisco-bcos.json
  • 【计算机组成原理 第5版】白、戴编著 第六章 总线系统 课后题总结
  • 大模型安全测试报告:千问、GPT 全系列、豆包、Claude 表现优异,DeepSeek、Grok-3 与 Kimi 存在安全隐患
  • LabVIEW工业级多任务实时测控系统
  • ComfyUI 文生图教程,进行第一次的图片生成
  • 连续小波变换(Continuous Wavelet Transform, CWT)
  • 【HarmonyOS 5】出行导航开发实践介绍以及详细案例
  • Spring Boot实现接口时间戳鉴权
  • ABAP设计模式之---“简单设计原则(Simple Design)”
  • Windows无限期暂停更新
  • 模板方法模式:优雅封装不变,灵活扩展可变
  • web3-基于贝尔曼福特算法(Bellman-Ford )与 SMT 的 Web3 DeFi 套利策略研究
  • 贝叶斯深度学习!华科大《Nat. Commun.》发表BNN重大突破!
  • Science Robotics:UCLA 贺曦敏团队综述自主软体机器人