open 0.1.30 → 0.2.22

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +110 -31
  3. data/bin/open +1 -1
  4. data/bin/open_in_browser +1 -1
  5. data/doc/README.gen +72 -8
  6. data/doc/deprecated_code/deprecated_code.md +26 -0
  7. data/img/open_logo.png +0 -0
  8. data/lib/open/base/base.rb +265 -128
  9. data/lib/open/books/README.md +8 -0
  10. data/lib/open/books/books.rb +100 -0
  11. data/lib/open/constants/constants.rb +86 -8
  12. data/lib/open/in_browser/in_browser.rb +501 -319
  13. data/lib/open/in_editor/in_editor.rb +367 -162
  14. data/lib/open/last/last.rb +14 -12
  15. data/lib/open/last_url/last_url.rb +4 -3
  16. data/lib/open/nano_open/nano_open.rb +30 -5
  17. data/lib/open/{open.rb → open/open.rb} +596 -654
  18. data/lib/open/project/project.rb +3 -2
  19. data/lib/open/requires/failsafe_require_of_beautiful_url.rb +9 -0
  20. data/lib/open/requires/require_the_project.rb +2 -2
  21. data/lib/open/requires/require_yaml.rb +13 -0
  22. data/lib/open/these_files/these_files.rb +1 -1
  23. data/lib/open/toplevel_methods/browser.rb +71 -0
  24. data/lib/open/toplevel_methods/delay.rb +23 -0
  25. data/lib/open/toplevel_methods/e.rb +14 -0
  26. data/lib/open/toplevel_methods/editor.rb +41 -0
  27. data/lib/open/toplevel_methods/host_os.rb +25 -0
  28. data/lib/open/toplevel_methods/is_on_roebe.rb +16 -0
  29. data/lib/open/toplevel_methods/is_on_windows.rb +26 -0
  30. data/lib/open/toplevel_methods/misc.rb +50 -0
  31. data/lib/open/version/version.rb +2 -2
  32. data/lib/open/with_delay/with_delay.rb +10 -11
  33. data/open.gemspec +3 -3
  34. data/test/testing_open.rb +1 -1
  35. data/test/testing_open_in_browser.rb +16 -0
  36. data/test/testing_open_via_launchy.rb +3 -0
  37. data/test/testing_shortcuts.rb +3 -0
  38. metadata +25 -9
  39. data/lib/open/toplevel_code/toplevel_code.rb +0 -189
@@ -5,15 +5,15 @@
5
5
  # === Open::Open
6
6
  #
7
7
  # This class will simply open a given file. It may also "open" the file
8
- # in the browser, that is, to use primarily firefox to display the file
9
- # content as such.
8
+ # in the browser, that is, to use firefox or chrome or thorium to
9
+ # display the file content as such.
10
10
  #
11
11
  # Usage example:
12
12
  #
13
13
  # Open::Open.new(ARGV)
14
14
  #
15
15
  # =========================================================================== #
16
- # require 'open/open.rb'
16
+ # require 'open/open/open.rb'
17
17
  # Open::Open.new(ARGV)
18
18
  # =========================================================================== #
19
19
  require 'open/base/base.rb'
@@ -22,13 +22,13 @@ module Open
22
22
 
23
23
  class Open < ::Open::Base # === Open::Open
24
24
 
25
- require 'open/in_editor/in_editor.rb'
26
25
  require 'open/in_browser/in_browser.rb'
27
- require 'open/toplevel_code/toplevel_code.rb'
26
+ require 'open/in_editor/in_editor.rb'
27
+ require 'open/toplevel_methods/misc.rb'
28
28
 
29
29
  begin
30
30
  require 'roebe/classes/find_expanded_alias.rb'
31
- rescue LoadError; end
31
+ rescue LoadError => error; puts error; end
32
32
 
33
33
  # ========================================================================= #
34
34
  # === LIBREOFFICE
@@ -41,7 +41,7 @@ class Open < ::Open::Base # === Open::Open
41
41
  if ENV['PATH_TO_THE_LIBREOFFICE_EXECUTABLE']
42
42
  LIBREOFFICE = ENV['PATH_TO_THE_LIBREOFFICE_EXECUTABLE'].to_s.dup
43
43
  else
44
- LIBREOFFICE = '/usr/bin/soffice'
44
+ LIBREOFFICE = '/usr/bin/soffice' # This will be valid for most users of the gem.
45
45
  end
46
46
 
47
47
  # ========================================================================= #
@@ -63,26 +63,6 @@ class Open < ::Open::Base # === Open::Open
63
63
  # ========================================================================= #
64
64
  USE_THIS_DELAY = 0.65
65
65
 
66
- # ========================================================================= #
67
- # === ARRAY_EXTENSIONS_THAT_CAN_BE_OPENED_IN_THE_EDITOR
68
- #
69
- # All extensions that can be opened in the editor should be registered
70
- # in this constant.
71
- #
72
- # For example, "h" means ".h" aka a C or C++ header file.
73
- # ========================================================================= #
74
- ARRAY_EXTENSIONS_THAT_CAN_BE_OPENED_IN_THE_EDITOR = %w(
75
- php rb txt yaml yml cgi
76
- md gemspec html csv py
77
- conf c sh js log css cr
78
- ascii la pl perl json
79
- js fasta fa m3u cpp
80
- go gff gff3
81
- gen h
82
- erb sinatra
83
- sql
84
- ).sort << '' # The last part is for files like "TODO" which lack an extension.
85
-
86
66
  # ========================================================================= #
87
67
  # === ARRAY_LIBREOFFICE_EXTENSIONS
88
68
  #
@@ -104,42 +84,18 @@ class Open < ::Open::Base # === Open::Open
104
84
  pps
105
85
  )
106
86
 
107
- # ========================================================================= #
108
- # === ARRAY_IMAGES
109
- #
110
- # All file-extensions for images can be registered in this Array.
111
- # ========================================================================= #
112
- ARRAY_IMAGES = %w(
113
- png
114
- jpg
115
- gif
116
- jpeg
117
- tiff
118
- tif
119
- bmp
120
- )
121
-
122
- # ========================================================================= #
123
- # === ARRAY_VIDEOS
124
- # ========================================================================= #
125
- ARRAY_VIDEOS = %w(
126
- vob
127
- avi
128
- flv
129
- mp3
130
- mp4
131
- )
132
-
133
87
  # ========================================================================= #
134
88
  # === initialize
135
89
  # ========================================================================= #
136
90
  def initialize(
137
- i = nil,
138
- run_already = true,
91
+ commandline_arguments = ARGV,
92
+ run_already = true,
139
93
  &block
140
94
  )
141
95
  reset
142
- set_input(i)
96
+ set_commandline_arguments(
97
+ commandline_arguments
98
+ )
143
99
  # ======================================================================= #
144
100
  # === Handle blocks next
145
101
  # ======================================================================= #
@@ -159,7 +115,7 @@ class Open < ::Open::Base # === Open::Open
159
115
  # === :open_via_fullscreen
160
116
  # =================================================================== #
161
117
  if yielded.has_key? :open_via_fullscreen
162
- @open_via_fullscreen = yielded.delete(:open_via_fullscreen)
118
+ @internal_hash[:open_via_fullscreen] = yielded.delete(:open_via_fullscreen)
163
119
  end
164
120
  end
165
121
  end
@@ -173,75 +129,102 @@ class Open < ::Open::Base # === Open::Open
173
129
  super()
174
130
  infer_the_namespace
175
131
  # ======================================================================= #
176
- # === @image_editor
132
+ # === @internal_hash
133
+ # ======================================================================= #
134
+ @internal_hash = {}
177
135
  # ======================================================================= #
178
- @image_editor = 'gimp'
136
+ # === :open_via_fullscreen
179
137
  # ======================================================================= #
180
- # === @editor
138
+ @internal_hash[:open_via_fullscreen] = false
181
139
  # ======================================================================= #
182
- @editor = use_which_editor?
140
+ # === :set_the_main_book
183
141
  # ======================================================================= #
184
- # === @set_the_main_book
142
+ @internal_hash[:set_the_main_book] = false
185
143
  # ======================================================================= #
186
- @set_the_main_book = false
144
+ # === :image_editor_to_use
187
145
  # ======================================================================= #
