;;; ====================================================================
;;;  @Emacs-Lisp-file{
;;;     filename  = "acmcr.el",
;;;     version   = "1.01",
;;;     date      = "12 September 1995",
;;;     time      = "15:11:29 EDT",
;;;     author    = "David M. Jones",
;;;     email     = "dmjones@theory.lcs.mit.edu",
;;;     url       = "http://theory.lcs.mit.edu/~dmjones",
;;;     address   = "MIT Laboratory for Computer Science
;;;                  Room NE43-316
;;;                  545 Technology Square
;;;                  Cambridge, MA 02139
;;;                  USA",
;;;     telephone = "(617) 253-5936",
;;;     FAX       = "(617) 253-3480",
;;;     checksum  = "33040 454 1900 17409",
;;;     codetable = "ISO/ASCII",
;;;     keywords  = "",
;;;     supported = "yes",
;;;     abstract  = "This is a GNU Emacs package for expanding ACM
;;;                  Computing Classification category codes into the
;;;                  appropriate \category markup for use with the
;;;                  acmart document style.  To use it, type a
;;;                  category code, such as ``B.1.5'', place the point
;;;                  within the code, and then type ``M-x
;;;                  expand-acm-code''.  This will replace the
;;;                  category code by its expanded \category macro, in
;;;                  this case
;;;
;;;                      \category{B.1.5}{Control Structures and
;;;                      Microprogramming}{Microcode Applications}
;;;
;;;                  If given a prefix argument (e.g, by typing ``C-u
;;;                  M-x expand-acm-code''), the code will be expanded
;;;                  into LaTeX2e code instead, e.g.,
;;;
;;;                      B.1.5 [\textbf{Control Structures and
;;;                      Microprogramming}]: Microcode Applications
;;;
;;;                  Please note that this is in no way an official
;;;                  product of the ACM.  It's merely a by-product of
;;;                  my work on the JACM bibliography that I thought
;;;                  might be of some interest to others.",
;;;     docstring = "The checksum field above contains a CRC-16
;;;                  checksum as the first value, followed by the
;;;                  equivalent of the standard UNIX wc (word
;;;                  count) utility output of lines, words, and
;;;                  characters.  This is produced by Robert
;;;                  Solovay's checksum utility.",
;;;  }
;;; ====================================================================

