Thursday, May 14, 2009

Linking MingW32 with psapi

Here's something I came across that didn't have a good answer when I googled. If you get link errors with MingW32 like:

undefined reference to `GetProcessMemoryInfo@12' or
c:/justinhj/mem.cpp:55: undefined reference to `EnumProcesses@12'

Then you are not linking with the library psapi, and you need it.

To do this just add -lpsapi to your command line. It needs to be last in the list of libraries too!

Writing quick C++ programs in emacs without a makefile

Cheetah by Storm Crypt

Often when programming in C++ (or many other languages) it's desirable to write a quick program, usually a console application, to test something quickly. Working in developer studio, it takes an amount of fiddling and clicking large enough that you may not want to bother.

Working in emacs and MingW32 (or Microsoft's CL.exe) I can quickly create a CPP file, and then compile it. To compile a program though you need to either write a makefile, or build up a complex g++ command line.

Rather than do either of those I make a helloworld.cpp file which can be built like this:

g++ helloworld.cpp -g -o helloworld.exe

So I hit

M-x compile

then type in the above to compile and link it.

However there's a better way. You can add the compile command as the first line of the file as a file local variable.

// -*- compile-command:"g++ helloworld.cpp -g -o helloworld.exe"; -*-

Now I can hit the compile and it works right away, between sessions. No makefile or project settings in sight, and now there are minimal barriers involved in creating a simple test program beyond copy and pasting the file.

Tuesday, May 12, 2009

Converting flac to mp3

This is just a link to a great description of converting flac to mp3 files. There's a tonne of commercial and useless sites that try to answer this question

So I thought I'd add another link to this one that actually works, and uses free software.

Friday, May 8, 2009

emacs: Renaming multiple files at once with a regular expression

M-x dired
(navigate to the folder)
M-x wdired-change-to-wdired-mode
M-x replace-regexp
(enter search and replace expressions)
C-c C-c

Which writes out the changes to the directory.

Thursday, May 7, 2009

emacs: Searching programming API's with webjump

Image by MarkyBon

In a previous post I outlined how to use webjump, a built in feature of emacs to quickly launch web pages in a browser, after querying the user for search phrases and so on.

This describes how to simply setup webjump to search the API documentation for whatever framework you are using. The example will be Java's Platform documentation. 

  1. Find the base url you want to search. We will use google to only find things that begin with this. In the case of Javas documentation I'm going to search at
  2. Set up the web jump code as below. The three arguments in square brackets are [ base url, normal search url prefix, search url post fix ]. You just need to replace the JAVA API part of my URL with the URL you are interested in.
  3. M-x webjump
  4. Type your query and hit enter
  5. Browser opens with search results

(require 'webjump)
(global-set-key [f2] 'webjump)
(setq webjump-sites
(append '(
("Java API" .
[simple-query "" "" ""])

Wednesday, April 15, 2009

Running an elisp function on each marked file in a dired buffer

Chain Tool by Florian 

If you have some function, say some custom refactoring operation, you can call it from a dired buffer, and have it run on each marked file, using the code below. 

Go into a dired buffer, mark some files, and then do M-x and run your command. In this case it would be test-for-each-dired-marked-file

;;; iterating over files in dired

;;; usage example - for-each-dired-marked-file returns a filename and path
;;; for each marked file, so this is what a function using it looks like
(defun view-stuff(filename)
"opens up the file and gets the length of it, then messages the result"
(let (fpath fname mybuffer len)
(setq fpath filename)
(setq fname (file-name-nondirectory fpath))
(setq mybuffer (find-file fpath))
(setq len (buffer-size))
(kill-buffer mybuffer)
(message "Buffer length %d %s" len (buffer-file-name mybuffer))))

; Usage example
(defun test-for-each-dired-marked-file()
(for-each-dired-marked-file 'view-stuff))

(defun for-each-dired-marked-file(fn)
"Do stuff for each marked file, only works in dired window"
(if (eq major-mode 'dired-mode)
(let ((filenames (dired-get-marked-files)))
(mapcar fn filenames))
(error (format "Not a Dired buffer \(%s\)" major-mode))))