unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Changing buffer mode
@ 2022-07-20  3:55 carlmarcos--- via Users list for the GNU Emacs text editor
  2022-07-20 15:50 ` Emanuel Berg
  0 siblings, 1 reply; 15+ messages in thread
From: carlmarcos--- via Users list for the GNU Emacs text editor @ 2022-07-20  3:55 UTC (permalink / raw)
  To: Help Gnu Emacs

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? 

(defun mode-sweep ()
  "Cycle the buffer through three major modes (text, org, normal)."
  (interactive)

  (let* ( (modes (list #'org-mode #'text-mode #'normal-mode))
          (new-mode
              (or (cadr (member major-mode modes)) (car modes))) )
    (apply (list new-mode)) ))




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Changing buffer mode
  2022-07-20  3:55 Changing buffer mode carlmarcos--- via Users list for the GNU Emacs text editor
@ 2022-07-20 15:50 ` Emanuel Berg
  2022-07-21 14:39   ` carlmarcos--- via Users list for the GNU Emacs text editor
  2022-07-21 16:52   ` Jean Louis
  0 siblings, 2 replies; 15+ messages in thread
From: Emanuel Berg @ 2022-07-20 15:50 UTC (permalink / raw)
  To: help-gnu-emacs

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))

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




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Changing buffer mode
  2022-07-20 15:50 ` Emanuel Berg
@ 2022-07-21 14:39   ` carlmarcos--- via Users list for the GNU Emacs text editor
  2022-07-21 14:50     ` Emanuel Berg
  2022-07-21 16:52   ` Jean Louis
  1 sibling, 1 reply; 15+ messages in thread
From: carlmarcos--- via Users list for the GNU Emacs text editor @ 2022-07-21 14:39 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs


Jul 20, 2022, 15:50 by incal@dataswamp.org:

> 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?  




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Changing buffer mode
  2022-07-21 14:39   ` carlmarcos--- via Users list for the GNU Emacs text editor
@ 2022-07-21 14:50     ` Emanuel Berg
  2022-07-21 15:55       ` carlmarcos--- via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 15+ messages in thread
From: Emanuel Berg @ 2022-07-21 14:50 UTC (permalink / raw)
  To: help-gnu-emacs

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?

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




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Changing buffer mode
  2022-07-21 14:50     ` Emanuel Berg
@ 2022-07-21 15:55       ` carlmarcos--- via Users list for the GNU Emacs text editor
  2022-07-21 18:41         ` Eduardo Ochs
  2022-07-22  0:21         ` Emanuel Berg
  0 siblings, 2 replies; 15+ messages in thread
From: carlmarcos--- via Users list for the GNU Emacs text editor @ 2022-07-21 15:55 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs


Jul 21, 2022, 14:50 by incal@dataswamp.org:

> 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.  

Have started with the following but with few ideas of how to supply appropriate value to buffer.

(defun mode-sweep (mode)
  "Cycle the buffer through three major modes (text, org, normal)."

  (interactive
   (list
    (let ( (cseq '("org-mode" "text-mode" "normal-mode")) )
      (completing-read "Major-mode: " cseq nil t "normal-mode"))))

  (with-current-buffer buffer
   (funcall (intern mode))) )






^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Changing buffer mode
  2022-07-20 15:50 ` Emanuel Berg
  2022-07-21 14:39   ` carlmarcos--- via Users list for the GNU Emacs text editor
@ 2022-07-21 16:52   ` Jean Louis
  1 sibling, 0 replies; 15+ messages in thread
From: Jean Louis @ 2022-07-21 16:52 UTC (permalink / raw)
  To: help-gnu-emacs

* Emanuel Berg <incal@dataswamp.org> [2022-07-20 18:51]:
> 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)) 👀

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Changing buffer mode
  2022-07-21 15:55       ` carlmarcos--- via Users list for the GNU Emacs text editor
@ 2022-07-21 18:41         ` Eduardo Ochs
  2022-07-21 19:02           ` tomas
  2022-07-22  0:21         ` Emanuel Berg
  1 sibling, 1 reply; 15+ messages in thread
From: Eduardo Ochs @ 2022-07-21 18:41 UTC (permalink / raw)
  To: carlmarcos; +Cc: Emanuel Berg, help-gnu-emacs

