Showing posts with label emacs. Show all posts
Showing posts with label emacs. Show all posts

Monday, May 26, 2014

Sending notifications from Emacs (Mac OS X)


Sending notifications from emacs is something I find useful. In an earlier blog post I talked about how to use Growl to do so. http://justinsboringpage.blogspot.com/2009/09/making-emacs-growl.html

Well now you don't need Growl any longer. There's a neat github project called terminal notifier https://github.com/alloy/terminal-notifier which let's you send notifications from the terminal.

You can install it simply, via Homebrew or Rubygems as follows:
$ [sudo] gem install terminal-notifier
OR
brew install terminal-notifier
Then you can send notifications using a command like this:

terminal-notifier -message "hello"

Finally in order to send the notification from emacs we need to write a little Emacs lisp.

Check out this gist for the code I use:

https://gist.github.com/justinhj/eb2d354d06631076566f#file-gistfile1-el

This lets you send a notification in the future using M-x timed-notification

You are prompted for a time, and the format of that time can be given in a human readable way such as "2 seconds" or "5 minutes" (If you're curious for the allowed options look at the info page in emacs for the function timer-duration )

Then you are prompted for the message "Go to the store", and the message will be sent.

The code is very simple, it simply uses run-at-time to run the terminal command in the future. A useful command is find-executable, which given a name will find that executable in your path and run it. This makes configuring tools like this less effort.

Troubleshooting

Hey if it doesn't work first time what can you do?


  1. Check if terminal notify is installed correctly by running at terminal
  2. If that succeeds and you don't get a message check if you have enabled do not disturb mode
  3. Otherwise if that succeeds and yet emacs isn't sending messages you likely don't have the executable on the path. M-x customize-variable exec-path








Wednesday, February 6, 2013

Configuring emacs to send iCloud mail on Mac OS X

Pic from ajc1 on Flikr
It's handy to be able to send emails from emacs, and this guide will show how to set up SMTP via an iCloud email account.

Step 1. Install gnutls

iCloud requires you to send emails over secure channel, and emacs supports sending email with starttls or gnutls. gnutls is available through brew

To install it is easy:

brew install gnutls

Wait a few minutes while your Mac gets hot downloading and compiling!

Step 2. Create an authinfo file

emacs can look in a file ~/.authinfo to find your login credentials, so create that file and fill in the blanks.

touch ~/.authinfo
chmod 600 ~/.authinfo

The contents of the file should read:

machine smtp.mail.me.com port 587 login YOURNAME@icloud.com password YOURPASSWORD
Step 3. Configure emacs

Add the following to your .emacs file:


(setq
 send-mail-function 'smtpmail-send-it
 message-send-mail-function 'smtpmail-send-it
 user-mail-address "YOURNAME@icloud.com"
 user-full-name "YOUR FULLNAME"
 smtpmail-starttls-credentials '(("smtp.mail.me.com" 587 nil nil))
 smtpmail-auth-credentials  (expand-file-name "~/.authinfo")
 smtpmail-default-smtp-server "smtp.mail.me.com"
 smtpmail-smtp-server "smtp.mail.me.com"
 smtpmail-smtp-service 587
 smtpmail-debug-info t
 starttls-extra-arguments nil
 starttls-gnutls-program (executable-find "gnutls-cli")
 smtpmail-warn-about-unknown-extensions t
 starttls-use-gnutls t)

Note that your gnutls program may be in a different spot. Find it with:

mdfind -name gnutls-cli 
Step 4. Testing

To compose an email C-x m

Enter an email and hit C-c c to send it.

If it works, great! If not switch to the *Messages* buffer for hints on what may have gone wrong.

Step 5. Sending emails from elisp code



(message-mail recipient subject)
(message-send-and-exit)))))


Saturday, July 30, 2011

eredis update

I've been busy on my emacs redis client eredis and it now supports the entire API. It still needs a bit more polish but it should be a workable Redis client now, and I will continue to play with the org-mode table integration which I think has a lot of potential uses: for example making a gui to edit server parameters in just a few seconds.

Also made a new video showing some of the new org table creating commands and the monitor mode that shows the Redis commands as they are run on the server:


Monday, July 25, 2011

eredis: a Redis client in emacs lisp



