2

I'm using doom emacs.

It enables prettify-symbols in all modes (I think) by default, although I would like to disable it in c-mode.

I have placed the following in my config.el file:

(add-hook 'c-mode-hook (lambda ()
                     (prettify-symbols-mode -1)))

I have also tried this:

(add-hook 'c-mode-hook (lambda ()
                     (setq prettify-symbols-alist '())))

Nothing has worked, but I am able to disable it manually with M-x prettify-symbols-mode, but I find this tedious to do every time I want to edit a c file. Any help is appreciated.

Edit: I tried Phils's answer, to no avail. I also tried putting

(global-prettify-symbols-mode -1)

before that answer. I also did that, but changing the function definition to:

(prettify-symbols-mode -1)

Edit: Per Phils's comment I used M-x debug-on-entry RET prettify-symbols-mode and then opened a C file and the following traceback occurred:

Debugger entered--entering a function:
* global-prettify-symbols-mode(-1)
  my-c-mode-hook()
  run-hooks(change-major-mode-after-body-hook prog-mode-hook c-mode-common-hook c-mode-hook)
  apply(run-hooks (change-major-mode-after-body-hook prog-mode-hook c-mode-common-hook c-mode-hook))
  run-mode-hooks(c-mode-hook)
  c-mode()
  set-auto-mode-0(c-mode nil)
  #f(compiled-function (&optional keep-mode-if-same) "Select major mode appropriate for current buffer.\n\nTo find the right major mode, this function checks for a -*- mode tag\nchecks for a `mode:' entry in the Local Variables section of the file,\nchecks if it uses an interpreter listed in `interpreter-mode-alist',\nmatches the buffer beginning against `magic-mode-alist',\ncompares the filename against the entries in `auto-mode-alist',\nthen matches the buffer beginning against `magic-fallback-mode-alist'.\n\nIf `enable-local-variables' is nil, or if the file name matches\n`inhibit-local-variables-regexps', this function does not check\nfor any mode: tag anywhere in the file.  If `local-enable-local-variables'\nis nil, then the only mode: tag that can be relevant is a -*- one.\n\nIf the optional argument KEEP-MODE-IF-SAME is non-nil, then we\nset the major mode only if that would change it.  In other words\nwe don't actually set it to the same mode the buffer already has." #<bytecode -0x14884ef2754764cc>)()
  apply(#f(compiled-function (&optional keep-mode-if-same) "Select major mode appropriate for current buffer.\n\nTo find the right major mode, this function checks for a -*- mode tag\nchecks for a `mode:' entry in the Local Variables section of the file,\nchecks if it uses an interpreter listed in `interpreter-mode-alist',\nmatches the buffer beginning against `magic-mode-alist',\ncompares the filename against the entries in `auto-mode-alist',\nthen matches the buffer beginning against `magic-fallback-mode-alist'.\n\nIf `enable-local-variables' is nil, or if the file name matches\n`inhibit-local-variables-regexps', this function does not check\nfor any mode: tag anywhere in the file.  If `local-enable-local-variables'\nis nil, then the only mode: tag that can be relevant is a -*- one.\n\nIf the optional argument KEEP-MODE-IF-SAME is non-nil, then we\nset the major mode only if that would change it.  In other words\nwe don't actually set it to the same mode the buffer already has." #<bytecode -0x14884ef2754764cc>) nil)
  #f(advice-wrapper :after #f(compiled-function (&optional keep-mode-if-same) "Select major mode appropriate for current buffer.\n\nTo find the right major mode, this function checks for a -*- mode tag\nchecks for a `mode:' entry in the Local Variables section of the file,\nchecks if it uses an interpreter listed in `interpreter-mode-alist',\nmatches the buffer beginning against `magic-mode-alist',\ncompares the filename against the entries in `auto-mode-alist',\nthen matches the buffer beginning against `magic-fallback-mode-alist'.\n\nIf `enable-local-variables' is nil, or if the file name matches\n`inhibit-local-variables-regexps', this function does not check\nfor any mode: tag anywhere in the file.  If `local-enable-local-variables'\nis nil, then the only mode: tag that can be relevant is a -*- one.\n\nIf the optional argument KEEP-MODE-IF-SAME is non-nil, then we\nset the major mode only if that would change it.  In other words\nwe don't actually set it to the same mode the buffer already has." #<bytecode -0x14884ef2754764cc>) auto-minor-mode-set)()
  apply(#f(advice-wrapper :after #f(compiled-function (&optional keep-mode-if-same) "Select major mode appropriate for current buffer.\n\nTo find the right major mode, this function checks for a -*- mode tag\nchecks for a `mode:' entry in the Local Variables section of the file,\nchecks if it uses an interpreter listed in `interpreter-mode-alist',\nmatches the buffer beginning against `magic-mode-alist',\ncompares the filename against the entries in `auto-mode-alist',\nthen matches the buffer beginning against `magic-fallback-mode-alist'.\n\nIf `enable-local-variables' is nil, or if the file name matches\n`inhibit-local-variables-regexps', this function does not check\nfor any mode: tag anywhere in the file.  If `local-enable-local-variables'\nis nil, then the only mode: tag that can be relevant is a -*- one.\n\nIf the optional argument KEEP-MODE-IF-SAME is non-nil, then we\nset the major mode only if that would change it.  In other words\nwe don't actually set it to the same mode the buffer already has." #<bytecode -0x14884ef2754764cc>) auto-minor-mode-set) nil)
  so-long--set-auto-mode(#f(advice-wrapper :after #f(compiled-function (&optional keep-mode-if-same) "Select major mode appropriate for current buffer.\n\nTo find the right major mode, this function checks for a -*- mode tag\nchecks for a `mode:' entry in the Local Variables section of the file,\nchecks if it uses an interpreter listed in `interpreter-mode-alist',\nmatches the buffer beginning against `magic-mode-alist',\ncompares the filename against the entries in `auto-mode-alist',\nthen matches the buffer beginning against `magic-fallback-mode-alist'.\n\nIf `enable-local-variables' is nil, or if the file name matches\n`inhibit-local-variables-regexps', this function does not check\nfor any mode: tag anywhere in the file.  If `local-enable-local-variables'\nis nil, then the only mode: tag that can be relevant is a -*- one.\n\nIf the optional argument KEEP-MODE-IF-SAME is non-nil, then we\nset the major mode only if that would change it.  In other words\nwe don't actually set it to the same mode the buffer already has." #<bytecode -0x14884ef2754764cc>) auto-minor-mode-set))
  apply(so-long--set-auto-mode #f(advice-wrapper :after #f(compiled-function (&optional keep-mode-if-same) "Select major mode appropriate for current buffer.\n\nTo find the right major mode, this function checks for a -*- mode tag\nchecks for a `mode:' entry in the Local Variables section of the file,\nchecks if it uses an interpreter listed in `interpreter-mode-alist',\nmatches the buffer beginning against `magic-mode-alist',\ncompares the filename against the entries in `auto-mode-alist',\nthen matches the buffer beginning against `magic-fallback-mode-alist'.\n\nIf `enable-local-variables' is nil, or if the file name matches\n`inhibit-local-variables-regexps', this function does not check\nfor any mode: tag anywhere in the file.  If `local-enable-local-variables'\nis nil, then the only mode: tag that can be relevant is a -*- one.\n\nIf the optional argument KEEP-MODE-IF-SAME is non-nil, then we\nset the major mode only if that would change it.  In other words\nwe don't actually set it to the same mode the buffer already has." #<bytecode -0x14884ef2754764cc>) auto-minor-mode-set) nil)
  set-auto-mode()
  normal-mode(t)
  #f(compiled-function (&optional error warn noauto after-find-file-from-revert-buffer nomodes) "Called after finding a file and by the default revert function.\nSets buffer mode, parses file-local and directory-local variables.\nOptional args ERROR, WARN, and NOAUTO: ERROR non-nil means there was an\nerror in reading the file.  WARN non-nil means warn if there\nexists an auto-save file more recent than the visited file.\nNOAUTO means don't mess with auto-save mode.\nFourth arg AFTER-FIND-FILE-FROM-REVERT-BUFFER is ignored\n(see `revert-buffer-in-progress-p' for similar functionality).\nFifth arg NOMODES non-nil means don't alter the file's modes.\nFinishes by calling the functions in `find-file-hook'\nunless NOMODES is non-nil." #<bytecode 0x68a5324eea0fe54>)(t t)
  apply(#f(compiled-function (&optional error warn noauto after-find-file-from-revert-buffer nomodes) "Called after finding a file and by the default revert function.\nSets buffer mode, parses file-local and directory-local variables.\nOptional args ERROR, WARN, and NOAUTO: ERROR non-nil means there was an\nerror in reading the file.  WARN non-nil means warn if there\nexists an auto-save file more recent than the visited file.\nNOAUTO means don't mess with auto-save mode.\nFourth arg AFTER-FIND-FILE-FROM-REVERT-BUFFER is ignored\n(see `revert-buffer-in-progress-p' for similar functionality).\nFifth arg NOMODES non-nil means don't alter the file's modes.\nFinishes by calling the functions in `find-file-hook'\nunless NOMODES is non-nil." #<bytecode 0x68a5324eea0fe54>) (t t))
  (if (setq doom-large-file-p (and buffer-file-name (not doom-large-file-p) (file-exists-p buffer-file-name) (condition-case nil (progn (> (nth 7 (file-attributes buffer-file-name)) (* 1024 1024 (assoc-default buffer-file-name doom-large-file-size-alist ...)))) (error nil)))) (prog1 (apply orig-fn args) (if (memq major-mode doom-large-file-excluded-modes) (setq doom-large-file-p nil) (if (fboundp 'so-long-minor-mode) (progn (so-long-minor-mode 1))) (message "Large file detected! Cutting a few corners to impr..."))) (apply orig-fn args))
  doom--optimize-for-large-files-a(#f(compiled-function (&optional error warn noauto after-find-file-from-revert-buffer nomodes) "Called after finding a file and by the default revert function.\nSets buffer mode, parses file-local and directory-local variables.\nOptional args ERROR, WARN, and NOAUTO: ERROR non-nil means there was an\nerror in reading the file.  WARN non-nil means warn if there\nexists an auto-save file more recent than the visited file.\nNOAUTO means don't mess with auto-save mode.\nFourth arg AFTER-FIND-FILE-FROM-REVERT-BUFFER is ignored\n(see `revert-buffer-in-progress-p' for similar functionality).\nFifth arg NOMODES non-nil means don't alter the file's modes.\nFinishes by calling the functions in `find-file-hook'\nunless NOMODES is non-nil." #<bytecode 0x68a5324eea0fe54>) t t)
  apply(doom--optimize-for-large-files-a #f(compiled-function (&optional error warn noauto after-find-file-from-revert-buffer nomodes) "Called after finding a file and by the default revert function.\nSets buffer mode, parses file-local and directory-local variables.\nOptional args ERROR, WARN, and NOAUTO: ERROR non-nil means there was an\nerror in reading the file.  WARN non-nil means warn if there\nexists an auto-save file more recent than the visited file.\nNOAUTO means don't mess with auto-save mode.\nFourth arg AFTER-FIND-FILE-FROM-REVERT-BUFFER is ignored\n(see `revert-buffer-in-progress-p' for similar functionality).\nFifth arg NOMODES non-nil means don't alter the file's modes.\nFinishes by calling the functions in `find-file-hook'\nunless NOMODES is non-nil." #<bytecode 0x68a5324eea0fe54>) (t t))
  #f(advice-wrapper :around #f(compiled-function (&optional error warn noauto after-find-file-from-revert-buffer nomodes) "Called after finding a file and by the default revert function.\nSets buffer mode, parses file-local and directory-local variables.\nOptional args ERROR, WARN, and NOAUTO: ERROR non-nil means there was an\nerror in reading the file.  WARN non-nil means warn if there\nexists an auto-save file more recent than the visited file.\nNOAUTO means don't mess with auto-save mode.\nFourth arg AFTER-FIND-FILE-FROM-REVERT-BUFFER is ignored\n(see `revert-buffer-in-progress-p' for similar functionality).\nFifth arg NOMODES non-nil means don't alter the file's modes.\nFinishes by calling the functions in `find-file-hook'\nunless NOMODES is non-nil." #<bytecode 0x68a5324eea0fe54>) doom--optimize-for-large-files-a)(t t)
  apply(#f(advice-wrapper :around #f(compiled-function (&optional error warn noauto after-find-file-from-revert-buffer nomodes) "Called after finding a file and by the default revert function.\nSets buffer mode, parses file-local and directory-local variables.\nOptional args ERROR, WARN, and NOAUTO: ERROR non-nil means there was an\nerror in reading the file.  WARN non-nil means warn if there\nexists an auto-save file more recent than the visited file.\nNOAUTO means don't mess with auto-save mode.\nFourth arg AFTER-FIND-FILE-FROM-REVERT-BUFFER is ignored\n(see `revert-buffer-in-progress-p' for similar functionality).\nFifth arg NOMODES non-nil means don't alter the file's modes.\nFinishes by calling the functions in `find-file-hook'\nunless NOMODES is non-nil." #<bytecode 0x68a5324eea0fe54>) doom--optimize-for-large-files-a) (t t))
  #f(advice-wrapper :before #f(advice-wrapper :around #f(compiled-function (&optional error warn noauto after-find-file-from-revert-buffer nomodes) "Called after finding a file and by the default revert function.\nSets buffer mode, parses file-local and directory-local variables.\nOptional args ERROR, WARN, and NOAUTO: ERROR non-nil means there was an\nerror in reading the file.  WARN non-nil means warn if there\nexists an auto-save file more recent than the visited file.\nNOAUTO means don't mess with auto-save mode.\nFourth arg AFTER-FIND-FILE-FROM-REVERT-BUFFER is ignored\n(see `revert-buffer-in-progress-p' for similar functionality).\nFifth arg NOMODES non-nil means don't alter the file's modes.\nFinishes by calling the functions in `find-file-hook'\nunless NOMODES is non-nil." #<bytecode 0x68a5324eea0fe54>) doom--optimize-for-large-files-a) doom-first-file-hook-h)(t t)
  apply(#f(advice-wrapper :before #f(advice-wrapper :around #f(compiled-function (&optional error warn noauto after-find-file-from-revert-buffer nomodes) "Called after finding a file and by the default revert function.\nSets buffer mode, parses file-local and directory-local variables.\nOptional args ERROR, WARN, and NOAUTO: ERROR non-nil means there was an\nerror in reading the file.  WARN non-nil means warn if there\nexists an auto-save file more recent than the visited file.\nNOAUTO means don't mess with auto-save mode.\nFourth arg AFTER-FIND-FILE-FROM-REVERT-BUFFER is ignored\n(see `revert-buffer-in-progress-p' for similar functionality).\nFifth arg NOMODES non-nil means don't alter the file's modes.\nFinishes by calling the functions in `find-file-hook'\nunless NOMODES is non-nil." #<bytecode 0x68a5324eea0fe54>) doom--optimize-for-large-files-a) doom-first-file-hook-h) (t t))
  after-find-file(t t)
  find-file-noselect-1(#<buffer wueygfqweu.c> "c:/Users/Username/wueygfqweu.c" nil nil "c:/Users/username/wueygfqweu.c" nil)
  find-file-noselect("wueygfqweu.c" nil nil nil)
  find-file("wueygfqweu.c")
  evil-edit("wueygfqweu.c" nil)
  funcall-interactively(evil-edit "wueygfqweu.c" nil)
  call-interactively(evil-edit)
  evil-ex-call-command(nil #("e" 0 1 (ex-index 1)) #("wueygfqweu.c" 0 1 (ex-index 3) 1 2 (ex-index 4) 2 3 (ex-index 5) 3 4 (ex-index 6) 4 5 (ex-index 7) 5 6 (ex-index 8) 6 7 (ex-index 9) 7 8 (ex-index 10) 8 9 (ex-index 11) 9 10 (ex-index 12) 10 11 (ex-index 13) 11 12 (ex-index 14)))
  eval((evil-ex-call-command nil #("e" 0 1 (ex-index 1)) #("wueygfqweu.c" 0 1 (ex-index 3) 1 2 (ex-index 4) 2 3 (ex-index 5) 3 4 (ex-index 6) 4 5 (ex-index 7) 5 6 (ex-index 8) 6 7 (ex-index 9) 7 8 (ex-index 10) 8 9 (ex-index 11) 9 10 (ex-index 12) 10 11 (ex-index 13) 11 12 (ex-index 14))))
  evil-ex-execute(#("e wueygfqweu.c" 0 1 (ex-index 1) 1 2 (ex-index 2) 2 3 (ex-index 3) 3 4 (ex-index 4) 4 5 (ex-index 5) 5 6 (ex-index 6) 6 7 (ex-index 7) 7 8 (ex-index 8) 8 9 (ex-index 9) 9 10 (ex-index 10) 10 11 (ex-index 11) 11 12 (ex-index 12) 12 13 (ex-index 13) 13 14 (ex-index 14)))
  #f(compiled-function (&optional initial-input) (interactive (list (let ((s (concat (cond ((and (evil-visual-state-p) evil-ex-visual-char-range (memq (evil-visual-type) '(inclusive exclusive))) "`<,`>") ((evil-visual-state-p) "'<,'>") (current-prefix-arg (let ((arg (prefix-numeric-value current-prefix-arg))) (cond ((< arg 0) (setq arg (1+ arg))) ((> arg 0) (setq arg (1- arg)))) (if (= arg 0) "." (format ".,.%+d" arg))))) evil-ex-initial-input))) (and (> (length s) 0) s)))) #<bytecode -0x1f4f683ad84e3442>)(nil)
  apply(#f(compiled-function (&optional initial-input) (interactive (list (let ((s (concat ... evil-ex-initial-input))) (and (> (length s) 0) s)))) #<bytecode -0x1f4f683ad84e3442>) nil)
  (let ((completion-in-region-function #'completion--in-region)) (apply orig-fn args))
  +ivy--inhibit-completion-in-region-a(#f(compiled-function (&optional initial-input) (interactive (list (let ((s (concat ... evil-ex-initial-input))) (and (> (length s) 0) s)))) #<bytecode -0x1f4f683ad84e3442>) nil)
  apply(+ivy--inhibit-completion-in-region-a #f(compiled-function (&optional initial-input) (interactive (list (let ((s (concat ... evil-ex-initial-input))) (and (> (length s) 0) s)))) #<bytecode -0x1f4f683ad84e3442>) nil)
  evil-ex(nil)
  funcall-interactively(evil-ex nil)
  call-interactively(evil-ex nil nil)
  command-execute(evil-ex)
Aronurr64
  • 23
  • 3
  • Ok, get rid of all your attempts to disable this; Start a new Emacs instance; Use `M-x debug-on-entry RET prettify-symbols-mode RET` and then open a C file. You should get a backtrace. Copy it, and edit it into your question. – phils Sep 14 '20 at 03:18
  • I after running this command, I am unable to open a C file because when I press `M-x` I am shown the backtrace. I have added it anyway. – Aronurr64 Sep 14 '20 at 16:46
  • Apologies, I should also have pointed you to `C-h i g (elisp)Debugger Commands` so you knew how to dismiss the debugger buffer, as well as `M-x cancel-debug-on-entry`. – phils Sep 14 '20 at 20:58
  • The fact that it's triggering from `after-change-major-mode-hook` though seems *very* much like this is `global-prettify-symbols-mode`, per the behaviour detailed in the link in my answer. I suggest you now try adding `(debug-on-entry 'global-prettify-symbols-mode)` at the very start of your init file, and then start Emacs and confirm whether something is calling it, which would be useful information. – phils Sep 14 '20 at 21:02
  • I've tweaked the code in my answer; see if that changes things. – phils Sep 14 '20 at 21:13

1 Answers1

1

I presume you have global-prettify-symbols-mode enabled, which is a globalized minor mode. They can be a bit tricky.

Calling (prettify-symbols-mode -1) in c-mode-hook wouldn't work because the mode isn't being enabled until later.

Using (setq prettify-symbols-alist '()) in c-mode-hook might work? It won't prevent the buffer-local mode being enabled, but it might stop it doing anything?

turn-on-prettify-symbols-mode decides whether to enable the buffer-local mode, and it checks (local-variable-p 'prettify-symbols-alist); so rather than setting the local value to an empty list, you might instead want to (kill-local-variable 'prettify-symbols-alist)

Don't use lambdas in hooks, though -- you might leave the old code in the hook inadvertently. I suggest purging your existing functions from the hook, and then making a named function. Untested, but try this:

(defun my-c-mode-hook ()
  "Custom `c-mode' behaviours."
  ;; Inhibit `global-prettify-symbols-mode'.
  (kill-local-variable 'prettify-symbols-alist))

(add-hook 'c-mode-hook #'my-c-mode-hook :append)

Whether it actually works might depend on exactly what is setting that alist in the first place; but if it's another use of c-mode-hook, then hopefully the APPEND argument is sufficient to ensure that you act later than it does.

phils
  • 48,657
  • 3
  • 76
  • 115
  • The `:append` argument did not work. The function is at the end of `c-mode-hook`, but it does not prevent entering `prettify-symbols-mode`. – Aronurr64 Sep 15 '20 at 23:33
  • I suggest grepping your config for `prettify-symbols-alist` to find out what might be responsible for setting that after `c-mode-hook`. You could alternatively use `C-h i g (elisp)Watching Variables` to trigger the debugger when something sets it. – phils Sep 16 '20 at 01:19
  • Have a look through `after-change-major-mode-hook` as well. You'd be looking for something which is setting `prettify-symbols-alist` before the globalized mode acts (which happens during this hook). If the hook is buffer-local and contains `t` then remember to check the global value as well. – phils Sep 16 '20 at 01:24