188
- # === @open_via_fullscreen
146
+ @internal_hash[:image_editor_to_use] = 'gimp'
189
147
  # ======================================================================= #
190
- @open_via_fullscreen = false
148
+ # === :editor
149
+ # ======================================================================= #
150
+ set_editor(use_which_editor?)
151
+ # ======================================================================= #
152
+ # === :open_these_elements
153
+ # ======================================================================= #
154
+ @internal_hash[:open_these_elements] = []
191
155
  end
192
156
 
193
157
  # ========================================================================= #
194
158
  # === set_the_main_book?
195
159
  # ========================================================================= #
196
160
  def set_the_main_book?
197
- @set_the_main_book
161
+ @internal_hash[:set_the_main_book]
198
162
  end
199
163
 
200
164
  # ========================================================================= #
201
- # === set_use_this_editor
165
+ # === open_in_editor (open tag, open1 tag)
166
+ #
167
+ # This method will simply delegate towards class Open::InEditor.
168
+ #
169
+ # Take note that Open.in_editor() will already handle delays, so
170
+ # we do not have to do anything special here.
202
171
  # ========================================================================= #
203
- def set_use_this_editor(
204
- i = use_which_editor?
205
- )
206
- @editor = i.to_s
207
- end
172
+ def open_in_editor(i)
173
+ ::Open.in_editor(i)
174
+ end; alias try_to_open_this_file_in_the_editor open_in_editor # === try_to_open_this_file_in_the_editor
175
+ alias consider_opening_the_given_input open_in_editor # === consider_opening_the_given_input
176
+ alias open_these_files open_in_editor # === open_these_files
177
+ alias do_open_in_editor open_in_editor # === do_open_in_editor
208
178
 
209
179
  # ========================================================================= #
210
- # === replace_localhost_with_data
180
+ # === try_to_require_the_studium_project
211
181
  # ========================================================================= #
212
- def replace_localhost_with_data(i)
213
- if i.include? 'programming'
214
- return i.sub('localhost','/home/x/')
215
- else
216
- return i.sub('localhost','/home/x/data/')
182
+ def try_to_require_the_studium_project
183
+ unless Object.const_defined?(:Studium) and
184
+ Studium.respond_to?(:include_this_exam_topic?)
185
+ begin # We pull in only the relevant files from the Studium project.
186
+ require 'studium/constants/constants.rb'
187
+ require 'studium/toplevel_methods/toplevel_methods.rb'
188
+ rescue LoadError
189
+ if is_on_roebe?
190
+ e 'The two files ('\
191
+ 'studium/constants/constants.rb,'\
192
+ 'studium/toplevel_methods/toplevel_methods.rb '\
193
+ 'forth) from the studium-project could not be loaded.'
194
+ end
195
+ end
217
196
  end
218
197
  end
219
198
 
220
199
  # ========================================================================= #
221
- # === set_input
200
+ # === show_help (help tag)
222
201
  # ========================================================================= #
223
- def set_input(i = ARGV)
224
- i = [i] unless i.is_a? Array
225
- i.map! {|entry|
226
- entry = entry.to_s.dup
227
- # ===================================================================== #
228
- # Split away at '::' if this is part of that substring.
229
- # ===================================================================== #
230
- if entry.include? '::'
231
- splitted = entry.split('::')
232
- entry = splitted.last
233
- end
234
- if entry.include? '^'
235
- entry.delete!('^')
236
- end
237
- case entry
238
- # ===================================================================== #
239
- # === open --everything
240
- #
241
- # This actually refers only to files, so we select for files next.
242
- # ===================================================================== #
243
- when /^-?-?everything?$/i
244
- entry = Dir['**/**'].select {|inner_entry| File.file?(inner_entry) }
202
+ def show_help
203
+ opne 'The following options are available:'
204
+ e
205
+ ecomment ' SELF # Open this .rb file here.'
206
+ ecomment ' --use-this-browser=firefox # Assign a new browser, in this case firefox.'
207
+ ecomment ' --browser? # '\
208
+ 'Show which browser is the currently assigned one.'
209
+ ecomment ' --everything # '\
210
+ 'This will open every file in the current working directory, and '\
211
+ 'all subdirectories.'
212
+ ecomment ' --important-exam # '\
213
+ 'Open all important exams. Only useful if the studium gem is '\
214
+ 'also installed.'
215
+ e
216
+ end
217
+
218
+ # ========================================================================= #
219
+ # === menu (menu tag)
220
+ # ========================================================================= #
221
+ def menu(
222
+ i = commandline_arguments?
223
+ )
224
+ if i.is_a? Array
225
+ i.each {|entry| menu(entry) }
226
+ else
227
+ case i # (case tag)
245
228
  # ===================================================================== #
246
229
  # === open --important-exam
247
230
  # ===================================================================== #
@@ -249,140 +232,46 @@ class Open < ::Open::Base # === Open::Open
249
232
  base_dir = ENV['USERS']
250
233
  base_dir = '/home' if base_dir.nil?
251
234
  entry = "#{base_dir}/x/programming/ruby/src/studium/lib/studium/yaml/important_exams.yml"
252
- end
253
- # ===================================================================== #
254
- # Get rid of localhost next. This must be handled with more care,
255
- # because some files may include the string 'localhost' as part of
256
- # their name, such as 'remove_localhost.rb'. In this case, we will
257
- # additionally check whether the file exists.
258
- # ===================================================================== #
259
- if entry.is_a?(String) and entry.include?('localhost')
260
- unless File.exist? entry # See the above header for an explanation.
261
- entry = replace_localhost_with_data(entry)
262
- end
263
- end
264
- if entry.is_a? String
265
- entry.chop! if entry.end_with? '#' # Don't need trailing '#' tokens.
266
- entry.chop! if entry.end_with? ':-' # ← This check must come before the check against ':'.
267
- entry.chop! if entry.end_with? ':' # Chop off trailing ':' tokens.
268
- # ===================================================================== #
269
- # Next, we handle input such as:
270
- # 'lib/lpc/requires/basic_requires.rb:begin'
271
- # This evidently does not end with a ':' character, but it clearly
272
- # has a fairly useless ':' component in it. If that is the case,
273
- # we will treat this as input that is only valid up towards the
274
- # very right ':', anchored on the '.rb:' part.
275
- # ===================================================================== #
276
- if entry.include? '.rb:'
277
- # =================================================================== #
278
- # The following code means to find the index at '.rb:' and then add
279
- # 2 to it - this should be the correct name of the entry at hand.
280
- # =================================================================== #
281
- entry = entry[0 .. (entry.index('.rb:')+2)]
282
- end
283
- if entry.end_with?(',') and !File.exist?(',')
284
- entry.chop!
285
- elsif entry.end_with?('.') and File.exist?(entry[0..-2])
286
- # =================================================================== #
287
- # This clause will be entered if we have input something like
288
- # "colours.rb." where the user may have wanted to type in
289
- # "colours.rb" - and IF the file exists.
290
- # =================================================================== #
291
- opnn; e "Assuming a superfluous trailing '.', so removing "\
292
- "the last '.' there."
293
- entry.chop!
294
- end
295
- end
296
- case entry # Work only on the first entry. (case tag)
235
+ @internal_hash[:open_these_elements].clear
236
+ @internal_hash[:open_these_elements] << entry
297
237
  # ===================================================================== #
298
- # === open ME
238
+ # === open --everything
239
+ #
240
+ # This actually refers only to files, so we select for files next.
299
241
  # ===================================================================== #
300
- when 'ME',
301
- 'SELF',
302
- 'ALL',/
303
- ^-?-?this(_|-)?file/
304
- open_this_file_here
305
- exit
242
+ when /^-?-?everything?$/i
243
+ elements = Dir['**/**'].select {|inner_entry| File.file?(inner_entry) }
244
+ @internal_hash[:open_these_elements].clear
245
+ @internal_hash[:open_these_elements] << elements
306
246
  # ===================================================================== #
307
247
  # === open --help
308
248
  # ===================================================================== #
309
249
  when /^-?-?help$/i,
310
- 'show_help'
250
+ /^-?-?show(-|_)?help$/i
311
251
  show_help
312
252
  exit
313
- end unless File.exist?(entry.to_s)
314
- entry
315
- }
316
- @input = i # This must be an Array.
317
- end; alias set_input_files set_input # === set_input_files
318
-
319
- # ========================================================================= #
320
- # === input?
321
- # ========================================================================= #
322
- def input?
323
- @input
324
- end; alias input_file? input? # === input_file?
325
- alias input_files? input? # === input_files?
326
-
327
- # ========================================================================= #
328
- # === check_whether_the_user_has_provided_any_input
329
- # ========================================================================= #
330
- def check_whether_the_user_has_provided_any_input
331
- _ = input_files?
332
- local_files = Dir['*'].select {|entry| File.file? entry }
333
- if _.empty? and !local_files.empty? and (local_files.size == 1)
334
253
  # ===================================================================== #
