File: codeguide_emacs.rst

package info (click to toggle)
python-astropy 1.3-8~bpo8%2B2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 44,292 kB
  • sloc: ansic: 160,360; python: 137,322; sh: 11,493; lex: 7,638; yacc: 4,956; xml: 1,796; makefile: 474; cpp: 364
file content (207 lines) | stat: -rw-r--r-- 6,568 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
=============================================
 Emacs setup for following coding guidelines
=============================================

.. _flymake: http://www.emacswiki.org/emacs/FlyMake
.. _pyflakes: http://pypi.python.org/pypi/pyflakes
.. _pycodestyle: http://pypi.python.org/pypi/pycodestyle
.. include:: workflow/known_projects.inc

The Astropy coding guidelines are listed in :doc:`codeguide`. This
document will describe some configuration options for Emacs, that will
help in ensuring that Python code satisfies the guidelines. Emacs can
be configured in several different ways. So instead of providing a drop
in configuration file, only the individual configurations are presented
below.

For this setup we will need flymake_, pyflakes_ and the pycodestyle_ Python
script, in addition to ``python-mode``.

Flymake comes with Emacs 23. The rest can be obtained from their websites,
or can be installed using `pip`_.

Global settings
===============

No tabs
-------

This setting will cause all tabs to be replaced with spaces. The number
of spaces to use is set in the :ref:`basic settings` section below.

.. code-block:: scheme

  ;; Don't use TABS for indentations.
  (setq-default indent-tabs-mode nil)

Maximum number of characters in a line
--------------------------------------

Emacs will automatically insert a new line after "fill-column" number
of columns. PEP8 specifies a maximum of 79, but this can be set to a
smaller value also, for example 72.

.. code-block:: scheme

  ;; Set the number to the number of columns to use.
  (setq-default fill-column 79)

  ;; Add Autofill mode to mode hooks.
  (add-hook 'text-mode-hook 'turn-on-auto-fill)

  ;; Show line number in the mode line.
  (line-number-mode 1)

  ;; Show column number in the mode line.
  (column-number-mode 1)

Syntax highlighting
-------------------

Enable syntax highlighting. This will also highlight lines that form a
region.

.. code-block:: scheme

  (global-font-lock-mode 1)

Python specific settings
========================

.. _`basic settings`:

Basic settings
--------------

Indentation is automatically added. When a tab is pressed it is
replaced with 4 spaces. When backspace is pressed on an empty line, the
cursor will jump to the previous indentation level.

.. code-block:: scheme

  (load-library "python")

  (autoload 'python-mode "python-mode" "Python Mode." t)
  (add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode))
  (add-to-list 'interpreter-mode-alist '("python" . python-mode))

  (setq interpreter-mode-alist
        (cons '("python" . python-mode)
              interpreter-mode-alist)
        python-mode-hook
        '(lambda () (progn
                      (set-variable 'py-indent-offset 4)
                      (set-variable 'indent-tabs-mode nil))))

Highlight the column where a line must stop
-------------------------------------------

The "fill-column" column is highlighted in red. For this to work,
download `column-marker.el
<http://www.emacswiki.org/emacs/column-marker.el>`_ and place it in the
Emacs configuration directory.

.. code-block:: scheme

  ;; Highlight character at "fill-column" position.
  (require 'column-marker)
  (set-face-background 'column-marker-1 "red")
  (add-hook 'python-mode-hook
            (lambda () (interactive)
              (column-marker-1 fill-column)))

Flymake
-------

Flymake will mark lines that do not satisfy syntax requirements in
red. When cursor is on such a line a message is displayed in the
mini-buffer. When mouse pointer is on such a line a "tool tip" message
is also shown.

For flymake to work with `pycodestyle`_ and `pyflakes`_, create an
executable file named `pychecker`_ with the following contents. This
file must be in the system path.

.. code-block:: sh

  #!/bin/bash

  pyflakes "$1"
  pycodestyle --ignore=E221,E701,E202 --repeat "$1"
  true

Also download `flymake-cursor.el
<http://www.emacswiki.org/emacs/flymake-cursor.el>`_ and place it in the
Emacs configuration directory.  Then add the following code to the Emacs
configuration:

.. code-block:: scheme

  ;; Setup for Flymake code checking.
  (require 'flymake)
  (load-library "flymake-cursor")

  ;; Script that flymake uses to check code. This script must be
  ;; present in the system path.
  (setq pycodechecker "pychecker")

  (when (load "flymake" t)
    (defun flymake-pycodecheck-init ()
      (let* ((temp-file (flymake-init-create-temp-buffer-copy
                         'flymake-create-temp-inplace))
             (local-file (file-relative-name
                          temp-file
                          (file-name-directory buffer-file-name))))
        (list pycodechecker (list local-file))))
    (add-to-list 'flymake-allowed-file-name-masks
                 '("\\.py\\'" flymake-pycodecheck-init)))

  (add-hook 'python-mode-hook 'flymake-mode)

.. note::

    Flymake will save files with suffix *_flymake* in the current
    directory. If it crashes for some reason, then these files will not
    get deleted.

    Sometimes there is a delay in refreshing the results.

Delete trailing white spaces and blank lines
--------------------------------------------

To manually delete trailing whitespaces, press ``C-t C-w``, which will run
the command "delete-whitespaces`. This command is also run when a file is
saved, and hence all trailing whitespaces will be deleted on saving a Python
file.

To make sure that all "words" are separated by only one space, type
``M-SPC`` (use the ALT key since ``M-SPC`` sometimes brings up a context
menu.).

To collapse a set of blank lines to one blank line, place the cursor on one
of these and press ``C-x C-o``. This is useful for deleting multiple black
lines at the end of a file.

.. code-block:: scheme

  ;; Remove trailing whitespace manually by typing C-t C-w.
  (add-hook 'python-mode-hook
            (lambda ()
              (local-set-key (kbd "C-t C-w")
                             'delete-trailing-whitespace)))

  ;; Automatically remove trailing whitespace when file is saved.
  (add-hook 'python-mode-hook
        (lambda()
          (add-hook 'local-write-file-hooks
                '(lambda()
                   (save-excursion
                     (delete-trailing-whitespace))))))

  ;; Use M-SPC (use ALT key) to make sure that words are separated by
  ;; just one space. Use C-x C-o to collapse a set of empty lines
  ;; around the cursor to one empty line. Useful for deleting all but
  ;; one blank line at end of file. To do this go to end of file (M->)
  ;; and type C-x C-o.

..  LocalWords:  whitespaces