pbpb
pbpb

Reputation: 11

Emacs: How to add a new face to dired

I'm trying to extend Dired in emacs 26 windows to show junctions similarly to symlinks. I've got everything done except I can't get the junction file name part to be fontified.

Here's what I've done so far:

(defface dired-junction
  '((t (:inherit font-lock-keyword-face)))
  "Face used for junctions."
  :group 'dired-faces
  :version "22.1")

(defvar dired-junction-face 'dired-junction
  "Face name used for junctions.")

(defvar dired-re-jun (concat dired-re-maybe-mark dired-re-inode-size "J[^:]"))

; ;; Symbolic links. pb:  this needs to be part of dired-font-lock-keywords
;   (list dired-re-sym
;    '(".+" (dired-move-to-filename) nil (0 dired-symlink-face)))

 ;; Junctions.
(push (list dired-re-jun
        '(".+" (dired-move-to-filename) nil (0 dired-junction-face)))
      dired-font-lock-keywords)

I don't know where Dired uses the dired-font-lock-keywords for fontification, so either I've left something out, or else I don't understand how to add to the dired-font-lock-keywords variable properly.

Note the J for junction is what I am using to replace the l for symlink or d for directory.

Upvotes: 0

Views: 111

Answers (2)

pbpb
pbpb

Reputation: 11

It turns out that the way I was trying to do it actually does work. My problem was that it needs to eval'ed before dired is first loaded. That is no doubt also related to the elisp doc saying that font-lock-add-keywords should only be called in the init file, and therefore before all the structures are set up apparently.

No doubt the other approaches suggested would also work, but I gave up debugging them once I figured out the source of my initial problem.

Thanks for the suggestions; without them I would never have figured it out. Emacs code is truly byzantine. In fact, I had to refactor a lot of ls-lisp-insert-directory just to figure out what it was doing.

Upvotes: 0

Drew
Drew

Reputation: 30701

Maybe this will get you started...

  1. You don't need a variable whose value is a face. Just use the face directly. Not important, but just FYI.

  2. Use font-lock-add-keywords. See (elisp) Customizing Keywords. And see (elisp) Search-based Fontification, which tells you not to set variable font-lock-keywords directly:

    The value of this variable is a list of the keywords to highlight. Lisp programs should not set this variable directly. Normally, the value is automatically set by Font Lock mode, using the KEYWORDS element in font-lock-defaults. The value can also be altered using the functions font-lock-add-keywords and font-lock-remove-keywords (See Customizing Keywords).

Upvotes: 0

Related Questions