335
- # Handle the case where no specific input was given, but only
336
- # one file exists in the local directory.
254
+ # === open --browser?
337
255
  # ===================================================================== #
338
- set_input_files(local_files.first)
339
- _ = input_files?
340
- end
341
- if _.empty?
342
- if is_on_roebe? and !is_bluefish_running?
343
- start_bluefish
344
- else
345
- opnn; e 'Please provide at least one argument - the file '\
346
- 'you wish to open.'
256
+ when /^-?-?browser\??$/
257
+ _ = YAML.load_file(::Open.file_browser?)
258
+ e "#{rev}The browser currently in use is `#{sfancy(_)}#{rev}`."
259
+ exit
260
+ # ===================================================================== #
261
+ # === open --use-this-browser=firefox
262
+ # ===================================================================== #
263
+ when /^-?-?use(-|_)?this(-|_)?browser=(.+)/
264
+ ::Open.permanently_use_this_browser = $3.to_s.dup
347
265
  exit
348
266
  end
349
267
  end
350
268
  end
351
269
 
352
270
  # ========================================================================= #
353
- # === start_bluefish
354
- # ========================================================================= #
355
- def start_bluefish
356
- esystem 'bluefish &'
357
- end
358
-
359
- # ========================================================================= #
360
- # === is_bluefish_running?
361
- # ========================================================================= #
362
- def is_bluefish_running?
363
- `ps ax | grep bluefish`.split("\n").reject {|entry|
364
- entry.include? 'grep'
365
- }.size > 0
366
- end
367
-
368
- # ========================================================================= #
369
- # === consider_padding
370
- #
371
- # Simply add a surrounding '"' to the given input, so that any resulting
372
- # system() call will still work if we have awkward filenames.
373
- # ========================================================================= #
374
- def consider_padding(i)
375
- if i.include?(' ') or i.include?('(')
376
- i = '"'+i+'"'
377
- end
378
- return i
379
- end
380
-
381
- # ========================================================================= #
382
- # === play_multimedia_file
271
+ # === editor?
383
272
  # ========================================================================= #
384
- def play_multimedia_file(i)
385
- esystem "#{USE_THIS_MPLAYER_COMMAND} #{i}" # open
273
+ def editor?
274
+ @internal_hash[:editor]
386
275
  end
387
276
 
388
277
  # ========================================================================= #
@@ -392,13 +281,6 @@ class Open < ::Open::Base # === Open::Open
392
281
  do_open_in_editor(this_file?+IN_BACKGROUND)
393
282
  end
394
283
 
395
- # ========================================================================= #
396
- # === return_this_file_here
397
- # ========================================================================= #
398
- def return_this_file_here
399
- __FILE__
400
- end; alias this_file? return_this_file_here # === this_file?
401
-
402
284
  # ========================================================================= #
403
285
  # === delay?
404
286
  # ========================================================================= #
@@ -407,186 +289,70 @@ class Open < ::Open::Base # === Open::Open
407
289
  end
408
290
 
409
291
  # ========================================================================= #
410
- # === find_and_return_last_file
411
- # ========================================================================= #
412
- def find_and_return_last_file
413
- array = Dir['*'].reject {|entry| ! File.file? entry }
414
- array.map! {|entry|
415
- [entry, File.ctime(entry)]
416
- }
417
- result = array.sort_by {|entry| entry[1] }.reverse
418
- result = result.first[0]
419
- return result
420
- end
421
-
422
- # ========================================================================= #
423
- # === can_be_opened_in_the_editor?
292
+ # === set_use_this_editor
424
293
  # ========================================================================= #
425
- def can_be_opened_in_the_editor?(i)
426
- _ = File.extname(i).delete('.')
427
- ARRAY_EXTENSIONS_THAT_CAN_BE_OPENED_IN_THE_EDITOR.include? i
428
- end
294
+ def set_use_this_editor(
295
+ i = use_which_editor?
296
+ )
297
+ @internal_hash[:editor] = i.to_s
298
+ end; alias set_editor set_use_this_editor # === set_editor
429
299
 
430
300
  # ========================================================================= #
431
- # === try_to_open_this_file_in_the_editor
301
+ # === open_in_pdf_viewer (pdf tag)
302
+ #
303
+ # This method can be used to open a .pdf file. We will let the project
304
+ # PdfParadise handle this situation.
305
+ #
306
+ # Since as of 19.09.2018 we will keep a log of the opened .pdf files
307
+ # if we are on roebe. This can then be used to determine which were
308
+ # the last .pdf files that were read.
432
309
  # ========================================================================= #
433
- def try_to_open_this_file_in_the_editor(
434
- i = input_files?
435
- )
310
+ def open_in_pdf_viewer(i)
311
+ require 'pdf_paradise' unless Object.const_defined? :PdfParadise
436
312
  # ======================================================================= #
437
- # === First check whether we have passed an Array
313
+ # Since as of April 2022 we will use the absolute path to the .pdf
314
+ # file at hand. The reason for this is because this makes it easier
315
+ # to know which files are open, and we can store these file paths
316
+ # into a local file (such as a .yml file), and then use this at a
317
+ # later time to restore all .pdf files that were open. This thus
318
+ # constitutes some type of "session management".
438
319
  # ======================================================================= #
439
- if i.is_a? Array # Array tag
440
- if i.size > DELAY_IF_WE_HAVE_MORE_THAN_N_ENTRIES
441
- opnn; e 'More than '+simp(DELAY_IF_WE_HAVE_MORE_THAN_N_ENTRIES.to_s)+
442
- ' entries given ('+i.size.to_s+'), hence we will use a '\
443
- 'small delay (of '+sfancy(delay?.to_s)+' seconds).'
444
- end
445
- i.each {|entry|
446
- sleep delay? if i.size > DELAY_IF_WE_HAVE_MORE_THAN_N_ENTRIES # Small delay.
447
- try_to_open_this_file_in_the_editor(entry)
448
- }
449
- else
450
- # ===================================================================== #
451
- # Get rid of localhost-string parts next.
452
- # ===================================================================== #
453
- if i.include? 'localhost'
454
- i = replace_localhost_with_data(i)
455
- end unless File.exist? i
456
- # ===================================================================== #
457
- # Next, we chop off '?' characters. This is, however had, not good if
458
- # this is part of the filename. So we must check if the file exists,
459
- # prior to doing this check.
460
- # ===================================================================== #
461
- unless File.exist? i
462
- i = i[0..i.index('?')] if i.include? '?'
463
- end
464
- # ===================================================================== #
465
- # Get rid of '#' in the input, if it exists.
466
- # ===================================================================== #
467
- i = i[0..i.index('#')] if i.include? '#'
468
- case i
469
- when 'LAST' # Assign the last file.
470
- i = find_and_return_last_file
471
- end
472
- # ===================================================================== #
473
- # Next, act on the extension name.
474
- # ===================================================================== #
475
- _ = File.extname(i).delete('.').downcase
476
- case _ # case tag
477
- # ===================================================================== #
478
- # === open .pdf files
479
- # ===================================================================== #
480
- when 'pdf'
481
- open_in_pdf_viewer(i)
482
- # ===================================================================== #
483
- # === epub
484
- # ===================================================================== #
485
- when 'epub'
486
- esystem "okular #{i} &"
487
- # ===================================================================== #
488
- # === torrent
489
- # ===================================================================== #
490
- when 'torrent'
491
- open_this_torrent_file(i)
492
- # ===================================================================== #
493
- # === Extensions that can be opened in the editor
494
- # ===================================================================== #
495
- when *ARRAY_EXTENSIONS_THAT_CAN_BE_OPENED_IN_THE_EDITOR
496
- open_in_browser_or_in_editor(i)
497
- # ===================================================================== #
498
- # === All registered images
499
- # ===================================================================== #
500
- when *ARRAY_IMAGES
501
- open_in_gimp(i)
502
- # ===================================================================== #
503
- # === All libreoffice extensions
504
- # ===================================================================== #
505
- when *ARRAY_LIBREOFFICE_EXTENSIONS
506
- open_in_office(i)
320
+ i = File.absolute_path(i)
321
+ if File.exist? i
322
+ require 'pdf_paradise/main_pdf/main_pdf.rb'
323
+ require 'pdf_paradise/set_main_book.rb'
507
324
  # ===================================================================== #
