Extending Emacs with Advice
January 14, 2009 at 04:30 PM | categories: emacs | View CommentsThe greatest single thing about Emacs is it's extensibility. If you think Emacs is missing something, or don't like how something works, you can change it. But that's true of any open-source software. The difference with Emacs is how easy it is to make that change.
There are several different strategies to extend Emacs. I'll list a few of them in order of hardest to easiest (and arguably from worst to best):
- Write your own major or minor mode from scratch.
- Edit someone else's mode (and hopefully submit a patch.)
- Write an Advice function.
- Write a Mode Hook.
Where Advice fits in
(Don't worry, I get into the meat of it in the next section. For the impatient, skip down.)
Mode hooks are almost always the easiest and best way to extend a particular mode, but they are also limited in scope. The author of the mode created these hooks specifically to allow you to extend his mode, and are therefore the best maintainable way to extend a mode (the author is unlikely to remove those hooks in future versions). However, if the author didn't anticipate certain functionality and there is no appropriate hook provided, you'll have to come up with a different solution.
At this point, obviously, you could just write your own mode -- but what a pain that would be if you already have something that's close to what you want. So why not just edit that particular mode to suit your needs (perhaps creating that missing mode hook yourself?) This approach is probably the best one especially if the feature you want is something other people would benefit from. You can submit your changes as a patch to the original author and (if he accepts them) your changes go into the next release.
But what if for some reason your changes are not acceptable to the author? You can still use your changes on your local machine, but every time a new version of the original mode comes out you'll have to merge your changes back into that new version. At this point you've successfully "forked" the project, but that's usually a bad thing in the long run.
One other option exists to Emacs developers: advice. Advice allows you to wrap existing functions with your own functions. This allows some types of behavior to be modified without even having to change the original file at all. You can keep your changes completely separated in your own customized files. This gives you the benefit of inheriting new versions of the mode by simply downloading the new version and putting it in your load path. In most cases your advice function will continue to work on the new version and you didn't have to merge any changes at all. If significant changes have been made you may need to modify your advice function, or create a new one, but you still won't need to merge any changes.
Continue reading and I'll give you a real-world example of how I've used advice, but I want to first re-iterate the proper use cases of advice:
- If you can use the mode hooks provided by the author, use them instead.
- If there is a bug in the original mode, just fix it in the original code and submit a patch.
- If there is a new feature you want, add it to the original mode and submit a patch. Talk to the author, work with him, and it will most likely end up in the next release.
- If you are an Emacs developer, working on Emacs itself, or one of the modes shipped with Emacs, never use advice. It's the least maintainable method of extending Emacs with the exception of a pure fork, and since you're working on Emacs itself, it's not a fork.
- If your patch is not accepted, or you know that what you want is fringe enough or hackish enough to not warrant submitting a patch, only then should you use advice or fork the project.
Using Advice
Alright, onward to the example I promised. I really liked the tip over at Minor Emacs Wizardry on combining EasyPG with Org mode -- it's an awesome way to keep a lot of things in an organized and encrypted way -- I use it all the time now.
My main machine is running Ubuntu and it's default GnuPG agent is Seahorse. It's actually a pretty nice manager for encryption (SSH and PGP) and is completely integrated with the Gnome desktop. The one problem I have with it is that I use Multi-TTY Emacs and when I'm using a terminal on a remote connection and I try and open an encrypted file, Seahorse pops up on the desktop instead of prompting me for the passphrase on my remote connection. In the past I've had to fire up VNC to my desktop just so that I could enter the password; damn inconvenient.
I could setup something like keychain, it's a real nice GPG agent that works equally well on the desktop as on the console. I've used it in the past and liked it. Seahorse requires no setup though, it comes with Ubuntu by default, and like I said, I really quite like it.
So, when I'm on my remote connection and Seahorse doesn't have my password cached, I'd like to be able to bypass the GPG agent entirely and just enter my password. EasyPG doesn't appear to have this functionality so I added it with advice:
(defadvice epg--start (around advice-epg-disable-agent disable) "Make epg--start not able to find a gpg-agent" (let ((agent (getenv "GPG_AGENT_INFO"))) (setenv "GPG_AGENT_INFO" nil) ad-do-it (setenv "GPG_AGENT_INFO" agent)))
That little bit of code wraps the main EasyPG function that will ask for a password. It temporarily removes the environment variable that EasyPG looks for to connect to the GPG agent, and therefore EasyPG asks the user for the password as if there were no agent running.
Let's analyze the different parts of that advice function:
- All advice starts with a "defadvice" definition.
- "epg--start" is the name of the original function I'm wrapping.
- "around" is indicating that I'm specifying code to run both before and after the function I'm wrapping (other options would include "before" and "after")
- The word "disable" means that the advice is initially turned off (you could say "activate" to have it immediately turned on, read the manual for more options here)
- Starting with the "let" function is the body of my custom function
- The "ad-do-it" line in the middle is the point in the function where the original function gets executed. (This would not be used in a "before" or "after" style function.)
One more important thing to understand about advising a function is that you aren't creating a new function with a new name. You are adding data to the existing function definition. Wherever the original function gets called, your advice will be called along with it. However, advice does offer the convenience of turning on or off your changes. For the above example I added two user commands to turn on and off the GPG agent (bound to M-x epg-disable-agent and M-x epg-enable-agent):
(defun epg-disable-agent () "Make EasyPG bypass any gpg-agent" (interactive) (ad-enable-advice 'epg--start 'around 'advice-epg-disable-agent) (ad-activate 'epg--start) (message "EasyPG gpg-agent bypassed")) (defun epg-enable-agent () "Make EasyPG use a gpg-agent after having been disabled with epg-disable-agent" (interactive) (ad-disable-advice 'epg--start 'around 'advice-epg-disable-agent) (ad-activate 'epg--start) (message "EasyPG gpg-agent re-enabled"))
These functions use the (ad-enable-advice) and (ad-disable-advice) functions respectively. Whenever advice is switched on or off, a call to (ad-activate) must also be made on the function to update it.
The manual goes into more depth about how to advice functions. Hopefully you got something useful out of this, I sure learned a lot in writing it.
Emacs ansi-term tricks
December 26, 2008 at 11:30 AM | categories: emacs | View CommentsBeing on vacation is soo nice. With some of my free time I'm planning on revamping my emacs environment, so hopefully that means a few more articles showing up here on that topic.
I read some great tips on ansi-term. Ansi-term is a terminal emulator written in emacs lisp that is as close to a real terminal as possible. That means you can run virtually all command line programs, even the ones that use ncurses like top or screen, all within emacs. You can still switch between line and char modes which means you can still edit the buffer as you could in a regular (emacs) shell too.
F2 Keybinding
In the above mentioned article Joseph wrote a nice little bit of elisp to get to a running ansi-term efficiently, by hitting F2. The nice thing about it is that it does what I mean:
- If I'm already in an ansi-term, but it's called "*ansi-term*" rename it.
- If I'm already in an ansi-term, but it's called something else, start a new ansi-term called "*ansi-term*"
- If I'm in another non-terminal buffer, switch to a buffer called "*ansi-term*" or create a new one if it doesn't exist
There's one more catch though, as Joseph explains, an ansi-term can be considered "stopped" such that it is no longer running but the buffer still exists. In that case I don't want the third rule to switch me to a defunct terminal, so instead I want it to kill the buffer and create a new ansi-term. Here is my enhanced elisp:
(require 'term) (defun visit-ansi-term () "If the current buffer is: 1) a running ansi-term named *ansi-term*, rename it. 2) a stopped ansi-term, kill it and create a new one. 3) a non ansi-term, go to an already running ansi-term or start a new one while killing a defunt one" (interactive) (let ((is-term (string= "term-mode" major-mode)) (is-running (term-check-proc (buffer-name))) (term-cmd "/bin/bash") (anon-term (get-buffer "*ansi-term*"))) (if is-term (if is-running (if (string= "*ansi-term*" (buffer-name)) (call-interactively 'rename-buffer) (if anon-term (switch-to-buffer "*ansi-term*") (ansi-term term-cmd))) (kill-buffer (buffer-name)) (ansi-term term-cmd)) (if anon-term (if (term-check-proc "*ansi-term*") (switch-to-buffer "*ansi-term*") (kill-buffer "*ansi-term*") (ansi-term term-cmd)) (ansi-term term-cmd))))) (global-set-key (kbd "<f2>") 'visit-ansi-term)
TRAMP Integration
This is cool.Put the following inside of your .bash_profile on any computer that you ssh into frequently:
#Emacs ansi-term directory tracking # track directory, username, and cwd for remote logons if [ $TERM = eterm-color ]; then function eterm-set-cwd { $@ echo -e "\033AnSiTc" $(pwd) } # set hostname, user, and cwd function eterm-reset { echo -e "\033AnSiTu" $(whoami) echo -e "\033AnSiTc" $(pwd) echo -e "\033AnSiTh" $(hostname) } for temp in cd pushd popd; do alias $temp="eterm-set-cwd $temp" done # set hostname, user, and cwd now eterm-reset fi
Now when you ssh into a machine from within ansi-term and open a file with C-x C-f you'll be loading a file via TRAMP from the current working directory on the remote machine. Amazingly cool.
Ubuntu: coping with a height restricted display
December 10, 2008 at 01:06 PM | categories: linux | View CommentsI'm eagerly awaiting my new Samsung NC10 to be shipped from Amazon. It's a nice little netbook that I can use when traveling and when going to social occasions. I'm specifically buying this laptop because of its small size, but I know that one thing specifically will bother me: the screen resolution, at 1024x600 is wide enough for most tasks, but not very tall at all.
So while I'm waiting, I'm playing around with Ubuntu 8.10 inside a VirtualBox instance. I've got the window resized to 1024x600 and am playing around with different settings to make maximum use of the screen. Here are my tweaks:
Gnome Panel (the toolbar):
Open gconf-editor (Alt-F2 'gconf-editor') and navigate to apps -> panel -> toplevels -> top_panel_screen0 and change the following:
- auto_hide: checked -- makes the main toolbar panel autohide
- auto_hide_size: 0 -- makes the toolbar completely hide instead of leaving a few pixels visible
- enable_animations: unchecked -- make the hiding/unhiding as fast as possible
- hide_delay: 500 -- makes the toolbar stay on screen for half a second after using it
- unhide_delay: 0 -- makes the toolbar immediately popup when your mouse goes to the very top (or bottom) of the screen
Gnome Terminal:
Open the default profile properties, right click in the window and click "Edit Current Profile"
- Turn off the menu bar: on the General tab uncheck "Show menubar by default in new terminals"
- Turn off the scrollbar: on the Scrolling tab select "Scrollbar is: Disabled"
Oversized windows:
Uncheck the following key in gconf-editor:
/apps/compiz/plugins/move/allscreens/options/constrain_y
Now you can Alt-left-click any window and drag it around even above the top of the screen.
Firefox:
- Move the bookmarks bar up to the file menu bar and get rid of the bookmarks bar:
- Right click on the Home button and click customize.
- Your bookmarklets should now disappear and read "Bookmarks Toolbar items."
- Drag "Bookmarks Toolbar items" along the right hand side of the File .. Edit .... Help menus.
- Click "Done."
- Right click on the now empty bookmarks toolbar and uncheck "Bookmarks Toolbar."
- Get rid of the status bar: Go to View -> uncheck "Status Bar."
- Install a theme that uses smaller buttons and text: I like Classic Compact.
- Remember that Firefox has a nice fullscreen option : F11.
- Check out Tree Style Tabs. It puts the tabs vertically on the left side of the browser and visually keeps track of what sites you were at that prompted you to open a new tab, all in a tree like fashion.
Here is the desktop with gnome panel completely hidden and one terminal with menu bar hidden (click to see native size):
Here is firefox maximised with two tabs open (click to see native size):
Here is firefox in fullscreen (click to see native size):
Firefox maximized with Tree Style Tabs:
In Pursuit of the Aggressor
December 05, 2008 at 12:08 PM | categories: free state project, pissed off | View CommentsI've been feeling pretty good lately, I have a decent job, a wonderful wife, awesome friends, and a new-found purpose in life: liberty activism in New Hampshire. In a world as messed up as ours, its been a profound comfort to have found such peace and purpose. But I've also been a bit startled recently including having feelings of helplessness, distrust, and insecurity.
My truck got broken into.
I had just gotten home from work and not 10 minutes transpired before I heard a loud crash and some rustling outside. As I rushed out the door, I saw someone had broken the window of my truck and was diving inside to grab my video camera. Before I knew it, I was several blocks away from my house, running full speed 10 feet behind the guy who was also running like hell. I kept chasing him, never quite closing the gap between him and myself until I ran out of energy. He turned the corner and I followed a bit more slowly. Knowing he was hiding, I looked closely in the darkness for him and I finally saw the guy dash from his hiding spot and continue running away. I said a few more unkind words in his direction as he fled, but there was nothing more for me to do. Out of breath, dejected, and quite frustrated, I walked back home. Kellie was already outside at this point wondering what had happened, we assessed the damage and confirmed that he had destroyed the passenger side window of my truck and managed to steal my video camera.
My Landlord had already called the cops and they showed up about 10 minutes later. Since they were there anyway, I gave them my take on what happened, although I had never really gotten a good look at the guy. I really didn't have much hope that the police would ever do anything (and in an ideal world we wouldn't have such agents of force). Kellie left to do her own reconnaissance at the corner store and surprised me when she told me that she saw the same cops doing actual detective work, questioning the store manager. Although I'm almost sure that these same men have violated the rights of peaceful individuals before, and will most likely continue to do so in their roles as agents of the state, I'm glad that they take their jobs seriously enough to want to protect peace when they can.
I don't really leave anything that valuable in my truck. The camera is a somewhat cheap Samsung SC-MX10, I had it mounted to my dash with a Modifry camera mount (The mount got ripped out with the camera), this setup was supposed to record the cops if I ever got pulled over. Sadly, I never got to try that out. I'm sure that the way I had it mounted made it stand out as an easy target for a thief.
I called up my insurance rep as well as an auto glass shop and got things taken care of pretty quickly, the auto glass shop was even willing to drive out to my house the next day and repair it right there even while I went to work that day. I love how market competition makes this so easy.
In total, I'm out $200 for the camera, $25 for the mount, and $163.15 for the glass. Yea, $388 is kind of a lot. However I was much more irritated with the inconvenience of it all, as well as the knowledge that there are people right in my neighborhood that have no reservations in actively destroying their society. The reason I'm here in NH is to work towards creating a civil and voluntary society. Its one thing to disagree with my ideas, I'm used to that. Its quite another to find that you live in a community where some percentage of the people do the exact opposite; creating a society of violence and distrust.
I'm left with a renewed sense of the importance of security, including responsible firearms ownership and carrying whenever possible. I don't actually think that a gun would have been useful in this situation, especially after the incident was carried onto the open street at 12 mph, but knowing first hand that desperation has led people to such forceful actions, it makes me much more cautious.
Is Ryan going to jail?
November 14, 2008 at 03:56 PM | categories: liberty rants, pissed off | View CommentsI've come to the realization that I will be going to jail in the near future. Not for hurting anyone, but because the oppressive and unconstitutional powers-that-be see me as an Enemy of the State and an affront to their illegitimate power.
Two friends of mine have recently been thrown in jail. Neither of them have harmed, had any intention of harming, nor even inconvenienced, anyone. Their "crimes" are for having "contempt" for the state-funded thugs that wish to bully them into compliance with their procedures. Their only "crime" is that they had some questions, questions that the state did not want to hear.
Lauren Canario, who was invited to the federal court building to retrieve her camera (that the feds stole from her), walked through a metal detector checkpoint at a time when the guards weren't looking. She was ordered to go back through the checkpoint and was subsequently arrested when she asked why she was required to do so. She'll be in federal prison for 30 days.
Ian Freeman, of Free Talk Live, today was arrested over a couch. You can read the backstory of his case where his tenants have an old couch in his yard that the city of Keene wants removed. Ian is very willing to remove the couch, but simply wants to talk to the person who made the original complaint. Instead of hiding behind the force of government, the original complainant should speak to Ian in a friendly, neighborly way just as civilized communities ought to behave.
Instead, Ian was compelled to go to court today. He went there having every intention of complying with the request to remove the couch. He has also made this very clear to the court on previous occasions. He simply wanted to demand that he be confronted by his accuser, as is his constitutional right. However, within 45 seconds of entering the court room, he was railroaded into being found in contempt of court and was immediately arrested. The court removed the public observers from the room and only allowed them to view Ian on camera (without audio) to monitor the proceedings. The judge found him guilty, despite not being shown his accuser, and he has been sentenced to three days in prison for the matter of the couch and 90 days for being in contempt of the court's proceedings.
The Keene city court has today shown that it is a court that does not recognize the right to be confronted with one's accusers, a court that does not recognize the right to have a public trial, and a court which has gone way beyond simply upholding the "rule of law". What country is this exactly?!? The Keene city court has decided that it has a personal vendetta against all local liberty activists. Accordingly, the prospect of me going to prison is a virtual forgone conclusion. I will refuse to back down from asserting my rights when the time comes. My line in the sand was crossed quite some time ago.
The state thinks it can quell liberty simply by flexing its muscles. On the contrary, an equal, and very much opposite reaction is about to occur.
« Previous Page -- Next Page »
