<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>coder . cl &#187; emacs</title>
	<atom:link href="http://coder.cl/category/programming/emacs/feed/" rel="self" type="application/rss+xml" />
	<link>http://coder.cl</link>
	<description>system programmer &#38; web developer</description>
	<lastBuildDate>Thu, 29 Jul 2010 21:52:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>emacs and integrating ecb with cscope</title>
		<link>http://coder.cl/2009/08/emacs-and-integrating-ecb-with-cscope/</link>
		<comments>http://coder.cl/2009/08/emacs-and-integrating-ecb-with-cscope/#comments</comments>
		<pubDate>Sat, 01 Aug 2009 17:54:45 +0000</pubDate>
		<dc:creator>Daniel Molina Wegener</dc:creator>
				<category><![CDATA[c]]></category>
		<category><![CDATA[emacs]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=85</guid>
		<description><![CDATA[Emacs is a powerful editor, the most powerful and the fastest programmable editor that I know. Emacs uses a Lisp dialect to create configurations, functions and modes. My favorite mode on Emacs is ECB, a powerful code browsing one, like many IDEs that I know. The difference is that ECB, do not does code indexing [...]]]></description>
			<content:encoded><![CDATA[<p><!--:en-->
<p><a href="http://www.gnu.org/software/emacs/">Emacs</a> is a powerful editor, the most powerful and the fastest <i>programmable editor</i> that I know. Emacs uses a <a href="http://en.wikipedia.org/wiki/Lisp">Lisp</a> dialect to create configurations, functions and <a href="http://tldp.org/HOWTO/Emacs-Beginner-HOWTO-3.html">modes</a>. My favorite mode on Emacs is <a href="http://ecb.sourceforge.net/">ECB</a>, a powerful code browsing one, like many IDEs that I know. The difference is that ECB, do not does code indexing until you configure it. For large C projects, I like to use external tools, but not <i>ctags</i> or <i>etags</i>, instead I prefer to use <a href="http://cscope.sourceforge.net/">cscope</a>. But you may ask <i>&quot;how can I integrate cscope and Emacs running the ECB mode?&quot;</i>. Well, in this post I will try to explain how to integrate both, cscope and Emacs.</p>
<p><!--:--><span id="more-85"></span><!--:en--></p>
<p>To know more about <a href="http://ecb.sourceforge.net/">ECB</a>, <a href="http://cedet.sourceforge.net/">CEDET</a> and related Emacs modes you can look some articles on the Internet, such as <a href="http://xtalk.msk.su/~ott/en/writings/emacs-devenv/EmacsCedet.html">&quot;A Gentle introduction to Cedet&quot;</a>, by Alex Ott. All of them would help a lot on configuring both, Emacs and ECB. Also the CEDET &mdash; the base package for ECB &mdash; and ECB itself are well documented.</p>
<p>The cscope mode that I&#8217;m using is the cscope mode contributed to the cscope source tree. It&#8217;s called <i>xcscope.el</i> and comes with a small cscope indexing script called <i>cscope-indexer</i>.</p>
<p>On ECB there is a <i>hook</i> that runs on every time that you change your working directory by using the directory navigator. The hook is called <i>ecb-after-directory-change-hook</i>. For my C and C++ projects I use various indentation settings, depending on the project that I&#8217;m working on. Then, I&#8217;ve created a <i>hook function</i> to <i>ecb-after-directory-change-hook</i> that sets the indenting style depending on the project that I&#8217;m working on. I&#8217;ve created the <i>dmw-ecb-directory-change-alist</i> variable, and it holds the project path and the indenting style to be applied to the project.</p>
<pre name="code" class="lisp" cols="80" rows="43">
(defvar dmw-ecb-directory-change-alist
  '((&quot;/work/dmw/c/caffeine&quot; caffeine-c-mode-hook)
    (&quot;/work/dmw/c/pyxser&quot; python-c-mode-hook)
    (&quot;/work/dmw/cxx/caffeine++&quot; caffeine-c++-mode-hook)
    (&quot;/work/dmw/cxx/qstats&quot; caffeine-c++-mode-hook)
    (&quot;/usr/src/sys&quot; knf)
    (&quot;/current/usr/src/sys&quot; knf)))
</pre>
<p></p>
<p>Then, I&#8217;ve created the hook to apply the indenting style.</p>
<pre name="code" class="lisp" cols="80" rows="43">
(defun dmw-ecb-directory-change-hook (dirold dirnew)
  (dolist (prji dmw-ecb-directory-change-alist)
    (let ((prj-name (first prji))
          (prj-match (string-match (concat (first prji) &quot;.*&quot;) dirnew)))
      (if (numberp prj-match)
          (progn
            ;; run hooks and functions related to
            ;; relative path matching on directory browsing
            (if (&gt;= prj-match 0)
                (dolist (h (last prji))
                  (run-hook-with-args h)))
            ;; run hooks and functions related to
            ;; full path matching on directory browsing
            (if (string= prj-name dirnew)
                (let ((newpath (concat dirnew &quot;/cscope.out&quot;))
                      (dirnew dirnew))
                  (progn
                    (setq cscope-database-file newpath
                          cscope-do-not-update-database nil
                          cscope-use-relative-path t
                          cscope-initial-directory dirnew)
                    (cscope-index-files dirnew))))
            )))))
</pre>
<p></p>
<p>What does this hook? See the documentation for <i>ecb-after-directory-change-hook</i>. On every directory change on the directory browsing window, the hook runs passing the old directory and the new directory as it&#8217;s arguments.</p>
<blockquote><p>
Hook which run directly after the selected directory has changed. This means not only after a click onto a directory in the directory-window of ECB but it means this hook runs always when the current directory changes regardless of the trigger of this change[...]
</p></blockquote>
<p>Then, when the hook matches my C and C++ projects, it pass by the <i>(if (>= prj-match 0)</i> expression and setup the indenting style configured in <i>dmw-ecb-directory-change-alist</i>. If the directory pass by the <i>(if (string= prj-name dirnew)</i> expression, then it runs the block related to the new directory bellow the if sentence. What it does? It sets the <i>cscope-database-file</i> variable to the <i>newpath</i> local variable, which have the value of the new directory concatenated with <i>&quot;/cscope.out&quot;</i>, also set some variable to ensure that the cscope database is updated, to enable cscope to use relative paths &mdash; this enables that cscope can lookup on different directories if the path changes &mdash; and set the initial directory to the new directory. Then, it runs the <i>cscope-indexer</i>. In few words, this hooks refresh the cscope database, and sets a permanent database location relative to the project that I&#8217;m working on.</p>
<p>Then, somewhere in your <i>.emacs</i> file, you should put your ECB variables as follows.</p>
<pre name="code" class="lisp" cols="80" rows="43">
(add-hook 'ecb-after-directory-change-hook
          'dmw-ecb-directory-change-hook)
</pre>
<p></p>
<p>Then, just setup the keybinding to use the cscope mode &mdash; I do a lot of C programming under emacs, that&#8217;s the reason that I&#8217;ve configured the keybinding as global, and not local to the C programming mode. Those keybindings bellow are a little bit tricky, you can search for C symbols just by typing <i>Ctrl + Meta + Down</i>. <img src='http://coder.cl/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<pre name="code" class="lisp" cols="80" rows="43">
;; cscope keys
(define-key global-map [(control meta s)] 'cscope-set-initial-directory)
(define-key global-map [(control meta u)] 'cscope-unset-initial-directory)
(define-key global-map [(control meta f)] 'cscope-find-this-symbol)
(define-key global-map [(control meta g)] 'cscope-find-global-definition)
(define-key global-map [(control meta x)] 'cscope-find-global-definition-no-prompting)
(define-key global-map [(control meta m)] 'cscope-pop-mark)
(define-key global-map [(control meta n)] 'cscope-next-symbol)
(define-key global-map [(control meta N)] 'cscope-next-file)
(define-key global-map [(control meta p)] 'cscope-prev-symbol)
(define-key global-map [(control meta P)] 'cscope-prev-file)
(define-key global-map [(control meta c)] 'cscope-display-buffer)
(define-key global-map [(control meta C)] 'cscope-display-buffer-toggle)
</pre>
<p>I prefer <i>cscope</i> since it have the capability of searching symbols on many ways that other code indexer do not do, and large IDEs like eclipse with CDT gets leaked with large projects like the kernel sources &mdash; CDT takes just a few <i>hours</i> to index the complete kernel! &mdash; and since cscope is purely made in C and runs really fast.</p>
<p><!--:--></p>



share this article at: 


	<a rel="nofollow"  target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fcoder.cl%2F2009%2F08%2Femacs-and-integrating-ecb-with-cscope%2F&amp;title=emacs%20and%20integrating%20ecb%20with%20cscope&amp;source=coder+.+cl+system+programmer+%26amp%3B+web+developer&amp;summary=Emacs%20is%20a%20powerful%20editor%2C%20the%20most%20powerful%20and%20the%20fastest%20programmable%20editor%20that%20I%20know.%20Emacs%20uses%20a%20Lisp%20dialect%20to%20create%20configurations%2C%20functions%20and%20modes.%20My%20favorite%20mode%20on%20Emacs%20is%20ECB%2C%20a%20powerful%20code%20browsing%20one%2C%20like%20many%20IDEs%20tha" title="LinkedIn"><img src="http://coder.cl/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fcoder.cl%2F2009%2F08%2Femacs-and-integrating-ecb-with-cscope%2F&amp;title=emacs%20and%20integrating%20ecb%20with%20cscope&amp;notes=Emacs%20is%20a%20powerful%20editor%2C%20the%20most%20powerful%20and%20the%20fastest%20programmable%20editor%20that%20I%20know.%20Emacs%20uses%20a%20Lisp%20dialect%20to%20create%20configurations%2C%20functions%20and%20modes.%20My%20favorite%20mode%20on%20Emacs%20is%20ECB%2C%20a%20powerful%20code%20browsing%20one%2C%20like%20many%20IDEs%20tha" title="del.icio.us"><img src="http://coder.cl/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fcoder.cl%2F2009%2F08%2Femacs-and-integrating-ecb-with-cscope%2F&amp;title=emacs%20and%20integrating%20ecb%20with%20cscope&amp;bodytext=Emacs%20is%20a%20powerful%20editor%2C%20the%20most%20powerful%20and%20the%20fastest%20programmable%20editor%20that%20I%20know.%20Emacs%20uses%20a%20Lisp%20dialect%20to%20create%20configurations%2C%20functions%20and%20modes.%20My%20favorite%20mode%20on%20Emacs%20is%20ECB%2C%20a%20powerful%20code%20browsing%20one%2C%20like%20many%20IDEs%20tha" title="Digg"><img src="http://coder.cl/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fcoder.cl%2F2009%2F08%2Femacs-and-integrating-ecb-with-cscope%2F&amp;title=emacs%20and%20integrating%20ecb%20with%20cscope" title="Reddit"><img src="http://coder.cl/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fcoder.cl%2F2009%2F08%2Femacs-and-integrating-ecb-with-cscope%2F&amp;t=emacs%20and%20integrating%20ecb%20with%20cscope" title="Facebook"><img src="http://coder.cl/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=emacs%20and%20integrating%20ecb%20with%20cscope%20-%20http%3A%2F%2Fcoder.cl%2F2009%2F08%2Femacs-and-integrating-ecb-with-cscope%2F" title="Twitter"><img src="http://coder.cl/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fcoder.cl%2F2009%2F08%2Femacs-and-integrating-ecb-with-cscope%2F&amp;title=emacs%20and%20integrating%20ecb%20with%20cscope&amp;annotation=Emacs%20is%20a%20powerful%20editor%2C%20the%20most%20powerful%20and%20the%20fastest%20programmable%20editor%20that%20I%20know.%20Emacs%20uses%20a%20Lisp%20dialect%20to%20create%20configurations%2C%20functions%20and%20modes.%20My%20favorite%20mode%20on%20Emacs%20is%20ECB%2C%20a%20powerful%20code%20browsing%20one%2C%20like%20many%20IDEs%20tha" title="Google Bookmarks"><img src="http://coder.cl/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>


<br/><br/><br/><hr height="1px" width="50%" />
<div style='text-align: center !important;'><b>Copyright © 2009 Daniel Molina Wegener</b><br/><b>Atribución-No Comercial-Sin Derivadas 2.0 Chile</b><br/><a target='_new' rel="license" href="http://creativecommons.org/licenses/by-nc-nd/2.0/cl/"><img alt="Creative Commons License" style="border-width:0" src="/cc88x31.png" /></a></div>
<br/><hr height="1px" width="100%" />
<p><small>© Daniel Molina Wegener for <a href="http://coder.cl">coder . cl</a>, 2009. | <a href="http://coder.cl/2009/08/emacs-and-integrating-ecb-with-cscope/">Permalink</a> | <a href="http://coder.cl/2009/08/emacs-and-integrating-ecb-with-cscope/#comments">No comment</a><br/>Post tags: <br/></small></p>
]]></content:encoded>
			<wfw:commentRss>http://coder.cl/2009/08/emacs-and-integrating-ecb-with-cscope/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
