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

Sunday, October 10, 2010

Emacs Tip #37: fic-mode.el

I saw this question on Stack Overflow asking to highlight FIXME (and similar strings) in your code, but only in comments and strings.

The current fixme-mode.el found on the Emacs Wiki is kind of clunky (it's really a major mode) and awkward to read. So I took the challenge to write a new minor-mode which answers the question.

I give you fic-mode.el. It's named fic as an acronym for Fixme In Comments.

To use it, add something like the following to your .emacs:



(require 'fic-mode)
(add-hook 'c++-mode-hook 'turn-on-fic-mode)
(add-hook 'emacs-lisp-mode-hook 'turn-on-fic-mode)



Or, you can manually start it with M-x fic-mode.

Wednesday, April 28, 2010

Emacs Tip #36: Abort the minibuffer when using the mouse

A friend of mine loves using Emacs, but is always complaining about something. This time it's Emacs' behavior to keep the minibuffer active when you use the mouse to select another window. For example, you start doing a C-x C-f, click elsewhere, and do the C-x C-f again. Emacs will beep and tell you Command attempted to use minibuffer while in minibuffer.

After swallowing my, "well don't use the mouse" response I set about trying to fix it. I'm sure this has been solved before, I just didn't have enough google-fu to find the solution.


(defun stop-using-minibuffer ()
"kill the minibuffer"
(when (and (>= (recursion-depth) 1) (active-minibuffer-window))
(abort-recursive-edit)))

(add-hook 'mouse-leave-buffer-hook 'stop-using-minibuffer)


Edited to add:
A commenter suggested using recursive minibuffers. I think that the resulting behavior is more confusing than helpful.

Yes, it avoids the error, but then you get this ever-increasing stack of minibuffers building up. If/when the user notices the minibuffer hanging around it'll be annoying (I've had people ask, "Why is it trying to find a file?" (because the minibuffer still shows Find file: /path/to/somewhere)). And heaven forbid they click in the minibuffer, type C-g and get back to the window configuration they were looking at when they started that command, which was ... 15 minutes ago.

Saturday, February 20, 2010

Emacs Tip #35: framemove

Ever wanted to easily navigate between frames? Perhaps using arrow keys? Surprisingly, this didn't exist before (AFAIK).

I wrote the package framemove to have the same usage as Emacs' built in windmove package. And, even better, it can integrate with windmove so that when you run out of windows to move between, you'll jump to the next frame in that direction.

To install framemove on its own:


(require 'framemove)
(framemove-default-keybindings) ;; default prefix is Meta

But you might want to use this in conjunction with windmove, in which case this is the integration code to add to your .emacs:

(require 'framemove)
(windmove-default-keybindings)
(setq framemove-hook-into-windmove t)

With the integration with windmove, you just do SHIFT-right to move focus to the window to the right of the current, and when there are no more, focus will shift to the frame to the right.

Switching frames is a little more challenging than switching windows - mostly because windows are simply rectangular divisions of a window - it is easy to tell what window (if any) is to the right of the point. For frames, the answer is ambiguous because the placements of frames is free-form, and frames can overlap. I think this initial implementation does a reasonable job of choosing which frame to select. It first tries the frame directly in line with the point, and if one doesn't exist, it then uses the distance between the point (projected onto the edge of the current frame) and the frames in the given direction, and after that it just tries any frame that extends beyond the current frame in that direction.

Check it out.

Currently, when focus shifts to a new frame - the point does not change inside the frame.

Thursday, February 11, 2010

Emacs Tip #34: line-beginning-position, line-end-position

Such a simple thing - finding the position of the beginning or end of the current line. I've been completely oblivious to the existence of the functions line-beginning-position and line-end-position.

They return the position of the beginning or end of the current line.
They both accept an optional argument, which has the same
meaning as the argument to beginning-of-line or end-of-line.

They were first introduced in Emacs 20, in the middle of 2006. Which means I've been unnecessarily typing this for 4 years:
(save-excursion
(beginning-of-line)
(point))

Friday, June 12, 2009

Emacs Tip #30: igrep

Finding text in files is an everyday (every hour) occurrence for most programmers. And in Unix-land, people generally use grep to do the searching.

Of course Emacs has an interface to grep, which now (as of Emacs 22) even has interfaces for using both find and grep together to search through directory structures (grep-find, rgrep).

But, before those existed, someone wrote a nice package igrep.el, which has a very intuitive (IMO) interface.

M-x igrep-find RET RET /search/in/here/*.cxx RET

which will search for regexp in all the files with the extension .cxx underneath the /search/in/here/ directory.

As you'd expect, the results of the search show up in a buffer, and clicking on any one of them jumps you to that spot in the file. And the standard M-x next-error (aka C-x `) will jump you to the next match.

About the only thing I don't like is that the paths returned in the results are the fully expanded path. In other words, when the paths to the files are really long, most of the result buffer is taken up with just the pathname. So I wrote this little snippet of code which post-processes the buffer to trim the names relative to the search path entered (in the example above, all the /search/in/here/ would be removed from the file names.


(defun my-shorten-filenames-in-compilation (buffer &optional stat)
"remove the absolute filenames if they match the default-directory"
(interactive "b")
(save-excursion
(set-buffer buffer)
(goto-char (point-min))
(let ((buffer-read-only nil)
(base-dir (if (re-search-forward "find \\([^ ]+\\) " nil t)
(if (string-match "/$" (match-string 1))
(match-string 1)
(concat (match-string 1) "/"))
default-directory)))
(setq default-directory base-dir)
(while (re-search-forward (concat "^" default-directory) nil t)
(replace-match "")))))

(add-hook 'compilation-finish-functions 'my-shorten-filenames-in-compilation)


I do realize that the built-in M-x rgrep does this automatically, I just prefer the igrep interface.

Monday, April 27, 2009

dot-emacs trickery (emacs-fu)

It's been a while, and to be honest, emacs-fu has been doing such a great job putting up useful snippets of Emacs information...

He just put up a question asking for folk's favorite dot-emacs tricks. I added my two cents with:

(defun tj-find-file-check-make-large-file-read-only-hook ()
"If a file is over a given size, make the buffer read only."
(when (> (buffer-size) (* 1024 1024))
(setq buffer-read-only t)
(buffer-disable-undo)
(message "Buffer is set to read-only because it is large. Undo also disabled.")))


(add-hook 'find-file-hooks 'tj-find-file-check-make-large-file-read-only-hook)

Tuesday, August 19, 2008

Emacs Tip #25: Shell Dirtrack By Prompt

Note: I've since been notified of this problem already being solved by a package provided with Emacs. I looked at it and prefer the implementation in this post, only because it modifies the prompt so I don't have to see the full path in my prompt.

I'd been butting my head against this issue for a while now, but wasn't quite annoyed enough to come up with a solution. Luckily for me (and you), ccthomas wrote this little snippet which parses your shell prompt to determine the current directory.

Other than having to modify your shell prompt (no big deal), it's super clean and easy.

I modified the code slightly because I didn't see the need/purpose for/of the "%300<.<" in the prompt. So here' s my slightly modified version:


(add-hook 'shell-mode-hook
#'(lambda ()
(shell-dirtrack-mode nil)
(add-hook 'comint-preoutput-filter-functions
'shell-sync-dir-with-prompt nil t)))

(defun shell-sync-dir-with-prompt (string)
"A preoutput filter function (see `comint-preoutput-filter-functions')
which sets the shell buffer's path to the path embedded in a prompt string.
This is a more reliable way of keeping the shell buffer's path in sync
with the shell, without trying to pattern match against all
potential directory-changing commands, ala `shell-dirtrack-mode'.

In order to work, your shell must be configured to embed its current
working directory into the prompt. Here is an example .zshrc
snippet which turns this behavior on when running as an inferior Emacs shell:

if [ $EMACS ]; then
prompt='|Pr0mPT|%~|[%n@%m]%~%# '
fi

The part that Emacs cares about is the '|Pr0mPT|%~|'
Everything past that can be tailored to your liking.
"
(if (string-match "|Pr0mPT|\\([^|]*\\)|" string)
(let ((cwd (match-string 1 string)))
(setq default-directory
(if (string-equal "/" (substring cwd -1))
cwd
(setq cwd (concat cwd "/"))))
(replace-match "" t t string 0))
string))


The original can be found on the Emacs Wiki: Shell Dirtrack By Prompt

Wednesday, June 4, 2008

A guided tour of Emacs

I recently came across the GNU page: A guided tour of Emacs. It is a gem of an introduction to Emacs. Unlike the help distributed with Emacs (tutorial, FAQ, *info* pages), this tour does a good job illustrating the wide variety of Emacs capabilities, and I think it is much more motivating for a newbie than anything else I've seen.

Monday, June 2, 2008

Emacs Tip #24: view-emacs-problems

M-x view-emacs-problems

Like any good tool, Emacs can tell you about problems about which it knows. I just learned something myself (see 'Emacs pauses for several seconds when changing the default font').

To see the problems file, use the keybinding:
   C-h C-e
[edited to correct binding, thanks rodrigo]

Friday, May 30, 2008

Emacs Tip #23: set-goal-column

M-x set-goal-column  (C-x C-n)

Emacs generally tries to maintain the same horizontal position for the
cursor as you move up and down in the buffer. However, you may want
to have it "remember" a particular position when perusing a data file
of some sort - perhaps when a certain column has particular interest.

C-x C-n does just that. It sets the current horizontal position as
the goal column. You can move around the buffer however you want, but
as soon as you use C-n or C-p, you're moved back to the goal column.

To unset, just use the universal prefix:

C-u C-x C-n

Tuesday, May 20, 2008

Emacs Tip #22: blink-cursor-mode

I couldn't care less about whether or not the cursor blinks. (note proper use of that phrase, rare in this culture)

However, some people really like it, need it, and some people don't like it at all. A recent thread in comp.emacs brought this to light.

So, if you'd like your cursor to blink (or not), add the line:


(blink-cursor-mode 1) ;; or pass in -1 to turn it off

to your .emacs. By default the cursor will blink.

You can also have the cursor stretch to cover the full width of the glyph (character) under it. e.g. it'd be as wide as the tab character (should there be one in your buffer):

(setq x-stretch-cursor t)

For more documentation on cursor display, read the manual.

Monday, May 5, 2008

Emacs Tip #20: Exiting Emacs

I almost never use this tip, but some might find it handy:

    C-x C-c
That is the key-binding to exit emacs, it runs save-buffers-kill-emacs, and the documentation for exiting emacs can be read here: Exiting - GNU Emacs Manual

Monday, April 28, 2008

Emacs Tip #19: Startup Options (-q)

The most useful two options I've found for starting up emacs are:

    -q
and
    -debug-init
The first disables the loading of your .emacs and default.el files, which is handy when you want to differentiate between problems in your .emacs file and problems outside (.Xdefaults, site specific .emacs, etc.).

The second gives you a backtrace when any error happens during loading of your .emacs file. While it doesn't often pinpoint the exact line where things went wrong, it usually gives you a clue as to where to start looking for problems.

For all options, read the manual here: Initial Options - GNU Emacs Manual

Monday, April 7, 2008

Emacs Tip #17: flyspell and flyspell-prog-mode

Being in front of the computer hasn't helped my spelling because it's so easy to let the computer catch and fix the spelling errors for me. Of course Emacs has spell checking (M-x spell-{buffer,region,string,word}), but what is really handy is spell-checking as you type.

Enter flyspell.

flyspell is a minor-mode, so enabling it only causes spell-checking in that buffer, so it's handy to add the hooks to turn it on for the modes you deem appropriate (mail messages, text files, etc.). But wait, you do most of your work programming, right? Just turn on flyspell-prog-mode, and only your comments and strings will be checked for spelling errors.

It has the standard installation - only the major entry point is the routine 'flyspell-mode where the filename is "flyspell.el", so you need to tell Emacs to look for that appropriately. Here's what I use to enable flyspell and turn it on for a couple of different modes.


(autoload 'flyspell-mode "flyspell" "On-the-fly spelling checker." t)
(add-hook 'message-mode-hook 'turn-on-flyspell)
(add-hook 'text-mode-hook 'turn-on-flyspell)
(add-hook 'c-mode-common-hook 'flyspell-prog-mode)
(add-hook 'tcl-mode-hook 'flyspell-prog-mode)
(defun turn-on-flyspell ()
"Force flyspell-mode on using a positive arg. For use in hooks."
(interactive)
(flyspell-mode 1))

Tuesday, April 1, 2008

Emacs Tip #16: check-parens

Ever had trouble finding a mismatched parentheses in a file? Of course not, you don't make mistakes.

But, if you do, you can use the built in functionality of M-x check-parens

The documentation is here Parentheses - GNU Emacs Manual

It doesn't pinpoint the problem as closely as I would have hoped in some modes, but it's better than nothing.

Monday, March 17, 2008

Emacs Tip #14: caps lock on windows

In my previous job, everyone had Windows laptops, and worked using VNC. But they never learned the trick to change the Caps Lock key into a Ctrl key.

From the GNU Emacs FAQ for Windows: "Caps"

Download caps-as-ctrl.reg to make CapsLock a Control key (leaving your original control keys as they were), or caps-ctrl-swap.reg to swap CapsLock and the left Control key. Once you've downloaded them, double-click on the .reg files in Explorer or "run" them from a command prompt to have them update your registry.
If you use Emacs and you are using Windows and your Caps Lock key is still functioning as Caps Lock, just download the registry change and install it. If you really think you need to type in all caps, learn M-x upcase-region

Monday, March 10, 2008

Emacs Tip #13: browse-kill-ring

The kill-ring. You know about the kill-ring, right?

C-h i m emacs RET kill ring RET

Ok, now you know about the kill-ring, it is basically a list of all the chunks of text that have been cut (C-w) (or just saved using M-w). The basic interaction with the kill-ring is:

C-y (aka "yank" aka "paste")

After you've pasted text, if you didn't want that, but an earlier chunk of text, the key M-y will cycle through the earlier chunks, replacing what was just pasted with the earlier text.

If that's too much for you to handle, you can browse the entries in the kill-ring and paste them in (more than one if you want).

To get it, go to the wiki and download it.

To install and use, do the standard download, load-path manipulation, and:

(require 'browse-kill-ring)
M-x browse-kill-ring

[edited to correct the cut/paste commands]
[edited to fix broken link]

Monday, March 3, 2008

Emacs Tip #12: show-trailing-whitespace

If you don't like having lines of code/text with whitespace at the
ends, you can turn on the variable 'show-trailing-whitespace' to have
Emacs highlight the offending whitespace.

When set, the variable's value becomes buffer local, so set it to true
in the mode-hooks for your preferred modes. Or, if you want it on all
the time, change the default value with:


(setq-default show-trailing-whitespace t)

Monday, January 21, 2008

Emacs Tip #11: uniquify

Do you dislike the buffer names Emacs generates when two files have the same name? e.g. "myfile.txt" and "myfile.txt<2>"

A package 'uniquify' changes the default naming of buffers to include parts of the file name (directory names) until the buffer names are unique.

For instance, buffers visiting the files:


/u/mernst/tmp/Makefile
/usr/projects/zaphod/Makefile

would be named

Makefile|tmp
Makefile|zaphod

respectively (instead of "Makefile" and "Makefile<2>").

There are other naming schemes possible.

I use this to my .emacs:

(require 'uniquify)
(setq uniquify-buffer-name-style 'reverse)
(setq uniquify-separator "/")
(setq uniquify-after-kill-buffer-p t) ; rename after killing uniquified
(setq uniquify-ignore-buffers-re "^\\*") ; don't muck with special buffers


For the *info* page,

C-h i m emacs RET s uniquify RET

Thursday, December 27, 2007

9 Tips for the aspiring Emacs playboy | LispCast

Ha, saw this today and had to laugh because of the images. They are pretty good tips.


9 Tips for the aspiring Emacs playboy


The only other tip I'd add would be to subscribe to this blog.