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: Rodolfo Medina
Subject: Re: [AUCTeX] Insert counter in Emacs tex file
Date: Sun, 26 Aug 2018 20:10:43 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2 (gnu/linux)

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...

Thanks,

Rodolfo




reply via email to

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