508
- # === All videos
325
+ # Only enable the following on my home-system, and only in the
326
+ # books-subdirectory, and university-subdirectory.
509
327
  # ===================================================================== #
510
- when *ARRAY_VIDEOS
511
- play_multimedia_file(i)
512
- else
513
- if File.exist? i
514
- opnn; e "Not registered extension `#{sfancy(_)}`."
515
- opnn; e 'We will attempt to open it anyway though.'
516
- open_in_browser_or_in_editor(i)
517
- else # else we conclude that the file does not exist.
518
- no_file_was_found_at(i)
328
+ if is_on_roebe? and set_the_main_book?
329
+ case return_pwd
330
+ # =================================================================== #
331
+ # The following checks were explained above. We will also run
332
+ # rcfiles, and send pidof.
333
+ # =================================================================== #
334
+ when /^#{Regexp.quote('/home/x/books/')}/,
335
+ /^#{Regexp.quote('/home/x/studium/')}/
336
+ PdfParadise::SetMainBook.new(i)
337
+ run_rcfiles_then_run_ata_via_qdbus
519
338
  end
520
339
  end
340
+ PdfParadise.main_pdf(i) {{
341
+ open_via_fullscreen: open_via_fullscreen?
342
+ }}
343
+ register_this_pdf_file(i)
344
+ else
345
+ this_file_was_not_found(i)
521
346
  end
522
- end; alias consider_opening_the_given_input try_to_open_this_file_in_the_editor # === consider_opening_the_given_input
523
- alias open_in_editor try_to_open_this_file_in_the_editor # === open_in_editor
524
- alias open_these_files try_to_open_this_file_in_the_editor # === open_these_files
525
-
526
- # ========================================================================= #
527
- # === open_this_torrent_file
528
- # ========================================================================= #
529
- def open_this_torrent_file(i)
530
- if i.include?("'") and !i.include?('"')
531
- i = '"'+i+'"'
532
- end
533
- _ = "transmission #{i} &"
534
- esystem(_)
535
347
  end
536
348
 
537
349
  # ========================================================================= #
538
- # === try_to_find_files_based_on_hyphens_as_part_of_the_input
539
- #
540
- # This method will try to find files based on '::' input.'
541
- # ========================================================================= #
542
- def try_to_find_files_based_on_hyphens_as_part_of_the_input(i)
543
- if i.include? '::'
544
- # ===================================================================== #
545
- # In this case, replace '::', after passing it through the
546
- # snakecase() method.
547
- # ===================================================================== #
548
- i = snakecase_and_prepend_first_name_and_lib(i).tr('::','/')+'/'
549
- end
550
- unless i.start_with?('/home')
551
- # ===================================================================== #
552
- # We assume that only ruby files follow the '::' layout, for now. (Jun 2017)
553
- # ===================================================================== #
554
- i.prepend('$RUBY_SRC/')
555
- end
556
- if i.include? '$'
557
- i = convert_global_env(i)
558
- end
559
- i.squeeze!('/')
560
- unless i.end_with?('*')
561
- i << '*'
562
- end
563
- these_files = Dir[i]
564
- open_these_files(these_files)
565
- end
566
-
567
- # ========================================================================= #
568
- # === snakecase_and_prepend_first_name_and_lib
569
- #
570
- # This combines snakecase() by also prepending the first-name
571
- # and "lib/" prefix.
572
- # ========================================================================= #
573
- def snakecase_and_prepend_first_name_and_lib(i)
574
- if i.include? '::'
575
- splitted = i.split('::')
576
- first_name = splitted.first.downcase
577
- end
578
- i = snakecase(i)
579
- return first_name+'/lib/'+i
580
- end
581
-
582
- # ========================================================================= #
583
- # === register_this_pdf_file
350
+ # === register_this_pdf_file
584
351
  # ========================================================================= #
585
352
  def register_this_pdf_file(what)
586
353
  begin
587
354
  require 'save_file'
588
- require 'roebe/constants/file_constants.rb'
589
- require 'roebe/constants/directory_constants.rb'
355
+ require 'roebe/constants/file_and_directory_constants.rb'
590
356
  rescue LoadError; end
591
357
  into = Roebe.file_these_pdf_files_were_opened
592
358
  old_dataset = nil
@@ -627,36 +393,17 @@ class Open < ::Open::Base # === Open::Open
627
393
  end
628
394
 
629
395
  # ========================================================================= #
630
- # === open_in_browser_or_in_editor
631
- #
632
- # This method was required because sometimes we wish to open in the
633
- # browser, and sometimes we wish to open in the editor.
396
+ # === return_this_file_here
634
397
  # ========================================================================= #
635
- def open_in_browser_or_in_editor(i)
636
- if i.include? '//'
637
- i = i.dup if i.frozen?
638
- i.squeeze!('/')
639
- end
640
- # ======================================================================= #
641
- # Next, we get the extension name.
642
- # ======================================================================= #
643
- _ = File.extname(File.basename(i)).delete '.'
644
- case _
645
- when 'html','php' # Open in both, in this case.
646
- do_open_in_editor(i)
647
- unless File.zero? i
648
- ::Open.in_browser(i) unless _.include? 'php'
649
- end
650
- else
651
- do_open_in_editor(i)
652
- end
653
- end
398
+ def return_this_file_here
399
+ __FILE__
400
+ end; alias this_file? return_this_file_here # === this_file?
654
401
 
655
402
  # ========================================================================= #
656
403
  # === open_in_gimp
657
404
  # ========================================================================= #
658
405
  def open_in_gimp(i)
659
- esystem "#{@image_editor} #{i}#{IN_BACKGROUND}"
406
+ esystem "#{@internal_hash[:image_editor_to_use]} #{i}#{IN_BACKGROUND}"
660
407
  end
661
408
 
662
409
  # ========================================================================= #
@@ -676,147 +423,277 @@ class Open < ::Open::Base # === Open::Open
676
423
  end
677
424
 
678
425
  # ========================================================================= #
679
- # === try_to_require_the_studium_project
426
+ # === open_via_fullscreen?
680
427
  # ========================================================================= #
