How do I byte-compile everything in my .emacs.d directory?


C-u 0 M-x byte-recompile-directory

will compile all the .el files in the directory and in all subdirectories below.

The C-u 0 part is to make it not ask about every .el file that does not have a .elc counterpart.


How to enter view-only mode when browsing Emacs source code from help?


When I browse Emacs help for functions via C-h f, I often want to peek into the Elisp/C implementation. I want to enter view-mode automatically when I access source code this way to avoid unnecessary modification. Is there a hook or function I can advise to accomplish this?

shareimprove this question

I use the alternative approach which you can specify entirely in your init file (as opposed to creating a .dir-locals.el file), and I simply make the files read-only rather than using view-mode. My config looks like this:

;; Emacs
 '((nil . ((buffer-read-only . t)
           (show-trailing-whitespace . nil)
           (tab-width . 8)
           (eval . (whitespace-mode -1))))))

(dir-locals-set-directory-class "/usr/local/src/emacs" 'emacs)
(dir-locals-set-directory-class "/usr/local/share/emacs" 'emacs)
(dir-locals-set-directory-class "/usr/share/emacs" 'emacs)

Obviously you can do the same thing for your elpa directory, and any other directory which contains third-party source code.

shareimprove this answer

Is there a simple way to time a function call in elisp?

[Originally Posted By]:

I found exactly what I was looking for at

(defmacro measure-time 
  "Measure and return the running time of the code block."
  (declare (indent defun)) 
  (let ((start (make-symbol "start"))) 
    `(let ((,start (float-time))) ,@body (- (float-time) ,start))))

How to automatically timestamp messages in *Messages* buffer?

[Originally posted by]:

The following, by /u/wadcann, works for me in 24.5:

;; timestamps in *Messages*
;; via

(defun current-time-microseconds ()
  (let* ((nowtime (current-time))
         (now-ms (nth 2 nowtime)))
    (concat (format-time-string "[%Y-%m-%dT%T" nowtime) (format ".%d] " now-ms))))

(defadvice message (before test-symbol activate)
  (if (not (string-equal (ad-get-arg 0) "%s%s"))
      (let ((inhibit-read-only t)
            (deactivate-mark nil))
        (with-current-buffer "*Messages*"
          (goto-char (point-max))
          (if (not (bolp))
          (insert (current-time-microseconds))))))

after-init-hook vs eval-after-load/with-eval-after-load


Afaik, (add-hook 'after-init-hook ...) is the so-to-say brute-force approach to setting up customizations for packages.

No. If you want to configure packages in your init file, the typical approach is to call (package-initialize) at the top of the file, and then do the configurations. after-init-hook is unnecessarily complicated for this purpose.

However, eval-after-load and its more syntactically sugary with-eval-after-load seem more appropriate for more dynamic and fine grained configuration that plays well with the gradual, separate and lazy loading of extensions.

(with-)eval-after-load is used for things you want to do after a package is loaded. package-initialize doesn’t load the packages, it only “prepares” them (loads their autoloads).

If you use eval-after-load around every set of configurations, you won’t need to call (package-initialize) at the top. But what people usually do is to call (package-initialize), so that most configurations can be written straight to the init file, and then only eval-after-load code which is very long or which depends on the package being loaded.

shareimprove this answer