Home | History | Annotate | Line # | Download | only in misc
      1  1.1  christos ;;; po-mode.el -- major mode for GNU gettext PO files
      2  1.1  christos 
      3  1.1  christos ;; Copyright (C) 1995-1999, 2000-2002, 2005-2006 Free Software Foundation, Inc.
      4  1.1  christos 
      5  1.1  christos ;; Authors: Franois Pinard <pinard (at) iro.umontreal.ca>
      6  1.1  christos ;;          Greg McGary <gkm (at) magilla.cichlid.com>
      7  1.1  christos ;; Keywords: i18n gettext
      8  1.1  christos ;; Created: 1995
      9  1.1  christos 
     10  1.1  christos ;; This file is part of GNU gettext.
     11  1.1  christos 
     12  1.1  christos ;; GNU gettext is free software; you can redistribute it and/or modify
     13  1.1  christos ;; it under the terms of the GNU General Public License as published by
     14  1.1  christos ;; the Free Software Foundation; either version 2, or (at your option)
     15  1.1  christos ;; any later version.
     16  1.1  christos 
     17  1.1  christos ;; GNU gettext is distributed in the hope that it will be useful,
     18  1.1  christos ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     19  1.1  christos ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     20  1.1  christos ;; GNU General Public License for more details.
     21  1.1  christos 
     22  1.1  christos ;; You should have received a copy of the GNU General Public License
     23  1.1  christos ;; along with GNU Emacs; see the file COPYING.  If not, write to the
     24  1.1  christos ;; Free Software Foundation, 51 Franklin Street, Fifth Floor,
     25  1.1  christos ;; Boston, MA 02110-1301, USA.
     26  1.1  christos 
     27  1.1  christos ;;; Commentary:
     28  1.1  christos 
     29  1.1  christos ;; This package provides the tools meant to help editing PO files,
     30  1.1  christos ;; as documented in the GNU gettext user's manual.  See this manual
     31  1.1  christos ;; for user documentation, which is not repeated here.
     32  1.1  christos 
     33  1.1  christos ;; To install, merely put this file somewhere GNU Emacs will find it,
     34  1.1  christos ;; then add the following lines to your .emacs file:
     35  1.1  christos ;;
     36  1.1  christos ;;   (autoload 'po-mode "po-mode"
     37  1.1  christos ;;             "Major mode for translators to edit PO files" t)
     38  1.1  christos ;;   (setq auto-mode-alist (cons '("\\.po\\'\\|\\.po\\." . po-mode)
     39  1.1  christos ;;				  auto-mode-alist))
     40  1.1  christos ;;
     41  1.1  christos ;; To use the right coding system automatically under Emacs 20 or newer,
     42  1.1  christos ;; also add:
     43  1.1  christos ;;
     44  1.1  christos ;;   (autoload 'po-find-file-coding-system "po-compat")
     45  1.1  christos ;;   (modify-coding-system-alist 'file "\\.po\\'\\|\\.po\\."
     46  1.1  christos ;;				  'po-find-file-coding-system)
     47  1.1  christos ;;
     48  1.1  christos ;; You may also adjust some variables, below, by defining them in your
     49  1.1  christos ;; '.emacs' file, either directly or through command 'M-x customize'.
     50  1.1  christos 
     51  1.1  christos ;;; Code:
     52  1.1  christos 
     53  1.1  christos (defconst po-mode-version-string "2.02" "\
     55  1.1  christos Version number of this version of po-mode.el.")
     56  1.1  christos 
     57  1.1  christos ;;; Emacs portability matters - part I.
     58  1.1  christos ;;; Here is the minimum for customization to work.  See part II.
     59  1.1  christos 
     60  1.1  christos ;; Identify which Emacs variety is being used.
     61  1.1  christos ;; This file supports:
     62  1.1  christos ;;   - XEmacs (version 19 and above) -> po-XEMACS = t,
     63  1.1  christos ;;   - GNU Emacs (version 20 and above) -> po-EMACS20 = t,
     64  1.1  christos ;;   - GNU Emacs (version 19) -> no flag.
     65  1.1  christos (eval-and-compile
     66  1.1  christos   (cond ((string-match "XEmacs\\|Lucid" emacs-version)
     67  1.1  christos 	 (setq po-EMACS20 nil po-XEMACS t))
     68  1.1  christos 	((and (string-lessp "19" emacs-version) (featurep 'faces))
     69  1.1  christos 	 (setq po-EMACS20 t po-XEMACS nil))
     70  1.1  christos 	(t (setq po-EMACS20 nil po-XEMACS nil))))
     71  1.1  christos 
     72  1.1  christos ;; Experiment with Emacs LISP message internationalisation.
     73  1.1  christos (eval-and-compile
     74  1.1  christos   (or (fboundp 'set-translation-domain)
     75  1.1  christos       (defsubst set-translation-domain (string) nil))
     76  1.1  christos   (or (fboundp 'translate-string)
     77  1.1  christos       (defsubst translate-string (string) string)))
     78  1.1  christos (defsubst _ (string) (translate-string string))
     79  1.1  christos (defsubst N_ (string) string)
     80  1.1  christos 
     81  1.1  christos ;; Handle missing 'customs' package.
     82  1.1  christos (eval-and-compile
     83  1.1  christos   (condition-case ()
     84  1.1  christos       (require 'custom)
     85  1.1  christos     (error nil))
     86  1.1  christos   (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
     87  1.1  christos       nil
     88  1.1  christos     (defmacro defgroup (&rest args)
     89  1.1  christos       nil)
     90  1.1  christos     (defmacro defcustom (var value doc &rest args)
     91  1.1  christos       (` (defvar (, var) (, value) (, doc))))))
     92  1.1  christos 
     93  1.1  christos ;;; Customisation.
     95  1.1  christos 
     96  1.1  christos (defgroup po nil
     97  1.1  christos   "Major mode for editing PO files"
     98  1.1  christos   :group 'i18n)
     99  1.1  christos 
    100  1.1  christos (defcustom po-auto-edit-with-msgid nil
    101  1.1  christos   "*Automatically use msgid when editing untranslated entries."
    102  1.1  christos   :type 'boolean
    103  1.1  christos   :group 'po)
    104  1.1  christos 
    105  1.1  christos (defcustom po-auto-fuzzy-on-edit nil
    106  1.1  christos   "*Automatically mark entries fuzzy when being edited."
    107  1.1  christos   :type 'boolean
    108  1.1  christos   :group 'po)
    109  1.1  christos 
    110  1.1  christos (defcustom po-auto-select-on-unfuzzy nil
    111  1.1  christos   "*Automatically select some new entry while making an entry not fuzzy."
    112  1.1  christos   :type 'boolean
    113  1.1  christos   :group 'po)
    114  1.1  christos 
    115  1.1  christos (defcustom po-auto-replace-revision-date t
    116  1.1  christos   "*Automatically revise date in headers.  Value is nil, t, or ask."
    117  1.1  christos   :type '(choice (const nil)
    118  1.1  christos 		 (const t)
    119  1.1  christos 		 (const ask))
    120  1.1  christos   :group 'po)
    121  1.1  christos 
    122  1.1  christos (defcustom po-default-file-header "\
    123  1.1  christos # SOME DESCRIPTIVE TITLE.
    124  1.1  christos # Copyright (C) YEAR Free Software Foundation, Inc.
    125  1.1  christos # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
    126  1.1  christos #
    127  1.1  christos #, fuzzy
    128  1.1  christos msgid \"\"
    129  1.1  christos msgstr \"\"
    130  1.1  christos \"Project-Id-Version: PACKAGE VERSION\\n\"
    131  1.1  christos \"PO-Revision-Date: YEAR-MO-DA HO:MI +ZONE\\n\"
    132  1.1  christos \"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n\"
    133  1.1  christos \"Language-Team: LANGUAGE <LL (at) li.org>\\n\"
    134  1.1  christos \"MIME-Version: 1.0\\n\"
    135  1.1  christos \"Content-Type: text/plain; charset=CHARSET\\n\"
    136  1.1  christos \"Content-Transfer-Encoding: 8bit\\n\"
    137  1.1  christos "
    138  1.1  christos   "*Default PO file header."
    139  1.1  christos   :type 'string
    140  1.1  christos   :group 'po)
    141  1.1  christos 
    142  1.1  christos (defcustom po-translation-project-address
    143  1.1  christos   "translation (at) iro.umontreal.ca"
    144  1.1  christos   "*Electronic mail address of the Translation Project.
    145  1.1  christos Typing \\[po-send-mail] (normally bound to `M') the user will send the PO file
    146  1.1  christos to this email address."
    147  1.1  christos   :type 'string
    148  1.1  christos   :group 'po)
    149  1.1  christos 
    150  1.1  christos (defcustom po-translation-project-mail-label "TP-Robot"
    151  1.1  christos   "*Subject label when sending the PO file to `po-translation-project-address'.
    152  1.1  christos Don't change it when you send PO files to \"translation (at) iro.umontreal.ca\", the
    153  1.1  christos Translation Project Robot at http://www.iro.umontreal.ca/contrib/po/HTML/.  If
    154  1.1  christos the label is different, your submission will be consiedered as a regular mail
    155  1.1  christos and not stored at the TP site and also not forwarded to the package maintainer."
    156  1.1  christos   :type 'string
    157  1.1  christos   :group 'po)
    158  1.1  christos 
    159  1.1  christos (defcustom po-highlighting (or po-EMACS20 po-XEMACS)
    160  1.1  christos   "*Highlight text whenever appropriate, when non-nil.
    161  1.1  christos However, on older Emacses, a yet unexplained highlighting bug causes files
    162  1.1  christos to get mangled."
    163  1.1  christos   :type 'boolean
    164  1.1  christos   :group 'po)
    165  1.1  christos 
    166  1.1  christos (defcustom po-highlight-face 'highlight
    167  1.1  christos   "*The face used for PO mode highlighting.  For Emacses with overlays.
    168  1.1  christos Possible values are 'highlight', 'modeline', 'secondary-selection',
    169  1.1  christos 'region', and 'underline'.
    170  1.1  christos This variable can be set by the user to whatever face they desire.
    171  1.1  christos It's most convenient if the cursor color and highlight color are
    172  1.1  christos slightly different."
    173  1.1  christos   :type 'face
    174  1.1  christos   :group 'po)
    175  1.1  christos 
    176  1.1  christos (defcustom po-team-name-to-code
    177  1.1  christos   ;; All possible languages, a complete ISO 639 list and a little more.
    178  1.1  christos   '(("LANGUAGE" . "LL")
    179  1.1  christos     ("(Afan) Oromo" . "om")
    180  1.1  christos     ("Abkhazian" . "ab")
    181  1.1  christos     ("Afar" . "aa")
    182  1.1  christos     ("Afrikaans" . "af")
    183  1.1  christos     ("Albanian" . "sq")
    184  1.1  christos     ("Amharic" . "am")
    185  1.1  christos     ("Arabic" . "ar")
    186  1.1  christos     ("Argentinian" . "es_AR")
    187  1.1  christos     ("Armenian" . "hy")
    188  1.1  christos     ("Assamese" . "as")
    189  1.1  christos     ("Avestan" . "ae")
    190  1.1  christos     ("Aymara" . "ay")
    191  1.1  christos     ("Azerbaijani" . "az")
    192  1.1  christos     ("Bashkir" . "ba")
    193  1.1  christos     ("Basque" . "eu")
    194  1.1  christos     ("Belarusian" . "be")
    195  1.1  christos     ("Bengali" . "bn")
    196  1.1  christos     ("Bihari" . "bh")
    197  1.1  christos     ("Bislama" . "bi")
    198  1.1  christos     ("Bosnian" . "bs")
    199  1.1  christos     ("Brazilian Portuguese" . "pt_BR")
    200  1.1  christos     ("Breton" . "br")
    201  1.1  christos     ("Bulgarian" . "bg")
    202  1.1  christos     ("Burmese" . "my")
    203  1.1  christos     ("Catalan" . "ca")
    204  1.1  christos     ("Chamorro" . "ch")
    205  1.1  christos     ("Chechen" . "ce")
    206  1.1  christos     ("Chinese" . "zh")
    207  1.1  christos     ("Chinese (simplified)" . "zh_CN")
    208  1.1  christos     ("Chinese (traditional)" . "zh_TW")
    209  1.1  christos     ("Church Slavic" . "cu")
    210  1.1  christos     ("Chuvash" . "cv")
    211  1.1  christos     ("Cornish" . "kw")
    212  1.1  christos     ("Corsican" . "co")
    213  1.1  christos     ("Croatian" . "hr")
    214  1.1  christos     ("Czech" . "cs")
    215  1.1  christos     ("Danish" . "da")
    216  1.1  christos     ("Dutch" . "nl")
    217  1.1  christos     ("Dzongkha" . "dz")
    218  1.1  christos     ("English" . "en")
    219  1.1  christos     ("Esperanto" . "eo")
    220  1.1  christos     ("Estonian" . "et")
    221  1.1  christos     ("Faroese" . "fo")
    222  1.1  christos     ("Fijian" . "fj")
    223  1.1  christos     ("Finnish" . "fi")
    224  1.1  christos     ("French" . "fr")
    225  1.1  christos     ("Frisian" . "fy")
    226  1.1  christos     ("Galician" . "gl")
    227  1.1  christos     ("Georgian" . "ka")
    228  1.1  christos     ("German" . "de")
    229  1.1  christos     ("Greek" . "el")
    230  1.1  christos     ("Guarani" . "gn")
    231  1.1  christos     ("Gujarati" . "gu")
    232  1.1  christos     ("Hausa" . "ha")
    233  1.1  christos     ("Hebrew" . "he")
    234  1.1  christos     ("Herero" . "hz")
    235  1.1  christos     ("Hindi" . "hi")
    236  1.1  christos     ("Hiri Motu" . "ho")
    237  1.1  christos     ("Hungarian" . "hu")
    238  1.1  christos     ("Hyam" . "jab")
    239  1.1  christos     ("Icelandic" . "is")
    240  1.1  christos     ("Ido" . "io")
    241  1.1  christos     ("Indonesian" . "id")
    242  1.1  christos     ("Interlingua" . "ia")
    243  1.1  christos     ("Interlingue" . "ie")
    244  1.1  christos     ("Inuktitut" . "iu")
    245  1.1  christos     ("Inupiak" . "ik")
    246  1.1  christos     ("Irish" . "ga")
    247  1.1  christos     ("Italian" . "it")
    248  1.1  christos     ("Japanese" . "ja")
    249  1.1  christos     ("Javanese" . "jv")
    250  1.1  christos     ("Jju" . "kaj")
    251  1.1  christos     ("Kagoma" . "kdm")
    252  1.1  christos     ("Kalaallisut" . "kl")
    253  1.1  christos     ("Kannada" . "kn")
    254  1.1  christos     ("Kashmiri" . "ks")
    255  1.1  christos     ("Kazakh" . "kk")
    256  1.1  christos     ("Khmer" . "km")
    257  1.1  christos     ("Kikuyu" . "ki")
    258  1.1  christos     ("Kinyarwanda" . "rw")
    259  1.1  christos     ("Kirghiz" . "ky")
    260  1.1  christos     ("Kirundi" . "rn")
    261  1.1  christos     ("Komi" . "kv")
    262  1.1  christos     ("Konkani" . "kok")
    263  1.1  christos     ("Korean" . "ko")
    264  1.1  christos     ("Kuanyama" . "kj")
    265  1.1  christos     ("Kurdish" . "ku")
    266  1.1  christos     ("Laotian" . "lo")
    267  1.1  christos     ("Latin" . "la")
    268  1.1  christos     ("Latvian" . "lv")
    269  1.1  christos     ("Letzeburgesch" . "lb")
    270  1.1  christos     ("Lingala" . "ln")
    271  1.1  christos     ("Lithuanian" . "lt")
    272  1.1  christos     ("Low Saxon" . "nds")
    273  1.1  christos     ("Macedonian" . "mk")
    274  1.1  christos     ("Maithili" . "mai")
    275  1.1  christos     ("Malagasy" . "mg")
    276  1.1  christos     ("Malay" . "ms")
    277  1.1  christos     ("Malayalam" . "ml")
    278  1.1  christos     ("Maltese" . "mt")
    279  1.1  christos     ("Manipuri" . "mni")
    280  1.1  christos     ("Manx" . "gv")
    281  1.1  christos     ("Maori" . "mi")
    282  1.1  christos     ("Marathi" . "mr")
    283  1.1  christos     ("Marshall" . "mh")
    284  1.1  christos     ("Mayan" . "myn")
    285  1.1  christos     ("Moldavian" . "mo")
    286  1.1  christos     ("Mongolian" . "mn")
    287  1.1  christos     ("Nahuatl" . "nah")
    288  1.1  christos     ("Nauru" . "na")
    289  1.1  christos     ("Navajo" . "nv")
    290  1.1  christos     ("Ndonga" . "ng")
    291  1.1  christos     ("Nepali" . "ne")
    292  1.1  christos     ("North Ndebele" . "nd")
    293  1.1  christos     ("Northern Sami" . "se")
    294  1.1  christos     ("Northern Sotho" . "nso")
    295  1.1  christos     ("Norwegian Bokmal" . "nb")
    296  1.1  christos     ("Norwegian Nynorsk" . "nn")
    297  1.1  christos     ("Norwegian" . "no")
    298  1.1  christos     ("Nyanja" . "ny")
    299  1.1  christos     ("Occitan" . "oc")
    300  1.1  christos     ("Old English" . "ang")
    301  1.1  christos     ("Oriya" . "or")
    302  1.1  christos     ("Ossetian" . "os")
    303  1.1  christos     ("Pez" . "pbb")
    304  1.1  christos     ("Pali" . "pi")
    305  1.1  christos     ("Pashto" . "ps")
    306  1.1  christos     ("Persian" . "fa")
    307  1.1  christos     ("Polish" . "pl")
    308  1.1  christos     ("Portuguese" . "pt")
    309  1.1  christos     ("Punjabi" . "pa")
    310  1.1  christos     ("Quechua" . "qu")
    311  1.1  christos     ("Rhaeto-Roman" . "rm")
    312  1.1  christos     ("Romanian" . "ro")
    313  1.1  christos     ("Russian" . "ru")
    314  1.1  christos     ("Samoan" . "sm")
    315  1.1  christos     ("Sango" . "sg")
    316  1.1  christos     ("Sanskrit" . "sa")
    317  1.1  christos     ("Sardinian" . "sc")
    318  1.1  christos     ("Scots" . "gd")
    319  1.1  christos     ("Serbian" . "sr")
    320  1.1  christos     ("Sesotho" . "st")
    321  1.1  christos     ("Setswana" . "tn")
    322  1.1  christos     ("Shona" . "sn")
    323  1.1  christos     ("Sindhi" . "sd")
    324  1.1  christos     ("Sinhalese" . "si")
    325  1.1  christos     ("Siswati" . "ss")
    326  1.1  christos     ("Slovak" . "sk")
    327  1.1  christos     ("Slovenian" . "sl")
    328  1.1  christos     ("Somali" . "so")
    329  1.1  christos     ("Sorbian" . "wen")
    330  1.1  christos     ("South Ndebele" . "nr")
    331  1.1  christos     ("Spanish" . "es")
    332  1.1  christos     ("Sundanese" . "su")
    333  1.1  christos     ("Swahili" . "sw")
    334  1.1  christos     ("Swedish" . "sv")
    335  1.1  christos     ("Tagalog" . "tl")
    336  1.1  christos     ("Tahitian" . "ty")
    337  1.1  christos     ("Tajik" . "tg")
    338  1.1  christos     ("Tamil" . "ta")
    339  1.1  christos     ("Tatar" . "tt")
    340  1.1  christos     ("Telugu" . "te")
    341  1.1  christos     ("Tetum" . "tet")
    342  1.1  christos     ("Thai" . "th")
    343  1.1  christos     ("Tibetan" . "bo")
    344  1.1  christos     ("Tigrinya" . "ti")
    345  1.1  christos     ("Tonga" . "to")
    346  1.1  christos     ("Tsonga" . "ts")
    347  1.1  christos     ("Turkish" . "tr")
    348  1.1  christos     ("Turkmen" . "tk")
    349  1.1  christos     ("Twi" . "tw")
    350  1.1  christos     ("Tyap" . "kcg")
    351  1.1  christos     ("Uighur" . "ug")
    352  1.1  christos     ("Ukrainian" . "uk")
    353  1.1  christos     ("Urdu" . "ur")
    354  1.1  christos     ("Uzbek" . "uz")
    355  1.1  christos     ("Vietnamese" . "vi")
    356  1.1  christos     ("Volapuk" . "vo")
    357  1.1  christos     ("Walloon" . "wa")
    358  1.1  christos     ("Welsh" . "cy")
    359  1.1  christos     ("Wolof" . "wo")
    360  1.1  christos     ("Xhosa" . "xh")
    361  1.1  christos     ("Yiddish" . "yi")
    362  1.1  christos     ("Yoruba" . "yo")
    363  1.1  christos     ("Zapotec" . "zap")
    364  1.1  christos     ("Zhuang" . "za")
    365  1.1  christos     ("Zulu" . "zu")
    366  1.1  christos     )
    367  1.1  christos   "*Association list giving team codes from team names.
    368  1.1  christos This is used for generating a submission file name for the 'M' command.
    369  1.1  christos If a string instead of an alist, it is a team code to use unconditionnally."
    370  1.1  christos   :type 'sexp
    371  1.1  christos   :group 'po)
    372  1.1  christos 
    373  1.1  christos (defcustom po-gzip-uuencode-command "gzip -9 | uuencode -m"
    374  1.1  christos   "*The filter to use for preparing a mail invoice of the PO file.
    375  1.1  christos Normally \"gzip -9 | uuencode -m\", remove the -9 for lesser compression,
    376  1.1  christos or remove the -m if you are not using the GNU version of 'uuencode'."
    377  1.1  christos   :type 'string
    378  1.1  christos   :group 'po)
    379  1.1  christos 
    380  1.1  christos (defvar po-subedit-mode-syntax-table
    381  1.1  christos   (copy-syntax-table text-mode-syntax-table)
    382  1.1  christos   "Syntax table used while in PO mode.")
    383  1.1  christos 
    384  1.1  christos ;;; Emacs portability matters - part II.
    386  1.1  christos 
    387  1.1  christos ;;; Many portability matters are addressed in this page.  The few remaining
    388  1.1  christos ;;; cases, elsewhere, all involve  'eval-and-compile', 'boundp' or 'fboundp'.
    389  1.1  christos 
    390  1.1  christos ;; Protect string comparisons from text properties if possible.
    391  1.1  christos (eval-and-compile
    392  1.1  christos   (fset 'po-buffer-substring
    393  1.1  christos 	(symbol-function (if (fboundp 'buffer-substring-no-properties)
    394  1.1  christos 			     'buffer-substring-no-properties
    395  1.1  christos 			   'buffer-substring)))
    396  1.1  christos 
    397  1.1  christos   (if (fboundp 'match-string-no-properties)
    398  1.1  christos       (fset 'po-match-string (symbol-function 'match-string-no-properties))
    399  1.1  christos     (defun po-match-string (number)
    400  1.1  christos       "Return string of text matched by last search."
    401  1.1  christos       (po-buffer-substring (match-beginning number) (match-end number)))))
    402  1.1  christos 
    403  1.1  christos ;; Handle missing 'with-temp-buffer' function.
    404  1.1  christos (eval-and-compile
    405  1.1  christos   (if (fboundp 'with-temp-buffer)
    406  1.1  christos       (fset 'po-with-temp-buffer (symbol-function 'with-temp-buffer))
    407  1.1  christos 
    408  1.1  christos     (defmacro po-with-temp-buffer (&rest forms)
    409  1.1  christos       "Create a temporary buffer, and evaluate FORMS there like 'progn'."
    410  1.1  christos       (let ((curr-buffer (make-symbol "curr-buffer"))
    411  1.1  christos 	    (temp-buffer (make-symbol "temp-buffer")))
    412  1.1  christos 	`(let ((,curr-buffer (current-buffer))
    413  1.1  christos 	       (,temp-buffer (get-buffer-create
    414  1.1  christos 			      (generate-new-buffer-name " *po-temp*"))))
    415  1.1  christos 	   (unwind-protect
    416  1.1  christos 	       (progn
    417  1.1  christos 		 (set-buffer ,temp-buffer)
    418  1.1  christos 		 ,@forms)
    419  1.1  christos 	     (set-buffer ,curr-buffer)
    420  1.1  christos 	     (and (buffer-name ,temp-buffer)
    421  1.1  christos 		  (kill-buffer ,temp-buffer))))))))
    422  1.1  christos 
    423  1.1  christos ;; Handle missing 'kill-new' function.
    424  1.1  christos (eval-and-compile
    425  1.1  christos   (if (fboundp 'kill-new)
    426  1.1  christos       (fset 'po-kill-new (symbol-function 'kill-new))
    427  1.1  christos 
    428  1.1  christos     (defun po-kill-new (string)
    429  1.1  christos       "Push STRING onto the kill ring, for Emacs 18 where kill-new is missing."
    430  1.1  christos       (po-with-temp-buffer
    431  1.1  christos 	(insert string)
    432  1.1  christos 	(kill-region (point-min) (point-max))))))
    433  1.1  christos 
    434  1.1  christos ;; Handle missing 'read-event' function.
    435  1.1  christos (eval-and-compile
    436  1.1  christos   (fset 'po-read-event
    437  1.1  christos 	(cond ((fboundp 'read-event)
    438  1.1  christos 	       ;; GNU Emacs.
    439  1.1  christos 	       'read-event)
    440  1.1  christos 	      ((fboundp 'next-command-event)
    441  1.1  christos 	       ;; XEmacs.
    442  1.1  christos 	       'next-command-event)
    443  1.1  christos 	      (t
    444  1.1  christos 	       ;; Older Emacses.
    445  1.1  christos 	       'read-char))))
    446  1.1  christos 
    447  1.1  christos ;; Handle missing 'force-mode-line-update' function.
    448  1.1  christos (eval-and-compile
    449  1.1  christos   (if (fboundp 'force-mode-line-update)
    450  1.1  christos       (fset 'po-force-mode-line-update
    451  1.1  christos 	    (symbol-function 'force-mode-line-update))
    452  1.1  christos 
    453  1.1  christos     (defun po-force-mode-line-update ()
    454  1.1  christos       "Force the mode-line of the current buffer to be redisplayed."
    455  1.1  christos       (set-buffer-modified-p (buffer-modified-p)))))
    456  1.1  christos 
    457  1.1  christos ;; Handle portable highlighting.  Code has been adapted (OK... stolen! :-)
    458  1.1  christos ;; from 'ispell.el'.
    459  1.1  christos (eval-and-compile
    460  1.1  christos   (cond
    461  1.1  christos    (po-EMACS20
    462  1.1  christos 
    463  1.1  christos     (defun po-create-overlay ()
    464  1.1  christos       "Create and return a deleted overlay structure.
    465  1.1  christos The variable 'po-highlight-face' selects the face to use for highlighting."
    466  1.1  christos       (let ((overlay (make-overlay (point) (point))))
    467  1.1  christos 	(overlay-put overlay 'face po-highlight-face)
    468  1.1  christos 	;; The fun thing is that a deleted overlay retains its face, and is
    469  1.1  christos 	;; movable.
    470  1.1  christos 	(delete-overlay overlay)
    471  1.1  christos 	overlay))
    472  1.1  christos 
    473  1.1  christos     (defun po-highlight (overlay start end &optional buffer)
    474  1.1  christos       "Use OVERLAY to highlight the string from START to END.
    475  1.1  christos If limits are not relative to the current buffer, use optional BUFFER."
    476  1.1  christos       (move-overlay overlay start end (or buffer (current-buffer))))
    477  1.1  christos 
    478  1.1  christos     (defun po-rehighlight (overlay)
    479  1.1  christos       "Ensure OVERLAY is highlighted."
    480  1.1  christos       ;; There is nothing to do, as GNU Emacs allows multiple highlights.
    481  1.1  christos       nil)
    482  1.1  christos 
    483  1.1  christos     (defun po-dehighlight (overlay)
    484  1.1  christos       "Display normally the last string which OVERLAY highlighted.
    485  1.1  christos The current buffer should be in PO mode, when this function is called."
    486  1.1  christos       (delete-overlay overlay)))
    487  1.1  christos 
    488  1.1  christos    (po-XEMACS
    489  1.1  christos 
    490  1.1  christos     (defun po-create-overlay ()
    491  1.1  christos       "Create and return a deleted overlay structure."
    492  1.1  christos       ;; The same as for GNU Emacs above, except the created extent is
    493  1.1  christos       ;; already detached, so there's no need to "delete" it
    494  1.1  christos       ;; explicitly.
    495  1.1  christos       (let ((extent (make-extent nil nil)))
    496  1.1  christos 	(set-extent-face extent po-highlight-face)
    497  1.1  christos 	extent))
    498  1.1  christos 
    499  1.1  christos     (defun po-highlight (extent start end &optional buffer)
    500  1.1  christos       "Use EXTENT to highlight the string from START to END.
    501  1.1  christos If limits are not relative to the current buffer, use optional BUFFER."
    502  1.1  christos       (set-extent-endpoints extent start end (or buffer (current-buffer))))
    503  1.1  christos 
    504  1.1  christos     (defun po-rehighlight (extent)
    505  1.1  christos       "Ensure EXTENT is highlighted."
    506  1.1  christos       ;; Nothing to do here.
    507  1.1  christos       nil)
    508  1.1  christos 
    509  1.1  christos     (defun po-dehighlight (extent)
    510  1.1  christos       "Display normally the last string which EXTENT highlighted."
    511  1.1  christos       (detach-extent extent)))
    512  1.1  christos 
    513  1.1  christos    (t
    514  1.1  christos 
    515  1.1  christos     (defun po-create-overlay ()
    516  1.1  christos       "Create and return a deleted overlay structure."
    517  1.1  christos       (cons (make-marker) (make-marker)))
    518  1.1  christos 
    519  1.1  christos     (defun po-highlight (overlay start end &optional buffer)
    520  1.1  christos       "Use OVERLAY to highlight the string from START to END.
    521  1.1  christos If limits are not relative to the current buffer, use optional BUFFER.
    522  1.1  christos No doubt that highlighting, when Emacs does not allow it, is a kludge."
    523  1.1  christos       (save-excursion
    524  1.1  christos 	(and buffer (set-buffer buffer))
    525  1.1  christos 	(let ((modified (buffer-modified-p))
    526  1.1  christos 	      (buffer-read-only nil)
    527  1.1  christos 	      (inhibit-quit t)
    528  1.1  christos 	      (buffer-undo-list t)
    529  1.1  christos 	      (text (buffer-substring start end)))
    530  1.1  christos 	  (goto-char start)
    531  1.1  christos 	  (delete-region start end)
    532  1.1  christos 	  (insert-char ?  (- end start))
    533  1.1  christos 	  (sit-for 0)
    534  1.1  christos 	  (setq inverse-video (not inverse-video))
    535  1.1  christos 	  (delete-region start end)
    536  1.1  christos 	  (insert text)
    537  1.1  christos 	  (sit-for 0)
    538  1.1  christos 	  (setq inverse-video (not inverse-video))
    539  1.1  christos 	  (set-buffer-modified-p modified)))
    540  1.1  christos       (set-marker (car overlay) start (or buffer (current-buffer)))
    541  1.1  christos       (set-marker (cdr overlay) end (or buffer (current-buffer))))
    542  1.1  christos 
    543  1.1  christos     (defun po-rehighlight (overlay)
    544  1.1  christos       "Ensure OVERLAY is highlighted."
    545  1.1  christos       (let ((buffer (marker-buffer (car overlay)))
    546  1.1  christos 	    (start (marker-position (car overlay)))
    547  1.1  christos 	    (end (marker-position (cdr overlay))))
    548  1.1  christos 	(and buffer
    549  1.1  christos 	     (buffer-name buffer)
    550  1.1  christos 	     (po-highlight overlay start end buffer))))
    551  1.1  christos 
    552  1.1  christos     (defun po-dehighlight (overlay)
    553  1.1  christos       "Display normally the last string which OVERLAY highlighted."
    554  1.1  christos       (let ((buffer (marker-buffer (car overlay)))
    555  1.1  christos 	    (start (marker-position (car overlay)))
    556  1.1  christos 	    (end (marker-position (cdr overlay))))
    557  1.1  christos 	(if buffer
    558  1.1  christos 	    (save-excursion
    559  1.1  christos 	      (set-buffer buffer)
    560  1.1  christos 	      (let ((modified (buffer-modified-p))
    561  1.1  christos 		    (buffer-read-only nil)
    562  1.1  christos 		    (inhibit-quit t)
    563  1.1  christos 		    (buffer-undo-list t))
    564  1.1  christos 		(let ((text (buffer-substring start end)))
    565  1.1  christos 		  (goto-char start)
    566  1.1  christos 		  (delete-region start end)
    567  1.1  christos 		  (insert-char ?  (- end start))
    568  1.1  christos 		  (sit-for 0)
    569  1.1  christos 		  (delete-region start end)
    570  1.1  christos 		  (insert text)
    571  1.1  christos 		  (sit-for 0)
    572  1.1  christos 		  (set-buffer-modified-p modified)))))
    573  1.1  christos 	(setcar overlay (make-marker))
    574  1.1  christos 	(setcdr overlay (make-marker))))
    575  1.1  christos 
    576  1.1  christos     )))
    577  1.1  christos 
    578  1.1  christos ;;; Buffer local variables.
    580  1.1  christos 
    581  1.1  christos ;; The following block of declarations has the main purpose of avoiding
    582  1.1  christos ;; byte compiler warnings.  It also introduces some documentation for
    583  1.1  christos ;; each of these variables, all meant to be local to PO mode buffers.
    584  1.1  christos 
    585  1.1  christos ;; Flag telling that MODE-LINE-STRING should be displayed.  See 'Window'
    586  1.1  christos ;; page below.  Exceptionally, this variable is local to *all* buffers.
    587  1.1  christos (defvar po-mode-flag)
    588  1.1  christos 
    589  1.1  christos ;; PO buffers are kept read-only to prevent random modifications.  READ-ONLY
    590  1.1  christos ;; holds the value of the read-only flag before PO mode was entered.
    591  1.1  christos (defvar po-read-only)
    592  1.1  christos 
    593  1.1  christos ;; The current entry extends from START-OF-ENTRY to END-OF-ENTRY, it
    594  1.1  christos ;; includes preceding whitespace and excludes following whitespace.  The
    595  1.1  christos ;; start of keyword lines are START-OF-MSGID and START-OF-MSGSTR.
    596  1.1  christos ;; ENTRY-TYPE classifies the entry.
    597  1.1  christos (defvar po-start-of-entry)
    598  1.1  christos (defvar po-start-of-msgid)
    599  1.1  christos (defvar po-start-of-msgstr)
    600  1.1  christos (defvar po-end-of-entry)
    601  1.1  christos (defvar po-entry-type)
    602  1.1  christos 
    603  1.1  christos ;; A few counters are usefully shown in the Emacs mode line.
    604  1.1  christos (defvar po-translated-counter)
    605  1.1  christos (defvar po-fuzzy-counter)
    606  1.1  christos (defvar po-untranslated-counter)
    607  1.1  christos (defvar po-obsolete-counter)
    608  1.1  christos (defvar po-mode-line-string)
    609  1.1  christos 
    610  1.1  christos ;; PO mode keeps track of fields being edited, for one given field should
    611  1.1  christos ;; have one editing buffer at most, and for exiting a PO buffer properly
    612  1.1  christos ;; should offer to close all pending edits.  Variable EDITED-FIELDS holds an
    613  1.1  christos ;; an list of "slots" of the form: (ENTRY-MARKER EDIT-BUFFER OVERLAY-INFO).
    614  1.1  christos ;; To allow simultaneous edition of the comment and the msgstr of an entry,
    615  1.1  christos ;; ENTRY-MARKER points to the msgid line if a comment is being edited, or to
    616  1.1  christos ;; the msgstr line if the msgstr is being edited.  EDIT-BUFFER is the
    617  1.1  christos ;; temporary Emacs buffer used to edit the string.  OVERLAY-INFO, when not
    618  1.1  christos ;; nil, holds an overlay (or if overlays are not supported, a cons of two
    619  1.1  christos ;; markers) for this msgid string which became highlighted for the edit.
    620  1.1  christos (defvar po-edited-fields)
    621  1.1  christos 
    622  1.1  christos ;; We maintain a set of movable pointers for returning to entries.
    623  1.1  christos (defvar po-marker-stack)
    624  1.1  christos 
    625  1.1  christos ;; SEARCH path contains a list of directories where files may be found,
    626  1.1  christos ;; in a format suitable for read completion.  Each directory includes
    627  1.1  christos ;; its trailing slash.  PO mode starts with "./" and "../".
    628  1.1  christos (defvar po-search-path)
    629  1.1  christos 
    630  1.1  christos ;; The following variables are meaningful only when REFERENCE-CHECK
    631  1.1  christos ;; is identical to START-OF-ENTRY, else they should be recomputed.
    632  1.1  christos ;; REFERENCE-ALIST contains all known references for the current
    633  1.1  christos ;; entry, each list element is (PROMPT FILE LINE), where PROMPT may
    634  1.1  christos ;; be used for completing read, FILE is a string and LINE is a number.
    635  1.1  christos ;; REFERENCE-CURSOR is a cycling cursor into REFERENCE-ALIST.
    636  1.1  christos (defvar po-reference-alist)
    637  1.1  christos (defvar po-reference-cursor)
    638  1.1  christos (defvar po-reference-check)
    639  1.1  christos 
    640  1.1  christos ;; The following variables are for marking translatable strings in program
    641  1.1  christos ;; sources.  KEYWORDS is the list of keywords for marking translatable
    642  1.1  christos ;; strings, kept in a format suitable for reading with completion.
    643  1.1  christos ;; STRING-CONTENTS holds the value of the most recent string found in sources,
    644  1.1  christos ;; and when it is not nil, then STRING-BUFFER, STRING-START and STRING-END
    645  1.1  christos ;; describe where it is.  MARKING-OVERLAY, if not 'nil', holds the overlay
    646  1.1  christos ;; which highlight the last found string; for older Emacses, it holds the cons
    647  1.1  christos ;; of two markers around the highlighted region.
    648  1.1  christos (defvar po-keywords)
    649  1.1  christos (defvar po-string-contents)
    650  1.1  christos (defvar po-string-buffer)
    651  1.1  christos (defvar po-string-start)
    652  1.1  christos (defvar po-string-end)
    653  1.1  christos (defvar po-marking-overlay)
    654  1.1  christos 
    655  1.1  christos ;;; PO mode variables and constants (usually not to customize).
    657  1.1  christos 
    658  1.1  christos ;; The textdomain should really be "gettext", only trying it for now.
    659  1.1  christos ;; All this requires more thinking, we cannot just do this like that.
    660  1.1  christos (set-translation-domain "po-mode")
    661  1.1  christos 
    662  1.1  christos (defun po-mode-version ()
    663  1.1  christos   "Show Emacs PO mode version."
    664  1.1  christos   (interactive)
    665  1.1  christos   (message (_"Emacs PO mode, version %s") po-mode-version-string))
    666  1.1  christos 
    667  1.1  christos (defconst po-help-display-string
    668  1.1  christos   (_"\
    669  1.1  christos PO Mode Summary           Next Previous            Miscellaneous
    670  1.1  christos *: Later, /: Docum        n    p    Any type       .     Redisplay
    671  1.1  christos                           t    T    Translated     /v    Version info
    672  1.1  christos Moving around             f    F    Fuzzy          ?, h  This help
    673  1.1  christos <    First if any         o    O    Obsolete       =     Current index
    674  1.1  christos >    Last if any          u    U    Untranslated   0     Other window
    675  1.1  christos /SPC Auto select                                   V     Validate
    676  1.1  christos                         Msgstr Comments            M     Mail officially
    677  1.1  christos Modifying entries         RET  #    Call editor    _     Undo
    678  1.1  christos TAB   Remove fuzzy mark   k    K    Kill to        E     Edit out full
    679  1.1  christos DEL   Fuzzy or fade out   w    W    Copy to        Q     Forceful quit
    680  1.1  christos LFD   Init with msgid     y    Y    Yank from      q     Confirm and quit
    681  1.1  christos 
    682  1.1  christos gettext Keyword Marking                            Position Stack
    683  1.1  christos ,    Find next string     Compendiums              m  Mark and push current
    684  1.1  christos M-,  Mark translatable    *c    To compendium      r  Pop and return
    685  1.1  christos M-.  Change mark, mark    *M-C  Select, save       x  Exchange current/top
    686  1.1  christos 
    687  1.1  christos Program Sources           Auxiliary Files          Lexicography
    688  1.1  christos s    Cycle reference      a    Cycle file          *l    Lookup translation
    689  1.1  christos M-s  Select reference     C-c C-a  Select file     *M-l  Add/edit translation
    690  1.1  christos S    Consider path        A    Consider PO file    *L    Consider lexicon
    691  1.1  christos M-S  Ignore path          M-A  Ignore PO file      *M-L  Ignore lexicon
    692  1.1  christos ")
    693  1.1  christos   "Help page for PO mode.")
    694  1.1  christos 
    695  1.1  christos (defconst po-mode-menu-layout
    696  1.1  christos   `("PO"
    697  1.1  christos     ("Moving around"
    698  1.1  christos      ["Auto select" po-auto-select-entry
    699  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    700  1.1  christos 	  '(:help "Jump to next interesting entry"))]
    701  1.1  christos      "---"
    702  1.1  christos      "Forward"
    703  1.1  christos      ["Any next" po-next-entry
    704  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    705  1.1  christos 	  '(:help "Jump to next entry"))]
    706  1.1  christos      ["Next translated" po-next-translated-entry
    707  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    708  1.1  christos 	  '(:help "Jump to next translated entry"))]
    709  1.1  christos      ["Next fuzzy" po-next-fuzzy-entry
    710  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    711  1.1  christos 	  '(:help "Jump to next fuzzy entry"))]
    712  1.1  christos      ["Next obsolete" po-next-obsolete-entry
    713  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    714  1.1  christos 	  '(:help "Jump to next obsolete entry"))]
    715  1.1  christos      ["Next untranslated" po-next-untranslated-entry
    716  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    717  1.1  christos 	  '(:help "Jump to next untranslated entry"))]
    718  1.1  christos      ["Last file entry" po-last-entry
    719  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    720  1.1  christos 	  '(:help "Jump to last entry"))]
    721  1.1  christos      "---"
    722  1.1  christos      "Backward"
    723  1.1  christos      ["Any previous" po-previous-entry
    724  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    725  1.1  christos 	  '(:help "Jump to previous entry"))]
    726  1.1  christos      ["Previous translated" po-previous-translated-entry
    727  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    728  1.1  christos 	  '(:help "Jump to previous translated entry"))]
    729  1.1  christos      ["Previous fuzzy" po-previous-fuzzy-entry
    730  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    731  1.1  christos 	  '(:help "Jump to previous fuzzy entry"))]
    732  1.1  christos      ["Previous obsolete" po-previous-obsolete-entry
    733  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    734  1.1  christos 	  '(:help "Jump to previous obsolete entry"))]
    735  1.1  christos      ["Previous untranslated" po-previous-untranslated-entry
    736  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    737  1.1  christos 	  '(:help "Jump to previous untranslated entry"))]
    738  1.1  christos      ["First file entry" po-first-entry
    739  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    740  1.1  christos 	  '(:help "Jump to first entry"))]
    741  1.1  christos      "---"
    742  1.1  christos      "Position stack"
    743  1.1  christos      ["Mark and push current" po-push-location
    744  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    745  1.1  christos 	  '(:help "Remember current location"))]
    746  1.1  christos      ["Pop and return" po-pop-location
    747  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    748  1.1  christos 	  '(:help "Jump to last remembered location and forget about it"))]
    749  1.1  christos      ["Exchange current/top" po-exchange-location
    750  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    751  1.1  christos 	  '(:help "Jump to last remembered location and remember current location"))]
    752  1.1  christos      "---"
    753  1.1  christos      ["Redisplay" po-current-entry
    754  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    755  1.1  christos 	  '(:help "Make current entry properly visible"))]
    756  1.1  christos      ["Current index" po-statistics
    757  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    758  1.1  christos 	  '(:help "Statistical info on current translation file"))])
    759  1.1  christos     ("Modifying entries"
    760  1.1  christos      ["Undo" po-undo
    761  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    762  1.1  christos 	  '(:help "Revoke last changed entry"))]
    763  1.1  christos      "---"
    764  1.1  christos      "Msgstr"
    765  1.1  christos      ["Edit msgstr" po-edit-msgstr
    766  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    767  1.1  christos 	  '(:help "Edit current translation"))]
    768  1.1  christos      ["Ediff and merge msgstr" po-edit-msgstr-and-ediff
    769  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    770  1.1  christos 	  '(:help "Call `ediff' on current translation for merging"))]
    771  1.1  christos      ["Cut msgstr" po-kill-msgstr
    772  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    773  1.1  christos 	  '(:help "Cut (kill) current translation"))]
    774  1.1  christos      ["Copy msgstr" po-kill-ring-save-msgstr
    775  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    776  1.1  christos 	  '(:help "Copy current translation"))]
    777  1.1  christos      ["Paste msgstr" po-yank-msgstr
    778  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    779  1.1  christos 	  '(:help "Paste (yank) text most recently cut/copied translation"))]
    780  1.1  christos      "---"
    781  1.1  christos      "Comments"
    782  1.1  christos      ["Edit comment" po-edit-comment
    783  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    784  1.1  christos 	  '(:help "Edit current comment"))]
    785  1.1  christos      ["Ediff and merge comment" po-edit-comment-and-ediff
    786  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    787  1.1  christos 	  '(:help "Call `ediff' on current comment for merging"))]
    788  1.1  christos      ["Cut comment" po-kill-comment
    789  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    790  1.1  christos 	  '(:help "Cut (kill) current comment"))]
    791  1.1  christos      ["Copy comment" po-kill-ring-save-comment
    792  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    793  1.1  christos 	  '(:help "Copy current translation"))]
    794  1.1  christos      ["Paste comment" po-yank-comment
    795  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    796  1.1  christos 	  '(:help "Paste (yank) text most recently cut/copied"))]
    797  1.1  christos      "---"
    798  1.1  christos      ["Remove fuzzy mark" po-unfuzzy
    799  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    800  1.1  christos 	  '(:help "Remove \"#, fuzzy\""))]
    801  1.1  christos      ["Fuzzy or fade out" po-fade-out-entry
    802  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    803  1.1  christos 	  '(:help "Set current entry fuzzy, or if already fuzzy delete it"))]
    804  1.1  christos      ["Init with msgid" po-msgid-to-msgstr
    805  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    806  1.1  christos 	  '(:help "\
    807  1.1  christos Initialize or replace current translation with the original message"))])
    808  1.1  christos     ("Other files"
    809  1.1  christos      ["Other window" po-other-window
    810  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    811  1.1  christos 	  '(:help "Select other window; if necessay split current frame"))]
    812  1.1  christos      "---"
    813  1.1  christos      "Program sources"
    814  1.1  christos      ["Cycle reference" po-cycle-source-reference t]
    815  1.1  christos      ["Select reference" po-select-source-reference t]
    816  1.1  christos      ["Consider path" po-consider-source-path t]
    817  1.1  christos      ["Ignore path" po-ignore-source-path t]
    818  1.1  christos      "---"
    819  1.1  christos      "Compendiums"
    820  1.1  christos      ["To compendium" po-save-entry nil]
    821  1.1  christos      ["Select, save" po-select-and-save-entry nil]
    822  1.1  christos      "---"
    823  1.1  christos      "Auxiliary files"
    824  1.1  christos      ["Cycle file" po-cycle-auxiliary t]
    825  1.1  christos      ["Select file" po-select-auxiliary t]
    826  1.1  christos      ["Consider file" po-consider-as-auxiliary t]
    827  1.1  christos      ["Ignore file" po-ignore-as-auxiliary t]
    828  1.1  christos      "---"
    829  1.1  christos      "Lexicography"
    830  1.1  christos      ["Lookup translation" po-lookup-lexicons nil]
    831  1.1  christos      ["Add/edit translation" po-edit-lexicon-entry nil]
    832  1.1  christos      ["Consider lexicon" po-consider-lexicon-file nil]
    833  1.1  christos      ["Ignore lexicon" po-ignore-lexicon-file nil])
    834  1.1  christos     "---"
    835  1.1  christos     "Source marking"
    836  1.1  christos     ["Find first string" (po-tags-search '(nil)) t]
    837  1.1  christos     ["Prefer keyword" (po-select-mark-and-mark '(nil)) t]
    838  1.1  christos     ["Find next string" po-tags-search t]
    839  1.1  christos     ["Mark preferred" po-mark-translatable t]
    840  1.1  christos     ["Mark with keyword" po-select-mark-and-mark t]
    841  1.1  christos     "---"
    842  1.1  christos     ["Version info" po-mode-version
    843  1.1  christos      ,@(if (featurep 'xemacs) '(t)
    844  1.1  christos 	  '(:help "Display version number of PO mode"))]
    845  1.1  christos     ["Help page" po-help
    846  1.1  christos      ,@(if (featurep 'xemacs) '(t)
    847  1.1  christos 	  '(:help "Show the PO mode help screen"))]
    848  1.1  christos     ["Validate" po-validate
    849  1.1  christos      ,@(if (featurep 'xemacs) '(t)
    850  1.1  christos 	  '(:help "Check validity of current translation file using `msgfmt'"))]
    851  1.1  christos     ["Mail officially" po-send-mail
    852  1.1  christos      ,@(if (featurep 'xemacs) '(t)
    853  1.1  christos 	  '(:help "Send current translation file to the Translation Robot by mail"))]
    854  1.1  christos     ["Edit out full" po-edit-out-full
    855  1.1  christos      ,@(if (featurep 'xemacs) '(t)
    856  1.1  christos 	  '(:help "Leave PO mode to edit translation file using fundamental mode"))]
    857  1.1  christos     "---"
    858  1.1  christos     ["Forceful quit" po-quit
    859  1.1  christos      ,@(if (featurep 'xemacs) '(t)
    860  1.1  christos 	  '(:help "Close (kill) current translation file without saving"))]
    861  1.1  christos     ["Soft quit" po-confirm-and-quit
    862  1.1  christos      ,@(if (featurep 'xemacs) '(t)
    863  1.1  christos 	  '(:help "Save current translation file, than close (kill) it"))])
    864  1.1  christos   "Menu layout for PO mode.")
    865  1.1  christos 
    866  1.1  christos (defconst po-subedit-mode-menu-layout
    867  1.1  christos   `("PO-Edit"
    868  1.1  christos     ["Ediff and merge translation variants" po-subedit-ediff
    869  1.1  christos       ,@(if (featurep 'xemacs) '(t)
    870  1.1  christos 	  '(:help "Call `ediff' for merging variants"))]
    871  1.1  christos     ["Cycle through auxiliary files" po-subedit-cycle-auxiliary t]
    872  1.1  christos     "---"
    873  1.1  christos     ["Abort edit" po-subedit-abort
    874  1.1  christos      ,@(if (featurep 'xemacs) '(t)
    875  1.1  christos 	  '(:help "Don't change the translation"))]
    876  1.1  christos     ["Exit edit" po-subedit-exit
    877  1.1  christos      ,@(if (featurep 'xemacs) '(t)
    878  1.1  christos 	 '(:help "Use this text as the translation and close current edit buffer"))])
    879  1.1  christos   "Menu layout for PO subedit mode.")
    880  1.1  christos 
    881  1.1  christos (defconst po-subedit-message
    882  1.1  christos   (_"Type 'C-c C-c' once done, or 'C-c C-k' to abort edit")
    883  1.1  christos   "Message to post in the minibuffer when an edit buffer is displayed.")
    884  1.1  christos 
    885  1.1  christos (defvar po-auxiliary-list nil
    886  1.1  christos   "List of auxiliary PO files, in completing read format.")
    887  1.1  christos 
    888  1.1  christos (defvar po-auxiliary-cursor nil
    889  1.1  christos   "Cursor into the 'po-auxiliary-list'.")
    890  1.1  christos 
    891  1.1  christos (defvar po-compose-mail-function
    892  1.1  christos   (let ((functions '(compose-mail-other-window
    893  1.1  christos 		     message-mail-other-window
    894  1.1  christos 		     compose-mail
    895  1.1  christos 		     message-mail))
    896  1.1  christos 	result)
    897  1.1  christos     (while (and (not result) functions)
    898  1.1  christos       (if (fboundp (car functions))
    899  1.1  christos 	  (setq result (car functions))
    900  1.1  christos 	(setq functions (cdr functions))))
    901  1.1  christos     (cond (result)
    902  1.1  christos 	  ((fboundp 'mail-other-window)
    903  1.1  christos 	   (function (lambda (to subject)
    904  1.1  christos 		       (mail-other-window nil to subject))))
    905  1.1  christos 	  ((fboundp 'mail)
    906  1.1  christos 	   (function (lambda (to subject)
    907  1.1  christos 		       (mail nil to subject))))
    908  1.1  christos 	  (t (function (lambda (to subject)
    909  1.1  christos 			 (error (_"I do not know how to mail to '%s'") to))))))
    910  1.1  christos   "Function to start composing an electronic message.")
    911  1.1  christos 
    912  1.1  christos (defvar po-any-msgid-regexp
    913  1.1  christos   "^\\(#~[ \t]*\\)?msgid.*\n\\(\\(#~[ \t]*\\)?\".*\n\\)*"
    914  1.1  christos   "Regexp matching a whole msgid field, whether obsolete or not.")
    915  1.1  christos 
    916  1.1  christos (defvar po-any-msgstr-regexp
    917  1.1  christos   ;; "^\\(#~[ \t]*\\)?msgstr.*\n\\(\\(#~[ \t]*\\)?\".*\n\\)*"
    918  1.1  christos   "^\\(#~[ \t]*\\)?msgstr\\(\\[[0-9]\\]\\)?.*\n\\(\\(#~[ \t]*\\)?\".*\n\\)*"
    919  1.1  christos   "Regexp matching a whole msgstr or msgstr[] field, whether obsolete or not.")
    920  1.1  christos 
    921  1.1  christos (defvar po-msgstr-idx-keyword-regexp
    922  1.1  christos   "^\\(#~[ \t]*\\)?msgstr\\[[0-9]\\]"
    923  1.1  christos   "Regexp matching an indexed msgstr keyword, whether obsolete or not.")
    924  1.1  christos 
    925  1.1  christos (defvar po-msgfmt-program "msgfmt"
    926  1.1  christos   "Path to msgfmt program from GNU gettext package.")
    927  1.1  christos 
    928  1.1  christos ;; Font lock based highlighting code.
    929  1.1  christos (defconst po-font-lock-keywords
    930  1.1  christos   '(
    931  1.1  christos     ;; ("^\\(msgid \\|msgstr \\)?\"\\|\"$" . font-lock-keyword-face)
    932  1.1  christos     ;; (regexp-opt
    933  1.1  christos     ;;  '("msgid " "msgid_plural " "msgstr " "msgstr[0] " "msgstr[1] "))
    934  1.1  christos     ("\
    935  1.1  christos ^\\(\\(msg\\(id\\(_plural\\)?\\|str\\(\\[[0-9]\\]\\)?\\)?\\) \\)?\"\\|\"$"
    936  1.1  christos      . font-lock-keyword-face)
    937  1.1  christos     ("\\\\.\\|%\\*?[-.0-9ul]*[a-zA-Z]" . font-lock-variable-name-face)
    938  1.1  christos     ("^# .*\\|^#[:,]?" . font-lock-comment-face)
    939  1.1  christos     ("^#:\\(.*\\)" 1 font-lock-reference-face)
    940  1.1  christos     ;; The following line does not work, and I wonder why.
    941  1.1  christos     ;;("^#,\\(.*\\)" 1 font-function-name-reference-face)
    942  1.1  christos     )
    943  1.1  christos   "Additional expressions to highlight in PO mode.")
    944  1.1  christos 
    945  1.1  christos ;; Old activator for 'font lock'.  Is it still useful?  I don't think so.
    946  1.1  christos ;;(if (boundp 'font-lock-keywords)
    947  1.1  christos ;;    (put 'po-mode 'font-lock-keywords 'po-font-lock-keywords))
    948  1.1  christos 
    949  1.1  christos ;; 'hilit19' based highlighting code has been disabled, as most probably
    950  1.1  christos ;; nobody really needs it (it also generates ugly byte-compiler warnings).
    951  1.1  christos ;;
    952  1.1  christos ;;(if (fboundp 'hilit-set-mode-patterns)
    953  1.1  christos ;;    (hilit-set-mode-patterns 'po-mode
    954  1.1  christos ;;			     '(("^# .*\\|^#$" nil comment)
    955  1.1  christos ;;			       ("^#[.,:].*" nil include)
    956  1.1  christos ;;			       ("^\\(msgid\\|msgstr\\) *\"" nil keyword)
    957  1.1  christos ;;			       ("^\"\\|\"$" nil keyword))))
    958  1.1  christos 
    959  1.1  christos ;;; Mode activation.
    961  1.1  christos 
    962  1.1  christos ;; Emacs 21.2 comes with po-find-file-coding-system. We give preference
    963  1.1  christos ;; to the version shipped with Emacs.
    964  1.1  christos (if (not (fboundp 'po-find-file-coding-system))
    965  1.1  christos   (require 'po-compat))
    966  1.1  christos 
    967  1.1  christos (defvar po-mode-abbrev-table nil
    968  1.1  christos   "Abbrev table used while in PO mode.")
    969  1.1  christos (define-abbrev-table 'po-mode-abbrev-table ())
    970  1.1  christos 
    971  1.1  christos (defvar po-mode-map
    972  1.1  christos   ;; Use (make-keymap) because (make-sparse-keymap) does not work on Demacs.
    973  1.1  christos   (let ((po-mode-map (make-keymap)))
    974  1.1  christos     (suppress-keymap po-mode-map)
    975  1.1  christos     (define-key po-mode-map "\C-i" 'po-unfuzzy)
    976  1.1  christos     (define-key po-mode-map "\C-j" 'po-msgid-to-msgstr)
    977  1.1  christos     (define-key po-mode-map "\C-m" 'po-edit-msgstr)
    978  1.1  christos     (define-key po-mode-map " " 'po-auto-select-entry)
    979  1.1  christos     (define-key po-mode-map "?" 'po-help)
    980  1.1  christos     (define-key po-mode-map "#" 'po-edit-comment)
    981  1.1  christos     (define-key po-mode-map "," 'po-tags-search)
    982  1.1  christos     (define-key po-mode-map "." 'po-current-entry)
    983  1.1  christos     (define-key po-mode-map "<" 'po-first-entry)
    984  1.1  christos     (define-key po-mode-map "=" 'po-statistics)
    985  1.1  christos     (define-key po-mode-map ">" 'po-last-entry)
    986  1.1  christos     (define-key po-mode-map "a" 'po-cycle-auxiliary)
    987  1.1  christos ;;;;  (define-key po-mode-map "c" 'po-save-entry)
    988  1.1  christos     (define-key po-mode-map "f" 'po-next-fuzzy-entry)
    989  1.1  christos     (define-key po-mode-map "h" 'po-help)
    990  1.1  christos     (define-key po-mode-map "k" 'po-kill-msgstr)
    991  1.1  christos ;;;;  (define-key po-mode-map "l" 'po-lookup-lexicons)
    992  1.1  christos     (define-key po-mode-map "m" 'po-push-location)
    993  1.1  christos     (define-key po-mode-map "n" 'po-next-entry)
    994  1.1  christos     (define-key po-mode-map "o" 'po-next-obsolete-entry)
    995  1.1  christos     (define-key po-mode-map "p" 'po-previous-entry)
    996  1.1  christos     (define-key po-mode-map "q" 'po-confirm-and-quit)
    997  1.1  christos     (define-key po-mode-map "r" 'po-pop-location)
    998  1.1  christos     (define-key po-mode-map "s" 'po-cycle-source-reference)
    999  1.1  christos     (define-key po-mode-map "t" 'po-next-translated-entry)
   1000  1.1  christos     (define-key po-mode-map "u" 'po-next-untranslated-entry)
   1001  1.1  christos     (define-key po-mode-map "v" 'po-mode-version)
   1002  1.1  christos     (define-key po-mode-map "w" 'po-kill-ring-save-msgstr)
   1003  1.1  christos     (define-key po-mode-map "x" 'po-exchange-location)
   1004  1.1  christos     (define-key po-mode-map "y" 'po-yank-msgstr)
   1005  1.1  christos     (define-key po-mode-map "A" 'po-consider-as-auxiliary)
   1006  1.1  christos     (define-key po-mode-map "E" 'po-edit-out-full)
   1007  1.1  christos     (define-key po-mode-map "F" 'po-previous-fuzzy-entry)
   1008  1.1  christos     (define-key po-mode-map "K" 'po-kill-comment)
   1009  1.1  christos ;;;;  (define-key po-mode-map "L" 'po-consider-lexicon-file)
   1010  1.1  christos     (define-key po-mode-map "M" 'po-send-mail)
   1011  1.1  christos     (define-key po-mode-map "O" 'po-previous-obsolete-entry)
   1012  1.1  christos     (define-key po-mode-map "T" 'po-previous-translated-entry)
   1013  1.1  christos     (define-key po-mode-map "U" 'po-previous-untranslated-entry)
   1014  1.1  christos     (define-key po-mode-map "Q" 'po-quit)
   1015  1.1  christos     (define-key po-mode-map "S" 'po-consider-source-path)
   1016  1.1  christos     (define-key po-mode-map "V" 'po-validate)
   1017  1.1  christos     (define-key po-mode-map "W" 'po-kill-ring-save-comment)
   1018  1.1  christos     (define-key po-mode-map "Y" 'po-yank-comment)
   1019  1.1  christos     (define-key po-mode-map "_" 'po-undo)
   1020  1.1  christos     (define-key po-mode-map "0" 'po-other-window)
   1021  1.1  christos     (define-key po-mode-map "\177" 'po-fade-out-entry)
   1022  1.1  christos     (define-key po-mode-map "\C-c\C-a" 'po-select-auxiliary)
   1023  1.1  christos     (define-key po-mode-map "\C-c\C-e" 'po-edit-msgstr-and-ediff)
   1024  1.1  christos     (define-key po-mode-map [?\C-c?\C-#] 'po-edit-comment-and-ediff)
   1025  1.1  christos     (define-key po-mode-map "\C-c\C-C" 'po-edit-comment-and-ediff)
   1026  1.1  christos     (define-key po-mode-map "\M-," 'po-mark-translatable)
   1027  1.1  christos     (define-key po-mode-map "\M-." 'po-select-mark-and-mark)
   1028  1.1  christos ;;;;  (define-key po-mode-map "\M-c" 'po-select-and-save-entry)
   1029  1.1  christos ;;;;  (define-key po-mode-map "\M-l" 'po-edit-lexicon-entry)
   1030  1.1  christos     (define-key po-mode-map "\M-s" 'po-select-source-reference)
   1031  1.1  christos     (define-key po-mode-map "\M-A" 'po-ignore-as-auxiliary)
   1032  1.1  christos ;;;;  (define-key po-mode-map "\M-L" 'po-ignore-lexicon-file)
   1033  1.1  christos     (define-key po-mode-map "\M-S" 'po-ignore-source-path)
   1034  1.1  christos     po-mode-map)
   1035  1.1  christos   "Keymap for PO mode.")
   1036  1.1  christos 
   1037  1.1  christos (defun po-mode ()
   1038  1.1  christos   "Major mode for translators when they edit PO files.
   1039  1.1  christos 
   1040  1.1  christos Special commands:
   1041  1.1  christos \\{po-mode-map}
   1042  1.1  christos Turning on PO mode calls the value of the variable 'po-mode-hook',
   1043  1.1  christos if that value is non-nil.  Behaviour may be adjusted through some variables,
   1044  1.1  christos all reachable through 'M-x customize', in group 'Emacs.Editing.I18n.Po'."
   1045  1.1  christos   (interactive)
   1046  1.1  christos   (kill-all-local-variables)
   1047  1.1  christos   (setq major-mode 'po-mode
   1048  1.1  christos 	mode-name "PO")
   1049  1.1  christos   (use-local-map po-mode-map)
   1050  1.1  christos   (if (fboundp 'easy-menu-define)
   1051  1.1  christos       (progn
   1052  1.1  christos 	(easy-menu-define po-mode-menu po-mode-map "" po-mode-menu-layout)
   1053  1.1  christos 	(and po-XEMACS (easy-menu-add po-mode-menu))))
   1054  1.1  christos   (set (make-local-variable 'font-lock-defaults) '(po-font-lock-keywords t))
   1055  1.1  christos 
   1056  1.1  christos   (set (make-local-variable 'po-read-only) buffer-read-only)
   1057  1.1  christos   (setq buffer-read-only t)
   1058  1.1  christos 
   1059  1.1  christos   (make-local-variable 'po-start-of-entry)
   1060  1.1  christos   (make-local-variable 'po-start-of-msgid)
   1061  1.1  christos   (make-local-variable 'po-start-of-msgstr)
   1062  1.1  christos   (make-local-variable 'po-end-of-entry)
   1063  1.1  christos   (make-local-variable 'po-entry-type)
   1064  1.1  christos 
   1065  1.1  christos   (make-local-variable 'po-translated-counter)
   1066  1.1  christos   (make-local-variable 'po-fuzzy-counter)
   1067  1.1  christos   (make-local-variable 'po-untranslated-counter)
   1068  1.1  christos   (make-local-variable 'po-obsolete-counter)
   1069  1.1  christos   (make-local-variable 'po-mode-line-string)
   1070  1.1  christos 
   1071  1.1  christos   (setq po-mode-flag t)
   1072  1.1  christos 
   1073  1.1  christos   (po-check-file-header)
   1074  1.1  christos   (po-compute-counters nil)
   1075  1.1  christos 
   1076  1.1  christos   (set (make-local-variable 'po-edited-fields) nil)
   1077  1.1  christos   (set (make-local-variable 'po-marker-stack) nil)
   1078  1.1  christos   (set (make-local-variable 'po-search-path) '(("./") ("../")))
   1079  1.1  christos 
   1080  1.1  christos   (set (make-local-variable 'po-reference-alist) nil)
   1081  1.1  christos   (set (make-local-variable 'po-reference-cursor) nil)
   1082  1.1  christos   (set (make-local-variable 'po-reference-check) 0)
   1083  1.1  christos 
   1084  1.1  christos   (set (make-local-variable 'po-keywords)
   1085  1.1  christos        '(("gettext") ("gettext_noop") ("_") ("N_")))
   1086  1.1  christos   (set (make-local-variable 'po-string-contents) nil)
   1087  1.1  christos   (set (make-local-variable 'po-string-buffer) nil)
   1088  1.1  christos   (set (make-local-variable 'po-string-start) nil)
   1089  1.1  christos   (set (make-local-variable 'po-string-end) nil)
   1090  1.1  christos   (set (make-local-variable 'po-marking-overlay) (po-create-overlay))
   1091  1.1  christos 
   1092  1.1  christos   (add-hook 'write-contents-hooks 'po-replace-revision-date)
   1093  1.1  christos 
   1094  1.1  christos   (run-hooks 'po-mode-hook)
   1095  1.1  christos   (message (_"You may type 'h' or '?' for a short PO mode reminder.")))
   1096  1.1  christos 
   1097  1.1  christos (defvar po-subedit-mode-map
   1098  1.1  christos   ;; Use (make-keymap) because (make-sparse-keymap) does not work on Demacs.
   1099  1.1  christos   (let ((po-subedit-mode-map (make-keymap)))
   1100  1.1  christos     (define-key po-subedit-mode-map "\C-c\C-a" 'po-subedit-cycle-auxiliary)
   1101  1.1  christos     (define-key po-subedit-mode-map "\C-c\C-c" 'po-subedit-exit)
   1102  1.1  christos     (define-key po-subedit-mode-map "\C-c\C-e" 'po-subedit-ediff)
   1103  1.1  christos     (define-key po-subedit-mode-map "\C-c\C-k" 'po-subedit-abort)
   1104  1.1  christos     po-subedit-mode-map)
   1105  1.1  christos   "Keymap while editing a PO mode entry (or the full PO file).")
   1106  1.1  christos 
   1107  1.1  christos ;;; Window management.
   1109  1.1  christos 
   1110  1.1  christos (make-variable-buffer-local 'po-mode-flag)
   1111  1.1  christos 
   1112  1.1  christos (defvar po-mode-line-entry '(po-mode-flag ("  " po-mode-line-string))
   1113  1.1  christos   "Mode line format entry displaying MODE-LINE-STRING.")
   1114  1.1  christos 
   1115  1.1  christos ;; Insert MODE-LINE-ENTRY in mode line, but on first load only.
   1116  1.1  christos (or (member po-mode-line-entry mode-line-format)
   1117  1.1  christos     ;; mode-line-format usually contains global-mode-string, but some
   1118  1.1  christos     ;; people customize this variable. As a last resort, append at the end.
   1119  1.1  christos     (let ((prev-entry (or (member 'global-mode-string mode-line-format)
   1120  1.1  christos                           (member "   " mode-line-format)
   1121  1.1  christos                           (last mode-line-format))))
   1122  1.1  christos       (setcdr prev-entry (cons po-mode-line-entry (cdr prev-entry)))))
   1123  1.1  christos 
   1124  1.1  christos (defun po-update-mode-line-string ()
   1125  1.1  christos   "Compute a new statistics string to display in mode line."
   1126  1.1  christos   (setq po-mode-line-string
   1127  1.1  christos 	(concat (format "%dt" po-translated-counter)
   1128  1.1  christos 		(if (> po-fuzzy-counter 0)
   1129  1.1  christos 		    (format "+%df" po-fuzzy-counter))
   1130  1.1  christos 		(if (> po-untranslated-counter 0)
   1131  1.1  christos 		    (format "+%du" po-untranslated-counter))
   1132  1.1  christos 		(if (> po-obsolete-counter 0)
   1133  1.1  christos 		    (format "+%do" po-obsolete-counter))))
   1134  1.1  christos   (po-force-mode-line-update))
   1135  1.1  christos 
   1136  1.1  christos (defun po-type-counter ()
   1137  1.1  christos   "Return the symbol name of the counter appropriate for the current entry."
   1138  1.1  christos   (cond ((eq po-entry-type 'obsolete) 'po-obsolete-counter)
   1139  1.1  christos 	((eq po-entry-type 'fuzzy) 'po-fuzzy-counter)
   1140  1.1  christos 	((eq po-entry-type 'translated) 'po-translated-counter)
   1141  1.1  christos 	((eq po-entry-type 'untranslated) 'po-untranslated-counter)
   1142  1.1  christos 	(t (error (_"Unknown entry type")))))
   1143  1.1  christos 
   1144  1.1  christos (defun po-decrease-type-counter ()
   1145  1.1  christos   "Decrease the counter corresponding to the nature of the current entry."
   1146  1.1  christos   (let ((counter (po-type-counter)))
   1147  1.1  christos     (set counter (1- (eval counter)))))
   1148  1.1  christos 
   1149  1.1  christos (defun po-increase-type-counter ()
   1150  1.1  christos   "Increase the counter corresponding to the nature of the current entry.
   1151  1.1  christos Then, update the mode line counters."
   1152  1.1  christos   (let ((counter (po-type-counter)))
   1153  1.1  christos     (set counter (1+ (eval counter))))
   1154  1.1  christos   (po-update-mode-line-string))
   1155  1.1  christos 
   1156  1.1  christos ;; Avoid byte compiler warnings.
   1157  1.1  christos (defvar po-fuzzy-regexp)
   1158  1.1  christos (defvar po-untranslated-regexp)
   1159  1.1  christos 
   1160  1.1  christos (defun po-compute-counters (flag)
   1161  1.1  christos   "Prepare counters for mode line display.  If FLAG, also echo entry position."
   1162  1.1  christos   (and flag (po-find-span-of-entry))
   1163  1.1  christos   (setq po-translated-counter 0
   1164  1.1  christos 	po-fuzzy-counter 0
   1165  1.1  christos 	po-untranslated-counter 0
   1166  1.1  christos 	po-obsolete-counter 0)
   1167  1.1  christos   (let ((position 0) (total 0) current here)
   1168  1.1  christos     ;; FIXME 'here' looks obsolete / 2001-08-23 03:54:26 CEST -ke-
   1169  1.1  christos     (save-excursion
   1170  1.1  christos       (po-find-span-of-entry)
   1171  1.1  christos       (setq current po-start-of-msgstr)
   1172  1.1  christos       (goto-char (point-min))
   1173  1.1  christos       ;; While counting, skip the header entry, for consistency with msgfmt.
   1174  1.1  christos       (po-find-span-of-entry)
   1175  1.1  christos       (if (string-equal (po-get-msgid nil) "")
   1176  1.1  christos 	  (goto-char po-end-of-entry))
   1177  1.1  christos       (if (re-search-forward "^msgid" (point-max) t)
   1178  1.1  christos 	  (progn
   1179  1.1  christos 	    ;; Start counting
   1180  1.1  christos 	    (while (re-search-forward po-any-msgstr-regexp nil t)
   1181  1.1  christos 	      (and (= (% total 20) 0)
   1182  1.1  christos 		   (if flag
   1183  1.1  christos 		       (message (_"Position %d/%d") position total)
   1184  1.1  christos 		     (message (_"Position %d") total)))
   1185  1.1  christos 	      (setq here (point))
   1186  1.1  christos 	      (goto-char (match-beginning 0))
   1187  1.1  christos 	      (setq total (1+ total))
   1188  1.1  christos 	      (and flag (eq (point) current) (setq position total))
   1189  1.1  christos 	      (cond ((eq (following-char) ?#)
   1190  1.1  christos 		     (setq po-obsolete-counter (1+ po-obsolete-counter)))
   1191  1.1  christos 		    ((looking-at po-untranslated-regexp)
   1192  1.1  christos 		     (setq po-untranslated-counter (1+ po-untranslated-counter)))
   1193  1.1  christos 		    (t (setq po-translated-counter (1+ po-translated-counter))))
   1194  1.1  christos 	      (goto-char here))
   1195  1.1  christos 
   1196  1.1  christos 	    ;; Make another pass just for the fuzzy entries, kind of kludgey.
   1197  1.1  christos 	    ;; FIXME: Counts will be wrong if untranslated entries are fuzzy, yet
   1198  1.1  christos 	    ;; this should not normally happen.
   1199  1.1  christos 	    (goto-char (point-min))
   1200  1.1  christos 	    (while (re-search-forward po-fuzzy-regexp nil t)
   1201  1.1  christos 	      (setq po-fuzzy-counter (1+ po-fuzzy-counter)))
   1202  1.1  christos 	    (setq po-translated-counter (- po-translated-counter po-fuzzy-counter)))
   1203  1.1  christos 	'()))
   1204  1.1  christos 
   1205  1.1  christos     ;; Push the results out.
   1206  1.1  christos     (if flag
   1207  1.1  christos 	(message (_"\
   1208  1.1  christos Position %d/%d; %d translated, %d fuzzy, %d untranslated, %d obsolete")
   1209  1.1  christos 		 position total po-translated-counter po-fuzzy-counter
   1210  1.1  christos 		 po-untranslated-counter po-obsolete-counter)
   1211  1.1  christos       (message "")))
   1212  1.1  christos   (po-update-mode-line-string))
   1213  1.1  christos 
   1214  1.1  christos (defun po-redisplay ()
   1215  1.1  christos   "Redisplay the current entry."
   1216  1.1  christos   ;; FIXME: Should try to fit the whole entry on the window.  If this is not
   1217  1.1  christos   ;; possible, should try to fit the comment and the msgid.  Otherwise,
   1218  1.1  christos   ;; should try to fit the msgid.  Else, the first line of the msgid should
   1219  1.1  christos   ;; be at the top of the window.
   1220  1.1  christos   (goto-char po-start-of-msgid))
   1221  1.1  christos 
   1222  1.1  christos (defun po-other-window ()
   1223  1.1  christos   "Get the cursor into another window, out of PO mode."
   1224  1.1  christos   (interactive)
   1225  1.1  christos   (if (one-window-p t)
   1226  1.1  christos       (progn
   1227  1.1  christos 	(split-window)
   1228  1.1  christos 	(switch-to-buffer (other-buffer)))
   1229  1.1  christos     (other-window 1)))
   1230  1.1  christos 
   1231  1.1  christos ;;; Processing the PO file header entry.
   1233  1.1  christos 
   1234  1.1  christos (defun po-check-file-header ()
   1235  1.1  christos   "Create a missing PO mode file header, or replace an oldish one."
   1236  1.1  christos   (save-excursion
   1237  1.1  christos     (let ((buffer-read-only po-read-only)
   1238  1.1  christos 	  insert-flag end-of-header)
   1239  1.1  christos       (goto-char (point-min))
   1240  1.1  christos       (if (re-search-forward po-any-msgstr-regexp nil t)
   1241  1.1  christos 	  (progn
   1242  1.1  christos 	    ;; There is at least one entry.
   1243  1.1  christos 	    (goto-char (match-beginning 0))
   1244  1.1  christos 	    (previous-line 1)
   1245  1.1  christos 	    (setq end-of-header (match-end 0))
   1246  1.1  christos 	    (if (looking-at "msgid \"\"\n")
   1247  1.1  christos 		;; There is indeed a PO file header.
   1248  1.1  christos 		(if (re-search-forward "\n\"PO-Revision-Date: "
   1249  1.1  christos 				       end-of-header t)
   1250  1.1  christos 		    nil
   1251  1.1  christos 		  ;; This is an oldish header.  Replace it all.
   1252  1.1  christos 		  (goto-char end-of-header)
   1253  1.1  christos 		  (while (> (point) (point-min))
   1254  1.1  christos 		    (previous-line 1)
   1255  1.1  christos 		    (insert "#~ ")
   1256  1.1  christos 		    (beginning-of-line))
   1257  1.1  christos 		  (beginning-of-line)
   1258  1.1  christos 		  (setq insert-flag t))
   1259  1.1  christos 	      ;; The first entry is not a PO file header, insert one.
   1260  1.1  christos 	      (setq insert-flag t)))
   1261  1.1  christos 	;; Not a single entry found.
   1262  1.1  christos 	(setq insert-flag t))
   1263  1.1  christos       (goto-char (point-min))
   1264  1.1  christos       (if insert-flag
   1265  1.1  christos 	  (progn
   1266  1.1  christos 	    (insert po-default-file-header)
   1267  1.1  christos 	    (if (not (eobp))
   1268  1.1  christos 		(insert "\n")))))))
   1269  1.1  christos 
   1270  1.1  christos (defun po-replace-revision-date ()
   1271  1.1  christos   "Replace the revision date by current time in the PO file header."
   1272  1.1  christos   (if (fboundp 'format-time-string)
   1273  1.1  christos       (if (or (eq po-auto-replace-revision-date t)
   1274  1.1  christos 	      (and (eq po-auto-replace-revision-date 'ask)
   1275  1.1  christos 		   (y-or-n-p (_"May I set PO-Revision-Date? "))))
   1276  1.1  christos 	  (save-excursion
   1277  1.1  christos 	    (goto-char (point-min))
   1278  1.1  christos 	    (if (re-search-forward "^\"PO-Revision-Date:.*" nil t)
   1279  1.1  christos 		(let* ((buffer-read-only po-read-only)
   1280  1.1  christos 		       (time (current-time))
   1281  1.1  christos 		       (seconds (or (car (current-time-zone time)) 0))
   1282  1.1  christos 		       (minutes (/ (abs seconds) 60))
   1283  1.1  christos 		       (zone (format "%c%02d%02d"
   1284  1.1  christos 				     (if (< seconds 0) ?- ?+)
   1285  1.1  christos 				     (/ minutes 60)
   1286  1.1  christos 				     (% minutes 60))))
   1287  1.1  christos 		  (replace-match
   1288  1.1  christos 		       (concat "\"PO-Revision-Date: "
   1289  1.1  christos 			       (format-time-string "%Y-%m-%d %H:%M" time)
   1290  1.1  christos 			       zone "\\n\"")
   1291  1.1  christos 		       t t))))
   1292  1.1  christos 	(message ""))
   1293  1.1  christos     (message (_"PO-Revision-Date should be adjusted..."))))
   1294  1.1  christos 
   1295  1.1  christos ;;; Handling span of entry, entry type and entry attributes.
   1297  1.1  christos 
   1298  1.1  christos (defun po-find-span-of-entry ()
   1299  1.1  christos   "Find the extent of the PO file entry where the cursor is.
   1300  1.1  christos Set variables PO-START-OF-ENTRY, PO-START-OF-MSGID, PO-START-OF-MSGSTR,
   1301  1.1  christos PO-END-OF-ENTRY and PO-ENTRY-TYPE to meaningful values.  Decreasing priority
   1302  1.1  christos of type interpretation is: obsolete, fuzzy, untranslated or translated."
   1303  1.1  christos   (let ((here (point)))
   1304  1.1  christos     (if (re-search-backward po-any-msgstr-regexp nil t)
   1305  1.1  christos 	(progn
   1306  1.1  christos 	  ;; After a backward match, (match-end 0) will not extend
   1307  1.1  christos 	  ;; beyond point, in case point was *inside* the regexp.  We
   1308  1.1  christos 	  ;; need a dependable (match-end 0), so we redo the match in
   1309  1.1  christos 	  ;; the forward direction.
   1310  1.1  christos 	  (re-search-forward po-any-msgstr-regexp)
   1311  1.1  christos 	  (if (<= (match-end 0) here)
   1312  1.1  christos 	      (progn
   1313  1.1  christos 		;; We most probably found the msgstr of the previous
   1314  1.1  christos 		;; entry.  The current entry then starts just after
   1315  1.1  christos 		;; its end, save this information just in case.
   1316  1.1  christos 		(setq po-start-of-entry (match-end 0))
   1317  1.1  christos 		;; However, it is also possible that we are located in
   1318  1.1  christos 		;; the crumb after the last entry in the file.  If
   1319  1.1  christos 		;; yes, we know the middle and end of last PO entry.
   1320  1.1  christos 		(setq po-start-of-msgstr (match-beginning 0)
   1321  1.1  christos 		      po-end-of-entry (match-end 0))
   1322  1.1  christos 		(if (re-search-forward po-any-msgstr-regexp nil t)
   1323  1.1  christos 		    (progn
   1324  1.1  christos 		      ;; We definitely were not in the crumb.
   1325  1.1  christos 		      (setq po-start-of-msgstr (match-beginning 0)
   1326  1.1  christos 			    po-end-of-entry (match-end 0)))
   1327  1.1  christos 		  ;; We were in the crumb.  The start of the last PO
   1328  1.1  christos 		  ;; file entry is the end of the previous msgstr if
   1329  1.1  christos 		  ;; any, or else, the beginning of the file.
   1330  1.1  christos 		  (goto-char po-start-of-msgstr)
   1331  1.1  christos 		  (setq po-start-of-entry
   1332  1.1  christos 			(if (re-search-backward po-any-msgstr-regexp nil t)
   1333  1.1  christos 			    (match-end 0)
   1334  1.1  christos 			  (point-min)))))
   1335  1.1  christos 	    ;; The cursor was inside msgstr of the current entry.
   1336  1.1  christos 	    (setq po-start-of-msgstr (match-beginning 0)
   1337  1.1  christos 		  po-end-of-entry (match-end 0))
   1338  1.1  christos 	    ;; The start of this entry is the end of the previous
   1339  1.1  christos 	    ;; msgstr if any, or else, the beginning of the file.
   1340  1.1  christos 	    (goto-char po-start-of-msgstr)
   1341  1.1  christos 	    (setq po-start-of-entry
   1342  1.1  christos 		  (if (re-search-backward po-any-msgstr-regexp nil t)
   1343  1.1  christos 		      (match-end 0)
   1344  1.1  christos 		    (point-min)))))
   1345  1.1  christos       ;; The cursor was before msgstr in the first entry in the file.
   1346  1.1  christos       (setq po-start-of-entry (point-min))
   1347  1.1  christos       (goto-char po-start-of-entry)
   1348  1.1  christos       ;; There is at least the PO file header, so this should match.
   1349  1.1  christos       (re-search-forward po-any-msgstr-regexp)
   1350  1.1  christos       (setq po-start-of-msgstr (match-beginning 0)
   1351  1.1  christos 	    po-end-of-entry (match-end 0)))
   1352  1.1  christos     ;; Find start of msgid.
   1353  1.1  christos     (goto-char po-start-of-entry)
   1354  1.1  christos     (re-search-forward po-any-msgid-regexp)
   1355  1.1  christos     (setq po-start-of-msgid (match-beginning 0))
   1356  1.1  christos     ;; Classify the entry.
   1357  1.1  christos     (setq po-entry-type
   1358  1.1  christos 	  (if (eq (following-char) ?#)
   1359  1.1  christos 	      'obsolete
   1360  1.1  christos 	    (goto-char po-start-of-entry)
   1361  1.1  christos 	    (if (re-search-forward po-fuzzy-regexp po-start-of-msgid t)
   1362  1.1  christos 		'fuzzy
   1363  1.1  christos 	      (goto-char po-start-of-msgstr)
   1364  1.1  christos 	      (if (looking-at po-untranslated-regexp)
   1365  1.1  christos 		  'untranslated
   1366  1.1  christos 		'translated))))
   1367  1.1  christos     ;; Put the cursor back where it was.
   1368  1.1  christos     (goto-char here)))
   1369  1.1  christos 
   1370  1.1  christos (defun po-add-attribute (name)
   1371  1.1  christos   "Add attribute NAME to the current entry, unless it is already there."
   1372  1.1  christos   (save-excursion
   1373  1.1  christos     (let ((buffer-read-only po-read-only))
   1374  1.1  christos       (goto-char po-start-of-entry)
   1375  1.1  christos       (if (re-search-forward "\n#[,!] .*" po-start-of-msgid t)
   1376  1.1  christos 	  (save-restriction
   1377  1.1  christos 	    (narrow-to-region (match-beginning 0) (match-end 0))
   1378  1.1  christos 	    (goto-char (point-min))
   1379  1.1  christos 	    (if (re-search-forward (concat "\\b" name "\\b") nil t)
   1380  1.1  christos 		nil
   1381  1.1  christos 	      (goto-char (point-max))
   1382  1.1  christos 	      (insert ", " name)))
   1383  1.1  christos 	(skip-chars-forward "\n")
   1384  1.1  christos 	(while (eq (following-char) ?#)
   1385  1.1  christos 	  (next-line 1))
   1386  1.1  christos 	(insert "#, " name "\n")))))
   1387  1.1  christos 
   1388  1.1  christos (defun po-delete-attribute (name)
   1389  1.1  christos   "Delete attribute NAME from the current entry, if any."
   1390  1.1  christos   (save-excursion
   1391  1.1  christos     (let ((buffer-read-only po-read-only))
   1392  1.1  christos       (goto-char po-start-of-entry)
   1393  1.1  christos       (if (re-search-forward "\n#[,!] .*" po-start-of-msgid t)
   1394  1.1  christos 	  (save-restriction
   1395  1.1  christos 	    (narrow-to-region (match-beginning 0) (match-end 0))
   1396  1.1  christos 	    (goto-char (point-min))
   1397  1.1  christos 	    (if (re-search-forward
   1398  1.1  christos 		 (concat "\\(\n#[,!] " name "$\\|, " name "$\\| " name ",\\)")
   1399  1.1  christos 		 nil t)
   1400  1.1  christos 		(replace-match "" t t)))))))
   1401  1.1  christos 
   1402  1.1  christos ;;; Entry positionning.
   1404  1.1  christos 
   1405  1.1  christos (defun po-say-location-depth ()
   1406  1.1  christos   "Tell how many entries in the entry location stack."
   1407  1.1  christos   (let ((depth (length po-marker-stack)))
   1408  1.1  christos     (cond ((= depth 0) (message (_"Empty location stack")))
   1409  1.1  christos 	  ((= depth 1) (message (_"One entry in location stack")))
   1410  1.1  christos 	  (t (message (_"%d entries in location stack") depth)))))
   1411  1.1  christos 
   1412  1.1  christos (defun po-push-location ()
   1413  1.1  christos   "Stack the location of the current entry, for later return."
   1414  1.1  christos   (interactive)
   1415  1.1  christos   (po-find-span-of-entry)
   1416  1.1  christos   (save-excursion
   1417  1.1  christos     (goto-char po-start-of-msgid)
   1418  1.1  christos     (setq po-marker-stack (cons (point-marker) po-marker-stack)))
   1419  1.1  christos   (po-say-location-depth))
   1420  1.1  christos 
   1421  1.1  christos (defun po-pop-location ()
   1422  1.1  christos   "Unstack a saved location, and return to the corresponding entry."
   1423  1.1  christos   (interactive)
   1424  1.1  christos   (if po-marker-stack
   1425  1.1  christos       (progn
   1426  1.1  christos 	(goto-char (car po-marker-stack))
   1427  1.1  christos 	(setq po-marker-stack (cdr po-marker-stack))
   1428  1.1  christos 	(po-current-entry)
   1429  1.1  christos 	(po-say-location-depth))
   1430  1.1  christos     (error (_"The entry location stack is empty"))))
   1431  1.1  christos 
   1432  1.1  christos (defun po-exchange-location ()
   1433  1.1  christos   "Exchange the location of the current entry with the top of stack."
   1434  1.1  christos   (interactive)
   1435  1.1  christos   (if po-marker-stack
   1436  1.1  christos       (progn
   1437  1.1  christos 	(po-find-span-of-entry)
   1438  1.1  christos 	(goto-char po-start-of-msgid)
   1439  1.1  christos 	(let ((location (point-marker)))
   1440  1.1  christos 	  (goto-char (car po-marker-stack))
   1441  1.1  christos 	  (setq po-marker-stack (cons location (cdr po-marker-stack))))
   1442  1.1  christos 	(po-current-entry)
   1443  1.1  christos 	(po-say-location-depth))
   1444  1.1  christos     (error (_"The entry location stack is empty"))))
   1445  1.1  christos 
   1446  1.1  christos (defun po-current-entry ()
   1447  1.1  christos   "Display the current entry."
   1448  1.1  christos   (interactive)
   1449  1.1  christos   (po-find-span-of-entry)
   1450  1.1  christos   (po-redisplay))
   1451  1.1  christos 
   1452  1.1  christos (defun po-first-entry-with-regexp (regexp)
   1453  1.1  christos   "Display the first entry in the file which msgstr matches REGEXP."
   1454  1.1  christos   (let ((here (point)))
   1455  1.1  christos     (goto-char (point-min))
   1456  1.1  christos     (if (re-search-forward regexp nil t)
   1457  1.1  christos 	(progn
   1458  1.1  christos 	  (goto-char (match-beginning 0))
   1459  1.1  christos 	  (po-current-entry))
   1460  1.1  christos       (goto-char here)
   1461  1.1  christos       (error (_"There is no such entry")))))
   1462  1.1  christos 
   1463  1.1  christos (defun po-last-entry-with-regexp (regexp)
   1464  1.1  christos   "Display the last entry in the file which msgstr matches REGEXP."
   1465  1.1  christos   (let ((here (point)))
   1466  1.1  christos     (goto-char (point-max))
   1467  1.1  christos     (if (re-search-backward regexp nil t)
   1468  1.1  christos 	(po-current-entry)
   1469  1.1  christos       (goto-char here)
   1470  1.1  christos       (error (_"There is no such entry")))))
   1471  1.1  christos 
   1472  1.1  christos (defun po-next-entry-with-regexp (regexp wrap)
   1473  1.1  christos   "Display the entry following the current entry which msgstr matches REGEXP.
   1474  1.1  christos If WRAP is not nil, the search may wrap around the buffer."
   1475  1.1  christos   (po-find-span-of-entry)
   1476  1.1  christos   (let ((here (point)))
   1477  1.1  christos     (goto-char po-end-of-entry)
   1478  1.1  christos     (if (re-search-forward regexp nil t)
   1479  1.1  christos 	(progn
   1480  1.1  christos 	  (goto-char (match-beginning 0))
   1481  1.1  christos 	  (po-current-entry))
   1482  1.1  christos       (if (and wrap
   1483  1.1  christos 	       (progn
   1484  1.1  christos 		 (goto-char (point-min))
   1485  1.1  christos 		 (re-search-forward regexp po-start-of-entry t)))
   1486  1.1  christos 	  (progn
   1487  1.1  christos 	    (goto-char (match-beginning 0))
   1488  1.1  christos 	    (po-current-entry)
   1489  1.1  christos 	    (message (_"Wrapping around the buffer")))
   1490  1.1  christos 	(goto-char here)
   1491  1.1  christos 	(error (_"There is no such entry"))))))
   1492  1.1  christos 
   1493  1.1  christos (defun po-previous-entry-with-regexp (regexp wrap)
   1494  1.1  christos   "Redisplay the entry preceding the current entry which msgstr matches REGEXP.
   1495  1.1  christos If WRAP is not nil, the search may wrap around the buffer."
   1496  1.1  christos   (po-find-span-of-entry)
   1497  1.1  christos   (let ((here (point)))
   1498  1.1  christos     (goto-char po-start-of-entry)
   1499  1.1  christos     (if (re-search-backward regexp nil t)
   1500  1.1  christos 	(po-current-entry)
   1501  1.1  christos       (if (and wrap
   1502  1.1  christos 	       (progn
   1503  1.1  christos 		 (goto-char (point-max))
   1504  1.1  christos 		 (re-search-backward regexp po-end-of-entry t)))
   1505  1.1  christos 	  (progn
   1506  1.1  christos 	    (po-current-entry)
   1507  1.1  christos 	    (message (_"Wrapping around the buffer")))
   1508  1.1  christos 	(goto-char here)
   1509  1.1  christos 	(error (_"There is no such entry"))))))
   1510  1.1  christos 
   1511  1.1  christos ;; Any entries.
   1512  1.1  christos 
   1513  1.1  christos (defun po-first-entry ()
   1514  1.1  christos   "Display the first entry."
   1515  1.1  christos   (interactive)
   1516  1.1  christos   (po-first-entry-with-regexp po-any-msgstr-regexp))
   1517  1.1  christos 
   1518  1.1  christos (defun po-last-entry ()
   1519  1.1  christos   "Display the last entry."
   1520  1.1  christos   (interactive)
   1521  1.1  christos   (po-last-entry-with-regexp po-any-msgstr-regexp))
   1522  1.1  christos 
   1523  1.1  christos (defun po-next-entry ()
   1524  1.1  christos   "Display the entry following the current entry."
   1525  1.1  christos   (interactive)
   1526  1.1  christos   (po-next-entry-with-regexp po-any-msgstr-regexp nil))
   1527  1.1  christos 
   1528  1.1  christos (defun po-previous-entry ()
   1529  1.1  christos   "Display the entry preceding the current entry."
   1530  1.1  christos   (interactive)
   1531  1.1  christos   (po-previous-entry-with-regexp po-any-msgstr-regexp nil))
   1532  1.1  christos 
   1533  1.1  christos ;; Untranslated entries.
   1534  1.1  christos 
   1535  1.1  christos (defvar po-after-entry-regexp
   1536  1.1  christos   "\\(\\'\\|\\(#[ \t]*\\)?$\\)"
   1537  1.1  christos   "Regexp which should be true after a full msgstr string matched.")
   1538  1.1  christos 
   1539  1.1  christos (defvar po-untranslated-regexp
   1540  1.1  christos   (concat "^msgstr[ \t]*\"\"\n" po-after-entry-regexp)
   1541  1.1  christos   "Regexp matching a whole msgstr field, but only if active and empty.")
   1542  1.1  christos 
   1543  1.1  christos (defun po-next-untranslated-entry ()
   1544  1.1  christos   "Find the next untranslated entry, wrapping around if necessary."
   1545  1.1  christos   (interactive)
   1546  1.1  christos   (po-next-entry-with-regexp po-untranslated-regexp t))
   1547  1.1  christos 
   1548  1.1  christos (defun po-previous-untranslated-entry ()
   1549  1.1  christos   "Find the previous untranslated entry, wrapping around if necessary."
   1550  1.1  christos   (interactive)
   1551  1.1  christos   (po-previous-entry-with-regexp po-untranslated-regexp t))
   1552  1.1  christos 
   1553  1.1  christos (defun po-msgid-to-msgstr ()
   1554  1.1  christos   "Use another window to edit msgstr reinitialized with msgid."
   1555  1.1  christos   (interactive)
   1556  1.1  christos   (po-find-span-of-entry)
   1557  1.1  christos   (if (or (eq po-entry-type 'untranslated)
   1558  1.1  christos 	  (eq po-entry-type 'obsolete)
   1559  1.1  christos 	  (y-or-n-p (_"Really loose previous translation? ")))
   1560  1.1  christos       (po-set-msgstr (po-get-msgid nil)))
   1561  1.1  christos   (message ""))
   1562  1.1  christos 
   1563  1.1  christos ;; Obsolete entries.
   1564  1.1  christos 
   1565  1.1  christos (defvar po-obsolete-msgstr-regexp
   1566  1.1  christos   "^#~[ \t]*msgstr.*\n\\(#~[ \t]*\".*\n\\)*"
   1567  1.1  christos   "Regexp matching a whole msgstr field of an obsolete entry.")
   1568  1.1  christos 
   1569  1.1  christos (defun po-next-obsolete-entry ()
   1570  1.1  christos   "Find the next obsolete entry, wrapping around if necessary."
   1571  1.1  christos   (interactive)
   1572  1.1  christos   (po-next-entry-with-regexp po-obsolete-msgstr-regexp t))
   1573  1.1  christos 
   1574  1.1  christos (defun po-previous-obsolete-entry ()
   1575  1.1  christos   "Find the previous obsolete entry, wrapping around if necessary."
   1576  1.1  christos   (interactive)
   1577  1.1  christos   (po-previous-entry-with-regexp po-obsolete-msgstr-regexp t))
   1578  1.1  christos 
   1579  1.1  christos ;; Fuzzy entries.
   1580  1.1  christos 
   1581  1.1  christos (defvar po-fuzzy-regexp "^#[,!] .*fuzzy"
   1582  1.1  christos   "Regexp matching the string inserted by msgmerge for translations
   1583  1.1  christos which does not match exactly.")
   1584  1.1  christos 
   1585  1.1  christos (defun po-next-fuzzy-entry ()
   1586  1.1  christos   "Find the next fuzzy entry, wrapping around if necessary."
   1587  1.1  christos   (interactive)
   1588  1.1  christos   (po-next-entry-with-regexp po-fuzzy-regexp t))
   1589  1.1  christos 
   1590  1.1  christos (defun po-previous-fuzzy-entry ()
   1591  1.1  christos   "Find the next fuzzy entry, wrapping around if necessary."
   1592  1.1  christos   (interactive)
   1593  1.1  christos   (po-previous-entry-with-regexp po-fuzzy-regexp t))
   1594  1.1  christos 
   1595  1.1  christos (defun po-unfuzzy ()
   1596  1.1  christos   "Remove the fuzzy attribute for the current entry."
   1597  1.1  christos   (interactive)
   1598  1.1  christos   (po-find-span-of-entry)
   1599  1.1  christos   (cond ((eq po-entry-type 'fuzzy)
   1600  1.1  christos 	 (po-decrease-type-counter)
   1601  1.1  christos 	 (po-delete-attribute "fuzzy")
   1602  1.1  christos 	 (po-current-entry)
   1603  1.1  christos 	 (po-increase-type-counter)))
   1604  1.1  christos   (if po-auto-select-on-unfuzzy
   1605  1.1  christos       (po-auto-select-entry))
   1606  1.1  christos   (po-update-mode-line-string))
   1607  1.1  christos 
   1608  1.1  christos ;; Translated entries.
   1609  1.1  christos 
   1610  1.1  christos (defun po-next-translated-entry ()
   1611  1.1  christos   "Find the next translated entry, wrapping around if necessary."
   1612  1.1  christos   (interactive)
   1613  1.1  christos   (if (= po-translated-counter 0)
   1614  1.1  christos       (error (_"There is no such entry"))
   1615  1.1  christos     (po-next-entry-with-regexp po-any-msgstr-regexp t)
   1616  1.1  christos     (po-find-span-of-entry)
   1617  1.1  christos     (while (not (eq po-entry-type 'translated))
   1618  1.1  christos       (po-next-entry-with-regexp po-any-msgstr-regexp t)
   1619  1.1  christos       (po-find-span-of-entry))))
   1620  1.1  christos 
   1621  1.1  christos (defun po-previous-translated-entry ()
   1622  1.1  christos   "Find the previous translated entry, wrapping around if necessary."
   1623  1.1  christos   (interactive)
   1624  1.1  christos   (if (= po-translated-counter 0)
   1625  1.1  christos       (error (_"There is no such entry"))
   1626  1.1  christos     (po-previous-entry-with-regexp po-any-msgstr-regexp t)
   1627  1.1  christos     (po-find-span-of-entry)
   1628  1.1  christos     (while (not (eq po-entry-type 'translated))
   1629  1.1  christos       (po-previous-entry-with-regexp po-untranslated-regexp t)
   1630  1.1  christos       (po-find-span-of-entry))))
   1631  1.1  christos 
   1632  1.1  christos ;; Auto-selection feature.
   1633  1.1  christos 
   1634  1.1  christos (defun po-auto-select-entry ()
   1635  1.1  christos   "Select the next entry having the same type as the current one.
   1636  1.1  christos If none, wrap from the beginning of the buffer with another type,
   1637  1.1  christos going from untranslated to fuzzy, and from fuzzy to obsolete.
   1638  1.1  christos Plain translated entries are always disregarded unless there are
   1639  1.1  christos no entries of the other types."
   1640  1.1  christos   (interactive)
   1641  1.1  christos   (po-find-span-of-entry)
   1642  1.1  christos   (goto-char po-end-of-entry)
   1643  1.1  christos   (if (and (= po-untranslated-counter 0)
   1644  1.1  christos 	   (= po-fuzzy-counter 0)
   1645  1.1  christos 	   (= po-obsolete-counter 0))
   1646  1.1  christos       ;; All entries are plain translated.  Next entry will do, or
   1647  1.1  christos       ;; wrap around if there is none.
   1648  1.1  christos       (if (re-search-forward po-any-msgstr-regexp nil t)
   1649  1.1  christos 	  (goto-char (match-beginning 0))
   1650  1.1  christos 	(goto-char (point-min)))
   1651  1.1  christos     ;; If over a translated entry, look for an untranslated one first.
   1652  1.1  christos     ;; Else, look for an entry of the same type first.
   1653  1.1  christos     (let ((goal (if (eq po-entry-type 'translated)
   1654  1.1  christos 		    'untranslated
   1655  1.1  christos 		  po-entry-type)))
   1656  1.1  christos       (while goal
   1657  1.1  christos 	;; Find an untranslated entry, or wrap up for a fuzzy entry.
   1658  1.1  christos 	(if (eq goal 'untranslated)
   1659  1.1  christos 	    (if (and (> po-untranslated-counter 0)
   1660  1.1  christos 		     (re-search-forward po-untranslated-regexp nil t))
   1661  1.1  christos 		(progn
   1662  1.1  christos 		  (goto-char (match-beginning 0))
   1663  1.1  christos 		  (setq goal nil))
   1664  1.1  christos 	      (goto-char (point-min))
   1665  1.1  christos 	      (setq goal 'fuzzy)))
   1666  1.1  christos 	;; Find a fuzzy entry, or wrap up for an obsolete entry.
   1667  1.1  christos 	(if (eq goal 'fuzzy)
   1668  1.1  christos 	    (if (and (> po-fuzzy-counter 0)
   1669  1.1  christos 		     (re-search-forward po-fuzzy-regexp nil t))
   1670  1.1  christos 		(progn
   1671  1.1  christos 		  (goto-char (match-beginning 0))
   1672  1.1  christos 		  (setq goal nil))
   1673  1.1  christos 	      (goto-char (point-min))
   1674  1.1  christos 	      (setq goal 'obsolete)))
   1675  1.1  christos 	;; Find an obsolete entry, or wrap up for an untranslated entry.
   1676  1.1  christos 	(if (eq goal 'obsolete)
   1677  1.1  christos 	    (if (and (> po-obsolete-counter 0)
   1678  1.1  christos 		     (re-search-forward po-obsolete-msgstr-regexp nil t))
   1679  1.1  christos 		(progn
   1680  1.1  christos 		  (goto-char (match-beginning 0))
   1681  1.1  christos 		  (setq goal nil))
   1682  1.1  christos 	      (goto-char (point-min))
   1683  1.1  christos 	      (setq goal 'untranslated))))))
   1684  1.1  christos   ;; Display this entry nicely.
   1685  1.1  christos   (po-current-entry))
   1686  1.1  christos 
   1687  1.1  christos ;;; Killing and yanking fields.
   1689  1.1  christos 
   1690  1.1  christos (defun po-extract-unquoted (buffer start end)
   1691  1.1  christos   "Extract and return the unquoted string in BUFFER going from START to END.
   1692  1.1  christos Crumb preceding or following the quoted string is ignored."
   1693  1.1  christos   (save-excursion
   1694  1.1  christos     (goto-char start)
   1695  1.1  christos     (search-forward "\"")
   1696  1.1  christos     (setq start (point))
   1697  1.1  christos     (goto-char end)
   1698  1.1  christos     (search-backward "\"")
   1699  1.1  christos     (setq end (point)))
   1700  1.1  christos   (po-extract-part-unquoted buffer start end))
   1701  1.1  christos 
   1702  1.1  christos (defun po-extract-part-unquoted (buffer start end)
   1703  1.1  christos   "Extract and return the unquoted string in BUFFER going from START to END.
   1704  1.1  christos Surrounding quotes are already excluded by the position of START and END."
   1705  1.1  christos   (po-with-temp-buffer
   1706  1.1  christos    (insert-buffer-substring buffer start end)
   1707  1.1  christos    ;; Glue concatenated strings.
   1708  1.1  christos    (goto-char (point-min))
   1709  1.1  christos    (while (re-search-forward "\"[ \t]*\\\\?\n\\(#~\\)?[ \t]*\"" nil t)
   1710  1.1  christos      (replace-match "" t t))
   1711  1.1  christos    ;; Remove escaped newlines.
   1712  1.1  christos    (goto-char (point-min))
   1713  1.1  christos    (while (re-search-forward "\\\\[ \t]*\n" nil t)
   1714  1.1  christos      (replace-match "" t t))
   1715  1.1  christos    ;; Unquote individual characters.
   1716  1.1  christos    (goto-char (point-min))
   1717  1.1  christos    (while (re-search-forward "\\\\[\"abfnt\\0-7]" nil t)
   1718  1.1  christos      (cond ((eq (preceding-char) ?\") (replace-match "\"" t t))
   1719  1.1  christos 	   ((eq (preceding-char) ?a) (replace-match "\a" t t))
   1720  1.1  christos 	   ((eq (preceding-char) ?b) (replace-match "\b" t t))
   1721  1.1  christos 	   ((eq (preceding-char) ?f) (replace-match "\f" t t))
   1722  1.1  christos 	   ((eq (preceding-char) ?n) (replace-match "\n" t t))
   1723  1.1  christos 	   ((eq (preceding-char) ?t) (replace-match "\t" t t))
   1724  1.1  christos 	   ((eq (preceding-char) ?\\) (replace-match "\\" t t))
   1725  1.1  christos 	   (t (let ((value (- (preceding-char) ?0)))
   1726  1.1  christos 		(replace-match "" t t)
   1727  1.1  christos 		(while (looking-at "[0-7]")
   1728  1.1  christos 		  (setq value (+ (* 8 value) (- (following-char) ?0)))
   1729  1.1  christos 		  (replace-match "" t t))
   1730  1.1  christos 		(insert value)))))
   1731  1.1  christos    (buffer-string)))
   1732  1.1  christos 
   1733  1.1  christos (defun po-eval-requoted (form prefix obsolete)
   1734  1.1  christos   "Eval FORM, which inserts a string, and return the string fully requoted.
   1735  1.1  christos If PREFIX, precede the result with its contents.  If OBSOLETE, comment all
   1736  1.1  christos generated lines in the returned string.  Evaluating FORM should insert the
   1737  1.1  christos wanted string in the buffer which is current at the time of evaluation.
   1738  1.1  christos If FORM is itself a string, then this string is used for insertion."
   1739  1.1  christos   (po-with-temp-buffer
   1740  1.1  christos     (if (stringp form)
   1741  1.1  christos 	(insert form)
   1742  1.1  christos       (push-mark)
   1743  1.1  christos       (eval form))
   1744  1.1  christos     (goto-char (point-min))
   1745  1.1  christos     (let ((multi-line (re-search-forward "[^\n]\n+[^\n]" nil t)))
   1746  1.1  christos       (goto-char (point-min))
   1747  1.1  christos       (while (re-search-forward "[\"\a\b\f\n\r\t\\]" nil t)
   1748  1.1  christos 	(cond ((eq (preceding-char) ?\") (replace-match "\\\"" t t))
   1749  1.1  christos 	      ((eq (preceding-char) ?\a) (replace-match "\\a" t t))
   1750  1.1  christos 	      ((eq (preceding-char) ?\b) (replace-match "\\b" t t))
   1751  1.1  christos 	      ((eq (preceding-char) ?\f) (replace-match "\\f" t t))
   1752  1.1  christos 	      ((eq (preceding-char) ?\n)
   1753  1.1  christos 	       (replace-match (if (or (not multi-line) (eobp))
   1754  1.1  christos 				  "\\n"
   1755  1.1  christos 				"\\n\"\n\"")
   1756  1.1  christos 			      t t))
   1757  1.1  christos 	      ((eq (preceding-char) ?\r) (replace-match "\\r" t t))
   1758  1.1  christos 	      ((eq (preceding-char) ?\t) (replace-match "\\t" t t))
   1759  1.1  christos 	      ((eq (preceding-char) ?\\) (replace-match "\\\\" t t))))
   1760  1.1  christos       (goto-char (point-min))
   1761  1.1  christos       (if prefix (insert prefix " "))
   1762  1.1  christos       (insert (if multi-line "\"\"\n\"" "\""))
   1763  1.1  christos       (goto-char (point-max))
   1764  1.1  christos       (insert "\"")
   1765  1.1  christos       (if prefix (insert "\n"))
   1766  1.1  christos       (if obsolete
   1767  1.1  christos 	  (progn
   1768  1.1  christos 	    (goto-char (point-min))
   1769  1.1  christos 	    (while (not (eobp))
   1770  1.1  christos 	      (or (eq (following-char) ?\n) (insert "#~ "))
   1771  1.1  christos 	      (search-forward "\n"))))
   1772  1.1  christos       (buffer-string))))
   1773  1.1  christos 
   1774  1.1  christos (defun po-get-msgid (kill)
   1775  1.1  christos   "Extract and return the unquoted msgid string.
   1776  1.1  christos If KILL, then add the unquoted string to the kill ring."
   1777  1.1  christos   (let ((string (po-extract-unquoted (current-buffer)
   1778  1.1  christos 				     po-start-of-msgid po-start-of-msgstr)))
   1779  1.1  christos     (if kill (po-kill-new string))
   1780  1.1  christos     string))
   1781  1.1  christos 
   1782  1.1  christos (defun po-get-msgstr (kill)
   1783  1.1  christos   "Extract and return the unquoted msgstr string.
   1784  1.1  christos If KILL, then add the unquoted string to the kill ring."
   1785  1.1  christos   (let ((string (po-extract-unquoted (current-buffer)
   1786  1.1  christos 				     po-start-of-msgstr po-end-of-entry)))
   1787  1.1  christos     (if kill (po-kill-new string))
   1788  1.1  christos     string))
   1789  1.1  christos 
   1790  1.1  christos (defun po-set-msgid (form)
   1791  1.1  christos   "Replace the current msgid, using FORM to get a string.
   1792  1.1  christos Evaluating FORM should insert the wanted string in the current buffer.  If
   1793  1.1  christos FORM is itself a string, then this string is used for insertion.  The string
   1794  1.1  christos is properly requoted before the replacement occurs.
   1795  1.1  christos 
   1796  1.1  christos Returns 'nil' if the buffer has not been modified, for if the new msgid
   1797  1.1  christos described by FORM is merely identical to the msgid already in place."
   1798  1.1  christos   (let ((string (po-eval-requoted form "msgid" (eq po-entry-type 'obsolete))))
   1799  1.1  christos     (save-excursion
   1800  1.1  christos       (goto-char po-start-of-entry)
   1801  1.1  christos       (re-search-forward po-any-msgid-regexp po-start-of-msgstr)
   1802  1.1  christos       (and (not (string-equal (po-match-string 0) string))
   1803  1.1  christos 	   (let ((buffer-read-only po-read-only))
   1804  1.1  christos 	     (replace-match string t t)
   1805  1.1  christos 	     (goto-char po-start-of-msgid)
   1806  1.1  christos 	     (po-find-span-of-entry)
   1807  1.1  christos 	     t)))))
   1808  1.1  christos 
   1809  1.1  christos (defun po-set-msgstr (form)
   1810  1.1  christos   "Replace the current msgstr or msgstr[], using FORM to get a string.
   1811  1.1  christos Evaluating FORM should insert the wanted string in the current buffer.  If
   1812  1.1  christos FORM is itself a string, then this string is used for insertion.  The string
   1813  1.1  christos is properly requoted before the replacement occurs.
   1814  1.1  christos 
   1815  1.1  christos Returns 'nil' if the buffer has not been modified, for if the new msgstr
   1816  1.1  christos described by FORM is merely identical to the msgstr already in place."
   1817  1.1  christos   (let ((string (po-eval-requoted form "msgstr" (eq po-entry-type 'obsolete)))
   1818  1.1  christos         (msgstr-idx nil))
   1819  1.1  christos     (save-excursion
   1820  1.1  christos       (goto-char po-start-of-entry)
   1821  1.1  christos       (save-excursion                   ; check for an indexed msgstr
   1822  1.1  christos         (if (re-search-forward po-msgstr-idx-keyword-regexp
   1823  1.1  christos 			       po-end-of-entry t)
   1824  1.1  christos 	    (setq msgstr-idx (buffer-substring-no-properties
   1825  1.1  christos 			      (match-beginning 0) (match-end 0)))))
   1826  1.1  christos       (re-search-forward po-any-msgstr-regexp po-end-of-entry)
   1827  1.1  christos       (and (not (string-equal (po-match-string 0) string))
   1828  1.1  christos 	   (let ((buffer-read-only po-read-only))
   1829  1.1  christos 	     (po-decrease-type-counter)
   1830  1.1  christos 	     (replace-match string t t)
   1831  1.1  christos              (goto-char (match-beginning 0))
   1832  1.1  christos              (if (eq msgstr-idx nil) ; hack: replace msgstr with msgstr[d]
   1833  1.1  christos 		 nil
   1834  1.1  christos 	       (insert msgstr-idx)
   1835  1.1  christos 	       (looking-at "\\(#~[ \t]*\\)?msgstr")
   1836  1.1  christos 	       (replace-match ""))
   1837  1.1  christos 	     (goto-char po-start-of-msgid)
   1838  1.1  christos 	     (po-find-span-of-entry)
   1839  1.1  christos 	     (po-increase-type-counter)
   1840  1.1  christos 	     t)))))
   1841  1.1  christos 
   1842  1.1  christos (defun po-kill-ring-save-msgstr ()
   1843  1.1  christos   "Push the msgstr string from current entry on the kill ring."
   1844  1.1  christos   (interactive)
   1845  1.1  christos   (po-find-span-of-entry)
   1846  1.1  christos   (po-get-msgstr t))
   1847  1.1  christos 
   1848  1.1  christos (defun po-kill-msgstr ()
   1849  1.1  christos   "Empty the msgstr string from current entry, pushing it on the kill ring."
   1850  1.1  christos   (interactive)
   1851  1.1  christos   (po-kill-ring-save-msgstr)
   1852  1.1  christos   (po-set-msgstr ""))
   1853  1.1  christos 
   1854  1.1  christos (defun po-yank-msgstr ()
   1855  1.1  christos   "Replace the current msgstr string by the top of the kill ring."
   1856  1.1  christos   (interactive)
   1857  1.1  christos   (po-find-span-of-entry)
   1858  1.1  christos   (po-set-msgstr (if (eq last-command 'yank) '(yank-pop 1) '(yank)))
   1859  1.1  christos   (setq this-command 'yank))
   1860  1.1  christos 
   1861  1.1  christos (defun po-fade-out-entry ()
   1862  1.1  christos   "Mark an active entry as fuzzy; obsolete a fuzzy or untranslated entry;
   1863  1.1  christos or completely delete an obsolete entry, saving its msgstr on the kill ring."
   1864  1.1  christos   (interactive)
   1865  1.1  christos   (po-find-span-of-entry)
   1866  1.1  christos 
   1867  1.1  christos   (cond ((eq po-entry-type 'translated)
   1868  1.1  christos 	 (po-decrease-type-counter)
   1869  1.1  christos 	 (po-add-attribute "fuzzy")
   1870  1.1  christos 	 (po-current-entry)
   1871  1.1  christos 	 (po-increase-type-counter))
   1872  1.1  christos 
   1873  1.1  christos 	((or (eq po-entry-type 'fuzzy)
   1874  1.1  christos 	     (eq po-entry-type 'untranslated))
   1875  1.1  christos 	 (if (y-or-n-p (_"Should I really obsolete this entry? "))
   1876  1.1  christos 	     (progn
   1877  1.1  christos 	       (po-decrease-type-counter)
   1878  1.1  christos 	       (save-excursion
   1879  1.1  christos 		 (save-restriction
   1880  1.1  christos 		   (narrow-to-region po-start-of-entry po-end-of-entry)
   1881  1.1  christos 		   (let ((buffer-read-only po-read-only))
   1882  1.1  christos 		     (goto-char (point-min))
   1883  1.1  christos 		     (skip-chars-forward "\n")
   1884  1.1  christos 		     (while (not (eobp))
   1885  1.1  christos 		       (insert "#~ ")
   1886  1.1  christos 		       (search-forward "\n")))))
   1887  1.1  christos 	       (po-current-entry)
   1888  1.1  christos 	       (po-increase-type-counter)))
   1889  1.1  christos 	 (message ""))
   1890  1.1  christos 
   1891  1.1  christos 	((and (eq po-entry-type 'obsolete)
   1892  1.1  christos 	      (po-check-for-pending-edit po-start-of-msgid)
   1893  1.1  christos 	      (po-check-for-pending-edit po-start-of-msgstr))
   1894  1.1  christos 	 (po-decrease-type-counter)
   1895  1.1  christos 	 (po-update-mode-line-string)
   1896  1.1  christos 	 (po-get-msgstr t)
   1897  1.1  christos 	 (let ((buffer-read-only po-read-only))
   1898  1.1  christos 	   (delete-region po-start-of-entry po-end-of-entry))
   1899  1.1  christos 	 (goto-char po-start-of-entry)
   1900  1.1  christos 	 (if (re-search-forward po-any-msgstr-regexp nil t)
   1901  1.1  christos 	     (goto-char (match-beginning 0))
   1902  1.1  christos 	   (re-search-backward po-any-msgstr-regexp nil t))
   1903  1.1  christos 	 (po-current-entry)
   1904  1.1  christos 	 (message ""))))
   1905  1.1  christos 
   1906  1.1  christos ;;; Killing and yanking comments.
   1908  1.1  christos 
   1909  1.1  christos (defvar po-active-comment-regexp
   1910  1.1  christos   "^\\(#\n\\|# .*\n\\)+"
   1911  1.1  christos   "Regexp matching the whole editable comment part of an active entry.")
   1912  1.1  christos 
   1913  1.1  christos (defvar po-obsolete-comment-regexp
   1914  1.1  christos   "^\\(#~ #\n\\|#~ # .*\n\\)+"
   1915  1.1  christos   "Regexp matching the whole editable comment part of an obsolete entry.")
   1916  1.1  christos 
   1917  1.1  christos (defun po-get-comment (kill-flag)
   1918  1.1  christos   "Extract and return the editable comment string, uncommented.
   1919  1.1  christos If KILL-FLAG, then add the unquoted comment to the kill ring."
   1920  1.1  christos   (let ((buffer (current-buffer))
   1921  1.1  christos 	(obsolete (eq po-entry-type 'obsolete)))
   1922  1.1  christos     (save-excursion
   1923  1.1  christos       (goto-char po-start-of-entry)
   1924  1.1  christos       (if (re-search-forward (if obsolete po-obsolete-comment-regexp
   1925  1.1  christos 			         po-active-comment-regexp)
   1926  1.1  christos 			     po-end-of-entry t)
   1927  1.1  christos 	  (po-with-temp-buffer
   1928  1.1  christos 	    (insert-buffer-substring buffer (match-beginning 0) (match-end 0))
   1929  1.1  christos 	    (goto-char (point-min))
   1930  1.1  christos 	    (while (not (eobp))
   1931  1.1  christos 	      (if (looking-at (if obsolete "#~ # ?" "# ?"))
   1932  1.1  christos 		  (replace-match "" t t))
   1933  1.1  christos 	      (forward-line 1))
   1934  1.1  christos 	    (and kill-flag (copy-region-as-kill (point-min) (point-max)))
   1935  1.1  christos 	    (buffer-string))
   1936  1.1  christos 	""))))
   1937  1.1  christos 
   1938  1.1  christos (defun po-set-comment (form)
   1939  1.1  christos   "Using FORM to get a string, replace the current editable comment.
   1940  1.1  christos Evaluating FORM should insert the wanted string in the current buffer.
   1941  1.1  christos If FORM is itself a string, then this string is used for insertion.
   1942  1.1  christos The string is properly recommented before the replacement occurs."
   1943  1.1  christos   (let ((obsolete (eq po-entry-type 'obsolete))
   1944  1.1  christos 	string)
   1945  1.1  christos     (po-with-temp-buffer
   1946  1.1  christos       (if (stringp form)
   1947  1.1  christos 	  (insert form)
   1948  1.1  christos 	(push-mark)
   1949  1.1  christos 	(eval form))
   1950  1.1  christos       (if (not (or (bobp) (= (preceding-char) ?\n)))
   1951  1.1  christos 	  (insert "\n"))
   1952  1.1  christos       (goto-char (point-min))
   1953  1.1  christos       (while (not (eobp))
   1954  1.1  christos 	(insert (if (= (following-char) ?\n)
   1955  1.1  christos 		    (if obsolete "#~ #" "#")
   1956  1.1  christos 		  (if obsolete "#~ # " "# ")))
   1957  1.1  christos 	(search-forward "\n"))
   1958  1.1  christos       (setq string (buffer-string)))
   1959  1.1  christos     (goto-char po-start-of-entry)
   1960  1.1  christos     (if (re-search-forward
   1961  1.1  christos 	 (if obsolete po-obsolete-comment-regexp po-active-comment-regexp)
   1962  1.1  christos 	 po-end-of-entry t)
   1963  1.1  christos 	(if (not (string-equal (po-match-string 0) string))
   1964  1.1  christos 	    (let ((buffer-read-only po-read-only))
   1965  1.1  christos 	      (replace-match string t t)))
   1966  1.1  christos       (skip-chars-forward " \t\n")
   1967  1.1  christos       (let ((buffer-read-only po-read-only))
   1968  1.1  christos 	(insert string))))
   1969  1.1  christos   (po-current-entry))
   1970  1.1  christos 
   1971  1.1  christos (defun po-kill-ring-save-comment ()
   1972  1.1  christos   "Push the msgstr string from current entry on the kill ring."
   1973  1.1  christos   (interactive)
   1974  1.1  christos   (po-find-span-of-entry)
   1975  1.1  christos   (po-get-comment t))
   1976  1.1  christos 
   1977  1.1  christos (defun po-kill-comment ()
   1978  1.1  christos   "Empty the msgstr string from current entry, pushing it on the kill ring."
   1979  1.1  christos   (interactive)
   1980  1.1  christos   (po-kill-ring-save-comment)
   1981  1.1  christos   (po-set-comment "")
   1982  1.1  christos   (po-redisplay))
   1983  1.1  christos 
   1984  1.1  christos (defun po-yank-comment ()
   1985  1.1  christos   "Replace the current comment string by the top of the kill ring."
   1986  1.1  christos   (interactive)
   1987  1.1  christos   (po-find-span-of-entry)
   1988  1.1  christos   (po-set-comment (if (eq last-command 'yank) '(yank-pop 1) '(yank)))
   1989  1.1  christos   (setq this-command 'yank)
   1990  1.1  christos   (po-redisplay))
   1991  1.1  christos 
   1992  1.1  christos ;;; Editing management and submode.
   1994  1.1  christos 
   1995  1.1  christos ;; In a string edit buffer, BACK-POINTER points to one of the slots of the
   1996  1.1  christos ;; list EDITED-FIELDS kept in the PO buffer.  See its description elsewhere.
   1997  1.1  christos ;; Reminder: slots have the form (ENTRY-MARKER EDIT-BUFFER OVERLAY-INFO).
   1998  1.1  christos 
   1999  1.1  christos (defvar po-subedit-back-pointer)
   2000  1.1  christos 
   2001  1.1  christos (defun po-clean-out-killed-edits ()
   2002  1.1  christos   "From EDITED-FIELDS, clean out any edit having a killed edit buffer."
   2003  1.1  christos   (let ((cursor po-edited-fields))
   2004  1.1  christos     (while cursor
   2005  1.1  christos       (let ((slot (car cursor)))
   2006  1.1  christos 	(setq cursor (cdr cursor))
   2007  1.1  christos 	(if (buffer-name (nth 1 slot))
   2008  1.1  christos 	    nil
   2009  1.1  christos 	  (let ((overlay (nth 2 slot)))
   2010  1.1  christos 	    (and overlay (po-dehighlight overlay)))
   2011  1.1  christos 	  (setq po-edited-fields (delete slot po-edited-fields)))))))
   2012  1.1  christos 
   2013  1.1  christos (defun po-check-all-pending-edits ()
   2014  1.1  christos   "Resume any pending edit.  Return nil if some remains."
   2015  1.1  christos   (po-clean-out-killed-edits)
   2016  1.1  christos   (or (null po-edited-fields)
   2017  1.1  christos       (let ((slot (car po-edited-fields)))
   2018  1.1  christos 	(goto-char (nth 0 slot))
   2019  1.1  christos 	(pop-to-buffer (nth 1 slot))
   2020  1.1  christos 	(let ((overlay (nth 2 slot)))
   2021  1.1  christos 	  (and overlay (po-rehighlight overlay)))
   2022  1.1  christos 	(message po-subedit-message)
   2023  1.1  christos 	nil)))
   2024  1.1  christos 
   2025  1.1  christos (defun po-check-for-pending-edit (position)
   2026  1.1  christos   "Resume any pending edit at POSITION.  Return nil if such edit exists."
   2027  1.1  christos   (po-clean-out-killed-edits)
   2028  1.1  christos   (let ((marker (make-marker)))
   2029  1.1  christos     (set-marker marker position)
   2030  1.1  christos     (let ((slot (assoc marker po-edited-fields)))
   2031  1.1  christos       (if slot
   2032  1.1  christos 	  (progn
   2033  1.1  christos 	    (goto-char marker)
   2034  1.1  christos 	    (pop-to-buffer (nth 1 slot))
   2035  1.1  christos 	    (let ((overlay (nth 2 slot)))
   2036  1.1  christos 	      (and overlay (po-rehighlight overlay)))
   2037  1.1  christos 	    (message po-subedit-message)))
   2038  1.1  christos       (not slot))))
   2039  1.1  christos 
   2040  1.1  christos (defun po-edit-out-full ()
   2041  1.1  christos   "Get out of PO mode, leaving PO file buffer in fundamental mode."
   2042  1.1  christos   (interactive)
   2043  1.1  christos   (if (and (po-check-all-pending-edits)
   2044  1.1  christos 	   (yes-or-no-p (_"Should I let you edit the whole PO file? ")))
   2045  1.1  christos       (progn
   2046  1.1  christos 	(setq buffer-read-only po-read-only)
   2047  1.1  christos 	(fundamental-mode)
   2048  1.1  christos 	(message (_"Type 'M-x po-mode RET' once done")))))
   2049  1.1  christos 
   2050  1.1  christos (defun po-ediff-quit ()
   2051  1.1  christos   "Quit ediff and exit `recursive-edit'."
   2052  1.1  christos   (interactive)
   2053  1.1  christos   (ediff-quit t)
   2054  1.1  christos   (exit-recursive-edit))
   2055  1.1  christos 
   2056  1.1  christos (add-hook 'ediff-keymap-setup-hook
   2057  1.1  christos 	  '(lambda ()
   2058  1.1  christos 	     (define-key ediff-mode-map "Q" 'po-ediff-quit)))
   2059  1.1  christos 
   2060  1.1  christos (defun po-ediff-buffers-exit-recursive (b1 b2 oldbuf end)
   2061  1.1  christos   "Ediff buffer B1 and B2, pop back to OLDBUF and replace the old variants.
   2062  1.1  christos This function will delete the first two variants in OLDBUF, call
   2063  1.1  christos `ediff-buffers' to compare both strings and replace the two variants in
   2064  1.1  christos OLDBUF with the contents of B2.
   2065  1.1  christos Once done kill B1 and B2.
   2066  1.1  christos 
   2067  1.1  christos For more info cf. `po-subedit-ediff'."
   2068  1.1  christos   (ediff-buffers b1 b2)
   2069  1.1  christos   (recursive-edit)
   2070  1.1  christos   (pop-to-buffer oldbuf)
   2071  1.1  christos   (delete-region (point-min) end)
   2072  1.1  christos   (insert-buffer b2)
   2073  1.1  christos   (mapc 'kill-buffer `(,b1 ,b2))
   2074  1.1  christos   (display-buffer entry-buffer t))
   2075  1.1  christos 
   2076  1.1  christos (defun po-subedit-ediff ()
   2077  1.1  christos   "Edit the subedit buffer using `ediff'.
   2078  1.1  christos `po-subedit-ediff' calls `po-ediff-buffers-exit-recursive' to edit translation
   2079  1.1  christos variants side by side if they are actually different; if variants are equal just
   2080  1.1  christos delete the first one.
   2081  1.1  christos 
   2082  1.1  christos `msgcat' is able to produce those variants; every variant is marked with:
   2083  1.1  christos 
   2084  1.1  christos #-#-#-#-#  file name reference  #-#-#-#-#
   2085  1.1  christos 
   2086  1.1  christos Put changes in second buffer.
   2087  1.1  christos 
   2088  1.1  christos When done with the `ediff' session press \\[exit-recursive-edit] exit to
   2089  1.1  christos `recursive-edit', or call \\[po-ediff-quit] (`Q') in the ediff control panel."
   2090  1.1  christos   (interactive)
   2091  1.1  christos   (let* ((marker-regex "^#-#-#-#-#  \\(.*\\)  #-#-#-#-#\n")
   2092  1.1  christos 	 (buf1 " *po-msgstr-1") ; default if first marker is missing
   2093  1.1  christos 	 buf2 start-1 end-1 start-2 end-2
   2094  1.1  christos 	 (back-pointer po-subedit-back-pointer)
   2095  1.1  christos 	 (entry-marker (nth 0 back-pointer))
   2096  1.1  christos 	 (entry-buffer (marker-buffer entry-marker)))
   2097  1.1  christos     (goto-char (point-min))
   2098  1.1  christos     (if (looking-at marker-regex)
   2099  1.1  christos 	(and (setq buf1 (match-string-no-properties 1))
   2100  1.1  christos 	     (forward-line 1)))
   2101  1.1  christos     (setq start-1 (point))
   2102  1.1  christos     (if (not (re-search-forward marker-regex (point-max) t))
   2103  1.1  christos 	(error "Only 1 msgstr found")
   2104  1.1  christos       (setq buf2 (match-string-no-properties 1)
   2105  1.1  christos 	    end-1 (match-beginning 0))
   2106  1.1  christos       (let ((oldbuf (current-buffer)))
   2107  1.1  christos 	(save-current-buffer
   2108  1.1  christos 	  (set-buffer (get-buffer-create
   2109  1.1  christos 		       (generate-new-buffer-name buf1)))
   2110  1.1  christos 	  (setq buffer-read-only nil)
   2111  1.1  christos 	  (erase-buffer)
   2112  1.1  christos 	  (insert-buffer-substring oldbuf start-1 end-1)
   2113  1.1  christos 	  (setq buffer-read-only t))
   2114  1.1  christos 
   2115  1.1  christos 	(setq start-2 (point))
   2116  1.1  christos 	(save-excursion
   2117  1.1  christos 	  ;; check for a third variant; if found ignore it
   2118  1.1  christos 	  (if (re-search-forward marker-regex (point-max) t)
   2119  1.1  christos 	      (setq end-2 (match-beginning 0))
   2120  1.1  christos 	    (setq end-2 (goto-char (1- (point-max))))))
   2121  1.1  christos 	(save-current-buffer
   2122  1.1  christos 	  (set-buffer (get-buffer-create
   2123  1.1  christos 		       (generate-new-buffer-name buf2)))
   2124  1.1  christos 	  (erase-buffer)
   2125  1.1  christos 	  (insert-buffer-substring oldbuf start-2 end-2))
   2126  1.1  christos 
   2127  1.1  christos 	(if (not (string-equal (buffer-substring-no-properties start-1 end-1)
   2128  1.1  christos 			       (buffer-substring-no-properties start-2 end-2)))
   2129  1.1  christos 	    (po-ediff-buffers-exit-recursive buf1 buf2 oldbuf end-2)
   2130  1.1  christos 	  (message "Variants are equal; delete %s" buf1)
   2131  1.1  christos 	  (forward-line -1)
   2132  1.1  christos 	  (delete-region (point-min) (point)))))))
   2133  1.1  christos 
   2134  1.1  christos (defun po-subedit-abort ()
   2135  1.1  christos   "Exit the subedit buffer, merely discarding its contents."
   2136  1.1  christos   (interactive)
   2137  1.1  christos   (let* ((edit-buffer (current-buffer))
   2138  1.1  christos 	 (back-pointer po-subedit-back-pointer)
   2139  1.1  christos 	 (entry-marker (nth 0 back-pointer))
   2140  1.1  christos 	 (overlay-info (nth 2 back-pointer))
   2141  1.1  christos 	 (entry-buffer (marker-buffer entry-marker)))
   2142  1.1  christos     (if (null entry-buffer)
   2143  1.1  christos 	(error (_"Corresponding PO buffer does not exist anymore"))
   2144  1.1  christos       (or (one-window-p) (delete-window))
   2145  1.1  christos       (switch-to-buffer entry-buffer)
   2146  1.1  christos       (goto-char entry-marker)
   2147  1.1  christos       (and overlay-info (po-dehighlight overlay-info))
   2148  1.1  christos       (kill-buffer edit-buffer)
   2149  1.1  christos       (setq po-edited-fields (delete back-pointer po-edited-fields)))))
   2150  1.1  christos 
   2151  1.1  christos (defun po-subedit-exit ()
   2152  1.1  christos   "Exit the subedit buffer, replacing the string in the PO buffer."
   2153  1.1  christos   (interactive)
   2154  1.1  christos   (goto-char (point-max))
   2155  1.1  christos   (skip-chars-backward " \t\n")
   2156  1.1  christos   (if (eq (preceding-char) ?<)
   2157  1.1  christos       (delete-region (1- (point)) (point-max)))
   2158  1.1  christos   (run-hooks 'po-subedit-exit-hook)
   2159  1.1  christos   (let ((string (buffer-string)))
   2160  1.1  christos     (po-subedit-abort)
   2161  1.1  christos     (po-find-span-of-entry)
   2162  1.1  christos     (cond ((= (point) po-start-of-msgid)
   2163  1.1  christos 	   (po-set-comment string)
   2164  1.1  christos 	   (po-redisplay))
   2165  1.1  christos 	  ((= (point) po-start-of-msgstr)
   2166  1.1  christos 	   (let ((replaced (po-set-msgstr string)))
   2167  1.1  christos 	     (if (and replaced
   2168  1.1  christos 		      po-auto-fuzzy-on-edit
   2169  1.1  christos 		      (eq po-entry-type 'translated))
   2170  1.1  christos 		 (progn
   2171  1.1  christos 		   (po-decrease-type-counter)
   2172  1.1  christos 		   (po-add-attribute "fuzzy")
   2173  1.1  christos 		   (po-current-entry)
   2174  1.1  christos 		   (po-increase-type-counter)))))
   2175  1.1  christos 	  (t (debug)))))
   2176  1.1  christos 
   2177  1.1  christos (defun po-edit-string (string type expand-tabs)
   2178  1.1  christos   "Prepare a pop up buffer for editing STRING, which is of a given TYPE.
   2179  1.1  christos TYPE may be 'comment or 'msgstr.  If EXPAND-TABS, expand tabs to spaces.
   2180  1.1  christos Run functions on po-subedit-mode-hook."
   2181  1.1  christos   (let ((marker (make-marker)))
   2182  1.1  christos     (set-marker marker (cond ((eq type 'comment) po-start-of-msgid)
   2183  1.1  christos 			     ((eq type 'msgstr) po-start-of-msgstr)))
   2184  1.1  christos     (if (po-check-for-pending-edit marker)
   2185  1.1  christos 	(let ((edit-buffer (generate-new-buffer
   2186  1.1  christos 			    (concat "*" (buffer-name) "*")))
   2187  1.1  christos 	      (edit-coding buffer-file-coding-system)
   2188  1.1  christos 	      (buffer (current-buffer))
   2189  1.1  christos 	      overlay slot)
   2190  1.1  christos 	  (if (and (eq type 'msgstr) po-highlighting)
   2191  1.1  christos 	      ;; ;; Try showing all of msgid in the upper window while editing.
   2192  1.1  christos 	      ;; (goto-char (1- po-start-of-msgstr))
   2193  1.1  christos 	      ;; (recenter -1)
   2194  1.1  christos 	      (save-excursion
   2195  1.1  christos 		(goto-char po-start-of-entry)
   2196  1.1  christos 		(re-search-forward po-any-msgid-regexp nil t)
   2197  1.1  christos 		(let ((end (1- (match-end 0))))
   2198  1.1  christos 		  (goto-char (match-beginning 0))
   2199  1.1  christos 		  (re-search-forward "msgid +" nil t)
   2200  1.1  christos 		  (setq overlay (po-create-overlay))
   2201  1.1  christos 		  (po-highlight overlay (point) end buffer))))
   2202  1.1  christos 	  (setq slot (list marker edit-buffer overlay)
   2203  1.1  christos 		po-edited-fields (cons slot po-edited-fields))
   2204  1.1  christos 	  (pop-to-buffer edit-buffer)
   2205  1.1  christos 	  (set (make-local-variable 'po-subedit-back-pointer) slot)
   2206  1.1  christos 	  (set (make-local-variable 'indent-line-function)
   2207  1.1  christos 	       'indent-relative)
   2208  1.1  christos 	  (setq buffer-file-coding-system edit-coding)
   2209  1.1  christos 	  (setq local-abbrev-table po-mode-abbrev-table)
   2210  1.1  christos 	  (erase-buffer)
   2211  1.1  christos 	  (insert string "<")
   2212  1.1  christos 	  (goto-char (point-min))
   2213  1.1  christos 	  (and expand-tabs (setq indent-tabs-mode nil))
   2214  1.1  christos 	  (use-local-map po-subedit-mode-map)
   2215  1.1  christos 	  (if (fboundp 'easy-menu-define)
   2216  1.1  christos 	      (progn
   2217  1.1  christos 		(easy-menu-define po-subedit-mode-menu po-subedit-mode-map ""
   2218  1.1  christos 		  po-subedit-mode-menu-layout)
   2219  1.1  christos 		(and po-XEMACS (easy-menu-add po-subedit-mode-menu))))
   2220  1.1  christos 	  (set-syntax-table po-subedit-mode-syntax-table)
   2221  1.1  christos 	  (run-hooks 'po-subedit-mode-hook)
   2222  1.1  christos 	  (message po-subedit-message)))))
   2223  1.1  christos 
   2224  1.1  christos (defun po-edit-comment ()
   2225  1.1  christos   "Use another window to edit the current translator comment."
   2226  1.1  christos   (interactive)
   2227  1.1  christos   (po-find-span-of-entry)
   2228  1.1  christos   (po-edit-string (po-get-comment nil) 'comment nil))
   2229  1.1  christos 
   2230  1.1  christos (defun po-edit-comment-and-ediff ()
   2231  1.1  christos   "Use `ediff' to edit the current translator comment.
   2232  1.1  christos This function calls `po-edit-msgstr' and `po-subedit-ediff'; for more info
   2233  1.1  christos read `po-subedit-ediff' documentation."
   2234  1.1  christos   (interactive)
   2235  1.1  christos   (po-edit-comment)
   2236  1.1  christos   (po-subedit-ediff))
   2237  1.1  christos 
   2238  1.1  christos (defun po-edit-msgstr ()
   2239  1.1  christos   "Use another window to edit the current msgstr."
   2240  1.1  christos   (interactive)
   2241  1.1  christos   (po-find-span-of-entry)
   2242  1.1  christos   (po-edit-string (if (and po-auto-edit-with-msgid
   2243  1.1  christos 			   (eq po-entry-type 'untranslated))
   2244  1.1  christos 		      (po-get-msgid nil)
   2245  1.1  christos 		    (po-get-msgstr nil))
   2246  1.1  christos 		  'msgstr
   2247  1.1  christos 		  t))
   2248  1.1  christos 
   2249  1.1  christos (defun po-edit-msgstr-and-ediff ()
   2250  1.1  christos   "Use `ediff' to edit the current msgstr.
   2251  1.1  christos This function calls `po-edit-msgstr' and `po-subedit-ediff'; for more info
   2252  1.1  christos read `po-subedit-ediff' documentation."
   2253  1.1  christos   (interactive)
   2254  1.1  christos   (po-edit-msgstr)
   2255  1.1  christos   (po-subedit-ediff))
   2256  1.1  christos 
   2257  1.1  christos ;;; String normalization and searching.
   2259  1.1  christos 
   2260  1.1  christos (defun po-normalize-old-style (explain)
   2261  1.1  christos   "Normalize old gettext style fields using K&R C multiline string syntax.
   2262  1.1  christos To minibuffer messages sent while normalizing, add the EXPLAIN string."
   2263  1.1  christos   (let ((here (point-marker))
   2264  1.1  christos 	(counter 0)
   2265  1.1  christos 	(buffer-read-only po-read-only))
   2266  1.1  christos     (goto-char (point-min))
   2267  1.1  christos     (message (_"Normalizing %d, %s") counter explain)
   2268  1.1  christos     (while (re-search-forward
   2269  1.1  christos 	    "\\(^#?[ \t]*msg\\(id\\|str\\)[ \t]*\"\\|[^\" \t][ \t]*\\)\\\\\n"
   2270  1.1  christos 	    nil t)
   2271  1.1  christos       (if (= (% counter 10) 0)
   2272  1.1  christos 	  (message (_"Normalizing %d, %s") counter explain))
   2273  1.1  christos       (replace-match "\\1\"\n\"" t nil)
   2274  1.1  christos       (setq counter (1+ counter)))
   2275  1.1  christos     (goto-char here)
   2276  1.1  christos     (message (_"Normalizing %d...done") counter)))
   2277  1.1  christos 
   2278  1.1  christos (defun po-normalize-field (field explain)
   2279  1.1  christos   "Normalize FIELD of all entries.  FIELD is either the symbol msgid or msgstr.
   2280  1.1  christos To minibuffer messages sent while normalizing, add the EXPLAIN string."
   2281  1.1  christos   (let ((here (point-marker))
   2282  1.1  christos 	(counter 0))
   2283  1.1  christos     (goto-char (point-min))
   2284  1.1  christos     (while (re-search-forward po-any-msgstr-regexp nil t)
   2285  1.1  christos       (if (= (% counter 10) 0)
   2286  1.1  christos 	  (message (_"Normalizing %d, %s") counter explain))
   2287  1.1  christos       (goto-char (match-beginning 0))
   2288  1.1  christos       (po-find-span-of-entry)
   2289  1.1  christos       (cond ((eq field 'msgid) (po-set-msgid (po-get-msgid nil)))
   2290  1.1  christos 	    ((eq field 'msgstr) (po-set-msgstr (po-get-msgstr nil))))
   2291  1.1  christos       (goto-char po-end-of-entry)
   2292  1.1  christos       (setq counter (1+ counter)))
   2293  1.1  christos     (goto-char here)
   2294  1.1  christos     (message (_"Normalizing %d...done") counter)))
   2295  1.1  christos 
   2296  1.1  christos ;; Normalize, but the British way! :-)
   2297  1.1  christos (defsubst po-normalise () (po-normalize))
   2298  1.1  christos 
   2299  1.1  christos (defun po-normalize ()
   2300  1.1  christos   "Normalize all entries in the PO file."
   2301  1.1  christos   (interactive)
   2302  1.1  christos   (po-normalize-old-style (_"pass 1/3"))
   2303  1.1  christos   (po-normalize-field t (_"pass 2/3"))
   2304  1.1  christos   (po-normalize-field nil (_"pass 3/3"))
   2305  1.1  christos   ;; The last PO file entry has just been processed.
   2306  1.1  christos   (if (not (= po-end-of-entry (point-max)))
   2307  1.1  christos       (let ((buffer-read-only po-read-only))
   2308  1.1  christos 	(kill-region po-end-of-entry (point-max))))
   2309  1.1  christos   ;; A bizarre format might have fooled the counters, so recompute
   2310  1.1  christos   ;; them to make sure their value is dependable.
   2311  1.1  christos   (po-compute-counters nil))
   2312  1.1  christos 
   2313  1.1  christos ;;; Multiple PO files.
   2315  1.1  christos 
   2316  1.1  christos (defun po-show-auxiliary-list ()
   2317  1.1  christos   "Echo the current auxiliary list in the message area."
   2318  1.1  christos   (if po-auxiliary-list
   2319  1.1  christos       (let ((cursor po-auxiliary-cursor)
   2320  1.1  christos 	    string)
   2321  1.1  christos 	(while cursor
   2322  1.1  christos 	  (setq string (concat string (if string " ") (car (car cursor)))
   2323  1.1  christos 		cursor (cdr cursor)))
   2324  1.1  christos 	(setq cursor po-auxiliary-list)
   2325  1.1  christos 	(while (not (eq cursor po-auxiliary-cursor))
   2326  1.1  christos 	  (setq string (concat string (if string " ") (car (car cursor)))
   2327  1.1  christos 		cursor (cdr cursor)))
   2328  1.1  christos 	(message string))
   2329  1.1  christos     (message (_"No auxiliary files."))))
   2330  1.1  christos 
   2331  1.1  christos (defun po-consider-as-auxiliary ()
   2332  1.1  christos   "Add the current PO file to the list of auxiliary files."
   2333  1.1  christos   (interactive)
   2334  1.1  christos   (if (member (list buffer-file-name) po-auxiliary-list)
   2335  1.1  christos       nil
   2336  1.1  christos     (setq po-auxiliary-list
   2337  1.1  christos 	  (nconc po-auxiliary-list (list (list buffer-file-name))))
   2338  1.1  christos     (or po-auxiliary-cursor
   2339  1.1  christos 	(setq po-auxiliary-cursor po-auxiliary-list)))
   2340  1.1  christos   (po-show-auxiliary-list))
   2341  1.1  christos 
   2342  1.1  christos (defun po-ignore-as-auxiliary ()
   2343  1.1  christos   "Delete the current PO file from the list of auxiliary files."
   2344  1.1  christos   (interactive)
   2345  1.1  christos   (setq po-auxiliary-list (delete (list buffer-file-name) po-auxiliary-list)
   2346  1.1  christos 	po-auxiliary-cursor po-auxiliary-list)
   2347  1.1  christos   (po-show-auxiliary-list))
   2348  1.1  christos 
   2349  1.1  christos (defun po-seek-equivalent-translation (name string)
   2350  1.1  christos   "Search a PO file NAME for a 'msgid' STRING having a non-empty 'msgstr'.
   2351  1.1  christos STRING is the full quoted msgid field, including the 'msgid' keyword.  When
   2352  1.1  christos found, display the file over the current window, with the 'msgstr' field
   2353  1.1  christos possibly highlighted, the cursor at start of msgid, then return 't'.
   2354  1.1  christos Otherwise, move nothing, and just return 'nil'."
   2355  1.1  christos   (let ((current (current-buffer))
   2356  1.1  christos 	(buffer (find-file-noselect name)))
   2357  1.1  christos     (set-buffer buffer)
   2358  1.1  christos     (let ((start (point))
   2359  1.1  christos 	  found)
   2360  1.1  christos       (goto-char (point-min))
   2361  1.1  christos       (while (and (not found) (search-forward string nil t))
   2362  1.1  christos 	;; Screen out longer 'msgid's.
   2363  1.1  christos 	(if (looking-at "^msgstr ")
   2364  1.1  christos 	    (progn
   2365  1.1  christos 	      (po-find-span-of-entry)
   2366  1.1  christos 	      ;; Ignore an untranslated entry.
   2367  1.1  christos 	      (or (string-equal
   2368  1.1  christos 		   (buffer-substring po-start-of-msgstr po-end-of-entry)
   2369  1.1  christos 		   "msgstr \"\"\n")
   2370  1.1  christos 		  (setq found t)))))
   2371  1.1  christos       (if found
   2372  1.1  christos 	  (progn
   2373  1.1  christos 	    (switch-to-buffer buffer)
   2374  1.1  christos 	    (po-find-span-of-entry)
   2375  1.1  christos 	    (if po-highlighting
   2376  1.1  christos 		(progn
   2377  1.1  christos 		  (goto-char po-start-of-entry)
   2378  1.1  christos 		  (re-search-forward po-any-msgstr-regexp nil t)
   2379  1.1  christos 		  (let ((end (1- (match-end 0))))
   2380  1.1  christos 		    (goto-char (match-beginning 0))
   2381  1.1  christos 		    (re-search-forward "msgstr +" nil t)
   2382  1.1  christos 		    ;; Just "borrow" the marking overlay.
   2383  1.1  christos 		    (po-highlight po-marking-overlay (point) end))))
   2384  1.1  christos 	    (goto-char po-start-of-msgid))
   2385  1.1  christos 	(goto-char start)
   2386  1.1  christos 	(po-find-span-of-entry)
   2387  1.1  christos 	(set-buffer current))
   2388  1.1  christos       found)))
   2389  1.1  christos 
   2390  1.1  christos (defun po-cycle-auxiliary ()
   2391  1.1  christos   "Select the next auxiliary file having an entry with same 'msgid'."
   2392  1.1  christos   (interactive)
   2393  1.1  christos   (po-find-span-of-entry)
   2394  1.1  christos   (if po-auxiliary-list
   2395  1.1  christos       (let ((string (buffer-substring po-start-of-msgid po-start-of-msgstr))
   2396  1.1  christos 	    (cursor po-auxiliary-cursor)
   2397  1.1  christos 	    found name)
   2398  1.1  christos 	(while (and (not found) cursor)
   2399  1.1  christos 	  (setq name (car (car cursor)))
   2400  1.1  christos 	  (if (and (not (string-equal buffer-file-name name))
   2401  1.1  christos 		   (po-seek-equivalent-translation name string))
   2402  1.1  christos 	      (setq found t
   2403  1.1  christos 		    po-auxiliary-cursor cursor))
   2404  1.1  christos 	  (setq cursor (cdr cursor)))
   2405  1.1  christos 	(setq cursor po-auxiliary-list)
   2406  1.1  christos 	(while (and (not found) cursor)
   2407  1.1  christos 	  (setq name (car (car cursor)))
   2408  1.1  christos 	  (if (and (not (string-equal buffer-file-name name))
   2409  1.1  christos 		   (po-seek-equivalent-translation name string))
   2410  1.1  christos 	      (setq found t
   2411  1.1  christos 		    po-auxiliary-cursor cursor))
   2412  1.1  christos 	  (setq cursor (cdr cursor)))
   2413  1.1  christos 	(or found (message (_"No other translation found")))
   2414  1.1  christos         found)))
   2415  1.1  christos 
   2416  1.1  christos (defun po-subedit-cycle-auxiliary ()
   2417  1.1  christos   "Cycle auxiliary file, but from the translation edit buffer."
   2418  1.1  christos   (interactive)
   2419  1.1  christos   (let* ((entry-marker (nth 0 po-subedit-back-pointer))
   2420  1.1  christos 	 (entry-buffer (marker-buffer entry-marker))
   2421  1.1  christos 	 (buffer (current-buffer)))
   2422  1.1  christos     (pop-to-buffer entry-buffer)
   2423  1.1  christos     (po-cycle-auxiliary)
   2424  1.1  christos     (pop-to-buffer buffer)))
   2425  1.1  christos 
   2426  1.1  christos (defun po-select-auxiliary ()
   2427  1.1  christos   "Select one of the available auxiliary files and locate an equivalent entry.
   2428  1.1  christos If an entry having the same 'msgid' cannot be found, merely select the file
   2429  1.1  christos without moving its cursor."
   2430  1.1  christos   (interactive)
   2431  1.1  christos   (po-find-span-of-entry)
   2432  1.1  christos   (if po-auxiliary-list
   2433  1.1  christos       (let ((string (buffer-substring po-start-of-msgid po-start-of-msgstr))
   2434  1.1  christos 	    (name (car (assoc (completing-read (_"Which auxiliary file? ")
   2435  1.1  christos 					       po-auxiliary-list nil t)
   2436  1.1  christos 			      po-auxiliary-list))))
   2437  1.1  christos 	(po-consider-as-auxiliary)
   2438  1.1  christos 	(or (po-seek-equivalent-translation name string)
   2439  1.1  christos 	    (find-file name)))))
   2440  1.1  christos 
   2441  1.1  christos ;;; Original program sources as context.
   2443  1.1  christos 
   2444  1.1  christos (defun po-show-source-path ()
   2445  1.1  christos   "Echo the current source search path in the message area."
   2446  1.1  christos   (if po-search-path
   2447  1.1  christos       (let ((cursor po-search-path)
   2448  1.1  christos 	    string)
   2449  1.1  christos 	(while cursor
   2450  1.1  christos 	  (setq string (concat string (if string " ") (car (car cursor)))
   2451  1.1  christos 		cursor (cdr cursor)))
   2452  1.1  christos 	(message string))
   2453  1.1  christos     (message (_"Empty source path."))))
   2454  1.1  christos 
   2455  1.1  christos (defun po-consider-source-path (directory)
   2456  1.1  christos   "Add a given DIRECTORY, requested interactively, to the source search path."
   2457  1.1  christos   (interactive "DDirectory for search path: ")
   2458  1.1  christos   (setq po-search-path (cons (list (if (string-match "/$" directory)
   2459  1.1  christos 					 directory
   2460  1.1  christos 				       (concat directory "/")))
   2461  1.1  christos 			     po-search-path))
   2462  1.1  christos   (setq po-reference-check 0)
   2463  1.1  christos   (po-show-source-path))
   2464  1.1  christos 
   2465  1.1  christos (defun po-ignore-source-path ()
   2466  1.1  christos   "Delete a directory, selected with completion, from the source search path."
   2467  1.1  christos   (interactive)
   2468  1.1  christos   (setq po-search-path
   2469  1.1  christos 	(delete (list (completing-read (_"Directory to remove? ")
   2470  1.1  christos 				       po-search-path nil t))
   2471  1.1  christos 		po-search-path))
   2472  1.1  christos   (setq po-reference-check 0)
   2473  1.1  christos   (po-show-source-path))
   2474  1.1  christos 
   2475  1.1  christos (defun po-ensure-source-references ()
   2476  1.1  christos   "Extract all references into a list, with paths resolved, if necessary."
   2477  1.1  christos   (po-find-span-of-entry)
   2478  1.1  christos   (if (= po-start-of-entry po-reference-check)
   2479  1.1  christos       nil
   2480  1.1  christos     (setq po-reference-alist nil)
   2481  1.1  christos     (save-excursion
   2482  1.1  christos       (goto-char po-start-of-entry)
   2483  1.1  christos       (if (re-search-forward "^#:" po-start-of-msgid t)
   2484  1.1  christos 	  (let (current name line path file)
   2485  1.1  christos 	    (while (looking-at "\\(\n#:\\)? *\\([^: ]*\\):\\([0-9]+\\)")
   2486  1.1  christos 	      (goto-char (match-end 0))
   2487  1.1  christos 	      (setq name (po-match-string 2)
   2488  1.1  christos 		    line (po-match-string 3)
   2489  1.1  christos 		    path po-search-path)
   2490  1.1  christos 	      (if (string-equal name "")
   2491  1.1  christos 		  nil
   2492  1.1  christos 		(while (and (not (file-exists-p
   2493  1.1  christos 				  (setq file (concat (car (car path)) name))))
   2494  1.1  christos 			    path)
   2495  1.1  christos 		  (setq path (cdr path)))
   2496  1.1  christos 		(setq current (and path file)))
   2497  1.1  christos 	      (if current
   2498  1.1  christos 		  (setq po-reference-alist
   2499  1.1  christos 			(cons (list (concat current ":" line)
   2500  1.1  christos 				    current
   2501  1.1  christos 				    (string-to-number line))
   2502  1.1  christos 			      po-reference-alist)))))))
   2503  1.1  christos     (setq po-reference-alist (nreverse po-reference-alist)
   2504  1.1  christos 	  po-reference-cursor po-reference-alist
   2505  1.1  christos 	  po-reference-check po-start-of-entry)))
   2506  1.1  christos 
   2507  1.1  christos (defun po-show-source-context (triplet)
   2508  1.1  christos   "Show the source context given a TRIPLET which is (PROMPT FILE LINE)."
   2509  1.1  christos   (find-file-other-window (car (cdr triplet)))
   2510  1.1  christos   (goto-line (car (cdr (cdr triplet))))
   2511  1.1  christos   (other-window 1)
   2512  1.1  christos   (let ((maximum 0)
   2513  1.1  christos 	position
   2514  1.1  christos 	(cursor po-reference-alist))
   2515  1.1  christos     (while (not (eq triplet (car cursor)))
   2516  1.1  christos       (setq maximum (1+ maximum)
   2517  1.1  christos 	    cursor (cdr cursor)))
   2518  1.1  christos     (setq position (1+ maximum)
   2519  1.1  christos 	  po-reference-cursor cursor)
   2520  1.1  christos     (while cursor
   2521  1.1  christos       (setq maximum (1+ maximum)
   2522  1.1  christos 	    cursor (cdr cursor)))
   2523  1.1  christos     (message (_"Displaying %d/%d: \"%s\"") position maximum (car triplet))))
   2524  1.1  christos 
   2525  1.1  christos (defun po-cycle-source-reference ()
   2526  1.1  christos   "Display some source context for the current entry.
   2527  1.1  christos If the command is repeated many times in a row, cycle through contexts."
   2528  1.1  christos   (interactive)
   2529  1.1  christos   (po-ensure-source-references)
   2530  1.1  christos   (if po-reference-cursor
   2531  1.1  christos       (po-show-source-context
   2532  1.1  christos        (car (if (eq last-command 'po-cycle-source-reference)
   2533  1.1  christos 		(or (cdr po-reference-cursor) po-reference-alist)
   2534  1.1  christos 	      po-reference-cursor)))
   2535  1.1  christos     (error (_"No resolved source references"))))
   2536  1.1  christos 
   2537  1.1  christos (defun po-select-source-reference ()
   2538  1.1  christos   "Select one of the available source contexts for the current entry."
   2539  1.1  christos   (interactive)
   2540  1.1  christos   (po-ensure-source-references)
   2541  1.1  christos   (if po-reference-alist
   2542  1.1  christos       (po-show-source-context
   2543  1.1  christos        (assoc
   2544  1.1  christos 	(completing-read (_"Which source context? ") po-reference-alist nil t)
   2545  1.1  christos 	po-reference-alist))
   2546  1.1  christos     (error (_"No resolved source references"))))
   2547  1.1  christos 
   2548  1.1  christos ;;; String marking in program sources, through TAGS table.
   2550  1.1  christos 
   2551  1.1  christos ;; Globally defined within tags.el.
   2552  1.1  christos (defvar tags-loop-operate)
   2553  1.1  christos (defvar tags-loop-scan)
   2554  1.1  christos 
   2555  1.1  christos ;; Locally set in each program source buffer.
   2556  1.1  christos (defvar po-find-string-function)
   2557  1.1  christos (defvar po-mark-string-function)
   2558  1.1  christos 
   2559  1.1  christos ;; Dynamically set within po-tags-search for po-tags-loop-operate.
   2560  1.1  christos (defvar po-current-po-buffer)
   2561  1.1  christos (defvar po-current-po-keywords)
   2562  1.1  christos 
   2563  1.1  christos (defun po-tags-search (restart)
   2564  1.1  christos   "Find an unmarked translatable string through all files in tags table.
   2565  1.1  christos Disregard some simple strings which are most probably non-translatable.
   2566  1.1  christos With prefix argument, restart search at first file."
   2567  1.1  christos   (interactive "P")
   2568  1.1  christos   (require 'etags)
   2569  1.1  christos   ;; Ensure there is no highlighting, in case the search fails.
   2570  1.1  christos   (if po-highlighting
   2571  1.1  christos       (po-dehighlight po-marking-overlay))
   2572  1.1  christos   (setq po-string-contents nil)
   2573  1.1  christos   ;; Search for a string which might later be marked for translation.
   2574  1.1  christos   (let ((po-current-po-buffer (current-buffer))
   2575  1.1  christos 	(po-current-po-keywords po-keywords))
   2576  1.1  christos     (pop-to-buffer po-string-buffer)
   2577  1.1  christos     (if (and (not restart)
   2578  1.1  christos 	     (eq (car tags-loop-operate) 'po-tags-loop-operate))
   2579  1.1  christos 	;; Continue last po-tags-search.
   2580  1.1  christos 	(tags-loop-continue nil)
   2581  1.1  christos       ;; Start or restart po-tags-search all over.
   2582  1.1  christos       (setq tags-loop-scan '(po-tags-loop-scan)
   2583  1.1  christos 	    tags-loop-operate '(po-tags-loop-operate))
   2584  1.1  christos       (tags-loop-continue t))
   2585  1.1  christos     (select-window (get-buffer-window po-current-po-buffer)))
   2586  1.1  christos   (if po-string-contents
   2587  1.1  christos       (let ((window (selected-window))
   2588  1.1  christos 	    (buffer po-string-buffer)
   2589  1.1  christos 	    (start po-string-start)
   2590  1.1  christos 	    (end po-string-end))
   2591  1.1  christos 	;; Try to fit the string in the displayed part of its window.
   2592  1.1  christos 	(select-window (get-buffer-window buffer))
   2593  1.1  christos 	(goto-char start)
   2594  1.1  christos 	(or (pos-visible-in-window-p start)
   2595  1.1  christos 	    (recenter '(nil)))
   2596  1.1  christos 	(if (pos-visible-in-window-p end)
   2597  1.1  christos 	    (goto-char end)
   2598  1.1  christos 	  (goto-char end)
   2599  1.1  christos 	  (recenter -1))
   2600  1.1  christos 	(select-window window)
   2601  1.1  christos 	;; Highlight the string as found.
   2602  1.1  christos 	(and po-highlighting
   2603  1.1  christos 	     (po-highlight po-marking-overlay start end buffer)))))
   2604  1.1  christos 
   2605  1.1  christos (defun po-tags-loop-scan ()
   2606  1.1  christos   "Decide if the current buffer is still interesting for PO mode strings."
   2607  1.1  christos   ;; We have little choice, here.  The major mode is needed to dispatch to the
   2608  1.1  christos   ;; proper scanner, so we declare all files as interesting, to force Emacs
   2609  1.1  christos   ;; tags module to revisit files fully.  po-tags-loop-operate sets point at
   2610  1.1  christos   ;; end of buffer when it is done with a file.
   2611  1.1  christos   (not (eobp)))
   2612  1.1  christos 
   2613  1.1  christos (defun po-tags-loop-operate ()
   2614  1.1  christos   "Find an acceptable tag in the current buffer, according to mode.
   2615  1.1  christos Disregard some simple strings which are most probably non-translatable."
   2616  1.1  christos   (po-preset-string-functions)
   2617  1.1  christos   (let ((continue t)
   2618  1.1  christos 	data)
   2619  1.1  christos     (while continue
   2620  1.1  christos       (setq data (apply po-find-string-function po-current-po-keywords nil))
   2621  1.1  christos       (if data
   2622  1.1  christos 	  ;; Push the string just found into a work buffer for study.
   2623  1.1  christos 	  (po-with-temp-buffer
   2624  1.1  christos 	   (insert (nth 0 data))
   2625  1.1  christos 	   (goto-char (point-min))
   2626  1.1  christos 	   ;; Accept if at least three letters in a row.
   2627  1.1  christos 	   (if (re-search-forward "[A-Za-z][A-Za-z][A-Za-z]" nil t)
   2628  1.1  christos 	       (setq continue nil)
   2629  1.1  christos 	     ;; Disregard if single letters or no letters at all.
   2630  1.1  christos 	     (if (re-search-forward "[A-Za-z][A-Za-z]" nil t)
   2631  1.1  christos 		 ;; Here, we have two letters in a row, but never more.
   2632  1.1  christos 		 ;; Accept only if more letters than punctuations.
   2633  1.1  christos 		 (let ((total (buffer-size)))
   2634  1.1  christos 		   (goto-char (point-min))
   2635  1.1  christos 		   (while (re-search-forward "[A-Za-z]+" nil t)
   2636  1.1  christos 		     (replace-match "" t t))
   2637  1.1  christos 		   (if (< (* 2 (buffer-size)) total)
   2638  1.1  christos 		       (setq continue nil))))))
   2639  1.1  christos 	;; No string left in this buffer.
   2640  1.1  christos 	(setq continue nil)))
   2641  1.1  christos     (if data
   2642  1.1  christos 	;; Save information for marking functions.
   2643  1.1  christos 	(let ((buffer (current-buffer)))
   2644  1.1  christos 	  (save-excursion
   2645  1.1  christos 	    (set-buffer po-current-po-buffer)
   2646  1.1  christos 	    (setq po-string-contents (nth 0 data)
   2647  1.1  christos 		  po-string-buffer buffer
   2648  1.1  christos 		  po-string-start (nth 1 data)
   2649  1.1  christos 		  po-string-end (nth 2 data))))
   2650  1.1  christos       (goto-char (point-max)))
   2651  1.1  christos     ;; If nothing was found, trigger scanning of next file.
   2652  1.1  christos     (not data)))
   2653  1.1  christos 
   2654  1.1  christos (defun po-mark-found-string (keyword)
   2655  1.1  christos   "Mark last found string in program sources as translatable, using KEYWORD."
   2656  1.1  christos   (if (not po-string-contents)
   2657  1.1  christos     (error (_"No such string")))
   2658  1.1  christos   (and po-highlighting (po-dehighlight po-marking-overlay))
   2659  1.1  christos   (let ((contents po-string-contents)
   2660  1.1  christos 	(buffer po-string-buffer)
   2661  1.1  christos 	(start po-string-start)
   2662  1.1  christos 	(end po-string-end)
   2663  1.1  christos 	line string)
   2664  1.1  christos     ;; Mark string in program sources.
   2665  1.1  christos     (save-excursion
   2666  1.1  christos       (set-buffer buffer)
   2667  1.1  christos       (setq line (count-lines (point-min) start))
   2668  1.1  christos       (apply po-mark-string-function start end keyword nil))
   2669  1.1  christos     ;; Add PO file entry.
   2670  1.1  christos     (let ((buffer-read-only po-read-only))
   2671  1.1  christos       (goto-char (point-max))
   2672  1.1  christos       (insert "\n" (format "#: %s:%d\n"
   2673  1.1  christos 			   (buffer-file-name po-string-buffer)
   2674  1.1  christos 			   line))
   2675  1.1  christos       (save-excursion
   2676  1.1  christos 	(insert (po-eval-requoted contents "msgid" nil) "msgstr \"\"\n"))
   2677  1.1  christos       (setq po-untranslated-counter (1+ po-untranslated-counter))
   2678  1.1  christos       (po-update-mode-line-string))
   2679  1.1  christos     (setq po-string-contents nil)))
   2680  1.1  christos 
   2681  1.1  christos (defun po-mark-translatable ()
   2682  1.1  christos   "Mark last found string in program sources as translatable, using '_'."
   2683  1.1  christos   (interactive)
   2684  1.1  christos   (po-mark-found-string "_"))
   2685  1.1  christos 
   2686  1.1  christos (defun po-select-mark-and-mark (arg)
   2687  1.1  christos   "Mark last found string in program sources as translatable, ask for keywoard,
   2688  1.1  christos using completion.  With prefix argument, just ask the name of a preferred
   2689  1.1  christos keyword for subsequent commands, also added to possible completions."
   2690  1.1  christos   (interactive "P")
   2691  1.1  christos   (if arg
   2692  1.1  christos       (let ((keyword (list (read-from-minibuffer (_"Keyword: ")))))
   2693  1.1  christos 	(setq po-keywords (cons keyword (delete keyword po-keywords))))
   2694  1.1  christos     (or po-string-contents (error (_"No such string")))
   2695  1.1  christos     (let* ((default (car (car po-keywords)))
   2696  1.1  christos 	   (keyword (completing-read (format (_"Mark with keywoard? [%s] ")
   2697  1.1  christos 					     default)
   2698  1.1  christos 				     po-keywords nil t )))
   2699  1.1  christos       (if (string-equal keyword "") (setq keyword default))
   2700  1.1  christos       (po-mark-found-string keyword))))
   2701  1.1  christos 
   2702  1.1  christos ;;; Unknown mode specifics.
   2704  1.1  christos 
   2705  1.1  christos (defun po-preset-string-functions ()
   2706  1.1  christos   "Preset FIND-STRING-FUNCTION and MARK-STRING-FUNCTION according to mode.
   2707  1.1  christos These variables are locally set in source buffer only when not already bound."
   2708  1.1  christos   (let ((pair (cond ((string-equal mode-name "AWK")
   2709  1.1  christos 		     '(po-find-awk-string . po-mark-awk-string))
   2710  1.1  christos 		    ((member mode-name '("C" "C++"))
   2711  1.1  christos 		     '(po-find-c-string . po-mark-c-string))
   2712  1.1  christos 		    ((string-equal mode-name "Emacs-Lisp")
   2713  1.1  christos 		     '(po-find-emacs-lisp-string . po-mark-emacs-lisp-string))
   2714  1.1  christos 		    ((string-equal mode-name "Python")
   2715  1.1  christos 		     '(po-find-python-string . po-mark-python-string))
   2716  1.1  christos 		    ((and (string-equal mode-name "Shell-script")
   2717  1.1  christos 			  (string-equal mode-line-process "[bash]"))
   2718  1.1  christos 		     '(po-find-bash-string . po-mark-bash-string))
   2719  1.1  christos 		    (t '(po-find-unknown-string . po-mark-unknown-string)))))
   2720  1.1  christos     (or (boundp 'po-find-string-function)
   2721  1.1  christos 	(set (make-local-variable 'po-find-string-function) (car pair)))
   2722  1.1  christos     (or (boundp 'po-mark-string-function)
   2723  1.1  christos 	(set (make-local-variable 'po-mark-string-function) (cdr pair)))))
   2724  1.1  christos 
   2725  1.1  christos (defun po-find-unknown-string (keywords)
   2726  1.1  christos   "Dummy function to skip over a file, finding no string in it."
   2727  1.1  christos   nil)
   2728  1.1  christos 
   2729  1.1  christos (defun po-mark-unknown-string (start end keyword)
   2730  1.1  christos   "Dummy function to mark a given string.  May not be called."
   2731  1.1  christos   (error (_"Dummy function called")))
   2732  1.1  christos 
   2733  1.1  christos ;;; Awk mode specifics.
   2735  1.1  christos 
   2736  1.1  christos (defun po-find-awk-string (keywords)
   2737  1.1  christos   "Find the next Awk string, excluding those marked by any of KEYWORDS.
   2738  1.1  christos Return (CONTENTS START END) for the found string, or nil if none found."
   2739  1.1  christos   (let (start end)
   2740  1.1  christos     (while (and (not start)
   2741  1.1  christos 		(re-search-forward "[#/\"]" nil t))
   2742  1.1  christos       (cond ((= (preceding-char) ?#)
   2743  1.1  christos 	     ;; Disregard comments.
   2744  1.1  christos 	     (or (search-forward "\n" nil t)
   2745  1.1  christos 		 (goto-char (point-max))))
   2746  1.1  christos 	    ((= (preceding-char) ?/)
   2747  1.1  christos 	     ;; Skip regular expressions.
   2748  1.1  christos 	     (while (not (= (following-char) ?/))
   2749  1.1  christos 	       (skip-chars-forward "^/\\\\")
   2750  1.1  christos 	       (if (= (following-char) ?\\) (forward-char 2)))
   2751  1.1  christos 	     (forward-char 1))
   2752  1.1  christos 	    ;; Else find the end of the string.
   2753  1.1  christos 	    (t (setq start (1- (point)))
   2754  1.1  christos 	       (while (not (= (following-char) ?\"))
   2755  1.1  christos 		 (skip-chars-forward "^\"\\\\")
   2756  1.1  christos 		 (if (= (following-char) ?\\) (forward-char 2)))
   2757  1.1  christos 	       (forward-char 1)
   2758  1.1  christos 	       (setq end (point))
   2759  1.1  christos 	       ;; Check before string either for underline, or for keyword
   2760  1.1  christos 	       ;; and opening parenthesis.
   2761  1.1  christos 	       (save-excursion
   2762  1.1  christos 		 (goto-char start)
   2763  1.1  christos 		 (cond ((= (preceding-char) ?_)
   2764  1.1  christos 			;; Disregard already marked strings.
   2765  1.1  christos 			(setq start nil
   2766  1.1  christos 			      end nil))
   2767  1.1  christos 		       ((= (preceding-char) ?\()
   2768  1.1  christos 			(backward-char 1)
   2769  1.1  christos 			(let ((end-keyword (point)))
   2770  1.1  christos 			  (skip-chars-backward "_A-Za-z0-9")
   2771  1.1  christos 			  (if (member (list (po-buffer-substring
   2772  1.1  christos 					     (point) end-keyword))
   2773  1.1  christos 				      keywords)
   2774  1.1  christos 			      ;; Disregard already marked strings.
   2775  1.1  christos 			      (setq start nil
   2776  1.1  christos 				    end nil)))))))))
   2777  1.1  christos     (and start end
   2778  1.1  christos 	 (list (po-extract-unquoted (current-buffer) start end) start end))))
   2779  1.1  christos 
   2780  1.1  christos (defun po-mark-awk-string (start end keyword)
   2781  1.1  christos   "Mark the Awk string, from START to END, with KEYWORD.
   2782  1.1  christos Leave point after marked string."
   2783  1.1  christos   (if (string-equal keyword "_")
   2784  1.1  christos       (progn
   2785  1.1  christos 	(goto-char start)
   2786  1.1  christos 	(insert "_")
   2787  1.1  christos 	(goto-char (1+ end)))
   2788  1.1  christos     (goto-char end)
   2789  1.1  christos     (insert ")")
   2790  1.1  christos     (save-excursion
   2791  1.1  christos       (goto-char start)
   2792  1.1  christos       (insert keyword "("))))
   2793  1.1  christos 
   2794  1.1  christos ;;; Bash mode specifics.
   2796  1.1  christos 
   2797  1.1  christos (defun po-find-bash-string (keywords)
   2798  1.1  christos   "Find the next unmarked Bash string.  KEYWORDS are merely ignored.
   2799  1.1  christos Return (CONTENTS START END) for the found string, or nil if none found."
   2800  1.1  christos   (let (start end)
   2801  1.1  christos     (while (and (not start)
   2802  1.1  christos 		(re-search-forward "[#'\"]" nil t))
   2803  1.1  christos       (cond ((= (preceding-char) ?#)
   2804  1.1  christos 	     ;; Disregard comments.
   2805  1.1  christos 	     (or (search-forward "\n" nil t)
   2806  1.1  christos 		 (goto-char (point-max))))
   2807  1.1  christos 	    ((= (preceding-char) ?')
   2808  1.1  christos 	     ;; Skip single quoted strings.
   2809  1.1  christos 	     (while (not (= (following-char) ?'))
   2810  1.1  christos 	       (skip-chars-forward "^'\\\\")
   2811  1.1  christos 	       (if (= (following-char) ?\\) (forward-char 2)))
   2812  1.1  christos 	     (forward-char 1))
   2813  1.1  christos 	    ;; Else find the end of the double quoted string.
   2814  1.1  christos 	    (t (setq start (1- (point)))
   2815  1.1  christos 	       (while (not (= (following-char) ?\"))
   2816  1.1  christos 		 (skip-chars-forward "^\"\\\\")
   2817  1.1  christos 		 (if (= (following-char) ?\\) (forward-char 2)))
   2818  1.1  christos 	       (forward-char 1)
   2819  1.1  christos 	       (setq end (point))
   2820  1.1  christos 	       ;; Check before string for dollar sign.
   2821  1.1  christos 	       (save-excursion
   2822  1.1  christos 		 (goto-char start)
   2823  1.1  christos 		 (if (= (preceding-char) ?$)
   2824  1.1  christos 		     ;; Disregard already marked strings.
   2825  1.1  christos 		     (setq start nil
   2826  1.1  christos 			   end nil))))))
   2827  1.1  christos     (and start end
   2828  1.1  christos 	 (list (po-extract-unquoted (current-buffer) start end) start end))))
   2829  1.1  christos 
   2830  1.1  christos (defun po-mark-bash-string (start end keyword)
   2831  1.1  christos   "Mark the Bash string, from START to END, with '$'.  KEYWORD is ignored.
   2832  1.1  christos Leave point after marked string."
   2833  1.1  christos   (goto-char start)
   2834  1.1  christos   (insert "$")
   2835  1.1  christos   (goto-char (1+ end)))
   2836  1.1  christos 
   2837  1.1  christos ;;; C or C++ mode specifics.
   2839  1.1  christos 
   2840  1.1  christos ;;; A few long string cases (submitted by Ben Pfaff).
   2841  1.1  christos 
   2842  1.1  christos ;; #define string "This is a long string " \
   2843  1.1  christos ;; "that is continued across several lines " \
   2844  1.1  christos ;; "in a macro in order to test \\ quoting\\" \
   2845  1.1  christos ;; "\\ with goofy strings.\\"
   2846  1.1  christos 
   2847  1.1  christos ;; char *x = "This is just an ordinary string "
   2848  1.1  christos ;; "continued across several lines without needing "
   2849  1.1  christos ;; "to use \\ characters at end-of-line.";
   2850  1.1  christos 
   2851  1.1  christos ;; char *y = "Here is a string continued across \
   2852  1.1  christos ;; several lines in the manner that was sanctioned \
   2853  1.1  christos ;; in K&R C compilers and still works today, \
   2854  1.1  christos ;; even though the method used above is more esthetic.";
   2855  1.1  christos 
   2856  1.1  christos ;;; End of long string cases.
   2857  1.1  christos 
   2858  1.1  christos (defun po-find-c-string (keywords)
   2859  1.1  christos   "Find the next C string, excluding those marked by any of KEYWORDS.
   2860  1.1  christos Returns (CONTENTS START END) for the found string, or nil if none found."
   2861  1.1  christos   (let (start end)
   2862  1.1  christos     (while (and (not start)
   2863  1.1  christos 		(re-search-forward "\\([\"']\\|/\\*\\|//\\)" nil t))
   2864  1.1  christos       (cond ((= (preceding-char) ?*)
   2865  1.1  christos 	     ;; Disregard comments.
   2866  1.1  christos 	     (search-forward "*/"))
   2867  1.1  christos 	    ((= (preceding-char) ?/)
   2868  1.1  christos 	     ;; Disregard C++ comments.
   2869  1.1  christos 	     (end-of-line)
   2870  1.1  christos 	     (forward-char 1))
   2871  1.1  christos 	    ((= (preceding-char) ?\')
   2872  1.1  christos 	     ;; Disregard character constants.
   2873  1.1  christos 	     (forward-char (if (= (following-char) ?\\) 3 2)))
   2874  1.1  christos 	    ((save-excursion
   2875  1.1  christos 	       (beginning-of-line)
   2876  1.1  christos 	       (looking-at "^# *\\(include\\|line\\)"))
   2877  1.1  christos 	     ;; Disregard lines being #include or #line directives.
   2878  1.1  christos 	     (end-of-line))
   2879  1.1  christos 	    ;; Else, find the end of the (possibly concatenated) string.
   2880  1.1  christos 	    (t (setq start (1- (point))
   2881  1.1  christos 		     end nil)
   2882  1.1  christos 	       (while (not end)
   2883  1.1  christos 		 (cond ((= (following-char) ?\")
   2884  1.1  christos 			(if (looking-at "\"[ \t\n\\\\]*\"")
   2885  1.1  christos 			    (goto-char (match-end 0))
   2886  1.1  christos 			  (forward-char 1)
   2887  1.1  christos 			  (setq end (point))))
   2888  1.1  christos 		       ((= (following-char) ?\\) (forward-char 2))
   2889  1.1  christos 		       (t (skip-chars-forward "^\"\\\\"))))
   2890  1.1  christos 	       ;; Check before string for keyword and opening parenthesis.
   2891  1.1  christos 	       (goto-char start)
   2892  1.1  christos 	       (skip-chars-backward " \n\t")
   2893  1.1  christos 	       (if (= (preceding-char) ?\()
   2894  1.1  christos 		   (progn
   2895  1.1  christos 		     (backward-char 1)
   2896  1.1  christos 		     (skip-chars-backward " \n\t")
   2897  1.1  christos 		     (let ((end-keyword (point)))
   2898  1.1  christos 		       (skip-chars-backward "_A-Za-z0-9")
   2899  1.1  christos 		       (if (member (list (po-buffer-substring (point)
   2900  1.1  christos 							      end-keyword))
   2901  1.1  christos 				   keywords)
   2902  1.1  christos 			   ;; Disregard already marked strings.
   2903  1.1  christos 			   (progn
   2904  1.1  christos 			     (goto-char end)
   2905  1.1  christos 			     (setq start nil
   2906  1.1  christos 				   end nil))
   2907  1.1  christos 			 ;; String found.  Prepare to resume search.
   2908  1.1  christos 			 (goto-char end))))
   2909  1.1  christos 		 ;; String found.  Prepare to resume search.
   2910  1.1  christos 		 (goto-char end)))))
   2911  1.1  christos     ;; Return the found string, if any.
   2912  1.1  christos     (and start end
   2913  1.1  christos 	 (list (po-extract-unquoted (current-buffer) start end) start end))))
   2914  1.1  christos 
   2915  1.1  christos (defun po-mark-c-string (start end keyword)
   2916  1.1  christos   "Mark the C string, from START to END, with KEYWORD.
   2917  1.1  christos Leave point after marked string."
   2918  1.1  christos   (goto-char end)
   2919  1.1  christos   (insert ")")
   2920  1.1  christos   (save-excursion
   2921  1.1  christos     (goto-char start)
   2922  1.1  christos     (insert keyword)
   2923  1.1  christos     (or (string-equal keyword "_") (insert " "))
   2924  1.1  christos     (insert "(")))
   2925  1.1  christos 
   2926  1.1  christos ;;; Emacs LISP mode specifics.
   2928  1.1  christos 
   2929  1.1  christos (defun po-find-emacs-lisp-string (keywords)
   2930  1.1  christos   "Find the next Emacs LISP string, excluding those marked by any of KEYWORDS.
   2931  1.1  christos Returns (CONTENTS START END) for the found string, or nil if none found."
   2932  1.1  christos   (let (start end)
   2933  1.1  christos     (while (and (not start)
   2934  1.1  christos 		(re-search-forward "[;\"?]" nil t))
   2935  1.1  christos       (cond ((= (preceding-char) ?\;)
   2936  1.1  christos 	     ;; Disregard comments.
   2937  1.1  christos 	     (search-forward "\n"))
   2938  1.1  christos 	    ((= (preceding-char) ?\?)
   2939  1.1  christos 	     ;; Disregard character constants.
   2940  1.1  christos 	     (forward-char (if (= (following-char) ?\\) 2 1)))
   2941  1.1  christos 	    ;; Else, find the end of the string.
   2942  1.1  christos 	    (t (setq start (1- (point)))
   2943  1.1  christos 	       (while (not (= (following-char) ?\"))
   2944  1.1  christos 		 (skip-chars-forward "^\"\\\\")
   2945  1.1  christos 		 (if (= (following-char) ?\\) (forward-char 2)))
   2946  1.1  christos 	       (forward-char 1)
   2947  1.1  christos 	       (setq end (point))
   2948  1.1  christos 	       ;; Check before string for keyword and opening parenthesis.
   2949  1.1  christos 	       (goto-char start)
   2950  1.1  christos 	       (skip-chars-backward " \n\t")
   2951  1.1  christos 	       (let ((end-keyword (point)))
   2952  1.1  christos 		 (skip-chars-backward "-_A-Za-z0-9")
   2953  1.1  christos 		 (if (and (= (preceding-char) ?\()
   2954  1.1  christos 			  (member (list (po-buffer-substring (point)
   2955  1.1  christos 							     end-keyword))
   2956  1.1  christos 				  keywords))
   2957  1.1  christos 		     ;; Disregard already marked strings.
   2958  1.1  christos 		     (progn
   2959  1.1  christos 		       (goto-char end)
   2960  1.1  christos 		       (setq start nil
   2961  1.1  christos 			     end nil)))))))
   2962  1.1  christos     ;; Return the found string, if any.
   2963  1.1  christos     (and start end
   2964  1.1  christos 	 (list (po-extract-unquoted (current-buffer) start end) start end))))
   2965  1.1  christos 
   2966  1.1  christos (defun po-mark-emacs-lisp-string (start end keyword)
   2967  1.1  christos   "Mark the Emacs LISP string, from START to END, with KEYWORD.
   2968  1.1  christos Leave point after marked string."
   2969  1.1  christos   (goto-char end)
   2970  1.1  christos   (insert ")")
   2971  1.1  christos   (save-excursion
   2972  1.1  christos     (goto-char start)
   2973  1.1  christos     (insert "(" keyword)
   2974  1.1  christos     (or (string-equal keyword "_") (insert " "))))
   2975  1.1  christos 
   2976  1.1  christos ;;; Python mode specifics.
   2978  1.1  christos 
   2979  1.1  christos (defun po-find-python-string (keywords)
   2980  1.1  christos   "Find the next Python string, excluding those marked by any of KEYWORDS.
   2981  1.1  christos Also disregard strings when preceded by an empty string of the other type.
   2982  1.1  christos Returns (CONTENTS START END) for the found string, or nil if none found."
   2983  1.1  christos   (let (contents start end)
   2984  1.1  christos     (while (and (not contents)
   2985  1.1  christos 		(re-search-forward "[#\"']" nil t))
   2986  1.1  christos       (forward-char -1)
   2987  1.1  christos       (cond ((= (following-char) ?\#)
   2988  1.1  christos 	     ;; Disregard comments.
   2989  1.1  christos 	     (search-forward "\n"))
   2990  1.1  christos 	    ((looking-at "\"\"'")
   2991  1.1  christos 	     ;; Quintuple-quoted string
   2992  1.1  christos 	     (po-skip-over-python-string))
   2993  1.1  christos 	    ((looking-at "''\"")
   2994  1.1  christos 	     ;; Quadruple-quoted string
   2995  1.1  christos 	     (po-skip-over-python-string))
   2996  1.1  christos 	    (t
   2997  1.1  christos 	     ;; Simple-, double-, triple- or sextuple-quoted string.
   2998  1.1  christos 	     (if (memq (preceding-char) '(?r ?R))
   2999  1.1  christos 		 (forward-char -1))
   3000  1.1  christos 	     (setq start (point)
   3001  1.1  christos 		   contents (po-skip-over-python-string)
   3002  1.1  christos 		   end (point))
   3003  1.1  christos 	     (goto-char start)
   3004  1.1  christos 	     (skip-chars-backward " \n\t")
   3005  1.1  christos 	     (cond ((= (preceding-char) ?\[)
   3006  1.1  christos 		    ;; Disregard a string used as a dictionary index.
   3007  1.1  christos 		    (setq contents nil))
   3008  1.1  christos 		   ((= (preceding-char) ?\()
   3009  1.1  christos 		    ;; Isolate the keyword which precedes string.
   3010  1.1  christos 		    (backward-char 1)
   3011  1.1  christos 		    (skip-chars-backward " \n\t")
   3012  1.1  christos 		    (let ((end-keyword (point)))
   3013  1.1  christos 		      (skip-chars-backward "_A-Za-z0-9")
   3014  1.1  christos 		      (if (member (list (po-buffer-substring (point)
   3015  1.1  christos 							     end-keyword))
   3016  1.1  christos 				  keywords)
   3017  1.1  christos 			  ;; Disregard already marked strings.
   3018  1.1  christos 			  (setq contents nil)))))
   3019  1.1  christos 	     (goto-char end))))
   3020  1.1  christos     ;; Return the found string, if any.
   3021  1.1  christos     (and contents (list contents start end))))
   3022  1.1  christos 
   3023  1.1  christos (defun po-skip-over-python-string ()
   3024  1.1  christos   "Skip over a Python string, possibly made up of many concatenated parts.
   3025  1.1  christos Leave point after string.  Return unquoted overall string contents."
   3026  1.1  christos   (let ((continue t)
   3027  1.1  christos 	(contents "")
   3028  1.1  christos 	raw start end resume)
   3029  1.1  christos     (while continue
   3030  1.1  christos       (skip-chars-forward " \t\n")	; whitespace
   3031  1.1  christos       (cond ((= (following-char) ?#)	; comment
   3032  1.1  christos 	     (setq start nil)
   3033  1.1  christos 	     (search-forward "\n"))
   3034  1.1  christos 	    ((looking-at "\\\n")	; escaped newline
   3035  1.1  christos 	     (setq start nil)
   3036  1.1  christos 	     (forward-char 2))
   3037  1.1  christos 	    ((looking-at "[rR]?\"\"\"")	; sextuple-quoted string
   3038  1.1  christos 	     (setq raw (memq (following-char) '(?r ?R))
   3039  1.1  christos 		   start (match-end 0))
   3040  1.1  christos 	     (goto-char start)
   3041  1.1  christos 	     (search-forward "\"\"\"")
   3042  1.1  christos 	     (setq resume (point)
   3043  1.1  christos 		   end (- resume 3)))
   3044  1.1  christos 	    ((looking-at "[rr]?'''")	; triple-quoted string
   3045  1.1  christos 	     (setq raw (memq (following-char) '(?r ?R))
   3046  1.1  christos 		   start (match-end 0))
   3047  1.1  christos 	     (goto-char start)
   3048  1.1  christos 	     (search-forward "'''")
   3049  1.1  christos 	     (setq resume (point)
   3050  1.1  christos 		   end (- resume 3)))
   3051  1.1  christos 	    ((looking-at "[rR]?\"")	; double-quoted string
   3052  1.1  christos 	     (setq raw (memq (following-char) '(?r ?R))
   3053  1.1  christos 		   start (match-end 0))
   3054  1.1  christos 	     (goto-char start)
   3055  1.1  christos 	     (while (not (memq (following-char) '(0 ?\")))
   3056  1.1  christos 	       (skip-chars-forward "^\"\\\\")
   3057  1.1  christos 	       (if (= (following-char) ?\\) (forward-char 2)))
   3058  1.1  christos 	     (if (eobp)
   3059  1.1  christos 		 (setq contents nil
   3060  1.1  christos 		       start nil)
   3061  1.1  christos 	       (setq end (point))
   3062  1.1  christos 	       (forward-char 1))
   3063  1.1  christos 	     (setq resume (point)))
   3064  1.1  christos 	    ((looking-at "[rR]?'")	; single-quoted string
   3065  1.1  christos 	     (setq raw (memq (following-char) '(?r ?R))
   3066  1.1  christos 		   start (match-end 0))
   3067  1.1  christos 	     (goto-char start)
   3068  1.1  christos 	     (while (not (memq (following-char) '(0 ?\')))
   3069  1.1  christos 	       (skip-chars-forward "^'\\\\")
   3070  1.1  christos 	       (if (= (following-char) ?\\) (forward-char 2)))
   3071  1.1  christos 	     (if (eobp)
   3072  1.1  christos 		 (setq contents nil
   3073  1.1  christos 		       start nil)
   3074  1.1  christos 	       (setq end (point))
   3075  1.1  christos 	       (forward-char 1))
   3076  1.1  christos 	     (setq resume (point)))
   3077  1.1  christos 	    (t				; no string anymore
   3078  1.1  christos 	     (setq start nil
   3079  1.1  christos 		   continue nil)))
   3080  1.1  christos       (if start
   3081  1.1  christos 	  (setq contents (concat contents
   3082  1.1  christos 				 (if raw
   3083  1.1  christos 				     (buffer-substring start end)
   3084  1.1  christos 				   (po-extract-part-unquoted (current-buffer)
   3085  1.1  christos 							     start end))))))
   3086  1.1  christos     (goto-char resume)
   3087  1.1  christos     contents))
   3088  1.1  christos 
   3089  1.1  christos (defun po-mark-python-string (start end keyword)
   3090  1.1  christos   "Mark the Python string, from START to END, with KEYWORD.
   3091  1.1  christos If KEYWORD is '.', prefix the string with an empty string of the other type.
   3092  1.1  christos Leave point after marked string."
   3093  1.1  christos   (cond ((string-equal keyword ".")
   3094  1.1  christos 	 (goto-char end)
   3095  1.1  christos 	 (save-excursion
   3096  1.1  christos 	   (goto-char start)
   3097  1.1  christos 	   (insert (cond ((= (following-char) ?\') "\"\"")
   3098  1.1  christos 			 ((= (following-char) ?\") "''")
   3099  1.1  christos 			 (t "??")))))
   3100  1.1  christos 	(t (goto-char end)
   3101  1.1  christos 	   (insert ")")
   3102  1.1  christos 	   (save-excursion
   3103  1.1  christos 	     (goto-char start)
   3104  1.1  christos 	     (insert keyword "(")))))
   3105  1.1  christos 
   3106  1.1  christos ;;; Miscellaneous features.
   3108  1.1  christos 
   3109  1.1  christos (defun po-help ()
   3110  1.1  christos   "Provide an help window for PO mode."
   3111  1.1  christos   (interactive)
   3112  1.1  christos   (po-with-temp-buffer
   3113  1.1  christos    (insert po-help-display-string)
   3114  1.1  christos    (goto-char (point-min))
   3115  1.1  christos    (save-window-excursion
   3116  1.1  christos      (switch-to-buffer (current-buffer))
   3117  1.1  christos      (delete-other-windows)
   3118  1.1  christos      (message (_"Type any character to continue"))
   3119  1.1  christos      (po-read-event))))
   3120  1.1  christos 
   3121  1.1  christos (defun po-undo ()
   3122  1.1  christos   "Undo the last change to the PO file."
   3123  1.1  christos   (interactive)
   3124  1.1  christos   (let ((buffer-read-only po-read-only))
   3125  1.1  christos     (undo))
   3126  1.1  christos   (po-compute-counters nil))
   3127  1.1  christos 
   3128  1.1  christos (defun po-statistics ()
   3129  1.1  christos   "Say how many entries in each category, and the current position."
   3130  1.1  christos   (interactive)
   3131  1.1  christos   (po-compute-counters t))
   3132  1.1  christos 
   3133  1.1  christos (defun po-validate ()
   3134  1.1  christos   "Use 'msgfmt' for validating the current PO file contents."
   3135  1.1  christos   (interactive)
   3136  1.1  christos   ; The 'compile' subsystem is autoloaded through a call to (compile ...).
   3137  1.1  christos   ; We need to initialize it outside of any binding. Without this statement,
   3138  1.1  christos   ; all defcustoms and defvars of compile.el would be undone when the let*
   3139  1.1  christos   ; terminates.
   3140  1.1  christos   (require 'compile)
   3141  1.1  christos   (let* ((dev-null
   3142  1.1  christos 	  (cond ((boundp 'null-device) null-device) ; since Emacs 20.3
   3143  1.1  christos 		((memq system-type '(windows-nt windows-95)) "NUL")
   3144  1.1  christos 		(t "/dev/null")))
   3145  1.1  christos 	 (compilation-buffer-name-function
   3146  1.1  christos 	  (function (lambda (mode-name)
   3147  1.1  christos 		      (concat "*" mode-name " validation*"))))
   3148  1.1  christos 	 (compile-command (concat po-msgfmt-program
   3149  1.1  christos                                  " --statistics -c -v -o " dev-null " "
   3150  1.1  christos                                  buffer-file-name)))
   3151  1.1  christos     (po-msgfmt-version-check)
   3152  1.1  christos     (compile compile-command)))
   3153  1.1  christos 
   3154  1.1  christos (defvar po-msgfmt-version-checked nil)
   3155  1.1  christos (defun po-msgfmt-version-check ()
   3156  1.1  christos   "'msgfmt' from GNU gettext 0.10.36 or greater is required."
   3157  1.1  christos   (po-with-temp-buffer
   3158  1.1  christos     (or
   3159  1.1  christos      ;; Don't bother checking again.
   3160  1.1  christos      po-msgfmt-version-checked
   3161  1.1  christos 
   3162  1.1  christos      (and
   3163  1.1  christos       ;; Make sure 'msgfmt' is available.
   3164  1.1  christos       (condition-case nil
   3165  1.1  christos           (call-process po-msgfmt-program
   3166  1.1  christos 			nil t nil "--verbose" "--version")
   3167  1.1  christos 	(file-error nil))
   3168  1.1  christos 
   3169  1.1  christos       ;; Make sure there's a version number in the output:
   3170  1.1  christos       ;; 0.11 or 0.10.36 or 0.11-pre1
   3171  1.1  christos       (progn (goto-char (point-min))
   3172  1.1  christos              (or (looking-at ".* \\([0-9]+\\)\\.\\([0-9]+\\)$")
   3173  1.1  christos                  (looking-at ".* \\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)$")
   3174  1.1  christos 		 (looking-at ".* \\([0-9]+\\)\\.\\([0-9]+\\)[-_A-Za-z0-9]+$")))
   3175  1.1  christos 
   3176  1.1  christos       ;; Make sure the version is recent enough.
   3177  1.1  christos       (>= (string-to-number
   3178  1.1  christos 	   (format "%d%03d%03d"
   3179  1.1  christos 		   (string-to-number (match-string 1))
   3180  1.1  christos 		   (string-to-number (match-string 2))
   3181  1.1  christos 		   (string-to-number (or (match-string 3) "0"))))
   3182  1.1  christos 	  010036)
   3183  1.1  christos 
   3184  1.1  christos       ;; Remember the outcome.
   3185  1.1  christos       (setq po-msgfmt-version-checked t))
   3186  1.1  christos 
   3187  1.1  christos      (error (_"'msgfmt' from GNU gettext 0.10.36 or greater is required")))))
   3188  1.1  christos 
   3189  1.1  christos (defun po-guess-archive-name ()
   3190  1.1  christos   "Return the ideal file name for this PO file in the central archives."
   3191  1.1  christos   (let ((filename (file-name-nondirectory buffer-file-name))
   3192  1.1  christos 	start-of-header end-of-header package version team)
   3193  1.1  christos     (save-excursion
   3194  1.1  christos       ;; Find the PO file header entry.
   3195  1.1  christos       (goto-char (point-min))
   3196  1.1  christos       (re-search-forward po-any-msgstr-regexp)
   3197  1.1  christos       (setq start-of-header (match-beginning 0)
   3198  1.1  christos 	    end-of-header (match-end 0))
   3199  1.1  christos       ;; Get the package and version.
   3200  1.1  christos       (goto-char start-of-header)
   3201  1.1  christos       (if (re-search-forward "\n\
   3202  1.1  christos \"Project-Id-Version: \\(GNU \\|Free \\)?\\([^\n ]+\\) \\([^\n ]+\\)\\\\n\"$"
   3203  1.1  christos 	   end-of-header t)
   3204  1.1  christos 	  (setq package (po-match-string 2)
   3205  1.1  christos 		version (po-match-string 3)))
   3206  1.1  christos       (if (or (not package) (string-equal package "PACKAGE")
   3207  1.1  christos 	      (not version) (string-equal version "VERSION"))
   3208  1.1  christos 	  (error (_"Project-Id-Version field does not have a proper value")))
   3209  1.1  christos       ;; File name version and Project-Id-Version must match
   3210  1.1  christos       (cond (;; A `filename' w/o package and version info at all
   3211  1.1  christos 	     (string-match "^[^\\.]*\\.po\\'" filename))
   3212  1.1  christos 	    (;; TP Robot compatible `filename': PACKAGE-VERSION.LL.po
   3213  1.1  christos 	     (string-match (concat (regexp-quote package)
   3214  1.1  christos 				   "-\\(.*\\)\\.[^\\.]*\\.po\\'") filename)
   3215  1.1  christos 	     (if (not (equal version (po-match-string 1 filename)))
   3216  1.1  christos 		 (error (_"\
   3217  1.1  christos Version mismatch: file name: %s; header: %s.\n\
   3218  1.1  christos Adjust Project-Id-Version field to match file name and try again")
   3219  1.1  christos 			(po-match-string 1 filename) version))))
   3220  1.1  christos       ;; Get the team.
   3221  1.1  christos       (if (stringp po-team-name-to-code)
   3222  1.1  christos 	  (setq team po-team-name-to-code)
   3223  1.1  christos 	(goto-char start-of-header)
   3224  1.1  christos 	(if (re-search-forward "\n\
   3225  1.1  christos \"Language-Team: \\([^ ].*[^ ]\\) <.+@.+>\\\\n\"$"
   3226  1.1  christos 			       end-of-header t)
   3227  1.1  christos 	    (let ((name (po-match-string 1)))
   3228  1.1  christos 	      (if name
   3229  1.1  christos 		  (let ((pair (assoc name po-team-name-to-code)))
   3230  1.1  christos 		    (if pair
   3231  1.1  christos 			(setq team (cdr pair))
   3232  1.1  christos 		      (setq team (read-string (format "\
   3233  1.1  christos Team name '%s' unknown.  What is the team code? "
   3234  1.1  christos 						      name)))))))))
   3235  1.1  christos       (if (or (not team) (string-equal team "LL"))
   3236  1.1  christos 	  (error (_"Language-Team field does not have a proper value")))
   3237  1.1  christos       ;; Compose the name.
   3238  1.1  christos       (concat package "-" version "." team ".po"))))
   3239  1.1  christos 
   3240  1.1  christos (defun po-guess-team-address ()
   3241  1.1  christos   "Return the team address related to this PO file."
   3242  1.1  christos   (let (team)
   3243  1.1  christos     (save-excursion
   3244  1.1  christos       (goto-char (point-min))
   3245  1.1  christos       (re-search-forward po-any-msgstr-regexp)
   3246  1.1  christos       (goto-char (match-beginning 0))
   3247  1.1  christos       (if (re-search-forward
   3248  1.1  christos 	   "\n\"Language-Team: +\\(.*<\\(.*\\)@.*>\\)\\\\n\"$"
   3249  1.1  christos 	   (match-end 0) t)
   3250  1.1  christos 	  (setq team (po-match-string 2)))
   3251  1.1  christos       (if (or (not team) (string-equal team "LL"))
   3252  1.1  christos 	  (error (_"Language-Team field does not have a proper value")))
   3253  1.1  christos       (po-match-string 1))))
   3254  1.1  christos 
   3255  1.1  christos (defun po-send-mail ()
   3256  1.1  christos   "Start composing a letter, possibly including the current PO file."
   3257  1.1  christos   (interactive)
   3258  1.1  christos   (let* ((team-flag (y-or-n-p
   3259  1.1  christos 		     (_"\
   3260  1.1  christos Write to your team?  ('n' if writing to the Translation Project robot) ")))
   3261  1.1  christos 	 (address (if team-flag
   3262  1.1  christos 		      (po-guess-team-address)
   3263  1.1  christos 		    po-translation-project-address)))
   3264  1.1  christos     (if (not (y-or-n-p (_"Include current PO file in mail? ")))
   3265  1.1  christos 	(apply po-compose-mail-function address
   3266  1.1  christos 	       (read-string (_"Subject? ")) nil)
   3267  1.1  christos       (if (buffer-modified-p)
   3268  1.1  christos 	  (error (_"The file is not even saved, you did not validate it.")))
   3269  1.1  christos       (if (and (y-or-n-p (_"You validated ('V') this file, didn't you? "))
   3270  1.1  christos 	       (or (zerop po-untranslated-counter)
   3271  1.1  christos 		   (y-or-n-p
   3272  1.1  christos 		    (format (_"%d entries are untranslated, include anyway? ")
   3273  1.1  christos 			    po-untranslated-counter)))
   3274  1.1  christos 	       (or (zerop po-fuzzy-counter)
   3275  1.1  christos 		   (y-or-n-p
   3276  1.1  christos 		    (format (_"%d entries are still fuzzy, include anyway? ")
   3277  1.1  christos 			    po-fuzzy-counter)))
   3278  1.1  christos 	       (or (zerop po-obsolete-counter)
   3279  1.1  christos 		   (y-or-n-p
   3280  1.1  christos 		    (format (_"%d entries are obsolete, include anyway? ")
   3281  1.1  christos 			    po-obsolete-counter))))
   3282  1.1  christos 	  (let ((buffer (current-buffer))
   3283  1.1  christos 		(name (po-guess-archive-name))
   3284  1.1  christos 		(transient-mark-mode nil)
   3285  1.1  christos 		(coding-system-for-read buffer-file-coding-system)
   3286  1.1  christos 		(coding-system-for-write buffer-file-coding-system))
   3287  1.1  christos 	    (apply po-compose-mail-function address
   3288  1.1  christos 		   (if team-flag
   3289  1.1  christos 		       (read-string (_"Subject? "))
   3290  1.1  christos 		     (format "%s %s" po-translation-project-mail-label name))
   3291  1.1  christos 		   nil)
   3292  1.1  christos 	    (goto-char (point-min))
   3293  1.1  christos 	    (re-search-forward
   3294  1.1  christos 	     (concat "^" (regexp-quote mail-header-separator) "\n"))
   3295  1.1  christos 	    (save-excursion
   3296  1.1  christos 	      (insert-buffer buffer)
   3297  1.1  christos 	      (shell-command-on-region
   3298  1.1  christos 	       (region-beginning) (region-end)
   3299  1.1  christos 	       (concat po-gzip-uuencode-command " " name ".gz") t))))))
   3300  1.1  christos   (message ""))
   3301  1.1  christos 
   3302  1.1  christos (defun po-confirm-and-quit ()
   3303  1.1  christos   "Confirm if quit should be attempted and then, do it.
   3304  1.1  christos This is a failsafe.  Confirmation is asked if only the real quit would not."
   3305  1.1  christos   (interactive)
   3306  1.1  christos   (if (po-check-all-pending-edits)
   3307  1.1  christos       (progn
   3308  1.1  christos 	(if (or (buffer-modified-p)
   3309  1.1  christos 		(> po-untranslated-counter 0)
   3310  1.1  christos 		(> po-fuzzy-counter 0)
   3311  1.1  christos 		(> po-obsolete-counter 0)
   3312  1.1  christos 		(y-or-n-p (_"Really quit editing this PO file? ")))
   3313  1.1  christos 	    (po-quit))
   3314  1.1  christos 	(message ""))))
   3315  1.1  christos 
   3316  1.1  christos (defun po-quit ()
   3317  1.1  christos   "Save the PO file and kill buffer.
   3318  1.1  christos However, offer validation if appropriate and ask confirmation if untranslated
   3319  1.1  christos strings remain."
   3320  1.1  christos   (interactive)
   3321  1.1  christos   (if (po-check-all-pending-edits)
   3322  1.1  christos       (let ((quit t))
   3323  1.1  christos 	;; Offer validation of newly modified entries.
   3324  1.1  christos 	(if (and (buffer-modified-p)
   3325  1.1  christos 		 (not (y-or-n-p
   3326  1.1  christos 		       (_"File was modified; skip validation step? "))))
   3327  1.1  christos 	    (progn
   3328  1.1  christos 	      (message "")
   3329  1.1  christos 	      (po-validate)
   3330  1.1  christos 	      ;; If we knew that the validation was all successful, we should
   3331  1.1  christos 	      ;; just quit.  But since we do not know yet, as the validation
   3332  1.1  christos 	      ;; might be asynchronous with PO mode commands, the safest is to
   3333                	      ;; stay within PO mode, even if this implies that another
   3334                	      ;; 'po-quit' command will be later required to exit for true.
   3335                	      (setq quit nil)))
   3336                	;; Offer to work on untranslated entries.
   3337                	(if (and quit
   3338                		 (or (> po-untranslated-counter 0)
   3339                		     (> po-fuzzy-counter 0)
   3340                		     (> po-obsolete-counter 0))
   3341                		 (not (y-or-n-p
   3342                		       (_"Unprocessed entries remain; quit anyway? "))))
   3343                	    (progn
   3344                	      (setq quit nil)
   3345                	      (po-auto-select-entry)))
   3346                	;; Clear message area.
   3347                	(message "")
   3348                	;; Or else, kill buffers and quit for true.
   3349                	(if quit
   3350                	    (progn
   3351                	      (save-buffer)
   3352                	      (kill-buffer (current-buffer)))))))
   3353                
   3354                (provide 'po-mode)
   3355                
   3356                ;;; po-mode.el ends here
   3357