buffer-invisibility-spec
is a buffer local variable.
If you want globally set the key you must add a
to the default value of buffer-invisible-spec
.
global-set-key
is a function therefore the argument (add-text-properties ...)
is evaluated before global-set-key
. The return value of add-text-properties
is undefined in the doc. So you bind your keys to some undefined value which is probably not a function.
You need to define commands that you can bind to keys with global-set-key
.
You also need to be more careful when setting the invisible
text property. It may be that you destroy already existing settings.
There follows a version that takes care of the above comments:
(eval-when-compile (require 'cl-lib))
(defun my-change-invisible-a (b e &optional add)
"Make region from B to E invisible if ADD is t.
Otherwise remove invisible spec ni region from B to E."
(interactive "r")
(cl-loop
with iv-prop
for int being the intervals property 'invisible from b to e
when (or (null (listp (setq iv-prop (get-text-property (car int) 'invisible))))
(last iv-prop 0))
do (setq iv-prop (list iv-prop))
do
(setq iv-prop (remove 'a iv-prop))
(put-text-property (car int) (cdr int)
'invisible
(if add
(cons 'a iv-prop)
iv-prop))))
(defun my-remove-invisible-a (b e)
"Remove invisible spec a from region running from B to E."
(interactive "r")
(my-change-invisible-a b e nil))
(defun my-add-invisible-a (b e)
"Add invisible spec a to region from B to E."
(interactive "r")
(unless (listp buffer-invisibility-spec)
(setq buffer-invisibility-spec (list buffer-invisibility-spec)))
(cl-pushnew '(a . t) buffer-invisibility-spec :test #'equal)
(my-change-invisible-a b e t))
(global-set-key (kbd "C-c i") #'my-add-invisible-a)
(global-set-key (kbd "C-c v") #'my-remove-invisible-a)
Furthermore, note that it might be that the major mode of the buffer adds invisible
to the font-lock-extra-managed-props
. In that case one needs to use font-lock to add the invisible specs.
An alternative is using overlays with non-nil invisible specification.
interactive
spec, i.e., a command.