Get emacs to recognize python3 shebang - python-3.x

I'm using emacs to edit some Python 3 code, but it doesn't provide syntax highlighting when the shebang is #! /usr/bin/env python3. Highlighting works fine with just #! /usr/bin/env python. How do I get emacs to recognize a python3 shebang as a Python file, and provide appropriate syntax highlighting?
Edit: I'm using version 22.1.1, with no ability to change it.

I came across the same problem you had here and the other answer by Rorschach did not work for me, also because I had an old version (24.3) of emacs which I couldn't upgrade. After trial and error, this worked for me:
Add to your .emacs file the line:
(push '("python3" . python-mode) interpreter-mode-alist)
Old emacs (prior to 24.4) did not support regex for editing interpreter-mode-alist, which was why the other answer's suggested fix did not work.
The changelog for emacs 24.4 mentions the new support for regex: "The cars (sic) of the elements in interpreter-mode-alist are now treated as regexps rather than literal strings."

Check the value of auto-mode-interpreter-regexp, which should match the shebang entry correctly by default. Then, ensure there is an entry in your interpreter-mode-alist like
("python[0-9.]*" . python-mode)
If not for some reason, add it in your init file, eg.
(cl-pushnew '("python[0-9.]*" . python-mode) interpreter-mode-alist :test #'equal)
Edit
Since your emacs is quite ancient, try
(push '("python[0-9.]*" . python-mode) interpreter-mode-alist)

Related

python 3 'input()' with textmate 2 'run'? [duplicate]

I wrote the following for a homework assignment and it works fine in IDLE and Eclipse running Python 3.
However, I tried to run it from TextMate with the new line 1 -- which I found here -- to point it to Python 3 on the Mac. It seems to be running Python 3 but returns an error. It says: EOFError: EOF when reading a line. It's referring to line 5 below.
Anyone know why?
BTW, this TextMate issue is not part of the homework assignment, so I'm not trying to get homework help. I just want to figure out how to use TextMate with Python 3.
#! /usr/local/bin/python3
#
# Tests user string against two conditions.
#
user_string = input("Enter a string that is all upper case and ends with a period: ")
if user_string.isupper() and user_string.endswith("."):
print("Your string met both conditions.")
else:
if user_string.isupper():
print("Your string does not end with a period.")
elif user_string.endswith("."):
print("Your string is not all upper.")
else:
print("Your string failed both conditions.")
The problem you are seeing has nothing to do with the Python version. The problem is that TextMate does not try to redirect standard input so, when you are running via the TextMate's Python bundle Run Script command, the Python program sees an immediate end-of-file. As explained here, TextMate used to be fancier about this but the mechanism it used no longer works in OS X 10.6 so the feature was disabled.
One solution is to use the Shift-Command-R Run Script in Terminal command of TextMate's Python bundle. This causes TextMate to open a terminal window and run the script there and you can enter the input there. Unfortunately, while TextMate does respect the shebang line with the normal Command-R Run Script command, it doesn't seem to do so with the Run Script in Terminal command. You can verify that yourself in various ways. Try running this code snippet in TextMate:
#! /usr/local/bin/python3
import sys
print(sys.executable)
To get around that, you can set the TM_PYTHON environment variable in TextMate. See the answer here for more details on how to do that.
Textmate is using the built-in Python, rather than respecting the shebang line. You'd probably have to hack the bundle code to use the right python.

Creating ctags extension for markdown

I edit quite a few markdown files using Vim these days. One thing I'm missing is a map of the file like function list in C based on ctags. So I came up with the following .ctags file
--langdef=markdown
--langmap=markdown:.md
--regex-markdown=/^# ([a-zA-Z0-9]+)/\1/
It runs OK but generates no valid tags for my .md file. With verbose mode turned on I get the following:
Considering option file /home/wenliang/.ctags: reading...
Option: --langdef=markdown
Option: --langmap=markdown:.md
Setting markdown language map: .md
Option: --regex-markdown=/^# ([a-zA-Z0-9]+)/\1/
Considering option file ./.ctags: not found
What's wrong with what I did?
Your definition looks OK.
What command did you use to generate your tags file? $ ctags . won't index anything but $ ctags -R . will.
FWIW, here is a slightly modified version of your definition that provides meaningful tag names and kind informations:
--langdef=markdown
--langmap=markdown:.md
--regex-markdown=/^#[ \t](.*$)/\1/h,heading,headings/
As an alternative, you might be interested in these cheaper, built-in, solutions…
using the define option and :dlist:
:setlocal define=^#\\s*
:dli /<CR>
using :ilist and no setup:
:il /#<CR>
which both produce the same list, ready for you to type :126<CR>:
See :help :ilist, :help :dlist, :help 'define'.

Does LightTable's vim mode support regex substitution?

In LightTable, I've installed the vim plugin and activated it in user.behaviors. I can perform basic navigation and text editing with vim bindings, but when I try to do a command like:
:%s/test/prod/g
... this doens't work. Instead, typing ":" opens a right-hand bar sidebar like:
But typing a "substitute" command into this box has no effect (can't press enter on completion):
I've confirmed that the "substitute" is supported by CodeMirror's vim mode on which LightTable's vim mode is based.
Would appreciate any advice on how to do this properly!
If your using something like vim, I believe you need to declare sed before making a regex expression like s/test/prod/g. Try typing this in your script:
sed 's/test/prod/g'
EDIT: When I was researching LightTable, i found the syntax to be slightly different:
sed([options ,] search_regex, replace_str, file)
For example:
sed('-i', 'PROGRAM_VERSION', 'v0.1.3', 'source.js');
-i: Replace contents of 'file' in-place. Note that no backups will be created!

Generating ctags for Haskell Platform (standard library), specifically for prelude

I've installed Haskell on my Mac using Homebrew, that is brew install ghc haskell-platform.
I'm looking for a way to generate a ctags file of the standard Haskell Platform libraries (modules) so I could browse the source while coding in Vim. I specifically need Prelude and the other most popular modules, like Data.List and such.
I am aware that the source is available on the web via Hoogle, but It'll be easier for me to jump-to-source whenever I need to, for learning purposes.
Where is the source located when installing the Haskell Platform?
Is the source even installed when installing the Haskell Platform, or just the compiled binaries or something of the sort?
How can I make the source available for browsing in Vim? As in put the generated tags file somewhere and tell Vim to read from it. I also understand there's no need to re-generate the tags file, since these modules are pretty much static and don't get updated very often.
1) and 2) were answered by permeakra in comments. I'll try to cover 3) by describing setup similar to the one I'm using. First simple solution for base libraries, then more generic solution for whatever Haskell source package in general.
As a prerequisites we will need a tool which generates tags file for Haskell:
cabal install hothasktags
Instead of hothasktags you might use your favourite one. See for example https://github.com/bitc/lushtags page which enumerates some of these.
Then we need to have sources for base libraries available. Here I'm using the ones from GitHub:
cd /space/haskell/sources/ # tweak to your personal taste
git clone https://github.com/ghc/packages-base.git
Optionally we might switch to particular branch. E.g.:
git checkout ghc-7.4
Run git branch -a to see all possibilities.
Now let's generate tags for the base libraries (I do not have Mac available and thus have to assume the command works there or you are able to tweak it appropriately):
cd packages-base
export LC_ALL=C # needed for case-sensitive searching
find -type f | egrep \.hs$\|\.lhs$ | xargs -Ii hothasktags i | sort > tags
(Note about sort: My Vim complains when I do not use the sort. For LC_ALL explanation see for example this blog post)
Now we need to let the Vim know about the tags we generated. The easiest way is probably to put the following line into your $HOME/.vimrc:
autocmd FileType haskell setlocal tags+=/space/haskell/sources/packages-base/tags
This way the tags for base libraries will be set for each Haskell file we open. If this is not desirable we can put following Vim command into .vimrc:
autocmd FileType haskell command! SetGHCTags
\ setlocal tags+=/space/haskell/sources/packages-base/tags
and call :SetGHCTags on demand.
For more generic solution which works with all Haskell sources packages we can use the following function (put into .vimrc or into Vim file dedicated to Haskell filetype):
" Add 'tags' of the given package to the current tag stack. The package sources
" must be available in "/space/haskell/sources/<package>" and the tags must be
" generated for it.
fun! s:SetHaskellTags(pathInHaskellSrcDir) "{{{
let tagFile = "/space/haskell/sources/" . a:pathInHaskellSrcDir . "/tags"
if filereadable(tagFile)
exe "setlocal tags+=" . tagFile
else
echoerr "File does not exist or is not readable: " . tagFile
endif
endfunction "}}}
command! -nargs=1 SetHaskellTags call <SID>SetHaskellTags(<args>)
Utilizing it for example for Shelly.hs library:
cd /space/haskell/sources/
git clone https://github.com/yesodweb/Shelly.hs.git
cd Shelly.hs
regenerate-haskell-tags # [1]
In Vim just call:
:SetHaskellTags "Shelly.hs"
There is space for improvement - SetHaskellTags could generate tags if not exist, or could even fetch the sources, configurable Haskell source code storage, directory completion, etc. But works good enough for me now. So at least sharing the solution I have. Will come back here if I get to some of these improvement done.
[1]: It's better to store regenerate-haskell-tags in your $PAHT.

emacs syntax highlighting for jags / bugs

Are there packages to color-highlight jags amd bugs model files? I have ESS installed, but it doesn't seem to recognize .bug files or jags/bugs syntax out of the box.
Syntax highlighting
I'm using ESS 5.14 (from ELPA) and syntax highlighting or smart underscore works fine for me with GNU Emacs 24.1.1. If you want to highlight a given file, you can try M-x ess-jags-mode or add a hook to highlight JAGS file each time, e.g.
(add-to-list 'auto-mode-alist '("\\.jag\\'" . jags-mode))
However, that is not really needed since you can simply
(require 'ess-jags-d)
in your .emacs. There's a corresponding mode for BUGS file. This file was already included in earlier release (at least 5.13), and it comes with the corresponding auto-mode-alist (for "\\.[jJ][aA][gG]\\'" extension).
(Please note that there seems to exist subtle issue with using both JAGS and BUGS, but I can't tell more because I only use JAGS.)
Running command file
If you want to stick with Emacs for running JAGS (i.e., instead of rjags or other R interfaces to JAGS/BUGS), there's only one command to know:
As described in the ESS manual, when working on a command file, C-c C-c should create a .jmd file, and then C-c C-c'ing again should submit this command file to Emacs *shell* (in a new buffer), and call jags in batch mode. Internally, this command is binded to a 'Next Action' instruction (ess-*-next-action). For example, using the mice data that comes with JAGS sample files, you should get a mice.jmd that looks like that:
model in "mice.jag"
data in "mice.jdt"
compile, nchains(1)
parameters in "mice.in1", chain(1)
initialize
update 10000
update 10000
#
parameters to "mice.to1", chain(1)
coda \*, stem("mice")
system rm -f mice.ind
system ln -s miceindex.txt mice.ind
system rm -f mice1.out
system ln -s micechain1.txt mice1.out
exit
Local Variables:
ess-jags-chains:1
ess-jags-command:"jags"
End:
Be careful with default filenames! Here, data are assumed to be in file mice.jdt and initial values for parameters in mice.in1. You can change this in the Emacs buffer if you want, as well as modify the number of chains to use.

Resources