tags:

views:

184

answers:

2

I implemented a small function, which parses an SQL INSERT statement and highlights a column value when a cursor is on a column name and vice versa.

Then I wanted to add a possibility to quickly jump between column name and column value. I used push-mark in my implementation, so I can jump with C-x C-x (exchange-point-and-mark). It works too, the only thing which bothers me is a elisp doc, which says

Novice Emacs Lisp programmers often try to use the mark for the wrong purposes. The mark saves a location for the user's convenience. Most editing commands should not alter the mark.

My usage of mark - is it correct? Or what would be a better solution?

+2  A: 

AFAIK, this is the correct use of push-mark. I think the documentation discourages the use of push-mark instead of save-excursion for the purpose of saving the state of the buffer while performing certain operations.

David Hanak
FWIW, the documentation's intention is to make the programmer write (let ((foo(point))) ... (goto-char foo)) instead of the something like(set-mark) ... (exchange-point-and-mark).
jrockway
+6  A: 

Consider an analogy with the position of point: the user only wants point to move when they issue a point-moving command. It would be exceedingly annoying if random operations like font-locking moved the point. Hence the recommendation to wrap function bodies in (save-excursion ...).

If your function sets the mark explicitly for the user, that's fine. (In this case I suggest calling your function something like sql-mark-column-value to make it clear that setting the mark is one of the things it does.) The point of the documentation you quoted is that commands should not set the mark incidentally as a result of doing something else.

If your function just happens to set the mark when the user places point on a a column name in a SQL statement, that's probably not so convenient. Imagine a use case of someone trying to cut or copy a section of a SQL statement; every time they try to move point within the statement their mark gets clobbered! For this use case you probably want to provide a separate command like sql-goto-column-value instead of relying on exchange-point-and-mark.

Of course, if this is purely for your own use, anything goes.

Gareth Rees