Back to emacs
News, Technical, Work ·Saturday February 5, 2011 @ 16:59 EST (link)
After frustration with the limited programmability of my work editor (Source Insight) and its custom macro language (read: wheel reinvention with a square wheel, and what's now a dead language; they don't seem to be updating the editor any more), and dissatisfaction with vi as insufficiently GUI-integrated and powerful, I went through the basic Emacs tutorial (C-h t) and then through the online help built the macro I wanted to covert a reference (or local) variable to a pointer, binding it to a key:
(require 'cl) ; for flet
(defun replace-region (match replace)
(beginning-of-buffer)
(while (re-search-forward match nil t)
(replace-match replace nil nil))
)
(defun ref-to-pointer (beg end) (interactive "r")
(let ((old-var (read-string "Reference to replace? " "rt" nil "rt")))
(save-excursion
(undo-boundary)
(flet ((undo-boundary () nil))
(narrow-to-region beg end)
(replace-region (concat "\\b" old-var "\\.") (concat "p" old-var "->"))
(replace-region (concat "\\b" old-var "\\b") (concat "*p" old-var))
(widen)
)
)
(undo-boundary)
)
)
(global-set-key (kbd "<f11>") 'ref-to-pointer)
I originally used replace-regexp rather than the loop in replace-region, but the help for replace-regexp itself suggested the new method. Out of the box, Emacs (EmacsW32) has source code highlighting, which remains perhaps to customize, and tagging seems fairly simple to set up (and EmacsW32 comes with etags). One feature I do like from Source Insight is that files added to the current project are automatically prefix-matched on open, and functions can be tagged by Hungarian "syllable" (e.g., Foo can be found not only in FooFromBar but also ). But I suspect these can be overcome. Another command I'd like to write is one that reads an error output line and jumps to the location in the file (maybe even with next/previous commands). I suspect this already exists and just needs some tweaking to work in .err files.
Other notes: after a replace, the mark deactivates which hides the highlight (apparently the only way to bring it back is to do an exchange-point-and-mark) but it's still there, and it does get adjusted if text is inserted before it. The business with flet and undo-boundary make the action atomic for purposes of undo. Narrowing ensure that changes only occur within our region of interest (which is why beginning-of-buffer is safe, and it's helpful for regions defined by point being after mark: although beg is always ≤ end, it doesn't actually move point for the purposes of re-search-forward).
Books finished: The Woods Out Back, A People'S History of the United States, Passage To Dawn.