Emacs Hooks - An Introduction
Ranked #7,129 in Computers & Electronics, #149,313 overall
Emacs Hooks - How to Customize Emacs
Windows Notepad users can add .LOG to the top of a textfile in order to support a crude diary. Every time that file is opened, the date is inserted. We could add a function to find-file-hook to get a similar effect. How do you do this?
If you like this lens and found it useful, please consider giving it a rating. Thanks.
Once you have used hooks to customize emacs you might want to look at creating a full application using emacs comint.
Photo by Southern Foodways Alliance
Defining The Hook Function
The step by step guide
(defun jd-add-timestamp ()
Then, only when we are looking-at the text ".LOG " ... (in regexes period (.) means match anything, so we need to escape it with double backslash.)
(when (looking-at-p "\\.LOG ")
Go forward 5 characters... (".LOG " is 5 characters long.)
(forward-char 5)
Insert the timestamp... (format-time-string enables you to include whichever time elements you want, and in any order. %Y represents the year, %m the month, %d the day. Type C-h f format-time-string<RET> for more information.)
(insert (format-time-string "%Y%m%d %H:%M:%S"))
Insert two newlines...
(insert "\n\n")
...and go to the previous line
(previous-line)
Finally, add this function to find-file-hook.
(add-hook 'find-file-hook 'jd-add-timestamp)
The complete code looks like this:
(defun jd-add-timestamp ()
(when (looking-at-p "\\.LOG ")
(forward-char 5)
(insert (format-time-string "%Y%m%d %H:%M:%S"))
(insert "\n\n")
(previous-line)
(message "Added timestamp")))
(add-hook 'find-file-hook 'jd-add-timestamp)
How Do You Find Out Which Hooks are Available?
By convention, the difference between a hook ending in -hook and one ending in -functions is that one ending in -functions will have some arguments. For example, hooks added to after-change-functions, which is called each time the text in a buffer changes, are called with three arguments:
"the positions of the beginning and end of the range of changed text,
and the length in bytes of the pre-change text replaced by that range."
My Emacs Lenses
Reader Feedback
Was This Useful
Let me know if there is anything you would add or change. Thanks.
-
Reply
-
amy1985
Apr 19, 2009 @ 8:50 am | delete
- Thanks this is a great step by step, easy to follow guide. You made something which appears very complicated much more simple - thanks again.
-
Latest Emacs Blog Posts
- Programmer personality types: 13 profiles in code
- I know someone who hooked up a version of Eliza to Emacs so that he could add Eliza-grade comments to his code with the push of a button. No one noticed, he said, because even his manager had written scripts to count the words in the comments of the ...
Vanilla Emacs 23 Hook List
abbrev-with-wrapper-hook
activate-mark-hook
after-change-major-mode-hook
after-init-hook
after-revert-hook
after-save-hook
after-setting-font-hook
apropos-mode-hook
auto-save-hook
before-hack-local-variables-hook
before-init-hook
before-make-frame-hook
before-revert-hook
before-save-hook
buffer-menu-mode-hook
change-major-mode-hook
clone-buffer-hook
clone-indirect-buffer-hook
compilation-mode-hook
completion-list-mode-hook
completion-setup-hook
custom-define-hook
cvs-dired-use-hook
deactivate-mark-hook
delete-frame-hook
disabled-command-hook
echo-area-clear-hook
emacs-lisp-mode-hook
emacs-startup-hook
epa-file-find-file-hook
erc-ctcp-query-DCC-hook
exit-language-environment-hook
expand-abbrev-hook
find-file-hook
find-tag-hook
first-change-hook
grep-setup-hook
hack-local-variables-hook
help-mode-hook
inferior-lisp-mode-hook
input-method-activate-hook
input-method-after-insert-chunk-hook
input-method-inactivate-hook
isearch-mode-end-hook
isearch-mode-hook
kill-buffer-hook
kill-emacs-hook
lisp-interaction-mode-hook
lisp-mode-hook
mail-citation-hook
mail-setup-hook
make-local-hook
menu-bar-update-hook
minibuffer-exit-hook
minibuffer-setup-hook
minibuffer-with-setup-hook
mouse-leave-buffer-hook
next-error-follow-mode-post-command-hook
next-error-hook
occur-hook
occur-mode-find-occurrence-hook
occur-mode-hook
paragraph-indent-text-mode-hook
pmail-delete-message-hook
pmail-get-new-mail-hook
pmail-mode-hook
pmail-quit-hook
pmail-show-message-hook
post-command-hook
post-gc-hook
pre-abbrev-expand-hook
pre-command-hook
resume-suspend-hook
rfn-eshadow-setup-minibuffer-hook
rfn-eshadow-update-overlay-hook
rmail-delete-message-hook
rmail-get-new-mail-hook
rmail-mode-hook
rmail-quit-hook
rmail-show-message-hook
set-language-environment-hook
special-mode-hook
suspend-hook
suspend-resume-hook
table-cell-map-hook
table-load-hook
table-point-entered-cell-hook
table-point-left-cell-hook
temp-buffer-setup-hook
temp-buffer-show-hook
term-setup-hook
text-mode-hook
tooltip-hook
vc-before-checkin-hook
vc-checkin-hook
vc-checkout-hook
vc-default-find-file-hook
vc-default-find-file-not-found-hook
vc-file-not-found-hook
vc-find-file-hook
vc-kill-buffer-hook
view-mode-hook
window-configuration-change-hook
window-setup-hook
Vanilla Emacs 23 Functions List
after-change-functions
after-insert-file-functions
after-make-frame-functions
auto-coding-functions
before-change-functions
buffer-access-fontify-functions
choose-completion-string-functions
comint-output-filter-functions
command-line-functions
custom-print-functions
delete-frame-functions
delete-terminal-functions
find-directory-functions
find-file-not-found-functions
font-lock-extend-region-functions
fontification-functions
jit-lock-after-change-extend-region-functions
kill-buffer-query-functions
kill-emacs-query-functions
kkc-after-update-conversion-functions
resume-tty-functions
signal-hook-function
suspend-tty-functions
update-leim-list-functions
window-scroll-functions
window-size-change-functions
window-text-change-functions
write-contents-functions
write-file-functions
write-region-annotate-functions
x-lost-selection-functions
x-sent-selection-functions
by jareddavison2009
My Emacs Lenses:
Creating An Emacs Command
Emacs Hooks - An Introduction
All About Ediff
more »
- 5 featured lenses
- Winner of 3 trophies!
- Top lens » The Magic of Emacs Comint
Explore related pages
- *2012 Horse Calendars *2012 Horse Calendars
- VIM the Powerful Text Editor VIM the Powerful Text Editor
- You too can be an organized mom You too can be an organized mom
- Organised Mum Family Life Book Organised Mum Family Life Book
- Diaries and Journals for 2012 Diaries and Journals for 2012
- Luxury Diaries - Taschen Luxury Diaries - Taschen