1

I have the following line in my init.el:

(define-key
  org-mode-map 
  (kbd "C-c C-p") 
  (lambda () (interactive) (org-publish-project "publish-website"))
  )

The "publish-website" argument points to an element of my org-publish-project-alist. This works as expected, except that it moves my point to the start of the buffer when I execute it (which is very annoying). I have two questions (thanks in advance for your help):

  1. What am I doing wrong here?
  2. How do I fix it?
Drew
  • 75,699
  • 9
  • 109
  • 225
User12345
  • 145
  • 4

1 Answers1

3

You're not doing anything wrong, but org-publish probably is. However, you can fix the problem by using save-excursion:

(define-key
  org-mode-map
  (kbd "C-c C-p")
  (lambda ()
    (interactive)
    (save-excursion
      (org-publish-project "publish-website"))))

save-excursion is a macro that saves the point and the current buffer, runs the code you provided, then restores point and the current buffer to the saved values.

db48x
  • 15,741
  • 1
  • 19
  • 23
  • `save-excursion` is exactly what I needed; elisp has so many nice built-in functions I don't know about :) – User12345 Aug 22 '20 at 18:40
  • 1
    There are certainly quite a lot of them. There are a few other `save-foo` macros you might go look up as well. – db48x Aug 22 '20 at 19:01