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

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

Re: Changing buffer mode


From: Emanuel Berg
Subject: Re: Changing buffer mode
Date: Fri, 22 Jul 2022 02:21:45 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux)

carlmarcos--- via Users list for the GNU Emacs text editor wrote:

>>>>> With the following function I can change the mode of
>>>>> a buffer. I would like to change it to use
>>>>> `completing-read` using a list composed of a selection
>>>>> of mode names. How can I go about doing that?
>>>>
>>>> Use it?
>>>>
>>>>  (completing-read "drop: " '(little boy fat man))
>>>
>>> Yes, but then how can I update the buffer with the
>>> new mode?
>>
>> The same way you always do it?
>
> I do not know the usual way.

(emacs-lisp-mode) from Lisp and M-x emacs-lisp-mode RET
interactively ...

Anyway some code for you ... try the examples, last.

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/set-mode.el

(defun mode-p (fun)
  (let*((sfx "-mode")
        (len (length sfx) ))
    (and (functionp fun)
         (string= sfx (substring (symbol-name fun) (- len))) )))

(defun set-mode (&optional fun)
  (interactive
   (list (completing-read
          "mode: "
          obarray
          (lambda (e) (mode-p e))
          t
          nil
          nil
          (symbol-name #'normal-mode)
          )) )
  (if fun
      (let ((f (if (stringp fun)
                   (intern fun)
                 fun) ))
        (when (mode-p f)
          (apply (list f)) ))
    (normal-mode) ))

;; (set-mode #'kill-emacs)        ; safe as not a mode
;; (set-mode #'fundamental-mode)  ; function
;; (set-mode "emacs-lisp-mode")   ; function name
;; (set-mode)                     ; derive mode from extention/hashbang
;; M-x set-mode RET conf-mode RET ; interactive explicit
;; M-x set-mode RET RET           ; ditto implicit

-- 
underground experts united
https://dataswamp.org/~incal




reply via email to

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