auctex
[Top][All Lists]
Advanced

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

Re: [AUCTeX] Insert counter in Emacs tex file


From: David Kastrup
Subject: Re: [AUCTeX] Insert counter in Emacs tex file
Date: Sun, 26 Aug 2018 21:56:13 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Rodolfo Medina <address@hidden> writes:

> David Kastrup <address@hidden> writes:
>
>> Joost Kremers <address@hidden> writes:
>>
>>> On Fri, Aug 24 2018, Rodolfo Medina wrote:
>>>> Hi all...
>>>>
>>>> In a MusiXTeX document, you insert several \bar(s) TeX commands,
>>>> that I
>>>> manually count typing a number next to each occurence of `\bar':
>>>>
>>>>  \bar %1
>>>>  ...
>>>>  \bar %2
>>>>  ...
>>>>  \bar %3
>>>>
>>>> and so on.  Of course, it gets very uncomfortable when editing the
>>>> document and
>>>> adding, in the middle of it, new \bar-s: then I have to re-write all
>>>> the
>>>> numbers, that may be dozens or hundreds...!  So I was wondering if
>>>> was possible
>>>> to have an automatic \bar numbering within my Emacs tex file...
>>>
>>> I have a bunch of functions to help me with something similar. The
>>> following function allows me to renumber counters used as labels in a
>>> TeX document:
>>
>> [...]
>>
>> It's worth pointing out that query-replace-regexp can accept calculated
>> replacement texts.
>>
>> So one can write stuff like
>>
>> C-M-% \\bar %[0-9]+ RET \\bar %\,(+ 15 \#) RET
>>
>> in order to replace the respective statements, with the first
>> replacement getting the number 15 then (assuming that you don't need to
>> replace anything before that).
>>
>> The documentation mentions:
>>
>>        For computing replacement strings for ‘\,’, the ‘format’ function is
>>     often useful (*note (elisp)Formatting Strings::).  For example, to add
>>     consecutively numbered strings like ‘ABC00042’ to columns 73 to 80
>>     (unless they are already occupied), you can use
>>
>>          M-x replace-regexp <RET> ^.\{0,72\}$ <RET>
>>          \,(format "%-72sABC%05d" \& \#) <RET>
>>
>> which is a bit tongue-in-cheek since it refers to conventions used for
>> mitigating the damage from dropping stacks of punch cards.
>
>
> Thank you, Joost and David...  Sometimes my occurrences of `\bar' do not have 
> a
> number at all...  So I thought that, for the moment, I can adopt the
> replace-regexp for my purposes...  In two steps...  So I defined two kbd
> macros: my-cancel-bar-numbers and my-number-bars, getting the following (ugly
> but working) code in my .emacs:
>
> (fset 'my-cancel-bar-numbers
>    [?\M-h ?\M-x ?r ?e ?p ?l ?a tab ?r ?e tab ?g tab return ?\\ ?\\ ?b ?a ?r ? 
>  ?% ?\[ ?0 ?- ?9 ?\] ?+ return ?\\ ?\\ ?b ?a ?r return])
>
> (fset 'my-number-bars
>    [?\M-h ?\M-x ?r ?e ?p ?l ?a ?c tab ?r ?e ?g tab return ?\\ ?\\ ?b ?a ?r 
> return ?\\ ?\\ ?b ?a ?r ?  ?% ?\\ ?, ?\( ?+ ?  ?2 ?  ?\\ ?# ?\) return])
>
> Then I defined:
>
> (defun my-reset-bar-numbers ()
>     (interactive)
>     (execute-kbd-macro (symbol-function 'my-cancel-bar-numbers))
>     (execute-kbd-macro (symbol-function 'my-number-bars)))
>
> that acts within a single paragraph/music-piece as I want.  Just two things:
> when I want to `undo' my-reset-bar-numbers, I need hitting `C-/' twice: not
> possible to undo it with a single `C-/'?  (Maybe those two kbd macros may
> become one function that replaces any line including `\bar' with `\bar
> %[0-9]+'?)  And secondily...  well, just for eye's pleasure, I'd like to
> replace those fset(s) above with two functions...

Cf.

combine-change-calls is a Lisp macro in ‘subr.el’.

(combine-change-calls BEG END &rest BODY)

Evaluate BODY, running the change hooks just once.

BODY is a sequence of lisp forms to evaluate.  BEG and END bound
the region the change hooks will be run for.

Firstly, ‘before-change-functions’ is invoked for the region
(BEG END), then the BODY forms are evaluated with
‘before-change-functions’ and ‘after-change-functions’ bound to
nil, and finally ‘after-change-functions’ is invoked on the
updated region.  The change hooks are not run if
‘inhibit-modification-hooks’ is initially non-nil.

The result of ‘combine-change-calls’ is the value returned by the
last of the BODY forms to be evaluated.  BODY may not make a
different buffer current, except temporarily.  BODY may not
change the buffer outside the specified region.  It must not
change ‘before-change-functions’ or ‘after-change-functions’.

Additionally, the buffer modifications of BODY are recorded on
the buffer’s undo list as a single (apply ...) entry containing
the function ‘undo--wrap-and-run-primitive-undo’. 

[back]

With regard to functions: instead of using keyboard macros, you can
first call some complex command like query-replace-regexp, then use

C-x ESC ESC (translated from C-x <escape> <escape>) runs the command
repeat-complex-command (found in global-map), which is an interactive
compiled Lisp function in ‘simple.el’.

It is bound to <again>, <redo>, C-x M-:, C-x M-ESC.

(repeat-complex-command ARG)

Edit and re-evaluate last complex command, or ARGth from last.
A complex command is one which used the minibuffer.
The command is placed in the minibuffer as a Lisp form for editing.
The result is executed, repeating the command as changed.
If the command has been changed or is not the most recent previous
command it is added to the front of the command history.
You can use the minibuffer history commands M-n and M-p
to get different commands to edit and resubmit.

[back]

in order to get "a Lisp form for editing" which you can copy and paste
into a function definition.

-- 
David Kastrup



reply via email to

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