forked from davidmiller/pony-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpony-mode.el
1141 lines (992 loc) · 36.5 KB
/
pony-mode.el
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; pony-mode.el --- minor mode for working with Django projects
;; Copyright (C) 2011 David Miller <[email protected]>
;; Author: David Miller <[email protected]>
;; Maintainer: David Miller <[email protected]>
;; Created: 2011-02-20
;; Keywords: python django
;; URL: https://github.com/davidmiller/pony-mode
;; Version: 0.3b
;;
;; This file is NOT part of GNU Emacs
;;; License
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License
;; as published by the Free Software Foundation; either version 2
;; of the License, or (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
;;; Code:
;; Variables
(defgroup pony nil
"Djangification for Emacs"
:group 'programming
:prefix "pony-")
(defcustom pony-etags-command "find . | grep .py | xargs etags"
"Command to generate tags table for project"
:group 'pony
:type 'string)
(defcustom pony-server-host "localhost"
"Host to run pony dev server"
:group 'pony
:type 'string)
(defcustom pony-server-port "8000"
"Port to run pony dev server"
:group 'pony
:type 'string)
(defcustom pony-settings-module "settings"
"Settings file to use with manage.py"
:group 'pony
:type 'string)
(defcustom pony-test-failfast t
"Run pony tests with failfast?"
:group 'pony
:type 'bool)
(defcustom pony-sqlite-program "sqlite3"
"Name of the executable to use when running a database REPL for Django
projects using sqlite."
:group 'pony
:type 'string)
(defcustom pony-snippet-dir (expand-file-name
(concat (file-name-directory load-file-name)
"/snippets"))
"Directory in which to locate Yasnippet snippets for Pony Mode"
:group 'pony
:type 'string)
(defvar pony-filesystem-ceiling (if (eq 'windows-nt system-type)
"c:/" "/"))
;; Dependancies and environment sniffing
(require 'cl)
(require 'dired-aux)
(ignore-errors ; files-x gets stripped from some Debian packages
(require 'files-x))
(require 'python)
(require 'sgml-mode)
(require 'sql)
(require 'thingatpt)
;; Utility
;;;###autoload
(defun chomp (str)
"Chomp leading and tailing whitespace www.emacswiki.org/emacs/ElispCookbook"
(let ((s (if (symbolp str) (symbol-name str) str)))
(replace-regexp-in-string "\\(^[[:space:]\n]*\\|[[:space:]\n]*$\\)" "" s)))
;;;###autoload
(defun pony-find-file (path pattern)
"Find files matching pattern in or below path"
(setq matches (list))
(let ((files (list)))
(dolist (f-or-d
(directory-files path t "^[^\\.]"))
(if (file-directory-p f-or-d)
(dolist (filename (find-dot f-or-d pattern))
(add-to-list 'files filename))
(if (string-match pattern f-or-d)
(add-to-list 'files f-or-d))))
files))
;;;###autoload
(defun pony-locate (filepath)
"Essentially duplicates the functionality of `locate-dominating-file'
but allows paths rather than filenames"
(let ((dir (expand-file-name default-directory))
(found nil))
(while (and (not (equal pony-filesystem-ceiling dir))
(not found))
(let ((check (concat dir filepath)))
(if (file-exists-p check)
(setq found check)))
(setq dir (file-name-directory
(directory-file-name dir))))
found))
;;;###autoload
(defun pony-read-file (filepath)
"Read the contents of `filepath'"
(with-temp-buffer
(insert-file-contents filepath)
(read (current-buffer))))
;;
;; Emacs
;;
(defun* pony-pop (buffer-or-name &key dirlocals)
"Select `buffer-or-name' in some window, as for `pop-to-buffer'.
Return the selected buffer if successful, or `nil' otherwise.
If the optional keyword argument `:dirlocals' is non-nil, set the
variable `dir-local-variables-alist' in the target buffer to be
equal to the value in the current buffer. This is useful because
comint buffers without filenames associated will otherwise not
pick up directory-local settings."
(let ((buffer (get-buffer buffer-or-name))
(current-locals dir-local-variables-alist))
(when buffer
(pop-to-buffer buffer)
(pony-mode)
(and dirlocals
current-locals
(pony-local! 'dir-local-variables-alist current-locals)))
buffer))
;;;###autoload
(defun pony-comint-pop(name command args)
"This is the main entry point for sub-processes in Pony-mode.
It creates a comint interaction buffer, called `name', running
`command', called with `args'"
(ansi-color-for-comint-mode-on)
(apply 'make-comint name command nil args)
(pony-pop (concat "*" name "*") :dirlocals t))
;;;###autoload
(defun pony-manage-pop (name command args)
"Run manage.py commands in a commint buffer. Intended as a
wrapper around `pony-commint-pop', this function bypasses the
need to construct manage.py calling sequences in command
functions."
;; !!! This API is utterly stupid. call (pony-manage-cmd)
;; here rather than in the caller.
(let* ((settings (if (pony-project-newstructure-p)
(concat (pony-project-package) "."
(pony-get-settings-file-basename))
(pony-get-settings-file-basename)))
(python-args
(cons command (append args (list (concat "--settings=" settings))))))
(pony-comint-pop name (pony-active-python) python-args)))
;;;###autoload
(defun pony-manage-popif (name command args)
"This wrapper around `pony-manage-pop' will check to see if COMMAND exists
before attempting the manage-pop"
(if (pony-command-exists-p command)
(pony-manage-pop name (pony-manage-cmd) (cons command args))
(message "The Django command %s doesn't seem to be installed" command)))
;;;###autoload
(defun pony-dir-excursion(dir &rest rest)
"pony-comint-pop where we need to change into `dir` first"
(let ((curdir default-directory))
(cd dir)
(apply 'pony-comint-pop rest)
(cd curdir)))
;;;###autoload
(defun pony-mini-file(prompt &optional startdir)
"Read a file from the minibuffer."
(expand-file-name
(read-file-name prompt
(or startdir
(expand-file-name default-directory)))))
;;;###autoload
(defun pony-local! (var val)
"Set the buffer-local variable VAR to VAL.
Destructive function with no state checking - see `pony-localise' for a
more conservative local-var manipulation."
(set (make-local-variable var) val))
;;;###autoload
(defun pony-localise (var func)
"Return buffer local varible or get & set it"
(if (local-variable-p var)
(symbol-value var)
(let ((the-var (funcall func)))
(if the-var
(progn
(make-local-variable var)
(set var the-var))))))
;; Pony-mode
;;
;; Config files
;;
;; Commentary:
;;
;; Allow us to specify things per-project where our local
;; setup is not one of the ones anticipated...
;;
;; We then read in the .ponyrc file in the `pony-project-root',
;; which should define a pony-project variable
;;
(defstruct pony-project python settings)
;;;###autoload
(defun pony-configfile-p ()
"Establish whether this project has a .ponyrc file in the root"
(if (or
(dir-locals-find-file (pony-project-root))
(pony-rooted-sym-p '.ponyrc))
t nil))
;;;###autoload
(defun pony-rc ()
"Return the settings for the current project.
Evaluate the pony-settings variable from the directory-local
variables; if not found, evaluate .ponyrc instead."
(eval (cdr (or (assq 'pony-settings dir-local-variables-alist)
(cons nil (pony-read-file (concat (pony-project-root) ".ponyrc")))))))
(when (featurep 'files-x)
;;;###autoload
(defun pony-define-project ()
"Create or alter the pony-project settings for the current project"
(interactive)
(let* ((localsfile (concat (pony-project-root) ".dir-locals.el"))
(current (if (pony-configfile-p)
(pony-rc)
(make-pony-project)))
(interpreter (read-from-minibuffer "Python: " (pony-project-python current)))
(settings (read-from-minibuffer "Settings module: "
(or (pony-project-settings current)
pony-settings-module))))
(if (not (file-exists-p localsfile))
(dired-do-touch localsfile))
(modify-dir-local-variable nil 'pony-settings '(write list here) 'delete)))
)
;;;###autoload
(defun pony-reload-mode()
(interactive)
(load-library "pony-mode"))
;;
;; Python
;;
;; Commentary:
;;
;; Functions for getting contextually aware information
;; about the code near point
;;
(defun pony-get-app ()
"Return the name of the current app, or nil if no app found."
(let* ((root (pony-project-root))
(re (concat "^" (regexp-quote root) "\\([A-Za-z_]+\\)/"))
(path (or buffer-file-name (expand-file-name default-directory))))
(when (string-match re path)
(match-string 1 path))))
;; Environment
;;;###autoload
(defun pony-project-root()
"Return the root of the project(dir with manage.py in) or nil"
(pony-localise
'pony-this-project-root
'(lambda ()
(let ((curdir default-directory)
(max 10)
(found nil))
(while (and (not found) (> max 0))
(progn
(if (or (file-exists-p (concat curdir "/bin/django")) ; Buildout?
(file-exists-p (concat curdir "manage.py")))
(progn
(setq found t))
(progn
(setq curdir (concat curdir "../"))
(setq max (- max 1))))))
(if found (expand-file-name curdir))))))
(defun pony-project-newstructure-p()
"Predicate to determine whether the project has new structure.
In django ver. => 1.4 manage.py is in an upper directory relative to the
project module."
(let ((settings-base
(concat (pony-project-root)
(pony-get-settings-file-basename))))
(not (or (file-exists-p (concat settings-base ".py"))
(file-exists-p settings-base)))))
(defun pony-project-package()
"Return the project package name."
(pony-localise
'pony-this-project-package
'(lambda ()
(let ((diffsettings nil)
(package ""))
(if (not (pony-project-newstructure-p))
(setq package (file-name-nondirectory
(directory-file-name (pony-project-root))))
(progn
(setq diffsettings
(shell-command-to-string
(concat (pony-active-python) " "
(pony-manage-cmd) " "
"diffsettings")))
(if (string-match "SETTINGS_MODULE = '\\([^'.]+\\)" diffsettings)
(setq package (match-string 1 diffsettings)))))))))
(defun pony-project-package-root()
"Return the root of the project packege (dir with project
settings.py in) or nil"
(pony-localise
'pony-this-project-package-root
'(lambda ()
(let ((package-root nil))
(if (not (pony-project-newstructure-p))
(setq package-root (pony-project-root))
(progn
(setq package-root (pony-project-package))
(if package-root
(expand-file-name (file-name-as-directory package-root)
(pony-project-root)))))))))
;;;###autoload
(defun pony-rooted-sym-p (symb)
"Expand the concatenation of `symb` onto `pony-project-root` and determine whether
that file exists"
(file-exists-p (concat (pony-project-root) (symbol-name symb))))
;;;###autoload
(defun pony-manage-cmd()
"Return the current manage command
This command will only work if you run with point in a buffer that is within your project"
(let ((found nil)
(virtualenv '../bin/activate)
(cmds (list 'bin/django '../bin/django 'manage.py)))
(if (pony-rooted-sym-p virtualenv)
(expand-file-name (concat (pony-project-root) "manage.py"))
;; Otherwise, look for buildout, defaulting to the standard manage.py script
(progn
(dolist (test cmds)
(if (and (not found) (pony-rooted-sym-p test))
(setq found (expand-file-name
(concat (pony-project-root) (symbol-name test))))))
(if found
found)))))
;;;###autoload
(defun pony-active-python ()
"Fetch the active Python interpreter for this Django project.
Be aware of .ponyrc configfiles, 'clean', buildout, and
virtualenv situations"
(if (pony-configfile-p)
(pony-project-python (pony-rc))
(let ((venv-out (pony-locate "bin/python")))
(if venv-out
venv-out
(executable-find "python")))))
;;;###autoload
(defun pony-command-exists-p(cmd)
"Is cmd installed in this app"
(if (string-match cmd
(shell-command-to-string (concat (pony-active-python)
" " (pony-manage-cmd))))
t
nil))
;;;###autoload
(defun pony-command-if-exists(proc-name command args)
"Run `command` if it exists"
(if (pony-command-exists-p command)
(let ((process-buffer (concat "*" proc-name "*")))
(progn
(start-process proc-name process-buffer
(pony-active-python)
(pony-manage-cmd)
command args)
(pop-to-buffer (get-buffer process-buffer))))
nil))
;;;###autoload
(defun pony-get-settings-file-basename()
"Return the name of the settings file to use for this
project. By default this is 'settings', but it can be changed
locally with .dir-locals.el."
(if (pony-configfile-p)
(let ((settings (pony-project-settings (pony-rc))))
(if settings
settings
pony-settings-module))
pony-settings-module))
(defun pony-get-settings-module()
"Return the absolute path to the pony settings file"
(let* ((basename (concat (pony-project-package-root)
(pony-get-settings-file-basename)))
(pyfile (concat basename ".py"))
(exists (or (and (file-exists-p basename) basename)
(and (file-exists-p pyfile) pyfile))))
(if exists
exists
(progn
(message "Settings file not found")
nil))))
;;;###autoload
(defun pony-setting-p (setting)
"Predicate to determine whether a `setting' exists for the current project"
(let ((setting? (pony-get-setting setting)))
(and setting?
(if (string-match "Traceback" setting?)
nil
t))))
;;;###autoload
(defun pony-get-setting(setting)
"Get the pony settings.py value for `setting`"
(let ((settings (pony-get-settings-module))
(python-c (concat (pony-active-python)
" -c \"import settings; print settings.%s\""))
(working-dir default-directory)
(set-val nil))
(if settings
(progn
(cd (file-name-directory settings))
(setq set-val (chomp (shell-command-to-string
(format python-c setting))))
(cd working-dir)
set-val))))
;;;###autoload
(defun pony-setting()
"Interactively display a setting value in the minibuffer"
(interactive)
(let ((setting (read-from-minibuffer "Get setting: " (word-at-point))))
(message (concat setting " : " (pony-get-setting setting)))))
;; Buildout
;;;###autoload
(defun pony-buildout-cmd()
"Return the buildout command or nil if we're not in a buildout"
(pony-localise
'pony-this-buildout-root
'(lambda ()
(let ((root-parent
(expand-file-name (concat (pony-project-root) "../"))))
(if (file-exists-p
(expand-file-name (concat root-parent "bin/buildout")))
(expand-file-name (concat root-parent "bin/buildout"))
nil)))))
;;;###autoload
(defun pony-buildout-list-bin()
"List the commands available in the buildout bin dir"
(directory-files (file-name-directory (pony-buildout-cmd))))
;;;###autoload
(defun pony-buildout()
"Run buildout again on the current project"
(interactive)
(let ((buildout (pony-buildout-cmd))
(cfg (concat
(expand-file-name "../"
(file-name-directory (pony-buildout-cmd)))
"buildout.cfg")))
(if (not (file-exists-p cfg))
(progn
(message "couldn't find buildout.cfg")
(setq cfg nil)))
(if (and buildout cfg)
(progn
(message "Starting buildout... This may take some time")
(pony-comint-pop
"buildout" buildout
(list "-c" cfg))))))
;;;###autoload
(defun pony-buildout-bin()
"Run a script from the buildout bin/ dir"
(interactive)
(let ((buildout (pony-buildout-cmd)))
(if buildout
(pony-comint-pop "buildout"
(minibuffer-with-setup-hook 'minibuffer-complete
(completing-read "bin/: "
(pony-buildout-list-bin)))
nil))))
;; Database
;;;###autoload
(defun pony-db-shell ()
"Run interpreter for this project's default database as an inferior process."
(interactive)
(let ((buffer-name "*pony-db-shell*")
(db-format (if (pony-setting-p "DATABASE_ENGINE") "DATABASE_%s"
"DATABASES['default']['%s']")))
(if (comint-check-proc buffer-name)
(pop-to-buffer buffer-name)
(flet ((db-setting (name) (pony-get-setting (format db-format name)))
;; Rebind sql-get-login so that sql-product-interactive
;; doesn't try to prompt the user.
(sql-get-login (&rest what)))
(let* ((db-engine (db-setting "ENGINE"))
(engine (car (last (split-string db-engine "\\."))))
(sql-product
(loop for product in sql-product-alist
if (search (symbol-name (car product)) engine)
return (car product)
finally do
(error "Don't know how to connect to %s" engine)))
(sql-user (db-setting "USER"))
(sql-password (db-setting "PASSWORD"))
(sql-database (db-setting "NAME"))
(sql-server (db-setting "HOST")))
(save-excursion (sql-product-interactive))
(when (pony-pop "*SQL*" :dirlocals t)
(rename-buffer buffer-name t)))))))
;; Fabric
;;;###autoload
(defun pony-fabric-p ()
"Is this project using fabric?"
(let ((cmdlist (pony-fabric-list-commands)))
(if (and (equal "Fatal" (first cmdlist))
(equal "error:" (second cmdlist)))
nil
t)))
;;;###autoload
(defun pony-fabric-list-commands()
"List of all fabric commands for project as strings"
(split-string (shell-command-to-string "fab --list | awk '{print $1}'|grep -v Available")))
;;;###autoload
(defun pony-fabric-run(cmd)
"Run fabric command"
(pony-comint-pop "fabric" "fab" (list cmd)))
;;;###autoload
(defun pony-fabric()
"Run a fabric command"
(interactive)
(if (pony-fabric-p)
(pony-fabric-run (minibuffer-with-setup-hook 'minibuffer-complete
(completing-read "Fabric: "
(pony-fabric-list-commands)))))
(message "No fabfile found!"))
;;;###autoload
(defun pony-fabric-deploy()
"Deploy project with fab deploy"
(interactive)
(pony-fabric-run "deploy"))
;; GoTo
(defun pony-template-decorator()
"Return either the name of a template from within a decorator around a view
or nil. E.G. the following pattern:
@renders_to('some_template.html')
def my_view(request):
return 'HAI'
"
(save-excursion
(progn
(search-backward-regexp "^def")
(previous-line)
(if (looking-at "^@.*['\"]\\([a-z/_.]+html\\).*$")
(buffer-substring (match-beginning 1) (match-end 1))
nil))))
(defun pony-convert-string-sequence(python-string)
"Convert the string representatin of a python list/tuple of strings
to its lisp equivalent"
(split-string
(replace-regexp-in-string "[][()'\"]" "" python-string) ", ?"))
(defun pony-find-file-in-path(file path)
"Find FILE if it exists in one the directories in PATH"
(dolist (dir path)
(let ((file-absolute (expand-file-name file dir)))
(if (file-exists-p file-absolute)
(return (find-file file-absolute))))))
;;;###autoload
(defun pony-goto-template()
"Jump-to-template-at-point"
(interactive)
(save-excursion
(beginning-of-line)
(let ((template
(if (looking-at "^.*['\"]\\([a-z/_.]+html\\).*$")
(buffer-substring (match-beginning 1) (match-end 1))
(pony-template-decorator))))
(unless (and template
(pony-find-file-in-path
template (pony-convert-string-sequence
(pony-get-setting "TEMPLATE_DIRS"))))
(message (format "Template %s not found" template))))))
;; TODO
;;;###autoload?
;; (defun pony-reverse-url ()
;; "Get the URL for this view"
;; (interactive)
;; (setq found nil)
;; (setq view (concat (pony-get-app) ".views." (pony-get-func)))
;; (message view)
;; (dolist
;; (fpath (find-file default-directory "urls.py$"))
;; (setq mybuffer (get-buffer-create " myTemp"))
;; (switch-to-buffer mybuffer)
;; (insert-file-contents fpath)
;; (search-forward view)))
;;;###autoload
(defun pony-resolve (url)
"Jump to the view file that URL resolves to
This feature is somewhat experimental and known to break in some cases.
Bug reports welcome. Patches even more so :)"
(interactive "sUrl: ")
(let* ((workdir default-directory)
(program
(progn
(cd (pony-project-root))
(format (concat "python -c '"
(mapconcat 'identity
'("import os"
"os.environ[\"DJANGO_SETTINGS_MODULE\"]=\"settings\""
"from django.core.urlresolvers import resolve"
"from django.utils.importlib import import_module"
"f=resolve(\"%s\")[0]"
"path=import_module(f.__module__).__file__"
"path=path[-1]==\"c\" and path[:-1] or path"
"print f.__name__, path")
"; ")
"'") url)))
(output (shell-command-to-string program))
(fun (first (split-string output)))
(file (car (last (split-string output)))))
(cd workdir)
(find-file file)
;; TODO Search for file. This currently doesn't work because of decorators
))
;;;###autoload
(defun pony-goto-settings()
(interactive)
"Open the settings.py for this project"
(find-file (pony-get-settings-module)))
;; Manage
(defun pony-list-commands()
"List of managment commands for the current project"
(let ((command (concat (pony-active-python) " " (pony-manage-cmd) " --help")))
(with-temp-buffer
(insert (shell-command-to-string command))
(goto-char (point-min))
(if (looking-at
"\\(\\(.*\n\\)*Available subcommands:\\)\n\\(\\([^:]*\n\\)+?\\)")
(split-string (buffer-substring (match-beginning 3) (match-end 3)))
nil))))
(defun pony-manage-run(args)
"Run the pony-manage command completed from the minibuffer"
(pony-manage-pop "ponymanage" (pony-manage-cmd) args))
;;;###autoload
(defun pony-manage()
"Interactively call the pony manage command.
Second string that is read from minibuffer may be an actual
list of space separated arguments for the previously chosen management
command. If some of the arguments contain space itself they should be quoted
with double quotes like \"...\"."
(interactive)
(let* ((command (minibuffer-with-setup-hook 'minibuffer-complete
(completing-read "Manage: "
(pony-list-commands))))
(args (split-string-and-unquote
(read-from-minibuffer (concat command ": ")))))
(pony-manage-run (cons command args))))
;;;###autoload
(defun pony-flush()
"Flush the app"
(interactive)
(pony-manage-run (list "flush")))
;; Fixtures
;;;###autoload
(defun pony-dumpdata()
"Dumpdata to json"
(interactive)
(let ((dump (read-from-minibuffer "Dumpdata: " (pony-get-app)))
(target (pony-mini-file "File: ")))
(shell-command (concat
(pony-active-python) " "
(pony-manage-cmd) " dumpdata " dump " > " target))
(message (concat "Written to " target))))
;;;###autoload
(defun pony-loaddata ()
"Load a fixture into the current project's dev database"
(interactive)
(let ((fixture (pony-mini-file "Fixture: ")))
(pony-manage-pop "ponymanage" (pony-manage-cmd)
(list "loaddata" fixture))
(insert (concat "Loaded fixture at " fixture))))
;; Server
;;;###autoload
(defun pony-runserver()
"Start the Django development server.
If the server is currently running, just switch to the buffer.
If you are currently in the *ponyserver* buffer, restart the server"
(interactive)
(let* ((buffname "*ponyserver*")
(proc (get-buffer-process buffname))
(buff (get-buffer buffname))
(working-dir default-directory))
(if proc
(progn
(message "Pony Dev Server already running")
(if (and buff (equalp buff (current-buffer)))
(pony-restart-server)
(pony-pop buffname)))
(pony-startserver))))
(defun pony-startserver ()
"Start the Django development server.
If the project has django_extras installed and the excellent `runserver_plus'
command is available, use that, otherwise fall back to manage.py runserver."
(if (pony-command-exists-p "runserver_plus")
(setq command "runserver_plus")
(setq command "runserver"))
(progn
(cd (pony-project-root))
(pony-manage-pop "ponyserver" (pony-manage-cmd)
(list command
(concat pony-server-host ":" pony-server-port)))
(cd working-dir)))
;;;###autoload
(defun pony-stopserver()
"Stop the dev server"
(interactive)
(let ((proc (get-buffer-process "*ponyserver*")))
(when proc (kill-process proc t))))
;;;###autoload
(defun pony-restart-server ()
"Restart the pony Django dev server.
Django extras does this better with the Werkzeug server, but sometimes
you can't have nice things."
(interactive)
(pony-stopserver)
(run-with-timer 1 nil 'pony-startserver))
;;;###autoload
(defun pony-temp-server ()
"Relatively regularly during development, I need/want to set up a development
server instance either on a nonstandard (or second) port, or that will be accessible
to the outside world for some reason. Meanwhile, i don't want to set my default host to 0.0.0.0
This function allows you to run a server with a 'throwaway' host:port"
(interactive)
(let ((args (list "runserver" (read-from-minibuffer "host:port "))))
(pony-manage-pop "ponytempserver" (pony-manage-cmd)
args)))
;; View server
;;;###autoload
(defun pony-browser()
"Open a tab at the development server"
(interactive)
(let ((url (concat "http://" pony-server-host ":" pony-server-port))
(proc (get-buffer-process "*ponyserver*")))
;; use actual url if process is already running
(if proc
(save-excursion
(progn
(set-buffer "*ponyserver*")
(goto-char (point-max))
(if (search-backward-regexp "Development server is running at \\(.+\\)\n")
(setq url (match-string-no-properties 1)))))
(pony-runserver))
(run-with-timer 2 nil 'browse-url url)))
;; Shell
;;;###autoload
(defun pony-shell()
"Open a Python shell with the current pony project's context loaded.
If the project has the django_extras package installed, then use the excellent
`shell_plus' command. Otherwise, fall back to manage.py shell "
(interactive)
(let ((command (if (pony-command-exists-p "shell_plus")
"shell_plus" "shell")))
(pony-manage-pop "ponysh" (pony-manage-cmd) (list command))))
;; Startapp
;;;###autoload
(defun pony-startapp()
"Run the pony startapp command"
(interactive)
(let ((app (read-from-minibuffer "App name: ")))
(pony-command-if-exists "ponymigrations"
"startapp" app)))
;; Syncdb / South
;;;###autoload
(defun pony-syncdb()
"Run Syncdb on the current project"
(interactive)
(pony-manage-pop "ponymigrations" (pony-manage-cmd) (list "syncdb")))
;; (defun pony-south-get-migrations()
;; "Get a list of migration numbers for the current app"
;; )
;;;###autoload
(defun pony-south-convert()
"Convert an existing app to south"
(interactive)
(let ((app (read-from-minibuffer "Convert: " (pony-get-app))))
(pony-manage-popif "ponymigrations" "convert_to_south" (list app))))
;;;###autoload
(defun pony-south-schemamigration()
"Create migration for modification"
(interactive)
(let ((app (read-from-minibuffer "Schema Migration: " (pony-get-app))))
(pony-manage-popif "ponymigrations" "schemamigration"
(list app "--auto"))))
;;;###autoload
(defun pony-south-migrate()
"Migrate app"
(interactive)
(let ((app (read-from-minibuffer "Migrate: " (pony-get-app))))
(pony-manage-popif "ponymigrations" "migrate" (list app))))
;; (defun pony-south-fake ()
;; "Fake a migration for a model"
;; (interactive)
;; (let ((app (read-from-minibuffer "Convert: " (pony-get-app)))
;; (migration (read-from-minibuffer "migration: "
;; (pony-south-get-migrations))))
;; (pony-command-if-exists "ponymigrations"
;; "migrate" (list app migrations))))
;;;###autoload
(defun pony-south-initial ()
"Run the initial south migration for an app"
(interactive)
(let ((app (read-from-minibuffer "Initial migration: " (pony-get-app))))
(pony-manage-popif "ponymigrations" "schemamigration" (list app "--initial"))))
;; TAGS
;;;###autoload
(defun pony-tags()
"Generate new tags table"
(interactive)
(let ((working-dir default-directory)
(tags-dir (read-directory-name "TAGS location: "
(pony-project-root))))
(cd (expand-file-name tags-dir))
(message "TAGging... this could take some time")
(shell-command pony-etags-command )
(visit-tags-table (concat tags-dir "TAGS"))
(cd working-dir)
(message "TAGS table regenerated")))
;; Testing
;;;###autoload
(defun pony-test (command)
"Run the test(s) given by `command'."
(interactive
(let* ((defuns (subseq (split-string (python-current-defun) "\\.") 0 2))
(class (first defuns))
(func (let ((f (second defuns))) (and f (string-match "^test" f) f)))
(app (pony-get-app))
(default-command
(concat app (and app class ".") class (and class func ".") func)))
(list (read-string "Test: " default-command))))
(pony-manage-pop "ponytests" (pony-manage-cmd)
(list "test" (if pony-test-failfast "--failfast" "") command))
(pony-test-mode))
;;;###autoload
(defun pony-test-open ()
"Open the file in a traceback at the line specified"
(interactive)
(move-beginning-of-line nil)
(if (looking-at ".*File \"\\([a-z/_]+.py\\)\", line \\([0-9]+\\)")
(let ((file (buffer-substring (match-beginning 1) (match-end 1)))
(line (buffer-substring (match-beginning 2) (match-end 2))))
(find-file-other-window file)
(goto-line (string-to-number line)))
(message "failed")))
;;;###autoload
(defun pony-test-goto-err()
"Go to the file and line of the last stack trace in a test buffer"
(interactive)
(goto-char (search-backward "File"))
(pony-test-open))
;;;###autoload
(defun pony-test-up()
"Move up the traceback one level"
(interactive)
(search-backward-regexp "File \"\\([a-z_/]+.py\\)\"" nil t))
;;;###autoload
(defun pony-test-down()
"Move up the traceback one level"
(interactive)
(search-forward-regexp "File \"\\([a-z_/]+.py\\)\"" nil t))
;;;###autoload
(defun pony-test-hl-files ()
"Highlight instances of Files in Test buffers"
(hi-lock-face-buffer "File \"\\([a-z/_]+.py\\)\", line \\([0-9]+\\)"
'hi-blue))
;; Modes ;;
;; Snippets
;;;###autoload
;;;###autoload
(defun pony-load-snippets()
"Load snippets if yasnippet installed"
(interactive)
(if (fboundp 'yas/load-directory)
(yas/load-directory pony-snippet-dir)))
;; Keymaps