1

I want to change the major mode from fundamental-mode to compilation-mode for a buffer which contains some generated output named for eg. *results*. I've tried to add an entry to auto-mode-alist but if i understand correctly this works only with buffers which have a corresponding filename.

I've tried to add it in the list like this but this is not working.

(add-to-list 'auto-mode-alist '("\\*result\\*" . compilation-mode))

UPDATE:

After some digging in I found that this particular lisp snippet is filling the buffer with data.

(start-process "result" "*result*" "some_unix_command")

UPDATE:

This buffer is generated from a 3rd party plugin so i'd rather not start and change the src lisp code. I'd like to handle this on a different level.

I've already checked if I can attach a hook somewhere where the buffer is created and the answer is negative.

Anyone with some ideas on how I can do this?

  • 2
    Can you find the function that creates that buffer? Maybe it runs a hook or you can advise the function? – Dan Robertson Feb 3 at 17:55
  • 3
    Such buffers don't just magically get "generated output": some code must be creating them and putting output into it. What is this code you're using for that? – Stefan Feb 3 at 21:20
  • 1
    @Drew I'm not saying that there is a bug in emacs, the doc says clearly that what i'm trying to do won't work. The auto-mode-alist variable is an AssociationList that associates MajorModes with a pattern to match a buffer filename when it is first opened – mihai Feb 3 at 21:40
  • 1
    @Tobias i'm using cmake-ide and the buffer which captures some output is *cmake* – mihai Feb 4 at 10:11
  • 1
    @mihai: then your question should mention cmake-ide. Also please tell us which command (or Elisp chunk in your .emacs) you're using that ends up creating this buffer. – Stefan Feb 4 at 13:17
1

The general answer to problems of this kind is given by user Lawlist. I consider in the following your special situation.

You stated in a comment that you want to use compilation-mode for cmake-ide.

The following code advises that package for that purpose probably with minimal impact on the package:

(defcustom my-cide-callback-functions
  '(my-cide-compilation-mode-activate)
  "Special hook for cide.
Each function registered here is run like a process sentinel
for the cmake process after cmake has finished."
  :type 'hook
  :group 'cmake-ide)

(defun my-cide-compilation-mode-activate (process _event)
  "Activate `compilation-mode' in PROCESS buffer after compilation.
This function can be used in `my-cide-callback-functions'."
  (let ((buf (process-buffer process)))
    (when (and (null (process-live-p process))
               (buffer-live-p buf))
      (with-current-buffer buf
        (compilation-mode)))))

(defun my-cide--register-a-callback (callback)
  "Register CALLBACK to be called when CMake finishes running."
  (set-process-sentinel
   (get-process "cmake")
   `(lambda (process event)
     (funcall ,callback process event)
     (run-hook-with-args 'my-cide-callback-functions process event))))

(advice-add 'cide--register-a-callback :override #'my-cide--register-a-callback)

Disclaimer: I do not have cmake-ide and do not want to install it if it is not necessary for debugging. Please test the code and report back whether it works or not.

|improve this answer|||||
  • I'm getting the following error @Tobias Debugger entered--Lisp error: (void-variable callback) (funcall callback process event) (lambda (process event) (funcall callback process event) (run-hook-with-args (quote my-cide-callback-functions) process event))(#<process cmake> "finished\n") – mihai Feb 4 at 11:08
  • @mihai Very interesting. I thought it should work because cmake-ide.el uses lexical binding. I've changed the approach a bit. Could you try again? (Back-quoting + substituting by comma-operator) – Tobias Feb 4 at 11:18
  • Works exactly as i expected. – mihai Feb 4 at 11:22
  • @Tobias You won't get a lexical closure unless the file that your code is in uses lexical-binding. I presume that wasn't the case. – phils Feb 4 at 12:13
  • @phils Looking at the code there is -*- lexical-binding: t; -*- in the first comment line. Otherwise I would not have written that this is interesting. Maybe the OP uses an older version of cmake-ide. set-process-sentinel should work with lexical binding, shouldn't it? Do you have opposite experiences? – Tobias Feb 4 at 12:17
2

Instead of running start-process and then calling compilation-mode on the output:

(start-process "result" "*result*" "some_unix_command")

... another option is to use the compile command, which automatically generates the output buffer in compilation-mode:

(compile "some_unix_command")

Alternatively, consider invoking compilation-mode after the process finishes running when using start-process by using a process-sentinel:

https://www.gnu.org/software/emacs/manual/html_node/elisp/Sentinels.html

This following linked thread contains a few examples of using a process sentinel:

https://stackoverflow.com/questions/24548536/penetrating-the-set-process-sentinel-hierarchy-with-let-bound-variables

|improve this answer|||||
  • The actual question here is whether he has control of the code or not and how the environment of his lines looks like. IMHO your answer is a bit too early. – Tobias Feb 3 at 23:32
  • @Tobias -- In the event the O.P. chooses to modify his/her question to elaborate by providing the forum participants with details about a specific library being used by the O.P. that utilizes the example in the question; i.e., (start-process "result" "*result*" "some_unix_command"), then a more precise solution can be suggested. Inasmuch as the question is general in nature, only a general answer is appropriate at this time. – lawlist Feb 3 at 23:42
  • IMHO we are not yet at a point where we can decide whether your solution is applicable. – Tobias Feb 4 at 0:07
  • 1
    Well, only @mihai will be deciding whether this solution is applicable; but regardless this answer may well prove beneficial to other people looking at the Q&A, so I think it's a useful answer to include, even if the question currently remains somewhat unclear. – phils Feb 4 at 2:39
  • I've indicated in my answer that you described the general approach (+1). – Tobias Feb 4 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.