Hot answers tagged

14

One fairly common way of restricting the action of a hook to a particular major mode is to check the value of major-mode. For example, this hook prints a message before saving buffers using c++-mode: (defun my-c++-mode-before-save-hook () (when (eq major-mode 'c++-mode) (message "It's never too early to start saving (C++ code)!"))) (add-hook 'before-...


9

UPDATE: That pretest version that I put down there in the original question (emacs-26.0.90.tar.gz) had a bug that cause certain infinity loops while parsing the buffer, specifically when using the < token in certaing contexts, like writting < as the first character of an empty buffer in Java or C++ or after the keyword template. The bug has been fixed ...


8

Your question appears to be dealing with two independent issues: you want the RET character to automatically indent; and you want an indentation style that is different from the Emacs default. Electric RET A character that performs something else than just inserting itself is called electric in Emacs. There are two ways in Emacs to make RET electric: the ...


7

smartparens, a solution for advanced handling of pairs (such as (), [], {}, <>, ...) supports pre- and post-action hooks. With these it's possible to make Emacs indent the content of a {} pair after hitting RET after the beginning {, the } is inserted at the same time as the {. The following snippet is taken from the documentation and sets up the ...


7

This can be done by changing comment-start & comment-end. (add-hook 'c-mode-hook (lambda () (setq comment-start "//" comment-end ""))) Found the answer after further searching here


6

One option is to add "auto" to extra types recognized by c++-mode: (require 'cc-mode) (add-to-list 'c++-font-lock-extra-types "auto") The downside is that now it is not highlighted as a keyword any more. (But "auto" is both a type and a keyword, in a way.)


5

M-xcustomize-variableRETgrep-files-aliases and then use the interface to change the value of this variable. Alternatively, you could put: (setf (cdr (assoc "cc" grep-files-aliases)) (assoc "cchh" grep-files-aliases)) For future reference, you could have looked up the help for rgrep (by typing C-h frgrep) where it says: Recursively grep for REGEXP ...


4

You want to do: (add-hook 'c-mode-hook (lambda () (setq-local electric-indent-chars (remq ?\n electric-indent-chars))))


4

You could rebind RET to electric-indent-just-newline like this: (add-hook 'c-mode-hook (lambda() (local-set-key (kbd "<RET>") 'electric-indent-just-newline)))


4

This is not necessarily a direct answers to what you should put in your .emacs file but rather a more generic way to quickly figure out the parameters for the style you've chosen. cc-mode provide several functions to directly guess the right indentation parameters. They are aptly prefixed with c-guess-. For this case in particular you could use the ...


4

You can add a function for that to arglist-cont-nonempty. In the following example my-c-lineup-arglist-lambda should do what you want: (c-add-style "work" '((indent-tabs-mode . nil) (c-basic-offset . 4) (c-offsets-alist (substatement-open . 0) ...


3

After fiddling a bit longer with this, I think I figured it out. Adding the following snippet to my CC-mode init-configuration fixed this: (defun my-cc-init-hook () "Initialization hook for CC-mode runs before any other hooks." (setq c-doc-comment-style '((java-mode . javadoc) (pike-mode . autodoc) (c-mode . javadoc) (c++-mode ....


3

You can use describe-char or C-u C-x = (= what-cursor-position with a prefix arg) to obtain a buffer giving as much information as possible about the character at point : font, syntax, properties and, of course, faces. If not too unlucky, the name of the face will give you the library name that added it.


3

The incantation you want is: (font-lock-add-keywords 'c++-mode '(("constexpr" . 'font-lock-keyword-face)))


3

I asked this question in Xah Lee emacs blog entry, and he gave me these suggestions: i think it can be done, without much trouble. Read the elisp chapter on syntax table. basically, add the -- to the syntax table as comment. Note: syntax table is hard to work with, especially when specifying comment chars. Also, by default it only supports a few. I'...


3

Thanks to the University of Wyoming I have been able to achieve the result with: ;; Make M-x compile smarter in order to guess language (require 'compile) (defvar compile-guess-command-table '((c-mode . "gcc -Wall -g %s -o %s -lm") (c++-mode . "g++ -Wall %s -o %s -std=c++14") (fortran-mode . "gfortran -C %s -o %s") )) (defun compile-...


2

This looks like a variant of Emacs bug #18745 (really a bug of Windows' CreateProcess() function and/or cmd.exe), apparently it applies to .exes when called via the shell. It works if you don't quote the second argument: (concat (shell-quote-argument msbuild) " " proj-file " /p:Configuration=Debug") Or use the short file name instead of quoting the exe: (...


2

You could put this in dotspacemacs/config: (global-linum-mode) (with-eval-after-load 'linum (linum-relative-toggle)) The relative number line will be by default.


2

If it happens with emacs -Q, file a bug report with M-x bug-report. If you verify that this doesn't happen on Emacs 24.4, you can even: Send an email to the dev list. It's likely that the person who inadvertently introduced the bug will immediately know what's happening. Open the ChangeLog file and search for changes to the affected package. There should be ...


2

The Ruby editing mode defines ruby-indent-tabs-mode and sets indent-tabs-mode to that value when the mode is invoked. The default for this is nil. To fix your problem, set this variable instead. This seems like a bug to me. There's no reason Ruby needs its own special setting for this. Note also that c-basic-offset doesn't affect Ruby. Ruby has ruby-...


2

company-mode is just a framework. If you want completions from company-clang, try adding your major mode to company-clang-modes. But if you only created a new major mode for minor reasons like new font-lock keywords, maybe you would be better served by moving that logic to a new minor mode instead (and adding it to c++-mode-hook).


2

I saw that you already solved this, but I thought that I could share the following anyway: You can use Font-lock studio (an interactive debugger for font-lock keywords) to single step each font-lock rule using the n command. That way, you can easily see which one caused the problem. If you are lucky and the rule contains a function call (instead of a plain ...


2

How embarrassing. A few minutes after asking the question, the solution came to me. I might make the case that the asking of the question led to the solution, though. In any case, the answer that came to me is that I should find the face description using a character in the pinkish text. This led to using M-x describe-face which put a query into the ...


2

Font-lock use two phases: The syntactic phase. In this phase, Emacs determines things like strings and comments. The keyword phase, i.e. the normal font-lock rules. If you want to make Emacs ignore single quotes in digits, you have to do this in the first phase. Effectively, you will have to supply a custom syntax-propertize-function, which should set a ...


2

In some recent versions of Emacs, there's actually a new syntactic element available to handle lambdas: inlambda. This might be a bit clearner to use rather than a custom specifier function. As an example, I believe the following snippet would implement your desired lambda indentation: (defconst my-c++-style '("stroustrup" (c-basic-offset . 4) (...


2

I guess one way is to simply unmap the undesired keys from the mode's keymap: (eval-when-compile (require 'cc-mode)) (defun my-c-mode-hook () "My `c-mode' hook." (define-key c-mode-map (kbd ",") nil) (define-key c-mode-map (kbd "(") nil) (define-key c-mode-map (kbd ")") nil)) (add-hook 'c-mode-hook 'my-c-mode-hook)


2

You could make c-mode follow your style like this: (defun lineup-if-with-else (context) (pcase context (`(substatement . ,anchor) (save-excursion (back-to-indentation) (when (looking-at-p "if\\_>") (goto-char anchor) (when (looking-at-p "else\\_>") 0)))))) (defun my-c-mode-hook () "My `c-mode' ...


2

The usual way to check if you are in a comment is to use syntax-ppss. A simple way to do what you want would be something like: (defun my/c-indent-or-tab-in-comment () (interactive) (if (nth 4 (syntax-ppss)) (insert "\t") (call-interactively 'indent-for-tab-command))) (define-key c-mode-map (kbd "TAB") #'my/c-indent-or-tab-in-comment) Some ...


Only top voted, non community-wiki answers of a minimum length are eligible