Escaped strings in elisp - string

I am very new to Lisp - and Elisp especially - and I have a problem with string handling:
I want to convert a Windows style path to a Unix style path - especially I need to convert
a path I get from Visual Studio to a Cygwin path, as I want to be able to open a file from
Visual Studio in Emacs (I hope to use emacsclient --eval for this):
The Visual Studio path has the following format:
C:\Users\name\documents\visual studio 2010\projects\test
I want to change it into the appropriate Cygwin path which would be:
/cygdrive/c/Users/name/documents/visual studio 2010/projects/test
However trying the following in the scratch-buffer already fails:
(replace-regexp-in-string "\\" "\/" "C:\users\someone")
(subst-char-in-string ?\ ?/ "C:\users\someone")
>> Debugger entered--Lisp error: (error "Non-hex digit used for Unicode escape")
Is there any way to make Elisp not escape the backslashes in every string?
EDIT:
Here is how I call emacs from Visual Studio via external tools:
emacsclient -d 127.0.0.1:0.0 --eval '(convert-win-to-cygwin-path $(ItemPath))'
$(ItemPath) will be replaced with C:\Users\asf which I can not influence - so it will pass a String with single backslashes to emacs that I need to modify in emacs.
Can I make emacs KNOW that it needs to make double backslashes out of the single backslashes?
EDIT 2: Solution
I changed the way I attempt to start emacs by actually calling a shell-script that will start emacs - this way I can make sure that emacs gets the right path:
#!/bin/bash
export PATH="/usr/bin/:/bin/:$PATH"
filename=$1
line=$2
column=$3
cyged_path=$(cygpath "$filename")
echo "Cyged path: $cyged_path"
emacsclient -d 127.0.0.1:0.0 -n +$line:$column "$cyged_path"
And I call it from Visual Studio with the following arguments in the external tools window:
Path: <path_to_cygwin>\bin\bash.exe
Arguments: <path_to_script> $(ItemPath) $(CurLine) $(CurCol)

You don't need to do any of that.
Cygwin provides a command specifically for converting Windows paths to Cygwin paths, so that you don't need to bother about this sort of thing.
cmd> C:\cygwin\bin\cygpath.exe "C:\Users\name\documents\visual studio 2010\projects\test"
/cygdrive/c/Users/name/documents/visual studio 2010/projects/test
Edit: I was curious about a Windows shell equivalent of backticks, and found Batch equivalent of Bash backticks, which suggests you might be able to do it all with this one-liner?
cmd> for /f "usebackq tokens=*" %a in (`C:\cygwin\bin\cygpath.exe "$(ItemPath)"`) do emacsclient -d 127.0.0.1:0.0 "%a"

Another way to look at your problem is that your emacsclient -d 127.0.0.1:0.0 --eval '(convert-win-to-cygwin-path $(ItemPath))' passes the file name inside an Elisp string, which is why the backslashes are treated as escapes. So the solution is to pass it as data, as in emacsclient -d 127.0.0.1:0.0 $(ItemPath). The next problem is to make your Emacs understand those filenames. If you use the normal Windows build of Emacs that will work without doing anything special, but if you use the Cygwin build, you'll need to do something like the equivalent (but in reverse) of cygwin-mout.el.

Here's the answer in emacs lisp, even though I like Phils answer. This function converts the region which is expected to be a full windows filepath including the drive, into a cygwin path.
For example:
C:\Users\name\documents\visual studio 2010\projects\test
becomes...
/cygdrive/C/Users/name/documents/visual studio 2010/projects/test
Here's the code:
(defun win32-to-cygwin-path()
"Converts a win32 path into a cygwin happy one"
(interactive)
(save-excursion
(save-restriction
(narrow-to-region (point) (mark))
(goto-char (point-min))
(insert "/cygdrive/")
(goto-char (point-min))
(while (search-forward ":" nil t)
(replace-match "" nil t))
(while (search-forward "\\" nil t)
(replace-match "/" nil t)))))

A backslash needs to be escaped. This would work:
ELISP> (subst-char-in-string ?\\ ?/ "C:\\users\\someone")
"C:/users/someone"