I set up a google code project today for eredis. A Redis client in emacs lisp.

The program consists of a single emacs lisp file eredis.el

emacs lisp includes facilities for writing network applications. In my code I use `make-network-process' to open a connection to a specified redis server. Then the Redis api is exposed.

One nice feature of emacs I have used is org-table-mode. This lets you edit and manage the data in a Redis server in an org table. For example, you can grab all keys matching a pattern and create an org table from the key value pairs, then edit that table. You can then send it back to Redis with interactive commands that send either the whole table, or just the current row back to Redis using mset or set.

This work flow is not safe when working with multiple users, if you care about overwriting each others data. For example, I could store the last values you got from Redis in addition to your edited values. When you go to set a new value I first grab it from Redis, check if it has changed since you got it, and if so warn you (showing you the new value). For many work flows this would work well. For example the use case of a group of users editing a shared DB of configuration data.



Tuesday, June 14, 2011

Emacs progress indication

When programming in emacs lisp, there is an easy way to show progress feedback to the user when a task will take some time. Here's a block of code from the elisp manual showing how to do it.

(let ((progress-reporter
(make-progress-reporter "Collecting mana for Emacs..."
0 500)))
(dotimes (k 500)
(sit-for 0.01)
(progress-reporter-update progress-reporter k))
(progress-reporter-done progress-reporter))

I've incorporated this into my duplicate files code, linked below...

http://code.google.com/p/justinhj-emacs-utils/source/browse/trunk/duplicates.el


Sunday, June 5, 2011

More on duplicate file handling in emacs dired

I had some good feedback on my last post, that it would be useful to be leave just the superfluous duplicate files marked in the dired buffer. After doing that you can then copy them to another folder, or delete them.

I've added a function to do this `dired-mark-duplicate-files', and updated the google code site with the changes.

To copy to another folder use R and select a folder to move the dupes to. In order to delete them hit D.



Wednesday, June 1, 2011

Finding duplicate files in a dired buffer



picture by Donald MacLeod

This is a an example of programming emacs in emacs-lisp just to give an idea of what you can put together in an hour or two. I was looking at a dired buffer with a bunch of photos in, and some were the same photo that I'd downloaded twice. So I started thinking about writing a utility in emacs to automatically find and remove the duplicate files. In this post I'll just show the code for finding the files and display their filenames in a buffer.

I've put the source on google code.

After downloading you can load the source into emacs and call `eval-buffer', then open up a dired buffer to try it out. For this to be useful you need some duplicated files, so make some if you need to.

Mark the files you want to check for duplicates. For example to mark all jpg files you would type %m to mark files matching a regexp and type .*\.jpg

Now execute the command `dired-show-marked-duplicate-files' and after a short delay (in my test 80 jpg photos took about 5 seconds) you'll see a buffer called 'Duplicated files' which contains a list of the files which have the same contents.

Next steps for this little project will be to give you an interactive way to delete the duplicated files. I haven't decided quite how I'd like that to work, drop me an email if you have an idea. I've been thinking about perhaps resetting which files are marked so that only the duplicates are marked. At that point you can then hit R to move them to another spot, or delete them with x.

Now some comments about the code involved...

Most of the work is done in the function dired-show-marked-duplicate-files. First line " (interactive)" makes it an interactive function, meaning the user of emacs can invoke it.

"(if (eq major-mode 'dired-mode)" will check that we're in the right kind of buffer, because it makes no sense to run this in another mode.

In order to find the duplicate files I just need to walk the list of marked files, generate the md5 value of the contents of each one and add it to hash table. The keys in the hash table will be the md5, and the values will be a list of files with that md5. Once we've done that, finding duplicates is a simple matter of walking the hash table keys and displaying any where the value has multiple entries.

"(let ((md5-map (make-hash-table :test 'equal :size 40)))" Creates the hash table, making sure we use 'equal to match our filenames.

"(let ((filenames (dired-get-marked-files)))" this gets the marked files as a list of filenames

The next little bit of code is just to store the item in the hash table after getting the md5. There's no function in emacs to get the md5 of a file, but you can get the md5 of a string, so I wrote a helper function for getting the contents of a file into a temporary buffer first.

