-1

I am currently learning C++ and I am in the process of configuring Emacs as my C++ IDE. I am reading a PDF guide and copying code into my Emacs buffer. However upon pasting the code the formatting is incorrect which is what I expected.

For example:

  int count_x(char
∗
p, char x)
//
count the number of occurrences of x in p[]
//
p is assumed to point to a zero-ter minated array of char (or to nothing)
{
if (p==nullptr) return 0;
int count = 0;
for (;
∗
p!=0; ++p)
if (
∗
p==x)
++count;
return count;
}

How can I reformat the above code to look like this:

int count_x(char* p, char x)
      // count the number of occurrences of x in p[]
      // p is assumed to point to a zero-terminated array of char (or to nothing)
{
    if(p==nullptr) return 0;
    int count = 0;
    for(; *p!=0; ++p)
     if(*p==x)
          ++count;
    return count;
}

Thanks!

  • If there are really newlines after //, then there's no reliable way to tell what should be in the comment. You can fix the indentation by selecting everything and pressing tab, but it won't fix the extra newlines after the //'s or around the *'s. – Qudit Jul 14 '17 at 1:46
  • @Qudit Thanks for the suggestion. However what you have suggested just indents the code and doesn't solve my issue. Thanks anyway. – Us3rname Jul 14 '17 at 11:16
  • Downvoting my question is cool but only with an explanation. Why downvote? Thanks – Us3rname Jul 15 '17 at 9:05
2

The code you posted has several issues which needs to be fixed.
I will explain a semiautomatic method. This will enable you to fix code which has different issues, with the same method.

  1. This code is no valid c source code:
    1. the obvious: comment lines are not marked as comments. This is fixed easy with search and replace.
      Type: M-x query-replace-regexp RET // C-q C-j RET // RET !.
      Explanation: with this input you replace the string "//\n" with "//", eventually putting comment and comment markers on the same line.
      C-q means: press Control and q this quotes the next character. C-j means: insert a linefeed character.
    2. The not so obvious issue: your * is not a asterisk but some unicode character which a c-compiler does not recognize. Go with your cursor on an * and enter M-x describe-char. You have to replace this unicode-* with an ascii-*. To do this (and also, remove the newlines before and after it), do the following:
      • highlight (i.e.: mark, select) just the * in your code and copy it to kill ring with M-w.
      • then type: M-x query-replace-regexp RET C-q C-j \ C-y C-q C-j RET * RET !
  2. now you have valid c-source code and you can use an automatic tool to prettify your code. The tool I describe here is clang-format.
    1. install the software clang-format on your computer (with Linux (Debian and Ubuntu like distributions) it is done with: sudo apt-get install clang-format)
    2. Install the package clang-format from melpa.
      More documentation about package management in Emacs here.
    3. Insert following line into your emacs configuration (e.g. init.el):
      (require 'clang-format) For further configuration of clang-format see here and here.
    4. Use M-x clang-format-buffer to prettify the whole code.

If you copy code from your book again, then verify it is valid c-source code. If not, then use steps from 1. to get valid c-source code.
Afterwards you only need to do step 2.4. to prettify your code.

Steps from 1. could be automated, but your problem is very special.

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.