681
- def try_to_require_the_studium_project
682
- unless Object.const_defined?(:Studium) and
683
- Studium.respond_to?(:include_this_exam_topic?)
684
- begin # We pull in only the relevant files from the Studium project.
685
- require 'studium/constants/constants.rb'
686
- require 'studium/toplevel_methods/find_exam_topic_and_exam_title.rb'
687
- require 'studium/toplevel_methods/is_this_exam_topic_included.rb'
688
- rescue LoadError
689
- if is_on_roebe?
690
- e 'The three files ('\
691
- 'studium/constants/constants.rb,'\
692
- 'studium/toplevel_methods/find_exam_topic_and_exam_title.rb and '\
693
- 'studium/toplevel_methods/is_this_exam_topic_included.rb '\
694
- 'forth) from the studium-project could not be loaded.'
695
- end
428
+ def open_via_fullscreen?
429
+ @internal_hash[:open_via_fullscreen]
430
+ end
431
+
432
+ # ========================================================================= #
433
+ # === joined_and_stripped_commandline_arguments
434
+ # ========================================================================= #
435
+ def joined_and_stripped_commandline_arguments
436
+ _ = commandline_arguments?.map {|entry|
437
+ if entry.is_a? Symbol
438
+ entry = ":#{entry}" # Symbols become "fake" symbols here, on purpose.
696
439
  end
440
+ entry
441
+ }
442
+ return _.join(' ').strip
443
+ end
444
+
445
+ # ========================================================================= #
446
+ # === set_open_these_elements
447
+ # ========================================================================= #
448
+ def set_open_these_elements(i)
449
+ @internal_hash[:open_these_elements] = [i].flatten.compact
450
+ end; alias set_input set_open_these_elements # === set_input
451
+ alias set_input_files set_open_these_elements # === set_input_files
452
+
453
+ # ========================================================================= #
454
+ # === play_multimedia_file
455
+ # ========================================================================= #
456
+ def play_multimedia_file(i)
457
+ esystem "#{USE_THIS_MPLAYER_COMMAND} #{i}" # open
458
+ end
459
+
460
+ # ========================================================================= #
461
+ # === can_be_opened_in_the_editor?
462
+ # ========================================================================= #
463
+ def can_be_opened_in_the_editor?(i)
464
+ _ = File.extname(i).delete('.')
465
+ ARRAY_EXTENSIONS_THAT_CAN_BE_OPENED_IN_THE_EDITOR.include?(i)
466
+ end
467
+
468
+ # ========================================================================= #
469
+ # === find_and_return_last_file
470
+ #
471
+ # This method will select all files from the current directory and
472
+ # add the ctime (into the Array). Then it will simply sort based
473
+ # the ctime, and return the first entry.
474
+ # ========================================================================= #
475
+ def find_and_return_last_file
476
+ array = Dir['*'].select {|entry| File.file? entry }
477
+ array.map! {|entry|
478
+ [entry, File.ctime(entry)]
479
+ }
480
+ result = array.sort_by {|entry| entry[1] }.reverse
481
+ result = result.first[0] # Return the first entry here.
482
+ return result
483
+ end
484
+
485
+ # ========================================================================= #
486
+ # === open_this_torrent_file
487
+ #
488
+ # This method will properly open a .torrent file - right now hardcoded
489
+ # towards the "transmission" application.
490
+ # ========================================================================= #
491
+ def open_this_torrent_file(i)
492
+ if i.include?("'") and !i.include?('"')
493
+ i = '"'+i+'"'
697
494
  end
495
+ _ = "transmission #{i} &"
496
+ esystem(_)
497
+ end
498
+
499
+ # ========================================================================= #
500
+ # === open_these_elements?
501
+ # ========================================================================= #
502
+ def open_these_elements?
503
+ @internal_hash[:open_these_elements]
504
+ end; alias input_files? open_these_elements? # === input_files?
505
+ alias input_file? open_these_elements? # === input_file?
506
+ alias input? open_these_elements? # === input?
507
+
508
+ # ========================================================================= #
509
+ # === remove_empty_entries
510
+ # ========================================================================= #
511
+ def remove_empty_entries
512
+ @internal_hash[:open_these_elements].reject! {|entry|
513
+ entry.nil? or entry.empty?
514
+ }
698
515
  end
699
516
 
700
517
  # ========================================================================= #
701
- # === do_open_in_editor
518
+ # === snakecase_and_prepend_first_name_and_lib
702
519
  #
703
- # This method will try to open the file via the specified (main)
704
- # editor. Since as of August 2019, this will also be logged into
705
- # a local file, if we are on a roebe-system.
520
+ # This combines snakecase() by also prepending the first-name
521
+ # and "lib/" prefix.
706
522
  # ========================================================================= #
707
- def do_open_in_editor(
708
- i, use_this_editor = @editor
709
- )
710
- if i.include? '//'
711
- i.squeeze!('/')
523
+ def snakecase_and_prepend_first_name_and_lib(i)
524
+ if i.include? '::'
525
+ splitted = i.split('::')
526
+ first_name = splitted.first.downcase
712
527
  end
713
- if Object.const_defined? :Roebe
714
- expanded_alias = Roebe::FindExpandedAlias.new(i) { :be_quiet }
715
- if expanded_alias.is_included?
716
- unless File.exist? i
717
- points_at = expanded_alias.points_at?
718
- if points_at.include?(';')
719
- splitted = points_at.split(';')
720
- if File.exist? splitted.first
721
- points_at = splitted.first
722
- end
723
- end
724
- opnn; e 'Making use of the aliased-pointer at'
725
- opnn; e ' `'+steelblue(points_at)+'`.'
726
- i = points_at
727
- end
728
- end
528
+ i = snakecase(i)
529
+ return first_name+'/lib/'+i
530
+ end
531
+
532
+ # ========================================================================= #
533
+ # === replace_localhost_with_data
534
+ # ========================================================================= #
535
+ def replace_localhost_with_data(i)
536
+ if i.include? 'programming'
537
+ return i.sub('localhost',HOME_DIRECTORY_OF_USER_X)
538
+ else
539
+ return i.sub('localhost',"#{HOME_DIRECTORY_OF_USER_X}data/")
729
540
  end
730
- i = rds(i) # Get rid of // again, just in case.
731
- # ======================================================================= #
732
- # Make an exception for .cpp files:
733
- # ======================================================================= #
734
- if (File.extname(i).delete('.') == 'cpp')
735
- if is_on_roebe? and ENV.has_key?('CPP_EDITOR')
736
- use_this_editor = ENV['CPP_EDITOR'].to_s.dup
737
- else
738
- use_this_editor = 'geany' # We use geany for cpp files.
739
- end
541
+ end
542
+
543
+ # ========================================================================= #
544
+ # === try_to_find_files_based_on_hyphens_as_part_of_the_input
545
+ #
546
+ # This method will try to find files based on '::' input.'
547
+ # ========================================================================= #
548
+ def try_to_find_files_based_on_hyphens_as_part_of_the_input(i)
549
+ if i.include? '::'
550
+ # ===================================================================== #
551
+ # In this case, replace '::', after passing it through the
552
+ # snakecase() method.
553
+ # ===================================================================== #
554
+ i = snakecase_and_prepend_first_name_and_lib(i).tr('::','/')+'/'
740
555
  end
741
- if i.include?("'") and !i.include?('"')
742
- i = '"'+i+'"'
556
+ unless i.start_with?('/home')
557
+ # ===================================================================== #
558
+ # We assume that only ruby files follow the '::' layout, for now. (Jun 2017)
559
+ # ===================================================================== #
560
+ i.prepend('$RUBY_SRC/')
743
561
  end
744
- if i.include? '('
745
- i = '"'+i+'"'
562
+ if i.include? '$'
563
+ i = convert_global_env(i)
746
564
  end
747
- _ = "#{use_this_editor} #{i}"
748
- esystem(_)
749
- if File.file?(i) and Object.const_defined?(:Roebe) and
750
- File.directory?(Roebe.log_dir?) and
751
- is_on_roebe?
752
- into = Roebe.log_dir?+'opened_files.yml'
753
- if File.exist? into
754
- array = YAML.load_file(into)
755
- else
756
- array = [into].flatten.compact
757
- end
758
- what = YAML.dump(array)
759
- SaveFile.write_what_into(what, into)
565
+ i.squeeze!('/')
566
+ unless i.end_with?('*')
567
+ i << '*'
760
568
  end
569
+ these_files = Dir[i]
570
+ open_these_files(these_files)
761
571
  end
762
572
 
763
573
  # ========================================================================= #
764
- # === run_rcfiles_then_run_ata_via_qdbus
574
+ # === is_bluefish_running?
765
575
  # ========================================================================= #
766
- def run_rcfiles_then_run_ata_via_qdbus
767
- require 'rcfiles'
768
- Rcfiles.run
769
- esystem 'qdbus org.kde.konsole-`pidof -s konsole` /Sessions/2 runCommand "ata"'
770
- esystem 'qdbus org.kde.konsole-`pidof -s konsole` /Sessions/3 runCommand "ata"'
771
- esystem 'qdbus org.kde.konsole-`pidof -s konsole` /Sessions/4 runCommand "ata"'
576
+ def is_bluefish_running?
577
+ `ps ax | grep bluefish`.split("\n").reject {|entry|
578
+ entry.include? 'grep'
579
+ }.size > 0
772
580
  end
773
581
 
774
582
  # ========================================================================= #
775
- # === open_in_pdf_viewer (pdf tag)
583
+ # === start_bluefish
776
584
  #
