Giter Site home page Giter Site logo

Comments (4)

lambdalisue avatar lambdalisue commented on August 15, 2024 1

Interesting. I'd love to see the PR.

My thought for the idea are

  1. I don't think the internal code need to be changed a lot. Adding extra BufReadPre and FileReadPre seems enough (not confirme)
  2. The behavior need to be optional while some users would prefer explicit way
  3. Using BufWritePre/FileWritePre may help for writing a file which only miss a write permission

Thanks for your idea :-]

from suda.vim.

doronbehar avatar doronbehar commented on August 15, 2024

Hello again @lambdalisue,

I've started working on this PR and I have already wrote a nice function that returns if sudo is needed at all for a given file, it considers if we know it exists and if we can read it's parent directories contents and their permissions.

I'm not sure whether a new configuration variable should be introduced or whether we should call suda#init() differently according to a g:suda#prefix which would be empty? My vision is that it would be much more elegant if a user wanting to change the prefix or make the plugin act automatically would add a single command in his init.vim, something like this:

call suda#init('sudo:*')

I must say that I find the current relationship between g:suda#prefix and suda#init() rather complicated. For example, IMO, having to write 2 lines just to change the prefix makes things a little bit confusing.

What do you think about this patch:

 autoload/suda.vim | 16 ++++++----------
 doc/suda.txt      |  5 ++---
 plugin/suda.vim   |  2 +-
 3 files changed, 9 insertions(+), 14 deletions(-)

diff --git c/autoload/suda.vim i/autoload/suda.vim
index fa623d9..5e0a3f4 100644
--- c/autoload/suda.vim
+++ i/autoload/suda.vim
@@ -1,11 +1,11 @@
-function! suda#init(...) abort
-  let pat = a:0 ? a:1 : printf('%s*', g:suda#prefix)
+function! suda#init(pat) abort
+  let g:suda#prefix = a:pat[:-2]
   augroup suda_internal
     autocmd! *
-    execute printf('autocmd BufReadCmd %s call suda#BufReadCmd()', pat)
-    execute printf('autocmd FileReadCmd %s call suda#FileReadCmd()', pat)
-    execute printf('autocmd BufWriteCmd %s call suda#BufWriteCmd()', pat)
-    execute printf('autocmd FileWriteCmd %s call suda#FileWriteCmd()', pat)
+    execute printf('autocmd BufReadCmd %s call suda#BufReadCmd()', a:pat)
+    execute printf('autocmd FileReadCmd %s call suda#FileReadCmd()', a:pat)
+    execute printf('autocmd BufWriteCmd %s call suda#BufWriteCmd()', a:pat)
+    execute printf('autocmd FileWriteCmd %s call suda#FileWriteCmd()', a:pat)
   augroup END
 endfunction
 
@@ -247,7 +247,3 @@ function! s:doautocmd(name) abort
         \ fnameescape(expand('<afile>'))
         \)
 endfunction
-
-
-" Configure
-let g:suda#prefix = get(g:, 'suda#prefix', 'suda://')
diff --git c/doc/suda.txt i/doc/suda.txt
index d0d28d4..7aead83 100644
--- c/doc/suda.txt
+++ i/doc/suda.txt
@@ -117,10 +117,9 @@ g:suda#prefix
 QUESTIONS					*suda-questions*
 
 Q. How to use "sudo:" instead of "suda://" like "sudo.vim".
-A. Use |g:suda#prefix|| like below.
+A. Call |suda#init| with a pattern of your choice.
 >
-	let g:suda#prefix = 'sudo:'
-	call suda#init('sudo:*,sudo:*/*')
+	call suda#init('sudo:*')
 <
 Q. How to use SudoRead/SudoWrite command like "sudo.vim".
 A. Write the following scripts in your |vimrc|.
diff --git c/plugin/suda.vim i/plugin/suda.vim
index 9ed1c74..e7187ae 100644
--- c/plugin/suda.vim
+++ i/plugin/suda.vim
@@ -4,5 +4,5 @@ endif
 let g:loaded_suda = 1
 
 if get(g:, 'suda_startup', 1)
-  call suda#init()
+  call suda#init('suda://*')
 endif

from suda.vim.

lambdalisue avatar lambdalisue commented on August 15, 2024

You said "make it completely automatic", mean that users with your optional feature actually do not need to use suda:// prefix or whatever.
So I think you don't need to care about g:suda#prefix, just overwrite the default value forcedly.

" autoload/suda.vim
function! suda#init(...) abort
  " ...
endfunction

function! suda#init_auto() abort
  let g:suda#prefix = 'suda://'
  call suda#init()
  " autocmd for normal files
  augroup suda_auto_internal
    autocmd BufReadCmd * call suda#YOUR_OPTIONAL_FEATURE_BufReadCmd()
    autocmd BufWriteCmd * call suda#YOUR_OPTIONAL_FEATURE_BufWriteCmd()
    " ...
  augroup END
endfunction

" plugin/suda.vim
if get(g:, 'suda_startup', 1)
  if get(g:, 'suda_automode')
  call suda#init_auto()
  else
  call suda#init()
  endif
endif

from suda.vim.

doronbehar avatar doronbehar commented on August 15, 2024

This seems rather too complicated IMO but I don't have enough experience to judge or suggest a better idea so I'll stick with this in the meantime.

from suda.vim.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.