Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why shouldn't I use a push-mark

Tags:

emacs

elisp

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?

like image 423
Oleg Pavliv Avatar asked Feb 23 '09 13:02

Oleg Pavliv


2 Answers

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.

like image 157
Gareth Rees Avatar answered Oct 14 '22 20:10

Gareth Rees


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.

like image 36
David Hanak Avatar answered Oct 14 '22 22:10

David Hanak