Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Writing software is more fun than working.


computers / comp.editors / Re: In VIM, how to make sure b:is_bash is set (when it should be set)

SubjectAuthor
* In VIM, how to make sure b:is_bash is set (when it should be set)Kenny McCormack
+* Re: In VIM, how to make sure b:is_bash is set (when it should be set)Benjamin Esham
|`* Re: In VIM, how to make sure b:is_bash is set (when it should be set)Kenny McCormack
| `- Re: In VIM, how to make sure b:is_bash is set (when it should be set)Spiros Bousbouras
`* Re: In VIM, how to make sure b:is_bash is set (when it should be set)Spiros Bousbouras
 +- Re: In VIM, how to make sure b:is_bash is set (when it should be set)Spiros Bousbouras
 `- Re: In VIM, how to make sure b:is_bash is set (when it should be set)Kenny McCormack

1
In VIM, how to make sure b:is_bash is set (when it should be set)

<u7c5m0$1fufd$1@news.xmission.com>

  copy mid

https://www.rocksolidbbs.com/computers/article-flat.php?id=8&group=comp.editors#8

  copy link   Newsgroups: comp.editors
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.editors
Subject: In VIM, how to make sure b:is_bash is set (when it should be set)
Date: Mon, 26 Jun 2023 14:00:32 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <u7c5m0$1fufd$1@news.xmission.com>
Injection-Date: Mon, 26 Jun 2023 14:00:32 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="1571309"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Mon, 26 Jun 2023 14:00 UTC

I use GVIM to edit shell scripts. Almost all (but not all) of my scripts
are bash, so I generally want b:is_bash to be set so that syntax
highlighting works correctly (i.e., bash-isms, which I use liberally,
aren't flagged as errors).

AFAICT, the determination is made based in the #! line in the file. If
this is something recognized, like #!/bin/bash, then all is well. However,
the problem is that I often compile my own versions of bash, for various
reasons, so the #! line is likely to be something like: #!/home/userid/bin/bash_521

The built-in filetype stuff doesn't recognize this and thus: fail.

Now, over the years, I've worked out various kludges (hacks, workarounds,
whatever you want to call them). Here's the most recent one, and it seems
to work well enough, but I'm not real happy with it - hence the reason for
this post. In .vimrc, I have:

--- Cut Here ---
function! SetBash()
silent! let fle = readfile(expand("%"),'',&modelines)
for line in fle
if line =~ '^#.*bash'
exe 'let b:is_bash=1|set ft=sh|e'
endif
endfor
endfunction
au BufRead * :call SetBash()
--- Cut Here ---

Note, BTW, that you can't do this with ordinary "modelines", b/c (AIUI),
modelines only allows you to do "set"; it doesn't allow you to do "let".

So, what this all boils down to is: There should be a more systematic,
maker-approved, way to do this. But there doesn't seem to be. Or is there?

Note: I'm not looking for commentary or criticism or suggestions-for-improvement
on my code above. I posted it only to show one way of doing it.

--
Senator Marsha Blackburn (R-TN), who sits on the Judiciary Committee, said it was
"extremely inappropriate" for the president to nominate a Supreme Court justice on a
day ending with the letter "Y", and she said that "Biden is putting the demands of the
radical progressive left ahead of what is best for our nation."

Re: In VIM, how to make sure b:is_bash is set (when it should be set)

<rwvzu3.lg14es@bdesham.net>

  copy mid

