简体   繁体   中英

automatic header for org-mode in emacs

I am using define-auto-insert '("\\.org\\'" . "org skeleton") macro to insert the header. This is working perfectly fine whenever I'm creating any new org file. But it is also inserting the auto header to the other empty orgs file (created using touch command for some particular reason). Can someone please suggest me any way such that the macro does not add the header in already created empty files when I open them in emacs?

Autoinsert isn't really designed to make this distinction. The simplest way of achieving this is to wrap the auto-insert function with another function and register that with the new file hook instead. For example:

(defun auto-insert-guard ()
   "Prevent auto-insertion for files that exist already"
   (interactive)
   (unless (file-exists-p (buffer-file-name))
     (auto-insert)))

Use this for the hook instead:

(add-hook 'find-file-hook 'auto-insert-guard)

It can obviously be more sophisticated than this, where you might only want to have this guard for certain types of file.

Sometimes you see similar things done with defadvice , but this is often more fragile and can be difficult to understand when you come back to it later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM