Namespaces
Variants
Actions

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 \"$_$\".
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 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 $_$.
"
  (interactive)
  ;; (setq file (make-temp-file "EoM." nil ".txt"))
  (if (search-forward-regexp "<img align[^>]*>" nil t)
      (progn 
	(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))
	(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))
	(setq run t)
	(while (and run (re-search-forward "====References====\n" (point-max) t))
	  (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))
	  )
	nil)
    )
  
  (setq run t)
  ;; This loop replaces "$ $" by "$_$"
  ;; Necessary for re-entry:
  ;; (unhighlight-regexp "\\$ \\$")
  (goto-char (point-min)) 
  (while (and run (re-search-forward "\\$ \\$" (point-max) t))
     (replace-match "$_$")
     )
  (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   "\\$ \\$")
    (search-forward "$ $" (point-max))
    (set-mark (point))
    (setq s (concat "\$" (read-string "? " nil nil "_") "\$"))
    (if (equal s "$_$") 
	(progn 
	  (unhighlight-regexp "\\$ \\$")
	  (replace-match s t "\\")
	  (exit-minibuffer)
	  (setq run nil))
      (replace-match s t "\\")
      (goto-char (point-min))
      (unhighlight-regexp s)
      (goto-char (point-min)))))

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

;;-------------------------------------------------------------------------
How to Cite This Entry:
Emacs eom macros. Encyclopedia of Mathematics. URL: http://encyclopediaofmath.org/index.php?title=Emacs_eom_macros&oldid=19646