我在'before-save-hook
中设置了各种各样的内容。例如,我运行'delete-trailing-whitespace
。这几乎是我在几乎所有场合都想要的。
但有时,我正在处理与其他人共享的文件,并且该文件已经有一堆尾随空格。如果我保存文件,我会得到一个非常令人困惑的大差异,因为我的变化被埋没在数十或数百个毫无意义的变化中。是的,每个人都可以告诉他们的差异工具不显示空白变化,但这是每个人每次看到差异时都要做的事情。我甚至不想改变空白。
在没有空格更改的情况下,我可以做些什么来保存文件,缺少启动没有init.el
文件的新Emacs实例,或者没有修改init.el
的没有{{1}}文件的实例钩?
答案 0 :(得分:7)
我提出的一个更简单的解决方案是我的<table id="students" class="table table-bordered table-hover">
<thead>
<tr>
<th>Name</th>
<th>Roll No</th>
<th>Result</th>
<th>Delete</th>
</tr>
</thead>
</table>
@section scripts
{
<script>
$(document).ready(function () {
$("#students").DataTable({
ajax: {
url: "/api/students",
dataSrc: ""
},
columns: [
{
data: "name",
render: function (data, type, student) {
return "<a href='/students/edit/" + student.id + "'>"
+ student.name + "</a>";
}
},
{
data: "rollNo"
},
{
data: "result"
},
{
data: "id",
render: function (data) {
return "<button class = 'btn-link js-delete' data-customer-id=" + data + ">Delete</button";
}
}
]
});
$("#students").on("click", ".js-delete", function () {
var button = $(this);
bootbox.confirm("Are you sure you want to delete this student?", function (result) {
if (result) {
$.ajax({
url: "/api/students/" + button.attr("data-student-id"),
method: "DELETE",
success: function () {
button.parents("tr").remove();
}
});
}
});
});
});
</script>
}
没有安装钩子,因为我希望它尽可能简单。因此,如果我想在不运行挂钩的情况下保存文件,我会暂时切换到fundamental-mode
。
答案 1 :(得分:5)
根据与@Stefan的评论讨论,这里有两个可能的(未经测试的)解决方案:
使用let
:
(defun save-buffer-without-dtw ()
(interactive)
(let ((b (current-buffer))) ; memorize the buffer
(with-temp-buffer ; new temp buffer to bind the global value of before-save-hook
(let ((before-save-hook (remove 'delete-trailing-whitespace before-save-hook)))
(with-current-buffer b ; go back to the current buffer, before-save-hook is now buffer-local
(let ((before-save-hook (remove 'delete-trailing-whitespace before-save-hook)))
(save-buffer)))))))
使用unwind-protect
:
(defun save-buffer-without-dtw ()
(interactive)
(let ((restore-global
(memq 'delete-trailing-whitespace (default-value before-save-hook)))
(restore-local
(and (local-variable-p 'before-save-hook)
(memq 'delete-trailing-whitespace before-save-hook))))
(unwind-protect
(progn
(when restore-global
(remove-hook 'before-save-hook 'delete-trailing-whitespace))
(when restore-local
(remove-hook 'before-save-hook 'delete-trailing-whitespace t))
(save-buffer))
(when restore-global
(add-hook 'before-save-hook 'delete-trailing-whitespace))
(when restore-local
(add-hook 'before-save-hook 'delete-trailing-whitespace nil t)))))
第二种解决方案的问题是before-save-hook
中的函数顺序可能会改变。
答案 2 :(得分:2)
这是另一种解决方案(100%保证未经测试):
(defvar my-inhibit-dtw nil)
(defun my-delete-trailing-whitespace ()
(unless my-inhibit-dtw (delete-trailing-whitespace)))
(add-hook 'before-save-hook 'my-delete-trailing-whitespace)
然后
(defun my-inhibit-dtw ()
(interactive)
(set (make-local-variable 'my-inhibit-dtw) t))
所以你可以在你不想修剪空格的缓冲区中M-x my-inhibit-dtw RET
。
答案 3 :(得分:1)
我们需要做的是从'delete-trailing-whitespace
删除before-save-hook
,保存缓冲区,然后将其添加回来。
此代码将执行此操作,但只有删除并添加它才能开始使用。
;; save the buffer, removing and readding the 'delete-trailing-whitespace function
;; to 'before-save-hook if it's there
(defun save-buffer-no-delete-trailing-whitespace ()
(interactive)
(let ((normally-should-delete-trailing-whitespace (memq 'delete-trailing-whitespace before-save-hook)))
(when normally-should-delete-trailing-whitespace
(remove-hook 'before-save-hook 'delete-trailing-whitespace))
(save-buffer)
(when normally-should-delete-trailing-whitespace
(add-hook 'before-save-hook 'delete-trailing-whitespace))))
(global-set-key (kbd "C-c C-s") 'save-buffer-no-delete-trailing-whitespace)
为方便起见,它还将命令绑定到(kbd C-c C-s)
。
答案 4 :(得分:1)
这是在不触发delete-trailing-whitespace
的情况下进行保存的方法:
C-x C-q C-x C-s C-x C-q
:只读,保存,还原为只读
答案 5 :(得分:1)
我根据 Nicholas Douma 的解决方案编写了一个命令。
(defun olav-save-buffer-as-is ()
"Save file \"as is\", that is in read-only-mode."
(interactive)
(if buffer-read-only
(save-buffer)
(read-only-mode 1)
(save-buffer)
(read-only-mode 0)))