Help:Emacs eom macros

From Encyclopedia of Mathematics
Jump to: navigation, search
;; Store this under the name emacs_eom_macros.el, load it into emacs
;; using the command (load-file "emacs_eom_macros.el"). If the current
;; emacs buffer does contain an "old" EoM file with references to
;; images of tex code, the command "C-." (control key + ',' key) will
;; replace every image reference by a string "$ $" resp. "$$ $$" in
;; the buffer, and the mini-buffer will prompt for the input of the
;; corresponding tex or latex code, while highlighting the appropriate
;; place in the buffer. The minibuffer input is completed by the
;; <enter> key, the input will be copied at the highlighted place and
;; you will be prompted for the next input, until all references are
;; replaced.

(defun eom-remove-images ()
  "Apply to a buffer containing an old EoM page.  All references
of png images will be removed and replaced by strings \"$_$\" 
for inline TeX code (resp. \"$$_$$\" for displayed TeX code).
The first such string is highlighted, and the minibuffer prompts
for input of some TeX/LaTeX code <tex code> followed by <Enter>,
after which the string \"$_$\" will be replaced by 
\"$<tex code>$\", and the next string \"$_$\" is highlighted, expecting
similar input etc.. 

Function stops after all items \"$_$\" are replaced by TeX
strings, or when an ampty string or a string \"_\" was keyed in. 
This allows to stop the program and will leave the actual \"$_$\"
and all the following intact. Stopping allows to edit other parts
of the buffer or to correct erroneous input.

Resuming the function for the same buffer will restart with the
first remaining string $_$.
  ;; (setq file (make-temp-file "EoM." nil ".txt"))
  (if (search-forward-regexp "<img align[^>]*>" nil t)
	(goto-char (point-min))
	;; Displayed material: Insert '\n$$_$$\n'
	(replace-regexp "\n*\<table .*?\<\/table\>\n*\s*" "\n\$\$_\$\$\n")
	(goto-char (point-min))
	;; Inline material: Insert '$_$'
	(replace-regexp "<img align[^>]*>" "\$_\$")
	(goto-char (point-min))
	(replace-regexp " \\[\\[" "\n\n\[\[")
;	(mark-whole-buffer)
;	(fill-region (point) (mark))
	(goto-char (point-min))
	;; Correct a few false linebreaks:
	(replace-regexp "\n\\[\\[" "\[\[")
	(goto-char (point-min))
	(replace-regexp "\\]\\]\n" "\]\]")
	(goto-char (point-min))
	(replace-regexp "==== " "====\n")
	(goto-char (point-min))
	(replace-regexp "\.== " ".==\n")
	(goto-char (point-min))
	(replace-regexp "\\$\\$_\\$\\$\n* *" "\n\$\$_\$\$\n")
	(goto-char (point-min))
	;; 'References' need corrections of their '<table> ... </table>':
	(while (re-search-forward "====References====\n" (point-max) t)
	  (if (search-forward-regexp "<table>" (point-max) t)
	      (let ((beg (point))
		    (end (search-forward-regexp "</table>" (point-max) t)))
		(replace-string "\n" " " nil beg end)
		(replace-string "</TD>" "</TD>\n" nil beg end))

  ;; In order to highlight the actual place for inserting,
  ;; this is encoded by $ $.
  ;; This loop replaces "$ $" by "$_$".
  ;; Necessary for re-entry, in case some "$ $" did remain
  ;; from an earlier application of this function.
  (setq b "eom-secure") ;; name of intermediate buffer and file
  (goto-char (point-min)) 
  (while (re-search-forward "\\$ \\$" (point-max) t)
    (replace-match "$_$"))
  ;; This is the interactive loop
  ;; to replace '$_$' by '$<tex input code>$':
  ;; with early exit for empty input or "_" input
  (goto-char (point-min)) 
  (setq run t)
  ;; This is the interactive loop
  ;; to replace '$_$' by '$<tex input code>$':
  (while (and run (re-search-forward "\\$_\\$" (point-max) t))
    (replace-match "$ $")
    (goto-char (point-min))
    (unhighlight-regexp "\\$ \\$")
    (highlight-regexp   "\\$ \\$")
    ;; the following two lines are necessary to position the
    ;; buffer to show insertion place while read-string works 
    (search-forward "$ $" (point-max))
    (backward-char 4)
    (setq s (concat "\$" (read-string "? " nil nil "_") "\$"))
    (if (equal s "$_$") 
	(progn ;; early exit
	  (unhighlight-regexp "\\$ \\$")
	  (forward-char 2)
	  (setq run nil))
      (search-forward "$ $" (point-max))
      ;; readstring here may cause wrong replace-match for
      ;; multiline minibuffer input
      (replace-match s t t)
      (copy-to-buffer b (point-min) (point-max))
	(set-buffer b)
	(write-file b))

(global-set-key (kbd "C-.") 'eom-remove-images)

How to Cite This Entry:
Emacs eom macros. Encyclopedia of Mathematics. URL: