Namespaces
Variants
Actions

Difference between revisions of "Help:Emacs eom macros"

From Encyclopedia of Mathematics
Jump to: navigation, search
m (corrected malfunction for articles with wiki style ref tables)
 
Line 17: Line 17:
 
(defun eom-remove-images ()
 
(defun eom-remove-images ()
 
   "Apply to a buffer containing an old EoM page.  All references
 
   "Apply to a buffer containing an old EoM page.  All references
of png images will be removed and replaced by strings \"$_$\".
+
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
 
The first such string is highlighted, and the minibuffer prompts
 
for input of some TeX/LaTeX code <tex code> followed by <Enter>,
 
for input of some TeX/LaTeX code <tex code> followed by <Enter>,
Line 25: Line 26:
  
 
Function stops after all items \"$_$\" are replaced by TeX
 
Function stops after all items \"$_$\" are replaced by TeX
strings, or when a string \"_\" was keyed in. This allows to stop
+
strings, or when an ampty string or a string \"_\" was keyed in.  
the program and will leave the actual \"$_$\" and all the following
+
This allows to stop the program and will leave the actual \"$_$\"
intact. Stopping allows to edit other parts of the buffer or to
+
and all the following intact. Stopping allows to edit other parts
correct erroneous input.
+
of the buffer or to correct erroneous input.
  
 
Resuming the function for the same buffer will restart with the
 
Resuming the function for the same buffer will restart with the
Line 46: Line 47:
 
(goto-char (point-min))
 
(goto-char (point-min))
 
(replace-regexp " \\[\\[" "\n\n\[\[")
 
(replace-regexp " \\[\\[" "\n\n\[\[")
(mark-whole-buffer)
+
; (mark-whole-buffer)
(fill-region (point) (mark))
+
; (fill-region (point) (mark))
 
(goto-char (point-min))
 
(goto-char (point-min))
 +
;; Correct a few false linebreaks:
 
(replace-regexp "\n\\[\\[" "\[\[")
 
(replace-regexp "\n\\[\\[" "\[\[")
 
(goto-char (point-min))
 
(goto-char (point-min))
Line 59: Line 61:
 
(replace-regexp "\\$\\$_\\$\\$\n* *" "\n\$\$_\$\$\n")
 
(replace-regexp "\\$\\$_\\$\\$\n* *" "\n\$\$_\$\$\n")
 
(goto-char (point-min))
 
(goto-char (point-min))
(setq run t)
+
;; 'References' need corrections of their '<table> ... </table>':
(while (and run (re-search-forward "====References====\n" (point-max) t))
+
(while (re-search-forward "====References====\n" (point-max) t)
  (search-forward-regexp "<table>" (point-max) t)
+
  (if (search-forward-regexp "<table>" (point-max) t)
  (let ((beg (point))
+
      (let ((beg (point))
(end (search-forward-regexp "</table>" (point-max) t)))
+
    (end (search-forward-regexp "</table>" (point-max) t)))
    (replace-string "\n" " " nil beg end)
+
(replace-string "\n" " " nil beg end)
    (replace-string "</TD>" "</TD>\n" nil beg end))
+
(replace-string "</TD>" "</TD>\n" nil beg end))
 +
    )
 
  )
 
  )
 
nil)
 
nil)
 
     )
 
     )
    
+
 
   (setq run t)
+
   ;; In order to highlight the actual place for inserting,
   ;; This loop replaces "$ $" by "$_$"
+
   ;; this is encoded by $ $.
   ;; Necessary for re-entry:
+
   ;; This loop replaces "$ $" by "$_$".
   ;; (unhighlight-regexp "\\$ \\$")
+
   ;; 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))  
 
   (goto-char (point-min))  
   (while (and run (re-search-forward "\\$ \\$" (point-max) t))
+
   (while (re-search-forward "\\$ \\$" (point-max) t)
    (replace-match "$_$")
+
    (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))  
 
   (goto-char (point-min))  
 
   (setq run t)
 
   (setq run t)
Line 87: Line 94:
 
     (unhighlight-regexp "\\$ \\$")
 
     (unhighlight-regexp "\\$ \\$")
 
     (highlight-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))
 
     (search-forward "$ $" (point-max))
     (set-mark (point))
+
     (backward-char 4)
 
     (setq s (concat "\$" (read-string "? " nil nil "_") "\$"))
 
     (setq s (concat "\$" (read-string "? " nil nil "_") "\$"))
 
     (if (equal s "$_$")  
 
     (if (equal s "$_$")  
(progn  
+
(progn ;; early exit
 
  (unhighlight-regexp "\\$ \\$")
 
  (unhighlight-regexp "\\$ \\$")
  (replace-match s t "\\")
+
  (forward-char 2)
  (exit-minibuffer)
 
 
  (setq run nil))
 
  (setq run nil))
       (replace-match s t "\\")
+
      (search-forward "$ $" (point-max))
       (goto-char (point-min))
+
      ;; readstring here may cause wrong replace-match for
       (unhighlight-regexp s)
+
      ;; multiline minibuffer input
      (goto-char (point-min)))))
+
       (replace-match s t t)
 +
       (copy-to-buffer b (point-min) (point-max))
 +
       (save-excursion
 +
(set-buffer b)
 +
(write-file b))
 +
      )))
  
 
(global-set-key (kbd "C-.") 'eom-remove-images)
 
(global-set-key (kbd "C-.") 'eom-remove-images)

Latest revision as of 21:08, 28 October 2014

;;--------------------------------------------------------------------------
;; 
;; 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 $_$.
"
  (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))
	;; 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))
	    )
	  )
	nil)
    )

  ;; 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))
      (save-excursion 
	(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: http://encyclopediaofmath.org/index.php?title=Emacs_eom_macros&oldid=34112