Emacs - move cursor back after mark-whole-buffer & indent-region

370
xwinus

I'm Emacs newbie and for automatic re-formating of my source codes in Emacs I've created simple macro using mark-whole-buffer and indent-region commands, mapped to C-j shortcut:

(fset 'format-document "\C-[xmark-whole-buffer\C-m\C-[xindent-region\C-m") (global-set-key (kbd "C-j") 'format-document) 

However, when executed, the original position of cursor is lost and new cursor position is set to the beginning of the buffer. Is there any way how to perform this macro and return the cursor back to its previous position? I'm using GNU/Emacs 24.3 on Ubuntu 14.04.

Thanks

4

1 ответ на вопрос

2
lawlist

Instead of using a macro, consider using a function. The function indent-region contains arguments for the beginning and ending of the region. Thus, you could evaluate (indent-region (point-min) (point-max)) to handle the entire buffer. You could also use a simple function to do the same thing:

(defun my-format-document () (interactive) (indent-region (point-min) (point-max)) ) 

Although not needed here, in the future you may need to use something like save-excursion which brings you back to the original point.

решил мою проблему, большое спасибо, к сожалению не могу дать вам +1 сейчас из-за моего низкого уровня репутации xwinus 10 лет назад 1