(defun md5-file(filename)
"Open FILENAME, load it into a buffer and generate the md5 of its contents"
(interactive "f")
(with-temp-buffer
(insert-file-contents filename)
(md5 (current-buffer))))

Finally I want to display the results, so I create a buffer and then use maphash (walks the keys of a hash table executing a function on each) with a helper function `show-duplicate' which simply writes the values of the hash table entry into that buffer.

Tuesday, November 2, 2010

Just browsing

It's handy in emacs to be able to go straight to your browser to view a page, and you can do fancy stuff with `webjump' as I mentioned in a previous post.

Another handy function is `browse-url' which will prompt for the url but default to whatever your point is at.

If you're in a html file and you'd like to open that with your browser then `browse-url-of-file' will open it up.

Finally, and I just found this today, if you have some html code in a buffer that isn't even a file, you can open that in your browser to using `browse-url-of-buffer' and emacs will write it to a temporary file and open that in the browser for you.


Tuesday, October 19, 2010

rgrep on windows 7 for emacs

I lost an hour configuring this, so seems worthy of a blog post.

A fresh install of emacs for windows will have functionality that does not work because it depends on unix style utilities.

One very useful example is the command rgrep, which searches files recursively through subfolders looking for a regular expression in those files.

Under the hood it uses the unix command line tools find, and egrep. Unfortunately the windows version of find takes entirely different parameters and will not function. In fact you will get an error that looks like this:

FIND: Wrong parameter format

Using the set of native ports of Unix command line tools UnxUtils you can easily fix this:

  1. Download the zip file and extract it to c:\unxutils
  2. Add the following path to the very front of your path by editing your system environment variables C:\unxutils\usr\local\wbin\;
That's it. You can run rgrep now and hopefully you're up and running.

If not make sure you have restarted emacs so it picks up the new setting of PATH. Open a shell in emacs and type 'find --version'. You should see something like this if your path is configured correctly:

c:\find --version
find --version
GNU find version 4.1

and if not you will see:

C:\Windows\system32>find --help
FIND: Parameter format not correct




Sunday, May 16, 2010

Growling Mac




Last year I wrote a blog post about how to make growl notifications (little pop ups driven by the application Growl) appear from emacs in windows. Since then I've become a total Mac-head. Here's an update on how you get that working from emacs on a mac...

Everything is the same except you need to install Growl for Mac (obv) and also make sure you copy the folder called Extras somewhere.

Open up a terminal and then cd to where you put the Extras directory. Find a sub-directory called growlnotify. Enter that and run 'sudo ./install.sh' which will prompt you for your admin password, and then go ahead and install growlnotify.

You can now run growlnotify from a terminal window. Try it out, if it doesn't work you won't be able to growl from emacs.

Now follow the instructions in my blog post above but ignore all the windows parts. You'll need to customize the growl command (M-x customize group, todochiku) and replace the full path I used for windows with just 'growlnotify'.

If everything is cool you can now do M-x todochiku-in and type a message, a time in minutes (zero for now), and pop, you should have a notification.

Monday, October 12, 2009

Transparent emacs on windows



