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 00:05:47 +0200

> 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.
> > 
> > Perhaps using `completing-read` 
> > 
> > (cseq '("sh-mode" "emacs-lisp-mode" "org-mode" "f90-mode" "c-mode" ))
> > (csel  (completing-read "Major Mode: " cseq nil t "org-mode"))
> > 
> > How can I introduce the above functionality in the `workbench' function?
> > 
> > (defun workbench (&optional name)
> >   "Make new buffer with unique name based on NAME."
> >   (interactive "s Buffer Name: ")
> >   (switch-to-buffer
> >    (generate-new-buffer
> >     (if (string-blank-p name) "wb" name))))
> 
> 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.
How can the code be modified to have filename saved with the following format?

%Y-%m-%d-T%H:%M:%S--<rcd-temp-buffer-name>-<name>.txt


 
> {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]