(provide 'acmcr)

(defconst acmcr-code-alist
  '(("A"     . "General Literature")
    ("A.0"   . "General")
    ("A.1"   . "Introductory and Survey")
    ("A.2"   . "Reference (e.g., dictionaries, encyclopedias, glossaries)")
    ("A.m"   . "Miscellaneous")
    ("B"     . "Hardware")
    ("B.0"   . "General")
    ("B.1"   . "Control Structures and Microprogramming")
    ("B.1.0" . "General")
    ("B.1.1" . "Control Design Styles")
    ("B.1.2" . "Control Structure Performance Analysis and Design Aids")
    ("B.1.3" . "Control Structure Reliability, Testing, and Fault-Tolerance")
    ("B.1.4" . "Microprogram Design Aids")
    ("B.1.5" . "Microcode Applications")
    ("B.1.m" . "Miscellaneous")
    ("B.2"   . "Arithmetic and Logic Structures")
    ("B.2.0" . "General")
    ("B.2.1" . "Design Styles")
    ("B.2.2" . "Performance Analysis and Design Aids")
    ("B.2.3" . "Reliability, Testing, and Fault-Tolerance")
    ("B.2.m" . "Miscellaneous")
    ("B.3"   . "Memory Structures")
    ("B.3.0" . "General")
    ("B.3.1" . "Unassigned")
    ("B.3.2" . "Design Styles")
    ("B.3.3" . "Performance Analysis and Design Aids")
    ("B.3.4" . "Reliability, Testing, and Fault-Tolerance")
    ("B.3.m" . "Miscellaneous")
    ("B.4"   . "Input/Output and Data Communications")
    ("B.4.0" . "General")
    ("B.4.1" . "Data Communications Devices")
    ("B.4.2" . "Input/Output Devices")
    ("B.4.3" . "Interconnections (subsystems)")
    ("B.4.4" . "Performance Analysis and Design Aids")
    ("B.4.5" . "Reliability, Testing and Fault-Tolerance")
    ("B.4.m" . "Miscellaneous")
    ("B.5"   . "Register-Transfer-Level Implementation")
    ("B.5.0" . "General")
    ("B.5.1" . "Design")
    ("B.5.2" . "Design Aids")
    ("B.5.3" . "Reliability and Testing")
    ("B.5.m" . "Miscellaneous")
    ("B.6"   . "Logic Design")
    ("B.6.0" . "General")
    ("B.6.1" . "Design Styles")
    ("B.6.2" . "Reliability and Testing")
    ("B.6.3" . "Design Aids")
    ("B.6.m" . "Miscellaneous")
    ("B.7"   . "Integrated Circuits")
    ("B.7.0" . "General")
    ("B.7.1" . "Types and Design Styles")
    ("B.7.2" . "Design Aids")
    ("B.7.3" . "Reliability and Testing")
    ("B.7.m" . "Miscellaneous")
    ("B.m"   . "Miscellaneous")
    ("C"     . "Computer Systems Organization")
    ("C.0"   . "General")
    ("C.1"   . "Processor Architectures")
    ("C.1.0" . "General")
    ("C.1.1" . "Single Data Stream Architectures")
    ("C.1.2" . "Multiple Data Stream Architectures (Multiprocessors)")
    ("C.1.3" . "Other Architecture Styles")
    ("C.1.m" . "Miscellaneous")
    ("C.2"   . "Computer-Communication Networks")
    ("C.2.0" . "General")
    ("C.2.1" . "Network Architecture and Design")
    ("C.2.2" . "Network Protocols")
    ("C.2.3" . "Network Operations")
    ("C.2.4" . "Distributed Systems")
    ("C.2.5" . "Local Networks")
    ("C.2.m" . "Miscellaneous")
    ("C.3"   . "Special-Purpose and Application-Based Systems")
    ("C.4"   . "Performance of Systems")
    ("C.5"   . "Computer System Implementation")
    ("C.5.0" . "General")
    ("C.5.1" . "Large and Medium (\"Mainframe\") Computers")
    ("C.5.2" . "Minicomputers")
    ("C.5.3" . "Microcomputers")
    ("C.5.4" . "VLSI Systems")
    ("C.5.m" . "Miscellaneous")
    ("C.m"   . "Miscellaneous")
    ("D"     . "Software")
    ("D.0"   . "General")
    ("D.1"   . "Programming Techniques")
    ("D.1.0" . "General")
    ("D.1.1" . "Applicative (Functional) Programming")
    ("D.1.2" . "Automatic Programming")
    ("D.1.3" . "Concurrent Programming")
    ("D.1.4" . "Sequential Programming")
    ("D.1.5" . "Object-oriented Programming")
    ("D.1.6" . "Logic Programming")
    ("D.1.7" . "Visual Programming")
    ("D.1.m" . "Miscellaneous")
    ("D.2"   . "Software Engineering")
    ("D.2.0" . "General")
    ("D.2.1" . "Requirements/Specifications")
    ("D.2.2" . "Tools and Techniques")
    ("D.2.3" . "Coding")
    ("D.2.4" . "Program Verification")
    ("D.2.5" . "Testing and Debugging")
    ("D.2.6" . "Programming Environments")
    ("D.2.7" . "Distribution and Maintenance")
    ("D.2.8" . "Metrics")
    ("D.2.9" . "Management")
    ("D.2.10" . "Design")
    ("D.2.m" . "Miscellaneous")
    ("D.3"   . "Programming Languages")
    ("D.3.0" . "General")
    ("D.3.1" . "Formal Definitions and Theory")
    ("D.3.2" . "Language Classifications")
    ("D.3.3" . "Language Constructs and Features")
    ("D.3.4" . "Processors")
    ("D.3.m" . "Miscellaneous")
    ("D.4"   . "Operating Systems")
    ("D.4.0" . "General")
    ("D.4.1" . "Process Management")
    ("D.4.2" . "Storage Management")
    ("D.4.3" . "File Systems Management")
    ("D.4.4" . "Communications Management")
    ("D.4.5" . "Reliability")
    ("D.4.6" . "Security and Protection")
    ("D.4.7" . "Organization and Design")
    ("D.4.8" . "Performance")
    ("D.4.9" . "Systems Programs and Utilities")
    ("D.4.m" . "Miscellaneous")
    ("D.m"   . "Miscellaneous")
    ("E"     . "Data")
    ("E.0"   . "General")
    ("E.1"   . "Data Structures")
    ("E.2"   . "Data Storage Representations")
    ("E.3"   . "Data Encryption")
    ("E.4"   . "Coding and Information Theory")
    ("E.5"   . "Files")
    ("E.m"   . "Miscellaneous")
    ("F"     . "Theory of Computation")
    ("F.0"   . "General")
    ("F.1"   . "Computation by Abstract Devices")
    ("F.1.0" . "General")
    ("F.1.1" . "Models of Computation")
    ("F.1.2" . "Modes of Computation")
    ("F.1.3" . "Complexity Classes")
    ("F.1.m" . "Miscellaneous")
    ("F.2"   . "Analysis of Algorithms and Problem Complexity")
    ("F.2.0" . "General")
    ("F.2.1" . "Numerical Algorithms and Problems")
    ("F.2.2" . "Nonnumerical Algorithms and Problems")
    ("F.2.3" . "Tradeoffs among Complexity Measures")
    ("F.2.m" . "Miscellaneous")
    ("F.3"   . "Logics and Meanings of Programs")
    ("F.3.0" . "General")
    ("F.3.1" . "Specifying and Verifying and Reasoning about Programs")
    ("F.3.2" . "Semantics of Programming Languages")
    ("F.3.3" . "Studies of Program Constructs")
    ("F.3.m" . "Miscellaneous")
    ("F.4"   . "Mathematical Logic and Formal Languages")
    ("F.4.0" . "General")
    ("F.4.1" . "Mathematical Logic")
    ("F.4.2" . "Grammars and Other Rewriting Systems")
    ("F.4.3" . "Formal Languages")
    ("F.4.m" . "Miscellaneous")
    ("F.m"   . "Miscellaneous")
    ("G"     . "Mathematics of Computing")
    ("G.0"   . "General")
    ("G.1"   . "Numerical Analysis")
    ("G.1.0" . "General")
    ("G.1.1" . "Interpolation")
    ("G.1.2" . "Approximation")
    ("G.1.3" . "Numerical Linear Algebra")
    ("G.1.4" . "Quadrature and Numerical Differentiation")
    ("G.1.5" . "Roots of Nonlinear Equations")
    ("G.1.6" . "Optimization")
    ("G.1.7" . "Ordinary Differential Equations")
    ("G.1.8" . "Partial Differential Equations")
    ("G.1.9" . "Integral Equations")
    ("G.1.m" . "Miscellaneous")
    ("G.2"   . "Discrete Mathematics")
    ("G.2.0" . "General")
    ("G.2.1" . "Combinatorics")
    ("G.2.2" . "Graph Theory")
    ("G.2.m" . "Miscellaneous")
    ("G.3"   . "Probability and Statistics")
    ("G.4"   . "Mathematical Software")
    ("G.m"   . "Miscellaneous")
    ("H"     . "Information Systems")
    ("H.0"   . "General")
    ("H.1"   . "Models and Principles")
    ("H.1.0" . "General")
    ("H.1.1" . "Systems and Information Theory")
    ("H.1.2" . "User/Machine Systems")
    ("H.1.m" . "Miscellaneous")
    ("H.2"   . "Database Management")
    ("H.2.0" . "General")
    ("H.2.1" . "Logical Design")
    ("H.2.2" . "Physical Design")
    ("H.2.3" . "Languages")
    ("H.2.4" . "Systems")
    ("H.2.5" . "Heterogeneous Databases")
    ("H.2.6" . "Database Machines")
    ("H.2.7" . "Database Administration")
    ("H.2.8" . "Database Applications")
    ("H.2.m" . "Miscellaneous")
    ("H.3"   . "Information Storage and Retrieval")
    ("H.3.0" . "General")
    ("H.3.1" . "Content Analysis and Indexing")
    ("H.3.2" . "Information Storage")
    ("H.3.3" . "Information Search and Retrieval")
    ("H.3.4" . "Systems and Software")
    ("H.3.5" . "Online Information Services")
    ("H.3.6" . "Library Automation")
    ("H.3.m" . "Miscellaneous")
    ("H.4"   . "Information Systems Applications")
    ("H.4.0" . "General")
    ("H.4.1" . "Office Automation")
    ("H.4.2" . "Types of Systems")
    ("H.4.3" . "Communications Applications")
    ("H.4.m" . "Miscellaneous")
    ("H.5"   . "Information Interfaces and Presentation")
    ("H.5.0" . "General")
    ("H.5.1" . "Multimedia Information Systems")
    ("H.5.2" . "User Interfaces")
    ("H.5.3" . "Group and Organization Interfaces")
    ("H.m"   . "Miscellaneous")
    ("I"     . "Computing Methodologies")
    ("I.0"   . "General")
    ("I.1"   . "Algebraic Manipulation")
    ("I.1.0" . "General")
    ("I.1.1" . "Expressions and Their Representation")
    ("I.1.2" . "Algorithms")
    ("I.1.3" . "Languages and Systems")
    ("I.1.4" . "Applications")
    ("I.1.m" . "Miscellaneous")
    ("I.2"   . "Artificial Intelligence")
    ("I.2.0" . "General")
    ("I.2.1" . "Applications and Expert Systems")
    ("I.2.2" . "Automatic Programming")
    ("I.2.3" . "Deduction and Theorem Proving")
    ("I.2.4" . "Knowledge Representation Formalisms and Methods")
    ("I.2.5" . "Programming Languages and Software")
    ("I.2.6" . "Learning")
    ("I.2.7" . "Natural Language Processing")
    ("I.2.8" . "Problem Solving, Control Methods, and Search")
    ("I.2.9" . "Robotics")
    ("I.2.10" . "Vision and Scene Understanding")
    ("I.2.11" . "Distributed Artificial Intelligence")
    ("I.2.m" . "Miscellaneous")
    ("I.3"   . "Computer Graphics")
    ("I.3.0" . "General")
    ("I.3.1" . "Hardware Architecture")
    ("I.3.2" . "Graphics Systems")
    ("I.3.3" . "Picture/Image Generation")
    ("I.3.4" . "Graphics Utilities")
    ("I.3.5" . "Computational Geometry and Object Modeling")
    ("I.3.6" . "Methodology and Techniques")
    ("I.3.7" . "Three-Dimensional Graphics and Realism")
    ("I.3.8" . "Applications")
    ("I.3.m" . "Miscellaneous")
    ("I.4"   . "Image Processing")
    ("I.4.0" . "General")
    ("I.4.1" . "Digitization")
    ("I.4.2" . "Compression (Coding)")
    ("I.4.3" . "Enhancement")
    ("I.4.4" . "Restoration")
    ("I.4.5" . "Reconstruction")
    ("I.4.6" . "Segmentation")
    ("I.4.7" . "Feature Measurement")
    ("I.4.8" . "Scene Analysis")
    ("I.4.9" . "Applications")
    ("I.4.10" . "Image Representation")
    ("I.4.m" . "Miscellaneous")
    ("I.5"   . "Pattern Recognition")
    ("I.5.0" . "General")
    ("I.5.1" . "Models")
    ("I.5.2" . "Design Methodology")
    ("I.5.3" . "Clustering")
    ("I.5.4" . "Applications")
    ("I.5.5" . "Implementation")
    ("I.5.m" . "Miscellaneous")
    ("I.6"   . "Simulation and Modeling")
    ("I.6.0" . "General")
    ("I.6.1" . "Simulation Theory")
    ("I.6.2" . "Simulation Languages")
    ("I.6.3" . "Applications")
    ("I.6.4" . "Model Validation and Analysis")
    ("I.6.5" . "Model Development")
    ("I.6.6" . "Simulation Output Analysis")
    ("I.6.7" . "Simulation Support Systems")
    ("I.6.8" . "Types of Simulation")
    ("I.6.m" . "Miscellaneous")
    ("I.7"   . "Text Processing")
    ("I.7.0" . "General")
    ("I.7.1" . "Text Editing")
    ("I.7.2" . "Document Preparation")
    ("I.7.3" . "Index Generation")
    ("I.7.m" . "Miscellaneous")
    ("I.m"   . "Miscellaneous")
    ("J"     . "Computer Applications")
    ("J.0"   . "General")
    ("J.1"   . "Administrative Data Processing")
    ("J.2"   . "Physical Sciences and Engineering")
    ("J.3"   . "Life and Medical Sciences")
    ("J.4"   . "Social and Behavioral Sciences")
    ("J.5"   . "Arts and Humanities")
    ("J.6"   . "Computer-Aided Engineering")
    ("J.7"   . "Computers in Other Systems")
    ("J.m"   . "Miscellaneous")
    ("K"     . "Computing Milieux")
    ("K.0"   . "General")
    ("K.1"   . "The Computer Industry")
    ("K.2"   . "History of Computing")
    ("K.3"   . "Computers and Education")
    ("K.3.0" . "General")
    ("K.3.1" . "Computer Uses in Education")
    ("K.3.2" . "Computer and Information Science Education")
    ("K.3.m" . "Miscellaneous")
    ("K.4"   . "Computers and Society")
    ("K.4.0" . "General")
    ("K.4.1" . "Public Policy Issues")
    ("K.4.2" . "Social Issues")
    ("K.4.3" . "Organizational Impacts")
    ("K.4.m" . "Miscellaneous")
    ("K.5"   . "Legal Aspects of Computing")
    ("K.5.0" . "General")
    ("K.5.1" . "Software Protection")
    ("K.5.2" . "Governmental Issues")
    ("K.5.m" . "Miscellaneous")
    ("K.6"   . "Management of Computing and Information Systems")
    ("K.6.0" . "General")
    ("K.6.1" . "Project and People Management")
    ("K.6.2" . "Installation Management")
    ("K.6.3" . "Software Management")
    ("K.6.4" . "System Management")
    ("K.6.5" . "Security and Protection")
    ("K.6.m" . "Miscellaneous")
    ("K.7"   . "The Computing Profession")
    ("K.7.0" . "General")
    ("K.7.1" . "Occupations")
    ("K.7.2" . "Organizations")
    ("K.7.3" . "Testing, Certification, and Licensing")
    ("K.7.m" . "Miscellaneous")
    ("K.8"   . "Personal Computing")
    ("K.8.0" . "General")
    ("K.8.1" . "Application Packages")
    ("K.8.2" . "Hardware")
    ("K.8.3" . "Management/Maintenance")
    ("K.m"   . "Miscellaneous")))

