home *** CD-ROM | disk | FTP | other *** search
/ vim.ftp.fu-berlin.de / 2015-02-03.vim.ftp.fu-berlin.de.tar / vim.ftp.fu-berlin.de / patches / 7.3 / 7.3.957 < prev    next >
Encoding:
Internet Message Format  |  2013-05-14  |  9.7 KB

  1. To: vim_dev@googlegroups.com
  2. Subject: Patch 7.3.957
  3. Fcc: outbox
  4. From: Bram Moolenaar <Bram@moolenaar.net>
  5. Mime-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. ------------
  9.  
  10. Patch 7.3.957
  11. Problem:    Python does not have a "do" command like Perl or Lua.
  12. Solution:   Add the ":py3do" command. (Lilydjwg)
  13. Files:        runtime/doc/if_pyth.txt, src/ex_cmds.h, src/ex_docmd.c,
  14.         src/if_python3.c, src/proto/if_python3.pro
  15.  
  16.  
  17. *** ../vim-7.3.956/runtime/doc/if_pyth.txt    2013-05-15 15:51:03.000000000 +0200
  18. --- runtime/doc/if_pyth.txt    2013-05-15 18:04:17.000000000 +0200
  19. ***************
  20. *** 490,495 ****
  21. --- 490,510 ----
  22.   <                            *:py3file*
  23.   The |:py3file| command works similar to |:pyfile|.
  24.   
  25. +                             *:py3do*
  26. + :[range]py3do {body}    Execute Python function "def _vim_pydo(line, linenr):
  27. +             {body}" for each line in the [range], with the
  28. +             function arguments being set to the text of each line
  29. +             in turn, without a trailing <EOL>, and the current
  30. +             line number. The function should return a string or
  31. +             None. If a string is returned, it becomes the text of
  32. +             the line in the current turn. The default for [range]
  33. +             is the whole file: "1,$".
  34. +             {not in Vi}
  35. + Examples:
  36. + >
  37. +     :py3do return "%s\t%d" % (line[::-1], len(line))
  38. +     :py3do if line: return "%4d: %s" % (linenr, line)
  39.   
  40.   Vim can be built in four ways (:version output):
  41.   1. No Python support        (-python, -python3)
  42. *** ../vim-7.3.956/src/ex_cmds.h    2012-11-14 20:52:22.000000000 +0100
  43. --- src/ex_cmds.h    2013-05-15 18:01:55.000000000 +0200
  44. ***************
  45. *** 743,748 ****
  46. --- 743,750 ----
  47.               RANGE|FILE1|NEEDARG|CMDWIN),
  48.   EX(CMD_py3,        "py3",        ex_py3,
  49.               RANGE|EXTRA|NEEDARG|CMDWIN),
  50. + EX(CMD_py3do,        "py3do",    ex_py3do,
  51. +             RANGE|DFLALL|EXTRA|NEEDARG|CMDWIN),
  52.   EX(CMD_python3,        "python3",    ex_py3,
  53.               RANGE|EXTRA|NEEDARG|CMDWIN),
  54.   EX(CMD_py3file,        "py3file",    ex_py3file,
  55. *** ../vim-7.3.956/src/ex_docmd.c    2013-05-07 05:18:15.000000000 +0200
  56. --- src/ex_docmd.c    2013-05-15 18:01:55.000000000 +0200
  57. ***************
  58. *** 272,277 ****
  59. --- 272,278 ----
  60.   #endif
  61.   #ifndef FEAT_PYTHON3
  62.   # define ex_py3            ex_script_ni
  63. + # define ex_py3do        ex_ni
  64.   # define ex_py3file        ex_ni
  65.   #endif
  66.   #ifndef FEAT_TCL
  67. *** ../vim-7.3.956/src/if_python3.c    2013-05-15 17:49:00.000000000 +0200
  68. --- src/if_python3.c    2013-05-15 18:23:30.000000000 +0200
  69. ***************
  70. *** 76,81 ****
  71. --- 76,82 ----
  72.   #else
  73.   # define CODEC_ERROR_HANDLER NULL
  74.   #endif
  75. + #define DOPY_FUNC "_vim_pydo"
  76.   
  77.   /* Python 3 does not support CObjects, always use Capsules */
  78.   #define PY_USE_CAPSULE
  79. ***************
  80. *** 126,131 ****
  81. --- 127,133 ----
  82.   # define PyErr_PrintEx py3_PyErr_PrintEx
  83.   # define PyErr_NoMemory py3_PyErr_NoMemory
  84.   # define PyErr_Occurred py3_PyErr_Occurred
  85. + # define PyErr_PrintEx py3_PyErr_PrintEx
  86.   # define PyErr_SetNone py3_PyErr_SetNone
  87.   # define PyErr_SetString py3_PyErr_SetString
  88.   # define PyErr_SetObject py3_PyErr_SetObject
  89. ***************
  90. *** 148,154 ****
  91.   # define PyTuple_GetItem py3_PyTuple_GetItem
  92.   # define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
  93.   # define PyImport_ImportModule py3_PyImport_ImportModule
  94. - # define PyImport_AddModule py3_PyImport_AddModule
  95.   # define PyObject_Init py3__PyObject_Init
  96.   # define PyDict_New py3_PyDict_New
  97.   # define PyDict_GetItemString py3_PyDict_GetItemString
  98. --- 150,155 ----
  99. ***************
  100. *** 163,168 ****
  101. --- 164,174 ----
  102.   # define PyRun_SimpleString py3_PyRun_SimpleString
  103.   #undef PyRun_String
  104.   # define PyRun_String py3_PyRun_String
  105. + # define PyObject_GetAttrString py3_PyObject_GetAttrString
  106. + # define PyObject_SetAttrString py3_PyObject_SetAttrString
  107. + # define PyObject_CallFunctionObjArgs py3_PyObject_CallFunctionObjArgs
  108. + # define PyEval_GetLocals py3_PyEval_GetLocals
  109. + # define PyEval_GetGlobals py3_PyEval_GetGlobals
  110.   # define PySys_SetObject py3_PySys_SetObject
  111.   # define PySys_SetArgv py3_PySys_SetArgv
  112.   # define PyType_Ready py3_PyType_Ready
  113. ***************
  114. *** 178,183 ****
  115. --- 184,190 ----
  116.   # define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
  117.   # define PyModule_AddObject py3_PyModule_AddObject
  118.   # define PyImport_AppendInittab py3_PyImport_AppendInittab
  119. + # define PyImport_AddModule py3_PyImport_AddModule
  120.   # if PY_VERSION_HEX >= 0x030300f0
  121.   #  undef _PyUnicode_AsString
  122.   #  define _PyUnicode_AsString py3_PyUnicode_AsUTF8
  123. ***************
  124. *** 254,259 ****
  125. --- 261,271 ----
  126.   static void (*py3_PyErr_SetObject)(PyObject *, PyObject *);
  127.   static int (*py3_PyRun_SimpleString)(char *);
  128.   static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
  129. + static PyObject* (*py3_PyObject_GetAttrString)(PyObject *, const char *);
  130. + static PyObject* (*py3_PyObject_SetAttrString)(PyObject *, const char *, PyObject *);
  131. + static PyObject* (*py3_PyObject_CallFunctionObjArgs)(PyObject *, ...);
  132. + static PyObject* (*py3_PyEval_GetGlobals)();
  133. + static PyObject* (*py3_PyEval_GetLocals)();
  134.   static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
  135.   static PyObject* (*py3_PyImport_ImportModule)(const char *);
  136.   static PyObject* (*py3_PyImport_AddModule)(const char *);
  137. ***************
  138. *** 386,391 ****
  139. --- 398,408 ----
  140.       {"PyErr_SetObject", (PYTHON_PROC*)&py3_PyErr_SetObject},
  141.       {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
  142.       {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
  143. +     {"PyObject_GetAttrString", (PYTHON_PROC*)&py3_PyObject_GetAttrString},
  144. +     {"PyObject_SetAttrString", (PYTHON_PROC*)&py3_PyObject_SetAttrString},
  145. +     {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&py3_PyObject_CallFunctionObjArgs},
  146. +     {"PyEval_GetGlobals", (PYTHON_PROC*)&py3_PyEval_GetGlobals},
  147. +     {"PyEval_GetLocals", (PYTHON_PROC*)&py3_PyEval_GetLocals},
  148.       {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
  149.       {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
  150.       {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
  151. ***************
  152. *** 990,995 ****
  153. --- 1007,1106 ----
  154.       DoPy3Command(eap, buffer, NULL);
  155.   }
  156.   
  157. + void ex_py3do(exarg_T *eap)
  158. + {
  159. +     linenr_T        i;
  160. +     const char        *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
  161. +     const char        *s = (const char *) eap->arg;
  162. +     size_t        len;
  163. +     char        *code;
  164. +     int            status;
  165. +     PyObject        *pyfunc, *pymain;
  166. +     PyGILState_STATE    pygilstate;
  167. +     if (Python3_Init())
  168. +     goto theend;
  169. +     if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
  170. +     {
  171. +     EMSG(_("cannot save undo information"));
  172. +     return;
  173. +     }
  174. +     len = strlen(code_hdr) + strlen(s);
  175. +     code = malloc(len + 1);
  176. +     STRCPY(code, code_hdr);
  177. +     STRNCAT(code, s, len + 1);
  178. +     pygilstate = PyGILState_Ensure();
  179. +     status = PyRun_SimpleString(code);
  180. +     vim_free(code);
  181. +     if (status)
  182. +     {
  183. +     EMSG(_("failed to run the code"));
  184. +     return;
  185. +     }
  186. +     status = 0; /* good */
  187. +     pymain = PyImport_AddModule("__main__");
  188. +     pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
  189. +     PyGILState_Release(pygilstate);
  190. +     for (i = eap->line1; i <= eap->line2; i++)
  191. +     {
  192. +     const char *line;
  193. +     PyObject *pyline, *pylinenr, *pyret, *pybytes;
  194. +     line = (char *)ml_get(i);
  195. +     pygilstate = PyGILState_Ensure();
  196. +     pyline = PyUnicode_Decode(line, strlen(line),
  197. +         (char *)ENC_OPT, CODEC_ERROR_HANDLER);
  198. +     pylinenr = PyLong_FromLong(i);
  199. +     pyret = PyObject_CallFunctionObjArgs(pyfunc, pyline, pylinenr, NULL);
  200. +     Py_DECREF(pyline);
  201. +     Py_DECREF(pylinenr);
  202. +     if (!pyret)
  203. +     {
  204. +         PyErr_PrintEx(0);
  205. +         PythonIO_Flush();
  206. +         status = 1;
  207. +         goto out;
  208. +     }
  209. +     if (pyret && pyret != Py_None)
  210. +     {
  211. +         if (!PyUnicode_Check(pyret))
  212. +         {
  213. +         /* TODO: a proper error number */
  214. +         EMSG(_("E000: return value must be an instance of str"));
  215. +         Py_XDECREF(pyret);
  216. +         status = 1;
  217. +         goto out;
  218. +         }
  219. +         pybytes = PyUnicode_AsEncodedString(pyret,
  220. +             (char *)ENC_OPT, CODEC_ERROR_HANDLER);
  221. +         ml_replace(i, (char_u *) PyBytes_AsString(pybytes), 1);
  222. +         Py_DECREF(pybytes);
  223. +         changed();
  224. + #ifdef SYNTAX_HL
  225. +         syn_changed(i); /* recompute syntax hl. for this line */
  226. + #endif
  227. +     }
  228. +     Py_XDECREF(pyret);
  229. +     PythonIO_Flush();
  230. +     PyGILState_Release(pygilstate);
  231. +     }
  232. +     pygilstate = PyGILState_Ensure();
  233. + out:
  234. +     Py_DECREF(pyfunc);
  235. +     PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
  236. +     PyGILState_Release(pygilstate);
  237. +     if (status)
  238. +     return;
  239. +     check_cursor();
  240. +     update_curbuf(NOT_VALID);
  241. + theend:
  242. +     return;
  243. + }
  244.   /******************************************************
  245.    * 2. Python output stream: writes output via [e]msg().
  246.    */
  247. *** ../vim-7.3.956/src/proto/if_python3.pro    2013-05-15 15:12:25.000000000 +0200
  248. --- src/proto/if_python3.pro    2013-05-15 18:01:55.000000000 +0200
  249. ***************
  250. *** 3,8 ****
  251. --- 3,9 ----
  252.   void python3_end __ARGS((void));
  253.   int python3_loaded __ARGS((void));
  254.   void ex_py3 __ARGS((exarg_T *eap));
  255. + void ex_py3do __ARGS((exarg_T *eap));
  256.   void ex_py3file __ARGS((exarg_T *eap));
  257.   void python3_buffer_free __ARGS((buf_T *buf));
  258.   void python3_window_free __ARGS((win_T *win));
  259. *** ../vim-7.3.956/src/version.c    2013-05-15 17:49:00.000000000 +0200
  260. --- src/version.c    2013-05-15 18:16:28.000000000 +0200
  261. ***************
  262. *** 730,731 ****
  263. --- 730,733 ----
  264.   {   /* Add new patch number below this line */
  265. + /**/
  266. +     957,
  267.   /**/
  268.  
  269. -- 
  270. FATHER: We are here today to witness the union of two young people in the
  271.         joyful bond of the holy wedlock.  Unfortunately, one of them, my son
  272.         Herbert, has just fallen to his death.
  273.    [Murmurs from CROWD;  the BRIDE smiles with relief, coughs.]
  274.                  "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD
  275.  
  276.  /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
  277. ///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
  278. \\\  an exciting new programming language -- http://www.Zimbu.org        ///
  279.  \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///
  280.