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

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

Re: Adding major-mode selection to new buffer


From: Christopher Dimech
Subject: Re: Adding major-mode selection to new buffer
Date: Wed, 29 Jun 2022 12:47:21 +0200



> Sent: Wednesday, June 29, 2022 at 10:20 AM
> From: "Christopher Dimech" <dimech@gmx.com>
> To: "Jean Louis" <bugs@gnu.support>
> Cc: carlmarcos@tutanota.com, "Help Gnu Emacs" <help-gnu-emacs@gnu.org>
> Subject: Re: Adding major-mode selection to new buffer
>
> > Sent: Tuesday, June 28, 2022 at 10:21 PM
> > From: "Jean Louis" <bugs@gnu.support>
> > To: carlmarcos@tutanota.com
> > Cc: "Help Gnu Emacs" <help-gnu-emacs@gnu.org>
> > Subject: Re: Adding major-mode selection to new buffer
> >
> > * carlmarcos--- via Users list for the GNU Emacs text editor 
> > <help-gnu-emacs@gnu.org> [2022-06-28 01:51]:
> > > With the following function I can make a new buffer with a name.  Would 
> > > also 
> > > like to set the major-mode from the minibuffer.
> > > 
> > I am using this below to open up new temporary buffers with name
> > assigned or with automatic name, and with the possibility to choose
> > the major mode.
> 
> Have noticed that the number 1 is inserted when calling `rcd-temp-buffer'.
> This makes a problem when one wants to insert the name when saving

This is because you are using (interactive "p")

> 
>           (file-name
>              (if name
>                  (concat rcd-temp-file-directory
>                          (format-time-string "%Y-%m-%d-T%H:%M:%S--")
>                           rcd-temp-buffer-name "-" name ".org")
>                (concat rcd-temp-file-directory
>                        (format-time-string "%Y-%m-%d-T%H:%M:%S--")
>                        rcd-temp-buffer-name ".org"))) )
> 
> 
>  
> > {C-u M-x rcd-temp-buffer RET} will ask for mode to be used
> > 
> > {M-x rcd-temp-buffer RET}} will open new temporary buffer
> > 
> > {M-x rcd-temp-buffer RET}} will open new temporary buffer
> > 
> > {M-x rcd-temp-buffer-ask-name RET} will ask for buffer name
> > 
> > {C-u M-x rcd-temp-buffer-ask-name RET} will ask for buffer name and
> > for the mode
> > 
> > Code:
> > 
> > (defvar rcd-temp-buffer-name "RCD TEMPORARY BUFFER")
> > 
> > (defvar rcd-temp-file-directory "~/tmp/")
> > 
> > (defvar rcd-temp-buffer-mode-history nil)
> > 
> > (defvar rcd-temp-buffer-modes '(("adoc-mode" . "adoc")
> >                             ("emacs-lisp-mode" . "el")
> >                             ("lisp-mode" . ".lisp")
> >                             ("markdown-mode" . ".md")
> >                             ("org-mode" . "org")
> >                             ("sql-mode" . "sql")
> >                             ("fundamental-mode" . "txt")
> >                             ("html-mode" . "html")))
> > 
> > (defun rcd-temp-buffer (&optional name mode)
> >   "Generate new temporary buffer."
> >   (interactive "p")
> >   (let* ((format (concat "*" rcd-temp-buffer-name "%s%s*"))
> >      (buffer (if name (format format ": " name) (format format "" "")))
> >      (file-name (concat rcd-temp-file-directory (format-time-string 
> > "%Y-%m-%d-%H:%M:%S.txt"))))
> >     (switch-to-buffer (generate-new-buffer buffer))
> >     (setq buffer-file-name file-name)
> >     (if current-prefix-arg
> >     (let* ((mode (completing-read
> >                   "Mode: "
> >                   (map-keys rcd-temp-buffer-modes) nil t nil
> >                   'rcd-temp-buffer-mode-history)))
> >       (funcall (intern mode)))
> >       (funcall (intern (or mode "fundamental-mode"))))
> >     buffer-file-name))
> > 
> > (defun rcd-temp-buffer-ask-name ()
> >   "Generate new temporary buffer by asking for buffer name."
> >   (interactive)
> >   (rcd-temp-buffer
> >    (read-from-minibuffer "Buffer name: ")))
> > 
> > (global-set-key (kbd "<f5>") #'rcd-temp-buffer)
> > 
> > -- 
> > Jean
> > 
> > Take action in Free Software Foundation campaigns:
> > https://www.fsf.org/campaigns
> > 
> > In support of Richard M. Stallman
> > https://stallmansupport.org/
> > 
> >
> 
>



reply via email to

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