emacs-devel
[Top][All Lists]
Advanced

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

Re: Proposal: make insert-pair-alist buffer local


From: Juri Linkov
Subject: Re: Proposal: make insert-pair-alist buffer local
Date: Thu, 23 Dec 2021 19:12:54 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (x86_64-pc-linux-gnu)

> This is a "don't shoot the pianist" (cf. cite below) sort of idea I have
> had while trying to rationalize my .emacs.d:
>
> If insert-pair-alist was buffer-local, we could have different auto-pairs
> for different modes:
> e.g. //,**, etc. in org-mode
>
> Am I too wrong in this?

In my experience, the value of insert-pair-alist could depend not only
on mode, but also on more language context around point, so for example,
to insert a pair of quotes, insert-pair-alist should insert “”
in some languages, and «» in other languages, so the logic can be
more complex than just setting a buffer-local value, and a more
flexible solution would be to let-bind insert-pair-alist, e.g.:

#+begin_src emacs-lisp
(defun use-fancy-quotes-p ()
  (and (memq buffer-file-coding-system '(utf-8 utf-8-unix utf-8-emacs-unix))
       (or (and comment-start (nth 4 (syntax-ppss)))
           (and (derived-mode-p 'text-mode)
                (not (and (derived-mode-p 'org-mode)
                          (consp (get-text-property (point) 'face))
                          (memq 'org-block (get-text-property (point) 'face))))
                (not (derived-mode-p 'vc-git-log-edit-mode))
                (not (derived-mode-p 'sgml-mode))
                (not (derived-mode-p 'yaml-mode)))
           (derived-mode-p 'fundamental-mode))))

(define-key esc-map "\""
  (lambda ()
    (interactive)
    (let ((insert-pair-alist
           (cons
            (if (use-fancy-quotes-p)
                (if (and (not (eobp)) (eq (aref char-script-table (char-after)) 
'cyrillic))
                    '(?\" ?\« ?\»)
                  '(?\" ?\“ ?\”))
              '(?\" ?\" ?\"))
            insert-pair-alist)))
      (call-interactively 'insert-pair))))

(define-key esc-map "'"
  (lambda ()
    (interactive)
    (let ((insert-pair-alist
           (cons
            (if (use-fancy-quotes-p)
                '(?\' ?\‘ ?\’)
              '(?\' ?\' ?\'))
            insert-pair-alist)))
      (call-interactively 'insert-pair))))

(define-key esc-map "["  'insert-pair)
(define-key esc-map "{"  'insert-pair)
#+end_src



reply via email to

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