;;  Process the code

(defun expand-acm-code (arg)
  (interactive "P")
  (mark-acmcr-code)
  (let* ((beg (mark))
         (end (point))
         (text (process-acmcr-code (buffer-substring beg end) arg)))
    (delete-region beg end)
    (insert text)))

(defun mark-acmcr-code ()
  (let ((regexp "\\b[A-Z]\\(\\.[0-9m]\\)?\\(\\.[0-9m]\\)?\\b"))
    (if (not (looking-at regexp))
        (re-search-backward "\\b[A-K]"))
    (set-mark (point))
    (re-search-forward regexp)))

;; SUBROUTINES

(defun process-acmcr-code (code arg)
  (let* ((components (acmcr-split-string code "."))
         (main (nth 0 components))
         (sub  (nth 1 components))
         (ssub (nth 2 components))
         (text ""))
    (if arg
        (progn
          (setq text
                (concat code " [\\textbf{"
                        (acmcr-title (concat main "." sub)) "}]"))
          (if ssub
              (setq text (concat text ": " (acmcr-title code)))))
      (setq text
            (concat "\\category{" code "}{"
                    (acmcr-title (concat main "." sub)) "}"))
      (if ssub
          (setq text (concat text "{" (acmcr-title code) "}"))
        (setq text (concat text "{}"))))
    text))

(defun acmcr-title (code)
  (cdr (assoc code acmcr-code-alist)))

(defun acmcr-split-string (string pattern &optional regexp)
  (or regexp (setq pattern (regexp-quote pattern)))
  (let (result)
    (while (string-match pattern string)
      (setq result
            (append result (list (substring string 0 (match-beginning 0)))))
      (setq string (substring string (match-end 0))))
    (append result (list string))))
