From mboxrd@z Thu Jan 1 00:00:00 1970 From: John Kitchin Subject: Re: Disable typo-mode in org source code blocks Date: Mon, 2 Sep 2019 14:26:22 -0400 Message-ID: References: <87mufni3li.fsf@pc-117-162.i-did-not-set--mail-host-address--so-tickle-me> <87r24z6s7z.fsf@ucl.ac.uk> <87d0gjhz9l.fsf@gmail.com> <87tv9uhosu.fsf@pc-117-162.i-did-not-set--mail-host-address--so-tickle-me> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="000000000000915a040591961cc4" Return-path: Received: from eggs.gnu.org ([2001:470:142:3::10]:41689) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i4r2E-00069O-Ot for emacs-orgmode@gnu.org; Mon, 02 Sep 2019 14:26:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i4r2D-0008GC-2G for emacs-orgmode@gnu.org; Mon, 02 Sep 2019 14:26:38 -0400 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]:45380) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1i4r2C-0008F2-Lu for emacs-orgmode@gnu.org; Mon, 02 Sep 2019 14:26:37 -0400 Received: by mail-wr1-x444.google.com with SMTP id q12so14848809wrj.12 for ; Mon, 02 Sep 2019 11:26:36 -0700 (PDT) In-Reply-To: <87tv9uhosu.fsf@pc-117-162.i-did-not-set--mail-host-address--so-tickle-me> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: garjola@garjola.net Cc: Tim Cross , org-mode-email --000000000000915a040591961cc4 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable You can use cursor-sensor mode for this if you have emacs 26ish. The idea is you set cursor-sensor functions on a region that do something depending on whether you enter or leave the region. Below, I tie into the org font lock mechanisms to add these properties so that when you enter, typo mode gets turned off, and when you leave it gets turned back on. I use a similar approach to put src-block specific key maps on src blocks. There are two examples of functions that are "lightly tested". I can see why you would want this disabled in src blocks, it never puts the right quote in! (defvar scimax-src-block-cursor-sensor-functions '() "List of functions to run in cursor-sensor mode. .") (defun scimax-add-cursor-sensor-functions-to-src-blocks (limit) "Function used in font-lock to add cursor-sensor functions." (let ((case-fold-search t)) (while (re-search-forward org-babel-src-block-regexp limit t) (let* ((beg (match-beginning 0)) (end (match-end 0)) (cursor-functions (or (get-text-property beg 'cursor-sensor-functions) `()))) (cl-loop for func in scimax-src-block-cursor-sensor-functions do (when (not (memq func cursor-functions)) (pushnew func cursor-functions))) (add-text-properties beg end `(cursor-sensor-functions ,cursor-functions)))))) (define-minor-mode scimax-cursor-sensor-mode "Minor mode to turn on cursor-sensor-mode for org src-blocks." :init-value nil (if scimax-cursor-sensor-mode (progn (add-hook 'org-font-lock-hook #'scimax-add-cursor-sensor-functions-to-src-blocks t) (add-to-list 'font-lock-extra-managed-props 'cursor-sensor-functions) (cursor-sensor-mode +1)) (remove-hook 'org-font-lock-hook #'scimax-add-cursor-sensor-functions-to-src-blocks) (cursor-sensor-mode -1)) (font-lock-fontify-buffer)) (defun scimax-cs-message-1 (win prev-pos sym) (message "%s %s %s" win prev-pos sym)) (defun scimax-src-toggle-typo-mode (win prev-pos sym) (if (eq sym 'entered) (typo-mode -1) (typo-mode +1))) (add-to-list 'scimax-src-block-cursor-sensor-functions 'scimax-cs-message-1) (add-to-list 'scimax-src-block-cursor-sensor-functions 'scimax-src-toggle-typo-mode) John ----------------------------------- Professor John Kitchin Doherty Hall A207F Department of Chemical Engineering Carnegie Mellon University Pittsburgh, PA 15213 412-268-7803 @johnkitchin http://kitchingroup.cheme.cmu.edu On Mon, Sep 2, 2019 at 8:21 AM wrote: > On Mon 02-Sep-2019 at 10:35:02 +02, Tim Cross > wrote: > > I think Eric is correct. There is also another reason. If you edit the > > source blocks with C-', then any escaping needed (such as putting a ',' > > before '*') will also be automatically handled, plus of course you get > > all the programing mode goodness. > > > > Fraga, Eric writes: > > > >> On Monday, 2 Sep 2019 at 09:01, garjola@garjola.net wrote: > >>> I am using typo-mode (https://github.com/jorgenschaefer/typoel) in my > >>> org buffers (actually with a hook for text-mode), but I would like to > >>> disable it in source code blocks. > >>> > >>> I have been unable to find a hook to do so (I understand that > >>> org-src-mode-hook is used when editing with =E2=80=98C-c '=E2=80=99 b= ut not in the org > >>> buffer). > >> > >> I am not entirely sure what you want here. If it is that you want to > >> turn off typo-mode when point moves into a src block but while still i= n > >> the whole org buffer, then you cannot do this AFAIK. The best approac= h > >> is to always edit src blocks using C-c ' (org-edit-special) and then y= ou > >> can use org-src-mode-hook to do what you want. > > Thanks to both of you. I usually edit in the separate buffer, but I > don=E2=80=99t for one liners. It=E2=80=99s a pity that this is not possib= le, but I can > live with that! > > Thanks again! > -- > > --000000000000915a040591961cc4 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
You can use cursor-sensor mode for this if you have e= macs 26ish. The idea is you set cursor-sensor functions on a region that do= something depending on whether you enter or leave the region. Below, I tie= into the org font lock mechanisms to add these properties so that when you= enter, typo mode gets turned off, and when you leave it gets turned back o= n. I use a similar approach to put src-block specific key maps on src block= s. There are two examples of functions that are "lightly tested".= I can see why you would want this disabled in src blocks, it never puts th= e right quote in!=C2=A0

(defvar scimax-src-block-c= ursor-sensor-functions '()
=C2=A0 "List of functions to run in = cursor-sensor mode. .")


(defun scimax-add-cursor-sensor-fun= ctions-to-src-blocks (limit)
=C2=A0 "Function used in font-lock to = add cursor-sensor functions."
=C2=A0 (let ((case-fold-search t))=C2=A0 =C2=A0 (while (re-search-forward org-babel-src-block-regexp limit t= )
=C2=A0 =C2=A0 =C2=A0 (let* ((beg (match-beginning 0))
=C2=A0 =C2= =A0 (end (match-end 0))
=C2=A0 =C2=A0 (cursor-functions (or
(ge= t-text-property beg 'cursor-sensor-functions)
`())))
(cl-loo= p for func in scimax-src-block-cursor-sensor-functions do
(when (not = (memq func cursor-functions))
=C2=A0 (pushnew func cursor-functions))= )
(add-text-properties
beg end `(cursor-sensor-functions ,cursor-f= unctions))))))


(define-minor-mode scimax-cursor-sensor-mode
= =C2=A0 "Minor mode to turn on cursor-sensor-mode for org src-blocks.&q= uot;
=C2=A0 :init-value nil
=C2=A0 (if scimax-cursor-sensor-mode
= =C2=A0 =C2=A0 =C2=A0 (progn
(add-hook 'org-font-lock-hook
=C2= =A0#'scimax-add-cursor-sensor-functions-to-src-blocks t)
(add-to-li= st 'font-lock-extra-managed-props 'cursor-sensor-functions)
(cu= rsor-sensor-mode +1))
=C2=A0 =C2=A0 (remove-hook 'org-font-lock-hook=
#'scimax-add-cursor-sensor-functions-to-src-blocks)

=C2= =A0 =C2=A0 (cursor-sensor-mode -1))
=C2=A0 (font-lock-fontify-buffer))
(defun scimax-cs-message-1 (win prev-pos sym)
=C2=A0 (message &quo= t;%s %s %s"
=C2=A0 win
=C2=A0 prev-pos
=C2=A0 sym))
=
(defun scimax-src-toggle-typo-mode (win prev-pos sym)
=C2=A0 (if (eq= sym 'entered)
=C2=A0 =C2=A0 =C2=A0 (typo-mode -1)
=C2=A0 =C2=A0 = (typo-mode +1)))


(add-to-list 'scimax-src-block-cursor-senso= r-functions
=C2=A0 =C2=A0 'scimax-cs-message-1)

(add-to-lis= t 'scimax-src-block-cursor-sensor-functions
=C2=A0 =C2=A0 'sci= max-src-toggle-typo-mode)



<= /div>
John<= br>
-----------------------------------
Professor John Kitchin=C2=A0<= br>Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mel= lon University
Pittsburgh, PA 15213
412-268-7803


On Mon, Sep 2, 2019 at 8:21 AM <garjola@garjola.net> wrote:
On Mon 02-Sep-2019 at 10:35:02 +02, Tim Cross = <theophilusx@= gmail.com>
wrote:
> I think Eric is correct. There is also another reason. If you edit the=
> source blocks with C-', then any escaping needed (such as putting = a ','
> before '*') will also be automatically handled, plus of course= you get
> all the programing mode goodness.
>
> Fraga, Eric <e.fraga@ucl.ac.uk> writes:
>
>> On Monday,=C2=A0 2 Sep 2019 at 09:01, garjola@garjola.net wrote:
>>> I am using typo-mode (https://github.com/jorgen= schaefer/typoel) in my
>>> org buffers (actually with a hook for text-mode), but I would = like to
>>> disable it in source code blocks.
>>>
>>> I have been unable to find a hook to do so (I understand that<= br> >>> org-src-mode-hook is used when editing with =E2=80=98C-c '= =E2=80=99 but not in the org
>>> buffer).
>>
>> I am not entirely sure what you want here.=C2=A0 If it is that you= want to
>> turn off typo-mode when point moves into a src block but while sti= ll in
>> the whole org buffer, then you cannot do this AFAIK.=C2=A0 The bes= t approach
>> is to always edit src blocks using C-c ' (org-edit-special) an= d then you
>> can use org-src-mode-hook to do what you want.

Thanks to both of you. I usually edit in the separate buffer, but I
don=E2=80=99t for one liners. It=E2=80=99s a pity that this is not possible= , but I can
live with that!

Thanks again!
--

--000000000000915a040591961cc4--