* Checking if find-file-hook was called interactively
@ 2010-08-27 8:52 Nordlöw
2010-08-28 0:20 ` Stefan Monnier
0 siblings, 1 reply; 2+ messages in thread
From: Nordlöw @ 2010-08-27 8:52 UTC (permalink / raw
To: help-gnu-emacs
Hey, there!
I have the following assistant that auto-inserts a C/C++ template code
when opening new or empty C/C++ files. How can I make this auto-
insertion be inhibited when find-file is called non-interactively?
This because I want prevent semantic's scanning functions from trigger
this template-auto-insertion.
I could solve this by either redefining find-file, advising find-file,
or defining a new function find-file-auto-template bound to C-x C-f.
But there are other variants of find-file I use so I would rather not
have to modify all of them so I guess advising find-file is the best
alternative in that case.
Thanks in advance,
Per Nordlöw
Exert of code follows. Note that buffer-empty-p and c-insert-header-
template is not given here.
(defun c-auto-insert-empty-file-template ()
"Interactive insertion of file creation template."
(interactive)
(unless buffer-read-only
(let ((filepath (buffer-file-name)))
(if (buffer-empty-p)
;; empty buffer
(progn
(if (fmd-file-match filepath 'C-Header 'name-recog)
(if (y-or-n-p (format "Insert C Header Template? "))
(c-insert-header-template filepath "y")))
(if (fmd-file-match filepath 'C++-Header 'name-recog)
(if (y-or-n-p (format "Insert C++ Header Template? "))
(c-insert-header-template filepath "y")))
(if (fmd-file-match filepath 'C-Source 'name-recog)
(if (y-or-n-p (format "Insert C Source Template? "))
(c-insert-source-template filepath)))
(if (fmd-file-match filepath 'C++-Source 'name-recog)
(if (y-or-n-p (format "Insert C++ Source Template? "))
(c-insert-source-template filepath)))
)))))
(add-hook 'find-file-hook 'c-auto-insert-empty-file-template t)
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Checking if find-file-hook was called interactively
2010-08-27 8:52 Checking if find-file-hook was called interactively Nordlöw
@ 2010-08-28 0:20 ` Stefan Monnier
0 siblings, 0 replies; 2+ messages in thread
From: Stefan Monnier @ 2010-08-28 0:20 UTC (permalink / raw
To: help-gnu-emacs
> (defun c-auto-insert-empty-file-template ()
> "Interactive insertion of file creation template."
> (interactive)
> (unless buffer-read-only
> (let ((filepath (buffer-file-name)))
> (if (buffer-empty-p)
> ;; empty buffer
> (progn
> (if (fmd-file-match filepath 'C-Header 'name-recog)
> (if (y-or-n-p (format "Insert C Header Template? "))
> (c-insert-header-template filepath "y")))
> (if (fmd-file-match filepath 'C++-Header 'name-recog)
> (if (y-or-n-p (format "Insert C++ Header Template? "))
> (c-insert-header-template filepath "y")))
> (if (fmd-file-match filepath 'C-Source 'name-recog)
> (if (y-or-n-p (format "Insert C Source Template? "))
> (c-insert-source-template filepath)))
> (if (fmd-file-match filepath 'C++-Source 'name-recog)
> (if (y-or-n-p (format "Insert C++ Source Template? "))
> (c-insert-source-template filepath)))
> )))))
> (add-hook 'find-file-hook 'c-auto-insert-empty-file-template t)
I'd recommend you use auto-insert-mode rather than your own code.
But you may find that auto-insert suffers from the same problem, in
which case you may want to M-x report-emacs-bug.
Now I'm not sure whether the bug is in Semantic which should not visit
those files with find-file-noselect but with something of lower level,
or in auto-insert which should be more careful to only perform the
auto-insertion (and/or prompt the user) when the command is really
a "find-file"-like command.
Stefan
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2010-08-28 0:20 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-08-27 8:52 Checking if find-file-hook was called interactively Nordlöw
2010-08-28 0:20 ` Stefan Monnier
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.