CcMode

CC Mode is a powerful package that provides modes for editing C and C-like (C++, Java, Objective C, CORBA IDL, etc.) files. For much more information (including an InfoMode manual), see the CC Mode homepage:

Modes provided by CC Mode:

Extensions to CC Mode:

Using different styles according to source location:

This could probably do with some modifications to support more than one style type. C++ code is also not affected.

  (setq my-linux-style-path-alist (list (expand-file-name
                               "~/src/version_control/sexy")))
  (add-hook 'c-mode-hook (lambda ()
                           (dolist (path my-linux-style-path-alist)
                             (if (string-match path (buffer-file-name))
                                 (c-set-style "linux")))))

I do it like:

(setq artm-c-styles-alist
      '(("/projects/vlc" . "vlc")
	(nil . "linux")))

(defun artm-guess-c-style ()
  (let ((style
	 (assoc-default buffer-file-name artm-c-styles-alist
			(lambda (pattern path)
			  (or (not pattern)
			      (and (stringp path)
				   (string-match pattern path))))
			;; factory default (in case you forget
			;; to add (nil . "some style") to
			;; artm-c-styles-alist
			'(nil . "linux"))))
    (cond
     ((stringp style) (c-set-style style))
     ((functionp style) (style)))))

(add-hook 
 'c-mode-hook 'artm-guess-c-style)

Refactoring C code:

(defun my-move-function-up ()
  "Move current function up."
  (interactive)
  (save-excursion
    (c-mark-function)
    (let ((fun-beg (point))
          (fun-end (mark)))
      (transpose-regions (progn
                           (c-beginning-of-defun 1)
                           (point))
                         (progn
                           (c-end-of-defun 1)
                           (point))
                         fun-beg fun-end))))

(defun my-move-function-down ()
  "Move current function down."
  (interactive)
  (save-excursion
    (c-mark-function)
    (let ((fun-beg (point))
          (fun-end (mark)))
      (transpose-regions fun-beg fun-end
                         (progn
                           (c-beginning-of-defun -1)
                           (point))
                         (progn
                           (c-end-of-defun 1)
                           (point))))))
(defun mark-c-scope-beg ()
  "Marks the c-scope (region between {}) enclosing the point. 
   Naive, as will be confused by { } within strings"
  (let 
	  ((scope-depth 1))
	(while (not (= scope-depth 0))
	  (search-backward-regexp "}\\|{")
	  (if (string= (char-to-string (char-after)) "}")
		  (setq scope-depth (1+ scope-depth))
			(setq scope-depth (1- scope-depth)))))
  (point))

(defun mark-c-scope-end ()
  "Marks the c-scope (region between {}) enclosing the point. 
   Naive, as will be confused by { } within strings"
  (let 
	  ((scope-depth 1))
	(while (not (= scope-depth 0))
	  (search-forward-regexp "}\\|{")
	  (if (string= (char-to-string (char-before)) "}")
		  (setq scope-depth (1- scope-depth))
			(setq scope-depth (1+ scope-depth)))))
  (point))

(defun kill-c-scope ()
  (interactive)
  (let
	  ((inital-point (point)))
	(save-excursion 
	  (let
		((beg (mark-c-scope-beg)))
		(goto-char inital-point)
		(let ((end (mark-c-scope-end))))))))

There must be a better way to do this…

There are c-beginning-of-defun and c-end-of-defun, plus c-mark-function in cc-cmds.el – that might be it!


CategoryProgramming CategoryModes