https://www.rocksolidbbs.com/computers/article-flat.php?id=9&group=comp.editors#9

  copy link   Newsgroups: comp.editors
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: usenet@esham.io (Benjamin Esham)
Newsgroups: comp.editors
Subject: Re: In VIM, how to make sure b:is_bash is set (when it should be set)
Date: Mon, 26 Jun 2023 21:37:15 -0400
Organization: United Federation of Planets
Lines: 38
Message-ID: <rwvzu3.lg14es@bdesham.net>
References: <u7c5m0$1fufd$1@news.xmission.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="f56acb4d0ddfcc122ced99a4d8ce9e19";
logging-data="1217226"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX187a42ZsGi28Pr20Fc9/ERDNMgs4YXmgbmq4XktxYQ73A=="
User-Agent: Gnus/5.13 (macOS)
Cancel-Lock: sha1:v13moqX3DLynwfS1eOr71QAJYYU=
sha1:4LnBZ6r82xvB8zbGxD9usO39LZA=
 by: Benjamin Esham - Tue, 27 Jun 2023 01:37 UTC

Kenny McCormack wrote:

> I use GVIM to edit shell scripts. Almost all (but not all) of my scripts
> are bash, so I generally want b:is_bash to be set so that syntax
> highlighting works correctly (i.e., bash-isms, which I use liberally,
> aren't flagged as errors).
>
> AFAICT, the determination is made based in the #! line in the file. If
> this is something recognized, like #!/bin/bash, then all is well.
> However, the problem is that I often compile my own versions of bash, for
> various reasons, so the #! line is likely to be something like:
> #!/home/userid/bin/bash_521
>
> [snip]
>
> So, what this all boils down to is: There should be a more systematic,
> maker-approved, way to do this. But there doesn't seem to be. Or is
> there?

To the best of my knowledge, there's no built-in way to get exactly the
behavior you want.

Since you already have a working solution, I'm guessing you aren't
interested in installing a third-party script to help with this. If you
were, I would recommend vim-shebang [1]. With that script in place, I think
you could get your desired behavior by putting

AddShebangPattern! sh ^#!.*/bin/bash_.\+ let\ b:is_bash=1

in your vimrc. Here the second argument ("sh") is the filetype to set, the
third argument is the regex to look for in the first line of the file--you
can make this more or less specific to your taste--and the third argument is
an additional bit of vimscript to run when a matching file is opened.

Benjamin

[1] https://github.com/vitalk/vim-shebang

Re: In VIM, how to make sure b:is_bash is set (when it should be set)

<u7eaon$1h1p6$1@news.xmission.com>

  copy mid

https://www.rocksolidbbs.com/computers/article-flat.php?id=10&group=comp.editors#10

  copy link   Newsgroups: comp.editors
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.editors
Subject: Re: In VIM, how to make sure b:is_bash is set (when it should be set)
Date: Tue, 27 Jun 2023 09:39:35 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <u7eaon$1h1p6$1@news.xmission.com>
References: <u7c5m0$1fufd$1@news.xmission.com> <rwvzu3.lg14es@bdesham.net>
Injection-Date: Tue, 27 Jun 2023 09:39:35 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="1607462"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Tue, 27 Jun 2023 09:39 UTC

In article <rwvzu3.lg14es@bdesham.net>,
Benjamin Esham <usenet@esham.io> wrote:
....
>> So, what this all boils down to is: There should be a more systematic,
>> maker-approved, way to do this. But there doesn't seem to be. Or is
>> there?
>
>To the best of my knowledge, there's no built-in way to get exactly the
>behavior you want.

Thanks for your response. Yes, it seems there's no built-in way.
I would argue that wherever the detection code is, it should be made more
general (i.e., more forgiving). The code seems to be looking specifically
for "/bin/bash". I would think that any string (in the first line of the
file, say) that contains "bash" should be enough to trigger bash mode.

Note, incidentally, that I also want to catch the case of non-executable
files that contain bash code. I.e., something like a .profile or .bashrc
normally is not marked as executable (i.e., mode 755 or similar) and also
does not start with #!/bin/bash, but still should be edited as with ft=sh
and b:is_bash=1. Also, files containing function definitions - e.g.,
something like:

myfun() {
...
}

is also to be edited as bash code.

>Since you already have a working solution, I'm guessing you aren't
>interested in installing a third-party script to help with this. If you
>were, I would recommend vim-shebang [1]. With that script in place, I think
>you could get your desired behavior by putting
>
> AddShebangPattern! sh ^#!.*/bin/bash_.\+ let\ b:is_bash=1
>
>in your vimrc. Here the second argument ("sh") is the filetype to set, the
>third argument is the regex to look for in the first line of the file--you
>can make this more or less specific to your taste--and the third argument is
>an additional bit of vimscript to run when a matching file is opened.

Interesting. As you say, about the same as my existing solution, but I
will take a look at it when I get a chance.

BTW, my knowledge/skill of vimscript is spotty. It has always been on a
"need-know" basis; I figure things out as needed, but don't really have a
good grasp on the language. But I think I just realized that the original
code that I posted (not contained in this latest post) should have a
"break" after the "exe ..." statement. Once you've done it, no need to
keep looping, right?

--
Most Southerners interest in, knowledge of, and participation in politics begins with
and ends with: Screw the blacks. If a guy is onboard with that, he's our guy!

Get them back in chains where they belong!

Re: In VIM, how to make sure b:is_bash is set (when it should be set)

<QXGcraEZfbz0wzw1B@bongo-ra.co>

  copy mid

https://www.rocksolidbbs.com/computers/article-flat.php?id=11&group=comp.editors#11

  copy link   Newsgroups: comp.editors
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: spibou@gmail.com (Spiros Bousbouras)
Newsgroups: comp.editors
Subject: Re: In VIM, how to make sure b:is_bash is set (when it should be set)
Date: Wed, 28 Jun 2023 12:52:04 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <QXGcraEZfbz0wzw1B@bongo-ra.co>
References: <u7c5m0$1fufd$1@news.xmission.com> <rwvzu3.lg14es@bdesham.net> <u7eaon$1h1p6$1@news.xmission.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 28 Jun 2023 12:52:04 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="edd7eca4a0f4888d2d63bc5eab897523";
logging-data="1873961"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19S2A5F5jz3QIkp+IcnZYnU"
Cancel-Lock: sha1:Ya5NxihweSvgvdFRZsI1YOQ/8U8=
X-Server-Commands: nowebcancel
In-Reply-To: <u7eaon$1h1p6$1@news.xmission.com>
X-Organisation: Weyland-Yutani
 by: Spiros Bousbouras - Wed, 28 Jun 2023 12:52 UTC

On Tue, 27 Jun 2023 09:39:35 -0000 (UTC)
gazelle@shell.xmission.com (Kenny McCormack) wrote:
> In article <rwvzu3.lg14es@bdesham.net>,
> Benjamin Esham <usenet@esham.io> wrote:
> ...
> >> So, what this all boils down to is: There should be a more systematic,
> >> maker-approved, way to do this. But there doesn't seem to be. Or is
> >> there?
> >
> >To the best of my knowledge, there's no built-in way to get exactly the
> >behavior you want.
>
> Thanks for your response. Yes, it seems there's no built-in way.
> I would argue that wherever the detection code is, it should be made more
> general (i.e., more forgiving). The code seems to be looking specifically
> for "/bin/bash". I would think that any string (in the first line of the
> file, say) that contains "bash" should be enough to trigger bash mode.
>
> Note, incidentally, that I also want to catch the case of non-executable
> files that contain bash code. I.e., something like a .profile or .bashrc
> normally is not marked as executable (i.e., mode 755 or similar) and also
> does not start with #!/bin/bash, but still should be edited as with ft=sh
> and b:is_bash=1. Also, files containing function definitions - e.g.,
> something like:
>
> myfun() {
> ...
> }
>
> is also to be edited as bash code.

For this level of generality , I can't think of an algorithm to do the
detection. Can you ? Personally , in my general vim start-up files I
have a line
command Shell source $VIM_SCRIPTS/shell.vim

and I enter :Shell when I want to edit shell code. So I don't use
any automatic detection.

> BTW, my knowledge/skill of vimscript is spotty. It has always been on a
> "need-know" basis; I figure things out as needed, but don't really have a
> good grasp on the language. But I think I just realized that the original
> code that I posted (not contained in this latest post) should have a
> "break" after the "exe ..." statement. Once you've done it, no need to
> keep looping, right?

Or you can do a return .But your code has edit so I don't know what
happens if an edit command is embedded in surrounding code. Does the rest
of the code get executed when you finish editing the file or is it simply
abandoned in which case it doesn't matter if you add break or return .

I will make a different post with improvements for you code but you
probably won't read it since you're not interested in improvements.

--
vlaho.ninja/prog

Re: In VIM, how to make sure b:is_bash is set (when it should be set)

<6AlaQ9TiRTeENcxJ5@bongo-ra.co>

  copy mid

https://www.rocksolidbbs.com/computers/article-flat.php?id=12&group=comp.editors#12

  copy link   Newsgroups: comp.editors
Path: i2pn2.org!i2pn.org!paganini.bofh.team!not-for-mail
From: spibou@gmail.com (Spiros Bousbouras)
Newsgroups: comp.editors
Subject: Re: In VIM, how to make sure b:is_bash is set (when it should be set)
Date: Wed, 28 Jun 2023 13:10:16 -0000 (UTC)
Organization: To protect and to server
Message-ID: <6AlaQ9TiRTeENcxJ5@bongo-ra.co>
References: <u7c5m0$1fufd$1@news.xmission.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 28 Jun 2023 13:10:16 -0000 (UTC)
Injection-Info: paganini.bofh.team; logging-data="3728803"; posting-host="9H7U5kayiTdk7VIdYU44Rw.user.paganini.bofh.team"; mail-complaints-to="usenet@bofh.team"; posting-account="9dIQLXBM7WM9KzA+yjdR4A";
Cancel-Lock: sha256:GV3RA46tJ5AqhsU3ccAdsJLc6Df96EahMW1ix4lFVX4=
X-Notice: Filtered by postfilter v. 0.9.3
X-Organisation: Weyland-Yutani
X-Server-Commands: nowebcancel
 by: Spiros Bousbouras - Wed, 28 Jun 2023 13:10 UTC

On Mon, 26 Jun 2023 14:00:32 -0000 (UTC)
gazelle@shell.xmission.com (Kenny McCormack) wrote:
> --- Cut Here ---
> function! SetBash()
> silent! let fle = readfile(expand("%"),'',&modelines)
> for line in fle
> if line =~ '^#.*bash'
> exe 'let b:is_bash=1|set ft=sh|e'
> endif
> endfor
> endfunction
> au BufRead * :call SetBash()
> --- Cut Here ---

The autocommand BufRead is for "When starting to edit a new buffer, after
reading the file into the buffer" so it doesn't make sense to do a readfile()
, vim has just read the file. I also don't understand what's the point of
having

exe 'let b:is_bash=1|set ft=sh|e'

as opposed to

let b:is_bash=1
set ft=sh
e

..What does it buy you to put the 3 commands in one string and do an
execute ?

I would write it like this (untested) :

function! SetBash()
let l:end = line('$')
let l:i = 1
while l:i <= l:end && l:i <= &modelines
if getline(l:i) =~ '^#.*bash'
let b:is_bash = 1
set ft=sh
break
endif
l:i += 1
endwhile
endfunction
autocommand BufRead * :call SetBash()

Regarding having function! as opposed to function :
I don't know in what way you have things (dis)organised so that
SetBash() gets redefined each time you read whatever file.

> Note: I'm not looking for commentary or criticism or suggestions-for-improvement
> on my code above. I posted it only to show one way of doing it.

Noted.

--
This media type UST NOT be used unless the sender knows that the recipient
can arse it
RFC 2616

Re: In VIM, how to make sure b:is_bash is set (when it should be set)

<wAheB6SlUN+1RnAHe@bongo-ra.co>

  copy mid

https://www.rocksolidbbs.com/computers/article-flat.php?id=13&group=comp.editors#13

  copy link   Newsgroups: comp.editors
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: spibou@gmail.com (Spiros Bousbouras)
Newsgroups: comp.editors
Subject: Re: In VIM, how to make sure b:is_bash is set (when it should be set)
Date: Wed, 28 Jun 2023 13:29:40 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <wAheB6SlUN+1RnAHe@bongo-ra.co>
References: <u7c5m0$1fufd$1@news.xmission.com> <6AlaQ9TiRTeENcxJ5@bongo-ra.co>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 28 Jun 2023 13:29:40 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="edd7eca4a0f4888d2d63bc5eab897523";
logging-data="1881568"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19ZN3jINmqydzarji0liLPz"
Cancel-Lock: sha1:5cDBtkVsTeNqEvQgoOG4X2U5Hag=
X-Server-Commands: nowebcancel
X-Organisation: Weyland-Yutani
In-Reply-To: <6AlaQ9TiRTeENcxJ5@bongo-ra.co>
 by: Spiros Bousbouras - Wed, 28 Jun 2023 13:29 UTC

On Wed, 28 Jun 2023 13:10:16 -0000 (UTC)
Spiros Bousbouras <spibou@gmail.com> wrote:
> function! SetBash()
> let l:end = line('$')
> let l:i = 1
> while l:i <= l:end && l:i <= &modelines
> if getline(l:i) =~ '^#.*bash'
> let b:is_bash = 1
> set ft=sh
> break
> endif
> l:i += 1

let l:i += 1

> endwhile
> endfunction
> autocommand BufRead * :call SetBash()

Re: In VIM, how to make sure b:is_bash is set (when it should be set)

<u7hef8$1ii8c$1@news.xmission.com>

  copy mid

https://www.rocksolidbbs.com/computers/article-flat.php?id=14&group=comp.editors#14

  copy link   Newsgroups: comp.editors
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.editors
Subject: Re: In VIM, how to make sure b:is_bash is set (when it should be set)
Date: Wed, 28 Jun 2023 14:01:12 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <u7hef8$1ii8c$1@news.xmission.com>
References: <u7c5m0$1fufd$1@news.xmission.com> <6AlaQ9TiRTeENcxJ5@bongo-ra.co>
Injection-Date: Wed, 28 Jun 2023 14:01:12 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="1657100"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Wed, 28 Jun 2023 14:01 UTC

In article <6AlaQ9TiRTeENcxJ5@bongo-ra.co>,
Spiros Bousbouras <spibou@gmail.com> wrote:
....
>The autocommand BufRead is for "When starting to edit a new buffer, after
>reading the file into the buffer" so it doesn't make sense to do a readfile()
>, vim has just read the file. I also don't understand what's the point of
>having

You have to do an explicit "e" in order for the syntax highlighting to be
correct. Otherwise, the screen stays as it was (with the bash-isms flagged
as errors).

I haven't tested this in a while, but I believe that was how it was when I
wrote that code (which was quite some time ago).

Oh, and to answer another of your questions: No, I'm not looking for a
magic algorithm for detecting that I'm editing shell code. The built-in
stuff works fine for that part. The issue is setting it specifically to
"bash" mode. And, no, I'm not looking for a magic "analyze the text"
algorithm there, either. I just want to be able to put something like:

# Yes, this is bash code

somewhere in the first five lines of the file, and have it see that and do
the right thing. Which is what my current solution does.

Or, alternatively, just have it recognize that if the first line is
something like:

#!/path/to/someother/version/of/bash_512OrWhatever

Then this is probably bash code, even if it isn't just: #!/bin/bash

--
https://en.wikipedia.org/wiki/Mansplaining

It describes comp.lang.c to a T!


computers / comp.editors / Re: In VIM, how to make sure b:is_bash is set (when it should be set)

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor