emacs 写C程序问题,几个实用的function

Vim、Emacs配置和使用
回复
xxdragen
帖子: 21
注册时间: 2007-12-11 17:34

emacs 写C程序问题,几个实用的function

#1

帖子 xxdragen » 2007-12-25 22:52

大家好,我用emacs写C程序,现在有几个问题:

1 如何高亮显示所有同名局部变量?
2 如果更改一个变量的名称(或者一个struct 里元素的名称),如何让所有的引用都同时更名?(这个好像对struct很困难)
3 怎么format一个C文件? (以前用Eclipse写Java程序的时候可以很简单的format一个Java文件,不知道对Emacs有没有类似的包)

我再贡献我自己配置里面的几个function,挺方便的,大家看看吧。有些我自己写的,但是因为我对Lisp并不是很熟,大家不要见笑。能用就好。

(defun insert-new-line-up ()
"Opens a new line before the current line. Equivalent to beginning-of-line, open-line"
(interactive) (beginning-of-line) (open-line 1) (indent-according-to-mode)
)

(global-set-key [(control shift return)] 'insert-new-line-up)


(defun insert-new-line-down()
"Opens a new line after the current line."

(interactive) (beginning-of-line 2) (open-line 1) (indent-according-to-mode)
)
(global-set-key [(control return)] 'insert-new-line-down)


;;++++++++++++++ scroll one line up and down ++++++++++++++
(defun scroll-up-one-line ()
(interactive)
(scroll-up 1))

(defun scroll-down-one-line ()
(interactive)
(scroll-down 1))

(global-set-key [(control down)] 'scroll-up-one-line)
(global-set-key [(control up)] 'scroll-down-one-line)

这个我忘了下载地址了,大家自己google一下吧。
;go to the last edit point.
(require 'goto-last-change)
(global-set-key [(meta left)] 'goto-last-change)
zhangsong023
帖子: 768
注册时间: 2006-09-20 19:56

#2

帖子 zhangsong023 » 2007-12-27 10:32

1 高亮局部变量名,我写了一个很简单的函数,可以用。
需要highlight库支持,去emacswiki下载highlight.el。

代码: 全选

  
(require 'highlight)
(defun zjs-hl-local-var ()
  (interactive)
  (let ((var_name (current-word t)))
    (when (and var_name
               (not (c-at-toplevel-p)))
      (save-excursion
        (hlt-highlight-regexp-region 
         (progn
           (c-beginning-of-defun)
           (point))
         (progn
           (c-end-of-defun)
           (point))
         (regexp-quote var_name))))))
另外: 高亮同名全局变量应该可以用semantic提供的语法分析器实现,我没试过,但应该不难。
2 xref,支持代码重构。
3 内置:C-x h C-M-\,当然,你也可以写一个函数,调用indent、astyle等外部工具来做。
xxdragen
帖子: 21
注册时间: 2007-12-11 17:34

#3

帖子 xxdragen » 2007-12-27 14:50

多谢,多谢。
xxdragen
帖子: 21
注册时间: 2007-12-11 17:34

#4

帖子 xxdragen » 2007-12-31 7:44

请问,你的函数怎么变一下能够高亮光标所在的局部变量,而不需要按任何键?
zhangsong023
帖子: 768
注册时间: 2006-09-20 19:56

#5

帖子 zhangsong023 » 2007-12-31 14:48

给你提供一个思路,具体的改动自己写吧,不难。

Emacs中有一个在空闲时刻定时执行函数的函数:

代码: 全选

run-with-idle-timer
利用它,可以很容易实现高亮光标下的变量。

当然,如果你有更好的思路,欢迎拍砖。
回复