LINUX.ORG.RU

Как это делать в Emacs?


0

0

Необходимо, чтобы при наборе, к примеру, if, while, и т. д. в c-mode автоматически дописывались скобки и курсор устанавливался между ними:

if (_)

while (_)

...

(тут _ - курсор)
Как это сделать?

anonymous

Посмотри в сторону define-skeleton. Тебе понравится.

Sectoid ★★★★★
()

(define-skeleton c++-for-stmt 
  "Abbrev for for statement"
  nil
  > "for( "_";"_";"_" )"
  \n > "{" _ "}"
)

(define-skeleton c++-while-stmt 
  "Abbrev for while statement"
  nil
  > "while( "_" )"
  \n > "{" _ "}"
)

(define-skeleton c++-switch-stmt 
  "Abbrev for switch statement"
  nil
  > "switch( "_" )"
  \n > "{"
  \n > "case " _ ":" _ "break;"
  \n > "case " _ ":" _ "break;"
  \n > "default :" _ "break;"
  \n > "}"
)

(define-skeleton c++-if-stmt
  "Abbrev for if statement"
  nil
  > "if ( "_" )"
  \n > "{" _ "}"
)

(define-skeleton c++-ifelse-stmt
  "Abbrev for if ... else  statement"
  nil
  > "if ( "_" )"
  \n > "{" _ "}"
  \n > "else"
  \n > "{" _ "}"
)

(define-skeleton c++-class-stmt
  "Abbrev for class  statement"
  nil
  > "class " _
  \n > "{" _ "};"
)

(define-skeleton c++-struct-stmt
  "Abbrev for class  statement"
  nil
  > "struct " _
  \n > "{" _ "};"
)

(add-hook 'c++-mode-hook 
	  (function (lambda () 
		      (progn 

			(define-abbrev c++-mode-abbrev-table "forr"    ""  `c++-for-stmt)
			(define-abbrev c++-mode-abbrev-table "whilee"  ""  `c++-while-stmt)
			(define-abbrev c++-mode-abbrev-table "switchh" ""  `c++-struct-stmt)
			(define-abbrev c++-mode-abbrev-table "iff"     ""  `c++-if-stmt)	    
			(define-abbrev c++-mode-abbrev-table "ifelse"  ""  `c++-ifelse-stmt)
			(define-abbrev c++-mode-abbrev-table "classs"  ""  `c++-class-stmt)))))

За правильность расставления скобок и вообще корректность последней формы не ручаюсь :) 

Burbaka ★★
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.