Here's a handy function which lets you choose a transparent level (0 is fully transparent and 100 is opaque), for the main emacs frame in both focused and unfocused state.
(defun transparent(alpha-level no-focus-alpha-level)
"Let's you make the window transparent"
(interactive "nAlpha level (0-100): \nnNo focus alpha level (0-100): ")
(set-frame-parameter (selected-frame) 'alpha (list alpha-level no-focus-alpha-level))
(add-to-list 'default-frame-alist `(alpha ,alpha-level)))

To run M-x transparent. 

Enter the values you want for when the window has focus and when it does not. In the background you can see system status information on the desktop, which is another excellent utility from sysinternals called BgInfo. It let's you display useful info about your system right on your desktop, similar to tools you may find on linux. 


Thursday, September 10, 2009

Making emacs growl

Roaring lion

I've always wished emacs could notify me of it's doings. For example the message function just pops up text in the minibuffer and is easy to miss, and impossible to see when the window is not in focus. So I was pleased when looking through the
twit.el code to find out about todochiku, and emacs interface to Growl.

Download Growl for windows and set it up. Send yourself a test growl at the command prompt like this:

"C:/Progra~1/Growlf~1/growlnotify.exe" /T:"title" "message"

Once that's working you can send notifications from emacs. todochiku is an emacs package for sending notifications to growl, snarl or whatever you have. In our case we have growl. Unfortunately the windows default is to use snarl, and there's no support for growl. I've made a few modifications to the original to get that working and uploaded the new file here. (Ideally I should make it so it searches both for windows and for growl or snarl being installed but for now the choice is made manually)

heyes-jones.com/todochiku.el

Download the elisp file into a directory in your emacs load-path (or add it) and add the (require 'todochiku) command to your .emacs file. Reload emacs, or just hit C-x e after the (require command in your .emacs)

You will need (require 'cl) somewhere before this is loaded (.emacs perhaps)

Customize the variables for the program using M-x customize-group todochiku

You'll need to set the `todochiku command' to something like this:

C:/Progra~1/Growlf~1/growlnotify.exe

Use "Dir /x *" in a folder to find out what the 8 character name is.

If you want icons that come with todochiku then download them from the todochiku wiki page and point to them with the variable `todochiku icons directory'. For example mine is set to:

~/localemacs/todochiku-icons

Finally you can do a growl... try this

(growl "Emacs" "Hello")



There's also `todochiku-message' which let's you specify an icon. This can be an image filename, and url, or an icon symbol from the built in list of icons you can find by the variable `todochiku-icons'.

(todochiku-message "Emacs" "You're growlingnow" 'social)

(todochiku-message "Emacs" "You're growlingnow" "http://www.growlforwindows.com/gfw/images/downloadlatest.gif")

(todochiku-message "Emacs" "You're growlingnow"
"c:/cygwin/home/Justin/localemacs/todochiku-icons/binary.png")

There's a command `todochiku-in' which will send you a notification from emacs in a set number of minutes.

(todochiku-in "hello" 3)


If you use twit.el you should find that todochiku automatically notifying you of tweets if you have called `show-recent-tweets'.

It's very simple to use todochiku and growl so you should find all kinds of applications for this. Have fun!



Tuesday, September 8, 2009

Directional window movement in emacs

One thing I sometimes miss about the text editor Brief, which I stopped using a decade ago, is being able to move between windows easily by pressing a function key and then an arrow key to switch to a window in that direction.

In emacs I only knew how to move between windows using `other-window', mapped to C-x o and that only cycles around the windows, and if you have a few windows open and possibly you're also editing the mini-buffer, that's quite a round trip.

I just learned about `M-x windmove-default-keybindings', which binds a set of commands that do exactly the kind of directional movement I'm talking about (`windmove-right', `windmove-up' and so on) to the cursor keys (with shift key held).

Tuesday, September 1, 2009

Handful of emacs tips

keep-lines
flush-lines

These handy functions prompt you for a regular expression and will either remove every line that does not contain that expression (keep) or remove the ones that do (flush
).

finder-list-keywords

This function shows all the keywords used in the packages within emacs. In other words you can find a lot of stuff in emacs that you never knew was there.





Wednesday, August 26, 2009

Simple budget forecasts in emacs

Money

When I first moved from England to North America one of the things I found hard to get used to was getting paid every two weeks instead of monthly. This means that some of your bills are monthly, some are biweekly, and occasionally I had more or less money than I expected as they got out of sync.

I've been using a simple elisp program I wrote to let you manage your upcoming bills, weekly budget items like groceries, and your regular income. It runs through a period of dates you supply, and a list of incomes and expenses, and produces a csv file and a buffer that contains the output of the forecast. It shows the highest and lowest balance of the period too, so you can spot if you will go over drawn, and also see peaks when it's a good time to move money to savings.

The code is hosted on google code as emacs-easy-budget and is also released as GPL v3. Hope somebody finds it useful.

Thursday, May 14, 2009

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.

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 http://java.sun.com/javase/6/docs/api/
  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 "www.google.com" "http://www.google.ca/search?hl=en&as_sitesearch=http://java.sun.com/javase/6/docs/api/&q=" ""])
)
webjump-sample-sites))



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()
(interactive)
(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"
(interactive)
(if (eq major-mode 'dired-mode)
(let ((filenames (dired-get-marked-files)))
(mapcar fn filenames))
(error (format "Not a Dired buffer \(%s\)" major-mode))))