Are you sure Windows isn't sending preescaped backslashes? It would be strange if it didn't do that.
See this wiki page for how other have solved this problem.

None of these solutions worked for me (emacs 24.3.1), anyway, a simple .bat script will do:
#ECHO OFF
c:\cygwin64\bin\cygpath.exe %1 > tmp.txt
set /p FILE_TO_EDIT= < tmp.txt
del tmp.txt
C:\cygwin64\bin\cygstart.exe -- /bin/emacs-w32.exe %FILE_TO_EDIT%
Save that in a file.bat and send the file you want to edit as the argument, for WinSCP, will look like:
C:\Users\xxxxx\Desktop\open.bat !.!

Related

Renaming files like 20141207_190822.jpg to "2014-12-07 19.08.22.jpg" in linux or MacOS X

How in Linux or MacOS X to rename a bunch of files with names 20141207_190822.jpg and 20141207_190823.mp4 to this format:
2014-12-07 19.08.22.jpg
and
2014-12-07 19.08.23.mp4
?
I've found many examples how to just add something to the beginning of filename, but here I need to change the mask by inserting symbols in the middle of filenames and replacing _ with space " ".
Thank you
Try doing this :
$ rename 's#^(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})#$1-$2-$3 $4.$5.$6#' *
There are other tools with the same name which may or may not be able to do this, so be careful.
If you run the following command :
$ file $(readlink -f $(type -p rename))
and you have a result like
.../rename: Perl script, ASCII text executable
then this seems to be the right tool =)
If you don't have this command, search your package manager to install it or do it manually
Last but not least, this tool was originally written by Larry Wall, the Perl's dad.

syntax error near unexpected token ' - bash

