2009-04-27

dot-emacs trickery

So, I will be offline for a few days, and Emacs-Fu has been a bit quiet anyways, so I'm inviting everyone to share their best little .emacs tricks. Things like:

(blink-cursor-mode nil)             ;; stop cursor from blinking
or
(setq search-highlight t            ;; highlight when searching... 
  query-replace-highlight t)        ;; ...and replacing
What useful trick should every emacs user know? Please use the comments to share your wisdom!

2009-04-24

emacs command of the day

A few weeks ago, I discussed twitter.el, a nice way to use twitter from the comfort of your own emacs - and the package is gaining new features as we speek, such as 'reply'-support.

Anyway, reason to bring up twitter again is the Emacs Command of the Day-twitter feed. The idea is that every day, it provides some useful command, and it's a response to a similar feed for Vim. It's only just started, let's see where this goes.

2009-04-19

package management with ELPA

Today, the two major surviving branches on the emacs family tree are GNU/Emacs (or 'FSF/Emacs'), and XEmacs – they split off after the so-called Emacs Schism of the 1990's. Much has been said and written about it, so I don't need to go into that here - see EmacsWiki for the details. Anyway, the 'schism' seems less important now, as XEmacs seems to have lost a bit of steam, with GNU/Emacs picking up all the cool stuff that only XEmacs used to have, and then some.

Admittedly, it's been a while since I used XEmacs, but the one thing I remember that it still somewhat missing in GNU/Emacs, is package management. XEmacs has a nice system where you can mark packages for installation, and then get them from a website; much easier than the GNU/Emacs way, where you need to manually download things, maybe compile them, and update your .emacs - at least for things that your OS or distribution does not provide.

This has changed now with ELPA, the Emacs Lisp Package Archive. It's Tom Tromey's answer to the XEmacs package manager. The ELPA website explains its installation quite clearly; basically, evaluate:

(let ((buffer (url-retrieve-synchronously
               "http://tromey.com/elpa/package-install.el")))
  (save-excursion
    (set-buffer buffer)
    (goto-char (point-min))
    (re-search-forward "^$" nil 'move)
    (eval-region (point) (point-max))
    (kill-buffer (current-buffer))))
(you can 'evaluate' code by going just to the right of the last parenthesis and press C-x e).

This will add some code at the end of your .emacs.

To get a list of the available packages, you can use M-x package-list-packages; ELPA will download the most recent list in a buffer (see the screenshot). Now, to install packages, move your cursor to them and press i. This will mark the packages for installation. When you're done with marking, press x, and ELPA will install the packages for you (under ~/.emacs.d/elpa/).

Now, the list of packages in ELPA is not very big yet, but it's growing. I wishfully think that ELPA is to be a really important part of the Emacs ecosystem of the future. I submitted my TexDrive package for inclusion into ELPA; hopefully many other package authors will do the same.

2009-04-10

adding custom menus

If you read emacs-fu and other emacs websites, you might gather an increasing number of useful functions in your .emacs; so many, in fact, that it can be hard to remember them all. One way to solve this, is by adding a new menu to emacs, in which you can put your most useful commands. So, how to add our own custom menu? It's easy - when you use the aptly-named easymenu-package. Suppose we want to add a top-level menu called MyMenu, with some useful functions; simply add this to your .emacs:
(easy-menu-define djcb-menu global-map "MyMenu"
  '("MyMenu"
     ;; http://emacs-fu.blogspot.com/2008/12/running-console-programs-inside-emacs.html
     ("Programs" ;; submenu
       ["mutt"  (djcb-term-start-or-switch "mutt" t)]
       ["mc"    (djcb-term-start-or-switch "mc" t)]
       ["htop"  (djcb-term-start-or-switch "htop" t)]
       ["iotop" (djcb-term-start-or-switch "iotop" t)])
  
     ;; http://emacs-fu.blogspot.com/2009/03/math-formulae-in-webpages.html
     ;; this submenu is only visible when in html-mode or html-helper-mode
     ("TeXDrive"  :visible (or (string= major-mode "html-helper-mode") (string= major-mode "html-mode"))
       ["Insert formula"   texdrive-insert-formula :help "Insert some formula"]
       ["Generate images"  texdrive-generate-images :help "(Re)generate the images for the formulae"])
     
     ;; http://emacs-fu.blogspot.com/2009/03/twitter.html
     ("Twitter" ;; submenu
       ["View friends" twitter-get-friends-timeline]
       ["What are you doing?" twitter-status-edit])

     ("Misc"  ;; submenu
       ;; http://emacs-fu.blogspot.com/2009/01/counting-words.html
       ["Count words" djcb-count-words]

       ;;http://emacs-fu.blogspot.com/2008/12/showing-line-numbers.html
       ["Show/hide line numbers" linum]

       ;; http://emacs-fu.blogspot.com/2008/12/running-emacs-in-full-screen-mode.html
       ["Toggle full-screen" djcb-fullscreen-toggle])))
Suffice it to say that you actually need to have the functions installed - see the links for the ones that interest you.

The menu-system is much more powerful than I show here. With easy-menu, your menus are mode-specific (or mode-map-specific); I use the global-map, so the menu is always visible. You can also include special 'toggle' menu items, you can make items active/inactive visible/invisible based on some function evaluation. You can also add your menu as a submenu to an existing one. I don't really need all that, but there are some great ways to spend some hours to get things just right :) Anyway, as an example of some of the extras, look at the TexDrive submenu, which is only visible when we're in html-mode or html-helper-mode. And see the help:-expressions, that define tooltips for the menu items.

There is one imperfection left. Right now, my menu is added at the left-most position. I haven't been able to figure out is to add the menu at the most-right position in the menubar (or maybe just before the 'Help' entry). It can be done with e.g. Drew Adams' menubar+, but I found that a bit less... easy. Anyway, an exercise for the reader...

2009-04-05

remember

remember

[ UPDATE: I have cleaned up the code; it should work better now if you don't have an open emacsclient. I also removed the comments relating to that, as to not confuse people…]

The human brain is quite good at forgetting things. Often, this is a good thing. However, sometimes you might actually not want to forget. Things you need to do – 'Buy milk', 'Register for Helsinki City Run', or simply things that might be of interest, such as books, funny quotations, articles to check out, and so on.

Such thoughts should me jotted down immediately, without thinking too much. It's an essential part of productivity methods like GTD ('Getting Things Done'). The idea is that you capture ideas, thoughts, plans, etc. at the very moment they enter your mind.

Periodically, you go through all the things you gathered, and decide what to do with them. Maybe you need to take some action, maybe you only need to store it somewhere. Or maybe you can simply throw it away. The main point is that whenever something is in your mind that might be interesting later, make sure it's not lost.

You can of course carry a simple notebook around and that's not a bad idea at all. However, a large part of the information inflow is digitized, computerized, and it's not very convenient to copy interesting tidbits by hand from your notebook. Clearly, our almighty emacs should be able to help with this, or?

Indeed. The Emacs-way to capture dispersed information is called remember-mode. It allows you to quickly write down your thoughts, press C-c C-c and be done with it. The important thing is that is should be a mindless little exercise to save these things, and get on with your work, life.

I'll describe my use of remember-mode in combination with org-mode. There are many other things you can do with remember-mode; just read the manual or the cookbook; also see the great tutorial, and the remember-mode section in the org-mode-manual.

To integrate remember-mode with org-mode, add to your .emacs:

(org-remember-insinuate)

Now, the important thing is to have 'remember' available anywhere on your desktop, not just when using emacs – after all, valuable ideas might come up even when not using emacs, for example when browsing the web. Jack Moffit showed how to get little pop-up windows for remember on MacOS; inspired by that, here is my solution for Linux/X (this is an updated version):

;; you might also want to set:
;;   (setq org-agenda-skip-unavailable-files t)
;; so these warnings won't annoy the little remember-frame
;; also: I have noted infrequent problems when using ElScreen
;;  (the wrong frame might be chosen for Remember in about 10% of the cases)

(defun djcb-remember-frame ()
  "turn the current frame into a small popup frame for remember mode;
this is meant to be called with 
     emacsclient -c -e '(djcb-remember-frame)'"
  (modify-frame-parameters nil
    '( (name . "*Remember*") ;; must be same as in mode-hook below  
       (width .  80)
       (height . 10)
       (vertical-scroll-bars . nil)
       (menu-bar-lines . nil)
       (tool-bar-lines . nil)))
  (org-remember)
  (when (fboundp 'x-focus-frame) (x-focus-frame nil)) ;; X only....

  (delete-other-windows)) 

;; when we're in such a remember-frame, close it when done.
(add-hook 'org-remember-mode-hook
  (lambda()
    (define-key org-remember-mode-map (kbd "C-c C-c")
      '(lambda()(interactive)
         (let ((remember-frame-p 
                 (string= (frame-parameter nil 'name) "*Remember*")))
           (when remember-frame-p (make-frame-invisible))  ;; hide quickly

           (org-remember-finalize)
           (when remember-frame-p (delete-frame)))))))

The next step is to have some easy way to run this. I am using emacsclient and xbindkeys for that. In my ~/.xbindkeysrc I have:

# pop-up a remember window
"emacsclient -c -e '(make-remember-frame)'"
  control+shift+r

After this (and assuming a running emacs daemon), you can run remember-mode by pressing Control-Shift-r. Make it part of your routine; whenever an idea comes up, press Control-Shift-r, quickly jot down the thing on your mind, and press C-c C-c to save it for eternity. Review your notes periodically.

Finally, as mentioned, remember-mode can do quite a bit more than described here - especially when used in combination with org-mode. One interesting extension are templates, which are a way to create your notes in a structured way. For example, I have the following:

(setq org-remember-templates 
  '(("Clipboard" ?c "* %T %^{Description}\n %x"
      "~/remember.org" "Interesting")
     ("ToDo" ?t "* TODO %T %^{Summary}" 
       "~/remember.org" "Todo")))

Please refer to the manual for the exact syntax. Basically, this defines two templates; the first one will ask you for a description, and then add then insert the contents of the (X) clipboard under the 'Interesting'-header; the second insert a simple 'TODO'-entry under the 'Todo'-header.

Exempli gratia – suppose I have selected some text in my web-browser. Now, I push the key-binding we set up before: C-R (Control-Shift r). A small window pops up, asking me if I want to make a 'Clipboard' or a 'Todo' entry. I press c for a clipboard entry. I asks me for a description, which I provide; it dumps the contents of the clipboard in the entry. I then push C-c C-c to save and close the pop-up window.

All of the notes will be saved to a file ~/remember.org; you can customize this of course, and you can also move different kinds of today items to different files. Anyway, after a while, ~/remember.org might look somewhat like this:

* Interesting
** <2009-04-05 Sun 18:06> what is emacs?

  Emacs is a powerful, customizable, self-documenting, modeless text
  editor. Emacs contains special code editing features, a scripting language
  (elisp), and the capability to read mail, news, and more without leaving the
  editor.
** <2009-04-05 Sun 19:39> socrates
  The hour of departure has arrived, and we go our ways - I to die, and you to
  live. Which is better God only knows.
* Todo
** TODO <2009-04-05 Sun 18:12> buy milk & cookies

As you see, remember.org is just another org-mode-file, so all its bells and whistles are available. Periodically, you should go through this file and take necessary actions.

You could go further and develop complicated templates for capturing specific information, tagging it and so on. The various manuals give some examples. All I wanted to do here is to give a small taste of the power of remember-mode in combination with org-mode. The rest is up to your imagination…