emacs - 用注释替换C ++注释(//)(/ * * /)

时间:2011-12-01 15:31:41

标签: emacs

有没有简单的方法可以在emacs中用C注释(//)替换C ++注释(/* */)?

是否有人写过elisp函数来执行此操作?

或者可以应用某种聪明的正则表达式吗?

2 个答案:

答案 0 :(得分:5)

使用query-replace-regex或类似功能,使用以下搜索字符串:^\(.*?\)//\(.*\)$并跟随替换字符串\1/* \2 */

答案 1 :(得分:2)

此C程序使用'/ * ... * /'将文件sample.txt中所有连续出现的'//'注释转换为output.txt。

#include <stdio.h>
#include<string.h>
void trimLeading(char * string);
int main(int argc, char* argv[])
{

FILE* file = fopen("sample.txt", "r");
FILE* fp = fopen("output.txt", "w"); //to create file if it does not exist and to empty file if it exists.
fclose(fp);
if(file==NULL) {
perror("Error opening file sample.txt.");
}
if(fp==NULL) {
perror("Error opening file output.txt.");
}
/* should check the result */
long pos;
char line[256];
char buff[256];
int flag=0;     //flag=1,when multi-line comment occurs in input file.
int line_length=0;

while (fgets(line, sizeof(line), file)) {

      trimLeading(line);
      while((strlen(line)==0)){
                    fgets(line,sizeof(line),file);
                    trimLeading(line);

                }
        //line_length=strlen(line);
        //printf("%d",line_length);
    strcpy(buff,line);

    if(strstr(buff,"//")){ 
        trimLeading(buff);
        //printf("\nbuff=%s,line=%s,flag=%d\n",buff,line,flag);
        if((buff[0]=='/')&&(buff[1]=='/')) {

            /*******************/
            if(flag==0){
                /*look at next line*/
                pos = ftell(file);
                fgets(line,sizeof(line),file);
                trimLeading(line);
                //line_length=strlen(line);
                //printf("%d",line_length);
                while((strlen(line)==0)){
                    fgets(line,sizeof(line),file);
                    trimLeading(line);

                }
                fseek( file,pos, SEEK_SET );
                //printf("line==%s,flag=%d",line,flag);
                if((line[0]=='/')&&(line[1]=='/') ){
                    flag=1;
                    buff[0]='/';
                    buff[1]='*';
                    /*write output to file output.txt*/
                    fp = fopen("output.txt", "a");
                    fseek( fp,0, SEEK_END);
                    fprintf(fp, "%s",buff);
                    fclose(fp);
                    /************/
                    printf("%s", buff); 
                } else{
                    /*write output to file output.txt*/
                    fp = fopen("output.txt", "a");
                    fseek( fp,0, SEEK_END);
                    fprintf(fp, "%s",buff);
                    fclose(fp);
                    /************/
                    printf("%s",buff);
                }
            }else if(flag==1){
                    buff[0]='*';
                    buff[1]='*';
                /*look at next line*/
                    pos = ftell(file);
                    fgets(line,sizeof(line),file);
                    trimLeading(line);
                    //line_length=strlen(line);
                    //printf("%d",line_length);
                    while((strlen(line)==0)){
                        fgets(line,sizeof(line),file);
                        trimLeading(line);

                    }
                    fseek( file,pos, SEEK_SET );
                    //printf("line==%s,flag=%d",line,flag);
                    //printf("line[0]=%c,line[1]=%c",line[0],line[1]);
                    if(!((line[0]=='/')&&(line[1]=='/') )){
                        strcat(buff,"*/ \n");
                        flag=0;
                        //printf("\nFLAG1==%d\n",flag);
                    }
                /*write output to file output.txt*/
                    fp = fopen("output.txt", "a");
                    fseek( fp,0, SEEK_END);
                    fprintf(fp, "%s",buff);
                    fclose(fp);
                    /************/
                    printf("%s", buff); 
                }


        } else{
            /*write output to file output.txt*/
                    fp = fopen("output.txt", "a");
                    fseek( fp,0, SEEK_END);
                    fprintf(fp, "%s",buff);
                    fclose(fp); 
                    /************/
            printf("%s",buff);
            flag=0;
            //printf("\nFLAG2==%d\n",flag);
        }

    } else {
            pos = ftell(file);
            trimLeading(line);
            line_length=strlen(line);
            //printf("%d",line_length);
            while((strlen(line)==0)){
                fgets(line,sizeof(line),file);
                    trimLeading(line);

            }
            fseek( file,pos, SEEK_SET );
        //printf("line==%s,flag=%d",line,flag);
        /*write output to file output.txt*/
                    fp = fopen("output.txt", "a");
                    fseek( fp,0, SEEK_END);                     
                    fprintf(fp, "%s",buff);
                    fclose(fp);
                    /************/
        printf("%s",buff);  
        flag=0;
        //printf("\nFLAG3==%d\n",flag);
    }
}



fclose(file);

return 0;
}
void trimLeading(char * string)
{
int lastSpaceIndex, i, j;

lastSpaceIndex = 0;

/* Finds the last index of whitespace character */
while(string[lastSpaceIndex] == ' ' || string[lastSpaceIndex] == '\t' || string[lastSpaceIndex] == '\n')
{
    lastSpaceIndex++;
}


/* Shifts all trailing characters to its left */
i = 0;
while(string[i + lastSpaceIndex] != '\0')
{
    string[i] = string[i + lastSpaceIndex];
    i++;
}
string[i] = '\0'; //Make sure that string is NULL terminated
}

Sample.txt的:

#include<stdio.h>
int main()
{

  int a=10,b=20,c=0;
  //samplea
  //sampleb
  //samplec

          ///////sampled


c=a+b;
//samplee 
 return 0;
}