[Top][All Lists]

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

RE: Another Emacs incompatibilty

From: Drew Adams
Subject: RE: Another Emacs incompatibilty
Date: Mon, 24 Aug 2020 22:46:49 -0700 (PDT)

> > (put 'insert-char 'delete-selection t)
> OK, so that's the syntax. This must be a very arcane
> way of configuration. I did use `put' but only to
> enable and disable, as in
>   (put 'help-for-help 'disabled t)
> But, what are properties and how do I know what
> properties a function has?

Ask Emacs: `C-h i m Elisp i symbol property'

A Lisp symbol is an object of sorts.  It has a name
and possibly other properties/attributes:

* a name           - `symbol-name'
* a function value - `symbol-function'
* a variable value - `symbol-value'
* a property list  - `symbol-plist'

And you can add any other properties you like.

> And how do I write a function with properties? (I
> don't think I want to, in general, but I always want
> to do things I didn't do, at least once...)

As I think I said before, it's not really the function
that has properties.  It's the function symbol - the
symbol whose `symbol-function' is the function named
with the function's name (which is also the symbol's

(defun foo ...)
(symbol-function 'foo) => foo ; symbol with name "foo"

(setq foo 42)
(symbol-value 'foo) => 42

See also `C-h f symbol-plist'.

> > Or if you instead want `insert-char' to kill the
> > selected text (so you can later yank it) instead of
> > just deleting it, add this to your init file, to
> > override the default behavior:
> >
> >   (put 'insert-char 'delete-selection 'kill)
> `insert-char'? Sure, that's ONE way to insert, but
> there are many other ways to insert things, and not
> just a single char, 

It was an _example_.  A command whose `delete-selection'
property is defined to kill, not delete.  To show that
you can change the behavior to kill instead of only

> and then (if you desire this
> functionality to begin with, that is) then you
> _always_ want the selection to be killed, I think is
> the objective here?

It presumably is your objective, if you use `kill'.
Again, an _example_ of getting kill behavior.  Do I
think you'd likely really prefer to have `insert-char'
kill instead of delete?  No.  But you might.  Or some
library might.

> Anyway, I don't get it to work:
>   (delete-selection-mode) ; t
>   (put 'insert-char 'delete-selection 'kill) ; kill
>   I type this
>   set-mark-command
>   beginning-of-line
>   set-mark-command
>   s ("I type this" disappears, the letter s appears)
>   yank
>   "I type this" does NOT appear

In your example, the kill behavior applies to
`insert-char', not to `self-insert-command', which
is what `s' is bound to.  So don't expect that when
you type `s' to replace the active region you can
then yank what was deleted.

If you instead select some text and use
`C-x 8 RET' `LATIN CAPITAL LETTER M', then the
selected text is replaced by `M', AND it's put in
the kill ring.

Maybe using `insert-char' to illustrate wasn't the
best choice.  I did so because it was the first one
in `delsel.el' after `self-insert-command' (which
is a special case).

> Here is a 19-liner how to do it:
> (delete-selection-mode)
> (defun delete-selection-pre-hook ()
>   (when (and delete-selection-mode
>              (use-region-p)
>              (not buffer-read-only) )
>     (if (member this-command '(self-insert-command insert-char insert) )
>         (delete-selection-helper 'kill)
>       (delete-selection-helper
>        (and (symbolp this-command)
>             (get this-command 'delete-selection) )))))

You don't need to redefine `delete-selection-pre-hook'.

You typically need only put a `delete-selection'
property on a command's symbol, to get the behavior
you want for it, which is just delete-the-selection
in most cases, i.e., value `t'.

And most people will never need to do even that.
They'll just use the out-of-the-box behavior.

People who add commands that insert or do some
other things might want to configure them to take
advantage of `delete-selection-mode'.  And even
then, they mostly just use `t': delete.

You didn't ask how to use `delete-selection-mode'.

I think you asked about its various behaviors and
configuring them by putting properties on command
symbols.  It can do more than your average "select
and type to replace".

reply via email to

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