Subject: 26.3; Emacs does not change fonts per mode when changing buffers
From: Ray Dillinger <[email-masked]>
To: [email-masked]
Date: Sat, 11 Jul 2020 11:35:32 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

Font changes set by mode hooks change when opening files but do not
change when switching buffers.  Font changes set by mode hooks can be
changed if the mode hook for a buffer is explicitly called, regardless
of whether the buffer is already in that mode.

When coding I prefer a 9-point monospace font. Accordingly my defaults
as set by Custom:

(custom-set-faces
 '(default ((t (:family "Latin Modern Mono" :foundry "unknown" :slant
normal :weight bold :height 90 :width condensed)))))

But recently I have been writing for publication, and I want to use the
publication font for text. Therefore I added a text hook:

(add-hook 'text-mode-hook
          (lambda()
            (setq fill-column 9999) ; each "line" (as defined by ending
in \n) is a paragraph.
            '(buffer-face-set "xft:-PfEd-DejaVu Serif-normal-normal-
semicondensed-*-21-*-*-*-*-0-iso10646-1 (#x4F)")
            (set-frame-font "DejaVuSerifCondensed 16" t t)
            (setq flyspell-mode t)
            ))

But then, after opening any text file, even though other mode-
appropriate changes would take place on opening/switching to a non-text 
buffer, emacs displayed ALL files with the text font.  I added a c-
mode-hook to explicitly undo the font changes for c files.

(add-hook 'c-mode-hook
          (lambda()
            (setq fill-column 160)
            (setq flyspell-mode nil)
            (set-frame-font "Latin Modern Mono Bold Condensed 9" t t)
            ))

Now if I open a text file it switches to the text font, and if I open a
c file it switches to the c font.  And that's correct behavior.

But if both files are already open, and I'm just switching between the
buffers, all the other c-mode or text-mode changes take place, but the
font does not change.

In order to get the font to change correctly, I have to run the hooks
explicitly: "M-x c-mode" seems like a silly thing to have to type if
already in C mode, but that's what it takes.  Likewise  "M-x text-mode" 
in the other case.

Something about the context of file opening allows font changes in the
mode hooks to succeed, where the context of buffer switching somehow
inhibits them.

In GNU Emacs 26.3 (build 2, x86_64-pc-linux-gnu, GTK+ Version 3.24.20)
 of 2020-05-16, modified by Debian built on x86-csail-01
Windowing system distributor 'The X.Org Foundation', version
11.0.12008000
System Description:     Debian GNU/Linux bullseye/sid

Recent messages:
Loading /etc/emacs/site-start.d/50openscad.el (source)...done
Loading /etc/emacs/site-start.d/50psvn.el (source)...done
Loading /etc/emacs/site-start.d/60hyperspec.el (source)...done
For information about GNU Emacs and the GNU system, type C-h C-a.
Auto-saving...done
Auto-saving...
Undo! [5 times]
Auto-saving...done
Mark set
Auto-saving...done

Configured using:
 'configure --build x86_64-linux-gnu --prefix=/usr --
sharedstatedir=/var/lib --libexecdir=/usr/lib --localstatedir=/var/lib
--infodir=/usr/share/info --mandir=/usr/share/man --enable-libsystemd
--with-pop=yes --enable-
locallisppath=/etc/emacs:/usr/local/share/emacs/26.3/site-
lisp:/usr/local/share/emacs/site-lisp:/usr/share/emacs/26.3/site-
lisp:/usr/share/emacs/site-lisp --with-sound=alsa --without-gconf --
with-mailutils --build x86_64-linux-gnu --prefix=/usr --
sharedstatedir=/var/lib --libexecdir=/usr/lib --localstatedir=/var/lib
--infodir=/usr/share/info --mandir=/usr/share/man --enable-libsystemd
--with-pop=yes --enable-
locallisppath=/etc/emacs:/usr/local/share/emacs/26.3/site-
lisp:/usr/local/share/emacs/site-lisp:/usr/share/emacs/26.3/site-
lisp:/usr/share/emacs/site-lisp --with-sound=alsa --without-gconf --
with-mailutils --with-x=yes --with-x-toolkit=gtk3 --with-toolkit-
scroll-bars 'CFLAGS=-g -O2 -fdebug-prefix-map=/build/emacs-
mHAik2/emacs-26.3+1=. -fstack-protector-strong -Wformat -Werror=format-
security -Wall' 'CPPFLAGS=-Wdate-time -D_FORTIFY_SOURCE=2' LDFLAGS=-
Wl,-z,relro'

Configured features:
XPM JPEG TIFF GIF PNG RSVG IMAGEMAGICK SOUND GPM DBUS GSETTINGS GLIB
NOTIFY ACL LIBSELINUX GNUTLS LIBXML2 FREETYPE M17N_FLT LIBOTF XFT ZLIB
TOOLKIT_SCROLL_BARS GTK3 X11 XDBE XIM THREADS LIBSYSTEMD LCMS2

Important settings:
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Emacs-Lisp

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  column-number-mode: t
  line-number-mode: t
  visual-line-mode: t
  transient-mark-mode: t

Load-path shadows:
/usr/share/emacs/site-lisp/elpa/dash-2.17.0/dash-autoloads hides
/usr/share/emacs/site-lisp/elpa-src/dash-2.17.0/dash-autoloads
/usr/share/emacs/site-lisp/elpa/dash-2.17.0/dash-pkg hides
/usr/share/emacs/site-lisp/elpa-src/dash-2.17.0/dash-pkg
/usr/share/emacs/site-lisp/elpa/dash-2.17.0/dash hides
/usr/share/emacs/site-lisp/elpa-src/dash-2.17.0/dash
/usr/share/emacs/site-lisp/elpa/ghub-3.3.0/glab hides
/usr/share/emacs/site-lisp/elpa-src/ghub-3.3.0/glab
/usr/share/emacs/site-lisp/elpa/ghub-3.3.0/ghub hides
/usr/share/emacs/site-lisp/elpa-src/ghub-3.3.0/ghub
/usr/share/emacs/site-lisp/elpa/ghub-3.3.0/buck hides
/usr/share/emacs/site-lisp/elpa-src/ghub-3.3.0/buck
/usr/share/emacs/site-lisp/elpa/ghub-3.3.0/gogs hides
/usr/share/emacs/site-lisp/elpa-src/ghub-3.3.0/gogs
/usr/share/emacs/site-lisp/elpa/ghub-3.3.0/gsexp hides
/usr/share/emacs/site-lisp/elpa-src/ghub-3.3.0/gsexp
/usr/share/emacs/site-lisp/elpa/ghub-3.3.0/ghub-autoloads hides
/usr/share/emacs/site-lisp/elpa-src/ghub-3.3.0/ghub-autoloads
/usr/share/emacs/site-lisp/elpa/ghub-3.3.0/gtea hides
/usr/share/emacs/site-lisp/elpa-src/ghub-3.3.0/gtea
/usr/share/emacs/site-lisp/elpa/ghub-3.3.0/ghub-pkg hides
/usr/share/emacs/site-lisp/elpa-src/ghub-3.3.0/ghub-pkg
/usr/share/emacs/site-lisp/elpa/ghub-3.3.0/ghub-graphql hides
/usr/share/emacs/site-lisp/elpa-src/ghub-3.3.0/ghub-graphql
/usr/share/emacs/site-lisp/elpa/git-commit-2.90.1/git-commit-autoloads
hides /usr/share/emacs/site-lisp/elpa-src/git-commit-2.90.1/git-commit-
autoloads
/usr/share/emacs/site-lisp/elpa/git-commit-2.90.1/git-commit-pkg hides
/usr/share/emacs/site-lisp/elpa-src/git-commit-2.90.1/git-commit-pkg
/usr/share/emacs/site-lisp/elpa/git-commit-2.90.1/git-commit hides
/usr/share/emacs/site-lisp/elpa-src/git-commit-2.90.1/git-commit
/usr/share/emacs/site-lisp/elpa/let-alist-1.0.6/let-alist hides
/usr/share/emacs/site-lisp/elpa-src/let-alist-1.0.6/let-alist
/usr/share/emacs/site-lisp/elpa/let-alist-1.0.6/let-alist-pkg hides
/usr/share/emacs/site-lisp/elpa-src/let-alist-1.0.6/let-alist-pkg
/usr/share/emacs/site-lisp/elpa/let-alist-1.0.6/let-alist-autoloads
hides /usr/share/emacs/site-lisp/elpa-src/let-alist-1.0.6/let-alist-
autoloads
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-imenu hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-imenu
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-core hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-core
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-clone hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-clone
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-ediff hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-ediff
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-submodule hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-submodule
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-git hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-git
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-patch hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-patch
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-process hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-process
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-reset hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-reset
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-files hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-files
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-margin hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-margin
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-push hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-push
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-stash hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-stash
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-blame hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-blame
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-fetch hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-fetch
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-remote hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-remote
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-branch hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-branch
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-section hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-section
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-apply hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-apply
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-obsolete hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-obsolete
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-autorevert hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-autorevert
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-refs hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-refs
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-utils hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-utils
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-subtree hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-subtree
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/git-rebase hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/git-rebase
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-extras hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-extras
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-status hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-status
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-collab hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-collab
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-autoloads hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-autoloads
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-pkg hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-pkg
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-mode hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-mode
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-gitignore hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-gitignore
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-bisect hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-bisect
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-repos hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-repos
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-merge hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-merge
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-commit hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-commit
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-notes hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-notes
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-log hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-log
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-worktree hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-worktree
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-bookmark hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-bookmark
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-pull hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-pull
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-sequence hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-sequence
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-diff hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-diff
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-tag hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-tag
/usr/share/emacs/site-lisp/elpa/magit-2.90.1/magit-wip hides
/usr/share/emacs/site-lisp/elpa-src/magit-2.90.1/magit-wip
/usr/share/emacs/site-lisp/elpa/magit-popup-2.13.2/magit-popup-pkg
hides /usr/share/emacs/site-lisp/elpa-src/magit-popup-2.13.2/magit-
popup-pkg
/usr/share/emacs/site-lisp/elpa/magit-popup-2.13.2/magit-popup hides
/usr/share/emacs/site-lisp/elpa-src/magit-popup-2.13.2/magit-popup
/usr/share/emacs/site-lisp/elpa/magit-popup-2.13.2/magit-popup-
autoloads hides /usr/share/emacs/site-lisp/elpa-src/magit-popup-
2.13.2/magit-popup-autoloads
/usr/share/emacs/site-lisp/elpa/slime-2.24/slime-tests hides
/usr/share/emacs/site-lisp/elpa-src/slime-2.24/slime-tests
/usr/share/emacs/site-lisp/elpa/slime-2.24/slime-autoloads hides
/usr/share/emacs/site-lisp/elpa-src/slime-2.24/slime-autoloads
/usr/share/emacs/site-lisp/elpa/slime-2.24/slime-pkg hides
/usr/share/emacs/site-lisp/elpa-src/slime-2.24/slime-pkg
/usr/share/emacs/site-lisp/elpa/slime-2.24/slime hides
/usr/share/emacs/site-lisp/elpa-src/slime-2.24/slime
/usr/share/emacs/site-lisp/elpa/treepy-0.1.1/treepy hides
/usr/share/emacs/site-lisp/elpa-src/treepy-0.1.1/treepy
/usr/share/emacs/site-lisp/elpa/treepy-0.1.1/treepy-autoloads hides
/usr/share/emacs/site-lisp/elpa-src/treepy-0.1.1/treepy-autoloads
/usr/share/emacs/site-lisp/elpa/treepy-0.1.1/treepy-pkg hides
/usr/share/emacs/site-lisp/elpa-src/treepy-0.1.1/treepy-pkg
/usr/share/emacs/site-lisp/elpa/with-editor-2.9.1/with-editor-autoloads 
hides /usr/share/emacs/site-lisp/elpa-src/with-editor-2.9.1/with-
editor-autoloads
/usr/share/emacs/site-lisp/elpa/with-editor-2.9.1/with-editor hides
/usr/share/emacs/site-lisp/elpa-src/with-editor-2.9.1/with-editor
/usr/share/emacs/site-lisp/elpa/with-editor-2.9.1/with-editor-pkg hides
/usr/share/emacs/site-lisp/elpa-src/with-editor-2.9.1/with-editor-pkg
/usr/share/emacs/site-lisp/elpa/let-alist-1.0.6/let-alist hides
/usr/share/emacs/26.3/lisp/emacs-lisp/let-alist
/usr/share/emacs/site-lisp/auctex/tex hides /usr/share/auctex/tex
/usr/share/emacs/site-lisp/auctex/plain-tex hides
/usr/share/auctex/plain-tex
/usr/share/emacs/site-lisp/auctex/latex hides /usr/share/auctex/latex
/usr/share/emacs/site-lisp/auctex/preview hides
/usr/share/auctex/preview
/usr/share/emacs/site-lisp/auctex/tex-fold hides /usr/share/auctex/tex-
fold
/usr/share/emacs/site-lisp/auctex/context hides
/usr/share/auctex/context
/usr/share/emacs/site-lisp/auctex/multi-prompt hides
/usr/share/auctex/multi-prompt
/usr/share/emacs/site-lisp/auctex/font-latex hides
/usr/share/auctex/font-latex
/usr/share/emacs/site-lisp/auctex/context-nl hides
/usr/share/auctex/context-nl
/usr/share/emacs/site-lisp/auctex/tex-ispell hides
/usr/share/auctex/tex-ispell
/usr/share/emacs/site-lisp/auctex/texmathp hides
/usr/share/auctex/texmathp
/usr/share/emacs/site-lisp/auctex/prv-emacs hides
/usr/share/auctex/prv-emacs
/usr/share/emacs/site-lisp/auctex/bib-cite hides /usr/share/auctex/bib-
cite
/usr/share/emacs/site-lisp/auctex/tex-info hides /usr/share/auctex/tex-
info
/usr/share/emacs/site-lisp/auctex/tex-style hides
/usr/share/auctex/tex-style
/usr/share/emacs/site-lisp/auctex/tex-jp hides /usr/share/auctex/tex-jp
/usr/share/emacs/site-lisp/auctex/context-en hides
/usr/share/auctex/context-en
/usr/share/emacs/site-lisp/auctex/tex-mik hides /usr/share/auctex/tex-
mik
/usr/share/emacs/site-lisp/auctex/tex-bar hides /usr/share/auctex/tex-
bar
/usr/share/emacs/site-lisp/auctex/toolbar-x hides
/usr/share/auctex/toolbar-x
/usr/share/emacs/site-lisp/auctex/tex-font hides /usr/share/auctex/tex-
font
/usr/share/emacs/site-lisp/auctex/tex-buf hides /usr/share/auctex/tex-
buf

Features:
(shadow sort mail-extr emacsbug message rmc puny dired dired-loaddefs
format-spec rfc822 mml mml-sec epa derived epg gnus-util rmail rmail-
loaddefs mm-decode mm-bodies mm-encode mail-parse rfc2231 mailabbrev
gmm-utils mailheader sendmail rfc2047 rfc2045 ietf-drums mm-util mail-
prsvr mail-utils elec-pair finder-inf slime-autoloads package easymenu
epg-config url-handlers url-parse auth-source cl-seq eieio eieio-core
cl-macs eieio-loaddefs password-cache url-vars seq byte-opt gv bytecomp
byte-compile cconv cl-loaddefs cl-lib devhelp preview-latex tex-site
auto-loads time-date mule-util tooltip eldoc electric uniquify ediff-
hook vc-hooks lisp-float-type mwheel term/x-win x-win term/common-win
x-dnd tool-bar dnd fontset image regexp-opt fringe tabulated-list
replace newcomment text-mode elisp-mode lisp-mode prog-mode register
page menu-bar rfn-eshadow isearch timer select scroll-bar mouse jit-
lock font-lock syntax facemenu font-core term/tty-colors frame cl-
generic cham georgian utf-8-lang misc-lang vietnamese tibetan thai tai-
viet lao korean japanese eucjp-ms cp51932 hebrew greek romanian slovak
czech european ethiopic indian cyrillic chinese composite charscript
charprop case-table epa-hook jka-cmpr-hook help simple abbrev obarray
minibuffer cl-preloaded nadvice loaddefs button faces cus-face macroexp
files text-properties overlay sha1 md5 base64 format env code-pages
mule custom widget hashtable-print-readable backquote threads dbusbind
inotify lcms2 dynamic-setting system-font-setting font-render-setting
move-toolbar gtk x-toolkit x multi-tty make-network-process emacs)

Memory information:
((conses 16 114727 11004)
 (symbols 48 22229 1)
 (miscs 40 72 291)
 (strings 32 33860 1728)
 (string-bytes 1 951529)
 (vectors 16 16148)
 (vector-slots 8 529561 8210)
 (floats 8 52 351)
 (intervals 56 514 125)
 (buffers 992 12))