open 0.1.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of open might be problematic. Click here for more details.

data/lib/open/open.rb ADDED
@@ -0,0 +1,905 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # === Open::Open
6
+ #
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.
10
+ #
11
+ # Usage example:
12
+ #
13
+ # Open::Open.new(ARGV)
14
+ #
15
+ # =========================================================================== #
16
+ # require 'open/open.rb'
17
+ # Open::Open.new(ARGV)
18
+ # =========================================================================== #
19
+ require 'open/base/base.rb'
20
+
21
+ module Open
22
+
23
+ class Open < ::Open::Base # === Open::Open
24
+
25
+ require 'open/in_editor/in_editor.rb'
26
+ require 'open/in_browser/in_browser.rb'
27
+
28
+ begin
29
+ require 'roebe/classes/find_expanded_alias.rb'
30
+ rescue LoadError; end
31
+
32
+ # ========================================================================= #
33
+ # === NAMESPACE
34
+ # ========================================================================= #
35
+ NAMESPACE = inspect
36
+
37
+ # ========================================================================= #
38
+ # === LIBREOFFICE
39
+ #
40
+ # A hardcoded path. This is no longer as important as it used to be,
41
+ # though. In the future, it may include an ENV value.
42
+ # ========================================================================= #
43
+ # LIBREOFFICE = '/usr/bin/soffice'
44
+ # LIBREOFFICE = '/opt/libreoffice/program/soffice'
45
+ if ENV['PATH_TO_THE_LIBREOFFICE_EXECUTABLE']
46
+ LIBREOFFICE = ENV['PATH_TO_THE_LIBREOFFICE_EXECUTABLE'].to_s.dup
47
+ else
48
+ LIBREOFFICE = '/usr/bin/soffice'
49
+ end
50
+
51
+ # ========================================================================= #
52
+ # === USE_THIS_MPLAYER_COMMAND
53
+ # ========================================================================= #
54
+ USE_THIS_MPLAYER_COMMAND =
55
+ 'mplayer -vo x11'
56
+
57
+ # ========================================================================= #
58
+ # === DELAY_IF_WE_HAVE_MORE_THAN_N_ENTRIES
59
+ # ========================================================================= #
60
+ DELAY_IF_WE_HAVE_MORE_THAN_N_ENTRIES = 4
61
+
62
+ # ========================================================================= #
63
+ # === USE_THIS_DELAY
64
+ #
65
+ # Sleep in n seconds before opening a file, if we have more
66
+ # entries than the above threshold value did define.
67
+ # ========================================================================= #
68
+ USE_THIS_DELAY = 0.65
69
+
70
+ # ========================================================================= #
71
+ # === ARRAY_EXTENSIONS_THAT_CAN_BE_OPENED_IN_THE_EDITOR
72
+ #
73
+ # All extensions that can be opened in the editor should be registered
74
+ # in this constant.
75
+ #
76
+ # For example, "h" means ".h" aka a C or C++ header file.
77
+ # ========================================================================= #
78
+ ARRAY_EXTENSIONS_THAT_CAN_BE_OPENED_IN_THE_EDITOR = %w(
79
+ php rb txt yaml yml cgi
80
+ md gemspec html csv py
81
+ conf c sh js log css cr
82
+ ascii la pl perl json
83
+ js fasta fa m3u cpp
84
+ go gff gff3
85
+ gen h
86
+ erb sinatra
87
+ ).sort << '' # The last part is for files like "TODO" which lack an extension.
88
+
89
+ # ========================================================================= #
90
+ # === ARRAY_LIBREOFFICE_EXTENSIONS
91
+ #
92
+ # All libreoffice extensions will be stored here.
93
+ # ========================================================================= #
94
+ ARRAY_LIBREOFFICE_EXTENSIONS = %w(
95
+ xls
96
+ odt
97
+ doc
98
+ docx
99
+ ppt
100
+ xlsx
101
+ sxw
102
+ swx
103
+ rtf
104
+ ods
105
+ odp
106
+ pptx
107
+ pps
108
+ )
109
+
110
+ # ========================================================================= #
111
+ # === ARRAY_IMAGES
112
+ #
113
+ # All file-extensions for images can be registered in this Array.
114
+ # ========================================================================= #
115
+ ARRAY_IMAGES = %w(
116
+ png
117
+ jpg
118
+ gif
119
+ jpeg
120
+ tiff
121
+ tif
122
+ bmp
123
+ )
124
+
125
+ # ========================================================================= #
126
+ # === ARRAY_VIDEOS
127
+ # ========================================================================= #
128
+ ARRAY_VIDEOS = %w(
129
+ vob
130
+ avi
131
+ flv
132
+ mp3
133
+ mp4
134
+ )
135
+
136
+ # ========================================================================= #
137
+ # === initialize
138
+ # ========================================================================= #
139
+ def initialize(
140
+ i = nil,
141
+ run_already = true
142
+ )
143
+ reset
144
+ set_input(i)
145
+ # ======================================================================= #
146
+ # === Handle blocks next
147
+ # ======================================================================= #
148
+ if block_given?
149
+ yielded = yield
150
+ # === Handle hashes next
151
+ if yielded.is_a? Hash
152
+ # =================================================================== #
153
+ # === :use_this_editor
154
+ # =================================================================== #
155
+ if yielded.has_key? :use_this_editor
156
+ set_use_this_editor(yielded.delete(:use_this_editor))
157
+ end
158
+ end
159
+ end
160
+ run if run_already
161
+ end
162
+
163
+ # ========================================================================= #
164
+ # === reset (reset tag)
165
+ # ========================================================================= #
166
+ def reset
167
+ super()
168
+ # ======================================================================= #
169
+ # === @namespace
170
+ # ======================================================================= #
171
+ @namespace = NAMESPACE
172
+ # ======================================================================= #
173
+ # === @image_editor
174
+ # ======================================================================= #
175
+ @image_editor = 'gimp'
176
+ # ======================================================================= #
177
+ # === @editor
178
+ # ======================================================================= #
179
+ @editor = use_which_editor?
180
+ end
181
+
182
+ # ========================================================================= #
183
+ # === set_use_this_editor
184
+ # ========================================================================= #
185
+ def set_use_this_editor(
186
+ i = use_which_editor?
187
+ )
188
+ @editor = i.to_s
189
+ end
190
+
191
+ # ========================================================================= #
192
+ # === replace_localhost_with_data
193
+ # ========================================================================= #
194
+ def replace_localhost_with_data(i)
195
+ if i.include? 'programming'
196
+ return i.sub('localhost','/home/x/')
197
+ else
198
+ return i.sub('localhost','/home/x/data/')
199
+ end
200
+ end
201
+
202
+ # ========================================================================= #
203
+ # === set_input
204
+ # ========================================================================= #
205
+ def set_input(i = ARGV)
206
+ i = [i] unless i.is_a? Array
207
+ i.map! {|entry|
208
+ entry = entry.to_s.dup
209
+ # ===================================================================== #
210
+ # Split away at '::' if this is part of that substring.
211
+ # ===================================================================== #
212
+ if entry.include? '::'
213
+ splitted = entry.split('::')
214
+ entry = splitted.last
215
+ end
216
+ if entry.include? '^'
217
+ entry.delete!('^')
218
+ end
219
+ case entry
220
+ # ===================================================================== #
221
+ # === open --important-exam
222
+ # ===================================================================== #
223
+ when /^-?-?important(-|_)?exams?$/i
224
+ base_dir = ENV['USERS']
225
+ base_dir = '/home' if base_dir.nil?
226
+ entry = "#{base_dir}/x/programming/ruby/src/studium/lib/studium/yaml/important_exams.yml"
227
+ end
228
+ # ===================================================================== #
229
+ # Get rid of localhost next. This must be handled with more care,
230
+ # because some files may include the string 'localhost' as part of
231
+ # their name, such as 'remove_localhost.rb'. In this case, we will
232
+ # additionally check whether the file exists.
233
+ # ===================================================================== #
234
+ if entry.include? 'localhost'
235
+ entry = replace_localhost_with_data(entry)
236
+ end unless File.exist? entry # See the above header for an explanation.
237
+ entry.chop! if entry.end_with? '#' # Don't need trailing '#' tokens.
238
+ entry.chop! if entry.end_with? ':-' # <- This check must come before the check against ':'.
239
+ entry.chop! if entry.end_with? ':' # Chop off trailing ':' tokens.
240
+ # ===================================================================== #
241
+ # Next, we handle input such as:
242
+ # 'lib/lpc/requires/basic_requires.rb:begin'
243
+ # This evidently does not end with a ':' character, but it clearly
244
+ # has a fairly useless ':' component in it. If that is the case,
245
+ # we will treat this as input that is only valid up towards the
246
+ # very right ':', anchored on the '.rb:' part.
247
+ # ===================================================================== #
248
+ if entry.include? '.rb:'
249
+ # =================================================================== #
250
+ # The following code means to find the index at '.rb:' and then add
251
+ # 2 to it - this should be the correct name of the entry at hand.
252
+ # =================================================================== #
253
+ entry = entry[0 .. (entry.index('.rb:')+2)]
254
+ end
255
+ if entry.end_with?(',') and !File.exist?(',')
256
+ entry.chop!
257
+ elsif entry.end_with?('.') and File.exist?(entry[0..-2])
258
+ # =================================================================== #
259
+ # This clause will be entered if we have input something like
260
+ # "colours.rb." where the user may have wanted to type in
261
+ # "colours.rb" - and IF the file exists.
262
+ # =================================================================== #
263
+ opnn; e "Assuming a superfluous trailing '.', so removing "\
264
+ "the last '.' there."
265
+ entry.chop!
266
+ end
267
+ case entry # Work only on the first entry. (case tag)
268
+ # ===================================================================== #
269
+ # === open ME
270
+ # ===================================================================== #
271
+ when 'ME',
272
+ 'SELF',
273
+ 'ALL',/^-?-?this(_|-)?file/
274
+ open_this_file_here
275
+ exit
276
+ # ===================================================================== #
277
+ # === open --help
278
+ # ===================================================================== #
279
+ when /^-?-?help$/i,
280
+ 'show_help'
281
+ show_help
282
+ exit
283
+ end unless File.exist?(entry.to_s)
284
+ entry
285
+ }
286
+ @input = i # This must be an Array.
287
+ end; alias set_input_files set_input # === set_input_files
288
+
289
+ # ========================================================================= #
290
+ # === show_help (help tag)
291
+ # ========================================================================= #
292
+ def show_help
293
+ opnn; e 'The following options are available:'
294
+ e
295
+ opnn; ecomment ' SELF # Open this .rb file here.'
296
+ e
297
+ end
298
+
299
+ # ========================================================================= #
300
+ # === input?
301
+ # ========================================================================= #
302
+ def input?
303
+ @input
304
+ end; alias input_file? input? # === input_file?
305
+ alias input_files? input? # === input_files?
306
+
307
+ # ========================================================================= #
308
+ # === check_whether_the_user_has_provided_any_input
309
+ # ========================================================================= #
310
+ def check_whether_the_user_has_provided_any_input
311
+ _ = input_files?
312
+ local_files = Dir['*'].select {|entry| File.file? entry }
313
+ if _.empty? and !local_files.empty? and (local_files.size == 1)
314
+ # ===================================================================== #
315
+ # Handle the case where no specific input was given, but only
316
+ # one file exists in the local directory.
317
+ # ===================================================================== #
318
+ set_input_files(local_files.first)
319
+ _ = input_files?
320
+ end
321
+ if _.empty?
322
+ if is_on_roebe? and !is_bluefish_running?
323
+ start_bluefish
324
+ else
325
+ opnn; e 'Please provide at least one argument - the file '\
326
+ 'you wish to open.'
327
+ exit
328
+ end
329
+ end
330
+ end
331
+
332
+ # ========================================================================= #
333
+ # === start_bluefish
334
+ # ========================================================================= #
335
+ def start_bluefish
336
+ esystem 'bluefish &'
337
+ end
338
+
339
+ # ========================================================================= #
340
+ # === is_bluefish_running?
341
+ # ========================================================================= #
342
+ def is_bluefish_running?
343
+ `ps ax | grep bluefish`.split("\n").reject {|entry|
344
+ entry.include? 'grep'
345
+ }.size > 0
346
+ end
347
+
348
+ # ========================================================================= #
349
+ # === consider_padding
350
+ #
351
+ # Simply add a surrounding '"' to the given input, so that any resulting
352
+ # system() call will still work if we have awkward filenames.
353
+ # ========================================================================= #
354
+ def consider_padding(i)
355
+ if i.include?(' ') or i.include?('(')
356
+ i = '"'+i+'"'
357
+ end
358
+ return i
359
+ end
360
+
361
+ # ========================================================================= #
362
+ # === play_multimedia_file
363
+ # ========================================================================= #
364
+ def play_multimedia_file(i)
365
+ esystem "#{USE_THIS_MPLAYER_COMMAND} #{i}" # open
366
+ end
367
+
368
+ # ========================================================================= #
369
+ # === open_this_file_here
370
+ # ========================================================================= #
371
+ def open_this_file_here
372
+ do_open_in_editor(this_file?+IN_BACKGROUND)
373
+ end
374
+
375
+ # ========================================================================= #
376
+ # === return_this_file_here
377
+ # ========================================================================= #
378
+ def return_this_file_here
379
+ __FILE__
380
+ end; alias this_file? return_this_file_here # === this_file?
381
+
382
+ # ========================================================================= #
383
+ # === delay?
384
+ # ========================================================================= #
385
+ def delay?
386
+ USE_THIS_DELAY
387
+ end
388
+
389
+ # ========================================================================= #
390
+ # === find_and_return_last_file
391
+ # ========================================================================= #
392
+ def find_and_return_last_file
393
+ array = Dir['*'].reject {|entry| ! File.file? entry }
394
+ array.map! {|entry|
395
+ [entry, File.ctime(entry)]
396
+ }
397
+ result = array.sort_by {|entry| entry[1] }.reverse
398
+ result = result.first[0]
399
+ return result
400
+ end
401
+
402
+ # ========================================================================= #
403
+ # === can_be_opened_in_the_editor?
404
+ # ========================================================================= #
405
+ def can_be_opened_in_the_editor?(i)
406
+ _ = File.extname(i).delete('.')
407
+ ARRAY_EXTENSIONS_THAT_CAN_BE_OPENED_IN_THE_EDITOR.include? i
408
+ end
409
+
410
+ # ========================================================================= #
411
+ # === try_to_open_this_file_in_the_editor
412
+ # ========================================================================= #
413
+ def try_to_open_this_file_in_the_editor(
414
+ i = input_files?
415
+ )
416
+ # ======================================================================= #
417
+ # === First check whether we have passed an Array
418
+ # ======================================================================= #
419
+ if i.is_a? Array # Array tag
420
+ if i.size > DELAY_IF_WE_HAVE_MORE_THAN_N_ENTRIES
421
+ opnn; e 'More than '+simp(DELAY_IF_WE_HAVE_MORE_THAN_N_ENTRIES.to_s)+
422
+ ' entries given ('+i.size.to_s+'), hence we will use a '\
423
+ 'small delay (of '+sfancy(delay?.to_s)+' seconds).'
424
+ end
425
+ i.each {|entry|
426
+ sleep delay? if i.size > DELAY_IF_WE_HAVE_MORE_THAN_N_ENTRIES # Small delay.
427
+ try_to_open_this_file_in_the_editor(entry)
428
+ }
429
+ else
430
+ # ===================================================================== #
431
+ # Get rid of localhost-string parts next.
432
+ # ===================================================================== #
433
+ if i.include? 'localhost'
434
+ i = replace_localhost_with_data(i)
435
+ end unless File.exist? i
436
+ # ===================================================================== #
437
+ # Next, we chop off '?' characters. This is, however had, not good if
438
+ # this is part of the filename. So we must check if the file exists,
439
+ # prior to doing this check.
440
+ # ===================================================================== #
441
+ unless File.exist? i
442
+ i = i[0..i.index('?')] if i.include? '?'
443
+ end
444
+ # ===================================================================== #
445
+ # Get rid of '#' in the input, if it exists.
446
+ # ===================================================================== #
447
+ i = i[0..i.index('#')] if i.include? '#'
448
+ case i
449
+ when 'LAST' # Assign the last file.
450
+ i = find_and_return_last_file
451
+ end
452
+ # ===================================================================== #
453
+ # Next, act on the extension name.
454
+ # ===================================================================== #
455
+ _ = File.extname(i).delete('.').downcase
456
+ case _ # case tag
457
+ # ===================================================================== #
458
+ # === open .pdf files
459
+ # ===================================================================== #
460
+ when 'pdf'
461
+ open_in_pdf_viewer(i)
462
+ # ===================================================================== #
463
+ # === epub
464
+ # ===================================================================== #
465
+ when 'epub'
466
+ esystem 'okular '+i+' &'
467
+ # ===================================================================== #
468
+ # === torrent
469
+ # ===================================================================== #
470
+ when 'torrent'
471
+ open_this_torrent_file(i)
472
+ # ===================================================================== #
473
+ # === Extensions that can be opened in the editor
474
+ # ===================================================================== #
475
+ when *ARRAY_EXTENSIONS_THAT_CAN_BE_OPENED_IN_THE_EDITOR
476
+ open_in_browser_or_in_editor(i)
477
+ # ===================================================================== #
478
+ # === All registered images
479
+ # ===================================================================== #
480
+ when *ARRAY_IMAGES
481
+ open_in_gimp(i)
482
+ # ===================================================================== #
483
+ # === All libreoffice extensions
484
+ # ===================================================================== #
485
+ when *ARRAY_LIBREOFFICE_EXTENSIONS
486
+ open_in_office(i)
487
+ # ===================================================================== #
488
+ # === All videos
489
+ # ===================================================================== #
490
+ when *ARRAY_VIDEOS
491
+ play_multimedia_file(i)
492
+ else
493
+ if File.exist? i
494
+ opnn; e "Not registered extension `#{sfancy(_)}`."
495
+ opnn; e 'We will attempt to open it anyway though.'
496
+ open_in_browser_or_in_editor(i)
497
+ else # else we conclude that the file does not exist.
498
+ no_file_was_found_at(i)
499
+ end
500
+ end
501
+ end
502
+ end; alias consider_opening_the_given_input try_to_open_this_file_in_the_editor # === consider_opening_the_given_input
503
+ alias open_in_editor try_to_open_this_file_in_the_editor # === open_in_editor
504
+ alias open_these_files try_to_open_this_file_in_the_editor # === open_these_files
505
+
506
+ # ========================================================================= #
507
+ # === open_this_torrent_file
508
+ # ========================================================================= #
509
+ def open_this_torrent_file(i)
510
+ if i.include?("'") and !i.include?('"')
511
+ i = '"'+i+'"'
512
+ end
513
+ _ = "transmission #{i} &"
514
+ esystem(_)
515
+ end
516
+
517
+ # ========================================================================= #
518
+ # === try_to_find_files_based_on_hyphens_as_part_of_the_input
519
+ #
520
+ # This method will try to find files based on '::' input.'
521
+ # ========================================================================= #
522
+ def try_to_find_files_based_on_hyphens_as_part_of_the_input(i)
523
+ if i.include? '::'
524
+ # ===================================================================== #
525
+ # In this case, replace '::', after passing it through the
526
+ # snakecase() method.
527
+ # ===================================================================== #
528
+ i = snakecase_and_prepend_first_name_and_lib(i).tr('::','/')+'/'
529
+ end
530
+ unless i.start_with?('/home')
531
+ # ===================================================================== #
532
+ # We assume that only ruby files follow the '::' layout, for now. (Jun 2017)
533
+ # ===================================================================== #
534
+ i.prepend('$RUBY_SRC/')
535
+ end
536
+ if i.include? '$'
537
+ i = convert_global_env(i)
538
+ end
539
+ i.squeeze!('/')
540
+ unless i.end_with?('*')
541
+ i << '*'
542
+ end
543
+ these_files = Dir[i]
544
+ open_these_files(these_files)
545
+ end
546
+
547
+ # ========================================================================= #
548
+ # === snakecase_and_prepend_first_name_and_lib
549
+ #
550
+ # This combines snakecase() by also prepending the first-name
551
+ # and "lib/" prefix.
552
+ # ========================================================================= #
553
+ def snakecase_and_prepend_first_name_and_lib(i)
554
+ if i.include? '::'
555
+ splitted = i.split('::')
556
+ first_name = splitted.first.downcase
557
+ end
558
+ i = snakecase(i)
559
+ return first_name+'/lib/'+i
560
+ end
561
+
562
+ # ========================================================================= #
563
+ # === register_this_pdf_file
564
+ # ========================================================================= #
565
+ def register_this_pdf_file(what)
566
+ begin
567
+ require 'save_file'
568
+ require 'roebe/constants/file_constants.rb'
569
+ require 'roebe/constants/directory_constants.rb'
570
+ rescue LoadError; end
571
+ into = Roebe.file_these_pdf_files_were_opened
572
+ old_dataset = nil
573
+ if File.exist? into
574
+ # ===================================================================== #
575
+ # Sanitize this .yml file, in that case.
576
+ # ===================================================================== #
577
+ old_dataset = YAML.load_file(into).uniq
578
+ this_content = old_dataset.map {|entry| entry.prepend(' - ') }.join("\n")
579
+ SaveFile.write_what_into(this_content, into)
580
+ end
581
+ _ = Roebe.log_directory?
582
+ mkdir(_) unless File.directory? _
583
+ if old_dataset and old_dataset.map {|entry|
584
+ # =================================================================== #
585
+ # ensure_main_encoding is necessary to avoid illegal byte sequences.
586
+ # =================================================================== #
587
+ # ensure_main_encoding(entry).strip.delete('-').strip
588
+ # ^^^ Disabled as of September 2021. Let's see if we really need it still.
589
+ entry.strip.delete('-').strip
590
+ }.include? what
591
+ # ===================================================================== #
592
+ # In this case, we will remove the old entries, and then append.
593
+ # ===================================================================== #
594
+ old_dataset = YAML.load_file(into).uniq.reject {|entry|
595
+ # entry = ensure_main_encoding(entry) # Need to use a consistent encoding here.
596
+ # ^^^ Disabled as of September 2021.
597
+ entry.include? what
598
+ }
599
+ this_content = old_dataset.map {|entry| entry.prepend(' - ') }.join(N)
600
+ SaveFile.write_what_into(this_content, into)
601
+ end
602
+ what = "\n - #{what}"
603
+ touch(into) unless File.exist? into
604
+ SaveFile.append_what_into(what, into) # Silently store that.
605
+ end
606
+
607
+ # ========================================================================= #
608
+ # === open_in_browser_or_in_editor
609
+ #
610
+ # This method was required because sometimes we wish to open in the
611
+ # browser, and sometimes we wish to open in the editor.
612
+ # ========================================================================= #
613
+ def open_in_browser_or_in_editor(i)
614
+ if i.include? '//'
615
+ i = i.dup if i.frozen?
616
+ i.squeeze!('/')
617
+ end
618
+ # ======================================================================= #
619
+ # Next, we get the extension name.
620
+ # ======================================================================= #
621
+ _ = File.extname(File.basename(i)).delete '.'
622
+ case _
623
+ when 'html','php' # Open in both, in this case.
624
+ do_open_in_editor(i)
625
+ unless File.zero? i
626
+ ::Open.in_browser(i) unless _.include? 'php'
627
+ end
628
+ else
629
+ do_open_in_editor(i)
630
+ end
631
+ end
632
+
633
+ # ========================================================================= #
634
+ # === open_in_gimp
635
+ # ========================================================================= #
636
+ def open_in_gimp(i)
637
+ esystem "#{@image_editor} #{i}#{IN_BACKGROUND}"
638
+ end
639
+
640
+ # ========================================================================= #
641
+ # === open_in_office
642
+ #
643
+ # This method should be used to open files that do have a file extension
644
+ # such as .xls or .odt or .ppt. In other words, libreoffice-specifi files.
645
+ # ========================================================================= #
646
+ def open_in_office(i)
647
+ i = consider_padding(i)
648
+ if File.exist? LIBREOFFICE
649
+ result = "#{LIBREOFFICE} #{i}#{IN_BACKGROUND}"
650
+ else # Try any other general location based on $PATH anyway.
651
+ result = File.basename(LIBREOFFICE)+' '+i+IN_BACKGROUND
652
+ end
653
+ esystem result
654
+ end
655
+
656
+ # ========================================================================= #
657
+ # === try_to_require_studium_project
658
+ # ========================================================================= #
659
+ def try_to_require_studium_project
660
+ unless Object.const_defined? :Studium
661
+ begin # We pull in only the relevant files from the Studium project.
662
+ require 'studium/constants/exam_topics.rb'
663
+ require 'studium/toplevel_methods/find_exam_topic_and_exam_title.rb'
664
+ require 'studium/toplevel_methods/is_this_exam_topic_included.rb'
665
+ rescue LoadError
666
+ if is_on_roebe?
667
+ e 'The three files (studium/constants/exam_topics.rb and so '\
668
+ 'forth) from the studium-project could not be loaded.'
669
+ end
670
+ end
671
+ end
672
+ end
673
+
674
+ # ========================================================================= #
675
+ # === open_in_pdf_viewer (pdf tag)
676
+ #
677
+ # This method can be used to open a .pdf file. We will let the project
678
+ # PdfParadise handle this situation.
679
+ #
680
+ # Since as of 19.09.2018 we will keep a log of the opened .pdf files
681
+ # if we are on roebe. This can then be used to determine which were
682
+ # the last .pdf files that were read.
683
+ # ========================================================================= #
684
+ def open_in_pdf_viewer(i)
685
+ require 'pdf_paradise' unless Object.const_defined? :PdfParadise
686
+ if File.exist? i
687
+ require 'pdf_paradise/main_pdf/main_pdf.rb'
688
+ require 'pdf_paradise/set_main_book.rb'
689
+ # ===================================================================== #
690
+ # Only enable the following on my home-system, and only in the
691
+ # books-subdirectory, and university-subdirectory.
692
+ # ===================================================================== #
693
+ if is_on_roebe?
694
+ case return_pwd
695
+ # =================================================================== #
696
+ # The following checks were explained above. We will also run
697
+ # rcfiles, and send pidof.
698
+ # =================================================================== #
699
+ when /^#{Regexp.quote('/home/x/books/')}/,
700
+ /^#{Regexp.quote('/home/x/studium/')}/
701
+ PdfParadise::SetMainBook.new(i)
702
+ run_rcfiles_then_run_ata_via_qdbus
703
+ end
704
+ end
705
+ PdfParadise.main_pdf(i) # , '--fullscreen'
706
+ register_this_pdf_file(i)
707
+ else
708
+ this_file_was_not_found(i)
709
+ end
710
+ end
711
+
712
+ # ========================================================================= #
713
+ # === run_everything
714
+ # ========================================================================= #
715
+ def run_everything
716
+ input_files = input_files?
717
+ # ======================================================================= #
718
+ # Iterate over all input files that were passed to us.
719
+ # ======================================================================= #
720
+ input_files.each {|this_file|
721
+ if this_file.include?(':') and !File.exist?(this_file) and
722
+ File.exist?(this_file.split(':').first.to_s)
723
+ this_file = this_file.split(':').first.to_s
724
+ end
725
+ if this_file.start_with?('http:') and !File.exist?(this_file)
726
+ this_file.delete_prefix!('http:')
727
+ end
728
+ # ===================================================================== #
729
+ # === Query whether the file exists or whether it does not
730
+ # ===================================================================== #
731
+ if File.exist? this_file # This is the simplest way - the file exists here.
732
+ try_to_open_this_file_in_the_editor(this_file)
733
+ else # Ok, the file was not instantly found. Run more checks here.
734
+ unless Object.const_defined? :BeautifulUrl
735
+ begin
736
+ require 'roebe/requires/failsafe_require_of_beautiful_url.rb'
737
+ rescue LoadError; end
738
+ end
739
+ possible_matches = Dir[('*'+this_file+'*').squeeze('*')] # <- This may be an empty Array as well.
740
+ # =================================================================== #
741
+ # Since as of Jan 2018, if the variable possible_matches is NOT
742
+ # empty, we will sort it in a way so that the shorter file names
743
+ # are on top of the array.
744
+ # =================================================================== #
745
+ unless possible_matches.empty?
746
+ possible_matches = possible_matches.sort_by {|entry| entry.size }
747
+ end
748
+ # =================================================================== #
749
+ # === Check whether the input is an exam-question next
750
+ #
751
+ # This check should come before we delegate towards FindExpandedAlias.
752
+ # =================================================================== #
753
+ if Object.const_defined?(:Studium) and
754
+ ::Studium.respond_to?(:include_this_exam_topic?) and
755
+ ::Studium.include_this_exam_topic?(this_file) # open ab2
756
+ _ = ::Studium.find_corresponding_exam_topic(this_file, :be_quiet) # This can be false.
757
+ if _
758
+ _ = ::Studium::DIRECTORY_EXAM_TOPICS+File.basename(_)
759
+ open_in_editor(_)
760
+ end
761
+ # =================================================================== #
762
+ # === Query whether BeautifulUrl includes this file.
763
+ # =================================================================== #
764
+ elsif BeautifulUrl.does_include?(this_file) # open linux
765
+ _ = beautiful_url(this_file)
766
+ consider_opening_the_given_input(_) # Then try again.
767
+ # =================================================================== #
768
+ # Ok, the following else-clauses handle the situation
769
+ # that the given input could not be found.
770
+ # =================================================================== #
771
+ elsif this_file.include? 'project_'
772
+ do_open_in_editor(this_file)
773
+ # =================================================================== #
774
+ # === Check for aliases next
775
+ #
776
+ # Next, try to find an expanded alias here.
777
+ # =================================================================== #
778
+ elsif Roebe.const_defined?(:FindExpandedAlias) and
779
+ (::Roebe::FindExpandedAlias[this_file] != this_file)
780
+ do_open_in_editor(this_file)
781
+ # =================================================================== #
782
+ # At the least one entry has been found.
783
+ # =================================================================== #
784
+ elsif ((_ = possible_matches).size > 0) # If we can find at least one entry.
785
+ consider_opening_the_given_input(_.first) # Recursive call.
786
+ # =================================================================== #
787
+ # Same as above, but both prepend and append a '*' character.
788
+ # =================================================================== #
789
+ elsif ((_ = possible_matches).size > 0) # If we can find at least one entry.
790
+ consider_opening_the_given_input(_.first)
791
+ # =================================================================== #
792
+ # Next, try to pass it through class Roebe::FindExpandedAlias.
793
+ # =================================================================== #
794
+ elsif File.exist?(_ = Roebe.find_expanded_alias(this_file).to_s)
795
+ open_in_editor(_)
796
+ # =================================================================== #
797
+ # Next, try to handle input containing two ':' tokens.
798
+ # =================================================================== #
799
+ elsif this_file.include?('::')
800
+ try_to_find_files_based_on_hyphens_as_part_of_the_input(this_file)
801
+ else
802
+ opnn; e 'No match could be found for `'+sfancy(this_file)+'`.'
803
+ end
804
+ end
805
+ }
806
+ end
807
+
808
+ # ========================================================================= #
809
+ # === do_open_in_editor
810
+ #
811
+ # This method will try to open the file via the specified (main)
812
+ # editor. Since as of August 2019, this will also be logged into
813
+ # a local file, if we are on a roebe-system.
814
+ # ========================================================================= #
815
+ def do_open_in_editor(
816
+ i, use_this_editor = @editor
817
+ )
818
+ if i.include? '//'
819
+ i.squeeze!('/')
820
+ end
821
+ if Object.const_defined? :Roebe
822
+ expanded_alias = Roebe::FindExpandedAlias.new(i) { :be_quiet }
823
+ if expanded_alias.is_included?
824
+ unless File.exist? i
825
+ points_at = expanded_alias.points_at?
826
+ opnn; e 'Making use of the aliased-pointer at'
827
+ opnn; e '`'+steelblue(points_at)+'`.'
828
+ i = points_at
829
+ end
830
+ end
831
+ end
832
+ i = rds(i) # Get rid of // again, just in case.
833
+ # ======================================================================= #
834
+ # Make an exception for .cpp files:
835
+ # ======================================================================= #
836
+ if (File.extname(i).delete('.') == 'cpp')
837
+ if is_on_roebe? and ENV.has_key?('CPP_EDITOR')
838
+ use_this_editor = ENV['CPP_EDITOR'].to_s.dup
839
+ else
840
+ use_this_editor = 'geany' # We use geany for cpp files.
841
+ end
842
+ end
843
+ if i.include?("'") and !i.include?('"')
844
+ i = '"'+i+'"'
845
+ end
846
+ if i.include? '('
847
+ i = '"'+i+'"'
848
+ end
849
+ _ = "#{use_this_editor} #{i}"
850
+ esystem(_)
851
+ if File.file?(i) and Object.const_defined?(:Roebe) and
852
+ File.directory?(Roebe.log_dir?) and
853
+ is_on_roebe?
854
+ into = Roebe.log_dir?+'opened_files.yml'
855
+ if File.exist? into
856
+ array = YAML.load_file(into)
857
+ else
858
+ array = [into].flatten.compact
859
+ end
860
+ what = YAML.dump(array)
861
+ SaveFile.write_what_into(what, into)
862
+ end
863
+ end
864
+
865
+ # ========================================================================= #
866
+ # === run_rcfiles_then_run_ata_via_qdbus
867
+ # ========================================================================= #
868
+ def run_rcfiles_then_run_ata_via_qdbus
869
+ require 'rcfiles'
870
+ Rcfiles.run
871
+ esystem 'qdbus org.kde.konsole-`pidof -s konsole` /Sessions/2 runCommand "ata"'
872
+ esystem 'qdbus org.kde.konsole-`pidof -s konsole` /Sessions/3 runCommand "ata"'
873
+ esystem 'qdbus org.kde.konsole-`pidof -s konsole` /Sessions/4 runCommand "ata"'
874
+ end
875
+
876
+ # ========================================================================= #
877
+ # === run (run tag)
878
+ # ========================================================================= #
879
+ def run
880
+ check_whether_the_user_has_provided_any_input
881
+ try_to_require_studium_project
882
+ run_everything
883
+ end
884
+
885
+ # ========================================================================= #
886
+ # === Open::Open[]
887
+ # ========================================================================= #
888
+ def self.[](i = '', &block)
889
+ new(i, &block)
890
+ end
891
+
892
+ end
893
+
894
+ # =========================================================================== #
895
+ # === Open.open
896
+ # =========================================================================== #
897
+ def self.open(i = ARGV, &block)
898
+ ::Open::Open.new(i, &block)
899
+ end
900
+
901
+ end
902
+
903
+ if __FILE__ == $PROGRAM_NAME
904
+ Open::Open.new(ARGV)
905
+ end # open