777
- # This method can be used to open a .pdf file. We will let the project
778
- # PdfParadise handle this situation.
585
+ # This method can be used to start the bluefish-editor.
586
+ # ========================================================================= #
587
+ def start_bluefish
588
+ esystem 'bluefish &'
589
+ end
590
+
591
+ # ========================================================================= #
592
+ # === check_whether_the_user_has_provided_any_input
593
+ # ========================================================================= #
594
+ def check_whether_the_user_has_provided_any_input
595
+ _ = input_files?
596
+ local_files = Dir['*'].select {|entry| File.file? entry }
597
+ if _.empty? and !local_files.empty? and (local_files.size == 1)
598
+ # ===================================================================== #
599
+ # Handle the case where no specific input was given, but only
600
+ # one file exists in the local directory.
601
+ # ===================================================================== #
602
+ set_input_files(local_files.first)
603
+ _ = input_files?
604
+ end
605
+ if _.empty?
606
+ if is_on_roebe? and !is_bluefish_running?
607
+ opne 'Starting bluefish now, as we are on a roebe-system and'
608
+ opne 'bluefish is currently not running.'
609
+ start_bluefish
610
+ else
611
+ opne tomato('Please provide at the least one argument - the '\
612
+ 'file you wish to open.')
613
+ exit
614
+ end
615
+ end
616
+ end
617
+
618
+ # ========================================================================= #
619
+ # === general_open_functionality
779
620
  #
780
- # Since as of 19.09.2018 we will keep a log of the opened .pdf files
781
- # if we are on roebe. This can then be used to determine which were
782
- # the last .pdf files that were read.
621
+ # This method will act based on the extension name of the file usually -
622
+ # or, if not applicable, it will simply open this in the default,
623
+ # designated editor.
783
624
  # ========================================================================= #
784
- def open_in_pdf_viewer(i)
785
- require 'pdf_paradise' unless Object.const_defined? :PdfParadise
625
+ def general_open_functionality(i)
626
+ extname = File.extname(i).delete('.').downcase
627
+ case extname # case tag
786
628
  # ======================================================================= #
787
- # Since as of April 2022 we will use the absolute path to the .pdf
788
- # file at hand. The reason for this is because this makes it easier
789
- # to know which files are open, and we can store these file paths
790
- # into a local file (such as a .yml file), and then use this at a
791
- # later time to restore all .pdf files that were open. This thus
792
- # constitutes some type of "session management".
629
+ # === Extensions that can be opened in the editor
793
630
  # ======================================================================= #
794
- i = File.absolute_path(i)
795
- if File.exist? i
796
- require 'pdf_paradise/main_pdf/main_pdf.rb'
797
- require 'pdf_paradise/set_main_book.rb'
798
- # ===================================================================== #
799
- # Only enable the following on my home-system, and only in the
800
- # books-subdirectory, and university-subdirectory.
801
- # ===================================================================== #
802
- if is_on_roebe? and set_the_main_book?
803
- case return_pwd
804
- # =================================================================== #
805
- # The following checks were explained above. We will also run
806
- # rcfiles, and send pidof.
807
- # =================================================================== #
808
- when /^#{Regexp.quote('/home/x/books/')}/,
809
- /^#{Regexp.quote('/home/x/studium/')}/
810
- PdfParadise::SetMainBook.new(i)
811
- run_rcfiles_then_run_ata_via_qdbus
812
- end
631
+ when *ARRAY_EXTENSIONS_THAT_CAN_BE_OPENED_IN_THE_EDITOR
632
+ open_in_browser_or_in_editor(i)
633
+ # ======================================================================= #
634
+ # === torrent
635
+ # ======================================================================= #
636
+ when 'torrent'
637
+ open_this_torrent_file(i)
638
+ # ======================================================================= #
639
+ # === All registered images
640
+ # ======================================================================= #
641
+ when *ARRAY_IMAGES
642
+ open_in_gimp(i)
643
+ # ======================================================================= #
644
+ # === All libreoffice extensions
645
+ # ======================================================================= #
646
+ when *ARRAY_LIBREOFFICE_EXTENSIONS
647
+ open_in_office(i)
648
+ # ======================================================================= #
649
+ # === epub
650
+ # ======================================================================= #
651
+ when 'epub'
652
+ esystem "okular #{i} &"
653
+ # ======================================================================= #
654
+ # === open .pdf files
655
+ # ======================================================================= #
656
+ when 'pdf'
657
+ open_in_pdf_viewer(i)
658
+ # ======================================================================= #
659
+ # === All videos will enter here
660
+ # ======================================================================= #
661
+ when *ARRAY_VIDEOS
662
+ play_multimedia_file(i)
663
+ else
664
+ if File.exist? i
665
+ opne "#{rev}Not registered extension `#{sfancy(i)}#{rev}`."
666
+ opne 'We will attempt to open it anyway though.'
667
+ open_in_browser_or_in_editor(i)
668
+ else # else we conclude that the file does not exist.
669
+ no_file_was_found_at(i)
670
+ end
671
+ end
672
+ end; alias try_to_open_this_file_in_the_editor general_open_functionality # === try_to_open_this_file_in_the_editor
673
+
674
+ # ========================================================================= #
675
+ # === open_in_browser_or_in_editor
676
+ #
677
+ # This method was required because sometimes we wish to open in the
678
+ # browser, and sometimes we wish to open in the editor.
679
+ # ========================================================================= #
680
+ def open_in_browser_or_in_editor(i)
681
+ if i.include? '//'
682
+ i = i.dup if i.frozen?
683
+ i = rds(i)
684
+ end
685
+ # ======================================================================= #
686
+ # Next, we get the extension name.
687
+ # ======================================================================= #
688
+ _ = File.extname(File.basename(i)).delete '.'
689
+ case _
690
+ when 'html','php' # Open in both, editor and browser, in this case.
691
+ do_open_in_editor(i)
692
+ unless File.zero? i
693
+ ::Open.in_browser(i) unless _.include? 'php'
813
694
  end
814
- PdfParadise.main_pdf(i) {{
815
- open_via_fullscreen: @open_via_fullscreen
816
- }}
817
- register_this_pdf_file(i)
818
695
  else
819
- this_file_was_not_found(i)
696
+ do_open_in_editor(i)
820
697
  end
821
698
  end
822
699
 
@@ -837,79 +714,135 @@ class Open < ::Open::Base # === Open::Open
837
714
  end
838
715
 
839
716
  # ========================================================================= #
840
- # === show_help (help tag)
717
+ # === run_rcfiles_then_run_ata_via_qdbus
841
718
  # ========================================================================= #
842
- def show_help
843
- opnn; e 'The following options are available:'
844
- e
845
- ecomment ' SELF # Open this .rb file here.'
846
- ecomment ' --use-this-browser=firefox # Assign a new browser, in this case firefox.'
847
- ecomment ' --browser? # '\
848
- 'Show which browser is the currently assigned one.'
849
- e
719
+ def run_rcfiles_then_run_ata_via_qdbus
720
+ require 'rcfiles'
721
+ Rcfiles.run
722
+ esystem 'qdbus org.kde.konsole-`pidof -s konsole` /Sessions/2 runCommand "ata"'
723
+ esystem 'qdbus org.kde.konsole-`pidof -s konsole` /Sessions/3 runCommand "ata"'
724
+ esystem 'qdbus org.kde.konsole-`pidof -s konsole` /Sessions/4 runCommand "ata"'
850
725
  end
851
726
 
852
727
  # ========================================================================= #
853
- # === menu (menu tag)
728
+ # === sanitize_the_commandline_arguments
854
729
  # ========================================================================= #
