help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Emacs lisp question


From: Barry Margolin
Subject: Re: Emacs lisp question
Date: Thu, 02 Jun 2016 15:20:33 -0000
User-agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X)

In article <mailman.616.1460989677.7477.help-gnu-emacs@gnu.org>,
 Matthias Pfeifer <mpfeifer77@gmail.com> wrote:

> Hi there,
> 
> My emacs version is 25.0.91.1 and it's running on Windows 7. I have this
> piece of Emacs lisp that does insert a template file in a fresh java-mode
> buffer and start my "pre-processor"
> 
> 
> (add-to-list 'auto-insert-alist '(".*\\.java$" . [ "template.java"
> mp:java-preprocessor] ))
> 
> pre-processor looks like this:
> 
> (defun mp:java-preprocessor()
>   (let ((classname (file-name-sans-extension (buffer-name)))
>     (packagename (mp:predict-package-name-for-current-buffer)))
>     (while (search-forward "CLASSNAME" nil t)
>       (replace-match classname))
>     (goto-char (point-min))
>     (while (search-forward "PACKAGE" nil t)
>       (replace-match packagename) ) ) )
> 
> (defun mp:predict-package-name-for-current-buffer ()
>   "Simply take two parent directories and concat with . inbetween."
>   (let* ((components (remq ""
>                (reverse
>                 (split-string
>                  (file-name-directory (buffer-file-name))
>                  "\\/")))))
>     (concat (nth 0 components) "." (nth 1 components) "." (nth 2
> components))))
> 
> 
> The problem is that the class-name and package-name are actually inserted
> in capital letters while all file-names involved are writtn with lower-case
> letters. So for a file with this path "c:/a/b/c/d/. The predict method
> actually inserts: C.D.

>From the documentation of replace-match:

If second arg fixedcase is non-nil, do not alter case of replacement 
text.
Otherwise maybe capitalize the whole text, or maybe just word initials,
based on the replaced text.
If the replaced text has only capital letters
and has at least one multiletter word, convert newtext to all caps.
Otherwise if all words are capitalized in the replaced text,
capitalize each word in newtext.

So use (replace-match packagename t)

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***


reply via email to

[Prev in Thread] Current Thread [Next in Thread]