Posts tagged ‘emacs’

M-x insert-c++-scope

Monday, August 12th, 2013

If you write C++ code like I do, when you add a new method to a class, you:

  1. type the function signature into the declaration in the header file;
  2. copy and paste it into the source file;
  3. either type in the name of the class by hand or hunt around looking for another instance of it to copy and paste in.

I finally got tired of step 3 and wrote a little Emacs Lisp code to semi-automate the process. It scans through the source file looking for things that look like scopes, presents you with a list of them to choose from (in descending order of frequency, so the one you’re most likely to want is first), and then inserts the one you choose.

Here it is:

(require 'ido)

;; Returns a list of all strings of the form "<x>::" in the current
;; buffer, sorted by decreasing frequency.
(defun all-c++-scopes-in-buffer ()
  (let ((scope-alist nil))
    (save-excursion
      (goto-char (point-min))
      (while (re-search-forward "\\w+::" nil t)
        (let* ((scope (match-string-no-properties 0))
               (ass (assoc scope scope-alist))
               (pair (if ass
                         ass
                       (setq scope-alist (cons (cons scope 0) scope-alist))
                       (car scope-alist))))
          (setcdr pair (1+ (cdr pair)))))
      (mapcar 'car (sort scope-alist (lambda (x y) (> (cdr x) (cdr y))))))))

(defun insert-c++-scope ()
  (interactive)
  (save-excursion
    (insert
     (ido-completing-read "Insert scope: " (all-c++-scopes-in-buffer)))))

If you don’t use ido, then 1) you are nuts, and 2) you can just use completing-read instead of ido-completing-read.

Of course, I have this bound to a key rather than type M-x insert-c++-scope every time.

My Emacs Lisp is rusty, so I imagine that there’s a more idiomatic way to do this (no convenience function to look something up in an alist and automatically add a default pair to it if it’s not already there? really?), but it works, and makes the experience of writing C++ in Emacs a little more pleasant.

Emacs: dedicated windows

Thursday, February 19th, 2009

Here’s something I had been wanting to do for a long time and could never find the right docs for.

When you perform some operation in Emacs, it often puts stuff (search results, some new buffer, etc.) in a window other than the current one.  (I’m using window in the Emacs sense, to mean a portion of what you might call a window but it calls a frame.)  But I have a huge Emacs frame, and always want to keep some buffers present in particular positions while rotating the rest of it around.  In my case, it’s my org-mode windows, which keep track of most everything about my work day, but you could imagine it being some documentation reference or something.  Because I have these buffers up for long stretches of time but rarely actually go to them and edit them, Emacs thinks they’re not important and is happy to reuse their windows when it has new data to display.  How to stop it from doing so?

It turns out that what I want is a dedicated window (if I had realized this, I could have found the answer a lot faster). The details are in the Emacs Lisp docs or you can just put this trivial but handy code in your .emacs, and use the Pause key to toggle the dedication of the current window:

(defun toggle-current-window-dedication ()
 (interactive)
 (let* ((window    (selected-window))
        (dedicated (window-dedicated-p window)))
   (set-window-dedicated-p window (not dedicated))
   (message "Window %sdedicated to %s"
            (if dedicated "no longer " "")
            (buffer-name))))

(global-set-key [pause] 'toggle-current-window-dedication)