-(add-hook 'pre-abbrev-expand-hook 'query-if-not-space)
-
-;; @r{This is the function invoked by @code{pre-abbrev-expand-hook}.}
-
-;; @r{If the user terminated the abbrev with a space, the function does}
-;; @r{nothing (that is, it returns so that the abbrev can expand). If the}
-;; @r{user entered some other character, this function asks whether}
-;; @r{expansion should continue.}
-
-;; @r{The function's return value makes no difference.}
-
-(defun query-if-not-space ()
- (if (/= ?\s last-command-char)
- (if (not (y-or-n-p "Do you want to expand this abbrev? "))
- (error "Not expanding this abbrev"))))
+(defun foo-mode-pre-abbrev-expand ()
+ (when (save-excursion (forward-line 0) (eq (char-after) ?#))
+ (let ((local-abbrev-table text-mode-abbrev-table)
+ ;; Avoid infinite loop.
+ (pre-abbrev-expand-hook nil))
+ (expand-abbrev))
+ ;; We have already called `expand-abbrev' in this hook.
+ ;; Hence we want the "actual" call following this hook to be a no-op.
+ (setq abbrev-start-location (point-max)
+ abbrev-start-location-buffer (current-buffer))))
+
+(add-hook 'foo-mode-hook
+ #'(lambda ()
+ (add-hook 'pre-abbrev-expand-hook
+ 'foo-mode-pre-abbrev-expand
+ nil t)))