使用astyle格式化代码后,如何回到当前编辑的那一行

配置如下,保存文件的时候会用astyle格式化,但是问题是格式化完了以后会自动跳到第一行。 请问有没有办法格式化完了以后回到保存时的行数?

 (defvar astyle-command "F:\\astyle\\bin\\AStyle.exe -n -c -j -Z")
  (defun astyle-region (start end)
    "Run astyle on region, formatting it in a pleasant way."
    (interactive "r")
    (save-excursion
      (shell-command-on-region start end astyle-command nil t (get-buffer-create "*Astyle Errors*") nil)
      )
    )
  (defun astyle-buffer ()
    "Run astyle on whole buffer, formatting it in a pleasant way."
    (interactive)
    (save-excursion
      (astyle-region (point-min) (point-max))))
  (add-hook 'c-mode-common-hook
    '(lambda ()
       (define-key c-mode-map "\C-cr" 'astyle-region)
       (define-key c-mode-map "\C-cb" 'astyle-buffer)
       (define-key c++-mode-map "\C-cr" 'astyle-region)
       (define-key c++-mode-map "\C-cb" 'astyle-buffer)))
  (add-hook 'before-save-hook
    '(lambda ()
       (if (file-exists-p (buffer-file-name))
         (if (member (file-name-extension (buffer-name))
               '("cpp" "h" "c"))
           (astyle-buffer))
         )))

这里有解决方案: http://stackoverflow.com/questions/8115460/emacs-save-excursion-not-restoring-point

搞定,多谢:grinning:

有办法同时保证当前行在窗口中位置不变吗?目前试下来发现有时候改变当前行距顶行的位置