On Thu, 21 Jul 2022 at 12:56, carlmarcos--- via Users list for the GNU
Emacs text editor <help-gnu-emacs@gnu.org> wrote:
> Have started with the following but with few ideas of how to supply appropriate value to buffer.
>
> (defun mode-sweep (mode)
>   "Cycle the buffer through three major modes (text, org, normal)."
>
>   (interactive
>    (list
>     (let ( (cseq '("org-mode" "text-mode" "normal-mode")) )
>       (completing-read "Major-mode: " cseq nil t "normal-mode"))))
>
>   (with-current-buffer buffer
>    (funcall (intern mode))) )

Is cseq an existing function? Where can I find it?
I have my own function to cycle between major modes, but it is ugly,
and I would like to rewrite it using something like cseq...
  [[]], Edrx
    http://angg.twu.net/#eev



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Changing buffer mode
  2022-07-21 18:41         ` Eduardo Ochs
@ 2022-07-21 19:02           ` tomas
  0 siblings, 0 replies; 15+ messages in thread
From: tomas @ 2022-07-21 19:02 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 786 bytes --]

On Thu, Jul 21, 2022 at 03:41:13PM -0300, Eduardo Ochs wrote:
> On Thu, 21 Jul 2022 at 12:56, carlmarcos--- via Users list for the GNU
> Emacs text editor <help-gnu-emacs@gnu.org> wrote:
> > Have started with the following but with few ideas of how to supply appropriate value to buffer.
> >
> > (defun mode-sweep (mode)
> >   "Cycle the buffer through three major modes (text, org, normal)."
> >
> >   (interactive
> >    (list
> >     (let ( (cseq '("org-mode" "text-mode" "normal-mode")) )
> >       (completing-read "Major-mode: " cseq nil t "normal-mode"))))
> >
> >   (with-current-buffer buffer
> >    (funcall (intern mode))) )
> 
> Is cseq an existing function? Where can I find it?

No, in the above code it is a variable just being declared in
the `let'.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Changing buffer mode
  2022-07-21 15:55       ` carlmarcos--- via Users list for the GNU Emacs text editor
  2022-07-21 18:41         ` Eduardo Ochs
@ 2022-07-22  0:21         ` Emanuel Berg
  2022-07-22  3:22           ` carlmarcos--- via Users list for the GNU Emacs text editor
  2022-07-22  4:00           ` carlmarcos--- via Users list for the GNU Emacs text editor
  1 sibling, 2 replies; 15+ messages in thread
From: Emanuel Berg @ 2022-07-22  0:21 UTC (permalink / raw)
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Changing buffer mode
  2022-07-22  0:21         ` Emanuel Berg
@ 2022-07-22  3:22           ` carlmarcos--- via Users list for the GNU Emacs text editor
  2022-07-22  4:00           ` carlmarcos--- via Users list for the GNU Emacs text editor
  1 sibling, 0 replies; 15+ messages in thread
From: carlmarcos--- via Users list for the GNU Emacs text editor @ 2022-07-22  3:22 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs


Jul 22, 2022, 00:21 by incal@dataswamp.org:

> 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.
>

Although it is good, my intention was to limit the modes to a select few, and for the mode names to be visible to the user through the minibuffer completing-read.  


> ;;; -*- 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
>




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Changing buffer mode
  2022-07-22  0:21         ` Emanuel Berg
  2022-07-22  3:22           ` carlmarcos--- via Users list for the GNU Emacs text editor
@ 2022-07-22  4:00           ` carlmarcos--- via Users list for the GNU Emacs text editor
  2022-07-22  4:13             ` Emanuel Berg
  1 sibling, 1 reply; 15+ messages in thread
From: carlmarcos--- via Users list for the GNU Emacs text editor @ 2022-07-22  4:00 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs


Jul 22, 2022, 00:21 by incal@dataswamp.org:

> 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 ...
>
Have come up with the following instead

(defun mode-sweep (mode)
  "Cycle the buffer through three major modes (text, org, normal)."

  (interactive
   (list
    (let ( (cseq '("org-mode" "text-mode" "normal-mode")) )
      (completing-read "Mode: " cseq nil t "normal"))))

  (apply (list (intern mode))) )




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Changing buffer mode
  2022-07-22  4:00           ` carlmarcos--- via Users list for the GNU Emacs text editor
@ 2022-07-22  4:13             ` Emanuel Berg
  2022-07-22 18:11               ` carlmarcos--- via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 15+ messages in thread
From: Emanuel Berg @ 2022-07-22  4:13 UTC (permalink / raw)
  To: help-gnu-emacs

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

> Have come up with the following [...]

How did you come up with that?

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




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Changing buffer mode
  2022-07-22  4:13             ` Emanuel Berg
@ 2022-07-22 18:11               ` carlmarcos--- via Users list for the GNU Emacs text editor
  2022-07-22 18:21                 ` Emanuel Berg
  0 siblings, 1 reply; 15+ messages in thread
From: carlmarcos--- via Users list for the GNU Emacs text editor @ 2022-07-22 18:11 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs


Jul 22, 2022, 04:13 by incal@dataswamp.org:

> carlmarcos--- via Users list for the GNU Emacs text editor wrote:
>
>> Have come up with the following [...]
>>
>
> How did you come up with that?
>
From studying your code and my original code.  Do you approve of it, or could it be improved
a bit more?

(defun mode-sweep (mode)
  "Cycle the buffer through three major modes (text, org, normal)."

  (interactive
   (list
    (let ( (cseq '("org-mode" "text-mode" "normal-mode")) )
      (completing-read "Mode: " cseq nil t "normal"))))

  (apply (list (intern mode))) )






^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Changing buffer mode
  2022-07-22 18:11               ` carlmarcos--- via Users list for the GNU Emacs text editor
@ 2022-07-22 18:21                 ` Emanuel Berg
  2022-07-22 18:36                   ` carlmarcos--- via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 15+ messages in thread
From: Emanuel Berg @ 2022-07-22 18:21 UTC (permalink / raw)
  To: help-gnu-emacs

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

> From studying your code and my original code. Do you
> approve of it, or could it be improved a bit more?

It looks good I guess but I don't think it's a good idea to
begin with, I think it's better to let Emacs handle this for
you, and if it doesn't the way you'd expect or like,
configure it, e.g.

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

(setq auto-mode-alist `(
    ("\\.bal\\'"     . balance-mode)
    ("\\.cl\\'"      . common-lisp-mode)
    ("\\.dat\\'"     . gnuplot-mode)
    ("\\.gpi\\'"     . gnuplot-mode)
    ("\\.grm\\'"     . sml-mode)
    ("\\.lu\\'"      . lua-mode)
    ("\\.nqp\\'"     . perl-mode)
    ("\\.php\\'"     . html-mode)
    ("\\.pic\\'"     . nroff-mode)
    ("\\.pl\\'"      . prolog-mode)
    ("\\.sed\\'"     . conf-mode)
    ("\\.service\\'" . conf-mode)
    ("\\.tap\\'"     . gcode-mode)
    ("\\.tex\\'"     . latex-mode)
    ("\\.xr\\'"      . conf-xdefaults-mode)
    ("keys\\'"       . conf-mode)
    ("*"             . text-mode)
  ,@auto-mode-alist) )

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




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Changing buffer mode
  2022-07-22 18:21                 ` Emanuel Berg
@ 2022-07-22 18:36                   ` carlmarcos--- via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 15+ messages in thread
From: carlmarcos--- via Users list for the GNU Emacs text editor @ 2022-07-22 18:36 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs


Jul 22, 2022, 18:21 by incal@dataswamp.org:

> carlmarcos--- via Users list for the GNU Emacs text editor wrote:
>
>> From studying your code and my original code. Do you
>> approve of it, or could it be improved a bit more?
>>
>
> It looks good I guess but I don't think it's a good idea to
> begin with, I think it's better to let Emacs handle this for
> you, and if it doesn't the way you'd expect or like,
> configure it, e.g.
>
> ;;; -*- lexical-binding: t -*-
> ;;
> ;; this file:
> ;;   https://dataswamp.org/~incal/emacs-init/mode-by-filename.el
>
> (setq auto-mode-alist `(
>  ("\\.bal\\'"     . balance-mode)
>  ("\\.cl\\'"      . common-lisp-mode)
>  ("\\.dat\\'"     . gnuplot-mode)
>  ("\\.gpi\\'"     . gnuplot-mode)
>  ("\\.grm\\'"     . sml-mode)
>  ("\\.lu\\'"      . lua-mode)
>  ("\\.nqp\\'"     . perl-mode)
>  ("\\.php\\'"     . html-mode)
>  ("\\.pic\\'"     . nroff-mode)
>  ("\\.pl\\'"      . prolog-mode)
>  ("\\.sed\\'"     . conf-mode)
>  ("\\.service\\'" . conf-mode)
>  ("\\.tap\\'"     . gcode-mode)
>  ("\\.tex\\'"     . latex-mode)
>  ("\\.xr\\'"      . conf-xdefaults-mode)
>  ("keys\\'"       . conf-mode)
>  ("*"             . text-mode)
>  ,@auto-mode-alist) )
>

I have done that actually.  But sometimes I introduce some tags that allow
some tools to be used if I switch to another mode, even though it
is not the mode usually associated with a particular file type.



^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2022-07-22 18:36 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-07-20  3:55 Changing buffer mode carlmarcos--- via Users list for the GNU Emacs text editor
2022-07-20 15:50 ` Emanuel Berg
2022-07-21 14:39   ` carlmarcos--- via Users list for the GNU Emacs text editor
2022-07-21 14:50     ` Emanuel Berg
2022-07-21 15:55       ` carlmarcos--- via Users list for the GNU Emacs text editor
2022-07-21 18:41         ` Eduardo Ochs
2022-07-21 19:02           ` tomas
2022-07-22  0:21         ` Emanuel Berg
2022-07-22  3:22           ` carlmarcos--- via Users list for the GNU Emacs text editor
2022-07-22  4:00           ` carlmarcos--- via Users list for the GNU Emacs text editor
2022-07-22  4:13             ` Emanuel Berg
2022-07-22 18:11               ` carlmarcos--- via Users list for the GNU Emacs text editor
2022-07-22 18:21                 ` Emanuel Berg
2022-07-22 18:36                   ` carlmarcos--- via Users list for the GNU Emacs text editor
2022-07-21 16:52   ` Jean Louis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).