Shallow Thoughts : : editors
Akkana's Musings on Open Source Computing, Science, and Nature.
Thu, 14 Jul 2011
Seems like every few years I need to change the way I specify my
preferred emacs fonts and window sizes.
Historically this all used to happen from one file, ~/.Xdefaults,
where you set up your defaults for all X programs. In a way that was
nice, since you could set up defaults and see the same font everywhere.
On the other hand, it made for a huge, somewhat hard to read file,
and it's increasingly out of favor on modern desktops, with modern
toolkits like GTK just ignoring it.
Emacs still reads Xdefaults -- but only sort of. A lot of the values I
used to set there no longer work properly. Some time ago I commented out
my various attempts at setting emacs font, like
Emacs*font: -*-clean-bold-*-*-*-13-*-*-*-c-*-*-*
Emacs*font: DejaVu Sans Mono-10:bold
Emacs*font: clean-13:bold
Wmacs*font: Liberation Mono-10:bold
Emacs.font: 7x13bold
Emacs.faceName: Dejavu-10:style=bold
since none of them worked, and worked out a way of setting fonts
inside my .emacs file:
(set-face-attribute 'default nil :font "Terminus-12:bold")
That worked to set the font, but it had another annoying attribute: it
doesn't happen at startup, so it messed up my window size. See, emacs
would start up, see the size I specified in .Xdefaults:
Emacs*geometry: 80x45
and try to set that. But it hadn't read .emacs yet, so it was still
using whatever its default font and size is, and that's huge -- so 45
lines made a window too tall to fit on my laptop screen. Emacs would
then shrink its window to fit the screen (41 lines). Only then would
it open .emacs, whereupon it would see the set-face-attribute, change the
font, and resize the window again, much, smaller, still 41 lines.
What a pain!
The emacs manual, in addition to talking about these various Xdefaults
properties and command-line options, does mention a couple of variables,
set-screen-height
and set-screen-width, that looked promising. I tried putting
(set-screen-height 45) in my .emacs right after I set the font -- no dice.
Apparently that doesn't work because by the time those are read, emacs
has already decided that 41 lines is as big as the window can possibly be.
Here's the answer: another variable that goes inside .emacs,
default-frame-alist, but this one can override that maximum-height
decision that emacs has already made. Here's an example of it in
some useful
defaults for emacs, and based on that, I was able to come up with this:
(setq default-frame-alist
'((top . 10) (left . 2)
(width . 80) (height . 53)
(font . "terminus-iso8859-1-bold-14")
))
Curiously, that height setting, 53, needs to be 3 more than what I
actually want according to the size emacs reports to the window manager.
So don't take the number too seriously; just try numbers a little bigger
than what you actually want until you get the size you're after.
The font setting is the X font specifier: I ran
xlsfonts | grep -i terminus | grep 14
then picked one of the simpler of the lines it printed out, but you
can use a full specifier like
-xos4-terminus-bold-r-normal--14-140-72-72-c-80-iso8859-1
like you get from xfontsel, if you prefer.
Startup still isn't pretty -- emacs still shows a big window at one
place on the screen, resizes it several times then jumps it over to the
top/left coordinates I specified. Of course, I could tell my window manager
to start it in the right place so the jumping-around would be minimized;
but that wouldn't help the visible resizing. Just a minor irritation.
I'm sure there's lots more useful stuff buried in that sample emacs
config file (it was suggested to me when I asked about this on the #emacs
IRC channel), so I'll be reading it to see what else I can glean.
Tags: emacs, editors, X11
[
11:24 Jul 14, 2011
More linux/editors |
permalink to this entry |
comments
]
Wed, 25 May 2011
Most of the time when I edit a text file in vim, I want lines to wrap
automatically as I type, at somewhere around 70 columns wide.
So I set textwidth=70 in .vimrc.
But sometimes that isn't appropriate. For instance, I have a procmail
rules file where I put common spam patterns that programs like
spamassassin don't seem to be able to catch. So I might have lines like:
*^Subject:.*(Ink Cartridges|Hummingbird Vine|who's who of executives|Avandia|Botox|Your Email ID|Zoosk|Best airfares on the internet|UGG Boots|police training)
... and so on -- you get the idea. I can't have lines breaking in the
middle, because then the procmail rule wouldn't work. So every time I
add a new phrase, I have to
:set tw=0 (or one of the other
umpteen ways one can tell vim not to wrap lines) first.
But you can set special behavior for one specific file by adding a
special comment called a "modeline" as the first line of the file.
Procmail treats any line starting with a hash, #, as a comment,
and vim recognizes # as a comment.
So I can add this as the first line of the procmail file:
# vim: set tw=0:
then vim will see that and un-set that default text width I specify
in .vimrc.
Vim understands most common comment styles, so it should understand lines like
/* vim: set tw=0: */ and // vim: set tw=0:
and ; vim: set tw=0: as well.
But to make this work I had to do one more thing: in .vimrc, I had to add
set modeline
Apparently on some versions of vim this is on by default; in others
it's turned off for security
reasons (someone could put an evil modeline into a file which
would make your vim do something when you edited it).
Definitely something to be aware of, but if you mostly edit files
you created yourself on your local machine, and no one else uses your
machine, it's your choice whether to worry about it.
Emacs has modelines too
Emacs has mode lines too, though it calls them
Local
variables lines.
For instance, C++ files in Mozilla's source tend to start with:
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
It's awfully handy to be able to define specific indentation style for the
files within a project, making it easy for emacs users, at least, to
follow your preferred coding style. If only all editors understood them!
Tags: editors, vim, emacs, tip
[
20:26 May 25, 2011
More linux/editors |
permalink to this entry |
comments
]
Fri, 04 Feb 2011
For some time I've been mildly annoyed that whenever I start emacs
and open a file that's under any sort of version control -- cvs,
svn, git or whatever -- I can't start editing right away, because
emacs has to pause for a while and load a bunch of version-control
cruft I never use. Sometimes it also causes problems later,
when I try to write to the file or if I update the directory.
It wasn't obvious what keywords to search for, but I finally found
a combination, emacs prevent OR disable autoload vc
(the vc was the important part), which led me to the
solution (found on
this page):
;; Disable all version control
(setq vc-handled-backends nil)
Files load much faster now!
Tags: editors, emacs, git, tip
[
12:11 Feb 04, 2011
More linux/editors |
permalink to this entry |
comments
]
Wed, 01 Dec 2010
Last week I found myself writing another article that includes code
snippets in HTML.
So what, you ask? The problem is, when you're writing articles in HTML,
every time you include a code snippet inside a <pre> tag you
invariably forget that special characters like < > & have
special meanings in HTML, and must be escaped. Every < has to
change to <, and so forth, after you paste the code.
In vi/vim, replacing characters is straightforward. But I usually
write longer articles in emacs, for various unimportant reasons,
and although emacs has global replace, it only works from wherever
you are now (called "point" in emacs lingo) to the end of the file.
So if you're trying to fix something you pasted in the middle of the
article, you can't do it with normal emacs replace.
Surely this is a wheel that has already been re-invented a thousand
times, I thought! But googling and asking emacs experts turned up nothing.
Looks like I'd have to write it.
And that turned out to be more difficult than I expected, for the same
reason: emacs replace-string works the same way from a
program as it does interactively, and replaces from point to the end
of the file, and there's no way to restrict it to a more limited range.
Several helpful people on #emacs chimed in with ideas, but most of
them didn't pan out. But ggole knew a way to do it that was both
clean and reliable (thanks!).
Here's the elisp function I ended up with.
It uses save-excursion
to put the cursor back where it started before you ran the function,
narrow-to-region to make replace-string work
only on the region, and save-restriction get rid of that
narrow-to-region after we're done. Nice!
(defun unhtml (start end)
(interactive "r")
(save-excursion
(save-restriction
(narrow-to-region start end)
(goto-char (point-min))
(replace-string "&" "&")
(goto-char (point-min))
(replace-string "<" "<")
(goto-char (point-min))
(replace-string ">" ">")
)))
And yes, I used it just now on that elisp snippet.
Tags: emacs, editors, programming
[
19:08 Dec 01, 2010
More linux/editors |
permalink to this entry |
comments
]
Mon, 29 Mar 2010
I maintain the websites for several clubs. No surprise there -- anyone
with even a moderate knowledge of HTML, or just a lack of fear of
it, invariably gets drafted into that job in any non-computer club.
In one club, the person in charge of scheduling sends out an elaborate
document every three months in various formats -- Word, RTF, Excel, it's
different each time. The only regularity is that it's always full of
crap that makes it hard to turn it into a nice simple HTML table.
This quarter, the formats were Word and RTF. I used unrtf to turn
the RTF version into HTML -- and found a horrifying page full of
lines like this:
<body><font size=3></font><font size=3><br>
</font><font size=3></font><b><font size=4></font></b><b><font size=4><table border=2>
</font></b><b><font size=4><tr><td><b><font size=4><font face="Arial">Club Schedule</font></font></b><b><font size=4></font></b><b><font size=4></font></b></td>
<font size=3></font><font size=3><td><font size=3><b><font face="Arial">April 13</font></b></font><font size=3></font><font size=3><br>
</font><font size=3></font><font size=3><b></b></font></td>
I've put the actual page content in bold; the rest is just junk,
mostly doing nothing, mostly not even legal HTML,
that needs to be eliminated if I want
the page to load and display reasonably.
I didn't want to clean up that mess by hand! So I needed some regular
expressions to clean it up in an editor.
I tried emacs first, but emacs makes it hard to try an expression then
modify it a little when the first try doesn't work, so I switched to vim.
The key to this sort of cleanup is non-greedy regular expressions.
When you have a bad tag sandwiched in the middle of a line containing
other tags, you want to remove everything from the <font
through the next > -- but no farther, or else you'll delete
real content. If you have a line like
<td><font size=3>Hello<font> world</td>
you only want to delete through the <font>, not through the </td>.
In general, you make a regular expression non-greedy by adding a ?
after the wildcard -- e.g. <font.*?>. But that doesn't work
in vim. In vim, you have to use \{M,N} which matches
from M to N repetitions of whatever immediately precedes it.
You can also use the shortcut \{-} to mean the same thing
as *? (0 or more matches) in other programs.
Using that, I built up a series of regexp substitutes to clean up
that unrtf mess in vim:
:%s/<\/\{0,1}font.\{-}>//g
:%s/<b><\/b>//g
:%s/<\/b><b>//g
:%s/<\/i><i>//g
:%s/<td><\/td>/<td><br><\/td>/g
:%s/<\/\{0,1}span.\{-}>//g
:%s/<\/\{0,1}center>//g
That took care of 90% of the work, leaving me with hardly any cleanup
I needed to do by hand. I'll definitely keep that list around for
the next time I need to do this.
Tags: regexp, html, editors, vim
[
22:02 Mar 29, 2010
More linux/editors |
permalink to this entry |
comments
]
Tue, 09 Feb 2010
I haven't been using the spare machine much lately. So I hadn't
noticed until last week that since upgrading to the emacs 23.1.1 on
Ubuntu Karmic koala, every time I press the Scroll Lock key -- the
key my KVM uses to switch to the other computer -- with focus in
an emacs window, emacs beeps and complains that the key is unbound.
That was a problem I thought I'd solved long ago, an easy fix in
.emacs:
(global-set-key [scroll-lock] 'ignore)
But in emacs 23, it wasn't working any more. Emacs listed the key
as "<Scroll_Lock>", but using that directly in global-set-key
doesn't work.
The friendly and helpful (really!) crew at #emacs found me a
solution, after some fiddling around.
(global-set-key (kbd "<Scroll_Lock>") 'ignore)
Tags: emacs, editors, kvm, tips
[
22:47 Feb 09, 2010
More linux/editors |
permalink to this entry |
comments
]
Wed, 13 Jan 2010
To wrap long lines, or not to wrap? It's always
a dilemma. Automatic wrapping is great when you're hammering away
typing lots of text. But it's infuriating when you're trying to format
something yourself and the editor decides it wants to line-wrap a
little too early.
Although of course you can set the wrapping width, Emacs has a tendency
to wrap early -- especially when you hit return. All too often, I'll
be typing away at a long line, get to the end of the sentence and
paragraph with the last word on the same line with the rest -- then
realize that as soon as I hit return, Emacs is going to move that
last word to a line by itself. Drives me nuts!
And the solution turns out to be so simple. The Return key,
"\C-m". was bound to the (newline) function (you can find out
by typing M-x, then describe-key, then hitting Return).
Apparently (newline) re-wraps the current line before
inserting a line break. But I just wanted it to insert a line break.
No problem -- just bind "C-m" to (insert "\n").
But there's a second way, too, if you don't want to rebind:
there's a magic internal emacs table you can change.
(set-char-table-range auto-fill-chars 10 nil)
But wait -- there's one other thing I want to fix in text mode.
Automatic indent is another one of those features that's very
convenient ... except when it's not.
If I have some text like:
First point:
- subpoint a
- subpoint b
then it's handy if, when I hit Return after
subpoint a,
emacs indents to the right level for
subpoint b.
But what happens when I get to the end of that list?
First point:
- subpoint a
- subpoint b
Second point:
- subpoint c
When I hit Return after subpoint b, Emacs quite reasonably
indents two spaces. If I immediately type another Return,
Emacs sensibly deletes the two spaces it just inserted, opens a
new line -- but then it indents that new line another two spaces.
After a blank line, I always want to
start at the beginning, not indented at all.
Here's how to fix that. Define a function that will be called
whenever you hit return in text mode. That function tests whether the
caret comes immediately after a blank line, or at the beginning of
the file. It indents except in those two cases; and in neither case
does it re-wrap the current line.
;; In text mode, I don't want it auto-indenting for the first
;; line in the file, or lines following blank lines.
;; Everywhere else is okay.
(defun newline-and-text-indent ()
"Insert a newline, then indent the next line sensibly for text"
(interactive)
(cond
;; Beginning of buffer, or beginning of an existing line, don't indent:
((or (bobp) (bolp)) (newline))
;; If we're on a whitespace-only line,
((and (eolp)
(save-excursion (re-search-backward "^\\(\\s \\)*$"
(line-beginning-position) t)))
;; ... delete the whitespace, then add another newline:
(kill-line 0)
(newline))
;; Else (not on whitespace-only) insert a newline,
;; then add the appropriate indent:
(t (insert "\n")
(indent-according-to-mode))
))
Then tell emacs to call that function when it sees the Return key in
text mode:
(defun text-indent-hook ()
(local-set-key "\C-m" 'newline-and-text-indent)
)
(setq text-mode-hook 'text-indent-hook)
Finally, this is great for HTML mode too, if you get irritated at
not being able to put an <a href="longurl"> all on one line:
(defun html-hook ()
(local-set-key "\C-m" (lambda () (interactive) (insert "\n")))
)
(setq sgml-mode-hook 'html-hook)
Tags: emacs, editors, tips
[
10:29 Jan 13, 2010
More linux/editors |
permalink to this entry |
comments
]
Wed, 29 Jul 2009
Wouldn't it be nice if Emacs HTML mode had a way to insert HTML
tags, so you didn't have to type <b></b> all the time?
Sort of like what's described in
this page --
except that page describes an HTML mode that clearly isn't the
one that's installed on Ubuntu, since none of those bindings
actually work?
I've been meaning to figure out a way to do that for ages, and
finally got around to it. Turns out Emacs SGML mode (which is really
what Ubuntu installs and uses for HTML files) doesn't have functions
for specific HTML tags like <b>, but it does have a general
tag-inserting function.
Type C-c C-t -- emacs prompts you for the tag, so type
b or whatever, and hit return -- and you get the tag, with
the cursor correctly positioned for you to type your new bold text.
But that's four keystrokes. What if you want shorter bindings for
particular tags, like C-b C-b to insert a bold?
For that, you need to use a lambda and a mode hook. In your .emacs
it looks like this:
;; Define keys for inserting tags in HTML mode:
(defun html-hook ()
(local-set-key "\C-c\C-b" (lambda () (interactive) (sgml-tag "b")))
)
(setq sgml-mode-hook 'html-hook)
There's apparently also supposed to be a command bound to C-c /
that closes the current tag, but my version of sgml-mode doesn't
bind anything to that key, and the only likely-looking function name,
sgml-maybe-end-tag, doesn't end the current tag.
Such is life!
But one more don't-miss feature that I'd missed all along is C-c C-n:
type it before a special character like < or & and emacs will insert
the appropriate < or & for you. Nice!
(Thanks to bojohan on #emacs for the tips!)
Tags: editors, emacs, html-mode
[
20:31 Jul 29, 2009
More linux/editors |
permalink to this entry |
comments
]