I have a written a sample script on my Mac
#!/bin/bash
test() {
echo "Example"
}
test
exit 0
and this works fine by displaying Example
When I run this script on a RedHat machine, it says
syntax error near unexpected token '
I checked that bash is available using
cat /etc/shells
which bash shows /bin/bash
Did anyone come across the same issue ?
Thanks in advance !
It could be a file encoding issue.
I have encountered file type encoding issues when working on files between different operating systems and editors - in my case particularly between Linux and Windows systems.
I suggest checking your file's encoding to make sure it is suitable for the target linux environment. I guess an encoding issue is less likely given you are using a MAC than if you had used a Windows text editor, however I think file encoding is still worth considering.
--- EDIT (Add an actual solution as recommended by #Potatoswatter)
To demonstrate how file type encoding could be this issue, I copy/pasted your example script into Notepad in Windows (I don't have access to a Mac), then copied it to a linux machine and ran it:
jdt#cookielin01:~/windows> sh ./originalfile
./originalfile: line 2: syntax error near unexpected token `$'{\r''
'/originalfile: line 2: `test() {
In this case, Notepad saved the file with carriage returns and linefeeds, causing the error shown above. The \r indicates a carriage return (Linux systems terminate lines with linefeeds \n only).
On the linux machine, you could test this theory by running the following to strip carriage returns from the file, if they are present:
cat originalfile | tr -d "\r" > newfile
Then try to run the new file sh ./newfile . If this works, the issue was carriage returns as hidden characters.
Note: This is not an exact replication of your environment (I don't have access to a Mac), however it seems likely to me that the issue is that an editor, somewhere, saved carriage returns into the file.
--- /EDIT
To elaborate a little, operating systems and editors can have different file encoding defaults. Typically, applications and editors will influence the filetype encoding used, for instance, I think Microsoft Notepad and Notepad++ default to Windows-1252. There may be newline differences to consider too (In Windows environments, a carriage return and linefeed is often used to terminate lines in files, whilst in Linux and OSX, only a Linefeed is usually used).
A similar question and answer that references file encoding is here: bad character showing up in bash script execution
try something like
$ sudo apt-get install dos2unix
$ dos2unix offendingfile
Easy way to convert example.sh file to UNIX if you are working in Windows is to use NotePad++ (Edit>EOL Conversion>UNIX/OSX Format)
You can also set the default EOL in notepad++ (Settings>Preferences>New Document/Default Directory>select Unix/OSX under the Format box)
Thanks #jdt for your answer.
Following that, and since I keep having this issue with carriage return, I wrote that small script. Only run carriage_return and you'll be prompted for the file to "clean".
https://gist.github.com/kartonnade/44e9842ed15cf21a3700
alias carriage_return=remove_carriage_return
remove_carriage_return(){
# cygwin throws error like :
# syntax error near unexpected token `$'{\r''
# due to carriage return
# this function runs the following
# cat originalfile | tr -d "\r" > newfile
read -p "File to clean ? "
file_to_clean=$REPLY
temp_file_to_clean=$file_to_clean'_'
# file to clean => temporary clean file
remove_carriage_return_one='cat '$file_to_clean' | tr -d "\r" > '
remove_carriage_return_one=$remove_carriage_return_one$temp_file_to_clean
# temporary clean file => new clean file
remove_carriage_return_two='cat '$temp_file_to_clean' | tr -d "\r" > '
remove_carriage_return_two=$remove_carriage_return_two$file_to_clean
eval $remove_carriage_return_one
eval $remove_carriage_return_two
# remove temporary clean file
eval 'rm '$temp_file_to_clean
}
I want to add to the answer above is how to check if it is carriage return issue in Unix like environment (I tested in MacOS)
1) Using cat
cat -e my_file_name
If you see the lines ended with ^M$, then yes, it is the carriage return issue.
2) Find first line with carriage return character
grep -r $'\r' Grader.sh | head -1
3) Using vim
vim my_file_name
Then in vim, type
:set ff
If you see fileformat=dos, then the file is from a dos environment which contains a carriage return.
After finding out, you can use the above mentioned methods by other people to correct your file.
I had the same problem when i was working with armbian linux and Windows .
i was trying to coppy my codes from windows to armbian and when i run it this Error Pops Up. My problem Solved this way :
1- try to Coppy your files from windows using WinSCP .
2- make sure that your file name does not have () characters

How to launch a shell script with an emacs keybinding, passing the word under the cursor as a variable

Executing the script below on osx via emacs, didn't work, I got a permission denied message, the answer to this question solved that problem: https://stackoverflow.com/a/12276562/912475
Tldr: How can I set up a system to automatically pass the word under the cursor in emacs directly to my shell script as a variable and then run that script?
I've created a somewhat rudimentary system for "linking" to folders from plain text files in a robust way. It uses a timestamp that's generated by a script and then set as the "value" of the clipboard. From the clipboard, the timestamp is then pasted into a txt with related notes and into the name field of a folder.
To find the folders when reading the txt I use an emacs function with a keybinding that makes it possible to copy the timestamp (as a word, it's all numbers) to the clipboard and search for it in spotlight (on osx). What I'd like to do instead is to automatically launch a shell script that searches for a directory whose name ends with that string and then opens it. I already have a script that does something like that, but I don't really know how to tie the elisp function and shell script together. I'd greatly appreciate a solution that works on either osx and linux (I use both). It will probably be easy to "port" a solution that works on either one for use with the other.
This is the emacs function for copying a word under the cursor:
;;; function for copying a word under the cursor http://www.emacswiki.org/emacs/CopyWithoutSelection
(global-set-key (kbd "C-c o") (quote copy-word))
(defun copy-word (&optional arg)
"Copy words at point into kill-ring"
(interactive "P")
(copy-thing 'backward-word 'forward-word arg)
;;(paste-to-mark arg)
)
This is the script to find and open the directory whose name ends with the timestamp:
#!/bin/bash
PATH=/opt/local/bin:/opt/local/sbin:$PATH #need this to make the gnu coreutils work on osx
file_number="20130812193913"
path_to_open=$(gfind ~/x/ | grep -e $file_number$) # $ means the end of the line, makes it possible to search for directories without finding their content
open "${path_to_open}"
An edited version of the script that accepts arguments from the commandline like this:
me$ sh script_path.sh 20130812193913
The script:
#!/bin/bash
PATH=/opt/local/bin:/opt/local/sbin:$PATH #need this to make the gnu coreutils work on osx
file_number=$1
echo $file_number
path_to_open=$(gfind ~/x/ | grep -e $file_number$) # $ means the end of the line, makes it possible to search for directories without finding their content
open "${path_to_open}"
See: http://www.bashguru.com/2009/11/how-to-pass-arguments-to-shell-script.html
You could try something like this:
(defvar script-name "/foo/bar/my-script")
(defun call-my-script-with-word ()
(interactive)
(shell-command
(concat script-name
" "
(thing-at-point 'word))))
(global-set-key (kbd "C-c o") 'call-my-script-with-word)

Is it possible to access vim's command-line arguments in vimscript?

I found the answer to this question while writing it, so I've broadened it a little. I wanted to access the --servername argument, in order to create dynamic settings in my .vimrc file.
Through vim's help, I found the v:servername variable, and my script is working. However, now I'm curious if it's possible to access any arbitrary command-line argument. For example, if I wanted to know if vim was in Lisp mode (-l) or Debugging mode (-D), how would I do it? There seems to be no corresponding v: variable for them.
Here are the variables I found by autocompleting :help v:<Tab>
Is there a general way to access command-line arguments from vimscript?
Strangely, I think the answer may be "No, there is no direct way to access startup options specified on the command line".
The :args command and argv() can be used to access the filename(s) specified on startup, but that's not what you want.
I see on Vim's forums that someone offered this solution to get the startup command line on Linux:
:exe '!tr "\0" " " </proc/' . getpid() . '/cmdline'
I assume there's analogous command on Windows. . . .
You can look over that forum thread here:
http://groups.google.com/group/vim_use/browse_thread/thread/43773f27cdc10265/ad17ae8180c0fb6e?show_docid=ad17ae8180c0fb6e
My googling indicates that this feature has been proposed but never implemented. However I did come up with a bit of a kludge that nevertheless works:
:echo split( system( "ps -o command= -p " . getpid() ) )
# => [ 'vim', ... arguments ... ]
(Tested on OS X Lion.)
The getpid() function gets Vim's PID, then we call ps externally with options to return nothing but the "command" value for process, then use split() to split the command into a list.

Vim with Powershell

I'm using gvim on Windows.
In my _vimrc I've added:
set shell=powershell.exe
set shellcmdflag=-c
set shellpipe=>
set shellredir=>
function! Test()
echo system("dir -name")
endfunction
command! -nargs=0 Test :call Test()
If I execute this function (:Test) I see nonsense characters (non number/letter ASCII characters).
If I use cmd as the shell, it works (without the -name), so the problem seems to be with getting output from powershell into vim.
Interestingly, this works great:
:!dir -name
As does this:
:r !dir -name
UPDATE: confirming behavior mentioned by David
If you execute the set commands mentioned above in the _vimrc, :Test outputs nonsense. However, if you execute them directly in vim instead of in the _vimrc, :Test works as expected.
Also, I've tried using iconv in case it was an encoding problem:
:echo iconv( system("dir -name"), "unicode", &enc )
But this didn't make any difference. I could be using the wrong encoding types though.
Anyone know how to make this work?
It is a bit of a hack, but the following works in Vim 7.2. Notice, I am running Powershell within a CMD session.
if has("win32")
set shell=cmd.exe
set shellcmdflag=/c\ powershell.exe\ -NoLogo\ -NoProfile\ -NonInteractive\ -ExecutionPolicy\ RemoteSigned
set shellpipe=|
set shellredir=>
endif
function! Test()
echo system("dir -name")
endfunction
Tested with the following...
:!dir -name
:call Test()
I ran into a similar problem described by many here.
Specifically, calling
:set shell=powershell
manually from within vim would cause powershell to work fine, but as soon as I added:
set shell=powershell
to my vimrc file I would get the error "Unable to open temp file .... "
The problem is that by default when shell is modified, vim automatically sets shellxquote to " which means that shell commands will look like the following:
powershell -c "cmd > tmpfile"
Where as this command needs to look like this, in order for vim to read the temp file:
powershell -c "cmd" > tmpfile
Setting shellquote to " in my vimrc file and unsetting shellxquote (i.e. setting it to a blank space) seem to fix all my problems:
set shell=powershell
set shellcmdflag=-c
set shellquote=\"
set shellxquote=
I've also tried taking this further and scripting vim a bit using the system() call:
system() with powershell in vim
I suspect that the problem is that Powershell uses the native String encoding for .NET, which is UTF-16 plus a byte-order-mark.
When it's piping objects between commands it's not a problem. It's a total PITA for external programs though.
You can pipe the output through out-file, which does support changing the encoding, but still formats the output for the terminal that it's in by default (arrgh!), so things like "Get-Process" will truncate with ellipses, etc. You can specify the width of the virtual terminal that Out-File uses though.
Not sure how useful this information is, but it does illuminate the problem a bit more.
Try replacing
"dir \*vim\*"
with
" -command { dir \*vim\* }"
EDIT: Try using cmd.exe as the shell and put "powershell.exe" before "-command"
Interesting question - here is something else to add to the confusion. Without making any changes to my .vimrc file, if I then run the following commands in gvim:
:set shell=powershell.exe
:set shellcmdflag=-noprofile
:echo system("dir -name")
It behaves as expected!
If I make the same changes to my .vimrc file, though (the shell and shellcmdflag options), running :echo system("dir -name") returns the nonsense characters!
The initial example code works fine for me when I plop it in vimrc.
So now I'm trying to figure out what in my vimrc is making it function. Possibly:
set encoding=utf8
Edit: Yep, that appears to do it. You probably want to have VIM defaulting to unicode anyway, these days...
None of the answers on this page were working for me until I found this hint from https://github.com/dougireton/mirror_pond/blob/master/vimrc - set shellxquote= [space character] was the missing piece.
if has("win32") || has("gui_win32")
if executable("PowerShell")
" Set PowerShell as the shell for running external ! commands
" http://stackoverflow.com/questions/7605917/system-with-powershell-in-vim
set shell=PowerShell
set shellcmdflag=-ExecutionPolicy\ RemoteSigned\ -Command
set shellquote=\"
" shellxquote must be a literal space character.
set shellxquote=
endif
endif
Combining the answers in this and the related thread, add the following to your $profile assuming you installed diffutils from chocolatey:
Remove-Item Alias:diff -force
And add the following to your ~/.vimrc:
if (has('win32') || has('gui_win32')) && executable('pwsh')
set shell=pwsh
set shellcmdflag=\ -ExecutionPolicy\ RemoteSigned\ -NoProfile\ -Nologo\ -NonInteractive\ -Command
endif
make sure shellcmdflag is exactly as shown
All credit for these solutions to their respective contributors, this is merely an aggregation post.
I propose an hackish solution. It doesn't really solve the problem, but it get the job done somehow.
This Vim plugin automate the creation of a temporary script file, powershell call through cmd.exe and paste of the result. It's not as nice as a proper powershell handling by vim, but it works.
Try instead
set shellcmdflag=\ -c
Explanation:
Vim uses tempname() to generate a temp file path that system() reads.
If &shell contains 'sh' and &shellcmdflag starts with '-'
then tempname() generates a temp file path with forward slashes.
Thus, if
set shell=powershell
set shellcmdflag=-c
then Vim will try to read a temp file with forward slashes that
cannot be found.
A remedy is to set instead
set shellcmdflag=\ -c
that is, add a whitespace to &shellcmdflag so that the first character
is no longer '-' and tempname() produces a temp file path with backward
slashes that can be found by system().
I remarked on the vim_dev mailing list ( https://groups.google.com/forum/#!topic/vim_dev/vTR05EZyfE0 ) that this deserves better documentation.
actf answer works for me, but because of Powershell built in DIFF (which is different from the Linux one) you must add this line to your Powershell profile to have diff working again in VIM:
Remove-Item Alias:diff -force
I'm running GVim v8.2 (Windows).
Using the fullpath to the executable works for me:
set shell=C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe
I don't use VIM but Powershell's default output is Unicode. Notepad can read unicode, you could use it to see if you are getting the output you expect.

Resources