855
- def menu(
856
- i = input?
857
- )
858
- if i.is_a? Array
859
- i.each {|entry| menu(entry) }
860
- else
861
- case i # (case tag)
730
+ def sanitize_the_commandline_arguments
731
+ @commandline_arguments.flatten!
732
+ @commandline_arguments.map! {|entry|
733
+ case entry
734
+ when '*'
735
+ entry = Dir['*']
736
+ end
737
+ entry
738
+ }
739
+ @commandline_arguments.flatten!
740
+ end
741
+
742
+ # ========================================================================= #
743
+ # === run (run tag)
744
+ # ========================================================================= #
745
+ def run
746
+ sanitize_the_commandline_arguments
747
+ # @internal_hash[:open_these_elements] << joined_and_stripped_commandline_arguments
748
+ @internal_hash[:open_these_elements] << commandline_arguments_without_hyphens
749
+ remove_empty_entries
750
+ check_whether_the_user_has_provided_any_input
751
+ try_to_require_the_studium_project
752
+ try_to_require_the_beautiful_url_project
753
+ menu # Now we can pass this into the main menu here.
754
+ array = [@internal_hash[:open_these_elements]].flatten.compact
755
+ array.each {|_|
756
+ _ = _.to_s.dup # Always work on a String-copy past this point.
757
+ # ===================================================================== #
758
+ # Check whether the input includes the ':' character - but this may
759
+ # have two different use cases. The first one is to check for a
760
+ # shortcut entry; the second will just ignore the part after ':'.
761
+ # ===================================================================== #
762
+ if _.start_with? ':'
763
+ open_in_editor(_)
764
+ return
765
+ end
766
+ if _.include?(':') and
767
+ !File.exist?(_) and
768
+ File.exist?(_.split(':').first.to_s)
769
+ _ = _.split(':').first.to_s
770
+ end
862
771
  # ===================================================================== #
863
- # === open --browser?
772
+ # Next, we handle input such as:
773
+ #
774
+ # 'lib/lpc/requires/basic_requires.rb:begin'
775
+ #
776
+ # This evidently does not end with a ':' character, but it clearly
777
+ # has a fairly useless ':' component in it. If that is the case,
778
+ # we will treat this as input that is only valid up towards the
779
+ # very right ':', anchored on the '.rb:' part.
864
780
  # ===================================================================== #
865
- when /^-?-?browser\??$/
866
- _ = YAML.load_file(::Open.file_browser?)
867
- e 'The browser currently in use is `'+sfancy(_)+'`.'
868
- exit
781
+ if _.include? '.rb:'
782
+ # =================================================================== #
783
+ # The following code means to find the index at '.rb:' and then add
784
+ # 2 to it - this should be the correct name of the entry at hand.
785
+ # =================================================================== #
786
+ _ = _[0 .. (_.index('.rb:')+2)]
787
+ end
788
+ unless File.exist? _
789
+ _.chop! if _.end_with? '#' # Don't need trailing '#' tokens.
790
+ _.chop! if _.end_with? ':-' # ← This check must come before the check against ':'.
791
+ _.chop! if _.end_with? ':' # Chop off trailing ':' tokens.
792
+ _.chop! if _.end_with? ';' # Chop off trailing ':' tokens.
793
+ _.delete_prefix!('http:') if _.start_with?('http:')
794
+ end
795
+ case _
869
796
  # ===================================================================== #
870
- # === open --use-this-browser=firefox
797
+ # === open ME
871
798
  # ===================================================================== #
872
- when /^-?-?use(-|_)?this(-|_)?browser=(.+)/
873
- ::Open.permanently_use_this_browser = $3.to_s.dup
799
+ when 'ME',
800
+ 'SELF',
801
+ 'ALL',/
802
+ ^-?-?this(_|-)?file/
803
+ open_this_file_here
874
804
  exit
805
+ end unless File.exist?(_)
806
+ _.delete!('^') if _.include?('^')
807
+ if _.end_with?('.') and File.exist?(_[0..-2])
808
+ # =================================================================== #
809
+ # This clause will be entered if we have input something like
810
+ # "colours.rb." where the user may have wanted to type in
811
+ # "colours.rb" - and IF the file exists.
812
+ # =================================================================== #
813
+ opne "Assuming a superfluous trailing '.', so removing "\
814
+ "the last '.' there."
815
+ _.chop!
875
816
  end
876
- end
877
- end
878
-
879
- # ========================================================================= #
880
- # === try_to_require_the_beautiful_url_project
881
- # ========================================================================= #
882
- def try_to_require_the_beautiful_url_project
883
- unless Object.const_defined? :BeautifulUrl
884
- begin
885
- require 'roebe/requires/failsafe_require_of_beautiful_url.rb'
886
- rescue LoadError; end
887
- end
888
- end
889
-
890
- # ========================================================================= #
891
- # === run_everything
892
- # ========================================================================= #
893
- def run_everything
894
- input_files = input_files?.flatten
895
- # ======================================================================= #
896
- # Iterate over all input files that were passed to us.
897
- # ======================================================================= #
898
- input_files.each {|this_file|
899
817
  # ===================================================================== #
900
- # Check whether the input includes the ':' character:
818
+ # Split away at '::' if this is part of that substring.
819
+ # ===================================================================== #
820
+ if _.include? '::'
821
+ splitted = _.split('::')
822
+ _ = splitted.last
823
+ end
901
824
  # ===================================================================== #
902
- if this_file.include?(':') and !File.exist?(this_file) and
903
- File.exist?(this_file.split(':').first.to_s)
904
- this_file = this_file.split(':').first.to_s
825
+ # === Get rid of localhost-string parts next.
826
+ #
827
+ # Get rid of the String "localhost" next. This must be handled with
828
+ # more care, because some files may include the string 'localhost' as
829
+ # part of their name, such as 'remove_localhost.rb'. In this case,
830
+ # we will additionally check whether the file exists.
831
+ # ===================================================================== #
832
+ if _.is_a?(String) and _.include?('localhost')
833
+ unless File.exist? _ # See the above header for an explanation.
834
+ _ = replace_localhost_with_data(_)
835
+ end
905
836
  end
906
- if this_file.start_with?('http:') and !File.exist?(this_file)
907
- this_file.delete_prefix!('http:')
837
+ case _
838
+ when 'LAST' # Assign the last file.
839
+ _ = find_and_return_last_file
908
840
  end
909
841
  # ===================================================================== #
910
- # === Query whether the file exists or whether it does not
842
+ # If the file exists, enter here first - thus, we will query whether
843
+ # the file exists or whether it does not.
911
844
  # ===================================================================== #
912
- if File.exist?(this_file) # This is the simplest way - the file exists here.
845
+ if File.exist? _ # This is the simplest way - the file exists here.
913
846
  # =================================================================== #
914
847
  # However had, in December 2022 it was noticed that a file such
915
848
  # as "curricula.yml" may exist, as well as a directory called
@@ -917,86 +850,91 @@ class Open < ::Open::Base # === Open::Open
917
850
  # curricula.yml rather than curricula/. So, what to do in this
918
851
  # case? Well - we will do a minimal glob, that is, if a file
919
852
  # exists as described above, then we will prioritize this over
920
- # the directory name.
853
+ # the directory name and immediately open it.
921
854
  # =================================================================== #
922
- if File.directory?(this_file) and !File.file?(this_file) and
923
- (Dir[this_file+'*'].size > 0)
924
- this_file = Dir[this_file+'.*'].first.to_s # Pick a real file in this case.
855
+ if File.directory?(_) and !File.file?(_) and
856
+ (Dir["#{_}*"].size > 0)
857
+ _ = Dir["#{_}*"].first.to_s # Pick a real file in this case.
925
858
  end
926
- try_to_open_this_file_in_the_editor(this_file)
927
- else # Ok, the file was not instantly found. Run more checks here.
928
- possible_matches = Dir[('*'+this_file+'*').squeeze('*')] # This may be an empty Array as well.
859
+ general_open_functionality(_)
860
+ # ===================================================================== #
861
+ # === Check whether the input is an exam-question next
862
+ #
863
+ # This check should come before we delegate towards FindExpandedAlias.
864
+ #
865
+ # Invocation example:
866
+ #
867
+ # open ab2
868
+ #
869
+ # ===================================================================== #
870
+ elsif Object.const_defined?(:Studium) and
871
+ ::Studium.respond_to?(:include_this_exam_topic?) and
872
+ ::Studium.respond_to?(:find_corresponding_exam_topic) and
873
+ ::Studium.include_this_exam_topic?(_) # open ab2
874
+ _ = ::Studium.find_corresponding_exam_topic(
875
+ ::Studium.find_corresponding_exam_topic(_),
876
+ :be_quiet
877
+ ) # This can be false.
878
+ if _
879
+ if is_on_roebe?
880
+ _ = Studium.my_exam_topics_directory?+File.basename(_)
881
+ else
882
+ _ = Studium.exam_topics_directory?+File.basename(_)
883
+ end
884
+ open_in_editor(_)
885
+ end
886
+ # ===================================================================== #
887
+ # === Query whether BeautifulUrl includes this file.
888
+ # ===================================================================== #
889
+ elsif BeautifulUrl.does_include?(_) # open linux
890
+ _ = beautiful_url(_)
891
+ consider_opening_the_given_input(_) # Then try again.
892
+ # ===================================================================== #
893
+ # Ok, the following else-clauses handle the situation
894
+ # that the given input could not be found.
895
+ # ===================================================================== #
896
+ elsif _.include? 'project_'
897
+ do_open_in_editor(_)
898
+ else
899
+ possible_matches = Dir[('*'+_+'*').squeeze('*')] # ← This may be an empty Array as well.
929
900
  # =================================================================== #
930
901
  # Since as of Jan 2018, if the variable possible_matches is NOT
931
902
  # empty, we will sort it in a way so that the shorter file names
932
- # are on top of the array.
903
+ # are on top of the array. See the following clause for that.
933
904
  # =================================================================== #
934
905
  unless possible_matches.empty?
935
906
  possible_matches = possible_matches.sort_by {|entry| entry.size }
936
907
  end
937
908
  # =================================================================== #
938
- # === Check whether the input is an exam-question next
939
- #
940
- # This check should come before we delegate towards FindExpandedAlias.
941
- # =================================================================== #
942
- if Object.const_defined?(:Studium) and
943
- ::Studium.respond_to?(:include_this_exam_topic?) and
944
- ::Studium.respond_to?(:find_corresponding_exam_topic) and
945
- ::Studium.include_this_exam_topic?(this_file) # open ab2
946
- _ = ::Studium.find_corresponding_exam_topic(
947
- ::Studium.find_corresponding_exam_topic(this_file),
948
- :be_quiet
949
- ) # This can be false.
950
- if _
951
- if is_on_roebe?
952
- _ = Studium.my_exam_topics_directory?+File.basename(_)
953
- else
954
- _ = Studium.exam_topics_directory?+File.basename(_)
955
- end
956
- open_in_editor(_)
957
- end
909
+ # At the least one entry has been found in the next case.
958
910
  # =================================================================== #
959
- # === Query whether BeautifulUrl includes this file.
960
- # =================================================================== #
961
- elsif BeautifulUrl.does_include?(this_file) # open linux
962
- _ = beautiful_url(this_file)
963
- consider_opening_the_given_input(_) # Then try again.
964
- # =================================================================== #
965
- # Ok, the following else-clauses handle the situation
966
- # that the given input could not be found.
967
- # =================================================================== #
968
- elsif this_file.include? 'project_'
969
- do_open_in_editor(this_file)
970
- # =================================================================== #
971
- # At the least one entry has been found.
972
- # =================================================================== #
973
- elsif ((_ = possible_matches).size > 0) # If we can find at least one entry.
974
- consider_opening_the_given_input(_.first) # Recursive call.
911
+ if (possible_matches.size > 0) # If we can find at least one entry.
912
+ consider_opening_the_given_input(possible_matches.first) # Recursive call here.
975
913
  # =================================================================== #
976
914
  # Same as above, but both prepend and append a '*' character.
977
915
  # =================================================================== #
978
- elsif ((_ = possible_matches).size > 0) # If we can find at least one entry.
979
- consider_opening_the_given_input(_.first)
916
+ elsif (possible_matches.size > 0) # If we can find at least one entry.
917
+ consider_opening_the_given_input(possible_matches.first)
980
918
  # =================================================================== #
981
919
  # Next, try to pass it through class Roebe::FindExpandedAlias.
982
920
  # =================================================================== #
983
- elsif File.exist?(_ = Roebe.find_expanded_alias(this_file).to_s)
984
- open_in_editor(_)
921
+ elsif File.exist?(more_matches = Roebe.find_expanded_alias(_).to_s)
922
+ open_in_editor(more_matches)
985
923
  # =================================================================== #
986
924
  # Next, try to handle input containing two ':' tokens.
987
925
  # =================================================================== #
988
- elsif this_file.include?('::')
989
- try_to_find_files_based_on_hyphens_as_part_of_the_input(this_file)
926
+ elsif _.include?('::')
927
+ try_to_find_files_based_on_hyphens_as_part_of_the_input(_)
990
928
  # =================================================================== #
991
929
  # Use RUBY_SRC as shortcut on my home system.
992
930
  # =================================================================== #
993
931
  elsif is_on_roebe? and
994
- File.exist?(return_assumed_path_to_ruby_src_lib_of(this_file))
995
- use_this_file_instead = return_assumed_path_to_ruby_src_lib_of(this_file)
996
- e rev+'Since we are on roebe, we will use another path, aka'
997
- e
998
- e " #{steelblue(use_this_file_instead)}"
999
- e
932
+ File.exist?(return_assumed_path_to_ruby_src_lib_of(_))
933
+ use_this_file_instead = return_assumed_path_to_ruby_src_lib_of(_)
934
+ opne "#{rev}Since we are on roebe, we will use another path, aka"
935
+ opne
936
+ opne " #{steelblue(use_this_file_instead)}"
937
+ opne
1000
938
  open_in_editor(use_this_file_instead)
1001
939
  # =================================================================== #
1002
940
  # === Check for aliases next
@@ -1004,30 +942,29 @@ class Open < ::Open::Base # === Open::Open
1004
942
  # Next, try to find an expanded alias here.
1005
943
  # =================================================================== #
1006
944
  elsif Roebe.const_defined?(:FindExpandedAlias) and
1007
- (::Roebe::FindExpandedAlias[this_file] != this_file)
1008
- do_open_in_editor(this_file)
945
+ (::Roebe::FindExpandedAlias[_] != _)
946
+ do_open_in_editor(_)
1009
947
  else
1010
- opnn; e 'No match could be found for `'+sfancy(this_file)+'`.'
948
+ # ================================================================= #
949
+ # Alright - the file was not found via the above checks, so we
950
+ # will simply delegate towards Open.in_browser() next.
951
+ # ================================================================= #
952
+ # Or we could notify the user that no entry was found:
953
+ # opne "No match could be found for `#{sfancy(_)}`."
954
+ # ================================================================= #
955
+ ::Open.in_browser(_)
1011
956
  end
1012
957
  end
1013
958
  }
1014
959
  end
1015
960
 
1016
- # ========================================================================= #
1017
- # === run (run tag)
1018
- # ========================================================================= #
1019
- def run
1020
- check_whether_the_user_has_provided_any_input
1021
- try_to_require_the_studium_project
1022
- try_to_require_the_beautiful_url_project
1023
- menu
1024
- run_everything
1025
- end
1026
-
1027
961
  # ========================================================================= #
1028
962
  # === Open::Open[]
1029
963
  # ========================================================================= #
1030
- def self.[](i = ARGV, &block)
964
+ def self.[](
965
+ i = ARGV,
966
+ &block
967
+ )
1031
968
  new(i, &block) # Always delegate here.
1032
969
  end
1033
970
 
@@ -1036,15 +973,20 @@ end
1036
973
  # =========================================================================== #
1037
974
  # === Open.open
1038
975
  # =========================================================================== #
1039
- def self.open(i = ARGV, &block)
976
+ def self.open(
977
+ i = ARGV, &block
978
+ )
1040
979
  ::Open::Open.new(i, &block)
1041
980
  end; self.instance_eval { alias [] open } # === Open[]
1042
981
 
1043
982
  # =========================================================================== #
1044
983
  # === Open.pdf_viewer?
984
+ #
985
+ # This method can be used to query the pdf-viewer in use.
1045
986
  # =========================================================================== #
1046
987
  def self.pdf_viewer?
1047
- unless (Object.const_defined?(:PdfParadise) and PdfParadise.const_defined?(:MainPdf))
988
+ unless (Object.const_defined?(:PdfParadise) and
989
+ PdfParadise.const_defined?(:MainPdf))
1048
990
  require 'pdf_paradise/main_pdf/main_pdf.rb'
1049
991
  end
1050
992
  return ::PdfParadise.use_which_pdf_viewer?
@@ -1054,4 +996,4 @@ end
1054
996
 
1055
997
  if __FILE__ == $PROGRAM_NAME
1056
998
  Open::Open.new(ARGV)
1057
- end # open
999
+ end # ruby open.rb local_ginger