11

I recently started using C++17, which brings constexpr if. This however screws up c++-modes default indentation.

// emacs 25.3.2 with --no-init
if constexpr (n == 1) {
    // Why?
  } else if constexpr (n == 2) {
    // This is a mess...
  } else {
  // and it keeps getting weird
}

Does anyone either

  1. Know of a simpler alternative to cc-mode?
  2. Know how i can fix this particular issue?
0

1 Answer 1

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 in the emacs-26 branch, so that is what you have to download instead.


It took a lot to me to find the answer and learning how to property customize the behaviour in the elisp language but, a way of fixing that is by telling to the cc-mode that constexpr is a "noise name", which means that it will be treated as a whitespace and thus ignored by the parser.

The thing is that in the last stable release of emacs (25.3), the feature of adding noise words is not added, even when it is in the repo since February 2016, so you have to download the a non-release version.

cd tmp_folder_of_your_choice

sudo apt-get install build-essential # if you don't have it yet
sudo apt-get build-dep emacs24 # get all dependancies to build emacs

# Not this: wget https://github.com/emacs-mirror/emacs/archive/emacs-26.0.90.tar.gz

wget -O emacs-26.zip https://github.com/emacs-mirror/emacs/archive/e8636ac8cc96e1e7e948f04091792da09dafcc76.zip

# or just the last version of the branch, but the exact version I have is the one above
# wget https://github.com/emacs-mirror/emacs/archive/emacs-26.zip

unzip emacs-26.zip
cd emacs-emacs-26

./autogen.sh
./configure
make
sudo make install
emacs ~/.emacs

And then, for example at the end of the .emacs file, add:

; Load the mode before-hand to make the symbols visible
(require 'cc-mode)
(custom-set-variables '(c-noise-macro-names '("constexpr")))
4
  • while this is great, and im trying it out now, it still kinda feels like a hack - is there a better way to add it in just that one syntactic position?
    – topisani
    Commented Oct 25, 2017 at 15:46
  • I don't know, but this solution works perfectly for me.
    – ABu
    Commented Oct 25, 2017 at 16:12
  • yeah, it does work for me, with this awesome project as well as the latest emacs git
    – topisani
    Commented Oct 25, 2017 at 18:28
  • @topisani Please, could you see that question if the same happens to you after installing the last emacs version? emacs.stackexchange.com/q/36423/14016
    – ABu
    Commented Oct 26, 2017 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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