How to display file while still in find

Currently, I use find-file-hook to invoke a lengthy compilation/checking of that file. I have therefore to wait for some time to actually see the file. What I would like to do instead is to be able to view (not edit) the file already while the checker is running, thus creating the illusion of instantaneous compilation. How can I do this?


Using find-file-hook means your code will run on every file you open; are you sure you want this? It may make more sense to create a new major or minor mode for the type of file you want to run your validation on and then use the corresponding mode hook. For instance, if you wanted to check all .chk files (with your new major mode inheriting from prog-mode ):

(define-derived-mode check-mode prog-mode "Checker")
(add-to-list 'auto-mode-alist '(".chk'" . check-mode))
(add-hook 'check-mode-hook 'check-mode-computation-hook)

As for the actual hook, this code (going off phils' comment) works for me:

;;; -*- lexical-binding: t -*-
(defun slow-computation ()
  (dotimes (i 10000000)
    (+ i 1)))

(defun check-mode-computation-hook ()
  (let ((cb (current-buffer))
        (ro buffer-read-only))
    (setq-local buffer-read-only t)
    (run-at-time .1 nil
                 (lambda ()
                   (with-current-buffer cb
                     (message "Loading...")
                     (slow-computation)
                     (setq-local buffer-read-only ro)
                     (message "Loaded!"))))))

Note, though, that though this will display the file, emacs will still be frozen until it finishes its processing, as emacs doesn't actually support multithreading. To get around this, you may have to use a library like async, deferred, or concurrent.


您应该考虑使用Flycheck,它为大多数编程语言提供异步语法检查,并为实现新/定制检查程序提供了一个很好的API。

链接地址: http://www.djcxy.com/p/93900.html

上一篇: 为什么我无法在浏览器中存储oauth刷新令牌?

下一篇: 如何在查找过程中显示文件